|
| 1 | +#' Post a vector of NCBI UIDs |
| 2 | +#' |
| 3 | +#' This function posts a vector of NCBI UIDs to the NCBI webservice. The purpose |
| 4 | +#' of this function is to generate an ncbi_uid object with web history which can |
| 5 | +#' then be used in downstream functions. |
| 6 | +#' @param uid numeric; a vector of NCBI UIDs. |
| 7 | +#' @param db character; the database the UIDs belong to. For options see |
| 8 | +#' `ncbi_dbs()`. |
| 9 | +#' @param batch_size integer; the number of search terms to query at once. If |
| 10 | +#' the number of search terms is larger than \code{batch_size}, the search terms |
| 11 | +#' are split into batches and queried separately. |
| 12 | +#' @param verbose logical; should verbose messages be printed to the console? |
| 13 | +#' @return An object of class \code{"ncbi_uid"} which is a list with three |
| 14 | +#' elements: |
| 15 | +#' \itemize{ |
| 16 | +#' \item \code{uid}: a vector of UIDs. |
| 17 | +#' \item \code{db}: the database the UIDs belong to. |
| 18 | +#' \item \code{web_history}: a tibble of web histories. |
| 19 | +#' } |
| 20 | +#' @details The default value for \code{batch_size} should work in most cases. |
| 21 | +#' However, if the vector of UIDs is very long, the function may fail with an |
| 22 | +#' error message. In this case, try reducing the \code{batch_size} value. |
| 23 | +#' @note In webseq NCBI UIDs are acquired using `ncbi_get_uid()` or |
| 24 | +#' `ncbi_link_uid()` and are used for acquiring linked UIDs in other NCBI |
| 25 | +#' databases or for retrieving the data itself. In some cases data retrieval is |
| 26 | +#' faster when using web history. However, you may have UIDs without a web |
| 27 | +#' history or your web history may expire. In this case you can use |
| 28 | +#' `ncbi_post_uid()` to generate a new ncbi_uid object with web history. |
| 29 | +#' @examples |
| 30 | +#' ncbi_post_uid(4505768, db = "biosample") |
| 31 | +#' @export |
| 32 | +ncbi_post_uid <- function( |
| 33 | + uid, |
| 34 | + db, |
| 35 | + batch_size = 100, |
| 36 | + verbose = getOption("verbose") |
| 37 | + ) { |
| 38 | + uid <- validate_ncbi_uid(uid) |
| 39 | + db <- match.arg(db, choices = ncbi_dbs()) |
| 40 | + uidlist <- get_idlist(uid, batch_size = batch_size) |
| 41 | + res <- lapply(uidlist, function(x) { |
| 42 | + res <- rentrez::entrez_post(db = db, id = x) |
| 43 | + out <- list( |
| 44 | + uid = x, |
| 45 | + web_history = tibble::tibble( |
| 46 | + WebEnv = res$WebEnv, |
| 47 | + QueryKey = res$QueryKey |
| 48 | + ) |
| 49 | + ) |
| 50 | + return(out) |
| 51 | + }) |
| 52 | + uid <- lapply(res, function(x) x$uid) |
| 53 | + web_histories <- lapply(res, function(x) x$web_history) |> |
| 54 | + dplyr::bind_rows() |
| 55 | + out <- list( |
| 56 | + uid = unlist(uid), |
| 57 | + db = db, |
| 58 | + web_history = web_histories |
| 59 | + ) |
| 60 | + class(out) <- c("ncbi_uid", class(out)) |
| 61 | + validate_webseq_class(out) |
| 62 | + return(out) |
| 63 | +} |
0 commit comments