tmp<-tmp/colSums(tmp)
#only top 20 taxa
x<-taxa_sums(compF)
#keepTaxa = which((x / sum(x)) > 0.005)
keepTaxa<-names(sort(x, decreasing = TRUE)[1:20])
tmp<-tmp[keepTaxa,]
rownames(tmp)
compF <- aggregate_sets(EC, "Family")
tmp<-otu_table(compF)
tmp<-tmp/colSums(tmp)
#only top 20 taxa
x<-taxa_sums(compF)
#keepTaxa = which((x / sum(x)) > 0.005)
keepTaxa<-names(sort(x, decreasing = TRUE)[1:20])
tmp<-tmp[rownames(tmp)%in%keepTaxa,]
tS<-tax_table(compF)
dim(tS)
tS<-tS[rownames(tS)%in%keepTaxa,]
dim(tS)
rownames(tmp)<-unname(sapply(rownames(tmp),function(x){str_split(x,"_")[[1]][5]}))
rownames(tS)<-unname(sapply(rownames(tS),function(x){str_split(x,"_")[[1]][5]}))
rownames(tS)
mS<-sample_data(compF)
mS$DMM<-paste("D",mS$DMM,sep="")
compF<-phyloseq(otu_table(tmp), sample_data(mS), tax_table(tS))
compF
plot_composition(compF, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM") + meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) 
compF <- aggregate_sets(EC, "Family")
tmp<-otu_table(compF)
tmp<-tmp/colSums(tmp)
#only top 20 taxa
x<-taxa_sums(compF)
#keepTaxa = which((x / sum(x)) > 0.005)
keepTaxa<-names(sort(x, decreasing = TRUE)[1:25])
tmp<-tmp[rownames(tmp)%in%keepTaxa,]
tS<-tax_table(compF)
tS<-tS[rownames(tS)%in%keepTaxa,]
rownames(tmp)<-unname(sapply(rownames(tmp),function(x){str_split(x,"_")[[1]][5]}))
rownames(tS)<-unname(sapply(rownames(tS),function(x){str_split(x,"_")[[1]][5]}))
#add D for facet title
mS<-sample_data(compF)
mS$DMM<-paste("D",mS$DMM,sep="")
compF<-phyloseq(otu_table(tmp), sample_data(mS), tax_table(tS))
plot_composition(compF, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM") + meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) 
keepTaxa_colors <- rainbow(length(keepTaxa))
compF <- aggregate_sets(EC, "Family")
tmp<-otu_table(compF)
tmp<-tmp/colSums(tmp)
#only top 20 taxa
x<-taxa_sums(compF)
#keepTaxa = which((x / sum(x)) > 0.005)
keepTaxa<-names(sort(x, decreasing = TRUE)[1:20])
keepTaxa_colors <- rainbow(length(keepTaxa))
keepTaxa
compF <- aggregate_sets(EC, "Family")
tmp<-otu_table(compF)
tmp<-tmp/colSums(tmp)
x<-taxa_sums(compF)
#keepTaxa = which((x / sum(x)) > 0.005)
keepTaxa<-names(sort(x, decreasing = TRUE)[1:20])
keepTaxa_colors <- rainbow(length(keepTaxa))
keepTaxa
keepTaxa_colors
unname(sapply(keepTaxa,function(x){str_split(x,"_")[[1]][5]}))
tS<-tax_table(compF)
rownames(tmp)<-unname(sapply(rownames(tmp),function(x){str_split(x,"_")[[1]][5]}))
rownames(tS)<-unname(sapply(rownames(tS),function(x){str_split(x,"_")[[1]][5]}))
rownames(tS)
mS<-sample_data(compF)
mS$DMM<-paste("D",mS$DMM,sep="")
compF<-phyloseq(otu_table(tmp), sample_data(mS), tax_table(tS))
plot_composition(compF, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM",legend_labels = list(color = keepTaxa_colors, label = keepTaxa)) + meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) 
plot_composition(compF, group_by="DMM",legend_labels = list(color = keepTaxa_colors, label = keepTaxa))
?legend_labels
?plot_composition
plot_composition(compF,legend_labels = list(color = keepTaxa_colors, label = keepTaxa))
plot_composition(compF, group_by="DMM")+scale_fill_manual(values = default_colors("Phylum")[taxa(pseq)][1:5])
plot_composition(compF, group_by="DMM")+scale_fill_manual(values = default_colors("Phylum")[taxa(compF)][1:5])
plot_composition(compF, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", lgend=FALSE)
plot_composition(compF, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+theme(legend.position="none")
plot_composition(compF, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+theme(legend.position="none")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) 
plot_composition(compF, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) +theme(legend.position="none")
legend("topright", legend = keepTaxa, fill = keepTaxa_colors, title = "Top 30 Taxa")
ggdraw()+legend("topright", legend = keepTaxa, fill = keepTaxa_colors, title = "Top 30 Taxa")
top <- EC %>% taxa_sums %>% sort(decreasing=TRUE) %>% names %>% as.data.frame(labels=TRUE) %>% rename(Class=".") %>% 
  mutate(aggClass=as.factor(case_when( #create aggClass variable
    row_number()<=20~Class,row_number()>20~'Other'
  )))
top
top <- compF %>% taxa_sums %>% sort(decreasing=TRUE) %>% names %>% as.data.frame(labels=TRUE) %>% rename(Class=".") %>% 
  mutate(aggClass=as.factor(case_when( #create aggClass variable
    row_number()<=20~Class,row_number()>20~'Other'
  )))
top
compF <- aggregate_sets(EC, "Family")
tmp<-otu_table(compF)
tmp<-tmp/colSums(tmp)
tS<-tax_table(compF)
rownames(tmp)<-unname(sapply(rownames(tmp),function(x){str_split(x,"_")[[1]][5]}))
rownames(tS)<-unname(sapply(rownames(tS),function(x){str_split(x,"_")[[1]][5]}))
mS<-sample_data(compF)
mS$DMM<-paste("D",mS$DMM,sep="")
compF<-phyloseq(otu_table(tmp), sample_data(mS), tax_table(tS))
top <- compF %>% taxa_sums %>% sort(decreasing=TRUE) %>% names %>% as.data.frame(labels=TRUE) %>% rename(Class=".") %>% 
  mutate(aggClass=as.factor(case_when( #create aggClass variable
    row_number()<=20~Class,row_number()>20~'Other'
  )))
top
<-top[,2]
top[,2]
rownames(tmp)<-top[,2]
rownames(tS)<-top[,2]
compF<-phyloseq(otu_table(compF), sample_data(mS), tax_table(tS))
compF<-phyloseq(otu_table(tmp), sample_data(compF), tax_table(tS))
compF
top <- compF %>% taxa_sums %>% sort(decreasing=TRUE) %>% names %>% as.data.frame(labels=TRUE) %>% rename(Class=".") %>% 
  mutate(aggClass=as.factor(case_when( #create aggClass variable
    row_number()<=20~Class,row_number()>20~'Other'
  )))
tax_table(compF)
tt<-tax_table(compF)
tt
tt$other<-top[,2]
tt
tt<-tax_table(compF)
dim(tt)
dim(top)
is.matrix(tt)
tt<-tax_table(compF)
tt[,ncol(tt)+1]<-top[,2]
tt<-tax_table(compF)
tt<-cbind[tt,other=top[,2]]
tt<-tax_table(compF)
tt<-cbind(tt,other=top[,2])
tt
tt<-matrix(tax_table(compF))
tt
tt<-cbind(tt,top[,2])
tt
tt<-data.frame(tax_table(compF))
tt
tt<-cbind(tt,Other=top[,2])
tt
tt[1,]
rownames(tt)
compF<-phyloseq(otu_table(compF), sample_data(compF), tax_table(tt))
tt<-matrix(tax_table(compF))
tt<-cbind(tt,top[,2])
colnames(tt)[2]<-"Other"
tt
tt<-tax_table(compF)
tt
tt<-cbind(tt,top[,2])
tt
tt[1,]
top
tt
tt<-tax_table(compF)
top[rownames(tt),2]
rownames(top)
top
rownames(top)<-top[,1]
top
tt<-tax_table(compF)
top[rownames(tt),2]
ttt<-cbind(tt,top[rownames(tt),2])
ttt[1,]
top[,2]
as.char(top$aggClass)
char(top$aggClass)
character(top$aggClass)
as.character(top$aggClass)
top$aggClass<-as.character(top$aggClass)
tt<-tax_table(compF)
ttt<-cbind(tt,top[rownames(tt),2])
ttt[,1]
ttt[1,]
ttt
colnames(ttt)[2]<-"Other"
compF<-phyloseq(otu_table(compF), sample_data(compF), tax_table(ttt))
compO <- aggregate_sets(compF, "Other")
colSums(otu_table(compO))
plot_composition(compO, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) 
compF <- aggregate_sets(EC, "Family")
tmp<-otu_table(compF)
tmp<-tmp/colSums(tmp)
mS<-sample_data(compF)
mS$DMM<-paste("D",mS$DMM,sep="")
compF<-phyloseq(otu_table(compF), sample_data(mS), tax_table(compF))
top <- compF %>% taxa_sums %>% sort(decreasing=TRUE) %>% names %>% as.data.frame(labels=TRUE) %>% rename(Class=".") %>% 
  mutate(aggClass=as.factor(case_when( #create aggClass variable
    row_number()<=20~Class,row_number()>20~'Other'
  )))
rownames(top)<-top[,1]
top$aggClass<-as.character(top$aggClass)
top
tt<-tax_table(compF)
ttt<-cbind(tt,top[rownames(tt),2])
colnames(ttt)[2]<-"Other"
compF<-phyloseq(otu_table(compF), sample_data(compF), tax_table(ttt))
compO <- aggregate_sets(compF, "Other")
ttt
rownames(ttt)
compO <- aggregate_sets(compF, "Other")
plot_composition(compO, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) 
tS<-tax_table(compF)
tS
unname(sapply(rownames(tmp),function(x){
xx<-str_split(x,"_")
xx[[1]][length(xx[[1]])]
}
getlast<-function(xxx){
me<-length(xxx[[1]])
xxx[[1]][me]
}
unname(sapply(rownames(tmp),function(x){getlast(str_split(x,"_"))}
)
)
tS<-tax_table(compO)
rownames(tmp)<-unname(sapply(rownames(tmp),function(x){getlast(str_split(x,"_"))}))
tmp
compF <- aggregate_sets(EC, "Family")
tmp<-otu_table(compF)
tmp<-tmp/colSums(tmp)
mS<-sample_data(compF)
mS$DMM<-paste("D",mS$DMM,sep="")
compF<-phyloseq(otu_table(compF), sample_data(mS), tax_table(compF))
top <- compF %>% taxa_sums %>% sort(decreasing=TRUE) %>% names %>% as.data.frame(labels=TRUE) %>% rename(Class=".") %>% 
  mutate(aggClass=as.factor(case_when( #create aggClass variable
    row_number()<=20~Class,row_number()>20~'Other'
  )))
rownames(top)<-top[,1]
top$aggClass<-as.character(top$aggClass)
tt<-tax_table(compF)
ttt<-cbind(tt,top[rownames(tt),2])
colnames(ttt)[2]<-"Other"
compF<-phyloseq(otu_table(compF), sample_data(compF), tax_table(ttt))
compO <- aggregate_sets(compF, "Other")
#fix legend names
tS<-tax_table(compO)
tS[1,]
tS
dim(tS)
compO <- aggregate_sets(compF, "Other")
#fix legend names
tS<-tax_table(compO)
rownames(tS)<-unname(sapply(tS[,1],function(x){getlast(str_split(x,"_"))}))
rownames(tS)
tO<-otu_table(compO)
rownames(tO)
unname(sapply(rownames(tO),function(x){getlast(str_split(x,"_"))}))
rownames(tO)<-unname(sapply(rownames(tO),function(x){getlast(str_split(x,"_"))}))
identical(rownames(tO), rownames(tS))
compO<-phyloseq(otu_table(tO), sample_data(compF), tax_table(tS))
plot_composition(compO, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) 
compF <- aggregate_sets(EC, "Family")
tmp<-otu_table(compF)
tmp<-tmp/colSums(tmp)
mS<-sample_data(compF)
mS$DMM<-paste("D",mS$DMM,sep="")
compF<-phyloseq(otu_table(compF), sample_data(mS), tax_table(compF))
top <- compF %>% taxa_sums %>% sort(decreasing=TRUE) %>% names %>% as.data.frame(labels=TRUE) %>% rename(Class=".") %>% 
  mutate(aggClass=as.factor(case_when( #create aggClass variable
    row_number()<=20~Class,row_number()>20~'Other'
  )))
rownames(top)<-top[,1]
top$aggClass<-as.character(top$aggClass)
tt<-tax_table(compF)
ttt<-cbind(tt,top[rownames(tt),2])
colnames(ttt)[2]<-"Other"
compF<-phyloseq(otu_table(compF), sample_data(compF), tax_table(ttt))
compO <- aggregate_sets(compF, "Other")
#fix legend names
tS<-tax_table(compO)
rownames(tS)<-unname(sapply(tS[,1],function(x){getlast(str_split(x,"_"))}))
tO<-otu_table(compO)
rownames(tO)<-unname(sapply(rownames(tO),function(x){getlast(str_split(x,"_"))}))
compO<-phyloseq(otu_table(tO), sample_data(compF), tax_table(tS))
plot_composition(compO, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) 
pC2<-plot_composition(compO, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) 
tmp<-otu_table(S4)
tmp<-tmp/colSums(tmp)
mS<-sample_data(S4)
mS$DMM<-paste("D",mS$DMM,sep="")
compS4<-phyloseq(otu_table(tmp), sample_data(mS), tax_table(S4))
plot_composition(compS4, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", labeller=labeller(DMM=c("1"="DMM1","2"="DMM2","3"="DMM3","4"="DMM4","5"="DMM5")))+ meg_stef_theme()+xlab("Samples") +ylab("Rel. abundance")+theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())+labs(fill="Aerobicity &\npathogenicity\ngroup")+scale_fill_discrete(labels=c("Anaerobes", "CF pathogens", "Fac. anaerobes","Aerobes","unknown"))
pC1<-plot_composition(compS4, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", labeller=labeller(DMM=c("1"="DMM1","2"="DMM2","3"="DMM3","4"="DMM4","5"="DMM5")))+ meg_stef_theme()+xlab("Samples") +ylab("Rel. abundance")+theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())+labs(fill="Aerobicity &\npathogenicity\ngroup")+scale_fill_discrete(labels=c("Anaerobes", "CF pathogens", "Fac. anaerobes","Aerobes","unknown"))
pC2
pC2<-plot_composition(compO, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) +labs(fill="Family")+xlab("Samples") +ylab("Rel. abundance")+ggtitle("Composition by DMM community classes")
pC2
pC1<-plot_composition(compS4, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", labeller=labeller(DMM=c("1"="DMM1","2"="DMM2","3"="DMM3","4"="DMM4","5"="DMM5")))+ meg_stef_theme()+xlab("Samples") +ylab("Rel. abundance")+theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())+labs(fill="Aerobicity &\npathogenicity\ngroup")+ggtitle("Composition by DMM community classes")+scale_fill_discrete(labels=c("Anaerobes", "CF pathogens", "Fac. anaerobes","Aerobes","unknown"))
pC1
l1<-get_legend(pC1)
l2<-get_legend(pC2)
plot_grid(pC1+theme(legend.position="none"),l1, pC2+theme(legend.position="none"),l2, labels=c("A","","B",""),ncol=2, rel_widths=c(1,0.2))
plot_grid(pC1+theme(legend.position="none"),l1, pC2+theme(legend.position="none"),l2, labels=c("A","","B",""),ncol=2, rel_widths=c(1,0.5))
plot_grid(pC1+theme(legend.position="none"),l1, pC2+theme(legend.position="none"),l2, labels=c("A","","B",""),ncol=2, rel_widths=c(0.8,0.5))
plot_grid(pC1+theme(legend.position="none"),l1, pC2+theme(legend.position="none"),l2, labels=c("A","","B",""),ncol=2, rel_widths=c(0.7,0.6))
plot_grid(pC1+theme(legend.position="none"),l1, pC2+theme(legend.position="none"),l2, labels=c("A","","B",""),ncol=2, rel_widths=c(0.8,0.6))
plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("A","B",),nrow=2)
plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("A","B"),nrow=2)
plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("A","B"),nrow=2, rel_heights=c(1,1.5))
plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("A","B"),nrow=2, rel_heights=c(1,1.3))
compF <- aggregate_sets(EC, "Family")
#composition
tmp<-otu_table(compF)
tmp<-tmp/colSums(tmp)
#change facette titles
mS<-sample_data(compF)
mS$DMM<-paste("D",mS$DMM,sep="")
#make phyloseq object
compF<-phyloseq(otu_table(compF), sample_data(mS), tax_table(compF))
#Identify top 20 (most abundant across taxon) and the rest to Other
top <- compF %>% taxa_sums %>% sort(decreasing=TRUE) %>% names %>% as.data.frame(labels=TRUE) %>% rename(Class=".") %>% 
  mutate(aggClass=as.factor(case_when( #create aggClass variable
    row_number()<=19~Class,row_number()>19~'Other'
  )))
rownames(top)<-top[,1]
top$aggClass<-as.character(top$aggClass)
tt<-tax_table(compF)
ttt<-cbind(tt,top[rownames(tt),2])
colnames(ttt)[2]<-"Other"
compF<-phyloseq(otu_table(compF), sample_data(compF), tax_table(ttt))
#aggregate Other
compO <- aggregate_sets(compF, "Other")
#fix legend names
tS<-tax_table(compO)
rownames(tS)<-unname(sapply(tS[,1],function(x){getlast(str_split(x,"_"))}))
tO<-otu_table(compO)
rownames(tO)<-unname(sapply(rownames(tO),function(x){getlast(str_split(x,"_"))}))
compO<-phyloseq(otu_table(tO), sample_data(compF), tax_table(tS))
#plot composition with legend displaying only the top 20 most abundant taxa
pC2<-plot_composition(compO, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) +labs(fill="Family")+xlab("Samples") +ylab("Rel. abundance")+ggtitle("Composition by families by DMM community classes")
pC2
pC2<-plot_composition(compO, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) +labs(fill="Family")+xlab("Samples") +ylab("Rel. abundance")+ggtitle("Composition by families and DMM community classes")
tmp<-otu_table(S4)
tmp<-tmp/colSums(tmp)
mS<-sample_data(S4)
mS$DMM<-paste("D",mS$DMM,sep="")
compS4<-phyloseq(otu_table(tmp), sample_data(mS), tax_table(S4))
pC1<-plot_composition(compS4, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", labeller=labeller(DMM=c("1"="DMM1","2"="DMM2","3"="DMM3","4"="DMM4","5"="DMM5")))+ meg_stef_theme()+xlab("Samples") +ylab("Rel. abundance")+theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())+labs(fill="Aerobicity &\npathogenicity\ngroup")+ggtitle("Composition by aerobicity & pathogenicity by DMM community classes")+scale_fill_discrete(labels=c("Anaerobes", "CF pathogens", "Fac. anaerobes","Aerobes","unknown"))
plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("A","B"),nrow=2, rel_heights=c(1,1.3))
tmp<-otu_table(S4)
tmp<-tmp/colSums(tmp)
mS<-sample_data(S4)
mS$DMM<-paste("D",mS$DMM,sep="")
compS4<-phyloseq(otu_table(tmp), sample_data(mS), tax_table(S4))
pC1<-plot_composition(compS4, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", labeller=labeller(DMM=c("1"="DMM1","2"="DMM2","3"="DMM3","4"="DMM4","5"="DMM5")))+ meg_stef_theme()+xlab("Samples") +ylab("Rel. abundance")+theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())+labs(fill="Tax. grouping")+ggtitle("Composition by aerobicity & pathogenicity and DMM community classes")+scale_fill_discrete(labels=c("Anaerobes", "CF pathogens", "Fac. anaerobes","Aerobes","unknown"))
plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("A","B"),nrow=2, rel_heights=c(1,1.3))
pC1<-plot_composition(compS4, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", labeller=labeller(DMM=c("1"="DMM1","2"="DMM2","3"="DMM3","4"="DMM4","5"="DMM5")))+ meg_stef_theme()+xlab("Samples") +ylab("Rel. abundance")+theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())+labs(fill="Tax. grouping", subtitle="Ta. aggregation by aerobicity and pathogenicity")+ggtitle("Composition DMM community classes")+scale_fill_discrete(labels=c("Anaerobes", "CF pathogens", "Fac. anaerobes","Aerobes","unknown"))
pC1
pC1<-plot_composition(compS4, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", labeller=labeller(DMM=c("1"="DMM1","2"="DMM2","3"="DMM3","4"="DMM4","5"="DMM5")))+ meg_stef_theme()+xlab("Samples") +ylab("Rel. abundance")+theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())+labs(fill="Tax. grouping", subtitle="Count aggregation by aerobicity and pathogenicity")+ggtitle("Composition DMM community classes")+scale_fill_discrete(labels=c("Anaerobes", "CF pathogens", "Fac. anaerobes","Aerobes","unknown"))
pC1
pC1<-plot_composition(compS4, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", labeller=labeller(DMM=c("1"="DMM1","2"="DMM2","3"="DMM3","4"="DMM4","5"="DMM5")))+ meg_stef_theme()+xlab("Samples") +ylab("Rel. abundance")+theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())+labs(fill="Tax. grouping", subtitle="Count aggregation by aerobicity and pathogenicity")+ggtitle("Composition by DMM community classes")+scale_fill_discrete(labels=c("Anaerobes", "CF pathogens", "Fac. anaerobes","Aerobes","unknown"))
pC1<-plot_composition(compS4, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", labeller=labeller(DMM=c("1"="DMM1","2"="DMM2","3"="DMM3","4"="DMM4","5"="DMM5")))+ meg_stef_theme()+xlab("Samples") +ylab("Rel. abundance")+theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())+labs(fill="Tax. grouping", subtitle="Count aggregation by aerobicity and pathogenicity")+ggtitle("Composition by DMM community classes")+scale_fill_discrete(labels=c("Anaerobes", "CF pathogens", "Fac. anaerobes","Aerobes","unknown"))
pC1
pC1<-plot_composition(compS4, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", labeller=labeller(DMM=c("1"="DMM1","2"="DMM2","3"="DMM3","4"="DMM4","5"="DMM5")))+ meg_stef_theme()+xlab("Samples") +ylab("Rel. abundance")+theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())+labs(fill="Tax", subtitle="Count aggregation by aerobicity and pathogenicity")+ggtitle("Composition by DMM community classes")+scale_fill_discrete(labels=c("Anaerobes", "CF pathogens", "Fac. anaerobes","Aerobes","unknown"))
pC1
compF <- aggregate_sets(EC, "Family")
#composition
tmp<-otu_table(compF)
tmp<-tmp/colSums(tmp)
#change facette titles
mS<-sample_data(compF)
mS$DMM<-paste("D",mS$DMM,sep="")
#make phyloseq object
compF<-phyloseq(otu_table(compF), sample_data(mS), tax_table(compF))
#Identify top 20 (most abundant across taxon) and the rest to Other
top <- compF %>% taxa_sums %>% sort(decreasing=TRUE) %>% names %>% as.data.frame(labels=TRUE) %>% rename(Class=".") %>% 
  mutate(aggClass=as.factor(case_when( #create aggClass variable
    row_number()<=19~Class,row_number()>19~'Other'
  )))
rownames(top)<-top[,1]
top$aggClass<-as.character(top$aggClass)
tt<-tax_table(compF)
ttt<-cbind(tt,top[rownames(tt),2])
colnames(ttt)[2]<-"Other"
compF<-phyloseq(otu_table(compF), sample_data(compF), tax_table(ttt))
#aggregate Other
compO <- aggregate_sets(compF, "Other")
#fix legend names
tS<-tax_table(compO)
rownames(tS)<-unname(sapply(tS[,1],function(x){getlast(str_split(x,"_"))}))
tO<-otu_table(compO)
rownames(tO)<-unname(sapply(rownames(tO),function(x){getlast(str_split(x,"_"))}))
compO<-phyloseq(otu_table(tO), sample_data(compF), tax_table(tS))
#plot composition with legend displaying only the top 20 most abundant taxa
pC2<-plot_composition(compO, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) +labs(fill="Tax", subtitle="Count aggregation at Family level")+xlab("Samples") +ylab("Rel. abundance")+ggtitle("Composition by DMM community classes")
plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("A","B"),nrow=2, rel_heights=c(1,1.3))
plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("A","B"),nrow=2, rel_heights=c(1,1.2))
plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("A","B"),nrow=2, rel_heights=c(1,1.25))
SComp<-plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("A","B"),nrow=2, rel_heights=c(1,1.25))
save_plot(sComp, file="SI_Compositions.pdf", ncol=1, base_height=6.5,  base_asp=1.4)
save_plot(SComp, file="SI_Compositions.pdf", ncol=1, base_height=6.5,  base_asp=1.4)
getwd()
save_plot(SComp, file="SI_Compositions.pdf", ncol=1, base_height=6.5,  base_asp=1.2)
save_plot(SComp, file="SI_Compositions.pdf", ncol=1, base_height=6.5,  base_asp=1.3)
save_plot(SComp, file="SI_Compositions.png", ncol=1, base_height=6.5,  base_asp=1.3)
pC2<-plot_composition(compO, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) +labs(fill="Tax", subtitle="Count aggregation at Family level")+xlab("Samples") +ylab("Rel. abundance")+ggtitle("Sample composition by DMM community classes")
pC1<-plot_composition(compS4, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", labeller=labeller(DMM=c("1"="DMM1","2"="DMM2","3"="DMM3","4"="DMM4","5"="DMM5")))+ meg_stef_theme()+xlab("Samples") +ylab("Rel. abundance")+theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())+labs(fill="Tax", subtitle="Count aggregation by aerobicity and pathogenicity")+ggtitle("Sample composition by DMM community classes")+scale_fill_discrete(labels=c("Anaerobes", "CF pathogens", "Fac. anaerobes","Aerobes","unknown"))
SComp<-plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("A","B"),nrow=2, rel_heights=c(1,1.25))
save_plot(SComp, file="SI_Compositions.pdf", ncol=1, base_height=6.5,  base_asp=1.3)
save_plot(SComp, file="SI_Compositions.png", ncol=1, base_height=6.5,  base_asp=1.3)
SComp<-plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("E","F"),nrow=2, rel_heights=c(1,1.25))
save_plot(SComp, file="SI_Compositions.pdf", ncol=1, base_height=6.5,  base_asp=1.3)
save_plot(SComp, file="SI_Compositions.png", ncol=1, base_height=6.5,  base_asp=1.3)
colSums(otu_table(compO))
compF <- aggregate_sets(EC, "Family")
#composition
tmp<-otu_table(compF)
tmp<-tmp/colSums(tmp)
#change facette titles
mS<-sample_data(compF)
mS$DMM<-paste("D",mS$DMM,sep="")
#make phyloseq object
compF<-phyloseq(otu_table(compF), sample_data(mS), tax_table(compF))
#Identify top 20 (most abundant across taxon) and the rest to Other
top <- compF %>% taxa_sums %>% sort(decreasing=TRUE) %>% names %>% as.data.frame(labels=TRUE) %>% rename(Class=".") %>% 
  mutate(aggClass=as.factor(case_when( #create aggClass variable
    row_number()<=19~Class,row_number()>19~'Other'
  )))
rownames(top)<-top[,1]
top$aggClass<-as.character(top$aggClass)
tt<-tax_table(compF)
ttt<-cbind(tt,top[rownames(tt),2])
colnames(ttt)[2]<-"Other"
compF<-phyloseq(otu_table(compF), sample_data(compF), tax_table(ttt))
#aggregate Other
compO <- aggregate_sets(compF, "Other")
colSums(otu_table(compO))
colSums(otu_table(compF))
colSums(tmp)
compF <- aggregate_sets(EC, "Family")
#composition
tmp<-otu_table(compF)
tmp<-tmp/colSums(tmp)
#change facette titles
mS<-sample_data(compF)
mS$DMM<-paste("D",mS$DMM,sep="")
#make phyloseq object
compF<-phyloseq(otu_table(tmp), sample_data(mS), tax_table(compF))
#Identify top 20 (most abundant across taxon) and the rest to Other
top <- compF %>% taxa_sums %>% sort(decreasing=TRUE) %>% names %>% as.data.frame(labels=TRUE) %>% rename(Class=".") %>% 
  mutate(aggClass=as.factor(case_when( #create aggClass variable
    row_number()<=19~Class,row_number()>19~'Other'
  )))
rownames(top)<-top[,1]
top$aggClass<-as.character(top$aggClass)
tt<-tax_table(compF)
ttt<-cbind(tt,top[rownames(tt),2])
colnames(ttt)[2]<-"Other"
compF<-phyloseq(otu_table(compF), sample_data(compF), tax_table(ttt))
#aggregate Other
compO <- aggregate_sets(compF, "Other")
#fix legend names
tS<-tax_table(compO)
rownames(tS)<-unname(sapply(tS[,1],function(x){getlast(str_split(x,"_"))}))
tO<-otu_table(compO)
rownames(tO)<-unname(sapply(rownames(tO),function(x){getlast(str_split(x,"_"))}))
compO<-phyloseq(otu_table(tO), sample_data(compF), tax_table(tS))
#plot composition with legend displaying only the top 20 most abundant taxa
pC2<-plot_composition(compO, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM")+ meg_stef_theme() +theme(axis.text.x=element_blank(),axis.ticks.x=element_blank()) +labs(fill="Tax", subtitle="Count aggregation at Family level")+xlab("Samples") +ylab("Rel. abundance")+ggtitle("Sample composition by DMM community classes")
SComp<-plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("E","F"),nrow=2, rel_heights=c(1,1.25))
save_plot(SComp, file="SI_Compositions.pdf", ncol=1, base_height=6.5,  base_asp=1.3)
save_plot(SComp, file="SI_Compositions.png", ncol=1, base_height=6.5,  base_asp=1.3)
q()
library(phyloseq)
library(microbiome)
library(DirichletMultinomial)
library(lattice)
library(xtable)
library(parallel)
library(vegan)
library(tidyverse)
library(ggplot2)
library(ggpubr)
source("../HARMONIZE_FIGURES/1_plot_utils.R")
phyloseq(otu_table(tmp), sample_data(mS), tax_table(S4))
pC1<-plot_composition(compS4, sample.sort="DMM", otu.sort="abundance", verbose=TRUE, group_by="DMM", labeller=labeller(DMM=c("1"="DMM1","2"="DMM2","3"="DMM3","4"="DMM4","5"="DMM5")))+ meg_stef_theme()+xlab("Samples") +ylab("Rel. abundance")+theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())+labs(fill="ASV groups", subtitle="Count aggregation by aerobicity and pathogenicity")+ggtitle("Sample composition by DMM community classes")+scale_fill_discrete(labels=c("Anaerobes", "CF pathogens", "Fac. anaerobes","Aerobes","unknown"))
pC1
plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("E","F"),nrow=2, rel_heights=c(1,1.25))
SComp<-plot_grid(pC1+theme(legend.position="bottom"), pC2+theme(legend.position="bottom"), labels=c("E","F"),nrow=2, rel_heights=c(1,1.25))
save_plot(SComp, file="SI_1EF.pdf", ncol=1, base_height=6.5,  base_asp=1.3)
save_plot(SComp, file="SI_1EF.png", ncol=1, base_height=6.5,  base_asp=1.3)
ggplot(cc,aes(x=as.factor(Patient),y=n,fill=DMM)) + geom_bar(position="stack", stat="identity") + meg_stef_theme()+scale_x_discrete(labels=c('Sub A', 'Sub B', 'Sub C', 'Sub D', 'Sub E', 'Sub F','Sub G', 'Sub H', 'Sub I', 'Sub J', 'Sub K')) + theme(axis.text.x = element_text(angle = 90, vjust=0.5, hjust=0)) + guides(color=guide_legend(override.aes=list(fill=NA))) + scale_fill_manual(values=dmm,name="Community\nstates",labels=clear) + ggtitle(tit) + xlab("Cohort") + ylab("Sample number")+theme(legend.key.size=unit(16, 'points'))
s1C<-ggplot(cc,aes(x=as.factor(Patient),y=n,fill=DMM)) + geom_bar(position="stack", stat="identity") + meg_stef_theme()+scale_x_discrete(labels=c('Sub A', 'Sub B', 'Sub C', 'Sub D', 'Sub E', 'Sub F','Sub G', 'Sub H', 'Sub I', 'Sub J', 'Sub K')) + theme(axis.text.x = element_text(angle = 90, vjust=0.5, hjust=0)) + guides(color=guide_legend(override.aes=list(fill=NA))) + scale_fill_manual(values=dmm,name="Community\nclass",labels=clear) + ggtitle(tit) + xlab("Cohort") + ylab("Sample number")+theme(legend.key.size=unit(16, 'points'))
s1C
cc<-as(sample_data(EC),"data.frame") %>% count(DMM, Patient)
tit<-"Subjects by DMM community class"
s1D<-ggplot(cc,aes(x=as.factor(DMM),y=n,fill=Patient)) + geom_bar(position="stack", stat="identity") + meg_stef_theme() + guides(color=guide_legend(override.aes=list(fill=NA))) + scale_fill_manual(values=coh,labels=c("Subject A","Subject B","Subject C","Subject D","Subject E", "Subject F","Subject G","Subject H","Subject I","Subject J", "Subject K"),name="Cohort") + ggtitle(tit) + xlab("Community class") + ylab("Sample number")+theme(legend.key.size=unit(16, 'points'))
s1D
s1CD<-plot_grid(s1C,s1D, labels=c("C","D"), align = "hv", ncol=2,label_size = 12, label_fontface = "bold")
s1CD
-plot_grid(s1AB,s1CD,nrow=2,align = "v", rel_heights=c(1,1.2))
plot_grid(s1AB,s1CD,nrow=2,align = "v", rel_heights=c(1,1.2))
save_plot("supp_figure1.pdf",sup1, ncol=1, base_height=6.5,  base_asp=1.4)
save_plot("supp_figure1.png",sup1, ncol=1, base_height=6.5,  base_asp=1.4)
getwd()
sup1<-plot_grid(s1AB,s1CD,nrow=2,align = "v", rel_heights=c(1,1.2))
sup1
save_plot("supp_figure1.pdf",sup1, ncol=1, base_height=6.5,  base_asp=1.4)
save_plot("supp_figure1.png",sup1, ncol=1, base_height=6.5,  base_asp=1.4)
getwd()
ggplot(p,aes(x=cclass,y=relAb,fill=Aerobicity))+geom_bar(stat="identity")+scale_fill_manual(values=c( "#E69F00" ,"purple2","#CC3366","#56B4E9","#C0C0C0"),labels=c("S4__C"="CF pathogens","S4__A"="Aerobes", "S4__F"="Fac. anaerobes", "S4__N"="Anaerobes", "S4__U"="Unknown"))+ylab("Mean rel. abundance")+xlab("Community class (DMM component)")+meg_stef_theme()+ggtitle("Mean composition of DMM comunity classes")+theme(legend.key.size=unit(16, 'points'))+ guides(fill=guide_legend(title="ASV group"))
s1A<-ggplot(p,aes(x=cclass,y=relAb,fill=Aerobicity))+geom_bar(stat="identity")+scale_fill_manual(values=c( "#E69F00" ,"purple2","#CC3366","#56B4E9","#C0C0C0"),labels=c("S4__C"="CF pathogens","S4__A"="Aerobes", "S4__F"="Fac. anaerobes", "S4__N"="Anaerobes", "S4__U"="Unknown"))+ylab("Mean rel. abundance")+xlab("Community class (DMM component)")+meg_stef_theme()+ggtitle("Mean composition of DMM comunity classes")+theme(legend.key.size=unit(16, 'points'))+ guides(fill=guide_legend(title="ASV group"))
plot_grid(s1A,s1B+theme(legend.position="none"), labels=c("A","B"),align = "hv", ncol=2, rel_widths=c(1.2,1),label_size = 12, label_fontface = "bold")
s1AB<-plot_grid(s1A,s1B+theme(legend.position="none"), labels=c("A","B"),align = "hv", ncol=2, rel_widths=c(1.2,1),label_size = 12, label_fontface = "bold")
sup1<-plot_grid(s1AB,s1CD,nrow=2,align = "v", rel_heights=c(1,1.2))
sup1
save_plot("supp_figure1.pdf",sup1, ncol=1, base_height=6.5,  base_asp=1.4)
save_plot("supp_figure1.png",sup1, ncol=1, base_height=6.5,  base_asp=1.4)
q()
