Skip to content

Instantly share code, notes, and snippets.

@adamwespiser
Last active January 8, 2016 08:39
Show Gist options
  • Save adamwespiser/2004382 to your computer and use it in GitHub Desktop.
Save adamwespiser/2004382 to your computer and use it in GitHub Desktop.
Burrows Wheeler Tranformation
(ns bwt.lib)
(defn pTest [ys]
(sort-by #(second %) (map vector (range) ys)))
(defn p [ys]
(map second (sort-by #(first %)
(map list ys (range)))))
(defn recon[s index]
(let [l (vec (map first (sort-by #(second %) (map conj (bwt.lib/pTest s)(range)))))
nl (fn[x](l x))
c (count l) ]
(reverse (map (vec s) (take c (iterate nl index))))))
(defn reconstruct[s index]
(let [l (vec (map second (sort-by #(first %) (map list (bwt.lib/p s) (range)))))
nl (fn[x](l x))
c (count l) ]
(reverse (map (vec s) (take c (iterate nl index))))))
;; code for transform
(defn rots [coll]
"returns all rotatations a sequence"
(take (count coll) (iterate #(concat (rest %) [(first %)]) coll)))
(defn position [xs xss]
(count (take-while #(not (= % xs)) xss)))
(defn transform [coll]
"takes the BWT transform of the sequence"
(let [xss (sort-by #(.toLowerCase (apply str %)) (rots coll))]
(vector (map last xss) (position coll xss))))
(defn takeCols [j coll]
"Takes the jth first columns(1 indexed) from the collection"
(map #(take j %) coll))
(defn rrot[coll]
"[a] -> [a]\n performs a single right handed rotation of a sequence"
(concat [(last coll)] (butlast coll)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment