MultisampleMappingReport.scala 27.2 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

rhpvorderman's avatar
rhpvorderman committed
17 18 19 20 21 22
import nl.lumc.sasc.biopet.core.report.{
  MultisampleReportBuilder,
  ReportBuilderExtension,
  ReportPage,
  ReportSection
}
23
import nl.lumc.sasc.biopet.pipelines.bammetrics.{BammetricsReport, BammetricsReportPage}
rhpvorderman's avatar
rhpvorderman committed
24 25 26 27 28
import nl.lumc.sasc.biopet.pipelines.flexiprep.{
  FlexiprepReadSummary,
  FlexiprepBaseSummary,
  FlexiprepReport
}
29
import nl.lumc.sasc.biopet.pipelines.gears.GearsKronaPlot
30
import nl.lumc.sasc.biopet.utils.config.Configurable
31
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb.Implicts._
32
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb._
33

34
import scala.concurrent.{Await, Future}
Peter van 't Hof's avatar
Peter van 't Hof committed
35 36
import scala.concurrent.duration.Duration

37
/**
38 39
  * Created by pjvanthof on 11/01/16.
  */
Peter van 't Hof's avatar
Peter van 't Hof committed
40
class MultisampleMappingReport(val parent: Configurable) extends ReportBuilderExtension {
41 42 43 44
  def builder = MultisampleMappingReport
}

object MultisampleMappingReport extends MultisampleMappingReportTrait {
45

46 47 48 49 50
  /** Name of the report */
  def reportName = "Mapping Report"
}

