Commit abe8e6ec authored by Wai Yi Leung's avatar Wai Yi Leung
Browse files

Merge branch 'feature-tagging' into 'develop'

Feature tagging

Fixes #254 

See merge request !334
parents b34fc129 6e1a2700
......@@ -47,16 +47,24 @@ For BAM files as input one should use a config like this:
``` yaml
samples:
Sample_ID_1:
tags:
gender: male
father: sampleNameFather
mother: sampleNameMother
libraries:
Lib_ID_1:
tags:
key: value
bam: MyFirst.bam
Lib_ID_2:
bam: MySecond.bam
```
Note that there is a tool called [SamplesTsvToJson](../tools/SamplesTsvToJson.md) this enables a user to get the sample config without any chance of creating a wrongly formatted JSON file.
#### Tags
In the `tags` key inside a sample or library users can supply tags that belong to samples/libraries. These tags will we automatically parsed inside the summary of a pipeline.
### The settings config
The settings config enables a user to alter the settings for almost all settings available in the tools used for a given pipeline.
......
......@@ -21,12 +21,18 @@ Usage: SamplesTsvToJson [options]
Print version
-i <file> | --inputFiles <file>
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
-t <file> | --tagFiles <file>
-o <file> | --outputFile <file>
~~~
The tool is designed in such a way that a user can provide a TAB seperated file (TSV) with sample specific properties and even those will be parsed by the tool.
For example: a user wants to have certain properties e.g. which treatment a sample got than the user should provide a extra columns called treatment and then the
JSON file is parsed with those properties inside it as well. The order of columns does not matter.
The tag files works the same only the value are prefixed in the key `tags`.
#### Example
~~~ json
......
......@@ -112,11 +112,15 @@ class WriteSummary(val root: Configurable) extends InProcessFunction with Config
Map("samples" -> q.samples.map {
case (sampleName, sample) =>
sampleName -> Map(
qscript.summaryName -> Map("settings" -> sample.summarySettings),
qscript.summaryName -> Map(
"settings" -> sample.summarySettings,
"tags" -> sample.sampleTags),
"libraries" -> sample.libraries.map {
case (libName, lib) =>
libName -> Map(
qscript.summaryName -> Map("settings" -> lib.summarySettings)
qscript.summaryName -> Map(
"settings" -> lib.summarySettings,
"tags" -> lib.libTags)
)
}
)
......
......@@ -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