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

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

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

Peter van 't Hof's avatar
Peter van 't Hof committed
30
  abstract class AbstractSample(val sampleId: String) {
Peter van 't Hof's avatar
Peter van 't Hof committed
31
    val config = new ConfigFunctions(defaultSample = sampleId)
Peter van 't Hof's avatar
Peter van 't Hof committed
32
33

    abstract class AbstractLibrary(val libraryId: String) {
Peter van 't Hof's avatar
Peter van 't Hof committed
34
      val config = new ConfigFunctions(defaultSample = sampleId, defaultLibrary = libraryId)
35
36
37
38
39
40
41
42
43
      final def run(): Unit = {
        currentSample = Some(sampleId)
        currentLibrary = Some(libraryId)
        runJobs()
        currentLibrary = None
        currentSample = None
      }

      def getLibraryDir: String = {
Peter van 't Hof's avatar
Peter van 't Hof committed
44
        getSampleDir + "lib_" + libraryId + File.separator
45
46
47
48
      }

      protected def runJobs()
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
49

50
    type Library <: AbstractLibrary
Peter van 't Hof's avatar
Peter van 't Hof committed
51

Peter van 't Hof's avatar
Peter van 't Hof committed
52
    val libraries: Map[String, Library] = getLibrariesIds.map(id => id -> makeLibrary(id)).toMap
53

Peter van 't Hof's avatar
Peter van 't Hof committed
54
    def makeLibrary(id: String): Library
Peter van 't Hof's avatar
Peter van 't Hof committed
55

56
57
58
    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
59

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

66
    protected def runJobs()
Peter van 't Hof's avatar
Peter van 't Hof committed
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
80
  type Sample <: AbstractSample

Peter van 't Hof's avatar
Peter van 't Hof committed
81
  def makeSample(id: String): Sample
Peter van 't Hof's avatar
Peter van 't Hof committed
82

Peter van 't Hof's avatar
Peter van 't Hof committed
83
  val samples: Map[String, Sample] = getSamplesIds.map(id => id -> makeSample(id)).toMap
84

85
86
87
  /** 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
88
89
  }

90
91
92
93
94
  /** Runs runSingleSampleJobs method for each sample */
  final def runSamplesJobs() {
    for ((sampleId, sample) <- samples) {
      sample.run()
    }
95
96
  }

97
98
99
  private var currentSample: Option[String] = None
  private var currentLibrary: Option[String] = None

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