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

18
import java.io.{File, FileOutputStream}
Wai Yi Leung's avatar
Wai Yi Leung committed
19
20

import nl.lumc.sasc.biopet.core.BiopetCommandLineFunction
Peter van 't Hof's avatar
Peter van 't Hof committed
21

Peter van 't Hof's avatar
Peter van 't Hof committed
22
import scala.sys.process._
Wai Yi Leung's avatar
Wai Yi Leung committed
23
24

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
25
26
 * General rscript extension
 *
Wai Yi Leung's avatar
Wai Yi Leung committed
27
28
29
30
 * Created by wyleung on 17-2-15.
 */
trait RscriptCommandLineFunction extends BiopetCommandLineFunction {

Peter van 't Hof's avatar
Peter van 't Hof committed
31
  protected var script: File
Wai Yi Leung's avatar
Wai Yi Leung committed
32

Peter van 't Hof's avatar
Peter van 't Hof committed
33
  executable = config("exe", default = "Rscript", submodule = "Rscript")
Wai Yi Leung's avatar
Wai Yi Leung committed
34

Peter van 't Hof's avatar
Peter van 't Hof committed
35
  override def beforeGraph(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
36
    checkScript()
Wai Yi Leung's avatar
Wai Yi Leung committed
37
38
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
39
40
41
42
43
  /**
   * If script not exist in file system it try to copy it from the jar
   * @param local if true it use File.createTempFile instead of ".queue/tmp/"
   */
  protected def checkScript(local: Boolean = false): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
44
45
46
47
48
49
    if (script.exists()) {
      script = script.getAbsoluteFile
    } else {
      val rScript: File = {
        if (local) File.createTempFile(script.getName, ".R")
        else new File(".queue/tmp/" + script)
Wai Yi Leung's avatar
Wai Yi Leung committed
50
      }
Peter van 't Hof's avatar
Peter van 't Hof committed
51
      if (!rScript.getParentFile.exists) rScript.getParentFile.mkdirs
Wai Yi Leung's avatar
Wai Yi Leung committed
52

Peter van 't Hof's avatar
Peter van 't Hof committed
53
54
      val is = getClass.getResourceAsStream(script.getPath)
      val os = new FileOutputStream(rScript)
Wai Yi Leung's avatar
Wai Yi Leung committed
55

Peter van 't Hof's avatar
Peter van 't Hof committed
56
57
      org.apache.commons.io.IOUtils.copy(is, os)
      os.close()
Wai Yi Leung's avatar
Wai Yi Leung committed
58

Peter van 't Hof's avatar
Peter van 't Hof committed
59
60
61
      script = rScript
    }
  }
Wai Yi Leung's avatar
Wai Yi Leung committed
62

Peter van 't Hof's avatar
Peter van 't Hof committed
63
64
65
66
  /**
   * Execute rscript on local system
   * @param logger How to handle stdout and stderr
   */
Peter van 't Hof's avatar
Peter van 't Hof committed
67
68
  def runLocal(logger: ProcessLogger): Unit = {
    checkScript(local = true)
Wai Yi Leung's avatar
Wai Yi Leung committed
69

70
71
72
73
74
75
76
77
    this.logger.info(cmdLine)

    val cmd = cmdLine.stripPrefix(" '").stripSuffix("' ").split("' *'")

    this.logger.info(cmd.mkString(" "))

    val process = Process(cmd.toSeq).run(logger)
    this.logger.info(process.exitValue())
Wai Yi Leung's avatar
Wai Yi Leung committed
78
79
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
80
81
82
83
84
85
86
87
  /**
   * Execute rscript on local system
   * Stdout and stderr will go to biopet logger
   */
  def runLocal(): Unit = {
    runLocal(ProcessLogger(logger.info(_)))
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
88
  def cmdLine: String = {
Wai Yi Leung's avatar
Wai Yi Leung committed
89
    required(executable) +
Peter van 't Hof's avatar
Peter van 't Hof committed
90
      required(script)
Wai Yi Leung's avatar
Wai Yi Leung committed
91
92
  }
}