              {:rname rname, :pos pos, :n val}
              (if-not (or (nil? rst) (= rname (:rname (first rst))))
                (pileup* rst (:rname (first rst)) (:pos (first rst)))
-               (if (< pos (+ (:pos aln) (count (substantial-seq aln))))
+               (if (< pos (+ (:pos aln) (cgr/count-ref (:cigar aln))))
                  (pileup* alns rname (inc pos))
                  (pileup* rst rname (inc pos)))))))))))
 

    (let [[aln & rst] alns2]
      (if (or (nil? aln) (not= rname (:rname aln)) (< pos (:pos aln)))
        val
        (if (< pos (+ (:pos aln) (count (substantial-seq aln))))
          (recur rst (inc val))
          (recur rst val))))))


(ns cljam.pileup
  (:require [clojure.string :refer [join]]
            [cljam [cigar :as cgr]
                   [sorter :as sorter]]))

(defn substantial-seq [aln]
  (let [{seq :seq, cigar :cigar} aln]
