BiopetQScript.scala 3.81 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
18
package nl.lumc.sasc.biopet.core

import java.io.File
Peter van 't Hof's avatar
Peter van 't Hof committed
19
import java.io.PrintWriter
20
import nl.lumc.sasc.biopet.core.config.{ ConfigValueIndex, Config, Configurable }
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import org.broadinstitute.gatk.utils.commandline.Argument
Peter van 't Hof's avatar
Peter van 't Hof committed
22
23
import org.broadinstitute.gatk.queue.QSettings
import org.broadinstitute.gatk.queue.function.QFunction
24
import org.broadinstitute.gatk.queue.function.scattergather.ScatterGatherableFunction
25
import org.broadinstitute.gatk.queue.util.{ Logging => GatkLogging }
26

27
28
import scala.collection.mutable.ListBuffer

29
trait BiopetQScript extends Configurable with GatkLogging {
30

31
  @Argument(doc = "JSON config file(s)", fullName = "config_file", shortName = "config", required = false)
32
  val configfiles: List[File] = Nil
bow's avatar
bow committed
33

34
35
  var outputDir: String = {
    val temp = Config.getValueFromMap(Config.global.map, ConfigValueIndex(this.configName, configPath, "output_dir"))
36
    if (temp.isEmpty) ""
37
38
39
40
41
    else {
      val t = temp.get.value.toString
      if (!t.endsWith("/")) t + "/" else t
    }
  }
bow's avatar
bow committed
42

43
  @Argument(doc = "Disable all scatters", shortName = "DSC", required = false)
44
  var disableScatter: Boolean = false
45

bow's avatar
bow committed
46
47
  var outputFiles: Map[String, File] = Map()

Peter van 't Hof's avatar
Peter van 't Hof committed
48
  var qSettings: QSettings
bow's avatar
bow committed
49

50
51
  def init
  def biopetScript
bow's avatar
bow committed
52

53
  var functions: Seq[QFunction]
Peter van 't Hof's avatar
Peter van 't Hof committed
54

55
  final def script() {
56
    outputDir = config("output_dir")
57
58
    if (outputDir.isEmpty) outputDir = new File(".").getAbsolutePath()
    else if (!outputDir.endsWith("/")) outputDir += "/"
59
60
    init
    biopetScript
61

62
    if (disableScatter) for (function <- functions) function match {
63
64
65
      case f: ScatterGatherableFunction => f.scatterCount = 1
      case _                            =>
    }
66
    for (function <- functions) function match {
67
68
69
70
      case f: BiopetCommandLineFunctionTrait => {
        f.checkExecutable
        f.afterGraph
      }
Peter van 't Hof's avatar
Peter van 't Hof committed
71
      case _                                 =>
72
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
73

74
75
    if (new File(outputDir).canWrite) Config.global.writeReport(qSettings.runName, outputDir + ".log/" + qSettings.runName)
    else BiopetQScript.addError("Output dir: '" + outputDir + "' is not writeable")
76
77

    BiopetQScript.checkErrors
78
  }
bow's avatar
bow committed
79

80
  def add(functions: QFunction*) // Gets implemeted at org.broadinstitute.sting.queue.QScript
bow's avatar
bow committed
81
  def add(function: QFunction, isIntermediate: Boolean = false) {
82
83
84
    function.isIntermediate = isIntermediate
    add(function)
  }
85
}
86
87
88
89

object BiopetQScript extends Logging {
  private val errors: ListBuffer[Exception] = ListBuffer()

90
91
  def addError(error: String, debug: String = null): Unit = {
    val msg = error + (if (debug != null && logger.isDebugEnabled) "; " + debug else "")
92
93
94
95
96
    errors.append(new Exception(msg))
  }

  protected def checkErrors: Unit = {
    if (!errors.isEmpty) {
97
98
99
100
101
102
103
104
105
106
      logger.error("*************************")
      logger.error("Biopet found some errors:")
      if (logger.isDebugEnabled) {
        for (e <- errors) {
          logger.error(e.getMessage)
          logger.debug(e.getStackTrace.mkString("Stack trace:\n", "\n", "\n"))
        }
      } else {
        val set = errors.map(_.getMessage).toSet
        set.toList.sorted.foreach(logger.error(_))
107
108
109
110
111
      }
      throw new IllegalStateException("Biopet found errors")
    }
  }
}