Commit ad393299 authored by Peter van 't Hof's avatar Peter van 't Hof

Merge branch 'fix-BIOPET-419' into 'develop'

Fix biopet 419



See merge request !481
parents 4561f688 da81e979
......@@ -40,7 +40,7 @@ object BiopetToolsExecutable extends BiopetExecutable {
nl.lumc.sasc.biopet.tools.MpileupToVcf,
nl.lumc.sasc.biopet.tools.PrefixFastq,
nl.lumc.sasc.biopet.tools.SageCountFastq,
nl.lumc.sasc.biopet.tools.SamplesTsvToJson,
nl.lumc.sasc.biopet.tools.SamplesTsvToConfig,
nl.lumc.sasc.biopet.tools.SeqStat,
nl.lumc.sasc.biopet.tools.SquishBed,
nl.lumc.sasc.biopet.tools.SummaryToTsv,
......
......@@ -14,18 +14,18 @@
*/
package nl.lumc.sasc.biopet.tools
import java.io.{ PrintWriter, File }
import java.io.{ File, PrintWriter }
import nl.lumc.sasc.biopet.utils.ConfigUtils._
import nl.lumc.sasc.biopet.utils.ToolCommand
import scala.collection.mutable
import nl.lumc.sasc.biopet.utils.{ ConfigUtils, ToolCommand }
import scala.collection.mutable
import scala.io.Source
/**
* This tool can convert a tsv to a json file
*/
object SamplesTsvToJson extends ToolCommand {
object SamplesTsvToConfig extends ToolCommand {
case class Args(inputFiles: List[File] = Nil,
tagFiles: List[File] = Nil,
outputFile: Option[File] = None) extends AbstractArgs
......@@ -39,7 +39,10 @@ object SamplesTsvToJson extends ToolCommand {
}
opt[File]('o', "outputFile") unbounded () valueName "<file>" action { (x, c) =>
c.copy(outputFile = Some(x))
}
} text """
|When the extension is .yml or .yaml the output is in yaml format, otherwise it is in json.
|When no extension is given the output goes to stdout as yaml.
""".stripMargin
}
/** Executes SamplesTsvToJson */
......@@ -47,14 +50,16 @@ object SamplesTsvToJson extends ToolCommand {
val argsParser = new OptParser
val cmdArgs: Args = argsParser.parse(args, Args()) getOrElse (throw new IllegalArgumentException)
val jsonString = stringFromInputs(cmdArgs.inputFiles, cmdArgs.tagFiles)
val configMap = stringFromInputs(cmdArgs.inputFiles, cmdArgs.tagFiles)
cmdArgs.outputFile match {
case Some(file) if file.getName.endsWith(".yml") || file.getName.endsWith(".yaml") =>
ConfigUtils.mapToYamlFile(configMap, file)
case Some(file) => {
val writer = new PrintWriter(file)
writer.println(jsonString)
writer.println(ConfigUtils.mapToJson(configMap).spaces2)
writer.close()
}
case _ => println(jsonString)
case _ => println(ConfigUtils.mapToYaml(configMap))
}
}
......@@ -94,11 +99,11 @@ object SamplesTsvToJson extends ToolCommand {
librariesValues.foldLeft(Map[String, Any]())((acc, kv) => mergeMaps(acc, kv))
}
def stringFromInputs(inputs: List[File], tagsInputs: List[File]): String = {
def stringFromInputs(inputs: List[File], tagsInputs: List[File]): Map[String, Any] = {
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
mergeMaps(map, tags)
}
}
......@@ -17,6 +17,7 @@ package nl.lumc.sasc.biopet.tools
import java.io.File
import java.nio.file.Paths
import nl.lumc.sasc.biopet.utils.ConfigUtils
import org.scalatest.Matchers
import org.scalatest.mock.MockitoSugar
import org.scalatest.testng.TestNGSuite
......@@ -25,8 +26,8 @@ import org.testng.annotations.Test
/**
* Created by ahbbollen on 28-8-15.
*/
class SamplesTsvToJsonTest extends TestNGSuite with MockitoSugar with Matchers {
import SamplesTsvToJson._
class SamplesTsvToConfigTest extends TestNGSuite with MockitoSugar with Matchers {
import SamplesTsvToConfig._
private def resourcePath(p: String): String = {
Paths.get(getClass.getResource(p).toURI).toString
}
......@@ -73,7 +74,7 @@ class SamplesTsvToJsonTest extends TestNGSuite with MockitoSugar with Matchers {
val tsv = new File(resourcePath("/sample.tsv"))
val json = stringFromInputs(List(tsv), Nil)
json should equal(
ConfigUtils.mapToJson(json).spaces2 should equal(
"""|{
| "samples" : {
| "Sample_ID_1" : {
......
......@@ -17,7 +17,7 @@ package nl.lumc.sasc.biopet.tools
import java.io.File
import java.nio.file.Paths
import nl.lumc.sasc.biopet.tools.SamplesTsvToJson._
import nl.lumc.sasc.biopet.tools.SamplesTsvToConfig._
import org.scalatest.Matchers
import org.scalatest.mock.MockitoSugar
import org.scalatest.testng.TestNGSuite
......
......@@ -76,7 +76,7 @@
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.15</version>
<version>1.17</version>
</dependency>
<dependency>
<groupId>io.argonaut</groupId>
......
......@@ -14,7 +14,7 @@
*/
package nl.lumc.sasc.biopet.utils
import java.io.File
import java.io.{File, PrintWriter}
import java.util
import argonaut.Argonaut._
......@@ -154,6 +154,16 @@ object ConfigUtils extends Logging {
else ConfigUtils.any2map(a)
}
lazy val yaml = new Yaml()
def mapToYaml(map: Map[String, Any]) = yaml.dump(yaml.load(ConfigUtils.mapToJson(map).nospaces))
def mapToYamlFile(map: Map[String, Any], outputFile: File) = {
val writer = new PrintWriter(outputFile)
writer.println(mapToYaml(map))
writer.close()
}
/** Convert json to native scala map/values */
def jsonToMap(json: Json): Map[String, Any] = {
var output: Map[String, Any] = Map()
......
# SamplesTsvToJson
# SamplesTsvToConfig
This tool enables a user to create a full sample sheet in JSON format, suitable for all our Queue pipelines, from TSV file(s).
This tool enables a user to create a full sample sheet in JSON format or YAML format, suitable for all our Queue pipelines, from TSV file(s).
The tool can be called as follows:
~~~ bash
biopet tool SamplesTsvToJson
biopet tool SamplesTsvToConfig
~~~
To open the help:
......@@ -24,40 +24,15 @@ Usage: SamplesTsvToJson [options]
-t <file> | --tagFiles <file>
-o <file> | --outputFile <file>
When extension is .yml or .yaml output is in yaml format, otherwise in json. When not given output goes to stdout as yaml.
~~~
A user provides a TAB separated file (TSV) with sample specific properties which are parsed into JSON format by the tool.
For example, a user wants to add certain properties to the description of a sample, such as the treatment a sample received. Then a TSV file with an extra column called treatment is provided.
The resulting JSON file will have the 'treatment' property in it as well. The order of the columns is not relevant to the end result
The resulting file will have the 'treatment' property in it as well. The order of the columns is not relevant to the end result
The tag files works the same only the value is prefixed in the key `tags`.
#### Example
~~~ json
{
"samples" : {
"Sample_ID_1" : {
"treatment" : "heatshock",
"libraries" : {
"Lib_ID_1" : {
"bam" : "MyFirst.bam"
}
}
},
"Sample_ID_2" : {
"treatment" : "heatshock",
"libraries" : {
"Lib_ID_2" : {
"bam" : "MySecond.bam"
}
}
}
}
}
~~~
#### Sample definition
To get the above example out of the tool one should provide 2 TSV files as follows:
......@@ -83,3 +58,45 @@ Basically anything you want to pass to your pipeline is possible.
| Sample_ID_1 | heatshock |
| Sample_ID_2 | heatshock |
#### Example
###### Yaml
~~~ yaml
samples:
Sample_ID_1:
treatment: heatshock
libraries:
Lib_ID_1:
bam: MyFirst.bam
Sample_ID_2:
treatment: heatshock
libraries:
Lib_ID_2:
bam: MySecond.bam
~~~
###### Json
~~~ json
{
"samples" : {
"Sample_ID_1" : {
"treatment" : "heatshock",
"libraries" : {
"Lib_ID_1" : {
"bam" : "MyFirst.bam"
}
}
},
"Sample_ID_2" : {
"treatment" : "heatshock",
"libraries" : {
"Lib_ID_2" : {
"bam" : "MySecond.bam"
}
}
}
}
}
~~~
......@@ -21,7 +21,7 @@ pages:
- Toucan (Annotation): 'pipelines/toucan.md'
- Tools:
- AnnotateVcfWithBed: 'tools/AnnotateVcfWithBed.md'
- SamplesTsvToJson: 'tools/SamplesTsvToJson.md'
- SamplesTsvToConfig: 'tools/SamplesTsvToConfig.md'
- BedToInterval: 'tools/bedtointerval.md'
- BastyGenerateFasta: 'tools/BastyGenerateFasta.md'
- BedToInterval: 'tools/bedtointerval.md'
......
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