GearsReport.scala 23.7 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
/**
rhpvorderman's avatar
rhpvorderman committed
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.gears

Peter van 't Hof's avatar
Peter van 't Hof committed
17 18
import java.io.File

19 20 21 22 23 24 25 26 27 28 29
import nl.lumc.sasc.biopet.core.report.{
  MultisampleReportBuilder,
  ReportBuilderExtension,
  ReportPage,
  ReportSection
}
import nl.lumc.sasc.biopet.pipelines.flexiprep.{
  FlexiprepBaseSummary,
  FlexiprepReadSummary,
  FlexiprepReport
}
30
import nl.lumc.sasc.biopet.pipelines.gears
31
import nl.lumc.sasc.biopet.utils.config.Configurable
32
import nl.lumc.sasc.biopet.utils.summary.db.Schema.{Library, Sample}
rhpvorderman's avatar
rhpvorderman committed
33
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb
Peter van 't Hof's avatar
Peter van 't Hof committed
34
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb.Implicts._
35
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb.{ModuleName, NoLibrary, NoModule, SampleId}
36

37
import scala.concurrent.duration.Duration
38
import scala.concurrent.{Await, Future}
39

40
/**
rhpvorderman's avatar
rhpvorderman committed
41 42 43 44
  * Report for Gears
  *
  * Created by pjvan_thof on 12/10/15.
  */
Peter van 't Hof's avatar
Peter van 't Hof committed
45
class GearsReport(val parent: Configurable) extends ReportBuilderExtension {
46 47 48 49 50
  def builder = GearsReport
}

object GearsReport extends MultisampleReportBuilder {

51 52
  def pipelineName = "gears"

53 54
  def reportName = "Gears Report"

rhpvorderman's avatar
rhpvorderman committed
55 56 57 58 59 60 61
  override def extFiles: List[gears.GearsReport.ExtFile] =
    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))
62

63
  def indexPage: Future[ReportPage] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
64 65
    val run = Await.result(summary.getRuns(runId).map(_.head), Duration.Inf)

rhpvorderman's avatar
rhpvorderman committed
66 67 68 69 70 71 72 73 74 75
    val krakenExecuted = summary.getStatsSize(runId = runId,
                                              pipeline = "gearskraken",
                                              module = "krakenreport",
                                              library = NoLibrary,
                                              mustHaveSample = true) >= samples.size
    val centrifugeExecuted = summary.getStatsSize(runId,
                                                  "gearscentrifuge",
                                                  "centrifuge_report",
                                                  library = NoLibrary,
                                                  mustHaveSample = true) >= samples.size
Peter van 't Hof's avatar
Peter van 't Hof committed
76 77
    val qiimeClosesOtuTable = summary.getFile(runId, "gears", key = "qiime_closed_otu_table")
    val qiimeOpenOtuTable = summary.getFile(runId, "gears", key = "qiime_open_otu_table")
78 79
    val centrifugePageAllMappingsKronaPlot = GearsKronaPlot.values(summary,
                                                                   runId,
80
                                                                   "gearscentrifuge",
81 82 83 84 85 86 87 88
                                                                   "centrifuge_report",
                                                                   samples,
                                                                   libraries,
                                                                   sampleId,
                                                                   libId,
                                                                   Some("centrifuge"))
    val centrifugePageUniqueMappingsKronaPlot = GearsKronaPlot.values(summary,
                                                                      runId,
89
                                                                      "gearscentrifuge",
90 91 92 93 94
                                                                      "centrifuge_unique_report",
                                                                      samples,
                                                                      libraries,
                                                                      sampleId,
                                                                      libId)
95

rhpvorderman's avatar
rhpvorderman committed
96 97 98 99
    val centrifugePage =
      if (centrifugeExecuted)
        Some(
          "Centrifuge analysis" -> Future.successful(ReportPage(
100 101 102 103 104 105 106 107 108 109
            List(
              "Non-unique" ->
                Future.successful(
                  ReportPage(
                    List(),
                    List("All mappings" -> ReportSection(
                      "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
                      centrifugePageAllMappingsKronaPlot)),
                    Map()
                  ))),
rhpvorderman's avatar
rhpvorderman committed
110 111
            List("Unique mappings" -> ReportSection(
              "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
112
              centrifugePageUniqueMappingsKronaPlot)),
rhpvorderman's avatar
rhpvorderman committed
113 114 115
            Map("summaryModuleTag" -> "gearscentrifuge", "centrifugeTag" -> Some("centrifuge"))
          )))
      else None
