ShivaSvCalling.scala 4.37 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof 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
Peter van 't Hof's avatar
Peter van 't Hof committed
12 13 14 15 16 17
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
package nl.lumc.sasc.biopet.pipelines.shiva

import nl.lumc.sasc.biopet.core.summary.SummaryQScript
18
import nl.lumc.sasc.biopet.core.{ PipelineCommand, Reference, SampleLibraryTag }
19
import nl.lumc.sasc.biopet.extensions.Pysvtools
Wai Yi Leung's avatar
style  
Wai Yi Leung committed
20
import nl.lumc.sasc.biopet.pipelines.shiva.svcallers._
21
import nl.lumc.sasc.biopet.utils.config.Configurable
22
import nl.lumc.sasc.biopet.utils.{ BamUtils, Logging }
Peter van 't Hof's avatar
Peter van 't Hof committed
23
import org.broadinstitute.gatk.queue.QScript
24

Peter van 't Hof's avatar
Peter van 't Hof committed
25 26 27 28 29
/**
 * Common trait for ShivaVariantcalling
 *
 * Created by pjvan_thof on 2/26/15.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
30
class ShivaSvCalling(val root: Configurable) extends QScript with SummaryQScript with SampleLibraryTag with Reference {
Peter van 't Hof's avatar
Peter van 't Hof committed
31 32
  qscript =>

Peter van 't Hof's avatar
Peter van 't Hof committed
33 34
  def this() = this(null)

35 36 37
  var outputMergedVCFbySample: Map[String, File] = Map()
  var outputMergedVCF: File = _

Peter van 't Hof's avatar
Peter van 't Hof committed
38
  @Input(doc = "Bam files (should be deduped bams)", shortName = "BAM", required = true)
Peter van 't Hof's avatar
Peter van 't Hof committed
39
  protected[shiva] var inputBamsArg: List[File] = Nil
Peter van 't Hof's avatar
Peter van 't Hof committed
40

41
  var inputBams: Map[String, File] = Map()
Peter van 't Hof's avatar
Peter van 't Hof committed
42 43 44

  /** Executed before script */
  def init(): Unit = {
45
    if (inputBamsArg.nonEmpty) inputBams = BamUtils.sampleBamMap(inputBamsArg)
46
    outputMergedVCF = new File(outputDir, "allsamples.merged.vcf")
Peter van 't Hof's avatar
Peter van 't Hof committed
47 48 49
  }

  /** Variantcallers requested by the config */
Peter van 't Hof's avatar
Peter van 't Hof committed
50
  protected val configCallers: Set[String] = config("sv_callers", default = Set("breakdancer", "clever", "delly"))
Peter van 't Hof's avatar
Peter van 't Hof committed
51 52 53 54 55

  /** This will add jobs for this pipeline */
  def biopetScript(): Unit = {
    for (cal <- configCallers) {
      if (!callersList.exists(_.name == cal))
56
        Logging.addError("variantcaller '" + cal + "' does not exist, possible to use: " + callersList.map(_.name).mkString(", "))
Peter van 't Hof's avatar
Peter van 't Hof committed
57 58
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
59
    val callers = callersList.filter(x => configCallers.contains(x.name))
Peter van 't Hof's avatar
Peter van 't Hof committed
60 61

    require(inputBams.nonEmpty, "No input bams found")
62
    require(callers.nonEmpty, "Please select at least 1 SV caller, choices are: " + callersList.map(_.name).mkString(", "))
Peter van 't Hof's avatar
Peter van 't Hof committed
63

Peter van 't Hof's avatar
Peter van 't Hof committed
64
    callers.foreach { caller =>
65
      caller.inputBams = inputBams
Peter van 't Hof's avatar
Peter van 't Hof committed
66 67 68
      caller.outputDir = new File(outputDir, caller.name)
      add(caller)
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
69

70 71
    // merge VCF by sample
    for ((sample, bamFile) <- inputBams) {
72 73 74 75 76 77 78 79 80 81 82 83
      if (callers.size > 1) {
        var sampleVCFS: List[Option[File]] = List.empty
        callers.foreach { caller =>
          sampleVCFS ::= caller.outputVCF(sample)
        }
        val mergeSVcalls = new Pysvtools(this)
        mergeSVcalls.input = sampleVCFS.flatten
        mergeSVcalls.output = new File(outputDir, sample + ".merged.vcf")
        add(mergeSVcalls)
        outputMergedVCFbySample += (sample -> mergeSVcalls.output)
      } else {
        outputMergedVCFbySample += (sample -> callers.head.outputVCF(sample))
84 85 86
      }
    }

87 88 89 90 91 92 93
    if (inputBams.size > 1) {
      // merge all files from all samples in project
      val mergeSVcallsProject = new Pysvtools(this)
      mergeSVcallsProject.input = outputMergedVCFbySample.values.toList
      mergeSVcallsProject.output = outputMergedVCF
      add(mergeSVcallsProject)
    }
94 95 96 97 98
    // merging the VCF calls by project
    // basicly this will do all samples from this pipeline run
    // group by "tags"
    // sample tagging is however not available within this pipeline

Peter van 't Hof's avatar
Peter van 't Hof committed
99 100 101 102
    addSummaryJobs()
  }

  /** Will generate all available variantcallers */
103
  protected def callersList: List[SvCaller] = List(new Breakdancer(this), new Clever(this), new Delly(this), new Pindel(this))
Peter van 't Hof's avatar
Peter van 't Hof committed
104 105 106 107 108 109 110 111

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

  /** Settings for the summary */
  def summarySettings = Map("sv_callers" -> configCallers.toList)

  /** Files for the summary */
112
  def summaryFiles: Map[String, File] = Map("final_mergedvcf" -> (if (inputBams.size > 1) outputMergedVCF else outputMergedVCFbySample.values.head))
Peter van 't Hof's avatar
Peter van 't Hof committed
113 114 115
}

object ShivaSvCalling extends PipelineCommand