Hi all. I am porting my ruby model to clojure, and wondered if any of
you could comment_on / rewrite_to_a_more_clojure-way the following
four functions.
The purpose of it all is to generate a regular expression, which is
ran past a string. This is used as a simple way to mimic the antigen
presentation pathway that recognizes certain patterns in viruses.
; some constants
(def amino_acids 10)
(def window_size 9)
(def virus_length 1000)
; generates a virus
; there is probably an easier way to create a random string
(reduce str (take virus_length (map #(char (+ (rand-int amino_acids)
65 %)) (cycle (list 0)))))
; I start with a fully indiscriminate gene (i.e. (10 10 10 10 10 10 10
10 10)),
; and reduce those numbers till the 1/multiplication of that whole
string is
; lower than the given specificity. This will generate a list that
when transformed
; into a regexp will for example only return matches for 10% of a
random string.
(defn construct-specificity
"reduces a list to a certain specificity"
[gene specificity]
(if (> (reduce * (map #(/ % amino_acids) gene)) specificity)
(construct-specificity (map (fn [_] (if (< (rand) (/ 0.1 (count
gene))) (max (dec _) 1) _)) gene) specificity)
gene))
; This one translates a number into a set of letters.
; 2 would be translated into #{\C \F}, or #{\A \D}
; etc. In this way we can translate (1 5 3 2 6) into
; something like (#{\C} #{\A \C \F \G \H} #{\B \C \F} etc..
(defn construct-atom
"translates a number n into an set of letters of size n"
[construct length]
(if (< (count construct) length)
(construct-atom (conj construct (char (+ (rand-int amino_acids)
65))) length)
construct))
; This turns the seq of sets into a regexp
(defn construct-regexp
"creates a gene (i.e. a list of sets) and forms it into a regexp"
[specificity]
(let [gene (map #(reduce str (construct-atom #{} %)) (construct-
specificity (replicate window_size amino_acids) specificity))]
(re-pattern (str "(?=(" (reduce str (map #(str "[" % "]") gene))
"))"))))
; done! works!
(re-seq (construct-regexp 0.3)
"ABCCABABABCDADFAFDACCCADFABADFASACSADFBA")
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---