POST allows you to run a query with multiple inputs at once. The output will be a list of data-frames.
library(httr)
library(jsonlite)
server <- "https://rest.ensembl.org"
ext <- "/lookup/id"
genes <- c("ENSG00000157764", "ENSG00000248378")
body_values <- toJSON(list(ids=genes))
r <- POST(paste(server, ext, sep = ""), content_type("application/json"), accept("application/json"), body = body_values)
prettify(content(r, "text", encoding = "UTF-8"))
There is a helper function in POST. You can specify both helper functions in your script and use whichever one you need.
fetch_endpoint_POST <- function(server, request, data, content_type='application/json'){
r <- POST(paste(server, request, sep = ""), content_type("application/json"), accept("application/json"), body = data)
stop_for_status(r)
if (content_type == 'application/json'){
return (fromJSON(content(r, "text", encoding = "UTF-8")))
} else {
return (content(r, "text", encoding = "UTF-8"))
}
}
In order to add optional parameters to your POST query, you can just add them onto the extention with a slash. For example if you wanted to mask UTRs when running the sequence_id_post endpoint, you could specify your extension as:
`ext <- "sequence/id/mask_feature=1"`
Your input list for POST queries need to be a JSON list. You can create this from a list in R using the jsonlite library:
`data <- toJSON(list(ids=mylist))`
The Output from POST queries will be a list of dataframes.
The following scripts inputs a list of variants in HGVS format into the VEP and gets out the IDs of known colocated variants, including failed variants (an optional parameter):
library(httr)
library(jsonlite)
fetch_endpoint <- function(server, request, content_type){
r <- GET(paste(server, request, sep = ""), accept(content_type))
stop_for_status(r)
if (content_type == 'application/json'){
return (fromJSON(content(r, "text", encoding = "UTF-8")))
} else {
return (content(r, "text", encoding = "UTF-8"))
}
}
fetch_endpoint_POST <- function(server, request, data, content_type='application/json'){
r <- POST(paste(server, request, sep = ""), content_type("application/json"), accept("application/json"), body = data)
stop_for_status(r)
if (content_type == 'application/json'){
return (fromJSON(content(r, "text", encoding = "UTF-8")))
} else {
return (content(r, "text", encoding = "UTF-8"))
}
}
# define the server, extension and content type
server <- "http://rest.ensembl.org/"
con <- "application/json"
vep_ext <- "vep/homo_sapiens/hgvs/failed=1"
# create the list of HGVS annotations
hgvs <- c("ENST00000366667.4:c.803T>C", "ENST00000335295.4:c.20A>T", "ENST00000415952.1:c.-149-34206G>T")
# convert the list into json format
hgvs_json <- toJSON(list(hgvs_notations=hgvs))
# run a post query with the list of variants
post_vep <- fetch_endpoint_POST(server, vep_ext, hgvs_json, con)
#
for (i in 1:nrow(post_vep)) {
variant <- post_vep[i, ]
input <- variant$input
colocated <- variant$colocated_variants
for (col in colocated) {
variant_ids = col$id
}
print (paste(input, variant_ids, sep = ": "))
}
1. Fetch the all the transcripts of ESPN using the lookup function. Fetch the cDNA sequences of all transcripts using a single POST request, and print in FASTA format.
# Exercise 6.1
2. You have the following list of variants:
rs1415919662, rs957333053, rs762944488, rs1372123943, rs553810871, rs1451237599, rs751376931
Get the variant class, evidence attributes, source and the most_severe_consequence for all variants using the variation POST endpoint.
# Exercise 6.2