Skip to content

Commit

Permalink
Add tests for CRAM container/slice partitioning
Browse files Browse the repository at this point in the history
  • Loading branch information
athos committed Aug 29, 2024
1 parent 11cf60f commit 7c93d23
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 5 deletions.
201 changes: 201 additions & 0 deletions test/cljam/io/cram/encode/partitioning_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
(ns cljam.io.cram.encode.partitioning-test
(:require [cljam.io.cram.encode.partitioning :as partition]
[clojure.test :refer [are deftest 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"}]]]]))))
11 changes: 6 additions & 5 deletions test/cljam/io/cram/encode/record_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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"}}
Expand Down Expand Up @@ -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)))
Expand All @@ -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"}}
Expand Down Expand Up @@ -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 []}
Expand Down

0 comments on commit 7c93d23

Please sign in to comment.