Skip to content

Commit 4e29c25

Browse files
committed
Add function for posting UIDs to get a web history
1 parent 0c85fe1 commit 4e29c25

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export(ncbi_link_uid)
1818
export(ncbi_parse)
1919
export(ncbi_parse_assembly_xml)
2020
export(ncbi_parse_biosample_txt)
21+
export(ncbi_post_uid)
2122
export(ncbi_recover_id)
2223
export(parse_report)
2324
importFrom(curl,curl_download)

R/ncbi_post_uid.R

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

man/ncbi_post_uid.Rd

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)