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

Working version of ParseSamples

parent a6ed777d
......@@ -3,72 +3,44 @@ package nl.lumc.sasc.biopet.tools
import java.io.File
import nl.lumc.sasc.biopet.core.ToolCommand
import scala.io.Source
import java.io.PrintWriter
import argonaut._, Argonaut._
import nl.lumc.sasc.biopet.core.config.Config
object ParseSamples extends ToolCommand {
case class Args(librariesTsv: File = null) extends AbstractArgs
case class Args(inputFiles: List[File] = Nil) extends AbstractArgs
class OptParser extends AbstractOptParser {
opt[File]('i', "librariesTsv") required () maxOccurs (1) valueName ("<file>") action { (x, c) =>
c.copy(librariesTsv = x)
} text ("output file, default to stdout")
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)
// Libraries tsv
val reader = Source.fromFile(commandArgs.librariesTsv)
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: " + commandArgs.librariesTsv)
if (libraryColumn == -1) throw new IllegalStateException("library column does not exist in: " + commandArgs.librariesTsv)
val librariesValues = (for (tsvLine <- lines.tail) yield {
val values = tsvLine.split("\t")
val sample = values(sampleColumn)
val library = values(libraryColumn)
val libraryValues = (for (t <- 0 until values.size if t != sampleColumn if t != libraryColumn) yield (header(t) -> values(t))).toMap
((sample, library) -> libraryValues)
}).toMap
for (((sample, library), values) <- librariesValues) {
println("sample=" + sample + ", library=" + library + " = " + values)
// format: OFF
val summary =
("samples" := (sample :=
("libraries" := (library := (
jEmptyObject)) ->: jEmptyObject)->: jEmptyObject) ->: jEmptyObject) ->: jEmptyObject
// format: ON
val summeryText = summary.spaces2
println(summeryText)
//logger.debug("\n" + summeryText)
//val writer = new PrintWriter(out)
//writer.write(summeryText)
//writer.close()
//logger.debug("Stop")
}
def getSampleJson(sample: String): Json = {
val libraries = (for (((s, l), values) <- librariesValues if s == sample) yield getLibraryJson(sample, l)).toList
(sample := libraries) ->: jEmptyObject
}
def getLibraryJson(sample: String, library: String): Json = {
(library := "bla") ->: jEmptyObject
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 samples = (for (((s, l), values) <- librariesValues) yield s).toSet
val samplesJson = (for (s <- samples) yield getSampleJson(s))
val summary = ("samples" := samplesJson) ->: jEmptyObject
println(summary.spaces2)
val map = fileMaps.foldLeft(Map[String, Any]())((acc, kv) => Config.mergeMaps(acc, kv))
val json = Config.mapToJson(map)
println(json.spaces2)
}
}
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