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 reading bgzipped VCF files #184

Merged
merged 2 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/cljam/io/vcf.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
header (with-open [r (cio/reader (util/compressor-input-stream f))]
(vcf-reader/load-header r))]
(VCFReader. (util/as-url f) meta-info header

(if (bgzf/bgzip? f)
(bgzf/bgzf-input-stream f)
(cio/reader (util/compressor-input-stream f)))
Expand Down
12 changes: 7 additions & 5 deletions src/cljam/io/vcf/reader.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[proton.core :refer [as-long]]
[cljam.io.util.bin :as util-bin]
[cljam.io.vcf.util :as vcf-util])
(:import [java.io Closeable]
(:import [java.io Closeable BufferedReader]
[clojure.lang LazilyPersistentVector]
bgzf4j.BGZFInputStream))

Expand Down Expand Up @@ -109,7 +109,7 @@
v)]))

(defn load-meta-info
[^java.io.BufferedReader rdr]
[^BufferedReader rdr]
(loop [line (.readLine rdr), meta-info {}]
(if (meta-line? line)
(let [[k v] (parse-meta-info-line line)]
Expand All @@ -133,7 +133,7 @@
(cstr/split (subs line 1) #"\t"))

(defn load-header
[^java.io.BufferedReader rdr]
[^BufferedReader rdr]
(loop [line (.readLine rdr)]
(if (header-line? line)
(parse-header-line line)
Expand Down Expand Up @@ -164,8 +164,10 @@
(apply hash-map))))

(defn- read-data-lines
[^java.io.BufferedReader rdr header kws]
(when-let [line (.readLine rdr)]
[rdr header kws]
(when-let [line (if (instance? BufferedReader rdr)
(.readLine ^BufferedReader rdr)
(.readLine ^BGZFInputStream rdr))]
(if-not (or (meta-line? line) (header-line? line))
(cons (parse-data-line line kws)
(lazy-seq (read-data-lines rdr header kws)))
Expand Down
17 changes: 15 additions & 2 deletions test/cljam/io/vcf_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@
(is (= (vcf/read-variants rdr) test-vcf-no-samples-variants-deep)))))

(deftest read-variants-complex-test
(with-open [v (vcf/reader test-vcf-complex-file)
b (vcf/reader test-bcf-complex-file)]
(with-open [v (vcf/reader test-vcf-complex-file) ;; uncompressed VCF
z (vcf/reader test-vcf-complex-gz-file) ;; bgzipped VCF
b (vcf/reader test-bcf-complex-file)] ;; bgzipped BCF
(is (= (vcf/read-variants v)
(vcf/read-variants z)
(vcf/read-variants b)))))

(deftest-remote bin-index-is-done-without-errors-with-a-large-file
Expand Down Expand Up @@ -286,6 +288,17 @@
(testing "v4.3 complex"
(let [temp-file (.getAbsolutePath (cio/file temp-dir "test_v4_3_complex.bcf"))]
(with-open [v (vcf/reader test-vcf-complex-file)]
(let [xs (vcf/read-variants v)
m (vcf/meta-info v)
h (vcf/header v)]
(with-open [b (vcf/writer temp-file m h)]
(vcf/write-variants b xs))
(with-open [b (vcf/reader temp-file)]
(is (= xs (vcf/read-variants b))))))))
(testing "v4.3 complex bgzip"
(let [temp-file (.getAbsolutePath
(cio/file temp-dir "test_v4_3_complex_bgzip.bcf"))]
(with-open [v (vcf/reader test-vcf-complex-gz-file)]
(let [xs (vcf/read-variants v)
m (vcf/meta-info v)
h (vcf/header v)]
Expand Down