BiopetCommandLineFunctionTrait.scala 4.12 KB
Newer Older
1
2
3
4
package nl.lumc.sasc.biopet.core

import java.io.File
import nl.lumc.sasc.biopet.core.config._
Peter van 't Hof's avatar
Peter van 't Hof committed
5
6
7
import org.broadinstitute.gatk.queue.QException
import org.broadinstitute.gatk.queue.function.CommandLineFunction
import org.broadinstitute.gatk.utils.commandline._
8
9
10
11
12
import scala.sys.process._
import scala.util.matching.Regex

trait BiopetCommandLineFunctionTrait extends CommandLineFunction with Configurable {
  analysisName = getClass.getSimpleName
bow's avatar
bow committed
13
14

  @Input(doc = "deps", required = false)
15
  var deps: List[File] = Nil
bow's avatar
bow committed
16
17

  @Argument(doc = "Threads", required = false)
18
19
  var threads = 0
  val defaultThreads = 1
bow's avatar
bow committed
20
21

  @Argument(doc = "Vmem", required = false)
22
23
  var vmem: String = _
  val defaultVmem: String = ""
bow's avatar
bow committed
24
25

  @Argument(doc = "Executable")
Peter van 't Hof's avatar
Peter van 't Hof committed
26
  var executable: String = _
bow's avatar
bow committed
27

28
29
  protected def beforeCmd {
  }
bow's avatar
bow committed
30

31
32
  protected def afterGraph {
  }
bow's avatar
bow committed
33

34
  override def freezeFieldValues() {
Peter van 't Hof's avatar
Peter van 't Hof committed
35
    checkExecutable
36
    afterGraph
bow's avatar
bow committed
37
38
    jobOutputFile = new File(firstOutput.getParent + "/." + firstOutput.getName + "." + analysisName + ".out")

39
40
    if (threads == 0) threads = getThreads(defaultThreads)
    if (threads > 1) nCoresRequest = Option(threads)
bow's avatar
bow committed
41

42
    if (vmem == null) {
Peter van 't Hof's avatar
Peter van 't Hof committed
43
44
      vmem = config("vmem")
      if (vmem == null && !defaultVmem.isEmpty) vmem = defaultVmem
45
46
47
    }
    if (vmem != null) jobResourceRequests :+= "h_vmem=" + vmem
    jobName = this.analysisName + ":" + firstOutput.getName
bow's avatar
bow committed
48

49
50
    super.freezeFieldValues()
  }
bow's avatar
bow committed
51

Peter van 't Hof's avatar
Peter van 't Hof committed
52
53
  protected def checkExecutable {
    try if (executable != null) {
54
      val buffer = new StringBuffer()
Peter van 't Hof's avatar
Peter van 't Hof committed
55
      val cmd = Seq("which", executable)
56
57
      val process = Process(cmd).run(ProcessLogger(buffer.append(_)))
      if (process.exitValue == 0) {
Peter van 't Hof's avatar
Peter van 't Hof committed
58
59
60
        executable = buffer.toString
        val file = new File(executable)
        executable = file.getCanonicalPath
61
      } else {
Peter van 't Hof's avatar
Peter van 't Hof committed
62
63
        logger.error("executable: '" + executable + "' not found, please check config")
        throw new QException("executable: '" + executable + "' not found, please check config")
64
65
      }
    } catch {
Peter van 't Hof's avatar
Peter van 't Hof committed
66
      case ioe: java.io.IOException => logger.warn("Could not use 'which', check on executable skipped: " + ioe)
67
68
    }
  }
bow's avatar
bow committed
69

70
  final protected def preCmdInternal {
Peter van 't Hof's avatar
Peter van 't Hof committed
71
    checkExecutable
72
73
    //for (input <- this.inputs) if (!input.exists) throw new IllegalStateException("Input: " + input + " for " + analysisName + " is missing")
    //logger.debug("Config for " + analysisName + ": " + localConfig)
bow's avatar
bow committed
74

75
    beforeCmd
bow's avatar
bow committed
76

77
    addJobReportBinding("cores", if (nCoresRequest.get.toInt > 0) nCoresRequest.get.toInt else 1)
78
    addJobReportBinding("version", getVersion)
79
  }
bow's avatar
bow committed
80

81
  protected def versionCommand: String = null
82
  protected val versionRegex: Regex = null
83
  protected val versionExitcode = List(0) // Can select multiple
bow's avatar
bow committed
84
  def getVersion: String = {
85
    if (versionCommand == null || versionRegex == null) return "N/A"
86
87
88
89
    val stdout = new StringBuffer()
    val stderr = new StringBuffer()
    def outputLog = "\n output log: \n stdout: \n" + stdout.toString + "\n stderr: \n" + stderr.toString
    val process = Process(versionCommand).run(ProcessLogger(stdout append _, stderr append _))
90
    if (!versionExitcode.contains(process.exitValue)) {
91
      logger.warn("Version command: '" + versionCommand + "' give exit code " + process.exitValue + ", version not found" + outputLog)
92
93
      return "N/A"
    }
94
    for (line <- stdout.toString.split("\n") ++ stderr.toString.split("\n")) {
bow's avatar
bow committed
95
      line match {
96
        case versionRegex(m) => return m
bow's avatar
bow committed
97
        case _               =>
98
99
      }
    }
100
    logger.warn("Version command: '" + versionCommand + "' give a exit code " + process.exitValue + " but no version was found, executable correct?" + outputLog)
101
102
    return "N/A"
  }
bow's avatar
bow committed
103
104
105
106

  def getThreads(default: Int): Int = {
    val maxThreads: Int = config("maxthreads", default = 8)
    val threads: Int = config("threads", default = default)
107
108
109
    if (maxThreads > threads) return threads
    else return maxThreads
  }
bow's avatar
bow committed
110
111
112
113

  def getThreads(default: Int, module: String): Int = {
    val maxThreads: Int = config("maxthreads", default = 8, submodule = module)
    val threads: Int = config("threads", default = default, submodule = module)
114
115
116
117
    if (maxThreads > threads) return threads
    else return maxThreads
  }
}