[9abfcf]: / R / layers.R

Download this file

347 lines (329 with data), 13.4 kB

  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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#' Compute TF network and add it to hummus object
#'
#' Compute a protein-protein interaction layer from Omnipath request that will represent tf cooperativity.
#' This network is the top-layer of HuMMuS multilayer.
#'
#' @param hummus (Hummus_Object) - Hummus object
#' @param organism (integer) - Specie identifier from Omnipath to fetch
#' specific interactions
#' @param tfs vector(character) - List of tfs consider. If NA, tfs are extracted
#' from the hummus object with get_tfs function.
#' @param gene_assay (character) - Name of the assay to get tfs from if tfs is
#' not provided. If NULL, all TFs with motifs in the hummus object are used.
#' @param method (character) - Method used to infer network edges.
#' * \code{'Omnipath'} - Use Omnipath to infer tf-tf networks.
#' * \code{'NULL'} - A fake connected network is computed.
#' * \code{'Other method'} - TO DO.
#' @param store_network (bool) - Save the network directly (\code{TRUE},
#' default) or return without saving on disk (\code{FALSE}).
#' @param output_file (character) - Name of the output_file
#' (if store_network == \code{TRUE}).
#' @param source_target ('AND'|'OR') - Fetch only the interactions involving
#' two considered tfs (\code{'AND', default}), or one considered tfs and any
#' other element (\code{'OR'})
#' @param multiplex_name (character) - Name of the multiplex to add the network
#' to. Default is \code{'TF'}.
#' @param tf_network_name (character) - Name of the network in the multiplex to
#' add the network to. Default is \code{'TF_network'}.
#' @param verbose (integer) - Display function messages. Set to 0 for no message
#' displayed, >= 1 for more details.
#'
#' @return (Hummus_Object) - Return hummus object with the new network added.
#' @export
#'
#' @examples hummus <- compute_tf_network(hummus,
#' gene_assay = "RNA",
#' verbose = 1)
compute_tf_network <- function(
hummus, # Hummus object
organism = 9606, # Human by default
tfs = NA, # List of tfs considered.
gene_assay = NULL, # Name of the assay to get tfs from
# if tfs is not provided
method = NULL, # Method used to infer network edges.
# * 'Omnipath' - Use Omnipath to infer tf-tf networks.
# * 'NULL' - A fake connected network is computed.
# * 'Other method' - TO DO.
store_network = FALSE, # Save the network on disk (TRUE, default)
output_file = NULL, # Name of the output_file (if store_network == TRUE)
source_target = "AND", # 'AND' | 'OR'
multiplex_name = "TF", # Name of the multiplex to add the network to
tf_network_name = "TF_network", # Name of the network in the multiplex
verbose = 1
) {
a <- Sys.time()
# Check if method is implemented
if (is.null(method)) {
tf_network <- run_tf_null_wrapper(
hummus = hummus,
organism = organism,
tfs = tfs,
gene_assay = gene_assay,
verbose)
} else if (method == "Omnipath") {
if (!requireNamespace("OmnipathR", quietly = TRUE)) {
stop("Please install Omnipath.\n",
"github.com/saezlab/OmnipathR")
} else {
# infer network with cicero
tf_network <- run_omnipath_wrapper(
hummus = hummus,
organism = organism,
tfs = tfs,
gene_assay = gene_assay,
source_target = source_target,
verbose = verbose)
}
} else {
stop(cat("Method not implemented yet, choose between Omnipath and NULL..",
"that's it for now.\n But you can always compute the network",
"independently and add it to the hummus object manually !"))
}
if (verbose > 0) {
cat("TF network construction time:", Sys.time() - a)
}
# Save gene network
store_network(network = tf_network,
store_network = store_network,
output_file = output_file,
verbose = verbose)
# Add network to hummus object
hummus <- add_network(hummus,
multiplex_name = multiplex_name,
network = tf_network,
network_name = tf_network_name,
weighted = FALSE, # PPI could be weighted,
# could be added later
directed = FALSE, # PPI are not directed
verbose = verbose)
return(hummus)
}
#' Compute gene netwok from scRNA-seq data
#'
#' This function will create a network from rna data (or in theory any data
#' wtih genes as features).
#' Different method should be implemented at some point (any suggestion is welcomed ! :) ),
#' for now Genie3 is still the reference and only method available
#'
#' Method descriptions :
#' 1. Genie3
#' Use tree random forest to infer regulatory networks :
#' https://bioconductor.org/packages/release/bioc/html/GENIE3.html
#'
#' @param hummus (Hummus_Object) - Hummus object
#' @param gene_assay (character) - Name of the assay containing the gene
#' expression data.
#' @param tfs vector(character) - List of tfs considered. If NULL, all TFs with
#' motifs in the hummus object are used.
#' @param method (character) - Method used to infer network edges.
#' * \code{'Genie3'} - Use tree random forest to infer regulatory networks.
#' * \code{'Other method'} - TO DO.
#' @param multiplex_name (character) - Name of the multiplex to add the network
#' to. Default is \code{'RNA'}.
#' @param network_name (character) - Name of the network in the multiplex to
#' add the network to. Default is \code{'RNA_network'}.
#' @param store_network (bool) - Save the network directly (\code{TRUE},
#' default) or return without saving on disk (\code{FALSE}).
#' @param output_file (character) - Name of the output_file
#' (if store_network == \code{TRUE}).
#' @param threshold (interger, default 0) - Minimal threshold
#' to select tf-gene edges.
#' @param number_cores (interger, default 1) - Number of thread that should be
#' used for the parallelizable methods.
#' @param verbose (integer) - Display function messages. Set to 0 for no
#' message displayed, >= 1 for more details.
#'
#' @return (data.frame) - Return list of network interactions between genes
#' @export
#'
#' @examples hummus <- compute_gene_network(
#' hummus,
#' gene_assay = "RNA",
#' method = "GENIE3",
#' verbose = 1,
#' number_cores = 8,
#' store_network = FALSE)
#'
compute_gene_network <- function(
hummus,
gene_assay = "RNA",
tfs = NULL,
method = "GENIE3",
multiplex_name = NULL,
network_name = NULL,
store_network = FALSE,
output_file = NULL,
threshold = 0.0,
number_cores = 1,
verbose = 1
) {
a <- Sys.time()
# Check if method is implemented
if (method == "GENIE3") {
if (verbose > 0) {
cat("Computing gene network with ", method, " ...\n")
}
# Get tfs list
if (verbose > 0 && is.null(tfs)) {
cat("\tNo TFs list provided, fetching from hummus object...\n")
tfs <- get_tfs(hummus = hummus,
assay = gene_assay,
store_tfs = FALSE,
output_file = NULL,
verbose = verbose)
}
# infer network
weightMat <- GENIE3::GENIE3(
as.matrix(hummus@assays[[gene_assay]]$counts),
regulators = tfs,
nCores = number_cores)
# get edge list
linkList <- GENIE3::getLinkList(weightMat)
gene_network <- linkList[which(linkList$weight > threshold), ]
# TODO : add other methods
} else {
stop(cat("Method not implemented yet, choose between GENIE3 and..",
"that's it for now.\n but you can always compute the network",
"independently and add it to the hummus object."))
}
if (verbose > 0) {
cat("\tGene network construction time:", Sys.time() - a, "\n")
}
# Save gene network
store_network(network = gene_network,
store_network = store_network,
output_file = output_file,
verbose = verbose)
# If no multiplex name provided, use assay name
if (is.null(multiplex_name)) {
multiplex_name <- gene_assay
}
# If no network name provided, use method name + assay name
if (is.null(network_name)) {
network_name <- paste(multiplex_name, method, sep = "_")
}
# Add network to hummus object
hummus <- add_network(hummus,
multiplex_name = multiplex_name,
network = gene_network,
network_name = network_name,
weighted = TRUE,
directed = FALSE,
verbose = verbose)
# Return hummus object
return(hummus)
}
#' Compute peak network from scATAC-seq data
#'
#' This function will create a network from atac data (or in theory any data
#' wtih peaks coordinates as features).
#' Different method should be implemented at some point (e.g. RENIN),
#' for now Cicero is still the reference and only method available
#'
#' Method descriptions :
#' 1. Cicero
#' Use patial corelation between peaks that are in a given window (e.g. :
#' less distant than 500K base pairs)
#'
#' @param hummus (Hummus_Object) - Hummus object
#' @param atac_assay (character) - Name of the assay containing the atac
#' peaks data.
#' @param genome (BSgenome) - Genome used to compute the distance between peaks.
#' @param method (character) - Method used to infer network edges.
#' * \code{'cicero'} - Use cicero to infer regulatory networks.
#' * \code{'Other method'} - TO DO.
#' @param multiplex_name (character) - Name of the multiplex to add the network
#' to. Default is \code{'peaks'}.
#' @param network_name (character) - Name of the network in the multiplex to
#' add the network to. Default is \code{'peak_network'}.
#' @param store_network (bool) - Save the network directly (\code{TRUE},
#' default) or return without saving on disk (\code{FALSE}).
#' @param output_file (character) - Name of the output_file
#' (if store_network == \code{TRUE}).
#' @param threshold (interger, default 0) - Minimal threshold to select tf-gene
#' edges.
#' @param number_cells_per_clusters (integer) - Number of cells grouped by
#' territory to define pseudocells
#' @param sample_num (integer | Cicero) - Number of pseudocells to sample from
#' each territory. Default is 100.
#' @param seed (integer | Cicero) - Seed used to sample pseudocells. Default is
#' 2025
#' @param verbose (integer) - Display function messages. Set to 0 for no
#' message displayed, >= 1 for more details.
#' @param window (integer) - Size of window to consider potential
#' cis-regulatory cooperations between peaks. Default is 500K base pairs.
#' @param reduction_method (character | Cicero) - Method used to reduce dimensionality
#' of the data to identify territories. Default is \code{'UMAP'}.
#'
#' @return (data.frame) - Return list of network interactions between peaks
#' @export
#'
#' @examples hummus <- compute_atac_peak_network(hummus)
#'
compute_atac_peak_network <- function(
hummus,
atac_assay = "peaks",
genome = BSgenome.Hsapiens.UCSC.hg38::BSgenome.Hsapiens.UCSC.hg38,
method = "cicero",
multiplex_name = NULL,
network_name = NULL,
store_network = FALSE,
output_file = NULL,
threshold = 0.0,
number_cells_per_clusters = 50,
sample_num = 100,
seed = 2025,
verbose = 1,
window = 5e+05,
reduction_method = "UMAP") {
a <- Sys.time()
# Check if method is implemented
if (method == "cicero") {
if (!requireNamespace("cicero", quietly = TRUE)) {
stop("Please install cicero.\n",
"https://cole-trapnell-lab.github.io/cicero-release/docs_m3/")
} else {
# infer network with cicero
atac_peak_network <- run_cicero_wrapper(
hummus,
atac_assay,
genome,
window,
number_cells_per_clusters,
sample_num,
seed,
verbose,
threshold,
reduction_method)
}
} else {
stop(cat("Method not implemented yet, choose between Cicero and..",
"that's it for now.\n but you can always compute the network",
"independently and add it to the hummus object manually."))
}
if (verbose > 0) {
cat("Peak network construction time:", Sys.time() - a)
}
# Save peak network
store_network(network = atac_peak_network,
store_network = store_network,
output_file = output_file,
verbose = verbose)
# If no multiplex name provided, use assay name
if (is.null(multiplex_name)) {
multiplex_name <- atac_assay
}
# If no network name provided, use method name + assay name
if (is.null(network_name)) {
network_name <- paste0("peak_network_", method)
}
# Add network to hummus object
hummus <- add_network(
object = hummus,
network = atac_peak_network,
network_name = network_name,
multiplex_name = multiplex_name,
weighted = TRUE,
directed = FALSE,
verbose = verbose)
}