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


wyleung's avatar
wyleung committed
22
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
38
39

  class LibraryOutput extends AbstractLibraryOutput {
    var mappedBamFile: File = _
  }

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

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

  }
59

wyleung's avatar
wyleung committed
60
61
62
63
64
65
  override def onExecutionDone(jobs: Map[QFunction, JobRunInfo], success: Boolean) {
    logger.info("YAM SV Pipeline has run ...............................................................")
  }
  
  def runSingleSampleJobs(sampleConfig: Map[String, Any]): SampleOutput = {
    val sampleOutput = new SampleOutput
wyleung's avatar
wyleung committed
66
    var libraryBamfiles: List[File] = List()
wyleung's avatar
wyleung committed
67
    var outputFiles: Map[String, List[File]] = Map()
wyleung's avatar
wyleung committed
68
69
    var libraryFastqFiles: List[File] = List()
    val sampleID: String = sampleConfig("ID").toString
70
71
72
73
    val sampleDir: String = outputDir + sampleID + "/"

    val svcallingDir: String = sampleDir + "svcalls/"

wyleung's avatar
wyleung committed
74
75
76
77
    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
78
    }
79

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

    /// bamfile will be used as input for the SV callers. First run Clever
88
89
90
91
    //    val cleverVCF : File = sampleDir + "/" + sampleID + ".clever.vcf"

    val cleverDir = svcallingDir + sampleID + ".clever/"
    val clever = Clever(this, bamFile, this.reference, cleverDir)
wyleung's avatar
wyleung committed
92
    sampleOutput.vcf += ("clever" -> List(clever.outputvcf))
93
94
95
96
97
98
99
    add(clever)

    //    val breakdancerDir = sampleDir + sampleID + ".breakdancer/"
    //    val breakdancer = Breakdancer(this, bamFile, this.reference, breakdancerDir )
    //    outputFiles += ("breakdancer_vcf" -> List(breakdancer.output) )
    //    addAll(breakdancer.functions)

wyleung's avatar
wyleung committed
100
    return sampleOutput
wyleung's avatar
wyleung committed
101
  }
102

wyleung's avatar
wyleung committed
103
  // Called for each run from a sample
wyleung's avatar
wyleung committed
104
105
106
  
  def runSingleLibraryJobs(runConfig: Map[String, Any], sampleConfig: Map[String, Any]): LibraryOutput = {
    val libraryOutput = new LibraryOutput
wyleung's avatar
wyleung committed
107
108
109
    var outputFiles: Map[String, File] = Map()
    val runID: String = runConfig("ID").toString
    val sampleID: String = sampleConfig("ID").toString
110
111
112
    val alignmentDir: String = outputDir + sampleID + "/alignment/"

    val runDir: String = alignmentDir + "run_" + runID + "/"
wyleung's avatar
wyleung committed
113
    if (runConfig.contains("R1")) {
wyleung's avatar
wyleung committed
114
115
116
117
//      val mapping = Mapping.loadFromLibraryConfig(this, runConfig, sampleConfig, runDir)
      val mapping = new Mapping(this)
      
      mapping.defaultAligner = "stampy"
118
      mapping.skipFlexiprep = false
119
      mapping.skipMarkduplicates = true // we do the dedup marking using Sambamba
wyleung's avatar
wyleung committed
120
121
122
123
      
      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
124
125
126
127
128
129
      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
130

wyleung's avatar
wyleung committed
131
132
133
      mapping.init
      mapping.biopetScript
      addAll(mapping.functions)
134

wyleung's avatar
wyleung committed
135
136
137
138
      // start sambamba dedup
      
//      outputFiles += ("final_bam" -> mapping.outputFiles("finalBamFile"))
      libraryOutput.mappedBamFile = mapping.outputFiles("finalBamFile")
wyleung's avatar
wyleung committed
139
    } else this.logger.error("Sample: " + sampleID + ": No R1 found for run: " + runConfig)
wyleung's avatar
wyleung committed
140
141
142
    return libraryOutput
//    logger.debug(outputFiles)
//    return outputFiles
wyleung's avatar
wyleung committed
143
144
145
146
147
148
  }
}

object Yamsvp extends PipelineCommand {
  override val pipeline = "/nl/lumc/sasc/biopet/pipelines/yamsvp/Yamsvp.class"
}