-
Notifications
You must be signed in to change notification settings - Fork 92
/
Intervar.py
2128 lines (1864 loc) · 83.9 KB
/
Intervar.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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#########################################################################
# Author: Lee Quan ([email protected])
# Created Time: 2015-11-10 19:15:32 Tuesday
# File Name: InterVar.py File type: python
# Last Change:.
# Description: python script for Interpretation of Pathogenetic Benign
#########################################################################
import copy,logging,os,io,re,time,sys,platform,optparse,gzip,glob
prog="InterVar"
version = """%prog 2.2.2 20210727
Written by Quan LI,[email protected].
InterVar is free for non-commercial use without warranty.
Please contact the authors for commercial use.
Copyright (C) 2016 Wang Genomic Lab
============================================================================
"""
usage = """Usage: %prog [OPTION] -i INPUT -o OUTPUT ...
%prog --config=config.ini ...
"""
description = """=============================================================================
InterVar
Interpretation of Pathogenic/Benign for variants using python scripts.
.####.##....##.########.########.########..##.....##....###....########.
..##..###...##....##....##.......##.....##.##.....##...##.##...##.....##
..##..####..##....##....##.......##.....##.##.....##..##...##..##.....##
..##..##.##.##....##....######...########..##.....##.##.....##.########.
..##..##..####....##....##.......##...##....##...##..#########.##...##..
..##..##...###....##....##.......##....##....##.##...##.....##.##....##.
.####.##....##....##....########.##.....##....###....##.....##.##.....##
=============================================================================
"""
end = """=============================================================================
........................................................................
.####.##....##.########.########.########..##.....##....###....########.
..##..###...##....##....##.......##.....##.##.....##...##.##...##.....##
..##..####..##....##....##.......##.....##.##.....##..##...##..##.....##
..##..##.##.##....##....######...########..##.....##.##.....##.########.
..##..##..####....##....##.......##...##....##...##..#########.##...##..
..##..##...###....##....##.......##....##....##.##...##.....##.##....##.
.####.##....##....##....########.##.....##....###....##.....##.##.....##
.......................................................................
Thanks for using InterVar!
Report bugs to [email protected];
InterVar homepage: <http://wInterVar.wglab.org>
=============================================================================
"""
line_sum=0;
if platform.python_version()< '3.0.0' :
import ConfigParser
else:
import configparser
paras = {}
def ConfigSectionMap(config,section):
global paras
options = config.options(section)
for option in options:
try:
paras[option] = config.get(section, option)
if paras[option] == -1:
DebugPrint("skip: %s" % option)
except:
print("exception on %s!" % option)
paras[option] = None
return
user_evidence_dict={}
class myGzipFile(gzip.GzipFile):
def __enter__(self):
if self.fileobj is None:
raise ValueError("I/O operation on closed GzipFile object")
return self
def __exit__(self, *args):
self.close()
#begin read some important datsets/list firstly;
lof_genes_dict={}
aa_changes_dict={}
domain_benign_dict={}
mim2gene_dict={}
mim2gene_dict2={}
morbidmap_dict={}
morbidmap_dict2={}
PP2_genes_dict={}
BP1_genes_dict={}
PS4_snps_dict={}
exclude_snps_dict={}
mim_recessive_dict={}
mim_domin_dict={}
mim_adultonset_dict={}
mim_pheno_dict={}
mim_orpha_dict={}
orpha_dict={}
BS2_snps_recess_dict={}
BS2_snps_domin_dict={}
knownGeneCanonical_dict={}
knownGeneCanonical_st_dict={}
knownGeneCanonical_ed_dict={}
def flip_ACGT(acgt):
nt="";
if acgt=="A":
nt="T"
if acgt=="T":
nt="A"
if acgt=="C":
nt="G"
if acgt=="G":
nt="C"
if acgt=="N":
nt="N"
if acgt=="X":
nt="X"
return(nt)
def read_datasets():
#0. read the user specified evidence file
if os.path.isfile(paras['evidence_file']):
try:
fh=open(paras['evidence_file'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split('\t')
if len(cls2)>1:
keys=cls2[0]+"_"+cls2[1]+"_"+cls2[2]+"_"+cls2[3]
keys=re.sub("[Cc][Hh][Rr]","",keys)
#print("%s" %keys)
user_evidence_dict[keys]=cls2[4].upper()
except IOError:
print("Error: can\'t read the user specified evidence file %s" % paras['evidence_file'])
else:
fh.close()
#1.LOF gene list
try:
fh = open(paras['lof_genes'], "r")
str = fh.read()
for line2 in str.split('\n'):
cls2=line2.split('\t')
if len(cls2[0])>1:
lof_genes_dict[cls2[0]]='1'
except IOError:
print("Error: can\'t read the LOF genes file %s" % paras['lof_genes'])
print("Error: Please download it from the source website")
sys.exit()
return
else:
fh.close()
#2. AA change list
try:
fh = open(paras['ps1_aa'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split('\t')
if len(cls2)>1 :
keys=cls2[0]+"_"+cls2[1]+"_"+cls2[2]+"_"+cls2[4]
keys=re.sub("[Cc][Hh][Rr]","",keys)
aa_changes_dict[keys]=cls2[6]
except IOError:
print("Error: can\'t read the amino acid change file %s" % paras['ps1_aa'])
print("Error: Please download it from the source website")
sys.exit()
else:
fh.close()
#3. Domain with benign
try:
fh = open(paras['pm1_domain'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split('\t')
if len(cls2)>1:
keys=cls2[0]+"_"+cls2[1]+": "+cls2[2]
domain_benign_dict[keys]="1"
except IOError:
print("Error: can\'t read the PM1 domain file %s" % paras['pm1_domain'])
print("Error: Please download it from the source website")
sys.exit()
else:
fh.close()
#4. OMIM mim2gene.txt file
try:
fh = open(paras['mim2gene'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split('\t')
if len(cls2)>1:
cls0=cls2[4].split(',')
keys=cls0[0]
mim2gene_dict[keys]=cls2[0]
keys1=cls2[3]
keys=keys1.upper()
mim2gene_dict2[keys]=cls2[0]
except IOError:
print("Error: can\'t read the OMIM file %s" % paras['mim2gene'])
print("Error: Please download it from http://www.omim.org/downloads")
sys.exit()
else:
fh.close()
#5.PP2 gene list
try:
fh = open(paras['pp2_genes'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split('\t')
if len(cls2[0])>1:
PP2_genes_dict[cls2[0]]='1'
except IOError:
print("Error: can\'t read the PP2 genes file %s" % paras['PP2_genes'])
print("Error: Please download it from the source website")
sys.exit()
return
else:
fh.close()
#5.BP1 gene list
try:
fh = open(paras['bp1_genes'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split('\t')
if len(cls2[0])>1:
BP1_genes_dict[cls2[0]]='1'
except IOError:
print("Error: can\'t read the BP1 genes file %s" % paras['BP1_genes'])
print("Error: Please download it from the source website")
sys.exit()
return
else:
fh.close()
#6.morbidmap from OMIM for BP5 , multifactorial disorders list
#The reviewers suggeset to disable the OMIM morbidmap for BP5
'''
try:
fh = open(paras['morbidmap'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split('\t')
#print("%s %s %d" % (cls2[0], cls[Funcanno_flgs['Gene']], len(cls2[0])) )
#{Tuberculosis, protection against}, 607948 (3)|TIRAP, BACTS1|606252|11q24.2
if len(cls2[0])>1 and cls2[0].find('{')==0: # disorder start with "{"
morbidmap_dict2[ cls2[2] ]='1' # key as mim number
for cls3 in cls2[1].split(', '):
keys=cls3.upper()
morbidmap_dict[ keys ]='1' # key as gene name
except IOError:
print("Error: can\'t read the OMIM morbidmap disorder file %s" % paras['morbidmap'])
print("Error: Please download it from http://www.omim.org/downloads")
sys.exit()
else:
fh.close()
'''
#7.prevalence of the variant with OR>5 for PS4 , the dataset is from gwasdb jjwanglab.org/gwasdb
try:
fh = open(paras['ps4_snps'], "r")
str = fh.read()
for line2 in str.split('\n'):
cls2=line2.split('\t')
# PS4_snps_dict
if len(cls2[0])>=1: #
keys=cls2[0]+"_"+cls2[1]+"_"+cls2[1]+"_"+cls2[3]+"_"+cls2[4]
keys=re.sub("[Cc][Hh][Rr]","",keys)
PS4_snps_dict[ keys ]='1' # key as gene name
except IOError:
print("Error: can\'t read the snp list file for PS4 %s" % paras['ps4_snps'])
print("Error: Please download it from the source website")
sys.exit()
else:
fh.close()
#8. read the user specified SNP list, the variants will pass the frequency check.
if os.path.isfile(paras['exclude_snps']):
try:
fh=open(paras['exclude_snps'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split('\t')
if len(cls2)>1:
keys=cls2[0]+"_"+cls2[1]+"_"+cls2[2]+"_"+cls2[3]
keys=re.sub("[Cc][Hh][Rr]","",keys)
exclude_snps_dict[keys]="1"
except IOError:
print("Error: can\'t read the user specified SNP list file %s" % paras['exclude_snps'])
else:
fh.close()
#9. OMIM mim_recessive.txt file mim_domin mim_adultonset
try:
fh = open(paras['mim_recessive'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split('\t')
if len(cls2[0])>1:
mim_recessive_dict[cls2[0]]='1'
except IOError:
print("Error: can\'t read the OMIM recessive disorder file %s" % paras['mim_recessive'])
print("Error: Please download it from the source website")
sys.exit()
else:
fh.close()
try:
fh = open(paras['mim_domin'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split('\t')
if len(cls2[0])>1:
mim_domin_dict[cls2[0]]='1'
except IOError:
print("Error: can\'t read the OMIM dominant disorder file %s" % paras['mim_domin'])
print("Error: Please download it from the source website")
sys.exit()
else:
fh.close()
try:
fh = open(paras['mim_adultonset'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split('\t')
if len(cls2[0])>1:
mim_adultonset_dict[cls2[0]]='1'
except IOError:
print("Error: can\'t read the OMIM adult onset disorder file %s" % paras['mim_adultonset'])
print("Error: Please download it from the source website")
sys.exit()
else:
fh.close()
#10. knownGeneCanonical exon file # caution the build ver, now it is hg19
try:
fh = open(paras['knowngenecanonical'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split(' ')
if len(cls2)>1:
keys=cls2[0]
knownGeneCanonical_dict[keys]=cls2[1]
knownGeneCanonical_st_dict[keys]=cls2[2]
knownGeneCanonical_ed_dict[keys]=cls2[3]
#print("%s %s" %(keys,knownGeneCanonical_dict[keys]))
except IOError:
print("Error: can\'t read the knownGeneCanonical file %s" % paras['knowngenecanonical'])
print("Error: Please download it from the source website")
sys.exit()
else:
fh.close()
#11.BS2 variants of recessive homo, domin heter
try:
with myGzipFile(paras['bs2_snps'], "rb") as fh:
#fh = open(paras['bs2_snps'], "r")
strs = fh.read().decode()
for line2 in strs.split('\n'):
cls2=line2.split(' ')
# PS4_snps_dict
if len(cls2[0])>=1: #
keys=cls2[0]+"_"+cls2[1]+"_"+cls2[1]+"_"+cls2[2]+"_"+cls2[3]
#keys=re.sub("[Cc][Hh][Rr]","",keys)
BS2_snps_recess_dict[ keys ]=cls2[4] # key as snp info
BS2_snps_domin_dict[ keys ]=cls2[5] # key as snp info
keys=cls2[0]+"_"+cls2[1]+"_"+cls2[1]+"_"+flip_ACGT(cls2[2])+"_"+flip_ACGT(cls2[3])
#keys=re.sub("[Cc][Hh][Rr]","",keys)
BS2_snps_recess_dict[ keys ]=cls2[4] # key as snp info
BS2_snps_domin_dict[ keys ]=cls2[5] # key as snp info
except IOError:
print("Error: can\'t read the snp list file for BS2 %s" % paras['bs2_snps'])
print("Error: Please download it from the source website")
sys.exit()
else:
fh.close()
#12. OMIM mim_pheno.txt file
#mim_pheno = %(database_intervar)s/mim_pheno.txt
#mim_orpha = %(database_intervar)s/mim_orpha.txt
try:
fh = open(paras['mim_pheno'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split(' ')
if len(cls2)>1:
keys=cls2[0]
mim_pheno_dict[keys]=cls2[1]
#print("%s %s" %(keys,mim_pheno_dict[keys]))
except IOError:
print("Error: can\'t read the MIM file %s" % paras['mim_pheno'])
print("Error: Please download it from InterVar source website")
sys.exit()
else:
fh.close()
#13. OMIM mim_orpha.txt file
try:
fh = open(paras['mim_orpha'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split(' ')
if len(cls2)>1:
keys=cls2[0]
mim_orpha_dict[keys]=cls2[1]
#print("%s %s" %(keys,mim_orpha_dict[keys]))
except IOError:
print("Error: can\'t read the MIM file %s" % paras['mim_orpha'])
print("Error: Please download it from InterVar source website")
sys.exit()
else:
fh.close()
#14. orpha.txt file
try:
fh = open(paras['orpha'], "r")
strs = fh.read()
for line2 in strs.split('\n'):
cls2=line2.split('\t')
if len(cls2)>1:
keys=cls2[0]
orpha_dict[keys]=cls2[1]
#print("%s %s" %(keys,mim_orpha_dict[keys]))
except IOError:
print("Error: can\'t read the Orpha file %s" % paras['orpha'])
print("Error: Please download it from InterVar source website")
sys.exit()
else:
fh.close()
#end read datasets
return
def check_downdb():
path=paras['database_locat']
path=path.strip()
path=path.rstrip("\/")
isExists=os.path.exists(path)
if not isExists:
os.makedirs(path)
print("Notice: the folder of %s is created!" % path)
else:
print("Warning: the folder of %s is already created!" % path)
ds=paras['database_names']
ds.expandtabs(1);
# database_names = refGene 1000g2014oct esp6500siv2_all avsnp147 ljb26_all clinvar_20150629 gnomad_genome hg19_dbscsnv11 dbnsfp31a_interpro rmsk ensGene
if not os.path.isfile(paras['annotate_variation']):
print("Warning: The Annovar file [ %s ] is not here,please download ANNOVAR firstly: http://www.openbioinformatics.org/annovar"
% paras['annotate_variation'])
if paras['skip_annovar'] != True:
sys.exit()
for dbs in ds.split():
# os.path.isfile(options.table_annovar)
file_name=dbs
#if dbs=="1000g2014oct":
# file_name="ALL.sites.2014_10"
if dbs=="1000g2015aug":
file_name="ALL.sites.2015_08" # hg19_ALL.sites.2015_08.txt
dataset_file=paras['database_locat']+"/"+paras['buildver']+"_"+file_name+".txt"
if dbs != 'rmsk':
cmd="perl "+paras['annotate_variation']+" -buildver "+paras['buildver']+" -downdb -webfrom annovar "+file_name+" "+paras['database_locat']
if dbs == 'rmsk':
cmd="perl "+paras['annotate_variation']+" -buildver "+paras['buildver']+" -downdb "+file_name+" "+paras['database_locat']
if not os.path.isfile(dataset_file):
if dbs=="1000g2015aug":
file_name="1000g2015aug"
dataset_file=paras['database_locat']+"/"+paras['buildver']+"_"+file_name+".txt"
cmd="perl "+paras['annotate_variation']+" -buildver "+paras['buildver']+" -downdb -webfrom annovar "+file_name+" "+paras['database_locat']
if paras['skip_annovar'] != True:
print("Warning: The Annovar dataset file of %s is not in %s,begin to download this %s ..." %(dbs,paras['database_locat'],dataset_file))
if paras['skip_annovar'] != True:
print("%s" %cmd)
os.system(cmd)
def check_input():
inputft= paras['inputfile_type']
if inputft.lower() == 'avinput' :
return
if inputft.lower() == 'vcf':
if os.path.isfile(paras['convert2annovar']):
#convert2annovar.pl -format vcf4 variantfile > variant.avinput
cmd="perl "+paras['convert2annovar']+" -format vcf4 "+ paras['inputfile']+"> "+paras['inputfile']+".avinput"
print("Warning: Begin to convert your vcf file of %s to AVinput of Annovar ..." % paras['inputfile'])
print("%s" %cmd)
os.system(cmd)
else:
print("Warning: The Annovar file [ %s ] is not here,please download ANNOVAR firstly: http://www.openbioinformatics.org/annovar"
% paras['convert2annovar'])
if paras['skip_annovar'] != True:
sys.exit()
if inputft.lower() == 'vcf_m':
if os.path.isfile(paras['convert2annovar']):
#convert2annovar.pl -format vcf4 variantfile > variant.avinput
cmd="perl "+paras['convert2annovar']+" -format vcf4 "+ paras['inputfile']+" --allsample --outfile "+ paras['outfile']
print("Warning: Begin to convert your vcf file with multiple samples of %s to AVinput of Annovar with All.raw.highqc.vcf.<samplename>.avinput..." % paras['inputfile'])
print("Warning: Please attention that the sample names in VCF file should contain letters/numners only, otherwise the converting may be failure!")
print("%s" %cmd)
os.system(cmd)
else:
print("Warning: The Annovar file [ %s ] is not here,please download ANNOVAR firstly: http://www.openbioinformatics.org/annovar"
% paras['convert2annovar'])
if paras['skip_annovar'] != True:
sys.exit()
return
def check_annovar_result():
# table_annovar.pl example/ex1.avinput humandb/ -buildver hg19 -out myanno -remove -protocol refGene,esp6500siv2_all,1000g2015aug_all,avsnp147,ljb26_all,CLINSIG,gnomad_genome -operation g,f,f,f,f,f,f -nastring . -csvout
inputft= paras['inputfile_type']
annovar_options=" "
if re.findall('true',paras['otherinfo'], flags=re.IGNORECASE) :
annovar_options=annovar_options+"--otherinfo "
if re.findall('true',paras['onetranscript'], flags=re.IGNORECASE) :
annovar_options=annovar_options+"--onetranscript "
if not os.path.isfile(paras['table_annovar']):
print("Warning: The Annovar file [ %s ] is not here,please download ANNOVAR firstly: http://www.openbioinformatics.org/annovar"
% paras['table_annovar'])
if paras['skip_annovar'] != True:
sys.exit()
if inputft.lower() == 'avinput' :
cmd="perl "+paras['table_annovar']+" "+paras['inputfile']+" "+paras['database_locat']+" -buildver "+paras['buildver']+" -remove -out "+ paras['outfile']+" -protocol refGene,esp6500siv2_all,1000g2015aug_all,avsnp147,dbnsfp42a,clinvar_20210501,gnomad_genome,dbscsnv11,rmsk,ensGene,knownGene -operation g,f,f,f,f,f,f,f,r,g,g -nastring ."+annovar_options
print("%s" %cmd)
os.system(cmd)
if inputft.lower() == 'vcf' :
cmd="perl "+paras['table_annovar']+" "+paras['inputfile']+".avinput "+paras['database_locat']+" -buildver "+paras['buildver']+" -remove -out "+ paras['outfile']+" -protocol refGene,esp6500siv2_all,1000g2015aug_all,avsnp147,dbnsfp42a,clinvar_20210501,gnomad_genome,dbscsnv11,rmsk,ensGene,knownGene -operation g,f,f,f,f,f,f,f,r,g,g -nastring ."+annovar_options
print("%s" %cmd)
os.system(cmd)
if inputft.lower() == 'vcf_m' :
for f in glob.iglob(paras['outfile']+"*.avinput"):
print("INFO: Begin to annotate sample file of %s ...." %(f))
new_outfile=re.sub(".avinput","",f)
cmd="perl "+paras['table_annovar']+" "+f+" "+paras['database_locat']+" -buildver "+paras['buildver']+" -remove -out "+ new_outfile +" -protocol refGene,esp6500siv2_all,1000g2015aug_all,avsnp147,dbnsfp42a,clinvar_20210501,gnomad_genome,dbscsnv11,rmsk,ensGene,knownGene -operation g,f,f,f,f,f,f,f,r,g,g -nastring ."+annovar_options
print("%s" %cmd)
os.system(cmd)
return
'''
def get_gdi_rvis_lof(gene_name,line_out,dicts,temple):
try:
line_out=line_out+"\t"+'\t'.join(str(e) for e in dicts[gene_name])
except KeyError:
line_out=line_out+"\t"+'\t'.join(str(e) for e in temple)
else:
pass
return(line_out)
def check_gdi_rvis_LOF(anvfile):
gdi={}
rvis={}
lof={}
newoutfile=anvfile+".grl_p"
# begin open file and set dicts for gdi rvis and lof:
try:
fh = open(paras['gdi_file'], "r")
str = fh.read()
for line in str.split('\n'):
cls=line.split('\t')
if len(cls)>1:
gdi[cls[0]]=cls[1:]
except IOError:
print("Error: can\'t read the annovar output file %s" % paras['gdi_file'])
sys.exit()
return
else:
pass
fh.close()
try:
fh = open(paras['rvis_file'], "r")
str = fh.read()
for line in str.split('\n'):
cls=line.split('\t')
rvis['Gene']=['RVIS_gnomAD_genome_0.05%(AnyPopn)','%RVIS_gnomAD_genome_0.05%(AnyPopn)']
if len(cls)>1:
rvis[cls[4]]=cls[5:]
except IOError:
print("Error: can\'t read the annovar output file %s" % paras['rvis_file'])
sys.exit()
return
else:
pass
fh.close()
try:
fh = open(paras['lof_file'], "r")
str = fh.read()
for line in str.split('\n'):
cls=line.split('\t')
if len(cls)>1:
lof[cls[0]]=cls[1:]
except IOError:
print("Error: can\'t read the annovar output file %s" % paras['lof_file'])
sys.exit()
return
else:
pass
fh.close()
try:
fh = open(anvfile, "r")
fw = open(newoutfile, "w")
str = fh.read()
sum=0
for line in str.split('\n'):
cls=line.split('\t')
if len(cls)>1:
gene_name=cls[6]
if cls[6] == 'Gene.refGene':
gene_name='Gene'
#some with multiple genes, so one gene by one gene to annote
gdi_temp=['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.']
rvis_temp=['.', '.']
lof_temp=['.']
sum=sum+1
for gg in gene_name.split(','):
line_out=line+"\t"+gg
line_out=get_gdi_rvis_lof(gg,line_out,gdi,gdi_temp)
line_out=get_gdi_rvis_lof(gg,line_out,rvis,rvis_temp)
line_out=get_gdi_rvis_lof(gg,line_out,lof,lof_temp)
fw.write("%s\n" % line_out)
# fh.write("This is my test file for exception handling!!")
except IOError:
print("Error: can\'t read/write the annovar output file %s %s" % (anvfile,newoutfile))
sys.exit()
return
else:
pass
fh.close()
fw.close()
return(sum)
'''
def check_genes(anvfile):
#check with multiple genes, so one gene by one gene to annote
newoutfile=anvfile+".grl_p"
try:
fh = open(anvfile, "r")
fw = open(newoutfile, "w")
strs = fh.read()
sum=0
otherinf_pos=1
line_sum=0;
for line in strs.split('\n'):
if line_sum==0:
line=re.sub("CLNSIG","CLINSIG",line)
cls=line.split('\t')
if len(cls)>1:
if sum==0 and re.findall('true',paras['otherinfo'], flags=re.IGNORECASE) :
for ii in range(0,len(cls)):
if re.findall('otherinfo',cls[ii], flags=re.IGNORECASE) :
otherinf_pos=ii
gene_name=cls[6]
if cls[6] == 'Gene.refGene':
gene_name='Gene'
#some with multiple genes, so one gene by one gene to annote
sum=sum+1
#for gg in gene_name.split(','):
for gg in re.split("[,;]",gene_name):
if not re.findall('true',paras['otherinfo'], flags=re.IGNORECASE) :
line_out=line+"\t"+gg
else:
line_out=cls[0]
for ii in range(1,len(cls)):
if ii != otherinf_pos :
line_out=line_out+"\t"+cls[ii]
if ii == otherinf_pos :
line_out=line_out+"\t"+gg+"\t"+cls[ii]
if sum >1: line_out=re.sub("^[Cc][Hh][Rr]","",line_out)
#line_out=line+"\t"+gg
# re.sub("[Cc][Hh][Rr]","",keys)
fw.write("%s\t\n" % line_out)
line_sum=line_sum+1
except IOError:
print("Error: can\'t read/write the annovar output file %s %s" % (anvfile,newoutfile))
sys.exit()
return
else:
pass
fh.close()
fw.close()
return(sum)
def sum_of_list(list):
sum=0
for i in list:
sum=sum+i
return(sum)
def classfy(PVS1,PS,PM,PP,BA1,BS,BP,Allels_flgs,cls):
BPS=["Pathogenic","Likely pathogenic","Benign","Likely benign","Uncertain significance"]
PAS_out=-1
BES_out=-1
BPS_out=4 # BPS=[4]:Uncertain significance
PS_sum=sum_of_list(PS)
PM_sum=sum_of_list(PM)
PP_sum=sum_of_list(PP)
BS_sum=sum_of_list(BS)
BP_sum=sum_of_list(BP)
#print("Before up/down grade, the sum of PS %s, PM %s,PP %s,BS %s,BP %s" %(PS_sum,PM_sum,PP_sum,BS_sum,BP_sum));
#begin process the user's flexible grade to get the final interpretation
if os.path.isfile(paras['evidence_file']):
keys=cls[Allels_flgs['Chr']]+"_"+cls[Allels_flgs['Start']]+"_"+cls[Allels_flgs['Ref']]+"_"+cls[Allels_flgs['Alt']]
keys=re.sub("[Cc][Hh][Rr]","",keys)
try:
evds=user_evidence_dict[keys] #PS1=1;PM1=1;BA1=1;PVS1 PP BS BP
for evd in evds.split(';'):
evd_t=evd.split('=')
if(len(evd_t)>1 and re.findall('grade', evd_t[0], flags=re.IGNORECASE) ):
#10 104353782 G A PVS1=1;PP1=1;PM3=1;grade_PP1=2;
if int(evd_t[1])<=3:
#print ("%s %s %s " %(keys,evd_t[1],evd_t[0]))
if(evd_t[0].find('PS')!=-1):
t=evd_t[0].find('PS');
tt=evd_t[0];
tt3=int(tt[t+2:t+3])
PS_sum=PS_sum-1
if(t<len(evd_t[0])-2 and tt3<=5 ):
if int(evd_t[1]) ==1 :
PS_sum=PS_sum+1
if int(evd_t[1]) ==2 :
PM_sum=PM_sum+1
if int(evd_t[1]) ==3 :
PP_sum=PP_sum+1
if(evd_t[0].find('PM')!=-1):
t=evd_t[0].find('PM');
tt=evd_t[0];
tt3=int(tt[t+2:t+3])
PM_sum=PM_sum-1
if(t<len(evd_t[0])-2 and tt3<=7 ):
if int(evd_t[1]) ==1 :
PS_sum=PS_sum+1
if int(evd_t[1]) ==2 :
PM_sum=PM_sum+1
if int(evd_t[1]) ==3 :
PP_sum=PP_sum+1
if(evd_t[0].find('PP')!=-1):
t=evd_t[0].find('PP');
tt=evd_t[0];
tt3=int(tt[t+2:t+3])
PP_sum=PP_sum-1
if(t<len(evd_t[0])-2 and tt3<=6 ):
if int(evd_t[1]) ==1 :
PS_sum=PS_sum+1
if int(evd_t[1]) ==2 :
PM_sum=PM_sum+1
if int(evd_t[1]) ==3 :
PP_sum=PP_sum+1
if(evd_t[0].find('BS')!=-1):
t=evd_t[0].find('BS');
tt=evd_t[0];
tt3=int(tt[t+2:t+3])
BS_sum=BS_sum-1
if(t<len(evd_t[0])-2 and tt3<=5 ):
if int(evd_t[1]) ==1 :
BS_sum=BS_sum+1
if int(evd_t[1]) ==3 :
BP_sum=BP_sum+1
if(evd_t[0].find('BP')!=-1):
t=evd_t[0].find('BP');
tt=evd_t[0];
tt3=int(tt[t+2:t+3])
BP_sum=BP_sum-1
if(t<len(evd_t[0])-2 and tt3<=8 ):
if int(evd_t[1]) ==1 :
BS_sum=BS_sum+1
if int(evd_t[1]) ==3 :
BP_sum=BP_sum+1
except KeyError:
pass
else:
pass
# end process the user's flexible grade
#print("After up/down grade, the sum of PS %s, PM %s,PP %s,BS %s,BP %s" %(PS_sum,PM_sum,PP_sum,BS_sum,BP_sum));
#print("%d %d %d %d %d " %(PS_sum,PM_sum,PP_sum,BS_sum, BP_sum))
if PS_sum ==1:
if PM_sum ==1 or PM_sum ==2: PAS_out=1
if PVS1 ==1 :
if PM_sum ==1: PAS_out=1 # 1:Likely pathogenic
if PS_sum ==1 and PP_sum >=2: PAS_out=1
if PM_sum >=3: PAS_out=1
if PM_sum ==2 and PP_sum >=2: PAS_out=1
if PM_sum ==1 and PP_sum >=4: PAS_out=1
if PVS1 ==1 :
if PS_sum >=1: PAS_out=0 # 0:Pathogenic
if PM_sum >=2: PAS_out=0
if PM_sum ==1 and PP_sum ==1: PAS_out=0
if PP_sum >=2: PAS_out=0
if PS_sum >=2: PAS_out=0
if PS_sum ==1:
if PM_sum >=3: PAS_out=0
if PM_sum ==2 and PP_sum >=2: PAS_out=0
if PM_sum ==1 and PP_sum >=4: PAS_out=0
if BS_sum==1 and BP_sum==1 :BES_out=3 #3:Likely benign
if BP_sum>=2 :BES_out=3
if BA1 ==1 or BS_sum>=2 : BES_out=2 #2:Benign
if PAS_out != -1 and BES_out == -1: BPS_out=PAS_out
if PAS_out == -1 and BES_out != -1: BPS_out=BES_out
if PAS_out == -1 and BES_out == -1: BPS_out=4
if PAS_out != -1 and BES_out != -1: BPS_out=4
#print("%d %d %d " %(PAS_out,BES_out,BPS_out))
return(BPS[BPS_out])
def check_PVS1(line,Funcanno_flgs,Allels_flgs,lof_genes_dict):
'''
Certain types of variants (e.g., nonsense, frameshift, canonical
+- 1 or 2 splice sites, initiation codon, single exon or multiexon
deletion) in a gene where LOF is a known mechanism of disease
'''
cls=line.split('\t')
funcs_tmp=["nonsense","frameshift","splic","stopgain"]
funcs_tmp2="nonframe"
funcs_tmp3="splic"
line_tmp=cls[Funcanno_flgs['Func.refGene']]+" "+cls[Funcanno_flgs['ExonicFunc.refGene']]
PVS=0
PVS_t1=0
PVS_t2=0
PVS_t3=0
dbscSNV_cutoff=0.6 #either score(ada and rf) >0.6 as splicealtering
# Funcanno_flgs={'Func.refGene':0,'ExonicFunc.refGene':0
for fc in funcs_tmp:
if line_tmp.find(fc)>=0 and line_tmp.find(funcs_tmp2)<0 :
PVS_t1=1
break
# wait to check LOF genes use the LoFtool_percentile,but how to know is the disese mechanism
try:
if lof_genes_dict[ cls[Funcanno_flgs['Gene']] ] == '1' :
PVS_t2=1
except KeyError:
PVS_t2=0
else:
pass
#print("PVSt1= %d PVSt2= %d" % (PVS_t1,PVS_t2) )
# begin check the site is really affect the splicing
try:
if float(cls[Funcanno_flgs['dbscSNV_RF_SCORE']])>dbscSNV_cutoff or float(cls[Funcanno_flgs['dbscSNV_ADA_SCORE']])>dbscSNV_cutoff:
PVS_t3=1
except ValueError:
pass
else:
pass
if PVS_t1 !=0 and PVS_t2 != 0 :
PVS=1
if line_tmp.find(funcs_tmp3)>=0 and PVS_t3 !=1:
PVS=0
#begin check it in the AAChange.knownGene for the major/Canonical isoform, not 1/last exon
#SUFU:uc001kvy.2:exon6:c.G716A:p.R239Q
line_tmp2=cls[Funcanno_flgs['AAChange.knownGene']]
#for cls0 in line_tmp2.split(','):
for cls0 in re.split("[,;]",line_tmp2):
cls0_1=cls0.split(':')
if len(cls0_1)>1:
trans_id=cls0_1[1]
exon=cls0_1[2]
try:
exon_lth="exon"+knownGeneCanonical_dict[trans_id]
#if exon==exon_lth or exon =="exon1": # not 1 or last exon
if exon==exon_lth: # relax for only last exon
PVS=0
try:
if (float(knownGeneCanonical_ed_dict[trans_id])-float( cls[Allels_flgs['Start']] ))<50: # means close 3' of gene 50 bp.
PVS=0
except ValueError:
pass
else:
pass
except KeyError:
pass
else:
pass
return(PVS)
def check_PS1(line,Funcanno_flgs,Allels_flgs,aa_changes_dict):
'''
PS1 Same amino acid change as a previously established pathogenic variant regardless of nucleotide change
Example: Val->Leu caused by either G>C or G>T in the same codon
AAChange.refGene
NOD2:NM_001293557:exon3:c.C2023T:p.R675W,NOD2:NM_022162:exon4:c.C2104T:p.R702W
'''
PS1=0
PS1_t1=0
PS1_t2=0
PS1_t3=0
dbscSNV_cutoff=0.6 #either score(ada and rf) >0.6 as splicealtering
cls=line.split('\t')
funcs_tmp=["missense","nonsynony"]
ACGTs=["A","C","G","T"]
line_tmp=cls[Funcanno_flgs['Func.refGene']]+" "+cls[Funcanno_flgs['ExonicFunc.refGene']]
for fc in funcs_tmp:
if line_tmp.find(fc)>=0 :
PS1_t1=1;
# need to wait to check Same amino acid change as a previously pathogenic variant
line_tmp2=cls[Funcanno_flgs['AAChange.refGene']]
#cls0=line_tmp2.split(',')
cls0=re.split("[,;]",line_tmp2)
cls0_1=cls0[0].split(':')
aa=cls0_1[4]
aa_last=aa[len(aa)-1:]
keys_tmp2=cls[Allels_flgs['Chr']]+"_"+cls[Allels_flgs['Start']]+"_"+cls[Allels_flgs['End']]+"_"+cls[Allels_flgs['Alt']]
try:
if aa_changes_dict[keys_tmp2]:
PS1_t2=0
except KeyError:
for nt in ACGTs:
if nt != cls[Allels_flgs['Alt']] and nt != cls[Allels_flgs['Ref']]:
keys_tmp3=cls[Allels_flgs['Chr']]+"_"+cls[Allels_flgs['Start']]+"_"+cls[Allels_flgs['End']]+"_"+nt
try:
if aa_changes_dict[keys_tmp3] == aa_last:
PS1_t2=1
except KeyError:
pass
else:
pass
else:
pass
try:
if float(cls[Funcanno_flgs['dbscSNV_RF_SCORE']])>dbscSNV_cutoff or float(cls[Funcanno_flgs['dbscSNV_ADA_SCORE']])>dbscSNV_cutoff: # means alter the splicing
PS1_t3=1
if cls[Funcanno_flgs['dbscSNV_RF_SCORE']] == "." or cls[Funcanno_flgs['dbscSNV_ADA_SCORE']] == ".": # absent also means not in splicing
PS1_t3=0
except ValueError:
pass
else:
pass
if PS1_t1 !=0 and PS1_t2 != 0 :
PS1=1
if PS1_t3 ==1: # remove the splicing affect
PS1=0
return(PS1)
def check_PS2(line,Funcanno_flgs,Allels_flgs):
'''
De novo (both maternity and paternity confirmed) in a patient with the disease and no family history
'''
PS2=0
return(PS2)
def check_PS3(line,Funcanno_flgs,Allels_flgs):