Commit 6dd0c8cf authored by Peter van 't Hof's avatar Peter van 't Hof

Change sample and libraries to createOrUpdate

parent cfb37cde
......@@ -264,12 +264,12 @@ trait MultiSampleQScript extends SummaryQScript { qscript: QScript =>
for ((sampleName, sample) <- samples) {
val sampleTags = if (sample.sampleTags.nonEmpty) Some(ConfigUtils.mapToJson(sample.sampleTags).nospaces) else None
val sampleId: Int = if (!namesOld.contains(sampleName))
Await.result(db.createSample(sampleName, summaryRunId, sampleTags), Duration.Inf)
Await.result(db.createOrUpdateSample(sampleName, summaryRunId, sampleTags), Duration.Inf)
else Await.result(db.getSamples(runId = Some(summaryRunId), name = Some(sampleName)).map(_.head.id), Duration.Inf)
val libNamesOld = Await.result(db.getLibraries(runId = summaryRunId, sampleId = sampleId).map(_.map(_.name)), Duration.Inf)
for ((libName, lib) <- sample.libraries) {
val libraryTags = if (lib.libTags.nonEmpty) Some(ConfigUtils.mapToJson(sample.sampleTags).nospaces) else None
if (!libNamesOld.contains(libName)) Await.result(db.createLibrary(libName, summaryRunId, sampleId, libraryTags), Duration.Inf)
if (!libNamesOld.contains(libName)) Await.result(db.createOrUpdateLibrary(libName, summaryRunId, sampleId, libraryTags), Duration.Inf)
}
}
}
......
......@@ -67,11 +67,11 @@ class WriteSummary(val parent: SummaryQScript) extends InProcessFunction with Co
case t: SampleLibraryTag => t.sampleId.foreach {
case sampleName =>
val sampleId = Await.result(db.getSamples(name = Some(sampleName), runId = Some(qscript.summaryRunId)).map(_.headOption.map(_.id)), Duration.Inf).getOrElse {
Await.result(db.createSample(sampleName, qscript.summaryRunId), Duration.Inf)
Await.result(db.createOrUpdateSample(sampleName, qscript.summaryRunId), Duration.Inf)
}
t.libId.foreach { libName =>
val libId = Await.result(db.getSamples(name = Some(libName), runId = Some(qscript.summaryRunId), sampleId = Some(sampleId)).map(_.headOption.map(_.id)), Duration.Inf).getOrElse {
Await.result(db.createLibrary(libName, qscript.summaryRunId, sampleId), Duration.Inf)
Await.result(db.createOrUpdateLibrary(libName, qscript.summaryRunId, sampleId), Duration.Inf)
}
}
}
......
......@@ -62,6 +62,16 @@ class SummaryDb(val db: Database) extends Closeable {
db.run(samples.forceInsert(Sample(id, name, runId, tags))).map(_ => id)
}
def createOrUpdateSample(name: String, runId: Int, tags: Option[String] = None): Future[Int] = {
getSampleId(runId, name).flatMap(_ match {
case Some(id: Int) =>
db.run(samples.filter(_.name === name).filter(_.id === id).map(_.tags).update(tags))
.map(_ => id)
case _ => createSample(name, runId, tags)
})
}
/** This will return all samples that match given criteria */
def getSamples(sampleId: Option[Int] = None, runId: Option[Int] = None, name: Option[String] = None): Future[Seq[Sample]] = {
val q = samples.filter { sample =>
......@@ -96,6 +106,15 @@ class SummaryDb(val db: Database) extends Closeable {
db.run(libraries.forceInsert(Library(id, name, runId, sampleId, tags))).map(_ => id)
}
def createOrUpdateLibrary(name: String, runId: Int, sampleId: Int, tags: Option[String] = None): Future[Int] = {
getLibraryId(runId, sampleId, name).flatMap(_ match {
case Some(id: Int) =>
db.run(libraries.filter(_.name === name).filter(_.id === id).filter(_.sampleId === sampleId).map(_.tags).update(tags))
.map(_ => id)
case _ => createLibrary(name, runId, sampleId, tags)
})
}
/** This returns all libraries that match the given criteria */
def getLibraries(libId: Option[Int] = None, name: Option[String] = None, runId: Option[Int] = None, sampleId: Option[Int] = None): Future[Seq[Library]] = {
val q = libraries.filter { lib =>
......
......@@ -49,14 +49,17 @@ class SummaryDbTest extends TestNGSuite with Matchers {
val runId = Await.result(db.createRun("name", "dir", "version", "hash", date), Duration.Inf)
Await.result(db.getSamples(), Duration.Inf) shouldBe empty
val sampleId = Await.result(db.createSample("test_sample", runId), Duration.Inf)
Await.result(db.createOrUpdateSample("test_sample", runId), Duration.Inf) shouldBe sampleId
Await.result(db.getSamples(), Duration.Inf) shouldBe Seq(Schema.Sample(sampleId, "test_sample", runId, None))
Await.result(db.getSampleName(sampleId), Duration.Inf) shouldBe Some("test_sample")
Await.result(db.getSampleId(runId, "test_sample"), Duration.Inf) shouldBe Some(sampleId)
Await.result(db.getSampleTags(sampleId), Duration.Inf) shouldBe None
Await.result(db.createOrUpdateSample("test_sample", runId, Some("""{"test": "test"}""")), Duration.Inf) shouldBe sampleId
Await.result(db.getSampleTags(sampleId), Duration.Inf) shouldBe Some(Map("test" -> "test"))
val sampleId2 = Await.result(db.createSample("test_sample2", runId, Some("""{"test": "test"}""")), Duration.Inf)
Await.result(db.getSampleTags(sampleId2), Duration.Inf) shouldBe Some(Map("test" -> "test"))
Await.result(db.getSamples(), Duration.Inf) shouldBe Seq(Schema.Sample(sampleId, "test_sample", runId, None), Schema.Sample(sampleId2, "test_sample2", runId, Some("""{"test": "test"}""")))
Await.result(db.getSamples(), Duration.Inf) shouldBe Seq(Schema.Sample(sampleId, "test_sample", runId, Some("""{"test": "test"}""")), Schema.Sample(sampleId2, "test_sample2", runId, Some("""{"test": "test"}""")))
db.close()
}
......@@ -74,6 +77,7 @@ class SummaryDbTest extends TestNGSuite with Matchers {
val sampleId = Await.result(db.createSample("test_sample", runId), Duration.Inf)
Await.result(db.getLibraries(), Duration.Inf) shouldBe empty
val libraryId = Await.result(db.createLibrary("test_lib", runId, sampleId), Duration.Inf)
Await.result(db.createOrUpdateLibrary("test_lib", runId, sampleId), Duration.Inf) shouldBe libraryId
Await.result(db.getLibraries(), Duration.Inf) shouldBe Seq(Schema.Library(libraryId, "test_lib", runId, sampleId, None))
Await.result(db.getLibraryName(libraryId), Duration.Inf) shouldBe Some("test_lib")
......
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