Skip to content

Commit

Permalink
#508 work on integrating taxa ropensci/taxa#197
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Sep 2, 2020
1 parent 26562bb commit c1c2e32
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 13 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ Imports:
crayon,
cli,
phangorn,
conditionz
conditionz,
taxa
Suggests:
testthat,
knitr,
rmarkdown,
vegan,
vcr
Remotes: ropensci/taxa@vectorize
RoxygenNote: 7.1.1
Roxygen: list(markdown = TRUE)
X-schema.org-applicationCategory: Taxonomy
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ S3method(as.data.frame,pow)
S3method(as.data.frame,tolid)
S3method(as.data.frame,tpsid)
S3method(as.data.frame,tsn)
S3method(as.data.frame,txid)
S3method(as.data.frame,uid)
S3method(as.data.frame,wiki)
S3method(as.data.frame,wormsid)
Expand Down
92 changes: 81 additions & 11 deletions R/get_gbifid.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,35 @@
#' xx
#'
#' # multiple names
#' get_gbifid(c("Poa annua", "Pinus contorta"))
#' x <- get_gbifid(c("Poa annua", "Pinus contorta", "adf"))
#' x
#' as.data.frame(x) # returns a tibble
#' nms <- c("Poa", "Abies magnifica", "Pinaceae", "Pinopsida", "Eukaryota", "Abies")
#' w <- get_gbifid(nms)
#' w
#' as.data.frame(w)
#' class(w)
#'
#' # extract parts (maintains the txid/taxa_taxon class)
#' library(taxa)
#' tax_rank(w)
#' as.character(tax_rank(w))
#' tax_id(w)
#' as.character(tax_id(w))
#' tax_name(w)
#' as.character(tax_name(w))
#'
#' # subset (maintains the txid/taxa_taxon class)
#' w[1]
#' w[1:3]
#' w[tax_rank(w) > 'genus']
#' names(w) <- letters[1:6]
#' w
#' w[c('b', 'c')]
#'
#' ## convert to taxonomy object
#' taxonomy(w)
#'
#'
#' # specify rows to limit choices available
#' get_gbifid(sci='Pinus')
Expand Down Expand Up @@ -167,30 +195,39 @@ get_gbifid <- function(sci, ask = TRUE, messages = TRUE, rows = NA,
)
mm <- NROW(df) > 1

if (is.null(df)) df <- data.frame(NULL)
if (is.null(df) || NROW(df) == 0) df <- data.frame(NULL)

if (nrow(df) == 0) {
if (NROW(df) == 0) {
mssg(messages, m_not_found_sp_altclass)
id <- NA_character_
name <- NA_character_
rank_taken <- NA_character_
att <- "not found"
} else {
names(df)[1] <- "gbifid"
id <- df$gbifid
name <- df$canonicalname
rank_taken <- df$rank
att <- "found"
}

# not found
if (length(id) == 0) {
mssg(messages, m_not_found_sp_altclass)
id <- NA_character_
name <- NA_character_
rank_taken <- NA_character_
att <- "not found"
}

if (length(id) > 1) {
# check for exact match
matchtmp <- df[as.character(df$canonicalname) %in% sci[i], "gbifid"]
matchtmp <- df[as.character(df$canonicalname) %in% sci[i], ]
if (length(matchtmp) == 1) {
id <- as.character(matchtmp)
# id <- as.character(matchtmp)
id <- as.character(matchtmp$gbifid)
name <- matchtmp$canonicalname
rank_taken <- matchtmp$rank
direct <- TRUE
} else {
if (!is.null(phylum) || !is.null(class) || !is.null(order) ||
Expand All @@ -205,11 +242,14 @@ get_gbifid <- function(sci, ask = TRUE, messages = TRUE, rows = NA,
df <- sub_rows(df, rows)
if (NROW(df) == 0) {
id <- NA_character_
name <- NA_character_
rank_taken <- NA_character_
att <- "not found"
} else {
id <- df$gbifid
if (length(id) == 1) {
rank_taken <- as.character(df$rank)
name <- df$canonicalname
att <- "found"
}
}
Expand Down Expand Up @@ -239,9 +279,13 @@ get_gbifid <- function(sci, ask = TRUE, messages = TRUE, rows = NA,
message("Input accepted, took gbifid '",
as.character(df$gbifid[take]), "'.\n")
id <- as.character(df$gbifid[take])
name <- df$scientificname[take] %||% df$canonicalname[take]
rank_taken <- df$rank[take]
att <- "found"
} else {
id <- NA_character_
name <- NA_character_
rank_taken <- NA_character_
att <- "not found"
mssg(messages, "\nReturned 'NA'!\n\n")
}
Expand All @@ -250,25 +294,35 @@ get_gbifid <- function(sci, ask = TRUE, messages = TRUE, rows = NA,
warning(sprintf(m_more_than_one_found, "gbifid", sci[i]),
call. = FALSE)
id <- NA_character_
name <- NA_character_
rank_taken <- NA_character_
att <- m_na_ask_false
}
}
}
}
}
res <- list(id = id, att = att, multiple = mm, direct = direct)
res <- list(id = id, name = name, rank = rank_taken, att = att,
multiple = mm, direct = direct)
prog$completed(sci[i], att)
prog$prog(att)
tstate$add(sci[i], res)
}
out <- tstate$get()
ids <- structure(as.character(unlist(pluck(out, "id"))), class = "gbifid",
match = pluck_un(out, "att", ""),
multiple_matches = pluck_un(out, "multiple", logical(1)),
pattern_match = pluck_un(out, "direct", logical(1)))
ids <- as.character(unlist(pluck(out, "id")))
ids <- replace_nas(ids, "999")
res <- .taxa_taxon(
name = unlist(pluck(out, "name")),
id = taxa::taxon_id(ids, db = "gbif"),
rank = unlist(pluck(out, "rank")),
uri = sprintf(get_url_templates$gbif, ids),
match = unname(unlist(pluck(out, "att"))),
multiple_matches = unname(unlist(pluck(out, "multiple"))),
pattern_match = unname(unlist(pluck(out, "direct")))
)
on.exit(prog$prog_summary(), add = TRUE)
on.exit(tstate$exit, add = TRUE)
add_uri(ids, get_url_templates$gbif)
return(res)
}

