-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fine-grained partition of CRAM containers and slices #322
Merged
+318
−29
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
(ns cljam.io.cram.encode.partitioning | ||
(:require [cljam.io.sam.util.header :as sam.header]) | ||
(:import [java.util ArrayList List])) | ||
|
||
(defn- slice-record-collector | ||
[header {:keys [^long records-per-slice ^long min-single-ref-slice-size] | ||
:or {records-per-slice 10000, min-single-ref-slice-size 1000}}] | ||
(let [sort-by-coord? (= (sam.header/sort-order header) sam.header/order-coordinate) | ||
;; CRAM files sorted by coord can easily get back from a multi-ref state | ||
;; to a single-ref state. To support this, multi-ref slices should be kept | ||
;; as small as possible | ||
multi-upper-bound (if sort-by-coord? | ||
min-single-ref-slice-size | ||
records-per-slice)] | ||
(fn [empty-container? alns] | ||
(let [slice-records (ArrayList.)] | ||
(loop [ref-state nil, alns alns] | ||
(if (seq alns) | ||
(let [[{:keys [rname] :as aln} & more] alns | ||
n-records (.size slice-records)] | ||
(case ref-state | ||
nil | ||
(let [ref-state' (if (= rname "*") :unmapped rname)] | ||
(.add slice-records aln) | ||
(recur ref-state' more)) | ||
|
||
:unmapped | ||
(if (< n-records records-per-slice) | ||
(let [ref-state' (cond (= rname "*") ref-state | ||
sort-by-coord? (throw | ||
(ex-info | ||
(str "Unmapped records " | ||
"must be last") | ||
{})) | ||
:else :multi-ref)] | ||
(.add slice-records aln) | ||
(recur ref-state' more)) | ||
[ref-state slice-records alns]) | ||
|
||
:multi-ref | ||
(if (< n-records multi-upper-bound) | ||
(do (.add slice-records aln) | ||
(recur ref-state more)) | ||
[ref-state slice-records alns]) | ||
|
||
(if (= ref-state rname) | ||
(if (< n-records records-per-slice) | ||
(do (.add slice-records aln) | ||
(recur ref-state more)) | ||
[ref-state slice-records alns]) | ||
;; If the container already contains one or more single-ref | ||
;; slices, instead of creating and adding a new multi-ref slice | ||
;; to that container, add the accumulated single-ref slice | ||
;; to the container and add the current record to the next slice | ||
(if (and empty-container? (< n-records min-single-ref-slice-size)) | ||
(do (.add slice-records aln) | ||
(recur :multi-ref more)) | ||
[ref-state slice-records alns])))) | ||
[ref-state slice-records alns])))))) | ||
|
||
(defn with-each-container | ||
"Partitions the given alignment records into containers, which are represented | ||
as a List of Lists of record maps, and calls the function f with them." | ||
[header options alns f] | ||
(let [{:keys [^long slices-per-container] :or {slices-per-container 1}} options | ||
collect-fn (slice-record-collector header options) | ||
container-records (ArrayList.)] | ||
(loop [ref-state nil, written 0, ready 0, alns alns] | ||
(if (seq alns) | ||
(let [n-slices (.size container-records) | ||
[ref-state' ^List slice-records alns'] (collect-fn (zero? n-slices) alns)] | ||
(if (or (= n-slices slices-per-container) | ||
(= ref-state' :multi-ref) | ||
(and (some? ref-state) (not= ref-state ref-state'))) | ||
(let [written' (+ written ready)] | ||
(when-not (.isEmpty container-records) | ||
(f written container-records)) | ||
(.clear container-records) | ||
(if (= ref-state' :multi-ref) | ||
(do (f written' (doto (ArrayList.) (.add slice-records))) | ||
(recur nil (+ written' (.size slice-records)) 0 alns')) | ||
(let [ready' (.size slice-records)] | ||
(.add container-records slice-records) | ||
(recur ref-state' written' ready' alns')))) | ||
(let [ready' (+ ready (.size slice-records))] | ||
(.add container-records slice-records) | ||
(recur ref-state' written ready' alns')))) | ||
(when-not (.isEmpty container-records) | ||
(f written container-records)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}]]]])))) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that
write-container
now doesn't return the nextcounter
value.Previously, the CRAM writer needed to count the number of container records as writing out the container since the CRAM writer couldn't tell in advance how many records the container had in it.
Now the CRAM writer can tell in advance how many records are contained in each container by counting the number of records and packing them into slices/containers before writing them out.