-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransforms.R
98 lines (71 loc) · 2.04 KB
/
transforms.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
#' @include KINOMO-class.R
NULL
#'
setGeneric('nneg', function(object, ...) standardGeneric('nneg'))
setMethod('nneg', 'matrix'
, function(object, method=c('pmax', 'posneg', 'absolute', 'min'), threshold=0, shift=TRUE){
# match argument
method <- match.arg(method)
if( !is.numeric(threshold) || length(threshold) != 1L )
stop("nneg - Invalid threshold value in argument `threshold` [",threshold,"]: must be a single numeric value.")
if( threshold < 0 )
stop("nneg - Invalid threshold value in argument `threshold` [",threshold,"]: must be nonnegative.")
# 1. Transform if there is any negative entry
m <- min(object)
if( m < 0 ){
object <-
switch(method
, pmax = pmax(object, 0)
, posneg = rbind(pmax(object, 0), pmax(-object, 0))
, absolute = pmax(abs(object), 0)
, min = object - m
, stop("KINOMO::nneg - Unexpected error: unimplemented transformation method '", method, "'.")
)
}
if( threshold > 0 ){
# 2. Apply threshold if any
object <- pmax(object, threshold)
# 3. Shifting: entries under threshold
if( shift ) object[object<=threshold] <- 0
}
# return modified object
object
}
)
setMethod('nneg', 'KINOMO',
function(object, ...){
basis(object) <- nneg(basis(object), ...)
object
}
)
#'
posneg <- function(...) nneg(..., method='posneg')
#'
setGeneric('rposneg', function(object, ...) standardGeneric('rposneg'))
setMethod('rposneg', 'matrix'
, function(object, unstack=TRUE){
# check that the number of rows is pair
if( nrow(object) %% 2 != 0 )
stop("rposneg - Invalid input matrix: must have a pair number of rows [",nrow(object),"].")
n2 <- nrow(object)
n <- n2/2
if( unstack ) object <- object[1:n,,drop=FALSE] - object[(n+1):n2,,drop=FALSE]
else object[(n+1):n2,] <- - object[(n+1):n2,,drop=FALSE]
# return modified object
object
}
)
setMethod('rposneg', 'KINOMO'
, function(object, ...){
basis(object) <- rposneg(basis(object), ...)
object
}
)
t.KINOMO <- function(x){
# transpose and swap factors
w <- t(basis(x))
.basis(x) <- t(coef(x))
.coef(x) <- w
# return object
x
}