Yamsvp.scala 5.62 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
13
14
15
import nl.lumc.sasc.biopet.extensions.sambamba.{ SambambaIndex, SambambaMerge }
import nl.lumc.sasc.biopet.extensions.svcallers.pindel.Pindel
import nl.lumc.sasc.biopet.extensions.svcallers.{ Breakdancer, Clever }

wyleung's avatar
wyleung committed
16
17
18
19
20

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

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

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

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

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

38
  override def init() {
wyleung's avatar
wyleung committed
39
40
41
42
43
44
45
46
47
48
49
50
51
    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
52
    logger.info("Starting YAM SV Pipeline")
wyleung's avatar
wyleung committed
53
    runSamplesJobs
54
    //
wyleung's avatar
wyleung committed
55
56

  }
57

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

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

71
72
73

    val svcallingDir: String = sampleDir + "svcalls/"

wyleung's avatar
wyleung committed
74
75
76
    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++
77
      libraryBamfiles ++= List(libraryOutput.mappedBamFile)
wyleung's avatar
wyleung committed
78
    }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

    val bamFile: File = 
      if (libraryBamfiles.size == 1) libraryBamfiles.head
      else if (libraryBamfiles.size > 1) {
        val mergeSamFiles = new SambambaMerge(root)
        mergeSamFiles.input = libraryBamfiles
        mergeSamFiles.output = alignmentDir + sampleID + ".merged.bam"
        add(mergeSamFiles)
        
        val bamIndex = SambambaIndex(root, mergeSamFiles.output)
        add(bamIndex)
        
        mergeSamFiles.output
      } else null
    
94
    /// bamfile will be used as input for the SV callers. First run Clever
95
96
97
    //    val cleverVCF : File = sampleDir + "/" + sampleID + ".clever.vcf"

    val cleverDir = svcallingDir + sampleID + ".clever/"
98
    val clever = Clever(this, bamFile, this.reference, svcallingDir, cleverDir)
wyleung's avatar
wyleung committed
99
    sampleOutput.vcf += ("clever" -> List(clever.outputvcf))
100
101
    add(clever)

102
    val breakdancerDir = svcallingDir + sampleID + ".breakdancer/"
103
    val breakdancer = Breakdancer(this, bamFile, this.reference, breakdancerDir)
wyleung's avatar
Renames    
wyleung committed
104
    sampleOutput.vcf += ("breakdancer" -> List(breakdancer.outputvcf))
105
    addAll(breakdancer.functions)
106
107

    // for pindel we should use per library config collected into one config file
108
109
110
111
112
113
//    val pindelDir = svcallingDir + sampleID + ".pindel/"
//    val pindel = Pindel(this, bamFile, this.reference, pindelDir)
//    sampleOutput.vcf += ("pindel" -> List(pindel.outputvcf))
//    addAll(pindel.functions)
//
//    
wyleung's avatar
wyleung committed
114
    return sampleOutput
wyleung's avatar
wyleung committed
115
  }
116

wyleung's avatar
wyleung committed
117
  // Called for each run from a sample
118

wyleung's avatar
wyleung committed
119
120
  def runSingleLibraryJobs(runConfig: Map[String, Any], sampleConfig: Map[String, Any]): LibraryOutput = {
    val libraryOutput = new LibraryOutput
121
    
wyleung's avatar
wyleung committed
122
123
    val runID: String = runConfig("ID").toString
    val sampleID: String = sampleConfig("ID").toString
124
125
    val alignmentDir: String = outputDir + sampleID + "/alignment/"
    val runDir: String = alignmentDir + "run_" + runID + "/"
126
    
wyleung's avatar
wyleung committed
127
    if (runConfig.contains("R1")) {
wyleung's avatar
wyleung committed
128
      val mapping = new Mapping(this)
129

wyleung's avatar
wyleung committed
130
      mapping.defaultAligner = "stampy"
131
      mapping.skipFlexiprep = false
132
      mapping.skipMarkduplicates = true // we do the dedup marking using Sambamba
133

wyleung's avatar
wyleung committed
134
135
136
      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
137
138
139
140
141
142
      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
143

wyleung's avatar
wyleung committed
144
145
146
      mapping.init
      mapping.biopetScript
      addAll(mapping.functions)
147

wyleung's avatar
wyleung committed
148
      // start sambamba dedup
149

wyleung's avatar
wyleung committed
150
      libraryOutput.mappedBamFile = mapping.outputFiles("finalBamFile")
wyleung's avatar
wyleung committed
151
    } else this.logger.error("Sample: " + sampleID + ": No R1 found for run: " + runConfig)
wyleung's avatar
wyleung committed
152
    return libraryOutput
153
154
    //    logger.debug(outputFiles)
    //    return outputFiles
wyleung's avatar
wyleung committed
155
156
157
  }
}

158
object Yamsvp extends PipelineCommand