CollectMultipleMetrics.scala 4.38 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
3
4
5
6
package nl.lumc.sasc.biopet.extensions.picard

import java.io.File

import nl.lumc.sasc.biopet.core.BiopetQScript
import nl.lumc.sasc.biopet.core.config.Configurable
7
import nl.lumc.sasc.biopet.core.summary.{ Summarizable, SummaryQScript }
Peter van 't Hof's avatar
Peter van 't Hof committed
8
9
10
11
12
import org.broadinstitute.gatk.utils.commandline.{ Output, Argument, Input }

/**
 * Created by pjvan_thof on 4/16/15.
 */
13
class CollectMultipleMetrics(val root: Configurable) extends Picard with Summarizable {
Peter van 't Hof's avatar
Peter van 't Hof committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  import CollectMultipleMetrics._

  javaMainClass = new picard.analysis.CollectMultipleMetrics().getClass.getName

  @Input(doc = "The input SAM or BAM files to analyze", required = true)
  var input: File = null

  @Output(doc = "Base name of output files", required = true)
  var outputName: File = null

  @Argument(doc = "Base name of output files", required = true)
  var program: List[String] = config("metrics_programs",
    default = Programs.values.iterator.toList.map(_.toString))

  @Argument(doc = "Assume alignment file is sorted by position", required = false)
  var assumeSorted: Boolean = config("assume_sorted", default = false)

  @Argument(doc = "Stop after processing N reads", required = false)
  var stopAfter: Option[Long] = config("stop_after")

  @Output
  protected var outputFiles: List[File] = Nil

  override def beforeGraph: Unit = {
    program.foreach(p => p match {
      case _ if p == Programs.CollectAlignmentSummaryMetrics.toString => {
        outputFiles :+= new File(outputName + ".alignment_summary_metrics")
      }
      case _ if p == Programs.CollectInsertSizeMetrics.toString => {
        outputFiles :+= new File(outputName + ".insert_size_metrics")
        outputFiles :+= new File(outputName + ".insert_size_Histogram.pdf")
      }
      case _ if p == Programs.QualityScoreDistribution.toString => {
        outputFiles :+= new File(outputName + ".quality_distribution_metrics")
        outputFiles :+= new File(outputName + ".test.quality_distribution.pdf")
      }
      case _ if p == Programs.MeanQualityByCycle.toString => {
        outputFiles :+= new File(outputName + ".quality_by_cycle_metrics")
        outputFiles :+= new File(outputName + ".quality_by_cycle.pdf")
      }
      case _ if p == Programs.CollectBaseDistributionByCycle.toString => {
        outputFiles :+= new File(outputName + ".base_distribution_by_cycle_metrics")
        outputFiles :+= new File(outputName + ".base_distribution_by_cycle.pdf")
      }
      case _ => BiopetQScript.addError("Program '" + p + "' does not exist for 'CollectMultipleMetrics'")
    })
  }

  override def commandLine = super.commandLine +
    required("INPUT=", input, spaceSeparated = false) +
    required("OUTPUT=", outputName, spaceSeparated = false) +
Peter van 't Hof's avatar
Peter van 't Hof committed
65
66
    conditional(assumeSorted, "ASSUME_SORTED=true") +
    optional("STOP_AFTER=", stopAfter, spaceSeparated = false) +
Peter van 't Hof's avatar
Peter van 't Hof committed
67
    repeat("PROGRAM=", program, spaceSeparated = false)
68
69
70

  override def addToQscriptSummary(qscript: SummaryQScript, name: String): Unit = {
    program.foreach(p => {
Peter van 't Hof's avatar
Peter van 't Hof committed
71
      val stats: Any = p match {
72
        case _ if p == Programs.CollectAlignmentSummaryMetrics.toString =>
Peter van 't Hof's avatar
Peter van 't Hof committed
73
          Picard.getMetrics(new File(outputName + ".alignment_summary_metrics"), groupBy = Some("CATEGORY"))
74
        case _ if p == Programs.CollectInsertSizeMetrics.toString =>
75
          Map(
Peter van 't Hof's avatar
Peter van 't Hof committed
76
77
            "metrics" -> Picard.getMetrics(new File(outputName + ".insert_size_metrics")),
            "histogram" -> Picard.getHistogram(new File(outputName + ".insert_size_metrics"))
78
          )
79
        case _ if p == Programs.QualityScoreDistribution.toString =>
Peter van 't Hof's avatar
Peter van 't Hof committed
80
          Picard.getHistogram(new File(outputName + ".quality_distribution_metrics"))
81
        case _ if p == Programs.MeanQualityByCycle.toString =>
Peter van 't Hof's avatar
Peter van 't Hof committed
82
          Picard.getHistogram(new File(outputName + ".quality_by_cycle_metrics"))
83
        case _ if p == Programs.CollectBaseDistributionByCycle.toString =>
Peter van 't Hof's avatar
Peter van 't Hof committed
84
85
          Picard.getMetrics(new File(outputName + ".base_distribution_by_cycle_metrics"))
        case _ => None
86
87
88
      }
      val sum = new Summarizable {
        override def summaryFiles: Map[String, File] = Map()
Peter van 't Hof's avatar
Peter van 't Hof committed
89
        override def summaryStats = stats
90
91
92
93
94
95
96
97
98
      }
      qscript.addSummarizable(sum, p)
    })

  }

  def summaryFiles = Map()

  def summaryStats = Map()
Peter van 't Hof's avatar
Peter van 't Hof committed
99
100
101
102
103
104
105
}

object CollectMultipleMetrics {
  object Programs extends Enumeration {
    val CollectAlignmentSummaryMetrics, CollectInsertSizeMetrics, QualityScoreDistribution, MeanQualityByCycle, CollectBaseDistributionByCycle = Value
  }
}