ShivaVariantcalling.scala 8.25 KB
Newer Older
bow's avatar
bow committed
1
2
3
4
5
6
7
8
9
10
/**
 * Biopet is built on top of GATK Queue for building bioinformatic
 * pipelines. It is mainly intended to support LUMC SHARK cluster which is running
 * SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
 * should also be able to execute Biopet tools and pipelines.
 *
 * Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
 *
 * Contact us at: sasc@lumc.nl
 *
11
 * A dual licensing mode is applied. The source code within this project is freely available for non-commercial use under an AGPL
bow's avatar
bow committed
12
13
14
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
15
16
package nl.lumc.sasc.biopet.pipelines.shiva

17
18
19
20
21
22
23
24
25
import nl.lumc.sasc.biopet.core.{ PipelineCommand, Reference, SampleLibraryTag }
import nl.lumc.sasc.biopet.core.summary.SummaryQScript
import nl.lumc.sasc.biopet.extensions.Tabix
import nl.lumc.sasc.biopet.extensions.gatk.{ CombineVariants, GenotypeConcordance }
import nl.lumc.sasc.biopet.extensions.tools.VcfStats
import nl.lumc.sasc.biopet.extensions.vt.{ VtDecompose, VtNormalize }
import nl.lumc.sasc.biopet.pipelines.bammetrics.TargetRegions
import nl.lumc.sasc.biopet.pipelines.shiva.variantcallers.{ VarscanCnsSingleSample, _ }
import nl.lumc.sasc.biopet.utils.{ BamUtils, Logging }
Peter van 't Hof's avatar
Peter van 't Hof committed
26
import nl.lumc.sasc.biopet.utils.config.Configurable
Peter van 't Hof's avatar
Peter van 't Hof committed
27
import org.broadinstitute.gatk.queue.QScript
28
import org.broadinstitute.gatk.queue.extensions.gatk.TaggedFile
Peter van 't Hof's avatar
Peter van 't Hof committed
29
30

/**
31
 * Implementation of ShivaVariantcalling
Peter van 't Hof's avatar
Peter van 't Hof committed
32
 *
Peter van 't Hof's avatar
Peter van 't Hof committed
33
34
 * Created by pjvan_thof on 2/26/15.
 */
35
36
37
38
39
40
41
class ShivaVariantcalling(val root: Configurable) extends QScript
  with SummaryQScript
  with SampleLibraryTag
  with Reference
  with TargetRegions {
  qscript =>

Peter van 't Hof's avatar
Peter van 't Hof committed
42
  def this() = this(null)
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

  @Input(doc = "Bam files (should be deduped bams)", shortName = "BAM", required = true)
  protected var inputBamsArg: List[File] = Nil

  var inputBams: Map[String, File] = Map()

  /** Executed before script */
  def init(): Unit = {
    if (inputBamsArg.nonEmpty) inputBams = BamUtils.sampleBamMap(inputBamsArg)
  }

  var referenceVcf: Option[File] = config("reference_vcf")

  var referenceVcfRegions: Option[File] = config("reference_vcf_regions")

  /** Name prefix, can override this methods if neeeded */
  def namePrefix: String = {
    (sampleId, libId) match {
      case (Some(s), Some(l)) => s + "-" + l
      case (Some(s), _)       => s
      case _                  => config("name_prefix")
    }
  }

  override def defaults = Map("bcftoolscall" -> Map("f" -> List("GQ")))

  /** Final merged output files of all variantcaller modes */
  def finalFile = new File(outputDir, namePrefix + ".final.vcf.gz")

  /** Variantcallers requested by the config */
  protected val configCallers: Set[String] = config("variantcallers")

Sander Bollen's avatar
Sander Bollen committed
75
  val callers: List[Variantcaller] = {
76
    (for (name <- configCallers) yield {
77
78
79
      if (!ShivaVariantcalling.callersList(this).exists(_.name == name))
        Logging.addError(s"variantcaller '$name' does not exist, possible to use: " + ShivaVariantcalling.callersList(this).map(_.name).mkString(", "))
      ShivaVariantcalling.callersList(this).find(_.name == name)
80
81
82
83
84
    }).flatten.toList.sortBy(_.prio)
  }

  /** This will add jobs for this pipeline */
  def biopetScript(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
85
86
    if (inputBams.isEmpty) {
      Logging.addError("No input bams found")
Peter van 't Hof's avatar
Peter van 't Hof committed
87
      inputBams = Map("test" -> new File("."))
Peter van 't Hof's avatar
Peter van 't Hof committed
88
89
90
    }
    if (callers.isEmpty) Logging.addError("must select at least 1 variantcaller, choices are: " + ShivaVariantcalling.callersList(this).map(_.name).mkString(", "))
    if (!callers.exists(_.mergeVcfResults)) Logging.addError("must select at least 1 variantcaller where merge_vcf_results is true")
91

Peter van 't Hof's avatar
Peter van 't Hof committed
92
    addAll(dbsnpVcfFile.map(Shiva.makeValidateVcfJobs(this, _, referenceFasta(), new File(outputDir, ".validate"))).getOrElse(Nil))
93

94
    val cv = new CombineVariants(qscript)
95
    cv.out = finalFile
Peter van 't Hof's avatar
Peter van 't Hof committed
96
    cv.setKey = Some("VariantCaller")
97
    cv.genotypemergeoption = Some("PRIORITIZE")
98
    cv.rod_priority_list = Some(callers.filter(_.mergeVcfResults).map(_.name).mkString(","))
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    for (caller <- callers) {
      caller.inputBams = inputBams
      caller.namePrefix = namePrefix
      caller.outputDir = new File(outputDir, caller.name)
      add(caller)
      addStats(caller.outputFile, caller.name)
      val normalize: Boolean = config("execute_vt_normalize", default = false, namespace = caller.configNamespace)
      val decompose: Boolean = config("execute_vt_decompose", default = false, namespace = caller.configNamespace)

      val vtNormalize = new VtNormalize(this)
      vtNormalize.inputVcf = caller.outputFile
      val vtDecompose = new VtDecompose(this)

      if (normalize && decompose) {
        vtNormalize.outputVcf = swapExt(caller.outputDir, caller.outputFile, ".vcf.gz", ".normalized.vcf.gz")
        vtNormalize.isIntermediate = true
        add(vtNormalize, Tabix(this, vtNormalize.outputVcf))
        vtDecompose.inputVcf = vtNormalize.outputVcf
        vtDecompose.outputVcf = swapExt(caller.outputDir, vtNormalize.outputVcf, ".vcf.gz", ".decompose.vcf.gz")
        add(vtDecompose, Tabix(this, vtDecompose.outputVcf))
Peter van 't Hof's avatar
Peter van 't Hof committed
119
        if (caller.mergeVcfResults) cv.variant :+= TaggedFile(vtDecompose.outputVcf, caller.name)
120
121
122
      } else if (normalize && !decompose) {
        vtNormalize.outputVcf = swapExt(caller.outputDir, caller.outputFile, ".vcf.gz", ".normalized.vcf.gz")
        add(vtNormalize, Tabix(this, vtNormalize.outputVcf))
Peter van 't Hof's avatar
Peter van 't Hof committed
123
        if (caller.mergeVcfResults) cv.variant :+= TaggedFile(vtNormalize.outputVcf, caller.name)
124
125
126
127
      } else if (!normalize && decompose) {
        vtDecompose.inputVcf = caller.outputFile
        vtDecompose.outputVcf = swapExt(caller.outputDir, caller.outputFile, ".vcf.gz", ".decompose.vcf.gz")
        add(vtDecompose, Tabix(this, vtDecompose.outputVcf))
Peter van 't Hof's avatar
Peter van 't Hof committed
128
129
130
131
132
133
        if (caller.mergeVcfResults) cv.variant :+= TaggedFile(vtDecompose.outputVcf, caller.name)
      } else if (caller.mergeVcfResults) cv.variant :+= TaggedFile(caller.outputFile, caller.name)
    }
    if (cv.variant.nonEmpty) {
      add(cv)
      addStats(finalFile, "final")
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    }

    addSummaryJobs()
  }

  protected def addStats(vcfFile: File, name: String): Unit = {
    val vcfStats = new VcfStats(qscript)
    vcfStats.input = vcfFile
    vcfStats.setOutputDir(new File(vcfFile.getParentFile, "vcfstats"))
    if (name == "final") vcfStats.infoTags :+= "VariantCaller"
    add(vcfStats)
    addSummarizable(vcfStats, s"$namePrefix-vcfstats-$name")

    referenceVcf.foreach(referenceVcfFile => {
      val gc = new GenotypeConcordance(this)
149
150
151
      gc.eval = vcfFile
      gc.comp = referenceVcfFile
      gc.out = new File(vcfFile.getParentFile, s"$namePrefix-genotype_concordance.$name.txt")
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
      referenceVcfRegions.foreach(gc.intervals ::= _)
      add(gc)
      addSummarizable(gc, s"$namePrefix-genotype_concordance-$name")
    })

    for (bedFile <- ampliconBedFile.toList ::: roiBedFiles) {
      val regionName = bedFile.getName.stripSuffix(".bed")
      val vcfStats = new VcfStats(qscript)
      vcfStats.input = vcfFile
      vcfStats.intervals = Some(bedFile)
      vcfStats.setOutputDir(new File(vcfFile.getParentFile, s"vcfstats-$regionName"))
      if (name == "final") vcfStats.infoTags :+= "VariantCaller"
      add(vcfStats)
      addSummarizable(vcfStats, s"$namePrefix-vcfstats-$name-$regionName")
    }
  }

  /** Location of summary file */
  def summaryFile = new File(outputDir, "ShivaVariantcalling.summary.json")

  /** Settings for the summary */
  def summarySettings = Map(
    "variantcallers" -> configCallers.toList,
Peter van 't Hof's avatar
Peter van 't Hof committed
175
176
    "regions_of_interest" -> roiBedFiles.map(_.getName),
    "amplicon_bed" -> ampliconBedFile.map(_.getAbsolutePath)
177
178
179
180
181
182
  )

  /** Files for the summary */
  def summaryFiles: Map[String, File] = {
    callers.map(x => x.name -> x.outputFile).toMap + ("final" -> finalFile)
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
183
184
}

185
186
object ShivaVariantcalling extends PipelineCommand {
  /** Will generate all available variantcallers */
Peter van 't Hof's avatar
Peter van 't Hof committed
187
  protected[pipelines] def callersList(root: Configurable): List[Variantcaller] =
Peter van 't Hof's avatar
Peter van 't Hof committed
188
189
190
191
192
193
194
195
196
197
    new HaplotypeCallerGvcf(root) ::
      new HaplotypeCallerAllele(root) ::
      new UnifiedGenotyperAllele(root) ::
      new UnifiedGenotyper(root) ::
      new HaplotypeCaller(root) ::
      new Freebayes(root) ::
      new RawVcf(root) ::
      new Bcftools(root) ::
      new BcftoolsSingleSample(root) ::
      new VarscanCnsSingleSample(root) :: Nil
198
}