trait MultisampleMappingReportTrait extends MultisampleReportBuilder {
51

52
  /** Front section for the report */
53 54
  def frontSection: ReportSection =
    ReportSection("/nl/lumc/sasc/biopet/pipelines/mapping/multisampleMappingFront.ssp")
55

56 57
  def additionalSections: List[(String, ReportSection)] = Nil

Peter van 't Hof's avatar
Peter van 't Hof committed
58 59
  def pipelineName = "multisamplemapping"

60 61
  override def extFiles =
    super.extFiles ++ List("js/gears.js",
rhpvorderman's avatar
rhpvorderman committed
62 63 64 65
                           "js/krona-2.0.js",
                           "img/krona/loading.gif",
                           "img/krona/hidden.png",
                           "img/krona/favicon.ico")
66
      .map(x => ExtFile("/nl/lumc/sasc/biopet/pipelines/gears/report/ext/" + x, x))
Peter van 't Hof's avatar
Peter van 't Hof committed
67

68
  /** Root page for the carp report */
69
  def indexPage: Future[ReportPage] = {
70

71
    val krakenExecuted = Await.result(summary.getStatsSize(runId,
rhpvorderman's avatar
rhpvorderman committed
72 73 74 75 76
                                                           "gearskraken",
                                                           "krakenreport",
                                                           library = NoLibrary,
                                                           mustHaveSample = true),
                                      Duration.Inf) >= 1
77
    val centrifugeExecuted = Await.result(summary.getStatsSize(runId,
rhpvorderman's avatar
rhpvorderman committed
78 79 80 81 82
                                                               "gearscentrifuge",
                                                               "centrifuge_report",
                                                               library = NoLibrary,
                                                               mustHaveSample = true),
                                          Duration.Inf) >= 1
83 84 85 86 87 88 89 90
    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,
rhpvorderman's avatar
rhpvorderman committed
91 92 93
                          "bammetrics",
                          "CollectInsertSizeMetrics",
                          keyValues = Map("metrics" -> List("metrics")))
94 95 96 97 98
      .exists(_._2("metrics").isDefined)
    val mappingExecuted = Await.result(
      summary.getStatsSize(runId, "mapping", NoModule, mustHaveLibrary = true),
      Duration.Inf) >= 1
    val mappingSettings = summary.getSettingsForLibraries(runId,
rhpvorderman's avatar
rhpvorderman committed
99 100 101 102
                                                          "mapping",
                                                          NoModule,
                                                          keyValues =
                                                            Map("paired" -> List("paired")))
Peter van 't Hof's avatar
Peter van 't Hof committed
103
    val pairedFound = !mappingExecuted || mappingSettings.exists(_._2.exists(_._2 == Option(true)))
104 105
    val flexiprepExecuted = Await
      .result(summary.getStatsSize(runId, "flexiprep", mustHaveLibrary = true), Duration.Inf) >= 1
106

107
    val krakenDustbinAnalysis = GearsKronaPlot.values(summary,
rhpvorderman's avatar
rhpvorderman committed
108 109 110 111 112 113 114
                                                      runId,
                                                      "gearskraken",
                                                      "krakenreport",
                                                      samples,
                                                      libraries,
                                                      sampleId,
                                                      libId)
115
    val centrifugeAnalysis = GearsKronaPlot.values(summary,
rhpvorderman's avatar
rhpvorderman committed
116 117 118 119 120 121 122
                                                   runId,
                                                   "gearscentrifuge",
                                                   "centrifuge_report",
                                                   samples,
                                                   libraries,
                                                   sampleId,
                                                   libId)
123
    val centrifugeUniqueAnalysis = GearsKronaPlot.values(summary,
rhpvorderman's avatar
rhpvorderman committed
124 125 126 127 128 129 130
                                                         runId,
                                                         "gearscentrifuge",
                                                         "centrifuge_unique_report",
                                                         samples,
                                                         libraries,
                                                         sampleId,
                                                         libId)
131
    val alignmentReportValues = BammetricsReportPage.alignmentSummaryValues(summary,
rhpvorderman's avatar
rhpvorderman committed
132 133 134 135 136 137 138 139
                                                                            runId,
                                                                            samples,
                                                                            libraries,
                                                                            sampleId,
                                                                            libId,
                                                                            sampleLevel = true,
                                                                            showPlot = true,
                                                                            showTable = false)
140
    val mappingQualityReportValues = BammetricsReportPage.mappingQualityValues(summary,
rhpvorderman's avatar
rhpvorderman committed
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
                                                                               runId,
                                                                               samples,
                                                                               libraries,
                                                                               sampleId,
                                                                               libId,
                                                                               sampleLevel = true,
                                                                               showPlot = true,
                                                                               showTable = false)
    val clippingReportValues = BammetricsReportPage.clippingValues(summary,
                                                                   runId,
                                                                   samples,
                                                                   libraries,
                                                                   sampleId,
                                                                   libId,
                                                                   sampleLevel = true,
                                                                   showPlot = true,
                                                                   showTable = false)
    val insertSizeReportValues = BammetricsReportPage.insertSizeValues(summary,
                                                                       runId,
                                                                       samples,
                                                                       libraries,
                                                                       sampleId,
                                                                       libId,
                                                                       sampleLevel = true,
                                                                       showPlot = true,
                                                                       showTable = false)
    val wgsHistogramReportValues = BammetricsReportPage.wgsHistogramValues(summary,
                                                                           runId,
                                                                           samples,
                                                                           libraries,
                                                                           sampleId,
                                                                           libId,
                                                                           sampleLevel = true,
                                                                           showPlot = true,
                                                                           showTable = false)
    val rnaHistogramReportValues = BammetricsReportPage.rnaHistogramValues(summary,
                                                                           runId,
                                                                           samples,
                                                                           libraries,
                                                                           sampleId,
                                                                           libId,
                                                                           sampleLevel = true,
                                                                           showPlot = true,
                                                                           showTable = false)
    val flexiprepReadSummary = FlexiprepReadSummary.values(summary,
                                                           runId,
                                                           samples,
                                                           libraries,
                                                           sampleId,
                                                           libId,
                                                           showPlot = true,
                                                           showTable = false)
    val flexiprepBaseSummary = FlexiprepBaseSummary.values(summary,
                                                           runId,
                                                           samples,
                                                           libraries,
                                                           sampleId,
                                                           libId,
                                                           showPlot = true,
                                                           showTable = false)
201 202 203 204
    Future {
      ReportPage(
        List("Samples" -> generateSamplesPage(pageArgs)) ++
          (if (krakenExecuted)
rhpvorderman's avatar
rhpvorderman committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
             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) ++
229 230 231 232 233 234
          List(
            "Reference" -> Future.successful(
              ReportPage(
                List(),
                List(
                  "Reference" -> ReportSection("/nl/lumc/sasc/biopet/core/report/reference.ssp",
rhpvorderman's avatar
rhpvorderman committed
235
                                               Map("pipeline" -> pipelineName))
236 237 238 239 240 241 242
                ),
                Map()))),
        List("Report" -> frontSection) ++
          additionalSections ++
          List(
            "Alignment" -> ReportSection(
              "/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp",
243
              alignmentReportValues),
244 245
            "Mapping Quality" -> ReportSection(
              "/nl/lumc/sasc/biopet/pipelines/bammetrics/mappingQuality.ssp",
246
              mappingQualityReportValues),
247
            "Clipping" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/clipping.ssp",
rhpvorderman's avatar
rhpvorderman committed
248
                                        clippingReportValues)
249 250
          ) ++
          (if (pairedFound && insertsizeExecuted)
rhpvorderman's avatar
rhpvorderman committed
251 252 253 254 255
             List(
               "Insert Size" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/bammetrics/insertSize.ssp",
                 insertSizeReportValues))
           else Nil) ++
256
          (if (wgsExecuted)
rhpvorderman's avatar
rhpvorderman committed
257 258 259 260 261
             List(
               "Whole genome coverage" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/bammetrics/wgsHistogram.ssp",
                 wgsHistogramReportValues))
           else Nil) ++
