diff --git a/test/cljam/io/cram/encode/partitioning_test.clj b/test/cljam/io/cram/encode/partitioning_test.clj new file mode 100644 index 00000000..c988f81e --- /dev/null +++ b/test/cljam/io/cram/encode/partitioning_test.clj @@ -0,0 +1,201 @@ +(ns cljam.io.cram.encode.partitioning-test + (:require [cljam.io.cram.encode.partitioning :as partition] + [clojure.test :refer [are deftest is testing]])) + +(defn- partition-alignments [header options alns] + (let [acc (volatile! [])] + (partition/with-each-container header options alns + (fn [counter container-records] + (vswap! acc conj [counter (mapv vec container-records)]))) + @acc)) + +(deftest with-each-container-test + (let [options {:slices-per-container 2 + :records-per-slice 3 + :min-single-ref-slice-size 2}] + (testing "sorted by coord" + (let [header {:HD {:SO "coordinate"}}] + (are [input expected] + (= expected + (partition-alignments header options input)) + [] + [] + + [{:qname "q1", :rname "chr1"}] + [[0 [[{:qname "q1", :rname "chr1"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"} + {:qname "q3", :rname "chr1"} + {:qname "q4", :rname "chr1"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"} + {:qname "q3", :rname "chr1"}] + [{:qname "q4", :rname "chr1"}]]]] + + (map (fn [i] {:qname (str \q (inc (long i))), :rname "chr1"}) (range 6)) + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"} + {:qname "q3", :rname "chr1"}] + [{:qname "q4", :rname "chr1"} + {:qname "q5", :rname "chr1"} + {:qname "q6", :rname "chr1"}]]]] + + (map (fn [i] {:qname (str \q (inc (long i))), :rname "chr1"}) (range 7)) + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"} + {:qname "q3", :rname "chr1"}] + [{:qname "q4", :rname "chr1"} + {:qname "q5", :rname "chr1"} + {:qname "q6", :rname "chr1"}]]] + [6 [[{:qname "q7", :rname "chr1"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr2"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr2"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"} + {:qname "q3", :rname "chr2"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"}]]] + [2 [[{:qname "q3", :rname "chr2"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"} + {:qname "q3", :rname "chr1"} + {:qname "q4", :rname "chr2"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"} + {:qname "q3", :rname "chr1"}]]] + [3 [[{:qname "q4", :rname "chr2"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"} + {:qname "q3", :rname "chr2"} + {:qname "q4", :rname "chr2"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"}]]] + [2 [[{:qname "q3", :rname "chr2"} + {:qname "q4", :rname "chr2"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr2"} + {:qname "q3", :rname "chr2"} + {:qname "q4", :rname "chr2"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr2"}]]] + [2 [[{:qname "q3", :rname "chr2"} + {:qname "q4", :rname "chr2"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"} + {:qname "q3", :rname "chr1"} + {:qname "q4", :rname "chr1"} + {:qname "q5", :rname "chr2"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"} + {:qname "q3", :rname "chr1"}] + [{:qname "q4", :rname "chr1"}]]] + [4 [[{:qname "q5", :rname "chr2"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr2"} + {:qname "q3", :rname "chr2"} + {:qname "q4", :rname "chr3"} + {:qname "q5", :rname "chr4"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr2"}]]] + [2 [[{:qname "q3", :rname "chr2"} + {:qname "q4", :rname "chr3"}]]] + [4 [[{:qname "q5", :rname "chr4"}]]]] + + [{:qname "q1", :rname "*"}] + [[0 [[{:qname "q1", :rname "*"}]]]] + + [{:qname "q1", :rname "*"} + {:qname "q2", :rname "*"}] + [[0 [[{:qname "q1", :rname "*"} + {:qname "q2", :rname "*"}]]]] + + [{:qname "q1", :rname "*"} + {:qname "q2", :rname "*"} + {:qname "q3", :rname "*"} + {:qname "q4", :rname "*"}] + [[0 [[{:qname "q1", :rname "*"} + {:qname "q2", :rname "*"} + {:qname "q3", :rname "*"}] + [{:qname "q4", :rname "*"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "*"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "*"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"} + {:qname "q3", :rname "*"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"}]]] + [2 [[{:qname "q3", :rname "*"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"} + {:qname "q3", :rname "*"} + {:qname "q4", :rname "*"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr1"}]]] + [2 [[{:qname "q3", :rname "*"} + {:qname "q4", :rname "*"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "*"} + {:qname "q3", :rname "*"} + {:qname "q4", :rname "*"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "*"}]]] + [2 [[{:qname "q3", :rname "*"} + {:qname "q4", :rname "*"}]]]]) + (are [input] (thrown? Exception (partition-alignments header options input)) + [{:qname "q1", :rname "*"} + {:qname "q2", :rname "chr1"}] + + [{:qname "q1", :rname "*"} + {:qname "q2", :rname "*"} + {:qname "q3", :rname "chr1"}]))) + (testing "unsorted" + (are [input expected] + (= expected (partition-alignments {:HD {:SO "unsorted"}} options input)) + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr2"} + {:qname "q3", :rname "chr1"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr2"} + {:qname "q3", :rname "chr1"}]]]] + + [{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr2"} + {:qname "q3", :rname "chr1"} + {:qname "q4", :rname "chr2"}] + [[0 [[{:qname "q1", :rname "chr1"} + {:qname "q2", :rname "chr2"} + {:qname "q3", :rname "chr1"}]]] + [3 [[{:qname "q4", :rname "chr2"}]]]] + + [{:qname "q1", :rname "*"} + {:qname "q2", :rname "chr1"}] + [[0 [[{:qname "q1", :rname "*"} + {:qname "q2", :rname "chr1"}]]]] + + [{:qname "q1", :rname "*"} + {:qname "q2", :rname "*"} + {:qname "q3", :rname "chr1"}] + [[0 [[{:qname "q1", :rname "*"} + {:qname "q2", :rname "*"} + {:qname "q3", :rname "chr1"}]]]])))) diff --git a/test/cljam/io/cram/encode/record_test.clj b/test/cljam/io/cram/encode/record_test.clj index 3fda2a61..b00f0503 100644 --- a/test/cljam/io/cram/encode/record_test.clj +++ b/test/cljam/io/cram/encode/record_test.clj @@ -6,7 +6,8 @@ [cljam.io.sequence :as cseq] [cljam.test-common :as common] [clojure.test :refer [are deftest is testing]] - [clojure.walk :as walk])) + [clojure.walk :as walk]) + (:import [java.util ArrayList])) (def ^:private test-seq-resolver (let [seqs (with-open [r (cseq/reader common/test-fa-file)] @@ -78,7 +79,7 @@ (deftest preprocess-slice-records-test (let [cram-header {:SQ [{:SN "ref"}]} - records (object-array + records (ArrayList. [{:rname "ref", :pos 1, :cigar "5M", :seq "AGAAT", :qual "HFHHH" :options [{:RG {:type "Z", :value "rg001"}} {:MD {:type "Z", :value "2C2"}} @@ -132,7 +133,7 @@ ::record/flag 0x0b, ::record/ref-index -1, ::record/end 10, ::record/tags-index 1 ::record/features []}] (walk/prewalk #(if (.isArray (class %)) (vec %) %) - records))) + (vec records)))) (is (= [[{:tag :MD, :type \Z} {:tag :NM, :type \c}] []] (:tag-dict container-ctx))) @@ -158,7 +159,7 @@ :RG [{:ID "rg001"} {:ID "rg002"}]} - records (object-array + records (ArrayList. [{:qname "q001", :flag 99, :rname "ref", :pos 1, :end 5, :mapq 0, :cigar "5M", :rnext "=", :pnext 151, :tlen 150, :seq "AGAAT", :qual "HFHHH" :options [{:RG {:type "Z", :value "rg001"}} @@ -337,7 +338,7 @@ (let [cram-header {:SQ [{:SN "ref"} {:SN "ref2"}]} - records (object-array + records (ArrayList. [{:qname "q001", :flag 77, :rname "*", :pos 0, :end 0, :mapq 0, :cigar "*", :rnext "*", :pnext 0, :tlen 0, :seq "AATCC", :qual "CCFFF" :options []}