-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKINOMOOffset-class.R
82 lines (64 loc) · 1.82 KB
/
KINOMOOffset-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
#' @include KINOMOstd-class.R
NULL
setClass('KINOMOOffset'
, representation(
offset = 'numeric' # offset vector
)
, contains = 'KINOMOstd'
, prototype=prototype(
offset = numeric()
)
)
#' Show method for objects of class \code{KINOMOOffset}
#' @export
setMethod('show', 'KINOMOOffset',
function(object)
{
callNextMethod()
cat("offset: ")
if( length(object@offset) > 0 ){
cat('[', head(object@offset, 5)
, if( length(object@offset) > 5 ) "..." else NULL
, ']')
}
else cat('none')
cat("\n")
}
)
setMethod("initialize", 'KINOMOOffset',
function(.Object, ..., offset){
.Object <- callNextMethod()
# correct the offset slot if possible
if( missing(offset) ) offset <- numeric()
if( !is.numeric(offset) ) stop("Unvalid value for parameter 'offset': a numeric vector is expected")
# force length to be consistent with the factorization's dimension
n <- nrow(.Object)
if( n > 0 ) .Object@offset <- c( offset, rep(NA, max(0, n - length(offset))) )[1:n]
# return the initialized valid object
.Object
}
)
#' @export
setGeneric('offset', package='stats')
#'
setMethod('offset', signature(object='KINOMOOffset'),
function(object){
object@offset
}
)
setMethod('fitted', signature(object='KINOMOOffset'),
function(object, W, H, offset=object@offset){
if( missing(W) ) W <- object@W
if( missing(H) ) H <- object@H
object@W %*% object@H + offset
}
)
setMethod('rKINOMO', signature(x='KINOMOOffset', target='numeric'),
function(x, target, ...){
# call the parent's 'rKINOMO' method to build a standard random KINOMO factorization
res <- callNextMethod()
#Vc# Initialize a random offset of length the number of genes
res@offset <- runif(nrow(res), min=0, max=max(basis(res), coef(res)));
# return the initialized KINOMOOffset object
res
})