Functions to read and write the SAM (Sequence Alignment/Map) format and BAM (its binary equivalent). See https://samtools.github.io/hts-specs/ for the detail SAM/BAM specifications.

(ns cljam.io.sam
  (:refer-clojure :exclude [indexed?])
  (:require [cljam.io.sam.reader :as sam-reader]
            [cljam.io.sam.writer :as sam-writer]
            [cljam.io.bam.core :as bam-core]
            [cljam.io.protocols :as protocols]
            [cljam.io.util :as io-util])
  (:import java.io.Closeable
           cljam.io.sam.reader.SAMReader
           cljam.io.sam.writer.SAMWriter
           cljam.io.bam.reader.BAMReader
           cljam.io.bam.writer.BAMWriter))

Reading

Returns an open cljam.io.sam.reader.SAMReader of f. Should be used inside with-open to ensure the reader is properly closed.

(defn ^SAMReader sam-reader
  [f]
  (sam-reader/reader f))

Returns an open cljam.io.bam.reader.BAMReader of f. Should be used inside with-open to ensure the reader is properly closed.

(defn ^BAMReader bam-reader
  [f]
  (bam-core/reader f))

Clones bam reader sharing persistent objects.

(defn ^BAMReader clone-bam-reader
  [r]
  (bam-core/clone-reader r))

Selects suitable reader from f's extension, returning the reader. Opens a new reader if the arg represents a file such as String path, java.io.File, or java.net.URL. If a reader is given, clones the reader. This function supports SAM and BAM formats.

(defn ^Closeable reader
  [f]
  (if (io-util/bam-reader? f)
    (clone-bam-reader f)
    (case (try
            (io-util/file-type f)
            (catch IllegalArgumentException _
              (io-util/file-type-from-contents f)))
      :sam (sam-reader f)
      :bam (bam-reader f)
      (throw (IllegalArgumentException. "Invalid source type")))))

Returns header of the SAM/BAM file.

(defn read-header
  [rdr]
  (protocols/read-header rdr))

Returns references of the SAM/BAM file.

(defn read-refs
  [rdr]
  (protocols/read-refs rdr))

Reads alignments of the SAM/BAM file, returning the alignments as an eduction.

(defn read-alignments
  ([rdr] (protocols/read-alignments rdr))
  ([rdr region] (protocols/read-alignments rdr region)))

Reads alignment blocks of the SAM/BAM file, returning the blocks as an eduction.

(defn read-blocks
  ([rdr] (protocols/read-blocks rdr))
  ([rdr region] (protocols/read-blocks rdr region))
  ([rdr region option] (protocols/read-blocks rdr region option)))

Returns true if the reader can be randomly accessed, false if not. Note this function immediately realizes a delayed index.

(defn indexed?
  [rdr]
  (protocols/indexed? rdr))

Writing

Returns an open cljam.io.sam.writer.SAMWriter of f. Should be used inside with-open to ensure the writer is properly closed.

(defn ^SAMWriter sam-writer
  [f]
  (sam-writer/writer f))

Returns an open cljam.io.bam.writer.BAMWriter of f. Should be used inside with-open to ensure the writer is properly closed.

(defn ^BAMWriter bam-writer
  ([f]
   (bam-writer f false))
  ([f create-index?]
   (bam-core/writer f create-index?)))

Selects suitable writer from f's extension, returning the writer. This function supports SAM and BAM format.

(defn ^Closeable writer
  ([f]
   (writer f false))
  ([f create-index?]
   (case (io-util/file-type f)
     :sam (if create-index?
            (throw (ex-info "SAM file indexing is not implemented." {}))
            (sam-writer f))
     :bam (bam-writer f create-index?)
     (throw (IllegalArgumentException. "Invalid file type")))))

Writes header to the SAM/BAM file.

(defn write-header
  [wtr header]
  (protocols/write-header wtr header))

Writes references to the SAM/BAM file.

(defn write-refs
  [wtr header]
  (protocols/write-refs wtr header))

Writes alignments to the SAM/BAM file.

(defn write-alignments
  [wtr alignments header]
  (protocols/write-alignments wtr alignments header))

Writes alignment blocks of the SAM/BAM file.

(defn write-blocks
  [wtr blocks]
  (protocols/write-blocks wtr blocks))