Commit 194946db authored by Matija Obreza's avatar Matija Obreza

list_species() fetches taxonomic data of selected accessions

parent 5ca33a29
......@@ -12,11 +12,13 @@ export(download_pdci)
export(fetch_accessions)
export(filter_DOI)
export(filter_GENUS)
export(filter_INSTCODE)
export(filter_ORIGCTY)
export(filter_SAMPSTAT)
export(filter_SPECIES)
export(get_accessions)
export(list_crops)
export(list_species)
export(mcpd_filter)
export(me)
export(print_setup)
......
......@@ -41,6 +41,7 @@ MCPD <- list(
#' See FAO/Bioversity Multi-Crop Passport Descriptors.
#'
#' @param filter Existing filters (or blank list if not provided)
#' @param INSTCODE WIEWS Institute Code of the holding institute
#' @param DOI Accession DOI
#' @param ORIGCTY Country of origin
#' @param SAMPSTAT Biological status of sample
......@@ -53,7 +54,7 @@ MCPD <- list(
#'
#'
#' @export
mcpd_filter <- function(filter = list(), DOI = NULL, ORIGCTY = NULL, SAMPSTAT = NULL, GENUS = NULL, SPECIES = NULL) {
mcpd_filter <- function(filter = list(), INSTCODE = NULL, DOI = NULL, ORIGCTY = NULL, SAMPSTAT = NULL, GENUS = NULL, SPECIES = NULL) {
f <- c(filter)
f <- filter_DOI(f, DOI)
......@@ -61,7 +62,7 @@ mcpd_filter <- function(filter = list(), DOI = NULL, ORIGCTY = NULL, SAMPSTAT =
f <- filter_SAMPSTAT(f, SAMPSTAT)
f <- filter_GENUS(f, GENUS)
f <- filter_SPECIES(f, SPECIES)
f <- filter_INSTCODE(f, INSTCODE)
f
}
......@@ -126,3 +127,16 @@ filter_SPECIES <- function(filter = list(), SPECIES) {
}
f
}
#' Add filter by genus
#' @param filter Existing filters (or blank list if not provided)
#' @param INSTCODE List of WIEWS institute codes
#' @export
filter_INSTCODE <- function(filter = list(), INSTCODE) {
f <- c(filter)
if (!is.null(INSTCODE)) {
f$institute$code = c(f$institute$code, INSTCODE)
}
f
}
......@@ -325,3 +325,43 @@ list_crops <- function() {
data
}
#' Fetch taxonomic data of selected accessions.
#'
#' @param filters an R \code{structure} with Genesys filters
#'
#' @examples
#' \dontrun{
#' # Retrieve taxa of selected accessions
#' taxa <- genesysr::list_species(mcpd_filter(INSTCODE = c("LBN002", "MEX002")))
#' }
#'
#' @seealso \code{\link{mcpd_filter}}
#'
#' @export
#' @return Taxonomic records of selected accessions
list_species <- function(filters = list()) {
start_time <- as.numeric(as.numeric(Sys.time())*1000, digits=15)
resp <- .post(path = api1_url("/acn/species"), body = filters, accept = "text/csv")
if (httr::status_code(resp) != 200) {
stop("Genesys responded with HTTP status code ", httr::status_code(resp), ". Expected 200.")
}
if (httr::http_type(resp) != "text/csv") {
stop("API returned ", httr::http_type(resp), ". Expected text/csv.")
}
end_time <- as.numeric(as.numeric(Sys.time())*1000, digits=15)
headers <- httr::headers(resp)
message(paste("Retrieved taxonomic data in", end_time - start_time, "ms."))
body <- httr::content(resp, "text")
if (nchar(trimws(body)) == 0) {
message(paste("Received 0 bytes"))
data <- data.frame()
} else {
data <- read.csv(text = body, quote = '"', sep = '\t', stringsAsFactors = FALSE)
}
data
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment