This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInducible_genes_motif_cooccurence_version1.0.R
220 lines (205 loc) · 7.75 KB
/
Inducible_genes_motif_cooccurence_version1.0.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
# Co-occurence of motifs in EP pairs
setwd("~/Documents/IFN_enhancer/R/")
ee <- read.table("Data/Inducible_eRNA.motif.positions", colClasses = "character", header = T, sep = "\t")
pp <- read.table("Data/Inducible_genes.motif.positions", colClasses = "character", header = T, sep = "\t")
####################################################################
get_expressed_tf <- function() {
gene.exp <- read.csv("Data/Expression_matrix.symbol.csv", sep = "\t")
tf_list <- unique(c(ee$Motif.Name, pp$Motif.Name))
active_tf <- NULL
for (i in 1:length(tf_list)) {
tp <- strsplit(tf_list[i], "_HUMAN")[[1]][1]
if (tp %in% active_tf) {
next
} # Avoid duplicates
if (tp %in% rownames(gene.exp)) {
if (max(gene.exp[tp, ]) > 1) {
active_tf <- c(active_tf, tf_list[i])
}
}
}
return(active_tf)
}
short_id <- function(xx) {
short <- xx
for (i in 1:length(xx)) {
short[i] <- strsplit(xx[i], "_HUMAN")[[1]][1]
}
return(short)
}
occurence_table <- function(xx, motif_list) {
region_list <- unique(xx$PositionID)
zeros <- rep(0, length(region_list))
names(zeros) <- region_list
output <- NULL
for (i in 1:length(motif_list)) {
temp <- zeros
tp <- xx[xx$Motif.Name == motif_list[i], 1]
tp <- unique(tp)
temp[tp] <- 1
output <- c(output, temp)
}
output <- matrix(output, nrow = length(motif_list), byrow = T, dimnames = list(motif_list, region_list))
return(output)
}
motif_dist <- function(xx) {
motif_list <- rownames(xx)
output <- matrix(rep(0, length(motif_list)^2), nrow = length(motif_list), dimnames = list(motif_list, motif_list))
for (i in 1:(length(motif_list) - 1)) {
for (j in (i + 1):length(motif_list)) {
output[i, j] <- sum((xx[i, ] - xx[j, ])^2) / ncol(xx)
output[j, i] <- output[i, j]
}
}
return(output)
}
motif_cooccur <- function(xx) {
motif_list <- rownames(xx)
output <- matrix(rep(1, length(motif_list)^2), nrow = length(motif_list), dimnames = list(motif_list, motif_list))
for (i in 1:(length(motif_list) - 1)) {
for (j in (i + 1):length(motif_list)) {
output[i, j] <- sum((xx[i, ] + xx[j, ]) == 2) / ncol(xx)
output[j, i] <- output[i, j]
}
}
return(output)
}
ep_cooccur <- function(ee = ee, pp = pp, ep = ep, tf1, tf2) {
ne <- NULL
np <- NULL
for (i in 1:nrow(ep)) {
ne <- c(ne, sum((ee$PositionID == ep$id_b[i]) &
(ee$Motif.Name == tf1)) > 0)
np <- c(np, sum((pp$PositionID == ep$id_a[i]) &
(pp$Motif.Name == tf2)) > 0)
}
ne <- as.integer(ne)
np <- as.integer(np)
return(cbind(ne, np))
}
####################################################################
motif_list <- get_expressed_tf()
ee <- ee[ee$Motif.Name %in% motif_list, ]
pp <- pp[pp$Motif.Name %in% motif_list, ]
ep <- read.csv("Data/Induced_EP.csv", colClasses = "character")
##################################################
# Co-occurnece of motifs in EP pairs
##################################################
motif_sel <- NULL # Only use motifs with enough occurence
short_id <- NULL
for (i in 1:length(motif_list)) {
tp <- ep_cooccur(ee, pp, ep, motif_list[i], motif_list[i])
if (colSums(tp)[1] > 20 & colSums(tp)[2] > 20) {
motif_sel <- c(motif_sel, motif_list[i])
short_id <- c(short_id, strsplit(motif_list[i], "_HUMAN")[[1]][1])
}
}
ee_sel <- ee[ee$Motif.Name %in% motif_sel, ]
pp_sel <- pp[pp$Motif.Name %in% motif_sel, ]
n <- length(motif_sel)
cooccur_table <- matrix(ncol = n, nrow = n, dimnames = list(short_id, short_id))
pv_pos <- matrix(ncol = n, nrow = n, dimnames = list(short_id, short_id))
pv_neg <- matrix(ncol = n, nrow = n, dimnames = list(short_id, short_id))
for (i in 1:n) {
for (j in 1:n) {
tp <- ep_cooccur(ee_sel, pp_sel, ep, motif_sel[i], motif_sel[j])
cooccur_table[i, j] <- sum(rowSums(tp) == 2) / nrow(tp)
ta <- factor(tp[, 1], levels = c(0, 1))
tb <- factor(tp[, 2], levels = c(0, 1))
pv_pos[i, j] <- (-1) * log10(fisher.test(ta, tb, alternative = "greater")$p.value) # TF1 and TF2 are possitively associated
pv_neg[i, j] <- (-1) * log10(fisher.test(ta, tb, alternative = "less")$p.value) # TF1 and TF2 are mutually exclusive
}
}
heatmap.2(cooccur_table, symm = F, symbreaks = F, trace = "none")
heatmap.2(pv_pos, symm = F, symbreaks = F, trace = "none")
# heatmap.2(pv_neg, symm=F, symbreaks=F, trace="none")
pv_org <- 10^(-pv_pos)
pv <- sort(as.vector(pv_org))
pv_adj <- p.adjust(pv, method = "BH")
cutoff <- pv[sum(pv_adj < 0.05)] # Adjust P values by BH procedure and find significance cutoff
out_table <- NULL
for (i in 1:n) {
for (j in 1:n) {
if (pv_org[i, j] < cutoff) {
out_table <- c(out_table, short_id[i], short_id[j]) # Fields: Enhancer_TF, Promoter_TF
tp <- ep_cooccur(ee_sel, pp_sel, ep, motif_sel[i], motif_sel[j])
out_table <- c(out_table, colSums(tp), sum(rowSums(tp) == 2)) # Fields: Enhancer_occurrence, Promoter_occurrence, Co_occurrence
out_table <- c(
out_table, 10^(-pv_pos[i, j]), # Fields: P_val
pv_adj[which(pv == pv_org[i, j])[1]]
) # Fields: P_val_adj
}
}
}
out_table <- matrix(out_table, ncol = 7, byrow = T)
colnames(out_table) <- c("Enhancer_TF", "Promoter_TF", "Enhancer_occurrence", "Promoter_occurrence", "Co_occurrence", "P-val", "P-val.adj")
out_table <- as.data.frame(out_table)
out_table <- out_table[order(out_table$"P-val.adj"), ]
rownames(out_table) <- c(1:nrow(out_table))
# Control experiment: TF pairs that are exclusive
pv_org <- 10^(-pv_neg)
pv <- sort(as.vector(pv_org))
pv_adj <- p.adjust(pv, method = "BH")
cutoff <- pv[sum(pv_adj < 0.05)] # Adjust P values by BH procedure and find significance cutoff
out_table <- NULL
for (i in 1:n) {
for (j in 1:n) {
if (pv_org[i, j] < cutoff) {
out_table <- c(out_table, short_id[i], short_id[j]) # Fields: Enhancer_TF, Promoter_TF
tp <- ep_cooccur(ee_sel, pp_sel, ep, motif_sel[i], motif_sel[j])
out_table <- c(out_table, colSums(tp), sum(rowSums(tp) == 2)) # Fields: Enhancer_occurrence, Promoter_occurrence, Co_occurrence
out_table <- c(
out_table, 10^(-pv_pos[i, j]), # Fields: P_val
pv_adj[which(pv == pv_org[i, j])[1]]
) # Fields: P_val_adj
}
}
}
##################################################
# Occurnece table of enhancers
##################################################
ee_motif <- occurence_table(ee, motif_list)
# Motif similarity measured by Euclidian distance
ee_motif_dist <- motif_dist(ee_motif)
print(summary(ee_motif_dist[upper.tri(ee_motif_dist)]))
for (i in 1:(length(motif_list) - 1)) {
for (j in (i + 1):length(motif_list)) {
if (ee_motif_dist[i, j] < 0.1) {
print(motif_list[c(i, j)])
}
}
}
# Motif co-occurence ratio
ee_motif_cooccur <- motif_cooccur(ee_motif)
print(summary(ee_motif_cooccur[upper.tri(ee_motif_cooccur)]))
for (i in 1:(length(motif_list) - 1)) {
for (j in (i + 1):length(motif_list)) {
if (ee_motif_cooccur[i, j] > 0.5) {
print(c(motif_list[c(i, j)], ee_motif_dist[i, j], ee_motif_cooccur[i, j]))
}
}
}
##################################################
# Occurnece table of promoters
##################################################
pp_motif <- occurence_table(pp, motif_list)
# Motif similarity measured by Euclidian distance
pp_motif_dist <- motif_dist(pp_motif)
print(summary(pp_motif_dist[upper.tri(pp_motif_dist)]))
for (i in 1:(length(motif_list) - 1)) {
for (j in (i + 1):length(motif_list)) {
if (pp_motif_dist[i, j] < 0.1) {
print(c(motif_list[c(i, j)], pp_motif_dist[i, j], pp_motif_cooccur[i, j]))
}
}
}
# Motif co-occurence ratio
pp_motif_cooccur <- motif_cooccur(pp_motif)
print(summary(pp_motif_cooccur[upper.tri(pp_motif_cooccur)]))
for (i in 1:(length(motif_list) - 1)) {
for (j in (i + 1):length(motif_list)) {
if (pp_motif_cooccur[i, j] > 0.5) {
print(c(motif_list[c(i, j)], pp_motif_dist[i, j], pp_motif_cooccur[i, j]))
}
}
}