Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/ena2ncbi.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Take a vector of ENA accessions and convert them to NCBI accessions.
#' @param accessions character; a vector or ENA accessions.
#' @param type character; type of accessions. Supported types: `sample`,
#' @param type character; type of ENA accessions. Supported types: `sample`,
#' `study`.
#' @return A tibble with two columns, `ena` and `ncbi`.
#' @examples
Expand Down
4 changes: 2 additions & 2 deletions R/ncbi2ena.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#'
#' Take a vector of NCBI accessions and convert them to ENA accessions.
#' @param accessions character; a vector or ENA accessions.
#' @param type character; type of accessions. Supported types: `biosample`,
#' `bioproject`.
#' @param type character; type of NCBI accessions. Supported types:
#' `biosample`, `bioproject`.
#' @return A tibble with two columns, `ncbi` and `ena`.
#' @examples
#' ncbi2ena("SAMEA111452506", type = "biosample")
Expand Down
4 changes: 1 addition & 3 deletions R/ncbi_get_meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ ncbi_get_meta <- function(
if ("ncbi_uid" %in% class(query)) {
query <- query$uid
}
if (!is.numeric(query)) {
stop("Query must be an ncbi_uid object or a numeric vector or UIDs.")
}
query <- as_numeric(query)
idlist <- get_idlist(query, batch_size, verbose)
res <- sapply(idlist, function(x) {
foo_from_ids(x, db = db)
Expand Down
4 changes: 1 addition & 3 deletions R/ncbi_get_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ ncbi_get_summary <- function(
}
return(res)
}
if (!is.numeric(query)) {
stop("Query must be an ncbi_uid object or a numeric vector or UIDs.")
}
query <- as_numeric(query)
idlist <- get_idlist(query, batch_size = batch_size, verbose = verbose)
res <- lapply(idlist, function(x) {
foo_from_ids(x, db = db, verbose = verbose)
Expand Down
6 changes: 2 additions & 4 deletions R/ncbi_link_uid.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ ncbi_link_uid <- function(
foo_from_ids <- function(x, from, to) {
if (length(x) == 1 && is.na(x)) {
if (verbose) message("No valid UIDs.")
return(tibble::tibble(query = x, uid = NA_real_))
return(tibble::tibble(query = x, uid = NA_integer_))
}
id_hit <- suppressWarnings(wrap(
"entrez_link",
Expand Down Expand Up @@ -131,9 +131,7 @@ ncbi_link_uid <- function(
} else {
query_vector <- query
}
if (!is.numeric(query_vector)) {
stop("Query must be an ncbi_uid object or a numeric vector or UIDs.")
}
query_vector <- as_numeric(query_vector)
idlist <- get_idlist(query_vector, batch_size, verbose)
res <- lapply(idlist, function(x) {
foo_from_ids(
Expand Down
4 changes: 3 additions & 1 deletion R/ncbi_recover_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ ncbi_recover_id <- function(
msg <- "Specify a 'db' argument or query an object of class 'ncbi_uid'."
stop(msg)
}
query <- as_numeric(query)
}
db <- match.arg(db, ncbi_dbs())
summaries <- ncbi_get_summary(
query,
db = db,
Expand All @@ -64,6 +64,8 @@ ncbi_recover_id <- function(
id <- unname(sapply(summaries, function(x) x$accessionversion))
} else if (db == "protein") {
id <- unname(sapply(summaries, function(x) x$accessionversion))
} else if (db == "pubmed") {
id <- unname(sapply(summaries, function(x) x$uid))
} else {
stop("Not supported.")
}
Expand Down
12 changes: 12 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,16 @@ get_mc_cores <- function(mc_cores, verbose = getOption("verbose")) {
mc_cores <- n_cores
}
return(mc_cores)
}

#' Convert a vector to numeric
#'
#' @param x vector; Vector to convert.
#' @noRd
as_numeric <- function(x) {
numeric_x <- suppressWarnings(as.numeric(x))
if (any(is.na(numeric_x) & !is.na(x))) {
stop("Query must be either an ncbi_uid object or a vector of UIDs.")
}
return(numeric_x)
}
Binary file modified data/examples.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion man/ena2ncbi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/ncbi2ena.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/test-ena2ncbi.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ test_that("ena2ncbi() removes invalid queries and returns the rest", {
)
expect_equal(dim(E), c(2,2))
})

test_that("ena2ncbi() works with studies", {
G <- ena2ncbi("ERP161024", type = "study")
expect_equal(dim(G), c(1,2))
expect_equal(names(G), c("ena", "ncbi"))
expect_equal(G$ncbi, "PRJEB76496")
})
8 changes: 8 additions & 0 deletions tests/testthat/test-ncbi_recover_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ test_that("ncbi_recover_id() works with nuccore", {
id <- ncbi_recover_id(uid)
expect_equal(id,"OP617744.1")
})

test_that("ncbi_recover_id() works with pubmed", {
uid <- ncbi_get_uid(examples$pubmed, db = "pubmed")
id1 <- ncbi_recover_id(uid)
id2 <- ncbi_recover_id(uid$uid, db = "pubmed")
expect_equal(id1, c("39332413", "36759752"))
expect_equal(id1, c("39332413", "36759752"))
})
Loading