-
Notifications
You must be signed in to change notification settings - Fork 0
/
nparLD_analysis.R
155 lines (122 loc) · 5.59 KB
/
nparLD_analysis.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
###
# Author: Xiyu Peng
# stat analysis and visualization for single clusters.
#
library(tidyverse)
library(ggpubr)
library(patchwork)
library(ggplot2)
merge.data <- read.csv("~/flow_cytometry/lda/merge_data_X50_1.5_new.csv")
rm_samples<-c("17-162-32C","17-162-34B","17-162-07B") ## not contains enough cells
merge.data<-merge.data[!merge.data$sample %in% rm_samples,]
merge.data %>% select(starts_with("cluster")) %>% colnames()->colnames_data
merge.data$res<-factor(merge.data$res)
levels(merge.data$res)
levels(merge.data$res)<-c("PD/SD","CR/PR")
merge.data.reformat<-merge.data %>% gather(.,key = "cluster",value = "freq",colnames_data)
library(nparLD)
merge.data.sub.reformat<-merge.data.reformat %>% filter(pt %in% names(which(colSums(table(merge.data$time,merge.data$pt))==3)))
plot_clusters<-paste("cluster",0:19,sep = "")
### across imtypes
for (c in plot_clusters){
re<-merge.data.sub.reformat %>% filter(cluster == c) %>% nparLD(freq ~ imtype*time, data = .,subject = "pt",description = FALSE)
print(c)
print(re)
}
### across response
for (c in plot_clusters){
re<-merge.data.sub.reformat %>% filter(cluster == c) %>% nparLD(freq ~ res*time, data = .,subject = "pt",description = FALSE)
print(c)
print(re)
}
### across toxicity
for (c in plot_clusters){
re<-merge.data.sub.reformat %>% filter(cluster == c) %>% nparLD(freq ~ tox*time, data = .,subject = "pt",description = FALSE)
print(c)
print(re)
}
### across time
pvalues<-c()
for (c in plot_clusters){
re<-merge.data.sub.reformat %>% filter(cluster == c) %>% nparLD(freq ~ time, data = .,subject = "pt",description = FALSE)
print(c)
print(re)
pvalues<-c(pvalues,re$ANOVA.test$`p-value`)
}
p.adjust(pvalues,method = "BH")
names(pvalues)<-paste0("cluster",0:19)
## organize re$ANOVA.test and re$ANOVA.test.mod.Box into tables
result<-list()
for (c in plot_clusters){
### immunotypes
re<-merge.data.sub.reformat %>% filter(cluster == c) %>% nparLD(freq ~ imtype*time, data = .,subject = "pt",description = FALSE)
imm_array<-c(array(t(re$ANOVA.test)),re$ANOVA.test.mod.Box)
imm_header<-outer(rownames(t(re$ANOVA.test)),colnames(t(re$ANOVA.test)),paste, sep = ".")
imm_header<-c(imm_header, outer(rownames(t(re$ANOVA.test.mod.Box)),colnames(t(re$ANOVA.test.mod.Box)),paste, sep = "."))
imm_header[13]<-"p-value.mod.imtype"
### response
re<-merge.data.sub.reformat %>% filter(cluster == c) %>% nparLD(freq ~ res*time, data = .,subject = "pt",description = FALSE)
res_array<-c(array(t(re$ANOVA.test)),re$ANOVA.test.mod.Box)
res_header<-outer(rownames(t(re$ANOVA.test)),colnames(t(re$ANOVA.test)),paste, sep = ".")
res_header<-c(res_header, outer(rownames(t(re$ANOVA.test.mod.Box)),colnames(t(re$ANOVA.test.mod.Box)),paste, sep = "."))
res_header[13]<-"p-value.mod.res"
### toxicity
re<-merge.data.sub.reformat %>% filter(cluster == c) %>% nparLD(freq ~ tox*time, data = .,subject = "pt",description = FALSE)
tox_array<-c(array(t(re$ANOVA.test)),re$ANOVA.test.mod.Box)
tox_header<-outer(rownames(t(re$ANOVA.test)),colnames(t(re$ANOVA.test)),paste, sep = ".")
tox_header<-c(tox_header, outer(rownames(t(re$ANOVA.test.mod.Box)),colnames(t(re$ANOVA.test.mod.Box)),paste, sep = "."))
tox_header[13]<-"p-value.mod.tox"
result[[c]]<-c(imm_array,res_array,tox_array)
}
resultdf<-t(as.data.frame(result))
colnames(resultdf)<-c(imm_header,res_header,tox_header)
resultdf<-as.data.frame(resultdf)
## adjust pvalue with selected columns
select_col<-c("p-value.mod.imtype","p-value.mod.res","p-value.mod.tox","p-value.imtype:time","p-value.res:time","p-value.tox:time")
adj_pvalues<-resultdf %>%
dplyr::select(select_col) %>%
mutate(across(select_col,p.adjust,method = "BH"))
colnames(adj_pvalues)<-paste0("adj.",colnames(adj_pvalues))
resultdf<-cbind(resultdf,adj_pvalues)
## write the result
write.csv(resultdf,file = "~/flow_cytometry/lda/single_cluster_stat_analysis.csv")
#### make boxplots
colors<-c("#D55E00", "#009E73", "#0072B2")
merge.data.reformat$week<-as.factor(merge.data.reformat$time)
levels(merge.data.reformat$week)<-c("0","3","6")
## immunotypes
plot_clusters<-paste("cluster",0:19,sep = "")
ps_imtype<-list()
for (c in plot_clusters){
## boxplot with data points connected with lines
p<-merge.data.reformat %>%
mutate (freq = freq/100) %>%
filter(cluster == c) %>%
ggboxplot(.,x = "week", y = "freq", add = "point", fill = "imtype", palette = colors, facet.by = "imtype")+geom_line(aes(group = pt),color = 'gray')+theme(legend.position = "none")+ggtitle(c)+ylab("Percentage")
ps_imtype[[c]]<-p
}
wrap_plots(ps_imtype[1:20] ,guides = 'collect')
ggsave("imtype_clusters.png",width = 20, height = 20,units = "in")
## response
ps_res<-list()
for (c in plot_clusters){
## boxplot with data points connected with lines
p<-merge.data.reformat %>%
mutate (freq = freq/100) %>%
filter(cluster == c) %>%
ggboxplot(.,x = "week", y = "freq", add = "point", fill = "res", palette = colors, facet.by = "res")+geom_line(aes(group = pt),color = 'gray')+theme(legend.position = "none")+ggtitle(c)+ylab("Percentage")
ps_res[[c]]<-p
}
wrap_plots(ps_res[1:20] ,guides = 'collect')
ggsave("res_clusters.png",width = 16, height = 20,units = "in")
## tox
ps_tox<-list()
for (c in plot_clusters){
p<-merge.data.reformat %>%
mutate (freq = freq/100) %>%
filter(cluster == c) %>%
ggboxplot(.,x = "week", y = "freq", add = "point", fill = "tox", palette = colors, facet.by = "tox")+geom_line(aes(group = pt),color = 'gray')+theme(legend.position = "none")+ggtitle(c)+ylab("Percentage")
ps_tox[[c]]<-p
}
wrap_plots(ps_tox[1:20] ,guides = 'collect')
ggsave("tox_clusters.png",width = 16, height = 20,units = "in")