Shiva.scala 4.58 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
3
4
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
5
import nl.lumc.sasc.biopet.extensions.gatk.broad._
Peter van 't Hof's avatar
Peter van 't Hof committed
6
import nl.lumc.sasc.biopet.pipelines.shiva.{ ShivaVariantcallingTrait, ShivaTrait }
Peter van 't Hof's avatar
Peter van 't Hof committed
7
8
9
10
11
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
12
class Shiva(val root: Configurable) extends QScript with ShivaTrait {
13
  qscript =>
Peter van 't Hof's avatar
Peter van 't Hof committed
14
15
  def this() = this(null)

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

Peter van 't Hof's avatar
Peter van 't Hof committed
28
  /** Makes a sample */
Peter van 't Hof's avatar
Peter van 't Hof committed
29
  override def makeSample(id: String) = new this.Sample(id)
Peter van 't Hof's avatar
Peter van 't Hof committed
30
31

  /** Class will generate sample jobs */
Peter van 't Hof's avatar
Peter van 't Hof committed
32
  class Sample(sampleId: String) extends super.Sample(sampleId) {
Peter van 't Hof's avatar
Peter van 't Hof committed
33
    /** Makes a library */
Peter van 't Hof's avatar
Peter van 't Hof committed
34
    override def makeLibrary(id: String) = new this.Library(id)
Peter van 't Hof's avatar
Peter van 't Hof committed
35
36

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

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

Peter van 't Hof's avatar
Peter van 't Hof committed
65
    /** This methods will add double preprocess steps, with GATK indel realignment */
Peter van 't Hof's avatar
Peter van 't Hof committed
66
67
68
    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 {
69
        case file => {
Peter van 't Hof's avatar
Peter van 't Hof committed
70
          config("use_indel_realigner", default = true).asBoolean match {
71
72
73
74
            case true  => addIndelRealign(file, sampleDir, false)
            case false => file
          }
        }
Peter van 't Hof's avatar
Peter van 't Hof committed
75
76
77
78
      }
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
79
  /** Adds indel realignment jobs */
80
81
82
83
84
85
86
87
88
89
90
91
  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
92
  /** Adds base recalibration jobs */
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  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
117
118
}

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