-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSection6_keyTF_Step1_increase_decrease.R
More file actions
221 lines (160 loc) · 7.7 KB
/
Section6_keyTF_Step1_increase_decrease.R
File metadata and controls
221 lines (160 loc) · 7.7 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
########################
#### Section6_keyTF ####
########################
#### Systematic nomination of key transcription factors for cell type specification
#################################
#### Step1_increase_decrease ####
#################################
### increase key TFs
rm(list=ls())
library(Seurat)
library(dplyr)
source("help_code/help_code.R")
path = ""
args = commandArgs(trailingOnly=TRUE)
time_i = as.numeric(args[1])
time_point = paste0("E", c(3.5, 4.5, 5.25, 5.5, 6.25, seq(6.5, 8.25, 0.25), "8.5a", "8.5b", seq(9.5, 13.5, 1)))
mouse_tf = read.table(paste0(path, "help_code/Mus_musculus_TF.txt"), header=T, as.is=TRUE, sep="\t")
mouse_tf <- as.vector(unique(mouse_tf$Ensembl))
edge = readRDS(paste0(path, "/edge_all.rds"))
edge = edge[edge$prob >= 0.2,]
edge = edge[!edge$nex %in% c("E5.25:Extraembryonic ectoderm","E7:Parietal endoderm"),]
###
getKeyTF <- function(time_i, t1 = 0.7, t2 = 0.3, t3 = 0.3){
obj_pre = readRDS(paste0(path, "/seurat_object_", time_point[time_i], ".rds"))
anno_pre = data.frame(obj_pre[[]])
anno_pre$Anno = as.vector(anno_pre$cell_state)
obj_pre$Anno = as.vector(anno_pre$Anno)
Idents(obj_pre) = as.vector(anno_pre$Anno)
exp_pre <- GetAssayData(object = obj_pre, slot = "counts")
exp_pre <- t(t(exp_pre) / colSums(exp_pre)) * 100000
exp_pre@x <- log(exp_pre@x + 1)
obj_nex = readRDS(paste0(path, "/revision/exp/obj_", time_point[time_i+1], "_exp.rds"))
anno_nex = data.frame(obj_nex[[]])
anno_nex$Anno = as.vector(anno_nex$cell_state)
obj_nex$Anno = as.vector(anno_nex$Anno)
Idents(obj_nex) = as.vector(anno_nex$Anno)
obj_nex <- NormalizeData(obj_nex, normalization.method = "LogNormalize", scale.factor = 10000)
obj_conn <- merge(obj_pre, obj_nex)
obj_conn <- NormalizeData(obj_conn, normalization.method = "LogNormalize", scale.factor = 10000)
Idents(obj_conn) <- as.vector(obj_conn$Anno)
result <- NULL
pre_state <- as.vector(unique(edge[edge$pre_time == time_point[time_i],]$pre))
for (i in 1:length(pre_state)){
print(pre_state[i])
edge_sub <- edge[edge$pre == pre_state[i],]
if(nrow(edge_sub)!=1 || edge_sub$pre_cell!=edge_sub$nex_cell){
exp_pre_sub <- exp_pre[,obj_pre$Anno == pre_state[i]]
exp_pre_sub_mean <- rowMeans(exp_pre_sub)
DEG_1 = rownames(exp_pre)
gene_use = intersect(mouse_tf, DEG_1)
for(j in 1:nrow(edge_sub)){
if (edge_sub$pre_cell[j]!=edge_sub$nex_cell[j]){
print(paste0("running nex state:",j,"/",nrow(edge_sub)))
### requirement 2: TF shoule be increasing at child state
gene_use_sub = intersect(gene_use, rownames(obj_conn))
DEG_2 <- FindMarkers(obj_conn, ident.1 = edge_sub$nex[j], ident.2 = edge_sub$pre[j], features = gene_use_sub, min.pct = 0, logfc.threshold = t2, only.pos = TRUE)
DEG_2 <- DEG_2[DEG_2$p_val_adj<0.05 & DEG_2$pct.1 > 0.1,]
if(nrow(DEG_2)!=0){
### requirement 3: TF should be decreasing at sister state
if(nrow(edge_sub)!=1){
gene_use_sub = intersect(gene_use, rownames(obj_nex))
DEG_3 <- FindMarkers(obj_nex, ident.1 = edge_sub$nex[j], ident.2 = c(as.vector(edge_sub$nex[-j])), features = gene_use_sub, min.pct = 0, logfc.threshold = t3, only.pos = TRUE)
DEG_3 <- DEG_3[DEG_3$p_val_adj<0.05 & DEG_3$pct.1 > 0.1,]
gene_intersect = intersect(rownames(DEG_2), rownames(DEG_3))
DEG_2 = DEG_2[gene_intersect,]
DEG_3 = DEG_3[gene_intersect,]
} else {
DEG_3 = DEG_2
DEG_3[,] = NA
}
DEG = cbind(DEG_2, DEG_3)
} else {
DEG = DEG_2
}
if(nrow(DEG)>0){
DEG$edge = paste0(edge_sub$nex[j], "_", edge_sub$pre[j])
DEG$state = edge_sub$nex[j]
DEG$gene_short_name = mouse_gene[rownames(DEG),]$gene_short_name
DEG$gene_ID = rownames(DEG)
rownames(DEG) = NULL
result = rbind(result, DEG)
}
}
}
}
}
return(result)
}
res <- getKeyTF(time_i, t1 = 0, t2 = 0, t3 = 0)
saveRDS(res, paste0(path, '/increase/', time_i, '_tf.rds'))
#### decrease
getKeyTF <- function(time_i, t1 = 0.7, t2 = 0.3, t3 = 0.3){
obj_pre = readRDS(paste0(path, "/revision/exp/obj_", time_point[time_i], "_exp.rds"))
anno_pre = data.frame(obj_pre[[]])
anno_pre$Anno = as.vector(anno_pre$cell_state)
obj_pre$Anno = as.vector(anno_pre$Anno)
Idents(obj_pre) = as.vector(anno_pre$Anno)
exp_pre <- GetAssayData(object = obj_pre, slot = "counts")
exp_pre <- t(t(exp_pre) / colSums(exp_pre)) * 100000
exp_pre@x <- log(exp_pre@x + 1)
obj_nex = readRDS(paste0(path, "/revision/exp/obj_", time_point[time_i+1], "_exp.rds"))
anno_nex = data.frame(obj_nex[[]])
anno_nex$Anno = as.vector(anno_nex$cell_state)
obj_nex$Anno = as.vector(anno_nex$Anno)
Idents(obj_nex) = as.vector(anno_nex$Anno)
obj_nex <- NormalizeData(obj_nex, normalization.method = "LogNormalize", scale.factor = 10000)
obj_conn <- merge(obj_pre, obj_nex)
obj_conn <- NormalizeData(obj_conn, normalization.method = "LogNormalize", scale.factor = 10000)
Idents(obj_conn) <- as.vector(obj_conn$Anno)
result <- NULL
pre_state <- as.vector(unique(edge[edge$pre_time == time_point[time_i],]$pre))
for (i in 1:length(pre_state)){
print(pre_state[i])
edge_sub <- edge[edge$pre == pre_state[i],]
if(nrow(edge_sub)!=1 || edge_sub$pre_cell!=edge_sub$nex_cell){
exp_pre_sub <- exp_pre[,obj_pre$Anno == pre_state[i]]
exp_pre_sub_mean <- rowMeans(exp_pre_sub)
DEG_1 = rownames(exp_pre)
gene_use = intersect(mouse_tf, DEG_1)
for(j in 1:nrow(edge_sub)){
if (edge_sub$pre_cell[j]!=edge_sub$nex_cell[j]){
print(paste0("running nex state:",j,"/",nrow(edge_sub)))
### requirement 2: TF shoule be decreasing at child state
gene_use_sub = intersect(gene_use, rownames(obj_conn))
DEG_2 <- FindMarkers(obj_conn, ident.1 = edge_sub$pre[j], ident.2 = edge_sub$nex[j], features = gene_use_sub, min.pct = 0, logfc.threshold = t2, only.pos = TRUE)
DEG_2 <- DEG_2[DEG_2$p_val_adj<0.05 & DEG_2$pct.1 > 0.1,]
if(nrow(DEG_2)!=0){
### requirement 3: TF should be decreasing at sister state
if(nrow(edge_sub)!=1){
gene_use_sub = intersect(gene_use, rownames(obj_nex))
DEG_3 <- FindMarkers(obj_nex, ident.1 = c(as.vector(edge_sub$nex[-j])), ident.2 = edge_sub$nex[j], features = gene_use_sub, min.pct = 0, logfc.threshold = t3, only.pos = TRUE)
DEG_3 <- DEG_3[DEG_3$p_val_adj<0.05 & DEG_3$pct.1 > 0.1,]
gene_intersect = intersect(rownames(DEG_2), rownames(DEG_3))
DEG_2 = DEG_2[gene_intersect,]
DEG_3 = DEG_3[gene_intersect,]
} else {
DEG_3 = DEG_2
DEG_3[,] = NA
}
DEG = cbind(DEG_2, DEG_3)
} else {
DEG = DEG_2
}
if(nrow(DEG)>0){
DEG$edge = paste0(edge_sub$nex[j], "_", edge_sub$pre[j])
DEG$state = edge_sub$nex[j]
DEG$gene_short_name = mouse_gene[rownames(DEG),]$gene_short_name
DEG$gene_ID = rownames(DEG)
rownames(DEG) = NULL
result = rbind(result, DEG)
}
}
}
}
}
return(result)
}
###
res <- getKeyTF(time_i, t1 = 0, t2 = 0, t3 = 0)
saveRDS(res, paste0(path, '/decrease/', time_i, '_tf.rds'))