Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

238 genus family lookup #239

Merged
merged 11 commits into from
Aug 13, 2024
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(align_taxa)
export(create_species_state_origin_matrix)
export(create_taxonomic_update_lookup)
export(default_version)
export(get_apc_genus_family_lookup)
export(load_taxonomic_resources)
export(native_anywhere_in_australia)
export(standardise_names)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# APCalign 1.0.2

Minor update to fix

- Deal with the vignette issues that emerged on CRAN
- Improve "graceful failing", based on issues that have come up on github CI
- minor formatting

# APCalign 1.0.1

First major release of APCalign. A preprint is available at
Expand Down
52 changes: 42 additions & 10 deletions R/state_diversity_counts.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' @title State- and territory-level diversity
#'
#'
#' @description
#' For Australian states and territories, use geographic distribution data from
#' the APC to calculate state-level diversity for native, introduced,
#' For Australian states and territories, use geographic distribution data from
#' the APC to calculate state-level diversity for native, introduced,
#' and more complicated species origins
#'
#' @family diversity methods
Expand All @@ -26,6 +26,7 @@
#'
#' @examples
#' \donttest{state_diversity_counts(state = "NSW")}

state_diversity_counts <- function(state,
resources = load_taxonomic_resources()) {

Expand Down Expand Up @@ -77,14 +78,45 @@ state_diversity_counts <- function(state,


#' @noRd
get_apc_genus_family_lookup <-
function(resources = load_taxonomic_resources()) {
apc_s <- dplyr::filter(resources$APC,
taxon_rank == "species")
dplyr::tibble(genus = word(apc_s$scientific_name, 1, 1),
family = apc_s$family) %>%
create_apc_genus_family_lookup <-
function(resources) {
apc_s <- dplyr::filter(resources$APC, taxon_rank == "species")
dplyr::tibble(genus = word(apc_s$accepted_name_usage, 1, 1),
family = apc_s$family) |>
dplyr::distinct() -> lu
return(lu)
}


#' @title Lookup Family by Genus from APC
#'
#' @description
#' Retrieve the family name for a given genus using taxonomic data from the
#' Australian Plant Census (APC).
#'
#' @param genus A character vector of genus names for which to retrieve the
#' corresponding family names.
#' @param resources The taxonomic resources required to make the lookup.
#' Loading this can be slow, so call \code{\link{load_taxonomic_resources}}
#' separately to speed up this function and pass the resources in.
#'
#' @return A data frame with two columns: "genus", indicating the genus name,
#' and "family", indicating the corresponding family name from the APC.
#'
#' @seealso \code{\link{load_taxonomic_resources}}
#'
#' @export
#'
#' @examples
#' \donttest{get_apc_genus_family_lookup(genus = c("Acacia", "Eucalyptus"))}
get_apc_genus_family_lookup <-
function(genus, resources = load_taxonomic_resources()) {
if (is.null(resources)) {
message("Not finding taxonomic resources; check internet connection?")
return(NULL)
}
fam_lu <- create_apc_genus_family_lookup(resources = resources)
lu <- dplyr::tibble(genus = genus) %>%
dplyr::left_join(fam_lu, by = "genus")
if (any(is.na(lu$family))) warning("some non-matches with the APC accepted genus list, check the formatting of your genus vector.")
return(lu)
}
30 changes: 30 additions & 0 deletions man/get_apc_genus_family_lookup.Rd

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

5 changes: 5 additions & 0 deletions tests/testthat/benchmarks/family_check.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
genus,family
Eucalyptus,Myrtaceae
Pinus,Pinaceae
Brassica,Brassicaceae
not a species,NA
19 changes: 19 additions & 0 deletions tests/testthat/test-state_diversity.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,22 @@ test_that("native_anywhere_in_australia() works", {
expect_equal(native_check, previous_check)
expect_warning(native_anywhere_in_australia(species = "NOTASPECIES", resources = resources))
})


test_that("get_apc_genus_family_lookup() works", {
expect_warning(family_check <-
get_apc_genus_family_lookup(
c(
"Eucalyptus",
"Pinus",
"Brassica",
"not a species"
),
resources = resources
))
# readr::write_csv(family_check,"tests/testthat/benchmarks/family_check.csv")
previous_check <- readr::read_csv("benchmarks/family_check.csv", show_col_types = FALSE)
expect_equal(family_check, previous_check)
})