ShivaVariantcallingTrait.scala 3.75 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
3
4
package nl.lumc.sasc.biopet.pipelines.shiva

import java.io.File

Peter van 't Hof's avatar
Peter van 't Hof committed
5
import nl.lumc.sasc.biopet.core.{ PipelineCommand, SampleLibraryTag }
Peter van 't Hof's avatar
Peter van 't Hof committed
6
import nl.lumc.sasc.biopet.core.summary.SummaryQScript
Peter van 't Hof's avatar
Peter van 't Hof committed
7
import nl.lumc.sasc.biopet.extensions.gatk.CombineVariants
Peter van 't Hof's avatar
Peter van 't Hof committed
8
import nl.lumc.sasc.biopet.tools.{ VcfStats, VcfFilter, MpileupToVcf }
Peter van 't Hof's avatar
Peter van 't Hof committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import nl.lumc.sasc.biopet.utils.ConfigUtils
import org.broadinstitute.gatk.utils.commandline.Input

import scala.collection.generic.Sorted

/**
 * Created by pjvan_thof on 2/26/15.
 */
trait ShivaVariantcallingTrait extends SummaryQScript with SampleLibraryTag {
  qscript =>

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

23
24
  def namePrefix: String = {
    (sampleId, libId) match {
Peter van 't Hof's avatar
Peter van 't Hof committed
25
26
27
      case (Some(sampleId), Some(libId)) => sampleId + "-" + libId
      case (Some(sampleId), _)           => sampleId
      case _                             => config("name_prefix")
28
29
    }
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
30

Peter van 't Hof's avatar
Peter van 't Hof committed
31
  def init: Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
32
33
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
34
  def finalFile = new File(outputDir, namePrefix + ".final.vcf.gz")
35

Peter van 't Hof's avatar
Peter van 't Hof committed
36
  def biopetScript: Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
37
    val callers = usedCallers.sortBy(_.prio)
Peter van 't Hof's avatar
Peter van 't Hof committed
38

Peter van 't Hof's avatar
Peter van 't Hof committed
39
    val cv = new CombineVariants(qscript)
40
41
    cv.outputFile = finalFile
    cv.setKey = "VariantCaller"
Peter van 't Hof's avatar
Peter van 't Hof committed
42
43
44
    cv.genotypeMergeOptions = Some("PRIORITIZE")
    cv.rodPriorityList = callers.map(_.name).mkString(",")
    for (caller <- callers) {
Peter van 't Hof's avatar
Peter van 't Hof committed
45
46
      caller.addJobs()
      cv.addInput(caller.outputFile, caller.name)
Peter van 't Hof's avatar
Peter van 't Hof committed
47
48
49
50
51
52

      val vcfStats = new VcfStats(qscript)
      vcfStats.input = caller.outputFile
      vcfStats.setOutputDir(new File(caller.outputDir, "vcfstats"))
      add(vcfStats)
      addSummarizable(vcfStats, namePrefix + "-vcfstats-" + caller.name)
Peter van 't Hof's avatar
Peter van 't Hof committed
53
54
55
    }
    add(cv)

Peter van 't Hof's avatar
Peter van 't Hof committed
56
57
58
59
    val vcfStats = new VcfStats(qscript)
    vcfStats.input = finalFile
    vcfStats.setOutputDir(new File(outputDir, "vcfstats"))
    add(vcfStats)
Peter van 't Hof's avatar
Peter van 't Hof committed
60
    addSummarizable(vcfStats, namePrefix + "-vcfstats-final")
Peter van 't Hof's avatar
Peter van 't Hof committed
61

Peter van 't Hof's avatar
Peter van 't Hof committed
62
63
64
    addSummaryJobs
  }

65
66
67
  def callers: List[Variantcaller] = List(new RawVcf)

  def usedCallers: List[Variantcaller] = callers.filter(_.use)
Peter van 't Hof's avatar
Peter van 't Hof committed
68

Peter van 't Hof's avatar
Peter van 't Hof committed
69
70
71
  trait Variantcaller {
    val name: String
    def outputDir = new File(qscript.outputDir, name)
Peter van 't Hof's avatar
Peter van 't Hof committed
72
73
74
75
76
77
    protected val defaultUse: Boolean
    lazy val use: Boolean = config("use_" + name, default = defaultUse)
    protected val defaultPrio: Int
    lazy val prio: Int = config("prio_" + name, default = defaultPrio)
    def addJobs()
    def outputFile: File
Peter van 't Hof's avatar
Peter van 't Hof committed
78
79
80
81
  }

  class RawVcf extends Variantcaller {
    val name = "raw"
Peter van 't Hof's avatar
Peter van 't Hof committed
82
83
84
    protected val defaultPrio = 999
    protected val defaultUse = true

Peter van 't Hof's avatar
Peter van 't Hof committed
85
    def outputFile = new File(outputDir, namePrefix + ".raw.vcf.gz")
Peter van 't Hof's avatar
Peter van 't Hof committed
86

Peter van 't Hof's avatar
Peter van 't Hof committed
87
    def addJobs() {
Peter van 't Hof's avatar
Peter van 't Hof committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
      val rawFiles = inputBams.map(bamFile => {
        val m2v = new MpileupToVcf(qscript)
        m2v.inputBam = bamFile
        m2v.output = new File(outputDir, bamFile.getName.stripSuffix(".bam") + ".raw.vcf")
        add(m2v)

        val vcfFilter = new VcfFilter(qscript) {
          override def configName = "vcffilter"
          override def defaults = ConfigUtils.mergeMaps(Map("min_sample_depth" -> 8,
            "min_alternate_depth" -> 2,
            "min_samples_pass" -> 1,
            "filter_ref_calls" -> true
          ), super.defaults)
        }
        vcfFilter.inputVcf = m2v.output
        vcfFilter.outputVcf = new File(outputDir, bamFile.getName.stripSuffix(".bam") + ".raw.filter.vcf.gz")
        add(vcfFilter)
        vcfFilter.outputVcf
      })

Peter van 't Hof's avatar
Peter van 't Hof committed
108
109
110
      val cv = new CombineVariants(qscript)
      cv.inputFiles = rawFiles
      cv.outputFile = outputFile
Peter van 't Hof's avatar
Peter van 't Hof committed
111
      cv.setKey = "null"
Peter van 't Hof's avatar
Peter van 't Hof committed
112
      add(cv)
Peter van 't Hof's avatar
Peter van 't Hof committed
113
114
115
116
117
118
119
    }
  }

  def summaryFile = new File(outputDir, "ShivaVariantcalling.summary.json")

  def summarySettings = Map()

120
  def summaryFiles: Map[String, File] = usedCallers.map(x => (x.name -> x.outputFile)).toMap + ("final" -> finalFile)
Peter van 't Hof's avatar
Peter van 't Hof committed
121
}