ShivaReport.scala 6.03 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
17
18
class ShivaReport(val root: Configurable) extends ReportBuilderExtension {
  val builder = ShivaReport
}

19
object ShivaReport extends MultisampleReportBuilder {
20
21
22
23
24
25

  // FIXME: Not yet finished

  def indexPage = {
    ReportPage(
      Map(
26
        "Samples" -> generateSamplesPage(pageArgs),
27
        "Files" -> filesPage,
28
        "Versions" -> ReportPage(Map(), List(), Map())
29
30
31
32
      ),
      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
33
          Map("showPlot" -> true, "showTable" -> true)),
34
        "Alignment" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp",
Peter van 't Hof's avatar
Peter van 't Hof committed
35
          Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> true)
36
        ),
37
        "Insert Size" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/insertSize.ssp",
Peter van 't Hof's avatar
Peter van 't Hof committed
38
          Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> true)),
39
        "QC reads" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp",
Peter van 't Hof's avatar
Peter van 't Hof committed
40
          Map("showPlot" -> true, "showTable" -> true)),
41
        "QC bases" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp",
Peter van 't Hof's avatar
Peter van 't Hof committed
42
          Map("showPlot" -> true, "showTable" -> true))
43
44
45
46
      ),
      pageArgs
    )
  }
47

48
49
50
51
52
53
54
55
  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"),
    "Bam files per lib" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/mapping/outputBamfiles.ssp"),
    "Preprocessed bam files" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/outputBamfiles.ssp"),
    "VCF files" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/outputVcfFiles.ssp", Map("sampleId" -> None))
  ), Map())

56
  def samplePage(sampleId: String, args: Map[String, Any]) = {
57
58
    ReportPage(Map(
      "Libraries" -> generateLibraryPage(args),
59
60
      "Alignment" -> BammetricsReport.bamMetricsPage,
      "Files" -> filesPage
61
    ), List(
62
63
      "Alignment" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp",
        if (summary.libraries(sampleId).size > 1) Map("showPlot" -> true) else Map()),
64
      "Preprocessing" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp", Map("sampleLevel" -> true)),
Peter van 't Hof's avatar
Peter van 't Hof committed
65
      "Variantcalling" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/sampleVariants.ssp"),
66
67
      "QC reads" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp"),
      "QC bases" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp")
68
69
70
71
    ), args)
  }

  def libraryPage(libId: String, args: Map[String, Any]) = {
72
73
74
75
76
    ReportPage(Map(
      "Alignment" -> BammetricsReport.bamMetricsPage,
      "QC" -> FlexiprepReport.flexiprepPage
    ), List(
      "Alignment" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp"),
77
78
79
      "QC reads" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp"),
      "QC bases" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp")
    ), args)
80
81
  }

82
  def reportName = "Shiva Report"
83

84
  def variantSummaryPlot(outputDir: File,
85
86
87
88
                         prefix: String,
                         summary: Summary,
                         libraryLevel: Boolean = false,
                         sampleId: Option[String] = None): Unit = {
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    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) {
114
115
116
117
      for (
        sample <- summary.samples if (sampleId.isEmpty || sample == sampleId.get);
        lib <- summary.libraries(sample)
      ) {
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        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")
Peter van 't Hof's avatar
Peter van 't Hof committed
132
    plot.width = Some(1200)
133
134
    plot.runLocal()
  }
135
}