Skip to content

Commit

Permalink
Merge pull request #179 from chrovis/fix/create-index-header-only-bam
Browse files Browse the repository at this point in the history
Avoid an ArityException on creating an index for BAM files without alignments
  • Loading branch information
alumi committed Sep 30, 2019
2 parents 2db7fe7 + 53e10f7 commit 53720ae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/cljam/io/bam_index/writer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
indices)]
(recur rest rid' idx-status' no-coordinate-alns' indices'))
(assoc indices rid idx-status
:no-coordinate-alns no-coordinate-alns))))
:no-coordinate-alns no-coordinate-alns))))

;; Merging indices
;; -------------
Expand All @@ -148,9 +148,9 @@
(MetaData. (let [f1 (.first-offset meta1)
f2 (.first-offset meta2)]
(cond
(= f1 -1) f2
(= f2 -1) f1
:else (min f1 f2)))
(= f1 -1) f2
(= f2 -1) f1
:else (min f1 f2)))
(max (.last-offset meta1) (.last-offset meta2))
(+ (.aligned-alns meta1) (.aligned-alns meta2))
(+ (.unaligned-alns meta1) (.unaligned-alns meta2))))
Expand Down Expand Up @@ -307,7 +307,7 @@
(->> sub-blocks
(eduction (map bam-decoder/decode-pointer-block))
make-index*)))
(reduce merge-index)))))]
(reduce merge-index {:no-coordinate-alns 0})))))]
(->> blocks
make-index-fn
(finalize-index nrefs))))
Expand Down
11 changes: 11 additions & 0 deletions test/cljam/algo/bam_indexer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,14 @@
(is (not-throw? (bai/create-index temp-file-sorted
(str temp-file-sorted ".bai"))))
(is (.isFile (cio/file (str temp-file-sorted ".bai"))))))

(deftest bam-without-alignments-test
(with-before-after {:before (prepare-cache!)
:after (clean-cache!)}
(let [header {:SQ [{:SN "chr1", :LN 100}]}
target (cio/file temp-dir "no_aln.bam")
target-bai (cio/file temp-dir "no_aln.bam.bai")]
(with-open [w (sam/writer target)]
(sam/write-header w header)
(sam/write-refs w header))
(is (not-throw? (bai/create-index target target-bai))))))

0 comments on commit 53720ae

Please sign in to comment.