Shiva.scala 5.29 KB
Newer Older
bow's avatar
bow committed
1
2
3
4
5
/**
 * Due to the license issue with GATK, this part of Biopet can only be used inside the
 * LUMC. Please refer to https://git.lumc.nl/biopet/biopet/wikis/home for instructions
 * on how to use this protected part of biopet or contact us at sasc@lumc.nl
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
6
7
8
package nl.lumc.sasc.biopet.pipelines.gatk

import nl.lumc.sasc.biopet.core.PipelineCommand
Peter van 't Hof's avatar
Peter van 't Hof committed
9
import nl.lumc.sasc.biopet.utils.config.Configurable
Peter van 't Hof's avatar
Peter van 't Hof committed
10
import nl.lumc.sasc.biopet.extensions.gatk.broad._
11
import nl.lumc.sasc.biopet.pipelines.shiva.{ ShivaVariantcallingTrait, ShivaTrait }
Peter van 't Hof's avatar
Peter van 't Hof committed
12
13
14
import org.broadinstitute.gatk.queue.QScript

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
15
16
 * Shiva inplementation with GATK steps
 *
Peter van 't Hof's avatar
Peter van 't Hof committed
17
18
 * Created by pjvan_thof on 2/26/15.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
19
class Shiva(val root: Configurable) extends QScript with ShivaTrait {
20
  qscript =>
Peter van 't Hof's avatar
Peter van 't Hof committed
21
22
  def this() = this(null)

Peter van 't Hof's avatar
Peter van 't Hof committed
23
  /** Make variantcalling submodule, this with the gatk modes in there */
