Skip to content

Commit

Permalink
Update schema for storing DRM references in higher granularity; fix D…
Browse files Browse the repository at this point in the history
…RM bugs; tweak DRM rules
  • Loading branch information
philiptzou committed Apr 5, 2023
1 parent 01d9f29 commit 501a3d3
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 136 deletions.
10 changes: 5 additions & 5 deletions derived_tables/04_susc_results.sql
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,11 @@ UPDATE susc_results SET

UPDATE susc_results S SET
rx_type = CASE
WHEN S.potency_type = 'Kcat/Km' AND EXISTS (
SELECT 1 FROM assays A WHERE
S.assay_name = A.assay_name AND
A.virus_type = 'enzyme'
) THEN 'enzyme-kinetics'
WHEN EXISTS (
SELECT 1 FROM rx_antibodies rxab WHERE
S.ref_name = rxab.ref_name AND
Expand Down Expand Up @@ -676,11 +681,6 @@ UPDATE susc_results S SET
)
)
) THEN 'vacc-plasma'
WHEN S.potency_type = 'Kcat/Km' AND EXISTS (
SELECT 1 FROM assays A WHERE
S.assay_name = A.assay_name AND
A.virus_type = 'enzyme'
) THEN 'enzyme-kinetics'

END::rx_type_enum;

Expand Down
61 changes: 18 additions & 43 deletions derived_tables/06_mab_resistance_mutations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SELECT ab_name, priority
'AMU', 'ROM', 'ADI'
) OR ab_name IN ('C135', 'C144');

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
im.gene,
position,
Expand All @@ -29,6 +29,7 @@ SELECT
JOIN approved_mabs ab ON
rxab.ab_name = ab.ab_name
WHERE
s.rx_type = 'antibody' AND
im.gene = 'S' AND
ip.num_mutations = 1 AND
(SELECT COUNT(*)
Expand All @@ -40,9 +41,9 @@ SELECT
GROUP BY im.gene, position, amino_acid, col_name, priority
ORDER BY im.gene, position, amino_acid, priority;

INSERT INTO candidate_resistance_mutation_articles
SELECT
im.gene, position, amino_acid, s.ref_name
INTO _drm_articles
im.gene, position, amino_acid, s.ref_name, 'FOLD'
FROM isolate_mutations im
JOIN susc_results s ON
im.iso_name = s.iso_name
Expand All @@ -68,7 +69,7 @@ SELECT
) = 1
GROUP BY im.gene, position, amino_acid, s.ref_name;

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
gene, position, amino_acid,
'DMS:' || ab.ab_name AS col_name,
Expand All @@ -91,9 +92,9 @@ SELECT
GROUP BY gene, position, amino_acid, col_name, priority
ORDER BY gene, position, amino_acid, priority;

INSERT INTO _drm_articles
INSERT INTO candidate_resistance_mutation_articles
SELECT
gene, position, amino_acid, dms.ref_name
gene, position, amino_acid, dms.ref_name, 'DMS'
FROM dms_escape_results dms
JOIN rx_antibodies rxab ON
dms.ref_name=rxab.ref_name AND
Expand All @@ -112,7 +113,7 @@ SELECT
GROUP BY gene, position, amino_acid, dms.ref_name
ON CONFLICT DO NOTHING;

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
gene, position, amino_acid,
'INVIVO' AS col_name,
Expand All @@ -124,17 +125,17 @@ SELECT
GROUP BY gene, position, amino_acid
ORDER BY gene, position, amino_acid;

INSERT INTO _drm_articles
INSERT INTO candidate_resistance_mutation_articles
SELECT
gene, position, amino_acid, ref_name
gene, position, amino_acid, ref_name, 'INVIVO'
FROM invivo_selection_results sel
WHERE
gene = 'S' AND
amino_acid != 'stop'
GROUP BY gene, position, amino_acid, ref_name
ON CONFLICT DO NOTHING;

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
gene, position, amino_acid,
'INVITRO' AS col_name,
Expand All @@ -144,15 +145,15 @@ SELECT
GROUP BY gene, position, amino_acid
ORDER BY gene, position, amino_acid;

