MultiSampleQScript.scala 5.73 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
import nl.lumc.sasc.biopet.utils.ConfigUtils._
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import org.broadinstitute.gatk.utils.commandline.{ Argument }
22
23

trait MultiSampleQScript extends BiopetQScript {
Peter van 't Hof's avatar
Peter van 't Hof committed
24
  type LibraryOutput <: AbstractLibraryOutput
Peter van 't Hof's avatar
Peter van 't Hof committed
25
  type SampleOutput <: AbstractSampleOutput
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
29
  @Argument(doc = "Only Sample", shortName = "sample", required = false)
  val onlySample: List[String] = Nil

Peter van 't Hof's avatar
Peter van 't Hof committed
30
  abstract class AbstractLibraryOutput
Peter van 't Hof's avatar
Peter van 't Hof committed
31
32
  abstract class AbstractSampleOutput {
    var libraries: Map[String, LibraryOutput] = Map()
Peter van 't Hof's avatar
Peter van 't Hof committed
33
    def getAllLibraries = libraries
Peter van 't Hof's avatar
Peter van 't Hof committed
34
    def getLibrary(key: String) = libraries(key)
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
  if (!Config.global.map.contains("samples")) logger.warn("No Samples found in config")
Peter van 't Hof's avatar
Peter van 't Hof committed
38
39
40
41

  /**
   * Returns a map with all sample configs
   */
42
  val getSamplesConfig: Map[String, Any] = ConfigUtils.any2map(Config.global.map.getOrElse("samples", Map()))
Peter van 't Hof's avatar
Peter van 't Hof committed
43

Peter van 't Hof's avatar
Peter van 't Hof committed
44
45
46
47
48
49
  /** Returns a list of all sampleIDs */
  def getSamples: Set[String] = if (onlySample == Nil) getSamplesConfig.keySet else onlySample.toSet

