PythonCommandLineFunction.scala 2.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/**
 * 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
 *
11
 * A dual licensing mode is applied. The source code within this project is freely available for non-commercial use under an AGPL
12
13
14
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
15
package nl.lumc.sasc.biopet.core.extensions
16

17
import java.io.{ File, FileOutputStream }
Peter van 't Hof's avatar
Peter van 't Hof committed
18

Peter van 't Hof's avatar
Peter van 't Hof committed
19
import nl.lumc.sasc.biopet.core.BiopetCommandLineFunction
Peter van 't Hof's avatar
Peter van 't Hof committed
20
import nl.lumc.sasc.biopet.utils.Logging
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import org.broadinstitute.gatk.utils.commandline.Input
22
23

trait PythonCommandLineFunction extends BiopetCommandLineFunction {
bow's avatar
bow committed
24
  @Input(doc = "Python script", required = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
25
  var pythonScript: File = _
bow's avatar
bow committed
26

Sander Bollen's avatar
Sander Bollen committed
27
  executable = config("exe", default = "python", namespace = "python", freeVar = false)
bow's avatar
bow committed
28

Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
29
  protected var pythonScriptName: String = _
Peter van 't Hof's avatar
Peter van 't Hof committed
30
31
32
33
34

  /**
   * checks if script already exist in jar otherwise try to fetch from the jar
   * @param script name / location of script
   */
Wai Yi Leung's avatar
Wai Yi Leung committed
35
  def setPythonScript(script: String) {
36
    pythonScript = new File(script).getAbsoluteFile
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
37
    if (!pythonScript.exists()) {
Wai Yi Leung's avatar
Wai Yi Leung committed
38
39
      setPythonScript(script, "")
    } else {
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
40
      pythonScriptName = script
Wai Yi Leung's avatar
Wai Yi Leung committed
41
42
    }
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
43
44
45
46
47
48

  /**
   * Set and extract python script from jar file
   * @param script name of script in jar
   * @param subpackage location of script in jar
   */
bow's avatar
bow committed
49
  def setPythonScript(script: String, subpackage: String) {
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
50
    pythonScriptName = script
51
    pythonScript = new File(".queue/tmp/" + subpackage + pythonScriptName).getAbsoluteFile
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
52
53
    if (!pythonScript.getParentFile.exists) pythonScript.getParentFile.mkdirs
    val is = getClass.getResourceAsStream(subpackage + pythonScriptName)
Peter van 't Hof's avatar
Peter van 't Hof committed
54
55
56
57
58
    if (is != null) {
      val os = new FileOutputStream(pythonScript)
      org.apache.commons.io.IOUtils.copy(is, os)
      os.close()
    } else Logging.addError(s"Python script not found: $pythonScriptName")
59
  }
bow's avatar
bow committed
60

Peter van 't Hof's avatar
Peter van 't Hof committed
61
  /** return basic command to prefix the complete command with */
Peter van 't Hof's avatar
Peter van 't Hof committed
62
  def getPythonCommand: String = {
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
63
    required(executable) + required(pythonScript)
64
65
  }
}