Commit f9cef581 authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

New abstract classes for samples and libraries

parent 1c6ea1d9
......@@ -15,126 +15,82 @@
*/
package nl.lumc.sasc.biopet.core
import java.io.File
import nl.lumc.sasc.biopet.core.config.{ ConfigValue, Config, Configurable }
import nl.lumc.sasc.biopet.utils.ConfigUtils
import nl.lumc.sasc.biopet.utils.ConfigUtils._
import org.broadinstitute.gatk.utils.commandline.{ Argument }
trait MultiSampleQScript extends BiopetQScript {
type LibraryOutput <: AbstractLibraryOutput
type SampleOutput <: AbstractSampleOutput
@Argument(doc = "Only Sample", shortName = "sample", required = false)
val onlySample: List[String] = Nil
abstract class AbstractLibraryOutput
abstract class AbstractSampleOutput {
var libraries: Map[String, LibraryOutput] = Map()
def getAllLibraries = libraries
def getLibrary(key: String) = libraries(key)
}
if (!Config.global.map.contains("samples")) logger.warn("No Samples found in config")
/**
* Returns a map with all sample configs
*/
val getSamplesConfig: Map[String, Any] = ConfigUtils.any2map(Config.global.map.getOrElse("samples", Map()))
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
/** Returns a list of all sampleIDs */
def getSamples: Set[String] = if (onlySample == Nil) getSamplesConfig.keySet else onlySample.toSet
protected def getLibrariesIds: Set[String] = {
ConfigUtils.getMapFromPath(Config.global.map, List("samples", sampleId, "libraries")).getOrElse(Map()).keySet
}
def getLibraries(sample: String): Set[String] = {
ConfigUtils.getMapFromPath(getSamplesConfig, List(sample, "libraries")).getOrElse(Map()).keySet
}
final def run(): Unit = {
currentSample = Some(sampleId)
runJobs()
currentSample = None
}
/**
* Returns the global sample directory
* @return global sample directory
*/
def globalSampleDir: String = outputDir + "samples/"
protected def runJobs()
var samplesOutput: Map[String, SampleOutput] = Map()
val libraries: Map[String, Library]
/** Runs runSingleSampleJobs method for each sample */
final def runSamplesJobs() {
for (sampleID <- getSamples) {
currentSample = Some(sampleID)
samplesOutput += sampleID -> runSingleSampleJobs(sampleID)
currentSample = None
protected final def runLibraryJobs(): Unit = {
for ((libraryId, library) <- libraries) {
library.run()
}
}
}
/**
* Run sample with only sampleID
* @param sampleID sampleID
* @return
*/
def runSingleSampleJobs(sampleID: String): SampleOutput
/**
* Runs runSingleLibraryJobs method for each library found in sampleConfig
* @param sampleID sampleID
* @return Map with libraryID -> LibraryOutput object
*/
final def runLibraryJobs(sampleID: String = null): Map[String, LibraryOutput] = {
var output: Map[String, LibraryOutput] = Map()
for (libraryID <- getLibraries(sampleID)) {
currentLibrary = Some(libraryID)
output += libraryID -> runSingleLibraryJobs(sampleID, libraryID)
currentLibrary = None
def getSampleDir: String = {
outputDir + "samples" + File.pathSeparator + sampleId + File.pathSeparator
}
return output
}
def runSingleLibraryJobs(sampleID: String, libraryID: String): LibraryOutput
protected var currentSample: Option[String] = None
protected var currentLibrary: Option[String] = None
/**
* Set current sample manual, only use this when not using runSamplesJobs method
* @param sample
*/
def setCurrentSample(sample: String) {
logger.debug("Manual sample set to: " + sample)
currentSample = Some(sample)
}
/**
* Gets current sample
* @return current sample
*/
def getCurrentSample = currentSample
/**
* Reset current sample manual, only use this when not using runSamplesJobs method
*/
def resetCurrentSample() {
logger.debug("Manual sample reset")
currentSample = None
}
val samples: Map[String, Sample]
/**
* Set current library manual, only use this when not using runLibraryJobs method
* @param library
*/
def setCurrentLibrary(library: String) {
logger.debug("Manual library set to: " + library)
currentLibrary = Some(library)
/** 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
}
/**
* Gets current library
* @return current library
*/
def getCurrentLibrary = currentLibrary
/** Reset current library manual, only use this when not using runLibraryJobs method */
def resetCurrentLibrary() {
logger.debug("Manual library reset")
currentLibrary = None
/** Runs runSingleSampleJobs method for each sample */
final def runSamplesJobs() {
for ((sampleId, sample) <- samples) {
sample.run()
}
}
private var currentSample: Option[String] = None
private var currentLibrary: Option[String] = None
override protected[core] def configFullPath: List[String] = {
val s = currentSample match {
case Some(s) => "samples" :: s :: Nil
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment