This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
mantaWorkflow.py
849 lines (625 loc) · 33 KB
/
mantaWorkflow.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
#
# Manta - Structural Variant and Indel Caller
# Copyright (c) 2013-2019 Illumina, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
"""
Manta SV discovery workflow
"""
import os.path
import sys
import shutil
# add script path to pull in utils in same directory:
scriptDir=os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.abspath(scriptDir))
# add pyflow path:
pyflowDir=os.path.join(scriptDir,"pyflow")
sys.path.append(os.path.abspath(pyflowDir))
from checkChromSet import getTabixChromSet
from configBuildTimeInfo import workflowVersion
from configureUtil import safeSetBool
from pyflow import WorkflowRunner
from sharedWorkflow import getMkdirCmd, getMvCmd, getRmCmd, getRmdirCmd, \
getDepthFromAlignments
from workflowUtil import checkFile, ensureDir, preJoin, \
getGenomeSegmentGroups, getNextGenomeSegment, getFastaChromOrderSize, \
cleanPyEnv
__version__ = workflowVersion
def getOverlapCallRegions(params, genomeRegion) :
"""
determine a set of overlapping regions between regions arguments and the callRegions bed file
\return a list of overlapping genomic regions for calling
"""
import subprocess
subregions = []
chrom = genomeRegion["chrom"]
genomeRegionStart = genomeRegion["start"]
genomeRegionEnd = genomeRegion["end"]
genomeRegionStr = ("%s:%s-%s" % (chrom, genomeRegionStart, genomeRegionEnd))
# get overlapping subregions
tabixCmd = [params.tabixBin, params.callRegionsBed, genomeRegionStr]
proc = subprocess.Popen(tabixCmd, stdout=subprocess.PIPE)
for line in proc.stdout:
# format check of the bed file
words = line.strip().split()
if len(words) < 3 :
raise Exception("Unexpected format in bed file: %s\n%s" %
(params.callRegionsBed, line))
callRegionStart = int(words[1])+1
callRegionEnd = int(words[2])
if callRegionEnd < callRegionStart :
raise Exception("Unexpected format in bed file: %s\n%s" %
(params.callRegionsBed, line))
subregionStart = max(genomeRegionStart, callRegionStart)
subregionEnd = min(genomeRegionEnd, callRegionEnd)
subregions.append({"chrom": chrom, "start":subregionStart, "end":subregionEnd})
proc.stdout.close()
proc.wait()
return subregions
def getCallRegions(params) :
"""
determine
1) a set of genomic regions for calling
2) a set of chromosomes that are completely skipped over,
where "skipped" means that not a single base on the chrom is requested for calling
\return a list of genomic regions for calling
\return a set of chromLabels which are skipped
"""
callRegionList = []
chromIsSkipped = set()
# when no region selections have been made:
if ((params.genomeRegionList is None) and
(params.callRegionsBed is None)) :
return (callRegionList, chromIsSkipped)
# check chromosome coverage of "regions" arguments
chromIsSkipped = set(params.chromOrder)
if params.genomeRegionList is not None :
for genomeRegion in params.genomeRegionList :
chrom = genomeRegion["chrom"]
if chrom not in params.chromOrder:
raise Exception("Unexpected chromosome '%s' in the argument of target regions (--region)" %
(chrom))
if chrom in chromIsSkipped :
chromIsSkipped.remove(chrom)
if params.callRegionsBed is None :
return (params.genomeRegionList, chromIsSkipped)
# check chromsome coverage based on callRegions BED file
callChromList = []
chromIsSkipped2 = set(params.chromOrder)
for chrom in getTabixChromSet(params.tabixBin, params.callRegionsBed) :
if chrom not in params.chromOrder:
raise Exception("Unexpected chromosome '%s' in the bed file of call regions %s " %
(chrom, params.callRegionsBed))
callChromList.append(chrom)
if chrom in chromIsSkipped2 :
chromIsSkipped2.remove(chrom)
if params.genomeRegionList is None :
chromIsSkipped = chromIsSkipped2
for chrom in callChromList:
chromRegion = {"chrom":chrom, "start":1, "end":params.chromSizes[chrom]}
callRegions = getOverlapCallRegions(params, chromRegion)
callRegionList.extend(callRegions)
else:
chromIsSkipped = chromIsSkipped | chromIsSkipped2
for genomeRegion in params.genomeRegionList:
chrom = genomeRegion['chrom']
if genomeRegion["start"] is None:
genomeRegion["start"] = 1
if genomeRegion["end"] is None:
genomeRegion["end"] = params.chromSizes[chrom]
subCallRegions = getOverlapCallRegions(params, genomeRegion)
callRegionList.extend(subCallRegions)
return (callRegionList, chromIsSkipped)
def summarizeStats(self, taskPrefix="", dependencies=None) :
statsPath=self.paths.getStatsPath()
summaryTasks = set()
# summarize stats in format that's easier for human review
cmd = [self.params.mantaStatsSummaryBin]
cmd.extend(["--align-stats ", statsPath])
cmd.extend(["--output-file", self.paths.getStatsSummaryPath()])
summarizeTask = self.addTask(preJoin(taskPrefix,"summarizeStats"),cmd,dependencies=dependencies, isForceLocal=True)
summaryTasks.add(summarizeTask)
return summaryTasks
def runStats(self,taskPrefix="",dependencies=None) :
statsPath=self.paths.getStatsPath()
statsFilename=os.path.basename(statsPath)
tmpStatsDir=statsPath+".tmpdir"
makeTmpStatsDirCmd = getMkdirCmd() + [tmpStatsDir]
dirTask=self.addTask(preJoin(taskPrefix,"makeTmpDir"), makeTmpStatsDirCmd, dependencies=dependencies, isForceLocal=True)
tmpStatsFiles = []
statsTasks = set()
for (bamIndex,bamPath) in enumerate(self.params.normalBamList + self.params.tumorBamList) :
indexStr = str(bamIndex).zfill(3)
tmpStatsFiles.append(os.path.join(tmpStatsDir,statsFilename+"."+ indexStr +".xml"))
cmd = [ self.params.mantaStatsBin ]
cmd.extend(["--ref", self.params.referenceFasta])
cmd.extend(["--output-file",tmpStatsFiles[-1]])
cmd.extend(["--align-file",bamPath])
if self.params.defaultAlignStatsFile:
cmd.extend(["--default-stats-file",self.params.defaultAlignStatsFile])
statsTasks.add(self.addTask(preJoin(taskPrefix,"generateStats_"+indexStr),cmd,dependencies=dirTask))
cmd = [ self.params.mantaMergeStatsBin ]
cmd.extend(["--output-file",statsPath])
for tmpStatsFile in tmpStatsFiles :
cmd.extend(["--align-stats-file",tmpStatsFile])
mergeTask = self.addTask(preJoin(taskPrefix,"mergeStats"),cmd,dependencies=statsTasks,isForceLocal=True)
nextStepWait = set()
nextStepWait.add(mergeTask)
if not self.params.isRetainTempFiles :
rmStatsTmpCmd = getRmdirCmd() + [tmpStatsDir]
rmTask=self.addTask(preJoin(taskPrefix,"removeTmpDir"),rmStatsTmpCmd,dependencies=mergeTask, isForceLocal=True)
return nextStepWait
def mantaGetDepthFromAlignments(self,taskPrefix="getChromDepth",dependencies=None):
bamList=[]
if len(self.params.normalBamList) :
bamList = self.params.normalBamList
elif len(self.params.tumorBamList) :
bamList = self.params.tumorBamList
else :
return set()
outputPath=self.paths.getChromDepth()
return getDepthFromAlignments(self, bamList, outputPath, taskPrefix, dependencies)
def runLocusGraph(self,taskPrefix="",dependencies=None):
"""
Create the full SV locus graph
"""
statsPath=self.paths.getStatsPath()
graphPath=self.paths.getGraphPath()
graphStatsPath=self.paths.getGraphStatsPath()
tmpGraphDir=self.paths.getTmpGraphDir()
makeTmpGraphDirCmd = getMkdirCmd() + [tmpGraphDir]
dirTask = self.addTask(preJoin(taskPrefix,"makeGraphTmpDir"), makeTmpGraphDirCmd, dependencies=dependencies, isForceLocal=True)
tmpGraphFiles = []
graphTasks = set()
for gsegGroup in getGenomeSegmentGroups(getNextGenomeSegment(self.params)) :
assert(len(gsegGroup) != 0)
gid=gsegGroup[0].id
if len(gsegGroup) > 1 :
gid += "_to_"+gsegGroup[-1].id
tmpGraphFiles.append(self.paths.getTmpGraphFile(gid))
graphCmd = [ self.params.mantaGraphBin ]
graphCmd.extend(["--output-file", tmpGraphFiles[-1]])
graphCmd.extend(["--align-stats",statsPath])
for gseg in gsegGroup :
graphCmd.extend(["--region",gseg.bamRegion])
graphCmd.extend(["--min-candidate-sv-size", self.params.minCandidateVariantSize])
graphCmd.extend(["--min-edge-observations", self.params.minEdgeObservations])
graphCmd.extend(["--ref",self.params.referenceFasta])
for bamPath in self.params.normalBamList :
graphCmd.extend(["--align-file",bamPath])
for bamPath in self.params.tumorBamList :
graphCmd.extend(["--tumor-align-file",bamPath])
if self.params.isHighDepthFilter :
graphCmd.extend(["--chrom-depth", self.paths.getChromDepth()])
if self.params.isIgnoreAnomProperPair :
graphCmd.append("--ignore-anom-proper-pair")
if self.params.isRNA :
graphCmd.append("--rna")
graphTask=preJoin(taskPrefix,"makeLocusGraph_"+gid)
graphTasks.add(self.addTask(graphTask,graphCmd,dependencies=dirTask,memMb=self.params.estimateMemMb))
if len(tmpGraphFiles) == 0 :
raise Exception("No SV Locus graphs to create. Possible target region parse error.")
tmpGraphFileList = self.paths.getTmpGraphFileListPath()
tmpGraphFileListTask = preJoin(taskPrefix,"mergeLocusGraphInputList")
self.addWorkflowTask(tmpGraphFileListTask,listFileWorkflow(tmpGraphFileList,tmpGraphFiles),dependencies=graphTasks)
mergeCmd = [ self.params.mantaGraphMergeBin ]
mergeCmd.extend(["--output-file", graphPath])
mergeCmd.extend(["--graph-file-list",tmpGraphFileList])
mergeTask = self.addTask(preJoin(taskPrefix,"mergeLocusGraph"),mergeCmd,dependencies=tmpGraphFileListTask,memMb=self.params.mergeMemMb)
# Run a separate process to rigorously check that the final graph is valid, the sv candidate generators will check as well, but
# this makes the check much more clear:
checkCmd = [ self.params.mantaGraphCheckBin ]
checkCmd.extend(["--graph-file", graphPath])
checkTask = self.addTask(preJoin(taskPrefix,"checkLocusGraph"),checkCmd,dependencies=mergeTask,memMb=self.params.mergeMemMb)
if not self.params.isRetainTempFiles :
rmGraphTmpCmd = getRmdirCmd() + [tmpGraphDir]
rmTask=self.addTask(preJoin(taskPrefix,"removeTmpDir"),rmGraphTmpCmd,dependencies=mergeTask)
graphStatsCmd = [self.params.mantaGraphStatsBin,"--global"]
graphStatsCmd.extend(["--graph-file",graphPath])
graphStatsCmd.extend(["--output-file",graphStatsPath])
graphStatsTask = self.addTask(preJoin(taskPrefix,"locusGraphStats"),graphStatsCmd,dependencies=mergeTask,memMb=self.params.mergeMemMb)
nextStepWait = set()
nextStepWait.add(checkTask)
return nextStepWait
class listFileWorkflow(WorkflowRunner) :
"""
creates a file which enumerates the values in a list, one line per item
"""
def __init__(self2, listFile, listItems) :
self2.listFile = listFile
self2.listItems = listItems
def workflow(self2) :
fp = open(self2.listFile, "w")
for listItem in self2.listItems :
fp.write(listItem+"\n")
def sortEvidenceBams(self, sortBamTasks, taskPrefix="", binStr="", dependencies=None):
for bamIdx, _ in enumerate(self.params.normalBamList + self.params.tumorBamList):
supportBam = self.paths.getSupportBamPath(bamIdx, binStr)
sortedBam = self.paths.getSortedSupportBamPath(bamIdx, binStr)
# first check the existence of the supporting bam
# then sort the bam only if it exists
sortBamCmd = [ sys.executable, self.params.mantaSortBam,
self.params.samtoolsBin, supportBam, sortedBam ]
sortBamTask = preJoin(taskPrefix, "sortEvidenceBam_%s_%s" % (binStr, bamIdx))
sortBamTasks.add(self.addTask(sortBamTask, sortBamCmd, dependencies=dependencies))
def sortAllVcfs(self, taskPrefix="", dependencies=None) :
"""sort/prep final vcf outputs"""
nextStepWait = set()
def getVcfSortCmd(vcfListFile, outPath, isDiploid, isCandidate) :
cmd = "\"%s\" \"%s\" " % (sys.executable, self.params.mantaSortVcf)
cmd += "-f \"%s\"" % (vcfListFile)
# Boolean variable isCandidate is set "True" for candidateSV.vcf
# If it is True, commandline argument "-a" is passed on to sortBam.py to print out all vcf records
if isCandidate:
cmd += " -a"
# apply the ploidy filter to diploid variants
if isDiploid:
tempVcf = self.paths.getTempDiploidPath()
cmd += " > \"%s\"" % (tempVcf)
cmd += " && \"%s\" \"%s\" \"%s\"" % (sys.executable, self.params.mantaPloidyFilter, tempVcf)
cmd += " | \"%s\" -c > \"%s\"" % (self.params.bgzipBin, outPath)
if isDiploid:
cmd += " && " + " ".join(getRmCmd()) + " \"%s\"" % (self.paths.getTempDiploidPath())
return cmd
def getVcfTabixCmd(vcfPath) :
return [self.params.tabixBin,"-f","-p","vcf", vcfPath]
def sortVcfs(pathList, outPath, label, isDiploid=False, isCandidate=False) :
if len(pathList) == 0 : return set()
# make header modifications to first vcf in list of files to be sorted:
headerFixTask=preJoin(taskPrefix,"fixVcfHeader_"+label)
def getHeaderFixCmd(fileName) :
tmpName=fileName+".reheader.tmp"
cmd = "\"%s\" \"%s\"" % (sys.executable, self.params.vcfCmdlineSwapper)
cmd += ' "' + " ".join(self.params.configCommandLine) + '"'
cmd += " < \"%s\" > \"%s\"" % (fileName,tmpName)
cmd += " && " + " ".join(getMvCmd()) + " \"%s\" \"%s\"" % (tmpName, fileName)
return cmd
self.addTask(headerFixTask, getHeaderFixCmd(pathList[0]), dependencies=dependencies, isForceLocal=True)
vcfListFile = self.paths.getVcfListPath(label)
inputVcfTask = self.addWorkflowTask(preJoin(taskPrefix,label+"InputList"),listFileWorkflow(vcfListFile,pathList),dependencies=headerFixTask)
sortCmd = getVcfSortCmd(vcfListFile, outPath, isDiploid, isCandidate)
sortTask=self.addTask(preJoin(taskPrefix,"sort_"+label),sortCmd,dependencies=inputVcfTask)
nextStepWait.add(self.addTask(preJoin(taskPrefix,"tabix_"+label),getVcfTabixCmd(outPath),dependencies=sortTask,isForceLocal=True))
return sortTask
candSortTask = sortVcfs(self.candidateVcfPaths,
self.paths.getSortedCandidatePath(),
"sortCandidateSV",
isCandidate=True)
sortVcfs(self.diploidVcfPaths,
self.paths.getSortedDiploidPath(),
"sortDiploidSV",
isDiploid=True)
sortVcfs(self.somaticVcfPaths,
self.paths.getSortedSomaticPath(),
"sortSomaticSV")
sortVcfs(self.tumorVcfPaths,
self.paths.getSortedTumorPath(),
"sortTumorSV")
sortVcfs(self.rnaVcfPaths,
self.paths.getSortedRnaPath(),
"sortRnaSV")
def getExtractSmallCmd(maxSize, inPath, outPath) :
cmd = "\"%s\" -dc \"%s\"" % (self.params.bgzipBin, inPath)
cmd += " | \"%s\" \"%s\" --maxSize %i" % (sys.executable, self.params.mantaExtraSmallVcf, maxSize)
cmd += " | \"%s\" -c > \"%s\"" % (self.params.bgzipBin, outPath)
return cmd
def extractSmall(inPath, outPath) :
maxSize = int(self.params.minScoredVariantSize) - 1
if maxSize < 1 : return
smallCmd = getExtractSmallCmd(maxSize, inPath, outPath)
smallTask=self.addTask(preJoin(taskPrefix,"extractSmallIndels"), smallCmd, dependencies=candSortTask, isForceLocal=True)
nextStepWait.add(self.addTask(smallTask+"_tabix", getVcfTabixCmd(outPath), dependencies=smallTask, isForceLocal=True))
extractSmall(self.paths.getSortedCandidatePath(), self.paths.getSortedCandidateSmallIndelsPath())
return nextStepWait
def mergeEvidenceBams(self, mergeBamTasks, taskPrefix="", dependencies=None) :
for bamIdx, bamPath in enumerate(self.params.normalBamList + self.params.tumorBamList) :
# merge support bams
evidenceBamFile = self.paths.getFinalSupportBamPath(bamPath, bamIdx)
mergeCmd = [ sys.executable, self.params.mantaMergeBam,
self.params.samtoolsBin,
self.paths.getSortedSupportBamMask(bamIdx),
evidenceBamFile,
self.paths.getSupportBamListPath(bamIdx) ]
mergeBamTask=self.addTask(preJoin(taskPrefix,"merge_evidenceBam_%s" % (bamIdx)),
mergeCmd, dependencies=dependencies)
# index the filtered bam
### TODO still needs to handle the case where supportBamFile does not exist
indexCmd = [ self.params.samtoolsBin, "index", evidenceBamFile ]
indexBamTask = self.addTask(preJoin(taskPrefix,"index_evidenceBam_%s" % (bamIdx)),
indexCmd, dependencies=mergeBamTask)
mergeBamTasks.add(indexBamTask)
def runHyGen(self, taskPrefix="", dependencies=None) :
"""
Run hypothesis generation on each SV locus
"""
import copy
statsPath=self.paths.getStatsPath()
graphPath=self.paths.getGraphPath()
hygenDir=self.paths.getHyGenDir()
makeHyGenDirCmd = getMkdirCmd() + [hygenDir]
dirTask = self.addTask(preJoin(taskPrefix,"makeHyGenDir"), makeHyGenDirCmd, dependencies=dependencies, isForceLocal=True)
isTumorNormal = (len(self.params.normalBamList) and len(self.params.tumorBamList))
isTumorOnly = ((not isTumorNormal) and len(self.params.tumorBamList))
hygenTasks=set()
if self.params.isGenerateSupportBam :
sortEvidenceBamTasks = set()
self.candidateVcfPaths = []
self.diploidVcfPaths = []
self.somaticVcfPaths = []
self.tumorVcfPaths = []
self.rnaVcfPaths = []
edgeRuntimeLogPaths = []
if True:
binId = 0
binStr = str(binId).zfill(4)
self.candidateVcfPaths.append(self.paths.getHyGenCandidatePath(binStr))
if isTumorOnly :
self.tumorVcfPaths.append(self.paths.getHyGenTumorPath(binStr))
elif self.params.isRNA:
self.rnaVcfPaths.append(self.paths.getHyGenRnaPath(binStr))
else:
self.diploidVcfPaths.append(self.paths.getHyGenDiploidPath(binStr))
if isTumorNormal :
self.somaticVcfPaths.append(self.paths.getHyGenSomaticPath(binStr))
hygenCmd = [ self.params.mantaHyGenBin ]
hygenCmd.extend(["--threads", str(self.getNCores())])
hygenCmd.extend(["--align-stats",statsPath])
hygenCmd.extend(["--graph-file",graphPath])
hygenCmd.extend(["--bin-index", str(binId)])
hygenCmd.extend(["--bin-count", "1"])
hygenCmd.extend(["--max-edge-count", str(self.params.graphNodeMaxEdgeCount)])
hygenCmd.extend(["--min-candidate-sv-size", self.params.minCandidateVariantSize])
hygenCmd.extend(["--min-candidate-spanning-count", self.params.minCandidateSpanningCount])
hygenCmd.extend(["--min-scored-sv-size", self.params.minScoredVariantSize])
hygenCmd.extend(["--ref",self.params.referenceFasta])
hygenCmd.extend(["--candidate-output-file", self.candidateVcfPaths[-1]])
# tumor-only mode
if isTumorOnly :
hygenCmd.extend(["--tumor-output-file", self.tumorVcfPaths[-1]])
elif self.params.isRNA:
hygenCmd.extend(["--rna-output-file", self.rnaVcfPaths[-1]])
else:
hygenCmd.extend(["--diploid-output-file", self.diploidVcfPaths[-1]])
hygenCmd.extend(["--min-qual-score", self.params.minDiploidVariantScore])
hygenCmd.extend(["--min-pass-qual-score", self.params.minPassDiploidVariantScore])
hygenCmd.extend(["--min-pass-gt-score", self.params.minPassDiploidGTScore])
# tumor/normal mode
if isTumorNormal :
hygenCmd.extend(["--somatic-output-file", self.somaticVcfPaths[-1]])
hygenCmd.extend(["--min-somatic-score", self.params.minSomaticScore])
hygenCmd.extend(["--min-pass-somatic-score", self.params.minPassSomaticScore])
# Setup remote read retrieval for insertions:
def isEnableRemoteReadRetrieval() :
if isTumorOnly or isTumorNormal :
return self.params.enableRemoteReadRetrievalForInsertionsInCancerCallingModes
else :
return self.params.enableRemoteReadRetrievalForInsertionsInGermlineCallingModes
if isEnableRemoteReadRetrieval() :
hygenCmd.append("--enable-remote-read-retrieval")
if self.params.isHighDepthFilter :
hygenCmd.extend(["--chrom-depth", self.paths.getChromDepth()])
edgeRuntimeLogPaths.append(self.paths.getHyGenEdgeRuntimeLogPath(binStr))
hygenCmd.extend(["--edge-runtime-log", edgeRuntimeLogPaths[-1]])
hygenCmd.extend(["--edge-stats-log", self.paths.getFinalEdgeStatsPath()])
hygenCmd.extend(["--edge-stats-report", self.paths.getFinalEdgeStatsReportPath()])
if self.params.isGenerateSupportBam :
hygenCmd.extend(["--evidence-bam-stub", self.paths.getSupportBamStub(binStr)])
for bamPath in self.params.normalBamList :
hygenCmd.extend(["--align-file", bamPath])
for bamPath in self.params.tumorBamList :
hygenCmd.extend(["--tumor-align-file", bamPath])
if self.params.isIgnoreAnomProperPair :
hygenCmd.append("--ignore-anom-proper-pair")
if self.params.useOverlapPairEvidence:
hygenCmd.append("--use-overlapping-pair")
if self.params.isRNA :
hygenCmd.append("--rna")
if self.params.isUnstrandedRNA :
hygenCmd.append("--unstranded")
if self.params.isOutputContig :
hygenCmd.append("--output-contigs")
hygenTask = preJoin(taskPrefix,"generateCandidateSV_"+binStr)
hygenTasks.add(self.addTask(hygenTask,hygenCmd,dependencies=dirTask, nCores=self.getNCores(), memMb=self.params.hyGenMemMb))
if self.params.isGenerateSupportBam :
# sort evidence bams
#
# TODO: if the bam is large, for efficiency, consider
# 1) filtering the bin-specific bam first w.r.t. the final candidate vcf
# 2) then sort the bin-specific bam and merge them
# This would require moving the filter/sort bam jobs outside the hygen loop
sortEvidenceBams(self, sortEvidenceBamTasks, taskPrefix=taskPrefix, binStr=binStr, dependencies=hygenTask)
nextStepWait = copy.deepcopy(hygenTasks)
vcfTasks = sortAllVcfs(self,taskPrefix=taskPrefix,dependencies=hygenTasks)
nextStepWait = nextStepWait.union(vcfTasks)
if self.params.isGenerateSupportBam :
mergeBamTasks = set()
# merge evidence bams
mergeEvidenceBams(self, mergeBamTasks, taskPrefix=taskPrefix,
dependencies=sortEvidenceBamTasks)
nextStepWait = nextStepWait.union(mergeBamTasks)
#
# sort the edge runtime logs
#
logListFile = self.paths.getEdgeRuntimeLogListPath()
logListTask = preJoin(taskPrefix,"sortEdgeRuntimeLogsInputList")
self.addWorkflowTask(logListTask,listFileWorkflow(logListFile,edgeRuntimeLogPaths),dependencies=hygenTasks)
def getEdgeLogSortCmd(logListFile, outPath) :
cmd = [sys.executable, self.params.mantaSortEdgeLogs,"-f", logListFile,"-o",outPath]
return cmd
edgeSortCmd=getEdgeLogSortCmd(logListFile,self.paths.getSortedEdgeRuntimeLogPath())
self.addTask(preJoin(taskPrefix,"sortEdgeRuntimeLogs"), edgeSortCmd, dependencies=logListTask, isForceLocal=True)
if not self.params.isRetainTempFiles :
# we could delete the temp hygenDir directory here, but it is used for debug so frequently it doesn't seem worth it at present.
# rmDirCmd = getRmdirCmd() + [hygenDir]
# rmDirTask=self.addTask(preJoin(taskPrefix,"removeTmpDir"),rmDirCmd,dependencies=TBD_XXX_MANY)
pass
return nextStepWait
class PathInfo:
"""
object to centralize shared workflow path names
"""
def __init__(self, params) :
self.params = params
def getStatsPath(self) :
return os.path.join(self.params.workDir,"alignmentStats.xml")
def getStatsSummaryPath(self) :
return os.path.join(self.params.statsDir,"alignmentStatsSummary.txt")
def getChromDepth(self) :
return os.path.join(self.params.workDir,"chromDepth.txt")
def getGraphPath(self) :
return os.path.join(self.params.workDir,"svLocusGraph.bin")
def getTmpGraphDir(self) :
return os.path.join(self.getGraphPath()+".tmpdir")
def getTmpGraphFile(self, gid) :
return os.path.join(self.getTmpGraphDir(),"svLocusGraph.%s.bin" % (gid))
def getHyGenDir(self) :
return os.path.join(self.params.workDir,"svHyGen")
def getHyGenCandidatePath(self, binStr) :
return os.path.join(self.getHyGenDir(),"candidateSV.%s.vcf" % (binStr))
def getSortedCandidatePath(self) :
return os.path.join(self.params.variantsDir,"candidateSV.vcf.gz")
def getSortedCandidateSmallIndelsPath(self) :
return os.path.join(self.params.variantsDir,"candidateSmallIndels.vcf.gz")
def getHyGenDiploidPath(self, binStr) :
return os.path.join(self.getHyGenDir(),"diploidSV.%s.vcf" % (binStr))
def getTempDiploidPath(self) :
return os.path.join(self.getHyGenDir(),"diploidSV.vcf.temp")
def getSortedDiploidPath(self) :
return os.path.join(self.params.variantsDir,"diploidSV.vcf.gz")
def getHyGenSomaticPath(self, binStr) :
return os.path.join(self.getHyGenDir(),"somaticSV.%s.vcf" % (binStr))
def getHyGenTumorPath(self, binStr) :
return os.path.join(self.getHyGenDir(),"tumorSV.%s.vcf" % (binStr))
def getHyGenRnaPath(self, binStr) :
return os.path.join(self.getHyGenDir(),"rnaFusion.%s.vcf" % (binStr))
def getSortedSomaticPath(self) :
return os.path.join(self.params.variantsDir,"somaticSV.vcf.gz")
def getSortedTumorPath(self) :
return os.path.join(self.params.variantsDir,"tumorSV.vcf.gz")
def getSortedRnaPath(self) :
return os.path.join(self.params.variantsDir,"rnaSV.vcf.gz")
def getHyGenEdgeRuntimeLogPath(self, binStr) :
return os.path.join(self.getHyGenDir(),"edgeRuntimeLog.%s.txt" % (binStr))
def getSortedEdgeRuntimeLogPath(self) :
return os.path.join(self.params.workDir,"edgeRuntimeLog.txt")
def getFinalEdgeStatsPath(self) :
return os.path.join(self.params.statsDir,"svCandidateGenerationStats.xml")
def getFinalEdgeStatsReportPath(self) :
return os.path.join(self.params.statsDir,"svCandidateGenerationStats.tsv")
def getGraphStatsPath(self) :
return os.path.join(self.params.statsDir,"svLocusGraphStats.tsv")
def getSupportBamPath(self, bamIdx, binStr):
return os.path.join(self.getHyGenDir(),
"evidence_%s.bam_%s.bam" % (binStr, bamIdx))
def getSupportBamStub(self, binStr):
return os.path.join(self.getHyGenDir(),
"evidence_%s" % (binStr))
def getSortedSupportBamPath(self, bamIdx, binStr):
return os.path.join(self.getHyGenDir(),
"evidence_%s.bam_%s.sorted.bam" % (binStr, bamIdx))
def getSortedSupportBamMask(self, bamIdx):
return os.path.join(self.getHyGenDir(),
"evidence_*.bam_%s.sorted.bam" % (bamIdx))
def getFinalSupportBamPath(self, bamPath, bamIdx):
bamPrefix = os.path.splitext(os.path.basename(bamPath))[0]
return os.path.join(self.params.evidenceDir,
"evidence_%s.%s.bam" % (bamIdx, bamPrefix))
def getSupportBamListPath(self, bamIdx):
return os.path.join(self.getHyGenDir(),
"list.evidence.bam_%s.txt" % (bamIdx))
def getTmpGraphFileListPath(self) :
return os.path.join(self.getTmpGraphDir(),"list.svLocusGraph.txt")
def getVcfListPath(self, label) :
return os.path.join(self.getHyGenDir(),"list.%s.txt" % (label))
def getEdgeRuntimeLogListPath(self) :
return os.path.join(self.getHyGenDir(),"list.edgeRuntimeLog.txt")
class MantaWorkflow(WorkflowRunner) :
"""
Manta SV discovery workflow
"""
def __init__(self,params) :
cleanPyEnv()
self.params=params
# normalize boolean option input:
safeSetBool(self.params,"enableRemoteReadRetrievalForInsertionsInGermlineCallingModes")
safeSetBool(self.params,"enableRemoteReadRetrievalForInsertionsInCancerCallingModes")
safeSetBool(self.params,"useOverlapPairEvidence")
# Use RNA option for minCandidate size
if self.params.isRNA:
self.params.minCandidateVariantSize = self.params.rnaMinCandidateVariantSize
# format bam lists:
if self.params.normalBamList is None : self.params.normalBamList = []
if self.params.tumorBamList is None : self.params.tumorBamList = []
# make sure run directory is setup:
self.params.runDir=os.path.abspath(self.params.runDir)
ensureDir(self.params.runDir)
# everything that's not intended to be a final result should dump directories/files in workDir
self.params.workDir=os.path.join(self.params.runDir,"workspace")
ensureDir(self.params.workDir)
# all finalized pretty results get transfered to resultsDir
self.params.resultsDir=os.path.join(self.params.runDir,"results")
ensureDir(self.params.resultsDir)
self.params.statsDir=os.path.join(self.params.resultsDir,"stats")
ensureDir(self.params.statsDir)
self.params.variantsDir=os.path.join(self.params.resultsDir,"variants")
ensureDir(self.params.variantsDir)
self.params.evidenceDir=os.path.join(self.params.resultsDir,"evidence")
ensureDir(self.params.evidenceDir)
# self.params.reportsDir=os.path.join(self.params.resultsDir,"reports")
# ensureDir(self.params.reportsDir)
indexRefFasta=self.params.referenceFasta+".fai"
if self.params.referenceFasta is None:
raise Exception("No reference fasta defined.")
else:
checkFile(self.params.referenceFasta,"reference fasta")
checkFile(indexRefFasta,"reference fasta index")
# read fasta index
(self.params.chromOrder,self.params.chromSizes) = getFastaChromOrderSize(indexRefFasta)
# determine subset of chroms where we can skip calling entirely
(self.params.callRegionList, self.params.chromIsSkipped) = getCallRegions(self.params)
self.paths = PathInfo(self.params)
self.params.isHighDepthFilter = (not (self.params.isExome or self.params.isRNA))
self.params.isIgnoreAnomProperPair = (self.params.isRNA)
# always use overlapping pairs for RNA calling
if (self.params.isRNA) :
self.params.useOverlapPairEvidence = True
def setCallMemMb(self) :
"Setup default task memory requirements"
if self.params.callMemMbOverride is not None :
self.params.estimateMemMb = self.params.callMemMbOverride
self.params.hyGenMemMb = self.params.callMemMbOverride
else :
if self.getRunMode() == "sge" :
self.params.hyGenMemMb = self.params.hyGenSGEMemMb
else :
self.params.hyGenMemMb = self.params.hyGenLocalMemMb
def getSuccessMessage(self) :
"Message to be included in email for successful runs"
msg = "Manta workflow successfully completed.\n\n"
msg += "\tworkflow version: %s\n" % (__version__)
return msg
def workflow(self) :
self.flowLog("Initiating Manta workflow version: %s" % (__version__))
self.setCallMemMb()
graphTaskDependencies = set()
statsTasks = runStats(self,taskPrefix="getAlignmentStats")
graphTaskDependencies |= statsTasks
summarizeStats(self, dependencies=statsTasks)
if not ((not self.params.isHighDepthFilter) or self.params.useExistingChromDepths) :
depthTasks = mantaGetDepthFromAlignments(self)
graphTaskDependencies |= depthTasks
graphTasks = runLocusGraph(self,dependencies=graphTaskDependencies)
hygenTasks = runHyGen(self,dependencies=graphTasks)