grunwaldlab / metacoder

Parsing, Manipulation, and Visualization of Metabarcoding/Taxonomic data
http://grunwaldlab.github.io/metacoder_documentation
Other
135 stars 28 forks source link

Add `remove_redundant_names` back to metacoder #181

Closed zachary-foster closed 6 years ago

zachary-foster commented 6 years ago
#' Remove the redundant taxon names
#' 
#' Remove the names of parent taxa in the begining of their children's names in a \code{classified} object.
#' This is useful for remove genus names in species binomials.
#' 
#' @param obj a \code{taxmap} object
#' 
#' @return \code{character} 
#' 
#' @export
remove_redundant_names <- function(obj) {
  new_names <- vapply(supertaxa(obj, recursive = FALSE, include_input = TRUE), 
                      function(x) gsub(obj$taxon_names()[x[1]],
                                       pattern = paste0("^", obj$taxon_names()[x[2]], "[_ ]+"),
                                       replacement = ""),
                      character(1))
  lapply(seq_along(new_names), function(i) {
    obj$taxa[[i]]$name$name <- new_names[i]
  })
  return(obj)
}
zachary-foster commented 6 years ago

added to taxa instaead