From 67b93ea9549e6301819156565d9410f2c08a0885 Mon Sep 17 00:00:00 2001 From: Jakob Russel Date: Wed, 19 Feb 2020 12:42:11 +0100 Subject: [PATCH] new version --- DESCRIPTION | 4 ++-- NAMESPACE | 6 ++++++ R/phylo_euler.R | 40 ++++++++++++++++++++++++++++++++++++++++ R/ps_refactor.R | 1 + README.md | 5 +++++ man/phylo_euler.Rd | 30 ++++++++++++++++++++++++++++++ 6 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 R/phylo_euler.R create mode 100644 man/phylo_euler.Rd diff --git a/DESCRIPTION b/DESCRIPTION index a74ff78..bb9bbd4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,10 +1,10 @@ Package: MicEco Title: Various functions for microbial community data -Version: 0.9.5 +Version: 0.9.6 Authors@R: person("Jakob", "Russel", email = "russel2620@gmail.com", 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) -Imports: stats, utils, phyloseq, foreach, doSNOW, picante, vegan, snow, bbmle, Hmisc, abind, reshape2 +Imports: stats, utils, phyloseq, foreach, doSNOW, picante, vegan, snow, bbmle, Hmisc, abind, reshape2, eulerr License: GPL (>= 3) | file LICENSE Encoding: UTF-8 LazyData: true diff --git a/NAMESPACE b/NAMESPACE index c1afa76..2329f51 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,6 +7,7 @@ export(comdistnt.par) export(community_rrna) export(neutral.fit) export(neutral.rand) +export(phylo_euler) export(proportionality) export(ps_refactor) export(rarefy_rrna) @@ -24,6 +25,7 @@ export(ses.permtest) import(Hmisc) import(bbmle) import(doSNOW) +import(eulerr) import(foreach) import(parallel) import(phyloseq) @@ -32,9 +34,13 @@ import(utils) import(vegan) importFrom(abind,abind) importFrom(reshape2,dcast) +importFrom(stats,aggregate) importFrom(stats,as.dist) +importFrom(stats,as.formula) importFrom(stats,cov) +importFrom(stats,dist) importFrom(stats,dnorm) +importFrom(stats,hclust) importFrom(stats,na.omit) importFrom(stats,p.adjust) importFrom(stats,pbeta) diff --git a/R/phylo_euler.R b/R/phylo_euler.R new file mode 100644 index 0000000..6ea3084 --- /dev/null +++ b/R/phylo_euler.R @@ -0,0 +1,40 @@ +#' Make Euler (Venn) diagram of shared taxa (ASVs, OTUs) across sample groups +#' +#' Make Euler (Venn) diagram of shared taxa (ASVs, OTUs) across sample groups from a phyloseq object. Overlap can be weighted by relative abundance +#' @param ps A phyloseq object +#' @param group The grouping factor. Should match variable in sample_data(ps) +#' @param weight If TRUE, the overlaps are weighted by abundance +#' @param type "percent" or "count" +#' @param relative Should abundances be made relative +#' @param ... Additional arguments +#' @keywords venn euler diagram +#' @return An euler plot +#' @import phyloseq +#' @import eulerr +#' @importFrom stats aggregate as.formula +#' @export + +phylo_euler <- function(ps, group, weight = FALSE, type = "percent", relative = TRUE, ...){ + + if(relative){ + ps <- transform_sample_counts(ps, function(x) x/sum(x)) + } + + ps_melted <- psmelt(ps) + + ps_agg <- aggregate(as.formula(paste("Abundance ~ OTU +",group)), data = ps_melted, function(x) mean(x)) + + ps_mat <- reshape2::dcast(as.formula(paste("OTU ~ ",group)), data = ps_agg, value.var = "Abundance") + + ps_mat <- ps_mat[, -1] + ps_mat_bin <- (ps_mat>0)*1 + + if(weight){ + df <- euler(ps_mat_bin, weights = rowMeans(ps_mat)) + } else { + df <- euler(ps_mat_bin) + } + + plot(df, quantities = list(type=type), ...) + +} \ No newline at end of file diff --git a/R/ps_refactor.R b/R/ps_refactor.R index 849800f..4da9f8c 100644 --- a/R/ps_refactor.R +++ b/R/ps_refactor.R @@ -6,6 +6,7 @@ #' @param psmelted A phyloseq object melted into a data.frame with psmelt #' @param ... Arguments passed to hclust #' @importFrom reshape2 dcast +#' @importFrom stats dist hclust #' @export ps_refactor <- function(psmelted, ...){ if(!is(psmelted, "data.frame")){ diff --git a/README.md b/README.md index d578143..420de15 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,11 @@ MicEco: Various functions for analysis for microbial community data ### Citation [![DOI](https://zenodo.org/badge/83547545.svg)](https://zenodo.org/badge/latestdoi/83547545) + +#### phylo_euler + +Make Euler (Venn) diagram of shared taxa (ASVs, OTUs) across sample groups from a phyloseq object. Overlap can be weighted by relative abundance + #### adonis_OmegaSq Calculate the unbiased effect size estimation (partial) omega-squared for adonis (PERMANOVA) models diff --git a/man/phylo_euler.Rd b/man/phylo_euler.Rd new file mode 100644 index 0000000..6a713e0 --- /dev/null +++ b/man/phylo_euler.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/phylo_euler.R +\name{phylo_euler} +\alias{phylo_euler} +\title{Make Euler (Venn) diagram of shared taxa (ASVs, OTUs) across sample groups} +\usage{ +phylo_euler(ps, group, weight = FALSE, type = "percent", relative = TRUE, ...) +} +\arguments{ +\item{ps}{A phyloseq object} + +\item{group}{The grouping factor. Should match variable in sample_data(ps)} + +\item{weight}{If TRUE, the overlaps are weighted by abundance} + +\item{type}{"percent" or "count"} + +\item{relative}{Should abundances be made relative} + +\item{...}{Additional arguments} +} +\value{ +An euler plot +} +\description{ +Make Euler (Venn) diagram of shared taxa (ASVs, OTUs) across sample groups from a phyloseq object. Overlap can be weighted by relative abundance +} +\keyword{diagram} +\keyword{euler} +\keyword{venn}