From 9c9edf13243970e1d98214551dc134e9c5b5984b Mon Sep 17 00:00:00 2001
From: Peter van 't Hof <p.j.van_t_hof@lumc.nl>
Date: Thu, 10 Sep 2015 12:55:53 +0200
Subject: [PATCH] Adding -o option to hide output from stdout

---
 .../sasc/biopet/tools/FindRepeatsPacBio.scala | 26 +++++++++++++++----
 .../biopet/tools/FindRepeatsPacBioTest.scala  |  3 ++-
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/FindRepeatsPacBio.scala b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/FindRepeatsPacBio.scala
index e6ffb81df..b6666fcf1 100644
--- a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/FindRepeatsPacBio.scala
+++ b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/FindRepeatsPacBio.scala
@@ -15,7 +15,7 @@
  */
 package nl.lumc.sasc.biopet.tools
 
-import java.io.File
+import java.io.{ PrintWriter, File }
 
 import htsjdk.samtools.{ QueryInterval, SAMRecord, SamReaderFactory, ValidationStringency }
 import nl.lumc.sasc.biopet.core.ToolCommand
@@ -24,12 +24,17 @@ import scala.collection.JavaConversions._
 import scala.io.Source
 
 object FindRepeatsPacBio extends ToolCommand {
-  case class Args(inputBam: File = null, inputBed: File = null) extends AbstractArgs
+  case class Args(inputBam: File = null,
+                  outputFile: Option[File] = None,
+                  inputBed: File = null) extends AbstractArgs
 
   class OptParser extends AbstractOptParser {
     opt[File]('I', "inputBam") required () maxOccurs 1 valueName "<file>" action { (x, c) =>
       c.copy(inputBam = x)
     } text "Path to input file"
+    opt[File]('o', "outputFile") maxOccurs 1 valueName "<file>" action { (x, c) =>
+      c.copy(outputFile = Some(x))
+    } text "Path to input file"
     opt[File]('b', "inputBed") required () maxOccurs 1 valueName "<file>" action { (x, c) =>
       c.copy(inputBed = x)
     } text "Path to bed file"
@@ -50,7 +55,6 @@ object FindRepeatsPacBio extends ToolCommand {
     val header = List("chr", "startPos", "stopPos", "Repeat_seq", "repeatLength",
       "original_Repeat_readLength", "Calculated_repeat_readLength",
       "minLength", "maxLength", "inserts", "deletions", "notSpan")
-    println(header.mkString("\t"))
 
     for (
       bedLine <- Source.fromFile(commandArgs.inputBed).getLines();
@@ -84,9 +88,21 @@ object FindRepeatsPacBio extends ToolCommand {
           if (length < minLength || minLength == -1) minLength = length
         }
       }
-      println(List(chr, startPos, stopPos, typeRepeat, repeatLength, oriRepeatLength, calcRepeatLength.mkString(","), minLength,
-        maxLength, inserts.mkString("/"), deletions.mkString("/"), notSpan).mkString("\t"))
       bamIter.close()
+      commandArgs.outputFile match {
+        case Some(file) => {
+          val writer = new PrintWriter(file)
+          writer.println(header.mkString("\t"))
+          writer.println(List(chr, startPos, stopPos, typeRepeat, repeatLength, oriRepeatLength, calcRepeatLength.mkString(","), minLength,
+            maxLength, inserts.mkString("/"), deletions.mkString("/"), notSpan).mkString("\t"))
+          writer.close()
+        }
+        case _ => {
+          println(header.mkString("\t"))
+          println(List(chr, startPos, stopPos, typeRepeat, repeatLength, oriRepeatLength, calcRepeatLength.mkString(","), minLength,
+            maxLength, inserts.mkString("/"), deletions.mkString("/"), notSpan).mkString("\t"))
+        }
+      }
     }
   }
 
diff --git a/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/tools/FindRepeatsPacBioTest.scala b/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/tools/FindRepeatsPacBioTest.scala
index 8057c9d25..b6de2f8bc 100644
--- a/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/tools/FindRepeatsPacBioTest.scala
+++ b/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/tools/FindRepeatsPacBioTest.scala
@@ -28,7 +28,8 @@ class FindRepeatsPacBioTest extends TestNGSuite with MockitoSugar with Matchers
   @Test
   def testMain() = {
 
-    val args = Array("-I", bam, "-b", bed)
+    val outputFile = File.createTempFile("repeats", ".tsv")
+    val args = Array("-I", bam, "-b", bed, "-o", outputFile.toString)
     main(args)
   }
 
-- 
GitLab