Yamsvp.scala 7.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Biopet is built on top of GATK Queue for building bioinformatic
 * pipelines. It is mainly intended to support LUMC SHARK cluster which is running
 * SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
 * should also be able to execute Biopet tools and pipelines.
 *
 * Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
 *
 * Contact us at: sasc@lumc.nl
 *
 * A dual licensing mode is applied. The source code within this project that are
 * not part of GATK Queue is freely available for non-commercial use under an AGPL
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
wyleung's avatar
wyleung committed
16
17
18
19
20
21
22
23
24
25
/*
 * Structural variation calling
 */

package nl.lumc.sasc.biopet.pipelines.yamsvp

import nl.lumc.sasc.biopet.core.config.Configurable
import nl.lumc.sasc.biopet.core.MultiSampleQScript
import nl.lumc.sasc.biopet.core.PipelineCommand

26
import nl.lumc.sasc.biopet.extensions.Ln
27
import nl.lumc.sasc.biopet.extensions.igvtools.IGVToolsCount
28
import nl.lumc.sasc.biopet.extensions.sambamba.{ SambambaIndex, SambambaMerge, SambambaMarkdup }
29
import nl.lumc.sasc.biopet.extensions.svcallers.pindel.Pindel
30
import nl.lumc.sasc.biopet.extensions.svcallers.{ Breakdancer, Delly, CleverCaller }
31

wyleung's avatar
wyleung committed
32
33
34
35
import nl.lumc.sasc.biopet.pipelines.mapping.Mapping

import org.broadinstitute.gatk.queue.QScript
import org.broadinstitute.gatk.queue.function._
wyleung's avatar
wyleung committed
36
37
import org.broadinstitute.gatk.queue.engine.JobRunInfo

wyleung's avatar
wyleung committed
38
39
40
class Yamsvp(val root: Configurable) extends QScript with MultiSampleQScript {
  def this() = this(null)

Peter van 't Hof's avatar
Peter van 't Hof committed
41
  var reference: File = config("reference", required = true)
wyleung's avatar
wyleung committed
42
  var finalBamFiles: List[File] = Nil
43

wyleung's avatar
wyleung committed
44
45
46
47
48
49
50
51
52
  class LibraryOutput extends AbstractLibraryOutput {
    var mappedBamFile: File = _
  }

  class SampleOutput extends AbstractSampleOutput {
    var vcf: Map[String, List[File]] = Map()
    var mappedBamFile: File = _
  }

53
  override def init() {
wyleung's avatar
wyleung committed
54
55
56
57
58
59
60
61
62
63
64
    if (outputDir == null)
      throw new IllegalStateException("Output directory is not specified in the config / argument")
    else if (!outputDir.endsWith("/"))
      outputDir += "/"
  }

