-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hist_match5_brca.R
250 lines (188 loc) · 10.5 KB
/
test_hist_match5_brca.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
# rm(list = ls())
## Set-up system path...
info <- Sys.getenv(c("USERNAME", "HOMEPATH"))
if (info["USERNAME"] == "SRDhruba"){
info["DIRPATH"] <- sprintf("%s\\Dropbox%s\\ResearchWork\\Rtest\\", info["HOMEPATH"], " (Personal)")
} else {
info["DIRPATH"] <- sprintf("%s\\Dropbox%s\\ResearchWork\\Rtest\\", info["HOMEPATH"], "")
}
setwd(info["DIRPATH"]); cat("Current system path = ", getwd(), "\n")
## Packages...
# library(ggplot2)
# library(ggpubr)
# library(randomForest)
# library(ks)
library(progress)
library(DMTL)
#### Functions...
printf <- function(..., end = "\n") {
if ((nargs() > 1) & (grepl(list(...)[1], pattern = "%")))
cat(sprintf(...), end)
else
cat(..., end)
}
norm01 <- function(z) { z <- if (min(z)) z - min(z) else z; z <- z / max(z); z }
norm.data <- function(df) as.data.frame(apply(df, MARGIN = 2, norm01))
### Pick top genes...
get.top.genes <- function(ranks, m.top = 150, verbose = FALSE) {
## Initialization...
nI <- 0; nGN <- 300
gene.rank <- intersect(ranks[1:nGN, 1], ranks[1:nGN, 2])
m <- length(gene.rank); m0 <- if (verbose) m
## Run iterations...
while(m < m.top) {
nI <- nI + 1; nGN <- nGN + 100
gene.rank <- intersect(ranks[1:nGN, 1], ranks[1:nGN, 2])
m <- length(gene.rank)
}
gene.rank <- sort(gene.rank, decreasing = FALSE) # Sort ranks
## Print results...
if (verbose)
printf("#top genes chosen = %d (nGN = %d, nI = %d, m0 = %d)", m, nGN, nI, m0)
gene.rank
}
### Get model performance...
calc.perf <- function(y, y.pred, measures = c("NRMSE", "NMAE", "SCC")) {
## Initialize results array...
perf.vals <- c()
for (mes in measures) {
## Calculate squared error...
if (grepl(pattern = "MSE", mes, ignore.case = TRUE)) {
num <- mean((y - y.pred)^2)
den <- if (mes == "NRMSE") mean((y - mean(y))^2) else 1
pow <- if (mes == "MSE") 1 else 0.5
perf.vals[mes] <- (num / den)^pow
}
## Calculate absolute error...
else if (grepl(pattern = "MAE", mes, ignore.case = TRUE)) {
num <- mean(abs(y - y.pred))
den <- if (mes == "NMAE") mean(abs(y - mean(y))) else 1
perf.vals[mes] <- num / den
}
## Calculate similarity measures...
else if (grepl(pattern = "CC", mes, ignore.case = TRUE)) {
alg <- if (mes == "SCC") "spearman" else "pearson"
perf.vals[mes] <- cor(y, y.pred, method = alg)
}
## Doesn't match any...
else
stop("Invalid performance measure! Please use common variants of MSE, MAE or CC (correlation coefficient).")
}
perf.vals
}
#### Read tumor-cell line data...
Xdata1 <- read.table("Data/BRCA_gene_expression_METABRIC_26_Oct_2020.txt", sep = "\t", header = TRUE)
Xdata2 <- read.table("Data/BRCA_gene_expression_CCLE_26_Oct_2020.txt", sep = "\t", header = TRUE)
Xdata3 <- read.table("Data/BRCA_gene_expression_GDSC_26_Oct_2020.txt", sep = "\t", header = TRUE)
Ydata1 <- read.table("Data/BRCA_biomarker_expression_METABRIC_26_Oct_2020.txt", sep = "\t", header = TRUE)
Ydata2 <- read.table("Data/BRCA_biomarker_expression_CCLE_26_Oct_2020.txt", sep = "\t", header = TRUE)
Ydata3 <- read.table("Data/BRCA_biomarker_expression_GDSC_26_Oct_2020.txt", sep = "\t", header = TRUE)
rank1 <- read.table("Data/BRCA_biomarker_ranks_METABRIC_27_Oct_2020.txt", sep = "\t", header = TRUE)
rank2 <- read.table("Data/BRCA_biomarker_ranks_CCLE_27_Oct_2020.txt", sep = "\t", header = TRUE)
rank3 <- read.table("Data/BRCA_biomarker_ranks_GDSC_27_Oct_2020.txt", sep = "\t", header = TRUE)
biomarkers <- colnames(Ydata1); q <- length(biomarkers)
## Get results for all biomarkers...
# source("dist.match.trans.learn.R") ## Load function
# source("dist_match_trans_learn.R") ## Load function
run <- function(q.run, n.feat, random.seed, density.opt = FALSE, model = "RF") {
# q.run <- 1 # drug idx
# random.seed <- 4321 # 0, 654321, 4321
# method.opt <- "dens" # hist, dens
# source("RF_predict.R") # Random forest modeling
perf.mes <- c("NRMSE", "NMAE", "PCC", "SCC")
results.all <- lapply(perf.mes, function(mes) data.frame("DMTL" = double(), "DMTL_SS" = double(), "BL" = double()))
names(results.all) <- perf.mes; results.all[["genes"]] <- data.frame("num.genes" = double())
# q.run <- 1:q; n.feat <- 150; random.seed <- 97531; density.opt <- FALSE
pb <- progress_bar$new(format = " running [:bar] :percent eta: :eta", total = length(q.run), clear = FALSE, width = 64)
pb$tick(0)
for (k in q.run) {
pb$tick()
# k <- 1; n.feat <- 150; density.opt <- FALSE; random.seed <- 7531
## Select biomarker...
bmChosen <- biomarkers[k]; #printf("\nChosen biomarker = %s", bmChosen)
ranks <- cbind(rank1[, bmChosen], rank2[, bmChosen], rank3[, bmChosen])
gnRank <- get.top.genes(ranks[, 2:3], m.top = n.feat, verbose = FALSE); m <- length(gnRank)
## Prepare datasets...
X1 <- Xdata1[, gnRank]; X2 <- rbind(Xdata2[, gnRank], Xdata3[, gnRank])
Y1 <- norm01(Ydata1[, bmChosen]); Y2 <- norm01(c(Ydata2[, bmChosen], Ydata3[, bmChosen]))
names(Y1) <- rownames(X1); names(Y2) <- rownames(X2)
## DMTL model...
prediction <- DMTL(target_set = list("X" = X1, "y" = Y1), source_set = list("X" = X2, "y" = Y2), pred_model = model,
model_optimize = FALSE, use_density = density.opt, random_seed = random.seed, all_pred = TRUE)
# Y1.pred <- prediction$mapped; Y1.pred.src <- prediction$unmapped
Y1.pred <- prediction$target; Y1.pred.src <- prediction$source
## Baseline model...
# set.seed(random.seed)
# RF.base <- randomForest(x = norm.data(X2), y = Y2, ntree = 200, mtry = 5, replace = TRUE)
# Y1.pred.base <- predict(RF.base, norm.data(X1))
# Y1.pred.base[Y1.pred.base < 0] <- 0; Y1.pred.base[Y1.pred.base > 1] <- 1
if (model == "RF") {
Y1.pred.base <- RF_predict(x_train = norm.data(X2), y_train = Y2, x_test = norm.data(X1), lims = c(0, 1), optimize = FALSE,
n_tree = 200, m_try = 0.4, seed = random.seed)
} else if (model == "SVM") {
Y1.pred.base <- SVM_predict(x_train = norm.data(X2), y_train = Y2, x_test = norm.data(X1), lims = c(0, 1), optimize = TRUE,
kernel = "poly", C = 1, eps = 0.01, kpar = list(degree = 3), seed = random.seed)
} else if (model == "EN") {
Y1.pred.base <- EN_predict(x_train = norm.data(X2), y_train = Y2, x_test = norm.data(X1), lims = c(0, 1), optimize = FALSE,
alpha = 0.8, seed = random.seed)
}
## Generate & save results...
results <- data.frame("DMTL" = calc.perf(Y1, Y1.pred, measures = perf.mes),
"DMTL_SS" = calc.perf(Y1, Y1.pred.src, measures = perf.mes),
"BL" = calc.perf(Y1, Y1.pred.base, measures = perf.mes), row.names = perf.mes)
## Print option...
if (length(q.run) == 1) { printf("\nResults for %s using top %d features = ", bmChosen, n.feat); print(results) }
for (mes in perf.mes) { results.all[[mes]][bmChosen, ] <- results[mes, ] }
results.all$genes[bmChosen, ] <- m
}
## Calculate mean performance...
for (mes in perf.mes) { results.all[[mes]]["Mean", ] <- colMeans(results.all[[mes]][biomarkers, ], na.rm = TRUE) }
results.all$genes["Mean", ] <- mean(results.all$genes[biomarkers, ], na.rm = TRUE)
results.all[["table"]] <- do.call(rbind, lapply(perf.mes, function(mes) results.all[[mes]]["Mean", ]))
rownames(results.all$table) <- perf.mes
## Print options...
if (length(q.run) > 1) { printf("\nResults summary for top %d features = ", n.feat); print(results.all$table) }
results.all
}
# source("dist.match.trans.learn.R") ## Load function
# source("dist_match_trans_learn.R") ## Load function
results.all.rf <- run(q.run = 1:q, n.feat = 150, random.seed = 7531, density.opt = FALSE, model = "RF")
results.all.svm <- run(q.run = 1:q, n.feat = 150, random.seed = 7531, density.opt = FALSE, model = "SVM")
results.all.en <- run(q.run = 1:q, n.feat = 150, random.seed = 7531, density.opt = FALSE, model = "EN")
# c(sum(results.all$NRMSE$DMTL >= 1), sum(results.all$NMAE$DMTL >= 1), sum(abs(results.all$SCC$DMTL) <= 0.2))
# ## Write in temporary file...
# write.in.file <- function() {
# write.table(results.all$NRMSE, file = sprintf("results_temp_%s.csv", format(Sys.Date(), "%d_%b_%Y")),
# sep = "\t", row.names = TRUE, col.names = TRUE)
# write.table(results.all$NMAE, file = sprintf("results_temp_%s.csv", format(Sys.Date(), "%d_%b_%Y")),
# sep = "\t", append = TRUE, row.names = TRUE, col.names = TRUE)
# write.table(results.all$SCC, file = sprintf("results_temp_%s.csv", format(Sys.Date(), "%d_%b_%Y")),
# sep = "\t", append = TRUE, row.names = TRUE, col.names = TRUE)
# }
# write.in.file()
# ##
MB.df <- as.data.frame(cbind(Y1, Y1.pred, Y1.pred.src, Y1.pred.base), row.names = rownames(Ydata1))
CG.df <- as.data.frame(Y2, row.names = c(rownames(Ydata2), rownames(Ydata3)))
plot.dist <- function() {
gg.pp <- list()
gg.pp[["Tumor"]] <- ggplot(MB.df, aes(x = Y1)) + geom_density(fill = "cyan4", alpha = 0.7) +
geom_histogram(aes(y = ..density..), bins = 100, color = "gray2",
fill = "brown2", alpha = 0.3) + theme_light() + xlab("") +
ylab("") + ggtitle("Tumor") + theme(plot.title = element_text(hjust = 0.5))
gg.pp[["Cell line"]] <- ggplot(CG.df, aes(x = Y2)) + geom_density(fill = "brown4", alpha = 0.7) +
geom_histogram(aes(y = ..density..), bins = 100, color = "gray2",
fill = "cyan2", alpha = 0.3) + theme_light() + xlab("") +
ylab("") + ggtitle("Cell line") + theme(plot.title = element_text(hjust = 0.5))
gg.pp[["Tumor.Pred"]] <- ggplot(MB.df, aes(x = Y1.pred)) + geom_density(fill = "cyan4", alpha = 0.7) +
geom_histogram(aes(y = ..density..), bins = 100, color = "gray2",
fill = "brown2", alpha = 0.3) + theme_light() + xlab("") +
ylab("") + ggtitle("Tumor (Predicted)") + theme(plot.title = element_text(hjust = 0.5))
gg.pp[["Tumor.Pred.Src"]] <- ggplot(MB.df, aes(x = Y1.pred.src)) + geom_density(fill = "brown4", alpha = 0.7) +
geom_histogram(aes(y = ..density..), bins = 100, color = "gray2",
fill = "cyan2", alpha = 0.3) + theme_light() + xlab("") +
ylab("") + ggtitle("Tumor (Predicted - Source)") +
theme(plot.title = element_text(hjust = 0.5))
gg.pp[c("ncol", "nrow")] <- list(2, 2)
annotate_figure(do.call(ggarrange, gg.pp), top = text_grob(bmChosen, face = "bold"))
}
plot.dist()