-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeak_gene_correlation.R
347 lines (333 loc) · 12.9 KB
/
peak_gene_correlation.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
#to get average expression/peak level by cell type
PeakByCellType <- sapply(split(colnames(peak_matrix_sr@assays$RNA@data), peak_matrix_sr$major_type), #colnames(peak_matrix_sr@assays$RNA@data): expression matrix, colnames; peak_matrix_sr$major_type: meta.data
function(cells) rowMeans(peak_matrix_sr@assays$RNA@data[,cells])) #peak_matrix_sr@assays$RNA@data: seurat matrix;
#write it to csv
#gene_table is a data frame with each row as a gene and each column as a cell type (averaged from single cell by cell type annotation)
gene_table<-read.csv("AverageExpressionBySubtype.csv", row.names = 1)
df_gene<-data.frame(gene = rownames(gene_table), proportion = rowSums(gene_table != 0)/ncol(gene_table))#remove super low genes
df_gene$gene<-as.character(df_gene$gene)
rownames(df_gene)<-df_gene$gene
df_gene<-df_gene[df_gene$proportion >0.8, ]
gene_table<-gene_table[df_gene$gene, ]
#same thing, each row is a peak and each column is a cell type
peak_table<-read.csv("AveragePeakBySubtype.csv", row.names = 1)
common_col<-intersect(colnames(gene_table), colnames(peak_table))
gene_table<-gene_table[, common_col]
peak_table<-peak_table[, common_col]
df_peak<-data.frame(peak = rownames(peak_table), proportion = rowSums(peak_table != 0)/ncol(peak_table))
df_peak$peak<-as.character(df_peak$peak)
rownames(df_peak)<-df_peak$peak
df_peak<-df_peak[df_peak$proportion >0.8, ]
peak_table<-peak_table[df_peak$peak, ]
peak_table$chr<-gsub("\\:.*","",rownames(peak_table))
peak_table$start<-gsub(".*:", "", gsub("\\-.*","", rownames(peak_table)))
peak_table$end<-gsub(".*-", "",rownames(peak_table))
peak_table$start<-as.numeric(peak_table$start)
peak_table$end<-as.numeric(peak_table$end)
library(TxDb.Hsapiens.UCSC.hg19.knownGene)
txdb <- TxDb.Hsapiens.UCSC.hg19.knownGene
library(org.Hs.eg.db)
library(clusterProfiler)
gene_info<-genes(txdb)
gene_info<-as.data.frame(gene_info)
gene_info_conv<-bitr(rownames(gene_info), fromType="ENTREZID", toType=c("SYMBOL"), OrgDb="org.Hs.eg.db")
#substitute with gene names
gene_info<-gene_info[gene_info_conv$ENTREZID, ]
rownames(gene_info)<-gene_info_conv$SYMBOL
gene_info$gene_name<-rownames(gene_info)
gene_info$tss<-ifelse(gene_info$strand == "+", gene_info$start, gene_info$end)
saveRDS(gene_info, "gene_info_tss.rds")#save
common_genes<-intersect(rownames(gene_table), rownames(gene_info))#13888
write.csv(gene_table, "gene_table.csv")
write.csv(peak_table, "peak_table.csv")
#run correlation: spearman
list<-list()
k<-1
for (i in 1:length(common_genes)){
tryCatch({
gene_name<-as.character(common_genes[i])
chr<-as.character(gene_info[gene_name, 1])
tss<-gene_info[gene_name, 8]
peak_subset<-peak_table[peak_table$chr == chr, ]
peak_subset<-peak_subset[abs((peak_subset$start + peak_subset$end)/2 - tss) < 250000, ]
peaks.id = rownames(peak_subset)
models = lapply(peaks.id, function(t){
result<-cor.test(as.numeric(peak_subset[t, 1:51]), method = "spearman", as.numeric(gene_table[gene_name, ])) #here, use 1:51 because we only compute the correlation for the first 51 column (52-54 will be chr, start, end)
data.frame(peak = t, gene = gene_name, result[c("estimate","p.value")], tss = tss,
stringsAsFactors=FALSE)
})
models <- do.call(rbind, models);
print(head(models))
list[[k]]<-models
print(i)
print(k)
k <- k+1
}, error = function(e){cat("ERROR :",conditionMessage(e), "\n")})
}
correlation_table<-do.call(rbind, list)
write.csv(correlation_table, "cor_table_sub_type_spearman.csv")
#do FDR
data<-read.csv("cor_table_sub_type_spearman.csv", row.names = 1)
head(data)
data<-data[complete.cases(data), ]
rownames(data)<-1:nrow(data)
data$chr<-gsub("\\:.*","",data$peak)
data$start<-gsub(".*:", "", gsub("\\-.*","", data$peak))
data$end<-gsub(".*-", "",data$peak)
data$start<-as.numeric(data$start)
data$end<-as.numeric(data$end)
data$distance<-abs((data$start + data$end)/2 - data$tss)
fdr<-lapply(1:nrow(data), function(t){
gene<-as.character(data[t, "gene"])
print(gene)
count<-length(which(data$gene == gene))
p.adjust(data[t, "p.value"], method = 'fdr', n = count)
})
fdr_df<-do.call(rbind, fdr)
data$fdr<-fdr_df[, 1]
write.csv(data, "cor_table_sub_type_spearman_fdr.csv")
#permutation#if you don't want to run the permutation, you can skip it
#shuffle ct
list<-list()
k<-1
for (i in 1:length(common_genes)){
tryCatch({
gene_name<-as.character(common_genes[i])
chr<-as.character(gene_info[gene_name, 1])
tss<-gene_info[gene_name, 8]
peak_subset<-peak_table[peak_table$chr == chr, ]
peak_subset<-peak_subset[abs((peak_subset$start + peak_subset$end)/2 - tss) < 250000, ]
peaks.id = rownames(peak_subset)
models = lapply(peaks.id, function(t){
result<-cor.test(as.numeric(peak_subset[t, sample(1:51)]), method = "spearman", as.numeric(gene_table[gene_name, sample(1:51)]))
data.frame(peak = t, gene = gene_name, result[c("estimate","p.value")], tss = tss,
stringsAsFactors=FALSE)
})
models <- do.call(rbind, models);
print(head(models))
list[[k]]<-models
print(i)
print(k)
k <- k+1
}, error = function(e){cat("ERROR :",conditionMessage(e), "\n")})
}
correlation_table_permute_gene<-do.call(rbind, list)
write.csv(correlation_table_permute_gene, "cor_table_sub_type_spearman_shuffle_ct.csv")
#
data<-read.csv("cor_table_sub_type_spearman_shuffle_ct.csv", row.names = 1)
head(data)
data<-data[complete.cases(data), ]
rownames(data)<-1:nrow(data)
data$chr<-gsub("\\:.*","",data$peak)
data$start<-gsub(".*:", "", gsub("\\-.*","", data$peak))
data$end<-gsub(".*-", "",data$peak)
data$start<-as.numeric(data$start)
data$end<-as.numeric(data$end)
data$distance<-abs((data$start + data$end)/2 - data$tss)
fdr<-lapply(1:nrow(data), function(t){
gene<-as.character(data[t, "gene"])
print(gene)
count<-length(which(data$gene == gene))
p.adjust(data[t, "p.value"], method = 'fdr', n = count)
})
fdr_df<-do.call(rbind, fdr)
data$fdr<-fdr_df[, 1]
write.csv(data, "cor_table_sub_type_spearman_shuffle_ct_fdr.csv")
#
#shuffle regions
#shuffle cell types
#permutation
list<-list()
k<-1
for (i in 1:length(common_genes)){
tryCatch({
gene_name<-as.character(common_genes[i])
chr<-as.character(gene_info[gene_name, 1])
tss<-gene_info[gene_name, 8]
peak_subset<-peak_table[peak_table$chr == chr, ]
peak_subset<-peak_subset[abs((peak_subset$start + peak_subset$end)/2 - tss) < 250000, ]
peak_subset<-peak_table[!(rownames(peak_table) %in% rownames(peak_subset)), ]
x <- sample(1:nrow(peak_subset), 50)
peak_subset<-peak_subset[x, ]
peaks.id = rownames(peak_subset)
models = lapply(peaks.id, function(t){
result<-cor.test(as.numeric(peak_subset[t, 1:51]), method = "spearman", as.numeric(gene_table[gene_name, 1:51]))
data.frame(peak = t, gene = gene_name, result[c("estimate","p.value")], tss = tss,
stringsAsFactors=FALSE)
})
models <- do.call(rbind, models);
print(head(models))
list[[k]]<-models
print(i)
print(k)
k <- k+1
}, error = function(e){cat("ERROR :",conditionMessage(e), "\n")})
}
correlation_table_permute_gene<-do.call(rbind, list)
write.csv(correlation_table_permute_gene, "cor_table_sub_type_spearman_shuffle_region_2.csv")
#
data<-read.csv("cor_table_sub_type_spearman_shuffle_region_2.csv", row.names = 1)
head(data)
data<-data[complete.cases(data), ]
rownames(data)<-1:nrow(data)
data$chr<-gsub("\\:.*","",data$peak)
data$start<-gsub(".*:", "", gsub("\\-.*","", data$peak))
data$end<-gsub(".*-", "",data$peak)
data$start<-as.numeric(data$start)
data$end<-as.numeric(data$end)
data$distance<-abs((data$start + data$end)/2 - data$tss)
fdr<-lapply(1:nrow(data), function(t){
gene<-as.character(data[t, "gene"])
print(gene)
count<-length(which(data$gene == gene))
p.adjust(data[t, "p.value"], method = 'fdr', n = count)
})
fdr_df<-do.call(rbind, fdr)
data$fdr<-fdr_df[, 1]
write.csv(data, "cor_table_sub_type_spearman_shuffle_region_fdr_2.csv")
##```````````````````````````````````````````````````````````````````````````````````````````````````````
#run correlation: pearson
list<-list()
k<-1
for (i in 1:length(common_genes)){
tryCatch({
gene_name<-as.character(common_genes[i])
chr<-as.character(gene_info[gene_name, 1])
tss<-gene_info[gene_name, 8]
peak_subset<-peak_table[peak_table$chr == chr, ]
peak_subset<-peak_subset[abs((peak_subset$start + peak_subset$end)/2 - tss) < 250000, ]
peaks.id = rownames(peak_subset)
models = lapply(peaks.id, function(t){
result<-cor.test(as.numeric(peak_subset[t, 1:51]), as.numeric(gene_table[gene_name, ]))
data.frame(peak = t, gene = gene_name, result[c("estimate","p.value")], tss = tss,
stringsAsFactors=FALSE)
})
models <- do.call(rbind, models);
print(head(models))
list[[k]]<-models
print(i)
print(k)
k <- k+1
}, error = function(e){cat("ERROR :",conditionMessage(e), "\n")})
}
correlation_table<-do.call(rbind, list)
write.csv(correlation_table, "cor_table_sub_type_pearson.csv")
#do FDR
data<-read.csv("cor_table_sub_type_pearson.csv", row.names = 1)
head(data)
data<-data[complete.cases(data), ]
rownames(data)<-1:nrow(data)
data$chr<-gsub("\\:.*","",data$peak)
data$start<-gsub(".*:", "", gsub("\\-.*","", data$peak))
data$end<-gsub(".*-", "",data$peak)
data$start<-as.numeric(data$start)
data$end<-as.numeric(data$end)
data$distance<-abs((data$start + data$end)/2 - data$tss)
fdr<-lapply(1:nrow(data), function(t){
gene<-as.character(data[t, "gene"])
print(gene)
count<-length(which(data$gene == gene))
p.adjust(data[t, "p.value"], method = 'fdr', n = count)
})
fdr_df<-do.call(rbind, fdr)
data$fdr<-fdr_df[, 1]
write.csv(data, "cor_table_sub_type_pearson_fdr.csv")
#permutation
#shuffle ct
list<-list()
k<-1
for (i in 1:length(common_genes)){
tryCatch({
gene_name<-as.character(common_genes[i])
chr<-as.character(gene_info[gene_name, 1])
tss<-gene_info[gene_name, 8]
peak_subset<-peak_table[peak_table$chr == chr, ]
peak_subset<-peak_subset[abs((peak_subset$start + peak_subset$end)/2 - tss) < 250000, ]
peaks.id = rownames(peak_subset)
models = lapply(peaks.id, function(t){
result<-cor.test(as.numeric(peak_subset[t, sample(1:51)]), as.numeric(gene_table[gene_name, sample(1:51)]))
data.frame(peak = t, gene = gene_name, result[c("estimate","p.value")], tss = tss,
stringsAsFactors=FALSE)
})
models <- do.call(rbind, models);
print(head(models))
list[[k]]<-models
print(i)
print(k)
k <- k+1
}, error = function(e){cat("ERROR :",conditionMessage(e), "\n")})
}
correlation_table_permute_gene<-do.call(rbind, list)
write.csv(correlation_table_permute_gene, "cor_table_sub_type_pearson_shuffle_ct.csv")
#
data<-read.csv("cor_table_sub_type_pearson_shuffle_ct.csv", row.names = 1)
head(data)
data<-data[complete.cases(data), ]
rownames(data)<-1:nrow(data)
data$chr<-gsub("\\:.*","",data$peak)
data$start<-gsub(".*:", "", gsub("\\-.*","", data$peak))
data$end<-gsub(".*-", "",data$peak)
data$start<-as.numeric(data$start)
data$end<-as.numeric(data$end)
data$distance<-abs((data$start + data$end)/2 - data$tss)
fdr<-lapply(1:nrow(data), function(t){
gene<-as.character(data[t, "gene"])
print(gene)
count<-length(which(data$gene == gene))
p.adjust(data[t, "p.value"], method = 'fdr', n = count)
})
fdr_df<-do.call(rbind, fdr)
data$fdr<-fdr_df[, 1]
write.csv(data, "cor_table_sub_type_pearson_shuffle_ct_fdr.csv")
#
#shuffle regions
#shuffle cell types
#permutation
list<-list()
k<-1
for (i in 1:length(common_genes)){
tryCatch({
gene_name<-as.character(common_genes[i])
chr<-as.character(gene_info[gene_name, 1])
tss<-gene_info[gene_name, 8]
peak_subset<-peak_table[peak_table$chr == chr, ]
peak_subset<-peak_subset[abs((peak_subset$start + peak_subset$end)/2 - tss) < 250000, ]
peak_subset<-peak_table[!(rownames(peak_table) %in% rownames(peak_subset)), ]
x <- sample(1:nrow(peak_subset), 50)
peak_subset<-peak_subset[x, ]
peaks.id = rownames(peak_subset)
models = lapply(peaks.id, function(t){
result<-cor.test(as.numeric(peak_subset[t, 1:51]), as.numeric(gene_table[gene_name, 1:51]))
data.frame(peak = t, gene = gene_name, result[c("estimate","p.value")], tss = tss,
stringsAsFactors=FALSE)
})
models <- do.call(rbind, models);
print(head(models))
list[[k]]<-models
print(i)
print(k)
k <- k+1
}, error = function(e){cat("ERROR :",conditionMessage(e), "\n")})
}
correlation_table_permute_gene<-do.call(rbind, list)
write.csv(correlation_table_permute_gene, "cor_table_sub_type_pearson_shuffle_region_2.csv")
#
data<-read.csv("cor_table_sub_type_pearson_shuffle_region_2.csv", row.names = 1)
head(data)
data<-data[complete.cases(data), ]
rownames(data)<-1:nrow(data)
data$chr<-gsub("\\:.*","",data$peak)
data$start<-gsub(".*:", "", gsub("\\-.*","", data$peak))
data$end<-gsub(".*-", "",data$peak)
data$start<-as.numeric(data$start)
data$end<-as.numeric(data$end)
data$distance<-abs((data$start + data$end)/2 - data$tss)
fdr<-lapply(1:nrow(data), function(t){
gene<-as.character(data[t, "gene"])
print(gene)
count<-length(which(data$gene == gene))
p.adjust(data[t, "p.value"], method = 'fdr', n = count)
})
fdr_df<-do.call(rbind, fdr)
data$fdr<-fdr_df[, 1]
write.csv(data, "cor_table_sub_type_pearson_shuffle_region_fdr_2.csv")