116
    val krakenPageKronaPlot = GearsKronaPlot
rhpvorderman's avatar
rhpvorderman committed
117
      .values(summary, runId, "gearskraken", "krakenreport", samples, libraries, sampleId, libId)
rhpvorderman's avatar
rhpvorderman committed
118 119 120
    val krakenPage =
      if (krakenExecuted)
        Some(
121 122 123 124
          "Kraken analysis" -> Future.successful(
            ReportPage(
              List(),
              List("Krona plot" -> Future.successful(
125
                ReportSection("/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
126 127 128
                              krakenPageKronaPlot))),
              Map()
            )))
rhpvorderman's avatar
rhpvorderman committed
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
      else None

    val qiimeClosedPage =
      if (qiimeClosesOtuTable.isDefined)
        Some(
          "Qiime closed reference analysis" -> Future.successful(ReportPage(
            List(),
            List("Krona plot" -> Future.successful(
              ReportSection("/nl/lumc/sasc/biopet/pipelines/gears/qiimeKrona.ssp"))),
            Map("biomFile" -> new File(
              run.outputDir + File.separator + qiimeClosesOtuTable.get.path))
          )))
      else None

    val qiimeOpenPage =
      if (qiimeOpenOtuTable.isDefined)
        Some(
          "Qiime open reference analysis" -> Future.successful(ReportPage(
            List(),
            List("Krona plot" -> ReportSection(
              "/nl/lumc/sasc/biopet/pipelines/gears/qiimeKrona.ssp")),
            Map(
              "biomFile" -> new File(run.outputDir + File.separator + qiimeOpenOtuTable.get.path))
          )))
      else None
154

155
    val flexiprepReadSummary =
rhpvorderman's avatar
rhpvorderman committed
156 157 158 159 160 161 162 163
      FlexiprepReadSummary.values(summary,
                                  runId,
                                  samples,
                                  libraries,
                                  sampleId,
                                  libId,
                                  showPlot = true,
                                  showTable = false)
164
    val flexiprepBaseSummary =
rhpvorderman's avatar
rhpvorderman committed
165 166 167 168 169 170 171 172
      FlexiprepBaseSummary.values(summary,
                                  runId,
                                  samples,
                                  libraries,
                                  sampleId,
                                  libId,
                                  showPlot = true,
                                  showTable = false)
173 174 175 176
    Future {
      ReportPage(
        List(centrifugePage, krakenPage, qiimeClosedPage, qiimeOpenPage).flatten ::: List(
          "Samples" -> generateSamplesPage(pageArgs)
177
        ),
rhpvorderman's avatar
rhpvorderman committed
178
        List("Report" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/gears/gearsFront.ssp")) ++
179
          List(
rhpvorderman's avatar
rhpvorderman committed
180 181
            "QC reads" -> ReportSection(
              "/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp",
182
              flexiprepReadSummary),
rhpvorderman's avatar
rhpvorderman committed
183 184
            "QC bases" -> ReportSection(
              "/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp",
185
              flexiprepBaseSummary)
186 187 188 189
          ),
        pageArgs
      )
    }
190 191 192
  }

  /** Single sample page */
193
  def samplePage(sampleId: Int, args: Map[String, Any]): Future[ReportPage] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
194
    val run = Await.result(summary.getRuns(runId).map(_.head), Duration.Inf)
rhpvorderman's avatar
rhpvorderman committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    val krakenExecuted = Await.result(summary.getStatsSize(runId,
                                                           "gearskraken",
                                                           "krakenreport",
                                                           sample = sampleId,
                                                           library = NoLibrary),
                                      Duration.Inf) == 1
    val centrifugeExecuted = Await.result(summary.getStatsSize(runId,
                                                               "gearscentrifuge",
                                                               "centrifuge_report",
                                                               sample = sampleId,
                                                               library = None),
                                          Duration.Inf) == 1
    val qiimeClosesOtuTable = Await.result(summary.getFile(runId,
                                                           "gearssingle",
                                                           NoModule,
                                                           sampleId,
                                                           NoLibrary,
                                                           "qiime_closed_otu_table"),
                                           Duration.Inf)
    val qiimeOpenOtuTable = Await.result(
      summary.getFile(runId, "gearssingle", NoModule, sampleId, NoLibrary, "qiime_open_otu_table"),
      Duration.Inf)
