From b03177621ce6f8e0481e6be17f98f82b7c57f186 Mon Sep 17 00:00:00 2001
From: Peter van 't Hof <p.j.van_t_hof@lumc.nl>
Date: Mon, 26 May 2014 17:32:48 +0200
Subject: [PATCH] Added default_clip_mode option

---
 .../lumc/sasc/biopet/wrappers/Cutadapt.scala  | 39 +++++++++++++++----
 1 file changed, 31 insertions(+), 8 deletions(-)

diff --git a/biopet-framework/src/main/java/nl/lumc/sasc/biopet/wrappers/Cutadapt.scala b/biopet-framework/src/main/java/nl/lumc/sasc/biopet/wrappers/Cutadapt.scala
index 644326bef..0e2ea0174 100644
--- a/biopet-framework/src/main/java/nl/lumc/sasc/biopet/wrappers/Cutadapt.scala
+++ b/biopet-framework/src/main/java/nl/lumc/sasc/biopet/wrappers/Cutadapt.scala
@@ -11,6 +11,7 @@ class Cutadapt(val globalConfig: Config) extends CommandLineFunction {
   def this() = this(new Config(Map()))
   analysisName = "cutadapt"
   val config: Config = Config.mergeConfigs(globalConfig.getAsConfig("cutadapt"), globalConfig)
+  logger.debug("Config for " + this.analysisName + ": " + config)
   
   @Input(doc="Cutadapt exe", required=false)
   var cutadapt_exe: File = new File(config.getAsString("exe","/usr/local/bin/cutadapt"))
@@ -18,17 +19,22 @@ class Cutadapt(val globalConfig: Config) extends CommandLineFunction {
   @Input(doc="Fastq contams file", required=false) var contams_file: File = _
   @Output(doc="Output fastq file") var fastq_output: File = _
   
+  var default_clip_mode = config.getAsString("default_clip_mode", "3")
   var opt_adapter: Set[String] = config.getAsListOfStrings("adapter", Nil).to[Set]
   var opt_anywhere: Set[String] = config.getAsListOfStrings("anywhere", Nil).to[Set]
   var opt_front: Set[String] = config.getAsListOfStrings("front", Nil).to[Set]
   
   var opt_discard: Boolean = config.getAsBoolean("discard",false)
   var opt_minimum_length: String = config.getAsInt("minimum_length", 1).toString
-  var opt_maximum_length: String = if (config.contains("maximum_length")) config.getAsInt("maximum_length").toString else null 
+  var opt_maximum_length: String = config.getAsString("maximum_length", null) 
   
-  def commandLine = {
+  def init() {
     this.addJobReportBinding("version", getVersion)
     this.getContamsFromFile
+  }
+  
+  def commandLine = {
+    init()
     if (!opt_adapter.isEmpty || !opt_anywhere.isEmpty || !opt_front.isEmpty) {
       required(cutadapt_exe) +
       // options
@@ -53,7 +59,13 @@ class Cutadapt(val globalConfig: Config) extends CommandLineFunction {
       if (contams_file.exists()) {
         for (line <- fromFile(contams_file).getLines) {
           var s: String = line.substring(line.lastIndexOf("\t")+1, line.size)
-          opt_adapter += s
+          if (default_clip_mode == "3") opt_adapter += s
+          else if (default_clip_mode == "5") opt_front += s
+          else if (default_clip_mode == "both") opt_anywhere += s
+          else {
+            opt_adapter += s
+            logger.warn("Option default_clip_mode should be '3', '5' or 'both', falling back to default: '3'")
+          }
           logger.info("Adapter: " + s + " found in: " + fastq_input)
         }
       } else logger.warn("File : " + contams_file + " does not exist")
@@ -61,11 +73,22 @@ class Cutadapt(val globalConfig: Config) extends CommandLineFunction {
   }
   
   private var version: String = _
-  def getVersion : String = {
-    if (version == null) {
-      val v: String = (cutadapt_exe + " --version").!!.replace("\n", "")
-      if (!v.isEmpty) version = v
+  var versionCommand = cutadapt_exe + " --version"
+  var versionRegex = """(.*)"""
+  def getVersion: String = getVersion(versionCommand, versionRegex)
+  def getVersion(cmd:String, regex:String) : String = {
+    val REG = regex.r
+    if (cmd.! != 0) {
+      logger.warn("Version command: '" + cmd + "' give a none-zero exit code, version not found")
+      return "NA"
+    }
+    for (line <- cmd.!!.split("\n")) {
+      line match { 
+        case REG(m) => return m
+        case _ =>
+      }
     }
-    return version
+    logger.warn("Version command: '" + cmd + "' give a exit code 0 but no version was found, executeble oke?")
+    return "NA"
   }
 }
\ No newline at end of file
-- 
GitLab