Configurable.scala 5.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Biopet is built on top of GATK Queue for building bioinformatic
 * pipelines. It is mainly intended to support LUMC SHARK cluster which is running
 * SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
 * should also be able to execute Biopet tools and pipelines.
 *
 * Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
 *
 * Contact us at: sasc@lumc.nl
 *
 * A dual licensing mode is applied. The source code within this project that are
 * not part of GATK Queue is freely available for non-commercial use under an AGPL
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
16
17
package nl.lumc.sasc.biopet.core.config

18
import nl.lumc.sasc.biopet.core.Logging
Peter van 't Hof's avatar
Peter van 't Hof committed
19
import nl.lumc.sasc.biopet.utils.ConfigUtils.ImplicitConversions
Peter van 't Hof's avatar
Peter van 't Hof committed
20
import scala.collection.JavaConversions._
Peter van 't Hof's avatar
Peter van 't Hof committed
21

Peter van 't Hof's avatar
Peter van 't Hof committed
22
trait Configurable extends ImplicitConversions {
Peter van 't Hof's avatar
Peter van 't Hof committed
23
  /** Should be object of parant object */
24
  val root: Configurable
Peter van 't Hof's avatar
Peter van 't Hof committed
25
  def globalConfig: Config = if (root != null) root.globalConfig else Config.global
Peter van 't Hof's avatar
Peter van 't Hof committed
26

Peter van 't Hof's avatar
Peter van 't Hof committed
27
28
  /** subfix to the path */
  def subPath: List[String] = Nil
Peter van 't Hof's avatar
Peter van 't Hof committed
29

Peter van 't Hof's avatar
Peter van 't Hof committed
30
31
32
33
  /** Get default path to search config values for current object */
  def configPath: List[String] = if (root != null) root.configFullPath ::: subPath else subPath

  /** Gets name of module for config */
34
  protected[core] def configName = getClass.getSimpleName.toLowerCase
Peter van 't Hof's avatar
Peter van 't Hof committed
35

Peter van 't Hof's avatar
Peter van 't Hof committed
36
  /** ull path with module in there */
37
  protected[core] def configFullPath: List[String] = configPath ::: configName :: Nil
Peter van 't Hof's avatar
Peter van 't Hof committed
38

Peter van 't Hof's avatar
Peter van 't Hof committed
39
  /** Map to store defaults for config */
40
41
  def defaults: Map[String, Any] = {
    if (root != null) root.defaults
42
    else globalConfig.defaults
Peter van 't Hof's avatar
Peter van 't Hof committed
43
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
44

Peter van 't Hof's avatar
Peter van 't Hof committed
45
  val config = new ConfigFunctions
Peter van 't Hof's avatar
Peter van 't Hof committed
46

Peter van 't Hof's avatar
Peter van 't Hof committed
47
48
49
50
51
52
53
54
  /**
   * Creates path with a prefix for sample and library
   * "samples" -> "sampleID" -> "libraries" -> "libraryID" -> rest of path
   * @param sample
   * @param library
   * @param submodule
   * @return
   */
55
56
57
  def path(sample: String = null, library: String = null, submodule: String = null) = {
    (if (sample != null) "samples" :: sample :: Nil else Nil) :::
      (if (library != null) "libraries" :: library :: Nil else Nil) :::
Peter van 't Hof's avatar
Peter van 't Hof committed
58
      (if (submodule != null) configPath ::: configName :: Nil else configPath)
59
60
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
61
62
63
  /**
   * Class is used for retrieval of config values
   */
64
  protected class ConfigFunctions(val defaultSample: Option[String] = None, val defaultLibrary: Option[String] = None) {
Peter van 't Hof's avatar
Peter van 't Hof committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    def this(defaultSample: String, defaultLibrary: String) = {
      this(defaultSample = Some(defaultSample), defaultLibrary = Some(defaultLibrary))
    }

    def this(defaultSample: String) = {
      this(defaultSample = Some(defaultSample), defaultLibrary = None)
    }

    (defaultSample, defaultLibrary) match {
      case (Some(null), _) => throw new IllegalArgumentException("defaultSample can not be null")
      case (_, Some(null)) => throw new IllegalArgumentException("defaultLibrary can not be null")
      case _               =>
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
79
80
81
82
83
84
85
86
87
88
    /**
     *
     * @param key Name of value
     * @param default Default value if not found
     * @param submodule Adds to the path
     * @param freeVar Default true, if set false value must exist in module
     * @param sample Default null, when set path is prefixed with "samples" -> "sampleID"
     * @param library Default null, when set path is prefixed with "libraries" -> "libraryID"
     * @return
     */
89
90
91
92
93
94
    def apply(key: String,
              default: Any = null,
              submodule: String = null,
              freeVar: Boolean = true,
              sample: String = null,
              library: String = null): ConfigValue = {
95
96
      val s = if (sample != null || defaultSample.isEmpty) sample else defaultSample.get
      val l = if (library != null || defaultLibrary.isEmpty) library else defaultLibrary.get
97
      val m = if (submodule != null) submodule else configName
98
      val p = path(s, l, submodule)
99
      val d = {
Peter van 't Hof's avatar
Peter van 't Hof committed
100
101
        val value = Config.getValueFromMap(defaults.toMap, ConfigValueIndex(m, p, key, freeVar))
        if (value.isDefined) value.get.value else default
102
      }
103
104
      if (d == null) globalConfig(m, p, key, freeVar = freeVar)
      else globalConfig(m, p, key, d, freeVar)
Peter van 't Hof's avatar
Peter van 't Hof committed
105
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
106

Peter van 't Hof's avatar
Peter van 't Hof committed
107
108
109
110
111
112
113
114
115
    /**
     * Check if value exist in config
     * @param key Name of value
     * @param submodule Adds to the path
     * @param freeVar Default true, if set false value must exist in module
     * @param sample Default null, when set path is prefixed with "samples" -> "sampleID"
     * @param library Default null, when set path is prefixed with "libraries" -> "libraryID"
     * @return true when value is found in config
     */
116
117
118
119
120
    def contains(key: String,
                 submodule: String = null,
                 freeVar: Boolean = true,
                 sample: String = null,
                 library: String = null) = {
121
122
      val s = if (sample != null || defaultSample.isEmpty) sample else defaultSample.get
      val l = if (library != null || defaultLibrary.isEmpty) library else defaultLibrary.get
123
      val m = if (submodule != null) submodule else configName
124
      val p = path(s, l, submodule)
bow's avatar
bow committed
125

126
      globalConfig.contains(m, p, key, freeVar) || !(Config.getValueFromMap(defaults.toMap, ConfigValueIndex(m, p, key, freeVar)) == None)
127
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
128
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
129
}