 
 (defn writer
   [f refs]
-  (->BAIWriter (DataOutputStream. (FileOutputStream. (file f)))
+  (->BAIWriter (DataOutputStream. (FileOutputStream. (io/file f)))
                refs
-               f))
+               (.getAbsolutePath (io/file f))))
 
 (defn write-index
   [^BAIWriter wtr alns]
   (try
     (write-index*! (.writer wtr) (.refs wtr) alns)
     (catch Exception e (do
-                         (let [f (file (.f wtr))]
+                         (let [f (io/file (.f wtr))]
                            (when (.exists f)
                              (.delete f)))
                          (logging/error "Failed to create BAM index")

;; index
;;

; OPTIMIZE: make-index! is slow
(defn- make-index!
  [refs alns]
  (let [indices (atom {})
        ref-name (atom nil)
        bins (atom nil)
        bins-seen (atom 0)
        lidx (atom (gen-vec max-lidx-size 0))
        largest-lidx-seen (atom -1)
        meta (atom {:first-offset -1
                    :last-offset 0
                    :aligned-alns 0
                    :unaligned-alns 0})
        no-coordinate-alns (atom 0)]
    ;; meta data
    (doseq [aln alns]
      (when (nil? @ref-name)
        (reset! ref-name (:rname aln))
        (reset! bins (gen-vec (bin-count refs (:rname aln)))))
      (when-not (= (:rname aln) @ref-name)
        (swap! indices assoc @ref-name {:meta @meta
                                        :bins @bins
                                        :lidx (optimize-lidx @lidx @largest-lidx-seen)})
        (reset! ref-name (:rname aln))
        (reset! bins (gen-vec (bin-count refs (:rname aln))))
        (reset! bins-seen 0)
        (reset! lidx (gen-vec max-lidx-size 0))
        (reset! largest-lidx-seen -1)
        (reset! meta {:first-offset -1
                      :last-offset 0
                      :aligned-alns 0
                      :unaligned-alns 0}))
      (if (zero? (:pos aln))
        (swap! no-coordinate-alns inc)
        (do (if-not (zero? (bit-and (:flag aln) 4))
              (swap! meta update-in [:unaligned-alns] inc)
              (swap! meta update-in [:aligned-alns] inc))
            (if (or (< (bgzf-util/compare (:beg (:chunk (:meta aln)))
                                          (:first-offset @meta)) 1)
                    (= (:first-offset @meta) -1))
              (swap! meta assoc :first-offset (:beg (:chunk (:meta aln)))))
            (if (< (bgzf-util/compare (:last-offset @meta)
                                      (:end (:chunk (:meta aln)))) 1)
              (swap! meta assoc :last-offset (:end (:chunk (:meta aln)))))))
      ;; bins
      (let [chunk-beg (:beg (:chunk (:meta aln)))
            chunk-end (:end (:chunk (:meta aln)))]
        (let [bin-idx (sam-util/compute-bin aln)
              bin (if (nil? (nth @bins bin-idx))
                    (do
                      (swap! bins assoc bin-idx {:idx bin-idx, :chunks nil})
                      (swap! bins-seen inc)
                      (nth @bins bin-idx))
                    (nth @bins bin-idx))]
          ;; chunk
          (if (seq (:chunks bin))
            (if (bgzf-util/same-or-adjacent-blocks? (:end (last (:chunks bin)))
                                                    chunk-beg)
              (let [last-idx (dec (count (:chunks bin)))
                    last-chunk (last (:chunks bin))
                    new-chunks (assoc (:chunks bin) last-idx (assoc last-chunk :end chunk-end))
                    new-bin (assoc bin :chunks new-chunks)]
                (swap! bins assoc bin-idx new-bin))
              (let [new-bin (update-in bin [:chunks] conj (:chunk (:meta aln)))]
                (swap! bins assoc bin-idx new-bin)))
            (let [new-bin (assoc bin :chunks (vector (:chunk (:meta aln))))]
              (swap! bins assoc bin-idx new-bin))))
        ;; linear index
        (let [aln-beg (:pos aln)
              aln-end (+ aln-beg (cgr/count-ref (:cigar aln)))
              window-beg (if (zero? aln-end)
                           (pos->lidx-offset (dec aln-beg))
                           (pos->lidx-offset aln-beg))
              window-end (if (zero? aln-end)
                           window-beg
                           (pos->lidx-offset aln-end))]
          (if (> window-end @largest-lidx-seen)
            (reset! largest-lidx-seen window-end))
          (loop [win window-beg]
            (when (<= win window-end)
              (if (or (zero? (nth @lidx win))
                      (< chunk-beg (nth @lidx win)))
                (swap! lidx assoc win chunk-beg))
              (recur (inc win)))))))
    (swap! indices assoc
           @ref-name {:meta @meta
                      :bins @bins
                      :lidx (optimize-lidx @lidx @largest-lidx-seen)}
           :no-coordinate-alns @no-coordinate-alns)
    @indices))

(defn- make-index
  [refs alns]

(ns cljam.bam-index.writer
  (:require [clojure.java.io :as io]
            [clojure.tools.logging :as logging]
            [cljam.lsb :as lsb]
            [cljam.cigar :as cgr]
            [cljam.util :refer [gen-vec]]
