-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hist_match6_brca.R
199 lines (146 loc) · 7.81 KB
/
test_hist_match6_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
# rm(list = ls())
## Set-up system path...
info <- Sys.getenv(c("USERNAME", "HOMEPATH"))
if (info["USERNAME"] == "SRDhruba"){
info["DIRPATH"] <- sprintf("%s\\Dropbox (Personal)\\ResearchWork\\Rtest\\", info["HOMEPATH"])
} else {
info["DIRPATH"] <- sprintf("%s\\Dropbox\\ResearchWork\\Rtest\\", info["HOMEPATH"])
}
setwd(info["DIRPATH"]); cat("Current system path = ", getwd(), "\n")
## Packages...
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...
run <- function(q.run, n.feat, random.seed, density.opt = FALSE, model = "RF", optimize = FALSE) {
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())
## Prints progress...
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()
# ## When testing for a single drug...
# 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 = optimize, use_density = density.opt, random_seed = random.seed, all_pred = TRUE)
Y1.pred <- prediction$target; Y1.pred.src <- prediction$source
## Baseline model...
Y1.pred.base <- if (model == "RF") {
RF_predict(x_train = norm.data(X2), y_train = Y2, x_test = norm.data(X1), lims = c(0, 1), optimize = optimize,
n_tree = 200, m_try = 0.4, seed = random.seed)
} else if (model == "SVM") {
SVM_predict(x_train = norm.data(X2), y_train = Y2, x_test = norm.data(X1), lims = c(0, 1), optimize = optimize,
kernel = "rbf", C = 2, eps = 0.01, kpar = list(sigma = 0.1), seed = random.seed)
} else if (model == "EN") {
EN_predict(x_train = norm.data(X2), y_train = Y2, x_test = norm.data(X1), lims = c(0, 1), optimize = optimize,
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
}
## Try out different models...
results.all.rf <- run(q.run = 1:q, n.feat = 150, random.seed = 7531, density.opt = FALSE, model = "RF", optimize = FALSE)
results.all.svm <- run(q.run = 1:q, n.feat = 150, random.seed = 7531, density.opt = FALSE, model = "SVM", optimize = FALSE)
results.all.en <- run(q.run = 1:q, n.feat = 150, random.seed = 7531, density.opt = FALSE, model = "EN", optimize = FALSE)
# ## 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()