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

Added tags to SampleTsvToJson

parent 4ffd661d
......@@ -27,12 +27,17 @@ import scala.io.Source
* This tool can convert a tsv to a json file
*/
object SamplesTsvToJson extends ToolCommand {
case class Args(inputFiles: List[File] = Nil, outputFile: Option[File] = None) extends AbstractArgs
case class Args(inputFiles: List[File] = Nil,
tagFiles: List[File] = Nil,
outputFile: Option[File] = None) extends AbstractArgs
class OptParser extends AbstractOptParser {
opt[File]('i', "inputFiles") required () unbounded () valueName "<file>" action { (x, c) =>
opt[File]('i', "inputFiles") unbounded () valueName "<file>" action { (x, c) =>
c.copy(inputFiles = x :: c.inputFiles)
} text "Input must be a tsv file, first line is seen as header and must at least have a 'sample' column, 'library' column is optional, multiple files allowed"
opt[File]('t', "tagFiles") unbounded () valueName "<file>" action { (x, c) =>
c.copy(tagFiles = x :: c.tagFiles)
}
opt[File]('o', "outputFile") unbounded () valueName "<file>" action { (x, c) =>
c.copy(outputFile = Some(x))
}
......@@ -41,10 +46,10 @@ object SamplesTsvToJson extends ToolCommand {
/** Executes SamplesTsvToJson */
def main(args: Array[String]): Unit = {
val argsParser = new OptParser
val commandArgs: Args = argsParser.parse(args, Args()) getOrElse sys.exit(1)
val cmdArgs: Args = argsParser.parse(args, Args()) getOrElse sys.exit(1)
val jsonString = stringFromInputs(commandArgs.inputFiles)
commandArgs.outputFile match {
val jsonString = stringFromInputs(cmdArgs.inputFiles, cmdArgs.tagFiles)
cmdArgs.outputFile match {
case Some(file) => {
val writer = new PrintWriter(file)
writer.println(jsonString)
......@@ -54,7 +59,7 @@ object SamplesTsvToJson extends ToolCommand {
}
}
def mapFromFile(inputFile: File): Map[String, Any] = {
def mapFromFile(inputFile: File, tags: Boolean = false): Map[String, Any] = {
val reader = Source.fromFile(inputFile)
val lines = reader.getLines().toList.filter(!_.isEmpty)
val header = lines.head.split("\t")
......@@ -81,15 +86,20 @@ object SamplesTsvToJson extends ToolCommand {
t <- 0 until values.size if !values(t).isEmpty && t != sampleColumn && t != libraryColumn
) yield header(t) -> values(t)).toMap
library match {
case Some(lib) => Map("samples" -> Map(sample -> Map("libraries" -> Map(lib -> valuesMap))))
case _ => Map("samples" -> Map(sample -> valuesMap))
case Some(lib) if tags => Map("samples" -> Map(sample -> Map("libraries" -> Map(lib -> Map("tags" -> valuesMap)))))
case Some(lib) => Map("samples" -> Map(sample -> Map("libraries" -> Map(lib -> valuesMap))))
case _ if tags => Map("samples" -> Map(sample -> Map("tags" -> valuesMap)))
case _ => Map("samples" -> Map(sample -> valuesMap))
}
}
librariesValues.foldLeft(Map[String, Any]())((acc, kv) => mergeMaps(acc, kv))
}
def stringFromInputs(inputs: List[File]): String = {
val map = inputs.map(f => mapFromFile(f)).foldLeft(Map[String, Any]())((acc, kv) => mergeMaps(acc, kv))
mapToJson(map).spaces2
def stringFromInputs(inputs: List[File], tagsInputs: List[File]): String = {
val map = inputs.map(f => mapFromFile(f))
.foldLeft(Map[String, Any]())((acc, kv) => mergeMaps(acc, kv))
val tags = tagsInputs.map(f => mapFromFile(f, tags = true))
.foldLeft(Map[String, Any]())((acc, kv) => mergeMaps(acc, kv))
mapToJson(mergeMaps(map, tags)).spaces2
}
}
......@@ -72,7 +72,7 @@ class SamplesTsvToJsonTest extends TestNGSuite with MockitoSugar with Matchers {
@Test
def testJson = {
val tsv = new File(resourcePath("/sample.tsv"))
val json = stringFromInputs(List(tsv))
val json = stringFromInputs(List(tsv), Nil)
json should equal(
"""|{
......
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