#' @export
Expand Down Expand Up @@ -311,6 +365,22 @@ as.data.frame.gbifid <- function(x, ...){
stringsAsFactors = FALSE)
}

#' @export
#' @rdname get_gbifid
as.data.frame.txid <- function(x, ...){
tibble::as_tibble(
data.frame(ids = as.character(taxa::tax_id(x)),
name = as.character(taxa::tax_name(x)),
rank = unname(as.character(taxa::tax_rank(x))),
uri = txz_uri(x),
match = txz_match(x),
multiple_matches = txz_mm(x),
pattern_match = txz_pm(x),
stringsAsFactors = FALSE)
)
}


make_gbifid <- function(x, check=TRUE) make_generic(x, 'https://www.gbif.org/species/%s', "gbifid", check)

check_gbifid <- function(x){
Expand Down
91 changes: 91 additions & 0 deletions R/taxa_taxon.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
.new_taxa_taxon <- function(.names = NULL, name = character(),
rank = taxa::taxon_rank(), id = taxa::taxon_id(), auth = taxa::taxon_authority(),
uri = character(), match = character(),
multiple_matches = character(), pattern_match = character(), ...) {

# Set names to NA if not set
if (is.null(names) || all(is.na(.names))) {
.names_set <- FALSE
.names <- vctrs::vec_recycle(NA_character_, length(name))
} else {
.names_set <- TRUE
vctrs::vec_assert(.names, ptype = character())
}

# Check that values are the correct type
vctrs::vec_assert(name, ptype = character())
vctrs::vec_assert(id, ptype = taxa::taxon_id())
vctrs::vec_assert(auth, ptype = taxa::taxon_authority())
vctrs::vec_assert(uri, ptype = character())
vctrs::vec_assert(match, ptype = character())
vctrs::vec_assert(multiple_matches, ptype = logical())
vctrs::vec_assert(pattern_match, ptype = logical())

vctrs::new_rcrd(
list(.names = .names, name = name, rank = rank,
id = id, auth = auth, uri = uri, match = match,
multiple_matches = multiple_matches, pattern_match = pattern_match),
.names_set = .names_set, ..., class = c("txid", "gbif", "taxa_taxon"))
}


.taxa_taxon <- function(name = character(0), rank = NA, id = NA,
auth = NA, .names = NA, uri = NA, match = NA,
multiple_matches = NA, pattern_match = NA, ...) {

# Cast inputs to correct values
name <- vctrs::vec_cast(name, character())
rank <- vctrs::vec_cast(rank, taxa::taxon_rank())
id <- vctrs::vec_cast(id, taxa::taxon_id())
auth <- vctrs::vec_cast(auth, taxa::taxon_authority())
uri <- vctrs::vec_cast(uri, character())
match <- vctrs::vec_cast(match, character())
multiple_matches <- vctrs::vec_cast(multiple_matches, logical())
pattern_match <- vctrs::vec_cast(pattern_match, logical())
.names <- vctrs::vec_cast(.names, character())

# Recycle ranks and databases to common length
recycled <- vctrs::vec_recycle_common(name, rank, id, auth, uri,
match, multiple_matches, pattern_match, .names)
name <- recycled[[1]]
rank <- recycled[[2]]
id <- recycled[[3]]
auth <- recycled[[4]]
uri <- recycled[[5]]
match <- recycled[[6]]
multiple_matches <- recycled[[7]]
pattern_match <- recycled[[8]]
.names <- recycled[[9]]

# Create taxon object
.new_taxa_taxon(.names = .names, name = name, rank = rank, id = id,
auth = auth, uri = uri, match = match,
multiple_matches = multiple_matches, pattern_match = pattern_match, ...)
}


vec_cast.txid <- function(x, to, ..., x_arg, to_arg) {
UseMethod("vec_cast.txid")
}

vec_cast.txid.default <- function(x, to, ..., x_arg, to_arg) {
vctrs::vec_default_cast(x, to, x_arg, to_arg)
}

vec_cast.txid.txid <- function(x, to, ..., x_arg, to_arg) x

vec_cast.txid.character <- function(x, to, ..., x_arg, to_arg) taxa::taxon(x)

vec_cast.character.txid <- function(x, to, ..., x_arg, to_arg) {
as.character(vctrs::field(x, "name"))
}

txz_named_field <- function(x, name) {
out <- vctrs::field(x, name)
if (!is.null(names(x))) names(out) <- names(x)
return(out)
}
txz_uri <- function(x) txz_named_field(x, "uri")
txz_match <- function(x) txz_named_field(x, "match")
txz_mm <- function(x) txz_named_field(x, "multiple_matches")
txz_pm <- function(x) txz_named_field(x, "pattern_match")
5 changes: 5 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,8 @@ pchk <- function(from, to, pkg_version = "v0.9.97") {
if (!is.null(from))
taxize_ck$handle_conditions(warning(mssg))
}

replace_nas <- function(x, with = "") {
x[is.na(x)] <- with
return(x)
}
33 changes: 32 additions & 1 deletion man/get_gbifid.Rd

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

0 comments on commit c1c2e32

Please sign in to comment.