     (repeat (count positions) (PileupStatus. 0 nil nil))))
 
 (defn- pickup-ref
-  [ref-line pos]
-  (if (nil? ref-line)
+  [ref-seq pos]
+  (if (nil? ref-seq)
     \N
     (let [idx (dec pos)]
       (if (neg? idx)
         \N
-        (if-let [ref (nth (:seq ref-line) idx)]
+        (if-let [ref (nth ref-seq idx)]
           ref
           \N)))))
 
 (defn- count-for-positions
   [alns ref-line rname positions]
   (if (pos? (count alns))
-    (let [cfas (map #(count-for-alignment % rname positions) alns)
+    (let [cfas (map #(count-for-alignment % (:seq ref-line) rname positions) alns)
           plp1 (apply map (fn [& a]
                             {:rname rname
                              :count (reduce + (map :count a))
                              :seq   (str/join (map :seq a))
                              :qual  (str/join (map :qual a))}) cfas)]
-      (map #(assoc %2 :pos %1 :ref (pickup-ref ref-line %2))
+      (map #(assoc %2 :pos %1 :ref (pickup-ref (:seq ref-line) %1))
            positions plp1))
     (let [plp1 (repeat (count positions) {:rname rname
                                           :count 0
                                           :seq   nil
                                           :qual  nil})]
-      (map #(assoc %2 :pos %1 :ref (pickup-ref ref-line %2))
+      (map #(assoc %2 :pos %1 :ref (pickup-ref (:seq ref-line) %1))
            positions plp1))))
 
 (defn- read-alignments

            [cljam.sequence :as cseq]
            [cljam.io :as io]
            [cljam.fasta :as fa]
            [cljam.util :refer [ubyte]]
            [cljam.util.sam-util :refer [phred->fastq fastq->phred]]
            [cljam.pileup.common :refer [window-width step center]]
            [cljam.pileup.pileup :refer [rpositions]])
  (:import cljam.fasta.FASTAReader))


(defrecord ^:private PileupStatus [count seq qual])

(defn- ref-wrap
  "Modify sequence strings to referenced version.
  e.g. ({:n 2, :op \\M :seq [\\T \\A]} ...)
       => ({:n 2, :op \\M :seq [\\. \\.] :pos 7} ...)"
  [seq* ref-seq left]
  (let [positioned-seq (loop [ret     []
                              [f & r] seq*
                              pos     left]
                         (if (nil? f)
                           ret
                           (recur (conj ret (assoc f :pos pos))
                                  r
                                  (if-not (nil? (#{\M \D \N \= \X} (:op f)))
                                    (+ pos (count (:seq f)))
                                    pos))))]
    (map (fn [s]
           (if-not (nil? (#{\M \D \N \= \X} (:op s)))
             (update-in s [:seq] (partial map-indexed
                                          (fn [idx itm]
                                            (if (= itm (nth ref-seq (+ (dec (:pos s)) idx)))
                                              \.
                                              itm))))
             s))
         positioned-seq)))

(defn- count-for-alignment
  [aln ref-seq rname positions]
  (if (= rname (:rname aln))
    (let [left  (:pos aln)
          right (dec (+ left (cgr/count-ref (:cigar aln))))
          seq** (cseq/parse (:seq aln) (:cigar aln))
          seq*  (encode-seq (if (nil? ref-seq)
                              seq**
                              (ref-wrap seq** ref-seq left)))]
      (map (fn [p]
             (if (<= left p right)
               (PileupStatus. 1 (nth seq* (- p left)) (pickup-qual aln (- p left)))
