Skip to content
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

Fix a bug in BAM random reader. #21

Merged
merged 2 commits into from
Jan 23, 2017
Merged

Fix a bug in BAM random reader. #21

merged 2 commits into from
Jan 23, 2017

Conversation

alumi
Copy link
Member

@alumi alumi commented Jan 19, 2017

I found sometimes BAM random reading function like (cljam.io/read-alignment reader {:chr "chr1" :start 1 :end 10000000 :depth :deep}) returns fewer alignments than actual for certain BAM files.

The root cause was in cljam.bam.reader/read-alignments* function:

(let [...
      candidates (flatten (map (fn [[^Long begin ^Long finish]]
                                 (.seek ^BGZFInputStream (.reader rdr) begin)
                                 (read-to-finish rdr finish read-fn)) spans))]
  (if (= deep-or-shallow :first-only)
    (read-alignments-first-only rdr spans window read-fn)
    (filter window candidates)))

where cljam.bam.reader/read-to-finish returns a lazy sequence:

(defn- read-to-finish
   [^BAMReader rdr
    ^Long finish
    ^clojure.lang.IFn read-fn]
   (let [r ^BGZFInputStream (.reader rdr)]
    (when (and (not (zero? (.available r)))
               (> finish (.getFilePointer r)))
      (cons (read-fn rdr (.refs rdr))
            (lazy-seq (read-to-finish rdr finish read-fn))))))

Because (.seek ^BGZFInputStream (.reader rdr) begin) for all spans are called when flattened, read-to-finishreads alignments in the last span only. (+ the first one in all spans)

This problem is now fixed by seeking just before reading data
and its effect to performance of reading from single long span is limited.

@alumi alumi added the bug label Jan 19, 2017
@alumi alumi requested a review from totakke January 19, 2017 09:59
Copy link
Member

@totakke totakke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@totakke totakke merged commit 9bbc061 into master Jan 23, 2017
totakke added a commit that referenced this pull request Jan 23, 2017
@totakke totakke deleted the fix/bam-random-read branch January 24, 2017 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants