-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_barplot.R
executable file
·234 lines (213 loc) · 10.2 KB
/
function_barplot.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/home/admin1/miniconda2/bin/Rscript
#utf-8
library(optparse)
#######arguments
option_list <- list(
make_option(c("-i", "--input"),metavar="path", dest="func",help="Abundance table. Required",default=NULL),
make_option(c("-f", "--feature-col"),metavar="int", dest="fcol",help="which column contain the feature name. -1 for the last column",default=-1),
make_option(c("-m", "--map"),metavar="path",dest="map", help="Specify the path of mapping file. Required",default=NULL),
make_option(c("-c", "--category"),metavar="string",dest="group", help="Category to compare. Required",default=NULL),
make_option(c("-t", "--colors"),metavar="string",dest="colors", help="Comma seprated group colors.",default=NULL),
make_option(c("-l", "--log"),metavar = "logical",dest="log", help="If TRUE, log the data before comparing. Options of logical type are TRUE, T, FALSE, F. default = FALSE",default = "FALSE"),
make_option(c("-j", "--adjust-p"),metavar = "logical",dest="ap", help="If TRUE, adjust the p value before barplot. default = TRUE",default = "TRUE"),
make_option(c("-e", "--add-se"),metavar = "logical",dest="se", help="If TRUE, add SE error bar, otherwise add SD error bar. default = TRUE",default = "TRUE"),
make_option(c("-a", "--alpha"),metavar = "float",dest="alpha", help="Alpha for significance. default=0.05",default=0.05),
make_option(c("-b", "--output-by-L1"),metavar = "logical",dest="bl", help="IF TRUE, output barplot for each pathway of L1 level. If FALSE, output only one plot anyway. Or use auto to determine by the number of siginficant function (10)",default="auto"),
make_option(c("-s", "--scale"),metavar = "logical",dest="scale", help="IF TRUE, scale the data before plot.",default="F"),
make_option(c("-k", "--skip"),metavar = "logical",dest="skip", help="IF TRUE, skip the first line.",default=TRUE),
make_option(c("-p", "--prefix"),metavar="str", dest="prefix",help="The prefix of output files, default if ''",default=""),
make_option(c("-o", "--output"),metavar="directory",dest="out", help="Specify the directory of output files. default=./",default="./")
)
opt <- parse_args(OptionParser(option_list=option_list,description = "This script is used to compare the predicted pathway of level 3"))
library(stringr)
library(ggpubr)
library(reshape2)
library(agricolae)
library(ggplot2)
library(getopt)
if(!dir.exists(opt$out)){dir.create(opt$out,recursive = T)}
opt$out<-paste(opt$out,"/",opt$prefix, sep="")
if(as.logical(opt$skip)){
func<-read.table(opt$func,comment.char="",quote = "",skip = 1,check.names=F,stringsAsFactors=F, header = TRUE, sep = "\t")
}else{
func<-read.table(opt$func,comment.char="",quote = "",check.names=F,stringsAsFactors=F, header = TRUE, sep = "\t")
}
map<-read.table(opt$map,sep="\t",na.strings="",header = T,row.names=1,comment.char = "",check.names = F,stringsAsFactors = F)
group<-map[opt$group]
group<-na.omit(group)
#clean na of group
#兼容云平台和16S流程
fcol = as.numeric(opt$fcol)
if(fcol==-1){
rownames(func)<-func[,ncol(func)]
}else{
rownames(func)<-func[,fcol]
}
df_map<-function(df, func){
out<-c()
for(i in 1:ncol(df)){
out[i]<-func(df[, i])
}
return(out)
}
#func<-func[,-c(1,ncol(func))]
func<-func[,df_map(func, is.numeric)]
#清理数据
# print(group)
# print(func)
func<-func[,match(rownames(group),colnames(func))]
#clean na of func
func<-apply(func,2,function(x){x/sum(x)})
#求相对丰度
func<-t(func)
func[is.na(func)] <- 0
func<-func[,colSums(func>0)>=nrow(func)*0.5]
#剔除观测样本数小于50%总样本数的功能
if(as.logical(opt$log)){
func<-func-min(func)
min2<-min(func[func!=0])
func<-log(func+min2,base = 10)-log(min2,base = 10)
}
#log转化,使得尽量符合正太分布
if(as.logical(opt$scale)){
func<-scale(func,center=T,scale=T)
}
#normalize the data, make the plot easier to compare
func<-data.frame(t(func),check.names = F)
group<-group[match(colnames(func),rownames(group)),]
N_sample<-ncol(func)
sprintf("Proccesing anova of %d samples",N_sample)
data_for_anova<-melt(data.frame(group=group,t(func),check.names = FALSE),id.vars = "group")
anova_results<-compare_means(value~group,data = data_for_anova,method = "anova",group.by = "variable")
write.table(anova_results,paste(opt$out, opt$group,"_all_pathway_anova_results.xls",sep = ""),sep = "\t",row.names = F)
if(as.logical(opt$ap)){
pvalue<-anova_results$p.adj
}else{
pvalue<-anova_results$p
}
siginficant_function<-as.character(anova_results$variable[pvalue<as.numeric(opt$alpha)])
uni_group<-sort(unique(group))
N_group<-length(uni_group)
my_duncan<-function(x){
data_for_duncan<-data.frame(group=group,value=x)
anova <- aov(value~group,data = data_for_duncan)
plotdata<-duncan.test(anova,"group", alpha = 0.05)
out<-plotdata$groups
out<-out[match(uni_group,rownames(out)),]
out1<-as.character(out[,2])
names(out1)<-rownames(out)
return(out1)
}
if(opt$bl=="auto"){
lsig<-length(siginficant_function)
if(lsig>10){
how<-1
}else{
how<-2
}
}else{
if(as.logical(opt$bl)){
how<-1
}else{
how<-2
}
}
if(is.null(opt$colors)){
base_dir<-normalizePath(dirname(get_Rscript_filename()))
source(paste(base_dir,"/piputils/get_colors.R", sep = ""))
groups_color<-get_colors(opt$group, opt$map)
}else{
groups_color<-str_split(opt$colors, ",")[[1]]
}
if(how==1){
LF<-str_extract(rownames(func),'^[^;]+')
unilf<-unique(LF)
for (l1 in unilf){
if(sum(LF==l1&rownames(func)%in%siginficant_function)>=1){
sub_func<-func[LF==l1&rownames(func)%in%siginficant_function,]
res<-apply(sub_func,1,my_duncan)
LR<-str_extract(colnames(res),"[^;]+$")
N_res<-ncol(res)
sum_mean<-apply(sub_func,1,function(x){tapply(x,INDEX = group,mean)})
sum_se<-apply(sub_func,1,function(x){tapply(x,INDEX = group,ifelse(as.logical(opt$se),function(x){sd(x)/sqrt(N_sample)},sd))})
sum_mean<-sum_mean[match(uni_group,rownames(sum_mean)),]
res<-res[match(uni_group,rownames(res)),]
sum_se<-sum_se[match(uni_group,rownames(sum_se)),]
bar_data<-data.frame(matrix(nrow = N_group*N_res,ncol = 5))
colnames(bar_data)<-c("pathway","group","mean","se","sig")
bar_data$pathway<-rep(LR,each=N_group)
bar_data$group<-rep(uni_group,times=N_res)
bar_data$mean<-as.vector(sum_mean)
bar_data$se<-as.vector(sum_se)
bar_data$sig<-as.vector(res)
bar_data<-bar_data[order(bar_data$pathway),]
# print(bar_data$group)
p1<-ifelse(0.2*N_group*N_res+2<50,0.2*N_group*N_res+2,49.9)
p2<-max(bar_data$mean)/18
cud<-0.8/(2*N_group)
pos<-rep(seq(from=0,to=N_res-1),each=N_group)
cood_x<-rep(seq(0.6+cud,1.4-cud,length.out = N_group),times=N_res)+pos
p<-ggplot(bar_data, aes(x=pathway, y=mean, fill=group)) +
geom_bar(width = 0.8,position=position_dodge(0.8), stat="identity") +
geom_errorbar(aes(ymin=mean-se, ymax=mean+se),width=0.2,position=position_dodge(0.8))+
geom_text(aes(x=cood_x,y=mean+se+p2,label=sig))+
guides(fill=guide_legend(title = NULL))+
xlab("")+ylab("Abundance")+theme_bw()+
theme(text = element_text(size = 12),
panel.grid.major = element_blank(),panel.grid.minor = element_blank(),
axis.line = element_line(),panel.border = element_blank(),
axis.text.x = element_text(angle = 90,size = 9,vjust = 1,hjust = 1))+scale_y_continuous(expand = c(0, 0))+
scale_fill_manual(values = groups_color)
ggsave(plot = p,paste(opt$out, opt$group,"_",l1,"_barplot_of_duncan.pdf",sep = ""),dpi=300,height = 7,width = p1)
if(as.logical(opt$se)){
colnames(bar_data)<-c("KEGG pathway","Group","Mean","SE","Duncan significance")
}else{
colnames(bar_data)<-c("KEGG pathway","Group","Mean","SD","Duncan significance")
}
write.table(bar_data,paste(opt$out, opt$group,"_",l1,"_duncan_results.xls",sep = ""),sep = "\t",row.names = F)
}
}
}else{
if(sum(rownames(func)%in%siginficant_function)>=1){
sub_func<-func[rownames(func)%in%siginficant_function,]
res<-apply(sub_func,1,my_duncan)
LR<-str_extract(colnames(res),"[^;]+$")
N_res<-ncol(res)
sum_mean<-apply(sub_func,1,function(x){tapply(x,INDEX = group,mean)})
sum_se<-apply(sub_func,1,function(x){tapply(x,INDEX = group,ifelse(as.logical(opt$se),function(x){sd(x)/sqrt(N_sample)},sd))})
sum_mean<-sum_mean[match(uni_group,rownames(sum_mean)),]
res<-res[match(uni_group,rownames(res)),]
sum_se<-sum_se[match(uni_group,rownames(sum_se)),]
bar_data<-data.frame(matrix(nrow = N_group*N_res,ncol = 5))
colnames(bar_data)<-c("pathway","group","mean","se","sig")
bar_data$pathway<-rep(LR,each=N_group)
bar_data$group<-rep(uni_group,times=N_res)
bar_data$mean<-as.vector(sum_mean)
bar_data$se<-as.vector(sum_se)
bar_data$sig<-as.vector(res)
bar_data<-bar_data[order(bar_data$pathway),]
p1<-ifelse(0.2*N_group*N_res+2<50,0.2*N_group*N_res+2,49.9)
p2<-max(bar_data$mean)/18
cud<-0.8/(2*N_group)
pos<-rep(seq(from=0,to=N_res-1),each=N_group)
cood_x<-rep(seq(0.6+cud,1.4-cud,length.out = N_group),times=N_res)+pos
p<-ggplot(bar_data, aes(x=pathway, y=mean, fill=group)) +
geom_bar(width = 0.8,position=position_dodge(0.8), stat="identity") +
geom_errorbar(aes(ymin=mean-se, ymax=mean+se),width=0.2,position=position_dodge(0.8))+
geom_text(aes(x=cood_x,y=mean+se+p2,label=sig))+
guides(fill=guide_legend(title = NULL))+
xlab("")+ylab("Abundance")+theme_bw()+
theme(text = element_text(size = 12),
panel.grid.major = element_blank(),panel.grid.minor = element_blank(),
axis.line = element_line(),panel.border = element_blank(),
axis.text.x = element_text(angle = 90,size = 9,vjust = 1,hjust = 1))+scale_y_continuous(expand = c(0, 0))+
scale_fill_manual(values = groups_color)
ggsave(plot = p,paste(opt$out, opt$group,"_all_significant_pathway_barplot_of_duncan.pdf",sep = ""),dpi=300,height = 7,width = p1)
if(as.logical(opt$se)){
colnames(bar_data)<-c("KEGG pathway","Group","Mean","SE","Duncan significance")
}else{
colnames(bar_data)<-c("KEGG pathway","Group","Mean","SD","Duncan significance")
}
write.table(bar_data,paste(opt$out, opt$group,"_all_significant_pathway_duncan_results.xls",sep = ""),sep = "\t",row.names = F)
}
}