BiopetCommandLineFunctionTrait.scala 4.82 KB
Newer Older
1
2
package nl.lumc.sasc.biopet.core

3
//import java.io.BufferedInputStream
4
import java.io.File
Peter van 't Hof's avatar
Peter van 't Hof committed
5
import nl.lumc.sasc.biopet.core.config.Configurable
Peter van 't Hof's avatar
Peter van 't Hof committed
6
7
import org.broadinstitute.gatk.queue.QException
import org.broadinstitute.gatk.queue.function.CommandLineFunction
8
import org.broadinstitute.gatk.utils.commandline.{ Input, Argument }
9
//import scala.io.Source
10
import scala.sys.process.{ Process, ProcessLogger }
11
import scala.util.matching.Regex
12
13
import java.io.FileInputStream
import java.security.MessageDigest
14
15

trait BiopetCommandLineFunctionTrait extends CommandLineFunction with Configurable {
Peter van 't Hof's avatar
Peter van 't Hof committed
16
  analysisName = configName
bow's avatar
bow committed
17
18

  @Input(doc = "deps", required = false)
19
  var deps: List[File] = Nil
bow's avatar
bow committed
20
21

  @Argument(doc = "Threads", required = false)
22
23
  var threads = 0
  val defaultThreads = 1
bow's avatar
bow committed
24
25

  @Argument(doc = "Vmem", required = false)
26
27
  var vmem: String = _
  val defaultVmem: String = ""
bow's avatar
bow committed
28

Peter van 't Hof's avatar
Peter van 't Hof committed
29
  @Argument(doc = "Executable", required = false)
Peter van 't Hof's avatar
Peter van 't Hof committed
30
  var executable: String = _
bow's avatar
bow committed
31

32
  protected[core] def beforeCmd {
33
  }
bow's avatar
bow committed
34

35
  protected[core] def afterGraph {
36
  }
bow's avatar
bow committed
37

38
  override def freezeFieldValues() {
Peter van 't Hof's avatar
Peter van 't Hof committed
39
    checkExecutable
40
    afterGraph
Peter van 't Hof's avatar
Peter van 't Hof committed
41
    jobOutputFile = new File(firstOutput.getParent + "/." + firstOutput.getName + "." + configName + ".out")
bow's avatar
bow committed
42

43
44
    if (threads == 0) threads = getThreads(defaultThreads)
    if (threads > 1) nCoresRequest = Option(threads)
bow's avatar
bow committed
45

46
    if (vmem == null) {
Peter van 't Hof's avatar
Peter van 't Hof committed
47
48
      vmem = config("vmem")
      if (vmem == null && !defaultVmem.isEmpty) vmem = defaultVmem
49
50
    }
    if (vmem != null) jobResourceRequests :+= "h_vmem=" + vmem
Peter van 't Hof's avatar
Peter van 't Hof committed
51
    jobName = configName + ":" + firstOutput.getName
bow's avatar
bow committed
52

53
54
    super.freezeFieldValues()
  }
bow's avatar
bow committed
55

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

Peter van 't Hof's avatar
Peter van 't Hof committed
70
71
72
73
74
      val is = new FileInputStream(executable)
      val cnt = is.available
      val bytes = Array.ofDim[Byte](cnt)
      is.read(bytes)
      is.close()
Peter van 't Hof's avatar
Peter van 't Hof committed
75
      val md5: String = MessageDigest.getInstance("MD5").digest(bytes).map("%02X".format(_)).mkString.toLowerCase
Peter van 't Hof's avatar
Peter van 't Hof committed
76
77

      addJobReportBinding("md5sum_exe", md5)
78
    } catch {
Peter van 't Hof's avatar
Peter van 't Hof committed
79
      case ioe: java.io.IOException => logger.warn("Could not use 'which', check on executable skipped: " + ioe)
80
81
    }
  }
bow's avatar
bow committed
82

83
  final protected def preCmdInternal {
Peter van 't Hof's avatar
Peter van 't Hof committed
84
    checkExecutable
bow's avatar
bow committed
85

86
    beforeCmd
bow's avatar
bow committed
87

88
    addJobReportBinding("cores", if (nCoresRequest.get.toInt > 0) nCoresRequest.get.toInt else 1)
89
    addJobReportBinding("version", getVersion)
90
  }
bow's avatar
bow committed
91

92
  protected def versionCommand: String = null
93
  protected val versionRegex: Regex = null
94
  protected val versionExitcode = List(0) // Can select multiple
Peter van 't Hof's avatar
Peter van 't Hof committed
95
  private def getVersionInternal: String = {
96
    if (versionCommand == null || versionRegex == null) return "N/A"
97
98
    val stdout = new StringBuffer()
    val stderr = new StringBuffer()
99
100
101
102
    def outputLog = "Version command: \n" + versionCommand +
      "\n output log: \n stdout: \n" + stdout.toString +
      "\n stderr: \n" + stderr.toString
    val process = Process(versionCommand).run(ProcessLogger(stdout append _ + "\n", stderr append _ + "\n"))
103
    if (!versionExitcode.contains(process.exitValue)) {
104
      logger.warn("getVersion give exit code " + process.exitValue + ", version not found \n" + outputLog)
105
106
      return "N/A"
    }
107
    for (line <- stdout.toString.split("\n") ++ stderr.toString.split("\n")) {
bow's avatar
bow committed
108
      line match {
109
        case versionRegex(m) => return m
Peter van 't Hof's avatar
Peter van 't Hof committed
110
        case _               =>
111
112
      }
    }
113
    logger.warn("getVersion give a exit code " + process.exitValue + " but no version was found, executable correct? \n" + outputLog)
114
115
    return "N/A"
  }
bow's avatar
bow committed
116

Peter van 't Hof's avatar
Peter van 't Hof committed
117
118
119
120
121
122
  def getVersion: String = {
    if (!BiopetCommandLineFunctionTrait.versionCache.contains(executable))
      BiopetCommandLineFunctionTrait.versionCache += executable -> getVersionInternal
    return BiopetCommandLineFunctionTrait.versionCache(executable)
  }

bow's avatar
bow committed
123
124
125
  def getThreads(default: Int): Int = {
    val maxThreads: Int = config("maxthreads", default = 8)
    val threads: Int = config("threads", default = default)
126
127
128
    if (maxThreads > threads) return threads
    else return maxThreads
  }
bow's avatar
bow committed
129
130
131
132

  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)
133
134
135
136
    if (maxThreads > threads) return threads
    else return maxThreads
  }
}
Peter van 't Hof's avatar
Peter van 't Hof committed
137
138
139
140
141

object BiopetCommandLineFunctionTrait {
  import scala.collection.mutable.Map
  private val versionCache: Map[String, String] = Map()
}