-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterVariants.py
More file actions
1534 lines (1386 loc) · 77.4 KB
/
FilterVariants.py
File metadata and controls
1534 lines (1386 loc) · 77.4 KB
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
'''
Created by Ryan O. Schenck
Date: 14 August 2018
Settings: rescomp1 python3.6 interpreter
Deployment: Mapping to /well/leedham/users/rschenck/AbbyMice/VariantCalls
Deploy on cluster: Tools -> Deployment -> Configuration (Set mapping in here)
'''
import sys
import os
import argparse
import logging
import glob
import subprocess
import gzip
import pysam
try:
from pyfiglet import Figlet
except:
pass
def VariantFilterBanner():
try:
print('\n')
f = Figlet(font='colossal')
print(f.renderText('Variant Filter'))
except:
pass
def Parser():
# get user variables
parser = argparse.ArgumentParser()
parser.add_argument("-i", dest="inDir", default='./', type=str, help="Input directory of vcf files with .passed.vcf extension, in the case of Strelka2 it expects the directory containing all information. Default: ./")
parser.add_argument("-c", dest="caller", default=1, type=int, help="Callers to be used, 1) MuTect2 2) Strelka2 3) Octopus 4) Shearwater ML; Default: 1) MuTect2")
parser.add_argument("-ievs", dest="ignoreLowEVS", default=False, action="store_true", help="Whether to ignore the LowEVS filter on Strelka2 output. Default=False. (Only applies for Strelka2 caller)")
parser.add_argument("-nr", dest="minnr", default=10, type=int, help="Minimum reads in normal. Default: 10")
parser.add_argument("-vr", dest="minvr", default=2, type=int, help="Minimum variant reads. Default: 2")
parser.add_argument("-vd", dest="varsamdepth", default=10, type=int, help="Total reads at variant site sum(ref,alt). Default=10")
parser.add_argument("-af", dest="minvaf", default=0.1, type=float, help="Minimum variant allele frequency. Default=0.1")
parser.add_argument("-e", dest="maxevents", default=1, type=int, help="Maximum number of events allowed at a loci. This ignores germline for example. Default=1") # TODO change code to handle germline variants and subsequent mutation. Works for MuTect currently.
parser.add_argument("-l", dest="clusterFlank", default=10, type=int, help="Flanking region to omit clustered events. Default: 10")
parser.add_argument("-t", dest="table", default=False, action="store_true", help="Flag to build an informative table. Useful for quick R processing without additional Bioconductor packages.")
parser.add_argument("-u", "--usesamplemap", dest="usesamplemap", default=False, action="store_true",help="Boolean indicating whether to use same sample filter options. Default: True, must specify --samplemap <file>.")
parser.add_argument("--samplemap", default='./nonenonenonenone?!', dest='samplemap', type=str, help="File with sample mapping in comma separated text file. Default: None, required if -u ")
parser.add_argument("-uvr", dest="uminvr", default=2, help="Minimum variant reads if -u setting is present. Default: 2")
parser.add_argument("-uaf", dest="uminvaf", default=0.01, type=float, help="Minimum variant allele frequency if -u setting is present. Default: 0.005")
parser.add_argument("-v", dest="verbose", default=True, action="store_false", help="Verbosity setting. Use -v to suppress some stdout.")
parser.add_argument("-a", dest="ann", default=False, action="store_true", help="Annotate using snpEff. This requires -ref and -j. Default=F")
parser.add_argument("-ref", dest="ref", default=None, type=str, help="Reference genome to be used for snpEff annotations (See http://snpeff.sourceforge.net/index.html). Default=None")
parser.add_argument("-j", dest="jarfile", default=None, type=str, help="snpEff.jar executable (See http://snpeff.sourceforge.net/index.html) Default=None")
parser.add_argument("-reffasta", dest="refgenome", default='/well/leedham/users/rschenck/References/Ensembl/GRCm38/Mus_musculus.GRCm38.dna.primary_assembly.fa', type=str, help="Reference genome that the variants are called with. Default=/well/leedham/users/rschenck/References/Ensembl/GRCm38/Mus_musculus.GRCm38.dna.primary_assembly.fa")
Options = parser.parse_args()
# Run options tests
if Options.inDir[-1] is not '/':
Options.inDir += '/'
if os.path.isdir(Options.inDir)==False:
logging.error("Unable to locate input directory. Please check -i option.")
sys.exit()
else:
logging.info("Input directory found: %s"%(Options.inDir))
if Options.usesamplemap and os.path.exists(Options.samplemap) == False:
logging.error("If --usesamplemap/-u is set then a sample map must be provided.")
sys.exit()
elif Options.usesamplemap==False:
logging.info("No sample map provided. Will not apply same specimen filters...")
else:
logging.info("Performing joint filtering.")
if Options.minvaf<0.0 or Options.minvaf>1. or Options.uminvaf<0.0 or Options.uminvaf>1.0:
logging.error("Variant allele frequency options (-af or -uaf) are outside acceptable ranges.")
sys.exit()
elif Options.verbose:
logging.info("Minimum variant reads: %s"%(Options.minvr))
logging.info("Minimum normal reads: %s"%(Options.minnr))
logging.info("Minimum variant allele frequency: %s"%(Options.minvaf))
logging.info("Minimum clustered events: %sbp"%(Options.clusterFlank))
if Options.usesamplemap:
logging.info("Minimum variant reads in at least one sample: %s"%(Options.uminvr))
logging.info("Minimum variant allele freq in at least one sample: %s"%(Options.uminvaf))
if Options.ann:
logging.info("Will attempt to perform annotations using snpEff using reference %s"%(Options.ref))
if Options.ann:
if os.path.exists(Options.jarfile)==False or Options.ref is None:
logging.error("If -a is specified a jarfile (-j) and reference (-ref) for snpEff must be used.")
sys.exit()
logging.info("Beginning...")
return(Options)
def SetupEnvironment():
'''
Setup logging function and print banner.
:return: None
'''
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s-%(levelname)s: %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
logging.info("Thank you for using Variant Filter. Created by Ryan Schenck...")
VariantFilterBanner()
class MuTect2Sample:
def __init__(self, vcf, Options, theFlags):
logging.info("Setting filter flags for %s..."%(os.path.basename(vcf)))
self.flagentries = theFlags
self.sampleidentifier = os.path.basename(vcf)
self.samplePath = os.path.dirname(vcf) + '/'
with open(vcf, 'r') as inFile:
self.passedlines = [line.replace('\n','') for line in inFile]
# Step 1: Read in muts and get tumor and normal column indices
self.normal_col = None
self.tumor_col = None
self.header,self.rawmuts = self._ParseFileAndBuildHeader(Options)
# Step 1a: Get positions for cluster events
self.chromsPos = PrepareClusteredEventCheck(self.rawmuts)
# Step 2: Construct flags for specific attributes to filter on.
self.flags = ['']*len(self.rawmuts)
# Step 3: Set flags for normal filtering (@,?,*,^)
self._normalcheck(Options)
# Step 4: Set flags for variant filtering (&,!,`)
self._variantcheck(Options)
if Options.usesamplemap:
self.jointsampleref = self._constructjointrefdict()
def _ParseFileAndBuildHeader(self, Options):
argparsevars = {'minnr': '-nr', 'minvr': '-vr', 'minvaf': '-af', 'clusterFlank': '-l', 'table': '-t',
'inDir': '-i', 'caller': '-c', 'usesamplemap': '--usesamplemap', 'samplemap': '--samplemap',
'uminvr': '-uvr', 'uminvaf': '-uaf', 'verbose': '-v', 'maxevents':'-e', 'varsamdepth':'-vd',
'ann':'-a', 'ref':'-ref','jarfile':'-jar', 'refgenome':'-reffasta', 'ignoreLowEVS':'-ievs'}
commandLine = 'FilterVariants.py ' + ' '.join([' '.join([argparsevars[arg], str(getattr(Options, arg))]) for arg in vars(Options)])
outline = '##FilterVariants=<ID=FilterVariants,CommandLine="%s">'%(commandLine)
newHeader = ''
rawMuts = []
for line in self.passedlines:
if line.startswith('#'):
if line.startswith('##FORMAT=<ID=SA_POST_PROB'):
newHeader+=outline+'\n'
elif line.startswith('##normal_sample'):
normcolumn = line.split('=')[1]
newHeader+=line+'\n'
elif line.startswith('##tumor_sample'):
tumorcolumn = line.split('=')[1]
newHeader+=line+'\n'
elif line.startswith('#CHROM'):
chromline = line.split('\t')
newHeader+=line+'\n'
else:
newHeader+=line+'\n'
else:
rawMuts.append(line)
# Determine and set the column number for normal and blood...since mutect randomly switches this shit! :(
self.normal_col = chromline.index(normcolumn)
self.tumor_col = chromline.index(tumorcolumn)
return(newHeader, rawMuts)
def _normalcheck(self, Options):
normalchecks = {'@':0,'?':0,'*':0,'^':0}
for i,mut in enumerate(self.rawmuts):
m = mut.split('\t')
mnorm = dict(zip(m[8].split(':'), m[self.normal_col].split(':')))
if int(mnorm['AD'].split(',')[1]) > 0:
self.flags[i] += '@'
normalchecks['@']+=1
if int(mnorm['AD'].split(',')[0]) < Options.minnr:
self.flags[i] += '*'
normalchecks['*']+=1
if min([float(af) for af in mnorm['AF'].split(',')]) > 0.05:
self.flags[i] += '?'
normalchecks['?']+=1
if len(m[4].split(','))>1:
self.flags[i] += '^'
normalchecks['^']+= 1
if Options.verbose:
logging.info('Normal sample check metrics...')
for item in normalchecks:
if normalchecks[item] > 0:
logging.info('%s: %s (%.1f%%)'%(self.flagentries[item], normalchecks[item], normalchecks[item]/float(len(self.rawmuts))*100))
def _variantcheck(self, Options):
variantchecks = {'&':0,'!':0,'`':0,';':0}
for i,mut in enumerate(self.rawmuts):
m = mut.split('\t')
mvar = dict(zip(m[8].split(':'), m[self.tumor_col].split(':')))
# check overall depth Options.varsamdepth
if sum([int(d) for d in mvar['AD'].split(',')]) < Options.varsamdepth:
self.flags[i] += '`'
variantchecks['`']+=1
# check variant read depth Options.minvr
if int(mvar['AD'].split(',')[1]) < Options.minvr and len(mvar['AD'].split(','))==2:
self.flags[i] += '&'
variantchecks['&'] += 1
elif len(mvar['AD'].split(','))>2:
checkmultipleEvents = [int(val) for val in mvar['AD'].split(',')[1:]]
if min(checkmultipleEvents) < Options.minvr:
self.flags[i] += '&' # TODO Need to check for one of these events (>1 event) being true with proper support later on...
variantchecks['&'] += 1
elif int(mvar['AD'].split(',')[1]) >= Options.minvr and len(mvar['AD'].split(','))==2:
pass
# check variant allele frequency Options.minvaf
if len(mvar['AD'].split(','))==2 and int(mvar['AD'].split(',')[1])/float(int(mvar['AD'].split(',')[0])+int(mvar['AD'].split(',')[1])) < Options.minvaf:
self.flags[i] += '!'
variantchecks['!'] += 1
elif len(mvar['AD'].split(','))>2:
checkmultipleEvents = [int(val) for val in mvar['AD'].split(',')[1:]]
totalReads = sum(checkmultipleEvents) + int(mvar['AD'].split(',')[0])
multipleVAFs = [val/float(totalReads) for val in checkmultipleEvents]
if min(multipleVAFs) < Options.minvaf:
self.flags[i] += '!'
variantchecks['!'] += 1
# check clustered event Options.clusterFlank
if ClusterMutCount(int(m[1]),self.chromsPos[m[0]],Options) > 0:
self.flags[i] += ';'
variantchecks[';'] += 1
if Options.verbose:
logging.info('Variant sample check metrics...')
for item in variantchecks:
if variantchecks[item] > 0:
logging.info('%s: %s (%.1f%%)'%(self.flagentries[item], variantchecks[item], variantchecks[item]/float(len(self.rawmuts))*100))
def _constructjointrefdict(self):
'''
Constructs a dictionary with the following information {mutpos: [index,flags]}
:return: a nested dictionary
'''
theRef = {}
for i,m in enumerate(self.rawmuts):
key = m.split('\t')[0] + ':' + m.split('\t')[1] + '_' + m.split('\t')[3] + '/' + m.split('\t')[4]
theRef.update({key:[i,self.flags[i]]})
return(theRef)
def _applyCNVcorrection(self):
#TODO once CNV calls have been made and implemented
pass
class MuTect2Filter:
def __init__(self, Options, inFiles, theFlags):
self.allMutectSamples = []
# Step 1: Perform individual sample based filtering
self._ApplyMutect2Filter(Options, inFiles, theFlags)
# Step 2: Perform joint based filtering
if Options.usesamplemap:
self._Mutect2JointFiltering(Options, inFiles[0], theFlags)
else:
pass
logging.info("Writing filtered vcf files...")
for sam in self.allMutectSamples:
WriteFilteredVCFFiles(sam.samplePath, sam.sampleidentifier, sam.header, sam.rawmuts, sam.flags)
if Options.ann:
logging.info("Annotating vcf files using snpEff...")
for sam in self.allMutectSamples:
AnnotateVCFFiles(sam.samplePath, sam.sampleidentifier, Options)
if Options.table:
self._BuildMutect2Table(Options)
def _ApplyMutect2Filter(self, Options, inFiles, theFlags):
if Options.usesamplemap: # with sample mapping
for theFile in inFiles[1]:
self.allMutectSamples.append(MuTect2Sample(theFile, Options, theFlags))
else: # no sample mapping
for theFile in inFiles:
self.allMutectSamples.append(MuTect2Sample(theFile, Options, theFlags))
def _Mutect2JointFiltering(self, Options, samplemap, theFlags):
logging.info('...')
logging.info('...')
logging.info("Performing joint filtering by updating filter flags...")
# Responsible for modifying the flags for each of the variant filtered sets for the final output.
for sam in samplemap:
if len(sam)>1:
sampleIDs = []
for indsam in sam:
for i, sample in enumerate(self.allMutectSamples):
if indsam in sample.sampleidentifier:
sampleIDs.append(i)
# Perform joint filter flag adjustments
#~~~~~ Get Candidate Sites ~~~~#
# logging.info("Gathering shared mutation loci within specimen...")
# Step 1: loop over each sample set of positions w/ mutations and find the matching ones
matchingPositions = {}
for theid in sampleIDs:
for pos in self.allMutectSamples[theid].jointsampleref:
try:
matchingPositions[pos] += 1
except KeyError:
matchingPositions.update({pos:1})
# Step 2: Trim to shared positions w/ muations to examine
candidateSites = []
for pos in matchingPositions:
if matchingPositions[pos] > 1:
candidateSites.append(pos)
for pos in candidateSites:
flagsets = []
for theid in sampleIDs:
try:
# Must do it this way to avoid errors later on. This means that >2 samples in a specimen and mutation shared between < total samples
a = theid
b = self.allMutectSamples[theid].sampleidentifier
c = self.allMutectSamples[theid].jointsampleref[pos]
d = self.allMutectSamples[theid].rawmuts[self.allMutectSamples[theid].jointsampleref[pos][0]]
flagsets.append(a) # id of the sample inside of self.allMutectSamples
flagsets.append(b) # Get Identifier
flagsets.append(c) # Get Index and Flagset
flagsets.append(d) # Get full mutation information
except KeyError:
pass
# Check if a flagset needs to be jointly filtered
checkcount = 0
flagspresent = []
for z in range(0,len(flagsets),4):
if flagsets[z+2][1]!='':
checkcount+=1
flagspresent.append(flagsets[z+2][1])
if checkcount > 0:
# Check if all flags are the same within the first (cond1 and cond2) or truly different...then adjust flags
if (len(flagspresent)==len(flagsets)/4 and len(list(set(flagspresent)))!=1) or len(flagspresent)==1:
if any(i in '.'.join(flagspresent) for i in ['!','&']):
# ~~~~ Adjust flags ~~~~#
allVafs = []
allnvr = []
for z in range(0, len(flagsets), 4):
m = flagsets[z+3].split('\t')
mvar = dict(zip(m[8].split(':'), m[self.allMutectSamples[flagsets[z]].tumor_col].split(':')))
allnvr.append(int(mvar['AD'].split(',')[1]))
totaldepth = float(sum([int(val) for val in mvar['AD'].split(',')]))
allVafs.append(int(mvar['AD'].split(',')[1])/totaldepth)
# Step 1: Adjust for variant supporting reads flags (&)
if all(vr >= Options.uminvr for vr in allnvr) and any(vr>= Options.minvr for vr in allnvr):
for z in range(0, len(flagsets), 4):
self.allMutectSamples[flagsets[z]].flags[flagsets[z+2][0]] = self.allMutectSamples[flagsets[z]].flags[flagsets[z+2][0]].replace('&','')
# Step 2: Adjust for variant allele frequencies flags (!)
if all(vaf >= Options.uminvaf for vaf in allVafs) and any(vaf >= Options.minvaf for vaf in allVafs):
for z in range(0, len(flagsets), 4):
self.allMutectSamples[flagsets[z]].flags[flagsets[z+2][0]] = self.allMutectSamples[flagsets[z]].flags[flagsets[z+2][0]].replace('!','')
else:
pass # Don't do any flag adjustments as none of the joint filtering options apply
logging.info("Filter flags have been updated...")
logging.info("Joint filtering completed...")
logging.info('...')
logging.info('...')
def _BuildMutect2Table(self, Options):
if Options.ann==False:
fullFail = ['@','*','&','!','^','`',';'] # All except '?'
allMuts = []
for sam in self.allMutectSamples:
with open(sam.samplePath + sam.sampleidentifier.replace("passed.vcf","passed.variantfilter.vcf"), 'r') as inFile:
lines = inFile.readlines()
for line in lines:
if line.startswith('#')==False:
allMuts.append(line)
trinucs = GetTrinucs(allMuts, Options)
outTableName = self.allMutectSamples[0].samplePath + 'Mutect2.passed.variantfilter.digest.txt'
header = "sample\tchr\tpos\tref_nt\tref_nt_pred\tmut_nt\treads\treads_fw\treads_rv\tdepth\tvaf\ttrinuc\tgene_name\timpact\tprotein_change\tnt_change\tprotein_id\n"
outLines = []
for sam in self.allMutectSamples:
sample = sam.sampleidentifier.replace('.passed.vcf','')
for i, flagIDs in enumerate(sam.flags):
if any(f in flagIDs for f in fullFail) == False:
m = sam.rawmuts[i].split('\t') # Get the mutation
# Check for multiple events
if len(m[4].split(',')) > 1:
for z in range(0,len(m[4].split(','))):
chr = m[0]
pos = m[1]
ref_nt = m[3]
ref_nt_pred = m[3]
mut_nt = m[4].split(',')[z]
mvar = dict(zip(m[8].split(':'), m[sam.tumor_col].split(':')))
reads = int(mvar['AD'].split(',')[z+1])
reads_fw = int(mvar['F1R2'].split(',')[z+1])
reads_rv = int(mvar['F2R1'].split(',')[z+1])
depth = sum([int(reads) for reads in mvar['AD'].split(',')])
vaf = reads / float(depth)
if len(mut_nt) == 1 and len(ref_nt) == 1:
try:
trinuc = trinucs[str(chr) + ':' + str(pos)]
except KeyError:
trinuc = '.'
logging.warning("Unable to locate trinucleotide context...")
else:
trinuc = '.'
gene_name = '.'
impact = '.'
protein_change = '.'
nt_change = '.'
protein_id = '.'
l = [sample, chr, pos, ref_nt, ref_nt_pred, mut_nt, reads, reads_fw, reads_rv, depth,
vaf, trinuc, gene_name, impact, protein_change, nt_change, protein_id]
strLine = [str(item) for item in l]
theLine = '\t'.join(strLine)
outLines.append(theLine)
else:
chr = m[0]
pos = m[1]
ref_nt = m[3]
ref_nt_pred = m[3]
mut_nt = m[4]
mvar = dict(zip(m[8].split(':'), m[sam.tumor_col].split(':')))
reads = int(mvar['AD'].split(',')[1])
reads_fw = int(mvar['F1R2'].split(',')[1])
reads_rv = int(mvar['F2R1'].split(',')[1])
depth = sum([int(reads) for reads in mvar['AD'].split(',')])
vaf = reads/float(depth)
if len(mut_nt)==1 and len(ref_nt)==1:
try:
trinuc = trinucs[str(chr)+':'+str(pos)]
except KeyError:
trinuc = '.'
logging.warning("Unable to locate trinucleotide context...")
else:
trinuc = '.'
gene_name = '.'
impact = '.'
protein_change = '.'
nt_change = '.'
protein_id = '.'
l = [sample, chr, pos, ref_nt, ref_nt_pred, mut_nt, reads, reads_fw, reads_rv, depth,
vaf, trinuc, gene_name, impact, protein_change, nt_change, protein_id]
strLine = [str(item) for item in l]
theLine = '\t'.join(strLine)
outLines.append(theLine)
with open(outTableName, 'w') as outFile:
outFile.write(header)
outFile.write('\n'.join(outLines) + '\n')
else: # If annotations are done...
# Get trinucleotide context information
allMuts = []
for sam in self.allMutectSamples:
with open(sam.samplePath + sam.sampleidentifier.replace("passed.vcf", "passed.variantfilter.ann.vcf"),
'r') as inFile:
lines = inFile.readlines()
for line in lines:
if line.startswith('#') == False:
allMuts.append(line)
trinucs = GetTrinucs(allMuts, Options)
annMuts = {}
for sam in self.allMutectSamples:
with open(sam.samplePath + sam.sampleidentifier.replace("passed.vcf","passed.variantfilter.ann.vcf"), 'r') as inFile:
lines = [line.replace('\n','') for line in inFile.readlines()]
allLines = []
for line in lines:
if line.startswith('#') == False:
allLines.append(line)
annMuts.update({sam.sampleidentifier:allLines})
outTableName = self.allMutectSamples[0].samplePath + 'Mutect2.passed.variantfilter.ann.digest.txt'
header = "sample\tchr\tpos\tref_nt\tref_nt_pred\tmut_nt\treads\treads_fw\treads_rv\tdepth\tvaf\ttrinuc\tgene_name\timpact\tprotein_change\tnt_change\ttranscript_id\tgene_id\n"
outLines = []
for sam in self.allMutectSamples:
sample = sam.sampleidentifier.replace('.passed.vcf','')
muts = annMuts[sam.sampleidentifier]
for mut in muts:
m = mut.split('\t') # Get the mutation
info = {}
for item in m[7].split(';'):
infopair = item.split('=')
if len(infopair) != 2:
info.update({infopair[0]:'.'})
else:
info.update({infopair[0]:infopair[1]})
info = info['ANN'].split(',')[0]
# Check for multiple events
if len(m[4].split(',')) > 1:
for z in range(0, len(m[4].split(','))):
chr = m[0]
pos = m[1]
ref_nt = m[3]
ref_nt_pred = m[3]
mut_nt = m[4].split(',')[z]
mvar = dict(zip(m[8].split(':'), m[sam.tumor_col].split(':')))
reads = int(mvar['AD'].split(',')[z + 1])
reads_fw = int(mvar['F1R2'].split(',')[z + 1])
reads_rv = int(mvar['F2R1'].split(',')[z + 1])
depth = sum([int(reads) for reads in mvar['AD'].split(',')])
vaf = reads / float(depth)
if len(mut_nt) == 1 and len(ref_nt) == 1:
try:
trinuc = trinucs[str(chr) + ':' + str(pos)]
except KeyError:
trinuc = '.'
logging.warning("Unable to locate trinucleotide context...")
else:
trinuc = '.'
gene_name = info.split('|')[3]
impact = info.split('|')[1].split('&')[0]
protein_change = info.split('|')[10]
nt_change = info.split('|')[9]
transcript_id = info.split('|')[6]
gene_id = info.split('|')[4]
l = [sample, chr, pos, ref_nt, ref_nt_pred, mut_nt, reads, reads_fw, reads_rv, depth,
vaf, trinuc, gene_name, impact, protein_change, nt_change, transcript_id,gene_id]
strLine = [str(item) for item in l]
theLine = '\t'.join(strLine)
outLines.append(theLine)
else:
chr = m[0]
pos = m[1]
ref_nt = m[3]
ref_nt_pred = m[3]
mut_nt = m[4]
mvar = dict(zip(m[8].split(':'), m[sam.tumor_col].split(':')))
reads = int(mvar['AD'].split(',')[1])
reads_fw = int(mvar['F1R2'].split(',')[1])
reads_rv = int(mvar['F2R1'].split(',')[1])
depth = sum([int(reads) for reads in mvar['AD'].split(',')])
vaf = reads / float(depth)
if len(mut_nt) == 1 and len(ref_nt) == 1:
try:
trinuc = trinucs[str(chr) + ':' + str(pos)]
except KeyError:
trinuc = '.'
logging.warning("Unable to locate trinucleotide context...")
else:
trinuc = '.'
gene_name = info.split('|')[3]
impact = info.split('|')[1].split('&')[0]
protein_change = info.split('|')[10]
nt_change = info.split('|')[9]
transcript_id = info.split('|')[6]
gene_id = info.split('|')[4]
l = [sample, chr, pos, ref_nt, ref_nt_pred, mut_nt, reads, reads_fw, reads_rv, depth,
vaf, trinuc, gene_name, impact, protein_change, nt_change, transcript_id, gene_id]
strLine = [str(item) for item in l]
theLine = '\t'.join(strLine)
outLines.append(theLine)
with open(outTableName, 'w') as outFile:
outFile.write(header)
outFile.write('\n'.join(outLines) + '\n')
class Strelka2Sample:
def __init__(self, Options, theFlags, SamNam, germDir, somDir):
self.SamNam = SamNam
self.GermlineDir = germDir
self.somDir = somDir
self.bamFile = None
self.rawGermMuts = []
self.filteredGermMuts = []
self.rawGermMutHead = []
self.rawGermLineFlags = []
self.rawSNVS = []
self.rawSNVSHead = []
self.SNVSHeadout = ''
self.rawSNVSflags = []
self.rawIndels = []
self.rawIndelsHead = []
self.rawIndelsflags = []
self.rawPassIndels = []
# Step 1: Read in all corresponding files for GermMuts and Somatic SNVs
self._readFiles()
self._newHeader(Options)
# Step 1a: Get positions for cluster events
self.chromsPos = PrepareClusteredEventCheck(self.rawSNVS)
self.chromsPosGerm = PrepareClusteredEventCheck(self.rawGermMuts)
# Step 2: Perform Filtering
logging.info("Filtering calls for %s..." % (self.SamNam))
logging.info("GERMLINE filtering...")
self._filterRawGermlineVariant(Options, theFlags)
logging.info("SOMATIC filtering...")
self._filterRawSomaticSNVs(Options, theFlags)
self._filterRawIndelSNVs(Options, theFlags)
if Options.usesamplemap:
self.germjointsampleref = self._constructjointrefdictGerm()
self.snvjointsampleref = self._constructjointrefdictSomatic()
def _readFiles(self):
germFile = self.GermlineDir + '/results/variants/variants.vcf.gz'
somSNVS = self.somDir + '/results/variants/somatic.snvs.vcf.gz'
somINDELS = self.somDir + '/results/variants/somatic.indels.vcf.gz'
logging.info("Reading somatic SNVs...")
with gzip.open(somSNVS, 'rb') as inSom:
SomLines = [line.decode('UTF-8').replace('\n', '') for line in inSom.readlines()]
for line in SomLines:
if line.startswith('#'):
self.rawSNVSHead.append(line)
else:
self.rawSNVS.append(line)
logging.info("Reading somatic Indels...")
with gzip.open(somINDELS, 'rb') as inSomIndels:
SomLinesIndels = [line.decode('UTF-8').replace('\n', '') for line in inSomIndels.readlines()]
for line in SomLinesIndels:
if line.startswith('#'):
self.rawIndelsHead.append(line)
else:
self.rawIndels.append(line)
logging.info("Reading germline variant calls...")
with gzip.open(germFile, 'rb') as inGerm:
GermLine = [line.decode('UTF-8').replace('\n', '') for line in inGerm.readlines()]
for line in GermLine:
if line.startswith('#'):
self.rawGermMutHead.append(line)
else:
if line.split('\t')[6] == "PASS":
self.rawGermMuts.append(line)
def _newHeader(self, Options):
argparsevars = {'minnr': '-nr', 'minvr': '-vr', 'minvaf': '-af', 'clusterFlank': '-l', 'table': '-t',
'inDir': '-i', 'caller': '-c', 'usesamplemap': '--usesamplemap', 'samplemap': '--samplemap',
'uminvr': '-uvr', 'uminvaf': '-uaf', 'verbose': '-v', 'maxevents': '-e', 'varsamdepth': '-vd',
'ann': '-a', 'ref': '-ref', 'jarfile': '-jar', 'refgenome': '-reffasta',
'ignoreLowEVS': '-ievs'}
commandLine = 'FilterVariants.py ' + ' '.join(
[' '.join([argparsevars[arg], str(getattr(Options, arg))]) for arg in vars(Options)])
outline = '##FilterVariants=<ID=FilterVariants,CommandLine="%s">' % (commandLine)
newHeader = ''
for line in self.rawSNVSHead:
if line.startswith('#'):
if line.startswith('##reference='):
newHeader+=outline+'\n'
newHeader += line + '\n'
elif line.startswith("##cmdline="):
self.bamFile = line.split("--tumorBam=")[1].split(" ")[0]
newHeader += line + '\n'
else:
newHeader+=line+'\n'
self.SNVSHeadout = newHeader
def _filterRawGermlineVariant(self, Options, theFlags):
vafs = []
candidates = 0
checks = {'^':0, '`':0, '&':0, '!':0, ';':0}
self.rawGermLineFlags = ['' for i in range(0,len(self.rawGermMuts))]
for i, line in enumerate(self.rawGermMuts):
# if 'PASS' == line.split('\t')[6]:
line = line.split('\t')
# Filter multiple events at same loci
if len(line[4].split(','))>1:
self.rawGermLineFlags[i] += '^'
checks['^'] += 1
field = line[9].split(':')
# Filter depth less than
if int(field[3])<Options.minnr:
self.rawGermLineFlags[i] += '`'
checks['`'] += 1
# Filter Variant Depth
if int(field[5].split(',')[1]) < 2:
self.rawGermLineFlags[i] += '&'
checks['&'] += 1
elif int(field[6].split(',')[1]) < 2 or int(field[6].split(',')[1]) < 2:
self.rawGermLineFlags[i] += '&'
checks['&'] += 1
else:
pass
if int(field[3]) > 0:
if int(field[5].split(',')[1])/float(field[3]) < 0.1:
self.rawGermLineFlags[i] += '!'
checks['!'] += 1
# check clustered event Options.clusterFlank
if ClusterMutCount(int(line[1]), self.chromsPosGerm[line[0]], Options) > 0:
self.rawGermLineFlags[i] += ';'
checks[';'] += 1
# Total candidates
if self.rawGermLineFlags[i]=='':
candidates += 1
vaf = int(field[5].split(',')[1])/float(field[3])
vafs.append(vaf)
self.filteredGermMuts.append('\t'.join(line))
if Options.verbose:
for item in checks:
if checks[item] > 0:
logging.info('%s: %s (%.1f%%)'%(theFlags[item], checks[item], checks[item]/float(len(self.rawGermMuts))*100))
logging.info('Mean VAF: %.2f ; Min VAF: %.2f ; Max VAF: %.2f'%( sum(vafs)/float(len(vafs)) , min(vafs), max(vafs)))
logging.info("Total candidate germline mutations: %s"%(candidates))
def _filterRawSomaticSNVs(self, Options, theFlags):
vafs = []
candidates = 0
checks = {'^': 0, '`': 0, '&': 0, '!': 0, '*':0, '@':0, ';':0}
base = {'A':'AU','C':'CU','G':'GU','T':'TU'}
self.rawSNVSflags = ['' for i in range(0, len(self.rawSNVS))]
keys = self.rawSNVSHead[len(self.rawSNVSHead)-1].split('\t')
for i, line in enumerate(self.rawSNVS):
line = line.split('\t')
lineDat = dict(zip(keys, line))
alt = lineDat['ALT']
ref = lineDat['REF']
# Filter multiple events at same loci
if len(lineDat['ALT'].split(',')) > Options.maxevents:
self.rawSNVSflags[i] += '^'
checks['^'] += 1
formatKey = lineDat['FORMAT'].split(':')
normVals = dict(zip(formatKey, lineDat['NORMAL'].split(':')))
tumorVals = dict(zip(formatKey, lineDat['TUMOR'].split(':')))
# Filter depth less than opt in normal
if int(normVals['DP']) < Options.minnr:
self.rawSNVSflags[i] += '*'
checks['*'] += 1
# Filter variant reads in normal check
if sum([int(t) for t in normVals[base[alt]].split(',')]) > 0:
self.rawSNVSflags[i] += '@'
checks['@'] += 1
# Filter Tumor Total Depth
if int(tumorVals['DP']) < Options.varsamdepth:
self.rawSNVSflags[i] += '`'
checks['`'] += 1
# Filter Variant Reads Depth
if int(tumorVals[base[alt]].split(',')[1]) < Options.minvr:
self.rawSNVSflags[i] += '&'
checks['&'] += 1
# check clustered event Options.clusterFlank
if ClusterMutCount(int(lineDat['POS']), self.chromsPos[lineDat['#CHROM']], Options) > 0:
self.rawSNVSflags[i] += ';'
checks[';'] += 1
# Filter for VAF check
if float(tumorVals['DP']) > 0:
if int(tumorVals[base[alt]].split(',')[0])/float(int(tumorVals['DP'])-int(tumorVals['FDP'])) < Options.minvaf:
self.rawSNVSflags[i] += '!'
checks['!'] += 1
# Total candidates
if self.rawSNVSflags[i] == '':
candidates += 1
vaf = int(tumorVals[base[alt]].split(',')[0])/float(int(tumorVals['DP'])-int(tumorVals['FDP']))
if vaf>1.0:
print(vaf)
print(tumorVals)
print(line)
print(self.rawSNVSflags[i])
logging.error("Appears to be a problem with FilterVariant.py...see source code or use a different method.")
sys.exit()
vafs.append(vaf)
if Options.verbose:
for item in checks:
if checks[item] > 0:
logging.info(
'%s: %s (%.1f%%)' % (theFlags[item], checks[item], checks[item] / float(len(self.rawSNVS)) * 100))
logging.info('Mean VAF: %.2f ; Min VAF: %.2f ; Max VAF: %.2f' % (
sum(vafs) / float(len(vafs)), min(vafs), max(vafs)))
logging.info("Total candidate somatic SNVs: %s" % (candidates))
def _filterRawIndelSNVs(self, Options, theFlags):
vafs = []
candidates = 0
checks = {'^': 0, '`': 0, '&': 0, '!': 0, '*':0, '@':0, ';':0}
base = {'A':'AU','C':'CU','G':'GU','T':'TU'}
self.rawIndelsflags = ['' for i in range(0, len(self.rawIndels))]
keys = self.rawIndelsHead[len(self.rawIndelsHead)-1].split('\t')
for i, line in enumerate(self.rawIndels):
if line.split('\t')[6] == "PASS":
self.rawPassIndels.append(line)
#TODO implement way of filtering Strelka2 Indels...
def _constructjointrefdictGerm(self):
'''
Constructs a dictionary with the following information {mutpos: [index,flags]}
:return: a nested dictionary
'''
theRef = {}
for i,m in enumerate(self.filteredGermMuts):
key = m.split('\t')[0] + ':' + m.split('\t')[1] + '_' + m.split('\t')[3] + '/' + m.split('\t')[4]
theRef.update({key:[i,'']})
return(theRef)
def _constructjointrefdictSomatic(self):
'''
Constructs a dictionary with the following information {mutpos: [index,flags]}
:return: a nested dictionary
'''
theRef = {}
for i,m in enumerate(self.rawSNVS):
key = m.split('\t')[0] + ':' + m.split('\t')[1] + '_' + m.split('\t')[3] + '/' + m.split('\t')[4]
theRef.update({key:[i,self.rawSNVSflags[i]]})
return(theRef)
class Strelka2Main:
def __init__(self, Options, theFlags):
self.samplemap = []
self.germlineDirs = {}
self.somaticDirs = {}
self.theSamples = []
self.allSampleClasses = []
self.germlineFilters = {}
if Options.usesamplemap == True:
self._sampleMapping(Options)
# Step 1: Find all of the samples and set the above three items
self._getSamples(Options)
# Step 2: Read in all of the information for the files and setup classes
self._setupSample(Options, theFlags)
# Step 3: Perform Further Germline filtering...
self._germlineFilterPart2(Options, theFlags)
# Step 4: Append flags with Germline Evidence Flag...
self._AddGermLineFlag(Options, theFlags)
# Step 5: Update flags based on joint filtering...
if Options.usesamplemap:
self._PerformJointFiltering(Options, theFlags)
else:
pass
# Step 6: Build files with headers....
self._Strelka2WriteVCFFile(Options)
if Options.ann:
logging.info("Annotating vcf files using snpEff...")
for sam in self.allSampleClasses:
self._strelka2_AnnotateVCFFiles(Options.inDir, sam.SamNam, Options)
if Options.table:
self._BuildStrelka2Table(Options)
def _sampleMapping(self, Options):
with open(Options.samplemap, 'r') as samMap:
self.samplemap = [sam.replace('\n', '').split(',') for sam in samMap.readlines()]
def _getSamples(self, Options):
'''
Sets up the class variables for which directories house which files
:param Options: Execution options.
:return: None.
'''
logging.info("Searching for files...")
indirs = glob.glob(Options.inDir + '*')
sams = []
for item in indirs:
if 'germline' in item or 'somatic' in item:
f = os.path.basename(item).split('.')
sams.append(f[0])
if 'germline' in item:
self.germlineDirs.update({f[0]: item})
elif 'somatic' in item:
self.somaticDirs.update({f[0]: item})
else:
logging.error("Unable to determine what to do with file %s"%(item))
self.theSamples = list(set(sams))
def _setupSample(self, Options, theFlags):
for sam in self.theSamples:
self.allSampleClasses.append(Strelka2Sample(Options, theFlags, sam, self.germlineDirs[sam], self.somaticDirs[sam]))
def _germlineFilterPart2(self, Options, theFlags):
if Options.usesamplemap:
logging.info('...')
logging.info('...')
logging.info("Performing joint filtering on germline variants by updating filter flags...")
for sam in self.samplemap:
if len(sam) > 1:
sampleIDs = []
for indsam in sam:
for i, sample in enumerate(self.allSampleClasses):
if indsam in sample.SamNam:
sampleIDs.append(i)
# The mutation must be present in all samples for a germline filter to be applied...
# ~~~~~ Get Candidate Sites ~~~~#
logging.info("Gathering shared germline variant loci within specimen...")
# Step 1: loop over each sample set of positions w/ mutations and find the matching ones
matchingPositions = {}
for theid in sampleIDs:
for pos in self.allSampleClasses[theid].germjointsampleref:
try:
matchingPositions[pos] += 1
except KeyError:
matchingPositions.update({pos: 1})
# Step 2: Trim to shared positions w/ mutations to examine
candidateSites = []
for pos in matchingPositions:
if matchingPositions[pos] == len(sam):
candidateSites.append(pos)
logging.info("A total of %s candidate positions for joint filtering on %s..."%(len(candidateSites),','.join(sam)))
finalGermlinePos = []
for pos in candidateSites:
flagsets = []
for theid in sampleIDs:
try:
# Must do it this way to avoid errors later on. This means that >2 samples in a specimen and mutation shared between < total samples
a = theid
b = self.allSampleClasses[theid].SamNam
c = self.allSampleClasses[theid].germjointsampleref[pos]
d = self.allSampleClasses[theid].filteredGermMuts[self.allSampleClasses[theid].germjointsampleref[pos][0]]
flagsets.append(a) # id of the sample inside of self.allMutectSamples
flagsets.append(b) # Get Identifier
flagsets.append(c) # Get Index and Flagset
flagsets.append(d) # Get full mutation information
except KeyError:
pass
gpos = []
for z in range(0,len(flagsets),4):
indpos = '\t'.join(flagsets[z+3].split('\t')[0:5])
gpos.append(indpos)
gpos = list(set(gpos))
if len(gpos)==1:
finalGermlinePos.append(gpos[0])
else:
logging.error("GERMLINE MUTATIONS NOT UNIQUE. DO NOT TRUST RESULTS...")
sys.exit()
for theSam in sam:
self.germlineFilters.update({theSam:finalGermlinePos}) # Holds the final germline filters to be applied...
else: # No paired for joint filtering
# TODO need to look at this section in greater detail when mixed matched and unmatched samples
finalGermlinePos = []
for i, sample in enumerate(self.allSampleClasses):
if sam[0] in sample.SamNam:
for m in self.allSampleClasses[i].filteredGermMuts:
finalGermlinePos.append('\t'.join(m.split('\t')[0:5]))
self.germlineFilters.update({sam[0]: finalGermlinePos})
else:
# Use all germline filters without flags, no need to further filter these.
logging.info('...')
logging.info('...')
logging.info("Preparing germline variant extraction for filtering...")
for i in range(0,len(self.allSampleClasses)):
finalGermlinePos = []
for m in self.allSampleClasses[i].filteredGermMuts:
finalGermlinePos.append('\t'.join(m.split('\t')[0:5]))
self.germlineFilters.update({self.allSampleClasses[i].filteredGermMuts.SamNam:finalGermlinePos})
def _AddGermLineFlag(self, Options, theFlags):
# Updates the flags with variant called as germline flag
logging.info("Appending variant flags with germline flag...")
for i in range(0,len(self.allSampleClasses)):
count = 0
for z,m in enumerate(self.allSampleClasses[i].rawSNVS):
cond = '\t'.join(m.split('\t')[0:5])
if cond in self.germlineFilters[self.allSampleClasses[i].SamNam]:
count += 1
self.allSampleClasses[i].rawSNVSflags[z]+="G"
logging.info("%s variants with germline evidence in %s..."%(count, self.allSampleClasses[i].SamNam))
def _PerformJointFiltering(self, Options, theFlags):
base = {'A': 'AU', 'C': 'CU', 'G': 'GU', 'T': 'TU'}
logging.info('...')
logging.info('...')
logging.info("Performing joint filtering by updating filter flags...")
count = 0
# Responsible for modifying the flags for each of the variant filtered sets for the final output.
for sam in self.samplemap:
if len(sam) > 1:
sampleIDs = []
for indsam in sam:
for i, sample in enumerate(self.allSampleClasses):
if indsam in sample.SamNam:
sampleIDs.append(i)
# Perform joint filter flag adjustments