ShivaReport.scala 7.85 KB
Newer Older
1
2
package nl.lumc.sasc.biopet.pipelines.shiva

3
import java.io.{ PrintWriter, File }
4

5
import nl.lumc.sasc.biopet.core.config.Configurable
Peter van 't Hof's avatar
Peter van 't Hof committed
6
import nl.lumc.sasc.biopet.core.report._
7
import nl.lumc.sasc.biopet.core.summary.{ SummaryValue, Summary }
8
import nl.lumc.sasc.biopet.extensions.rscript.StackedBarPlot
9
import nl.lumc.sasc.biopet.pipelines.bammetrics.BammetricsReport
10
11
12
13
14
import nl.lumc.sasc.biopet.pipelines.flexiprep.FlexiprepReport

/**
 * Created by pjvan_thof on 3/30/15.
 */
15
16
class ShivaReport(val root: Configurable) extends ReportBuilderExtension {
  val builder = ShivaReport
Peter van 't Hof's avatar
Peter van 't Hof committed
17
18

  override val defaultCoreMemory = 3.0
19
20
}

21
object ShivaReport extends MultisampleReportBuilder {
22
23
24
25

  // FIXME: Not yet finished

  def indexPage = {
26
    val regions = regionsPage
27
    ReportPage(
28
      Map("Samples" -> generateSamplesPage(pageArgs)) ++
29
30
31
32
33
34
        (if (regions.isDefined) Map(regions.get) else Map()) ++
        Map("Files" -> filesPage,
          "Versions" -> ReportPage(Map(), List((
            "Executables" -> ReportSection("/nl/lumc/sasc/biopet/core/report/executables.ssp"
            ))), Map())
        ),
35
36
37
      List(
        "Report" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/shivaFront.ssp"),
        "Variantcalling" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/sampleVariants.ssp",
Peter van 't Hof's avatar
Peter van 't Hof committed
38
          Map("showPlot" -> true, "showTable" -> false)),
39
        "Alignment" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp",
Peter van 't Hof's avatar
Peter van 't Hof committed
40
          Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false)
41
        ),
42
        "Insert Size" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/insertSize.ssp",
Peter van 't Hof's avatar
Peter van 't Hof committed
43
          Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false)),
44
        "QC reads" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp",
Peter van 't Hof's avatar
Peter van 't Hof committed
45
          Map("showPlot" -> true, "showTable" -> false)),
46
        "QC bases" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp",
Peter van 't Hof's avatar
Peter van 't Hof committed
47
          Map("showPlot" -> true, "showTable" -> false))
48
49
50
51
      ),
      pageArgs
    )
  }
52

