Created
January 25, 2024 17:22
-
-
Save tomsing1/0a637df48c157b1dcfb9fc2a9de60e1b to your computer and use it in GitHub Desktop.
Retrieve sample attributes from EBI's ENA repository in JSON format
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(glue) | |
library(httr) | |
library(jsonlite) | |
library(xml2) | |
#' Query ENA's REST API for information about records | |
#' | |
#' @param accessions Character vector of one or more ENA record identifiers | |
#' @return An `xml_document` object | |
get_records <- function(accessions) { | |
r <- httr::GET( | |
url = glue::glue( | |
"https://www.ebi.ac.uk/ena/browser/api/xml/", | |
"{glue::glue_collapse(accessions, sep = ',')}"), | |
httr::content_type("text/xml") | |
) | |
httr::stop_for_status(r) | |
httr::content(r, as = "parsed", type = "text/xml", | |
encoding = "UTF-8") | |
} | |
# return a list of JSON strings with sample attributes | |
sample_ids <- c("SAMN12207420", "SAMN08358280", "SAMN06470116") | |
json <- lapply(setNames(sample_ids, sample_ids), \(sample_id) { | |
# retrieve the record in XML format | |
get_records(sample_id) |> | |
# extract the attributes | |
xml2::xml_find_all(xpath = "//SAMPLE_ATTRIBUTES") |> | |
xml2::as_list() |> | |
# convert into JSON | |
jsonlite::toJSON(auto_unbox = TRUE, pretty = TRUE) | |
}) | |
json[[2]] # second sample | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment