diff --git a/public/biopet-framework/pom.xml b/public/biopet-framework/pom.xml index e2bf9afa18e0b58b7896ea4b6843026169f5d41e..905ae9fda5733ccb769a159669c6bfbfaa0a19b1 100644 --- a/public/biopet-framework/pom.xml +++ b/public/biopet-framework/pom.xml @@ -110,5 +110,10 @@ <artifactId>scopt_2.10</artifactId> <version>3.3.0</version> </dependency> + <dependency> + <groupId>org.yaml</groupId> + <artifactId>snakeyaml</artifactId> + <version>1.15</version> + </dependency> </dependencies> </project> diff --git a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/ConfigUtils.scala b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/ConfigUtils.scala index eaea3ad41a20a8cf82af63d973a1a06a10ff42e2..5f03703f1928caf942198d8fb1862aef5d693708 100644 --- a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/ConfigUtils.scala +++ b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/ConfigUtils.scala @@ -20,7 +20,10 @@ import nl.lumc.sasc.biopet.core.BiopetQScript import nl.lumc.sasc.biopet.core.Logging import nl.lumc.sasc.biopet.core.config.ConfigValue import argonaut._, Argonaut._ +import org.yaml.snakeyaml.Yaml +import scala.collection.mutable import scalaz._, Scalaz._ +import scala.collection.JavaConversions._ /** * This object contains general function for the config @@ -78,8 +81,9 @@ object ConfigUtils extends Logging { val value = map.get(path.head) if (path.tail == Nil || value == None) value else value.get match { - case map: Map[_, _] => getValueFromPath(map.asInstanceOf[Map[String, Any]], path.tail) - case _ => None + case map: Map[_, _] => getValueFromPath(map.asInstanceOf[Map[String, Any]], path.tail) + case map: java.util.LinkedHashMap[_, _] => getValueFromPath(map.toMap.asInstanceOf[Map[String, Any]], path.tail) + case _ => None } } @@ -105,9 +109,24 @@ object ConfigUtils extends Logging { * @return Config map */ def fileToConfigMap(configFile: File): Map[String, Any] = { - val configJson = jsonToMap(fileToJson(configFile)) - logger.debug("Contain: " + configJson) - return configJson + + val configMap = { + if (configFile.getName.endsWith(".yaml")) yamlToMap(configFile) + else jsonToMap(fileToJson(configFile)) + } + logger.debug("Contain: " + configMap) + return configMap + } + + /** + * Convert a yaml file to map[String, Any] + * @param file Input file + * @return config map + */ + def yamlToMap(file: File): Map[String, Any] = { + val yaml = new Yaml() + val a = yaml.load(scala.io.Source.fromFile(file).reader()) + ConfigUtils.any2map(a) } /** @@ -331,11 +350,22 @@ object ConfigUtils extends Logging { def any2map(any: Any): Map[String, Any] = { if (any == null) return null any match { - case m: Map[_, _] => m.map(x => x._1.toString -> x._2) - case _ => throw new IllegalStateException("Value '" + any + "' is not an Map") + case m: Map[_, _] => m.map(x => x._1.toString -> x._2) + case m: java.util.LinkedHashMap[_, _] => nestedJavaHashMaptoScalaMap(m) + case _ => throw new IllegalStateException("Value '" + any + "' is not an Map") } } + /** Convert nested java hash map to scala hash map */ + def nestedJavaHashMaptoScalaMap(input: java.util.LinkedHashMap[_, _]): Map[String, Any] = { + input.map(value => { + value._2 match { + case m: java.util.LinkedHashMap[_, _] => value._1.toString -> nestedJavaHashMaptoScalaMap(m) + case _ => value._1.toString -> value._2 + } + }).toMap + } + /** * Trait for implicit conversions for ConfigValue to native scala values */ diff --git a/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/core/config/ConfigTest.scala b/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/core/config/ConfigTest.scala index 125579d13b78ee78ceab5547e986026e624be086..0878ae43ef0f8dd624c0210d4b0c8313f8f75cf0 100644 --- a/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/core/config/ConfigTest.scala +++ b/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/core/config/ConfigTest.scala @@ -141,7 +141,7 @@ object ConfigTest { ) ) - val file = ConfigUtilsTest.writeTemp(ConfigUtils.mapToJson(map).spaces2) + val file = ConfigUtilsTest.writeTemp(ConfigUtils.mapToJson(map).spaces2, "json") val config = new Config config.loadConfigFile(file) diff --git a/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/utils/ConfigUtilsTest.scala b/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/utils/ConfigUtilsTest.scala index b6c21092c9dd1acb375bce4964b230e8aca11806..e945da32c3f2679de81193f60ce37ad1c7af4783 100644 --- a/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/utils/ConfigUtilsTest.scala +++ b/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/utils/ConfigUtilsTest.scala @@ -205,8 +205,8 @@ class ConfigUtilsTest extends TestNGSuite with Matchers { } } object ConfigUtilsTest { - def writeTemp(text: String): File = { - val file = File.createTempFile("TestConfigUtils.", ".json") + def writeTemp(text: String, extension: String): File = { + val file = File.createTempFile("TestConfigUtils.", extension) val w = new PrintWriter(file) w.write(text) w.close() @@ -229,7 +229,7 @@ object ConfigUtilsTest { |} """.stripMargin - val file1 = writeTemp(jsonText1) + val file1 = writeTemp(jsonText1, ".json") val json1 = { ("int" := 1337) ->: @@ -262,7 +262,7 @@ object ConfigUtilsTest { |} """.stripMargin - val file2 = writeTemp(jsonText2) + val file2 = writeTemp(jsonText2, ".yaml") val json2 = { ("int" := 7331) ->: @@ -283,5 +283,5 @@ object ConfigUtilsTest { |} """.stripMargin - val corruptFile = writeTemp(corruptJson) + val corruptFile = writeTemp(corruptJson, ".json") } \ No newline at end of file