  def getLibraries(sample: String): Set[String] = {
    ConfigUtils.getMapFromPath(getSamplesConfig, List(sample, "libraries")).getOrElse(Map()).keySet
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
50
51
52
53
54

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

57
58
  var samplesOutput: Map[String, SampleOutput] = Map()

Peter van 't Hof's avatar
Peter van 't Hof committed
59
  /** Runs runSingleSampleJobs method for each sample */
Peter van 't Hof's avatar
Peter van 't Hof committed
60
  final def runSamplesJobs() {
Peter van 't Hof's avatar
Peter van 't Hof committed
61
62
63
64
    for (sampleID <- getSamples) {
      currentSample = Some(sampleID)
      samplesOutput += sampleID -> runSingleSampleJobs(sampleID)
      currentSample = null
bow's avatar
bow committed
65
    }
66
  }
bow's avatar
bow committed
67

Peter van 't Hof's avatar
Peter van 't Hof committed
68
69
  /**
   * Run sample with only sampleID
Peter van 't Hof's avatar
Peter van 't Hof committed
70
   * @param sampleID sampleID
Peter van 't Hof's avatar
Peter van 't Hof committed
71
72
   * @return
   */
Peter van 't Hof's avatar
Peter van 't Hof committed
73
  def runSingleSampleJobs(sampleID: String): SampleOutput
bow's avatar
bow committed
74

Peter van 't Hof's avatar
Peter van 't Hof committed
75
76
  /**
   * Runs runSingleLibraryJobs method for each library found in sampleConfig
Peter van 't Hof's avatar
Peter van 't Hof committed
77
   * @param sampleID sampleID
Peter van 't Hof's avatar
Peter van 't Hof committed
78
79
   * @return Map with libraryID -> LibraryOutput object
   */
Peter van 't Hof's avatar
Peter van 't Hof committed
80
  final def runLibraryJobs(sampleID: String = null): Map[String, LibraryOutput] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
81
    var output: Map[String, LibraryOutput] = Map()
Peter van 't Hof's avatar
Peter van 't Hof committed
82
83
84
85
86
    for (libraryID <- getLibraries(sampleID)) {
      currentLibrary = Some(libraryID)
      output += libraryID -> runSingleLibraryJobs(sampleID, libraryID)
      currentLibrary = None
    }
87
88
    return output
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
89
  def runSingleLibraryJobs(sampleID: String, libraryID: String): LibraryOutput
90

Peter van 't Hof's avatar
Peter van 't Hof committed
91
92
  protected var currentSample: Option[String] = None
  protected var currentLibrary: Option[String] = None
93

Peter van 't Hof's avatar
Peter van 't Hof committed
94
95
96
97
  /**
   * Set current sample manual, only use this when not using runSamplesJobs method
   * @param sample
   */
98
  def setCurrentSample(sample: String) {
Peter van 't Hof's avatar
Peter van 't Hof committed
99
    logger.debug("Manual sample set to: " + sample)
Peter van 't Hof's avatar
Peter van 't Hof committed
100
    currentSample = Some(sample)
101
102
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
103
104
105
106
  /**
   * Gets current sample
   * @return current sample
   */
107
108
  def getCurrentSample = currentSample

Peter van 't Hof's avatar
Peter van 't Hof committed
109
110
111
112
113
  /**
   * Reset current sample manual, only use this when not using runSamplesJobs method
   */
  def resetCurrentSample() {
    logger.debug("Manual sample reset")
Peter van 't Hof's avatar
Peter van 't Hof committed
114
    currentSample = None
115
116
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
117
118
119
120
  /**
   * Set current library manual, only use this when not using runLibraryJobs method
   * @param library
   */
121
  def setCurrentLibrary(library: String) {
Peter van 't Hof's avatar
Peter van 't Hof committed
122
    logger.debug("Manual library set to: " + library)
Peter van 't Hof's avatar
Peter van 't Hof committed
123
    currentLibrary = Some(library)
124
125
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
126
127
128
129
  /**
   * Gets current library
   * @return current library
   */
130
131
  def getCurrentLibrary = currentLibrary

132
  /** Reset current library manual, only use this when not using runLibraryJobs method */
Peter van 't Hof's avatar
Peter van 't Hof committed
133
134
  def resetCurrentLibrary() {
    logger.debug("Manual library reset")
Peter van 't Hof's avatar
Peter van 't Hof committed
135
    currentLibrary = None
136
137
138
  }

  override protected[core] def configFullPath: List[String] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
139
140
    (if (currentSample.isDefined) "samples" :: currentSample.get :: Nil else Nil) :::
      (if (currentLibrary.isDefined) "libraries" :: currentLibrary.get :: Nil else Nil) :::
141
142
143
      super.configFullPath
  }

144
145
146
  override val config = new ConfigFunctionsExt

  protected class ConfigFunctionsExt extends super.ConfigFunctions {
147
148
149
150
151
    override def apply(key: String,
                       default: Any = null,
                       submodule: String = null,
                       required: Boolean = false,
                       freeVar: Boolean = true,
152
153
                       sample: String = null,
                       library: String = null): ConfigValue = {
Peter van 't Hof's avatar
Peter van 't Hof committed
154
155
      val s = if (sample == null) currentSample.getOrElse(null) else sample
      val l = if (library == null) currentLibrary.getOrElse(null) else library
156
      super.apply(key, default, submodule, required, freeVar, s, l)
157
158
159
160
161
    }

    override def contains(key: String,
                          submodule: String = null,
                          freeVar: Boolean = true,
162
163
                          sample: String = null,
                          library: String = null) = {
Peter van 't Hof's avatar
Peter van 't Hof committed
164
165
      val s = if (sample == null) currentSample.getOrElse(null) else sample
      val l = if (library == null) currentLibrary.getOrElse(null) else library
166
      super.contains(key, submodule, freeVar, s, l)
167
168
    }
  }
169
}