|
a |
|
b/R/hummus_objet.R |
|
|
1 |
#' @importFrom methods setClass |
|
|
2 |
#' @importClassesFrom Signac Motif |
|
|
3 |
#' @importClassesFrom SeuratObject Seurat |
|
|
4 |
#' @importClassesFrom TFBSTools PWMatrixList |
|
|
5 |
NULL |
|
|
6 |
|
|
|
7 |
|
|
|
8 |
#' @title Motifs database class |
|
|
9 |
#' |
|
|
10 |
#' @description MotifsDatabase object stores motifs(PFM matrices) |
|
|
11 |
#' and tf2motifs (TF to motifs names mapping) data. |
|
|
12 |
#' |
|
|
13 |
#' @slot motifs (TFBSTools::PWMatrixList) - PFM matrices. |
|
|
14 |
#' @slot tf2motifs (data.frame) - TF to motif names mapping. Columns: motif, tf. |
|
|
15 |
#' |
|
|
16 |
#' @name motifs_db-class |
|
|
17 |
#' @rdname motifs_db-class |
|
|
18 |
#' @exportClass motifs_db |
|
|
19 |
motifs_db <- setClass("motifs_db", |
|
|
20 |
representation( |
|
|
21 |
motifs = "PWMatrixList", |
|
|
22 |
tf2motifs = "data.frame", |
|
|
23 |
tfs = "character" |
|
|
24 |
)) |
|
|
25 |
setMethod("show", "motifs_db", |
|
|
26 |
function(object) { |
|
|
27 |
cat( |
|
|
28 |
paste("Motifs database object with :\n- ", |
|
|
29 |
length(object@motifs), "motifs\n- ", |
|
|
30 |
length(unique(object@tf2motifs$tf)), " TFs\n- ", |
|
|
31 |
nrow(object@tf2motifs), "TF to motif names mapping" |
|
|
32 |
) |
|
|
33 |
) |
|
|
34 |
}) |
|
|
35 |
|
|
|
36 |
|
|
|
37 |
#' @title Multiplex class |
|
|
38 |
#' @description Multiplex object stores a list of networks, a list of features and |
|
|
39 |
#' a list of logicals indicating if the network is directed or weighted. |
|
|
40 |
#' @slot networks (list) - List of networks. |
|
|
41 |
#' @slot features (vector) - Vector of features. |
|
|
42 |
#' @slot directed (list) - List of logical indicating if networks are directed. |
|
|
43 |
#' @slot weighted (list) - List of logical indicating if networks are weighted. |
|
|
44 |
#' |
|
|
45 |
#' @name multiplex-class |
|
|
46 |
#' @rdname multiplex-class |
|
|
47 |
#' @exportClass multiplex |
|
|
48 |
multiplex <- setClass(Class = "multiplex", |
|
|
49 |
slots = c( |
|
|
50 |
"networks" = "list", # List of networks |
|
|
51 |
"features" = "vector", # Vector of features |
|
|
52 |
"directed" = "list", # Logical indicating |
|
|
53 |
# if the network is directed |
|
|
54 |
"weighted" = "list" # Logical indicating |
|
|
55 |
# if the network is weighted |
|
|
56 |
# "network_names" = "vector" # Vector of network names |
|
|
57 |
) |
|
|
58 |
) |
|
|
59 |
|
|
|
60 |
setMethod("show", "multiplex", |
|
|
61 |
function(object) { |
|
|
62 |
cat( |
|
|
63 |
# Reprensentation of the multiplex object |
|
|
64 |
# with the number of networks and features, and the list of network names |
|
|
65 |
paste("Multiplex of ", length(object@networks), |
|
|
66 |
" networks with", length(object@features), "features.\n", |
|
|
67 |
"Networks names: ", paste(names(object@networks), collapse = ", ")) |
|
|
68 |
) |
|
|
69 |
}) |
|
|
70 |
|
|
|
71 |
|
|
|
72 |
#' @title Bipartite class |
|
|
73 |
#' |
|
|
74 |
#' @description Bipartite object stores a bipartite network (edge list) and the names of the |
|
|
75 |
#' left and right features' multiplexes. |
|
|
76 |
#' @slot network (data.frame) - Bipartite network (edge list) |
|
|
77 |
#' @slot multiplex_left (character) - Left features' multiplex |
|
|
78 |
#' @slot multiplex_right (character) - Right features' multiplex |
|
|
79 |
#' |
|
|
80 |
#' @name bipartite-class |
|
|
81 |
#' @rdname bipartite-class |
|
|
82 |
#' @exportClass bipartite |
|
|
83 |
#' |
|
|
84 |
#' @examples bipartite <- bipartite( |
|
|
85 |
#' network = bipartite_network, |
|
|
86 |
#' multiplex_left = "RNA", |
|
|
87 |
#' multiplex_right = "peaks") |
|
|
88 |
#' |
|
|
89 |
bipartite <- setClass(Class = "bipartite", |
|
|
90 |
slots = c( |
|
|
91 |
"network" = "data.frame", # Bipartite network (edge list) |
|
|
92 |
"multiplex_left" = "character", # left features' multiplex |
|
|
93 |
"multiplex_right" = "character" # right features multiplex |
|
|
94 |
) |
|
|
95 |
) |
|
|
96 |
|
|
|
97 |
setMethod("show", "bipartite", |
|
|
98 |
function(object) { |
|
|
99 |
cat( |
|
|
100 |
paste("Bipartite network of ", nrow(object@network), " edges.\n", |
|
|
101 |
"Multiplexes names: ", object@multiplex_left, |
|
|
102 |
" and ", object@multiplex_right, "\n") |
|
|
103 |
) |
|
|
104 |
}) |
|
|
105 |
|
|
|
106 |
#' @title Multilayer class |
|
|
107 |
#' |
|
|
108 |
#' @description Multilayer object stores a list of bipartite networks and a |
|
|
109 |
#' list of multiplex networks. It can also stores a config list to create a |
|
|
110 |
#' yaml file, which is used to parametrize the random walk with restart to |
|
|
111 |
#' explore the multilayer. |
|
|
112 |
#' |
|
|
113 |
#' @slot bipartites (list) - List of bipartite networks |
|
|
114 |
#' @slot multiplex (list) - List of multiplex networks |
|
|
115 |
#' @slot config (list) - List of parameters to parametrize the random walk with |
|
|
116 |
#' restart to explore the multilayer |
|
|
117 |
#' |
|
|
118 |
#' @name multilayer-class |
|
|
119 |
#' @rdname multilayer-class |
|
|
120 |
#' @exportClass multilayer |
|
|
121 |
#' |
|
|
122 |
multilayer <- setClass(Class = "multilayer", |
|
|
123 |
slots = c( |
|
|
124 |
"bipartites" = "list", # Bipartite networks |
|
|
125 |
"multiplex" = "list", # Multiplex networks |
|
|
126 |
"config" = "list" # Parameters to create the hmln |
|
|
127 |
) # representation of a yaml file |
|
|
128 |
) |
|
|
129 |
|
|
|
130 |
setMethod("show", "multilayer", |
|
|
131 |
# Representation of the multilayer object with the number of bipartite and |
|
|
132 |
# multiplex networks, and the list of bipartite names and multiplex names |
|
|
133 |
function(object) { |
|
|
134 |
cat( |
|
|
135 |
paste("Multilayer network containing ", |
|
|
136 |
length(object@bipartites), " bipartite networks and ", |
|
|
137 |
length(object@multiplex), " multiplex networks.\n", |
|
|
138 |
"\n- Multiplex names: ", paste(names(object@multiplex), |
|
|
139 |
collapse = ", "), |
|
|
140 |
"\n- Bipartite names: ", paste(names(object@bipartites), |
|
|
141 |
collapse = ", "), "\n" |
|
|
142 |
) |
|
|
143 |
) |
|
|
144 |
}) |
|
|
145 |
|
|
|
146 |
|
|
|
147 |
#' The Hummus_Object class |
|
|
148 |
#' |
|
|
149 |
#' The Hummus_Object object is an extended \code{Seurat} object |
|
|
150 |
#' for the storage and analysis of a heterogeneous multilayer network |
|
|
151 |
#' |
|
|
152 |
#' @slot multilayer (multilayer) - Multilayer object |
|
|
153 |
#' @slot motifs_db (motifs_db) - Motifs database |
|
|
154 |
#' @slot assay (list) - List of assays |
|
|
155 |
#' |
|
|
156 |
#' @name Hummus_Object-class |
|
|
157 |
#' @rdname Hummus_Object-class |
|
|
158 |
#' @exportClass Hummus_Object |
|
|
159 |
#' @export |
|
|
160 |
#' |
|
|
161 |
Hummus_Object <- setClass( |
|
|
162 |
Class = "Hummus_Object", |
|
|
163 |
slots = list( |
|
|
164 |
"assays" = "list", |
|
|
165 |
"active.assay" = "character", |
|
|
166 |
"multilayer" = "multilayer", |
|
|
167 |
"motifs_db" = "motifs_db" |
|
|
168 |
) |
|
|
169 |
) |
|
|
170 |
|
|
|
171 |
|
|
|
172 |
#' @title Initiate a hummus object |
|
|
173 |
#' |
|
|
174 |
#' @description Initiate a hummus object |
|
|
175 |
#' |
|
|
176 |
#' @param seurat_assays A Seurat object or a list of Seurat assays |
|
|
177 |
#' @param active.assay The name of the active assay. Default: NULL |
|
|
178 |
#' @param multilayer A multilayer object. Default: NULL |
|
|
179 |
#' @param motifs_db A motifs_db object. Default: NULL |
|
|
180 |
#' @return A hummus object |
|
|
181 |
#' @export |
|
|
182 |
#' |
|
|
183 |
#' @examples seurat_object <- Seurat::CreateSeuratObject(counts = matrix(rnorm(1000), nrow = 100, ncol = 10)) |
|
|
184 |
#' hummus <- InitiateHummus_Object(seurat_object) |
|
|
185 |
#' hummus |
|
|
186 |
Initiate_Hummus_Object <- function( |
|
|
187 |
seurat_assays, |
|
|
188 |
active.assay = NULL, |
|
|
189 |
multilayer = NULL, |
|
|
190 |
motifs_db = NULL) { |
|
|
191 |
|
|
|
192 |
# Check if seurat_assays is a Seurat object or a list of Seurat assays |
|
|
193 |
if (inherits(seurat_assays, "Seurat")) { |
|
|
194 |
assays <- seurat_assays@assays |
|
|
195 |
# setup active assay name |
|
|
196 |
active.assay <- seurat_assays@active.assay |
|
|
197 |
} else if (inherits(seurat_assays, "list")) { |
|
|
198 |
assays <- seurat_assays |
|
|
199 |
# setup active assay name |
|
|
200 |
if (is.null(active.assay)) { |
|
|
201 |
active.assay <- names(x = assays)[1] |
|
|
202 |
} else if (!(active.assay %in% names(x = assays))) { |
|
|
203 |
stop("active.assay must be a valid assay name.") |
|
|
204 |
} else { |
|
|
205 |
active.assay <- active.assay |
|
|
206 |
} |
|
|
207 |
} else { |
|
|
208 |
stop("seurat_assays must be a Seurat object or a list of Seurat assays.") |
|
|
209 |
} |
|
|
210 |
|
|
|
211 |
# Check if multilayer is a multilayer object or NULL |
|
|
212 |
if (!inherits(multilayer, "multilayer")) { |
|
|
213 |
if (!is.null(multilayer)) { |
|
|
214 |
stop("multilayer must be a multilayer object or NULL.") |
|
|
215 |
} else { |
|
|
216 |
multilayer <- new("multilayer") |
|
|
217 |
} |
|
|
218 |
} |
|
|
219 |
|
|
|
220 |
# Check if motifs_db is a motifs_db object or NULL |
|
|
221 |
if (!inherits(motifs_db, "motifs_db")) { |
|
|
222 |
if (!is.null(motifs_db)) { |
|
|
223 |
stop("motifs_db must be a motifs_db object or NULL.") |
|
|
224 |
} else { |
|
|
225 |
motifs_db <- new("motifs_db") |
|
|
226 |
} |
|
|
227 |
} |
|
|
228 |
|
|
|
229 |
object <- new( |
|
|
230 |
Class = "Hummus_Object", |
|
|
231 |
assays = assays, |
|
|
232 |
active.assay = active.assay, |
|
|
233 |
multilayer = multilayer, |
|
|
234 |
motifs_db = motifs_db |
|
|
235 |
) |
|
|
236 |
|
|
|
237 |
return(object) |
|
|
238 |
} |
|
|
239 |
|
|
|
240 |
|
|
|
241 |
#' @title Get Default assays of Hummus_Object (based on Seurat) |
|
|
242 |
#' @name DefaultAssay |
|
|
243 |
#' @export |
|
|
244 |
#' |
|
|
245 |
#' @examples |
|
|
246 |
#' # Get current default assay |
|
|
247 |
#' DefaultAssay(object = pbmc_small) |
|
|
248 |
#' |
|
|
249 |
DefaultAssay.Hummus_Object <- function(object, ...) { |
|
|
250 |
SeuratObject::CheckDots(...) |
|
|
251 |
default <- slot(object = object, name = 'active.assay') |
|
|
252 |
if (!length(x = default)) { |
|
|
253 |
default <- NULL |
|
|
254 |
} |
|
|
255 |
return(default) |
|
|
256 |
} |
|
|
257 |
|
|
|
258 |
#' Default Assay |
|
|
259 |
#' |
|
|
260 |
#' Get and set the default assay |
|
|
261 |
#' |
|
|
262 |
#' @param object An object |
|
|
263 |
#' |
|
|
264 |
#' @return \code{DefaultAssay}: The name of the default assay |
|
|
265 |
#' |
|
|
266 |
#' @rdname DefaultAssay |
|
|
267 |
#' @export DefaultAssay |
|
|
268 |
#' |
|
|
269 |
#' @concept data-access |
|
|
270 |
#' |
|
|
271 |
DefaultAssay <- function(object, ...) { |
|
|
272 |
UseMethod(generic = 'DefaultAssay', object = object) |
|
|
273 |
} |
|
|
274 |
|
|
|
275 |
#' @param value Name of assay to set as default |
|
|
276 |
#' |
|
|
277 |
#' @return \code{DefaultAssay<-}: An object with the default assay updated |
|
|
278 |
#' |
|
|
279 |
#' @rdname DefaultAssay |
|
|
280 |
#' @export DefaultAssay<- |
|
|
281 |
#' |
|
|
282 |
"DefaultAssay<-" <- function(object, ..., value) { |
|
|
283 |
UseMethod(generic = 'DefaultAssay<-', object = object) |
|
|
284 |
} |
|
|
285 |
|
|
|
286 |
|
|
|
287 |
#' @title Variable features of assays in Hummus_Object (based on Seurat) |
|
|
288 |
#' @name VariableFeatures |
|
|
289 |
#' @export |
|
|
290 |
#' |
|
|
291 |
VariableFeatures.Hummus_Object <- function( |
|
|
292 |
object, |
|
|
293 |
method = NULL, |
|
|
294 |
assay = NULL, |
|
|
295 |
nfeatures = NULL, |
|
|
296 |
layer = NA, |
|
|
297 |
simplify = TRUE, |
|
|
298 |
selection.method = lifecycle::deprecated(), |
|
|
299 |
... |
|
|
300 |
) { |
|
|
301 |
SeuratObject::CheckDots(...) |
|
|
302 |
if (lifecycle::is_present(arg = selection.method)) { |
|
|
303 |
SeuratObject.Deprecate( |
|
|
304 |
when = '5.0.0', |
|
|
305 |
what = 'VariableFeatures(selection.method = )', |
|
|
306 |
with = 'VariableFeatures(method = )' |
|
|
307 |
) |
|
|
308 |
method <- selection.method |
|
|
309 |
} |
|
|
310 |
assay <- assay %||% SeuratObject::DefaultAssay(object = object) |
|
|
311 |
return(SeuratObject::VariableFeatures( |
|
|
312 |
object = object[[assay]], |
|
|
313 |
method = method, |
|
|
314 |
nfeatures = nfeatures, |
|
|
315 |
layer = layer, |
|
|
316 |
simplify = simplify, |
|
|
317 |
... |
|
|
318 |
)) |
|
|
319 |
} |
|
|
320 |
#' @return \code{VariableFeatures}: a vector of the variable features |
|
|
321 |
#' |
|
|
322 |
#' @rdname VariableFeatures |
|
|
323 |
#' @export VariableFeatures |
|
|
324 |
#' |
|
|
325 |
#' |
|
|
326 |
VariableFeatures <- function(object, method = NULL, ...) { |
|
|
327 |
UseMethod(generic = 'VariableFeatures', object = object) |
|
|
328 |
} |
|
|
329 |
|
|
|
330 |
#' @param value A character vector of variable features |
|
|
331 |
#' |
|
|
332 |
#' @rdname VariableFeatures |
|
|
333 |
#' @export VariableFeatures<- |
|
|
334 |
#' |
|
|
335 |
"VariableFeatures<-" <- function(object, ..., value) { |
|
|
336 |
UseMethod(generic = 'VariableFeatures<-', object = object) |
|
|
337 |
} |
|
|
338 |
|
|
|
339 |
|
|
|
340 |
#' @title Access assays in Hummus_Object (based on Seurat) |
|
|
341 |
#' @method [[ Hummus_Object |
|
|
342 |
#' @name [[<-,Hummus_Object |
|
|
343 |
#' @export |
|
|
344 |
#' @aliases [[<-.Hummus_Object \S4method{[[<-}{Hummus_Object,character,missing,Assay} |
|
|
345 |
#' |
|
|
346 |
"[[.Hummus_Object" <- function(x, i = missing_arg(), ..., drop = FALSE, na.rm = FALSE) { |
|
|
347 |
md <- slot(object = x, name = 'assays') |
|
|
348 |
if (rlang::is_missing(x = i)) { |
|
|
349 |
return(md) |
|
|
350 |
} else if (is.null(x = i)) { |
|
|
351 |
return(NULL) |
|
|
352 |
} else if (!length(x = i)) { |
|
|
353 |
return(data.frame(row.names = row.names(x = md))) |
|
|
354 |
} |
|
|
355 |
# Correct invalid `i` |
|
|
356 |
meta.cols <- names(x = md) |
|
|
357 |
if (rlang::is_bare_integerish(x = i)) { |
|
|
358 |
if (all(i > length(x = meta.cols))) { |
|
|
359 |
abort(message = paste( |
|
|
360 |
"Invalid integer indexing:", |
|
|
361 |
"all integers greater than the number of meta columns" |
|
|
362 |
)) |
|
|
363 |
} |
|
|
364 |
i <- meta.cols[as.integer(x = i[i <= length(x = meta.cols)])] |
|
|
365 |
} |
|
|
366 |
if (!is.character(x = i)) { |
|
|
367 |
abort(message = "'i' must be a character vector") |
|
|
368 |
} |
|
|
369 |
# Determine if we're pulling cell-level meta data |
|
|
370 |
# or a sub-object |
|
|
371 |
slot.use <- if (length(x = i) == 1L) { |
|
|
372 |
SeuratObject::.FindObject(object = x, name = i) |
|
|
373 |
} else { |
|
|
374 |
NULL |
|
|
375 |
} |
|
|
376 |
# Pull cell-level meta data |
|
|
377 |
if (is.null(x = slot.use)) { |
|
|
378 |
i <- tryCatch( |
|
|
379 |
expr = arg_match(arg = i, values = meta.cols, multiple = TRUE), |
|
|
380 |
error = function(e) { |
|
|
381 |
#error message that indicates which colnames not found |
|
|
382 |
abort( |
|
|
383 |
message = paste( |
|
|
384 |
paste(sQuote(x = setdiff(x = i, y = meta.cols)), collapse = ', '), |
|
|
385 |
"not found in this HuMMuS object\n", |
|
|
386 |
e$body |
|
|
387 |
), |
|
|
388 |
call = rlang::caller_env(n = 4L) |
|
|
389 |
) |
|
|
390 |
} |
|
|
391 |
) |
|
|
392 |
# Pull the cell-level meta data |
|
|
393 |
data.return <- md[, i, drop = FALSE, ...] |
|
|
394 |
# If requested, remove NAs |
|
|
395 |
if (isTRUE(x = na.rm)) { |
|
|
396 |
idx.na <- apply(X = is.na(x = data.return), MARGIN = 1L, FUN = all) |
|
|
397 |
data.return <- data.return[!idx.na, , drop = FALSE] |
|
|
398 |
} else { |
|
|
399 |
idx.na <- rep_len(x = FALSE, length.out = ncol(x = x)) |
|
|
400 |
} |
|
|
401 |
# If requested, coerce to a vector |
|
|
402 |
if (isTRUE(x = drop)) { |
|
|
403 |
data.return <- unlist(x = data.return, use.names = FALSE) |
|
|
404 |
names(x = data.return) <- rep.int( |
|
|
405 |
x = colnames(x = x)[!idx.na], |
|
|
406 |
times = length(x = i) |
|
|
407 |
) |
|
|
408 |
} |
|
|
409 |
return(data.return) |
|
|
410 |
} |
|
|
411 |
# Pull a sub-object |
|
|
412 |
return(slot(object = x, name = slot.use)[[i]]) |
|
|
413 |
} |
|
|
414 |
|
|
|
415 |
|
|
|
416 |
setMethod("show", "Hummus_Object", |
|
|
417 |
function(object) { |
|
|
418 |
#object <- SeuratObject::UpdateSlots(object = object) |
|
|
419 |
assays <- SeuratObject::.FilterObjects(object = object, |
|
|
420 |
classes.keep = "Assay") |
|
|
421 |
nfeatures <- sum(vapply( |
|
|
422 |
X = assays, |
|
|
423 |
FUN = function(x) { |
|
|
424 |
return(nrow(x = object[[x]])) |
|
|
425 |
}, |
|
|
426 |
FUN.VALUE = integer(length = 1L) |
|
|
427 |
)) |
|
|
428 |
num.assays <- length(x = assays) |
|
|
429 |
|
|
|
430 |
cat("Hummus object containing a multilayer object :\n") |
|
|
431 |
show(object@multilayer) |
|
|
432 |
cat('\n\nAnd a Seurat object :\n\n') |
|
|
433 |
cat( |
|
|
434 |
nfeatures, |
|
|
435 |
"features across", |
|
|
436 |
ncol(x = object), |
|
|
437 |
"samples within", |
|
|
438 |
num.assays, |
|
|
439 |
ifelse(test = num.assays == 1, yes = "assay", no = "assays"), |
|
|
440 |
"\n" |
|
|
441 |
) |
|
|
442 |
cat( |
|
|
443 |
"Active assay:", |
|
|
444 |
SeuratObject::DefaultAssay(object = object), |
|
|
445 |
paste0('(', nrow(x = object), " features, ", |
|
|
446 |
length(x = SeuratObject::VariableFeatures(object = object))," variable features)") |
|
|
447 |
) |
|
|
448 |
other.assays <- assays[assays != SeuratObject::DefaultAssay(object = object)] |
|
|
449 |
if (length(x = other.assays) > 0) { |
|
|
450 |
cat( |
|
|
451 |
'\n', |
|
|
452 |
length(x = other.assays), |
|
|
453 |
'other', |
|
|
454 |
ifelse(test = length(x = other.assays) == 1, yes = 'assay', no = 'assays'), |
|
|
455 |
'present:', |
|
|
456 |
strwrap(x = paste(other.assays, collapse = ', ')) |
|
|
457 |
) |
|
|
458 |
} |
|
|
459 |
reductions <- SeuratObject::.FilterObjects(object = object, classes.keep = 'DimReduc') |
|
|
460 |
if (length(x = reductions) > 0) { |
|
|
461 |
cat( |
|
|
462 |
'\n', |
|
|
463 |
length(x = reductions), |
|
|
464 |
'dimensional', |
|
|
465 |
ifelse(test = length(x = reductions) == 1, yes = 'reduction', no = 'reductions'), |
|
|
466 |
'calculated:', |
|
|
467 |
strwrap(x = paste(reductions, collapse = ', ')) |
|
|
468 |
) |
|
|
469 |
} |
|
|
470 |
fovs <- SeuratObject::.FilterObjects(object = object, classes.keep = 'FOV') |
|
|
471 |
if (length(x = fovs)) { |
|
|
472 |
cat( |
|
|
473 |
'\n', |
|
|
474 |
length(x = fovs), |
|
|
475 |
'spatial', |
|
|
476 |
ifelse(test = length(x = fovs) == 1L, yes = 'field', no = 'fields'), |
|
|
477 |
'of view present:', |
|
|
478 |
strwrap(x = paste(fovs, sep = ', ')) |
|
|
479 |
) |
|
|
480 |
} |
|
|
481 |
images <- SeuratObject::.FilterObjects(object = object, classes.keep = 'SpatialImage') |
|
|
482 |
images <- setdiff(x = images, y = fovs) |
|
|
483 |
if (length(x = images)) { |
|
|
484 |
cat( |
|
|
485 |
'\n', |
|
|
486 |
length(x = images), |
|
|
487 |
ifelse(test = length(x = images) == 1L, yes = 'image', no = 'images'), |
|
|
488 |
'present:', |
|
|
489 |
strwrap(x = paste(images, collapse = ', ')) |
|
|
490 |
) |
|
|
491 |
} |
|
|
492 |
cat('\n') |
|
|
493 |
} |
|
|
494 |
) |
|
|
495 |
|
|
|
496 |
|
|
|
497 |
#' @title Save multilayer object files in a hierarchical structure on disk |
|
|
498 |
#' |
|
|
499 |
#' @description Save multilayer files from a Hummus_Object |
|
|
500 |
#' in a hierarchical structure on disk, inside a folder specified through |
|
|
501 |
#' folder_name |
|
|
502 |
#' |
|
|
503 |
#' @param hummus A hummus object |
|
|
504 |
#' @param folder_name The name of the folder to save the multilayer |
|
|
505 |
#' @param verbose (integer) - Display function messages. Set to 0 for no |
|
|
506 |
#' message displayed, >= 1 for more details. |
|
|
507 |
#' @param suffix The suffix of the files to save. Default: ".tsv" |
|
|
508 |
#' |
|
|
509 |
#' @return Nothing, but create a folder containing the multilayer object files |
|
|
510 |
#' @export |
|
|
511 |
#' |
|
|
512 |
#' @examples folder_name = "multilayer" |
|
|
513 |
#' save_multilayer(hummus = hummus, folder_name = "multilayer") |
|
|
514 |
#' |
|
|
515 |
save_multilayer <- function( |
|
|
516 |
hummus, |
|
|
517 |
folder_name, |
|
|
518 |
verbose = TRUE, |
|
|
519 |
suffix = ".tsv" |
|
|
520 |
) { |
|
|
521 |
|
|
|
522 |
multiplex_folder <- "multiplex" |
|
|
523 |
bipartite_folder <- "bipartite" |
|
|
524 |
seed_folder <- "seed" |
|
|
525 |
config_folder <- "config" |
|
|
526 |
|
|
|
527 |
dir.create(folder_name) |
|
|
528 |
dir.create(paste0(folder_name, "/", multiplex_folder)) |
|
|
529 |
dir.create(paste0(folder_name, "/", bipartite_folder)) |
|
|
530 |
dir.create(paste0(folder_name, "/", seed_folder)) |
|
|
531 |
dir.create(paste0(folder_name, "/", config_folder)) |
|
|
532 |
|
|
|
533 |
# For each multiplex, create a subfolder of multiplex, |
|
|
534 |
# and save its networks inside |
|
|
535 |
for (multiplex_name in names(hummus@multilayer@multiplex)){ |
|
|
536 |
dir.create(paste0(folder_name, "/", multiplex_folder, "/", multiplex_name)) |
|
|
537 |
print(hummus@multilayer@multiplex[[multiplex_name]]) |
|
|
538 |
for (network_name in names(hummus@multilayer@multiplex[[multiplex_name]]@networks)){ |
|
|
539 |
print(paste(multiplex_name, network_name)) |
|
|
540 |
write.table(hummus@multilayer@multiplex[[multiplex_name]]@networks[[network_name]], |
|
|
541 |
col.names = FALSE, row.names = FALSE, quote = FALSE, sep = "\t", |
|
|
542 |
file = paste0(folder_name, "/", |
|
|
543 |
multiplex_folder, "/", |
|
|
544 |
multiplex_name, "/", network_name, suffix)) |
|
|
545 |
} |
|
|
546 |
} |
|
|
547 |
# save bipartite networks |
|
|
548 |
for (bipartite in names(hummus@multilayer@bipartites)){ |
|
|
549 |
write.table(hummus@multilayer@bipartites[[bipartite]]@network, sep = "\t", |
|
|
550 |
col.names = FALSE, row.names = FALSE, quote = FALSE, |
|
|
551 |
file = paste0(folder_name, "/", |
|
|
552 |
bipartite_folder, "/", |
|
|
553 |
bipartite, ".tsv")) |
|
|
554 |
} |
|
|
555 |
} |
|
|
556 |
|
|
|
557 |
|
|
|
558 |
#' @title Add a network to a multiplex, a multilayer or an hummus object |
|
|
559 |
#' |
|
|
560 |
#' @description Add a network to a multiplex, a multilayer or an hummus object |
|
|
561 |
#' |
|
|
562 |
#' @param object A multiplex, a multilayer or an hummus object |
|
|
563 |
#' @param network A network (edge list) |
|
|
564 |
#' @param network_name The name of the network |
|
|
565 |
#' @param multiplex_name The name of the multiplex. Default: NULL if object is a |
|
|
566 |
#' multiplex already only |
|
|
567 |
#' @param directed Logical indicating if the network is directed. Default: FALSE |
|
|
568 |
#' @param weighted Logical indicating if the network is weighted. Default: FALSE |
|
|
569 |
#' @param verbose (integer) - Display function messages. Set to 0 for no |
|
|
570 |
#' message displayed, >= 1 for more details. |
|
|
571 |
#' |
|
|
572 |
#' @return A multiplex, a multilayer or an hummus object with the added network |
|
|
573 |
#' @export |
|
|
574 |
#' |
|
|
575 |
#' @examples hummus <- add_network( |
|
|
576 |
#' object = hummus, |
|
|
577 |
#' network = atac_peak_network, |
|
|
578 |
#' network_name = network_name, |
|
|
579 |
#' multiplex_name = multiplex_name, |
|
|
580 |
#' weighted = TRUE, |
|
|
581 |
#' directed = FALSE) |
|
|
582 |
#' |
|
|
583 |
add_network <- function( |
|
|
584 |
object, |
|
|
585 |
network, |
|
|
586 |
network_name, |
|
|
587 |
multiplex_name = NULL, |
|
|
588 |
directed = FALSE, |
|
|
589 |
weighted = FALSE, |
|
|
590 |
verbose = 1) { |
|
|
591 |
|
|
|
592 |
# Check if object is a multiplex, a multilayer or an hummus object |
|
|
593 |
if (inherits(object, "multiplex")) { |
|
|
594 |
multiplex <- object |
|
|
595 |
} else if (inherits(object, "multilayer") ) { |
|
|
596 |
# Check if multiplex_name is NULL |
|
|
597 |
if (is.null(multiplex_name)) { |
|
|
598 |
stop("You need to specify the multiplex name.") |
|
|
599 |
} |
|
|
600 |
# Check if multiplex_name already exists |
|
|
601 |
if (!(multiplex_name %in% names(object@multiplex))) { |
|
|
602 |
if (verbose > 0) { |
|
|
603 |
cat("\tCreating new multiplex : ", multiplex_name, "\n") |
|
|
604 |
} |
|
|
605 |
# Create new multiplex if not |
|
|
606 |
object@multiplex[[multiplex_name]] <- new("multiplex") |
|
|
607 |
} |
|
|
608 |
# Get working multiplex |
|
|
609 |
multiplex <- object@multiplex[[multiplex_name]] |
|
|
610 |
} else if (inherits(object, "Hummus_Object")) { |
|
|
611 |
# Check if multiplex_name is NULL |
|
|
612 |
if (is.null(multiplex_name)) { |
|
|
613 |
stop("You need to specify the multiplex name.") |
|
|
614 |
} |
|
|
615 |
# Check if multiplex_name already exists |
|
|
616 |
if (!(multiplex_name %in% names(object@multilayer@multiplex))) { |
|
|
617 |
if (verbose > 0) { |
|
|
618 |
cat("\tCreating new multiplex : ", multiplex_name, "\n") |
|
|
619 |
} |
|
|
620 |
# Create new multiplex if not |
|
|
621 |
object@multilayer@multiplex[[multiplex_name]] <- new("multiplex") |
|
|
622 |
} |
|
|
623 |
# Get working multiplex |
|
|
624 |
multiplex <- object@multilayer@multiplex[[multiplex_name]] |
|
|
625 |
|
|
|
626 |
} else { |
|
|
627 |
stop("Object is not a multiplex, a multilayer nor an hummus object.: ", class(object)) |
|
|
628 |
} |
|
|
629 |
|
|
|
630 |
# Check if network name already exists in the multiplex |
|
|
631 |
if (network_name %in% names(multiplex@networks)) { |
|
|
632 |
stop("Network name already exists in the multiplex.") |
|
|
633 |
} |
|
|
634 |
|
|
|
635 |
# Check if there is features in common |
|
|
636 |
features <- unique(c(unique(network[, 1]), unique(network[, 2]))) |
|
|
637 |
if (length(intersect(features, multiplex@features)) == 0 |
|
|
638 |
&& length(multiplex@features) != 0) { |
|
|
639 |
stop(cat("There is no features in common.", |
|
|
640 |
"Check if there is a mistake in the features names", |
|
|
641 |
" or if you want to create a new multiplex instead.")) |
|
|
642 |
} |
|
|
643 |
|
|
|
644 |
# Add network |
|
|
645 |
multiplex@networks[[network_name]] <- network |
|
|
646 |
multiplex@features <- unique(c(multiplex@features, features)) |
|
|
647 |
multiplex@directed[[network_name]] <- directed |
|
|
648 |
multiplex@weighted[[network_name]] <- weighted |
|
|
649 |
|
|
|
650 |
# Return object |
|
|
651 |
if (inherits(object, "multiplex")) { |
|
|
652 |
return(multiplex) |
|
|
653 |
} else if (inherits(object, "multilayer")) { |
|
|
654 |
object@multiplex[[multiplex_name]] <- multiplex |
|
|
655 |
return(object) |
|
|
656 |
} else if (inherits(object, "Hummus_Object")) { |
|
|
657 |
object@multilayer@multiplex[[multiplex_name]] <- multiplex |
|
|
658 |
return(object) |
|
|
659 |
} |
|
|
660 |
} |
|
|
661 |
|
|
|
662 |
|
|
|
663 |
#' @title Wrapper function to save a network or not |
|
|
664 |
#' |
|
|
665 |
#' @description Wrapper function to save a network or not in a file according |
|
|
666 |
#' to the store_network parameter. If store_network is TRUE, the network is |
|
|
667 |
#' saved in the output_file. |
|
|
668 |
#' |
|
|
669 |
#' @param network A network (edge list) |
|
|
670 |
#' @param store_network Logical indicating if the network should be saved |
|
|
671 |
#' @param output_file The name of the file to save the network |
|
|
672 |
#' @param verbose (integer) - Display function messages. Set to 0 for no |
|
|
673 |
#' message displayed, >= 1 for more details. |
|
|
674 |
#' |
|
|
675 |
#' @return Nothing, but save the network in a file if store_network is TRUE |
|
|
676 |
#' @export |
|
|
677 |
#' |
|
|
678 |
#' @examples network <- read.table("network.tsv", header = TRUE, sep = "\t") |
|
|
679 |
#' store_network(network = network, |
|
|
680 |
#' store_network = TRUE, |
|
|
681 |
#' output_file = "network.tsv", |
|
|
682 |
#' verbose = 1) |
|
|
683 |
#' |
|
|
684 |
store_network <- function( |
|
|
685 |
network, |
|
|
686 |
store_network, |
|
|
687 |
output_file, |
|
|
688 |
verbose = 1) { |
|
|
689 |
|
|
|
690 |
if (store_network) { |
|
|
691 |
if (is.null(output_file)) { |
|
|
692 |
stop("Please provide an output file name", |
|
|
693 |
" if you want to store the network.") |
|
|
694 |
} |
|
|
695 |
if (verbose > 0) { |
|
|
696 |
cat("\tStoring network in file : ", output_file, "\n") |
|
|
697 |
} |
|
|
698 |
write.table(network, |
|
|
699 |
output_file, |
|
|
700 |
col.names = TRUE, |
|
|
701 |
row.names = FALSE, |
|
|
702 |
quote = FALSE, |
|
|
703 |
sep = "\t") |
|
|
704 |
} |
|
|
705 |
} |