Commit b27102c7 authored by Peter van 't Hof's avatar Peter van 't Hof

Try with Either class

parent d4d307a2
......@@ -17,13 +17,15 @@ package nl.lumc.sasc.biopet.core.report
import java.io._
import nl.lumc.sasc.biopet.core.ToolCommandFunction
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.{IoUtils, Logging, ToolCommand}
import org.broadinstitute.gatk.utils.commandline.Input
import nl.lumc.sasc.biopet.utils.tryToParseNumber
import org.fusesource.scalate.{TemplateEngine, TemplateSource}
import scala.collection.mutable
import scala.concurrent.Await
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import scala.language.postfixOps
......@@ -72,6 +74,7 @@ trait ReportBuilderExtension extends ToolCommandFunction {
trait ReportBuilder extends ToolCommand {
implicit def toOption[T](x: T) : Option[T] = Option(x)
implicit def autoWait[T](x: Future[T]) : T = Await.result(x, Duration.Inf)
case class Args(summaryDbFile: File = null,
outputDir: File = null,
......@@ -115,21 +118,10 @@ trait ReportBuilder extends ToolCommand {
final def runId = setRunId
private var setSampleCache: Map[String, Int] = _
final def sampleCache = setSampleCache
private var setLibraryCache: Map[(String, String), Int] = _
final def libraryCache = setSampleCache
private var setPipelineCache: Map[String, Int] = _
final def pipelineCache = setPipelineCache
private var setModuleCache: Map[String, Int] = _
final def moduleCache = setModuleCache
private var _setSamples = Seq[Sample]()
final def samples = _setSamples
private var _setLibraries = Seq[Library]()
final def libraries = _setLibraries
/** default args that are passed to all page withing the report */
def pageArgs: Map[String, Any] = Map()
......@@ -137,9 +129,9 @@ trait ReportBuilder extends ToolCommand {
private var done = 0
private var total = 0
private var _sampleId: Option[String] = None
private var _sampleId: Option[Int] = None
protected[report] def sampleId = _sampleId
private var _libId: Option[String] = None
private var _libId: Option[Int] = None
protected[report] def libId = _libId
case class ExtFile(resourcePath: String, targetPath: String)
......@@ -168,20 +160,26 @@ trait ReportBuilder extends ToolCommand {
require(cmdArgs.outputDir.exists(), "Output dir does not exist")
require(cmdArgs.outputDir.isDirectory, "Output dir is not a directory")
setSummary = SummaryDb.openSqliteSummary(cmdArgs.summaryDbFile)
setRunId = cmdArgs.runId
cmdArgs.pageArgs.get("sampleId") match {
case Some(s: String) =>
cmdArgs.pageArgs += "sampleId" -> Some(s)
_sampleId = Some(s)
_sampleId = Await.result(summary.getSampleId(runId, s), Duration.Inf)
cmdArgs.pageArgs += "sampleId" -> sampleId
case _ =>
}
cmdArgs.pageArgs.get("libId") match {
case Some(l: String) =>
cmdArgs.pageArgs += "libId" -> Some(l)
_libId = Some(l)
_libId = Await.result(summary.getLibraryId(runId, sampleId.get, l), Duration.Inf)
cmdArgs.pageArgs += "libId" -> libId
case _ =>
}
_setSamples = Await.result(summary.getSamples(runId = Some(runId), sampleId = sampleId), Duration.Inf)
_setLibraries = Await.result(summary.getLibraries(runId = Some(runId), sampleId = sampleId, libId = libId), Duration.Inf)
logger.info("Copy Base files")
// Static files that will be copied to the output folder, then file is added to [resourceDir] it's need to be added here also
......@@ -196,14 +194,6 @@ trait ReportBuilder extends ToolCommand {
createDirs = true)
)
logger.info("Parsing summary")
setSummary = SummaryDb.openSqliteSummary(cmdArgs.summaryDbFile)
setRunId = cmdArgs.runId
setSampleCache = Await.result(summary.getSamples(runId = Some(runId)), Duration.Inf).map(r => r.name -> r.id).toMap
setLibraryCache = Await.result(summary.getLibraries(runId = Some(runId)), Duration.Inf).map(r => (setSampleCache.find(_._2 == r.sampleId).get._1, r.name) -> r.id).toMap
setPipelineCache = Await.result(summary.getPipelines(runId = Some(runId)), Duration.Inf).map(r => r.name -> r.id).toMap
setModuleCache = Await.result(summary.getModules(runId = Some(runId)), Duration.Inf).map(r => r.name -> r.id).toMap
total = ReportBuilder.countPages(indexPage)
logger.info(total + " pages to be generated")
......
package nl.lumc.sasc.biopet.utils.implicits
/**
* Created by pjvanthof on 20/02/2017.
*/
object Either {
implicit def optionEitherLeft[T](x: Option[Either[T,_]]): Option[Option[T]] = {
x match {
case Some(i:Either[T, _]) if i.isLeft => Some(i.left.toOption)
case _ => None
}
}
implicit def optionEitherRight[T](x: Option[Either[_,T]]): Option[Option[T]] = {
x match {
case Some(i:Either[_,T]) if i.isRight => Some(i.right.toOption)
case _ => None
}
}
implicit def eitherLeft[T](x: Either[T,_]): Option[T] = {
x match {
case Left(x) => Some(x)
case _ => None
}
}
implicit def eitherRight[T](x: Either[_,T]): Option[T] = {
x match {
case Right(x) => Some(x)
case _ => None
}
}
implicit def left[T](x:T): Left[T, _] = Left(x)
implicit def right[T](x:T): Right[_, T] = Right(x)
implicit def left[T](x:Option[T]): Option[Left[T, _]] = x.map(Left(_))
implicit def right[T](x:Option[T]): Option[Right[_, T]] = x.map(Right(_))
}
......@@ -23,9 +23,9 @@ class SummaryDbTest extends TestNGSuite with Matchers {
Await.result(db.createOrUpdateSetting(0, 0, None, None, None, """{"content": "test" }"""), Duration.Inf)
val bla = Await.result(db.getSettings(Some(0)), Duration.Inf)
Await.result(db.getSetting(0, 0, None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test"))
Await.result(db.getSetting(0, Left(0), None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test"))
Await.result(db.createOrUpdateSetting(0, 0, None, None, None, """{"content": "test2" }"""), Duration.Inf)
Await.result(db.getSetting(0, 0, None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test2"))
Await.result(db.getSetting(0, Left(0), None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test2"))
db.close()
}
......@@ -37,9 +37,9 @@ class SummaryDbTest extends TestNGSuite with Matchers {
db.createTables()
Await.result(db.createOrUpdateStat(0, 0, None, None, None, """{"content": "test" }"""), Duration.Inf)
Await.result(db.getStat(0, 0, None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test"))
Await.result(db.getStat(0, Left(0), None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test"))
Await.result(db.createOrUpdateStat(0, 0, None, None, None, """{"content": "test2" }"""), Duration.Inf)
Await.result(db.getStat(0, 0, None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test2"))
Await.result(db.getStat(0, Left(0), None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test2"))
db.close()
}
......
......@@ -14,8 +14,7 @@
*/
package nl.lumc.sasc.biopet.pipelines.gears
import java.io.File
import nl.lumc.sasc.biopet.utils.implicits.Either._
import nl.lumc.sasc.biopet.core.report.{MultisampleReportBuilder, ReportBuilderExtension, ReportPage, ReportSection}
import nl.lumc.sasc.biopet.pipelines.flexiprep.FlexiprepReport
import nl.lumc.sasc.biopet.utils.config.Configurable
......@@ -40,10 +39,10 @@ object GearsReport extends MultisampleReportBuilder {
.map(x => ExtFile("/nl/lumc/sasc/biopet/pipelines/gears/report/ext/" + x, x))
def indexPage = {
val krakenExecuted = Await.result(summary.getStatsSize(runId, "gearskraken", Some("krakenreport"), libName = None, mustHaveSample = true), Duration.Inf) >= sampleCache.size
val centrifugeExecuted = Await.result(summary.getStatsSize(runId, "gearscentrifuge", Some("centrifuge_report"), libName = None, mustHaveSample = true), Duration.Inf) >= sampleCache.size
val qiimeClosesOtuTable = Await.result(summary.getFile(runId, "gears", None, None, None, key = "qiime_closed_otu_table"), Duration.Inf)
val qiimeOpenOtuTable = Await.result(summary.getFile(runId, "gears", None, None, None, key = "qiime_open_otu_table"), Duration.Inf)
val krakenExecuted = summary.getStatsSize(runId = runId, pipeline = Right("gearskraken"), module = Some(Right("krakenreport")), library = Some(None), mustHaveSample = true) >= samples.size
val centrifugeExecuted = summary.getStatsSize(runId, Right("gearscentrifuge"), Some(Right("centrifuge_report")), library = Some(None), mustHaveSample = true) >= samples.size
val qiimeClosesOtuTable = summary.getFile(runId, "gears", None, None, None, key = "qiime_closed_otu_table")
val qiimeOpenOtuTable = summary.getFile(runId, "gears", None, None, None, key = "qiime_open_otu_table")
ReportPage(
(if (centrifugeExecuted) List("Centriguge analysis" -> ReportPage(List("Non-unique" -> ReportPage(List(), List("All mappings" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
......@@ -82,8 +81,8 @@ object GearsReport extends MultisampleReportBuilder {
/** Single sample page */
def samplePage(sampleId: Int, args: Map[String, Any]): ReportPage = {
val sampleName = Await.result(summary.getSampleName(sampleId), Duration.Inf)
val krakenExecuted = Await.result(summary.getStatsSize(runId, "gearskraken", Some("krakenreport"), sampleName = sampleName, libName = None), Duration.Inf) == 1
val centrifugeExecuted = Await.result(summary.getStatsSize(runId, "gearscentrifuge", Some("centrifuge_report"), sampleName = sampleName, libName = None), Duration.Inf) == 1
val krakenExecuted = Await.result(summary.getStatsSize(runId, Right("gearskraken"), Some(Right("krakenreport")), sample = Left(sampleId), library = Some(None)), Duration.Inf) == 1
val centrifugeExecuted = Await.result(summary.getStatsSize(runId, Right("gearscentrifuge"), Some(Right("centrifuge_report")), sample = Left(sampleId), library = None), Duration.Inf) == 1
val qiimeClosesOtuTable = Await.result(summary.getFile(runId, "gears", None, sampleName, None, key = "qiime_closed_otu_table"), Duration.Inf)
val qiimeOpenOtuTable = Await.result(summary.getFile(runId, "gears", None, sampleName, None, key = "qiime_open_otu_table"), Duration.Inf)
......@@ -115,10 +114,10 @@ object GearsReport extends MultisampleReportBuilder {
val sName = Await.result(summary.getSampleName(sampleId), Duration.Inf)
val lName = Await.result(summary.getLibraryName(libId), Duration.Inf)
val flexiprepExecuted = Await.result(summary.getStatsSize(Some(runId), Some("flexiprep"), None, Some(sName),Some(lName)), Duration.Inf) >= 1
val flexiprepExecuted = Await.result(summary.getStatsSize(runId, Some(Right("flexiprep")), sample = Some(Left(sampleId)), library = Some(Left(libId))), Duration.Inf) >= 1
val krakenExecuted = Await.result(summary.getStatsSize(runId, "gearskraken", Some("krakenreport"), sampleName = sName, libName = lName), Duration.Inf) == 1
val centrifugeExecuted = Await.result(summary.getStatsSize(runId, "gearscentrifuge", Some("centrifuge_report"), sampleName = sName, libName = lName), Duration.Inf) == 1
val krakenExecuted = Await.result(summary.getStatsSize(runId, Right("gearskraken"), Some(Right("krakenreport")), sample = Some(Left(sampleId)), library = Some(Left(libId))), Duration.Inf) == 1
val centrifugeExecuted = Await.result(summary.getStatsSize(runId, Right("gearscentrifuge"), Some(Right("centrifuge_report")), sample = Some(Left(sampleId)), library = Some(Left(libId))), Duration.Inf) == 1
val qiimeClosesOtuTable = Await.result(summary.getFile(runId, "gears", None, sName, lName, key = "qiime_closed_otu_table"), Duration.Inf)
val qiimeOpenOtuTable = Await.result(summary.getFile(runId, "gears", None, sName, lName, key = "qiime_open_otu_table"), Duration.Inf)
......
......@@ -30,9 +30,11 @@ object GearsSingleReport extends ReportBuilder {
.map(x => ExtFile("/nl/lumc/sasc/biopet/pipelines/gears/report/ext/" + x, x))
def indexPage = {
Await.result(summary.getStatsSize(runId, "gearscentrifuge", Some("centrifuge_report"), sampleId, libId), Duration.Inf) == 1
val krakenExecuted = Await.result(summary.getStatsSize(runId, "gearskraken", Some("krakenreport"), sampleId, libId), Duration.Inf) == 1
val centrifugeExecuted = Await.result(summary.getStatsSize(runId, "gearscentrifuge", Some("centrifuge_report"), sampleId, libId), Duration.Inf) == 1
val sampleName = sampleId.flatMap(x => Await.result(summary.getSampleName(x), Duration.Inf))
val libraryName = libId.flatMap(x => Await.result(summary.getLibraryName(x), Duration.Inf))
val krakenExecuted = summary.getStatsSize(runId, Right("gearskraken"), Some(Right("krakenreport")), sample = sampleId.map(Left(_)), library = libId.map(Left(_))) == 1
val centrifugeExecuted = summary.getStatsSize(runId, Right("gearscentrifuge"), Some(Right("centrifuge_report")), sample = sampleId.map(Left(_)), library = libId.map(Left(_))) == 1
ReportPage(
List(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment