Skip to content

Commit

Permalink
Make messages to console optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dfalster committed Apr 30, 2024
1 parent b4b13de commit 0cc6663
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 43 deletions.
33 changes: 20 additions & 13 deletions R/align_taxa.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' @param full Parameter to determine how many columns are output
#' @param resources the taxonomic resources used to align the taxa names. Loading this can be slow,
#' so call \code{\link{load_taxonomic_resources}} separately to greatly speed this function up and pass the resources in.
#' @param quiet Logical to indicate whether to display messages while aligning taxa.
#' @param fuzzy_abs_dist The number of characters allowed to be different for a fuzzy match.
#' @param fuzzy_rel_dist The proportion of characters allowed to be different for a fuzzy match.
#' @param fuzzy_matches Fuzzy matches are turned on as a default. The relative and absolute distances allowed for fuzzy matches to species and infraspecific taxon names are defined by the parameters `fuzzy_abs_dist` and `fuzzy_rel_dist`
Expand Down Expand Up @@ -69,17 +70,20 @@ align_taxa <- function(original_name,
output = NULL,
full = FALSE,
resources = load_taxonomic_resources(),
quiet = FALSE,
fuzzy_abs_dist = 3,
fuzzy_rel_dist = 0.2,
fuzzy_matches = TRUE,
imprecise_fuzzy_matches = FALSE,
APNI_matches = TRUE,
identifier = NA_character_) {

message("Checking alignments of ", dplyr::n_distinct(original_name, na.rm = TRUE), " taxa\n")
if(!quiet)
message("Checking alignments of ", dplyr::n_distinct(original_name, na.rm = TRUE), " taxa\n")

if (!is.null(output) && file.exists(output)) {
message(" - reading existing data from ", output)
if(!quiet)
message(" - reading existing data from ", output)

taxa_raw <-
readr::read_csv(
Expand Down Expand Up @@ -158,17 +162,18 @@ align_taxa <- function(original_name,
dplyr::filter(!duplicated(original_name))

if (all(taxa$tocheck$checked)|all(is.na(taxa$tocheck$checked))) {
message(" - all taxa are already checked, yay!")
if(!quiet)
message(" - all taxa are already checked, yay!")
return(invisible(taxa$tocheck))
}

# move all checked taxa to "checked"
taxa <- redistribute(taxa)

# messages if there is an saved list being added to
if (!is.null(output) && file.exists(output) && !all(taxa$tocheck$checked)) {
# check unknown taxa
message(
if (!is.null(output) && file.exists(output) && !all(taxa$tocheck$checked) && !quiet) {
# check unknown taxa
message(
" -> ",
crayon::blue(sum(!is.na(taxa$checked$accepted_name), na.rm = T)),
" names already matched; ",
Expand All @@ -190,12 +195,13 @@ align_taxa <- function(original_name,
filter(original_name %in% resources$`APC list (accepted)`$canonical_name) %>%
distinct() %>%
nrow()

message(
" -> of these ",
crayon::blue(perfect_matches),
" names have a perfect match to a scientific name in the APC. Alignments being sought for remaining names."
)

if(!quiet)
message(
" -> of these ",
crayon::blue(perfect_matches),
" names have a perfect match to a scientific name in the APC. Alignments being sought for remaining names."
)
}

# do the actual matching
Expand Down Expand Up @@ -228,7 +234,8 @@ align_taxa <- function(original_name,
taxa$checked<-TRUE
taxa$known<-!is.na(taxa$aligned_name)
readr::write_csv(taxa, output)
#message(" - output saved in file: ", output)
if(!quiet)
message(" - output saved in file: ", output)
}

return(taxa)
Expand Down
6 changes: 5 additions & 1 deletion R/create_taxonomic_update_lookup.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#' @param APNI_matches Name matches to the APNI (Australian Plant Names Index) are turned off as a default.
#' @param imprecise_fuzzy_matches Imprecise fuzzy matches are turned on as a default.
#' @param identifier A dataset, location or other identifier, which defaults to NA.
#' @param quiet Logical to indicate whether to display messages while aligning taxa.
#' @param output file path to save the output. If this file already exists, this function will check if it's a subset of the species passed in and try to add to this file. This can be useful for large and growing projects.
#' @return A lookup table containing the accepted and suggested names for each original name input, and additional taxonomic information such as taxon rank, taxonomic status, taxon IDs and genera.
#' - original_name: the original plant name.
Expand Down Expand Up @@ -58,6 +59,7 @@ create_taxonomic_update_lookup <- function(taxa,
imprecise_fuzzy_matches = FALSE,
identifier = NA_character_,
resources = load_taxonomic_resources(),
quiet = FALSE,
output = NULL) {

validate_taxonomic_splits_input(taxonomic_splits)
Expand All @@ -67,12 +69,14 @@ create_taxonomic_update_lookup <- function(taxa,
APNI_matches = APNI_matches,
identifier = identifier,
imprecise_fuzzy_matches = imprecise_fuzzy_matches,
quiet = quiet,
output=output)

updated_data <-
update_taxonomy(aligned_data,
taxonomic_splits = taxonomic_splits,
resources = resources,
resources = resources,
quiet = quiet,
output = output)

if (!full) {
Expand Down
6 changes: 4 additions & 2 deletions R/update_taxonomy.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' most_likely_species, which returns the species name in use before the split; alternative names are returned in a separate column
#' return_all, which returns all possible names
#' collapse_to_higher_taxon, which declares that an ambiguous name cannot be aligned to an accepted species/infraspecific name and the name is demoted to genus rank
#'
#' @param quiet Logical to indicate whether to display messages while updating taxa.
#' @param output (optional) Name of the file where results are saved. The default is NULL and no file is created.
#' If specified, the output will be saved in a CSV file with the given name.
#'
Expand Down Expand Up @@ -66,6 +66,7 @@

update_taxonomy <- function(aligned_data,
taxonomic_splits = "most_likely_species",
quiet = TRUE,
output = NULL,
resources = load_taxonomic_resources()) {

Expand Down Expand Up @@ -194,7 +195,8 @@ update_taxonomy <- function(aligned_data,
taxa_out$checked<-TRUE
taxa_out$known<-!is.na(taxa_out$accepted_name)
readr::write_csv(taxa_out, output)
message(" - output saved in file: ", output)
if(!quiet)
message(" - output saved in file: ", output)
}

taxa_out
Expand Down
3 changes: 3 additions & 0 deletions man/align_taxa.Rd

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

3 changes: 3 additions & 0 deletions man/create_taxonomic_update_lookup.Rd

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

3 changes: 3 additions & 0 deletions man/update_taxonomy.Rd

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

Loading

0 comments on commit 0cc6663

Please sign in to comment.