From a913c55e3dc3cc99b57b57828ee07ff9c0a43417 Mon Sep 17 00:00:00 2001
From: bow <bow@bow.web.id>
Date: Mon, 16 Feb 2015 10:49:31 +0100
Subject: [PATCH] Refactor syncIter logic

---
 .../nl/lumc/sasc/biopet/tools/FastqSync.scala | 36 +++++++++----------
 1 file changed, 17 insertions(+), 19 deletions(-)

diff --git a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/FastqSync.scala b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/FastqSync.scala
index 5096bdea8..d1b86906e 100644
--- a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/FastqSync.scala
+++ b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/tools/FastqSync.scala
@@ -128,25 +128,23 @@ object FastqSync extends ToolCommand {
       (pre.headOption, seqA.headOption, seqB.headOption) match {
         // where the magic happens!
         case (Some(r), Some(a), Some(b)) =>
-          // value of A iterator in the next recursion
-          val nextA =
-            // hold A if its head is not equal to reference
-            if (a.fragId != r.fragId) {
-              if (b.fragId == r.fragId) numDiscB += 1
-              seqA
-              // otherwise, go to next item
-            } else seqA.tail
-          // like A above
-          val nextB =
-            if (b.fragId != r.fragId) {
-              if (a.fragId == r.fragId) numDiscA += 1
-              seqB
-            } else seqB.tail
-          // write only if all IDs are equal
-          if (a.fragId == r.fragId && b.fragId == r.fragId) {
-            numKept += 1
-            seqOutA.write(a)
-            seqOutB.write(b)
+          val (nextA, nextB) = (a.fragId == r.fragId, b.fragId == r.fragId) match {
+            // all IDs are equal to ref
+            case (true, true) =>
+              numKept += 1
+              seqOutA.write(a)
+              seqOutB.write(b)
+              (seqA.tail, seqB.tail)
+            // B not equal to ref and A is equal, then we discard A and progress
+            case (true, false) =>
+              numDiscA += 1
+              (seqA.tail, seqB)
+            // A not equal to ref and B is equal, then we discard B and progress
+            case (false, true) =>
+              numDiscB += 1
+              (seqA, seqB.tail)
+            case (false, false) =>
+              (seqA, seqB)
           }
           syncIter(pre.tail, nextA, nextB)
         // recursion base case: both iterators have been exhausted
-- 
GitLab