-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo-RLS_KF.R
297 lines (274 loc) · 9.6 KB
/
demo-RLS_KF.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
# demo for RLS-KF algorithm of Drug-Target-Interaction (DTI) prediction
#setwd("To Your Directory Including All Required Files")
rm(list = ls())
# libraries for compiling C++ codes used in this work
library(Rcpp)
library(RcppArmadillo)
# library for checking positive semi-definite
library(matrixcalc)
# libraries for calAUPR
library(MESS)
library(pracma)
library(ROCR)
library(Bolstad2)
# sourceCpp
sourceCpp("fastKgipMat.cpp")
sourceCpp("fastKF.cpp")
sourceCpp("fastSolve.cpp")
# source
source("RLS_KF.R")
source("calAUPR.R")
###################################################################################################
# You just modify the partfn to different data sets
# file name to be used: nr, gpcr, ic, e
partfn = "nr"
# Take long time for Enzyme dataset
if (partfn == "e") cat("Need several hours to finish the big [Enzymes] data set, please be patient!\n")
###################################################################################################
switch(partfn,
nr = {
# y
yFn <- paste0(partfn, "_admat_dgc.txt")
y <- read.table(yFn)
# simmatCompd
simCompdFn <- paste0(partfn, "_simmat_dc.txt")
simmatCompd <- read.table(simCompdFn)
# simmatTarget
simTargetFn <- paste0(partfn, "_simmat_dg.txt")
simmatTarget <- read.table(simTargetFn)
# convert into matrix
y <- as.matrix(y)
simmatCompd <- as.matrix(simmatCompd)
simmatTarget <- as.matrix(simmatTarget)
# check matrix symmetric
if (!isSymmetric(simmatCompd)) simmatCompd <- (simmatCompd + t(simmatCompd))/2
# check matrix positive semi-definite
epsilon <- 0.1
while (!is.positive.semi.definite(simmatCompd)) simmatCompd <- simmatCompd + epsilon * diag(nrow(simmatCompd))
# check matrix symmetric
if (!isSymmetric(simmatTarget)) simmatTarget <- (simmatTarget + t(simmatTarget))/2
# check matrix positive semi-definite
epsilon <- 0.1
while (!is.positive.semi.definite(simmatTarget)) simmatTarget <- simmatTarget + epsilon * diag(nrow(simmatTarget))
},
gpcr = {
# y
yFn <- paste0(partfn, "_admat_dgc.txt")
y <- read.table(yFn)
# simmatCompd
simCompdFn <- paste0(partfn, "_simmat_dc.txt")
simmatCompd <- read.table(simCompdFn)
# simmatTarget
simTargetFn <- paste0(partfn, "_simmat_dg.txt")
simmatTarget <- read.table(simTargetFn)
# convert into matrix
y <- as.matrix(y)
simmatCompd <- as.matrix(simmatCompd)
simmatTarget <- as.matrix(simmatTarget)
# check matrix symmetric
if (!isSymmetric(simmatCompd)) simmatCompd <- (simmatCompd + t(simmatCompd))/2
# check matrix positive semi-definite
epsilon <- 0.1
while (!is.positive.semi.definite(simmatCompd)) simmatCompd <- simmatCompd + epsilon * diag(nrow(simmatCompd))
# check matrix symmetric
if (!isSymmetric(simmatTarget)) simmatTarget <- (simmatTarget + t(simmatTarget))/2
# check matrix positive semi-definite
epsilon <- 0.1
while (!is.positive.semi.definite(simmatTarget)) simmatTarget <- simmatTarget + epsilon * diag(nrow(simmatTarget))
},
ic = {
# y
yFn <- paste0(partfn, "_admat_dgc.txt")
y <- read.table(yFn)
# simmatCompd
simCompdFn <- paste0(partfn, "_simmat_dc.txt")
simmatCompd <- read.table(simCompdFn)
# simmatTarget
simTargetFn <- paste0(partfn, "_simmat_dg.txt")
simmatTarget <- read.table(simTargetFn)
# convert into matrix
y <- as.matrix(y)
simmatCompd <- as.matrix(simmatCompd)
simmatTarget <- as.matrix(simmatTarget)
# check matrix symmetric
if (!isSymmetric(simmatCompd)) simmatCompd <- (simmatCompd + t(simmatCompd))/2
# check matrix positive semi-definite
epsilon <- 0.1
while (!is.positive.semi.definite(simmatCompd)) simmatCompd <- simmatCompd + epsilon * diag(nrow(simmatCompd))
# check matrix symmetric
if (!isSymmetric(simmatTarget)) simmatTarget <- (simmatTarget + t(simmatTarget))/2
# check matrix positive semi-definite
epsilon <- 0.1
while (!is.positive.semi.definite(simmatTarget)) simmatTarget <- simmatTarget + epsilon * diag(nrow(simmatTarget))
},
e = {
# y
yFn <- paste0(partfn, "_admat_dgc.txt")
y <- read.table(yFn)
# simmatCompd
simCompdFn <- paste0(partfn, "_simmat_dc.txt")
simmatCompd <- read.table(simCompdFn)
# simmatTarget
simTargetFn <- paste0(partfn, "_simmat_dg.txt")
simmatTarget <- read.table(simTargetFn)
# convert into matrix
y <- as.matrix(y)
simmatCompd <- as.matrix(simmatCompd)
simmatTarget <- as.matrix(simmatTarget)
# check matrix symmetric
if (!isSymmetric(simmatCompd)) simmatCompd <- (simmatCompd + t(simmatCompd))/2
# check matrix positive semi-definite
epsilon <- 0.1
while (!is.positive.semi.definite(simmatCompd)) simmatCompd <- simmatCompd + epsilon * diag(nrow(simmatCompd))
# check matrix symmetric
if (!isSymmetric(simmatTarget)) simmatTarget <- (simmatTarget + t(simmatTarget))/2
# check matrix positive semi-definite
epsilon <- 0.1
while (!is.positive.semi.definite(simmatTarget)) simmatTarget <- simmatTarget + epsilon * diag(nrow(simmatTarget))
},
stop("partfn should be one of: {nr, gpcr, ic, enzyme}\n")
)
# parameters
gamma0 = 1
lambda = 1
nfold = 10
# k for KF
numNeig = 4
# t for KF
numIter = 2
# number of replicated runs
nreps <- 10
AUC_ave <- vector(length = nreps)
AUPR_ave <- vector(length = nreps)
AUC_max <- vector(length = nreps)
AUPR_max <- vector(length = nreps)
prob_ave <- NULL
for (i_rep in 1:nreps) {
cat(i_rep, "/", nreps, "\n")
cat("data set:", partfn, "\n")
cat("k =", numNeig, "\n")
cat("t =", numIter, "\n")
flush.console()
# (1) Prediction based on the target similarity
yTarget <- y
numRows <- nrow(yTarget)
numCols <- ncol(yTarget)
predBasedTarget <- matrix(0, nr = numRows, nc = numCols)
myColPrediction <- matrix(0, nr = numRows, nc = 1)
# Kernel matrix from similarity matrix
k4simmat <- simmatTarget
# Cross-validation folds
lenSeg <- ceiling(numRows / nfold)
incomplete <- nfold * lenSeg - numRows
complete <- nfold - incomplete
inds <- matrix(c(sample(1:numRows), rep(NA, incomplete)), nrow = lenSeg, byrow = TRUE)
folds <- lapply(as.data.frame(inds), function(x) c(na.omit(x)))
# Main fold prediction function
for (i in 1:numCols) {
currY <- yTarget[, i]
# Used when currY are all zeros: inferred currY = ytrMat %*% currSim2
currSim2 <- simmatCompd[, i]
for (j in 1:nfold) {
idxTe <- folds[[j]]
idxTr <- setdiff(1:numRows, idxTe)
ytrMat <- yTarget
# Put current 'test set' to zeros
ytrMat[idxTe, i] <- 0
currYsum <- sum(ytrMat[, i])
# Prediction for each fold
myColPrediction[idxTe, 1] <- RLS_KF(
currY = currY,
ytrMat = ytrMat,
currYsum = currYsum,
currSim2 = currSim2,
idxTr = idxTr,
idxTe = idxTe,
simmat = simmatTarget,
simmat2 = simmatCompd,
gamma0 = gamma0,
numNeig = numNeig,
numIter = numIter,
lambda = lambda,
k4simmat = k4simmat)
}
predBasedTarget[, i] <- myColPrediction
}
# (2) Prediction based on the compound similarity
yCompd <- t(y)
numRows <- nrow(yCompd)
numCols <- ncol(yCompd)
predBasedCompd <- matrix(0, nr = numRows, nc = numCols)
myColPrediction <- matrix(0, nr = numRows, nc = 1)
# Kernel matrix from similarity matrix
k4simmat <- simmatCompd
# Cross-validation folds
lenSeg <- ceiling(numRows / nfold)
incomplete <- nfold * lenSeg - numRows
complete <- nfold - incomplete
inds <- matrix(c(sample(1:numRows), rep(NA, incomplete)), nrow = lenSeg, byrow = TRUE)
folds <- lapply(as.data.frame(inds), function(x) c(na.omit(x)))
# Main fold prediction function
for (i in 1:numCols) {
currY <- yCompd[, i]
# Used when currY are all zeros: inferred currY = ytrMat %*% currSim2
currSim2 <- simmatTarget[, i]
for (j in 1:nfold) {
idxTe <- folds[[j]]
idxTr <- setdiff(1:numRows, idxTe)
ytrMat <- yCompd
# Put current 'test set' to zeros
ytrMat[idxTe, i] <- 0
currYsum <- sum(ytrMat[, i])
# Prediction for each fold
myColPrediction[idxTe, 1] <- RLS_KF(
currY = currY,
ytrMat = ytrMat,
currYsum = currYsum,
currSim2 = currSim2,
idxTr = idxTr,
idxTe = idxTe,
simmat = simmatCompd,
simmat2 = simmatTarget,
gamma0 = gamma0,
numNeig = numNeig,
numIter = numIter,
lambda = lambda,
k4simmat = k4simmat)
}
predBasedCompd[, i] <- myColPrediction
}
# (3) statistics
yLabel <- as.vector(y)
# (3-1) based on average
predProb_ave <- (predBasedTarget + t(predBasedCompd))/2
finalPred <- as.vector(predProb_ave)
# used for nrEXt
prob_ave <- cbind(prob_ave, finalPred)
statRes_ave <- calAUPR(yLabel, finalPred)
AUC_ave[i_rep] <- statRes_ave[, "auc"]
AUPR_ave[i_rep] <- statRes_ave[, "aupr"]
# (3-2) based on maximum
predBasedCompd <- t(predBasedCompd)
predProb_max <- ifelse(predBasedTarget > predBasedCompd, predBasedTarget, predBasedCompd)
finalPred <- as.vector(predProb_max)
statRes_max <- calAUPR(yLabel, finalPred)
AUC_max[i_rep] <- statRes_max[, "auc"]
AUPR_max[i_rep] <- statRes_max[, "aupr"]
}
# (4) show results
# (4-1) average result
meanAUC_ave <- mean(AUC_ave)
sdAUC_ave <- sd(AUC_ave)
meanAUPR_ave <- mean(AUPR_ave)
sdAUPR_ave <- sd(AUPR_ave)
# (4-2) maximum result
meanAUC_max <- mean(AUC_max)
sdAUC_max <- sd(AUC_max)
meanAUPR_max <- mean(AUPR_max)
sdAUPR_max <- sd(AUPR_max)
# output the main results
cat("Current data set is: ", partfn, "\n")
cat("mean AUC and sd based on average: ", meanAUC_ave, "+/-", sdAUC_ave, "\n")
cat("mean AUPR and sd based on average: ", meanAUPR_ave, "+/-", sdAUPR_ave, "\n")
cat("mean AUC and sd based on maximum: ", meanAUC_max, "+/-", sdAUC_max, "\n")
cat("mean AUPR and sd based on maximum: ", meanAUPR_max, "+/-", sdAUPR_max, "\n")