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

Ruben Vorderman's avatar
Ruben Vorderman 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}
Ruben Vorderman's avatar
Ruben Vorderman 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
Peter van 't Hof's avatar
Peter van 't Hof committed
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",
Ruben Vorderman's avatar
Ruben Vorderman 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] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
70

71
    val krakenExecuted = Await.result(summary.getStatsSize(runId,
Ruben Vorderman's avatar
Ruben Vorderman committed
72
73
74
75
76
                                                           "gearskraken",
                                                           "krakenreport",
                                                           library = NoLibrary,
                                                           mustHaveSample = true),
                                      Duration.Inf) >= 1
77
    val centrifugeExecuted = Await.result(summary.getStatsSize(runId,
Ruben Vorderman's avatar
Ruben Vorderman 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,
Ruben Vorderman's avatar
Ruben Vorderman 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,
Ruben Vorderman's avatar
Ruben Vorderman 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
Peter van 't Hof's avatar
Peter van 't Hof committed
106

107
    val krakenDustbinAnalysis = GearsKronaPlot.values(summary,
Ruben Vorderman's avatar
Ruben Vorderman committed
108
109
110
111
112
113
114
                                                      runId,
                                                      "gearskraken",
                                                      "krakenreport",
                                                      samples,
                                                      libraries,
                                                      sampleId,
                                                      libId)
115
    val centrifugeAnalysis = GearsKronaPlot.values(summary,
Ruben Vorderman's avatar
Ruben Vorderman committed
116
117
118
119
120
121
122
                                                   runId,
                                                   "gearscentrifuge",
                                                   "centrifuge_report",
                                                   samples,
                                                   libraries,
                                                   sampleId,
                                                   libId)
123
    val centrifugeUniqueAnalysis = GearsKronaPlot.values(summary,
Ruben Vorderman's avatar
Ruben Vorderman committed
124
125
126
127
128
129
130
                                                         runId,
                                                         "gearscentrifuge",
                                                         "centrifuge_unique_report",
                                                         samples,
                                                         libraries,
                                                         sampleId,
                                                         libId)
131
    val alignmentReportValues = BammetricsReportPage.alignmentSummaryValues(summary,
Ruben Vorderman's avatar
Ruben Vorderman 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,
Ruben Vorderman's avatar
Ruben Vorderman 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)
Ruben Vorderman's avatar
Ruben Vorderman 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",
Ruben Vorderman's avatar
Ruben Vorderman 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",
Ruben Vorderman's avatar
Ruben Vorderman committed
248
                                        clippingReportValues)
249
250
          ) ++
          (if (pairedFound && insertsizeExecuted)
Ruben Vorderman's avatar
Ruben Vorderman committed
251
252
253
254
255
             List(
               "Insert Size" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/bammetrics/insertSize.ssp",
                 insertSizeReportValues))
           else Nil) ++
256
          (if (wgsExecuted)
Ruben Vorderman's avatar
Ruben Vorderman 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)
Ruben Vorderman's avatar
Ruben Vorderman committed
263
264
265
266
267
             List(
               "Rna coverage" -> ReportSection(
                 "/nl/lumc/sasc/biopet/pipelines/bammetrics/rnaHistogram.ssp",
                 rnaHistogramReportValues))
           else Nil) ++
268
          (if (flexiprepExecuted)
Ruben Vorderman's avatar
Ruben Vorderman 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,
Ruben Vorderman's avatar
Ruben Vorderman committed
286
287
288
289
290
                                                           "gearskraken",
                                                           "krakenreport",
                                                           library = NoLibrary,
                                                           sample = sampleId),
                                      Duration.Inf) >= 1
291
    val centrifugeExecuted = Await.result(summary.getStatsSize(runId,
Ruben Vorderman's avatar
Ruben Vorderman 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

Ruben Vorderman's avatar
Ruben Vorderman 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

346
347
348
    Future {
      ReportPage(
        List("Libraries" -> generateLibraryPage(args),
Ruben Vorderman's avatar
Ruben Vorderman committed
349
             "Alignment" -> BammetricsReport.bamMetricsPage(summary, Some(sampleId), None)) ++
350
          (if (centrifugeExecuted)
Ruben Vorderman's avatar
Ruben Vorderman committed
351
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
             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),
377
378
379
380
381
382
383
384
385
        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)
Ruben Vorderman's avatar
Ruben Vorderman committed
386
387
388
389
390
391
392
393
394
             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),
395
396
397
398
399
        args
      )
    }

    /** Library page */
Ruben Vorderman's avatar
Ruben Vorderman committed
400
    def libraryPage(sampleId: Int, libId: Int, args: Map[String, Any]): Future[ReportPage] = {
401
      val krakenExecuted = Await.result(summary.getStatsSize(runId,
Ruben Vorderman's avatar
Ruben Vorderman committed
402
403
404
405
406
                                                             "gearskraken",
                                                             "krakenreport",
                                                             library = libId,
                                                             sample = sampleId),
                                        Duration.Inf) >= 1
407
      val centrifugeExecuted = Await.result(summary.getStatsSize(runId,
Ruben Vorderman's avatar
Ruben Vorderman committed
408
409
410
411
412
413
                                                                 "gearscentrifuge",
                                                                 "centrifuge_report",
                                                                 library = libId,
                                                                 sample = sampleId,
                                                                 mustHaveSample = true),
                                            Duration.Inf) >= 1
414
      val flexiprepExecuted = Await.result(summary.getStatsSize(runId,
Ruben Vorderman's avatar
Ruben Vorderman committed
415
416
417
418
419
420
                                                                "flexiprep",
                                                                library = libId,
                                                                sample = sampleId,
                                                                mustHaveLibrary = true),
                                           Duration.Inf) >= 1
      val flexiprepPageSummaries = FlexiprepReport.flexiprepPageSummaries(summary, sampleId, libId)
421
422
423
424
      Future {
        ReportPage(
          ("Alignment" -> BammetricsReport.bamMetricsPage(summary, Some(sampleId), Some(libId))) ::
            (if (flexiprepExecuted)
Ruben Vorderman's avatar
Ruben Vorderman committed
425
426
             List("QC" -> FlexiprepReport.flexiprepPage(flexiprepPageSummaries))
           else Nil) :::
427
            (if (centrifugeExecuted)
Ruben Vorderman's avatar
Ruben Vorderman committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
             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",
                              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),
449
450
451
          "Alignment" -> ReportSection(
            "/nl/lumc/sasc/biopet/pipelines/bammetrics/alignmentSummary.ssp") ::
            (if (flexiprepExecuted)
Ruben Vorderman's avatar
Ruben Vorderman committed
452
453
454
455
456
457
458
             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),
459
460
461
          args
        )
      }
462
463
    }
  }
Ruben Vorderman's avatar
Ruben Vorderman committed
464
}