From 5aedab83757d8266805659ec40a79aae29596d16 Mon Sep 17 00:00:00 2001
From: Peter van 't Hof <p.j.van_t_hof@lumc.nl>
Date: Tue, 14 Apr 2015 16:07:06 +0200
Subject: [PATCH] Added a option to allow fallback on strings

---
 .../biopet/extensions/picard/Picard.scala     |  2 +-
 .../nl/lumc/sasc/biopet/utils/package.scala   | 26 +++++++------------
 2 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/extensions/picard/Picard.scala b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/extensions/picard/Picard.scala
index 03f7ba975..50f6ffa3b 100644
--- a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/extensions/picard/Picard.scala
+++ b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/extensions/picard/Picard.scala
@@ -90,7 +90,7 @@ object Picard {
 
       val header = lines(start).split("\t")
       val content = (for (i <- (start + 1) until end) yield lines(i).split("\t"))
-        .map(row => row.map(col => tryToParseNumber(col).getOrElse(col)))
+        .map(row => row.map(col => tryToParseNumber(col, true).getOrElse(col)))
 
       Option((header, content.toList))
     } else {
diff --git a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/package.scala b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/package.scala
index eab8c3764..e64a1c4f6 100644
--- a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/package.scala
+++ b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/utils/package.scala
@@ -25,13 +25,10 @@ package object utils {
    * @param funcs one or more conversion functions to apply.
    * @return a [[Try]] object encapsulating the conversion result.
    */
-  def tryToConvert(raw: String, funcs: (String => Any)*): Try[Any] = funcs match {
-
-    case Seq(firstFunc, otherFuncs @ _*) =>
-      Try(firstFunc(raw))
-        .transform(s => Success(s), f => tryToConvert(raw, otherFuncs: _*))
-
-    case Nil => Try(throw new Exception(s"Can not extract value from string $raw"))
+  def tryToConvert(raw: String, funcs: (String => Any)*): Try[Any] = {
+    if (funcs.isEmpty) Try(throw new Exception(s"Can not extract value from string $raw"))
+    else Try(funcs.head(raw))
+      .transform(s => Success(s), f => tryToConvert(raw, funcs.tail: _*))
   }
 
   /**
@@ -44,16 +41,13 @@ package object utils {
    * a [[Double]], then a [[BigDecimal]].
    *
    * @param raw the string to convert.
+   * @param fallBack Allows also to return the string itself when converting fails, default false.
    * @return a [[Try]] object encapsulating the conversion result.
    */
-  def tryToParseNumber(raw: String) = raw match {
-
-    case isInteger(i) =>
-      tryToConvert(i, x => x.toInt, x => x.toLong, x => BigInt(x))
-
-    case isDecimal(f) =>
-      tryToConvert(f, x => x.toDouble, x => BigDecimal(x))
-
-    case otherwise => Try(throw new Exception(s"Can not extract number from string $raw"))
+  def tryToParseNumber(raw: String, fallBack: Boolean = false) = raw match {
+    case isInteger(i)  => tryToConvert(i, x => x.toInt, x => x.toLong, x => BigInt(x))
+    case isDecimal(f)  => tryToConvert(f, x => x.toDouble, x => BigDecimal(x))
+    case _ if fallBack => Try(raw)
+    case _             => Try(throw new Exception(s"Can not extract number from string $raw"))
   }
 }
-- 
GitLab