217 218
    val centrifugePageAllMappingsKronaPlot = GearsKronaPlot.values(summary,
                                                                   runId,
219
                                                                   "gearscentrifuge",
220 221 222 223 224 225 226 227
                                                                   "centrifuge_report",
                                                                   samples,
                                                                   libraries,
                                                                   sampleId,
                                                                   libId,
                                                                   Some("centrifuge"))
    val centrifugePageUniqueMappingsKronaPlot = GearsKronaPlot.values(summary,
                                                                      runId,
228
                                                                      "gearscentrifuge",
229 230 231 232 233
                                                                      "centrifuge_unique_report",
                                                                      samples,
                                                                      libraries,
                                                                      sampleId,
                                                                      libId)
rhpvorderman's avatar
rhpvorderman committed
234 235 236
    val gearsCentrifugePage = if (centrifugeExecuted) {
      Some(
        "Centrifuge analysis" -> Future.successful(ReportPage(
237 238 239 240 241 242 243 244 245 246
          List(
            "Non-unique" -> Future.successful(
              ReportPage(
                List(),
                List("All mappings" -> ReportSection(
                  "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
                  centrifugePageAllMappingsKronaPlot
                )),
                Map()
              ))),
rhpvorderman's avatar
rhpvorderman committed
247 248
          List("Unique mappings" -> ReportSection(
            "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
249
            centrifugePageUniqueMappingsKronaPlot)),
rhpvorderman's avatar
rhpvorderman committed
250 251 252
          Map("summaryModuleTag" -> "gearscentrifuge", "centrifugeTag" -> Some("centrifuge"))
        )))
    } else None
253
    val krakenPageKronaPlot = GearsKronaPlot
rhpvorderman's avatar
rhpvorderman committed
254
      .values(summary, runId, "gearskraken", "krakenreport", samples, libraries, sampleId, libId)
rhpvorderman's avatar
rhpvorderman committed
255 256
    val krakenAnalysisPage =
      if (krakenExecuted)
257
        Some(
258 259 260 261 262 263 264 265
          "Kraken analysis" -> Future.successful(
            ReportPage(
              List(),
              List("Krona plot" -> ReportSection(
                "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
                krakenPageKronaPlot)),
              Map()
            )))
266
      else None
rhpvorderman's avatar
rhpvorderman committed
267 268 269

    val qiimeClosesOtuTablePage =
      if (qiimeClosesOtuTable.isDefined)
270
        Some(
rhpvorderman's avatar
rhpvorderman committed
271 272 273 274 275 276 277
          "Qiime closed reference analysis" -> Future.successful(ReportPage(
            List(),
            List("Krona plot" -> ReportSection(
              "/nl/lumc/sasc/biopet/pipelines/gears/qiimeKrona.ssp")),
            Map("biomFile" -> new File(
              run.outputDir + File.separator + qiimeClosesOtuTable.get.path))
          )))
278
      else None
rhpvorderman's avatar
rhpvorderman committed
279 280 281

    val qiimeOpenOtuTablePage =
      if (qiimeOpenOtuTable.isDefined)
282
        Some(
rhpvorderman's avatar
rhpvorderman committed
283 284 285 286 287 288 289
          "Qiime open reference analysis" -> Future.successful(ReportPage(
            List(),
            List("Krona plot" -> ReportSection(
              "/nl/lumc/sasc/biopet/pipelines/gears/qiimeKrona.ssp")),
            Map(
              "biomFile" -> new File(run.outputDir + File.separator + qiimeOpenOtuTable.get.path))
          )))
290
      else None
291

292 293 294 295
    val flexiprepReadSummary =
      FlexiprepReadSummary.values(summary, runId, samples, libraries, sampleId)
    val flexiprepBaseSummary =
      FlexiprepBaseSummary.values(summary, runId, samples, libraries, sampleId)
296
    Future {
rhpvorderman's avatar
rhpvorderman committed
297 298
      ReportPage(
        subPages =
299
          List(gearsCentrifugePage,
rhpvorderman's avatar
rhpvorderman committed
300 301 302 303 304 305 306
               krakenAnalysisPage,
               qiimeClosesOtuTablePage,
               qiimeOpenOtuTablePage).flatten ::: List(
            "Libraries" -> generateLibraryPage(args)
          ),
        sections = List(
          "QC reads" -> ReportSection(
307 308
            "/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp",
            flexiprepReadSummary),
rhpvorderman's avatar
rhpvorderman committed
309
          "QC bases" -> ReportSection(
310 311
            "/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp",
            flexiprepBaseSummary)
rhpvorderman's avatar
rhpvorderman committed
312 313 314
        ),
        args = args
      )
315 316 317 318 319 320 321 322
    }
  }

  /** Library page */
  def libraryPage(sampleId: Int, libId: Int, args: Map[String, Any]): Future[ReportPage] = {
    val sName = Await.result(summary.getSampleName(sampleId), Duration.Inf)
    val lName = Await.result(summary.getLibraryName(libId), Duration.Inf)

rhpvorderman's avatar
rhpvorderman committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    val flexiprepExecuted = Await.result(
      summary.getStatsSize(runId, "flexiprep", sample = sampleId, library = libId),
      Duration.Inf) >= 1

    val krakenExecuted = Await.result(summary.getStatsSize(runId,
                                                           "gearskraken",
                                                           "krakenreport",
                                                           sample = sampleId,
                                                           library = libId),
                                      Duration.Inf) == 1
    val centrifugeExecuted = Await.result(summary.getStatsSize(runId,
                                                               "gearscentrifuge",
                                                               "centrifuge_report",
                                                               sample = sampleId,
                                                               library = libId),
                                          Duration.Inf) == 1
    val qiimeClosesOtuTable = Await.result(
      summary.getFile(runId, "gears", NoModule, sampleId, libId, "qiime_closed_otu_table"),
      Duration.Inf)
    val qiimeOpenOtuTable = Await.result(
      summary.getFile(runId, "gears", NoModule, sampleId, libId, "qiime_open_otu_table"),
      Duration.Inf)
345

rhpvorderman's avatar
rhpvorderman committed
346
    val flexiprepReportPage =
rhpvorderman's avatar
rhpvorderman committed
347 348 349 350
      if (flexiprepExecuted) {
        val flexiprepPageSummaries = FlexiprepReport.flexiprepPageSummaries(summary,sampleId,libId)
        Some("QC" -> FlexiprepReport.flexiprepPage(flexiprepPageSummaries))
      }
rhpvorderman's avatar
rhpvorderman committed
351
      else None
rhpvorderman's avatar
rhpvorderman committed
352

353 354
    val centrifugePageAllMappingsKronaPlot = GearsKronaPlot.values(summary,
                                                                   runId,
355
                                                                   "gearscentrifuge",
356 357 358 359 360 361 362 363
                                                                   "centrifuge_report",
                                                                   samples,
                                                                   libraries,
                                                                   sampleId,
                                                                   libId,
                                                                   Some("centrifuge"))
    val centrifugePageUniqueMappingsKronaPlot = GearsKronaPlot.values(summary,
                                                                      runId,
364
                                                                      "gearscentrifuge",
365 366 367 368 369
                                                                      "centrifuge_unique_report",
                                                                      samples,
                                                                      libraries,
                                                                      sampleId,
                                                                      libId)
rhpvorderman's avatar
rhpvorderman committed
370 371
    val centrifugeReportPage =
      if (centrifugeExecuted)
372
        Some(
rhpvorderman's avatar
rhpvorderman committed
373
          "Centrifuge analysis" -> Future.successful(ReportPage(
374 375 376 377 378 379 380 381 382 383
            List(
              "Non-unique" -> Future.successful(
                ReportPage(
                  List(),
                  List("All mappings" -> ReportSection(
                    "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
                    centrifugePageAllMappingsKronaPlot
                  )),
                  Map()
                ))),
rhpvorderman's avatar
rhpvorderman committed
384 385
            List("Unique mappings" -> ReportSection(
              "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
386
              centrifugePageUniqueMappingsKronaPlot)),
rhpvorderman's avatar
rhpvorderman committed
387 388
            Map("summaryModuleTag" -> "gearscentrifuge", "centrifugeTag" -> Some("centrifuge"))
          )))
389
      else None
390
    val krakenPageKronaPlot = GearsKronaPlot
rhpvorderman's avatar
rhpvorderman committed
391
      .values(summary, runId, "gearskraken", "krakenreport", samples, libraries, sampleId, libId)
rhpvorderman's avatar
rhpvorderman committed
392
    val krakenAnalysisPage =
393
      if (krakenExecuted)
394
        Some(
395 396 397 398 399 400 401 402
          "Kraken analysis" -> Future.successful(
            ReportPage(
              List(),
              List("Krona plot" -> ReportSection(
                "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
                krakenPageKronaPlot)),
              Map()
            )))
403
      else None
rhpvorderman's avatar
rhpvorderman committed
404 405 406

    val qiimeClosesOtuTablePage =
      if (qiimeClosesOtuTable.isDefined)
407
        Some(
rhpvorderman's avatar
rhpvorderman committed
408 409 410 411 412 413
          "Qiime closed reference analysis" -> Future.successful(ReportPage(
            List(),
            List("Krona plot" -> ReportSection(
              "/nl/lumc/sasc/biopet/pipelines/gears/qiimeKrona.ssp")),
            Map("biomFile" -> new File(qiimeClosesOtuTable.get.path))
          )))
414
      else None
rhpvorderman's avatar
rhpvorderman committed
415 416 417

    val qiimeOpenOtuTablePage =
      if (qiimeOpenOtuTable.isDefined)
418
        Some(
rhpvorderman's avatar
rhpvorderman committed
419 420 421 422 423
          "Qiime open reference analysis" -> Future.successful(
            ReportPage(List(),
                       List("Krona plot" -> ReportSection(
                         "/nl/lumc/sasc/biopet/pipelines/gears/qiimeKrona.ssp")),
                       Map("biomFile" -> new File(qiimeOpenOtuTable.get.path)))))
424
      else None
425 426 427 428
    val flexiprepReadSummary =
      FlexiprepReadSummary.values(summary, runId, samples, libraries, sampleId)
    val flexiprepBaseSummary =
      FlexiprepBaseSummary.values(summary, runId, samples, libraries, sampleId)
429 430
    Future {
      ReportPage(
rhpvorderman's avatar
rhpvorderman committed
431 432 433
        List(flexiprepReportPage,
             centrifugeReportPage,
             krakenAnalysisPage,
434 435
             qiimeClosesOtuTablePage,
             qiimeOpenOtuTablePage).flatten,
rhpvorderman's avatar
rhpvorderman committed
436 437
        List(
          "QC reads" -> ReportSection(
438 439
            "/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp",
            flexiprepReadSummary),
rhpvorderman's avatar
rhpvorderman committed
440
          "QC bases" -> ReportSection(
441 442
            "/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepBaseSummary.ssp",
            flexiprepBaseSummary)
rhpvorderman's avatar
rhpvorderman committed
443 444 445 446 447 448 449 450 451
        ),
        args
      )
    }
  }

}

