ShivaGatk.scala 3.84 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, ShivaVariantcalling, ShivaTrait }
Peter van 't Hof's avatar
Peter van 't Hof committed
7
8
9
10
11
12
import org.broadinstitute.gatk.queue.QScript

/**
 * Created by pjvan_thof on 2/26/15.
 */
class ShivaGatk(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)

16
17
  override def makeVariantcalling(multisample: Boolean = false): ShivaVariantcallingTrait = {
    if (multisample) new ShivaVariantcallingGatk(qscript) {
Peter van 't Hof's avatar
Peter van 't Hof committed
18
      override def namePrefix = "multisample"
19
20
21
22
23
24
25
26
      override def configName = "shivavariantcalling"
      override def configPath: List[String] = super.configPath ::: "multisample" :: Nil
    }
    else new ShivaVariantcallingGatk(qscript) {
      override def configName = "shivavariantcalling"
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
27
28
29
30
  override def makeSample(id: String) = new this.Sample(id)
  class Sample(sampleId: String) extends super.Sample(sampleId) {
    override def makeLibrary(id: String) = new this.Library(id)
    class Library(libId: String) extends super.Library(libId) {
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
      override def preProcess(input: File): Option[File] = {
        val useIndelRealigner: Boolean = config("use_indel_realign", default = true)
        val useBaseRecalibration: Boolean = config("use_base_recalibration", default = true)

        if (!useIndelRealigner && !useBaseRecalibration) None
        else {
          val indelRealignFile = useIndelRealigner match {
            case true  => addIndelRealign(input, libDir, useBaseRecalibration || libraries.size > 1)
            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
47
48
49
50
51
      }
    }

    override def doublePreProcess(input: List[File], isIntermediate: Boolean = false): Option[File] = {
      if (input.size <= 1) super.doublePreProcess(input)
52
53
54
55
56
57
58
      else super.doublePreProcess(input, true).collect {
        case file => {
          config("use_indel_realign", default = true).asBoolean match {
            case true  => addIndelRealign(file, sampleDir, false)
            case false => file
          }
        }
Peter van 't Hof's avatar
Peter van 't Hof committed
59
60
61
62
      }
    }
  }

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  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
  }

  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
99
100
101
}

object ShivaGatk extends PipelineCommand