MultisampleMappingReport.scala 17.4 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 19 20 21 22
import nl.lumc.sasc.biopet.core.report.{
  MultisampleReportBuilder,
  ReportBuilderExtension,
  ReportPage,
  ReportSection
}
23
import nl.lumc.sasc.biopet.pipelines.bammetrics.{BamMetricsAlignmentReport, BammetricsReport}
24
import nl.lumc.sasc.biopet.pipelines.flexiprep.FlexiprepReport
25
import nl.lumc.sasc.biopet.pipelines.gears.GearsKronaPlot
26
import nl.lumc.sasc.biopet.utils.config.Configurable
Peter van 't Hof's avatar
Peter van 't Hof committed
27
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb.Implicts._
28
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb._
29

30
import scala.concurrent.{Await, Future}
Peter van 't Hof's avatar
Peter van 't Hof committed
31 32
import scala.concurrent.duration.Duration

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

object MultisampleMappingReport extends MultisampleMappingReportTrait {
41

42 43 44 45 46
  /** Name of the report */
  def reportName = "Mapping Report"
}

trait MultisampleMappingReportTrait extends MultisampleReportBuilder {
47

48
  /** Front section for the report */
49 50
  def frontSection: ReportSection =
    ReportSection("/nl/lumc/sasc/biopet/pipelines/mapping/multisampleMappingFront.ssp")
51

52 53
  def additionalSections: List[(String, ReportSection)] = Nil

Peter van 't Hof's avatar
Peter van 't Hof committed
54 55
  def pipelineName = "multisamplemapping"

56 57 58 59 60 61 62
  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
63

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

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

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    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)
127
    val alignmentSummary = BammetricsReport.alignmentReportValues(summary,
128 129 130 131 132 133 134 135 136
                                                            runId,
                                                            samples,
                                                            libraries,
                                                            sampleId,
                                                            libId,
                                                            sampleLevel = true,
                                                            showPlot = true,
                                                            showTable = false)
    val mappingQualitySummary = BammetricsReport.mappingQualityPlotTables(summary)
137 138 139 140
    Future {
      ReportPage(
        List("Samples" -> generateSamplesPage(pageArgs)) ++
          (if (krakenExecuted)
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
             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) ++
165 166 167 168 169 170
          List(
            "Reference" -> Future.successful(
              ReportPage(
                List(),
                List(
                  "Reference" -> ReportSection("/nl/lumc/sasc/biopet/core/report/reference.ssp",
171
                                               Map("pipeline" -> pipelineName))
172 173 174 175 176 177 178 179 180 181 182 183
                ),
                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",
184 185 186
                                        Map("sampleLevel" -> true,
                                            "showPlot" -> true,
                                            "showTable" -> false))
187 188
          ) ++
          (if (pairedFound && insertsizeExecuted)
189 190 191 192 193
             List(
               "Insert Size" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/bammetrics/insertSize.ssp",
                 Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false)))
           else Nil) ++
194
          (if (wgsExecuted)
195 196 197 198 199
             List(
               "Whole genome coverage" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/bammetrics/wgsHistogram.ssp",
                 Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false)))
           else Nil) ++
200
          (if (rnaExecuted)
201 202 203 204 205
             List(
               "Rna coverage" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/bammetrics/rnaHistogram.ssp",
                 Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false)))
           else Nil) ++
206
          (if (flexiprepExecuted)
207 208 209 210 211 212 213 214 215
             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),
216 217 218
        pageArgs ++ Map("pipeline" -> pipelineName)
      )
    }
219 220 221
  }

  /** Single sample page */
Peter van 't Hof's avatar
Peter van 't Hof committed
222
  def samplePage(sampleId: Int, args: Map[String, Any]): Future[ReportPage] = Future {
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    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
245
             "Centrifuge analysis" -> Future.successful(ReportPage(
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
               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
    )
286 287 288
  }

  /** Library page */
Peter van 't Hof's avatar
Peter van 't Hof committed
289
  def libraryPage(sampleId: Int, libId: Int, args: Map[String, Any]): Future[ReportPage] = Future {
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    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
309

Peter van 't Hof's avatar
Peter van 't Hof committed
310 311
    ReportPage(
      ("Alignment" -> BammetricsReport.bamMetricsPage(summary, Some(sampleId), Some(libId))) ::
rhpvorderman's avatar
rhpvorderman committed
312 313 314
        (if (flexiprepExecuted)
         List("QC" -> FlexiprepReport.flexiprepPage(summary, sampleId, libId))
       else Nil) :::
315
        (if (centrifugeExecuted)
rhpvorderman's avatar
rhpvorderman committed
316
         List("Centrifuge analysis" -> Future.successful(ReportPage(
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 345 346 347
           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
    )
348
  }
349
}