Commit 70c19e12 authored by Peter van 't Hof's avatar Peter van 't Hof

Moved ImplictConversions to utils

parent f68e4733
......@@ -7,8 +7,9 @@ import org.broadinstitute.gatk.utils.commandline.Argument
import org.broadinstitute.gatk.queue.QSettings
import org.broadinstitute.gatk.queue.function.QFunction
import org.broadinstitute.gatk.queue.function.scattergather.ScatterGatherableFunction
import org.broadinstitute.gatk.queue.util.{ Logging => GatkLogging }
trait BiopetQScript extends Configurable {
trait BiopetQScript extends Configurable with GatkLogging {
@Argument(doc = "JSON config file(s)", fullName = "config_file", shortName = "config", required = false)
val configfiles: List[File] = Nil
......
package nl.lumc.sasc.biopet.core
import nl.lumc.sasc.biopet.core.config.{ Config, Configurable }
import nl.lumc.sasc.biopet.utils.ConfigUtils._
trait MultiSampleQScript extends BiopetQScript {
type LibraryOutput <: AbstractLibraryOutput
......@@ -20,7 +21,7 @@ trait MultiSampleQScript extends BiopetQScript {
final def runSamplesJobs() {
if (samplesConfig == null) samplesConfig = Map()
if (Config.global.contains("samples")) for ((key, value) <- samplesConfig) {
var sample = Configurable.any2map(value)
var sample = any2map(value)
if (!sample.contains("ID")) sample += ("ID" -> key)
if (sample("ID") == key) {
samplesOutput += key -> runSingleSampleJobs(sample)
......@@ -31,7 +32,7 @@ trait MultiSampleQScript extends BiopetQScript {
def runSingleSampleJobs(sampleConfig: Map[String, Any]): SampleOutput
def runSingleSampleJobs(sample: String): SampleOutput = {
var map = Configurable.any2map(samplesConfig(sample))
var map = any2map(samplesConfig(sample))
if (map.contains("ID") && map("ID") != sample)
throw new IllegalStateException("ID in config not the same as the key")
else map += ("ID" -> sample)
......@@ -42,9 +43,9 @@ trait MultiSampleQScript extends BiopetQScript {
var output: Map[String, LibraryOutput] = Map()
val sampleID = sampleConfig("ID").toString
if (sampleConfig.contains("libraries")) {
val runs = Configurable.any2map(sampleConfig("libraries"))
val runs = any2map(sampleConfig("libraries"))
for ((key, value) <- runs) {
var library = Configurable.any2map(value)
var library = any2map(value)
if (!library.contains("ID")) library += ("ID" -> key)
if (library("ID") == key) {
output += key -> runSingleLibraryJobs(library, sampleConfig)
......
package nl.lumc.sasc.biopet.core.config
import java.io.File
import nl.lumc.sasc.biopet.utils.ConfigUtils._
class ConfigValue(val requestIndex: ConfigValueIndex, val foundIndex: ConfigValueIndex, val value: Any, val default: Boolean) {
def getString = Configurable.any2string(value)
def getInt = Configurable.any2int(value)
def getDouble = Configurable.any2double(value)
def getList = Configurable.any2list(value)
def getFileList: List[File] = for (file <- Configurable.any2stringList(value)) yield new File(file)
def getStringList: List[String] = Configurable.any2stringList(value)
def getMap = Configurable.any2map(value)
def getBoolean = Configurable.any2boolean(value)
def getString = any2string(value)
def getInt = any2int(value)
def getDouble = any2double(value)
def getList = any2list(value)
def getFileList: List[File] = for (file <- any2stringList(value)) yield new File(file)
def getStringList: List[String] = any2stringList(value)
def getMap = any2map(value)
def getBoolean = any2boolean(value)
override def toString: String = {
var output = "key = " + requestIndex.key
......
package nl.lumc.sasc.biopet.core.config
import java.io.File
import org.broadinstitute.gatk.queue.util.Logging
import scala.language.implicitConversions
import nl.lumc.sasc.biopet.core.Logging
import nl.lumc.sasc.biopet.utils.ConfigUtils.ImplictConversions
trait Configurable extends Logging {
trait Configurable extends ImplictConversions {
val root: Configurable
//val globalConfig: Config = if (root != null) root.globalConfig else new Config()
def configPath: List[String] = if (root != null) root.configFullPath else List()
protected lazy val configName = getClass.getSimpleName.toLowerCase
protected lazy val configFullPath = configName :: configPath
......@@ -25,7 +24,7 @@ trait Configurable extends Logging {
}
if (!contains(key, submodule, freeVar) && d == null) {
if (required) {
logger.error("Value in config could not be found but it is required, key: " + key + " module: " + m + " path: " + p)
Logging.logger.error("Value in config could not be found but it is required, key: " + key + " module: " + m + " path: " + p)
throw new IllegalStateException("Value in config could not be found but it is required, key: " + key + " module: " + m + " path: " + p)
} else return null
}
......@@ -40,120 +39,4 @@ trait Configurable extends Logging {
Config.global.contains(m, p, key, freeVar) || !(Config.getValueFromMap(defaults.toMap, ConfigValueIndex(m, p, key, freeVar)) == None)
}
}
implicit def configValue2file(value: ConfigValue): File = if (value != null) new File(Configurable.any2string(value.value)) else null
implicit def configValue2string(value: ConfigValue): String = if (value != null) Configurable.any2string(value.value) else null
implicit def configValue2long(value: ConfigValue): Long = if (value != null) Configurable.any2long(value.value) else 0
implicit def configValue2optionLong(value: ConfigValue): Option[Long] = if (value != null) Option(Configurable.any2long(value.value)) else None
implicit def configValue2int(value: ConfigValue): Int = if (value != null) Configurable.any2int(value.value) else 0
implicit def configValue2optionInt(value: ConfigValue): Option[Int] = if (value != null) Option(Configurable.any2int(value.value)) else None
implicit def configValue2double(value: ConfigValue): Double = if (value != null) Configurable.any2double(value.value) else 0
implicit def configValue2optionDouble(value: ConfigValue): Option[Double] = if (value != null) Option(Configurable.any2double(value.value)) else None
implicit def configValue2float(value: ConfigValue): Float = if (value != null) Configurable.any2float(value.value) else 0
implicit def configValue2optionFloat(value: ConfigValue): Option[Float] = if (value != null) Option(Configurable.any2float(value.value)) else None
implicit def configValue2boolean(value: ConfigValue): Boolean = if (value != null) Configurable.any2boolean(value.value) else false
implicit def configValue2optionBoolean(value: ConfigValue): Option[Boolean] = if (value != null) Option(Configurable.any2boolean(value.value)) else None
implicit def configValue2list(value: ConfigValue): List[Any] = if (value != null) Configurable.any2list(value.value) else null
implicit def configValue2stringList(value: ConfigValue): List[String] = if (value != null) Configurable.any2stringList(value.value) else null
implicit def configValue2stringSet(value: ConfigValue): Set[String] = if (value != null) Configurable.any2stringList(value.value).toSet else null
implicit def configValue2map(value: ConfigValue): Map[String, Any] = if (value != null) Configurable.any2map(value.value) else null
}
object Configurable extends Logging {
def any2string(any: Any): String = {
if (any == null) return null
any match {
case s: String => return s
case _ => return any.toString
}
}
def any2int(any: Any): Int = {
any match {
case i: Double => return i.toInt
case i: Int => return i
case i: String => {
logger.warn("Value '" + any + "' is a string insteadof int in json file, trying auto convert")
return i.toInt
}
case _ => throw new IllegalStateException("Value '" + any + "' is not an int")
}
}
def any2long(any: Any): Long = {
any match {
case l: Double => return l.toLong
case l: Int => return l.toLong
case l: Long => return l
case l: String => {
logger.warn("Value '" + any + "' is a string insteadof int in json file, trying auto convert")
return l.toLong
}
case _ => throw new IllegalStateException("Value '" + any + "' is not an int")
}
}
def any2double(any: Any): Double = {
any match {
case d: Double => return d
case d: Float => return d.toDouble
case d: Int => return d.toDouble
case d: String => {
logger.warn("Value '" + any + "' is a string insteadof int in json file, trying auto convert")
return d.toDouble
}
case _ => throw new IllegalStateException("Value '" + any + "' is not an int")
}
}
def any2float(any: Any): Float = {
any match {
case f: Double => return f.toFloat
case f: Int => return f.toFloat
case f: Float => return f
case f: String => {
logger.warn("Value '" + any + "' is a string insteadof int in json file, trying auto convert")
return f.toFloat
}
case _ => throw new IllegalStateException("Value '" + any + "' is not an int")
}
}
def any2boolean(any: Any): Boolean = {
any match {
case b: Boolean => return b
case b: String => {
logger.warn("Value '" + any + "' is a string insteadof boolean in json file, trying auto convert")
return b.contains("true")
}
case b: Int => {
logger.warn("Value '" + any + "' is a int insteadof boolean in json file, trying auto convert")
return (b > 0)
}
case _ => throw new IllegalStateException("Value '" + any + "' is not an boolean")
}
}
def any2list(any: Any): List[Any] = {
if (any == null) return null
any match {
case l: List[_] => return l
case _ => List(any)
}
}
def any2stringList(any: Any): List[String] = {
if (any == null) return null
var l: List[String] = Nil
for (v <- any2list(any)) l :+= v.toString
return l
}
def any2map(any: Any): Map[String, Any] = {
if (any == null) return null
any match {
case m: Map[_, _] => return m.asInstanceOf[Map[String, Any]]
case _ => throw new IllegalStateException("Value '" + any + "' is not an Map")
}
}
}
\ No newline at end of file
package nl.lumc.sasc.biopet.utils
import java.io.File
import nl.lumc.sasc.biopet.core.Logging
import nl.lumc.sasc.biopet.core.config.ConfigValue
object ConfigUtils extends Logging {
def any2string(any: Any): String = {
if (any == null) return null
any match {
case s: String => return s
case _ => return any.toString
}
}
def any2int(any: Any): Int = {
any match {
case i: Double => return i.toInt
case i: Int => return i
case i: String => {
logger.warn("Value '" + any + "' is a string insteadof int in json file, trying auto convert")
return i.toInt
}
case _ => throw new IllegalStateException("Value '" + any + "' is not an int")
}
}
def any2long(any: Any): Long = {
any match {
case l: Double => return l.toLong
case l: Int => return l.toLong
case l: Long => return l
case l: String => {
logger.warn("Value '" + any + "' is a string insteadof int in json file, trying auto convert")
return l.toLong
}
case _ => throw new IllegalStateException("Value '" + any + "' is not an int")
}
}
def any2double(any: Any): Double = {
any match {
case d: Double => return d
case d: Float => return d.toDouble
case d: Int => return d.toDouble
case d: String => {
logger.warn("Value '" + any + "' is a string insteadof int in json file, trying auto convert")
return d.toDouble
}
case _ => throw new IllegalStateException("Value '" + any + "' is not an int")
}
}
def any2float(any: Any): Float = {
any match {
case f: Double => return f.toFloat
case f: Int => return f.toFloat
case f: Float => return f
case f: String => {
logger.warn("Value '" + any + "' is a string insteadof int in json file, trying auto convert")
return f.toFloat
}
case _ => throw new IllegalStateException("Value '" + any + "' is not an int")
}
}
def any2boolean(any: Any): Boolean = {
any match {
case b: Boolean => return b
case b: String => {
logger.warn("Value '" + any + "' is a string insteadof boolean in json file, trying auto convert")
return b.contains("true")
}
case b: Int => {
logger.warn("Value '" + any + "' is a int insteadof boolean in json file, trying auto convert")
return (b > 0)
}
case _ => throw new IllegalStateException("Value '" + any + "' is not an boolean")
}
}
def any2list(any: Any): List[Any] = {
if (any == null) return null
any match {
case l: List[_] => return l
case _ => List(any)
}
}
def any2stringList(any: Any): List[String] = {
if (any == null) return null
var l: List[String] = Nil
for (v <- any2list(any)) l :+= v.toString
return l
}
def any2map(any: Any): Map[String, Any] = {
if (any == null) return null
any match {
case m: Map[_, _] => return m.asInstanceOf[Map[String, Any]]
case _ => throw new IllegalStateException("Value '" + any + "' is not an Map")
}
}
trait ImplictConversions {
import scala.language.implicitConversions
implicit def configValue2file(value: ConfigValue): File = {
if (value != null) new File(any2string(value.value))
else null
}
implicit def configValue2string(value: ConfigValue): String = {
if (value != null) any2string(value.value)
else null
}
implicit def configValue2long(value: ConfigValue): Long = {
if (value != null) any2long(value.value)
else 0
}
implicit def configValue2optionLong(value: ConfigValue): Option[Long] = {
if (value != null) Option(any2long(value.value))
else None
}
implicit def configValue2int(value: ConfigValue): Int = {
if (value != null) any2int(value.value)
else 0
}
implicit def configValue2optionInt(value: ConfigValue): Option[Int] = {
if (value != null) Option(any2int(value.value))
else None
}
implicit def configValue2double(value: ConfigValue): Double = {
if (value != null) any2double(value.value)
else 0
}
implicit def configValue2optionDouble(value: ConfigValue): Option[Double] = {
if (value != null) Option(any2double(value.value))
else None
}
implicit def configValue2float(value: ConfigValue): Float = {
if (value != null) any2float(value.value)
else 0
}
implicit def configValue2optionFloat(value: ConfigValue): Option[Float] = {
if (value != null) Option(any2float(value.value))
else None
}
implicit def configValue2boolean(value: ConfigValue): Boolean = {
if (value != null) any2boolean(value.value)
else false
}
implicit def configValue2optionBoolean(value: ConfigValue): Option[Boolean] = {
if (value != null) Option(any2boolean(value.value))
else None
}
implicit def configValue2list(value: ConfigValue): List[Any] = {
if (value != null) any2list(value.value)
else null
}
implicit def configValue2stringList(value: ConfigValue): List[String] = {
if (value != null) any2stringList(value.value)
else null
}
implicit def configValue2stringSet(value: ConfigValue): Set[String] = {
if (value != null) any2stringList(value.value).toSet
else null
}
implicit def configValue2map(value: ConfigValue): Map[String, Any] = {
if (value != null) any2map(value.value)
else null
}
}
}
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