Skip to content

Commit

Permalink
add ps_tax_clean function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob Russel committed Feb 11, 2021
1 parent 62e8c55 commit 901f63e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: MicEco
Title: Various functions for microbial community data
Version: 0.9.11
Version: 0.9.12
Authors@R: person("Jakob", "Russel", email = "[email protected]", role = c("aut", "cre"))
Description: Collection of functions for microbiome analyses. E.g. fitting neutral models and standardized effect sizes of phylogenetic beta diversities, and much more.
Depends: R (>= 3.2.5)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export(ps_euler)
export(ps_pheatmap)
export(ps_prune)
export(ps_refactor)
export(ps_tax_clean)
export(ps_venn)
export(rarefy_rrna)
export(rarefy_rrna.matrix)
Expand Down
39 changes: 39 additions & 0 deletions R/ps_tax_clean.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#' Clean tax_table such that NAs are replaced with names of the most specific known taxonomy prefixed with the rank.
#'
#' Example: An OTU is annotated as Proteobacteria in the Phylum rank, but NAs in the more specific ranks.
#' All annotations below Phylum for this OTU will be replaced with Phylum_Proteobacteria.
#' @param data \code{phyloseq} or \code{tax_table} object.
#' @return Similar to input, but with NAs replaced in the tax_table
#' @export

ps_tax_clean <- function(data){

# Extract
if(is(data, "phyloseq")){
tax <- tax_table(data)
} else if(is(data, "taxonomyTable")) {
tax <- data
} else {
stop("Input should be a phyloseq or tax_table object")
}

rankn <- rank_names(data)

# Replace NAs
tax <- t(apply(tax, 1, function(x)
if(sum(is.na(x))>0){
c(x[!is.na(x)],
paste(rankn[max(which(!is.na(x)))], rep(x[max(which(!is.na(x)))], sum(is.na(x))), sep="_"))}
else{x}))

# Return
colnames(tax) <- rankn

if(is(data, "phyloseq")){
tax_table(data) <- tax_table(tax)
return(data)
} else if(is(data, "taxonomyTable")) {
return(tax_table(tax))
}

}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Make pretty heatmap directly from a phyloseq object. Built-in agglomoration, fil

Rarefaction curve (theoretical and fast) from a phyloseq object. Output ready for plotting in ggplot2

#### ps_tax_clean

Clean tax_table such that NAs are replaced with names of the most specific known taxonomy prefixed with the rank.

## Miscellaneous functions
#### adonis_OmegaSq

Expand Down
18 changes: 18 additions & 0 deletions man/ps_tax_clean.Rd

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

0 comments on commit 901f63e

Please sign in to comment.