MultiSampleQScript.scala 3.39 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
  type Sample <: AbstractSample
  abstract class AbstractSample {
    val sampleId: String
Peter van 't Hof's avatar
Peter van 't Hof committed
34
    val config = new ConfigFunctions(defaultSample = Some(sampleId))
35
36
    abstract class AbstractLibrary {
      val libraryId: String
Peter van 't Hof's avatar
Peter van 't Hof committed
37
      val config = new ConfigFunctions(defaultSample = Some(sampleId), defaultLibrary = Some(libraryId))
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
      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
53

54
55
56
    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
57

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

64
    protected def runJobs()
Peter van 't Hof's avatar
Peter van 't Hof committed
65

66
    val libraries: Map[String, Library]
67

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

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

Peter van 't Hof's avatar
Peter van 't Hof committed
79
  var samples: Map[String, Sample] = Map()
80

81
82
83
  /** 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
84
85
  }

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

93
94
95
  private var currentSample: Option[String] = None
  private var currentLibrary: Option[String] = None

96
  override protected[core] def configFullPath: List[String] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
97
98
99
100
101
102
103
104
105
    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
106
  }
107
}