  def biopetScript() {
    // write the pipeline here
    // start with QC, alignment, call sambamba, call sv callers, reporting

    // read config and set all parameters for the pipeline
65
    logger.info("Starting YAM SV Pipeline")
wyleung's avatar
wyleung committed
66
    runSamplesJobs
67
    //
wyleung's avatar
wyleung committed
68
69

  }
70

wyleung's avatar
wyleung committed
71
  override def onExecutionDone(jobs: Map[QFunction, JobRunInfo], success: Boolean) {
72
    logger.info("YAM SV Pipeline has run .......................")
wyleung's avatar
wyleung committed
73
  }
74

wyleung's avatar
wyleung committed
75
76
  def runSingleSampleJobs(sampleConfig: Map[String, Any]): SampleOutput = {
    val sampleOutput = new SampleOutput
wyleung's avatar
wyleung committed
77
    var libraryBamfiles: List[File] = List()
wyleung's avatar
wyleung committed
78
    var outputFiles: Map[String, List[File]] = Map()
wyleung's avatar
wyleung committed
79
80
    var libraryFastqFiles: List[File] = List()
    val sampleID: String = sampleConfig("ID").toString
81
    val sampleDir: String = outputDir + sampleID + "/"
82
83
    val alignmentDir: String = sampleDir + "alignment/"

84
85
    val svcallingDir: String = sampleDir + "svcalls/"

wyleung's avatar
wyleung committed
86
87
88
    sampleOutput.libraries = runLibraryJobs(sampleConfig)
    for ((libraryID, libraryOutput) <- sampleOutput.libraries) {
      // this is extending the libraryBamfiles list like '~=' in D or .append in Python or .push_back in C++
89
      libraryBamfiles ++= List(libraryOutput.mappedBamFile)
wyleung's avatar
wyleung committed
90
    }
91

Peter van 't Hof's avatar
Peter van 't Hof committed
92
    val bamFile: File =
93
94
      if (libraryBamfiles.size == 1) {
        // When the sample has only 1 run, make a link in the main alignment directory
95
        val alignmentlink = Ln(this, libraryBamfiles.head,
96
          alignmentDir + sampleID + ".merged.bam", true)
97
98
        add(alignmentlink, isIntermediate = true)
        alignmentlink.out
99
      } else if (libraryBamfiles.size > 1) {
100
        val mergeSamFiles = new SambambaMerge(this)
101
102
        mergeSamFiles.input = libraryBamfiles
        mergeSamFiles.output = alignmentDir + sampleID + ".merged.bam"
103
        add(mergeSamFiles, isIntermediate = true)
104
105
        mergeSamFiles.output
      } else null
Peter van 't Hof's avatar
Peter van 't Hof committed
106

107
    val bamMarkDup = SambambaMarkdup(this, bamFile)
108
109
    add(bamMarkDup)

110
111
112
113
    // create an IGV TDF file
    val tdfCount = IGVToolsCount(this, bamMarkDup.output, config("genomename", default = "hg19"))
    add(tdfCount)

114
    /// bamfile will be used as input for the SV callers. First run Clever
115
116
117
    //    val cleverVCF : File = sampleDir + "/" + sampleID + ".clever.vcf"

    val cleverDir = svcallingDir + sampleID + ".clever/"
118
    val clever = CleverCaller(this, bamMarkDup.output, this.reference, svcallingDir, cleverDir)
wyleung's avatar
wyleung committed
119
    sampleOutput.vcf += ("clever" -> List(clever.outputvcf))
120
121
    add(clever)

122
123
124
    val clever_vcf = Ln(this, clever.outputvcf, svcallingDir + sampleID + ".clever.vcf", relative = true)
    add(clever_vcf)

125
    val breakdancerDir = svcallingDir + sampleID + ".breakdancer/"
126
    val breakdancer = Breakdancer(this, bamMarkDup.output, this.reference, breakdancerDir)
wyleung's avatar
Renames    
wyleung committed
127
    sampleOutput.vcf += ("breakdancer" -> List(breakdancer.outputvcf))
128
    addAll(breakdancer.functions)
129

130
131
132
    val bd_vcf = Ln(this, breakdancer.outputvcf, svcallingDir + sampleID + ".breakdancer.vcf", relative = true)
    add(bd_vcf)

wyleung's avatar
wyleung committed
133
    val dellyDir = svcallingDir + sampleID + ".delly/"
134
    val delly = Delly(this, bamMarkDup.output, dellyDir)
wyleung's avatar
wyleung committed
135
136
137
138
139
140
    sampleOutput.vcf += ("delly" -> List(delly.outputvcf))
    addAll(delly.functions)

    val delly_vcf = Ln(this, delly.outputvcf, svcallingDir + sampleID + ".delly.vcf", relative = true)
    add(delly_vcf)

141
    // for pindel we should use per library config collected into one config file
Peter van 't Hof's avatar
Peter van 't Hof committed
142
    //    val pindelDir = svcallingDir + sampleID + ".pindel/"
143
    //    val pindel = Pindel(this, analysisBam, this.reference, pindelDir)
Peter van 't Hof's avatar
Peter van 't Hof committed
144
145
146
    //    sampleOutput.vcf += ("pindel" -> List(pindel.outputvcf))
    //    addAll(pindel.functions)
    //
147
148
149
    //    val pindel_vcf = Ln(this, pindel.outputvcf, svcallingDir + sampleID + ".pindel.vcf", relative = true)
    //    add(pindel_vcf)
    //
wyleung's avatar
wyleung committed
150
    return sampleOutput
wyleung's avatar
wyleung committed
151
  }
152

wyleung's avatar
wyleung committed
153
  // Called for each run from a sample
154

wyleung's avatar
wyleung committed
155
156
  def runSingleLibraryJobs(runConfig: Map[String, Any], sampleConfig: Map[String, Any]): LibraryOutput = {
    val libraryOutput = new LibraryOutput
Peter van 't Hof's avatar
Peter van 't Hof committed
157

wyleung's avatar
wyleung committed
158
159
    val runID: String = runConfig("ID").toString
    val sampleID: String = sampleConfig("ID").toString
160
161
    val alignmentDir: String = outputDir + sampleID + "/alignment/"
    val runDir: String = alignmentDir + "run_" + runID + "/"
Peter van 't Hof's avatar
Peter van 't Hof committed
162

wyleung's avatar
wyleung committed
163
    if (runConfig.contains("R1")) {
wyleung's avatar
wyleung committed
164
      val mapping = new Mapping(this)
165

166
167
168
169
      // TODO: check and test config[aligner] in json
      // yamsvp/aligner -> value
      // this setting causes error if not defined?
      mapping.aligner = config("aligner", default = "bwa")
170
      mapping.skipFlexiprep = false
171
      mapping.skipMarkduplicates = true // we do the dedup marking using Sambamba
172

wyleung's avatar
wyleung committed
173
174
175
      if (runConfig.contains("R1")) mapping.input_R1 = new File(runConfig("R1").toString)
      if (runConfig.contains("R2")) mapping.input_R2 = new File(runConfig("R2").toString)
      mapping.paired = (mapping.input_R2 != null)
wyleung's avatar
wyleung committed
176
177
178
179
180
181
      mapping.RGLB = runConfig("ID").toString
      mapping.RGSM = sampleConfig("ID").toString
      if (runConfig.contains("PL")) mapping.RGPL = runConfig("PL").toString
      if (runConfig.contains("PU")) mapping.RGPU = runConfig("PU").toString
      if (runConfig.contains("CN")) mapping.RGCN = runConfig("CN").toString
      mapping.outputDir = runDir
wyleung's avatar
wyleung committed
182

wyleung's avatar
wyleung committed
183
184
185
      mapping.init
      mapping.biopetScript
      addAll(mapping.functions)
186

wyleung's avatar
wyleung committed
187
      // start sambamba dedup
188

wyleung's avatar
wyleung committed
189
      libraryOutput.mappedBamFile = mapping.outputFiles("finalBamFile")
wyleung's avatar
wyleung committed
190
    } else this.logger.error("Sample: " + sampleID + ": No R1 found for run: " + runConfig)
wyleung's avatar
wyleung committed
191
    return libraryOutput
192
193
    //    logger.debug(outputFiles)
    //    return outputFiles
wyleung's avatar
wyleung committed
194
195
196
  }
}

197
object Yamsvp extends PipelineCommand