ShivaReport.scala 11 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Biopet is built on top of GATK Queue for building bioinformatic
 * pipelines. It is mainly intended to support LUMC SHARK cluster which is running
 * SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
 * should also be able to execute Biopet tools and pipelines.
 *
 * Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
 *
 * Contact us at: sasc@lumc.nl
 *
 * A dual licensing mode is applied. The source code within this project that are
 * not part of GATK Queue is freely available for non-commercial use under an AGPL
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
16
17
package nl.lumc.sasc.biopet.pipelines.shiva

Peter van 't Hof's avatar
Peter van 't Hof committed
18
import java.io.{ File, PrintWriter }
19

Peter van 't Hof's avatar
Peter van 't Hof committed
20
import nl.lumc.sasc.biopet.utils.config.Configurable
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import nl.lumc.sasc.biopet.core.report._
22
import nl.lumc.sasc.biopet.utils.summary.{ Summary, SummaryValue }
23
import nl.lumc.sasc.biopet.utils.rscript.StackedBarPlot
24
import nl.lumc.sasc.biopet.pipelines.bammetrics.BammetricsReport
25
26
27
import nl.lumc.sasc.biopet.pipelines.flexiprep.FlexiprepReport

/**
28
29
 * With this extension the report is executed within a pipeline
 *
30
31
 * Created by pjvan_thof on 3/30/15.
 */
32
class ShivaReport(val root: Configurable) extends ReportBuilderExtension {
33
  def builder = ShivaReport
34
35
}

36
/** Object for report generation for Shiva pipeline */
37
object ShivaReport extends MultisampleReportBuilder {
38

39
  def variantcallingExecuted = summary.getValue("shiva", "settings", "multisample_variantcalling") match {
Peter van 't Hof's avatar
Peter van 't Hof committed
40
    case Some(true) => true
Peter van 't Hof's avatar
Peter van 't Hof committed
41
    case _          => false
42
43
  }

44
45
46
  override def extFiles = super.extFiles ++ List("js/gears.js")
    .map(x => ExtFile("/nl/lumc/sasc/biopet/pipelines/gears/report/ext/" + x, x))

47
  /** Root page for the shiva report */
48
  def indexPage = {
49
    val regions = regionsPage
50
    ReportPage(
51
      List("Samples" -> generateSamplesPage(pageArgs)) ++
52
        (if (regions.isDefined) Map(regions.get) else Map()) ++
Peter van 't Hof's avatar
Peter van 't Hof committed
53
54
55
56
        Map("Reference" -> ReportPage(List(), List(
          "Reference" -> ReportSection("/nl/lumc/sasc/biopet/core/report/reference.ssp", Map("pipeline" -> "shiva"))
        ), Map()),
          "Files" -> filesPage,
Peter van 't Hof's avatar
Peter van 't Hof committed
57
58
59
          "Versions" -> ReportPage(List(), List(
            "Executables" -> ReportSection("/nl/lumc/sasc/biopet/core/report/executables.ssp")
          ), Map())
60
        ),
61
      List(
62
63
        "Report" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/shivaFront.ssp")) ++
        (if (variantcallingExecuted) List("Variantcalling" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/sampleVariants.ssp",
Peter van 't Hof's avatar
Peter van 't Hof committed
64
65
          Map("showPlot" -> true, "showTable" -> false)))
        else Nil) ++
66
        List("Alignment" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp",
Peter van 't Hof's avatar
Peter van 't Hof committed
67
          Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false)
68
        ),
Peter van 't Hof's avatar
Peter van 't Hof committed
69
70
71
72
73
74
75
76
77
          "Insert Size" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/insertSize.ssp",
            Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false)),
          "Whole genome coverage" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/wgsHistogram.ssp",
            Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false)),
          "QC reads" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp",
            Map("showPlot" -> true, "showTable" -> false)),
          "QC bases" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp",
            Map("showPlot" -> true, "showTable" -> false))
        ),
78
79
80
      pageArgs
    )
  }
81

82
83
84
  //TODO: Add variants per target
  /** Generate a page with all target coverage stats */
  def regionsPage: Option[(String, ReportPage)] = {
85
86
87
88
89
90
    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
91
      ReportPage(
92
        List(),
Peter van 't Hof's avatar
Peter van 't Hof committed
93
94
95
        List("Coverage" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/covstatsMultiTable.ssp")),
        Map("target" -> name)
      )
96
97
98
    }

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

    roi match {
104
105
      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))
106
107
108
      case _                =>
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
109
    if (regionPages.nonEmpty) Some("Regions" -> ReportPage(
110
111
112
113
114
115
      regionPages.map(p => p._1 -> ReportPage(Nil,
        List(
          "Variants" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/sampleVariants.ssp"),
          "Coverage" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/covstatsMultiTable.ssp")
        ),
        Map("target" -> Some(p._1.stripSuffix(" (Amplicon)")))
116
      )).toList.sortBy(_._1),
117
      List(),
Peter van 't Hof's avatar
Peter van 't Hof committed
118
      Map())
Peter van 't Hof's avatar
Peter van 't Hof committed
119
120
    )
    else None
121
122
  }

