MultiSampleQScript.scala 5.94 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.
 */
16
17
package nl.lumc.sasc.biopet.core

18
import nl.lumc.sasc.biopet.core.config.{ ConfigValue, Config, Configurable }
19
import nl.lumc.sasc.biopet.utils.ConfigUtils._
20
21

trait MultiSampleQScript extends BiopetQScript {
Peter van 't Hof's avatar
Peter van 't Hof committed
22
  type LibraryOutput <: AbstractLibraryOutput
Peter van 't Hof's avatar
Peter van 't Hof committed
23
  type SampleOutput <: AbstractSampleOutput
Peter van 't Hof's avatar
Peter van 't Hof committed
24

Peter van 't Hof's avatar
Peter van 't Hof committed
25
  abstract class AbstractLibraryOutput
Peter van 't Hof's avatar
Peter van 't Hof committed
26
27
  abstract class AbstractSampleOutput {
    var libraries: Map[String, LibraryOutput] = Map()
Peter van 't Hof's avatar
Peter van 't Hof committed
28
    def getAllLibraries = libraries
Peter van 't Hof's avatar
Peter van 't Hof committed
29
    def getLibrary(key: String) = libraries(key)
Peter van 't Hof's avatar
Peter van 't Hof committed
30
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
31

32
  if (!config.contains("samples")) logger.warn("No Samples found in config")
Peter van 't Hof's avatar
Peter van 't Hof committed
33
34
35
36

  /**
   * Returns a map with all sample configs
   */
37
  val getSamplesConfig: Map[String, Any] = config("samples", default = Map())
Peter van 't Hof's avatar
Peter van 't Hof committed
38
39
40
41

  /**
   * Returns a list of all sampleIDs
   */
42
  val getSamples: Set[String] = getSamplesConfig.keySet
Peter van 't Hof's avatar
Peter van 't Hof committed
43
44
45
46
47

  /**
   * Returns the global sample directory
   * @return global sample directory
   */
Peter van 't Hof's avatar
Peter van 't Hof committed
48
  def globalSampleDir: String = outputDir + "samples/"
Peter van 't Hof's avatar
Peter van 't Hof committed
49

50
51
  var samplesOutput: Map[String, SampleOutput] = Map()

Peter van 't Hof's avatar
Peter van 't Hof committed
52
53
54
  /**
   * Runs runSingleSampleJobs method for each sample
   */
Peter van 't Hof's avatar
Peter van 't Hof committed
55
  final def runSamplesJobs() {
56
    for ((key, value) <- getSamplesConfig) {
57
      var sample = any2map(value)
58
59
      if (!sample.contains("ID")) sample += ("ID" -> key)
      if (sample("ID") == key) {
Peter van 't Hof's avatar
Peter van 't Hof committed
60
        currentSample = key
Peter van 't Hof's avatar
Peter van 't Hof committed
61
        samplesOutput += key -> runSingleSampleJobs(sample)
Peter van 't Hof's avatar
Peter van 't Hof committed
62
        currentSample = null
Peter van 't Hof's avatar
Peter van 't Hof committed
63
      } else logger.warn("Key is not the same as ID on value for sample")
bow's avatar
bow committed
64
    }
65
  }
bow's avatar
bow committed
66

Peter van 't Hof's avatar
Peter van 't Hof committed
67
  def runSingleSampleJobs(sampleConfig: Map[String, Any]): SampleOutput
Peter van 't Hof's avatar
Peter van 't Hof committed
68
69
70
71
72
73

