-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.R
48 lines (41 loc) · 2.58 KB
/
functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
plot_anova_diversity_pval <- function(physeq, method, grouping_column,pValueCutoff=0.05)
{
#enforce orientation
if(taxa_are_rows(physeq)){
physeq <- t(physeq)
}
abund_table <- otu_table(physeq)
meta_table <- sample_data(physeq)
#get diversity measure using selected methods
div.df <- alpha_div(physeq,method)
#=add grouping information to alpha diversity measures
df<-data.frame(div.df,(meta_table[,grouping_column])[as.character(div.df$sample),])
#perform anova of diversity measure between groups
anova_res <- perform_anova(df,meta_table,grouping_column,pValueCutoff)
df_pw <- anova_res$df_pw #get pairwise p-values
#Draw the boxplots
p<-ggplot(aes_string(x=grouping_column,y="value",color=grouping_column),data=df)
p<-p+geom_boxplot()+geom_jitter(position = position_jitter(height = 0, width=0))
p<-p+theme_bw()
p<-p+theme(axis.text.x = element_text(angle = 90, hjust = 1))
p<-p+facet_wrap(~measure,scales="free_y",nrow=1)+ylab("Observed Values")+xlab("")
p<-p+theme(strip.background = element_rect(fill = "white"))+xlab("")
#This loop will generate the lines and signficances
if(!is.null(df_pw)){ #this only happens when we have significant pairwise anova results
for(i in 1:dim(df_pw)[1]){
p<-p+geom_path(inherit.aes=F,aes(x,y),data = data.frame(x = c(which(levels(df[,grouping_column])==as.character(df_pw[i,"from"])),which(levels(df[,grouping_column])==as.character(df_pw[i,"to"]))), y = c(as.numeric(as.character(df_pw[i,"y"])),as.numeric(as.character(df_pw[i,"y"]))), measure=c(as.character(df_pw[i,"measure"]),as.character(df_pw[i,"measure"]))), color="black",lineend = "butt",arrow = arrow(angle = 90, ends = "both", length = unit(0.1, "inches")))
p<-p+geom_text(inherit.aes=F,aes(x=x,y=y,label=label),data=data.frame(x=(which(levels(df[,grouping_column])==as.character(df_pw[i,"from"]))+which(levels(df[,grouping_column])==as.character(df_pw[i,"to"])))/2,y=as.numeric(as.character(df_pw[i,"y"])),measure=as.character(df_pw[i,"measure"]),label=as.character(cut(as.numeric(as.character(df_pw[i,"p"])),breaks=c(-Inf, 0.001, 0.01, 0.05, Inf),label=c("***", "**", "*", "")))))
}
}
newlist <- list(p, df_pw)
return(newlist)
}
###--------------------t-test---------------
t_test_treatment <- function(x){
ctrl <- subset_samples(x, trunk_canes_1=="canes")
excl <- subset_samples(x, trunk_canes_1=="trunks")
ctrlmean <- rowMeans(otu_table(Canes))
exclmean <- rowMeans(otu_table(Trunks))
test_temp <- t.test(ctrlmean, exclmean, paired=TRUE, conf.level = 0.99)
return(test_temp$p.value)
}