Peter van 't Hof's avatar
Peter van 't Hof committed
24
  override def makeVariantcalling(multisample: Boolean = false) = {
Peter van 't Hof's avatar
Peter van 't Hof committed
25
    if (multisample) new ShivaVariantcalling(qscript) {
Peter van 't Hof's avatar
Peter van 't Hof committed
26
      override def namePrefix = "multisample"
27
28
29
      override def configName = "shivavariantcalling"
      override def configPath: List[String] = super.configPath ::: "multisample" :: Nil
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
30
    else new ShivaVariantcalling(qscript) {
31
32
33
34
      override def configName = "shivavariantcalling"
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
35
  /** Makes a sample */
Peter van 't Hof's avatar
Peter van 't Hof committed
36
  override def makeSample(id: String) = new this.Sample(id)
Peter van 't Hof's avatar
Peter van 't Hof committed
37
38

  /** Class will generate sample jobs */
Peter van 't Hof's avatar
Peter van 't Hof committed
39
  class Sample(sampleId: String) extends super.Sample(sampleId) {
Peter van 't Hof's avatar
Peter van 't Hof committed
40
    /** Makes a library */
Peter van 't Hof's avatar
Peter van 't Hof committed
41
    override def makeLibrary(id: String) = new this.Library(id)
Peter van 't Hof's avatar
Peter van 't Hof committed
42
43

    /** Class will generate library jobs */
Peter van 't Hof's avatar
Peter van 't Hof committed
44
    class Library(libId: String) extends super.Library(libId) {
Peter van 't Hof's avatar
Peter van 't Hof committed
45

Peter van 't Hof's avatar
Peter van 't Hof committed
46
      lazy val useIndelRealigner: Boolean = config("use_indel_realigner", default = true)
Peter van 't Hof's avatar
Peter van 't Hof committed
47
48
49
50
51
52
53
      lazy val useBaseRecalibration: Boolean = {
        val c: Boolean = config("use_base_recalibration", default = true)
        val br = new BaseRecalibrator(qscript)
        if (c && br.knownSites.isEmpty)
          logger.warn("No Known site found, skipping base recalibration, file: " + inputBam)
        c && br.knownSites.nonEmpty
      }
Peter van 't Hof's avatar
Peter van 't Hof committed
54
55
56

      override def summarySettings = super.summarySettings +
        ("use_indel_realigner" -> useIndelRealigner) +
Peter van 't Hof's avatar
Peter van 't Hof committed
57
        ("use_base_recalibration" -> useBaseRecalibration)
Peter van 't Hof's avatar
Peter van 't Hof committed
58

59
60
61
62
63
64
65
66
67
68
69
70
      override def preProcessBam = if (useIndelRealigner && useBaseRecalibration)
        bamFile.map(swapExt(libDir, _, ".bam", ".realign.baserecal.bam"))
      else if (useIndelRealigner) bamFile.map(swapExt(libDir, _, ".bam", ".realign.bam"))
      else if (useBaseRecalibration) bamFile.map(swapExt(libDir, _, ".bam", ".baserecal.bam"))
      else bamFile

      override def addJobs(): Unit = {
        super.addJobs()
        if (useIndelRealigner && useBaseRecalibration) {
          val file = addIndelRealign(bamFile.get, libDir, isIntermediate = true)
          addBaseRecalibrator(file, libDir, libraries.size > 1)
        } else if (useIndelRealigner) {
Peter van 't Hof's avatar
Peter van 't Hof committed
71
          addIndelRealign(bamFile.get, libDir, libraries.size > 1)
72
73
        } else if (useBaseRecalibration) {
          addBaseRecalibrator(bamFile.get, libDir, libraries.size > 1)
74
        }
Peter van 't Hof's avatar
Peter van 't Hof committed
75
76
77
      }
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
78
    override def keepMergedFiles: Boolean = config("keep_merged_files", default = !useIndelRealigner)
Peter van 't Hof's avatar
Peter van 't Hof committed
79

Peter van 't Hof's avatar
Peter van 't Hof committed
80
81
82
83
    override def summarySettings = super.summarySettings + ("use_indel_realigner" -> useIndelRealigner)

    lazy val useIndelRealigner: Boolean = config("use_indel_realigner", default = true)

84
85
86
87
88
89
90
91
    override def preProcessBam = if (useIndelRealigner && libraries.values.flatMap(_.preProcessBam).size > 1) {
      bamFile.map(swapExt(sampleDir, _, ".bam", ".realign.bam"))
    } else bamFile

    override def addJobs(): Unit = {
      super.addJobs()

      if (useIndelRealigner && libraries.values.flatMap(_.preProcessBam).size > 1) {
Peter van 't Hof's avatar
Peter van 't Hof committed
92
        addIndelRealign(bamFile.get, sampleDir, false)
Peter van 't Hof's avatar
Peter van 't Hof committed
93
94
95
96
      }
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
97
  /** Adds indel realignment jobs */
98
99
100
101
102
103
104
105
106
  def addIndelRealign(inputBam: File, dir: File, isIntermediate: Boolean): File = {
    val realignerTargetCreator = RealignerTargetCreator(this, inputBam, dir)
    realignerTargetCreator.isIntermediate = true
    add(realignerTargetCreator)

    val indelRealigner = IndelRealigner(this, inputBam, realignerTargetCreator.out, dir)
    indelRealigner.isIntermediate = isIntermediate
    add(indelRealigner)

Peter van 't Hof's avatar
Peter van 't Hof committed
107
    indelRealigner.o
108
109
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
110
  /** Adds base recalibration jobs */
111
112
113
  def addBaseRecalibrator(inputBam: File, dir: File, isIntermediate: Boolean): File = {
    val baseRecalibrator = BaseRecalibrator(this, inputBam, swapExt(dir, inputBam, ".bam", ".baserecal"))

Peter van 't Hof's avatar
Peter van 't Hof committed
114
    if (baseRecalibrator.knownSites.isEmpty) return inputBam
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    add(baseRecalibrator)

    if (config("use_analyze_covariates", default = false).asBoolean) {
      val baseRecalibratorAfter = BaseRecalibrator(this, inputBam, swapExt(dir, inputBam, ".bam", ".baserecal.after"))
      baseRecalibratorAfter.BQSR = baseRecalibrator.o
      add(baseRecalibratorAfter)

      add(AnalyzeCovariates(this, baseRecalibrator.o, baseRecalibratorAfter.o, swapExt(dir, inputBam, ".bam", ".baserecal.pdf")))
    }

    val printReads = PrintReads(this, inputBam, swapExt(dir, inputBam, ".bam", ".baserecal.bam"))
    printReads.BQSR = baseRecalibrator.o
    printReads.isIntermediate = isIntermediate
    add(printReads)

Peter van 't Hof's avatar
Peter van 't Hof committed
130
    printReads.o
131
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
132
133
}

Peter van 't Hof's avatar
Peter van 't Hof committed
134
/** This object give a default main methods for this pipeline */
Peter van 't Hof's avatar
Peter van 't Hof committed
135
object Shiva extends PipelineCommand