Skip to content

Instantly share code, notes, and snippets.

@timyates
Created March 12, 2010 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timyates/330312 to your computer and use it in GitHub Desktop.
Save timyates/330312 to your computer and use it in GitHub Desktop.
; Amaturish Clojure version of uniprot mapping function by @sjcockell
; http://blog.fuzzierlogic.com/archives/339
; http://gist.github.com/329730
(ns uniprot
(:use clojure.contrib.duck-streams)
(:import java.net.URLEncoder))
(defn- url-encode
"Passes a String through java.net.URLEncoder.encode"
[ param ] (URLEncoder/encode param))
(defn- map-to-query
"Convert a map to a query string"
[ a-map ]
(let [ key-set (keys a-map) ]
(reduce
(fn [ accum curr-key ]
(let [ key (if (keyword? curr-key) (name curr-key) curr-key) val (curr-key a-map) ]
(str accum (url-encode key) "=" (url-encode val) (if (= curr-key (last key-set)) "" "&"))))
"" key-set)))
(defn uniprot-mapping
"Call the uniprot webservice and convert the given ID between the given Uniprot types"
[ from to query ]
(slurp* (str "http://www.uniprot.org/mapping?" (map-to-query { :from from :to to :query query :format "tab" }))))
(println (uniprot-mapping "ENSEMBL_ID" "ACC" "ENSG00000141510"))
// Groovy version of uniprot mapping function by @sjcockell
// http://blog.fuzzierlogic.com/archives/339
// http://gist.github.com/329730
import java.net.URLEncoder as U
def uniprot_mapping( fromtype, totype, identifier ) {
base = 'http://www.uniprot.org'
tool = 'mapping'
params = [ from : fromtype,
to : totype,
format : 'tab',
query : identifier ]
params = params.collect { "${U.encode(it.key)}=${U.encode(it.value)}" }.join( '&' )
new URL( "$base/$tool?$params" ).text
}
// For mapping example types, see:
// http://www.uniprot.org/faq/28#id_mapping_examples
println uniprot_mapping( 'ENSEMBL_ID', 'ACC', 'ENSG00000141510' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment