BamStats.scala 2.45 KB
Newer Older
1 2 3 4
package nl.lumc.sasc.biopet.extensions.tools

import java.io.File

5
import nl.lumc.sasc.biopet.core.summary.Summarizable
6
import nl.lumc.sasc.biopet.core.{Reference, ToolCommandFunction}
7
import nl.lumc.sasc.biopet.utils.ConfigUtils
8
import nl.lumc.sasc.biopet.utils.config.Configurable
9
import org.broadinstitute.gatk.utils.commandline.{Input, Output}
10 11

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
12 13
 * Created by pjvanthof on 18/11/2016.
 */
14
class BamStats(val root: Configurable) extends ToolCommandFunction with Reference with Summarizable {
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  def toolObject = nl.lumc.sasc.biopet.tools.bamstats.BamStats

  @Input(required = true)
  var reference: File = _

  @Input(required = true)
  var bamFile: File = _

  var outputDir: File = _

  var binSize: Option[Int] = config("bin_size")
  var threadBinSize: Option[Int] = config("thread_bin_size")

  override def defaultThreads = 3
  override def defaultCoreMemory = 5.0
Peter van 't Hof's avatar
Peter van 't Hof committed
30
  override def dictRequired = true
31

32
  def getOutputFile(name: String, contig: Option[String] = None): File = {
33
    contig match {
34
      case Some(contig) => new File(outputDir, "contigs" + File.separator + contig + File.separator + name)
Peter van 't Hof's avatar
Peter van 't Hof committed
35
      case _            => new File(outputDir, name)
36 37 38
    }
  }

39 40 41
  @Output
  private var outputFiles: List[File] = Nil

Peter van 't Hof's avatar
Peter van 't Hof committed
42
  def bamstatsSummary: File = new File(outputDir, "bamstats.summary.json")
43 44 45 46
  def flagstatSummaryFile(contig: Option[String] = None): File = getOutputFile("flagstats.summary.json", contig)
  def mappingQualityFile(contig: Option[String] = None): File = getOutputFile("mapping_quality.tsv", contig)
  def clipingFile(contig: Option[String] = None): File = getOutputFile("clipping.tsv", contig)

Peter van 't Hof's avatar
Peter van 't Hof committed
47 48
  override def beforeGraph() {
    super.beforeGraph()
Peter van 't Hof's avatar
Peter van 't Hof committed
49
    deps :+= new File(bamFile.getAbsolutePath.replaceAll(".bam$", ".bai"))
50 51 52 53
    outputFiles :+= bamstatsSummary
    outputFiles :+= flagstatSummaryFile()
    outputFiles :+= mappingQualityFile()
    outputFiles :+= clipingFile()
Peter van 't Hof's avatar
Peter van 't Hof committed
54
    jobOutputFile = new File(outputDir, ".bamstats.out")
Peter van 't Hof's avatar
Peter van 't Hof committed
55
    if (reference == null) reference = referenceFasta()
Peter van 't Hof's avatar
Peter van 't Hof committed
56 57
  }

58 59 60 61 62
  /** Creates command to execute extension */
  override def cmdLine = super.cmdLine +
    required("-b", bamFile) +
    required("-o", outputDir) +
    required("-R", reference) +
Peter van 't Hof's avatar
Peter van 't Hof committed
63
    optional("--binSize", binSize) +
64 65
    optional("--threadBinSize", threadBinSize)

66 67
  def summaryFiles: Map[String, File] = Map()

Peter van 't Hof's avatar
Peter van 't Hof committed
68
  def summaryStats: Map[String, Any] = ConfigUtils.fileToConfigMap(bamstatsSummary)
69 70

  override def summaryDeps: List[File] = bamstatsSummary :: super.summaryDeps
71
}