-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathT_cell_Cb.R
350 lines (280 loc) · 11.1 KB
/
T_cell_Cb.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#### Magdalena Matusiak
#### 20 JAN 2018
#### analysis of immune landscape changes in breast cancer progression
#### LCM bulk RNASeq deconvoluted with CibersortX
#### 6 batches and Megan's data batch corrected
#Tcells
mgsub <- function(pattern, replacement, x, ...) {
if (length(pattern)!=length(replacement)) {
stop("pattern and replacement do not have the same length.")
}
result <- x
for (i in 1:length(pattern)) {
result <- gsub(pattern[i], replacement[i], result, ...)
}
result
}
path = "~/Desktop/s7s_Breast/data_after_decon/9_megaclasses/with_Megan_data/batch_corr/Filtered/"
setwd(path)
files <- list.files(path=path)
files
#extract names
namesGEP = as.character()
for (i in files){
nam = mgsub(c("CIBERSORTxGEP_bulk_corr_n_",".txt_GEPs_Filtered.txt"),c("",""),i)
namesGEP = c(namesGEP, nam)
}
namesGEP
#put all files in the list
filteredGEP = list()
for (i in 1: length(files)){
r = read.table(files[i], sep = "\t", header = TRUE)
filteredGEP[[i]] = r
}
names(filteredGEP) = namesGEP
gene_count = data.frame()
for(i in namesGEP){
gene_count = rbind(gene_count, apply(filteredGEP[[i]], 2, function(x){sum(!is.na(x))}))
}
rownames(gene_count) = namesGEP
colnames(gene_count) =colnames(filteredGEP[[1]])
gene_count
lapply(filteredGEP, dim) ##dim differ between diferent stages (runs of CibersortX)
#extract all gene names from the files and put them in the list to find intersect
#of genes acros all the histological subtypes
results = list()
for (i in namesGEP){
results[[i]] = as.character(filteredGEP[[i]][,1])
}
universe = Reduce(intersect, results)
#extract genes in T_cell signature common for all samples
Tcell_ex = data.frame(matrix(NA, nrow = length(universe), ncol = 1))
for (i in namesGEP){
Tcell = filteredGEP[[i]][filteredGEP[[i]][,1] %in% universe,c("GeneSymbol","T.cells")] #select gene_names and T.cells
Tcell_ex = cbind(Tcell_ex,Tcell)
}
all(as.character(Tcell_ex[,2])==as.character(Tcell_ex[,4]))
rownames(Tcell_ex) = as.character(Tcell_ex[,2])
Tcell_ex = Tcell_ex[, seq(3, length(Tcell_ex), 2)]
colnames(Tcell_ex) = c("DCIS","EN","IDC","normal")
Tcell_ex[Tcell_ex<=1] <- NA
predicted_genes_Tcells = apply(Tcell_ex, 2, function(x){sum(!is.na(x))})
##############
#StErrors
#############
path = "~/Desktop/s7s_Breast/data_after_decon/9_megaclasses/with_Megan_data/batch_corr/StEr/"
setwd(path)
files <- list.files(path=path)
files
#extract names
namesStEr = as.character()
for (i in files){
nam = mgsub(c("CIBERSORTxGEP_bulk_corr_n_",".txt_GEPs_StdErrs.txt"),c("",""),i)
namesStEr = c(namesStEr, nam)
}
namesStEr
#put all files in the list
StEr = list()
for (i in 1: length(files)){
r = read.table(files[i], sep = "\t", header = TRUE)
StEr[[i]] = r
}
names(StEr) = namesStEr
#extract genes in Tcell signature common for all samples
Tcell_ster = data.frame(matrix(NA, nrow = length(universe), ncol = 1))
for (i in namesStEr){
Error = StEr[[i]][StEr[[i]][,1] %in% universe,c("GeneSymbol","T.cells")] #select gene_names and Tcells
Tcell_ster = cbind(Tcell_ster,Error)
}
all(as.character(Tcell_ster[,2])==as.character(Tcell_ster[,4]))
rownames(Tcell_ster) = as.character(Tcell_ster[,2])
Tcell_ster = Tcell_ster[, seq(3, length(Tcell_ster), 2)]
colnames(Tcell_ster) = c("DCIS","EN","IDC","normal")
all(rownames(Tcell_ex) == rownames(Tcell_ster))
# Mono_ster$gene = sub("HLA.","HLA-", Mono_ster$gene)
###########DE Chloe
Tcell_ex = Tcell_ex[,c(4,2,1,3)]
Tcell_ster = Tcell_ster[,c(4,2,1,3)]
Geps = Tcell_ex
StdErr = Tcell_ster
Zqvals = matrix(NA,nrow(Geps),1)
cnames = c()
for(i in names(Geps)){
for(j in names(Geps)){
if(i <= j){
next
}
cnames= c(cnames, paste0(i,"_",j))
vBetaZ <- sapply(1:nrow(Geps), function(x) (Geps[x,i]-Geps[x,j])/sqrt(StdErr[x,i]^2+StdErr[x,j]^2))
ZPs <- 2*pnorm(-abs(vBetaZ))
Zqvals <- cbind(Zqvals,p.adjust(ZPs, method="BH"))
}
}
Zqvals = Zqvals[,-1]
colnames(Zqvals) = cnames
rownames(Zqvals) = rownames(Geps)
dim(Zqvals)
head(Zqvals)
Zqvals = data.frame(Zqvals)
names(Zqvals)[5:6] = c("EN_IDC", "DCIS_IDC")
sPv = list()
for(i in colnames(Zqvals)){
sPv[[i]] = data.frame(genes = rownames(Zqvals)[which(Zqvals[,i]<= 0.1)], Zqv = Zqvals[which(Zqvals[,i]<= 0.1),i, drop = FALSE])
}
LFC = list()
for(i in names(Zqvals)){
fr = strsplit(i, "[_]")[[1]][1]
sd = strsplit(i, "[_]")[[1]][2]
LFC[[i]] = data.frame(names = rownames(Geps), FC = log2(Geps[,sd]/Geps[,fr]))
}
#extract only sig FC
LFC_s = list()
for(i in names(LFC)){
LFC_s[[i]] = LFC[[i]][LFC[[i]][,"names"] %in% sPv[[i]][,"genes"],]
}
for(i in names(LFC_s)){
print(all(as.character(LFC_s[[i]][,"names"]) == as.character(sPv[[i]][,"genes"])))
}
#merge LFC and p.adj for downstream GO/GSEA analysis
to_enr = list()
for(i in names(LFC_s)){
to_enr[[i]] = cbind(LFC_s[[i]], Zqv = sPv[[i]][,i])
}
##############
#plot interesting genes
##############
ImGenes = list()
ImGenes[["IL"]] = c("FOXP3","CXCL2","IL18R1","IL25","IL24","CD207","CD70","CXCL6","CD25")
ImGenes[["IFN"]] = c("IFNGR2","LYZ","NFKBIA")
genes = c("IL","IFN")
library(reshape2)
for(i in genes){
Immuno = Tcell_ex[rownames(Tcell_ex) %in% ImGenes[[i]],]
Immuno = data.frame(gene.names = rownames(Immuno), Immuno)
#Immuno$gene.names = as.factor(as.character(Immuno$gene.names))
print(Immuno)
long_Imm = melt(Immuno, id.vars = "gene.names")
colnames(long_Imm) = c("gene.names","stage","gene.expression")
er = Tcell_ster[rownames(Tcell_ster) %in% Immuno$gene.names,]
er = data.frame(gene = rownames(er), er)
#er$gene = as.factor(as.character(er$gene))
er_long = melt(er, id.vars = "gene")
long_Imm = data.frame(long_Imm, sd = er_long$value)
long_Imm$sd[is.na(long_Imm$gene.expression)] = NA
print(ggplot(long_Imm,aes(gene.names,gene.expression, group = stage, fill =stage)) +
geom_col(position = "dodge")+ theme_classic()+
theme(text = element_text(size=20))+
geom_errorbar(aes(ymin=gene.expression-sd, ymax=gene.expression+sd), width=.2, position=position_dodge(.9)) +
scale_fill_manual(values=c("dodgerblue3","orange","chartreuse4","red")) +
theme(axis.text.x = element_text(angle = 60, hjust = 1))+
ggtitle("batch_corrected"))
}
####BC GO enrichment
to_GO = list()
for(nm in names(to_enr)){
to_GO[[paste0("DE_",nm,"_up")]] = to_enr[[nm]][which(to_enr[[nm]][,"FC"] > 0), "names"]
to_GO[[paste0("DE_",nm,"_down")]] = to_enr[[nm]][which(to_enr[[nm]][,"FC"] < 0), "names"]
}
lengths(to_GO)
library("Rgraphviz")
library("DOSE")
library("clusterProfiler")
library(org.Hs.eg.db)
keytypes(org.Hs.eg.db)
#translate gene symbols to entrez ID
for(element in names(to_GO)){
to_GO[[element]] = sub("[.]","-", to_GO[[element]])
}
to_GO_EI = list()
for(element in names(to_GO)){
to_GO_EI[[element]] = bitr(to_GO[[element]],
fromType = "SYMBOL",
toType = "ENTREZID",
OrgDb = org.Hs.eg.db)
}
###run GO analysis, I am running ont= BP (biological processes) /MF/CC
GO_res = list()
for(element in names(to_GO_EI)){
GO_res[[element]] = enrichGO(gene = to_GO_EI[[element]]$ENTREZID,
OrgDb = org.Hs.eg.db,
ont = "BP",
pAdjustMethod = "BH",
pvalueCutoff = 0.01,
qvalueCutoff = 0.05,
readable = TRUE)
}
#path = setwd("~/Desktop/breast_cancer_progression/plots/BC_30_10_17/enrichment_analysis")
for(element in names(GO_res)){
#png(paste0(path,"/",element,".png"), width = 900, height = 1100)
plot(dotplot(GO_res[[element]],showCategory=50)+labs(title= paste0("Tcell_",element)))
#dev.off()
}
res = list()
for(element in names(GO_res)){
if(nrow(GO_res[[element]]@result) == 0){
next
}
res[[element]] = GO_res[[element]]@result[,c("ID","p.adjust", "Description","geneID","GeneRatio")]
}
#subset ID and p.adj for revigo analysis
to_REV1 = GO_res$DE_DCIS_IDC_up@result[,c("ID","p.adjust")]
write.table(to_REV1, "~/Desktop/breast_cancer_progression/plots/BC_30_10_17/GO_res_DCIS_IDC_up", sep = "\t", row.names = F, quote = FALSE)
######disect genes for ploting
to_plot = list()
for(i in c("T |T-","MHC|antigen|present","neutroph","lip|LDL","horm","vir")){
print(i)
for(j in names(res)){
print(j)
print(res[[j]][grepl(i,res[[j]][,"Description"]),"Description"])
gr = res[[j]][grepl(i,res[[j]][,"Description"]),"geneID"]
gr = unique(unlist(strsplit(gr,"/")))
to_plot[[i]] = unique(c(to_plot[[i]],gr))
}
print(to_plot[[i]])
}
to_plot
library(heatmaply)
library(gplots)
library(RColorBrewer)
library(genefilter)
rownames(Geps) = sub("[.]","-", rownames(Geps))
genes = to_plot
names(genes) = c("T_cell_responce","Antigen_presentation","neutrophil_responce","Lipid_metabolism","Horm_responce","Viral_responce")
for(element in names(genes)){
to_heatmap = Geps[rownames(Geps) %in% genes[[element]],]
# to_heatmap1 = log2(to_heatmap+1)
# to_heatmap1 = to_heatmap1 - rowMeans(to_heatmap1, na.rm = T)
# minv = min(to_heatmap1, na.rm = T)
# maxv = max(to_heatmap1, na.rm = T)
# print(heatmaply(to_heatmap1, dendrogram = "row", main = element, fontsize_row = 15, fontsize_col = 15, margins = c(80,120,NA,NA),
# scale_fill_gradient_fun = ggplot2::scale_fill_gradient2(low = "blue", high = "red", na.value = "black" ,midpoint = minv + ((maxv-minv)/2),
# limits = c(minv, maxv))))
#
# print(heatmaply(to_heatmap1, dendrogram = "row", main = element, fontsize_row = 15, fontsize_col = 15, margins = c(80,120,NA,NA),
# scale_fill_gradient_fun = ggplot2::scale_fill_gradient(low = "cornflowerblue", high = "red",na.value = "black",midpoint = minv + ((maxv-minv)/2))))
to_heatmap = as.matrix(to_heatmap)
heatmap.2(to_heatmap, scale="row", na.color = "gray",
trace="none", Rowv = TRUE, Colv = NA, margins=c(6,8),
col = colorRampPalette( rev(brewer.pal(9, "RdBu")) )(255),
main= element)
to_barplot = data.frame(gene.names = rownames(to_heatmap), to_heatmap)
long_barplot = melt(to_barplot, id.vars = "gene.names")
colnames(long_barplot) = c("gene.names","stage","gene.count")
print(ggplot(long_barplot,aes(gene.names,gene.count, group = stage, fill =stage)) +
geom_col(position = "dodge")+ theme_classic()+ labs(y = "normalized expression")+
theme(text = element_text(size=20),axis.text.x = element_text(angle = 90, hjust = 1),plot.title = element_text(hjust = 0.5))+
scale_fill_manual(values=c("dodgerblue3","orange","chartreuse4","red"))+ggtitle(element))
}
####KEGG enrichment
KEGG_res = list()
for(element in names(to_GO_EI)){
KEGG_res[[element]] = enrichKEGG(gene = to_GO_EI[[element]]$ENTREZID,
organism = 'hsa',
pvalueCutoff = 0.05)
}
#path = setwd("~/Desktop/breast_cancer_progression/plots/BC_30_10_17/enrichment_analysis")
for(element in names(KEGG_res)){
#png(paste0(path,"/","KEGG_MO_",element,".png"), width = 900, height = 1100)
plot(dotplot(KEGG_res[[element]],showCategory=50)+labs(title= paste0("KEGG_Macrophage_",element)))
#dev.off()
}