123
124
  /** Files page, can be used general or at sample level */
  def filesPage: ReportPage = ReportPage(List(), List(
125
126
    "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"),
127
128
    "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",
Peter van 't Hof's avatar
Peter van 't Hof committed
129
130
      Map("pipelineName" -> "shiva", "fileTag" -> "preProcessBam"))) ++
    (if (variantcallingExecuted) List("VCF files" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/outputVcfFiles.ssp",
Peter van 't Hof's avatar
Peter van 't Hof committed
131
132
      Map("sampleId" -> None)))
    else Nil), Map())
133

134
135
  /** Single sample page */
  def samplePage(sampleId: String, args: Map[String, Any]): ReportPage = {
136
    ReportPage(List(
137
      "Libraries" -> generateLibraryPage(args),
138
      "Alignment" -> BammetricsReport.bamMetricsPage(summary, Some(sampleId), None),
139
      "Files" -> filesPage
140
    ), List(
141
142
      "Alignment" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp",
        if (summary.libraries(sampleId).size > 1) Map("showPlot" -> true) else Map()),
143
144
145
      "Preprocessing" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp", Map("sampleLevel" -> true))) ++
      (if (variantcallingExecuted) List("Variantcalling" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/shiva/sampleVariants.ssp")) else Nil) ++
      List("QC reads" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp"),
Peter van 't Hof's avatar
Peter van 't Hof committed
146
147
        "QC bases" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp")
      ), args)
148
149
  }

150
151
  /** Library page */
  def libraryPage(sampleId: String, libId: String, args: Map[String, Any]): ReportPage = {
152
    val flexiprepExecuted = summary.getLibraryValue(sampleId, libId, "flexiprep").isDefined
153
    val krakenExecuted = summary.getValue(Some(sampleId), Some(libId), "gears", "stats", "krakenreport").isDefined
154

155
156
157
    ReportPage(
      "Alignment" -> BammetricsReport.bamMetricsPage(summary, Some(sampleId), Some(libId)) ::
        (if (flexiprepExecuted) List("QC" -> FlexiprepReport.flexiprepPage) else Nil
158
        ) ::: (if (krakenExecuted) List("Gears - Metagenomics" -> ReportPage(List(), List(
Peter van 't Hof's avatar
Peter van 't Hof committed
159
160
161
          "Sunburst analysis" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/gears/gearsSunburst.ssp"
          )), Map()))
        else Nil), "Alignment" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp") ::
162
163
164
165
166
        (if (flexiprepExecuted) List(
          "QC reads" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp"),
          "QC bases" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp")
        )
        else Nil), args)
167
168
  }

169
  /** Name of the report */
170
  def reportName = "Shiva Report"
171

172
173
174
175
176
177
178
179
  /**
   * Generate a stackbar plot for found variants
   * @param outputDir OutputDir for the tsv and png file
   * @param prefix Prefix of the tsv and png file
   * @param summary Summary class
   * @param libraryLevel Default false, when set true plot will be based on library stats instead of sample stats
   * @param sampleId Default it selects all sampples, when sample is giving it limits to selected sample
   */
180
  def variantSummaryPlot(outputDir: File,
181
182
183
                         prefix: String,
                         summary: Summary,
                         libraryLevel: Boolean = false,
184
185
186
                         sampleId: Option[String] = None,
                         caller: String = "final",
                         target: Option[String] = None): Unit = {
187
188
189
190
191
192
193
    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 = {
194
195
      val path = target match {
        case Some(t) => List("shivavariantcalling", "stats", s"multisample-vcfstats-$caller-$t", "genotype")
Peter van 't Hof's avatar
Peter van 't Hof committed
196
        case _       => List("shivavariantcalling", "stats", s"multisample-vcfstats-$caller", "genotype")
197
198
199
200
201
      }
      val homVar = new SummaryValue(path :+ "HomVar", summary, Some(sample), lib).value.getOrElse(0).toString.toLong
      val homRef = new SummaryValue(path :+ "HomRef", summary, Some(sample), lib).value.getOrElse(0).toString.toLong
      val noCall = new SummaryValue(path :+ "NoCall", summary, Some(sample), lib).value.getOrElse(0).toString.toLong
      val het = new SummaryValue(path :+ "Het", summary, Some(sample), lib).value.getOrElse(0).toString.toLong
202
203
204
205
206
207
208
209
210
211
      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) {
212
      for (
213
        sample <- summary.samples if sampleId.isEmpty || sample == sampleId.get;
214
215
        lib <- summary.libraries(sample)
      ) {
216
217
218
        tsvWriter.println(getLine(summary, sample, Some(lib)))
      }
    } else {
219
      for (sample <- summary.samples if sampleId.isEmpty || sample == sampleId.get) {
220
221
222
223
224
225
226
227
228
229
        tsvWriter.println(getLine(summary, sample))
      }
    }

    tsvWriter.close()

    val plot = new StackedBarPlot(null)
    plot.input = tsvFile
    plot.output = pngFile
    plot.ylabel = Some("VCF records")
230
231
    if (libraryLevel) {
      plot.width = Some(200 + (summary.libraries.filter(s => sampleId.getOrElse(s._1) == s._1).foldLeft(0)(_ + _._2.size) * 10))
232
    } else plot.width = Some(200 + (summary.samples.count(s => sampleId.getOrElse(s) == s) * 10))
233
234
    plot.runLocal()
  }
235
}