If you can pull a datapoint from the json, you can use it as input for another endpoint.
For example, this script gets the genomic locus of a gene then looks up repeats overlapping it:
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"))
}
}
server <- "http://rest.ensembl.org/"
gene_ext <- "lookup/id/TraesCS3D02G007500?"
json <- "application/json"
get_gene <- fetch_endpoint(server, gene_ext, json)
locus = paste(get_gene$species, "/", get_gene$seq_region_name, ":",get_gene$start, "..", get_gene$end, sep="")
ext_get_bed <- paste("overlap/region/", locus, "?feature=repeat", sep = "")
get_bed <- fetch_endpoint(server, ext_get_bed, "text/x-bed")
# print the bed file
cat(get_bed)
1. Using the script from 3.1, add a call to fetch and print the sequence for the gene BCH1 in FASTA format.
# Exercise 5.1
2. Print the ID of any variants that overlap the region 5000 bp upstream of the Brachypodium BRADI_5g16840v3 gene. (Hints: get the gene ID first, then check the strand of the gene to see which way is upstream.)
# Exercise 5.2