Commit 36241436 authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

Fixing BIOPET-614

parent 91336909
...@@ -96,14 +96,6 @@ class WriteSummary(val parent: SummaryQScript) extends InProcessFunction with Co ...@@ -96,14 +96,6 @@ class WriteSummary(val parent: SummaryQScript) extends InProcessFunction with Co
jobOutputFile = new File(qscript.outputDir, s".${qscript.summaryName}.summary.out") jobOutputFile = new File(qscript.outputDir, s".${qscript.summaryName}.summary.out")
} }
def createFile(db: SummaryDb, runId: Int, pipelineId: Int, moduleId: Option[Int], sampleId: Option[Int], libId: Option[Int], key: String, file: File, outputDir: File) = {
val path = file.getAbsolutePath.replace(s"^${outputDir.getAbsolutePath}", ".")
val md5 = WriteSummary.parseChecksum(SummaryQScript.md5sumCache(file))
val size = if (file.exists()) file.length() else 0L
val link = if (file.exists()) java.nio.file.Files.isSymbolicLink(file.toPath) else false
db.createOrUpdateFile(qscript.summaryRunId, pipelineId, moduleId, sampleId, libId, key, path, md5, link, size)
}
/** Function to create summary */ /** Function to create summary */
def run(): Unit = { def run(): Unit = {
val db = SummaryDb.openSqliteSummary(qscript.summaryDbFile) val db = SummaryDb.openSqliteSummary(qscript.summaryDbFile)
...@@ -129,7 +121,7 @@ class WriteSummary(val parent: SummaryQScript) extends InProcessFunction with Co ...@@ -129,7 +121,7 @@ class WriteSummary(val parent: SummaryQScript) extends InProcessFunction with Co
sampleId, libId, stats.nospaces) sampleId, libId, stats.nospaces)
for ((key, file) <- summarizables.head.summaryFiles.par) for ((key, file) <- summarizables.head.summaryFiles.par)
Await.result(createFile(db, qscript.summaryRunId, pipelineId, Some(moduleId), sampleId, libId, key, file, outputDir), Duration.Inf) Await.result(WriteSummary.createFile(db, qscript.summaryRunId, pipelineId, Some(moduleId), sampleId, libId, key, file, outputDir), Duration.Inf)
} }
qscript match { qscript match {
...@@ -137,32 +129,32 @@ class WriteSummary(val parent: SummaryQScript) extends InProcessFunction with Co ...@@ -137,32 +129,32 @@ class WriteSummary(val parent: SummaryQScript) extends InProcessFunction with Co
val sampleId = tag.sampleId.flatMap(name => Await.result(db.getSampleId(qscript.summaryRunId, name), Duration.Inf)) val sampleId = tag.sampleId.flatMap(name => Await.result(db.getSampleId(qscript.summaryRunId, name), Duration.Inf))
val libId = tag.libId.flatMap(name => sampleId.flatMap(sampleId => Await.result(db.getLibraryId(qscript.summaryRunId, sampleId, name), Duration.Inf))) val libId = tag.libId.flatMap(name => sampleId.flatMap(sampleId => Await.result(db.getLibraryId(qscript.summaryRunId, sampleId, name), Duration.Inf)))
for ((key, file) <- qscript.summaryFiles.par) for ((key, file) <- qscript.summaryFiles.par)
Await.result(createFile(db, qscript.summaryRunId, pipelineId, None, sampleId, libId, key, file, outputDir), Duration.Inf) Await.result(WriteSummary.createFile(db, qscript.summaryRunId, pipelineId, None, sampleId, libId, key, file, outputDir), Duration.Inf)
db.createOrUpdateSetting(qscript.summaryRunId, pipelineId, None, sampleId, libId, ConfigUtils.mapToJson(tag.summarySettings).nospaces) db.createOrUpdateSetting(qscript.summaryRunId, pipelineId, None, sampleId, libId, ConfigUtils.mapToJson(tag.summarySettings).nospaces)
case q: MultiSampleQScript => case q: MultiSampleQScript =>
// Global level // Global level
for ((key, file) <- qscript.summaryFiles.par) for ((key, file) <- qscript.summaryFiles.par)
Await.result(createFile(db, q.summaryRunId, pipelineId, None, None, None, key, file, outputDir), Duration.Inf) Await.result(WriteSummary.createFile(db, q.summaryRunId, pipelineId, None, None, None, key, file, outputDir), Duration.Inf)
db.createOrUpdateSetting(qscript.summaryRunId, pipelineId, None, None, None, ConfigUtils.mapToJson(q.summarySettings).nospaces) db.createOrUpdateSetting(qscript.summaryRunId, pipelineId, None, None, None, ConfigUtils.mapToJson(q.summarySettings).nospaces)
for ((sampleName, sample) <- q.samples) { for ((sampleName, sample) <- q.samples) {
// Sample level // Sample level
val sampleId = Await.result(db.getSampleId(qscript.summaryRunId, sampleName), Duration.Inf).getOrElse(throw new IllegalStateException("Sample should already exist in database")) val sampleId = Await.result(db.getSampleId(qscript.summaryRunId, sampleName), Duration.Inf).getOrElse(throw new IllegalStateException("Sample should already exist in database"))
for ((key, file) <- sample.summaryFiles.par) for ((key, file) <- sample.summaryFiles.par)
Await.result(createFile(db, q.summaryRunId, pipelineId, None, Some(sampleId), None, key, file, outputDir), Duration.Inf) Await.result(WriteSummary.createFile(db, q.summaryRunId, pipelineId, None, Some(sampleId), None, key, file, outputDir), Duration.Inf)
db.createOrUpdateSetting(qscript.summaryRunId, pipelineId, None, Some(sampleId), None, ConfigUtils.mapToJson(sample.summarySettings).nospaces) db.createOrUpdateSetting(qscript.summaryRunId, pipelineId, None, Some(sampleId), None, ConfigUtils.mapToJson(sample.summarySettings).nospaces)
for ((libName, lib) <- sample.libraries) { for ((libName, lib) <- sample.libraries) {
// Library level // Library level
val libId = Await.result(db.getLibraryId(qscript.summaryRunId, sampleId, libName), Duration.Inf).getOrElse(throw new IllegalStateException("Library should already exist in database")) val libId = Await.result(db.getLibraryId(qscript.summaryRunId, sampleId, libName), Duration.Inf).getOrElse(throw new IllegalStateException("Library should already exist in database"))
for ((key, file) <- lib.summaryFiles.par) for ((key, file) <- lib.summaryFiles.par)
Await.result(createFile(db, q.summaryRunId, pipelineId, None, Some(sampleId), Some(libId), key, file, outputDir), Duration.Inf) Await.result(WriteSummary.createFile(db, q.summaryRunId, pipelineId, None, Some(sampleId), Some(libId), key, file, outputDir), Duration.Inf)
db.createOrUpdateSetting(qscript.summaryRunId, pipelineId, None, Some(sampleId), Some(libId), ConfigUtils.mapToJson(lib.summarySettings).nospaces) db.createOrUpdateSetting(qscript.summaryRunId, pipelineId, None, Some(sampleId), Some(libId), ConfigUtils.mapToJson(lib.summarySettings).nospaces)
} }
} }
case q => case q =>
for ((key, file) <- q.summaryFiles.par) for ((key, file) <- q.summaryFiles.par)
Await.result(createFile(db, qscript.summaryRunId, pipelineId, None, None, None, key, file, outputDir), Duration.Inf) Await.result(WriteSummary.createFile(db, qscript.summaryRunId, pipelineId, None, None, None, key, file, outputDir), Duration.Inf)
db.createOrUpdateSetting(qscript.summaryRunId, pipelineId, None, None, None, ConfigUtils.mapToJson(q.summarySettings).nospaces) db.createOrUpdateSetting(qscript.summaryRunId, pipelineId, None, None, None, ConfigUtils.mapToJson(q.summarySettings).nospaces)
} }
...@@ -222,4 +214,14 @@ object WriteSummary { ...@@ -222,4 +214,14 @@ object WriteSummary {
def parseChecksum(checksumFile: File): String = { def parseChecksum(checksumFile: File): String = {
Source.fromFile(checksumFile).getLines().toList.head.split(" ")(0) Source.fromFile(checksumFile).getLines().toList.head.split(" ")(0)
} }
def createFile(db: SummaryDb, runId: Int, pipelineId: Int, moduleId: Option[Int], sampleId: Option[Int], libId: Option[Int], key: String, file: File, outputDir: File) = {
val path = if (file.getAbsolutePath.startsWith(outputDir.getAbsolutePath + File.separator)) {
"." + file.getAbsolutePath.stripPrefix(s"${outputDir.getAbsolutePath}")
} else file.getAbsolutePath
val md5 = SummaryQScript.md5sumCache.get(file).map(WriteSummary.parseChecksum(_))
val size = if (file.exists()) file.length() else 0L
val link = if (file.exists()) java.nio.file.Files.isSymbolicLink(file.toPath) else false
db.createOrUpdateFile(runId, pipelineId, moduleId, sampleId, libId, key, path, md5.getOrElse("checksum_not_found"), link, size)
}
} }
...@@ -39,6 +39,25 @@ import scala.util.matching.Regex ...@@ -39,6 +39,25 @@ import scala.util.matching.Regex
*/ */
class WriteSummaryTest extends TestNGSuite with Matchers { class WriteSummaryTest extends TestNGSuite with Matchers {
@Test
def testCreateFile: Unit = {
val dbFile = File.createTempFile("summary.", ".db")
dbFile.deleteOnExit()
val db = SummaryDb.openSqliteSummary(dbFile)
db.createTables()
val outputDir = new File("/tmp")
Await.ready(WriteSummary.createFile(db, 0, 0, Some(0), Some(0), Some(0), "test", new File(outputDir, "test.tsv"), outputDir), Duration.Inf)
val file = Await.result(db.getFile(0,0,0,0,0, "test"), Duration.Inf)
file.map(_.path) shouldBe Some("./test.tsv")
Await.ready(WriteSummary.createFile(db, 0, 0, Some(0), Some(0), Some(0), "test", new File("/tmp2/test.tsv"), outputDir), Duration.Inf)
val file2 = Await.result(db.getFile(0,0,0,0,0, "test"), Duration.Inf)
file2.map(_.path) shouldBe Some("/tmp2/test.tsv")
db.close()
}
@Test @Test
def testWrongRoot(): Unit = { def testWrongRoot(): Unit = {
intercept[IllegalArgumentException] { intercept[IllegalArgumentException] {
......
Supports Markdown
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