Yamsvp.scala 5.19 KB
Newer Older
wyleung's avatar
wyleung committed
1
2
3
4
5
6
7
8
9
10
11
/*
 * 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

import nl.lumc.sasc.biopet.extensions.picard.MergeSamFiles
12
import nl.lumc.sasc.biopet.extensions.svcallers.Breakdancer
13
import nl.lumc.sasc.biopet.extensions.svcallers.Clever
wyleung's avatar
wyleung committed
14
15
16
17
18

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
19
20
import org.broadinstitute.gatk.queue.engine.JobRunInfo

wyleung's avatar
wyleung committed
21
22
23
24
25
class Yamsvp(val root: Configurable) extends QScript with MultiSampleQScript {
  def this() = this(null)

  var reference: File = _
  var finalBamFiles: List[File] = Nil
26

wyleung's avatar
wyleung committed
27
28
29
30
31
32
33
34
35
  class LibraryOutput extends AbstractLibraryOutput {
    var mappedBamFile: File = _
  }

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

36
  override def init() {
wyleung's avatar
wyleung committed
37
38
39
40
41
42
43
44
45
46
47
48
49
    for (file <- configfiles) globalConfig.loadConfigFile(file)
    reference = config("reference", required = true)
    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
50
    logger.info("Starting YAM SV Pipeline")
wyleung's avatar
wyleung committed
51
    runSamplesJobs
52
    //
wyleung's avatar
wyleung committed
53
54

  }
55

wyleung's avatar
wyleung committed
56
57
58
  override def onExecutionDone(jobs: Map[QFunction, JobRunInfo], success: Boolean) {
    logger.info("YAM SV Pipeline has run ...............................................................")
  }
59

wyleung's avatar
wyleung committed
60
61
  def runSingleSampleJobs(sampleConfig: Map[String, Any]): SampleOutput = {
    val sampleOutput = new SampleOutput
wyleung's avatar
wyleung committed
62
    var libraryBamfiles: List[File] = List()
wyleung's avatar
wyleung committed
63
    var outputFiles: Map[String, List[File]] = Map()
wyleung's avatar
wyleung committed
64
65
    var libraryFastqFiles: List[File] = List()
    val sampleID: String = sampleConfig("ID").toString
66
67
68
69
    val sampleDir: String = outputDir + sampleID + "/"

    val svcallingDir: String = sampleDir + "svcalls/"

wyleung's avatar
wyleung committed
70
71
72
73
    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++
      libraryBamfiles = List(libraryOutput.mappedBamFile)
wyleung's avatar
wyleung committed
74
    }
75

wyleung's avatar
wyleung committed
76
    val bamFile: File = if (libraryBamfiles.size == 1) libraryBamfiles.head
77
78
79
80
81
    else if (libraryBamfiles.size > 1) {
      val mergeSamFiles = MergeSamFiles(this, libraryBamfiles, sampleDir)
      add(mergeSamFiles)
      mergeSamFiles.output
    } else null
82
83

    /// bamfile will be used as input for the SV callers. First run Clever
84
85
86
    //    val cleverVCF : File = sampleDir + "/" + sampleID + ".clever.vcf"

    val cleverDir = svcallingDir + sampleID + ".clever/"
87
    val clever = Clever(this, bamFile, this.reference, svcallingDir, cleverDir)
wyleung's avatar
wyleung committed
88
    sampleOutput.vcf += ("clever" -> List(clever.outputvcf))
89
90
    add(clever)

91
92
93
94
    val breakdancerDir = sampleDir + sampleID + ".breakdancer/"
    val breakdancer = Breakdancer(this, bamFile, this.reference, breakdancerDir)
    sampleOutput.vcf += ("breakdancer" -> List(breakdancer.outputraw))
    addAll(breakdancer.functions)
95

wyleung's avatar
wyleung committed
96
    return sampleOutput
wyleung's avatar
wyleung committed
97
  }
98

wyleung's avatar
wyleung committed
99
  // Called for each run from a sample
100

wyleung's avatar
wyleung committed
101
102
  def runSingleLibraryJobs(runConfig: Map[String, Any], sampleConfig: Map[String, Any]): LibraryOutput = {
    val libraryOutput = new LibraryOutput
wyleung's avatar
wyleung committed
103
104
105
    var outputFiles: Map[String, File] = Map()
    val runID: String = runConfig("ID").toString
    val sampleID: String = sampleConfig("ID").toString
106
107
108
    val alignmentDir: String = outputDir + sampleID + "/alignment/"

    val runDir: String = alignmentDir + "run_" + runID + "/"
wyleung's avatar
wyleung committed
109
    if (runConfig.contains("R1")) {
110
      //      val mapping = Mapping.loadFromLibraryConfig(this, runConfig, sampleConfig, runDir)
wyleung's avatar
wyleung committed
111
      val mapping = new Mapping(this)
112

wyleung's avatar
wyleung committed
113
      mapping.defaultAligner = "stampy"
114
      mapping.skipFlexiprep = false
115
      mapping.skipMarkduplicates = true // we do the dedup marking using Sambamba
116

wyleung's avatar
wyleung committed
117
118
119
      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
120
121
122
123
124
125
      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
126

wyleung's avatar
wyleung committed
127
128
129
      mapping.init
      mapping.biopetScript
      addAll(mapping.functions)
130

wyleung's avatar
wyleung committed
131
      // start sambamba dedup
132
133

      //      outputFiles += ("final_bam" -> mapping.outputFiles("finalBamFile"))
wyleung's avatar
wyleung committed
134
      libraryOutput.mappedBamFile = mapping.outputFiles("finalBamFile")
wyleung's avatar
wyleung committed
135
    } else this.logger.error("Sample: " + sampleID + ": No R1 found for run: " + runConfig)
wyleung's avatar
wyleung committed
136
    return libraryOutput
137
138
    //    logger.debug(outputFiles)
    //    return outputFiles
wyleung's avatar
wyleung committed
139
140
141
  }
}

142
object Yamsvp extends PipelineCommand