Commit 9bc8ae66 authored by Peter van 't Hof's avatar Peter van 't Hof

Added methods for runs and samples

parent 7eba2ad3
......@@ -2,8 +2,12 @@ package nl.lumc.sasc.biopet.tools
import java.io.File
import nl.lumc.sasc.biopet.utils.summary.db.Schema
import nl.lumc.sasc.biopet.utils.summary.SummaryDb
import nl.lumc.sasc.biopet.utils.{ ConfigUtils, ToolCommand }
import slick.driver.H2Driver.api._
import scala.concurrent.Await
import scala.concurrent.duration.Duration
/**
* Created by pjvanthof on 26/01/2017.
......@@ -40,7 +44,17 @@ object SummaryToSqlite extends ToolCommand {
} else throw new IllegalArgumentException(s"Db already exist: ${cmdArgs.outputSqlite}")
}
Schema.createEmptySqlite(cmdArgs.outputSqlite)
val db = Database.forURL(s"jdbc:sqlite:${cmdArgs.outputSqlite.getAbsolutePath}", driver = "org.sqlite.JDBC")
val summary = new SummaryDb(db)
summary.createTables
val runId = Await.result(summary.createRun("runName", "kdfhla"), Duration.Inf)
List("1", "2", "3", "4").foreach(x => Await.result(summary.createSample(runId, x), Duration.Inf))
println(Await.result(summary.getSamples(), Duration.Inf))
db.close()
logger.info("Done")
}
......
......@@ -22,6 +22,8 @@ import nl.lumc.sasc.biopet.utils.ConfigUtils
* This class can read in a summary and extract values from it
*
* Created by pjvan_thof on 3/26/15.
*
* @deprecated
*/
class Summary(val map: Map[String, Any]) {
......
package nl.lumc.sasc.biopet.utils.summary
import java.sql.Blob
import slick.driver.H2Driver.api._
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import nl.lumc.sasc.biopet.utils.summary.db.Schema._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.io.Source
/**
* Created by pjvanthof on 05/02/2017.
*/
class SummaryDb(db: Database) {
/** This method will create all table */
def createTables(): Unit = {
try {
val setup = DBIO.seq(
(runs.schema ++ samples.schema ++
libraries.schema ++ pipelineNames.schema ++
moduleNames.schema ++ stats.schema ++ settings.schema ++
files.schema ++ executables.schema).create
)
val setupFuture = db.run(setup)
Await.result(setupFuture, Duration.Inf)
}
}
def createRun(runName: String, outputDir: String): Future[Int] = {
val id = Await.result(db.run(runs.size.result), Duration.Inf)
db.run(runs.forceInsert(id, runName, outputDir)).map(_ => id)
}
def getRuns(runId: Option[Int] = None, runName: Option[String] = None, outputDir: Option[String] = None) = {
val q = runs.filter { run =>
List(
runId.map(run.id === _),
runName.map(run.runName === _),
outputDir.map(run.outputDir === _) // not a condition as `criteriaRoast` evaluates to `None`
).collect({ case Some(criteria) => criteria }).reduceLeftOption(_ && _).getOrElse(true: Rep[Boolean])
}
db.run(q.result)
}
def createSample(runId: Int, name: String, tags: Option[String] = None): Future[Int] = {
val id = Await.result(db.run(samples.size.result), Duration.Inf)
db.run(samples.forceInsert(id, runId, name, tags)).map(_ => id)
}
def getSamples(sampleId: Option[Int] = None, runId: Option[Int] = None, name: Option[String] = None) = {
val q = samples.filter { sample =>
List(
sampleId.map(sample.id === _),
runId.map(sample.runId === _),
name.map(sample.name === _) // not a condition as `criteriaRoast` evaluates to `None`
).collect({ case Some(criteria) => criteria }).reduceLeftOption(_ && _).getOrElse(true: Rep[Boolean])
}
db.run(q.map(x => (x.id, x.runId, x.name)).result)
}
def sampleTags(sampleId: Int): Map[String, Any] = {
samples.filter(_.id === sampleId).map(_.tags)
}
}
......@@ -18,6 +18,8 @@ package nl.lumc.sasc.biopet.utils.summary
* This case class is used for easy access and calculations on those values
*
* Created by pjvan_thof on 4/23/15.
*
* @deprecated
*/
case class SummaryValue(value: Option[Any]) {
......
package nl.lumc.sasc.biopet.utils.summary.db
import java.io.File
import java.sql.Blob
import slick.driver.H2Driver.api._
import scala.concurrent.Await
import scala.concurrent.duration.Duration
/**
* Created by pjvan_thof on 27-1-17.
*/
object Schema {
class Runs(tag: Tag) extends Table[(Int, String)](tag, "Runs") {
def runId = column[Int]("runId", O.PrimaryKey)
class Runs(tag: Tag) extends Table[(Int, String, String)](tag, "Runs") {
def id = column[Int]("id", O.PrimaryKey)
def runName = column[String]("runName")
def outputDir = column[String]("outputDir")
def * = (runId, runName)
def * = (id, runName, outputDir)
}
val runs = TableQuery[Runs]
class Samples(tag: Tag) extends Table[(Int, String, Option[Blob])](tag, "Samples") {
def sampleId = column[Int]("id", O.PrimaryKey)
def sampleName = column[String]("name")
def tags = column[Option[Blob]]("tags")
class Samples(tag: Tag) extends Table[(Int, Int, String, Option[String])](tag, "Samples") {
def id = column[Int]("id", O.PrimaryKey)
def runId = column[Int]("runId")
def name = column[String]("name")
def tags = column[Option[String]]("tags")
def * = (sampleId, sampleName, tags)
def * = (id, runId, name, tags)
}
val samples = TableQuery[Samples]
class SamplesRuns(tag: Tag) extends Table[(Int, Int)](tag, "SamplesRuns") {
def sampleId = column[Int]("sampleId")
class Libraries(tag: Tag) extends Table[(Int, Int, String, Int, Option[String])](tag, "Libraries") {
def id = column[Int]("id", O.PrimaryKey)
def runId = column[Int]("runId")
def * = (sampleId, runId)
def idx = index("idx_samples_runs", (sampleId, runId), unique = true)
}
val samplesRuns = TableQuery[SamplesRuns]
class Libraries(tag: Tag) extends Table[(Int, String, Int, Option[Blob])](tag, "Libraries") {
def libraryId = column[Int]("id", O.PrimaryKey)
def libraryName = column[String]("name")
def sampleId = column[Int]("sampleId")
def tags = column[Option[Blob]]("tags")
def tags = column[Option[String]]("tags")
def * = (libraryId, libraryName, sampleId, tags)
def * = (id, runId, libraryName, sampleId, tags)
}
val libraries = TableQuery[Libraries]
class LibrariesRuns(tag: Tag) extends Table[(Int, Int)](tag, "LibrariesRuns") {
def libraryId = column[Int]("libraryId")
def runId = column[Int]("runId")
def * = (libraryId, runId)
def idx = index("idx_libraries_runs", (libraryId, runId), unique = true)
}
val librariesRuns = TableQuery[LibrariesRuns]
class PipelineNames(tag: Tag) extends Table[(Int, String)](tag, "PipelineNames") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def id = column[Int]("id", O.PrimaryKey)
def name = column[String]("name")
def * = (id, name)
......@@ -81,13 +58,13 @@ object Schema {
val moduleNames = TableQuery[ModuleNames]
class Stats(tag: Tag) extends Table[(Int, Int, Option[Int], Option[Int], Option[Int], Blob, Option[String])](tag, "Stats") {
class Stats(tag: Tag) extends Table[(Int, Int, Option[Int], Option[Int], Option[Int], String, Option[String])](tag, "Stats") {
def pipelineId = column[Int]("pipelineId")
def runId = column[Int]("runId")
def moduleId = column[Option[Int]]("moduleId")
def sampleId = column[Option[Int]]("sampleId")
def libraryId = column[Option[Int]]("libraryId")
def stats = column[Blob]("stats")
def stats = column[String]("stats")
def schema = column[Option[String]]("schema")
def * = (pipelineId, runId, moduleId, sampleId, libraryId, stats, schema)
......@@ -96,13 +73,13 @@ object Schema {
}
val stats = TableQuery[Stats]
class Settings(tag: Tag) extends Table[(Int, Int, Option[Int], Option[Int], Option[Int], Blob, Option[String])](tag, "Settings") {
class Settings(tag: Tag) extends Table[(Int, Int, Option[Int], Option[Int], Option[Int], String, Option[String])](tag, "Settings") {
def pipelineId = column[Int]("pipelineId")
def runId = column[Int]("runId")
def moduleId = column[Option[Int]]("moduleId")
def sampleId = column[Option[Int]]("sampleId")
def libraryId = column[Option[Int]]("libraryId")
def stats = column[Blob]("stats")
def stats = column[String]("stats")
def schema = column[Option[String]]("schema")
def * = (pipelineId, runId, moduleId, sampleId, libraryId, stats, schema)
......@@ -111,20 +88,21 @@ object Schema {
}
val settings = TableQuery[Settings]
class Files(tag: Tag) extends Table[(Int, Int, Option[Int], Option[Int], Option[Int], String, String, Boolean, Long)](tag, "Files") {
class Files(tag: Tag) extends Table[(Int, Int, Option[Int], Option[Int], Option[Int], String, String, String, Boolean, Long)](tag, "Files") {
def pipelineId = column[Int]("pipelineId")
def runId = column[Int]("runId")
def moduleId = column[Option[Int]]("moduleId")
def sampleId = column[Option[Int]]("sampleId")
def libraryId = column[Option[Int]]("libraryId")
def name = column[String]("name")
def path = column[String]("path")
def md5 = column[String]("md5")
def link = column[Boolean]("link", O.Default(false))
def size = column[Long]("size")
def * = (pipelineId, runId, moduleId, sampleId, libraryId, path, md5, link, size)
def * = (pipelineId, runId, moduleId, sampleId, libraryId, name, path, md5, link, size)
def idx = index("idx_files", (pipelineId, runId, sampleId, libraryId, path), unique = true)
def idx = index("idx_files", (pipelineId, runId, sampleId, libraryId, name), unique = true)
}
val files = TableQuery[Files]
......@@ -142,20 +120,4 @@ object Schema {
}
val executables = TableQuery[Executables]
def createEmptySqlite(file: File): Unit = {
val db = Database.forURL(s"jdbc:sqlite:${file.getAbsolutePath}", driver = "org.sqlite.JDBC")
try {
val setup = DBIO.seq(
(runs.schema ++ samples.schema ++
samplesRuns.schema ++ libraries.schema ++
librariesRuns.schema ++ pipelineNames.schema ++
moduleNames.schema ++ stats.schema ++ settings.schema ++
files.schema ++ executables.schema).create
)
val setupFuture = db.run(setup)
Await.result(setupFuture, Duration.Inf)
} finally db.close
}
}
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