Commit 4228881c authored by Peter van 't Hof's avatar Peter van 't Hof

Adding stats to db

parent c8a93931
......@@ -26,9 +26,9 @@ import org.broadinstitute.gatk.utils.commandline.{Input, Output}
import scala.collection.mutable
import scala.io.Source
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
/**
* This will collect and write the summary
......@@ -87,7 +87,25 @@ class WriteSummary(val parent: SummaryQScript) extends InProcessFunction with Co
def run(): Unit = {
val db = SummaryDb.openSqliteSummary(qscript.summaryDbFile)
//TODO: Add stats
val pipelineId = Await.result(db.getPipelines(name = Some(qscript.summaryName), runId = Some(qscript.summaryRunId)).map(_.head.id), Duration.Inf)
for (((name, sampleName, libName), summarizables) <-qscript.summarizables) {
require(summarizables.nonEmpty)
val stats = ConfigUtils.anyToJson(if (summarizables.size == 1) summarizables.head.summaryStats
else {
val s = summarizables.map(_.summaryStats)
s.tail.foldLeft(Map("stats" -> s.head))((a,b) =>
ConfigUtils.mergeMaps(a, Map("stats" -> b), summarizables.head.resolveSummaryConflict))("stats")
})
val moduleId = db.getModules(name = Some(name), runId = Some(qscript.summaryRunId), pipelineId = Some(pipelineId))
.map(_.head.id)
val sampleId = sampleName.map(name => db.getSamples(runId = Some(qscript.summaryRunId), name = Some(name)).map(_.head.id))
val libId = libName.map(name => db.getLibraries(runId = Some(qscript.summaryRunId), name = Some(name),
sampleId = sampleId.map(Await.result(_, Duration.Inf))).map(_.head.id))
db.createOrUpdateStat(qscript.summaryRunId, pipelineId, Some(Await.result(moduleId, Duration.Inf)),
sampleId.map(Await.result(_, Duration.Inf)), libId.map(Await.result(_, Duration.Inf)), stats.nospaces)
}
//TODO: Add Files
......
......@@ -145,8 +145,16 @@ class SummaryDb(db: Database) extends Closeable {
db.run(stats.forceInsert(Stat(runId, pipelineId, moduleId, sampleId, libId, content)))
}
def getStats(runId: Option[Int] = None, pipelineId: Option[Int] = None, moduleId: Option[Option[Int]] = None,
sampleId: Option[Option[Int]] = None, libId: Option[Option[Int]] = None) = {
def createOrUpdateStat(runId: Int, pipelineId: Int, moduleId: Option[Int] = None,
sampleId: Option[Int] = None, libId: Option[Int] = None, content: String) = {
val filter = statsFilter(Some(runId), Some(pipelineId), Some(moduleId), Some(sampleId), Some(libId))
val r = Await.result(db.run(filter.size.result), Duration.Inf)
if (r == 0) createStat(runId, pipelineId, moduleId, sampleId, libId, content)
else db.run(filter.map(_.content).update(content))
}
private def statsFilter(runId: Option[Int] = None, pipelineId: Option[Int] = None, moduleId: Option[Option[Int]] = None,
sampleId: Option[Option[Int]] = None, libId: Option[Option[Int]] = None) = {
val l: List[Option[Query[Stats, Stats#TableElementType, Seq] => Query[Stats, Stats#TableElementType, Seq]]] = List(
runId.map(x => y => y.filter(_.runId === x)),
pipelineId.map(x => y => y.filter(_.pipelineId === x)),
......@@ -154,9 +162,12 @@ class SummaryDb(db: Database) extends Closeable {
sampleId.map(x => y => (if (x.isDefined) y.filter(_.sampleId === x) else y.filter(_.sampleId.isEmpty))),
libId.map(x => y => (if (x.isDefined) y.filter(_.libraryId === x) else y.filter(_.libraryId.isEmpty)))
)
val q = l.flatten.foldLeft(stats.subquery)((a,b) => b(a))
l.flatten.foldLeft(stats.subquery)((a,b) => b(a))
}
db.run(q.result)
def getStats(runId: Option[Int] = None, pipelineId: Option[Int] = None, moduleId: Option[Option[Int]] = None,
sampleId: Option[Option[Int]] = None, libId: Option[Option[Int]] = None) = {
db.run(statsFilter(runId, pipelineId, moduleId, sampleId, libId).result)
}
def getStat(runId: Int, pipelineId: Int, moduleId: Option[Int] = None,
......
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