-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKINOMOStrategy-class.R
300 lines (230 loc) · 8.17 KB
/
KINOMOStrategy-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
#' @include algorithmic.R
#' @include KINOMOSet-class.R
NULL
setClass('Strategy'
, contains = 'VIRTUAL'
, representation = representation(
name = 'character' # the strategy name
, package = 'character' # the package that defines the strategy
, defaults = 'list'
)
, prototype = prototype(
package = character()
, name = character()
)
, validity=function(object){
# slot 'name' must be a non-empty character string
obj <- name(object)
if( !length(obj) || (length(obj)>1L || obj=='') )
return(str_c("Slot 'name' must be a single non-empty character string [", obj, ']'))
TRUE
}
)
setGeneric('name', function(object, ...) standardGeneric('name'))
setMethod('name', signature(object='Strategy'),
function(object, all=FALSE){
n <- slot(object, 'name')
if( length(n) && !all ) n[1L] else n
}
)
setGeneric('name<-', function(object, ..., value) standardGeneric('name<-'))
setReplaceMethod('name', signature(object='Strategy', value='character'),
function(object, value){
slot(object, 'name') <- value
validObject(object)
object
}
)
defaultArgument <- function(name, object, value, force=FALSE){
# taken from methods::hasArg
aname <- as.character(substitute(name))
miss <- eval(substitute(missing(name)), sys.frame(sys.parent()))
defaults <- attr(object, 'defaults')
if( !miss && !force ) eval(substitute(name), sys.frame(sys.parent()))
else if( aname %in% names(defaults) ) defaults[[aname]]
else value
}
setClass('KINOMOStrategy'
, representation(
objective = '.functionSlot' # the objective function used to compute the error (defined by name or function)
, model = 'character' # KINOMO model to use
, mixed = 'logical' # can the input data be negative?
)
, prototype=prototype(objective='euclidean', model='KINOMOstd', mixed=FALSE)
, 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("Slot 'objective' must either be a non-empty character string or a function definition.")
# slot 'model' must be the name of a class that extends class 'KINOMO'
obj <- modelname(object)
if( !is.character(obj) )
return("Slot 'model' must be a character vector")
if( any(inv <- !sapply(obj, isKINOMOclass)) )
return(paste("Slot 'model' must contain only names of a class that extends class 'KINOMO' [failure on class(es) "
, paste( paste("'", obj[inv], "'", sep=''), collapse=', ')
,"]"
, sep=''))
# slot 'mixed' must be a single logical
obj <- slot(object, 'mixed')
if( length(obj) != 1 )
return( paste("Slot 'mixed' must be a single logical [length=", length(obj), "]", sep='') )
}
, contains = c('VIRTUAL', 'Strategy')
)
#' @export
#' @rdname KINOMOStrategy-class
setMethod('show', 'KINOMOStrategy',
function(object){
cat('<object of class: ', class(object), ">\n", sep='')
cat(" name: ", name(object), " [", packageSlot(object), "]\n", sep='')
svalue <- objective(object)
svalue <- if( is.function(svalue) ) str_args(svalue, exdent=10) else paste("'", svalue,"'", sep='')
cat(" objective:", svalue, "\n")
cat(" model:", modelname(object), "\n")
if( length(object@defaults) > 0L ){
cat(" defaults:", str_desc(object@defaults, exdent=10L), "\n")
}
return(invisible())
}
)
# Coerce method for 'KINOMOStrategy' objects into 'character': give the main name
setAs('KINOMOStrategy', 'character'
, def = function(from) name(from)
)
setGeneric('KINOMOStrategy', function(name, method, ...) standardGeneric('KINOMOStrategy') )
setMethod('KINOMOStrategy', signature(name='character', method='function'),
function(name, method, ...){
# build a KINOMOStrategyFunction object on the fly to wrap function 'method'
KINOMOStrategy(name=name, algorithm=method, ...)
}
)
#' Creates an \code{KINOMOStrategy} object based on a template object (Constructor-Copy).
setMethod('KINOMOStrategy', signature(name='character', method='KINOMOStrategy'),
function(name, method, ...){
package <- topns_name()
# build an KINOMOStrategy object based on template object
strategy <- new(class(method), method, name=name, ..., package=package)
# valid the new strategy
validObject(strategy)
# add trace of inheritance from parent KINOMO algorithm
attr(strategy, 'parent') <- name(method)[1]
# return new object
strategy
}
)
#' Creates an \code{KINOMOStrategy} based on a template object (Constructor-Copy),
#' in particular it uses the \strong{same} name.
setMethod('KINOMOStrategy', signature(name='KINOMOStrategy', method='missing'),
function(name, method, ...){
# do not change the object if single argument
if( nargs() == 1L ) return(name)
# use the name as a key
# NB: need special trick to avoid conflict between argument and function
mname <- match.fun('name')(name)
KINOMOStrategy(name=mname, method=name, ...)
}
)
setMethod('KINOMOStrategy', signature(name='missing', method='character'),
function(name, method, ...){
KINOMOStrategy(KINOMOAlgorithm(method, exact=TRUE), ...)
}
)
setMethod('KINOMOStrategy', signature(name='NULL', method='KINOMOStrategy'),
function(name, method, ...){
# use the name as a key
# NB: need special trick to avoid conflict between argument and function
mname <- match.fun('name')(method)
mname <- basename(tempfile(str_c(mname, '_')))
KINOMOStrategy(name=mname, method=method, ...)
}
)
setMethod('KINOMOStrategy', signature(name='character', method='character'),
function(name, method, ...){
KINOMOStrategy(name=name, method=KINOMOAlgorithm(method, exact=TRUE), ...)
}
)
setMethod('KINOMOStrategy', signature(name='NULL', method='character'),
function(name, method, ...){
KINOMOStrategy(NULL, method=KINOMOAlgorithm(method, exact=TRUE), ...)
}
)
setMethod('KINOMOStrategy', signature(name='character', method='missing'),
function(name, method, ...){
package <- topns_name()
# check iterative strategy
if( hasArg2('Update') ){ # create a new KINOMOStrategyIterative object
new('KINOMOStrategyIterative', name=name, ..., package=package)
}else if( hasArg2('algorithm') ){
new('KINOMOStrategyFunction', name=name, ..., package=package)
}else{
stop('KINOMOStrategy - Could not infer the type of KINOMO strategy to instantiate.')
}
}
)
setMethod('run', signature(object='KINOMOStrategy', y='matrix', x='KINOMOfit'),
function(object, y, x, ...){
stop("KINOMOStrategy::run is a pure virtual method that should be overloaded in class '", class(object),"'.")
}
)
setMethod('run', signature(object='KINOMOStrategy', y='matrix', x='KINOMO'),
function(object, y, x, ...){
run(object, y, KINOMOfit(fit=x, seed='none', method=name(object)), ...)
}
)
setMethod('deviance', 'KINOMOStrategy',
function(object, x, y, ...){
obj.fun <- slot(object, 'objective')
# return the distance computed using the strategy's objective function
if( !is.function(obj.fun) )
deviance(x, y, method=obj.fun, ...)
else # directly compute the objective function
obj.fun(x, y, ...)
}
)
setMethod('objective', 'KINOMOStrategy',
function(object){
slot(object, 'objective')
}
)
setReplaceMethod('objective', signature(object='KINOMOStrategy', value='character'),
function(object, value){
#TODO: test for the existence of objective method
slot(object, 'objective') <- value
validObject(object)
object
}
)
setReplaceMethod('objective', signature(object='KINOMOStrategy', value='function'),
function(object, value){
slot(object, 'objective') <- value
validObject(object)
object
}
)
setMethod('modelname', signature(object='KINOMOStrategy'),
function(object){
slot(object, 'model')
}
)
is.mixed <- function(object){
return( slot(object, 'mixed') )
}
KINOMOFormals <- function(x, ...){
UseMethod('KINOMOFormals')
}
#' @export
KINOMOFormals.character <- function(x, ...){
s <- KINOMOAlgorithm(x)
KINOMOFormals(s, ...)
}
#' @export
KINOMOFormals.KINOMOStrategy <- function(x, ...){
m <- getMethod('run', signature(object='KINOMOStrategy', y='matrix', x='KINOMOfit'))
args <- allFormals(m)
# prepend registered default arguments
expand_list(x@defaults, args)
}
KINOMOArgs <- function(x){
args(KINOMOWrapper(x))
}