The core of BAM index features. | (ns cljam.io.bam-index.core
(:require [clojure.java.io :as cio]
[clojure.tools.logging :as logging]
[cljam.io.bam-index [reader :as reader]
[writer :as writer]
[common :as common]]
[cljam.util :as util]
[cljam.io.util.bin :as util-bin])
(:import [java.io DataOutputStream FileOutputStream]
cljam.io.bam_index.reader.BAIReader
cljam.io.bam_index.writer.BAIWriter)) |
(deftype BAMIndex [url bidx lidx]
util-bin/IBinningIndex
(get-chunks [_ ref-idx bins]
(vec (mapcat (get bidx ref-idx) bins)))
(get-min-offset [_ ref-idx beg]
(get (get lidx ref-idx)
(util-bin/pos->lidx-offset beg common/linear-index-shift) 0))
(get-depth [_]
common/linear-index-depth)
(get-min-shift [_] common/linear-index-shift)) | |
Reads the given BAI file | (defn bam-index
[f]
(let [{:keys [bidx lidx]} (with-open [r ^BAIReader (reader/reader f)] (reader/read-all-index! r))]
(->BAMIndex (util/as-url f) bidx lidx))) |
Reading | |
Returns binning index for the given reference index. | (defn bin-index
[f ref-idx]
(with-open [r ^BAIReader (reader/reader f)]
(reader/read-bin-index! r ref-idx))) |
Returns linear index for the given reference index. | (defn linear-index
[f ref-idx]
(with-open [r ^BAIReader (reader/reader f)]
(reader/read-linear-index! r ref-idx))) |
Returns a sequence of [start end) pairs of virtual file offsets that may contain alignments that don't have RNAME. | (defn get-unplaced-spans
[^BAMIndex bai]
(if-let [begin (some->>
(for [[_ bins] (.bidx bai)
[_ chunks] bins
{:keys [end]} chunks]
end)
seq
(reduce max))]
[[begin Long/MAX_VALUE]]
[])) |
Returns regions of a BAM file that may contain an alignment for the given range. | (defn get-spans [^BAMIndex bai ^long ref-idx ^long beg ^long end] (util-bin/get-spans bai ref-idx beg end)) |
Writing | |
Returns an open | (defn writer
^BAIWriter
[f refs]
(BAIWriter. (DataOutputStream. (FileOutputStream. (cio/file f)))
refs
(util/as-url f))) |
Creates a BAM index file from the alignments and references data. | (defn create-index
[f alns refs]
(with-open [w (writer f refs)]
(try
(writer/write-index! w alns)
(catch Exception e
(cio/delete-file (.url w))
(logging/error "Failed to create BAM index")
(throw e))))) |