-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgnomad_api_cli.py
783 lines (727 loc) · 24.9 KB
/
gnomad_api_cli.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# gnomAD Python API by @furkanmtorun
# E-Mail: [email protected]
# GitHub: https://github.com/furkanmtorun
# Google Scholar: https://scholar.google.com/citations?user=d5ZyOZ4AAAAJ
# Personal Website : https://furkanmtorun.github.io/
# Import required libraries and packages
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
from pandas.io.json import json_normalize as json_normalize
from tqdm import tqdm
import pandas as pd
import requests
import argparse
import json
import os
import shutil
import sys
# Create folders for outputs in the current directory
if not os.path.exists('outputs/'):
os.mkdir('outputs/')
# Argument Parsing
def arg_parser():
global filter_by
global search_by
global dataset
global sv_dataset
parser = argparse.ArgumentParser()
parser.add_argument("-filter_by", type=str, required=True, default="gene_name", help="Get your variants according to: `gene_name`, `gene_id, `transcript_id` or `rs_id`.")
parser.add_argument("-search_by", type=str, required=True, default="TP53", help="Type your input for searching or type the file name (e.g: myGenes.txt) containing your inputs")
parser.add_argument("-dataset", type=str, required=True, default="gnomad_r2_1", help="Select your dataset: exac, gnomad_r2_1, gnomad_r3, gnomad_r2_1_controls, gnomad_r2_1_non_neuro, gnomad_r2_1_non_cancer, gnomad_r2_1_non_topmed")
parser.add_argument("-reference_genome", type=str, required=False, default="GRCh37", help="Select a proper reference genome build : `GRCh37` or `GRCh38`")
parser.add_argument("-sv_dataset", type=str, required=False, default="gnomad_sv_r2_1", help="Select your structural variants dataset : `gnomad_sv_r2_1`, `gnomad_sv_r2_1_controls` or `gnomad_sv_r2_1_non_neuro`")
args = parser.parse_args()
# Control the given arguments
if args.dataset not in ["exac", "gnomad_r2_1", "gnomad_r3", "gnomad_r2_1_controls", "gnomad_r2_1_non_neuro", "gnomad_r2_1_non_cancer", "gnomad_r2_1_non_topmed"]:
sys.exit("! Select a proper gnomAD data set:\n\texac, gnomad_r2_1, gnomad_r3, gnomad_r2_1_controls, gnomad_r2_1_non_neuro, gnomad_r2_1_non_cancer, gnomad_r2_1_non_topmed")
if args.sv_dataset not in ["gnomad_sv_r2_1", "gnomad_sv_r2_1_controls", "gnomad_sv_r2_1_non_neuro"]:
sys.exit("! Select a proper gnomAD data set:\n\t`gnomad_sv_r2_1`, `gnomad_sv_r2_1_controls` or `gnomad_sv_r2_1_non_neuro`")
if args.filter_by not in ["gene_name", "gene_id", "transcript_id", "rs_id"]:
sys.exit("! Select a proper filter type :\n\t `gene_name`, `gene_id, `transcript_id` or `rs_id`")
if args.reference_genome not in ["GRCh37", "GRCh38"]:
sys.exit("! Select a proper reference genome build :\n\t `GRCh37` or `GRCh38`")
if (args.dataset == "gnomad_r3") and (args.reference_genome == "GRCh37"):
sys.exit("! You need to select `GRCh38` reference genome build for getting data from `gnomad_r3`.")
# Define variables
filter_by = args.filter_by
search_by = args.search_by
dataset = args.dataset
sv_dataset = args.sv_dataset
reference_genome = args.reference_genome
return filter_by, search_by, dataset, sv_dataset, reference_genome
# gnomAD API
end_point = "https://gnomad.broadinstitute.org/api/"
# Main Function
def get_variants_by(filter_by, search_term, dataset, reference_genome, sv_dataset, timeout=None):
query_for_transcripts = """
{
transcript(transcript_id: "%s", reference_genome: %s) {
transcript_id,
transcript_version,
gene {
gene_id,
symbol,
start,
stop,
strand,
chrom,
hgnc_id,
gene_name,
full_gene_name,
omim_id
}
variants(dataset: %s) {
pos
rsid
ref
alt
consequence
genome {
genome_af:af
genome_ac:ac
genome_an:an
genome_ac_hemi:ac_hemi
genome_ac_hom:ac_hom
}
exome {
exome_af:af
exome_ac:ac
exome_an:an
exome_ac_hemi:ac_hemi
exome_ac_hom:ac_hom
}
flags
lof
consequence_in_canonical_transcript
gene_symbol
hgvsc
lof_filter
lof_flags
hgvsc
hgvsp
reference_genome
variant_id: variantId
}
gtex_tissue_expression{
adipose_subcutaneous,
adipose_visceral_omentum,
adrenal_gland,
artery_aorta,
artery_coronary,
artery_tibial,
bladder,
brain_amygdala,
brain_anterior_cingulate_cortex_ba24,
brain_caudate_basal_ganglia,
brain_cerebellar_hemisphere,
brain_cerebellum,
brain_cortex,
brain_frontal_cortex_ba9,
brain_hippocampus,
brain_hypothalamus,
brain_nucleus_accumbens_basal_ganglia,
brain_putamen_basal_ganglia,
brain_spinal_cord_cervical_c_1,
brain_substantia_nigra,
breast_mammary_tissue,
cells_ebv_transformed_lymphocytes,
cells_transformed_fibroblasts,
cervix_ectocervix,
cervix_endocervix,
colon_sigmoid,
colon_transverse,
esophagus_gastroesophageal_junction,
esophagus_mucosa,
esophagus_muscularis,
fallopian_tube,
heart_atrial_appendage,
heart_left_ventricle,
kidney_cortex,
liver,
lung,
minor_salivary_gland,
muscle_skeletal,
nerve_tibial,
ovary,
pancreas,
pituitary,
prostate,
skin_not_sun_exposed_suprapubic,
skin_sun_exposed_lower_leg,
small_intestine_terminal_ileum,
spleen,
stomach,
testis,
thyroid,
uterus,
vagina,
whole_blood
}
clinvar_variants{
variant_id,
clinvar_variation_id,
reference_genome,
chrom,
pos,
ref,
alt,
clinical_significance,
gold_stars,
major_consequence,
review_status
}
coverage(dataset: %s){
genome{
pos,
mean,
median,
over_1,
over_5,
over_10,
over_15,
over_20,
over_25,
over_30,
over_50,
over_100
}
exome{
pos,
mean,
median,
over_1,
over_5,
over_10,
over_15,
over_20,
over_25,
over_30,
over_50,
over_100
}
}
gnomad_constraint{
exp_lof,
exp_mis,
exp_syn,
obs_lof,
obs_mis,
obs_syn,
oe_lof,
oe_lof_lower,
oe_lof_upper,
oe_mis,
oe_mis_lower,
oe_mis_upper,
oe_syn,
oe_syn_lower,
oe_syn_upper,
lof_z,
mis_z,
syn_z,
pLI,
flags
}
exac_constraint{
exp_syn,
exp_mis,
exp_lof,
obs_syn,
obs_mis,
obs_lof,
mu_syn,
mu_mis,
mu_lof,
syn_z,
mis_z,
lof_z,
pLI
}
}
}
"""
query_for_variants = """
{
variant(%s: "%s", dataset: %s) {
variantId
reference_genome
chrom
pos
ref
alt
colocatedVariants
multiNucleotideVariants {
combined_variant_id
changes_amino_acids
n_individuals
other_constituent_snvs
}
exome {
ac
an
ac_hemi
ac_hom
faf95 {
popmax
popmax_population
}
filters
populations {
id
ac
an
ac_hemi
ac_hom
}
age_distribution {
het {
bin_edges
bin_freq
n_smaller
n_larger
}
hom {
bin_edges
bin_freq
n_smaller
n_larger
}
}
qualityMetrics {
alleleBalance {
alt {
bin_edges
bin_freq
n_smaller
n_larger
}
}
genotypeDepth {
all {
bin_edges
bin_freq
n_smaller
n_larger
}
alt {
bin_edges
bin_freq
n_smaller
n_larger
}
}
genotypeQuality {
all {
bin_edges
bin_freq
n_smaller
n_larger
}
alt {
bin_edges
bin_freq
n_smaller
n_larger
}
}
}
}
genome {
ac
an
ac_hemi
ac_hom
faf95 {
popmax
popmax_population
}
filters
populations {
id
ac
an
ac_hemi
ac_hom
}
age_distribution {
het {
bin_edges
bin_freq
n_smaller
n_larger
}
hom {
bin_edges
bin_freq
n_smaller
n_larger
}
}
qualityMetrics {
alleleBalance {
alt {
bin_edges
bin_freq
n_smaller
n_larger
}
}
genotypeDepth {
all {
bin_edges
bin_freq
n_smaller
n_larger
}
alt {
bin_edges
bin_freq
n_smaller
n_larger
}
}
genotypeQuality {
all {
bin_edges
bin_freq
n_smaller
n_larger
}
alt {
bin_edges
bin_freq
n_smaller
n_larger
}
}
}
}
flags
rsid
sortedTranscriptConsequences {
canonical
gene_id
gene_version
gene_symbol
hgvs
hgvsc
hgvsp
lof
lof_flags
lof_filter
major_consequence
polyphen_prediction
sift_prediction
transcript_id
transcript_version
}
}
}
"""
query_for_genes = """
{
gene(%s: "%s", reference_genome: %s) {
gene_id
symbol
start
stop
strand
chrom
hgnc_id
gene_name
symbol
full_gene_name
reference_genome
omim_id
canonical_transcript_id
structural_variants(dataset: %s){
ac,
ac_hom,
an,
af,
reference_genome,
chrom,
chrom2,
end,
end2,
consequence,
filters,
length,
pos,
pos2,
type,
variant_id
}
variants(dataset: %s) {
pos
rsid
ref
alt
consequence
genome {
genome_af:af
genome_ac:ac
genome_an:an
genome_ac_hemi:ac_hemi
genome_ac_hom:ac_hom
}
exome {
exome_af:af
exome_ac:ac
exome_an:an
exome_ac_hemi:ac_hemi
exome_ac_hom:ac_hom
}
flags
lof
consequence_in_canonical_transcript
gene_symbol
hgvsc
lof_filter
lof_flags
hgvsc
hgvsp
reference_genome
variant_id: variantId
}
mane_select_transcript{
ensembl_id
ensembl_version
refseq_id
refseq_version
}
transcripts{
reference_genome
gene_id
transcript_id
strand
start
stop
chrom
}
exac_regional_missense_constraint_regions {
start
stop
obs_mis
exp_mis
obs_exp
chisq_diff_null
}
clinvar_variants {
variant_id
clinvar_variation_id
reference_genome
chrom
pos
ref
alt
clinical_significance
gold_stars
major_consequence
review_status
}
coverage(dataset: %s) {
exome {
pos
mean
median
over_1
over_5
over_10
over_15
over_20
over_25
over_30
over_50
over_100
}
genome {
pos
mean
median
over_1
over_5
over_10
over_15
over_20
over_25
over_30
over_50
over_100
}
}
gnomad_constraint {
exp_lof
exp_mis
exp_syn
obs_lof
obs_mis
obs_syn
oe_lof
oe_lof_lower
oe_lof_upper
oe_mis
oe_mis_lower
oe_mis_upper
oe_syn
oe_syn_lower
oe_syn_upper
lof_z
mis_z
syn_z
pLI
flags
}
exac_constraint {
exp_syn
exp_mis
exp_lof
obs_syn
obs_mis
obs_lof
mu_syn
mu_mis
mu_lof
syn_z
mis_z
lof_z
pLI
}
}
}
"""
if filter_by == "transcript_id":
query = query_for_transcripts % (search_term.upper(), reference_genome, dataset, dataset)
elif filter_by == "rs_id":
query = query_for_variants % ("rsid", search_term.lower(), dataset)
elif filter_by == "gene_id":
query = query_for_genes % ("gene_id", search_term.upper(), reference_genome, sv_dataset, dataset, dataset)
elif filter_by == "gene_name":
query = query_for_genes % ("gene_name", search_term.upper(), reference_genome, sv_dataset, dataset, dataset)
else:
print("Unknown `filter_by` type!")
# Get repsonse
response = requests.post(end_point, data={'query': query}, timeout=timeout)
# Parse response
if response.status_code == 200:
try:
if filter_by == "transcript_id":
if not os.path.exists('outputs/' + search_term + "/"):
os.mkdir('outputs/'+ search_term + "/")
else:
shutil.rmtree('outputs/'+ search_term + "/")
os.mkdir('outputs/'+ search_term + "/")
json_keys = list(response.json()["data"]["transcript"].keys())
for json_key in json_keys:
if response.json()["data"]["transcript"][json_key] is not None and type(response.json()["data"]["transcript"][json_key]) not in [str, int]:
data = json_normalize(response.json()["data"]["transcript"][json_key])
data.columns = data.columns.map(lambda x: x.split(".")[-1])
data.to_csv("outputs/" + search_term + "/" + json_key + ".tsv", sep="\t", index=False)
elif filter_by == "rs_id":
if not os.path.exists('outputs/' + search_term + "/"):
os.mkdir('outputs/'+ search_term + "/")
else:
shutil.rmtree('outputs/'+ search_term + "/")
os.mkdir('outputs/'+ search_term + "/")
json_keys = list(response.json()["data"]["variant"].keys())
for json_key in json_keys:
# print(json_key, type(response.json()["data"]["variant"][json_key]))
# Basic info in `variant` part
if response.json()["data"]["variant"][json_key] is not None and type(response.json()["data"]["variant"][json_key]) in [str, int]:
with open("outputs/" + search_term + "/" + search_term + ".txt", "a") as f:
f.write("\n" + json_key + ":" + str(response.json()["data"]["variant"][json_key]))
# Other parts rather than `genome` and `exome`
if response.json()["data"]["variant"][json_key] is not None and type(response.json()["data"]["variant"][json_key]) not in [str, int] and json_key not in ["genome", "exome"]:
data = json_normalize(response.json()["data"]["variant"][json_key])
data.columns = data.columns.map(lambda x: x.split(".")[-1])
data.to_csv("outputs/" + search_term + "/" + json_key + ".tsv", sep="\t", index=False)
# Deep parsing for nested things in `genome` and `exome`
if json_key in ["genome", "exome"]:
for sub_json_key in list(response.json()["data"]["variant"][json_key].keys()):
# print(json_key, sub_json_key, type(response.json()["data"]["variant"][json_key][sub_json_key]))
if response.json()["data"]["variant"][json_key][sub_json_key] is not None and type(response.json()["data"]["variant"][json_key][sub_json_key]) in [str, int]:
with open("outputs/" + search_term + "/" + search_term + ".txt", "a") as f:
f.write("\n" + json_key + "_" + sub_json_key + ":" + str(response.json()["data"]["variant"][json_key][sub_json_key]))
if response.json()["data"]["variant"][json_key][sub_json_key] is not None and type(response.json()["data"]["variant"][json_key][sub_json_key]) not in [str, int]:
data = json_normalize(response.json()["data"]["variant"][json_key][sub_json_key])
data.columns = data.columns.map(lambda x: x.split(".")[-1])
data.to_csv("outputs/" + search_term + "/" + json_key + "_" + sub_json_key + ".tsv", sep="\t", index=False)
elif filter_by == "gene_id":
if not os.path.exists('outputs/' + search_term + "/"):
os.mkdir('outputs/'+ search_term + "/")
else:
shutil.rmtree('outputs/'+ search_term + "/")
os.mkdir('outputs/'+ search_term + "/")
json_keys = list(response.json()["data"]["gene"].keys())
for json_key in json_keys:
# print(json_key, type(response.json()["data"]["gene"][json_key]), response.json()["data"]["gene"][json_key] is None, type(response.json()["data"]["gene"][json_key]) not in [str, int])
if response.json()["data"]["gene"][json_key] is not None and type(response.json()["data"]["gene"][json_key]) in [str, int]:
with open("outputs/" + search_term + "/" + search_term + ".txt", "a") as f:
f.write("\n" + json_key + ":" + str(response.json()["data"]["gene"][json_key]))
if response.json()["data"]["gene"][json_key] is not None and type(response.json()["data"]["gene"][json_key]) not in [str, int]:
data = json_normalize(response.json()["data"]["gene"][json_key])
data.columns = data.columns.map(lambda x: x.split(".")[-1])
data.to_csv("outputs/" + search_term + "/" + json_key + ".tsv", sep="\t", index=False)
elif filter_by == "gene_name":
if not os.path.exists('outputs/' + search_term + "/"):
os.mkdir('outputs/'+ search_term + "/")
else:
shutil.rmtree('outputs/'+ search_term + "/")
os.mkdir('outputs/'+ search_term + "/")
json_keys = list(response.json()["data"]["gene"].keys())
for json_key in json_keys:
# print(json_key, type(response.json()["data"]["gene"][json_key]), response.json()["data"]["gene"][json_key] is None, type(response.json()["data"]["gene"][json_key]) not in [str, int])
if response.json()["data"]["gene"][json_key] is not None and type(response.json()["data"]["gene"][json_key]) in [str, int]:
with open("outputs/" + search_term + "/" + search_term + ".txt", "a") as f:
f.write("\n" + json_key + ":" + str(response.json()["data"]["gene"][json_key]))
if response.json()["data"]["gene"][json_key] is not None and type(response.json()["data"]["gene"][json_key]) not in [str, int]:
data = json_normalize(response.json()["data"]["gene"][json_key])
data.columns = data.columns.map(lambda x: x.split(".")[-1])
data.to_csv("outputs/" + search_term + "/" + json_key + ".tsv", sep="\t", index=False)
except (ConnectionError, ConnectionAbortedError, ConnectionRefusedError, ConnectionResetError):
sys.exit("An unknown error occured regarding the internet connection!")
except AttributeError as ae:
# Error Message from gnomAD
try:
for msg in response.json()["errors"]:
sys.exit("Errors from gnomAD for your process:\n\t" + msg["message"])
except Exception as anyOtherException:
pass
if filter_by != "rs_id":
# General Error Message
print("""
It might be caused since the search did not find a result from the database.
Try to check the `input` for `{}` or other `options`.
""".format(filter_by))
# Technical Error Message
print("""
> As a note, technical reason is `{}`.
>
> If you think this should not occur, you can contact with developer to issue this problem on Github page.
""".format(ae))
except (TypeError, KeyError):
try:
for msg in response.json()["errors"]:
print("Errors from gnomAD for your process:\n\t" + msg["message"])
except Exception as anyOtherException:
pass
else:
print(" ! DONE: Check out the 'outputs/' folder")
elif response.status_code == 404:
sys.exit('API is not accessible right now. Check the end point out!')
# Action
if __name__ == "__main__":
filter_by, search_by, dataset, sv_dataset, reference_genome = arg_parser()
if "." in search_by:
try:
with open(search_by, "r") as f:
search_list = [line.rstrip() for line in f]
for search_item in tqdm(search_list):
get_variants_by(filter_by, search_item, dataset, reference_genome, sv_dataset)
except:
print("A problem occured while reading the file namely `{}` or the filter type `{}` is wrong!"\
.format(search_by, filter_by))
finally:
f.close()
elif "." not in search_by:
get_variants_by(filter_by, search_by, dataset, reference_genome, sv_dataset)