|
a |
|
b/R/utils.R |
|
|
1 |
# from timeOmics |
|
|
2 |
check_matrix <- function(X){ |
|
|
3 |
# add rownames and colnames if absent, cast into matrix |
|
|
4 |
if(!(is.matrix(X) || is.data.frame(X))) return(FALSE) |
|
|
5 |
|
|
|
6 |
if(is.data.frame(X)){ |
|
|
7 |
X <- as.matrix(X) |
|
|
8 |
} |
|
|
9 |
if(is.null(rownames(X))){ |
|
|
10 |
rownames(X) <- seq_len(nrow(X)) |
|
|
11 |
} |
|
|
12 |
if(is.null(colnames(X))){ |
|
|
13 |
colnames(X) <- paste0("V", seq_len(ncol(X))) |
|
|
14 |
} |
|
|
15 |
return(X) |
|
|
16 |
} |
|
|
17 |
|
|
|
18 |
validate_matrix_X <- function(X, var.name = "'X' "){ |
|
|
19 |
# X should be a numeric matrix |
|
|
20 |
X <- check_matrix(X) |
|
|
21 |
if(!is.numeric(X)){ |
|
|
22 |
stop(var.name,"must be a numeric matrix/data.frame") |
|
|
23 |
} |
|
|
24 |
# if(any(!X)) stop("X must be a numeric matrix/data.frame") |
|
|
25 |
return(X) |
|
|
26 |
} |
|
|
27 |
|
|
|
28 |
validate_list_matrix_X <- function(X, var.name = "'X' "){ |
|
|
29 |
if(!is.list(X)){ |
|
|
30 |
stop(var.name, "must be a list of matrix/data.frame") |
|
|
31 |
} |
|
|
32 |
X <- lapply(X, validate_matrix_X) |
|
|
33 |
return(X) |
|
|
34 |
} |
|
|
35 |
|
|
|
36 |
# is_almostInteger <- function (X) |
|
|
37 |
# { |
|
|
38 |
# if (!is.numeric(X) & !is.vector(X)) |
|
|
39 |
# return(FALSE) |
|
|
40 |
# if (length(X) != 1) |
|
|
41 |
# return(FALSE) |
|
|
42 |
# if (!is.finite(X)) |
|
|
43 |
# return(FALSE) |
|
|
44 |
# X.round <- round(X) |
|
|
45 |
# if (X == X.round) |
|
|
46 |
# return(TRUE) |
|
|
47 |
# return(FALSE) |
|
|
48 |
# } |
|
|
49 |
|
|
|
50 |
#' @importFrom methods is |
|
|
51 |
check_getCluster <- function(X){ |
|
|
52 |
if(!(is(X, "cluster.df") || is.null(X))){ |
|
|
53 |
stop("cluster must be NULL or a result from getCluster()") |
|
|
54 |
} |
|
|
55 |
#stopifnot(is(X, "cluster.df") || is.null(X)) |
|
|
56 |
return(X) |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
check_graph <- function(X){ |
|
|
60 |
stopifnot(is(X, "igraph") || |
|
|
61 |
is(X, "grn") || |
|
|
62 |
is.list(X) || |
|
|
63 |
is(X, "list.igraph")) |
|
|
64 |
|
|
|
65 |
if(is(X, "list")){ |
|
|
66 |
stopifnot(all(as.logical(lapply(X, |
|
|
67 |
function(x) {is(x, "igraph") || |
|
|
68 |
is(x, "list.igraph")})))) |
|
|
69 |
class(X) <- c("list.igraph", class(X)) |
|
|
70 |
} |
|
|
71 |
return(X) |
|
|
72 |
} |
|
|
73 |
|
|
|
74 |
check_db <- function(X, var.name = "'db' "){ |
|
|
75 |
# ADD list of db |
|
|
76 |
# x is a dataframe with 2 columns (from, to) or igraph |
|
|
77 |
if(!(is(X, "igraph") || is(X, "data.frame"))){ |
|
|
78 |
stop(var.name, "must be an igraph or data.frame object") |
|
|
79 |
} |
|
|
80 |
if(is(X, "data.frame") & !(all(c("from", "to") %in% colnames(X)))){ |
|
|
81 |
stop(var.name, "must contains the columns 'from' and 'to'") |
|
|
82 |
} |
|
|
83 |
return(X) |
|
|
84 |
} |
|
|
85 |
|
|
|
86 |
#' @importFrom purrr is_empty |
|
|
87 |
#' @importFrom stats na.omit |
|
|
88 |
check_vector_char <- function(X, |
|
|
89 |
X.length = NULL, |
|
|
90 |
default = NULL, |
|
|
91 |
var.name = "'X' "){ |
|
|
92 |
if(is.null(X)){ |
|
|
93 |
return(default) |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
# remove NA |
|
|
97 |
X <- na.omit(X) |
|
|
98 |
if(is_empty(X)){ |
|
|
99 |
return(default) |
|
|
100 |
} else if(!is.character(X)){ |
|
|
101 |
stop(var.name, "must be a charactor vector") |
|
|
102 |
} else if(!is.null(X.length)){ |
|
|
103 |
if(length(X) != X.length){ |
|
|
104 |
stop("invalid length") |
|
|
105 |
} else { # good length |
|
|
106 |
return(X) |
|
|
107 |
} |
|
|
108 |
} else{ |
|
|
109 |
return(X) |
|
|
110 |
} |
|
|
111 |
# return(default) |
|
|
112 |
} |
|
|
113 |
|
|
|
114 |
return_true_false <- function(x, default){ |
|
|
115 |
if(is.logical(x)){ |
|
|
116 |
if(is.finite(x)){ |
|
|
117 |
return(x) |
|
|
118 |
} else { #NA |
|
|
119 |
return(default) |
|
|
120 |
} |
|
|
121 |
} else { |
|
|
122 |
return(default) |
|
|
123 |
} |
|
|
124 |
} |
|
|
125 |
|
|
|
126 |
|
|
|
127 |
check_single_numeric_value <- function(x, |
|
|
128 |
min = NULL, |
|
|
129 |
max = NULL, |
|
|
130 |
var.name = "'r' "){ |
|
|
131 |
if(!is.numeric(x) & !is.matrix(x) & length(x) == 1){ |
|
|
132 |
stop(var.name, "must be a numeric value") |
|
|
133 |
} |
|
|
134 |
if(!is.null(min) & !is.null(max)){ |
|
|
135 |
if(x < min | x > max){ |
|
|
136 |
# internal, no need to check min and max order |
|
|
137 |
stop(var.name, "must be a numeric value between ", min, " and ", max) |
|
|
138 |
} |
|
|
139 |
} |
|
|
140 |
return(x) |
|
|
141 |
} |
|
|
142 |
|
|
|
143 |
check_named_vector <- function(X, var.name = "'X' "){ |
|
|
144 |
if(!(is(X, 'list') | is(X, "atomic"))){ |
|
|
145 |
stop(var.name, "must be a named verctor or list") |
|
|
146 |
} |
|
|
147 |
if(is.null(names(X))){ |
|
|
148 |
stop(var.name, "must be a named verctor or list") |
|
|
149 |
} |
|
|
150 |
return(X) |
|
|
151 |
} |
|
|
152 |
|