-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jakob Russel
committed
Jan 2, 2020
1 parent
2c71b39
commit 0be90c2
Showing
6 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#' Calculate (partial) Omega-squared (effect-size calculation) for PERMANOVA and add it to the input object | ||
#' | ||
#' @param adonisOutput An adonis object | ||
#' @param partial Should partial omega-squared be calculated (sample size adjusted). Default TRUE | ||
#' @return Original adonis object with the (partial) Omega-squared values added | ||
#' @import vegan | ||
#' @export | ||
adonis_OmegaSq <- function(adonisOutput, partial = TRUE){ | ||
if(!is(adonisOutput, "adonis")) stop("Input should be an adonis object") | ||
aov_tab <- adonisOutput$aov.tab | ||
heading <- attr(aov_tab, "heading") | ||
MS_res <- aov_tab[rownames(aov_tab) == "Residuals", "MeanSqs"] | ||
SS_tot <- aov_tab[rownames(aov_tab) == "Total", "SumsOfSqs"] | ||
N <- aov_tab[rownames(aov_tab) == "Total", "Df"] + 1 | ||
if(partial){ | ||
omega <- apply(aov_tab, 1, function(x) (x["Df"]*(x["MeanSqs"]-MS_res))/(x["Df"]*x["MeanSqs"]+(N-x["Df"])*MS_res)) | ||
aov_tab$parOmegaSq <- c(omega[1:(length(omega)-2)], NA, NA) | ||
cn_order <- c("Df", "SumsOfSqs", "MeanSqs", "F.Model", "R2", "parOmegaSq", "Pr(>F)") | ||
} else { | ||
omega <- apply(aov_tab, 1, function(x) (x["SumsOfSqs"]-x["Df"]*MS_res)/(SS_tot+MS_res)) | ||
aov_tab$OmegaSq <- c(omega[1:(length(omega)-2)], NA, NA) | ||
cn_order <- c("Df", "SumsOfSqs", "MeanSqs", "F.Model", "R2", "OmegaSq", "Pr(>F)") | ||
} | ||
aov_tab <- aov_tab[, cn_order] | ||
attr(aov_tab, "names") <- cn_order | ||
attr(aov_tab, "heading") <- heading | ||
adonisOutput$aov.tab <- aov_tab | ||
return(adonisOutput) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#' Rarefaction curve on phyloseq object | ||
#' Theoretical richness with vegan's rarefy function | ||
#' | ||
#' @param physeq phyloseq object | ||
#' @param subsamp Vector of number of reads to subsample | ||
#' @param trim Remove richness estimations from subsamples larger than the library size | ||
#' @param add_sample_data Add sample data to data.frame | ||
#' @import vegan | ||
#' @import utils | ||
#' @importFrom stats na.omit | ||
#' @export | ||
rcurve <- function(physeq, subsamp = 10^c(1:5), trim = TRUE, add_sample_data = TRUE){ | ||
|
||
otu <- otu_table(physeq) | ||
if(!taxa_are_rows(physeq)){ | ||
otu <- t(otu) | ||
} | ||
colS <- colSums(otu) | ||
|
||
pb <- txtProgressBar(min = 0, max = length(subsamp), style = 3) | ||
rars <- list() | ||
for(i in seq_along(subsamp)){ | ||
setTxtProgressBar(pb, i) | ||
rars[[i]] <- vegan::rarefy(otu, sample = subsamp[i], MARGIN = 2) | ||
} | ||
mat <- do.call(cbind, rars) | ||
|
||
if(trim){ | ||
mat_bool <- sapply(subsamp, function(i) sapply(colS, function(j) i <= j)) | ||
mat_new <- mat * mat_bool | ||
mat_new[mat_new == 0] <- NA | ||
} else { | ||
mat_new <- mat | ||
} | ||
colnames(mat_new) <- subsamp | ||
|
||
# To a data.frame | ||
df <- as.data.frame.table(mat_new) | ||
colnames(df) <- c("Sample", "Reads", "Richness") | ||
|
||
if(trim){ | ||
df <- na.omit(df) | ||
} | ||
|
||
# Add sample data | ||
if(add_sample_data){ | ||
samp <- sample_data(physeq) | ||
df2 <- merge(df, samp, by = "Sample", by.y = "row.names") | ||
return(df2) | ||
} else { | ||
return(df) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.