Created
June 7, 2014 02:30
-
-
Save DarwinAwardWinner/55495c87d2e882dfb133 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(IRanges) | |
| library(httr) | |
| library(jsonlite) | |
| version <- '0.3' | |
| collapse <- function(...) { | |
| paste(unlist(list(...)), sep=",", collapse=",") | |
| } | |
| MyGene <- setClass("MyGene", | |
| slots=list(base.url="character", delay="numeric", step="numeric", agent="character"), | |
| prototype=list(base.url="http://mygene.info/v2", delay=1, step=1000, agent=sprintf('R-httr_mygene.R/httr.%s', version))) | |
| validMyGeneObject <- function(object) { | |
| errors <- character(0) | |
| for (sn in c("base.url", "delay", "step", "agent")) { | |
| if (length(slot(object, sn)) != 1) | |
| errors <- c(errors, sprintf("Slot %s must have length 1", sn)) | |
| } | |
| if (length(errors) > 0) | |
| errors | |
| else | |
| TRUE | |
| } | |
| setValidity("MyGene", validMyGeneObject) | |
| .Get <- function(mygene, path, params=list()) { | |
| url <- paste([email protected], path, sep="") | |
| res <- GET(url, query=params, config=add_headers(c(`User-Agent`=mygene@agent))) | |
| str(res) | |
| if (res$status_code != 200) | |
| stop("Request returned unexpected status code ", res$status_code) | |
| content(res, "text") | |
| } | |
| .Post <- function(mygene, path, params=list()) { | |
| url <- paste([email protected], path, sep="") | |
| res <- POST(url, query=params, config=add_headers(c(`User-Agent`=mygene@agent))) | |
| str(res) | |
| if (res$status_code != 200) | |
| stop("Request returned unexpected status code ", res$status_code) | |
| content(res, "text") | |
| } | |
| setMethod("metadata", c(x="MyGene"), function(x, ...) { | |
| .Get(x, "/metadata") | |
| }) | |
| available.fields <- function(mygene) { | |
| fromJSON(metadata(mygene))$available_fields | |
| } | |
| setGeneric("getGene", signature=c("mygene"), | |
| function(geneid, fields, ..., return.as=c("JSON", "text"), mygene) standardGeneric("getGene")) | |
| setMethod("getGene", c(mygene="MyGene"), function(geneid, fields, ..., return.as=c("JSON", "text"), mygene) { | |
| return.as <- match.arg(return.as) | |
| params <- list(...) | |
| params$fields <- collapse(fields) | |
| res <- .Get(mygene, paste("/gene/", geneid, sep=""), params) | |
| switch(return.as, | |
| JSON=fromJSON(res), | |
| text=res) | |
| }) | |
| ## If nothing is passed for the mygene argument, just construct a | |
| ## default MyGene object and use it. | |
| setMethod("getGene", c(mygene="missing"), function(geneid, fields, ..., return.as=c("JSON", "text"), mygene) { | |
| mygene <- MyGene() | |
| getGene(geneid, fields, ..., mygene=mygene) | |
| }) | |
| mg <- MyGene() | |
| metadata(mg) | |
| available.fields(mg) | |
| getGene("1017", fields = 'symbol,name,taxid,entrezgene') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment