Skip to content

Commit

Permalink
Generate taxon field from taxon_name when aggregating taxa
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOafidian authored Jan 11, 2024
1 parent e11304a commit 6f0ea07
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions R/handlers.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ aggregate_samples <- function(ta) {
#' @param rank An optional rank to aggregate on.
#' @export
aggregate_taxa <- function(ta, rank = NULL) {

# Temporarily replace any NA's with strings as they interfere with aggregation
ta$taxa[is.na(ta$taxa)] <- "unknown"

if (!is.null(rank)) {
rank_names <-
rank_names(ta) %>%
Expand All @@ -164,9 +168,6 @@ aggregate_taxa <- function(ta, rank = NULL) {
ta <- select_taxa(ta, taxon_id, !!rank_names_to_keep)
}

# this avoids some problems
ta$taxa[is.na(ta$taxa)] <- "unknown"

ta$taxa <-
ta$taxa %>%
chop(taxon_id) %>%
Expand Down Expand Up @@ -203,9 +204,18 @@ aggregate_taxa <- function(ta, rank = NULL) {
# cleanup
ta$taxa[ta$taxa == "unknown"] <- NA
# Adapt rank names to aggregate
ta %>% set_rank_names(
ta <- ta %>% set_rank_names(
rank_names(ta) %>% intersect(names(ta$taxa))
)
# Add new unique taxon label
if (!is.na(rank)){
include_species = eval(rank=="species")
ta <- ta %>%
add_taxon_name(include_species = include_species) %>%
mutate_taxa(taxon = taxon_name) %>%
select(-taxon_name)
}
ta
}

#' Trim all sequences
Expand Down

0 comments on commit 6f0ea07

Please sign in to comment.