Skip to content
Snippets Groups Projects
Commit d1bd5949 authored by Sander Bollen's avatar Sander Bollen
Browse files

Refactoring SummaryToTsv to make it more testable

Removing extraneous output flag (wasn't used)
Better help messages
And mode validation
parent 81b7da44
No related branches found
No related tags found
Loading
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package nl.lumc.sasc.biopet.tools package nl.lumc.sasc.biopet.tools
import java.io.File import java.io.File
import java.nio.file.Paths
import nl.lumc.sasc.biopet.core.ToolCommand import nl.lumc.sasc.biopet.core.ToolCommand
import nl.lumc.sasc.biopet.core.summary.Summary import nl.lumc.sasc.biopet.core.summary.Summary
...@@ -35,15 +36,24 @@ object SummaryToTsv extends ToolCommand { ...@@ -35,15 +36,24 @@ object SummaryToTsv extends ToolCommand {
opt[File]('s', "summary") required () unbounded () maxOccurs 1 valueName "<file>" action { (x, c) => opt[File]('s', "summary") required () unbounded () maxOccurs 1 valueName "<file>" action { (x, c) =>
c.copy(summary = x) c.copy(summary = x)
} }
opt[File]('o', "output") maxOccurs 1 unbounded () valueName "<file>" action { (x, c) => opt[String]('p', "path") required () unbounded () valueName "<string>" action { (x, c) =>
c.copy(outputFile = Some(x))
}
opt[String]('p', "path") required () unbounded () valueName "<value>" action { (x, c) =>
c.copy(values = c.values ::: x :: Nil) c.copy(values = c.values ::: x :: Nil)
} } text
"""
|String that determines the values extracted from the summary. Should be of the format:
|<header_name>=<namespace>:<lower_namespace>:<even_lower_namespace>...
""".stripMargin
opt[String]('m', "mode") maxOccurs 1 unbounded () valueName "<root|sample|lib>" action { (x, c) => opt[String]('m', "mode") maxOccurs 1 unbounded () valueName "<root|sample|lib>" action { (x, c) =>
c.copy(mode = x) c.copy(mode = x)
} } validate {
x => if (Set("root","sample","lib").contains(x)) success else failure("Unsupported mode")
} text
"""
|Determines on what level to aggregate data.
|root: at the root level
|sample: at the sample level
|lib: at the library level
""".stripMargin
} }
...@@ -56,14 +66,14 @@ object SummaryToTsv extends ToolCommand { ...@@ -56,14 +66,14 @@ object SummaryToTsv extends ToolCommand {
val paths = cmdArgs.values.map(x => { val paths = cmdArgs.values.map(x => {
val split = x.split("=", 2) val split = x.split("=", 2)
split(0) -> split(1).split(":") split(0) -> split(1).split(":")
}) }).toMap
val values = fetchValues(summary, paths.toMap, sample = cmdArgs.mode == "sample", lib = cmdArgs.mode == "lib") val values = fetchValues(summary, paths, sample = cmdArgs.mode == "sample", lib = cmdArgs.mode == "lib")
println(paths.map(_._1).mkString("\t", "\t", "")) println(createHeader(paths))
for (lineId <- values.head._2.keys) { for (lineId <- values.head._2.keys) {
println(paths.map(x => values(x._1)(lineId).getOrElse("")).mkString(lineId + "\t", "\t", "")) println(createLine(paths, values, lineId))
} }
} }
...@@ -71,9 +81,19 @@ object SummaryToTsv extends ToolCommand { ...@@ -71,9 +81,19 @@ object SummaryToTsv extends ToolCommand {
sample: Boolean = false, sample: Boolean = false,
lib: Boolean = false) = { lib: Boolean = false) = {
for ((name, path) <- paths) yield name -> { for ((name, path) <- paths) yield name -> {
if (lib) summary.getLibraryValues(path: _*).map(a => (a._1._1 + "-" + a._1._2) -> a._2) if (lib) {
else if (sample) summary.getSampleValues(path: _*) summary.getLibraryValues(path: _*).map(a => (a._1._1 + "-" + a._1._2) -> a._2)
} else if (sample) summary.getSampleValues(path: _*)
else Map("value" -> summary.getValue(path: _*)) else Map("value" -> summary.getValue(path: _*))
} }
} }
def createHeader(paths: Map[String, Array[String]]): String = {
paths.map(_._1).mkString("\t", "\t", "")
}
def createLine(paths: Map[String, Array[String]],
values: Map[String, Map[String, Option[Any]]], lineId: String): String = {
paths.map(x => values(x._1)(lineId).getOrElse("")).mkString(lineId + "\t", "\t", "")
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment