example-tool.md 5.46 KB
Newer Older
1
2
3
4
5
6
7
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
39
40
41
42
43
44
45
46
47
48
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Developer - Example tool

In this tutorial we explain how to create a tool within the biopet-framework. We provide convient helper methods which can be used in the tool.
We take a line counter as the use case.

### Initial tool code
```scala
package nl.lumc.sasc.biopet.tools

import java.io.{ PrintWriter, File }

import nl.lumc.sasc.biopet.utils.ConfigUtils._
import nl.lumc.sasc.biopet.utils.ToolCommand
import scala.collection.mutable

import scala.io.Source

/**
 */
object SimpleTool extends ToolCommand {
    /*
    * Main function executes the LineCounter.scala
    */    
    def main(args: Array[String]): Unit = {
        println("This is the SimpleTool");
    }
}
```

This is the minimum setup for having a working tool. (not functional yet)


### Program arguments and environment variables

A basic application/tool usually takes arguments to configure and set parameters to be used within the tool.
In biopet we facilitate an ``AbstractArgs`` case-class which stores the arguments read from commandline.


```scala
  case class Args(inputFile: File = Nil, outputFile: Option[File] = None) extends AbstractArgs
```

The arguments are stored in ``Args``

Then add code that fills the Args.

```scala
  class OptParser extends AbstractOptParser {

    head(
      s"""
         |$commandName - Count lines in a textfile
      """.stripMargin)

    opt[File]('i', "input") required () unbounded () valueName "<inputFile>" action { (x, c) =>
      c.copy(inputFile = x)
    } validate {
      x => if (x.exists) success else failure("Inputfile not found")
    } text "Count lines from this files"

    opt[File]('o', "output") unbounded () valueName "<outputFile>" action { (x, c) =>
      c.copy(outputFile = Some(x))
    } text "File to write output to, if not supplied output go to stdout"

  }
```

In the end your tool would look like the following:

```scala

package nl.lumc.sasc.biopet.tools

import java.io.{ PrintWriter, File }

import nl.lumc.sasc.biopet.utils.ConfigUtils._
import nl.lumc.sasc.biopet.utils.ToolCommand
import scala.collection.mutable

import scala.io.Source

/**
 */
object SimpleTool extends ToolCommand {
  case class Args(inputFile: File = Nil, outputFile: Option[File] = None) extends AbstractArgs
        
  class OptParser extends AbstractOptParser {
    
    head(
      s"""
         |$commandName - Count lines in a textfile
      """.stripMargin)
    
    opt[File]('i', "input") required () unbounded () valueName "<inputFile>" action { (x, c) =>
      c.copy(inputFile = x)
    } validate {
      x => if (x.exists) success else failure("Inputfile not found")
    } text "Count lines from this files"
    
    opt[File]('o', "output") unbounded () valueName "<outputFile>" action { (x, c) =>
      c.copy(outputFile = Some(x))
    } text "File to write output to, if not supplied output go to stdout"
    
  }

  def countToJSON(inputRaw: File): String = {
    val reader = Source.fromFile(inputRaw)
    val nLines = reader.getLines.size
    
    mapToJson(Map(
        "lines" -> nLines,
        "input" -> inputRaw
    )).spaces2
  }

  /*
   * Main function executes the LineCounter.scala
   */    
  def main(args: Array[String]): Unit = {
    val commandArgs: Args = parseArgs(args)
    
    // use the arguments
    val jsonString: String = countToJSON(commandArgs.input)
    commandArgs.outputJson match {
      case Some(file) =>
        val writer = new PrintWriter(file)
        writer.println(jsonString)
        writer.close()
       case _ => println(jsonString)
    }
  }
}
```

### Running your new tool

### Debugging the tool with IDEA

### Setting up unit tests

### Adding tool-extension for usage in pipeline

When this tool is used in a pipeline in biopet, one has to add a tool wrapper for the tool created.
 
The wrapper would look like:

```scala
package nl.lumc.sasc.biopet.extensions.tools

import java.io.File

import nl.lumc.sasc.biopet.core.ToolCommandFunction
import nl.lumc.sasc.biopet.core.summary.Summarizable
import nl.lumc.sasc.biopet.utils.ConfigUtils
import nl.lumc.sasc.biopet.utils.config.Configurable
import org.broadinstitute.gatk.utils.commandline.{ Argument, Output, Input }

/**
 * SimpleTool function class for usage in Biopet pipelines
 *
 * @param root Configuration object for the pipeline
 */
class SimpleTool(val root: Configurable) extends ToolCommandFunction with Summarizable {
  def toolObject = nl.lumc.sasc.biopet.tools.SimpleTool

  @Input(doc = "Input file to count lines from", shortName = "input", required = true)
  var input: File = _
  
  @Output(doc = "Output JSON", shortName = "output", required = true)
  var output: File = _

  override def defaultCoreMemory = 1.0

  override def cmdLine = super.cmdLine +
    required("-i", input) +
    required("-o", output)

  def summaryStats: Map[String, Any] = {
    ConfigUtils.fileToConfigMap(output)
  }

  def summaryFiles: Map[String, File] = Map(
    "simpletool" -> output
  )

}

object SimpleTool {
  def apply(root: Configurable, input: File, output: File): SimpleTool = {
    val report = new SimpleTool(root)
    report.inputReport = input
    report.output = new File(output, input.getName.substring(0, input.getName.lastIndexOf(".")) + ".simpletool.json")
    report
  }

  def apply(root: Configurable, input: File, outDir: String): SimpleTool = {
    val report = new SimpleTool(root)
    report.inputReport = input
    report.output = new File(outDir, input.getName.substring(0, input.getName.lastIndexOf(".")) + ".simpletool.json")
    report
  }
}
```


### Summary setup (for reporting results to JSON)