ShivaReport.scala 8.13 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
import nl.lumc.sasc.biopet.pipelines.flexiprep.FlexiprepReport

/**
13
14
 * With this extension the report is executed within a pipeline
 *
15
16
 * Created by pjvan_thof on 3/30/15.
 */
17
18
class ShivaReport(val root: Configurable) extends ReportBuilderExtension {
  val builder = ShivaReport
Peter van 't Hof's avatar
Peter van 't Hof committed
19
20

  override val defaultCoreMemory = 3.0
21
22
}

23
object ShivaReport extends MultisampleReportBuilder {
24
25
26
27

  // FIXME: Not yet finished

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

57
58
59
60
61
62
63
  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
64
      ReportPage(
65
        List(),
Peter van 't Hof's avatar
Peter van 't Hof committed
66
67
68
        List("Coverage" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/covstatsMultiTable.ssp")),
        Map("target" -> name)
      )
69
70
71
    }

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

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

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

93
  def filesPage = ReportPage(List(), List(
94
95
    "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"),
96
97
98
    "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")),
99
100
101
    "VCF files" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/outputVcfFiles.ssp", Map("sampleId" -> None))
  ), Map())

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

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

128
  def reportName = "Shiva Report"
129

130
  def variantSummaryPlot(outputDir: File,
131
132
133
134
                         prefix: String,
                         summary: Summary,
                         libraryLevel: Boolean = false,
                         sampleId: Option[String] = None): Unit = {
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    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) {
160
      for (
161
        sample <- summary.samples if sampleId.isEmpty || sample == sampleId.get;
162
163
        lib <- summary.libraries(sample)
      ) {
164
165
166
        tsvWriter.println(getLine(summary, sample, Some(lib)))
      }
    } else {
167
      for (sample <- summary.samples if sampleId.isEmpty || sample == sampleId.get) {
168
169
170
171
172
173
174
175
176
177
        tsvWriter.println(getLine(summary, sample))
      }
    }

    tsvWriter.close()

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