BiopetCommandLineFunctionTrait.scala 3.92 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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import scala.sys.process._
import scala.util.matching.Regex

trait BiopetCommandLineFunctionTrait extends CommandLineFunction with Configurable {
  analysisName = getClass.getSimpleName
  
  @Input(doc="deps", required=false)
  var deps: List[File] = Nil
  
  @Argument(doc="Threads", required=false)
  var threads = 0
  val defaultThreads = 1
  
  @Argument(doc="Vmem", required=false)
  var vmem: String = _
  val defaultVmem: String = ""
  
  @Argument(doc="Executeble")
  var executeble: String = _
  
  protected def beforeCmd {
  }
  
  protected def afterGraph {
  }
  
  override def freezeFieldValues() {
    checkExecuteble
    afterGraph
    jobOutputFile = new File(firstOutput.getParent + "/."  + firstOutput.getName + "." + analysisName + ".out")
    
39
40
41
42
43
44
45
46
47
48
    if (threads == 0) threads = getThreads(defaultThreads)
    if (threads > 1) nCoresRequest = Option(threads)
    
    if (vmem == null) {
      if (configContains("vmem")) vmem = config("vmem")
      else if (!defaultVmem.isEmpty) vmem = defaultVmem
    }
    if (vmem != null) jobResourceRequests :+= "h_vmem=" + vmem
    jobName = this.analysisName + ":" + firstOutput.getName
    
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    super.freezeFieldValues()
  }
  
  protected def checkExecuteble {
    try if (executeble != null) {
      val buffer = new StringBuffer()
      val cmd = Seq("which", executeble)
      val process = Process(cmd).run(ProcessLogger(buffer.append(_)))
      if (process.exitValue == 0) {
        executeble = buffer.toString
        val file = new File(executeble)
        executeble = file.getCanonicalPath
      } else {
        logger.error("executeble: '" + executeble + "' not found, please check config")
        throw new QException("executeble: '" + executeble + "' not found, please check config")
      }
    } catch {
      case ioe: java.io.IOException => logger.warn("Could not use 'which', check on executeble skipped: " + ioe)
    }
  }
  
  final protected def preCmdInternal {
    checkExecuteble
    //for (input <- this.inputs) if (!input.exists) throw new IllegalStateException("Input: " + input + " for " + analysisName + " is missing")
    //logger.debug("Config for " + analysisName + ": " + localConfig)
    
    beforeCmd
    
    addJobReportBinding("cores", if (nCoresRequest.get.toInt > 0) nCoresRequest.get.toInt else 1)
78
    addJobReportBinding("version", getVersion)
79
80
  }
  
81
  protected def versionCommand: String = null
82
  protected val versionRegex: Regex = null
83
  protected val versionExitcode = List(0) // Can select multiple
84
85
86
87
  def getVersion : String = {
    if (versionCommand == null || versionRegex == null) return "N/A"
    val buffer = new StringBuffer()
    val process = Process(versionCommand).run(ProcessLogger(buffer append _))
88
    if (!versionExitcode.contains(process.exitValue)) {
89
90
91
92
93
94
95
96
97
98
      logger.warn("Version command: '" + versionCommand + "' give exit code " + process.exitValue + ", version not found")
      return "N/A"
    }
    val lines = versionCommand lines_! ProcessLogger(buffer append _)
    for (line <- lines) {
      line match { 
        case versionRegex(m) => return m
        case _ =>
      }
    }
99
    logger.warn("Version command: '" + versionCommand + "' give a exit code " + process.exitValue + " but no version was found, executeble oke?")
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    return "N/A"
  }
  
  def getThreads(default:Int) : Int = {
    val maxThreads: Int = config("maxthreads", 8)
    val threads: Int = config("threads", default)
    if (maxThreads > threads) return threads
    else return maxThreads
  }
  
  def getThreads(default:Int, module:String) : Int = {
    val maxThreads: Int = config("maxthreads", 8, module)
    val threads: Int = config("threads", default, module)
    if (maxThreads > threads) return threads
    else return maxThreads
  }
}