-
Notifications
You must be signed in to change notification settings - Fork 0
/
DE_phylum_deseq2.R
224 lines (200 loc) · 8.32 KB
/
DE_phylum_deseq2.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
# This script perform phylum level DE analysis
library(DESeq2)
setwd("/Users/zrayw/Desktop/Alex_Lab/PN_microbiome/analysis")
## We first summarize shared file and taxonomy file to phylum level
shared_phylum = shared %>%
pivot_longer(-sample, names_to = "otu", values_to = "count") %>%
inner_join(., taxonomy %>% select(otu, phylum)) %>%
group_by(sample, phylum) %>%
summarize(count = sum(count)) %>%
pivot_wider(names_from = "phylum", values_from = "count") %>%
as.data.frame()
rownames(shared_phylum) = shared_phylum$sample
## non-lesional v.s. lesional areas at baseline, controlling for individual effect
map_temp = map_sample %>% filter(visit == "V3")
keep = names(which(tapply(map_temp$lesional, map_temp$subjid, function(x){length(unique(x))}) == 2))
count_data = shared_phylum %>%
mutate(sample = str_replace_all(string=sample, pattern="non_lesional", replacement="non-lesional")) %>%
separate(col="sample", into = c("unique", "subjid", "lesional","visit"), sep="_", remove=FALSE) %>%
filter(subjid %in% keep & visit == "V3") %>%
arrange(sample) %>%
select(-sample, -unique, -subjid, -lesional, -visit) %>%
t()
col_data = map_sample %>% filter(subjid %in% keep & visit == "V3") %>% arrange(sample)
dds = DESeqDataSetFromMatrix(countData = count_data,
colData = col_data,
design = ~ lesional + subjid)
dds$lesional = relevel(dds$lesional, ref = "non_lesional")
dds = DESeq(dds)
res = results(dds, name = "lesional_lesional_vs_non_lesional", pAdjustMethod="fdr" )
de_phylum_lesional = res@listData %>%
as.data.frame() %>%
mutate(phylum = res@rownames)
## baseline v.s. week 12 at lesional areas, controlling for individual effect, in treatment groups
map_temp = map_sample %>% filter(lesional == "lesional") ###
keep = names(which(tapply(map_temp$visit, map_temp$subjid, function(x){length(unique(x))}) == 2)) ###
count_data = shared_phylum %>%
inner_join(., map_sample %>% select(sample, subjid, trt01p, lesional)) %>%
filter(subjid %in% keep & lesional == "lesional" & trt01p == "Nemolizumab 0.5mg/kg") %>% ###
arrange(sample) %>%
select(-sample, -subjid, -lesional, -trt01p) %>%
t()
col_data = map_sample %>%
filter(subjid %in% keep & lesional == "lesional" & trt01p == "Nemolizumab 0.5mg/kg") %>% ###
arrange(sample)
dds = DESeqDataSetFromMatrix(countData = count_data,
colData = col_data,
design = ~ visit + subjid)
dds = DESeq(dds)
res = results(dds, name = "visit_V8_vs_V3", pAdjustMethod="fdr" )
de_phylum_visit_treatment = res@listData %>%
as.data.frame() %>%
mutate(phylum = res@rownames)
## baseline v.s. week 12 at lesional areas, controlling for individual effect, in placebo groups
map_temp = map_sample %>% filter(lesional == "lesional") ###
keep = names(which(tapply(map_temp$visit, map_temp$subjid, function(x){length(unique(x))}) == 2)) ###
count_data = shared_phylum %>%
inner_join(., map_sample %>% select(sample, subjid, trt01p, lesional)) %>%
filter(subjid %in% keep & lesional == "lesional" & trt01p == "Placebo") %>% ###
arrange(sample) %>%
select(-sample, -subjid, -lesional, -trt01p) %>%
t()
col_data = map_sample %>%
filter(subjid %in% keep & lesional == "lesional" & trt01p == "Placebo") %>% ###
arrange(sample)
dds = DESeqDataSetFromMatrix(countData = count_data,
colData = col_data,
design = ~ visit + subjid)
dds = DESeq(dds)
res = results(dds, name = "visit_V8_vs_V3", pAdjustMethod="fdr" )
de_phylum_visit_placebo = res@listData %>%
as.data.frame() %>%
mutate(phylum = res@rownames)
rbind(de_phylum_lesional %>%
select(phylum, log2FoldChange, padj) %>%
mutate(var = "l"),
de_phylum_visit_treatment %>%
select(phylum, log2FoldChange, padj) %>%
mutate(var = "t")) %>%
rbind(.,
de_phylum_visit_placebo %>%
select(phylum, log2FoldChange, padj) %>%
mutate(var = "p")) %>%
mutate(sig = ifelse(padj <= 0.1, "Y", "N")) %>%
ggplot(aes(x = var, y = log2FoldChange, fill = var, color = sig)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~ phylum, scales = "free") +
scale_fill_manual(name = NULL,
breaks = c("l", "p", "t"),
labels = c("non-lesional vs lesional",
"baseline vs week 12 in placebo",
"baseline vs week 12 in treatment"),
values = c("grey", "blue", "red")) +
scale_color_manual(name = NULL,
breaks = c("Y", "N"),
values = c("black", "white")) +
labs(title = NULL,
x = NULL,
y = "log-fold change") +
guides(color = FALSE) +
theme_bw() +
theme(axis.title.y = element_text(size = 25, face = "bold", family = "Times"),
axis.title.x = element_blank(),
axis.text = element_text(size = 13, family = "Times"),
axis.line = element_line(),
legend.text = element_text(size = 25, face = "bold", family = "Times"),
strip.text = element_text(size = 18, face = "bold.italic", family = "Times"),
strip.background = element_blank(),
panel.grid = element_blank(),
)
## log fold change correlation with lesional v.s. non-lesional at baseline (treatments/ placebos)
### treatments
data = inner_join(
de_phylum_lesional %>% select(phylum, log2FoldChange, padj),
de_phylum_visit_treatment %>% select(phylum, log2FoldChange, padj),
by = "phylum", suffix = c(".1", ".2")
)
cor.test(
data$log2FoldChange.1,
data$log2FoldChange.2,
method = "spearman"
) # -0.6607143 p = 0.009043
data %>%
summarize(con.t = sum(log2FoldChange.1*log2FoldChange.2 < 0)) # 14
data %>%
mutate(
sig.1 = (padj.1 <= 0.1)*1,
sig.2 = (padj.2 <= 0.1)*1,
group = sig.1 + sig.2
) %>%
ggplot(aes(
x = log2FoldChange.1, y = log2FoldChange.2,
color = as.character(sig.1),
shape = as.character(sig.2),
size = as.character(group)
)) +
geom_point() +
geom_abline(intercept = 0, slope = -1, lty = 2) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
labs(
title = NULL,
x = "non-lesional vs\nlesional at baseline",
y = "baseline vs week-\n12 in lesional skin"
) +
coord_cartesian(xlim = c(-2.5, 2.5), ylim = c(-2.5, 2.5)) +
scale_shape_manual(breaks = c("0", "1"), values = c(16, 17)) +
scale_color_manual(breaks = c("0", "1"), values = c("grey", "black")) +
scale_size_manual(breaks = c("0", "1", "2"), values = c(2, 3, 5)) +
theme_classic() +
theme(
legend.position = "none",
axis.title = element_text(size = 18, face = "bold", family = "Times"),
axis.text = element_text(size = 13, face = "bold", family = "Times")
)
ggsave("manuscript figures/scatter.logfc_deseq2_phylumn_treatment.tiff", width = 4, height = 4, dpi = 500)
### placebos
data = inner_join(
de_phylum_lesional %>% select(phylum, log2FoldChange, padj),
de_phylum_visit_placebo %>% select(phylum, log2FoldChange, padj),
by = "phylum", suffix = c(".1", ".2")
)
cor.test(
data$log2FoldChange.1,
data$log2FoldChange.2,
method = "spearman"
) # 0.2321429 p = 0.4039
data %>%
summarize(con.t = sum(log2FoldChange.1*log2FoldChange.2 < 0)) # 7
data %>%
mutate(
sig.1 = (padj.1 <= 0.1)*1,
sig.2 = (padj.2 <= 0.1)*1,
group = sig.1 + sig.2
) %>%
ggplot(aes(
x = log2FoldChange.1, y = log2FoldChange.2,
color = as.character(sig.1),
shape = as.character(sig.2),
size = as.character(group)
)) +
geom_point() +
geom_abline(intercept = 0, slope = -1, lty = 2) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
labs(
title = NULL,
x = "non-lesional vs\nlesional at baseline",
y = "baseline vs week-\n12 in lesional skin"
) +
coord_cartesian(xlim = c(-2.5, 2.5), ylim = c(-2.5, 2.5)) +
scale_shape_manual(breaks = c("0", "1"), values = c(16, 17)) +
scale_color_manual(breaks = c("0", "1"), values = c("grey", "black")) +
scale_size_manual(breaks = c("0", "1", "2"), values = c(2, 3, 5)) +
theme_classic() +
theme(
legend.position = "none",
axis.title = element_text(size = 18, face = "bold", family = "Times"),
axis.text = element_text(size = 13, face = "bold", family = "Times")
)
ggsave("manuscript figures/scatter.logfc_deseq2_phylum_placebo.tiff", width = 4, height = 4, dpi = 500)