forked from lumc-pgx/pharmvar-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.py
287 lines (227 loc) · 12.9 KB
/
check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import argparse
from itertools import combinations
import requests
import sys
from algebra import Relation, are_equivalent, compare
from algebra.relations.sequence_based import are_equivalent as are_equivalent_sequence
from algebra.utils import fasta_sequence, vcf_variant
from algebra.variants import Variant, parse_hgvs, patch, reverse_complement
from ratelimiter import RateLimiter
from api import get_alleles, get_variants, get_version
from config import get_gene
NCBI_URI = "https://api.ncbi.nlm.nih.gov/variation/v0"
MUTALYZER_URI = "https://mutalyzer.nl/api"
def mutalyzer_hgvs(query):
response = requests.get(f"{MUTALYZER_URI}/normalize/{query}")
if response.status_code == 200:
return response.json()["normalized_description"]
return None
def check_alleles_vs_variants(alleles, variants, ref_seq_id):
print(f"Checking variants with alleles for {ref_seq_id} ...")
for allele in alleles:
for variant in allele["variants"]:
if variant not in variants:
print(f"Variant {variant} from allele {allele['name']} not in variants.")
for variant in variants:
found = False
for allele in alleles:
if variant in allele["variants"]:
found = True
if not found:
print(f"Variant {variant} not in alleles.")
def ncbi_hgvs(query):
response = requests.get(f"{NCBI_URI}/hgvs/{query}/contextuals").json()
position = int(response["data"]["spdis"][0]["position"])
deleted = response["data"]["spdis"][0]["deleted_sequence"]
inserted = response["data"]["spdis"][0]["inserted_sequence"]
return Variant(position, position + len(deleted), inserted)
# NCBI Variation Services limited to 1 request/second.
@RateLimiter(max_calls=1, period=1)
def check_variants_with_ncbi(reference, ref_seq_id, variants):
print(f"Checking variants with NCBI for {ref_seq_id} ...")
for variant in variants:
pharm_var = parse_hgvs(variant["hgvs"], reference)
ncbi_var = [ncbi_hgvs(f"{ref_seq_id}:g.{variant['hgvs']}")]
if pharm_var != ncbi_var and not are_equivalent(reference, pharm_var, ncbi_var):
print(f"Non equivalent variants for {variant['hgvs']}: {pharm_var} vs {ncbi_var}")
def check_variants_with_mutalyzer(reference, ref_seq_id, variants):
print(f"Checking variants with Mutalyzer for {ref_seq_id} ...")
for variant in variants:
pharm_var = parse_hgvs(variant["hgvs"], reference)
mut_hgvs = mutalyzer_hgvs(f"{ref_seq_id}:g.{variant['hgvs']}")
mut_var = parse_hgvs(mut_hgvs, reference)
if pharm_var != mut_var and not are_equivalent(reference, pharm_var, mut_var):
print(f"Non equivalent variants for {variant['hgvs']}: {pharm_var} vs {mut_var}")
def check_hgvs_allele_vs_variant_list(reference, ref_seq_id, alleles):
print(f"Checking consistency between 'hgvs' and 'variants' entries in alleles for {ref_seq_id} ...")
for allele in alleles:
try:
hgvs_var = parse_hgvs(allele["hgvs"], reference)
except ValueError as error:
print(f"Parsing of {allele['hgvs']} ({allele['name']}) failed ({str(error)})")
continue
variants = [parse_hgvs(variant["hgvs"], reference)[0] for variant in allele["variants"]]
if hgvs_var != sorted(variants):
print(f"HGVS variant {allele['hgvs']} mismatches with variant list ({allele['name']})")
def check_allele_variants(reference, ref_seq_id, alleles):
print(f"Checking consistency of 'variants' entries of alleles for {ref_seq_id} ...")
for allele in alleles:
variants = set([parse_hgvs(variant["hgvs"], reference)[0] for variant in allele["variants"]])
try:
list(sorted(variants))
except ValueError:
print(f"{allele['name']} ({ref_seq_id}) unorderable")
for lhs, rhs in combinations(variants, 2):
relation = compare(reference, [lhs], [rhs])
if relation != Relation.DISJOINT:
print(f" {ref_seq_id}:g.{lhs.to_hgvs(reference)} and {ref_seq_id}:g.{rhs.to_hgvs(reference)} {relation.value} in {allele['name']}")
def check_allele_duplicates(reference, ref_seq_id, alleles):
print(f"Checking for duplicates in 'variants' entries of alleles for {ref_seq_id} ...")
for allele in alleles:
variants = [parse_hgvs(variant["hgvs"], reference)[0] for variant in allele["variants"]]
for duplicate in set([variant for variant in variants if variants.count(variant) > 1]):
print(f"Duplicate {ref_seq_id}:g.{duplicate.to_hgvs(reference)} in {allele['name']}")
def multi_fasta(lines):
name = None
sequence = None
for line in lines:
if line.startswith(">"):
if name is not None:
yield name, sequence
name, *_ = line[1:].split()
sequence = ""
else:
sequence += line.strip()
if name is not None:
yield name, sequence
def check_hgvs_allele_vs_fasta(reference, ref_seq_id, alleles, gene, version):
print(f"Checking consistency between allele hgvs and fasta for {ref_seq_id} ...")
fasta_alleles = {}
with open(f"data/pharmvar-{version}/{gene}/{gene}.haplotypes.fasta", encoding="utf-8") as file:
for name, sequence in multi_fasta(file):
fasta_alleles[name] = sequence
for allele in alleles:
try:
hgvs_var = parse_hgvs(allele["hgvs"], reference)
except ValueError as error:
print(f"Parsing of {allele['hgvs']} ({allele['name']}) failed ({str(error)})")
continue
if not are_equivalent_sequence(reference, patch(reference, hgvs_var), fasta_alleles[allele["name"]]):
print(f"Non equivalent variants for {allele['hgvs']}: {hgvs_var} vs fasta ({allele['name']})")
def check_hgvs_allele_vs_vcf_ng(gene, reference, ref_seq_id, alleles, version):
print(f"Checking consistency between allele hgvs and NG vcf for {ref_seq_id} ...")
for allele in alleles:
try:
hgvs_var = parse_hgvs(allele["hgvs"], reference)
except ValueError as error:
print(f"Parsing of {allele['hgvs']} ({allele['name']}) failed ({str(error)})")
continue
vcf_variants = []
with open(f"data/pharmvar-{version}/{gene}/RefSeqGene/{allele['name'].replace('*', '_')}.vcf", encoding="utf-8") as file:
for line in file:
if not line.startswith("#"):
vcf_variants.append(vcf_variant(line))
if hgvs_var != vcf_variants and not are_equivalent(reference, hgvs_var, vcf_variants):
print(f"Non equivalent variants for {allele['hgvs']}: {hgvs_var} vs {vcf_variants} ({allele['name']})")
def check_hgvs_allele_vs_vcf_nc(gene, reference, ref_seq_id, alleles, version):
print(f"Checking consistency between NC variants and NC vcf for {ref_seq_id} ...")
for allele in alleles:
try:
hgvs_var = [parse_hgvs(variant["hgvs"], reference)[0] for variant in allele["variants"]]
except ValueError as error:
print(f"Parsing of {allele['name']} failed ({str(error)})")
continue
vcf_variants = []
with open(f"data/pharmvar-{version}/{gene}/GRCh38/{allele['name'].replace('*', '_')}.vcf", encoding="utf-8") as file:
for line in file:
if not line.startswith("#"):
vcf_variants.append(vcf_variant(line))
try:
if hgvs_var != vcf_variants and not are_equivalent(reference, hgvs_var, vcf_variants):
print(f"Non equivalent variants for {allele['name']}: {hgvs_var} vs {vcf_variants}")
except ValueError:
# silently skip parsing/interpretation related problems checked elsewhere
pass
def check_nc_vs_ng(nc_reference, ng_reference, nc_alleles, ng_alleles, mapping):
print(f"Checking consistency between NC and NG ...")
nc_mapped = nc_reference[mapping["start"]:mapping["end"]]
for nc_allele in nc_alleles:
for ng_allele in ng_alleles:
if nc_allele["name"] == ng_allele["name"]:
try:
nc_variants = [parse_hgvs(variant["hgvs"], nc_reference)[0] for variant in nc_allele["variants"]]
ng_variants = [parse_hgvs(variant["hgvs"], ng_reference)[0] for variant in ng_allele["variants"]]
nc_observed = patch(nc_mapped, [Variant(variant.start - mapping["start"], variant.end - mapping["start"], variant.sequence) for variant in nc_variants])
if mapping["reverse"]:
nc_observed = reverse_complement(nc_observed)
ng_observed = patch(ng_reference, ng_variants)
except ValueError:
# silently skip parsing/interpretation related problems checked elsewhere
break
if nc_observed != ng_observed:
print(f"NC is not consistent with NG for {nc_allele['name']}")
print(nc_variants)
print(ng_variants)
break
def main():
parser = argparse.ArgumentParser(description="PharmVar data checker")
parser.add_argument("--all", help="Perform all checks", action="store_true")
parser.add_argument("--alleles-vs-variants", help="Check allele vs. variant endpoints", action="store_true")
parser.add_argument("--ncbi", help="Check variants against NCBI", action="store_true")
parser.add_argument("--mutalyzer", help="Check variants against Mutalyzer", action="store_true")
parser.add_argument("--variants", help="Check variants of alleles", action="store_true")
parser.add_argument("--duplicates", help="Check for duplicate variants in alleles", action="store_true")
parser.add_argument("--hgvs", help="Check allele hgvs entry vs. variant list", action="store_true")
parser.add_argument("--fasta", help="Check hgvs entry of allele vs. fasta files", action="store_true")
parser.add_argument("--vcf", help="Check hgvs entry of allele vs. vcf files", action="store_true")
parser.add_argument("--nc-vs-ng", help="Check NC variants vs. NG variants", action="store_true")
parser.add_argument("--gene", help="Gene to operate on", required=True)
parser.add_argument("--disable-cache", help="Disable read and write from cache", action="store_true")
parser.add_argument("--version", help="Specify PharmVar version", default=get_version())
args = parser.parse_args()
try:
gene_info = get_gene(args.gene)
except KeyError:
print(f"ERROR: Gene {args.gene} not in configuration!")
sys.exit(-1)
nc_ref_seq_id = gene_info["nc_ref_seq_id"]
ng_ref_seq_id = gene_info["ng_ref_seq_id"]
print("Loading reference data ...")
with open(f"data/{nc_ref_seq_id}.fasta", encoding="utf-8") as file:
nc_reference = fasta_sequence(file.readlines())
with open(f"data/{ng_ref_seq_id}.fasta", encoding="utf-8") as file:
ng_reference = fasta_sequence(file.readlines())
cache = not args.disable_cache
print("Retrieving variant data ...")
nc_variants = get_variants(args.gene, nc_ref_seq_id, args.version, cache)
ng_variants = get_variants(args.gene, ng_ref_seq_id, args.version, cache)
print("Retrieving allele data ...")
nc_alleles = get_alleles(args.gene, nc_ref_seq_id, args.version, cache)
ng_alleles = get_alleles(args.gene, ng_ref_seq_id, args.version, cache)
if args.alleles_vs_variants or args.all:
check_alleles_vs_variants(nc_alleles, nc_variants, nc_ref_seq_id)
check_alleles_vs_variants(ng_alleles, ng_variants, ng_ref_seq_id)
if args.ncbi or args.all:
check_variants_with_ncbi(nc_reference, nc_ref_seq_id, nc_variants)
check_variants_with_ncbi(ng_reference, ng_ref_seq_id, ng_variants)
if args.mutalyzer or args.all:
check_variants_with_mutalyzer(nc_reference, nc_ref_seq_id, nc_variants)
check_variants_with_mutalyzer(ng_reference, ng_ref_seq_id, ng_variants)
if args.variants or args.all:
check_allele_variants(ng_reference, ng_ref_seq_id, ng_alleles)
check_allele_variants(nc_reference, nc_ref_seq_id, nc_alleles)
if args.duplicates or args.all:
check_allele_duplicates(nc_reference, nc_ref_seq_id, nc_alleles)
check_allele_duplicates(ng_reference, ng_ref_seq_id, ng_alleles)
# Only for NG, as Allele["hgvs"] is always expressed as NG
if args.hgvs or args.all:
check_hgvs_allele_vs_variant_list(ng_reference, ng_ref_seq_id, ng_alleles)
if args.fasta or args.all:
check_hgvs_allele_vs_fasta(ng_reference, ng_ref_seq_id, ng_alleles, args.gene, args.version)
if args.vcf or args.all:
check_hgvs_allele_vs_vcf_nc(args.gene, nc_reference, nc_ref_seq_id, nc_alleles, args.version)
check_hgvs_allele_vs_vcf_ng(args.gene, ng_reference, ng_ref_seq_id, ng_alleles, args.version)
if args.nc_vs_ng or args.all:
check_nc_vs_ng(nc_reference, ng_reference, nc_alleles, ng_alleles, gene_info["nc_mapping"])
if __name__ == "__main__":
main()