262
          (if (rnaExecuted)
rhpvorderman's avatar
rhpvorderman committed
263 264 265 266 267
             List(
               "Rna coverage" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/bammetrics/rnaHistogram.ssp",
                 rnaHistogramReportValues))
           else Nil) ++
268
          (if (flexiprepExecuted)
rhpvorderman's avatar
rhpvorderman committed
269 270 271 272 273 274 275 276 277
             List(
               "QC reads" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp",
                 flexiprepReadSummary),
               "QC bases" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp",
                 flexiprepBaseSummary)
             )
           else Nil),
278 279 280
        pageArgs ++ Map("pipeline" -> pipelineName)
      )
    }
281 282 283
  }

  /** Single sample page */
284
  def samplePage(sampleId: Int, args: Map[String, Any]): Future[ReportPage] = {
285
    val krakenExecuted = Await.result(summary.getStatsSize(runId,
rhpvorderman's avatar
rhpvorderman committed
286 287 288 289 290
                                                           "gearskraken",
                                                           "krakenreport",
                                                           library = NoLibrary,
                                                           sample = sampleId),
                                      Duration.Inf) >= 1
291
    val centrifugeExecuted = Await.result(summary.getStatsSize(runId,
rhpvorderman's avatar
rhpvorderman committed
292 293 294 295 296 297
                                                               "gearscentrifuge",
                                                               "centrifuge_report",
                                                               library = NoLibrary,
                                                               sample = sampleId,
                                                               mustHaveSample = true),
                                          Duration.Inf) >= 1
298 299 300 301
    val flexiprepExecuted = Await.result(
      summary.getStatsSize(runId, "flexiprep", sample = sampleId, mustHaveLibrary = true),
      Duration.Inf) >= 1

rhpvorderman's avatar
rhpvorderman committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    val centrifugeAllMappingsReportValues = GearsKronaPlot.values(summary,
                                                                  runId,
                                                                  "gearscentrifuge",
                                                                  "centrifuge_report",
                                                                  samples,
                                                                  libraries,
                                                                  sampleId,
                                                                  libId,
                                                                  Some("centrifuge"))
    val centrifugeUniqueMappingsReportValues = GearsKronaPlot.values(summary,
                                                                     runId,
                                                                     "gearscentrifuge",
                                                                     "centrifuge_unique_report",
                                                                     samples,
                                                                     libraries,
                                                                     sampleId,
                                                                     libId)
    val dustbinAnalysisReportValues = GearsKronaPlot.values(summary,
                                                            runId,
                                                            "gearskraken",
                                                            "krakenreport",
                                                            samples,
                                                            libraries,
                                                            sampleId,
                                                            libId)
    val alignmentSummaryReportValues = BammetricsReportPage.alignmentSummaryValues(summary,
                                                                                   runId,
                                                                                   samples,
                                                                                   libraries,
                                                                                   sampleId,
                                                                                   libId,
                                                                                   showPlot = true)
    val preprocessingReportValues = BammetricsReportPage.alignmentSummaryValues(summary,
                                                                                runId,
                                                                                samples,
                                                                                libraries,
                                                                                sampleId,
                                                                                libId,
                                                                                sampleLevel = true)
    val flexiprepReadSummary =
      FlexiprepReadSummary.values(summary, runId, samples, libraries, sampleId, libId)
    val flexiprepBaseSummary =
      FlexiprepBaseSummary.values(summary, runId, samples, libraries, sampleId, libId)
345
    val bamMetricsReportValues = BammetricsReport.bamMetricsPageValues(summary, Some(sampleId), libId)
346

347 348 349
    Future {
      ReportPage(
        List("Libraries" -> generateLibraryPage(args),
350
             "Alignment" -> BammetricsReport.bamMetricsPage(bamMetricsReportValues)) ++
351
          (if (centrifugeExecuted)
rhpvorderman's avatar
rhpvorderman committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
             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",
                         centrifugeAllMappingsReportValues)),
                       Map()
                     ))),
                 List("Unique mappings" -> ReportSection(
                   "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
                   centrifugeUniqueMappingsReportValues)),
                 Map("summaryPipelineName" -> "gearscentrifuge")
               )))
           else
             Nil) ::: (if (krakenExecuted)
                         List(
                           "Dustbin analysis" -> Future.successful(
                             ReportPage(List(),
                                        List("Krona Plot" -> ReportSection(
                                          "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
                                          dustbinAnalysisReportValues)),
                                        Map())))
                       else Nil),
