Skip to content

Commit

Permalink
Changed default asv_as_id = T. Made sure that when tax_level is defin…
Browse files Browse the repository at this point in the history
…ed, no duplicates occur. Workaround for issue #10.
  • Loading branch information
gmteunisse committed Sep 26, 2022
1 parent c1b4fa6 commit b7df1e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
28 changes: 18 additions & 10 deletions R/label_duplicate_taxa.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#' ps_tmp <- label_duplicate_taxa(GlobalPatterns, tax_level = "Species")
#' View(tax_table(ps_tmp))
#' @export
label_duplicate_taxa <- function(ps_obj, tax_level = NULL, asv_as_id = F,
label_duplicate_taxa <- function(ps_obj, tax_level = NULL, asv_as_id = T,
duplicate_label = "<tax> <id>"){

# Check arguments
Expand All @@ -47,15 +47,18 @@ label_duplicate_taxa <- function(ps_obj, tax_level = NULL, asv_as_id = F,

# Find duplicate taxa and flag for labeling
tax_tab <- tax_table(ps_obj) %>%
data.frame(row_name = row.names(.)) %>%
group_by(across(-row_name)) %>%
mutate(n = row_number(),
dupl = n > 1) %>%
mutate(add_label = sum(dupl) > 0)
data.frame(row_name = row.names(.))

# Add label to the lowest non-NA taxonomic level
if(is.null(tax_level)){

# Find duplicates
tax_tab <- tax_tab %>%
group_by(across(-row_name)) %>%
mutate(n = row_number(),
dupl = n > 1) %>%
mutate(add_label = sum(dupl) > 0)

# Convert to long
tax_tab <- tax_tab %>%
pivot_longer(!c(row_name, n, dupl, add_label),
Expand All @@ -71,7 +74,7 @@ label_duplicate_taxa <- function(ps_obj, tax_level = NULL, asv_as_id = F,
tax),
tax = str_replace(label, "<tax>", tax))

# Fill in id field
# Fill in id field with either the row number or the ASV ID (row_name)
if(asv_as_id){
tax_tab <- tax_tab %>%
mutate(tax = str_replace(tax, "<id>", as.character(row_name)))
Expand All @@ -86,9 +89,16 @@ label_duplicate_taxa <- function(ps_obj, tax_level = NULL, asv_as_id = F,
select(!c(n, dupl, add_label, label)) %>%
pivot_wider(names_from = rank, values_from = tax)

# Add a label to a specific taxonomic rank
# Add a label to a specific taxonomic rank
} else {

# Find duplicates
tax_tab <- tax_tab %>%
group_by(!!sym(tax_level)) %>%
mutate(n = row_number(),
dupl = n > 1) %>%
mutate(add_label = sum(dupl) > 0)

# Add label and fill in tax field
tax_tab <- tax_tab %>%
mutate(label = ifelse(add_label,
Expand Down Expand Up @@ -119,6 +129,4 @@ label_duplicate_taxa <- function(ps_obj, tax_level = NULL, asv_as_id = F,
return(ps_obj)




}
2 changes: 1 addition & 1 deletion man/label_duplicate_taxa.Rd

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

0 comments on commit b7df1e0

Please sign in to comment.