INSERT INTO _drm_articles
INSERT INTO candidate_resistance_mutation_articles
SELECT
gene, position, amino_acid, ref_name
gene, position, amino_acid, ref_name, 'INVITRO'
FROM invitro_selection_results
WHERE gene = 'S' AND amino_acid != 'stop'
GROUP BY gene, position, amino_acid, ref_name
ON CONFLICT DO NOTHING;

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
gene, position, amino_acid,
'PREVALENCE' AS col_name,
Expand All @@ -161,17 +162,17 @@ SELECT
WHERE gene = 'S' AND ref_name = 'Martin21'
ORDER BY gene, position, amino_acid;

INSERT INTO _drm_articles
INSERT INTO candidate_resistance_mutation_articles
SELECT
gene, position, amino_acid, ref_name
gene, position, amino_acid, ref_name, 'PREVALENCE'
FROM amino_acid_prevalence
WHERE gene = 'S' AND ref_name = 'Martin21'
ON CONFLICT DO NOTHING;

INSERT INTO resistance_mutations
SELECT
gene, position, amino_acid
FROM resistance_mutation_attributes rma
FROM candidate_resistance_mutation_attributes rma
WHERE
gene = 'S' AND (
(col_name LIKE 'FOLD:%' AND
Expand All @@ -188,7 +189,7 @@ FROM resistance_mutation_attributes rma
)
GROUP BY gene, position, amino_acid;

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
vc.gene, vc.position, vc.amino_acid,
'VARCONS' AS col_name,
Expand All @@ -209,30 +210,4 @@ SELECT
GROUP BY vc.gene, vc.position, vc.amino_acid, col_name, col_value
ORDER BY vc.gene, vc.position, vc.amino_acid, col_name, col_value;

DELETE FROM resistance_mutation_attributes rma
WHERE
gene = 'S' AND
NOT EXISTS (
SELECT 1 FROM resistance_mutations rm
WHERE
rm.gene = rma.gene AND
rm.position = rma.position AND
rm.amino_acid = rma.amino_acid
);

DELETE FROM _drm_articles a
WHERE
NOT EXISTS (
SELECT 1 FROM resistance_mutations rm
WHERE
rm.gene = a.gene AND
rm.position = a.position AND
rm.amino_acid = a.amino_acid
);

INSERT INTO resistance_mutation_articles
SELECT gene, ref_name FROM _drm_articles
GROUP BY gene, ref_name;

DROP TABLE approved_mabs;
DROP TABLE _drm_articles;
69 changes: 23 additions & 46 deletions derived_tables/07_3clpro_resistance_mutations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SELECT drug_name, abbreviation_name
FROM compounds
WHERE abbreviation_name IN ('NTV', 'ENS');

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
im.gene,
position,
Expand All @@ -25,6 +25,7 @@ SELECT
JOIN approved_drugs drug ON
rxdrug.drug_name = drug.drug_name
WHERE
s.rx_type = 'compound' AND
im.gene = '_3CLpro' AND
ip.num_mutations = 1 AND
(SELECT COUNT(*)
Expand All @@ -36,9 +37,9 @@ SELECT
GROUP BY im.gene, position, amino_acid, col_name
ORDER BY im.gene, position, amino_acid;

INSERT INTO candidate_resistance_mutation_articles
SELECT
im.gene, position, amino_acid, s.ref_name
INTO _drm_articles
im.gene, position, amino_acid, s.ref_name, 'FOLD'
FROM isolate_mutations im
JOIN susc_results s ON
im.iso_name = s.iso_name
Expand All @@ -54,6 +55,7 @@ SELECT
JOIN approved_drugs drug ON
rxdrug.drug_name = drug.drug_name
WHERE
s.rx_type = 'compound' AND
im.gene = '_3CLpro' AND
ip.num_mutations = 1 AND
(SELECT COUNT(*)
Expand All @@ -64,7 +66,7 @@ SELECT
) = 1
GROUP BY im.gene, position, amino_acid, s.ref_name;

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
im.gene,
position,
Expand All @@ -87,12 +89,13 @@ SELECT
GROUP BY im.gene, position, amino_acid, col_name
ORDER BY im.gene, position, amino_acid;

INSERT INTO _drm_articles
INSERT INTO candidate_resistance_mutation_articles
SELECT
im.gene,
position,
amino_acid,
s.ref_name
s.ref_name,
'FITNESS'
FROM isolate_mutations im
JOIN susc_results s ON
im.iso_name = s.iso_name
Expand All @@ -109,7 +112,7 @@ SELECT
GROUP BY im.gene, position, amino_acid, s.ref_name
ON CONFLICT DO NOTHING;

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
gene, position, amino_acid,
'INVIVO' AS col_name,
Expand All @@ -133,9 +136,9 @@ SELECT
GROUP BY gene, position, amino_acid
ORDER BY gene, position, amino_acid;

INSERT INTO _drm_articles
INSERT INTO candidate_resistance_mutation_articles
SELECT
gene, position, amino_acid, ref_name
gene, position, amino_acid, ref_name, 'INVIVO'
FROM invivo_selection_results sel
WHERE
gene = '_3CLpro' AND
Expand All @@ -155,7 +158,7 @@ SELECT
GROUP BY gene, position, amino_acid, ref_name
ON CONFLICT DO NOTHING;

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
gene, position, amino_acid,
'INVITRO' AS col_name,
Expand All @@ -165,15 +168,15 @@ SELECT
GROUP BY gene, position, amino_acid
ORDER BY gene, position, amino_acid;

INSERT INTO _drm_articles
INSERT INTO candidate_resistance_mutation_articles
SELECT
gene, position, amino_acid, ref_name
gene, position, amino_acid, ref_name, 'INVITRO'
FROM invitro_selection_results
WHERE gene = '_3CLpro' AND amino_acid != 'stop'
GROUP BY gene, position, amino_acid, ref_name
ON CONFLICT DO NOTHING;

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
gene, position, amino_acid,
'PREVALENCE' AS col_name,
Expand All @@ -182,17 +185,17 @@ SELECT
WHERE gene = '_3CLpro' AND ref_name = 'Martin21'
ORDER BY gene, position, amino_acid;

INSERT INTO _drm_articles
INSERT INTO candidate_resistance_mutation_articles
SELECT
gene, position, amino_acid, ref_name
gene, position, amino_acid, ref_name, 'PREVALENCE'
FROM amino_acid_prevalence
WHERE gene = '_3CLpro' AND ref_name = 'Martin21'
ON CONFLICT DO NOTHING;

INSERT INTO resistance_mutations
SELECT
gene, position, amino_acid
FROM resistance_mutation_attributes rma
FROM candidate_resistance_mutation_attributes rma
WHERE
gene = '_3CLpro' AND (
(col_name LIKE 'FOLD:%' AND
Expand All @@ -212,7 +215,7 @@ FROM resistance_mutation_attributes rma
)
GROUP BY gene, position, amino_acid;

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
p.gene, p.position, rm.amino_acid,
'POCKET:' || p.drug_name AS col_name,
Expand All @@ -227,9 +230,9 @@ SELECT
GROUP BY p.gene, p.position, rm.amino_acid, col_name, col_value
ORDER BY p.gene, p.position, rm.amino_acid, col_name, col_value;

INSERT INTO _drm_articles
INSERT INTO candidate_resistance_mutation_articles
SELECT
p.gene, p.position, rm.amino_acid, p.ref_name
p.gene, p.position, rm.amino_acid, p.ref_name, 'POCKET'
FROM compound_binding_pockets p
JOIN resistance_mutations rm ON
p.gene = rm.gene AND
Expand All @@ -240,7 +243,7 @@ SELECT
GROUP BY p.gene, p.position, rm.amino_acid, p.ref_name
ON CONFLICT DO NOTHING;

INSERT INTO resistance_mutation_attributes
INSERT INTO candidate_resistance_mutation_attributes
SELECT
vc.gene, vc.position, vc.amino_acid,
'VARCONS' AS col_name,
Expand All @@ -261,30 +264,4 @@ SELECT
GROUP BY vc.gene, vc.position, vc.amino_acid, col_name, col_value
ORDER BY vc.gene, vc.position, vc.amino_acid, col_name, col_value;

DELETE FROM resistance_mutation_attributes rma
WHERE
gene = '_3CLpro' AND
NOT EXISTS (
SELECT 1 FROM resistance_mutations rm
WHERE
rm.gene = rma.gene AND
rm.position = rma.position AND
rm.amino_acid = rma.amino_acid
);

DELETE FROM _drm_articles a
WHERE
NOT EXISTS (
SELECT 1 FROM resistance_mutations rm
WHERE
rm.gene = a.gene AND
rm.position = a.position AND
rm.amino_acid = a.amino_acid
);

INSERT INTO resistance_mutation_articles
SELECT gene, ref_name FROM _drm_articles
GROUP BY gene, ref_name;

DROP TABLE approved_drugs;
DROP TABLE _drm_articles;
Loading

0 comments on commit 501a3d3

Please sign in to comment.