CollectMultipleMetrics.scala 4.8 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
  import CollectMultipleMetrics._

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

bow's avatar
bow committed
18
19
  override val defaultCoreMemory = 6.0

Peter van 't Hof's avatar
Peter van 't Hof committed
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
  @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")
46
        outputFiles :+= new File(outputName + ".insert_size_histogram.pdf")
Peter van 't Hof's avatar
Peter van 't Hof committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
      }
      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
67
68
    conditional(assumeSorted, "ASSUME_SORTED=true") +
    optional("STOP_AFTER=", stopAfter, spaceSeparated = false) +
Peter van 't Hof's avatar
Peter van 't Hof committed
69
    repeat("PROGRAM=", program, spaceSeparated = false)
70
71
72

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

  }

  def summaryStats = Map()
99
100
101
102
103
104
105
106
107
108

  def summaryFiles = {
    program.map {
      case p if p == Programs.CollectInsertSizeMetrics.toString =>
        Map(
          "insert_size_histogram" -> new File(outputName + ".insert_size_histogram.pdf"),
          "insert_size_metrics" -> new File(outputName + ".insert_size_metrics"))
      case otherwise => Map()
    }.foldLeft(Map.empty[String, File]) { case (acc, m) => (acc ++ m) }
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
109
110
111
112
113
114
115
}

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