BiopetCommandLineFunctionTrait.scala 4.39 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
16

trait BiopetCommandLineFunctionTrait extends CommandLineFunction with Configurable {
  analysisName = getClass.getSimpleName
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
29

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

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

35
36
  protected def afterGraph {
  }
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
bow's avatar
bow committed
41
42
    jobOutputFile = new File(firstOutput.getParent + "/." + firstOutput.getName + "." + analysisName + ".out")

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
51
    }
    if (vmem != null) jobResourceRequests :+= "h_vmem=" + vmem
    jobName = this.analysisName + ":" + 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
69
      }
    } catch {
Peter van 't Hof's avatar
Peter van 't Hof committed
70
      case ioe: java.io.IOException => logger.warn("Could not use 'which', check on executable skipped: " + ioe)
71
    }
72
73
74
75
76
77
78
79
80
    
    val is = new FileInputStream(executable)
    val cnt = is.available
    val bytes = Array.ofDim[Byte](cnt)
    is.read(bytes)
    is.close()
    val md5: String =  MessageDigest.getInstance("MD5").digest(bytes).map("%02X".format(_)).mkString.toLowerCase
    
    addJobReportBinding("md5sum_exe", md5)
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
bow's avatar
bow committed
95
  def getVersion: 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
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
117
118
119

  def getThreads(default: Int): Int = {
    val maxThreads: Int = config("maxthreads", default = 8)
    val threads: Int = config("threads", default = default)
120
121
122
    if (maxThreads > threads) return threads
    else return maxThreads
  }
bow's avatar
bow committed
123
124
125
126

  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)
127
128
129
130
    if (maxThreads > threads) return threads
    else return maxThreads
  }
}