From 9e5f2608cb8cfb76e5f1f54da57710ba38a78e2a Mon Sep 17 00:00:00 2001
From: bow <bow@bow.web.id>
Date: Tue, 7 Oct 2014 00:42:18 +0200
Subject: [PATCH] Improve main command line interface

---
 .../sasc/biopet/core/BiopetExecutable.scala   | 81 ++++++++++++++++---
 .../sasc/biopet/core/PipelineCommand.scala    |  3 +-
 .../lumc/sasc/biopet/core/ToolCommand.scala   | 12 +++
 3 files changed, 81 insertions(+), 15 deletions(-)
 create mode 100644 biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/ToolCommand.scala

diff --git a/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/BiopetExecutable.scala b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/BiopetExecutable.scala
index f1cd08b71..b71c87f12 100644
--- a/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/BiopetExecutable.scala
+++ b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/BiopetExecutable.scala
@@ -15,7 +15,7 @@ import nl.lumc.sasc.biopet.pipelines.sage.Sage
 
 object BiopetExecutable {
 
-  val pipelines: Map[String,PipelineCommand] = Map(
+  val pipelines: Map[String, PipelineCommand] = Map(
       "flexiprep" -> Flexiprep,
       "mapping" -> Mapping,
       "gentrap" -> Gentrap,
@@ -29,25 +29,80 @@ object BiopetExecutable {
       "sage" -> Sage,
       "basty" -> Basty
     )
-   
+
+  val tools: Map[String, ToolCommand] = Map(
+    )
+
   /**
    * @param args the command line arguments
    */
   def main(args: Array[String]): Unit = {
+
+    def toBulletedList(m: Map[String, Any], kind: String = "", bullet: String = "-") =
+      "Available %s:\n  ".format(kind) + bullet + " " +
+        m.keys.toVector.sorted.mkString("\n  " + bullet + " ")
+
+    lazy val pipelineList: String = toBulletedList(pipelines, "pipelines")
+
+    lazy val toolList: String = toBulletedList(tools, "tools")
+
+    lazy val addendum: String =
+      """Questions or comments? Email sasc@lumc.nl or check out the project page at https://git.lumc.nl/biopet/biopet.git"""".stripMargin
+
+    lazy val baseUsage: String =
+      """
+        |Usage: java -jar BiopetFramework.jar {pipeline,tool} {pipeline/tool name} {pipeline/tool-specific options}
+        |
+        |%s
+        |
+        |%s
+      """.stripMargin.format("%s", addendum)
+
+    lazy val mainUsage: String =
+      baseUsage.format(pipelineList + "\n\n" + toolList)
+
+    lazy val pipelineUsage: String = baseUsage
+      .replaceFirst("""\{pipeline,tool\}""", "pipeline")
+      .replace("""pipeline/tool""", "pipeline")
+      .format(pipelineList)
+
+    lazy val toolUsage: String = baseUsage
+      .replaceFirst("""\{pipeline,tool\}""", "tool")
+      .replace("""pipeline/tool""", "tool")
+      .format(toolList)
+
     if (args.isEmpty) {
-      System.err.println(pipelineList)
+      System.err.println(mainUsage)
       System.exit(1)
     }
-    else if (pipelines.contains(args.head)) pipelines(args.head).main(args.tail)
-    else {
-      System.err.println("Pipeline '" + args.head + "' does not exist")
-      System.err.println(pipelineList)
-      System.exit(1)
-    }
-    
-    def pipelineList: String = {
-      val pipelinesArray = for ((k,v) <- pipelines) yield k
-      "Available pipelines:" + pipelinesArray.mkString("\n- ", "\n- ", "\n") + "please supply a valid pipeline"
+
+    args match {
+      case Array("pipeline", pipelineName, pipelineArgs @ _*) =>
+        if (pipelines.contains(pipelineName))
+          if (pipelineArgs.isEmpty)
+            pipelines(pipelineName).main(Array("--help"))
+          else
+            pipelines(pipelineName).main(pipelineArgs.toArray)
+        else
+          System.err.println(s"ERROR: pipeline '$pipelineName' does not exist")
+        System.err.println(pipelineUsage)
+        System.exit(1)
+      case Array("pipeline") =>
+        System.err.println(pipelineUsage)
+        System.exit(1)
+      case Array("tool", toolName, toolArgs @ _*) =>
+        if (tools.contains(toolName))
+          tools(toolName).main(toolArgs.toArray)
+        else
+          System.err.println(s"ERROR: tool '$toolName' does not exist")
+          System.err.println(toolUsage)
+          System.exit(1)
+      case Array("tool") =>
+        System.err.println(toolUsage)
+        System.exit(1)
+      case _ =>
+        println(mainUsage)
+        System.exit(1)
     }
   }
 }
diff --git a/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/PipelineCommand.scala b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/PipelineCommand.scala
index 45c689886..7279704ea 100644
--- a/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/PipelineCommand.scala
+++ b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/PipelineCommand.scala
@@ -7,9 +7,8 @@ trait PipelineCommand extends Logging {
 
   def main(args: Array[String]): Unit = {
     var argv: Array[String] = Array()
-    //argv ++= Array("-S", tempFile.getAbsolutePath)
     argv ++= Array("-S", pipeline)
     argv ++= args
-    return BiopetQCommandLine.main(argv)
+    BiopetQCommandLine.main(argv)
   }
 }
\ No newline at end of file
diff --git a/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/ToolCommand.scala b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/ToolCommand.scala
new file mode 100644
index 000000000..af55981e7
--- /dev/null
+++ b/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/ToolCommand.scala
@@ -0,0 +1,12 @@
+package nl.lumc.sasc.biopet.core
+
+import org.broadinstitute.gatk.queue.util.Logging
+
+
+abstract trait ToolCommand extends Logging {
+
+  lazy val toolName = this.getClass.getName
+    .split("\\$").last.split("\\.").last
+
+  def main(args: Array[String])
+}
-- 
GitLab