BiopetExecutable.scala 4.44 KB
Newer Older
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
17
package nl.lumc.sasc.biopet.core

Peter van 't Hof's avatar
Peter van 't Hof committed
18
import java.io.{ PrintWriter, StringWriter }
Peter van 't Hof's avatar
Peter van 't Hof committed
19
import scala.io.Source
20

bow's avatar
bow committed
21
22
23
24
25
import org.apache.log4j.Logger

import nl.lumc.sasc.biopet.{ FullVersion, LastCommitHash }
import nl.lumc.sasc.biopet.core.BiopetExecutable._

26
trait BiopetExecutable extends Logging {
27

28
  def pipelines: List[MainCommand]
29

30
  def tools: List[MainCommand]
31

Peter van 't Hof's avatar
Peter van 't Hof committed
32
  val modules: Map[String, List[MainCommand]] = Map(
33
34
    "pipeline" -> pipelines,
    "tool" -> tools
Peter van 't Hof's avatar
Peter van 't Hof committed
35
  )
Peter van 't Hof's avatar
Peter van 't Hof committed
36

37
38
39
40
  /**
   * @param args the command line arguments
   */
  def main(args: Array[String]): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
41
    checkDirtyBuild(logger)
bow's avatar
bow committed
42

bow's avatar
bow committed
43
    def toBulletedList(m: List[MainCommand], kind: String = "", bullet: String = "-") =
44
      "Available %s(s):\n  ".format(kind) + bullet + " " + m.map(x => x.commandName).sorted.mkString("\n  " + bullet + " ")
Peter van 't Hof's avatar
Peter van 't Hof committed
45

wyleung's avatar
wyleung committed
46
    def usage(module: String = null): String = {
Peter van 't Hof's avatar
Peter van 't Hof committed
47
48
49
50
51
52
      if (module != null) checkModule(module)
      val usage: String = {
        val set = if (module == null) modules.keySet else Set(module)
        val u = for (m <- set) yield toBulletedList(modules(m), m)
        u.mkString("\n\n")
      }
bow's avatar
bow committed
53
      """
bow's avatar
bow committed
54
        |Usage   : java -jar <path/to/biopet.jar> {%s} <name> [args]
55
        |Version : %s
bow's avatar
bow committed
56
57
58
        |
        |%s
        |
59
60
        |Subcommands:
        |  - version
Peter van 't Hof's avatar
Peter van 't Hof committed
61
        |  - license
62
        |
Peter van 't Hof's avatar
Peter van 't Hof committed
63
        |Questions or comments? Email sasc@lumc.nl or check out the project page at https://git.lumc.nl/biopet/biopet.git
bow's avatar
bow committed
64
      """.stripMargin.format(modules.keys.mkString(","), FullVersion, usage)
65
    }
bow's avatar
bow committed
66

wyleung's avatar
wyleung committed
67
    def checkModule(module: String) {
Peter van 't Hof's avatar
Peter van 't Hof committed
68
      if (!modules.contains(module)) {
wyleung's avatar
wyleung committed
69
70
        System.err.println(s"ERROR: module '$module' does not exist\n" + usage())
        System.exit(1)
bow's avatar
bow committed
71
      }
72
    }
wyleung's avatar
wyleung committed
73
74

    def getCommand(module: String, name: String): MainCommand = {
Peter van 't Hof's avatar
Peter van 't Hof committed
75
      checkModule(module)
Peter van 't Hof's avatar
Peter van 't Hof committed
76
      val command = modules(module).find(p => p.commandName.toLowerCase == name.toLowerCase)
Peter van 't Hof's avatar
Peter van 't Hof committed
77
78
      if (command == None) {
        System.err.println(s"ERROR: command '$name' does not exist in module '$module'\n" + usage(module))
bow's avatar
bow committed
79
        System.exit(1)
Peter van 't Hof's avatar
Peter van 't Hof committed
80
      }
Peter van 't Hof's avatar
Peter van 't Hof committed
81
      command.get
Peter van 't Hof's avatar
Peter van 't Hof committed
82
    }
wyleung's avatar
wyleung committed
83

Peter van 't Hof's avatar
Peter van 't Hof committed
84
    args match {
85
      case Array("version") => {
bow's avatar
bow committed
86
        println("version: " + FullVersion)
87
      }
Peter van 't Hof's avatar
Peter van 't Hof committed
88
89
90
      case Array("license") => {
        println(getLicense)
      }
Peter van 't Hof's avatar
Peter van 't Hof committed
91
      case Array(module, name, passArgs @ _*) => {
Peter van 't Hof's avatar
Peter van 't Hof committed
92
93
94
95
96
97
98
99
100
101
102
        try {
          getCommand(module, name).main(passArgs.toArray)
        } catch {
          case e: Exception => {
            val sWriter = new StringWriter()
            val pWriter = new PrintWriter(sWriter)
            e.printStackTrace(pWriter)
            pWriter.close()
            val trace = (sWriter.toString.split("\n"))

            if (!logger.isDebugEnabled) {
103
              trace.filterNot(_.startsWith("\tat")).foreach(logger.error(_))
Peter van 't Hof's avatar
Peter van 't Hof committed
104
105
106
107
108
109
110
              logger.error("For more info please run with -l debug")
            } else {
              trace.foreach(logger.debug(_))
            }
            sys.exit(1)
          }
        }
Peter van 't Hof's avatar
Peter van 't Hof committed
111
112
      }
      case Array(module) => {
wyleung's avatar
wyleung committed
113
        System.err.println(usage(module))
Peter van 't Hof's avatar
Peter van 't Hof committed
114
        sys.exit(1)
Peter van 't Hof's avatar
Peter van 't Hof committed
115
116
      }
      case _ => {
wyleung's avatar
wyleung committed
117
        System.err.println(usage())
Peter van 't Hof's avatar
Peter van 't Hof committed
118
        sys.exit(1)
Peter van 't Hof's avatar
Peter van 't Hof committed
119
      }
120
121
    }
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
122

bow's avatar
bow committed
123
124
  def checkDirtyBuild(logger: Logger): Unit =
    if (LastCommitHash.endsWith("-dirty")) {
bow's avatar
bow committed
125
126
127
128
      logger.warn("***********************************************************")
      logger.warn("* This JAR was built while there were uncommited changes. *")
      logger.warn("* Reproducible results are *not* guaranteed.              *")
      logger.warn("***********************************************************")
129
    }
130
}
131
132

object BiopetExecutable {
Peter van 't Hof's avatar
Peter van 't Hof committed
133
134
135
136
  def getLicense: String = {
    val stream = getClass.getClassLoader.getResourceAsStream("nl/lumc/sasc/biopet/License.txt")
    Source.fromInputStream(stream).getLines().mkString("\n", "\n", "\n")
  }
137
}