Shiva.scala 5.23 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._
Peter van 't Hof's avatar
Peter van 't Hof committed
11
import nl.lumc.sasc.biopet.pipelines.shiva.{ ShivaTrait, ShivaVariantcallingTrait }
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 */
24
  override def makeVariantcalling(multisample: Boolean = false): ShivaVariantcallingTrait = {
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
47
48
49
50
51
      lazy val useIndelRealigner: Boolean = config("use_indel_realigner", default = true)
      lazy val useBaseRecalibration: Boolean = config("use_base_recalibration", default = true)

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

      /** Return true when baserecalibration is executed */
      protected def doneBaseRecalibrator: Boolean = {
        val br = new BaseRecalibrator(qscript)
Peter van 't Hof's avatar
Peter van 't Hof committed
56
        useBaseRecalibration && br.knownSites.nonEmpty
Peter van 't Hof's avatar
Peter van 't Hof committed
57
58
      }

Peter van 't Hof's avatar
Peter van 't Hof committed
59
      /** This will adds preprocess steps, gatk indel realignment and base recalibration is included here */
60
      override def preProcess(input: File): Option[File] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
61
        if (!useIndelRealigner && !doneBaseRecalibrator) None
62
63
        else {
          val indelRealignFile = useIndelRealigner match {
Peter van 't Hof's avatar
Peter van 't Hof committed
64
            case true  => addIndelRealign(input, libDir, doneBaseRecalibrator || libraries.size > 1)
65
66
67
68
69
70
71
72
            case false => input
          }

          useBaseRecalibration match {
            case true  => Some(addBaseRecalibrator(indelRealignFile, libDir, libraries.size > 1))
            case false => Some(indelRealignFile)
          }
        }
Peter van 't Hof's avatar
Peter van 't Hof committed
73
74
75
      }
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
76
77
78
79
    override def summarySettings = super.summarySettings + ("use_indel_realigner" -> useIndelRealigner)

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

Peter van 't Hof's avatar
Peter van 't Hof committed
80
    /** This methods will add double preprocess steps, with GATK indel realignment */
Peter van 't Hof's avatar
Peter van 't Hof committed
81
82
    override protected def addDoublePreProcess(input: List[File], isIntermediate: Boolean = false): Option[File] = {
      if (input.size <= 1) super.addDoublePreProcess(input)
Peter van 't Hof's avatar
Peter van 't Hof committed
83
      else super.addDoublePreProcess(input, isIntermediate = useIndelRealigner).collect {
Peter van 't Hof's avatar
Peter van 't Hof committed
84
        case file =>
Peter van 't Hof's avatar
Peter van 't Hof committed
85
          useIndelRealigner match {
Peter van 't Hof's avatar
Peter van 't Hof committed
86
            case true  => addIndelRealign(file, sampleDir, isIntermediate = false)
87
88
            case false => file
          }
Peter van 't Hof's avatar
Peter van 't Hof committed
89
90
91
92
      }
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
93
  /** Adds indel realignment jobs */
94
95
96
97
98
99
100
101
102
  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
103
    indelRealigner.o
104
105
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
106
  /** Adds base recalibration jobs */
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  def addBaseRecalibrator(inputBam: File, dir: File, isIntermediate: Boolean): File = {
    val baseRecalibrator = BaseRecalibrator(this, inputBam, swapExt(dir, inputBam, ".bam", ".baserecal"))

    if (baseRecalibrator.knownSites.isEmpty) {
      logger.warn("No Known site found, skipping base recalibration, file: " + inputBam)
      return inputBam
    }
    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
129
    printReads.o
130
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
131
132
}

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