-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBPSC_test.R
More file actions
271 lines (188 loc) · 8.56 KB
/
BPSC_test.R
File metadata and controls
271 lines (188 loc) · 8.56 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#### BPSC for Cell Type Specific AS events
library(BPSC)
#################################################################################################################################################
#### SE #########################################################################################################################################
#################################################################################################################################################
load("/mnt/data5/BGI/UCB/tangchao/DSU2/SE/SE_psi_new.RData")
PSI <- SE_psi[, 13:ncol(SE_psi)]
row.names(PSI) <- SE_psi$loci
Cell_type <- read.table("/mnt/data5/BGI/UCB/ExpMat_NewID/Cell_type.txt", header = F, row.names = 1, sep = "\t", stringsAsFactors=F)
colnames(Cell_type) <- "CellType"
Cell_type <- data.frame(CellType = Cell_type[order(Cell_type),], row.names = row.names(Cell_type)[order(Cell_type)])
Cell_type$Individual <- substr(row.names(Cell_type), 1, 4)
PSI <- PSI[, row.names(Cell_type)]
dim(PSI)
# [1] 17670 3039
rs <- rowSums(!is.na(PSI))
sum(rs == 0)
#[1] 53
PSI_tu <- PSI[rs>=1, ]
PSI_tu[is.na(PSI_tu)] <- 0
group=c(rep(1,sum(Cell_type$CellType == "B Cells")),rep(2,sum(Cell_type$CellType != "B Cells")))
controlIds = colnames(PSI_tu)[which(group == 2)]
design = model.matrix(~group)
coef = 2
# Run BPglm for differential expression analysis
library(doParallel)
registerDoParallel(cores=16)
res = BPglm(data = as.matrix(PSI_tu), controlIds = controlIds, design = design, coef = coef, estIntPar = TRUE, useParallel = TRUE)
hist(res$PVAL, breaks = 20)
# Summarize the resutls
ss = summary(res)
fdr = p.adjust(res$PVAL, method = "BH")
bpglm.DE.ids = row.names(PSI_tu)[which(fdr <= 0.01)]
length(bpglm.DE.ids)# Print the indices of the true DE genes:
# [1] 1379
bpglm.DE.ids -> BCel_Sep
table(Cell_type$CellType)
group <- rep(c(1,2,1,1,1,1), table(Cell_type$CellType))
controlIds = colnames(PSI_tu)[which(group == 1)]
design = model.matrix(~group)
coef = 2
res = BPglm(data = as.matrix(PSI_tu), controlIds = controlIds, design = design, coef = coef, estIntPar = TRUE, useParallel = TRUE)
hist(res$PVAL, breaks = 20)
# Summarize the resutls
ss = summary(res)
fdr = p.adjust(res$PVAL, method = "BH")
bpglm.DE.ids = row.names(PSI_tu)[which(fdr <= 0.01)]
length(bpglm.DE.ids)# Print the indices of the true DE genes:
# [1] 1153
bpglm.DE.ids -> CD4_Sep
group <- rep(c(1,1,2,1,1,1), table(Cell_type$CellType))
controlIds = colnames(PSI_tu)[which(group == 1)]
design = model.matrix(~group)
coef = 2
res = BPglm(data = as.matrix(PSI_tu), controlIds = controlIds, design = design, coef = coef, estIntPar = TRUE, useParallel = TRUE)
hist(res$PVAL, breaks = 20)
# Summarize the resutls
ss = summary(res)
fdr = p.adjust(res$PVAL, method = "BH")
bpglm.DE.ids = row.names(PSI_tu)[which(fdr <= 0.01)]
length(bpglm.DE.ids)# Print the indices of the true DE genes:
# [1] 793
bpglm.DE.ids -> CD8_Sep
group <- rep(c(1,1,1,2,1,1), table(Cell_type$CellType))
controlIds = colnames(PSI_tu)[which(group == 1)]
design = model.matrix(~group)
coef = 2
res = BPglm(data = as.matrix(PSI_tu), controlIds = controlIds, design = design, coef = coef, estIntPar = TRUE, useParallel = TRUE)
hist(res$PVAL, breaks = 20)
# Summarize the resutls
ss = summary(res)
fdr = p.adjust(res$PVAL, method = "BH")
bpglm.DE.ids = row.names(PSI_tu)[which(fdr <= 0.01)]
length(bpglm.DE.ids)# Print the indices of the true DE genes:
# [1] 152
bpglm.DE.ids -> MK_Sep
group <- rep(c(1,1,1,1,2,1), table(Cell_type$CellType))
controlIds = colnames(PSI_tu)[which(group == 1)]
design = model.matrix(~group)
coef = 2
res = BPglm(data = as.matrix(PSI_tu), controlIds = controlIds, design = design, coef = coef, estIntPar = TRUE, useParallel = TRUE)
hist(res$PVAL, breaks = 20)
# Summarize the resutls
ss = summary(res)
fdr = p.adjust(res$PVAL, method = "BH")
bpglm.DE.ids = row.names(PSI_tu)[which(fdr <= 0.01)]
length(bpglm.DE.ids)# Print the indices of the true DE genes:
# [1] 376
bpglm.DE.ids -> Mono_Sep
group <- rep(c(1,1,1,1,1,2), table(Cell_type$CellType))
controlIds = colnames(PSI_tu)[which(group == 1)]
design = model.matrix(~group)
coef = 2
res = BPglm(data = as.matrix(PSI_tu), controlIds = controlIds, design = design, coef = coef, estIntPar = TRUE, useParallel = TRUE)
hist(res$PVAL, breaks = 20)
# Summarize the resutls
ss = summary(res)
fdr = p.adjust(res$PVAL, method = "BH")
bpglm.DE.ids = row.names(PSI_tu)[which(fdr <= 0.01)]
length(bpglm.DE.ids)# Print the indices of the true DE genes:
# [1] 295
bpglm.DE.ids -> NK_Sep
lapply(list(BCel_Sep, CD4_Sep, CD8_Sep, MK_Sep, Mono_Sep, NK_Sep),length)
length(Reduce(union, list(BCel_Sep, CD4_Sep, CD8_Sep, MK_Sep, Mono_Sep, NK_Sep)))
c(BCel_Sep, CD4_Sep, CD8_Sep, MK_Sep, Mono_Sep, NK_Sep) -> juncs
PSI[juncs, row.names(Cell_type)] -> te_sub
dim(te_sub)
te_sub[is.na(te_sub)] <- -0.2
cells <- c("B Cells", "CD4+ T Cells", "CD8+ T Cells", "Megakaryocytes", "Monocytes", "NK cells")
sj_anno <- matrix(rep(cells, c(length(BCel_Sep),length(CD4_Sep),length(CD8_Sep),length(MK_Sep),length(Mono_Sep),length(NK_Sep))), ncol = 1)
rownames(sj_anno) <- row.names(te_sub)
#sj_anno <- data.frame(sj_anno[juncs,])
colnames(sj_anno) <- "Cell"
sj_anno <- as.data.frame(sj_anno)
sj_anno$Cell <- factor(sj_anno$Cell, levels = cells)
dim(sj_anno)
# [1] 1215 1
identical(row.names(sj_anno), row.names(te_sub))
# [1] TRUE
library(RColorBrewer)
library(pheatmap)
breaksList = seq(-0.2,1,0.1)
library(RColorBrewer)
colors = c(brewer.pal(3,"Greens")[1], colorRampPalette(brewer.pal(n = 9, name = "Reds"))(11))
#colors = c(rep(brewer.pal(3,"Greens")[1], 4), brewer.pal(,"Reds")[4:9])
brewer.pal(6,"BrBG")
ann_colors = list(
Individual = c(UCB1 = "#7570B3", UCB3 = "#E7298A", UCB4 = "#66A61E"),
CellType = c("#8C510A","#D8B365","#F6E8C3","#C7EAE5","#5AB4AC","#01665E"),
Cell = c("#8C510A","#D8B365","#F6E8C3","#C7EAE5","#5AB4AC","#01665E")
)
names(ann_colors[[2]]) <- names(table(Cell_type$CellType))
names(ann_colors[[3]]) <- c(names(table(Cell_type$CellType)))
cumsum(as.numeric(table(sj_anno$Cell)))[1:5]
library(pheatmap)
pdf("/mnt/data5/BGI/UCB/tangchao/DSU2/SE/Cell_Sep/All_Cell_specific_SE_heatmap_BPSC.pdf")
pheatmap(te_sub, annotation_col = Cell_type, color = colors, breaks = breaksList,
annotation_names_row = FALSE, gaps_row = cumsum(as.numeric(table(sj_anno$Cell)))[1:5],
gaps_col = cumsum(as.numeric(table(Cell_type$CellType)))[1:5],
cluster_cols = F, cluster_rows = F, show_colnames = F, show_rownames = F)
dev.off()
PSI_tu <- PSI[rs>=1, ]
PSI_tu[is.na(PSI_tu)] <- 0
PSI_tu <- PSI_tu[, row.names(Cell_type[Cell_type$CellType %in% c("B Cells", "CD8+ T Cells"),])]
group = c(rep(1,sum(Cell_type$CellType == "B Cells")),rep(2,sum(Cell_type$CellType == "CD8+ T Cells")))
controlIds = colnames(PSI_tu)[which(group == 1)]
design = model.matrix(~group)
coef = 2
# Run BPglm for differential expression analysis
library(doParallel)
registerDoParallel(cores=16)
res = BPglm(data = as.matrix(PSI_tu), controlIds = controlIds, design = design, coef = coef, estIntPar = TRUE, useParallel = TRUE)
hist(res$PVAL, breaks = 20)
# Summarize the resutls
ss = summary(res)
fdr = p.adjust(res$PVAL, method = "BH")
bpglm.DE.ids = row.names(PSI_tu)[which(fdr <= 0.01)]
length(bpglm.DE.ids)# Print the indices of the true DE genes:
# [1] 1313
bpglm.DE.ids
pdf("/mnt/data5/BGI/UCB/tangchao/DSU2/SE/Cell_Sep/Cell_specific_SE_heatmap_BPSC_CD8.Vs.BCel.pdf")
pheatmap(PSI_tu[bpglm.DE.ids,], cluster_cols = F, cluster_rows = T, show_colnames = F, show_rownames = F)
dev.off()
PSI_tu <- PSI[rs>=1, ]
PSI_tu[is.na(PSI_tu)] <- 0
PSI_tu <- PSI_tu[, row.names(Cell_type[Cell_type$CellType %in% c("CD4+ T Cells", "CD8+ T Cells"),])]
group = c(rep(1,sum(Cell_type$CellType == "CD4+ T Cells")),rep(2,sum(Cell_type$CellType == "CD8+ T Cells")))
controlIds = colnames(PSI_tu)[which(group == 1)]
design = model.matrix(~group)
coef = 2
# Run BPglm for differential expression analysis
library(doParallel)
registerDoParallel(cores=16)
res = BPglm(data = as.matrix(PSI_tu), controlIds = controlIds, design = design, coef = coef, estIntPar = TRUE, useParallel = TRUE)
hist(res$PVAL, breaks = 20)
# Summarize the resutls
ss = summary(res)
fdr = p.adjust(res$PVAL, method = "BH")
bpglm.DE.ids = row.names(PSI_tu)[which(fdr <= 0.01)]
length(bpglm.DE.ids)# Print the indices of the true DE genes:
# [1] 414
bpglm.DE.ids
PSI_tu[bpglm.DE.ids,] -> plot_psi
Cell_type[colnames(plot_psi),] -> Cell_type_plot
Cell_type_plot$CellType <- as.factor(as.character(Cell_type_plot$CellType))
pdf("/mnt/data5/BGI/UCB/tangchao/DSU2/SE/Cell_Sep/Cell_specific_SE_heatmap_BPSC_CD8.Vs.CD4.pdf")
pheatmap(plot_psi, cluster_cols = F, cluster_rows = T, show_colnames = F, show_rownames = F, annotation_col = Cell_type_plot)
dev.off()