diff --git a/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/BiopetExecutable.scala b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/BiopetExecutable.scala index c8c1751b812bfdd7e256b364d70ba272aa0c9280..c650581321a3db119ed524f9d71c596b90fb2151 100644 --- a/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/BiopetExecutable.scala +++ b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/BiopetExecutable.scala @@ -37,7 +37,8 @@ object BiopetExecutable extends Logging { nl.lumc.sasc.biopet.tools.SageCountFastq, nl.lumc.sasc.biopet.tools.SageCreateLibrary, nl.lumc.sasc.biopet.tools.SageCreateTagCounts, - nl.lumc.sasc.biopet.tools.MergeAlleles) + nl.lumc.sasc.biopet.tools.MergeAlleles, + nl.lumc.sasc.biopet.tools.SamplesTsvToJson) ) /** diff --git a/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/config/Config.scala b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/config/Config.scala index e373d7b5332651cb45006643a60d61ef1a24a438..f6f8e0cf7ada60b11d50e8671b804350b54c5ad5 100644 --- a/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/config/Config.scala +++ b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/config/Config.scala @@ -199,4 +199,28 @@ object Config extends Logging { return None } } + + def mapToJson(map: Map[String, Any]): Json = { + map.foldLeft(jEmptyObject)((acc, kv) => (kv._1 := { + kv._2 match { + case m: Map[_, _] => mapToJson(m.map(m => m._1.toString -> anyToJson(m._2))) + case _ => anyToJson(kv._2) + } + }) ->: acc) + } + + def anyToJson(any: Any): Json = { + any match { + case j: Json => j + case m: Map[_, _] => mapToJson(m.map(m => m._1.toString -> anyToJson(m._2))) + case l: List[_] => Json.array(l.map(anyToJson(_)): _*) + case n: Int => Json.jNumberOrString(n) + case n: Double => Json.jNumberOrString(n) + case n: Long => Json.jNumberOrString(n) + case n: Short => Json.jNumberOrString(n) + case n: Float => Json.jNumberOrString(n) + case n: Byte => Json.jNumberOrString(n) + case _ => jString(any.toString) + } + } } \ No newline at end of file diff --git a/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/SamplesTsvToJson.scala b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/SamplesTsvToJson.scala new file mode 100644 index 0000000000000000000000000000000000000000..da69c171a3e788b747c002917936fa2053a58c62 --- /dev/null +++ b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/SamplesTsvToJson.scala @@ -0,0 +1,46 @@ +package nl.lumc.sasc.biopet.tools + +import java.io.File +import nl.lumc.sasc.biopet.core.ToolCommand +import scala.io.Source +import nl.lumc.sasc.biopet.core.config.Config + +object SamplesTsvToJson extends ToolCommand { + case class Args(inputFiles: List[File] = Nil) extends AbstractArgs + + class OptParser extends AbstractOptParser { + opt[File]('i', "inputFiles") required () 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") + } + def main(args: Array[String]): Unit = { + val argsParser = new OptParser + val commandArgs: Args = argsParser.parse(args, Args()) getOrElse sys.exit(1) + + val fileMaps = for (inputFile <- commandArgs.inputFiles) yield { + val reader = Source.fromFile(inputFile) + val lines = reader.getLines.toList + val header = lines.head.split("\t") + val sampleColumn = header.indexOf("sample") + val libraryColumn = header.indexOf("library") + if (sampleColumn == -1) throw new IllegalStateException("sample column does not exist in: " + inputFile) + + val librariesValues: List[Map[String, Any]] = for (tsvLine <- lines.tail) yield { + val values = tsvLine.split("\t") + val sample = values(sampleColumn) + val library = if (libraryColumn != -1) values(libraryColumn) else null + val valuesMap = (for (t <- 0 until values.size if t != sampleColumn if t != libraryColumn) yield (header(t) -> values(t))).toMap + val map: Map[String, Any] = if (library != null) { + Map("samples" -> Map(sample -> Map("libraries" -> Map(library -> valuesMap)))) + } else { + Map("samples" -> Map(sample -> valuesMap)) + } + map + } + librariesValues.foldLeft(Map[String, Any]())((acc, kv) => Config.mergeMaps(acc, kv)) + } + val map = fileMaps.foldLeft(Map[String, Any]())((acc, kv) => Config.mergeMaps(acc, kv)) + val json = Config.mapToJson(map) + println(json.spaces2) + } +}