object GearsKronaPlot {
rhpvorderman's avatar
rhpvorderman committed
452 453
  def values(summary: SummaryDb,
             runId: Int,
rhpvorderman's avatar
rhpvorderman committed
454 455
             summaryPipelineName: String,
             summaryModuleName: String,
rhpvorderman's avatar
rhpvorderman committed
456 457 458 459 460
             allSamples: Seq[Sample],
             allLibraries: Seq[Library],
             sampleId: Option[Int] = None,
             libId: Option[Int] = None,
             centrifugeTag: Option[String] = None): Map[String, Any] = {
rhpvorderman's avatar
rhpvorderman committed
461
    val summariesVal =
rhpvorderman's avatar
rhpvorderman committed
462
      summaries(summary, runId, sampleId, libId, summaryPipelineName, summaryModuleName)
rhpvorderman's avatar
rhpvorderman committed
463 464 465 466
    val totalReadsVal = totalReads(summary,
                                   runId,
                                   sampleId,
                                   libId,
rhpvorderman's avatar
rhpvorderman committed
467
                                   summaryPipelineName,
rhpvorderman's avatar
rhpvorderman committed
468 469 470
                                   centrifugeTag,
                                   allSamples,
                                   allLibraries)
471 472 473 474 475
    Map(
      "summary" -> summary,
      "runId" -> runId,
      "sampleId" -> sampleId,
      "libId" -> libId,
rhpvorderman's avatar
rhpvorderman committed
476 477
      "summaryPipelineName" -> summaryPipelineName,
      "summaryModuleName" -> summaryModuleName,
478 479 480
      "centrifugeTag" -> centrifugeTag,
      "allSamples" -> allSamples,
      "allLibraries" -> allLibraries,
rhpvorderman's avatar
rhpvorderman committed
481 482
      "summaries" -> summariesVal,
      "totalReads" -> totalReadsVal
483
    )
484 485
  }

rhpvorderman's avatar
rhpvorderman committed
486 487 488 489
  def summaries(summary: SummaryDb,
                runId: Int,
                sampleId: Option[Int],
                libId: Option[Int],
rhpvorderman's avatar
rhpvorderman committed
490 491
                summaryPipelineName: String,
                summaryModuleName: String): Map[Int, Map[String, Option[Any]]] = {
rhpvorderman's avatar
rhpvorderman committed
492 493 494
    if (libId.isDefined)
      summary
        .getStatsForLibraries(runId,
rhpvorderman's avatar
rhpvorderman committed
495 496
                              summaryPipelineName,
                              summaryModuleName,
rhpvorderman's avatar
rhpvorderman committed
497 498 499 500 501 502
                              sampleId,
                              Map("all" -> Nil))
        .filter(_._1._2 == libId.get)
        .map(x => x._1._1 -> x._2)
    else
      summary.getStatsForSamples(runId,
rhpvorderman's avatar
rhpvorderman committed
503 504
                                 summaryPipelineName,
                                 summaryModuleName,
rhpvorderman's avatar
rhpvorderman committed
505 506 507 508 509 510 511 512
                                 sampleId.map(SampleId),
                                 Map("all" -> Nil))
  }
  def totalReads(
      summary: SummaryDb,
      runId: Int,
      sampleId: Option[Int],
      libId: Option[Int],
rhpvorderman's avatar
rhpvorderman committed
513
      summaryPipelineName: String,
rhpvorderman's avatar
rhpvorderman committed
514 515 516
      centrifugeTag: Option[String],
      allSamples: Seq[Sample],
      allLibraries: Seq[Library]
517
  ): Option[Map[String, Long]] = {
rhpvorderman's avatar
rhpvorderman committed
518 519 520 521
    centrifugeTag.map { tag =>
      if (libId.isDefined) {
        val stats = summary
          .getStatsForLibraries(runId,
rhpvorderman's avatar
rhpvorderman committed
522
                                summaryPipelineName,
rhpvorderman's avatar
rhpvorderman committed
523 524 525 526 527 528 529 530 531 532 533
                                ModuleName(tag),
                                sampleId,
                                Map("total" -> List("metrics", "Read")))
          .filter(_._1._2 == libId.get)
          .head
        val lib = allLibraries.filter(_.id == stats._1._2).head
        val sample = allSamples.filter(_.id == stats._1._1).head
        Map(s"${sample.name}" -> stats._2("total").map(_.toString.toLong).getOrElse(0L))
      } else
        summary
          .getStatsForSamples(runId,
rhpvorderman's avatar
rhpvorderman committed
534
                              summaryPipelineName,
rhpvorderman's avatar
rhpvorderman committed
535 536 537 538 539 540 541 542 543
                              ModuleName(tag),
                              sampleId.map(SummaryDb.SampleId),
                              Map("total" -> List("metrics", "Read")))
          .map(
            x =>
              allSamples
                .find(_.id == x._1)
                .head
                .name -> x._2("total").map(_.toString.toLong).getOrElse(0L))
544
    }
545 546
  }

rhpvorderman's avatar
rhpvorderman committed
547
}