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

18
19
import java.io.File

20
import nl.lumc.sasc.biopet.core.config.{ ConfigValue, Config, Configurable }
21
import nl.lumc.sasc.biopet.utils.ConfigUtils
22
import nl.lumc.sasc.biopet.utils.ConfigUtils._
Peter van 't Hof's avatar
Peter van 't Hof committed
23
import org.broadinstitute.gatk.utils.commandline.{ Argument }
24
25

trait MultiSampleQScript extends BiopetQScript {
Peter van 't Hof's avatar
Peter van 't Hof committed
26
27
28
  @Argument(doc = "Only Sample", shortName = "sample", required = false)
  val onlySample: List[String] = Nil

29
  if (!Config.global.map.contains("samples")) logger.warn("No Samples found in config")
Peter van 't Hof's avatar
Peter van 't Hof committed
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  type Sample <: AbstractSample
  abstract class AbstractSample {
    val sampleId: String
    abstract class AbstractLibrary {
      val libraryId: String
      final def run(): Unit = {
        currentSample = Some(sampleId)
        currentLibrary = Some(libraryId)
        runJobs()
        currentLibrary = None
        currentSample = None
      }

      def getLibraryDir: String = {
        getSampleDir + "libraries" + File.pathSeparator + libraryId + File.pathSeparator
      }

      protected def runJobs()
    }
    type Library <: AbstractLibrary
Peter van 't Hof's avatar
Peter van 't Hof committed
51

52
53
54
    protected def getLibrariesIds: Set[String] = {
      ConfigUtils.getMapFromPath(Config.global.map, List("samples", sampleId, "libraries")).getOrElse(Map()).keySet
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
55

56
57
58
59
60
    final def run(): Unit = {
      currentSample = Some(sampleId)
      runJobs()
      currentSample = None
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
61

62
    protected def runJobs()
Peter van 't Hof's avatar
Peter van 't Hof committed
63

64
    val libraries: Map[String, Library]
65

66
67
68
69
    protected final def runLibraryJobs(): Unit = {
      for ((libraryId, library) <- libraries) {
        library.run()
      }
bow's avatar
bow committed
70
71
    }

72
73
    def getSampleDir: String = {
      outputDir + "samples" + File.pathSeparator + sampleId + File.pathSeparator
Peter van 't Hof's avatar
Peter van 't Hof committed
74
    }
75
76
  }

77
  val samples: Map[String, Sample]
78

79
80
81
  /** Returns a list of all sampleIDs */
  protected def getSamplesIds: Set[String] = if (onlySample != Nil) onlySample.toSet else {
    ConfigUtils.any2map(Config.global.map.getOrElse("samples", Map())).keySet
82
83
  }

84
85
86
87
88
  /** Runs runSingleSampleJobs method for each sample */
  final def runSamplesJobs() {
    for ((sampleId, sample) <- samples) {
      sample.run()
    }
89
90
  }

91
92
93
  private var currentSample: Option[String] = None
  private var currentLibrary: Option[String] = None

94
  override protected[core] def configFullPath: List[String] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
95
96
97
98
99
100
101
102
103
    val s = currentSample match {
      case Some(s) => "samples" :: s :: Nil
      case _       => Nil
    }
    val l = currentLibrary match {
      case Some(l) => "libraries" :: l :: Nil
      case _       => Nil
    }
    s ::: l ::: super.configFullPath
104
105
  }

106
107
108
  override val config = new ConfigFunctionsExt

  protected class ConfigFunctionsExt extends super.ConfigFunctions {
109
110
111
112
113
    override def apply(key: String,
                       default: Any = null,
                       submodule: String = null,
                       required: Boolean = false,
                       freeVar: Boolean = true,
114
115
                       sample: String = null,
                       library: String = null): ConfigValue = {
Peter van 't Hof's avatar
Peter van 't Hof committed
116
117
      val s = currentSample.getOrElse(sample)
      val l = currentLibrary.getOrElse(library)
118
      super.apply(key, default, submodule, required, freeVar, s, l)
119
120
121
122
123
    }

    override def contains(key: String,
                          submodule: String = null,
                          freeVar: Boolean = true,
124
125
                          sample: String = null,
                          library: String = null) = {
Peter van 't Hof's avatar
Peter van 't Hof committed
126
127
      val s = currentSample.getOrElse(sample)
      val l = currentLibrary.getOrElse(library)
128
      super.contains(key, submodule, freeVar, s, l)
129
130
    }
  }
131
}