Yamsvp.scala 5.77 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.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
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
/*
 * 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.sambamba.{ SambambaIndex, SambambaMerge }
import nl.lumc.sasc.biopet.extensions.svcallers.pindel.Pindel
import nl.lumc.sasc.biopet.extensions.svcallers.{ Breakdancer, Clever }

import nl.lumc.sasc.biopet.pipelines.mapping.Mapping

import org.broadinstitute.gatk.queue.QScript
import org.broadinstitute.gatk.queue.function._
import org.broadinstitute.gatk.queue.engine.JobRunInfo

class Yamsvp(val root: Configurable) extends QScript with MultiSampleQScript {
  def this() = this(null)

  var reference: File = config("reference", required = true)
  var finalBamFiles: List[File] = Nil

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

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

  override def init() {
    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
    logger.info("Starting YAM SV Pipeline")
    runSamplesJobs
    //

  }

  override def onExecutionDone(jobs: Map[QFunction, JobRunInfo], success: Boolean) {
    logger.info("YAM SV Pipeline has run .......................")
  }

73
  def runSingleSampleJobs(sampleID: String): SampleOutput = {
Peter van 't Hof's avatar
Peter van 't Hof committed
74
75
76
77
78
79
80
81
82
    val sampleOutput = new SampleOutput
    var libraryBamfiles: List[File] = List()
    var outputFiles: Map[String, List[File]] = Map()
    var libraryFastqFiles: List[File] = List()
    val sampleDir: String = outputDir + sampleID + "/"
    val alignmentDir: String = sampleDir + "alignment/"

    val svcallingDir: String = sampleDir + "svcalls/"

83
    sampleOutput.libraries = runLibraryJobs(sampleID)
Peter van 't Hof's avatar
Peter van 't Hof committed
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
    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)
    }

    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)
        mergeSamFiles.output
      } else null

    val bamIndex = SambambaIndex(root, bamFile)
    add(bamIndex)

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

    val cleverDir = svcallingDir + sampleID + ".clever/"
    val clever = Clever(this, bamFile, this.reference, svcallingDir, cleverDir)
    clever.deps = List(bamIndex.output)
    sampleOutput.vcf += ("clever" -> List(clever.outputvcf))
    add(clever)

    val breakdancerDir = svcallingDir + sampleID + ".breakdancer/"
    val breakdancer = Breakdancer(this, bamFile, this.reference, breakdancerDir)
    sampleOutput.vcf += ("breakdancer" -> List(breakdancer.outputvcf))
    addAll(breakdancer.functions)

    // for pindel we should use per library config collected into one config file
    //    val pindelDir = svcallingDir + sampleID + ".pindel/"
    //    val pindel = Pindel(this, bamFile, this.reference, pindelDir)
    //    sampleOutput.vcf += ("pindel" -> List(pindel.outputvcf))
    //    addAll(pindel.functions)
    //
    //    
    return sampleOutput
  }

  // Called for each run from a sample

128
  def runSingleLibraryJobs(libraryId: String, sampleID: String): LibraryOutput = {
Peter van 't Hof's avatar
Peter van 't Hof committed
129
130
131
    val libraryOutput = new LibraryOutput

    val alignmentDir: String = outputDir + sampleID + "/alignment/"
132
    val runDir: String = alignmentDir + "run_" + libraryId + "/"
Peter van 't Hof's avatar
Peter van 't Hof committed
133

134
    if (config.contains("R1")) {
Peter van 't Hof's avatar
Peter van 't Hof committed
135
136
137
138
139
140
      val mapping = new Mapping(this)

      mapping.aligner = config("aligner", default = "stampy")
      mapping.skipFlexiprep = false
      mapping.skipMarkduplicates = true // we do the dedup marking using Sambamba

141
142
      mapping.input_R1 = config("R1")
      mapping.input_R2 = config("R2")
Peter van 't Hof's avatar
Peter van 't Hof committed
143
      mapping.paired = (mapping.input_R2 != null)
144
145
146
147
148
      mapping.RGLB = libraryId
      mapping.RGSM = sampleID
      mapping.RGPL = config("PL")
      mapping.RGPU = config("PU")
      mapping.RGCN = config("CN")
Peter van 't Hof's avatar
Peter van 't Hof committed
149
150
151
152
153
154
155
156
157
      mapping.outputDir = runDir

      mapping.init
      mapping.biopetScript
      addAll(mapping.functions)

      // start sambamba dedup

      libraryOutput.mappedBamFile = mapping.outputFiles("finalBamFile")
158
    } else this.logger.error("Sample: " + sampleID + ": No R1 found for library: " + libraryId)
Peter van 't Hof's avatar
Peter van 't Hof committed
159
160
161
162
163
164
165
    return libraryOutput
    //    logger.debug(outputFiles)
    //    return outputFiles
  }
}

object Yamsvp extends PipelineCommand