  /**
   * Run sample with only sampleID
   * @param sample sampleID
   * @return
   */
Peter van 't Hof's avatar
Peter van 't Hof committed
74
  def runSingleSampleJobs(sample: String): SampleOutput = {
75
    var map = any2map(getSamplesConfig(sample))
76
77
78
79
    if (map.contains("ID") && map("ID") != sample)
      throw new IllegalStateException("ID in config not the same as the key")
    else map += ("ID" -> sample)
    return runSingleSampleJobs(map)
80
  }
bow's avatar
bow committed
81

Peter van 't Hof's avatar
Peter van 't Hof committed
82
83
84
85
86
  /**
   * Runs runSingleLibraryJobs method for each library found in sampleConfig
   * @param sampleConfig sample config
   * @return Map with libraryID -> LibraryOutput object
   */
Peter van 't Hof's avatar
Peter van 't Hof committed
87
88
  final def runLibraryJobs(sampleConfig: Map[String, Any]): Map[String, LibraryOutput] = {
    var output: Map[String, LibraryOutput] = Map()
Peter van 't Hof's avatar
Peter van 't Hof committed
89
    val sampleID = sampleConfig("ID").toString
Peter van 't Hof's avatar
Peter van 't Hof committed
90
    if (sampleConfig.contains("libraries")) {
91
      val runs = any2map(sampleConfig("libraries"))
bow's avatar
bow committed
92
      for ((key, value) <- runs) {
93
        var library = any2map(value)
Peter van 't Hof's avatar
Peter van 't Hof committed
94
95
        if (!library.contains("ID")) library += ("ID" -> key)
        if (library("ID") == key) {
Peter van 't Hof's avatar
Peter van 't Hof committed
96
          currentLibrary = key
Peter van 't Hof's avatar
Peter van 't Hof committed
97
          output += key -> runSingleLibraryJobs(library, sampleConfig)
Peter van 't Hof's avatar
Peter van 't Hof committed
98
          currentLibrary = null
Peter van 't Hof's avatar
Peter van 't Hof committed
99
        } else logger.warn("Key is not the same as ID on value for run of sample: " + sampleID)
100
101
102
103
      }
    } else logger.warn("No runs found in config for sample: " + sampleID)
    return output
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
104
  def runSingleLibraryJobs(runConfig: Map[String, Any], sampleConfig: Map[String, Any]): LibraryOutput
105

Peter van 't Hof's avatar
Peter van 't Hof committed
106
107
  protected var currentSample: String = null
  protected var currentLibrary: String = null
108

Peter van 't Hof's avatar
Peter van 't Hof committed
109
110
111
112
  /**
   * Set current sample manual, only use this when not using runSamplesJobs method
   * @param sample
   */
113
  def setCurrentSample(sample: String) {
Peter van 't Hof's avatar
Peter van 't Hof committed
114
    logger.debug("Manual sample set to: " + sample)
115
116
117
    currentSample = sample
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
118
119
120
121
  /**
   * Gets current sample
   * @return current sample
   */
122
123
  def getCurrentSample = currentSample

Peter van 't Hof's avatar
Peter van 't Hof committed
124
125
126
127
128
  /**
   * Reset current sample manual, only use this when not using runSamplesJobs method
   */
  def resetCurrentSample() {
    logger.debug("Manual sample reset")
129
130
131
    currentSample = null
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
132
133
134
135
  /**
   * Set current library manual, only use this when not using runLibraryJobs method
   * @param library
   */
136
  def setCurrentLibrary(library: String) {
Peter van 't Hof's avatar
Peter van 't Hof committed
137
    logger.debug("Manual library set to: " + library)
138
139
140
    currentLibrary = library
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
141
142
143
144
  /**
   * Gets current library
   * @return current library
   */
145
146
  def getCurrentLibrary = currentLibrary

Peter van 't Hof's avatar
Peter van 't Hof committed
147
148
149
150
151
  /**
   * Reset current library manual, only use this when not using runLibraryJobs method
   */
  def resetCurrentLibrary() {
    logger.debug("Manual library reset")
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    currentLibrary = null
  }

  override protected[core] def configFullPath: List[String] = {
    (if (currentSample != null) "samples" :: currentSample :: Nil else Nil) :::
      (if (currentLibrary != null) "libraries" :: currentLibrary :: Nil else Nil) :::
      super.configFullPath
  }

  protected class ConfigFunctions extends super.ConfigFunctions {
    override def apply(key: String,
                       default: Any = null,
                       submodule: String = null,
                       required: Boolean = false,
                       freeVar: Boolean = true,
                       sample: String = currentSample,
                       library: String = currentLibrary): ConfigValue = {
      super.apply(key, default, submodule, required, freeVar, sample, library)
    }

    override def contains(key: String,
                          submodule: String = null,
                          freeVar: Boolean = true,
                          sample: String = currentSample,
                          library: String = currentLibrary) = {
      super.contains(key, submodule, freeVar, sample, library)
    }
  }
180
}