378 379 380 381 382 383 384 385 386
        List(
          "Alignment" -> ReportSection(
            "/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp",
            alignmentSummaryReportValues),
          "Preprocessing" -> ReportSection(
            "/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp",
            preprocessingReportValues)
        ) ++
          (if (flexiprepExecuted)
rhpvorderman's avatar
rhpvorderman committed
387 388 389 390 391 392 393 394 395
             List(
               "QC reads" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp",
                 flexiprepReadSummary),
               "QC bases" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp",
                 flexiprepBaseSummary)
             )
           else Nil),
396 397 398 399 400
        args
      )
    }

    /** Library page */
rhpvorderman's avatar
rhpvorderman committed
401
    def libraryPage(sampleId: Int, libId: Int, args: Map[String, Any]): Future[ReportPage] = {
402
      val krakenExecuted = Await.result(summary.getStatsSize(runId,
rhpvorderman's avatar
rhpvorderman committed
403 404 405 406 407
                                                             "gearskraken",
                                                             "krakenreport",
                                                             library = libId,
                                                             sample = sampleId),
                                        Duration.Inf) >= 1
408
      val centrifugeExecuted = Await.result(summary.getStatsSize(runId,
rhpvorderman's avatar
rhpvorderman committed
409 410 411 412 413 414
                                                                 "gearscentrifuge",
                                                                 "centrifuge_report",
                                                                 library = libId,
                                                                 sample = sampleId,
                                                                 mustHaveSample = true),
                                            Duration.Inf) >= 1
415
      val flexiprepExecuted = Await.result(summary.getStatsSize(runId,
rhpvorderman's avatar
rhpvorderman committed
416 417 418 419 420 421
                                                                "flexiprep",
                                                                library = libId,
                                                                sample = sampleId,
                                                                mustHaveLibrary = true),
                                           Duration.Inf) >= 1
      val flexiprepPageSummaries = FlexiprepReport.flexiprepPageSummaries(summary, sampleId, libId)
422 423 424 425 426 427 428
      val bamMetricsReportValues = BammetricsReport.bamMetricsPageValues(summary, Some(sampleId), Some(libId))
      val centrifugeAllMappingsReportValues = GearsKronaPlot.values(summary, runId, "gearscentrifuge","centrifuge_report",samples,libraries,Some(sampleId),Some(libId), centrifugeTag = Some("centrifuge"))
      val centrifugeUniqueMappingsReportValues = GearsKronaPlot.values(summary, runId, "gearscentrifuge","centrifuge_unique_report",samples,libraries,Some(sampleId),Some(libId), centrifugeTag = Some("centrifuge"))
      val krakenDustbinAnalysisReportValues = GearsKronaPlot.values(summary, runId, "gearskraken","krakenreport",samples,libraries,Some(sampleId),Some(libId))
      val alignmentSummaryReportValues = BammetricsReportPage.alignmentSummaryValues(summary,runId,samples,libraries,Some(sampleId),Some(libId))
      val flexiprepReadSummaryReportValues = FlexiprepReadSummary.values(summary, runId, samples, libraries, Some(sampleId),Some(libId))
      val flexiprepBaseSummaryReportValues = FlexiprepBaseSummary.values(summary, runId, samples, libraries, Some(sampleId),Some(libId))
429 430
      Future {
        ReportPage(
431
          ("Alignment" -> BammetricsReport.bamMetricsPage(bamMetricsReportValues)) ::
432
            (if (flexiprepExecuted)
rhpvorderman's avatar
rhpvorderman committed
433 434
             List("QC" -> FlexiprepReport.flexiprepPage(flexiprepPageSummaries))
           else Nil) :::
435
            (if (centrifugeExecuted)
rhpvorderman's avatar
rhpvorderman committed
436 437 438 439 440
             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",
441
                              centrifugeAllMappingsReportValues)),
rhpvorderman's avatar
rhpvorderman committed
442 443 444
                            Map()))),
               List("Unique mappings" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
445 446
                 centrifugeUniqueMappingsReportValues)),
               Map("summaryPipelineName" -> "gearscentrifuge", "centrifugeTag" -> Some("centrifuge"))
rhpvorderman's avatar
rhpvorderman committed
447 448 449 450 451 452 453
             )))
           else
             Nil) ::: (if (krakenExecuted)
                         List(
                           "Dustbin analysis" -> Future.successful(
                             ReportPage(List(),
                                        List("Krona Plot" -> ReportSection(
454
                                          "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",krakenDustbinAnalysisReportValues)),
rhpvorderman's avatar
rhpvorderman committed
455 456
                                        Map())))
                       else Nil),
457
          "Alignment" -> ReportSection(
458
            "/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp", alignmentSummaryReportValues) ::
459
            (if (flexiprepExecuted)
rhpvorderman's avatar
rhpvorderman committed
460 461
             List(
               "QC reads" -> ReportSection(
462
                 "/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp",flexiprepReadSummaryReportValues),
rhpvorderman's avatar
rhpvorderman committed
463
               "QC bases" -> ReportSection(
464
                 "/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp", flexiprepBaseSummaryReportValues)
rhpvorderman's avatar
rhpvorderman committed
465 466
             )
           else Nil),
467 468 469
          args
        )
      }
470 471
    }
  }
rhpvorderman's avatar
rhpvorderman committed
472
}