Skip to content

Instantly share code, notes, and snippets.

@timyates
Created June 14, 2010 13:12
Show Gist options
  • Save timyates/437658 to your computer and use it in GitHub Desktop.
Save timyates/437658 to your computer and use it in GitHub Desktop.
(ns user)
(defn- reverse-base [base]
(let [mapping { :A \T :T \A :G \C :C \G }
key (keyword (.toString base))]
(key mapping)))
(defn reverse-complement [sequence]
(let [rev-seq (reverse (.toUpperCase sequence))]
(map reverse-base rev-seq)))
(println "Reverse complement gives" (reverse-complement "TGAC"))
@jandot
Copy link

jandot commented Jun 14, 2010

Nice. My version reads like (println "Revcomp:" (reverse-str (complement "TGAC")))

My problem: how to use a "bio" namespace here? The "complement" function is already defined in clojure/core, so gives issues. Am trying to do something like (ns bio), but then it can't find "defn" anymore...

can't def complement because namespace: bio refers to:#'clojure.core/complement

jan.

@timyates
Copy link
Author

You might be able to use something like the following to exclude 'complement' from the namespace:

http://stackoverflow.com/questions/2832582/how-to-rename-an-operation-in-clojure/2834069#2834069

(That example excludes + but the principle should be the same)

To be honest though, I'm not sure... Namespaces are the one major thing in clojure which don't currently fit in my brain (which comes from C, Java and Groovy which all seem to follow the same or similar forms) :-(

@jandot
Copy link

jandot commented Jun 14, 2010

Thanks for that link. Does help to clear it up at least a little bit for me. Trouble is that the core 'complement' cannot be easily accessed then (apart from prepending "clojure.core/"). Would be nice if we could say:
(println bio/complement(bio/reverse-str "TGACC"))
i.e. adding "bio" in front.

@jandot
Copy link

jandot commented Jun 14, 2010

@wmacgyver: This is the code that we're trying to get going:


  (ns bio)
  (defn reverse-str
    "Reverse a string"
    [s]
    (apply str (reverse s))
  )
  
  (defn complement
    "Complement a base"
    [base]
    (let [mapping { :A \T :T \A :G \C :C \G }
      key (keyword (.toString base))]
      (key mapping)))
  )
  
  (println bio/complement (bio/reverse-str "TGACC"))

Output should be: "GGTCA"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment