MultisampleMappingReport.scala 14.6 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
  * 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 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.
  */
15 16
package nl.lumc.sasc.biopet.pipelines.mapping

17 18
import nl.lumc.sasc.biopet.core.report.{MultisampleReportBuilder, ReportBuilderExtension, ReportPage, ReportSection}
import nl.lumc.sasc.biopet.pipelines.bammetrics.{BamMetricsAlignmentReport, BammetricsReport}
19
import nl.lumc.sasc.biopet.pipelines.flexiprep.FlexiprepReport
20
import nl.lumc.sasc.biopet.pipelines.gears.GearsKronaPlot
21
import nl.lumc.sasc.biopet.utils.config.Configurable
Peter van 't Hof's avatar
Peter van 't Hof committed
22
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb.Implicts._
23
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb._
24

25
import scala.concurrent.{Await, Future}
Peter van 't Hof's avatar
Peter van 't Hof committed
26 27
import scala.concurrent.duration.Duration

28
/**
29 30
  * Created by pjvanthof on 11/01/16.
  */
Peter van 't Hof's avatar
Peter van 't Hof committed
31
class MultisampleMappingReport(val parent: Configurable) extends ReportBuilderExtension {
32 33 34 35
  def builder = MultisampleMappingReport
}

object MultisampleMappingReport extends MultisampleMappingReportTrait {
36

37 38 39 40 41
  /** Name of the report */
  def reportName = "Mapping Report"
}

trait MultisampleMappingReportTrait extends MultisampleReportBuilder {
42

43
  /** Front section for the report */
44 45
  def frontSection: ReportSection =
    ReportSection("/nl/lumc/sasc/biopet/pipelines/mapping/multisampleMappingFront.ssp")
46

47 48
  def additionalSections: List[(String, ReportSection)] = Nil

Peter van 't Hof's avatar
Peter van 't Hof committed
49 50
  def pipelineName = "multisamplemapping"

51 52 53 54 55 56 57
  override def extFiles =
    super.extFiles ++ List("js/gears.js",
                           "js/krona-2.0.js",
                           "img/krona/loading.gif",
                           "img/krona/hidden.png",
                           "img/krona/favicon.ico")
      .map(x => ExtFile("/nl/lumc/sasc/biopet/pipelines/gears/report/ext/" + x, x))
Peter van 't Hof's avatar
Peter van 't Hof committed
58

59
  /** Root page for the carp report */
60
  def indexPage: Future[ReportPage] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
61

62
    val krakenExecuted = Await.result(summary.getStatsSize(runId,
63 64 65 66 67
      "gearskraken",
      "krakenreport",
      library = NoLibrary,
      mustHaveSample = true),
      Duration.Inf) >= 1
68
    val centrifugeExecuted = Await.result(summary.getStatsSize(runId,
69 70 71 72 73
      "gearscentrifuge",
      "centrifuge_report",
      library = NoLibrary,
      mustHaveSample = true),
      Duration.Inf) >= 1
74 75 76 77 78 79 80 81
    val wgsExecuted = Await.result(
      summary.getStatsSize(runId, "bammetrics", "wgs", library = NoLibrary, mustHaveSample = true),
      Duration.Inf) >= 1
    val rnaExecuted = Await.result(
      summary.getStatsSize(runId, "bammetrics", "rna", library = NoLibrary, mustHaveSample = true),
      Duration.Inf) >= 1
    val insertsizeExecuted = summary
      .getStatsForSamples(runId,
82 83 84
        "bammetrics",
        "CollectInsertSizeMetrics",
        keyValues = Map("metrics" -> List("metrics")))
85 86 87 88 89
      .exists(_._2("metrics").isDefined)
    val mappingExecuted = Await.result(
      summary.getStatsSize(runId, "mapping", NoModule, mustHaveLibrary = true),
      Duration.Inf) >= 1
    val mappingSettings = summary.getSettingsForLibraries(runId,
90 91 92 93
      "mapping",
      NoModule,
      keyValues =
        Map("paired" -> List("paired")))
Peter van 't Hof's avatar
Peter van 't Hof committed
94
    val pairedFound = !mappingExecuted || mappingSettings.exists(_._2.exists(_._2 == Option(true)))
95 96
    val flexiprepExecuted = Await
      .result(summary.getStatsSize(runId, "flexiprep", mustHaveLibrary = true), Duration.Inf) >= 1
Peter van 't Hof's avatar
Peter van 't Hof committed
97

98 99 100 101
    val krakenDustbinAnalysis = GearsKronaPlot.values(summary, runId, "gearskraken", "krakenreport", samples, libraries, sampleId, libId )
    val centrifugeAnalysis = GearsKronaPlot.values(summary, runId, "gearscentrifuge", "centrifuge_report", samples, libraries, sampleId, libId )
    val centrifugeUniqueAnalysis = GearsKronaPlot.values(summary, runId, "gearscentrifuge", "centrifuge_unique_report", samples, libraries, sampleId, libId )
    val alignmentSummary = BamMetricsAlignmentReport.values(summary, runId, samples, libraries, sampleId, libId, sampleLevel =true,showPlot=true,showTable=false)
102
    val mappingQualitySummary= BammetricsReport.mappingQualityPlotSummary(summary)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    Future {
      ReportPage(
        List("Samples" -> generateSamplesPage(pageArgs)) ++
          (if (krakenExecuted)
            List(
              "Dustbin analysis - Kraken" -> Future.successful(
                ReportPage(List(),
                  List("Krona plot" -> ReportSection(
                    "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",krakenDustbinAnalysis)),
                  Map())))
          else Nil) ++ (if (centrifugeExecuted)
          List(
            "Centrifuge analysis" -> Future.successful(ReportPage(
              List("Non-unique" -> Future.successful(
                ReportPage(List(),
                  List("All mappings" -> ReportSection(
                    "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
                    centrifugeAnalysis)),
                  Map()))),
              List("Unique mappings" -> ReportSection(
                "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
                centrifugeUniqueAnalysis)),
              Map("summaryModuleTag" -> "gearscentrifuge",
                "centrifugeTag" -> Some("centrifuge"))
            )))
        else Nil) ++
          List(
            "Reference" -> Future.successful(
              ReportPage(
                List(),
                List(
                  "Reference" -> ReportSection("/nl/lumc/sasc/biopet/core/report/reference.ssp",
                    Map("pipeline" -> pipelineName))
                ),
                Map()))),
        List("Report" -> frontSection) ++
          additionalSections ++
          List(
            "Alignment" -> ReportSection(
              "/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp",
              alignmentSummary),
            "Mapping Quality" -> ReportSection(
              "/nl/lumc/sasc/biopet/pipelines/bammetrics/mappingQuality.ssp",
              Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false)),
            "Clipping" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/clipping.ssp",
              Map("sampleLevel" -> true,
                "showPlot" -> true,
                "showTable" -> false))
          ) ++
          (if (pairedFound && insertsizeExecuted)
            List(
              "Insert Size" -> ReportSection(
                "/nl/lumc/sasc/biopet/pipelines/bammetrics/insertSize.ssp",
                Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false)))
          else Nil) ++
          (if (wgsExecuted)
            List(
              "Whole genome coverage" -> ReportSection(
                "/nl/lumc/sasc/biopet/pipelines/bammetrics/wgsHistogram.ssp",
                Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false)))
          else Nil) ++
          (if (rnaExecuted)
            List(
              "Rna coverage" -> ReportSection(
                "/nl/lumc/sasc/biopet/pipelines/bammetrics/rnaHistogram.ssp",
                Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false)))
          else Nil) ++
          (if (flexiprepExecuted)
            List(
              "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))
            )
          else Nil),
        pageArgs ++ Map("pipeline" -> pipelineName)
      )
    }
183 184 185
  }

  /** Single sample page */
