-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTEP2-3.R
339 lines (293 loc) · 16 KB
/
STEP2-3.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
#STEP2-3:TRAIN/TEST SPLIT AND PHENSIM INPUT GENERATION
library(TCGAbiolinks)
library(limma)
library(edgeR)
library(org.Hs.eg.db)
library(data.table)
library(plyr)
library(stringr)
drug_sim <- function(sample_info, m_expr, output, m_comb, strat){
input <- readRDS(sample_info)
input$PROJECT <- unlist(input$PROJECT)
input <- input[!duplicated(input[c(1,2,3,4)]),]
print("input file loaded!")
input$CONDITION <- ifelse(input$CONDITION == "NON_RESPONDER", "NR","R")
input$"TR/TE" <- NA
saveRDS(input,"./ALLsamples_info_TrainTest.rds")
project <- unique(input$PROJECT)
for (p in project) {
p_name <- paste0("TCGA-", p)
drug <- unique(input$drug_name[which(input$PROJECT == p)])
for (d in drug) {
if(!file.exists(paste0(strat,p_name,"_",d,"_genes_stratification.rds"))){
if(!file.exists(paste0(m_comb,p_name,"_",d,"_expr_matrix_comb_normalized.rds"))){
print(paste("Load expression matrixs for project", p_name, d))
ex_mat <- readRDS(paste0(m_expr, p_name, "expr_matrix_normal.rds"))
p_mat <- readRDS(paste0(m_expr, p_name,"_",d, "_expr_matrix_tumor.rds"))
comb <- TCGAanalyze_Normalization(tabDF = cbind(ex_mat,p_mat),
geneInfoHT,
method = "gcContent")
comb <- na.omit(comb)
saveRDS(comb, paste0(m_comb,p_name,"_",d,"_expr_matrix_comb_normalized.rds"))
n <- ncol(p_mat)
group_case <- rep("CASE",n)
m <- ncol(ex_mat)
group_ctrl <- rep("CONTROL", m)
group_total <- c(group_case, group_ctrl)
d0 <- DGEList(comb)
mm <- model.matrix(~0 + group_total)
keep <- filterByExpr(d0, design = mm)
d0 <- d0[keep,keep.lib.sizes=FALSE]
d0 <- calcNormFactors(d0)
y <- voom(d0, mm,plot = F)
barcode <-colnames(p_mat)
new_mat <- y$E
new_mat <- new_mat[ , !(colnames(new_mat) %in% barcode)]
new_p_mat <- y$E
new_p_mat <- new_p_mat[,(colnames(new_p_mat) %in% barcode)]
print("Compute 75th and 25th")
df_normal <- matrix(nrow = nrow(new_mat), ncol = (3 + length(barcode)))
row.names(df_normal) <- row.names(new_mat)
colnames(df_normal) <- c("25th", "50th", "75th", barcode)
for (n in row.names(df_normal)) {
df_normal[n,c(1:3)] <- quantile(new_mat[n,], c(0.25,0.5,0.75))
}
print("Identify upregulated and downregulated genes")
for (s in colnames(df_normal)[4:ncol(df_normal)]) {
for (n in row.names(new_p_mat)) {
if (new_p_mat[n,s] < df_normal[n,1]){
df_normal[n,s] <- "DOWN_REG"
}
else if (new_p_mat[n,s] > df_normal[n,3]){
df_normal[n,s] <- "UP_REG"
}
else { df_normal[n,s] <- "NO_CHANGE" }
}
}
colnames(df_normal)[4:ncol(df_normal)]<- substring(colnames(df_normal)[4:ncol(df_normal)],1,12)
for(c in 4:ncol(df_normal)){
if(input$CONDITION[which(input$SAMPLES == colnames(df_normal)[c] & input$drug_name == d)] == "R"){
colnames(df_normal)[c] <- paste0("RESPONDER-", colnames(df_normal)[c])
}
else if(input$CONDITION[which(input$SAMPLES == colnames(df_normal)[c]& input$drug_name == d)] == "NR") {
colnames(df_normal)[c] <- paste0("PROGRESSION-", colnames(df_normal)[c])
}
}
saveRDS(df_normal, paste0(strat, p_name,"_",d,"_genes_stratification.rds"))
df_normal <- as.data.frame(df_normal)
df_normal$chiTest_pv <- NA
print("Generate contingency matrixs")
for (g in row.names(df_normal)) {
if(length(which(df_normal[g,] == "NO_CHANGE")) == (ncol(df_normal) - 4)){
df_normal[g,"chiTest_pv"] <- "NO_CHANGE"
} else {
cont_m <- matrix(nrow = 2, ncol = 2)
row.names(cont_m) <- c("R", "NR")
colnames(cont_m) <- c("DOWN", "UP")
cont_m[1,1] <- length(which(colnames(df_normal) %like% "RESPONDER" & df_normal[g,] == "DOWN_REG" ))
cont_m[2,1] <- length(which(colnames(df_normal) %like% "PROGRESSION" & df_normal[g,] == "DOWN_REG" ))
cont_m[1,2] <- length(which(colnames(df_normal) %like% "RESPONDER" & df_normal[g,] == "UP_REG" ))
cont_m[2,2] <- length(which(colnames(df_normal) %like% "PROGRESSION" & df_normal[g,] == "UP_REG" ))
ctst <- chisq.test(cont_m)
pv <- ctst$p.value
df_normal[g,"chiTest_pv"] <- pv
}
}
saveRDS(df_normal, paste0(strat, p_name,"_",d,"_genes_stratification_pval.rds"))
}
else {
print(paste("Expression matrix combined and normalized exists for project", p_name, d))
comb <- readRDS(paste0(m_comb, p_name, "_", d,"_expr_matrix_comb_normalized.rds"))
ex_mat <- readRDS(paste0(m_expr, p_name, "expr_matrix_normal.rds"))
p_mat <- readRDS(paste0(m_expr, p_name,"_",d, "_expr_matrix_tumor.rds"))
n <- ncol(p_mat)
group_case <- rep("CASE",n)
m <- ncol(ex_mat)
group_ctrl <- rep("CONTROL", m)
group_total <- c(group_case, group_ctrl)
d0 <- DGEList(comb)
mm <- model.matrix(~0 + group_total)
keep <- filterByExpr(d0, design = mm)
d0 <- d0[keep,keep.lib.sizes=FALSE]
d0 <- calcNormFactors(d0)
y <- voom(d0, mm,plot = F)
barcode <-colnames(p_mat)
new_mat <- y$E
new_mat <- new_mat[ , !(colnames(new_mat) %in% barcode)]
new_p_mat <- y$E
new_p_mat <- new_p_mat[,(colnames(new_p_mat) %in% barcode)]
print("Compute 75th and 25th")
df_normal <- matrix(nrow = nrow(new_mat), ncol = (3 + length(barcode)))
row.names(df_normal) <- row.names(new_mat)
colnames(df_normal) <- c("25th", "50th", "75th", colnames(new_p_mat))
for (n in row.names(df_normal)) {
df_normal[n,c(1:3)] <- quantile(new_mat[n,], c(0.25,0.5,0.75))
}
print("Identify upregulated and downregulated genes")
for (s in colnames(df_normal)[4:ncol(df_normal)]) {
for (n in row.names(new_p_mat)) {
if (new_p_mat[n,s] < df_normal[n,1]){
df_normal[n,s] <- "DOWN_REG"
}
else if (new_p_mat[n,s] > df_normal[n,3]){
df_normal[n,s] <- "UP_REG"
}
else { df_normal[n,s] <- "NO_CHANGE" }
saveRDS(df_normal, paste0(strat, p_name,"_genes_stratification.rds"))
}
}
colnames(df_normal)[4:ncol(df_normal)]<- substring(colnames(df_normal)[4:ncol(df_normal)],1,12)
for(c in 4:ncol(df_normal)){
if(input$CONDITION[which(input$SAMPLES == colnames(df_normal)[c] & input$drug_name == d)] == "R"){
colnames(df_normal)[c] <- paste0("RESPONDER-", colnames(df_normal)[c])
}
else if(input$CONDITION[which(input$SAMPLES == colnames(df_normal)[c] & input$drug_name == d)] == "NR") {
colnames(df_normal)[c] <- paste0("PROGRESSION-", colnames(df_normal)[c])
}
}
saveRDS(df_normal, paste0(strat, p_name,"_",d,"_genes_stratification.rds"))
df_normal <- as.data.frame(df_normal)
df_normal$chiTest_pv <- NA
print("Generate contingency matrixs")
for (g in row.names(df_normal)) {
if(length(which(df_normal[g,] == "NO_CHANGE")) == (ncol(df_normal) - 4)){
df_normal[g,"chiTest_pv"] <- "NO_CHANGE"
} else {
cont_m <- matrix(nrow = 2, ncol = 2)
row.names(cont_m) <- c("R", "NR")
colnames(cont_m) <- c("DOWN", "UP")
cont_m[1,1] <- length(which(colnames(df_normal) %like% "RESPONDER" & df_normal[g,] == "DOWN_REG" ))
cont_m[2,1] <- length(which(colnames(df_normal) %like% "PROGRESSION" & df_normal[g,] == "DOWN_REG" ))
cont_m[1,2] <- length(which(colnames(df_normal) %like% "RESPONDER" & df_normal[g,] == "UP_REG" ))
cont_m[2,2] <- length(which(colnames(df_normal) %like% "PROGRESSION" & df_normal[g,] == "UP_REG" ))
ctst <- chisq.test(cont_m)
pv <- ctst$p.value
df_normal[g,"chiTest_pv"] <- pv
}
}
saveRDS(df_normal, paste0(strat, p_name,"_",d,"_genes_stratification_pval.rds"))
}
}
else {
print(paste("Expression matrix combined and normalized exists for project", p_name, d))
ex_mat <- readRDS(paste0(m_expr, p_name, "expr_matrix_normal.rds"))
p_mat <- readRDS(paste0(m_expr, p_name,"_",d, "_expr_matrix_tumor.rds"))
if(!file.exists(paste0(strat,p_name,"_",d,"_genes_stratification_pval.rds"))){
print(paste("Matrix with genes stratification exists for project", p_name, d))
df_normal <- readRDS(paste0(strat, p_name, "_", d,"_genes_stratification.rds"))
df_normal <- as.data.frame(df_normal)
df_normal$chiTest_pv <- NA
print(paste(" Generate contingengy matrixs"))
for (g in row.names(df_normal)) {
if(length(which(df_normal[g,] == "NO_CHANGE")) == (ncol(df_normal) - 4)){
df_normal[g,"chiTest_pv"] <- "NO_CHANGE"
} else {
cont_m <- matrix(nrow = 2, ncol = 2)
row.names(cont_m) <- c("R", "NR")
colnames(cont_m) <- c("DOWN", "UP")
cont_m[1,1] <- length(which(colnames(df_normal) %like% "RESPONDER" & df_normal[g,] == "DOWN_REG" ))
cont_m[2,1] <- length(which(colnames(df_normal) %like% "PROGRESSION" & df_normal[g,] == "DOWN_REG" ))
cont_m[1,2] <- length(which(colnames(df_normal) %like% "RESPONDER" & df_normal[g,] == "UP_REG" ))
cont_m[2,2] <- length(which(colnames(df_normal) %like% "PROGRESSION" & df_normal[g,] == "UP_REG" ))
ctst <- chisq.test(cont_m)
pv <- ctst$p.value
df_normal[g,"chiTest_pv"] <- pv
}
}
saveRDS(df_normal, paste0(strat, p_name,"_",d,"_genes_stratification_pval.rds"))
} else {
print(paste("Stratification gene matrix with pvalue exists for project", p_name, d))
df_normal <- readRDS(paste0(strat, p_name, "_", d,"_genes_stratification_pval.rds"))
}
}
#Rimuovere tutti i geni che hanno un pvalue non significativo
print("Remove no significant genes!")
df_normal_sb <- subset(df_normal, chiTest_pv < 0.05)
df_normal_sb[,c(1:3,ncol(df_normal_sb))] <- NULL
#Train/test set split
print("Divide the samples in training and test set!")
info <- readRDS("./ALLsamples_info_TrainTest.rds")
set.seed(246)
sampling <- sample.int(n = ncol(df_normal_sb), size = floor(.75*ncol(df_normal)), replace = FALSE)
for (z in 1:ncol(df_normal_sb)) {
nm <- sub(".+?-", "",colnames(df_normal_sb)[z])
q <- which(info$SAMPLES == nm & info$PROJECT == p & info$drug_name == d)
if(z %in% sampling){
info$"TR/TE"[q] <- "TRAIN"
} else {
info$"TR/TE"[q] <- "TEST"
}
}
saveRDS(info, "./ALLsamples_info_TrainTest.rds")
colnames(df_normal_sb) <- sub(".+?-", "",colnames(df_normal_sb))
p_mat_mg <- p_mat
row.names(p_mat_mg) <- gsub("\\..*","",row.names(p_mat_mg))
print("Generate Annotation information.")
exp_info <- AnnotationDbi::select(org.Hs.eg.db, keys = row.names(p_mat_mg), columns = c("ENTREZID", "SYMBOL"), keytype = "ENSEMBL")
exp_info <- dplyr::distinct(exp_info)
exp_info <- ddply(exp_info, .(ENSEMBL, ENTREZID), summarize,
SYMBOL=paste(SYMBOL,collapse="/"))
df_normal_sb <- merge(df_normal_sb, exp_info, by.x=0, by.y="ENSEMBL")
p_mat_mg <- merge(p_mat_mg, exp_info, by.x=0, by.y="ENSEMBL" )
colnames(p_mat_mg) <- substring(colnames(p_mat_mg),1,12)
print(paste("Generate input file for Phensim for project", p_name, d))
for (s in colnames(df_normal_sb)[2:(ncol(df_normal_sb)-2)]) {
name_sb <- paste0(p, "_",d,"_", input$CONDITION[which(input$SAMPLES == s & input$drug_name ==d)],"_",info$`TR/TE`[which(info$SAMPLES == s & info$drug_name == d)],"_",s)
if(file.exists(paste0(output, name_sb,"_","enrich_file.txt"))){
print(paste("Input files for", s, "exist!"))
} else{
print(paste("Start - input file for Phensim:", s))
colonne <- c("Row.names", s, "ENTREZID", "SYMBOL")
no_exp_i <- p_mat_mg[,colonne]
no_exp_i <- subset(no_exp_i, no_exp_i[,s] < 10)
sb <- df_normal_sb[,colonne]
sb <- subset(sb, sb[,s] == "UP_REG" | sb[,s] == "DOWN_REG" )
sb[,5] <- ifelse(sb[,s] == "UP_REG", "ACTIVATION", "INHIBITION")
print(paste("###",s, "generation enrich patient file"))
enrich_file <- matrix(nrow = nrow(sb), ncol = 8)
enrich_file[,1] <- "P"
enrich_file[,2] <- s
enrich_file[,3] <- "PATIENT"
enrich_file[,4] <- sb[,3]
enrich_file[,5] <- sb[,4]
enrich_file[,6] <- "GENE"
enrich_file[,7] <- "PATIENT_EDGE"
enrich_file[,8] <- sb[,5]
enrich_file <- enrich_file[!is.na(enrich_file[,4]),]
colnames(enrich_file) <- c("X1", "X2", "X3", "X4", "X5", "X6","X7", "X8")
enrich_file <- as.data.frame(enrich_file)
enrich_file$X9 <- unique(input$CONDITION[which(input$SAMPLES == s & input$drug_name == d)])
enrich_file <- enrich_file[!is.na(enrich_file$X4),]
print(paste("###",s, "generation no-expression genes file"))
no_exp <- no_exp_i[!(no_exp_i$ENTREZID %in% enrich_file$X4),]
no_exp <- no_exp$ENTREZID
no_exp <- no_exp[complete.cases(no_exp)]
#name_sb <- paste0(p, "_", enrich_file[1,9],"_",s)
write.table(enrich_file, file = paste0(output, name_sb,"_","enrich_file.txt"), col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
write.table(no_exp, file = paste0(output,name_sb,"_","no_exp_genes.txt"), col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
print(paste("Finish - input file for Phensim:", s))
}
}
}
}
print("Generation simulation parameters file")
sim_par <- matrix(ncol = 2, nrow = 1 )
sim_par[,1] <- "P"
sim_par[,2] <- "OVEREXPRESSION"
print("Generation custom node file")
cust_nd <- matrix(ncol = 2, nrow = 1)
cust_nd[,1] <- "PATIENT"
cust_nd[,2] <- "0"
print("Generation custom edge file")
cust_ed <- matrix(ncol = 1, nrow = 1)
cust_ed[,1] <- c("PATIENT_EDGE")
write.table(sim_par, file = paste0(output,"sim_par.txt"), col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
write.table(cust_nd, file = paste0(output, "custom_node_file.txt"), col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
write.table(cust_ed, file = paste0(output,"custom_edge_file.txt"), col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
}
drug_sim(sample_info="./ALLsamples_info_downloaded.rds",m_expr = "./expression_matrix/", output="./input_PHENSIM/", m_comb = "./comb_matrix/", strat = "./gene_stratified/")
sample_info="./ALLsamples_info_downloaded.rds"
m_expr = "./expression_matrix/"
output="./input_PHENSIM/"
m_comb = "./comb_matrix/"
strat = "./gene_stratified/"