Repository: chrovis/cljam
Pull request #130 (2018-04-04 05:37:33)

Fix a bug in merging bed regions
#### Summary
Fix a bug in `cljam.io.bed/merge-fields` that adjacent regions are not properly merged.

#### Changes
Use a function in `cljam.util.region` to merge + upgrade dependencies

#### Tests

- `lein test :all` 🆗



Patch:
diff --git a/project.clj b/project.clj
index 275601f..d861771 100644
--- a/project.clj
+++ b/project.clj
@@ -5,17 +5,17 @@
             :url "https://www.apache.org/licenses/LICENSE-2.0"}
   :dependencies [[org.clojure/tools.logging "0.4.0"]
                  [org.clojure/tools.cli "0.3.5"]
-                 [org.apache.commons/commons-compress "1.15"]
-                 [clj-sub-command "0.3.0"]
-                 [digest "1.4.6"]
+                 [org.apache.commons/commons-compress "1.16.1"]
+                 [clj-sub-command "0.4.0"]
+                 [digest "1.4.8"]
                  [bgzf4j "0.1.0"]
                  [com.climate/claypoole "1.1.4"]
                  [camel-snake-kebab "0.4.0"]
-                 [proton "0.1.3"]]
+                 [proton "0.1.6"]]
   :profiles {:dev {:dependencies [[org.clojure/clojure "1.8.0"]
-                                  [cavia "0.4.2"]
+                                  [cavia "0.5.0"]
                                   [criterium "0.4.4"]
-                                  [net.totakke/libra "0.1.0"]
+                                  [net.totakke/libra "0.1.1"]
                                   [org.tcrawley/dynapath "1.0.0"]]
                    :plugins [[lein-binplus "0.6.2" :exclusions [org.clojure/clojure]]
                              [lein-codox "0.10.3"]
@@ -30,10 +30,10 @@
                    :global-vars {*warn-on-reflection* true}}
              :1.7 {:dependencies [[org.clojure/clojure "1.7.0"]]}
              :1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]}
-             :1.9 {:dependencies [[org.clojure/clojure "1.9.0-RC1"]]}
+             :1.9 {:dependencies [[org.clojure/clojure "1.9.0"]]}
              :uberjar {:dependencies [[org.clojure/clojure "1.8.0"]
-                                      [org.apache.logging.log4j/log4j-api "2.9.1"]
-                                      [org.apache.logging.log4j/log4j-core "2.9.1"]]
+                                      [org.apache.logging.log4j/log4j-api "2.11.0"]
+                                      [org.apache.logging.log4j/log4j-core "2.11.0"]]
                        :resource-paths ["bin-resources"]
                        :main cljam.tools.main
                        :jvm-opts ["-Dclojure.compiler.direct-linking=true"]
diff --git a/src/cljam/io/bed.clj b/src/cljam/io/bed.clj
index 6d2222d..67e1d57 100644
--- a/src/cljam/io/bed.clj
+++ b/src/cljam/io/bed.clj
@@ -7,6 +7,7 @@
             [cljam.io.protocols :as protocols]
             [cljam.util :as util]
             [cljam.util.chromosome :as chr]
+            [cljam.util.region :as region]
             [clojure.tools.logging :as logging])
   (:import [java.io BufferedReader BufferedWriter Closeable]))
 
@@ -199,13 +200,12 @@
   "Sort and merge overlapped regions.
   Currently, this function affects only :end and :name fields."
   [xs]
-  (reduce
-   (fn [r m]
-     (let [l (last r)]
-       (if (and l (= (:chr l) (:chr m)) (<= (:start m) (:end l)) (<= (:start l) (:end m)))
-         (update r (dec (count r)) (fn [n m] (-> n (assoc :end (:end m)) (update-some :name str "+" (:name m)))) m)
-         (conj r m))))
-   []
+  (region/merge-regions-with
+   (fn [x {:keys [name end]}]
+     (-> x
+         (update :end max end)
+         (update-some :name str "+" name)))
+   0
    (sort-fields xs)))
 
 (defn write-raw-fields


Test patch:
diff --git a/test/cljam/io/bed_test.clj b/test/cljam/io/bed_test.clj
index 1801665..b2a0b0a 100644
--- a/test/cljam/io/bed_test.clj
+++ b/test/cljam/io/bed_test.clj
@@ -162,7 +162,13 @@
   (is (= (bed/merge-fields []) []))
   (is (= (bed/merge-fields [{:chr "chr2" :start 1 :end 10} {:chr "chr1" :start 1 :end 10} {:chr "chr1" :start 4 :end 13}
                             {:chr "chr1" :start 13 :end 15} {:chr "chr1" :start 16 :end 20}])
-         [{:chr "chr1" :start 1 :end 15} {:chr "chr1" :start 16 :end 20} {:chr "chr2" :start 1 :end 10}]))
+         [{:chr "chr1" :start 1 :end 20} {:chr "chr2" :start 1 :end 10}]))
+  (is (= (bed/merge-fields [{:chr "chr2" :start 1 :end 10} {:chr "chr1" :start 1 :end 10} {:chr "chr1" :start 4 :end 13}
+                            {:chr "chr1" :start 14 :end 15} {:chr "chr1" :start 16 :end 20}])
+         [{:chr "chr1" :start 1 :end 20} {:chr "chr2" :start 1 :end 10}]))
+  (is (= (bed/merge-fields [{:chr "chr2" :start 1 :end 10} {:chr "chr1" :start 1 :end 10} {:chr "chr1" :start 4 :end 13}
+                            {:chr "chr1" :start 15 :end 15} {:chr "chr1" :start 16 :end 20}])
+         [{:chr "chr1" :start 1 :end 13} {:chr "chr1" :start 15 :end 20} {:chr "chr2" :start 1 :end 10}]))
   (is (= (bed/merge-fields [{:chr "chr1" :start 1 :end 10 :name "chr1:1-10"} {:chr "chr1" :start 4 :end 13 :name "chr1:4-13"}])
          [{:chr "chr1" :start 1 :end 13 :name "chr1:1-10+chr1:4-13"}])))