GearsKronaPlot.scala 3.27 KB
Newer Older
1 2 3 4
package nl.lumc.sasc.biopet.pipelines.gears
import nl.lumc.sasc.biopet.utils.summary.db.Schema.{Library, Sample}
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb.{ModuleName, SampleId}
rhpvorderman's avatar
rhpvorderman committed
5
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb.Implicts._
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

object GearsKronaPlot {
  def values(summary: SummaryDb,
             runId: Int,
             summaryPipelineName: String,
             summaryModuleName: String,
             allSamples: Seq[Sample],
             allLibraries: Seq[Library],
             sampleId: Option[Int] = None,
             libId: Option[Int] = None,
             centrifugeTag: Option[String] = None): Map[String, Any] = {
    val summariesVal =
      summaries(summary, runId, sampleId, libId, summaryPipelineName, summaryModuleName)
    val totalReadsVal = totalReads(summary,
      runId,
      sampleId,
      libId,
      summaryPipelineName,
      centrifugeTag,
      allSamples,
      allLibraries)
    Map(
      "sampleId" -> sampleId,
      "summaryPipelineName" -> summaryPipelineName,
      "summaryModuleName" -> summaryModuleName,
      "centrifugeTag" -> centrifugeTag,
      "allSamples" -> allSamples,
      "summaries" -> summariesVal,
      "totalReads" -> totalReadsVal
    )
  }

  def summaries(summary: SummaryDb,
                runId: Int,
                sampleId: Option[Int],
                libId: Option[Int],
                summaryPipelineName: String,
                summaryModuleName: String): Map[Int, Map[String, Option[Any]]] = {
    if (libId.isDefined)
      summary
        .getStatsForLibraries(runId,
          summaryPipelineName,
          summaryModuleName,
          sampleId,
          Map("all" -> Nil))
        .filter(_._1._2 == libId.get)
        .map(x => x._1._1 -> x._2)
    else
      summary.getStatsForSamples(runId,
        summaryPipelineName,
        summaryModuleName,
        sampleId.map(SampleId),
        Map("all" -> Nil))
  }
  def totalReads(
                  summary: SummaryDb,
                  runId: Int,
                  sampleId: Option[Int],
                  libId: Option[Int],
                  summaryPipelineName: String,
                  centrifugeTag: Option[String],
                  allSamples: Seq[Sample],
                  allLibraries: Seq[Library]
                ): Option[Map[String, Long]] = {
    centrifugeTag.map { tag =>
      if (libId.isDefined) {
        val stats = summary
          .getStatsForLibraries(runId,
            summaryPipelineName,
            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,
            summaryPipelineName,
            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))
    }
  }

}