Shiva.scala 4.83 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
9
package nl.lumc.sasc.biopet.pipelines.gatk

import nl.lumc.sasc.biopet.core.PipelineCommand
import nl.lumc.sasc.biopet.core.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
15
16
import org.broadinstitute.gatk.queue.QScript

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

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

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

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

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

Peter van 't Hof's avatar
Peter van 't Hof committed
44
45
46
47
48
49
50
51
52
      val useIndelRealigner: Boolean = config("use_indel_realigner", default = true)
      val useBaseRecalibration: Boolean = config("use_base_recalibration", default = true)

      /** Return true when baserecalibration is executed */
      protected def doneBaseRecalibrator: Boolean = {
        val br = new BaseRecalibrator(qscript)
        useBaseRecalibration && !br.knownSites.isEmpty
      }

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

Peter van 't Hof's avatar
Peter van 't Hof committed
70
    /** This methods will add double preprocess steps, with GATK indel realignment */
Peter van 't Hof's avatar
Peter van 't Hof committed
71
72
73
    override protected def addDoublePreProcess(input: List[File], isIntermediate: Boolean = false): Option[File] = {
      if (input.size <= 1) super.addDoublePreProcess(input)
      else super.addDoublePreProcess(input, true).collect {
74
        case file => {
Peter van 't Hof's avatar
Peter van 't Hof committed
75
          config("use_indel_realigner", default = true).asBoolean match {
76
77
78
79
            case true  => addIndelRealign(file, sampleDir, false)
            case false => file
          }
        }
Peter van 't Hof's avatar
Peter van 't Hof committed
80
81
82
83
      }
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
84
  /** Adds indel realignment jobs */
85
86
87
88
89
90
91
92
93
94
95
96
  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)

    return indelRealigner.o
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
97
  /** Adds base recalibration jobs */
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  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)

    return printReads.o
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
122
123
}

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