-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKINOMOfit-class.R
437 lines (354 loc) · 12.2 KB
/
KINOMOfit-class.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#' @include fixed-terms.R
#' @include KINOMOModel.R
NULL
#'
setClass('KINOMOfit'
, representation(
fit = 'KINOMO', # KINOMO model
residuals = 'numeric', # residuals from the target matrix
method = 'character', # method used to compute the factorization
seed = 'character', # seeding method used to compute the factorization
rng = 'ANY', # numerical random seed
distance = '.functionSlotNULL', # method used to compute the distance between the target matrix and its KINOMO estimate
parameters = 'list', # method used to compute the factorization
runtime = 'proc_time', # running time to perform the KINOMO
options = 'list', # run options
extra = 'list' # extra list of results output by the method
, call = 'call' # store last call to KINOMO()
)
, prototype = prototype(
residuals = numeric(),
method = '',
seed = '',
parameters = list(),
extra = list()
)
, validity = function(object){
# slot 'objective' must either be a non-empty character string or a function
obj <- objective(object)
if( is.character(obj) && obj == '')
return(paste("Slot 'objective' must either be a non-empty character string or a function definition", sep=''))
# everything went fine: return TRUE
TRUE
}
, contains = 'KINOMO'
)
KINOMOfit <- function(fit=KINOMOModel(), ..., rng=NULL){
# use current RNG settings if not otherwise provided
if( is.null(rng) )
rng <- getRNG()
new('KINOMOfit', fit=fit, ..., rng=rng)
}
setMethod('fitted', signature(object='KINOMOfit'),
function(object, ...){
fitted(fit(object), ...)
}
)
setMethod('.basis', signature(object='KINOMOfit'),
function(object, ...){
.basis(fit(object), ...)
}
)
setReplaceMethod('.basis', signature(object='KINOMOfit', value='matrix'),
function(object, value){
.basis(fit(object)) <- value
object
}
)
setMethod('.coef', signature(object='KINOMOfit'),
function(object, ...){
.coef(fit(object), ...)
}
)
setReplaceMethod('.coef', signature(object='KINOMOfit', value='matrix'),
function(object, value){
.coef(fit(object)) <- value
object
}
)
setMethod('ibterms', 'KINOMOfit',
function(object){
ibterms(fit(object))
}
)
setMethod('icterms', 'KINOMOfit',
function(object){
icterms(fit(object))
}
)
#' Returns the offset from the fitted model.
setMethod('offset', signature(object='KINOMOfit'),
function(object){
offset(fit(object))
}
)
setMethod('niter', signature(object='KINOMOfit'),
function(object, ...){
object@extra$iteration
}
)
setReplaceMethod('niter', signature(object='KINOMOfit', value='numeric'),
function(object, value){
if( (length(value) != 1) || value < 0 )
stop("KINOMO::niter - invalid value for 'niter': single non-negative value is required.", call.=FALSE)
object@extra$iteration <- value
object
}
)
#' Show method for objects of class \code{KINOMOfit}
setMethod('show', 'KINOMOfit',
function(object)
{
cat("<Object of class: ", class(object), ">\n", sep='')
cat(" # Model:\n ")
s <- capture.output(show(fit(object)))
cat(s, sep="\n ")
cat(" # Details:\n ")
.local <- function(){
if( algorithm(object) != '' ) cat("algorithm: ", algorithm(object), "\n")
if( seeding(object) != '' ) cat("seed: ", seeding(object), "\n")
# initial RNG stream
cat("RNG: ", RNGstr(object), "\n", sep='')
# distance/objective function
svalue <- objective(object)
svalue <- if( is.function(svalue) ) '<function>' else paste("'", svalue,"'", sep='')
cat("distance metric: ", svalue, "\n")
if( length(residuals(object)) !=0 ) cat("residuals: ", residuals(object), "\n");
# show the miscellaneous result values
if( length(object@misc) > 0L )
cat("miscellaneous:", str_desc(object@misc, exdent=12L), ". (use 'misc(object)')\n")
# show the parameters specific to the method
if( length(object@parameters) > 0 ){
cat("parameters:", str_desc(object@parameters, exdent=12L), "\n")
# p <- sapply(object@parameters, function(x){
# if( is.vector(x) && length(x) == 1L ) x
# else paste("<", class(x), ">", sep='')
# })
# cat(str_wrap(str_out(p, NA, use.names=TRUE, quote=FALSE), exdent=12), "\n")
}
# show number of iterations if present
if( !is.null(i <- niter(object)) ) cat("Iterations:", i, "\n")
# show elapsed time if present
if( length(runtime(object)) > 0 ){ cat("Timing:\n"); show(runtime(object));}
}
s <- capture.output(.local())
cat(s, sep="\n ")
}
)
#' @export
setGeneric('fit', function(object, ...) standardGeneric('fit'))
#' Returns the KINOMO model object stored in slot \code{'fit'}.
setMethod('fit', 'KINOMOfit', function(object) slot(object, 'fit'))
setGeneric('fit<-', function(object, value) standardGeneric('fit<-'))
#' Updates the KINOMO model object stored in slot \code{'fit'} with a new value.
setReplaceMethod('fit', signature(object='KINOMOfit', value='KINOMO'),
function(object, value){
slot(object, 'fit') <- value
object # TODO: valid object before returning it (+param check=TRUE or FALSE)
}
)
setGeneric('minfit', function(object, ...) standardGeneric('minfit') )
#' Returns the object its self, since there it is the result of a single KINOMO run.
setMethod('minfit', 'KINOMOfit', function(object) object)
#' Returns the type of a fitted KINOMO model.
#' It is a shortcut for \code{modelname(fit(object)}.
setMethod('modelname', signature(object='KINOMOfit'),
function(object)
{
modelname(fit(object))
}
)
#'
setGeneric('residuals', package='stats')
#'
setMethod('residuals', 'KINOMOfit',
function(object, track=FALSE, niter=NULL, ...){
## IMPORTANT: keep this '...' and do not add a 'method' argument as this
## one is passed by KINOMOfitX::fit (see bug #159) and is not supposed to be
## used
res <- slot(object, 'residuals')
if( track ) res
else if( is.null(niter) ) tail(res, n=1)
else res[as.character(niter)]
}
)
setGeneric('residuals<-', function(object, ..., value) standardGeneric('residuals<-') )
#' @inline
setReplaceMethod('residuals', 'KINOMOfit',
function(object, ..., niter=NULL, track=FALSE, value){
if( track ) slot(object, 'residuals') <- value
else{
if( !is.null(niter) ) value <- setNames(value, niter)
slot(object, 'residuals') <- c(slot(object, 'residuals'), value)
}
object
}
)
hasTrack <- function(object, niter=NULL){
if( is.null(niter) ) length( slot(object, 'residuals') ) > 1
else !is.na(slot(object, 'residuals')[as.character(niter)])
}
trackError <- function(object, value, niter, force=FALSE){
track <- run.options(object, 'error.track')
track.interval <- run.options(object, 'track.interval')
if( force || (track && niter %% track.interval == 0) ){
# add the new value to the error track
last.iter <- names(residuals(object))
duplicate <- if( !is.null(last.iter) ) niter == last.iter else FALSE
if( !duplicate ){
iter <- if( niter >= 0 ) niter
residuals(object, niter=iter) <- value
}
}
object
}
setMethod('deviance', 'KINOMOfit',
function(object, y, method, ...){
if( missing(y) ) setNames(residuals(object), NULL)
else{
# if missing retrieve the actual distance measure from the KINOMO object
if( missing(method) ) method = object@distance
# compute the distance between the target and the fitted KINOMO model
deviance(fit(object), y, method=method, ...)
}
}
)
#' Returns the name of the algorithm that fitted the KINOMO model \code{object}.
setMethod('algorithm', 'KINOMOfit', function(object){ object@method } )
#' @inline
setReplaceMethod('algorithm', 'KINOMOfit',
function(object, value){
object@method <- value
object
}
)
#' Returns the name of the seeding method that generated the starting point
#' for the KINOMO algorithm that fitted the KINOMO model \code{object}.
setMethod('seeding', 'KINOMOfit', function(object){ object@seed } )
#' @inline
setReplaceMethod('seeding', 'KINOMOfit',
function(object, value){
object@seed <- value
object
}
)
setMethod('objective', signature(object='KINOMOfit'),
function(object, y){
# when both x and y are missing then returns slot objective
if( missing(y) ) return(slot(object, 'distance'))
# return the distance computed using the strategy's objective function
deviance(fit(object), y, method=slot(object, 'distance'))
}
)
#' @inline
setReplaceMethod('objective', signature(object='KINOMOfit', value='ANY'),
function(object, value){
slot(object, 'distance') <- value
validObject(object)
object
}
)
#' Returns the CPU time required to compute a single KINOMO fit.
setMethod('runtime', 'KINOMOfit',
function(object, ...){
object@runtime
}
)
#' Identical to \code{runtime}, since their is a single fit.
setMethod('runtime.all', 'KINOMOfit', getMethod('runtime', 'KINOMOfit'))
###% Access methods to run options.
setGeneric('run.options', function(object, ...) standardGeneric('run.options') )
setMethod('run.options', 'KINOMOfit',
function(object, name){
if( missing(name) ) object@options
else object@options[[name]]
}
)
setGeneric('run.options<-', function(object, ..., value) standardGeneric('run.options<-') )
setReplaceMethod('run.options', 'KINOMOfit',
function(object, ..., value){
params <- list(...)
baseError <- 'Setting KINOMO runtime options: '
if ( length(params) == 0 ){
if( !is.list(value) ) stop(baseError, 'options must be given as a list')
object@options <- value
return(object)
}
else if ( length(params) > 1 ) stop(baseError, 'options cannot set more than one option at a time')
name <- params[[1]]
if( !is.character(name) ) stop(baseError, 'option name must be given as a character string')
# check if the option exists
#if( !is.element(name, names(KINOMO.options.runtime())) ) stop(baseError, "option '", name, "' is not defined.")
object@options[[name]] <- value
object
}
)
setGeneric('verbose', function(object, ...) standardGeneric('verbose') )
setMethod('verbose', 'KINOMOfit',
function(object){
return(run.options(object, 'verbose') || KINOMO.getOption('debug'))
}
)
setGeneric('plot', package='graphics' )
setMethod('plot', signature(x='KINOMOfit', y='missing'),
function(x, y, skip=-1L, ...){
# retrieve the residuals track
track <- residuals(x, track=TRUE)
if( length(track) <= 1 ){
warning(class(x), ' object has no residuals track')
return(invisible())
}
# skip part of the track
if( skip == -1L && !is.null(names(track)) ) track <- track[names(track)!='0'] # remove initial residual
else if( skip > 0 ) track <- track[-(1:skip)]
# set default graphical parameters (those can be overriden by the user)
params <- .set.list.defaults(list(...)
, xlab='Iterations'
, ylab=paste('Objective value ('
, if( is.character(x@distance) ) x@distance else algorithm(x), ')'
, sep='' )
, main=paste("KINOMO Residuals\nMethod: ", algorithm(x), " - Rank: ", nbasis(x), sep='')
, cex.main = 1
, col='#5555ff', lwd=1.4, type='l', cex=0.5)
do.call('plot', c(list(names(track), track), params))
points(names(track), track, type='p', cex=0.6, col=params$col)
}
)
setMethod('summary', signature(object='KINOMOfit'),
function(object, ...){
res <- summary(fit(object), ...)
## IMPORTANT: if adding a summary measure also add it in the sorting
## schema of method KINOMOfitX::compare to allow ordering on it
# retreive final residuals
res <- c(res, residuals=as.numeric(residuals(object)))
# nb of iterations
res <- c(res, niter=as.integer(niter(object)) )
# runtime
t <- runtime(object)
utime <- as.numeric(t['user.self'] + t['user.child'])
res <- c(res, cpu=utime, cpu.all=utime, nrun=1)
# return result
return(res)
}
)
#' Compares two KINOMO models when at least one comes from a KINOMOfit object,
#' i.e. an object returned by a single run of \code{\link{KINOMO}}.
setMethod('KINOMO.equal', signature(x='KINOMOfit', y='KINOMO'),
function(x, y, ...){
KINOMO.equal(fit(x), y, ...)
}
)
#' Compares two KINOMO models when at least one comes from a KINOMOfit object,
#' i.e. an object returned by a single run of \code{\link{KINOMO}}.
setMethod('KINOMO.equal', signature(x='KINOMO', y='KINOMOfit'),
function(x, y, ...){
KINOMO.equal(x, fit(y), ...)
}
)
#' Compares two fitted KINOMO models, i.e. objects returned by single runs of
#' \code{\link{KINOMO}}.
setMethod('KINOMO.equal', signature(x='KINOMOfit', y='KINOMOfit'),
function(x, y, ...){
KINOMO.equal(fit(x), fit(y), ...)
}
)