Utility functions for SAM references.

(ns cljam.io.sam.util.refs
  (:require [clojure.set :as cset]))

Returns a reference sequence from the sam header.

(defn make-refs
  [hdr]
  (map
   #(cset/rename-keys
     %
     {:SN :name, :LN :len, :AH :alt, :AS :assembly,
      :M5 :md5, :SP :species, :UR :uri})
   (:SQ hdr)))
(defn- ref-id*
  [refs name']
  (some #(when (= name' (:name (second %))) (first %))
        (map-indexed vector refs)))

Returns reference ID from the reference sequence and the specified reference name. If not found, returns nil.

(def ref-id
  (memoize ref-id*))

Returns a reference name from the reference ID. Returns nil if id is not mapped.

(defn ref-name
  [refs ^long id]
  (when (<= 0 id (dec (count refs)))
    (:name (nth refs id))))

Returns the first reference which has the specified name.

(defn ref-by-name
  [refs name']
  (some #(when (= (:name %) name') %) refs))