53
54
55
56
57
58
59
  def regionsPage = {
    val roi = summary.getValue("shiva", "settings", "regions_of_interest")
    val amplicon = summary.getValue("shiva", "settings", "amplicon_bed")

    var regionPages: Map[String, ReportPage] = Map()

    def createPage(name: String, amplicon: Boolean = false): ReportPage = {
Peter van 't Hof's avatar
Peter van 't Hof committed
60
61
62
63
64
      ReportPage(
        Map(),
        List("Coverage" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/covstatsMultiTable.ssp")),
        Map("target" -> name)
      )
65
66
67
68
69
70
71
72
73
74
75
76
77
    }

    amplicon match {
      case Some(x: String) => regionPages += (x + " (Amplicon)") -> createPage(x, true)
      case _               =>
    }

    roi match {
      case Some(x: String)  => regionPages += x -> createPage(x, false)
      case Some(x: List[_]) => x.foreach(x => regionPages += x.toString -> createPage(x.toString, false))
      case _                =>
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
78
79
80
81
82
83
84
85
    if (regionPages.nonEmpty) Some("Regions" -> ReportPage(
      Map(),
      regionPages.map(p => (p._1 -> ReportSection(
        "/nl/lumc/sasc/biopet/pipelines/bammetrics/covstatsMultiTable.ssp",
        Map("target" -> p._1.stripSuffix(" (Amplicon)"))
      ))).toList.sortBy(_._1),
      Map())
    ) else None
86
87
  }

88
89
90
  def filesPage = ReportPage(Map(), List(
    "Input fastq files" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepInputfiles.ssp"),
    "After QC fastq files" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepOutputfiles.ssp"),
91
92
93
    "Bam files per lib" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/mapping/outputBamfiles.ssp", Map("sampleLevel" -> false)),
    "Preprocessed bam files" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/mapping/outputBamfiles.ssp",
      Map("pipelineName" -> "shiva", "fileTag" -> "preProcessBam")),
94
95
96
    "VCF files" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/outputVcfFiles.ssp", Map("sampleId" -> None))
  ), Map())

97
  def samplePage(sampleId: String, args: Map[String, Any]) = {
98
99
    ReportPage(Map(
      "Libraries" -> generateLibraryPage(args),
100
      "Alignment" -> BammetricsReport.bamMetricsPage(summary, Some(sampleId), None),
101
      "Files" -> filesPage
102
    ), List(
103
104
      "Alignment" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp",
        if (summary.libraries(sampleId).size > 1) Map("showPlot" -> true) else Map()),
105
      "Preprocessing" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp", Map("sampleLevel" -> true)),
Peter van 't Hof's avatar
Peter van 't Hof committed
106
      "Variantcalling" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/sampleVariants.ssp"),
107
108
      "QC reads" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp"),
      "QC bases" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp")
109
110
111
    ), args)
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
112
  def libraryPage(sampleId: String, libId: String, args: Map[String, Any]) = {
113
    ReportPage(Map(
114
      "Alignment" -> BammetricsReport.bamMetricsPage(summary, Some(sampleId), Some(libId)),
115
116
117
      "QC" -> FlexiprepReport.flexiprepPage
    ), List(
      "Alignment" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp"),
118
119
120
      "QC reads" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp"),
      "QC bases" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp")
    ), args)
121
122
  }

123
  def reportName = "Shiva Report"
124

125
  def variantSummaryPlot(outputDir: File,
126
127
128
129
                         prefix: String,
                         summary: Summary,
                         libraryLevel: Boolean = false,
                         sampleId: Option[String] = None): Unit = {
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    val tsvFile = new File(outputDir, prefix + ".tsv")
    val pngFile = new File(outputDir, prefix + ".png")
    val tsvWriter = new PrintWriter(tsvFile)
    if (libraryLevel) tsvWriter.print("Library") else tsvWriter.print("Sample")
    tsvWriter.println("\tHomVar\tHet\tHomRef\tNoCall")

    def getLine(summary: Summary, sample: String, lib: Option[String] = None): String = {
      val homVar = new SummaryValue(List("shivavariantcalling", "stats", "multisample-vcfstats-final", "genotype", "HomVar"),
        summary, Some(sample), lib).value.getOrElse(0).toString.toLong
      val homRef = new SummaryValue(List("shivavariantcalling", "stats", "multisample-vcfstats-final", "genotype", "HomRef"),
        summary, Some(sample), lib).value.getOrElse(0).toString.toLong
      val noCall = new SummaryValue(List("shivavariantcalling", "stats", "multisample-vcfstats-final", "genotype", "NoCall"),
        summary, Some(sample), lib).value.getOrElse(0).toString.toLong
      val het = new SummaryValue(List("shivavariantcalling", "stats", "multisample-vcfstats-final", "genotype", "Het"),
        summary, Some(sample), lib).value.getOrElse(0).toString.toLong
      val sb = new StringBuffer()
      if (lib.isDefined) sb.append(sample + "-" + lib.get + "\t") else sb.append(sample + "\t")
      sb.append(homVar + "\t")
      sb.append(het + "\t")
      sb.append(homRef + "\t")
      sb.append(noCall)
      sb.toString
    }

    if (libraryLevel) {
155
156
157
158
      for (
        sample <- summary.samples if (sampleId.isEmpty || sample == sampleId.get);
        lib <- summary.libraries(sample)
      ) {
159
160
161
162
163
164
165
166
167
168
169
170
171
172
        tsvWriter.println(getLine(summary, sample, Some(lib)))
      }
    } else {
      for (sample <- summary.samples if (sampleId.isEmpty || sample == sampleId.get)) {
        tsvWriter.println(getLine(summary, sample))
      }
    }

    tsvWriter.close()

    val plot = new StackedBarPlot(null)
    plot.input = tsvFile
    plot.output = pngFile
    plot.ylabel = Some("VCF records")
173
174
175
    if (libraryLevel) {
      plot.width = Some(200 + (summary.libraries.filter(s => sampleId.getOrElse(s._1) == s._1).foldLeft(0)(_ + _._2.size) * 10))
    } else plot.width = Some(200 + (summary.samples.filter(s => sampleId.getOrElse(s) == s).size * 10))
176
177
    plot.runLocal()
  }
178
}