MultiSampleQScript.scala 4.69 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
33
34
  if (!config.contains("samples")) logger.warn("No Samples found in config")
  val getSamplesConfig: Map[String, Any] = config("samples", default = Map())
  val getSamples: Set[String] = getSamplesConfig.keySet
Peter van 't Hof's avatar
Peter van 't Hof committed
35
  def globalSampleDir: String = outputDir + "samples/"
Peter van 't Hof's avatar
Peter van 't Hof committed
36

37
38
  var samplesOutput: Map[String, SampleOutput] = Map()

Peter van 't Hof's avatar
Peter van 't Hof committed
39
  final def runSamplesJobs() {
40
    for ((key, value) <- getSamplesConfig) {
41
      var sample = any2map(value)
42
43
      if (!sample.contains("ID")) sample += ("ID" -> key)
      if (sample("ID") == key) {
44
        setCurrentSample(key)
Peter van 't Hof's avatar
Peter van 't Hof committed
45
        samplesOutput += key -> runSingleSampleJobs(sample)
46
        unsetCurrentSample()
Peter van 't Hof's avatar
Peter van 't Hof committed
47
      } else logger.warn("Key is not the same as ID on value for sample")
bow's avatar
bow committed
48
    }
49
  }
bow's avatar
bow committed
50

Peter van 't Hof's avatar
Peter van 't Hof committed
51
52
  def runSingleSampleJobs(sampleConfig: Map[String, Any]): SampleOutput
  def runSingleSampleJobs(sample: String): SampleOutput = {
53
    var map = any2map(getSamplesConfig(sample))
54
55
56
57
    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)
58
  }
bow's avatar
bow committed
59

Peter van 't Hof's avatar
Peter van 't Hof committed
60
61
  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
62
    val sampleID = sampleConfig("ID").toString
Peter van 't Hof's avatar
Peter van 't Hof committed
63
    if (sampleConfig.contains("libraries")) {
64
      val runs = any2map(sampleConfig("libraries"))
bow's avatar
bow committed
65
      for ((key, value) <- runs) {
66
        var library = any2map(value)
Peter van 't Hof's avatar
Peter van 't Hof committed
67
68
        if (!library.contains("ID")) library += ("ID" -> key)
        if (library("ID") == key) {
69
          setCurrentLibrary(key)
Peter van 't Hof's avatar
Peter van 't Hof committed
70
          output += key -> runSingleLibraryJobs(library, sampleConfig)
71
          unsetCurrentLibrary()
Peter van 't Hof's avatar
Peter van 't Hof committed
72
        } else logger.warn("Key is not the same as ID on value for run of sample: " + sampleID)
73
74
75
76
      }
    } else logger.warn("No runs found in config for sample: " + sampleID)
    return output
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
77
  def runSingleLibraryJobs(runConfig: Map[String, Any], sampleConfig: Map[String, Any]): LibraryOutput
78
79
80
81
82
83
84
85

  private var currentSample: String = null
  private var currentLibrary: String = null

  def setCurrentSample(sample: String) {
    currentSample = sample
  }

86
87
  def getCurrentSample = currentSample

88
89
90
91
92
93
94
95
  def unsetCurrentSample() {
    currentSample = null
  }

  def setCurrentLibrary(library: String) {
    currentLibrary = library
  }

96
97
  def getCurrentLibrary = currentLibrary

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  def unsetCurrentLibrary() {
    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)
    }
  }
127
}