MultiSampleQScript.scala 4.6 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
  var samplesConfig: Map[String, Any] = config("samples")
Peter van 't Hof's avatar
Peter van 't Hof committed
33
  var samplesOutput: Map[String, SampleOutput] = Map()
Peter van 't Hof's avatar
Peter van 't Hof committed
34
  def globalSampleDir: String = outputDir + "samples/"
Peter van 't Hof's avatar
Peter van 't Hof committed
35

Peter van 't Hof's avatar
Peter van 't Hof committed
36
37
  final def runSamplesJobs() {
    if (samplesConfig == null) samplesConfig = Map()
Peter van 't Hof's avatar
Peter van 't Hof committed
38
    if (Config.global.contains("samples")) for ((key, value) <- samplesConfig) {
39
      var sample = any2map(value)
40
41
      if (!sample.contains("ID")) sample += ("ID" -> key)
      if (sample("ID") == key) {
42
        setCurrentSample(key)
Peter van 't Hof's avatar
Peter van 't Hof committed
43
        samplesOutput += key -> runSingleSampleJobs(sample)
44
        unsetCurrentSample()
Peter van 't Hof's avatar
Peter van 't Hof committed
45
      } else logger.warn("Key is not the same as ID on value for sample")
bow's avatar
bow committed
46
47
    }
    else logger.warn("No Samples found in config")
48
  }
bow's avatar
bow committed
49

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

Peter van 't Hof's avatar
Peter van 't Hof committed
59
60
  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
61
    val sampleID = sampleConfig("ID").toString
Peter van 't Hof's avatar
Peter van 't Hof committed
62
    if (sampleConfig.contains("libraries")) {
63
      val runs = any2map(sampleConfig("libraries"))
bow's avatar
bow committed
64
      for ((key, value) <- runs) {
65
        var library = any2map(value)
Peter van 't Hof's avatar
Peter van 't Hof committed
66
67
        if (!library.contains("ID")) library += ("ID" -> key)
        if (library("ID") == key) {
68
          setCurrentLibrary(key)
Peter van 't Hof's avatar
Peter van 't Hof committed
69
          output += key -> runSingleLibraryJobs(library, sampleConfig)
70
          unsetCurrentLibrary()
Peter van 't Hof's avatar
Peter van 't Hof committed
71
        } else logger.warn("Key is not the same as ID on value for run of sample: " + sampleID)
72
73
74
75
      }
    } else logger.warn("No runs found in config for sample: " + sampleID)
    return output
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
76
  def runSingleLibraryJobs(runConfig: Map[String, Any], sampleConfig: Map[String, Any]): LibraryOutput
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

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

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

  def unsetCurrentSample() {
    currentSample = null
  }

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

  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)
    }
  }
122
}