BamStats.scala 1.98 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 9 10
import nl.lumc.sasc.biopet.utils.config.Configurable
import org.broadinstitute.gatk.utils.commandline.Input

11 12
import scala.collection.JavaConversions._

13 14 15
/**
  * Created by pjvanthof on 18/11/2016.
  */
16
class BamStats(val root: Configurable) extends ToolCommandFunction with Reference with Summarizable {
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  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
32
  override def dictRequired = true
33

34 35 36 37 38 39 40
  def flagstatSummaryFile(implicit contig: Option[String] = None): File = {
    contig match {
      case Some(contig) => new File(outputDir, "contigs" + File.separator + contig + File.separator + "flagstats.summary.json")
      case _ => new File(outputDir, "flagstats.summary.json")
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
41 42 43
  override def beforeGraph() {
    super.beforeGraph()
    jobOutputFile = new File(outputDir, ".bamstats.out")
Peter van 't Hof's avatar
Peter van 't Hof committed
44
    if (reference == null) reference = referenceFasta()
Peter van 't Hof's avatar
Peter van 't Hof committed
45 46
  }

47 48 49 50 51
  /** 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
52
    optional("--binSize", binSize) +
53 54
    optional("--threadBinSize", threadBinSize)

55 56 57 58 59 60 61 62 63

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

  def summaryStats: Map[String, Any] = Map(
    "flagstats" -> ConfigUtils.fileToConfigMap(flagstatSummaryFile),
    "flagstats_per_contig" -> referenceDict.getSequences.map {
      c => c.getSequenceName -> ConfigUtils.fileToConfigMap(flagstatSummaryFile(Some(c.getSequenceName)))
    }.toMap
  )
64
}