-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fine-tune CRAM container/slice partitioning
- Loading branch information
Showing
3 changed files
with
98 additions
and
24 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
(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) | ||
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 (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 | ||
"Partition the given alignment records into containers, which are represented | ||
as a List of Lists of record maps, and call 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