Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 10th, 2012  |  syntax: None  |  size: 3.59 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. (defn smith_waterman [seq1 seq2 & {match-weight :match-weight
  2.                                    mismatch-weight :mismatch-weight
  3.                                    :or {match-weight 2
  4.                                         mismatch-weight -1}} ]
  5.   (let [s1 (str "-" seq1)
  6.         s2 (str "-" seq2)
  7.         match match-weight  ;;match
  8.         mis mismatch-weight ;;mismatch or gap
  9.         all-loc (for [i (range (count s1)) ;;initialize scoring array. similar to a sparse matrix
  10.                       j (range (count s2))]
  11.                   [i j])
  12.         fill-array (fn fill_array [locations s1 s2 match mis]
  13.                      (reduce (fn [m [i j]];;score array format
  14.                                (if (or (= i 0) (= j 0)) ;;the key values in map are k=[i j] and value=[score [where it came from i,j] char1 char2]
  15.                                  (assoc m [i j] [0 [0 0] "-" "-"]) ;;top row and first col are 0
  16.                                  (let [d (first (get m [(dec i) (dec j)])) ;;score match/mismatch (diagonal)
  17.                                        u (first (get m [(dec i) j])) ;;score deletion (above)
  18.                                        l (first (get m [i (dec j)])) ;;score insertion (left)
  19.                                        aa1 (subs s1 i (inc i)) ;;current char in s1
  20.                                        aa2 (subs s2 j (inc j)) ;;current char in s2
  21.                                        score (max (if (= aa1 aa2 ) ;;chooses how to calc current [i j] in matrix by choosing from d, u, l.
  22.                                                     (+ d match)
  23.                                                     (+ d mis))
  24.                                                   (+ u mis) (+ l mis))]
  25.                                    (assoc m [i j] ;;insertion of the best score into the matrix
  26.                                           (cond
  27.                                            (and (= d (max d u l)) (or (= (+ d mis) score) (= (+ d match) score)))
  28.                                            [score [(dec i) (dec j)] aa1 aa2]
  29.                                            (and (= u (max d u l)) (= (+ u mis) score))
  30.                                            [score [(dec i) j] aa1 "-"]
  31.                                            (and (= l (max d u l)) (= (+ l mis) score))
  32.                                            [score [i (dec j)] "-" aa2]
  33.                                            :else
  34.                                            [0 [0 0] "-" "-"])))))
  35.                              {} locations))
  36.         trace-back (fn trace_back [score start-loc H]
  37.                      (loop [loc start-loc
  38.                             aln_s1 ""
  39.                             aln_s2 ""]
  40.                        (let [next-loc (get H loc) ;;stores the next location [i j] to go to in H
  41.                              a1 (nth next-loc 2)
  42.                              a2 (nth next-loc 3)]
  43.                          (if (not= 0 (+ (first (second next-loc)) (second (second next-loc))))
  44.                            (recur (second next-loc)
  45.                                   (str a1 aln_s1) ;;builds strings up from the right to left
  46.                                   (str a2 aln_s2))
  47.                            (if (= "-" a1 a2)
  48.                              [score aln_s1 aln_s2]
  49.                              [score (str a1 aln_s1) (str a2 aln_s2)])))))
  50.         H (fill_array all-loc s1 s2 match mis) ;;creates score matrix
  51.         start (last (sort-by first (vals H))) ;;finds highest value
  52.         ;;in matrix
  53.         start-locs (map first (filter #(= start (val %)) H))] ;;starts traceback from this highest value
  54.     (map #(trace-back (first start) % H) start-locs)))