Peter van 't Hof's avatar
Peter van 't Hof committed
186
  def samplePage(sampleId: Int, args: Map[String, Any]): Future[ReportPage] = Future {
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    val krakenExecuted = Await.result(summary.getStatsSize(runId,
                                                           "gearskraken",
                                                           "krakenreport",
                                                           library = NoLibrary,
                                                           sample = sampleId),
                                      Duration.Inf) >= 1
    val centrifugeExecuted = Await.result(summary.getStatsSize(runId,
                                                               "gearscentrifuge",
                                                               "centrifuge_report",
                                                               library = NoLibrary,
                                                               sample = sampleId,
                                                               mustHaveSample = true),
                                          Duration.Inf) >= 1
    val flexiprepExecuted = Await.result(
      summary.getStatsSize(runId, "flexiprep", sample = sampleId, mustHaveLibrary = true),
      Duration.Inf) >= 1

    ReportPage(
      List("Libraries" -> generateLibraryPage(args),
           "Alignment" -> BammetricsReport.bamMetricsPage(summary, Some(sampleId), None)) ++
        (if (centrifugeExecuted)
           List(
rhpvorderman's avatar
rhpvorderman committed
209
             "Centrifuge analysis" -> Future.successful(ReportPage(
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
               List("Non-unique" -> Future.successful(ReportPage(
                 List(),
                 List("All mappings" -> ReportSection(
                   "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
                   Map("summaryStatsTag" -> "centrifuge_report",
                       "centrifugeTag" -> Some("centrifuge")))),
                 Map()
               ))),
               List("Unique mappings" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
                 Map("summaryStatsTag" -> "centrifuge_unique_report"))),
               Map("summaryModuleTag" -> "gearscentrifuge")
             )))
         else
           Nil) ::: (if (krakenExecuted)
                       List(
                         "Dustbin analysis" -> Future.successful(
                           ReportPage(List(),
                                      List("Krona Plot" -> ReportSection(
                                        "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp")),
                                      Map())))
                     else Nil),
      List(
        "Alignment" -> ReportSection(
          "/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp",
          Map("showPlot" -> true)),
        "Preprocessing" -> ReportSection(
          "/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp",
          Map("sampleLevel" -> true))
      ) ++
        (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
    )
250 251 252
  }

  /** Library page */
Peter van 't Hof's avatar
Peter van 't Hof committed
253
  def libraryPage(sampleId: Int, libId: Int, args: Map[String, Any]): Future[ReportPage] = Future {
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    val krakenExecuted = Await.result(summary.getStatsSize(runId,
                                                           "gearskraken",
                                                           "krakenreport",
                                                           library = libId,
                                                           sample = sampleId),
                                      Duration.Inf) >= 1
    val centrifugeExecuted = Await.result(summary.getStatsSize(runId,
                                                               "gearscentrifuge",
                                                               "centrifuge_report",
                                                               library = libId,
                                                               sample = sampleId,
                                                               mustHaveSample = true),
                                          Duration.Inf) >= 1
    val flexiprepExecuted = Await.result(summary.getStatsSize(runId,
                                                              "flexiprep",
                                                              library = libId,
                                                              sample = sampleId,
                                                              mustHaveLibrary = true),
                                         Duration.Inf) >= 1
Peter van 't Hof's avatar
Peter van 't Hof committed
273

Peter van 't Hof's avatar
Peter van 't Hof committed
274 275
    ReportPage(
      ("Alignment" -> BammetricsReport.bamMetricsPage(summary, Some(sampleId), Some(libId))) ::
rhpvorderman's avatar
rhpvorderman committed
276 277 278
        (if (flexiprepExecuted)
         List("QC" -> FlexiprepReport.flexiprepPage(summary, sampleId, libId))
       else Nil) :::
279
        (if (centrifugeExecuted)
rhpvorderman's avatar
rhpvorderman committed
280
         List("Centrifuge analysis" -> Future.successful(ReportPage(
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
           List("Non-unique" -> Future.successful(
             ReportPage(List(),
                        List("All mappings" -> ReportSection(
                          "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
                          Map("summaryStatsTag" -> "centrifuge_report"))),
                        Map()))),
           List("Unique mappings" -> ReportSection(
             "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
             Map("summaryStatsTag" -> "centrifuge_unique_report"))),
           Map("summaryModuleTag" -> "gearscentrifuge", "centrifugeTag" -> Some("centrifuge"))
         )))
       else Nil) ::: (if (krakenExecuted)
                        List(
                          "Dustbin analysis" -> Future.successful(
                            ReportPage(List(),
                                       List("Krona Plot" -> ReportSection(
                                         "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp")),
                                       Map())))
                      else Nil),
      "Alignment" -> ReportSection(
        "/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp") ::
        (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
    )
312
  }
313
}