| Title: | Rust-accelerated standard bioinformatics, computational biology workflows |
|---|---|
| Description: | Your favourite methods used in bioinformatics and computational biology made VERY fast via Rust. Contains various gene set enrichment methods, graph- based methods, co-expression module detection and much more. Additionally, has now a single cell analysis suite, enabling million cell scale analysis on local compute. |
| Authors: | Gregor Lueg [aut, cre], Liesbeth Francois [ctb], Andrew Skelton [ctb], Grant Neilson [ctb] |
| Maintainer: | Gregor Lueg <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.7.0 |
| Built: | 2026-10-14 22:57:59 UTC |
| Source: | https://github.com/GregorLueg/bixverse |
%>>% chains pipeline steps. Either side can be a ScStep or a
ScPipeline; the result is always a ScPipeline.
lhs %>>% rhslhs %>>% rhs
lhs |
|
rhs |
|
A ScPipeline.
# either side may be a step, the result is always a pipeline step_hvg_sc(hvg_no = 30L) %>>% step_pca_sc(no_pcs = 10L)# either side may be a step, the result is always a pipeline step_hvg_sc(hvg_no = 30L) %>>% step_pca_sc(no_pcs = 10L)
SingleCellsMultiModal
This method allows you to add ADT counts to a SingleCellsMultiModal.
Assumes the transcriptomics data has already been ingested and the cells
to keep are known. The method subsets the ADT counts to the kept cells,
applies the requested normalisation (CLR or DSB), populates the "var_adt"
table in the DuckDB, and attaches an ADTCounts to the class.
add_adt_counts_sc(object, adt_counts, method = c("clr", "dsb"), ...)add_adt_counts_sc(object, adt_counts, method = c("clr", "dsb"), ...)
object |
|
adt_counts |
Numeric matrix. Cells x features matrix of raw ADT counts. |
method |
String. One of |
... |
Additional arguments forwarded to the normalisation constructor.
For |
Returns a SingleCellsMultiModal with the ADT data added.
# a CLR-normalised ADT layer on top of an ingested RNA modality rna <- generate_single_cell_test_data() adt <- generate_single_cell_test_data_adt() dir <- tempfile("bixverse_mm") dir.create(dir) object <- load_r_data( SingleCellsMultiModal(dir_data = dir), counts = rna$counts, obs = rna$obs, var = rna$var, sc_qc_param = params_sc_min_quality(min_unique_genes = 5L), .verbose = FALSE ) object <- add_adt_counts_sc(object, adt_counts = adt$counts, method = "clr") get_adt_names(object) unlink(dir, recursive = TRUE, force = TRUE)# a CLR-normalised ADT layer on top of an ingested RNA modality rna <- generate_single_cell_test_data() adt <- generate_single_cell_test_data_adt() dir <- tempfile("bixverse_mm") dir.create(dir) object <- load_r_data( SingleCellsMultiModal(dir_data = dir), counts = rna$counts, obs = rna$obs, var = rna$var, sc_qc_param = params_sc_min_quality(min_unique_genes = 5L), .verbose = FALSE ) object <- add_adt_counts_sc(object, adt_counts = adt$counts, method = "clr") get_adt_names(object) unlink(dir, recursive = TRUE, force = TRUE)
This function will replace the meta data within the given object
add_new_metadata(object, new_metadata, ...)add_new_metadata(object, new_metadata, ...)
object |
The class |
new_metadata |
data.table. The new meta data you wish to add. |
... |
Additional arguments to parse to the functions. |
The object with updated metadata.
# swap in a metadata table that carries an extra batch column set.seed(42) counts <- matrix(rpois(60, 20), nrow = 10, ncol = 6) rownames(counts) <- sprintf("gene_%i", 1:10) colnames(counts) <- sprintf("sample_%i", 1:6) meta <- data.table::data.table( sample_id = colnames(counts), case_control = rep(c("case", "control"), each = 3) ) object <- BulkDge(raw_counts = counts, meta_data = meta) new_meta <- data.table::copy(meta)[, batch := rep(c("b1", "b2"), 3)] object <- add_new_metadata(object, new_metadata = new_meta) head(S7::prop(object, "meta_data"))# swap in a metadata table that carries an extra batch column set.seed(42) counts <- matrix(rpois(60, 20), nrow = 10, ncol = 6) rownames(counts) <- sprintf("gene_%i", 1:10) colnames(counts) <- sprintf("sample_%i", 1:6) meta <- data.table::data.table( sample_id = colnames(counts), case_control = rep(c("case", "control"), each = 3) ) object <- BulkDge(raw_counts = counts, meta_data = meta) new_meta <- data.table::copy(meta)[, batch := rep(c("b1", "b2"), 3)] object <- add_new_metadata(object, new_metadata = new_meta) head(S7::prop(object, "meta_data"))
This function adds cell type composition information to the nhoods_info
slot within the miloR object. For each neighbourhood, it calculates
the proportion of the majority cell type and identifies which cell type
is most abundant. This is useful for annotating differential abundance
results with the cellular composition of each neighbourhood.
add_nhoods_info(x, cell_info) ## S3 method for class 'miloR' add_nhoods_info(x, cell_info)add_nhoods_info(x, cell_info) ## S3 method for class 'miloR' add_nhoods_info(x, cell_info)
x |
|
cell_info |
Character vector. Represents the cell type annotations you wish to add to the different neighbourhoods. Must be the same length as the number of cells (rows) in the nhoods matrix. |
Modified miloR object with updated nhoods_info containing
majority_celltype and majority_prop columns.
# tag each neighbourhood with its majority cell type sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) milo <- get_miloR_abundances_sc( sc, sample_id_col = "sample_id", miloR_params = params_sc_miloR(k_refine = 10L), .verbose = FALSE ) design_df <- data.frame( grp = rep(c("a", "b"), each = 3), row.names = sprintf("sample_%i", 1:6) ) milo <- test_nhoods(milo, design = ~grp, design_df = design_df) milo <- add_nhoods_info(milo, cell_info = get_sc_obs(sc)$cell_grp) head(get_differential_abundance_res(milo)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# tag each neighbourhood with its majority cell type sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) milo <- get_miloR_abundances_sc( sc, sample_id_col = "sample_id", miloR_params = params_sc_miloR(k_refine = 10L), .verbose = FALSE ) design_df <- data.frame( grp = rep(c("a", "b"), each = 3), row.names = sprintf("sample_%i", 1:6) ) milo <- test_nhoods(milo, design = ~grp, design_df = design_df) milo <- add_nhoods_info(milo, cell_info = get_sc_obs(sc)$cell_grp) head(get_differential_abundance_res(milo)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Add an obs table derived from a method to the SingleCells.
add_sc_new_obs(object, obs_data)add_sc_new_obs(object, obs_data)
object |
|
obs_data |
data.table. A data.table you generated with |
The class with updated obs table in the DuckDB
# cluster memberships carry their own cell_idx, so they join straight on sc <- demo_single_cells() clusters <- fast_cluster_sc(sc, resolutions = 1.0, .verbose = FALSE) sc <- add_sc_new_obs(sc, get_data(clusters)) head(get_sc_obs(sc), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# cluster memberships carry their own cell_idx, so they join straight on sc <- demo_single_cells() clusters <- fast_cluster_sc(sc, resolutions = 1.0, .verbose = FALSE) sc <- add_sc_new_obs(sc, get_data(clusters)) head(get_sc_obs(sc), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function will add a modality as an affinity matrix for subsequent fusion to the object. To note: all of the modalities need to have the same number of rows, i.e., samples!
add_snf_data_modality(object, data, data_name, params = NULL)add_snf_data_modality(object, data, data_name, params = NULL)
object |
The underlying class, see |
data |
matrix or data.table. The data to transform into adjacency data and add to the class. Any data supplied will be assumed to be samples x features. The provided data type can be a data.table (for categorical and/or mixed types) or a matrix (for continous types). If you provide a data.table, the function will assume the first column are the sample identifiers. Please ensure that setting. |
data_name |
String. The data modality name. |
params |
Optional List. If you wish to overwite the already set up
parameters for SNF, see |
The class with added adjacency matrix for this data.
# add a categorical clinical modality to a continuous one set.seed(42) continuous <- matrix(rnorm(120), nrow = 12, ncol = 10) rownames(continuous) <- sprintf("sample_%02i", 1:12) colnames(continuous) <- sprintf("feature_%i", 1:10) clinical <- data.table::data.table( sample_id = rownames(continuous), sex = factor(sample(c("M", "F"), 12, replace = TRUE)), stage = factor(sample(c("I", "II", "III"), 12, replace = TRUE)) ) object <- SimilarityNetworkFusion( data = continuous, data_name = "continuous", snf_params = params_snf(k = 3L) ) object <- add_snf_data_modality(object, clinical, data_name = "clinical") dim(get_snf_adjcacency_mat(object, name = "clinical"))# add a categorical clinical modality to a continuous one set.seed(42) continuous <- matrix(rnorm(120), nrow = 12, ncol = 10) rownames(continuous) <- sprintf("sample_%02i", 1:12) colnames(continuous) <- sprintf("feature_%i", 1:10) clinical <- data.table::data.table( sample_id = rownames(continuous), sex = factor(sample(c("M", "F"), 12, replace = TRUE)), stage = factor(sample(c("I", "II", "III"), 12, replace = TRUE)) ) object <- SimilarityNetworkFusion( data = continuous, data_name = "continuous", snf_params = params_snf(k = 3L) ) object <- add_snf_data_modality(object, clinical, data_name = "clinical") dim(get_snf_adjcacency_mat(object, name = "clinical"))
Reads one or more obs columns from a SingleCells object and stores them
in the reference's labels slot for use by transfer_labels_symphony().
The provided sc_object must have cells_to_keep matching the cells used
at reference-build time; this is enforced by a length check against
nrow(z_corr).
add_symphony_labels(reference, sc_object, columns, overwrite = FALSE)add_symphony_labels(reference, sc_object, columns, overwrite = FALSE)
reference |
|
sc_object |
|
columns |
Character vector of obs column names. |
overwrite |
Boolean. If |
The reference with updated labels.
# attach obs labels to a reference built without them ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, .verbose = FALSE ) symphony_ref <- add_symphony_labels( symphony_ref, sc_object = ref, columns = "cell_grp" ) head(get_symphony_labels(symphony_ref)) unlink(ref@dir_data, recursive = TRUE, force = TRUE)# attach obs labels to a reference built without them ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, .verbose = FALSE ) symphony_ref <- add_symphony_labels( symphony_ref, sc_object = ref, columns = "cell_grp" ) head(get_symphony_labels(symphony_ref)) unlink(ref@dir_data, recursive = TRUE, force = TRUE)
This class helps dealing with h5ad objects from Python AnnData. You have several options will allow for retrieval of the underlying data.
AnnDataParser$new()Initialises the Anndata Parser.
AnnDataParser$new(h5_path)
h5_pathString. Path to the h5 file.
Returns the initialised class.
AnnDataParser$get_obs_table()Returns the observation table with all the data from the h5ad file.
AnnDataParser$get_obs_table()
data.table. The found observations are returned. The pandas index
will be named sample_id. Remaining columns (if found) will be returned
as factors due to the way the data is stored in h5.
AnnDataParser$get_var_info()Returns the variable table with all the data from the h5ad file.
AnnDataParser$get_var_info()
data.table. The found observations are returned. The pandas index
will be named var_id. Remaining columns (if found) will be returned as
factors due to the way the data is stored in h5.
AnnDataParser$get_raw_counts()Returns the counts that are stored in X slot of the
anndata object.
AnnDataParser$get_raw_counts()
Returns the count matrix with samples = columns and rows = features.
AnnDataParser$get_key_data()Wrapper function that returns a list of the stored count data and the metadata found in the h5ad file.
AnnDataParser$get_key_data()
List with following elements:
metadata - metadata from the respective h5ad file
var_info - metadata on the variables from the respective h5ad file.
counts - counts that were found in the h5ad file.
AnnDataParser$clone()The objects of this class are cloneable with this method.
AnnDataParser$clone(deep = FALSE)
deepWhether to make a deep clone.
# parse counts and obs back out of a small h5ad file set.seed(42) counts <- matrix(rpois(50, 5), nrow = 10, ncol = 5) obs <- data.table::data.table(sample_id = sprintf("cell_%i", 1:10)) var <- data.table::data.table(var_id = sprintf("gene_%i", 1:5)) h5_path <- tempfile(fileext = ".h5ad") write_h5ad_sc_dense(h5_path, counts, obs, var, .verbose = FALSE) parser <- AnnDataParser$new(h5_path) dim(parser$get_raw_counts()) head(parser$get_obs_table()) unlink(h5_path)# parse counts and obs back out of a small h5ad file set.seed(42) counts <- matrix(rpois(50, 5), nrow = 10, ncol = 5) obs <- data.table::data.table(sample_id = sprintf("cell_%i", 1:10)) var <- data.table::data.table(var_id = sprintf("gene_%i", 1:5)) h5_path <- tempfile(fileext = ".h5ad") write_h5ad_sc_dense(h5_path, counts, obs, var, .verbose = FALSE) parser <- AnnDataParser$new(h5_path) dim(parser$get_raw_counts()) head(parser$get_obs_table()) unlink(h5_path)
Runs each step in order. The first step receives object; subsequent steps
receive the result of the previous step. The chain is validated against the
class of object before anything runs, see validate_pipeline().
Errors propagate; nothing is caught.
apply_pipeline(pipeline, object)apply_pipeline(pipeline, object)
pipeline |
|
object |
|
The object after all steps have run.
# HVG then PCA, run in order on a freshly loaded object sc <- demo_single_cells(prepped = FALSE) p <- step_hvg_sc(hvg_no = 30L, .verbose = FALSE) %>>% step_pca_sc(no_pcs = 10L, .verbose = FALSE) sc <- apply_pipeline(p, sc) dim(get_pca_factors(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# HVG then PCA, run in order on a freshly loaded object sc <- demo_single_cells(prepped = FALSE) p <- step_hvg_sc(hvg_no = 30L, .verbose = FALSE) %>>% step_pca_sc(no_pcs = 10L, .verbose = FALSE) sc <- apply_pipeline(p, sc) dim(get_pca_factors(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
SingleCells objectSplits object by group_col, applies pipeline to each subset, and
returns a named list of processed SingleCellsSubsets. Useful for
per-sample / per-cell-type re-analysis where the same chain (HVG, PCA,
neighbours, clusters, ...) is run on each group, e.g. sample-pure metacell
generation followed by an external merge.
apply_pipeline_per_group( pipeline, object, group_col, groups = NULL, progress = FALSE )apply_pipeline_per_group( pipeline, object, group_col, groups = NULL, progress = FALSE )
pipeline |
|
object |
|
group_col |
String. Column in obs used to split. |
groups |
Optional character vector. Restrict to these group values; if
|
progress |
Boolean. Shall big progress messages be printed to the
console. Defaults to |
Named list of processed objects, names being the group values.
Usually SingleCellsSubset, or MetaCells if the pipeline ends on
step_metacells_sc(), in which case merge_meta_cells() puts them back
together.
# the same chain re-run inside each cell type sc <- demo_single_cells(prepped = FALSE) p <- sc_pipeline() %>>% step_hvg_sc(hvg_no = 20L, .verbose = FALSE) res <- apply_pipeline_per_group(p, sc, group_col = "cell_grp") names(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the same chain re-run inside each cell type sc <- demo_single_cells(prepped = FALSE) p <- sc_pipeline() %>>% step_hvg_sc(hvg_no = 20L, .verbose = FALSE) res <- apply_pipeline_per_group(p, sc, group_col = "cell_grp") names(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Compresses counts_cells.bin into counts.bxa with zstd. The gene-based
file is not archived, as it is a transpose of the cell-based one. Normalised
values are only kept where they cannot be recomputed from the raw counts.
load_existing() restores both binaries automatically when it finds only
the archive. Save the in-memory data first with save_sc_exp_to_disk().
archive_sc_exp(object, level = 3L, remove_bins = TRUE, .verbose = TRUE)archive_sc_exp(object, level = 3L, remove_bins = TRUE, .verbose = TRUE)
object |
|
level |
Integer. zstd compression level, between 1 and 22. Higher levels compress more and take longer; decompression speed barely changes. |
remove_bins |
Boolean. Delete |
.verbose |
Boolean. Controls verbosity of the function. |
A list with n_cells, nnz, n_norm_stored and archive_bytes,
invisibly.
sc <- demo_single_cells() dir <- sc@dir_data save_sc_exp_to_disk(sc, type = "rds") archive_sc_exp(sc, .verbose = FALSE) sc <- load_existing(SingleCells(dir_data = dir), .verbose = FALSE) unlink(dir, recursive = TRUE, force = TRUE)sc <- demo_single_cells() dir <- sc@dir_data save_sc_exp_to_disk(sc, type = "rds") archive_sc_exp(sc, .verbose = FALSE) sc <- load_existing(SingleCells(dir_data = dir), .verbose = FALSE) unlink(dir, recursive = TRUE, force = TRUE)
LigandTargetInfluence
Turns a regulatory potential matrix built elsewhere, for example with
nichenetr's construct_ligand_target_matrix(), into a
LigandTargetInfluence object, so it can be scored with
ligand_activity_scores().
as_ligand_target_influence(x, ligands_as_cols = FALSE, params = NULL)as_ligand_target_influence(x, ligands_as_cols = FALSE, params = NULL)
x |
Numeric matrix with unique row and column names. |
ligands_as_cols |
Boolean. Set to |
params |
Optional list. Parameters used to build |
A LigandTargetInfluence object. Each ligand is its own seed.
# genes x ligands, as nichenetr returns it ltm <- matrix( c(0.5, 0.0, 0.1, 0.4, 0.3, 0.1), nrow = 3, dimnames = list(c("G1", "G2", "G3"), c("L1", "L2")) ) inf <- as_ligand_target_influence(ltm, ligands_as_cols = TRUE) ligand_activity_scores(inf, gene_sets = list(set_A = "G1"))# genes x ligands, as nichenetr returns it ltm <- matrix( c(0.5, 0.0, 0.1, 0.4, 0.3, 0.1), nrow = 3, dimnames = list(c("G1", "G2", "G3"), c("L1", "L2")) ) inf <- as_ligand_target_influence(ltm, ligands_as_cols = TRUE) ligand_activity_scores(inf, gene_sets = list(set_A = "G1"))
The hard tier. Called at the entry of anything that hands cached indices to Rust or writes a derived result back into the object, where a stale artefact means either a crash in the binding layer or silently mis-aligned biology.
assert_sc_state( x, artefacts, modality = "rna", .var.name = checkmate::vname(x), add = NULL )assert_sc_state( x, artefacts, modality = "rna", .var.name = checkmate::vname(x), add = NULL )
x |
|
artefacts |
Character vector. Artefacts to check, either an artefact
kind ( |
modality |
String. Modality to resolve unqualified names against. One
of |
.var.name |
Name of the checked object to print in assertions. Defaults to the heuristic implemented in checkmate. |
add |
Collection to store assertion messages. See
|
Invisibly x when the assertion holds, otherwise an error.
The cluster-level ScType path (score_clusters()) stamps one label on every
cell of a cluster, so a minority population sharing a Leiden community with a
bigger one gets absorbed without anything flagging it. This runs the scoring
per cell instead: the score matrix is smoothed over the sNN graph via label
spreading (Zhou et al.), then each cell takes its own argmax. Cells whose best
score falls below score_floor come back as NA.
Pass cluster_col to also get the per-cluster composition (purity, entropy,
runner-up cell type) and the hybrid assignment, where clusters at or above
purity_threshold keep the blanket cluster-level call and mixed clusters fall
back to the per-cell calls.
assign_sc_type( object, sc_type_res, cluster_col = NULL, sctype_cell_params = params_sctype_cells(), .verbose = TRUE )assign_sc_type( object, sc_type_res, cluster_col = NULL, sctype_cell_params = params_sctype_cells(), .verbose = TRUE )
object |
|
sc_type_res |
|
cluster_col |
Optional string. Name of the obs column with the cluster assignment. If provided, the composition and hybrid assignment are returned on top of the per-cell calls. |
sctype_cell_params |
List. Output of |
.verbose |
Boolean or integer. Controls verbosity. |
An ScTypeCellResults results class.
Ianevski et al., Nat Comm, 2022. Zhou et al., NIPS, 2004.
# per cell calls from the ScType scores sc <- demo_single_cells() markers <- data.table::data.table( cell_type = rep(sprintf("cell_type_%i", 1:3), each = 10), gene_id = sprintf("gene_%02d", 1:30) ) cell_markers <- prepare_cell_markers(obj = sc, marker_df = markers) scores <- calc_sc_type_scores( sc, cell_marker_list = cell_markers, .verbose = FALSE ) res <- assign_sc_type(sc, sc_type_res = scores, .verbose = FALSE) table(res$assignments) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# per cell calls from the ScType scores sc <- demo_single_cells() markers <- data.table::data.table( cell_type = rep(sprintf("cell_type_%i", 1:3), each = 10), gene_id = sprintf("gene_%02d", 1:30) ) cell_markers <- prepare_cell_markers(obj = sc, marker_df = markers) scores <- calc_sc_type_scores( sc, cell_marker_list = cell_markers, .verbose = FALSE ) res <- assign_sc_type(sc, sc_type_res = scores, .verbose = FALSE) table(res$assignments) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Calculates an AUC-type score akin to AUCell across the gene sets, see Aibar
et al. Three statistics are on offer, all consuming the same within-cell
ranking but weighting it differently. "recovery" (default) is the
recovery-curve AUC under a rank cutoff, i.e. the AUCell statistic of Aibar,
et al., and is top-heavy: only genes inside the top max_rank of the cell
contribute. "wilcox" is the AUC derived from the Mann-Whitney U statistic
over the full ranking; its null sits at 0.5 for any gene set size, which
makes it a good fit for pathway activity. "ap" is average precision, the
most top-heavy of the three, but its null tracks the gene set prevalence, so
raw values are not comparable across gene sets of different size unless
standardise is on.
aucell_sc( object, gs_list, aucell_params = params_sc_aucell(), streaming = NULL, .verbose = TRUE )aucell_sc( object, gs_list, aucell_params = params_sc_aucell(), streaming = NULL, .verbose = TRUE )
object |
|
gs_list |
Named list. The elements have the gene identifiers of the respective gene sets. |
aucell_params |
List with the AUCell parameters, see
|
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
AUCell results in form of a matrix that is cells x gene sets or as
ScMatrixRes pending the input.
Aibar, et al., Nat Methods, 2017
# recovery curve AUC for two marker programmes sc <- demo_single_cells() gs_list <- list( type_1 = get_gene_names(sc)[1:10], type_2 = get_gene_names(sc)[11:20] ) res <- aucell_sc(sc, gs_list = gs_list, .verbose = FALSE) dim(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# recovery curve AUC for two marker programmes sc <- demo_single_cells() gs_list <- list( type_1 = get_gene_names(sc)[1:10], type_2 = get_gene_names(sc)[11:20] ) res <- aucell_sc(sc, gs_list = gs_list, .verbose = FALSE) dim(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Runs a linear batch correction over the data regressing out batch effects
and adding normalised_counts_corrected to the object. Should these counts
be found by calculate_dge_hedges(), they will be used for
calculations of effect sizes based on Hedge's G.
batch_correction_bulk_dge( object, contrast_column, batch_col, scale_genes = FALSE, no_hvg_genes = 2500L )batch_correction_bulk_dge( object, contrast_column, batch_col, scale_genes = FALSE, no_hvg_genes = 2500L )
object |
The underlying class, see |
contrast_column |
String. The contrast column in which the groupings are stored. Needs to be found in the meta_data within the properties. |
batch_col |
String. The column in which the batch effect groups can be found. |
scale_genes |
Boolean. Shall the log(cpm) counts be scaled prior the PCA
calculation. Defaults to |
no_hvg_genes |
Integer. Number of highly variable genes to include. Defaults to 2500. |
Returns the class with additional data added to the outputs.
# regress out a batch column and keep the corrected counts syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50), batch = rep(c("b1", "b2"), times = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_pca_bulk_dge(object, no_hvg_genes = 500L) object <- batch_correction_bulk_dge( object, contrast_column = "case_control", batch_col = "batch", no_hvg_genes = 500L ) get_outputs(object)$normalised_counts_corrected[1:3, 1:3]# regress out a batch column and keep the corrected counts syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50), batch = rep(c("b1", "b2"), times = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_pca_bulk_dge(object, no_hvg_genes = 500L) object <- batch_correction_bulk_dge( object, contrast_column = "case_control", batch_col = "batch", no_hvg_genes = 500L ) get_outputs(object)$normalised_counts_corrected[1:3, 1:3]
This function implements the batch-balanced k-nearest neighbour algorithm from Polański, et al. Briefly, the algorithm generates a KNN index on a per batch basis and identifies the neighbours of cells for each individual index. Subsequently, it leverages UMAP connectivity calculations to reduce spurious connections. For more details, please refer to Polański, et al.
bbknn_sc( object, batch_column, no_neighbours_to_keep = 5L, embd_to_use = "pca", no_embd_to_use = NULL, bbknn_params = params_sc_bbknn(), seed = 42L, .verbose = TRUE )bbknn_sc( object, batch_column, no_neighbours_to_keep = 5L, embd_to_use = "pca", no_embd_to_use = NULL, bbknn_params = params_sc_bbknn(), seed = 42L, .verbose = TRUE )
object |
|
batch_column |
String. The column with the batch information in the obs data of the class. |
no_neighbours_to_keep |
Integer. Maximum number of neighbours to keep
from the BBKNN algorithm. Due to generating neighbours for each batch, there
might be a large number of generated neighbours. This will only keep the top
|
embd_to_use |
String. The embedding to use. Atm, the only option is
|
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
bbknn_params |
A list, please see
|
seed |
Integer. Random seed. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The object with added kNN matrix based on BBKNN and the graph based on the returned connectivities of the algorithm.
Polański, et al., Bioinformatics, 2020
# batch balanced kNN, replacing the graph the demo object carries sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) sc <- bbknn_sc( remove_knn(sc), batch_column = "batch_index", no_neighbours_to_keep = 5L, .verbose = FALSE ) dim(get_knn_mat(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# batch balanced kNN, replacing the graph the demo object carries sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) sc <- bbknn_sc( remove_knn(sc), batch_column = "batch_index", no_neighbours_to_keep = 5L, .verbose = FALSE ) dim(get_knn_mat(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
The last SCENIC step. Each regulon gets its own threshold derived from the shape of its AUC distribution across cells, and a cell counts as on when its score sits strictly above that threshold.
The thresholds come back alongside the calls, so you can inspect them, override any that look wrong and re-apply the comparison yourself. SCENIC does the same, it writes them to an editable file between scoring and assignment.
Regulons flagged as not bimodal fell back to mean + 2 * sd. If most of your
regulons land there, the AUC distributions are too flat to separate, which
usually points at the scoring statistic rather than the thresholding. Check
you used auc_type = "recovery", see params_sc_aucell().
binarise_regulon_activity( auc_matrix, binarise_params = params_scenic_binarise(), .verbose = TRUE )binarise_regulon_activity( auc_matrix, binarise_params = params_scenic_binarise(), .verbose = TRUE )
auc_matrix |
Numeric matrix of cells x regulons, or the |
binarise_params |
List. Output of |
.verbose |
Boolean. Controls verbosity of the function. |
A list with:
binary - Logical matrix of cells x regulons, TRUE where the
regulon is on.
thresholds - data.table with one row per regulon, holding the
threshold, whether it was bimodal and the number of cells called on.
Aibar, et al., Nat Methods, 2017
# on/off calls for a bimodal and a unimodal regulon set.seed(7L) auc <- cbind( regulon_a = c(rnorm(50, 0.1, 0.02), rnorm(50, 0.4, 0.02)), regulon_b = rnorm(100, 0.2, 0.05) ) rownames(auc) <- sprintf("cell_%i", 1:100) binarise_regulon_activity(auc, .verbose = FALSE)$thresholds# on/off calls for a bimodal and a unimodal regulon set.seed(7L) auc <- cbind( regulon_a = c(rnorm(50, 0.1, 0.02), rnorm(50, 0.4, 0.02)), regulon_b = rnorm(100, 0.2, 0.05) ) rownames(auc) <- sprintf("cell_%i", 1:100) binarise_regulon_activity(auc, .verbose = FALSE)$thresholds
Draws random gene sets across a log-spaced grid of set sizes, fits gamma tails to the resulting enrichment scores and smooths the fitted parameters across sizes, see Lachmann, et al. No gene set library enters here, so one calibration serves every library you score against that signature. This is where nearly all of the runtime sits; scoring afterwards costs one gamma tail evaluation per pathway.
The returned null is a plain list, so it survives saveRDS() and can be
cached against a signature.
blitzgsea_calibrate(stats, blitz_params = params_blitzgsea())blitzgsea_calibrate(stats, blitz_params = params_blitzgsea())
stats |
Named numeric vector. The gene level statistic. Sorted internally, so the order you hand it in does not matter. |
blitz_params |
List. The blitzGSEA parameters, see
|
An object of class BlitzGseaNull, a list with the following
elements:
anchor_sizes - Numeric vector. The anchor set sizes, ascending.
shape_pos - Numeric vector. Smoothed positive-tail gamma shape.
scale_pos - Numeric vector. Smoothed positive-tail gamma scale.
shape_neg - Numeric vector. Smoothed negative-tail gamma shape.
scale_neg - Numeric vector. Smoothed negative-tail gamma scale.
pos_ratio - Numeric vector. Smoothed fraction of positive null scores at each anchor.
ks_pos - Float. Mean goodness-of-fit p-value for the positive tail.
ks_neg - Float. Mean goodness-of-fit p-value for the negative tail.
centred - Boolean. Whether the signature was centred.
n_genes - Integer. Size of the signature it was calibrated on.
Lachmann, et al., Bioinformatics, 2022
# calibrate a null once, to be reused across gene set libraries set.seed(42L) stats <- stats::setNames(rnorm(500), sprintf("gene_%03i", 1:500)) null_model <- blitzgsea_calibrate( stats, blitz_params = params_blitzgsea(permutations = 1000L, anchors = 10L) ) null_model# calibrate a null once, to be reused across gene set libraries set.seed(42L) stats <- stats::setNames(rnorm(500), sprintf("gene_%03i", 1:500)) null_model <- blitzgsea_calibrate( stats, blitz_params = params_blitzgsea(permutations = 1000L, anchors = 10L) ) null_model
Bonsai reconstructs a tree over the cells in which every cell is a leaf and the internal nodes are inferred ancestral states, with branch lengths that carry the amount of change between them. Unlike a kNN graph it uses each measurement's error bar, which is what Sanity provides: the raw counts go through Sanity first, for posterior log fold changes with error bars, and Bonsai builds the tree on those. The tree is then laid out in 2D. For details, please refer to de Groot, et al. and Breda, et al.
No HVG selection needed. By default every gene goes in, streamed through
Sanity in chunks, and only the genes with enough signal over their own noise
(min_signal_to_noise in params_sc_bonsai()) are kept for the
tree. That is the gene selection of the Bonsai paper, and it keeps memory at
one chunk plus the survivors. Pass hvg to restrict the candidates.
Runtime grows a little faster than linearly with the number of cells. In bonsai-rs's own benchmarks the search took about 70 seconds at 10,000 cells and 210 seconds at 25,000 on ten cores. Sanity comes on top, linear in the number of genes it has to fit; on the CPU that is the larger share once all genes go in.
The object itself is not touched. Store the leaf coordinates with
set_bonsai_embedding() if you want them next to the other
embeddings.
On MetaCells every metacell is a leaf. Their raw counts are sums over
their cells, and a sum of Poisson counts is Poisson, so Sanity treats a
metacell exactly as it treats a cell: its total counts are the library size,
and a bigger metacell gets tighter error bars, which Bonsai weights by. That
is the way to large data: 100k cells in metacells of 50 is a 2,000-leaf tree.
What you give up is any structure inside a metacell.
bonsai_sc( object, hvg = NULL, bonsai_params = params_sc_bonsai(), .verbose = TRUE )bonsai_sc( object, hvg = NULL, bonsai_params = params_sc_bonsai(), .verbose = TRUE )
object |
|
hvg |
Optional integer. Restrict the candidate genes to these, e.g. the
output of |
bonsai_params |
List. See |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A BonsaiTree S3 object with:
nodes - data.table with node, parent (NA for the root),
branch, is_leaf, cell_id (NA for the inferred ancestors),
n_cells (the cells behind a leaf: 1 for single cells, the originating
cells for a metacell; NA for the ancestors), x and y. The leaves
come first, in the order of the object's cells or metacells.
loglik - The loglikelihood of the final tree.
steps - data.table with the loglikelihood after each search step and the step's wall time in seconds.
timings - data.table with the wall time in seconds of each stage:
sanity, ingest, bonsai (the whole search), layout, and total, the
whole call as R saw it.
genes_used - The genes the tree was built on.
genes_dropped - The candidate genes left out: no counts in the
cells, ill-conditioned Sanity posteriors, or a signal-to-noise ratio below
min_signal_to_noise.
cell_idx - The cells or metacells the tree was built over (0-indexed).
layout, hyperbolic - The current layout.
params - The parameters of the run.
de Groot, et al., Nat. Biotechnol., 2026; Breda, et al., Nat. Biotechnol., 2021.
# a tree over the demo cells, genes selected by their signal-to-noise sc <- demo_single_cells( prepped = FALSE, syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 50L) ) tree <- bonsai_sc(sc, .verbose = FALSE) tree unlink(sc@dir_data, recursive = TRUE, force = TRUE)# a tree over the demo cells, genes selected by their signal-to-noise sc <- demo_single_cells( prepped = FALSE, syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 50L) ) tree <- bonsai_sc(sc, .verbose = FALSE) tree unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Turns the TF to gene table into the gene sets you hand to aucell_sc().
tf_to_genes_motif_enrichment() only flags which pairs survived the
CisTarget leading edge, it does not remove anything, so this is the step that
applies that filter.
Following SCENIC, the TF is added back to its own target list and regulons
below min_genes are dropped. Note the CisTarget leading edge usually cuts
hard: a module of a few hundred candidate targets typically ends up as a
regulon of a few dozen.
build_regulons( x, use_leading_edge = TRUE, add_tf = TRUE, min_genes = 10L, mode = c("activating", "repressing", "both"), .verbose = TRUE ) ## S3 method for class 'ScenicGrn' build_regulons( x, use_leading_edge = TRUE, add_tf = TRUE, min_genes = 10L, mode = c("activating", "repressing", "both"), .verbose = TRUE )build_regulons( x, use_leading_edge = TRUE, add_tf = TRUE, min_genes = 10L, mode = c("activating", "repressing", "both"), .verbose = TRUE ) ## S3 method for class 'ScenicGrn' build_regulons( x, use_leading_edge = TRUE, add_tf = TRUE, min_genes = 10L, mode = c("activating", "repressing", "both"), .verbose = TRUE )
x |
|
use_leading_edge |
Boolean. Shall only the TF to gene pairs inside the
CisTarget leading edge be kept. Defaults to |
add_tf |
Boolean. Shall the TF be added to its own regulon. Defaults to
|
min_genes |
Integer. Regulons with fewer genes than this are dropped.
Defaults to |
mode |
String. Which links to keep, based on the |
.verbose |
Boolean. Controls verbosity of the function. |
A named list of character vectors, one per regulon.
Aibar, et al., Nat Methods, 2017
# turn the signed TF to gene table into regulon gene sets sc <- demo_single_cells() grn <- scenic_grn_sc( sc, tf_ids = sprintf("gene_%02d", 1:5), scenic_params = params_scenic( min_counts = 1L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) grn <- identify_tf_to_genes( grn, method = "top_k", k_tfs = 3L, .verbose = FALSE ) grn <- tf_to_genes_correlations(grn, object = sc, .verbose = FALSE) lengths(build_regulons( grn, use_leading_edge = FALSE, min_genes = 3L, .verbose = FALSE )) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# turn the signed TF to gene table into regulon gene sets sc <- demo_single_cells() grn <- scenic_grn_sc( sc, tf_ids = sprintf("gene_%02d", 1:5), scenic_params = params_scenic( min_counts = 1L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) grn <- identify_tf_to_genes( grn, method = "top_k", k_tfs = 3L, .verbose = FALSE ) grn <- tf_to_genes_correlations(grn, object = sc, .verbose = FALSE) lengths(build_regulons( grn, use_leading_edge = FALSE, min_genes = 3L, .verbose = FALSE )) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Runs sparse PCA over the provided HVGs, runs Harmony for batch correction,
and compresses the result into the cached terms used at query time. The
Harmony version is auto-detected from the class of harmony_params.
Optionally snapshots one or more obs columns (e.g. cell type annotations)
into the reference's labels slot for downstream label transfer.
build_symphony_ref( object, batch_column, additional_batch_columns = NULL, hvg, harmony_params = params_sc_harmony(), pca_params = params_sc_pca(), no_pcs = 30L, slim = FALSE, label_columns = NULL, seed = 42L, .verbose = TRUE )build_symphony_ref( object, batch_column, additional_batch_columns = NULL, hvg, harmony_params = params_sc_harmony(), pca_params = params_sc_pca(), no_pcs = 30L, slim = FALSE, label_columns = NULL, seed = 42L, .verbose = TRUE )
object |
|
batch_column |
String. Primary batch column in the obs table. |
additional_batch_columns |
Optional character vector. |
hvg |
Integer vector. R-style 1-based HVG indices into the gene
universe of |
harmony_params |
List. Output of |
pca_params |
List. Output of |
no_pcs |
Integer. Number of principal components. |
slim |
Boolean. If |
label_columns |
Optional character vector of obs column names to
snapshot into the reference. Read in |
seed |
Integer. |
.verbose |
Boolean or integer. |
A SymphonyReference object.
# two batch reference, cell type labels snapshotted along the way ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L, batch_effect_strength = "medium" ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) symphony_ref unlink(ref@dir_data, recursive = TRUE, force = TRUE)# two batch reference, cell type labels snapshotted along the way ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L, batch_effect_strength = "medium" ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) symphony_ref unlink(ref@dir_data, recursive = TRUE, force = TRUE)
This is a helper function that can be used to create a BulkDge object
(see BulkDge()) directly from h5ad objects.
bulk_dge_from_h5ad(h5_path, .verbose = TRUE)bulk_dge_from_h5ad(h5_path, .verbose = TRUE)
h5_path |
String. Path to the h5ad object. |
.verbose |
Controls verbosity of the function |
BulkDge object.
# round trip a synthetic count matrix through a temporary h5ad syn <- synthetic_bulk_cor_matrix() h5_path <- tempfile(fileext = ".h5ad") write_h5ad_sc_dense( f_path = h5_path, counts = t(syn$counts), obs = data.table::data.table(sample_id = colnames(syn$counts)), var = data.table::data.table(var_id = rownames(syn$counts)), .verbose = FALSE ) object <- bulk_dge_from_h5ad(h5_path, .verbose = FALSE) object unlink(h5_path)# round trip a synthetic count matrix through a temporary h5ad syn <- synthetic_bulk_cor_matrix() h5_path <- tempfile(fileext = ".h5ad") write_h5ad_sc_dense( f_path = h5_path, counts = t(syn$counts), obs = data.table::data.table(sample_id = colnames(syn$counts)), var = data.table::data.table(var_id = rownames(syn$counts)), .verbose = FALSE ) object <- bulk_dge_from_h5ad(h5_path, .verbose = FALSE) object unlink(h5_path)
Class for applying various co-expression module detection methods on top of bulk RNAseq data.
BulkCoExp(raw_data, meta_data, variable_info = NULL)BulkCoExp(raw_data, meta_data, variable_info = NULL)
raw_data |
The raw count matrix. Rows = samples, columns = features. |
meta_data |
data.table Metadata information on the samples. Expects to
have a |
variable_info |
data.table. Metadata information on the features. This is an optional table. |
Returns the BulkCoExp class for further operations.
A numerical matrix of the provided raw data.
A data.table with the meta-information about the samples.
An optional data.table containing the variable info.
A list in which various types of processed data will be stored.
A list in which key outputs will be stored.
A (nested) list that will store all the parameters of the applied function.
A data.table that will contain the final results.
# co-expression class over synthetic bulk counts (samples x genes) syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object# co-expression class over synthetic bulk counts (samples x genes) syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object
Class for coordinating differential gene expression analyses with subsequent
GSE in a structured format. The filtered counts, library sizes and
normalisation factors are stored in the class; get_dge_list()
turns them into an edgeR DGEList on demand.
BulkDge( raw_counts, meta_data, variable_info = NULL, alternative_gene_id = NULL )BulkDge( raw_counts, meta_data, variable_info = NULL, alternative_gene_id = NULL )
raw_counts |
matrix. The raw count matrix. Rows = genes, columns =
samples. Note: this is different from the |
meta_data |
data.table. Metadata information on the samples. It expects to have a column sample_id and case_control column. |
variable_info |
data.table. Metadata information on the features. This
is an optional table. Defaults to |
alternative_gene_id |
String. Optional alternative gene identifier to be used. Must be a column of variable_info! |
Returns the BulkDge class for further operations.
A numerical matrix of the provided raw data.
A data.table with the meta-information about the samples.
An optional data.table containing the variable info.
A list in which key outputs will be stored.
A list with the plots that are generated during subsequent QC steps.
A (nested) list that will store all the parameters of the applied function.
A list in which final results will be stored.
# DGE class over synthetic bulk counts (genes x samples) syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object# DGE class over synthetic bulk counts (genes x samples) syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object
This function will plot various alphas to highlight the most interesting alpha parameters akin to the implementation of contrastive PCA in Python.
c_pca_plot_alphas( object, label_column = NULL, min_alpha = 0.1, max_alpha = 100, n_alphas = 10L, .verbose = TRUE )c_pca_plot_alphas( object, label_column = NULL, min_alpha = 0.1, max_alpha = 100, n_alphas = 10L, .verbose = TRUE )
object |
The underlying class, see |
label_column |
An optional sample label column. Needs to exist in the
meta_data of the |
min_alpha |
Minimum alpha to test. |
max_alpha |
Maximum alpha to test. |
n_alphas |
Number of alphas to test. The function will generate a series of alphas from log(min_alpha) to log(max_alpha) to test out. |
.verbose |
Controls verbosity of function. |
A ggplot showing the impact of various alpha parameters on the samples in form of 2D plots.
Abid, et al., Nature Communications, 2018
# how the group separation responds to alpha cpca_data <- synthetic_c_pca_data() target <- t(cpca_data$target) background <- t(cpca_data$background) meta <- data.table::data.table( sample_id = rownames(target), grp = cpca_data$target_labels ) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, .verbose = FALSE) obj <- contrastive_pca_processing(obj, background, .verbose = FALSE) c_pca_plot_alphas(obj, label_column = "grp", n_alphas = 6L, .verbose = FALSE)# how the group separation responds to alpha cpca_data <- synthetic_c_pca_data() target <- t(cpca_data$target) background <- t(cpca_data$background) meta <- data.table::data.table( sample_id = rownames(target), grp = cpca_data$target_labels ) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, .verbose = FALSE) obj <- contrastive_pca_processing(obj, background, .verbose = FALSE) c_pca_plot_alphas(obj, label_column = "grp", n_alphas = 6L, .verbose = FALSE)
Rust-based version of blitzGSEA, see Lachmann, et al. Instead of permuting per pathway, it calibrates a gamma null once for the signature and then reads each pathway's p-value straight off the fitted tail. That makes the cost independent of how many pathways you throw at it, which is where it pulls away from permutation methods on large libraries.
Hand back a null_model from blitzgsea_calibrate() to reuse a
calibration across libraries. Left as NULL, one is calibrated on the fly.
calc_blitzgsea( stats, pathways, blitz_params = params_blitzgsea(), null_model = NULL )calc_blitzgsea( stats, pathways, blitz_params = params_blitzgsea(), null_model = NULL )
stats |
Named numeric vector. The gene level statistic. |
pathways |
List. A named list with each element containing the genes for this pathway. |
blitz_params |
List. The blitzGSEA parameters, see
|
null_model |
Optional |
A data.table with the results from the blitzGSEA with the following columns:
pathway_name - Character. The name of the pathway.
es - Float. The enrichment score for this pathway.
nes - Float. The normalised enrichment score, the signed normal quantile of the one-sided tail probability.
pvals - Float. The two-sided p-value from the gamma approximation.
fdr - Float. The Benjamini-Hochberg adjusted p-value.
sidak - Float. The Sidak adjusted p-value.
size - Integer. The size of the pathway after intersection.
leading_edge - List of character vectors with the leading edge genes.
Lachmann, et al., Bioinformatics, 2022
# blitzGSEA reading p-values off a fitted gamma null set.seed(42L) stats <- stats::setNames(rnorm(500), sprintf("gene_%03i", 1:500)) stats[1:20] <- stats[1:20] + 2 pathways <- list( up_set = sprintf("gene_%03i", 1:20), random_set = sprintf("gene_%03i", 200:240) ) res <- calc_blitzgsea( stats, pathways, blitz_params = params_blitzgsea(permutations = 1000L, anchors = 10L) ) res[, c("pathway_name", "es", "nes", "pvals")]# blitzGSEA reading p-values off a fitted gamma null set.seed(42L) stats <- stats::setNames(rnorm(500), sprintf("gene_%03i", 1:500)) stats[1:20] <- stats[1:20] + 2 pathways <- list( up_set = sprintf("gene_%03i", 1:20), random_set = sprintf("gene_%03i", 200:240) ) res <- calc_blitzgsea( stats, pathways, blitz_params = params_blitzgsea(permutations = 1000L, anchors = 10L) ) res[, c("pathway_name", "es", "nes", "pvals")]
To leverage the quality metrics from Persad, et al., we need the diffusion coordinates to then calculate if a cell is a dense or sparse region of the manifold, its compactness and separation to other meta cells. To do so, generate a diffusion map on the original data based on the approach of SEACells and add the data to the object, see Persad, et al.
calc_diffusion_coordinates( object, knn_data, n_dcs = 10L, k_density = 150L, seed = 42L, .verbose = TRUE )calc_diffusion_coordinates( object, knn_data, n_dcs = 10L, k_density = 150L, seed = 42L, .verbose = TRUE )
object |
|
knn_data |
|
n_dcs |
Integer. Number of diffusion coordinates to use. Defaults to
|
k_density |
Integer. The k-th neighbour to use for the density region
estimation. Defaults to |
seed |
Integer. Seed for reproducibility |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The class with the diffusion map coordinates, density distance and region attached.
Persad, et al. Nat Biotechnol, 2023
# diffusion map off the source kNN graph, giving each meta cell a region sc <- demo_single_cells() mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 50L), .verbose = FALSE ) mc <- calc_diffusion_coordinates( mc, knn_data = get_knn_obj(sc), .verbose = FALSE ) table(mc[["density_region"]]$density_region) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# diffusion map off the source kNN graph, giving each meta cell a region sc <- demo_single_cells() mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 50L), .verbose = FALSE ) mc <- calc_diffusion_coordinates( mc, knn_data = get_knn_obj(sc), .verbose = FALSE ) table(mc[["density_region"]]$density_region) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Rust-based version of the fgsea simple and multi-level algorithm. Initially, the simple method is run. For low p-values, the multi-level method is used to estimate lower p-values than possible just based on the permutations, see Korotkevich, et al.
calc_fgsea( stats, pathways, nperm = 1000L, gsea_params = params_gsea(), seed = 123L )calc_fgsea( stats, pathways, nperm = 1000L, gsea_params = params_gsea(), seed = 123L )
stats |
Named numeric vector. The gene level statistic. |
pathways |
List. A named list with each element containing the genes for this pathway. |
nperm |
Integer. Number of permutation tests. Defaults to |
gsea_params |
List. The GSEA parameters, see
|
seed |
Random seed for reproducibility. |
A data.table with the results from the GSEA with the following columns:
es - Float. The enrichment score for this pathway.
nes - Float. The normalised enrichment score for this pathway.
pvals - Float. The p-value for this pathway.
n_more_extreme - Integer. Number of permutation that had more extreme enrichment scores than the actual.
size - Integer. The size of the pathway.
pathway_name - Character. The name of the pathway.
leading_edge - List of character vectors with the leading edge genes.
fdr - Float. The adjusted pval.
Korotkevich, et al., bioRxiv
# fgsea with the multi-level refinement of low p-values set.seed(42L) stats <- stats::setNames(rnorm(500), sprintf("gene_%03i", 1:500)) stats[1:20] <- stats[1:20] + 2 pathways <- list( up_set = sprintf("gene_%03i", 1:20), random_set = sprintf("gene_%03i", 200:240) ) res <- calc_fgsea(stats, pathways, nperm = 1000L) res[, c("pathway_name", "es", "pvals")]# fgsea with the multi-level refinement of low p-values set.seed(42L) stats <- stats::setNames(rnorm(500), sprintf("gene_%03i", 1:500)) stats[1:20] <- stats[1:20] + 2 pathways <- list( up_set = sprintf("gene_%03i", 1:20), random_set = sprintf("gene_%03i", 200:240) ) res <- calc_fgsea(stats, pathways, nperm = 1000L) res[, c("pathway_name", "es", "pvals")]
Rust-based version of the fgsea simple algorithm. This one is permutation- based and similar to the traditional implementation, but leverages some clear tricks to be way faster, see Korotkevich, et al.
calc_fgsea_simple( stats, pathways, nperm = 2000L, gsea_params = params_gsea(), seed = 123L )calc_fgsea_simple( stats, pathways, nperm = 2000L, gsea_params = params_gsea(), seed = 123L )
stats |
Named numeric vector. The gene level statistic. |
pathways |
List. A named list with each element containing the genes for this pathway. |
nperm |
Integer. Number of permutation tests. Defaults to |
gsea_params |
List. The GSEA parameters, see
|
seed |
Random seed for reproducibility. |
A data.table with the results from the GSEA with the following columns:
es - Float. The enrichment score for this pathway.
nes - Float. The normalised enrichment score for this pathway.
pvals - Float. The p-value for this pathway.
n_more_extreme - Integer. Number of permutation that had more extreme enrichment scores than the actual.
size - Integer. The size of the pathway.
pathway_name - Character. The name of the pathway.
leading_edge - List of character vectors with the leading edge genes.
fdr - Float. The adjusted pval.
Korotkevich, et al., bioRxiv
# fgsea simple against a signature with one enriched set set.seed(42L) stats <- stats::setNames(rnorm(500), sprintf("gene_%03i", 1:500)) stats[1:20] <- stats[1:20] + 2 pathways <- list( up_set = sprintf("gene_%03i", 1:20), random_set = sprintf("gene_%03i", 200:240) ) res <- calc_fgsea_simple(stats, pathways, nperm = 1000L) res[, c("pathway_name", "es", "pvals")]# fgsea simple against a signature with one enriched set set.seed(42L) stats <- stats::setNames(rnorm(500), sprintf("gene_%03i", 1:500)) stats[1:20] <- stats[1:20] + 2 pathways <- list( up_set = sprintf("gene_%03i", 1:20), random_set = sprintf("gene_%03i", 200:240) ) res <- calc_fgsea_simple(stats, pathways, nperm = 1000L) res[, c("pathway_name", "es", "pvals")]
Rust-based version of the traditional permutation-based GSEA algorithm.
calc_gsea_traditional( stats, pathways, nperm = 2000L, gsea_params = params_gsea(), seed = 123L )calc_gsea_traditional( stats, pathways, nperm = 2000L, gsea_params = params_gsea(), seed = 123L )
stats |
Named numeric vector. The gene level statistic. |
pathways |
List. A named list with each element containing the genes for this pathway. |
nperm |
Integer. Number of permutation tests. Defaults to |
gsea_params |
List. The GSEA parameters, see
|
seed |
Random seed for reproducibility. |
A data.table with the results from the GSEA with the following columns:
es - Float. The enrichment score for this pathway.
nes - Float. The normalised enrichment score for this pathway.
pvals - Float. The p-value for this pathway.
n_more_extreme - Integer. Number of permutation that had more extreme enrichment scores than the actual.
size - Integer. The size of the pathway.
pathway_name - Character. The name of the pathway.
leading_edge - List of character vectors with the leading edge genes.
fdr - Float. The adjusted pval.
# permutation GSEA against a signature with one enriched set set.seed(42L) stats <- stats::setNames(rnorm(500), sprintf("gene_%03i", 1:500)) stats[1:20] <- stats[1:20] + 2 pathways <- list( up_set = sprintf("gene_%03i", 1:20), random_set = sprintf("gene_%03i", 200:240) ) res <- calc_gsea_traditional(stats, pathways, nperm = 1000L) res[, c("pathway_name", "es", "pvals")]# permutation GSEA against a signature with one enriched set set.seed(42L) stats <- stats::setNames(rnorm(500), sprintf("gene_%03i", 1:500)) stats[1:20] <- stats[1:20] + 2 pathways <- list( up_set = sprintf("gene_%03i", 1:20), random_set = sprintf("gene_%03i", 200:240) ) res <- calc_gsea_traditional(stats, pathways, nperm = 1000L) res[, c("pathway_name", "es", "pvals")]
Implementation of the bixverse version of the gene set variation analysis (GSVA), see Hänzelmann, et al.
calc_gsva( exp, pathways, kernel = c("gaussian", "poisson", "none"), gaussian = deprecated(), gsva_params = params_gsva(), .verbose = FALSE )calc_gsva( exp, pathways, kernel = c("gaussian", "poisson", "none"), gaussian = deprecated(), gsva_params = params_gsva(), .verbose = FALSE )
exp |
Numerical matrix. Rows represents the features, columns the features/genes. |
pathways |
List. A named list with each element containing the genes for this pathway. |
kernel |
String. One of |
gaussian |
Boolean. If set to |
gsva_params |
List. The GSVA parameters, see
|
.verbose |
Boolean. Controls verbosity. |
A matrix of shape pathways (that passed the thresholds) x samples.
see Hänzelmann, et al. Bmc Bioinformatics, 2013
# per-sample GSVA scores for two gene sets set.seed(123L) exp_mat <- matrix( rnorm(200 * 10), nrow = 200, dimnames = list(sprintf("gene_%03i", 1:200), sprintf("sample_%i", 1:10)) ) pathways <- list( set_a = sprintf("gene_%03i", 1:20), set_b = sprintf("gene_%03i", 50:80) ) dim(calc_gsva(exp_mat, pathways))# per-sample GSVA scores for two gene sets set.seed(123L) exp_mat <- matrix( rnorm(200 * 10), nrow = 200, dimnames = list(sprintf("gene_%03i", 1:200), sprintf("sample_%i", 1:10)) ) pathways <- list( set_a = sprintf("gene_%03i", 1:20), set_b = sprintf("gene_%03i", 50:80) ) dim(calc_gsva(exp_mat, pathways))
Helper function to compare the results of two SingleCellNearestNeighbour
against each other. The first one can serve as a reference (ground truth)
and you can compare against the second one.
calc_knn_metrics(ref_knn, query_knn)calc_knn_metrics(ref_knn, query_knn)
ref_knn |
The reference |
query_knn |
The query |
A list with:
matches - The intersecting indices between the reference and query kNN for each sample. In an ideal match up should be equal to k.
distance_ratio - The distance ratio. Calculates
sum(dist_query) / sum(dist_ref) per sample. Indicates how much worse the
reference is.
final_recall - The final recall across all samples.
final_ratio - The final distance ratio across all samples.
# recall of an annoy index against the default one set.seed(42L) embd <- matrix(rnorm(500 * 10), nrow = 500) rownames(embd) <- sprintf("cell_%03d", 1:500) ref <- generate_sc_knn(embd, .verbose = FALSE) query <- generate_sc_knn( embd, neighbours_params = params_sc_neighbours( knn = list(knn_method = "annoy") ), .verbose = FALSE ) calc_knn_metrics(ref, query)$final_recall# recall of an annoy index against the default one set.seed(42L) embd <- matrix(rnorm(500 * 10), nrow = 500) rownames(embd) <- sprintf("cell_%03d", 1:500) ref <- generate_sc_knn(embd, .verbose = FALSE) query <- generate_sc_knn( embd, neighbours_params = params_sc_neighbours( knn = list(knn_method = "annoy") ), .verbose = FALSE ) calc_knn_metrics(ref, query)$final_recall
This function will calculate the compactness and separation of your metacells
on the manifold (defined by the diffusion map). You must have run
calc_diffusion_coordinates() before calling this function. The idea is
that compactness indicates how tight the metacell spans the manifold, whereas
separation indicates how well the different metacells span the manifold.
calc_manifold_metrics(object)calc_manifold_metrics(object)
object |
|
The class with the compactness and separation scores added.
Persad, et al. Nat Biotechnol, 2023
# compactness and separation on top of the diffusion coordinates sc <- demo_single_cells() mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 50L), .verbose = FALSE ) mc <- calc_diffusion_coordinates( mc, knn_data = get_knn_obj(sc), .verbose = FALSE ) mc <- calc_manifold_metrics(mc) head(mc[[c("compactness", "separation")]]) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# compactness and separation on top of the diffusion coordinates sc <- demo_single_cells() mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 50L), .verbose = FALSE ) mc <- calc_diffusion_coordinates( mc, knn_data = get_knn_obj(sc), .verbose = FALSE ) mc <- calc_manifold_metrics(mc) head(mc[[c("compactness", "separation")]]) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Scores a meta cell assignment for compactness, i.e. the mean variance across
the diffusion components over the cells of each meta cell. Lower is better.
Unlike calc_manifold_metrics(), this works on any assignment, so meta
cells generated outside bixverse (or on a different object) can be scored
against an existing diffusion map.
calc_meta_cell_compactness(dcs, assignment)calc_meta_cell_compactness(dcs, assignment)
dcs |
Numeric matrix. The diffusion map coordinates, cells x
components, for example |
assignment |
Atomic vector of length |
Named numeric vector with one compactness value per meta cell,
named and ordered like split() orders assignment.
Persad, et al., Nat Biotechnol, 2023
set.seed(1L) dcs <- matrix(rnorm(200L), ncol = 2L) assignment <- rep(sprintf("mc_%i", 1:10), each = 10L) calc_meta_cell_compactness(dcs, assignment)set.seed(1L) dcs <- matrix(rnorm(200L), ncol = 2L) assignment <- rep(sprintf("mc_%i", 1:10), each = 10L) calc_meta_cell_compactness(dcs, assignment)
A potential metric to see how well the meta cells are aggregated is their cell type purity. This helper function helps to plot the meta-cell purity based on annotated cell types. These can be also just unsupervised memberships to graph-based clustering, etc.
calc_meta_cell_purity( object, original_cell_type, add_additional_info = c("none", "top_label", "top_two_labels"), add_entropy = FALSE )calc_meta_cell_purity( object, original_cell_type, add_additional_info = c("none", "top_label", "top_two_labels"), add_entropy = FALSE )
object |
|
original_cell_type |
Character vector. The original cell type
annotations of the object the meta cells came from. Either in the row order
of its full (unfiltered) obs table, i.e. |
add_additional_info |
String. Which label information to add on top of
the purity. One of |
add_entropy |
Boolean. Shall the normalised Shannon entropy of the
label distribution be added as a diversity measure. Defaults to |
Ties for the top (or second) label resolve to whichever label sorts first,
as the labels are factorised internally. The entropy is the Shannon entropy
of the label distribution within a meta cell, divided by
log(<number of distinct labels in original_cell_type>), so it sits in
[0, 1] and stays comparable between meta cells. It is 0 if there is only
a single label in the data.
The MetaCells with added columns to the observation table:
mc_purity - Fraction of the meta cell's cells that carry the most abundant label. Always added.
mc_top_label - Name of the most abundant label. Added for
add_additional_info %in% c("top_label", "top_two_labels").
mc_second_label - Name of the second most abundant label, NA for a
pure meta cell. Added for add_additional_info = "top_two_labels".
mc_second_frac - Fraction of the meta cell's cells carrying that
second label, 0 for a pure meta cell. Added for
add_additional_info = "top_two_labels".
mc_entropy - Normalised Shannon entropy of the label distribution,
see details. Added for add_entropy = TRUE.
# purity of the meta cells against the cell types they were built from sc <- demo_single_cells() mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 50L), .verbose = FALSE ) cell_types <- sc[["cell_grp"]]$cell_grp mc <- calc_meta_cell_purity(mc, original_cell_type = cell_types) head(mc[[c("meta_cell_id", "mc_purity")]]) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# purity of the meta cells against the cell types they were built from sc <- demo_single_cells() mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 50L), .verbose = FALSE ) cell_types <- sc[["cell_grp"]]$cell_grp mc <- calc_meta_cell_purity(mc, original_cell_type = cell_types) head(mc[[c("meta_cell_id", "mc_purity")]]) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Scores a meta cell assignment for separation, i.e. the Euclidean distance
from each meta cell centroid in diffusion space to the nearest other meta
cell centroid. Higher is better. Unlike calc_manifold_metrics(), this
works on any assignment, so meta cells generated outside bixverse (or on a
different object) can be scored against an existing diffusion map.
calc_meta_cell_separation(dcs, assignment)calc_meta_cell_separation(dcs, assignment)
dcs |
Numeric matrix. The diffusion map coordinates, cells x
components, for example |
assignment |
Atomic vector of length |
Named numeric vector with one separation value per meta cell,
named and ordered like split() orders assignment. A single meta cell
yields Inf.
Persad, et al., Nat Biotechnol, 2023
set.seed(1L) dcs <- matrix(rnorm(200L), ncol = 2L) assignment <- rep(sprintf("mc_%i", 1:10), each = 10L) calc_meta_cell_separation(dcs, assignment)set.seed(1L) dcs <- matrix(rnorm(200L), ncol = 2L) assignment <- rep(sprintf("mc_%i", 1:10), each = 10L) calc_meta_cell_separation(dcs, assignment)
Rust-based version of the mitch multi-contrast enrichment, see Kaspi and Ziemann. Takes in a matrix representing the contrasts you wish to test against.
calc_mitch(contrast_mat, gene_set_list, min_size = 5L)calc_mitch(contrast_mat, gene_set_list, min_size = 5L)
contrast_mat |
Numerical matrix. The rows represent the gene statistic per contrast and each column represents the contrast. |
gene_set_list |
Named list. Contains the pathways you wish to test against. |
min_size |
Integer. Minimum size of the gene set to be included. |
A data.table with the Mitch enrichment results.
Kaspi and Ziemann, Bmc Genomics, 2020
# multi-contrast enrichment over two contrasts set.seed(10L) contrast_mat <- matrix( rnorm(300 * 2), ncol = 2, dimnames = list(sprintf("gene_%03i", 1:300), c("contrast_a", "contrast_b")) ) contrast_mat[1:30, ] <- contrast_mat[1:30, ] + 1.5 gene_sets <- list( hit_set = sprintf("gene_%03i", 1:30), bg_set = sprintf("gene_%03i", 100:150) ) res <- calc_mitch(contrast_mat, gene_sets) res[, c("pathway_names", "manova_pval", "s_dist")]# multi-contrast enrichment over two contrasts set.seed(10L) contrast_mat <- matrix( rnorm(300 * 2), ncol = 2, dimnames = list(sprintf("gene_%03i", 1:300), c("contrast_a", "contrast_b")) ) contrast_mat[1:30, ] <- contrast_mat[1:30, ] + 1.5 gene_sets <- list( hit_set = sprintf("gene_%03i", 1:30), bg_set = sprintf("gene_%03i", 100:150) ) res <- calc_mitch(contrast_mat, gene_sets) res[, c("pathway_names", "manova_pval", "s_dist")]
Implements the approach from
calc_sc_type_scores( object, cell_marker_list, sensitivity = TRUE, weight_floor = NULL, .verbose = TRUE )calc_sc_type_scores( object, cell_marker_list, sensitivity = TRUE, weight_floor = NULL, .verbose = TRUE )
object |
|
cell_marker_list |
A list, see |
sensitivity |
Boolean. Shall shared marker genes be downweighted (like
in the original reference). Defaults to |
weight_floor |
Optional numeric. A value between 0 to 1 and sets the
floor for the weights if |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
An ScTypeResults results class
# ScType scores against the planted marker genes sc <- demo_single_cells() markers <- data.table::data.table( cell_type = rep(sprintf("cell_type_%i", 1:3), each = 10), gene_id = sprintf("gene_%02d", 1:30) ) cell_markers <- prepare_cell_markers(obj = sc, marker_df = markers) res <- calc_sc_type_scores( sc, cell_marker_list = cell_markers, .verbose = FALSE ) dim(get_scores(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# ScType scores against the planted marker genes sc <- demo_single_cells() markers <- data.table::data.table( cell_type = rep(sprintf("cell_type_%i", 1:3), each = 10), gene_id = sprintf("gene_%02d", 1:30) ) cell_markers <- prepare_cell_markers(obj = sc, marker_df = markers) res <- calc_sc_type_scores( sc, cell_marker_list = cell_markers, .verbose = FALSE ) dim(get_scores(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Scores all samples against one up-regulated gene set with an optional
paired down-regulated set. When n_permutations > 0, additionally runs a
permutation test and includes empirical p-values.
calc_singscore( ranks, up_set, down_set = NULL, center_score = TRUE, known_direction = TRUE, n_permutations = 0L, seed = 42L )calc_singscore( ranks, up_set, down_set = NULL, center_score = TRUE, known_direction = TRUE, n_permutations = 0L, seed = 42L )
ranks |
Numerical matrix. Output of |
up_set |
Character vector. Gene names of the up-regulated set. |
down_set |
Character vector or NULL. Optional paired down-regulated set. |
center_score |
Boolean. Centre scores around 0. Ignored when
|
known_direction |
Boolean. Whether the up-set direction is known.
Becomes irrelevant when |
n_permutations |
Integer. Number of permutations. |
seed |
Integer. RNG seed for the permutation test. |
A data.table with one row per sample. Columns: total_score,
total_dispersion, optionally up_score, up_dispersion, down_score,
down_dispersion, and (when permutations are run) pval. With
permutations, the null distribution is attached as
attr(., "null_distribution").
Foroutan et al., BMC Bioinformatics, 2018.
# score every sample against one up-regulated gene set set.seed(123L) exp_mat <- matrix( rnorm(200 * 10), nrow = 200, dimnames = list(sprintf("gene_%03i", 1:200), sprintf("sample_%i", 1:10)) ) ranks <- calc_singscore_rank(exp_mat) head(calc_singscore(ranks, up_set = sprintf("gene_%03i", 1:20)))# score every sample against one up-regulated gene set set.seed(123L) exp_mat <- matrix( rnorm(200 * 10), nrow = 200, dimnames = list(sprintf("gene_%03i", 1:200), sprintf("sample_%i", 1:10)) ) ranks <- calc_singscore_rank(exp_mat) head(calc_singscore(ranks, up_set = sprintf("gene_%03i", 1:20)))
Scores all samples against many up-regulated gene sets with optional paired down-regulated sets. Down sets are paired with up sets by name; unmatched names are dropped.
calc_singscore_multi( ranks, up_pathways, down_pathways = NULL, center_score = TRUE, known_direction = TRUE, min_size = 1L, max_size = 500L )calc_singscore_multi( ranks, up_pathways, down_pathways = NULL, center_score = TRUE, known_direction = TRUE, min_size = 1L, max_size = 500L )
ranks |
Numerical matrix. Output of |
up_pathways |
Named list of character vectors. Up-regulated gene sets. |
down_pathways |
Named list or NULL. Paired down-regulated gene sets. |
center_score |
Boolean. Centre scores around 0. Ignored when
|
known_direction |
Boolean. Whether the up-set direction is known.
Becomes irrelevant when |
min_size |
Integer. Minimum gene-set size after dropping missing genes. |
max_size |
Integer. Maximum gene-set size. |
A named list with two matrices, scores and dispersions, each of
shape gene_sets × samples.
Foroutan et al., BMC Bioinformatics, 2018.
# score every sample against several up-regulated gene sets set.seed(123L) exp_mat <- matrix( rnorm(200 * 10), nrow = 200, dimnames = list(sprintf("gene_%03i", 1:200), sprintf("sample_%i", 1:10)) ) ranks <- calc_singscore_rank(exp_mat) pathways <- list( set_a = sprintf("gene_%03i", 1:20), set_b = sprintf("gene_%03i", 50:80) ) res <- calc_singscore_multi(ranks, up_pathways = pathways) dim(res$scores)# score every sample against several up-regulated gene sets set.seed(123L) exp_mat <- matrix( rnorm(200 * 10), nrow = 200, dimnames = list(sprintf("gene_%03i", 1:200), sprintf("sample_%i", 1:10)) ) ranks <- calc_singscore_rank(exp_mat) pathways <- list( set_a = sprintf("gene_%03i", 1:20), set_b = sprintf("gene_%03i", 50:80) ) res <- calc_singscore_multi(ranks, up_pathways = pathways) dim(res$scores)
Column-ranks an expression matrix. If stable_genes is provided, uses the
stable-genes ranking method (unit-normalised ranks against a small set of
stable genes); otherwise standard column ranks. Sets attr(., "stable") so
downstream singscore functions know which bounds formula to use.
calc_singscore_rank(exp, stable_genes = NULL)calc_singscore_rank(exp, stable_genes = NULL)
exp |
Numerical matrix. Rows = genes, columns = samples. |
stable_genes |
Character vector or NULL. Gene names of stable genes.
Defaults to |
A rank matrix with the same shape as exp and attr(., "stable")
set to TRUE or FALSE.
1.) Foroutan et al., BMC Bioinformatics, 2018.; 2.) Bhuva, et al., Nucleic Acids Res., 2020
# column ranks of an expression matrix, ready for singscore set.seed(123L) exp_mat <- matrix( rnorm(200 * 10), nrow = 200, dimnames = list(sprintf("gene_%03i", 1:200), sprintf("sample_%i", 1:10)) ) ranks <- calc_singscore_rank(exp_mat) dim(ranks)# column ranks of an expression matrix, ready for singscore set.seed(123L) exp_mat <- matrix( rnorm(200 * 10), nrow = 200, dimnames = list(sprintf("gene_%03i", 1:200), sprintf("sample_%i", 1:10)) ) ranks <- calc_singscore_rank(exp_mat) dim(ranks)
Implementation of the bixverse version of the single sample gene set enrichment analysis (ssGSEA), see Barbie et al.
calc_ssgsea(exp, pathways, ssgsea_params = params_ssgsea(), .verbose = FALSE)calc_ssgsea(exp, pathways, ssgsea_params = params_ssgsea(), .verbose = FALSE)
exp |
Numerical matrix. Rows represents the features, columns the features/genes. |
pathways |
List. A named list with each element containing the genes for this pathway. |
ssgsea_params |
List. The GSVA parameters, see
|
.verbose |
Boolean. Controls verbosity. |
A matrix of shape pathways (that passed the thresholds) x samples.
Barbie et al., Nature, 2009
# per-sample ssGSEA scores for two gene sets set.seed(123L) exp_mat <- matrix( rnorm(200 * 10), nrow = 200, dimnames = list(sprintf("gene_%03i", 1:200), sprintf("sample_%i", 1:10)) ) pathways <- list( set_a = sprintf("gene_%03i", 1:20), set_b = sprintf("gene_%03i", 50:80) ) round(calc_ssgsea(exp_mat, pathways)[, 1:3], 3)# per-sample ssGSEA scores for two gene sets set.seed(123L) exp_mat <- matrix( rnorm(200 * 10), nrow = 200, dimnames = list(sprintf("gene_%03i", 1:200), sprintf("sample_%i", 1:10)) ) pathways <- list( set_a = sprintf("gene_%03i", 1:20), set_b = sprintf("gene_%03i", 50:80) ) round(calc_ssgsea(exp_mat, pathways)[, 1:3], 3)
This is a deprecated method and will raise an error. Please use
calculate_dge_limma() and calculate_dge_hedges().
calculate_all_dges( object, contrast_column, contrast_list = NULL, filter_column = NULL, co_variates = NULL, small_sample_correction = NULL, ..., .verbose = TRUE )calculate_all_dges( object, contrast_column, contrast_list = NULL, filter_column = NULL, co_variates = NULL, small_sample_correction = NULL, ..., .verbose = TRUE )
object |
The underlying class, see |
contrast_column |
String. The contrast column in which the groupings are stored. Needs to be found in the meta_data within the properties. |
contrast_list |
Optional string vector. A vectors that contains the
contrast formatted as |
filter_column |
Optional String. If there is a column you wish to use as sub groupings, this can be provided here. An example could be different sampled tissues and you wish to run the DGE analyses within each tissue separately. |
co_variates |
Optional string vector. Any co-variates you wish to consider during the Limma Voom modelling. |
small_sample_correction |
Can be NULL (automatic determination if a small sample size correction should be applied) or a Boolean. |
... |
Additional parameters that used to go to limma's |
.verbose |
Controls verbosity of the function. |
Throws an error, as it is deprecated.
Computes the average silhouette width (ASW) on batch labels using pairwise Euclidean distances in the embedding space. For each cell, the function estimates the mean distance to cells of the same batch (a) and the mean distance to cells of the nearest other batch (b), then computes s = (b - a) / max(a, b).
Values near 0 indicate good batch mixing, values near 1 indicate batch
separation, and negative values suggest overcorrection. This metric is
best suited for embedding-based correction methods (e.g. Harmony, fastMNN).
For graph-based methods like BBKNN, consider using
calculate_lisi_sc() instead.
calculate_batch_asw_sc( object, batch_column, embd_to_use = "pca", max_cells = 5000L, seed = 42L, .verbose = TRUE )calculate_batch_asw_sc( object, batch_column, embd_to_use = "pca", max_cells = 5000L, seed = 42L, .verbose = TRUE )
object |
|
batch_column |
String. The column with the batch information in the obs data of the class. |
embd_to_use |
String. Which embedding to compute the ASW on. One of
|
max_cells |
Integer or |
seed |
Integer. Seed for subsampling reproducibility. |
.verbose |
Boolean. Controls verbosity of the function. |
A BatchSilhouetteScores object with the following elements
per_cell - Per-cell silhouette scores in [-1, 1].
mean_asw - Mean silhouette width across all cells.
median_asw - Median silhouette width across all cells.
n_batches - Number of batches in the data.
embedding_used - Which embedding the ASW was computed on.
# batch silhouette width on the PCA embedding sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_batch_asw_sc( sc, batch_column = "batch_index", .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# batch silhouette width on the PCA embedding sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_batch_asw_sc( sc, batch_column = "batch_index", .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Average silhouette width on cell type labels in the embedding, rescaled to
[0, 1] via (s + 1) / 2 as in scIB. Higher values mean cell types stay
separated after correction. Counterpart to
calculate_batch_asw_sc() on the biology side.
calculate_cell_type_asw_sc( object, cell_type_column, embd_to_use = "pca", max_cells = 5000L, seed = 42L, .verbose = TRUE )calculate_cell_type_asw_sc( object, cell_type_column, embd_to_use = "pca", max_cells = 5000L, seed = 42L, .verbose = TRUE )
object |
|
cell_type_column |
String. The column with the cell type labels in the obs data of the class. |
embd_to_use |
String. Which embedding to compute the ASW on. Defaults
to |
max_cells |
Integer or |
seed |
Integer. Seed for subsampling reproducibility. |
.verbose |
Boolean. Controls verbosity of the function. |
A CellTypeAswScores object with the following elements
per_cell - Per-cell rescaled silhouette scores in [0, 1].
mean_asw - Mean rescaled silhouette width.
median_asw - Median rescaled silhouette width.
n_cell_types - Number of cell types.
embedding_used - Which embedding the ASW was computed on.
Luecken, et al., Nat. Methods, 2022
# cell type silhouette width on the PCA embedding sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_cell_type_asw_sc( sc, cell_type_column = "cell_grp", .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# cell type silhouette width on the PCA embedding sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_cell_type_asw_sc( sc, cell_type_column = "cell_grp", .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function calculates the critical value for a given ontology similarity matrix.
calculate_critical_value(x, alpha, permutations = 100000L, seed = 10101L)calculate_critical_value(x, alpha, permutations = 100000L, seed = 10101L)
x |
Numerical matrix or |
alpha |
Float. The alpha value. For example, 0.001 would mean that the critical value is smaller than 0.1 percentile of the random permutations. |
permutations |
Number of random permutations. |
seed |
Integer. For reproducibility purposes |
The critical value.
# critical value of a Wang similarity matrix at alpha 0.1 onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) weights <- c(part_of = 0.8, is_a = 0.6) sim_mat <- calculate_wang_sim_mat(onto, weights = weights) calculate_critical_value(sim_mat, alpha = 0.1)# critical value of a Wang similarity matrix at alpha 0.1 onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) weights <- c(part_of = 0.8, is_a = 0.6) sim_mat <- calculate_wang_sim_mat(onto, weights = weights) calculate_critical_value(sim_mat, alpha = 0.1)
This function will calculate the Hedge's G effect size on the normalised
counts. Should batch-corrected counts be found, these will be used. At a
minimum you will need to provide contrast_column that can be found in the
meta-data. If you do not provide a vector of contrasts that you wish to test
for, every permutation of groups represented in that column will be tested
against each other.
calculate_dge_hedges( object, contrast_column, contrast_list = NULL, filter_column = NULL, .verbose = TRUE )calculate_dge_hedges( object, contrast_column, contrast_list = NULL, filter_column = NULL, .verbose = TRUE )
object |
The underlying class, see |
contrast_column |
String. The contrast column in which the groupings are stored. Needs to be found in the meta_data within the properties. |
contrast_list |
Optional string vector. A vectors that contains the
contrast formatted as |
filter_column |
Optional String. If there is a column you wish to use as sub groupings, this can be provided here. An example could be different sampled tissues and you wish to run the DGE analyses within each tissue separately in the data. |
.verbose |
Controls verbosity of the function. |
Returns the class with additional data added to the outputs.
# effect sizes over every contrast in the case_control column syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_dge_hedges( object, contrast_column = "case_control", .verbose = FALSE ) head(get_dge_effect_sizes(object))# effect sizes over every contrast in the case_control column syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_dge_hedges( object, contrast_column = "case_control", .verbose = FALSE ) head(get_dge_effect_sizes(object))
This function will apply the Limma Voom DGE workflow. At a minimum you will
need to provide contrast_column that can be found in the meta-data. If you
do not provide a vector of contrasts that you wish to test for, every
permutation of groups represented in that column will be tested against each
other.
calculate_dge_limma( object, contrast_column, contrast_list = NULL, filter_column = NULL, co_variates = NULL, limma_params = params_limma_voom(), .verbose = TRUE )calculate_dge_limma( object, contrast_column, contrast_list = NULL, filter_column = NULL, co_variates = NULL, limma_params = params_limma_voom(), .verbose = TRUE )
object |
The underlying class, see |
contrast_column |
String. The contrast column in which the groupings are stored. Needs to be found in the meta_data within the properties. |
contrast_list |
Optional string vector. A vectors that contains the
contrast formatted as |
filter_column |
Optional String. If there is a column you wish to use as sub groupings, this can be provided here. An example could be different sampled tissues and you wish to run the DGE analyses within each tissue separately in the data. |
co_variates |
Optional string vector. Any co-variates you wish to consider during the Limma Voom modelling. |
limma_params |
List. The limma parameters, see
|
.verbose |
Controls verbosity of the function. |
Returns the class with additional data added to the outputs.
# limma voom over every contrast in the case_control column syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_dge_limma( object, contrast_column = "case_control", .verbose = FALSE ) head(get_dge_limma_voom(object))# limma voom over every contrast in the case_control column syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_dge_limma( object, contrast_column = "case_control", .verbose = FALSE ) head(get_dge_limma_voom(object))
This functions can take a given NetworkDiffusions object and calculates an
AUC and generates a Z-score based on random permutation of random_aucs for
test for statistical significance if desired.
calculate_diffusion_auc( object, hit_nodes, auc_iters = 10000L, random_aucs = 1000L, permutation_test = FALSE, seed = 42L )calculate_diffusion_auc( object, hit_nodes, auc_iters = 10000L, random_aucs = 1000L, permutation_test = FALSE, seed = 42L )
object |
|
hit_nodes |
String vector. Which nodes in the graph are considered a 'hit'. |
auc_iters |
Integer. How many iterations to run to approximate the AUROC. |
random_aucs |
Integer. How many random AUROCs to calculate to estimate
the Z-score. Only of relevance if permutation test is set to |
permutation_test |
Boolean. Shall a permutation based Z-score be calculated. |
seed |
Integer. Random seed. |
List with AUC and Z-score as the two named elements if permutations test set to TRUE; otherwise just the AUC.
# AUROC of the diffusion score against two known hit nodes set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes(object, c(node_1 = 1, node_3 = 1), "max") calculate_diffusion_auc(object, hit_nodes = c("node_2", "node_4"))# AUROC of the diffusion score against two known hit nodes set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes(object, c(node_1 = 1, node_3 = 1), "max") calculate_diffusion_auc(object, hit_nodes = c("node_2", "node_4"))
This function takes two matrices in and calculate on a per column basis the Hedge's G effect size and the standard error. These results can be subsequently used for meta-analyses or other approaches.
calculate_effect_size( mat_a, mat_b, small_sample_correction = NULL, .verbose = TRUE )calculate_effect_size( mat_a, mat_b, small_sample_correction = NULL, .verbose = TRUE )
mat_a |
Numerical matrix. Contains the values for group a. Assumes that rows = samples, and columns = features. |
mat_b |
Numerical matrix. Contains the values for group b. |
small_sample_correction |
Can be NULL (automatic determination if a small sample size correction should be applied) or Boolean. |
.verbose |
Boolean that controls verbosity of the function. |
x, robustly scaled.
# Hedge's G between two groups of ten samples set.seed(42) mat_a <- matrix(rnorm(100), nrow = 10, ncol = 10) mat_b <- matrix(rnorm(100, mean = 1), nrow = 10, ncol = 10) colnames(mat_a) <- colnames(mat_b) <- sprintf("gene_%i", 1:10) res <- calculate_effect_size(mat_a, mat_b, .verbose = FALSE) head(res$effect_sizes)# Hedge's G between two groups of ten samples set.seed(42) mat_a <- matrix(rnorm(100), nrow = 10, ncol = 10) mat_b <- matrix(rnorm(100, mean = 1), nrow = 10, ncol = 10) colnames(mat_a) <- colnames(mat_b) <- sprintf("gene_%i", 1:10) res <- calculate_effect_size(mat_a, mat_b, .verbose = FALSE) head(res$effect_sizes)
For each cell type, restricts the kNN graph to that cell type and takes the fraction of its cells in the largest connected component. A cell type split across batches after correction falls apart into several components and scores low. 1 means every cell type is one connected piece.
calculate_graph_connectivity_sc(object, cell_type_column, .verbose = TRUE)calculate_graph_connectivity_sc(object, cell_type_column, .verbose = TRUE)
object |
|
cell_type_column |
String. The column with the cell type labels in the obs data of the class. |
.verbose |
Boolean. Controls verbosity of the function. |
A GraphConnectivityScores object with the following elements
per_cell_type - Named numeric. Connectivity per cell type.
mean_connectivity - Mean connectivity across cell types.
median_connectivity - Median connectivity across cell types.
Luecken, et al., Nat. Methods, 2022
# connectivity of each cell type in the kNN graph sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_graph_connectivity_sc(sc, cell_type_column = "cell_grp") unlink(sc@dir_data, recursive = TRUE, force = TRUE)# connectivity of each cell type in the kNN graph sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_graph_connectivity_sc(sc, cell_type_column = "cell_grp") unlink(sc@dir_data, recursive = TRUE, force = TRUE)
this function will calculate the information content of each
provided term based on a list of ancestors, which is a named list of terms
with ancestor identifiers as their values. Can be calculated using
get_ontology_ancestry(). The information content is calculated
as -log2(number descendant/total terms in the ontology). More information
can be found here.
calculate_information_content(ancestor_list)calculate_information_content(ancestor_list)
ancestor_list |
List. Named list of terms with ancestor identifiers as their values |
A named list of each term and their information content as values.
# information content from the descendant counts of a toy ontology onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f") ) ancestry <- get_ontology_ancestry(onto) unlist(calculate_information_content(ancestry$descendants))# information content from the descendant counts of a toy ontology onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f") ) ancestry <- get_ontology_ancestry(onto) unlist(calculate_information_content(ancestry$descendants))
Runs the batch mixing and (if cell type labels are given) the biological
conservation metrics in one go and returns one row per call, so results
across correction methods can be rbind-ed into one table. Every column is
on [0, 1] (PCR comparison can go negative) and higher is better, the
scIB convention:
Batch mixing:
kbet_accept - 1 - kBET rejection rate.
batch_asw - mean(1 - |s|) over the per-cell batch silhouettes.
ilisi - Normalised iLISI.
pcr_comparison - (pre - post) / pre of the batch PCR.
Biological conservation:
clisi - Normalised cLISI.
cell_type_asw - Rescaled cell type silhouette width.
graph_connectivity - Mean graph connectivity over cell types.
The kNN metrics read the kNN graph currently stored in the object, so
recompute the neighbours on the corrected embedding first. Embedding metrics
are NA if embd_to_use = NULL (e.g. BBKNN, which only returns a graph).
calculate_integration_metrics_sc( object, batch_column, cell_type_column = NULL, embd_to_use = "pca", max_cells = 5000L, seed = 42L, .verbose = TRUE )calculate_integration_metrics_sc( object, batch_column, cell_type_column = NULL, embd_to_use = "pca", max_cells = 5000L, seed = 42L, .verbose = TRUE )
object |
|
batch_column |
String. The column with the batch information in the obs data of the class. |
cell_type_column |
Optional string. The column with the cell type
labels. If |
embd_to_use |
Optional string. The embedding for ASW and PCR. Defaults
to |
max_cells |
Integer or |
seed |
Integer. Seed for subsampling reproducibility. |
.verbose |
Boolean. Controls verbosity of the function. |
A one-row data.table with the columns embedding, kbet_accept,
batch_asw, ilisi, pcr_comparison, clisi, cell_type_asw and
graph_connectivity.
Luecken, et al., Nat. Methods, 2022
# all metrics on the uncorrected PCA sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_integration_metrics_sc( sc, batch_column = "batch_index", cell_type_column = "cell_grp", .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# all metrics on the uncorrected PCA sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_integration_metrics_sc( sc, batch_column = "batch_index", cell_type_column = "cell_grp", .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function calculates the k-nearest neighbour batch-effect test (kBET). Briefly, the function leverages a Chi-Square statistic to calculate the differences in batch proportions observed in the neighbourhood of a given cell with the overall batch proportions. If the test is significant for that cell it indicates poor mixing for that cell specifically. Large number of positive tests indicate bad mixing overall. For more details, please see Büttner et al.
calculate_kbet_sc(object, batch_column, threshold = 0.05, .verbose = TRUE)calculate_kbet_sc(object, batch_column, threshold = 0.05, .verbose = TRUE)
object |
|
batch_column |
String. The column with the batch information in the obs data of the class. |
threshold |
Numeric. Number between 0 and 1. Below this threshold, the
test is considered significant. Defaults to |
.verbose |
Boolean. Controls verbosity of the function. |
A KbetScores object with the following elements
kbet_score - Proportion of significant tests over all cells. 0 indicates perfect mixing, 1 indicates no mixing between batches.
significant_tests - Logical vector indicating for which cells the test was below the threshold.
p_values - The p-values from the Chi-Square test.
chi_square_stats - Per-cell Chi-Square statistics.
mean_chi_square - Mean Chi-Square statistic across all cells.
median_chi_square - Median Chi-Square statistic across all cells.
threshold - The significance threshold used.
n_batches - Number of batches in the data.
Büttner, et al., Nat. Methods, 2019
# kBET rejection rate over three batches sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_kbet_sc(sc, batch_column = "batch_index", .verbose = FALSE) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# kBET rejection rate over three batches sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_kbet_sc(sc, batch_column = "batch_index", .verbose = FALSE) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Computes the Local Inverse Simpson's Index (LISI) on the kNN graph: the effective number of labels in each cell's neighbourhood. On batch labels this is iLISI, where higher means better mixing. On cell type labels it is cLISI, where lower means cell types stay apart. Unlike kBET, LISI does not compare against global proportions, so it also works on graph-based corrections like BBKNN.
The normalised score follows scIB and lands in [0, 1], higher is better
for both: iLISI as (median - 1) / (n - 1), cLISI as
(n - median) / (n - 1).
calculate_lisi_sc( object, label_column, type = c("batch", "cell_type"), weighted = FALSE, perplexity = 30, .verbose = TRUE )calculate_lisi_sc( object, label_column, type = c("batch", "cell_type"), weighted = FALSE, perplexity = 30, .verbose = TRUE )
object |
|
label_column |
String. The column with the batch or cell type labels in the obs data of the class. |
type |
String. One of |
weighted |
Boolean. Weight the neighbours with a perplexity-calibrated
Gaussian kernel on the kNN distances, as in Korsunsky et al. If |
perplexity |
Numeric. Perplexity for the weighted version. Defaults to
|
.verbose |
Boolean. Controls verbosity of the function. |
A LisiScores object with the following elements
per_cell - Per-cell LISI scores in [1, n_labels].
mean_lisi - Mean LISI across all cells.
median_lisi - Median LISI across all cells.
lisi_norm - The normalised score in [0, 1], higher is better.
n_labels - Number of distinct labels.
type - "batch" (iLISI) or "cell_type" (cLISI).
Korsunsky, et al., Nat. Methods, 2019; Luecken, et al., Nat. Methods, 2022
# iLISI over the kNN graph sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_lisi_sc( sc, label_column = "batch_index", .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# iLISI over the kNN graph sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_lisi_sc( sc, label_column = "batch_index", .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function will run PCA - via (randomised) SVD - on the normalised counts and add the PCA results to the ScCache for the ADT counts.
calculate_pca_adt_sc( object, no_pcs, features = NULL, randomised_svd = FALSE, seed = 42L )calculate_pca_adt_sc( object, no_pcs, features = NULL, randomised_svd = FALSE, seed = 42L )
object |
|
no_pcs |
Integer. Number of PCs to calculate. |
features |
Optional string vector. If you want to subset to a specific set of ADT probes (for example to exclude isotypes). |
randomised_svd |
Boolean. Shall randomised SVD be used. Faster, but less precise. |
seed |
Integer. Controls reproducibility. Only relevant if
|
The function will add the PCA factors, loadings and singular values for the ADT data to the object.
# PCA over the CLR-normalised protein counts rna <- generate_single_cell_test_data() adt <- generate_single_cell_test_data_adt() dir <- tempfile("bixverse_mm") dir.create(dir) object <- load_r_data( SingleCellsMultiModal(dir_data = dir), counts = rna$counts, obs = rna$obs, var = rna$var, sc_qc_param = params_sc_min_quality(min_unique_genes = 5L), .verbose = FALSE ) object <- add_adt_counts_sc(object, adt_counts = adt$counts, method = "clr") object <- calculate_pca_adt_sc(object, no_pcs = 10L) dim(get_pca_factors(object, modality = "adt")) unlink(dir, recursive = TRUE, force = TRUE)# PCA over the CLR-normalised protein counts rna <- generate_single_cell_test_data() adt <- generate_single_cell_test_data_adt() dir <- tempfile("bixverse_mm") dir.create(dir) object <- load_r_data( SingleCellsMultiModal(dir_data = dir), counts = rna$counts, obs = rna$obs, var = rna$var, sc_qc_param = params_sc_min_quality(min_unique_genes = 5L), .verbose = FALSE ) object <- add_adt_counts_sc(object, adt_counts = adt$counts, method = "clr") object <- calculate_pca_adt_sc(object, no_pcs = 10L) dim(get_pca_factors(object, modality = "adt")) unlink(dir, recursive = TRUE, force = TRUE)
Calculates the principal component on top of the filtered count matrix and adds the information of the first 10 principal components to the outputs.
calculate_pca_bulk_dge( object, scale_genes = FALSE, pcs = 10L, no_hvg_genes = 2500L )calculate_pca_bulk_dge( object, scale_genes = FALSE, pcs = 10L, no_hvg_genes = 2500L )
object |
The underlying class, see |
scale_genes |
Boolean. Shall the log(cpm) counts be scaled prior the PCA
calculation. Defaults to |
pcs |
Integer. Number of PCs to return and add to the outputs slot. |
no_hvg_genes |
Integer. Number of highly variable genes to include. Defaults to 2500. |
Returns the class with additional data added to the outputs.
# PCA over the most variable genes of the normalised counts syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_pca_bulk_dge(object, no_hvg_genes = 500L) head(get_outputs(object)$pca)# PCA over the most variable genes of the normalised counts syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_pca_bulk_dge(object, no_hvg_genes = 500L) head(get_outputs(object)$pca)
This function will run PCA on the detected highly variable genes. The
solver is set via svd_solver in params_sc_pca(); the default builds the
gene x gene cross-product from the sparse data, which is exact and the
fastest option for a few thousand HVGs.
calculate_pca_sc( object, no_pcs, pca_params = params_sc_pca(), sparse_svd = NULL, hvg = NULL, seed = 42L, residuals = FALSE, .verbose = TRUE )calculate_pca_sc( object, no_pcs, pca_params = params_sc_pca(), sparse_svd = NULL, hvg = NULL, seed = 42L, residuals = FALSE, .verbose = TRUE )
object |
|
no_pcs |
Integer. Number of PCs to calculate. |
pca_params |
Named list. Controls the parameters to be used for the
PCA calculation which is single cell-specific, see |
sparse_svd |
Optional boolean. Solve on the sparse data with implicit
centring and scaling instead of materialising the dense scaled matrix.
|
hvg |
Optional integer. If you want to provide your own HVG genes.
Otherwise, the function will default to what is found in
|
seed |
Integer. Controls reproducibility. Only relevant for
|
residuals |
Boolean. Run the PCA on the Pearson residuals of a model
fitted with |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The function will add the PCA factors, loadings and singular values to the object cache in memory.
# PCA on the highly variable genes sc <- demo_single_cells(prepped = FALSE) sc <- find_hvg_sc(sc, hvg_no = 30L, .verbose = FALSE) sc <- calculate_pca_sc(sc, no_pcs = 10L, .verbose = FALSE) dim(get_pca_factors(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# PCA on the highly variable genes sc <- demo_single_cells(prepped = FALSE) sc <- find_hvg_sc(sc, hvg_no = 30L, .verbose = FALSE) sc <- calculate_pca_sc(sc, no_pcs = 10L, .verbose = FALSE) dim(get_pca_factors(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Regresses each embedding dimension on the batch labels and weights the
per-dimension R-squared by the variance of that dimension. On its own the
number says how much of the embedding variance batch explains. For a
corrected embedding, the function also runs it on the uncorrected PCA and
reports the scIB comparison (pre - post) / pre: 1 means the batch
variance is gone, 0 means nothing changed, negative means it got worse.
calculate_pcr_sc(object, batch_column, embd_to_use = "pca", .verbose = TRUE)calculate_pcr_sc(object, batch_column, embd_to_use = "pca", .verbose = TRUE)
object |
|
batch_column |
String. The column with the batch information in the obs data of the class. |
embd_to_use |
String. Which embedding to compute the PCR on. Defaults
to |
.verbose |
Boolean. Controls verbosity of the function. |
A PcrScores object with the following elements
pcr - Variance-weighted R-squared of batch on embd_to_use.
pcr_pca - The same on the uncorrected PCA.
pcr_comparison - (pcr_pca - pcr) / pcr_pca. NA if
embd_to_use = "pca".
var_explained - Variance per embedding dimension.
r_squared - Batch R-squared per embedding dimension.
embedding_used - Which embedding the PCR was computed on.
Büttner, et al., Nat. Methods, 2019; Luecken, et al., Nat. Methods, 2022
# share of PCA variance explained by batch sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_pcr_sc(sc, batch_column = "batch_index") unlink(sc@dir_data, recursive = TRUE, force = TRUE)# share of PCA variance explained by batch sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) calculate_pcr_sc(sc, batch_column = "batch_index") unlink(sc@dir_data, recursive = TRUE, force = TRUE)
RPKM calculation
calculate_rpkm(counts, gene_lengths)calculate_rpkm(counts, gene_lengths)
counts |
Numeric matrix. Count matrix (gene x sample) |
gene_lengths |
Named vector. Named vector with gene lengths. |
RPKM-normalised matrix.
# RPKM over synthetic counts with flat 2kb gene lengths syn <- synthetic_bulk_cor_matrix() gene_lengths <- stats::setNames( rep(2000, nrow(syn$counts)), rownames(syn$counts) ) rpkm <- calculate_rpkm(syn$counts, gene_lengths) rpkm[1:3, 1:3]# RPKM over synthetic counts with flat 2kb gene lengths syn <- synthetic_bulk_cor_matrix() gene_lengths <- stats::setNames( rep(2000, nrow(syn$counts)), rownames(syn$counts) ) rpkm <- calculate_rpkm(syn$counts, gene_lengths) rpkm[1:3, 1:3]
This function calculates the semantic similarities based on Resnik or Lin similarity for a given ontology. Has also the option to calculate a combined version of the two.
calculate_semantic_sim( terms, similarity_type, ancestor_list, ic_list, add_self = FALSE )calculate_semantic_sim( terms, similarity_type, ancestor_list, ic_list, add_self = FALSE )
terms |
Vector of strings. The terms in the ontology you wish to screen. |
similarity_type |
String. One of |
ancestor_list |
List. Names being the term and the elements in the
list the names of the ancestors, see |
ic_list |
List. The names being the term and the elements the
information content of this given term. Needs to be a single float! See
|
add_self |
Boolean. Shall self-similarities be added. Defaults to
|
A data.table with the calculated similarities.
# Lin similarity for a subset of terms only onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f") ) ancestry <- get_ontology_ancestry(onto) ic <- calculate_information_content(ancestry$descendants) calculate_semantic_sim( terms = c("c", "d", "f"), similarity_type = "lin", ancestor_list = ancestry$ancestors, ic_list = ic )# Lin similarity for a subset of terms only onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f") ) ancestry <- get_ontology_ancestry(onto) ic <- calculate_information_content(ancestry$descendants) calculate_semantic_sim( terms = c("c", "d", "f"), similarity_type = "lin", ancestor_list = ancestry$ancestors, ic_list = ic )
This function calculates the semantic similarities based on Resnik or Lin similarity for a given ontology. Has also the option to calculate a combined version of the two. Returns the full matrix of the data.
calculate_semantic_sim_mat(similarity_type, ancestor_list, ic_list)calculate_semantic_sim_mat(similarity_type, ancestor_list, ic_list)
similarity_type |
String. One of |
ancestor_list |
List. Names being the term and the elements in the
list the names of the ancestors, see |
ic_list |
List. The names being the term and the elements the
information content of this given term. Needs to be a single float! See
|
The symmetric similarity matrix for the specified data from the ontology.
# Resnik similarity matrix over a toy ontology onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f") ) ancestry <- get_ontology_ancestry(onto) ic <- calculate_information_content(ancestry$descendants) round(calculate_semantic_sim_mat("resnik", ancestry$ancestors, ic), 3)# Resnik similarity matrix over a toy ontology onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f") ) ancestry <- get_ontology_ancestry(onto) ic <- calculate_information_content(ancestry$descendants) round(calculate_semantic_sim_mat("resnik", ancestry$ancestors, ic), 3)
This function calculates the specified semantic similarities for the whole ontology and adds it to the class.
calculate_semantic_sim_onto( object, sim_type = c("resnik", "lin", "combined"), .verbose = TRUE )calculate_semantic_sim_onto( object, sim_type = c("resnik", "lin", "combined"), .verbose = TRUE )
object |
|
sim_type |
String. One of |
.verbose |
Boolean. Controls the verbosity of the function. |
The class with added semantic similarities to the properties.
# Resnik similarities for the whole ontology stored in the class onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) onto_obj <- pre_process_sim_onto( OntologySim(onto, .verbose = FALSE), .verbose = FALSE ) calculate_semantic_sim_onto(onto_obj, sim_type = "resnik", .verbose = FALSE)# Resnik similarities for the whole ontology stored in the class onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) onto_obj <- pre_process_sim_onto( OntologySim(onto, .verbose = FALSE), .verbose = FALSE ) calculate_semantic_sim_onto(onto_obj, sim_type = "resnik", .verbose = FALSE)
Helper function to calculate the induced sparsity
calculate_sparsity_stats(object, no_exp_bins = 10L)calculate_sparsity_stats(object, no_exp_bins = 10L)
object |
|
no_exp_bins |
Integer. Number of expression bins to check. Defaults to
|
A list with various statistics about the sparsity
original_sparsity - Original proportion of zeroes in the counts.
final_sparsity - Sparsity after applying
simulate_dropouts().
added_sparsity - Added sparsity.
gene_sparsity_mean - Mean sparsity for the genes.
gene_sparsity_sd - SD sparsity form the genes.
sample_sparsity_mean - Mean sparsity for the genes.
sample_sparsity_sd - SD sparsity for the genes.
dropout_by_expression - Dropout per expression bin level.
# how much sparsity the dropout simulation actually added syn <- synthetic_bulk_cor_matrix() syn <- simulate_dropouts(syn, params_bulk_sparsity()) stats <- calculate_sparsity_stats(syn) stats$added_sparsity stats$dropout_by_expression# how much sparsity the dropout simulation actually added syn <- synthetic_bulk_cor_matrix() syn <- simulate_dropouts(syn, params_bulk_sparsity()) stats <- calculate_sparsity_stats(syn) stats$added_sparsity stats$dropout_by_expression
Calculate the TOM from a correlation matrix
calculate_tom(cor_mat, signed, version = c("v1", "v2"))calculate_tom(cor_mat, signed, version = c("v1", "v2"))
cor_mat |
Numerical matrix. The symmetric correlation matrix. |
signed |
Boolean. Do you want to calculate the signed version. If set
to |
version |
String. One of |
Calculates the topological overlap matrix from a correlation matrix. The TOM is defined as:
Unsigned, v1:
Signed, v1:
Unsigned, v2:
Signed, v2:
where is the affinity between nodes and ,
and is the connectivity of node .
For signed networks, connectivity is calculated as .
Version 2 uses a different normalisation approach that scales the shared neighbour contribution separately before combining it with the direct connection strength.
A symmetric matrix of the same dimensions as cor_mat containing
the topological overlap measures.
# unsigned TOM from a small correlation matrix set.seed(42) cor_mat <- cor(matrix(rnorm(200), nrow = 20, ncol = 10)) tom <- calculate_tom(cor_mat, signed = FALSE) dim(tom)# unsigned TOM from a small correlation matrix set.seed(42) cor_mat <- cor(matrix(rnorm(200), nrow = 20, ncol = 10)) tom <- calculate_tom(cor_mat, signed = FALSE) dim(tom)
Calculate the TOM from an expression matrix
calculate_tom_from_exp(x, signed, version, cor_method)calculate_tom_from_exp(x, signed, version, cor_method)
x |
Numerical matrix. The expression matrix. Assumes that columns are the genes, and rows the samples. |
signed |
Boolean. Do you want to calculate the signed version. If set
to |
version |
String. One of |
cor_method |
String. One of |
Calculates the topological overlap matrix from an expression matrix. It will first calculate the specified correlation matrix and then generate the TOM. The TOM is defined as:
Unsigned, v1:
Signed, v1:
Unsigned, v2:
Signed, v2:
where is the affinity between nodes and ,
and is the connectivity of node .
For signed networks, connectivity is calculated as .
Version 2 uses a different normalisation approach that scales the shared neighbour contribution separately before combining it with the direct connection strength.
The topological overlap matrix.
# TOM straight from a samples x genes expression matrix set.seed(42) x <- matrix(rnorm(200), nrow = 20, ncol = 10) tom <- calculate_tom_from_exp( x, signed = FALSE, version = "v1", cor_method = "pearson" ) dim(tom)# TOM straight from a samples x genes expression matrix set.seed(42) x <- matrix(rnorm(200), nrow = 20, ncol = 10) tom <- calculate_tom_from_exp( x, signed = FALSE, version = "v1", cor_method = "pearson" ) dim(tom)
TPM calculation
calculate_tpm(counts, gene_lengths)calculate_tpm(counts, gene_lengths)
counts |
Numeric matrix. Count matrix (gene x sample) |
gene_lengths |
Named vector. Named vector with gene lengths. |
TPM-normalised matrix.
# TPM over synthetic counts with flat 2kb gene lengths syn <- synthetic_bulk_cor_matrix() gene_lengths <- stats::setNames( rep(2000, nrow(syn$counts)), rownames(syn$counts) ) tpm <- calculate_tpm(syn$counts, gene_lengths) colSums(tpm)[1:3]# TPM over synthetic counts with flat 2kb gene lengths syn <- synthetic_bulk_cor_matrix() gene_lengths <- stats::setNames( rep(2000, nrow(syn$counts)), rownames(syn$counts) ) tpm <- calculate_tpm(syn$counts, gene_lengths) colSums(tpm)[1:3]
This function calculates the Wang similarity for a set of temrs, based on the DAG for a given ontology.
calculate_wang_sim(terms, parent_child_dt, weights, add_self = FALSE)calculate_wang_sim(terms, parent_child_dt, weights, add_self = FALSE)
terms |
String vector. The terms for which to calculate the Wang similarity. |
parent_child_dt |
data.table. The data.table with column parent and child. You also need to have a type column for the Wang similarity to provide the weights for the relationships. |
weights |
Named numeric. The relationship of type to weight for this
specific edge. For example |
add_self |
Boolean. Shall self-similarities be added. Defaults to
|
A data.table with the calculated similarities.
# Wang similarity for a subset of terms only onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) weights <- c("part_of" = 0.8, "is_a" = 0.6) calculate_wang_sim(c("c", "d", "f"), onto, weights = weights)# Wang similarity for a subset of terms only onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) weights <- c("part_of" = 0.8, "is_a" = 0.6) calculate_wang_sim(c("c", "d", "f"), onto, weights = weights)
This function calculates the Wang similarity, based on the DAG for a given ontology. This function will return the full similarity matrix.
calculate_wang_sim_mat(parent_child_dt, weights)calculate_wang_sim_mat(parent_child_dt, weights)
parent_child_dt |
data.table. The data.table with column parent and child. You also need to have a type column for the Wang similarity to provide the weights for the relationships. |
weights |
Named numeric. The relationship of type to weight for this
specific edge. For example |
The symmetric Wang similarity matrix.
# Wang similarity matrix with relationship-specific weights onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) weights <- c("part_of" = 0.8, "is_a" = 0.6) round(calculate_wang_sim_mat(onto, weights = weights), 3)# Wang similarity matrix with relationship-specific weights onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) weights <- c("part_of" = 0.8, "is_a" = 0.6) round(calculate_wang_sim_mat(onto, weights = weights), 3)
This function calculates the Wang similarity for the whole ontology and adds it to the class in a memory-efficient format for subsequent usage.
calculate_wang_sim_onto(object, weights, .verbose = TRUE)calculate_wang_sim_onto(object, weights, .verbose = TRUE)
object |
|
weights |
Named numeric. The relationship of type to weight for this
specific edge. For example |
.verbose |
Boolean. Controls the verbosity of the function. |
The class with added semantic similarities to the properties.
# Wang similarities for the whole ontology stored in the class onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) onto_obj <- OntologySim(onto, .verbose = FALSE) calculate_wang_sim_onto( onto_obj, weights = c(part_of = 0.8, is_a = 0.6), .verbose = FALSE )# Wang similarities for the whole ontology stored in the class onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) onto_obj <- OntologySim(onto, .verbose = FALSE) calculate_wang_sim_onto( onto_obj, weights = c(part_of = 0.8, is_a = 0.6), .verbose = FALSE )
Updates doublet calls and associated summary statistics in a ScrubletRes
object using a user-supplied threshold. Intended for use after visual
inspection of the score histograms via plot.ScrubletRes().
call_doublets_manual( scrublet_res, threshold, for_sample = NULL, .verbose = TRUE )call_doublets_manual( scrublet_res, threshold, for_sample = NULL, .verbose = TRUE )
scrublet_res |
A |
threshold |
Numeric in |
for_sample |
Optional character. For grouped results, the name of the
group to update. Defaults to the first group if |
.verbose |
Logical. If |
The ScrubletRes object with updated predicted_doublets,
z_scores, threshold, detected_doublet_rate,
detectable_doublet_fraction, and overall_doublet_rate.
# move the automatic threshold after eyeballing the histograms sc <- demo_single_cells(prepped = FALSE) res <- scrublet_sc( sc, scrublet_params = params_scrublet( pca = list(no_pcs = 10L), hvg = list(min_gene_var_pctl = 0.0), n_bins = 20L ), .verbose = FALSE ) call_doublets_manual(res, threshold = 0.3, .verbose = FALSE) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# move the automatic threshold after eyeballing the histograms sc <- demo_single_cells(prepped = FALSE) res <- scrublet_sc( sc, scrublet_params = params_scrublet( pca = list(no_pcs = 10L), hvg = list(min_gene_var_pctl = 0.0), n_bins = 20L ), .verbose = FALSE ) call_doublets_manual(res, threshold = 0.3, .verbose = FALSE) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
(Human) cell cycle genes for scoring in single cell, please see
module_scores_sc() and Tirosh et al, Science (2016). These
genes are based on the updated version in Seurat.
cell_cycle_genescell_cycle_genes
cell_cycle_genesA data.table
The (HGNC) gene symbol
The corresponding ensembl identifiers.
To which cell cycle phase the gene belongs
Runs the CellPhoneDB v5 analysis on a SingleCells object. For every
interaction and every ordered (sender, receiver) cluster pair, partner a is
measured in the sender and partner b in the receiver. The interaction mean
is the average of the two partner means, zero if either is not expressed.
A complex takes the minimum over its subunits. The gate requires both
partners to be expressed in more than threshold of their cluster's cells.
Three methods, as in CellPhoneDB:
"statistical" - Permutes the cluster labels and reports
p = #(perm > real) / n_perm, 1 where the mean is zero or the gate fails.
"degs" - No permutations. The gate also needs partner a to be
differentially expressed in the sender or partner b in the receiver, as
given by deg_table.
"simple" - Means and the expression gate only.
Reads the normalised layer of the L/R genes only, once. Interactions with any subunit missing from the object are dropped with a warning.
cellphonedb_sc( object, celltype_colname, lr_db, method = c("statistical", "degs", "simple"), deg_table = NULL, senders = NULL, receivers = NULL, gene_id_col = "gene_id", params = params_sc_cellphonedb(), .verbose = TRUE )cellphonedb_sc( object, celltype_colname, lr_db, method = c("statistical", "degs", "simple"), deg_table = NULL, senders = NULL, receivers = NULL, gene_id_col = "gene_id", params = params_sc_cellphonedb(), .verbose = TRUE )
object |
A |
celltype_colname |
String. Name of the cluster column in |
lr_db |
data.table. The ligand-receptor database, see
|
method |
String. One of |
deg_table |
Optional data.table with the columns |
senders, receivers
|
Optional character vectors. Restrict the tested
pairs to these sender and receiver clusters. |
gene_id_col |
String. The var column holding the gene symbols used in
|
params |
List. See |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A data.table with one row per interaction and cluster pair:
interaction_id, partner_a, partner_b - From lr_db.
sender, receiver - The cluster pair.
mean - The interaction mean.
pval - Permutation p-value, NA unless method = "statistical".
gate - The expression gate, or the DEG gate for method = "degs".
Efremova et al., Nat Protoc, 2020; Troulé et al., Nat Protoc, 2025.
Fits the CellSweep multinomial mixture model and writes a new SingleCells
object holding the denoised counts. Every observed count is split three ways
by EM: ambient contamination drawn from a per-emulsion profile, bulk
contamination drawn from a global profile, and true expression drawn from the
barcode's cell-type profile. Subtracting the first two gives the denoised
matrix.
cellsweep_sc( target, input, celltype_column, sample_column, empty_params, cellsweep_params = params_sc_cellsweep(), sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )cellsweep_sc( target, input, celltype_column, sample_column, empty_params, cellsweep_params = params_sc_cellsweep(), sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
target |
|
input |
|
celltype_column |
String. Obs column with the cell-type labels. |
sample_column |
String. Obs column identifying the emulsion. One independent fit per level. |
empty_params |
List. See |
cellsweep_params |
List. See |
sc_qc_param |
List. See |
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
Two things about where this sits in the workflow, because both are easy to get wrong.
It runs after annotation, not before. The model subtracts against
cell-type profiles, so it needs the labels on input. The chain is: ingest the
raw barcodes, mask and cluster and annotate as usual, then cellsweep_sc(),
then redo feature selection and reduction on the clean counts.
It needs the empty droplets. The ambient profile is estimated from their pooled counts, and they stay in the EM with their contamination fraction pinned at 1. That means the object has to have been ingested permissively:
obj <- load_mtx(
obj,
sc_mtx_io_param = get_cell_ranger_params(path),
sc_qc_param = params_sc_min_quality(
min_unique_genes = 0L, min_lib_size = 0L, min_cells = 0L
)
)
The load-time cutoffs are irreversible, so the defaults
(min_lib_size = 250L) delete exactly the barcodes CellSweep trains on. This
function checks for that and errors rather than fitting a garbage profile.
One EM fit per sample, since the ambient profile is a property of a single
emulsion. sample_column is required for that reason; pooling samples into
one ambient profile is wrong even when it runs.
Barcodes partition three ways: empty droplets, annotated barcodes that passed QC, and everything else. The third group is excluded from the fit and does not appear in the output.
The denoised counts land in a new directory. The raw layer takes the
stochastically rounded values so the negative binomial methods downstream
still see integers, and the normalised layer keeps the float magnitudes.
Entries whose denoised value rounds to zero are dropped from both layers:
the two share one index set, so keeping them would put explicit zeros in the
raw layer and library_size would stop being the sum of what is stored,
breaking every consumer that computes a fraction of the library.
The target object, populated with the denoised counts. The fit
lands in obs as cellsweep_alpha, cellsweep_z, cellsweep_beta,
cellsweep_ll and cellsweep_converged, and in var as cellsweep_ambient.
Sullivan et al., CellSweep, 2025
Changes the primary gene identifier in the BulkDge class. To do so, you need
to either provide a variable_info data.table with the alternative gene
identifier you wish to use or it exists already in the object itself. If it
exists in the object, that variable_info will be used.
change_gene_identifier(object, alternative_gene_id, variable_info = NULL)change_gene_identifier(object, alternative_gene_id, variable_info = NULL)
object |
|
alternative_gene_id |
String. The column containing the alternative gene
identifier. Must be present in the provided |
variable_info |
Optional data.table with variable information. If
|
The class with modified primary gene identifier.
# swap the row names over to a symbol column of the variable info syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table(sample_id = colnames(syn$counts)) var_info <- data.table::data.table( var_id = rownames(syn$counts), symbol = sprintf("SYM%i", seq_len(nrow(syn$counts))) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- change_gene_identifier(object, "symbol", var_info) head(rownames(S7::prop(object, "raw_counts")))# swap the row names over to a symbol column of the variable info syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table(sample_id = colnames(syn$counts)) var_info <- data.table::data.table( var_id = rownames(syn$counts), symbol = sprintf("SYM%i", seq_len(nrow(syn$counts))) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- change_gene_identifier(object, "symbol", var_info) head(rownames(S7::prop(object, "raw_counts")))
Returns TRUE when every requested artefact is fresh, otherwise a message
describing the first problem. An artefact that is absent, or that predates
provenance stamping, passes: unknown provenance is not the same as known
bad.
Named in snake case rather than the checkXxx camel case of the other
checkmate extensions in this package, because it validates object state
rather than a params_*() list and sits alongside get_sc_cache_status().
check_sc_state(x, artefacts, modality = "rna")check_sc_state(x, artefacts, modality = "rna")
x |
|
artefacts |
Character vector. Artefacts to check, either an artefact
kind ( |
modality |
String. Modality to resolve unqualified names against. One
of |
TRUE if the check was successful, otherwise a string describing
the failure.
Detects privileged communities after a diffusion based on seed nodes.
community_detection( object, community_params = params_community_detection(), seed = 42L, .verbose = FALSE, .max_iters = 100L )community_detection( object, community_params = params_community_detection(), seed = 42L, .verbose = FALSE, .max_iters = 100L )
object |
|
community_params |
List. Parameters for the community detection within
the reduced network, see
|
seed |
Random seed. |
.verbose |
Controls the verbosity of the function. |
.max_iters |
Controls how many iterations shall be tried for the sub-clustering. To note, in each iteration of the sub-clustering, the resolution parameter is increased by 0.05, to identify more granular communities within the sub communities. |
The class with added diffusion community detection results (if any could be identified with the provided parameters).
# privileged communities in the heated part of the network set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes(object, c(node_1 = 1, node_3 = 1), "max") object <- permute_seed_nodes(object, perm_iters = 100L, .verbose = FALSE) object <- community_detection( object, community_params = params_community_detection( min_seed_nodes = 0L, min_nodes = 2L ) ) head(get_results(object))# privileged communities in the heated part of the network set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes(object, c(node_1 = 1, node_3 = 1), "max") object <- permute_seed_nodes(object, perm_iters = 100L, .verbose = FALSE) object <- community_detection( object, community_params = params_community_detection( min_seed_nodes = 0L, min_nodes = 2L ) ) head(get_results(object))
Thin R wrapper around the Rust compute_cluster_expression_stats routine.
Streams gene chunks from the on-disk store and aggregates expression across
user-supplied cell clusters. Cells outside any cluster are ignored.
If condition_colname and condition_oi are supplied, only cells from
that condition contribute to the aggregation.
compute_expression_info_sc( object, celltype_colname, genes, condition_colname = NULL, condition_oi = NULL )compute_expression_info_sc( object, celltype_colname, genes, condition_colname = NULL, condition_oi = NULL )
object |
A |
celltype_colname |
Name of the cluster column in |
genes |
Character vector of gene IDs to aggregate over. |
condition_colname |
Optional. Name of a condition column in |
condition_oi |
Optional. Value of |
A long data.table with columns cluster_id, gene,
avg_expr, frac_expr.
# mean expression and expressing fraction per planted cell type sc <- demo_single_cells() res <- compute_expression_info_sc( sc, celltype_colname = "cell_grp", genes = get_gene_names(sc)[1:5] ) head(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# mean expression and expressing fraction per planted cell type sc <- demo_single_cells() res <- compute_expression_info_sc( sc, celltype_colname = "cell_grp", genes = get_gene_names(sc)[1:5] ) head(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Runs n_runs HALS-NMF restarts, pools their components, drops unstable ones
by local density, k-means clusters the survivors into k groups and refits
the partner factor against the per-cluster median. This is cNMF: the answer
is the structure the restarts agree on, and the mean silhouette of those
clusters (stability) tells you how much they agreed.
Prefer this over stabilised_nmf_bulk(), which just picks the
lowest-loss restart and tells you nothing about whether that restart is
reproducible.
consensus_nmf_bulk( object, k, n_runs = 30L, preprocessing = c("none", "sd", "sqrt_sd"), nmf_hals_params = params_nmf_hals(), nmf_consensus_params = params_nmf_consensus(), membership_params = params_module_membership(), seed = 42L, .verbose = TRUE )consensus_nmf_bulk( object, k, n_runs = 30L, preprocessing = c("none", "sd", "sqrt_sd"), nmf_hals_params = params_nmf_hals(), nmf_consensus_params = params_nmf_consensus(), membership_params = params_module_membership(), seed = 42L, .verbose = TRUE )
object |
The class, see |
k |
Integer. Number of latent factors (modules). Must be at least 2. |
n_runs |
Integer. Number of random restarts. Must be at least 2. |
preprocessing |
String. One of |
nmf_hals_params |
List. Output of |
nmf_consensus_params |
List. Output of
|
membership_params |
List. Controls how the gene loadings are turned into
module membership, see |
seed |
Integer. Base random seed. Restart |
.verbose |
Boolean or integer |
Use nmf_k_sweep_bulk() first if you do not already know k.
The density filter is the part that bites. Components whose mean cosine
distance to their neighbours exceeds density_threshold are dropped, and if
that leaves fewer than k survivors the fit errors rather than returning
something degenerate. With few restarts the filter is jumpy, so either raise
n_runs or set density_threshold = 2 to switch it off.
The class with final_results populated from the consensus fit,
with the clustering diagnostics under diagnostics and the fit parameters
under params$nmf_fit.
Kotliar et al., eLife, 2019
# consensus over ten restarts syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- consensus_nmf_bulk(obj, k = 4L, n_runs = 10L, .verbose = FALSE) get_nmf_stability(obj)$stability# consensus over ten restarts syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- consensus_nmf_bulk(obj, k = 4L, n_runs = 10L, .verbose = FALSE) get_nmf_stability(obj)$stability
Runs n_runs HALS NMF restarts, pools their components, drops unstable ones
by local density, k-means clusters the survivors into k groups and refits
the partner factor against the per-cluster median. This is cNMF: the answer
is the structure the restarts agree on, and the mean silhouette of those
clusters (stability) says how much they agreed.
consensus_nmf_sc( object, k, cell_ids = NULL, gene_ids = NULL, preprocessing = "none", use_second_layer = TRUE, nmf_hals_params = params_nmf_hals(), nmf_consensus_params = params_nmf_consensus(), n_runs = 30L, seed = 42L, .verbose = TRUE )consensus_nmf_sc( object, k, cell_ids = NULL, gene_ids = NULL, preprocessing = "none", use_second_layer = TRUE, nmf_hals_params = params_nmf_hals(), nmf_consensus_params = params_nmf_consensus(), n_runs = 30L, seed = 42L, .verbose = TRUE )
object |
|
k |
Integer. Number of latent factors. Must be at least 2. |
cell_ids |
Optional character. Cell ids (or meta cell ids) to restrict
the NMF to. If |
gene_ids |
Optional character. Gene ids to restrict the NMF to. If
|
preprocessing |
String. One of |
use_second_layer |
Boolean. If |
nmf_hals_params |
List, see |
nmf_consensus_params |
List, see |
n_runs |
Integer. Number of random restarts. Must be at least 2. |
seed |
Integer. Base random seed. Restart |
.verbose |
Boolean or integer. Verbosity. |
Use nmf_k_sweep_sc() first if you do not already know k.
Memory is the thing to watch. The restarts are dense and all live at once:
roughly n_cells * k * n_runs plus n_runs * k * n_genes floats on top of
the counts. At 200k cells, k = 20 and n_runs = 50 that is a few hundred
megabytes before anything else, which is exactly the regime where running on
MetaCells instead is the honest answer.
The density filter is the part that bites. If it leaves fewer than k
components the fit errors rather than returning something degenerate. With
few restarts the filter is jumpy, so either raise n_runs or set
density_threshold = 2 in params_nmf_consensus() to switch it off.
A ConsensusNmfResult object.
Kotliar et al., eLife, 2019
# ten restarts pooled, density filter off on data this small sc <- demo_single_cells() res <- consensus_nmf_sc( sc, k = 3L, n_runs = 10L, nmf_consensus_params = params_nmf_consensus(density_threshold = 2), .verbose = FALSE ) res unlink(sc@dir_data, recursive = TRUE, force = TRUE)# ten restarts pooled, density filter off on data this small sc <- demo_single_cells() res <- consensus_nmf_sc( sc, k = 3L, n_runs = 10L, nmf_consensus_params = params_nmf_consensus(density_threshold = 2), .verbose = FALSE ) res unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function implements a constrained personalised page-rank. You can define
sink_nodes or sink_edges. In the case of the former, the vist of a
sink_node (via a type attribute in the igraph) will automatically cause the
surfer to reset. In the case of the latter, the traversal of a sink_edge is
allowed, however, subsequently, the surfer will be reseted. This function can
be useful for deriving PPR profiles under constraints in heterogenous graphs
and has been inspired by Ruiz, et al.
constrained_page_rank( graph, personalisation_vector, sink_nodes = NULL, sink_edges = NULL )constrained_page_rank( graph, personalisation_vector, sink_nodes = NULL, sink_edges = NULL )
graph |
igraph. This one needs to be directed and weighted and have
the node attribute |
personalisation_vector |
Numerical vector. The personalisation vector for the reset of the personalised page rank. The function will automatically normalise it. |
sink_nodes |
Optional String vector. The node types that should force a reset. |
sink_edges |
Optional String vector. The edge types after which there should be a forced reset. |
The constrained personalised page rank
Ruiz, et al., Nat Commun, 2021
# personalised page rank from a receptor over a small signalling graph nodes <- data.frame( name = c("rec_a", "kin_b", "tf_c", "gene_d"), type = c("receptor", "kinase", "tf", "target_gene") ) edges <- data.frame( from = c("rec_a", "kin_b", "tf_c"), to = c("kin_b", "tf_c", "gene_d"), weight = rep(1, 3), type = c("activation", "phosphorylation", "tf_activation") ) g <- igraph::graph_from_data_frame(edges, vertices = nodes, directed = TRUE) constrained_page_rank(g, personalisation_vector = c(1, 0, 0, 0))# personalised page rank from a receptor over a small signalling graph nodes <- data.frame( name = c("rec_a", "kin_b", "tf_c", "gene_d"), type = c("receptor", "kinase", "tf", "target_gene") ) edges <- data.frame( from = c("rec_a", "kin_b", "tf_c"), to = c("kin_b", "tf_c", "gene_d"), weight = rep(1, 3), type = c("activation", "phosphorylation", "tf_activation") ) g <- igraph::graph_from_data_frame(edges, vertices = nodes, directed = TRUE) constrained_page_rank(g, personalisation_vector = c(1, 0, 0, 0))
This function implements a personalised constrained page-rank over a list of
personalisation vectors for the same network. You can define sink_nodes
or sink_edges. In the case of the former, the vist of a sink_node (via a
type attribute in the igraph) will automatically cause the surfer to reset.
In the case of the latter, the traversal of a sink_edge is allowed, however,
subsequently, the surfer will be reseted. This function can be useful for
deriving PPR profiles under constraints in heterogenous graphs and has been
inspired by Ruiz, et al.
constrained_page_rank_ls( graph, personalisation_list, sink_nodes = NULL, sink_edges = NULL )constrained_page_rank_ls( graph, personalisation_list, sink_nodes = NULL, sink_edges = NULL )
graph |
igraph. This one needs to be directed and weighted and have
the node attribute |
personalisation_list |
A list of numerical vectors to use for the personalisation. |
sink_nodes |
Optional String vector. The node types that should force a reset. |
sink_edges |
Optional String vector. The edge types after which there should be a forced reset. |
A list with the constrained personalised page rank values.
Ruiz, et al., Nat Commun, 2021
# two personalisation vectors over the same signalling graph nodes <- data.frame( name = c("rec_a", "kin_b", "tf_c", "gene_d"), type = c("receptor", "kinase", "tf", "target_gene") ) edges <- data.frame( from = c("rec_a", "kin_b", "tf_c"), to = c("kin_b", "tf_c", "gene_d"), weight = rep(1, 3), type = c("activation", "phosphorylation", "tf_activation") ) g <- igraph::graph_from_data_frame(edges, vertices = nodes, directed = TRUE) res <- constrained_page_rank_ls( g, personalisation_list = list(a = c(1, 0, 0, 0), b = c(0, 1, 0, 0)) ) res$a# two personalisation vectors over the same signalling graph nodes <- data.frame( name = c("rec_a", "kin_b", "tf_c", "gene_d"), type = c("receptor", "kinase", "tf", "target_gene") ) edges <- data.frame( from = c("rec_a", "kin_b", "tf_c"), to = c("kin_b", "tf_c", "gene_d"), weight = rep(1, 3), type = c("activation", "phosphorylation", "tf_activation") ) g <- igraph::graph_from_data_frame(edges, vertices = nodes, directed = TRUE) res <- constrained_page_rank_ls( g, personalisation_list = list(a = c(1, 0, 0, 0), b = c(0, 1, 0, 0)) ) res$a
Applies the contrastive PCA algorithm given a specified alpha and a number of contrastive principal components to extract.
contrastive_pca(object, alpha, no_pcs)contrastive_pca(object, alpha, no_pcs)
object |
The underlying class, see |
alpha |
Alpha parameter to use. |
no_pcs |
Number of contrastive PCs to generate. |
BulkCoExp with additional data in the slots
Abid, et al., Nature Communications, 2018
# five contrastive PCs at alpha 2.5 cpca_data <- synthetic_c_pca_data() target <- t(cpca_data$target) background <- t(cpca_data$background) meta <- data.table::data.table(sample_id = rownames(target)) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, .verbose = FALSE) obj <- contrastive_pca_processing(obj, background, .verbose = FALSE) obj <- contrastive_pca(obj, alpha = 2.5, no_pcs = 5L) dim(get_c_pca_factors(obj))# five contrastive PCs at alpha 2.5 cpca_data <- synthetic_c_pca_data() target <- t(cpca_data$target) background <- t(cpca_data$background) meta <- data.table::data.table(sample_id = rownames(target)) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, .verbose = FALSE) obj <- contrastive_pca_processing(obj, background, .verbose = FALSE) obj <- contrastive_pca(obj, alpha = 2.5, no_pcs = 5L) dim(get_c_pca_factors(obj))
This function will prepare the BulkCoExp for subsequent usage of the
contrastive PCA functions. This is based on the work of Abid, et al.
contrastive_pca_processing( object, background_matrix, scale = FALSE, .verbose = TRUE )contrastive_pca_processing( object, background_matrix, scale = FALSE, .verbose = TRUE )
object |
The underlying class, see |
background_matrix |
Numeric matrix. The background matrix you wish to remove. You should apply any data transformation to this matrix, too! |
scale |
Boolean. Shall the data be scaled. Defaults to FALSE. |
.verbose |
Boolean. Controls verbosity of the function. |
BulkCoExp with the needed data for contrastive PCA in the
properties of the class.
Abid, et al., Nature Communications, 2018
# covariance matrices of the target and the background cpca_data <- synthetic_c_pca_data() target <- t(cpca_data$target) background <- t(cpca_data$background) meta <- data.table::data.table(sample_id = rownames(target)) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, .verbose = FALSE) obj <- contrastive_pca_processing(obj, background, .verbose = FALSE) dim(obj@processed_data$target_covar)# covariance matrices of the target and the background cpca_data <- synthetic_c_pca_data() target <- t(cpca_data$target) background <- t(cpca_data$background) meta <- data.table::data.table(sample_id = rownames(target)) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, .verbose = FALSE) obj <- contrastive_pca_processing(obj, background, .verbose = FALSE) dim(obj@processed_data$target_covar)
This functions iterates through a set of provided epsilons and checks for each one to what extend the underlying affinity matrix will follow a power law distribution.
cor_module_check_epsilon( object, rbf_func = c("bump", "gaussian", "inverse_quadratic"), epsilons = c(0.25, seq(from = 0.5, to = 10, by = 0.5)), .verbose = TRUE )cor_module_check_epsilon( object, rbf_func = c("bump", "gaussian", "inverse_quadratic"), epsilons = c(0.25, seq(from = 0.5, to = 10, by = 0.5)), .verbose = TRUE )
object |
The class, see |
rbf_func |
The type of RBF function you want to apply. A choice of
|
epsilons |
Vector of floats. The different epsilon parameters you would like to run. |
.verbose |
Boolean. Controls verbosity of the function. |
The class with added data to the properties for subsequent usage.
# scan epsilons of a Gaussian RBF for scale-free topology mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_check_epsilon( obj, rbf_func = "gaussian", .verbose = FALSE ) head(get_epsilon_res(obj))# scan epsilons of a Gaussian RBF for scale-free topology mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_check_epsilon( obj, rbf_func = "gaussian", .verbose = FALSE ) head(get_epsilon_res(obj))
This function creates gene modules, based on the framework from Srivastava et al., 2018. Briefly, it applies an RBF function to the correlation matrix to reduce the impact of weak correlations, leverages hierarchical clustering for clustering the genes. It optimises the R2 (cor^2) within the clusters to identify the optimal cut. Gene modules with low R2 are being considered as the 'junk module'.
cor_module_coremo_clustering( object, span = 0.1, coremo_params = params_coremo(), seed = 42L, .verbose = TRUE )cor_module_coremo_clustering( object, span = 0.1, coremo_params = params_coremo(), seed = 42L, .verbose = TRUE )
object |
The class, see |
span |
Float. Defines the span parameter for the loess function to
identify the inflection point. Defaults to |
coremo_params |
List. Parameters for the generation of the CoReMo
modules, see
|
seed |
Integer. Random seed for reproducibility purposes. |
.verbose |
Boolean. Controls verbosity of the function. |
The class with added data to the properties for subsequent usage.
Srivastava, et al., Nat. Commun., 2018
# CoReMo clustering with the default RBF and cut range mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_coremo_clustering(obj, .verbose = FALSE) head(obj@outputs$final_modules)# CoReMo clustering with the default RBF and cut range mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_coremo_clustering(obj, .verbose = FALSE) head(obj@outputs$final_modules)
This function will split the identified modules by their correlation sign.
In certain cases, positive and negatively correlated genes can be part of
the same module. This function will annotate them with "_pos" and "_neg"
respectively.
cor_module_coremo_cor_sign(object, min_stability = NULL, .verbose = TRUE)cor_module_coremo_cor_sign(object, min_stability = NULL, .verbose = TRUE)
object |
The class, see |
min_stability |
Optional float. The minimum stability for the gene
you wish to filter for based on the leave-one-out resampling. If |
.verbose |
Boolean. Controls verbosity of the function. |
The class with updated correlation module names.
Srivastava, et al., Nat. Commun., 2018; Francois, Romagnolo, et al., Nat. Commun., 2024.
# split the modules into positively and negatively correlated genes mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_coremo_clustering(obj, .verbose = FALSE) obj <- cor_module_coremo_cor_sign(obj, .verbose = FALSE) table(obj@outputs$final_modules$sign)# split the modules into positively and negatively correlated genes mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_coremo_clustering(obj, .verbose = FALSE) obj <- cor_module_coremo_cor_sign(obj, .verbose = FALSE) table(obj@outputs$final_modules$sign)
This function will calculate the eigengene values for the modules on a per sample basis and add correlations of the gene expression of a given gene within the module to its eigengene.
cor_module_coremo_eigengene(object, min_stability = NULL, .verbose = TRUE)cor_module_coremo_eigengene(object, min_stability = NULL, .verbose = TRUE)
object |
The class, see |
min_stability |
Optional float. The minimum stability for the gene
you wish to filter for based on the leave-one-out resampling. If |
.verbose |
Boolean. Controls verbosity of the function. |
The class with added correlations to the modules and the values for a given eigengene per sample as a data.table.
Srivastava, et al., Nat. Commun., 2018; Francois, Romagnolo, et al., Nat. Commun., 2024.
# eigengenes per module and the gene to eigengene correlations mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_coremo_clustering(obj, .verbose = FALSE) obj <- cor_module_coremo_eigengene(obj, .verbose = FALSE) head(get_modules(get_results(obj)))# eigengenes per module and the gene to eigengene correlations mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_coremo_clustering(obj, .verbose = FALSE) obj <- cor_module_coremo_eigengene(obj, .verbose = FALSE) head(get_modules(get_results(obj)))
The function assesses the stability of the CoReMo modules, leveraging a
leave-one-out sample method. In each iteration, one of the samples is left
out and the clustering is redone. Subsequently, the Jaccard similarity (as
a surrogate for membership stability) is calculated for each feature across
the different resamplings and added to the final_modules data.table.
cor_module_coremo_stability(object, chunk_size = 15L, .verbose = TRUE)cor_module_coremo_stability(object, chunk_size = 15L, .verbose = TRUE)
object |
The class, see |
chunk_size |
Integer. Chunk size in which to process the data. Defaults
to |
.verbose |
Boolean. Controls verbosity of the function. |
The class with added data to the properties for subsequent usage.
Srivastava, et al., Nat. Commun., 2018; Francois, Romagnolo, et al., Nat. Commun., 2024.
# leave-one-out stability of the CoReMo modules mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_coremo_clustering(obj, .verbose = FALSE) obj <- cor_module_coremo_stability(obj, .verbose = FALSE) summary(obj@outputs$final_modules$stability)# leave-one-out stability of the CoReMo modules mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_coremo_clustering(obj, .verbose = FALSE) obj <- cor_module_coremo_stability(obj, .verbose = FALSE) summary(obj@outputs$final_modules$stability)
This function will identify gene modules based on affinity graphs from the
single correlation or differential correlation methods. Briefly, in the case
of single correlation, the graph is generated based on the absolute
correlation coefficients that are subjected to a Gaussian affinity kernel.
This reduces spurious correlations and leaves a sparsely connected graph.
In the case of differential correlations, the graph is generated based on
significant differential correlations if one of the two correlations reached
the defined minimum thresholds.
Subsequently, Leiden community detection is applied on the respective graph
through a range of resolutions that the user can define. The function then
returns meta information about the resolutions (which can also be plotted) to
identify the best suitable resolution parameter to identify co-expression
modules.
cor_module_graph_check_res( object, resolution_params = params_graph_resolution(), graph_params = params_cor_graph(), random_seed = 123L, min_genes = 10L, parallel = TRUE, max_workers = NULL, .verbose = TRUE )cor_module_graph_check_res( object, resolution_params = params_graph_resolution(), graph_params = params_cor_graph(), random_seed = 123L, min_genes = 10L, parallel = TRUE, max_workers = NULL, .verbose = TRUE )
object |
The class, see |
resolution_params |
List. Parameters for the resolution search, see
|
graph_params |
List. Parameters for the generation of the (differential)
correlation graph, see
|
random_seed |
Integer. Random seed. |
min_genes |
Integer. Minimum number of genes that should be in a community. |
parallel |
Boolean. Parallelise the Leiden clustering. |
max_workers |
Optional Integer. Number of cores to use if parallel is
set to |
.verbose |
Controls the verbosity of the function. |
The class with added data to the properties.
# scan Leiden resolutions on the correlation graph mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_graph_check_res( obj, resolution_params = params_graph_resolution(number_res = 5L), parallel = FALSE, .verbose = FALSE ) head(get_resolution_res(obj))# scan Leiden resolutions on the correlation graph mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_graph_check_res( obj, resolution_params = params_graph_resolution(number_res = 5L), parallel = FALSE, .verbose = FALSE ) head(get_resolution_res(obj))
This function leverages graph-based clustering to identify gene co-expression modules. The class has the option to sub-cluster large communities within their respective sub graphs, akin to the approach taken by Barrio-Hernandez, et al.
cor_module_graph_final_modules( object, resolution = NULL, min_size = 10L, max_size = 500L, subclustering = TRUE, random_seed = 123L, .graph_params = params_cor_graph(), .max_iters = 100L, .verbose = TRUE )cor_module_graph_final_modules( object, resolution = NULL, min_size = 10L, max_size = 500L, subclustering = TRUE, random_seed = 123L, .graph_params = params_cor_graph(), .max_iters = 100L, .verbose = TRUE )
object |
The class, see |
resolution |
The Leiden resolution parameter you wish to use. If NULL,
it will use the optimal one identified by
|
min_size |
Integer. Minimum size of the communities. |
max_size |
Integer. Maximum size of the communities. |
subclustering |
Boolean. Shall after a first clustering communities that
are too large be further sub clustered. Defaults to |
random_seed |
Integer. Random seed. |
.graph_params |
List. Parameters for the generation of the
(differential) correlation graph, see
This parameter is only relevant if you did not run
|
.max_iters |
Integer. If sub clustering is set to |
.verbose |
Boolean. Controls the verbosity of the function. |
The class with added data to the properties.
Barrio-Hernandez, et al., Nat Genet, 2023.
# finalise the communities at the resolution picked by the scan mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_graph_check_res( obj, resolution_params = params_graph_resolution(number_res = 5L), parallel = FALSE, .verbose = FALSE ) obj <- cor_module_graph_final_modules(obj, .verbose = FALSE) head(get_modules(get_results(obj)))# finalise the communities at the resolution picked by the scan mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_graph_check_res( obj, resolution_params = params_graph_resolution(number_res = 5L), parallel = FALSE, .verbose = FALSE ) obj <- cor_module_graph_final_modules(obj, .verbose = FALSE) head(get_modules(get_results(obj)))
This function will calculate the correlation coefficients between the genes, using the highly variable genes (if available, otherwise the function will use the raw data). The data will be stored in a memory-efficient format in the properties of the class.
cor_module_processing( object, cor_method = c("pearson", "spearman"), .verbose = TRUE )cor_module_processing( object, cor_method = c("pearson", "spearman"), .verbose = TRUE )
object |
The class, see |
cor_method |
String. Option of |
.verbose |
Boolean. Controls verbosity of the function. |
The class with added data to the properties for subsequent usage.
# spearman correlations over the 300 most variable genes mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) dim(obj@processed_data$correlation_res$get_sym_matrix( .verbose = FALSE ))# spearman correlations over the 300 most variable genes mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) dim(obj@processed_data$correlation_res$get_sym_matrix( .verbose = FALSE ))
This function will update the correlation matrix to a topological overlap
matrix. It defaults to "v2" and the signed version, please see
calculate_tom() for details.
cor_module_tom(object, signed = TRUE, version = c("v2", "v1"), .verbose = TRUE)cor_module_tom(object, signed = TRUE, version = c("v2", "v1"), .verbose = TRUE)
object |
The class, see |
signed |
Boolean. Do you want to use the signed or unsigned version.
Defaults to |
version |
String. One of |
.verbose |
Boolean. Controls verbosity of the function. |
The class with added data to the properties for subsequent usage.
# replace the stored correlations with a signed topological overlap mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_tom(obj, signed = TRUE, version = "v2", .verbose = FALSE) obj@params$correlation_params$TOM# replace the stored correlations with a signed topological overlap mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_tom(obj, signed = TRUE, version = "v2", .verbose = FALSE) obj@params$correlation_params$TOM
SingleCells object for examples and testsWires generate_single_cell_test_data() into a SingleCells
object on disk in one call, so examples do not have to repeat the whole
ingestion dance. The default is deliberately tiny (500 cells x 50 genes) and
the quality thresholds are loose enough that every cell survives. This is
synthetic data for demonstration and testing, not something to analyse.
demo_single_cells( dir = tempfile("bixverse_demo"), prepped = TRUE, syn_data_params = params_sc_synthetic_data(n_cells = 500L, n_genes = 50L), hvg_no = 30L, no_pcs = 10L, k = 15L, seed = 42L, .verbose = FALSE )demo_single_cells( dir = tempfile("bixverse_demo"), prepped = TRUE, syn_data_params = params_sc_synthetic_data(n_cells = 500L, n_genes = 50L), hvg_no = 30L, no_pcs = 10L, k = 15L, seed = 42L, .verbose = FALSE )
dir |
String. Directory to hold the object. Created if it does not
exist. Defaults to a fresh path under the session |
prepped |
Boolean. Run the standard HVG -> PCA -> kNN chain before
returning? Defaults to |
syn_data_params |
List. Parameters for the synthetic data, see
|
hvg_no |
Integer. Number of highly variable genes, |
no_pcs |
Integer. Number of principal components, |
k |
Integer. Number of nearest neighbours, |
seed |
Integer. Seed for the data generation. |
.verbose |
Boolean. Controls verbosity of the function. |
A SingleCells object backed by dir.
# a prepped object, ready for clustering sc <- demo_single_cells() sc <- find_clusters_sc(sc, res = 1.0) sc unlink(sc@dir_data, recursive = TRUE, force = TRUE)# a prepped object, ready for clustering sc <- demo_single_cells() sc <- find_clusters_sc(sc, res = 1.0) sc unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Wrapper around manifoldsR::densmap() for the SingleCells and MetaCells
classes. densMAP is UMAP plus a density-preserving term: a tight population
stays tight in the embedding and a diffuse one stays diffuse. With plain
UMAP the relative size of a cluster on the plot tells you nothing, with
densMAP it does. Setting lambda = 0 in manifoldsR::params_densmap()
gives you back plain UMAP.
Neighbour handling is the same as in umap_sc(): with
use_knn = TRUE (the default) the cached kNN graph is reused, otherwise
neighbours are computed from the chosen embedding.
densmap_sc( object, use_knn = TRUE, embd_to_use = "pca", slot_name = "densmap", no_embd_to_use = NULL, modality = c("rna", "adt", "wnn"), n_dim = 2L, k = 15L, min_dist = 0.5, spread = 1, knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"), nn_params = manifoldsR::params_nn(), umap_params = manifoldsR::params_umap(), dens_params = manifoldsR::params_densmap(), seed = 42L, .verbose = TRUE )densmap_sc( object, use_knn = TRUE, embd_to_use = "pca", slot_name = "densmap", no_embd_to_use = NULL, modality = c("rna", "adt", "wnn"), n_dim = 2L, k = 15L, min_dist = 0.5, spread = 1, knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"), nn_params = manifoldsR::params_nn(), umap_params = manifoldsR::params_umap(), dens_params = manifoldsR::params_densmap(), seed = 42L, .verbose = TRUE )
object |
|
use_knn |
Boolean. Use the kNN graph found in the object. Defaults to
|
embd_to_use |
String. The embedding to use for densMAP. Must be available in the object. |
slot_name |
String. The name of this embedding within the object.
Defaults to |
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
modality |
String. On which modality to run densMAP. One of
|
n_dim |
Integer. Number of densMAP dimensions. Defaults to |
k |
Integer. Number of nearest neighbours. Defaults to |
min_dist |
Numeric. Minimum distance between embedded points. Defaults
to |
spread |
Numeric. Effective scale of embedded points. Defaults to |
knn_method |
String. Approximate nearest neighbour algorithm. One of
|
nn_params |
Named list. See |
umap_params |
Named list. See |
dens_params |
Named list. The density knobs, see
|
seed |
Integer. For reproducibility. |
.verbose |
Boolean. Controls verbosity. |
The object with a "densmap" embedding added.
Narayan, Berger & Cho, Nat. Biotechnol., 2021
# densMAP off the cached kNN graph sc <- demo_single_cells() sc <- densmap_sc(sc, .verbose = FALSE) dim(get_embedding(sc, "densmap")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# densMAP off the cached kNN graph sc <- demo_single_cells() sc <- densmap_sc(sc, .verbose = FALSE) dim(get_embedding(sc, "densmap")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Wrapper around manifoldsR::densne() for the SingleCells and MetaCells
classes. den-SNE is t-SNE plus a density-preserving term: a tight population
stays tight in the embedding and a diffuse one stays diffuse. Plain t-SNE
inflates dense clusters and shrinks sparse ones, so relative cluster sizes
on the plot mean nothing; with den-SNE they do. Setting lambda = 0 in
manifoldsR::params_densne() gives you back plain t-SNE.
Neighbour handling, approx_type and the k versus perplexity caveats
are the same as in tsne_sc().
densne_sc( object, use_knn = FALSE, embd_to_use = "pca", slot_name = "densne", no_embd_to_use = NULL, modality = c("rna", "adt", "wnn"), n_dim = 2L, perplexity = 10, approx_type = c("bh", "fft", "fft_3k"), knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"), nn_params = manifoldsR::params_nn(), tsne_params = manifoldsR::params_tsne(), dens_params = manifoldsR::params_densne(), seed = 42L, .verbose = TRUE )densne_sc( object, use_knn = FALSE, embd_to_use = "pca", slot_name = "densne", no_embd_to_use = NULL, modality = c("rna", "adt", "wnn"), n_dim = 2L, perplexity = 10, approx_type = c("bh", "fft", "fft_3k"), knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"), nn_params = manifoldsR::params_nn(), tsne_params = manifoldsR::params_tsne(), dens_params = manifoldsR::params_densne(), seed = 42L, .verbose = TRUE )
object |
|
use_knn |
Boolean. Use the kNN graph found in the object. Defaults to
|
embd_to_use |
String. The embedding to use for den-SNE. Must be available in the object. |
slot_name |
String. The name of this embedding within the object.
Defaults to |
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
modality |
String. On which modality to run den-SNE. One of
|
n_dim |
Integer. Number of den-SNE dimensions. Currently only |
perplexity |
Numeric. Perplexity parameter. Typical values between 5
and 50. Defaults to |
approx_type |
String. Approximation method. One of |
knn_method |
String. Approximate nearest neighbour algorithm. One of
|
nn_params |
Named list. See |
tsne_params |
Named list. See |
dens_params |
Named list. The density knobs, see
|
seed |
Integer. For reproducibility. |
.verbose |
Boolean. Controls verbosity. |
The object with a "densne" embedding added.
Narayan, Berger & Cho, Nat. Biotechnol., 2021
# Barnes-Hut den-SNE on the PCA factors sc <- demo_single_cells() sc <- densne_sc(sc, .verbose = FALSE) dim(get_embedding(sc, "densne")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# Barnes-Hut den-SNE on the PCA factors sc <- demo_single_cells() sc <- densne_sc(sc, .verbose = FALSE) dim(get_embedding(sc, "densne")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Detect likely isotype-control features by name pattern
detect_adt_isotypes(feature_names, pattern = "isotype")detect_adt_isotypes(feature_names, pattern = "isotype")
feature_names |
Character. ADT feature names (matrix colnames). |
pattern |
String. Case-insensitive regex. Defaults to |
Character vector of matching names, for inspection before passing
to add_adt_counts_sc() as isotype_names.
# find the isotype controls among the ADT features features <- c("CD3", "CD19", "IgG1_isotype", "IgG2a_isotype") detect_adt_isotypes(features)# find the isotype controls among the ADT features features <- c("CD3", "CD19", "IgG1_isotype", "IgG2a_isotype") detect_adt_isotypes(features)
This function allows you to quickly iterate over different initial seeds, number of neighbours for the KNN graph and dictionary sizes to identify optimal hyperparameters for your DGRDL run.
dgrdl_grid_search( object, neighbours_vec, dict_size_vec, seed_vec, dgrdl_params = params_dgrdl(), .verbose = TRUE )dgrdl_grid_search( object, neighbours_vec, dict_size_vec, seed_vec, dgrdl_params = params_dgrdl(), .verbose = TRUE )
object |
The class, see |
neighbours_vec |
Integer vector. The different k nearest neighbours to test. |
dict_size_vec |
Integer vector. The different dictionary sizes to test for. |
seed_vec |
Integer vector. The different initial seeds to test for the dictionary generation. |
dgrdl_params |
List. Output of
|
.verbose |
Boolean. Controls verbosity of the function. |
BulkCoExp with the grid search results added to the class.
Pan et al., Cell Syst, 2022
# small grid over neighbours and dictionary sizes syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) obj <- BulkCoExp(syn$data, syn$meta_data) obj <- preprocess_bulk_coexp(obj, hvg = NULL, .verbose = FALSE) obj <- dgrdl_grid_search( obj, neighbours_vec = c(3L, 5L), dict_size_vec = c(4L, 6L), seed_vec = 123L, .verbose = FALSE ) get_grid_search_res(obj)# small grid over neighbours and dictionary sizes syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) obj <- BulkCoExp(syn$data, syn$meta_data) obj <- preprocess_bulk_coexp(obj, hvg = NULL, .verbose = FALSE) obj <- dgrdl_grid_search( obj, neighbours_vec = c(3L, 5L), dict_size_vec = c(4L, 6L), seed_vec = 123L, .verbose = FALSE ) get_grid_search_res(obj)
Runs the DGRDL algorithm from Pan et al., with the specified hyperparamters.
To determine the hyperparameters, you can use
dgrdl_grid_search().
dgrdl_result( object, dgrdl_params = params_dgrdl(), membership_params = params_module_membership(), seed = 42L, .verbose = TRUE )dgrdl_result( object, dgrdl_params = params_dgrdl(), membership_params = params_module_membership(), seed = 42L, .verbose = TRUE )
object |
The class, see |
dgrdl_params |
List. Output of
|
membership_params |
List. Controls how the atom loadings are turned into
module membership, see |
seed |
Integer. Seed for the initialisation of the dictionary. |
.verbose |
Boolean. Controls verbosity of the function. |
Pan et al., Cell Syst, 2022
# fit DGRDL with a six-atom dictionary syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) obj <- BulkCoExp(syn$data, syn$meta_data) obj <- preprocess_bulk_coexp(obj, hvg = NULL, .verbose = FALSE) obj <- dgrdl_result( obj, dgrdl_params = params_dgrdl(dict_size = 6L, k_neighbours = 3L), .verbose = FALSE ) head(get_modules(get_results(obj)))# fit DGRDL with a six-atom dictionary syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) obj <- BulkCoExp(syn$data, syn$meta_data) obj <- preprocess_bulk_coexp(obj, hvg = NULL, .verbose = FALSE) obj <- dgrdl_result( obj, dgrdl_params = params_dgrdl(dict_size = 6L, k_neighbours = 3L), .verbose = FALSE ) head(get_modules(get_results(obj)))
DIALOGUE looks for programmes of cell-type-specific genes whose activity covaries across the samples that several cell types share. Where co-expression modules ask what varies together within a cell type, this asks what varies together between them, sample by sample.
dialogue_sc( object, cell_type_col, sample_col, features, quality_col = NULL, gene_ids = NULL, pmd_params = params_dialogue_pmd(), hlm_params = params_dialogue_hlm(), refine_params = params_dialogue_refine(), .verbose = TRUE )dialogue_sc( object, cell_type_col, sample_col, features, quality_col = NULL, gene_ids = NULL, pmd_params = params_dialogue_pmd(), hlm_params = params_dialogue_hlm(), refine_params = params_dialogue_refine(), .verbose = TRUE )
object |
|
cell_type_col |
String. Column in the obs table holding the cell type labels. |
sample_col |
String. Column in the obs table holding the sample labels.
The random intercept in stage two is over these. For |
features |
Named list of numeric matrices, one per cell type. Names must
match the levels in |
quality_col |
Optional string. Column in the obs table to use as the
cell quality covariate. If |
gene_ids |
Optional character. Genes to consider when building
signatures. If |
pmd_params |
List, see |
hlm_params |
List, see |
refine_params |
List, see |
.verbose |
Boolean or integer. Verbosity. |
The algorithm works in three stages. Each cell type's features are collapsed to one row per sample and put through a sparse multi-CCA, giving every programme a weight vector per cell type plus a provisional gene signature. Then, for every ordered pair of cell types and every candidate gene, a mixed model asks whether a cell's own programme score tracks the partner's expression of that gene in the same sample. Finally the partners are meta-analysed and the scores refit onto the surviving genes by non-negative least squares.
features is mandatory and there is no default. DIALOGUE does not compute
it: it is whatever low-dimensional description of each cell type you trust,
and the method is only as good as that choice. Do not hand it a slice of a
global PCA. Those components mostly carry between-cell-type identity, which
is near-constant inside one cell type and gets dropped by the ANOVA filter,
leaving the decomposition to work off whatever is left. Run a PCA per cell
type instead, which for SingleCells means SingleCellsSubset() followed by
calculate_pca_sc() on each subset.
The method is unforgiving about study design, and the failure modes are
errors rather than bad answers. It needs at least two cell types, at least
five samples present in every cell type, and enough cells per sample per
cell type to clear abn_c in params_dialogue_pmd(). A handful of samples
with thousands of cells each is the regime it was built for; many samples
with a dozen cells each is not.
A DialogueResult object.
Jerby-Arnon & Regev, Nature Biotechnology, 2022
# a planted multicellular programme recovered across three cell types data <- generate_dialogue_test_data() dir <- tempfile("bixverse_dlg") dir.create(dir) object <- load_r_data( SingleCells(dir_data = dir), counts = data$counts, obs = data$obs, var = data$var, sc_qc_param = params_sc_min_quality( min_unique_genes = 10L, min_lib_size = 50L, min_cells = 10L ), .verbose = FALSE ) res <- dialogue_sc( object, cell_type_col = "cell_grp", sample_col = "sample_id", features = data$features, gene_ids = data$var$gene_id, pmd_params = params_dialogue_pmd(k = 2L, n_permutations = 20L), .verbose = FALSE ) res unlink(dir, recursive = TRUE, force = TRUE)# a planted multicellular programme recovered across three cell types data <- generate_dialogue_test_data() dir <- tempfile("bixverse_dlg") dir.create(dir) object <- load_r_data( SingleCells(dir_data = dir), counts = data$counts, obs = data$obs, var = data$var, sc_qc_param = params_sc_min_quality( min_unique_genes = 10L, min_lib_size = 50L, min_cells = 10L ), .verbose = FALSE ) res <- dialogue_sc( object, cell_type_col = "cell_grp", sample_col = "sample_id", features = data$features, gene_ids = data$var$gene_id, pmd_params = params_dialogue_pmd(k = 2L, n_permutations = 20L), .verbose = FALSE ) res unlink(dir, recursive = TRUE, force = TRUE)
This function will calculate the differential correlation between the stored data set in the class and another background data set. To do so, it uses a Fisher transformation of the correlation coefficients and calculates a Z score based on the delta. The function will automatically subset into shared features between the two data sets.
diffcor_module_processing( object, background_mat, cor_method = c("pearson", "spearman"), .verbose = TRUE )diffcor_module_processing( object, background_mat, cor_method = c("pearson", "spearman"), .verbose = TRUE )
object |
The class, see |
background_mat |
Numerical matrix. The background data set. |
cor_method |
String. Option of |
.verbose |
Boolean. Controls verbosity of the function. |
The class with added data to the properties for subsequent usage.
# differential correlation of two sample groups of the same matrix sig <- synthetic_signal_matrix() mat <- t(sig$mat) target <- mat[sig$group %in% c("group1", "group2"), ] background <- mat[sig$group == "group3", ] meta <- data.table::data.table(sample_id = rownames(target)) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- diffcor_module_processing( obj, background, cor_method = "pearson", .verbose = FALSE ) obj@params$correlation_params$no_intersecting_features# differential correlation of two sample groups of the same matrix sig <- synthetic_signal_matrix() mat <- t(sig$mat) target <- mat[sig$group %in% c("group1", "group2"), ] background <- mat[sig$group == "group3", ] meta <- data.table::data.table(sample_id = rownames(target)) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- diffcor_module_processing( obj, background, cor_method = "pearson", .verbose = FALSE ) obj@params$correlation_params$no_intersecting_features
This function takes a diffusion vector and leverages personalised page-rank diffusion to identify influential nodes. These can be used subsequently for community detection or check AUROC values given a set of genes.
diffuse_seed_nodes( object, diffusion_vector, summarisation = c("max", "mean", "harmonic_sum") )diffuse_seed_nodes( object, diffusion_vector, summarisation = c("max", "mean", "harmonic_sum") )
object |
|
diffusion_vector |
Named nuermic. A named vector with values to use for the reset parameter in the personalised page-rank diffusion. Names should represent node names of the graph. |
summarisation |
String. If there are duplicated names in the
|
The class with added diffusion score based on a single set of seed genes. Additionally, the seed genes are stored in the class.
# personalised page-rank diffusion from three seed nodes set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes( object, c(node_1 = 1, node_3 = 1, node_10 = 1), summarisation = "max" ) head(get_diffusion_vector(object))# personalised page-rank diffusion from three seed nodes set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes( object, c(node_1 = 1, node_3 = 1, node_10 = 1), summarisation = "max" ) head(get_diffusion_vector(object))
This function implements the boosted doublet detection. It generates through several iterations simulated doublets, generate kNN graphs, runs Louvain clustering and assesses how often an observed cells clsuters together with the simulated doublets.
doublet_detection_boost_sc( object, boost_params = params_boost(), cells_to_use = NULL, group_by = NULL, seed = 42L, streaming = NULL, .verbose = TRUE )doublet_detection_boost_sc( object, boost_params = params_boost(), cells_to_use = NULL, group_by = NULL, seed = 42L, streaming = NULL, .verbose = TRUE )
object |
|
boost_params |
A list with the final scrublet parameters, see
|
cells_to_use |
Optional string. Names of the cells to use for the run of the boosted doublet detection. Useful when you wish to run doublet detection on individual batches within your data. The object returned will be specifically using these cells. |
group_by |
Optional grouping variable. Useful if you want to run the method on a per-sample basis. |
seed |
Integer. Random seed. |
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A boost_res class that has with the following items:
predicted_doublets - Boolean vector indicating which observed cells predicted as doublets (TRUE = doublet, FALSE = singlet).
doublet_scores_obs - Numerical vector with the likelihood of being a doublet for the observed cells.
voting_avg - Numerical vector with the average voting score.
# boosted doublet detection over five iterations sc <- demo_single_cells(prepped = FALSE) doublet_detection_boost_sc( sc, boost_params = params_boost( hvg = list(min_gene_var_pctl = 0.0), pca = list(no_pcs = 10L), n_iters = 5L ), .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# boosted doublet detection over five iterations sc <- demo_single_cells(prepped = FALSE) doublet_detection_boost_sc( sc, boost_params = params_boost( hvg = list(min_gene_var_pctl = 0.0), pca = list(no_pcs = 10L), n_iters = 5L ), .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function downloads the CD34 data set from the SEACells paper into the temperorary directory.
download_cd34_data(quiet = FALSE)download_cd34_data(quiet = FALSE)
quiet |
Boolean. If the download shall be quiet. |
String. The path to CD34 SEACells data set.
Persad, et al., Nat. Biotechnol., 2023
## Not run: # pulls the archive into the session tempdir() path <- download_cd34_data() get_h5ad_dimensions(path)$dims ## End(Not run)## Not run: # pulls the archive into the session tempdir() path <- download_cd34_data() get_h5ad_dimensions(path)$dims ## End(Not run)
Download CisTarget reference files for human (hg38)
download_cistarget_hg38( cache_dir = tools::R_user_dir("bixverse", which = "cache"), overwrite = FALSE )download_cistarget_hg38( cache_dir = tools::R_user_dir("bixverse", which = "cache"), overwrite = FALSE )
cache_dir |
String. Directory to cache the files. Defaults to a package-specific user cache directory. |
overwrite |
Logical. Re-download even if files already exist. |
Named list with paths: rankings and motif_annotations.
## Not run: # fetch the hg38 rankings and motif annotations (large, network) paths <- download_cistarget_hg38() paths$rankings ## End(Not run)## Not run: # fetch the hg38 rankings and motif annotations (large, network) paths <- download_cistarget_hg38() paths$rankings ## End(Not run)
This function downloads a PBMC data set with demuxlet information to test doublet detection methods.
download_demuxlet_pbmc(quiet = FALSE)download_demuxlet_pbmc(quiet = FALSE)
quiet |
Boolean. If the download shall be quiet. |
String. The path to the extracted doublet detection data.
## Not run: # pulls the archive into the session tempdir() path <- download_demuxlet_pbmc() list.files(path) ## End(Not run)## Not run: # pulls the archive into the session tempdir() path <- download_demuxlet_pbmc() list.files(path) ## End(Not run)
This function downloads the ulcerative colitis subset used by the DIALOGUE
paper into the temporary directory. 5,374 cells across five cell subtypes
and 30 donors, with the inflammation status per biopsy. Enough samples per
cell type for dialogue_sc(), which most single sample example
data sets are not.
download_dialogue_uc(quiet = FALSE)download_dialogue_uc(quiet = FALSE)
quiet |
Boolean. If the download shall be quiet. |
The published matrix holds log2(TPM / 10 + 1), which is what Smillie, et
al. released; the raw counts were never published. The file served here has
been linearised back to the TPM/10 scale and rounded, so it loads through
load_h5ad() like any count matrix and gets bixverse's own log
CPM on the way in. See data-raw/dialogue_uc.R for the preparation.
String. The path to the ulcerative colitis h5ad file.
Smillie, et al., Cell, 2019; Jerby-Arnon and Regev, Nat. Biotechnol., 2022
## Not run: # pulls the archive into the session tempdir() path <- download_dialogue_uc() get_h5ad_dimensions(path)$dims ## End(Not run)## Not run: # pulls the archive into the session tempdir() path <- download_dialogue_uc() get_h5ad_dimensions(path)$dims ## End(Not run)
Downloads the Kang, et al. demultiplexed PBMC experiment as a
SingleCellExperiment. 8 lupus donors, PBMCs, control against IFN-beta
stimulated in vitro, roughly 29k cells before quality control.
The design is paired within donor, which is what makes it the example for
nebula_sc(): the donor is a genuine random effect and every donor
contributes to both arms. Load it with load_sce().
download_kang_pbmc(quiet = FALSE)download_kang_pbmc(quiet = FALSE)
quiet |
Boolean. If the download shall be quiet. |
Sourced from the muscData Bioconductor package, itself from GEO accession
GSE96583. See data-raw/zenodo_sce_datasets.R for the preparation. The
multiplets column marks doublets and ambiguous droplets, which are worth
dropping before any modelling.
Served from the bixverse-data GitHub release. There is no Zenodo fallback
for this one yet.
String. The path to the qs2 file holding the
SingleCellExperiment.
Kang, et al., Nat. Biotechnol., 2018
## Not run: # pulls the archive into the session tempdir() path <- download_kang_pbmc() sce <- qs2::qs_read(path) ## End(Not run)## Not run: # pulls the archive into the session tempdir() path <- download_kang_pbmc() sce <- qs2::qs_read(path) ## End(Not run)
This function downloads the bone marrow CD34 data set from the Palantir paper into the temporary directory.
download_marrow_cd34(quiet = FALSE)download_marrow_cd34(quiet = FALSE)
quiet |
Boolean. If the download shall be quiet. |
String. The path to the marrow CD34 data set.
Setty, et al., Nat. Biotechnol., 2019
## Not run: # pulls the archive into the session tempdir() path <- download_marrow_cd34() get_h5ad_dimensions(path)$dims ## End(Not run)## Not run: # pulls the archive into the session tempdir() path <- download_marrow_cd34() get_h5ad_dimensions(path)$dims ## End(Not run)
Downloads the unfiltered raw_feature_bc_matrix.h5 of the 10x Genomics 5'
PBMC 1k run, straight from the 10x CDN rather than the bixverse-data mirror.
All 737,280 barcodes are in there, empty droplets included, which is what
cellsweep_sc() needs. The file also carries 19 Antibody Capture
features next to the 36,601 genes, so load it with
load_tenx_h5(feature_type = "Gene Expression").
download_pbmc_1k_5p(quiet = FALSE)download_pbmc_1k_5p(quiet = FALSE)
quiet |
Boolean. If the download shall be quiet. |
String. The path to the downloaded h5 file.
## Not run: # pulls roughly 17MB into the session tempdir() h5_path <- download_pbmc_1k_5p() read_tenx_h5_metadata(h5_path)$dims ## End(Not run)## Not run: # pulls roughly 17MB into the session tempdir() h5_path <- download_pbmc_1k_5p() read_tenx_h5_metadata(h5_path)$dims ## End(Not run)
This function downloads two different h5ad files for testing batch correction methods into the temporary directory.
download_pbmc_batches(quiet = FALSE)download_pbmc_batches(quiet = FALSE)
quiet |
Boolean. If the download shall be quiet. |
String. The path to the directory with the PBMC h5ad files.
## Not run: # pulls the archive into the session tempdir() path <- download_pbmc_batches() list.files(path) ## End(Not run)## Not run: # pulls the archive into the session tempdir() path <- download_pbmc_batches() list.files(path) ## End(Not run)
This function downloads the PBMC TotalSeq data set.
download_pbmc_totalseq_data(quiet = FALSE)download_pbmc_totalseq_data(quiet = FALSE)
quiet |
Boolean. If the download shall be quiet. |
String. The path to the TotalSeq data.
## Not run: # pulls the archive into the session tempdir() path <- download_pbmc_totalseq_data() read_tenx_h5_metadata(path)$feature_types ## End(Not run)## Not run: # pulls the archive into the session tempdir() path <- download_pbmc_totalseq_data() read_tenx_h5_metadata(path)$feature_types ## End(Not run)
This function downloads the PBMC3K dataset from 10x Genomics (uploaded on Zenodo) and extracts it and returns the (temporary) paths.
download_pbmc3k(quiet = FALSE)download_pbmc3k(quiet = FALSE)
quiet |
Boolean. If the download shall be quiet. |
String. The path to the extracted PBMC3K data.
## Not run: # pulls the archive into the session tempdir() path <- download_pbmc3k() list.files(path) ## End(Not run)## Not run: # pulls the archive into the session tempdir() path <- download_pbmc3k() list.files(path) ## End(Not run)
This function downloads the PBMC8k dataset from 10x Genomics (uploaded on Zenodo) and extracts it and returns the (temporary) paths.
download_pbmc8k(quiet = FALSE)download_pbmc8k(quiet = FALSE)
quiet |
Boolean. If the download shall be quiet. |
String. The path to the extracted PBMC8K data.
## Not run: # pulls the archive into the session tempdir() path <- download_pbmc8k() list.files(path) ## End(Not run)## Not run: # pulls the archive into the session tempdir() path <- download_pbmc8k() list.files(path) ## End(Not run)
Downloads the mouse ageing thymus droplet experiment as a
SingleCellExperiment. Roughly 69k thymic epithelial cells across three
ages, with real change in cell type proportions between them.
That compositional change is what makes it the example for
get_miloR_abundances_sc() and meld_sc(). A
stimulation experiment moves cells in embedding space without moving the
proportions, so almost every neighbourhood comes out significant and the
result says nothing. Load it with load_sce().
download_thymus_ageing(quiet = FALSE)download_thymus_ageing(quiet = FALSE)
quiet |
Boolean. If the download shall be quiet. |
Sourced from the MouseThymusAgeing Bioconductor package. See
data-raw/zenodo_sce_datasets.R for the preparation. The object carries no
gene names, so load_sce() falls back to the Ensembl identifiers
in the rowData, and 336 genes with no annotation at all get a generated
identifier.
Served from the bixverse-data GitHub release. There is no Zenodo fallback
for this one yet. At 430 MB it is the largest of the example datasets, so
expect the first call to take a while.
String. The path to the qs2 file holding the
SingleCellExperiment.
Baran-Gale, et al., Development, 2020
## Not run: # pulls the archive into the session tempdir() path <- download_thymus_ageing() sce <- qs2::qs_read(path) ## End(Not run)## Not run: # pulls the archive into the session tempdir() path <- download_thymus_ageing() sce <- qs2::qs_read(path) ## End(Not run)
Drops the named columns from the obs or var table of single cell-related
classes. Protected identifier and bookkeeping columns (cell_idx,
cell_id, to_keep for obs; gene_idx, gene_id for var) are refused
with a warning. Columns that do not exist trigger a warning and are
skipped.
drop_cols_sc(object, table = c("obs", "var"), cols)drop_cols_sc(object, table = c("obs", "var"), cols)
object |
|
table |
String. One of |
cols |
Character vector. Column names to drop. |
Invisible self.
# drop a column that is no longer needed sc <- demo_single_cells(prepped = FALSE) sc <- drop_cols_sc(sc, table = "obs", cols = "batch_index") names(get_sc_obs(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# drop a column that is no longer needed sc <- demo_single_cells(prepped = FALSE) sc <- drop_cols_sc(sc, table = "obs", cols = "batch_index") names(get_sc_obs(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Filters, orders and optionally bins a logical samples x features matrix,
e.g. the regulon on/off calls from binarise_regulon_activity(), so it can
be drawn as a single raster with bixverse.plots::plot_binary_heatmap().
Features are clustered within their group on the Jaccard distance, so
shared absences do not count as similarity. Samples are clustered within
their group on the Hamming distance. Groups larger than max_cluster_n are
instead ordered by barycentre, the mean plot position of the features a
sample has on, which avoids the quadratic distance matrix. With more
samples than max_cols, consecutive samples within a group are collapsed
into roughly max_cols bins that hold the fraction of samples on. Every
group keeps at least one bin.
extract_binary_heatmap_data( binary_mat, sample_groups = NULL, feature_groups = NULL, heatmap_params = params_binary_heatmap(), .verbose = TRUE )extract_binary_heatmap_data( binary_mat, sample_groups = NULL, feature_groups = NULL, heatmap_params = params_binary_heatmap(), .verbose = TRUE )
binary_mat |
Logical matrix of samples x features with unique row and column names. |
sample_groups |
Optional named character vector or factor mapping
every row name of |
feature_groups |
Optional named character vector or factor mapping
every column name of |
heatmap_params |
List. Output of |
.verbose |
Boolean. Controls verbosity of the function. |
An object of class BinaryHeatmapData, a list with:
mat - Numeric matrix of features x columns in plot order. Values are 0/1, or the fraction of samples on if binned.
col_annot - data.table with one row per column: col_idx, group
(factor) and n_samples in that column.
row_annot - data.table with one row per feature: row_idx,
feature and group (factor).
binned - Boolean. Whether the samples were binned.
Without groups, group is a single level "all".
Aibar, et al., Nat Methods, 2017
set.seed(7L) binary_mat <- matrix(runif(200 * 30) > 0.7, nrow = 200) dimnames(binary_mat) <- list( sprintf("cell_%i", 1:200), sprintf("regulon_%i", 1:30) ) sample_groups <- setNames(rep(c("a", "b"), each = 100), rownames(binary_mat)) res <- extract_binary_heatmap_data( binary_mat, sample_groups = sample_groups, .verbose = FALSE ) dim(res$mat)set.seed(7L) binary_mat <- matrix(runif(200 * 30) > 0.7, nrow = 200) dimnames(binary_mat) <- list( sprintf("cell_%i", 1:200), sprintf("regulon_%i", 1:30) ) sample_groups <- setNames(rep(c("a", "b"), each = 100), rownames(binary_mat)) res <- extract_binary_heatmap_data( binary_mat, sample_groups = sample_groups, .verbose = FALSE ) dim(res$mat)
Extracts per-group mean expression and percentage of expressing cells for a set of genes. Returns a long-format data.table suitable for dot plots.
extract_dot_plot_data( object, features, grouping_variable, scale_exp = TRUE, modality = c("rna", "adt") )extract_dot_plot_data( object, features, grouping_variable, scale_exp = TRUE, modality = c("rna", "adt") )
object |
A single cell class. |
features |
Character vector. Gene IDs to extract. |
grouping_variable |
String. Column name in the obs table to group by. |
scale_exp |
Boolean. Whether to min-max scale mean expression per gene. |
modality |
String. One of |
A data.table with columns: gene, group, mean_exp, scaled_exp and pct_exp.
# mean expression and expressing fraction per cell group sc <- demo_single_cells() dt <- extract_dot_plot_data( sc, features = get_gene_names(sc)[1:5], grouping_variable = "cell_grp" ) head(dt, 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# mean expression and expressing fraction per cell group sc <- demo_single_cells() dt <- extract_dot_plot_data( sc, features = get_gene_names(sc)[1:5], grouping_variable = "cell_grp" ) head(dt, 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Pulls an embedding into a long data.table with standardised coordinate
columns (dim_1, dim_2, ...) and, optionally, observation metadata. The
embedding name is stored as an embedding attribute for axis labelling.
extract_embedding_data(object, embedding, obs_cols = NULL, ...)extract_embedding_data(object, embedding, obs_cols = NULL, ...)
object |
A single cell class. |
embedding |
String. Name of the embedding (e.g. |
obs_cols |
Optional character vector. Obs columns to attach. |
... |
Additional arguments forwarded to |
A data.table with cell_id, dim_* columns and any requested obs
columns.
# PCA coordinates with a cell annotation riding along sc <- demo_single_cells() dt <- extract_embedding_data(sc, "pca", obs_cols = "cell_grp") head(dt[, c("cell_id", "dim_1", "dim_2", "cell_grp")]) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# PCA coordinates with a cell annotation riding along sc <- demo_single_cells() dt <- extract_embedding_data(sc, "pca", obs_cols = "cell_grp") head(dt[, c("cell_id", "dim_1", "dim_2", "cell_grp")]) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Extracts two features into a wide data.table with feature_1 and
feature_2 value columns, ready for a scatter or hex plot. Each feature may
carry a _rna or _adt suffix to choose its modality independently (e.g.
"ENSG00000167286_rna" against "CD3_adt"); features without a suffix fall
back to modality. For SingleCells / MetaCells only RNA exists, so an
_adt feature there errors via extract_gene_expression().
extract_feature_pair( object, feature_1, feature_2, obs_cols = NULL, scale = FALSE, clip = NULL, modality = c("rna", "adt"), layer = c("norm", "magic") )extract_feature_pair( object, feature_1, feature_2, obs_cols = NULL, scale = FALSE, clip = NULL, modality = c("rna", "adt"), layer = c("norm", "magic") )
object |
A single cell class. |
feature_1 |
String. First feature, optionally |
feature_2 |
String. Second feature, optionally |
obs_cols |
Optional character vector. Obs columns to attach (e.g. to colour the scatter). |
scale |
Boolean. Whether to z-score the expression values per feature. |
clip |
Optional numeric. Clip z-scores if |
modality |
String. Fallback modality for unsuffixed features. One of
|
layer |
String. One of |
A data.table with cell_id, feature_1, feature_2 and any
requested obs columns. The original feature labels are stored in a
features attribute as c(feature_1, feature_2).
# two genes side by side, ready for a scatter sc <- demo_single_cells(prepped = FALSE) genes <- get_gene_names(sc)[1:2] head(extract_feature_pair(sc, genes[1], genes[2], obs_cols = "cell_grp")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# two genes side by side, ready for a scatter sc <- demo_single_cells(prepped = FALSE) genes <- get_gene_names(sc)[1:2] head(extract_feature_pair(sc, genes[1], genes[2], obs_cols = "cell_grp")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Combines extract_gene_expression() with extract_embedding_data() and
melts to long format, ready for faceted feature plots. The expression
source and the embedding source are chosen independently via
expr_modality and embd_modality, so you can colour an embedding from
one modality by expression from another (e.g. RNA expression on an
ADT-derived UMAP, or either modality on a WNN embedding). All sources key
on the same kept-cell barcodes, so the merge stays aligned regardless of
the chosen combination.
extract_feature_plot_data( object, features, embedding, scale = FALSE, clip = NULL, obs_col = NULL, expr_modality = c("rna", "adt"), embd_modality = c("rna", "adt", "wnn"), layer = c("norm", "magic"), ... )extract_feature_plot_data( object, features, embedding, scale = FALSE, clip = NULL, obs_col = NULL, expr_modality = c("rna", "adt"), embd_modality = c("rna", "adt", "wnn"), layer = c("norm", "magic"), ... )
object |
A single cell class. |
features |
Character vector. Gene/feature IDs to extract, taken from
|
embedding |
String. Name of the embedding. |
scale |
Boolean. Whether to z-score the expression values. |
clip |
Optional numeric. Clip z-scores if |
obs_col |
Optional character vector. Obs columns to attach. |
expr_modality |
String. Modality the expression is pulled from. One of
|
embd_modality |
String. Modality the embedding is pulled from. One of
|
layer |
String. One of |
... |
Additional arguments forwarded to |
A long data.table with cell_id, dim_*, gene and expression.
# two genes melted onto the PCA coordinates sc <- demo_single_cells() dt <- extract_feature_plot_data( sc, features = get_gene_names(sc)[1:2], embedding = "pca" ) head(dt[, c("cell_id", "dim_1", "dim_2", "gene", "expression")]) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# two genes melted onto the PCA coordinates sc <- demo_single_cells() dt <- extract_feature_plot_data( sc, features = get_gene_names(sc)[1:2], embedding = "pca" ) head(dt[, c("cell_id", "dim_1", "dim_2", "gene", "expression")]) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Extracts dense normalised (log1p) expression values for a set of genes, optionally with additional observation metadata columns.
extract_gene_expression( object, features, obs_cols = NULL, scale = FALSE, clip = NULL, modality = c("rna", "adt"), layer = c("norm", "magic") )extract_gene_expression( object, features, obs_cols = NULL, scale = FALSE, clip = NULL, modality = c("rna", "adt"), layer = c("norm", "magic") )
object |
A single cell class. |
features |
Character vector. Gene IDs to extract. |
obs_cols |
Optional character vector. Column names from the obs table to include. |
scale |
Boolean. Whether to z-score the expression values. |
clip |
Optional numeric. If |
modality |
String. One of |
layer |
String. One of |
A data.table with a cell_id column, one column per gene, and
any requested obs columns.
# normalised expression of three genes with a cell annotation sc <- demo_single_cells() dt <- extract_gene_expression( sc, features = get_gene_names(sc)[1:3], obs_cols = "cell_grp" ) head(dt, 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# normalised expression of three genes with a cell annotation sc <- demo_single_cells() dt <- extract_gene_expression( sc, features = get_gene_names(sc)[1:3], obs_cols = "cell_grp" ) head(dt, 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Combines extract_gene_expression() with a grouping obs column and melts to
long format, ready for stacked (one gene per row) violin plots.
extract_gene_violin_data( object, features, grouping_variable, scale = FALSE, clip = NULL, modality = c("rna", "adt"), layer = c("norm", "magic") )extract_gene_violin_data( object, features, grouping_variable, scale = FALSE, clip = NULL, modality = c("rna", "adt"), layer = c("norm", "magic") )
object |
A single cell class. |
features |
Character vector. Gene IDs to extract. |
grouping_variable |
String. Obs column to group by. |
scale |
Boolean. Whether to z-score the expression values. |
clip |
Optional numeric. Clip z-scores if |
modality |
String. One of |
layer |
String. One of |
A long data.table with cell_id, group, gene and expression.
gene is an ordered factor following features.
# long format expression grouped by cell type sc <- demo_single_cells(prepped = FALSE) head(extract_gene_violin_data( sc, features = get_gene_names(sc)[1:2], grouping_variable = "cell_grp" )) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# long format expression grouped by cell type sc <- demo_single_cells(prepped = FALSE) head(extract_gene_violin_data( sc, features = get_gene_names(sc)[1:2], grouping_variable = "cell_grp" )) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Places every tested neighbourhood of a get_miloR_abundances_sc() result at
the embedding coordinate of its index cell, as Milo's plotNhoodGraphDA()
does, and returns the neighbourhood graph as node and edge tables. Two
neighbourhoods are connected by the number of cells they share.
The logFC is returned as is. Masking non-significant neighbourhoods is left
to the plot, is_sig carries the call.
extract_milo_plot_data( object, milo_res, embedding = "umap", alpha = 0.1, overlap = 1L, ... )extract_milo_plot_data( object, milo_res, embedding = "umap", alpha = 0.1, overlap = 1L, ... )
object |
A single cell class. The one the miloR result was built on. |
milo_res |
|
embedding |
String. Name of the embedding to position the nodes in. |
alpha |
Numeric. Spatial FDR threshold for |
overlap |
Integer. Minimum number of shared cells for an edge. Defaults
to |
... |
Additional arguments forwarded to |
A list with the embedding stored as an embedding attribute and
nodes - data.table with Nhood, dim_1, dim_2, size (cells in
the neighbourhood), every column of the differential abundance results and
is_sig.
edges - data.table with from, to, weight (shared cells) and
the segment coordinates x, y, xend, yend.
Dann, et al., Nat Biotechnol, 2022
Puts every cluster of a run_paga_sc() result at the centroid of its cells
in an embedding and returns the abstracted graph as node and edge tables. A
node-link plot drawn from these sits where the reader already knows the
biology is, which a free layout of the abstracted graph does not.
The abstracted graph is close to complete on real data, so threshold is
doing real work. Drop it to zero and you get a hairball. tree_only is the
shortcut to the backbone.
extract_paga_plot_data( object, paga_res, embedding = "umap", cluster_col = NULL, threshold = 0.01, tree_only = FALSE, centroid = c("median", "mean"), node_stat_col = NULL, ... )extract_paga_plot_data( object, paga_res, embedding = "umap", cluster_col = NULL, threshold = 0.01, tree_only = FALSE, centroid = c("median", "mean"), node_stat_col = NULL, ... )
object |
A single cell class. |
paga_res |
|
embedding |
String. Name of the embedding to position the nodes in. |
cluster_col |
Optional string. The obs column holding the clustering. Defaults to the one the PAGA run used, and errors if a different one is given: nodes from one clustering with edges from another produce a plausible looking graph that is wrong. |
threshold |
Numeric. Edges below this connectivity are dropped.
Defaults to |
tree_only |
Boolean. Use the maximum spanning forest rather than the
full abstracted graph. Defaults to |
centroid |
String. One of |
node_stat_col |
Optional string. A numeric obs column to summarise per
cluster with the same statistic, e.g. |
... |
Additional arguments forwarded to |
A list with the embedding stored as an embedding attribute and
nodes - data.table with cluster (a factor in graph order),
dim_1, dim_2, n_cells and, when node_stat_col is given, stat.
Clusters holding no cells are dropped, as they have no position.
edges - data.table with from, to, weight and the coordinates
x, y, xend, yend of both ends, ready for a segment layer. Each
edge appears once, despite the graph being stored symmetrically.
Wolf, et al., Genome Biol., 2019.
# the abstracted graph placed on the PCA coordinates sc <- demo_single_cells() sc <- find_clusters_sc(sc, res = 1.0) paga <- run_paga_sc(sc, cluster_col = "leiden_clustering", .verbose = FALSE) extract_paga_plot_data(sc, paga, embedding = "pca")$nodes unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the abstracted graph placed on the PCA coordinates sc <- demo_single_cells() sc <- find_clusters_sc(sc, res = 1.0) paga <- run_paga_sc(sc, cluster_col = "leiden_clustering", .verbose = FALSE) extract_paga_plot_data(sc, paga, embedding = "pca")$nodes unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Helper function to check for expected clustering vs actual clustering.
f1_score_confusion_mat(clusters_a, clusters_b)f1_score_confusion_mat(clusters_a, clusters_b)
clusters_a |
String or factor. The clustering of algorithm 1. |
clusters_b |
String or factor. The clustering of algorithm 2. |
Named vector with the F1 scores between the two clustering algorithms.
# agreement between two clusterings of the same six samples clusters_a <- c("c1", "c1", "c2", "c2", "c3", "c3") clusters_b <- c("x", "x", "y", "y", "z", "x") f1_score_confusion_mat(clusters_a, clusters_b)# agreement between two clusterings of the same six samples clusters_a <- c("c1", "c1", "c2", "c2", "c3", "c3") clusters_b <- c("x", "x", "y", "y", "z", "x") f1_score_confusion_mat(clusters_a, clusters_b)
Runs k-means on the chosen embedding, builds a kNN graph on the centroids, applies Louvain clustering and propagates memberships back to the cells. Optionally runs a grid over multiple seeds and returns stability statistics.
fast_cluster_sc( object, embd_to_use = "pca", no_embd_to_use = NULL, resolutions = c(2, 1, 0.5), km_type = c("kmeans", "minibatch"), n_centroids = NULL, fc_params = params_sc_fast_cluster(), snn = TRUE, return_kmeans = FALSE, grid_search = FALSE, no_seeds = 10L, seed = 42L, .verbose = TRUE )fast_cluster_sc( object, embd_to_use = "pca", no_embd_to_use = NULL, resolutions = c(2, 1, 0.5), km_type = c("kmeans", "minibatch"), n_centroids = NULL, fc_params = params_sc_fast_cluster(), snn = TRUE, return_kmeans = FALSE, grid_search = FALSE, no_seeds = 10L, seed = 42L, .verbose = TRUE )
object |
|
embd_to_use |
String. Embedding name. Defaults to |
no_embd_to_use |
Optional integer. Number of dimensions to keep. |
resolutions |
Numeric vector. Louvain resolutions. |
km_type |
String. One of |
n_centroids |
Optional integer. Number of k-means centroids. Defaults
to |
fc_params |
List. Output of |
snn |
Boolean. Convert kNN to sNN. |
return_kmeans |
Boolean. Return k-means assignments and centroids. |
grid_search |
Boolean. Run multi-seed grid version. |
no_seeds |
Integer. Number of additional seeds (only used when
|
seed |
Integer. Reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
SingleCellFastClusters S3 object with:
data.table with cell_idx and one column per
resolution (res_<value>).
data.table of grid statistics, or NULL.
Integer vector of k-means assignments, or NULL.
Numeric matrix of centroids, or NULL.
Resolutions used.
with cell_indices stored as an attribute (0-indexed).
# k-means centroids, Louvain on top, memberships back to the cells sc <- demo_single_cells() res <- fast_cluster_sc( sc, resolutions = c(1.0, 0.5), n_centroids = 30L, .verbose = FALSE ) head(get_data(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# k-means centroids, Louvain on top, memberships back to the cells sc <- demo_single_cells() res <- fast_cluster_sc( sc, resolutions = c(1.0, 0.5), n_centroids = 30L, .verbose = FALSE ) head(get_data(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This functions is a wrapper over the Rust implementation of fastICA and the
generation of the pre-processed data and pre-whitening matrix. It has
the same two options c("logcosh", "exp") to run ICA in parallel modus. You
can control the parameters of ICA via ica_params.
fast_ica_rust( X, n_icas, ica_fun = c("logcosh", "exp"), ica_params = params_ica_general(), fast_svd = TRUE, seed = NULL )fast_ica_rust( X, n_icas, ica_fun = c("logcosh", "exp"), ica_params = params_ica_general(), fast_svd = TRUE, seed = NULL )
X |
Numeric matrix. The data on which you want to run fastICA. |
n_icas |
Integer. Number of independent components to recover. |
ica_fun |
String, element of |
ica_params |
List. The ICA parameters, see
|
fast_svd |
Boolean. Shall the randomised SVD be used. This is faster, but less precise. |
seed |
Integer. Seed to ensure reproducible results. |
A list containing:
w The mixing matrix w.
A ICA results matrix A.
S ICA results matrix S.
converged Boolean indicating if algorithm converged.
# recover two mixed sources sources <- cbind(sin((1:1000) / 20), rep(((1:200) - 100) / 100, 5)) mixed <- sources %*% matrix(c(0.291, 0.6557, -0.5439, 0.5572), 2, 2) ica_res <- fast_ica_rust(mixed, n_icas = 2L, ica_fun = "logcosh", seed = 42L) max(abs(cor(sources[, 1], t(ica_res$S))))# recover two mixed sources sources <- cbind(sin((1:1000) / 20), rep(((1:200) - 100) / 100, 5)) mixed <- sources %*% matrix(c(0.291, 0.6557, -0.5439, 0.5572), 2, 2) ica_res <- fast_ica_rust(mixed, n_icas = 2L, ica_fun = "logcosh", seed = 42L) max(abs(cor(sources[, 1], t(ica_res$S))))
This functions is a wrapper over the Rust implementation of fastICA expected
already the pre-processed matrix and the pre-whitening matrix. It has
the same two options c("logcosh", "exp") to run ICA in parallel modus. You
can control the parameters of ICA via ica_params.
fast_ica_rust_helper( X, K, n_icas, ica_fun = c("logcosh", "exp"), seed = NULL, ica_params = params_ica_general() )fast_ica_rust_helper( X, K, n_icas, ica_fun = c("logcosh", "exp"), seed = NULL, ica_params = params_ica_general() )
X |
Numeric matrix. Processed data. Output of
|
K |
The K matrix. Pre-whitening matrix. Output of
|
n_icas |
Integer. Number of independent components to recover. |
ica_fun |
String, element of |
seed |
Integer. Seed to ensure reproducible results. |
ica_params |
List. The ICA parameters, see
|
A list containing:
w The mixing matrix w.
A ICA results matrix A.
S ICA results matrix S.
converged Boolean indicating if algorithm converged.
# same run, but with the whitening done up front sources <- cbind(sin((1:1000) / 20), rep(((1:200) - 100) / 100, 5)) mixed <- sources %*% matrix(c(0.291, 0.6557, -0.5439, 0.5572), 2, 2) whitened <- rs_prepare_whitening( x = mixed, fast_svd = TRUE, seed = 42L, rank = NULL, oversampling = NULL, n_power_iter = NULL ) ica_res <- fast_ica_rust_helper( X = whitened$x, K = whitened$k, n_icas = 2L, ica_fun = "logcosh", seed = 42L ) dim(ica_res$S)# same run, but with the whitening done up front sources <- cbind(sin((1:1000) / 20), rep(((1:200) - 100) / 100, 5)) mixed <- sources %*% matrix(c(0.291, 0.6557, -0.5439, 0.5572), 2, 2) whitened <- rs_prepare_whitening( x = mixed, fast_svd = TRUE, seed = 42L, rank = NULL, oversampling = NULL, n_power_iter = NULL ) ica_res <- fast_ica_rust_helper( X = whitened$x, K = whitened$k, n_icas = 2L, ica_fun = "logcosh", seed = 42L ) dim(ica_res$S)
This function implements the fast mutual nearest neighbour (MNN) from Haghverdi, et al. This version works on the PCA embedding and generates an embedding only and not a fully corrected count matrix. The function will iterate through the batches, identify the MNN and generate correction vectors and generate a corrected embedding which is added to the function.
fast_mnn_sc( object, batch_column, batch_hvg_genes, fastmnn_params = params_sc_fastmnn(), use_precomputed_pca = FALSE, seed = 42L, .verbose = TRUE )fast_mnn_sc( object, batch_column, batch_hvg_genes, fastmnn_params = params_sc_fastmnn(), use_precomputed_pca = FALSE, seed = 42L, .verbose = TRUE )
object |
|
batch_column |
String. The column with the batch information in the obs data of the class. |
batch_hvg_genes |
Integer vector. These are the highly variable genes,
identified by a batch-aware method. Please refer to
|
fastmnn_params |
A list, please see
|
use_precomputed_pca |
Boolean. Should the PCA in the object be used if found. If you decide to do this, make sure that you have run the PCA on the batch-aware HVG ideally. |
seed |
Integer. Random seed. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The object with the added fastMNN embeddings to the object.
Haghverdi, et al., Nat Biotechnol, 2018
# fastMNN over batch aware highly variable genes sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) hvg <- find_hvg_batch_aware_sc( sc, hvg_no = 20L, batch_column = "batch_index", .verbose = FALSE ) sc <- fast_mnn_sc( sc, batch_column = "batch_index", batch_hvg_genes = hvg$hvg_gene_idx, fastmnn_params = params_sc_fastmnn( no_pcs = 10L, knn = list(k = 5L) ), .verbose = FALSE ) dim(get_embedding(sc, "mnn")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# fastMNN over batch aware highly variable genes sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) hvg <- find_hvg_batch_aware_sc( sc, hvg_no = 20L, batch_column = "batch_index", .verbose = FALSE ) sc <- fast_mnn_sc( sc, batch_column = "batch_index", batch_hvg_genes = hvg$hvg_gene_idx, fastmnn_params = params_sc_fastmnn( no_pcs = 10L, knn = list(k = 5L) ), .verbose = FALSE ) dim(get_embedding(sc, "mnn")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This method takes the GeneOntologyElim and a vector of gene level statistics to perform fgsea (multi-level) leveraging ontological information. It starts at the lowest levels of the ontology and tests if there is significant enrichment for any GO terms. If the threshold of the p-value is below the elimination threshold, the genes from this term will be removed from all its ancestors. The function then proceeds to the next level of the ontology and repeats the process. Subsequently, it leverages the multi-level method to estimate lower p-values for significant terms, see Korotkevich, et al.
fgsea_go_elim( object, stats, elim_threshold = 0.05, nperm = 2000L, gsea_params = params_gsea(max_size = 2000L), seed = 42L )fgsea_go_elim( object, stats, elim_threshold = 0.05, nperm = 2000L, gsea_params = params_gsea(max_size = 2000L), seed = 42L )
object |
The underlying class, see |
stats |
Named numeric vector. The gene level statistic. |
elim_threshold |
Float. Threshold from which p-value onwards the elimination on the ancestors shall be conducted. |
nperm |
Integer. Number of permutation tests. Defaults to |
gsea_params |
List. The GSEA parameters, see
|
seed |
Random seed for reproducibility. |
data.table with enrichment results.
Korotkevich, et al., bioRxiv
# human GO terms with at least 25 genes go_obj <- GeneOntologyElim( get_go_data_human(.verbose = FALSE), min_genes = 25L ) genes <- unique(unlist(S7::prop(go_obj, "go_to_genes"))) set.seed(1L) stats <- stats::setNames(rnorm(length(genes)), genes) stats[1:200] <- stats[1:200] + 2 res <- fgsea_go_elim(go_obj, stats = stats, nperm = 1000L) head(res, 3)# human GO terms with at least 25 genes go_obj <- GeneOntologyElim( get_go_data_human(.verbose = FALSE), min_genes = 25L ) genes <- unique(unlist(S7::prop(go_obj, "go_to_genes"))) set.seed(1L) stats <- stats::setNames(rnorm(length(genes)), genes) stats[1:200] <- stats[1:200] + 2 res <- fgsea_go_elim(go_obj, stats = stats, nperm = 1000L) head(res, 3)
This method takes the GeneOntologyElim and a vector of gene level statistics to perform fgsea (simple) leveraging ontological information. It starts at the lowest levels of the ontology and tests if there is significant enrichment for any GO terms. If the threshold of the p-value is below the elimination threshold, the genes from this term will be removed from all its ancestors. The function then proceeds to the next level of the ontology and repeats the process.
fgsea_simple_go_elim( object, stats, elim_threshold = 0.05, nperm = 2000L, gsea_params = params_gsea(max_size = 2000L), seed = 42L )fgsea_simple_go_elim( object, stats, elim_threshold = 0.05, nperm = 2000L, gsea_params = params_gsea(max_size = 2000L), seed = 42L )
object |
The underlying class, see |
stats |
Named numeric vector. The gene level statistic. |
elim_threshold |
Float. Threshold from which p-value onwards the elimination on the ancestors shall be conducted. |
nperm |
Integer. Number of permutation tests. Defaults to |
gsea_params |
List. The GSEA parameters, see
|
seed |
Random seed for reproducibility. |
data.table with enrichment results.
# human GO terms with at least 25 genes go_obj <- GeneOntologyElim( get_go_data_human(.verbose = FALSE), min_genes = 25L ) genes <- unique(unlist(S7::prop(go_obj, "go_to_genes"))) set.seed(1L) stats <- stats::setNames(rnorm(length(genes)), genes) stats[1:200] <- stats[1:200] + 2 res <- fgsea_simple_go_elim(go_obj, stats = stats, nperm = 1000L) head(data.table::setorder(res, pvals), 3)# human GO terms with at least 25 genes go_obj <- GeneOntologyElim( get_go_data_human(.verbose = FALSE), min_genes = 25L ) genes <- unique(unlist(S7::prop(go_obj, "go_to_genes"))) set.seed(1L) stats <- stats::setNames(rnorm(length(genes)), genes) stats[1:200] <- stats[1:200] + 2 res <- fgsea_simple_go_elim(go_obj, stats = stats, nperm = 1000L) head(data.table::setorder(res, pvals), 3)
This function calculates the critical value, see
calculate_critical_value() and filters subsequently all the
term pairs to the ones with a value ≥ critical value.
filter_similarities( object, alpha, permutations = 100000L, seed = 10101L, .verbose = TRUE )filter_similarities( object, alpha, permutations = 100000L, seed = 10101L, .verbose = TRUE )
object |
|
alpha |
Float. The alpha value. For example, 0.001 would mean that the critical value is smaller than 0.1 percentile of the random permutations. |
permutations |
Number of random permutations. |
seed |
Integer. For reproducibility purposes. |
.verbose |
Boolean. Controls verbosity of the function. |
The class with filtered results added to the respective slot.
# keep only term pairs above the permutation-derived critical value onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) onto_obj <- OntologySim(onto, .verbose = FALSE) onto_obj <- calculate_wang_sim_onto( onto_obj, weights = c(part_of = 0.8, is_a = 0.6), .verbose = FALSE ) onto_obj <- filter_similarities(onto_obj, alpha = 0.1, .verbose = FALSE) head(get_results(onto_obj))# keep only term pairs above the permutation-derived critical value onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) onto_obj <- OntologySim(onto, .verbose = FALSE) onto_obj <- calculate_wang_sim_onto( onto_obj, weights = c(part_of = 0.8, is_a = 0.6), .verbose = FALSE ) onto_obj <- filter_similarities(onto_obj, alpha = 0.1, .verbose = FALSE) head(get_results(onto_obj))
This function can be used to run differential gene expression for every group of an unsupervised clustering method for example. You specify a column and the function calculates differential gene expression of the first cluster vs. everything else, second cluster vs. everything else, etc. All groups are tested in a single pass over the genes, so memory stays bounded however many cells there are.
find_all_markers_sc( object, column_of_interest, method = "wilcox", alternative = c("greater", "less", "twosided"), min_prop = 0.05, .verbose = TRUE )find_all_markers_sc( object, column_of_interest, method = "wilcox", alternative = c("greater", "less", "twosided"), min_prop = 0.05, .verbose = TRUE )
object |
|
column_of_interest |
String. The column you wish to use to identify the markers between all combination. Needs to be in the obs table |
method |
String. Which method to use for the calculations of the DGE.
At the moment the only option is |
alternative |
String. Test alternative. One of
|
min_prop |
Numeric. The minimum proportion of cells that need to express the gene to be tested in the group or in the rest. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
data.table with the DGE results from the test.
# one versus rest across the Leiden clusters sc <- demo_single_cells() sc <- find_clusters_sc(sc, res = 1.0) res <- find_all_markers_sc( sc, column_of_interest = "leiden_clustering", .verbose = FALSE ) head(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# one versus rest across the Leiden clusters sc <- demo_single_cells() sc <- find_clusters_sc(sc, res = 1.0) res <- find_all_markers_sc( sc, column_of_interest = "leiden_clustering", .verbose = FALSE ) head(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function will apply Leiden clustering on the sNN graph with the
given resolution and add a column to the obs table. Runs in Rust via
leiden_clustering(); the labels depend on seed only, 0 is the
largest cluster.
find_clusters_sc( object, cluster_algorithm = c("leiden", "louvain"), res = 1, name = "leiden_clustering", modality = c("rna", "adt", "wnn"), seed = 42L )find_clusters_sc( object, cluster_algorithm = c("leiden", "louvain"), res = 1, name = "leiden_clustering", modality = c("rna", "adt", "wnn"), seed = 42L )
object |
|
cluster_algorithm |
String. One of |
res |
Numeric. The modularity resolution parameter, see
|
name |
String. The name to add to the obs table in the DuckDB. |
modality |
String. On which modality to run the UMAP. One of
|
seed |
Integer. For reproducibility. |
The object with added clustering in the obs table.
# Leiden on the cached sNN graph sc <- demo_single_cells() sc <- find_clusters_sc(sc, res = 1.0, name = "clusters") table(get_sc_obs(sc)$clusters) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# Leiden on the cached sNN graph sc <- demo_single_cells() sc <- find_clusters_sc(sc, res = 1.0, name = "clusters") table(get_sc_obs(sc)$clusters) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This is a helper function to identify highly variable genes in a batch-aware manner. At the moment the implementation has only the VST-based version (known as Seurat v3). The other methods will be implemented in the future. This function will calculate the HVG per given experimental batch and you can choose the way to combine them. The choices are union (of Top x HVG per batch), based on the average variance per batch or only take genes that are amongst the Top X HVG in all batches. Important. The function returns 0-indices for the genes!
find_hvg_batch_aware_sc( object, batch_column, hvg_no = 2000L, gene_comb_method = c("union", "average", "intersection"), hvg_params = params_sc_hvg(), streaming = NULL, .verbose = TRUE )find_hvg_batch_aware_sc( object, batch_column, hvg_no = 2000L, gene_comb_method = c("union", "average", "intersection"), hvg_params = params_sc_hvg(), streaming = NULL, .verbose = TRUE )
object |
|
batch_column |
String. The column name of the batch column in the obs table. |
hvg_no |
Integer. Number of highly variable genes to include. Defaults
to |
gene_comb_method |
String. One of
|
hvg_params |
List, see
|
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
This function will return a list with:
hvg_genes - The gene names of the HVGs.
hvg_gene_idx - The (0-index) gene features.
hvg_data - data.table with the detailed information of the variance per batch.
# highly variable genes taken as the union over the batches sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) hvg <- find_hvg_batch_aware_sc( sc, hvg_no = 20L, batch_column = "batch_index", gene_comb_method = "union", .verbose = FALSE ) head(hvg$hvg_genes) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# highly variable genes taken as the union over the batches sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) hvg <- find_hvg_batch_aware_sc( sc, hvg_no = 20L, batch_column = "batch_index", gene_comb_method = "union", .verbose = FALSE ) head(hvg$hvg_genes) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This is a helper function to identify highly variable genes for SingleCells
(using the Rust-based streaming of data) or MetaCells.
Small cell selections (up to 10% of the stored cells and at most 100k
cells, e.g. most SingleCellsSubset objects) are read from the cell-major
file into memory rather than sweeping the whole gene-major file, which makes
the cost follow the selection instead of the store.
find_hvg_sc( object, hvg_no = 2000L, hvg_params = params_sc_hvg(), streaming = NULL, .verbose = TRUE )find_hvg_sc( object, hvg_no = 2000L, hvg_params = params_sc_hvg(), streaming = NULL, .verbose = TRUE )
object |
|
hvg_no |
Integer. Number of highly variable genes to include. Defaults
to |
hvg_params |
List, see
|
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
It will add the per-gene HVG statistics to the var table: mean,
var, var_exp and var_std for "vst"; mean, dispersion,
dispersion_scaled and bin for "meanvarbin" and "dispersion";
scran_mean, scran_var, scran_fitted and scran_residual (log2 scale)
for "scran".
# the twenty most variable genes by the vst method sc <- demo_single_cells(prepped = FALSE) sc <- find_hvg_sc(sc, hvg_no = 20L, .verbose = FALSE) length(get_hvg(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the twenty most variable genes by the vst method sc <- demo_single_cells(prepped = FALSE) sc <- find_hvg_sc(sc, hvg_no = 20L, .verbose = FALSE) length(get_hvg(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function can be used to calculate differentially expressed genes between two groups in the single cell data. At the moment, it has only an implementation for the Wilcox-based rank statistic.
find_markers_sc( object, cells_1, cells_2, method = c("wilcox"), alternative = c("twosided", "greater", "less"), min_prop = 0.05, .verbose = TRUE )find_markers_sc( object, cells_1, cells_2, method = c("wilcox"), alternative = c("twosided", "greater", "less"), min_prop = 0.05, .verbose = TRUE )
object |
|
cells_1 |
String. The names of the cells in group 1. Need to be part
of the cell names in the object, see |
cells_2 |
String. The names of the cells in group 2. Need to be part
of the cell names in the object, see |
method |
String. Which method to use for the calculations of the DGE.
At the moment the only option is |
alternative |
String. Test alternative. One of
|
min_prop |
Numeric. The minimum proportion of cells that need to express the gene to be tested in any of the two groups. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
data.table with the DGE results from the test.
# Wilcoxon test between two of the planted cell types sc <- demo_single_cells() obs <- get_sc_obs(sc) res <- find_markers_sc( sc, cells_1 = obs$cell_id[obs$cell_grp == "cell_type_1"], cells_2 = obs$cell_id[obs$cell_grp == "cell_type_2"], .verbose = FALSE ) head(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# Wilcoxon test between two of the planted cell types sc <- demo_single_cells() obs <- get_sc_obs(sc) res <- find_markers_sc( sc, cells_1 = obs$cell_id[obs$cell_grp == "cell_type_1"], cells_2 = obs$cell_id[obs$cell_grp == "cell_type_2"], .verbose = FALSE ) head(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function will generate the kNNs based on a given embedding. Available algorithms are:
kmknn - An exact kNN search that leverages k-means clustering under
the hood to prune out data points. The default setting.
exhaustive - An exhaustive, flat index. On smaller data sets often
faster than the approximate nearest neighbour search algorithms.
hnsw - Hierarchical Navigable Small World. A graph-based
approximate nearest neighbour search algorithm; works well on large data
sets. A benign race condition is leveraged during index build, making the
build non-deterministic. Bigger impact on smaller data sets.
nndescent - Nearest neighbour descent. Leverages concepts from
PyNNDescent and works well on very large data sets similar to hnsw.
Set extract_knn = TRUE in the kNN parameters to hand back the descent
graph directly instead of beam searching it. That drops the query pass
altogether, so it is much faster, but recall goes down a little.
ivf - Inverted file index. Uses first k-means clustering to
identify Voronoi cells and leverages these during querying. Works well
on large data sets with high dimensionality and when you need to return
large number of neighbours.
annoy - Approximate nearest neighbours Oh Yeah. Tree-based index,
used across different R single cell packages (Seurat, SCE). This version
is purely memory-based.
Subsequently, the kNN graph will be additionally transformed into a shared nearest neighbour graph for clustering methods.
find_neighbours_sc( object, embd_to_use = "pca", no_embd_to_use = NULL, modality = c("rna", "adt"), neighbours_params = params_sc_neighbours(), seed = 42L, .verbose = TRUE )find_neighbours_sc( object, embd_to_use = "pca", no_embd_to_use = NULL, modality = c("rna", "adt"), neighbours_params = params_sc_neighbours(), seed = 42L, .verbose = TRUE )
object |
|
embd_to_use |
String. The embedding to use. Whichever you chose, it needs to be part of the object. |
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
modality |
String. One of |
neighbours_params |
List. Output of
|
seed |
Integer. For reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The object with added KNN matrix.
# kNN and the sNN graph on top of the PCA sc <- demo_single_cells(prepped = FALSE) sc <- find_hvg_sc(sc, hvg_no = 30L, .verbose = FALSE) sc <- calculate_pca_sc(sc, no_pcs = 10L, .verbose = FALSE) sc <- find_neighbours_sc( sc, neighbours_params = params_sc_neighbours(knn = list(k = 15L)), .verbose = FALSE ) dim(get_knn_mat(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# kNN and the sNN graph on top of the PCA sc <- demo_single_cells(prepped = FALSE) sc <- find_hvg_sc(sc, hvg_no = 30L, .verbose = FALSE) sc <- calculate_pca_sc(sc, no_pcs = 10L, .verbose = FALSE) sc <- find_neighbours_sc( sc, neighbours_params = params_sc_neighbours(knn = list(k = 15L)), .verbose = FALSE ) dim(get_knn_mat(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function will identify communities in the reciprocal best hit (RBH) graph. It will iterate through resolutions and add the results to the class. Additionally, a column will be added that signifies the resolution with the best modularity.
find_rbh_communities( object, resolution_params = params_graph_resolution(), max_workers = NULL, parallel = TRUE, random_seed = 42L, .verbose = TRUE )find_rbh_communities( object, resolution_params = params_graph_resolution(), max_workers = NULL, parallel = TRUE, random_seed = 42L, .verbose = TRUE )
object |
The underlying class, see |
resolution_params |
List. Parameters for the resolution search, see
|
max_workers |
Integer. Number of maximum cores to use. Defaults to half of the identified cores (to a maximum of 8). |
parallel |
Boolean. Shall the resolution search be in parallel. |
random_seed |
Integer. Random seed for reproducibility. |
.verbose |
Boolean. Controls verbosity of the function. |
The class with added community detection results.
# Leiden communities across a resolution sweep of the RBH graph set.seed(123) modules <- data.table::data.table( origin = rep(c("set_a", "set_b"), each = 20), module = rep(c("m1", "m2", "m3", "m4"), each = 10), gene = unlist(replicate(4, sample(letters, 10), simplify = FALSE)) ) object <- RbhGraph( modules, rbh_type = "set", dataset_col = "origin", module_col = "module", value_col = "gene" ) object <- generate_rbh_graph(object, minimum_similarity = 0) object <- find_rbh_communities(object, parallel = FALSE, .verbose = FALSE) head(get_results(object))# Leiden communities across a resolution sweep of the RBH graph set.seed(123) modules <- data.table::data.table( origin = rep(c("set_a", "set_b"), each = 20), module = rep(c("m1", "m2", "m3", "m4"), each = 10), gene = unlist(replicate(4, sample(letters, 10), simplify = FALSE)) ) object <- RbhGraph( modules, rbh_type = "set", dataset_col = "origin", module_col = "module", value_col = "gene" ) object <- generate_rbh_graph(object, minimum_similarity = 0) object <- find_rbh_communities(object, parallel = FALSE, .verbose = FALSE) head(get_results(object))
This function scores one group of cells against every other group of a column
separately, instead of against all of them pooled together. That difference
matters for markers. A pooled test is dominated by whichever rival
contributes the most cells, so a gene that is high in the reference and just
as high in one small rival still comes out looking like a clean marker. Here
the gene has to hold up against every rival, and the per-gene summaries
(min_auroc, median_auroc, min_rank) tell you whether it does.
Leave reference_group as NULL to run every group of the column as the
reference, or name one group to only get that arm. Either way, everything is
calculated in a single pass over the genes.
The summaries rank on AUROC rather than the p-value on purpose. Group sizes vary a lot in practice and p-values scale with the group sizes, so a large rival would otherwise crowd out a small one regardless of effect size.
find_specific_markers_sc( object, column_of_interest, reference_group = NULL, method = "wilcox", alternative = c("greater", "less", "twosided"), min_prop = 0.05, .verbose = TRUE )find_specific_markers_sc( object, column_of_interest, reference_group = NULL, method = "wilcox", alternative = c("greater", "less", "twosided"), min_prop = 0.05, .verbose = TRUE )
object |
|
column_of_interest |
String. The column in the obs table holding the groups, e.g. a cell type annotation or a clustering. |
reference_group |
Optional string. The group to use as the reference. If
|
method |
String. Which method to use for the calculations of the DGE.
At the moment the only option is |
alternative |
String. Test alternative. One of
|
min_prop |
Numeric. The minimum proportion of cells that need to express the gene in at least one of the groups. Applied once, globally, so every comparison's FDR is calculated over the same gene set. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A ScSpecificMarkers class with the following elements
summary - data.table. Per gene and reference group, the summaries
across all rivals: prop_ref, median_auroc, min_auroc, mean_auroc,
max_auroc, worst_rival (the rival achieving min_auroc), min_rank
(best AUROC rank the gene reaches against any single rival), the
Simes-combined p-value with its FDR, and the maximum p-value with its FDR.
per_comparison - data.table. Per gene, reference group and rival, the
underlying auroc, lfc, prop_ref, prop_rival, z_scores, p_values
and fdr.
params - List. The parameters the run used.
Soneson and Robinson, Nat Methods, 2018; Lun, et al., F1000Research, 2016
# markers that hold up against every rival cell type sc <- demo_single_cells() res <- find_specific_markers_sc( sc, column_of_interest = "cell_grp", .verbose = FALSE ) head(res$summary) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# markers that hold up against every rival cell type sc <- demo_single_cells() res <- find_specific_markers_sc( sc, column_of_interest = "cell_grp", .verbose = FALSE ) head(res$summary) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Find a threshold via the Otsu method
find_threshold_otsu(x, bins = 100L)find_threshold_otsu(x, bins = 100L)
x |
Numerical vector. The vector for which to find the threshold via the Otsu method |
bins |
Integer. Number of bins to use. |
The threshold.
# threshold separating two Gaussian modes set.seed(42) find_threshold_otsu(c(rnorm(100), rnorm(100, mean = 5)))# threshold separating two Gaussian modes set.seed(42) find_threshold_otsu(c(rnorm(100), rnorm(100, mean = 5)))
Fits scTransform (v2) or the analytic Pearson residual model over the cells
currently kept. The fit lands in the cache, and
find_hvg_sc() with params_sc_hvg(method = "residual"),
calculate_pca_sc() with residuals = TRUE and
sct_corrected_counts_sc() all read it from there.
Unlike the log-normalised layer, which is computed once at ingestion and written to disk, nothing is precomputed here: the residual rows are regenerated on demand by whatever consumes the fit.
Small cell selections (up to 10% of the stored cells and at most 100k cells) are read from the cell-major file into memory for the fit, the residual HVG selection and the residual PCA, instead of sweeping the whole gene-major file each time.
With group_column one model is fitted per group. That is what a
multi-sample experiment wants, since each sample keeps its own depth and
composition, and it changes how the residual HVG selection behaves, see
params_sc_hvg().
For MetaCells the counts are summed UMIs, so the negative binomial still
applies, but at a much greater depth than a single cell. Prefer
method = "analytic_pearson" there, and revisit the defaults of
params_sc_sctransform(): n_genes and n_cells are sized for
raw cells, of which there are usually far more than meta cells.
fit_residuals_sc( object, method = c("sctransform", "analytic_pearson"), group_column = NULL, covariate_columns = NULL, residual_params = NULL, gene_batch_size = NULL, seed = 42L, .verbose = TRUE )fit_residuals_sc( object, method = c("sctransform", "analytic_pearson"), group_column = NULL, covariate_columns = NULL, residual_params = NULL, gene_batch_size = NULL, seed = 42L, .verbose = TRUE )
object |
|
method |
String. One of |
group_column |
String or |
covariate_columns |
Character vector or |
residual_params |
List or |
gene_batch_size |
Integer or |
seed |
Integer. Random seed for the step-1 subsample. |
.verbose |
Boolean or Integer. Controls verbosity. |
The object with the fitted model attached.
Choudhary and Satija, Genome Biology, 2022; Lause, Berens and Kobak, Genome Biology, 2021.
# analytic Pearson residuals over every kept cell sc <- demo_single_cells(prepped = FALSE) sc <- fit_residuals_sc(sc, method = "analytic_pearson", .verbose = FALSE) get_residual_fit(sc)$n_groups unlink(sc@dir_data, recursive = TRUE, force = TRUE)# analytic Pearson residuals over every kept cell sc <- demo_single_cells(prepped = FALSE) sc <- fit_residuals_sc(sc, method = "analytic_pearson", .verbose = FALSE) get_residual_fit(sc)$n_groups unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function will update the specified columns in the metadata of an
BulkDge() or BulkCoExp() to be conform with R standard
naming conventions. This is useful to do before running DGE methods as they
expect standardised names.
fix_meta_data_column(object, col_names, ...)fix_meta_data_column(object, col_names, ...)
object |
The underlying object, either |
col_names |
Character vector. The columns to fix. |
... |
Additional arguments to parse to the functions. |
Returns the object with the respective metadata columns updated.
# make a contrast column safe for model.matrix() syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case 1", "control 1"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- fix_meta_data_column(object, "case_control") unique(get_metadata(object)$case_control)# make a contrast column safe for model.matrix() syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case 1", "control 1"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- fix_meta_data_column(object, "case_control") unique(get_metadata(object)$case_control)
Unions new hard flags with existing ones per metric. Set reset = TRUE
to clear existing hard flags first.
flag_cells(x, hard_thresholds, reset = FALSE)flag_cells(x, hard_thresholds, reset = FALSE)
x |
A |
hard_thresholds |
Named list. See |
reset |
Logical. Clear existing hard flags before applying. |
Updated CellQc.
# add a hard mitochondrial cut on top of the MAD flags set.seed(42L) metrics <- list( lib_size = c(rnorm(99, 1000, 100), 50), pct_mt = runif(100, 0, 20) ) qc <- run_cell_qc( metrics, cells_to_keep = 0:99, directions = c(lib_size = "below", pct_mt = "above") ) flag_cells(qc, list(pct_mt = c(upper = 15)))# add a hard mitochondrial cut on top of the MAD flags set.seed(42L) metrics <- list( lib_size = c(rnorm(99, 1000, 100), 50), pct_mt = runif(100, 0, 20) ) qc <- run_cell_qc( metrics, cells_to_keep = 0:99, directions = c(lib_size = "below", pct_mt = "above") ) flag_cells(qc, list(pct_mt = c(upper = 15)))
Wrapper around manifoldsR::forceatlas2() and
manifoldsR::forceatlas2_from_graph() for the SingleCells and MetaCells
classes. ForceAtlas2 is a force-directed graph layout: every edge pulls,
every pair of cells pushes, and gravity keeps disconnected components from
drifting off. It is what scanpy's draw_graph does.
graph picks what gets laid out:
"knn" - The kNN graph (cached if use_knn = TRUE, otherwise built
from the chosen embedding), turned into the UMAP fuzzy union graph first.
This matches scanpy.
"snn" - The sNN graph from find_neighbours_sc(), i.e.
the same graph the Leiden/Louvain clustering runs on. use_knn,
embd_to_use, no_embd_to_use, k, knn_method and nn_params do not
apply here, and neither do the graph and initialisation knobs in
fa2_params. Pass init_embd to start from an existing 2D embedding,
otherwise the layout starts from random positions.
forceatlas2_sc( object, graph = c("knn", "snn"), use_knn = TRUE, embd_to_use = "pca", slot_name = "fa2", no_embd_to_use = NULL, init_embd = NULL, modality = c("rna", "adt", "wnn"), k = 15L, knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"), nn_params = manifoldsR::params_nn(), fa2_params = manifoldsR::params_fa2(), seed = 42L, .verbose = TRUE )forceatlas2_sc( object, graph = c("knn", "snn"), use_knn = TRUE, embd_to_use = "pca", slot_name = "fa2", no_embd_to_use = NULL, init_embd = NULL, modality = c("rna", "adt", "wnn"), k = 15L, knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"), nn_params = manifoldsR::params_nn(), fa2_params = manifoldsR::params_fa2(), seed = 42L, .verbose = TRUE )
object |
|
graph |
String. Which graph to lay out. One of |
use_knn |
Boolean. Use the kNN graph found in the object. Defaults to
|
embd_to_use |
String. The embedding to build the kNN graph from. Must
be available in the object. |
slot_name |
String. The name of this embedding within the object.
Defaults to |
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
init_embd |
Optional string. Name of a stored 2D embedding, e.g.
|
modality |
String. On which modality to run ForceAtlas2. One of
|
k |
Integer. Number of nearest neighbours. Defaults to |
knn_method |
String. Approximate nearest neighbour algorithm. One of
|
nn_params |
Named list. See |
fa2_params |
Named list. See |
seed |
Integer. For reproducibility. |
.verbose |
Boolean. Controls verbosity. |
The object with a "fa2" embedding added.
Jacomy, et al., PLoS ONE, 2014
# ForceAtlas2 on the cached kNN graph, then on the sNN graph sc <- demo_single_cells() sc <- forceatlas2_sc(sc, .verbose = FALSE) sc <- forceatlas2_sc( sc, graph = "snn", slot_name = "fa2_snn", .verbose = FALSE ) dim(get_embedding(sc, "fa2_snn")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# ForceAtlas2 on the cached kNN graph, then on the sNN graph sc <- demo_single_cells() sc <- forceatlas2_sc(sc, .verbose = FALSE) sc <- forceatlas2_sc( sc, graph = "snn", slot_name = "fa2_snn", .verbose = FALSE ) dim(get_embedding(sc, "fa2_snn")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This is a helper function that calculates proportions of reads belonging to given gene sets. This can be used for example for the calculation of percentage mitochondrial reads per cell. These will be automatically added to the obs table
gene_set_proportions_sc( object, gene_set_list, streaming = NULL, .verbose = TRUE )gene_set_proportions_sc( object, gene_set_list, streaming = NULL, .verbose = TRUE )
object |
|
gene_set_list |
A named list with each element containing the gene
identifiers of that set. These should be the same as
|
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
It will add the columns based on the names in the gene_set_list to
the obs table.
# read proportion of a gene set, the mitochondrial percentage pattern sc <- demo_single_cells(prepped = FALSE) sc <- gene_set_proportions_sc( sc, gene_set_list = list(set_a = c("gene_01", "gene_02", "gene_03")), .verbose = FALSE ) head(unlist(sc[["set_a"]])) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# read proportion of a gene set, the mitochondrial percentage pattern sc <- demo_single_cells(prepped = FALSE) sc <- gene_set_proportions_sc( sc, gene_set_list = list(set_a = c("gene_01", "gene_02", "gene_03")), .verbose = FALSE ) head(unlist(sc[["set_a"]])) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This class is used to store the gene ontology information for usage in GSE elimination methods.
GeneOntologyElim(go_data_dt, min_genes)GeneOntologyElim(go_data_dt, min_genes)
go_data_dt |
A data.table that contains the gene ontology information.
This can be extract with for example |
min_genes |
data.frame. Meta-data information in form of a data.frame. |
Returns the class for subsequent usage.
data.table. Contains the gene ontology identifiers and names.
List. Contains the genes within each gene ontology term.
List. Contains the ancestors for each gene ontology term.
List. Which gene ontology terms sit at which level.
Integer, the minimum genes in the gene ontology term to conduct the test.
# human GO restricted to terms with at least 25 genes go_obj <- GeneOntologyElim( get_go_data_human(.verbose = FALSE), min_genes = 25L ) go_obj# human GO restricted to terms with at least 25 genes go_obj <- GeneOntologyElim( get_go_data_human(.verbose = FALSE), min_genes = 25L ) go_obj
MetaCells objectThis function implements the meta cell aggregation from Morabito, et al. The generation of metacells is a useful approach for subsequent application of for example correlation-based methods to identify co-regulated genes.
generate_bt_meta_cells_sc( object, sc_meta_cell_params = params_sc_bt_metacells(), regenerate_knn = FALSE, embd_to_use = "pca", no_embd_to_use = NULL, cells_to_use = NULL, target_size = 1e+05, seed = 42L, .verbose = TRUE )generate_bt_meta_cells_sc( object, sc_meta_cell_params = params_sc_bt_metacells(), regenerate_knn = FALSE, embd_to_use = "pca", no_embd_to_use = NULL, cells_to_use = NULL, target_size = 1e+05, seed = 42L, .verbose = TRUE )
object |
|
sc_meta_cell_params |
List. Output of
|
regenerate_knn |
Boolean. Shall a kNN graph be regenerated. |
embd_to_use |
String. The embedding to use. Only relevant if you set
regenerate_knn to |
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
cells_to_use |
Optional string. Names of the cells to use for the generation of the meta-cells. If provided, this function will regenerate the kNN graph no matter what. |
target_size |
Numeric. The library target size to normalise the meta cells to. |
seed |
Integer. Seed for reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A MetaCells() with the data generated by this meta cell
aggregation method.
Morabito, et al. Cell Rep Methods, 2023
# 50 meta cells off the demo object sc <- demo_single_cells() mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells( target_no_metacells = 50L, knn = list(k = 10L) ), .verbose = FALSE ) mc unlink(sc@dir_data, recursive = TRUE, force = TRUE)# 50 meta cells off the demo object sc <- demo_single_cells() mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells( target_no_metacells = 50L, knn = list(k = 10L) ), .verbose = FALSE ) mc unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function generates synthetic data for CellSweep test purposes. Every
real barcode is a two-component multinomial: a planted fraction alpha of
its library comes from the soup, the rest from its own cell type profile.
Empty droplets are pure soup at a much smaller library size, which is what
the ambient profile is estimated off. Real barcodes come first, the empty
droplets after.
generate_cellsweep_test_data( syn_data_params = params_sc_synthetic_cellsweep(), seed = 42L )generate_cellsweep_test_data( syn_data_params = params_sc_synthetic_cellsweep(), seed = 42L )
syn_data_params |
List. Contains the parameters for the generation of
the synthetic data, see: |
seed |
Integer. The seed for the generation of the synthetic data. |
The empty droplets carry no cell type label, which is exactly what
cellsweep_sc() keys off: barcodes that are neither empty nor
annotated are excluded from the fit. Load the counts with a fully permissive
params_sc_min_quality(), otherwise the ingest deletes the empty
droplets the model trains on.
List with the following items
counts - dgRMatrix with cells x genes.
obs - data.table with cell_id, cell_grp (NA for the empty
droplets), sample_id, is_empty and alpha_true (NA for the empty
droplets).
var - data.table that contains the gene information.
ambient_true - Numeric vector. The soup the empty droplets were drawn from, named by gene and summing to one.
celltype_profiles_true - Numeric matrix of cell types x genes. Each row sums to one.
# a small synthetic experiment with a planted soup data <- generate_cellsweep_test_data( syn_data_params = params_sc_synthetic_cellsweep( n_real = 60L, n_empty = 200L, n_genes = 60L ) ) dim(data$counts) head(data$obs, 3)# a small synthetic experiment with a planted soup data <- generate_cellsweep_test_data( syn_data_params = params_sc_synthetic_cellsweep( n_real = 60L, n_empty = 200L, n_genes = 60L ) ) dim(data$counts) head(data$obs, 3)
Generates synthetic data DIALOGUE should be able to solve. Every cell type gets its own noise and its own sample-level nuisance factors; only the first feature column and the planted genes carry a shared per-sample latent, so anything found beyond that is spurious.
generate_dialogue_test_data( syn_data_params = params_sc_synthetic_dialogue(), seed = 42L )generate_dialogue_test_data( syn_data_params = params_sc_synthetic_dialogue(), seed = 42L )
syn_data_params |
List. Contains the parameters for the generation of
the synthetic data, see: |
seed |
Integer. The seed for the generation of the synthetic data. |
Same generator the Rust integration tests use, so the planted structure cannot drift between the two suites. What differs is the scale: R takes the raw count layer and lets the normal ingestion path log-normalise it, where the Rust tests feed the planted layer straight in.
Cells are laid out contiguously by cell type and, within a cell type, by sample, so every cell type sees every sample. That is the easy case for the method and is what a fixture should be.
List with the following items
counts - dgRMatrix with cells x genes of raw counts.
obs - data.table with cell_id, cell_grp, sample_id and
cell_quality. The last is pure noise, independent of the planted
programme: hand it to dialogue_sc(quality_col = "cell_quality") so the
covariate does not carry the signal you are trying to find.
var - data.table with the gene information.
features - Named list of numeric matrices, one per cell type, with
the cell identifiers as row names. Hand this straight to
dialogue_sc().
latent - Numeric vector. The per-sample latent the planted programme follows, named by sample.
planted - Named list of character vectors. The planted gene identifiers per cell type.
Jerby-Arnon & Regev, Nature Biotechnology, 2022
# synthetic data with a multicellular programme planted across samples data <- generate_dialogue_test_data() names(data$features) head(data$planted[[1]])# synthetic data with a multicellular programme planted across samples data <- generate_dialogue_test_data() names(data$features) head(data$planted[[1]])
Generates an artifical matrix of active modules (for a maximum of 8) in samples being active. Also allows for overlap between the modules. Designed to test some of the algorithms.
generate_gene_module_data( n_samples = 24L, n_genes = 60L, n_modules = 4L, overlap_fraction = 0.25, seed = 10101L )generate_gene_module_data( n_samples = 24L, n_genes = 60L, n_modules = 4L, overlap_fraction = 0.25, seed = 10101L )
n_samples |
Integer. Number of samples. |
n_genes |
Integer. Number of genes. |
n_modules |
Integer. Number of active gene modules. To a maximum of 10. |
overlap_fraction |
Float. How much the same modules can be active in the same samples. |
seed |
Integer. Initial random seed for generation of the synthetic data. Default: 10101L. |
A synthetic_matrix_modules class with the following items:
data - The data matrix.
meta_data - The sample metadata.
# four partially overlapping modules over 24 samples mods <- generate_gene_module_data( n_samples = 24L, n_genes = 60L, n_modules = 4L ) dim(mods$data) head(mods$meta_data)# four partially overlapping modules over 24 samples mods <- generate_gene_module_data( n_samples = 24L, n_genes = 60L, n_modules = 4L ) dim(mods$data) head(mods$meta_data)
Identify hotspot gene clusters
generate_hotspot_membership(x, fdr_threshold = 0.05, min_size = 10L) ## S3 method for class 'Hotspot' generate_hotspot_membership(x, fdr_threshold = 0.05, min_size = 10L)generate_hotspot_membership(x, fdr_threshold = 0.05, min_size = 10L) ## S3 method for class 'Hotspot' generate_hotspot_membership(x, fdr_threshold = 0.05, min_size = 10L)
x |
An object to generate the hotspot gene clusters for. |
fdr_threshold |
Numeric. The maximum FDR for a given gene-gene local correlation to be included. |
min_size |
Integer. Minimum cluster size. |
# cluster the local gene-gene correlations into modules sc <- demo_single_cells() hs <- hotspot_gene_cor_sc(sc, .verbose = FALSE) generate_hotspot_membership(hs) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# cluster the local gene-gene correlations into modules sc <- demo_single_cells() hs <- hotspot_gene_cor_sc(sc, .verbose = FALSE) generate_hotspot_membership(hs) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
SingleCellNearestNeighbour from a single cell classThis function will generate the kNNs based on a given embedding. Available algorithms are:
kmknn - An exact kNN search that leverages k-means clustering under
the hood to prune out data points. The default setting.
exhaustive - An exhaustive, flat index. On smaller data sets often
faster than the approximate nearest neighbour search algorithms.
hnsw - Hierarchical Navigable Small World. A graph-based
approximate nearest neighbour search algorithm; works well on large data
sets. A benign race condition is leveraged during index build, making the
build non-deterministic. Bigger impact on smaller data sets.
nndescent - Nearest neighbour descent. Leverages concepts from
PyNNDescent and works well on very large data sets similar to hnsw.
Set extract_knn = TRUE in the kNN parameters to hand back the descent
graph directly instead of beam searching it. That drops the query pass
altogether, so it is much faster, but recall goes down a little.
ivf - Inverted file index. Uses first k-means clustering to
identify Voronoi cells and leverages these during querying. Works well
on large data sets with high dimensionality and when you need to return
large number of neighbours.
annoy - Approximate nearest neighbours Oh Yeah. Tree-based index,
used across different R single cell packages (Seurat, SCE). This version
is purely memory-based.
generate_knn_sc( object, embd_to_use = "pca", cells_to_use = NULL, no_embd_to_use = NULL, neighbours_params = params_sc_neighbours(), seed = 42L, .validate_index = TRUE, .verbose = TRUE )generate_knn_sc( object, embd_to_use = "pca", cells_to_use = NULL, no_embd_to_use = NULL, neighbours_params = params_sc_neighbours(), seed = 42L, .validate_index = TRUE, .verbose = TRUE )
object |
|
embd_to_use |
String. The embedding to use. Whichever you chose, it needs to be part of the object. |
cells_to_use |
String. Optional cell names to include in the generation
of the kNN graph. If |
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
neighbours_params |
List. Output of
|
seed |
Integer. For reproducibility. |
.validate_index |
Boolean. Shall an exhaustive search against a subset of cells be run to validate the approximate nearest neighbour index. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
Initialised sc_knn with the kNN data.
# a standalone kNN object off the PCA embedding sc <- demo_single_cells() knn <- generate_knn_sc(sc, .validate_index = FALSE, .verbose = FALSE) dim(get_knn_mat(knn)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# a standalone kNN object off the PCA embedding sc <- demo_single_cells() knn <- generate_knn_sc(sc, .validate_index = FALSE, .verbose = FALSE) dim(get_knn_mat(knn)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Computes the NicheNet-style ligand to target gene influence matrix. Builds
the gene universe as the union of all symbols across both networks, remaps to
0-indexed integer node IDs for the Rust side, and wraps the resulting matrix
in a LigandTargetInfluence object.
generate_ligand_target_influence( ligand_seeds, ppi_network, grn_network, params = params_ligand_target() )generate_ligand_target_influence( ligand_seeds, ppi_network, grn_network, params = params_ligand_target() )
ligand_seeds |
List of character vectors. Each element is one
ligand symbol or a group treated as one complex (e.g.
|
ppi_network |
data.table with columns |
grn_network |
data.table with columns |
params |
List. As returned by |
A LigandTargetInfluence object.
# two disjoint signalling components over a toy network ppi <- data.table::data.table( from = c("L1", "SIG1", "L2", "SIG2"), to = c("SIG1", "TF1", "SIG2", "TF2"), weight = 1.0 ) grn <- data.table::data.table( from = rep(c("TF1", "TF2"), each = 3), to = c("G1", "G2", "G3", "G4", "G5", "G6"), weight = 1.0 ) inf <- generate_ligand_target_influence( ligand_seeds = list(L1 = "L1", L2 = "L2"), ppi_network = ppi, grn_network = grn, params = params_ligand_target(ltf_cutoff = 0) ) inf# two disjoint signalling components over a toy network ppi <- data.table::data.table( from = c("L1", "SIG1", "L2", "SIG2"), to = c("SIG1", "TF1", "SIG2", "TF2"), weight = 1.0 ) grn <- data.table::data.table( from = rep(c("TF1", "TF2"), each = 3), to = c("G1", "G2", "G3", "G4", "G5", "G6"), weight = 1.0 ) inf <- generate_ligand_target_influence( ligand_seeds = list(L1 = "L1", L2 = "L2"), ppi_network = ppi, grn_network = grn, params = params_ligand_target(ltf_cutoff = 0) ) inf
Helper function to create personalisation vectors
generate_personalisation_vec(graph, node_weights)generate_personalisation_vec(graph, node_weights)
graph |
igraph. The graph for which to produce the personalisation vector. |
node_weights |
Named numeric. The names represent the nodes and the values the strength of the reset. |
The personalisation vector for subsequent usage in page-rank
# reset weight split across two seed nodes, normalised to sum to one g <- igraph::graph_from_data_frame( data.frame(from = c("a", "b", "c"), to = c("b", "c", "d")), directed = TRUE ) generate_personalisation_vec(g, node_weights = c(a = 3, c = 1))# reset weight split across two seed nodes, normalised to sum to one g <- igraph::graph_from_data_frame( data.frame(from = c("a", "b", "c"), to = c("b", "c", "d")), directed = TRUE ) generate_personalisation_vec(g, node_weights = c(a = 3, c = 1))
This function will generate an RBH graph based on set similarity between gene modules. You have the option to use an overlap coefficient instead of Jaccard similarity and to specify a minimum similarity.
generate_rbh_graph( object, minimum_similarity, k_best = 1L, overlap_coefficient = FALSE, spearman = FALSE )generate_rbh_graph( object, minimum_similarity, k_best = 1L, overlap_coefficient = FALSE, spearman = FALSE )
object |
The underlying class, see |
minimum_similarity |
The minimum similarity to create an edge. |
k_best |
Integer. Number of best neighbours to consider. If set to
|
overlap_coefficient |
Boolean. Shall the overlap coefficient be used instead of Jaccard similarity. Only relevant if the underlying class is set to set similarity. |
spearman |
Boolean. Shall Spearman correlation be used. Only relevant if the underlying class is set to correlation-based similarity. |
The class with added properties.
# Jaccard-based reciprocal best hits between two module sets set.seed(123) modules <- data.table::data.table( origin = rep(c("set_a", "set_b"), each = 20), module = rep(c("m1", "m2", "m3", "m4"), each = 10), gene = unlist(replicate(4, sample(letters, 10), simplify = FALSE)) ) object <- RbhGraph( modules, rbh_type = "set", dataset_col = "origin", module_col = "module", value_col = "gene" ) object <- generate_rbh_graph(object, minimum_similarity = 0) head(get_rbh_res(object))# Jaccard-based reciprocal best hits between two module sets set.seed(123) modules <- data.table::data.table( origin = rep(c("set_a", "set_b"), each = 20), module = rep(c("m1", "m2", "m3", "m4"), each = 10), gene = unlist(replicate(4, sample(letters, 10), simplify = FALSE)) ) object <- RbhGraph( modules, rbh_type = "set", dataset_col = "origin", module_col = "module", value_col = "gene" ) object <- generate_rbh_graph(object, minimum_similarity = 0) head(get_rbh_res(object))
Generate a new SingleCellNearestNeighbour from data
generate_sc_knn( data, neighbours_params = params_sc_neighbours(), seed = 42L, .validate_index = FALSE, .verbose = TRUE )generate_sc_knn( data, neighbours_params = params_sc_neighbours(), seed = 42L, .validate_index = FALSE, .verbose = TRUE )
data |
Numerical matrix. Samplex x features. The embedding matrix from which to generate the kNN data. |
neighbours_params |
List. Output of
|
seed |
Integer. Random seed for reproducibility. |
.validate_index |
Boolean. Shall an exhaustive search against a subset of cells be run to validate the approximate nearest neighbour index. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The SingleCellNearestNeighbour for downstream usage.
# kNN over a random embedding; the rows have to carry cell names set.seed(42L) embd <- matrix(rnorm(500 * 10), nrow = 500) rownames(embd) <- sprintf("cell_%03d", 1:500) knn <- generate_sc_knn(embd, .verbose = FALSE) dim(get_knn_mat(knn))# kNN over a random embedding; the rows have to carry cell names set.seed(42L) embd <- matrix(rnorm(500 * 10), nrow = 500) rownames(embd) <- sprintf("cell_%03d", 1:500) knn <- generate_sc_knn(embd, .verbose = FALSE) dim(get_knn_mat(knn))
MetaCells objectThis function implements the meta cell aggregation from Persad et al., and returns the resulting SEACells. Compared to other algorithms, a kernel archetype analysis is used to identify the metacells. For details, please refer to the publication.
generate_seacells_sc( object, seacell_params = params_sc_seacells(), embd_to_use = "pca", no_embd_to_use = NULL, cells_to_use = NULL, regenerate_knn = FALSE, target_size = 1e+05, seed = 42L, .verbose = TRUE )generate_seacells_sc( object, seacell_params = params_sc_seacells(), embd_to_use = "pca", no_embd_to_use = NULL, cells_to_use = NULL, regenerate_knn = FALSE, target_size = 1e+05, seed = 42L, .verbose = TRUE )
object |
|
seacell_params |
List. Output of
|
embd_to_use |
String. The embedding to use. Atm, the only option is
|
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
cells_to_use |
Optional string. Names of the cells to use for the generation of the SEACells. |
regenerate_knn |
Boolean. Shall a kNN graph be regenerated. If not, the internal one will be used. |
target_size |
Numeric. The library target size to normalise the meta cells to. |
seed |
Integer. Seed for reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A MetaCells() with the data generated by this meta cell
aggregation method.
Persad, et al. Nat Biotechnol, 2023
# archetypal analysis based meta cells sc <- demo_single_cells() mc <- generate_seacells_sc( sc, seacell_params = params_sc_seacells( n_sea_cells = 50L, min_iter = 5L, knn = list(k = 10L) ), .verbose = FALSE ) mc unlink(sc@dir_data, recursive = TRUE, force = TRUE)# archetypal analysis based meta cells sc <- demo_single_cells() mc <- generate_seacells_sc( sc, seacell_params = params_sc_seacells( n_sea_cells = 50L, min_iter = 5L, knn = list(k = 10L) ), .verbose = FALSE ) mc unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function generates synthetic data for single cell test purposes. These data can be used for testing functionality of various single cell functions.
generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(), seed = 42L )generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(), seed = 42L )
syn_data_params |
List. Contains the parameters for the generation of
synthetic data, see:
|
seed |
Integer. The seed for the generation of the seed data. |
List with the following items
counts - dgRMatrix with cells x genes.
obs - data.table that contains the cell information.
var - data.table that contains the var information.
# a small synthetic experiment with three planted cell types data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) dim(data$counts) head(data$obs, 3)# a small synthetic experiment with three planted cell types data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) dim(data$counts) head(data$obs, 3)
MetaCells objectThis function implements the meta cell aggregation from Bilous, et al. The core idea is to use the walktrap community detection on the kNN graph. For details, please refer to the paper.
generate_supercells_sc( object, sc_supercell_params = params_sc_supercell(), regenerate_knn = FALSE, embd_to_use = "pca", no_embd_to_use = NULL, cells_to_use = NULL, target_size = 1e+05, seed = 42L, .verbose = TRUE )generate_supercells_sc( object, sc_supercell_params = params_sc_supercell(), regenerate_knn = FALSE, embd_to_use = "pca", no_embd_to_use = NULL, cells_to_use = NULL, target_size = 1e+05, seed = 42L, .verbose = TRUE )
object |
|
sc_supercell_params |
List. Output of
|
regenerate_knn |
Boolean. Shall a kNN graph be regenerated. |
embd_to_use |
String. The embedding to use. Only relevant if you set
regenerate_knn to |
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
cells_to_use |
Optional string. Names of the cells to use for the generation of the meta-cells. If provided, this function will regenerate the kNN graph no matter what. |
target_size |
Numeric. The library target size to normalise the meta cells to. |
seed |
Integer. Seed for reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A MetaCells() with the data generated by this meta cell
aggregation method.
Bilous, et al. BMC Bioinform., 2022
# walktrap based super cells at a graining factor of 10 sc <- demo_single_cells() mc <- generate_supercells_sc( sc, sc_supercell_params = params_sc_supercell( graining_factor = 10, knn = list(k = 10L) ), regenerate_knn = TRUE, .verbose = FALSE ) mc unlink(sc@dir_data, recursive = TRUE, force = TRUE)# walktrap based super cells at a graining factor of 10 sc <- demo_single_cells() mc <- generate_supercells_sc( sc, sc_supercell_params = params_sc_supercell( graining_factor = 10, knn = list(k = 10L) ), regenerate_knn = TRUE, .verbose = FALSE ) mc unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function implements the approach from Hao et al., to generate a weighted nearest neighbour graph given two 'omics modalities (for example RNA and ADT). Per-cell modality weights are computed by comparing within- and cross-modality neighbourhood distances, and these weights are used to fuse the two modalities into a single multimodal kNN graph.
The per-modality kNN graphs are always recomputed internally from the chosen
embeddings at knn_range neighbours (see params_sc_wnn()); the kNN graphs
previously stored on the object via find_neighbours_sc() are not
reused, as WNN requires a larger candidate pool than the default neighbour
search.
The result is stored in the object's other_data under "wnn", containing
the fused kNN graph as a SingleCellNearestNeighbour (with a kernelised
pseudo-distance metric), the sNN graph as an igraph and a table of per-cell
modality weights. This graph can subsequently be used for clustering and 2D
embedding via the "wnn" modality.
generate_wnn_graph_sc( object, modality_1 = "rna", modality_2 = "adt", embd_to_use_1 = "pca", embd_to_use_2 = "pca", no_embd_to_use_1 = NULL, no_embd_to_use_2 = NULL, wnn_params = params_sc_wnn(), full_snn = TRUE, pruning = 1/15, snn_similarity = "jaccard", seed = 42L, .verbose = TRUE )generate_wnn_graph_sc( object, modality_1 = "rna", modality_2 = "adt", embd_to_use_1 = "pca", embd_to_use_2 = "pca", no_embd_to_use_1 = NULL, no_embd_to_use_2 = NULL, wnn_params = params_sc_wnn(), full_snn = TRUE, pruning = 1/15, snn_similarity = "jaccard", seed = 42L, .verbose = TRUE )
object |
|
modality_1 |
String. First modality. Defaults to |
modality_2 |
String. Second modality. Defaults to |
embd_to_use_1 |
String. The embedding to use for the first modality.
Must be available in the object for |
embd_to_use_2 |
String. The embedding to use for the second modality.
Must be available in the object for |
no_embd_to_use_1 |
Optional integer. Number of embedding dimensions to
use for |
no_embd_to_use_2 |
Optional integer. Number of embedding dimensions to
use for |
wnn_params |
Named list. Controls the parameters for the WNN generation,
see |
full_snn |
Boolean. Shall the full shared nearest neighbour graph be generated that generates edges between all cells instead of between only neighbours. |
pruning |
Numeric. Weights below this threshold will be set to 0 in the generation of the sNN graph. Seurat uses for example 1/15 with k = 20. |
snn_similarity |
String. One of |
seed |
Integer. For reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The SingleCellsMultiModal object with the WNN graph and per-cell
modality weights added to other_data[["wnn"]].
Hao et al., Cell, 2021
# a fused RNA + ADT graph, both modalities reduced with PCA first rna <- generate_single_cell_test_data() adt <- generate_single_cell_test_data_adt() dir <- tempfile("bixverse_mm") dir.create(dir) object <- load_r_data( SingleCellsMultiModal(dir_data = dir), counts = rna$counts, obs = rna$obs, var = rna$var, sc_qc_param = params_sc_min_quality(min_unique_genes = 5L), .verbose = FALSE ) object <- add_adt_counts_sc(object, adt_counts = adt$counts, method = "clr") object <- find_hvg_sc(object, hvg_no = 30L, .verbose = FALSE) object <- calculate_pca_sc(object, no_pcs = 15L, .verbose = FALSE) object <- calculate_pca_adt_sc(object, no_pcs = 10L) object <- generate_wnn_graph_sc(object, .verbose = FALSE) get_snn_graph(object, modality = "wnn") unlink(dir, recursive = TRUE, force = TRUE)# a fused RNA + ADT graph, both modalities reduced with PCA first rna <- generate_single_cell_test_data() adt <- generate_single_cell_test_data_adt() dir <- tempfile("bixverse_mm") dir.create(dir) object <- load_r_data( SingleCellsMultiModal(dir_data = dir), counts = rna$counts, obs = rna$obs, var = rna$var, sc_qc_param = params_sc_min_quality(min_unique_genes = 5L), .verbose = FALSE ) object <- add_adt_counts_sc(object, adt_counts = adt$counts, method = "clr") object <- find_hvg_sc(object, hvg_no = 30L, .verbose = FALSE) object <- calculate_pca_sc(object, no_pcs = 15L, .verbose = FALSE) object <- calculate_pca_adt_sc(object, no_pcs = 10L) object <- generate_wnn_graph_sc(object, .verbose = FALSE) get_snn_graph(object, modality = "wnn") unlink(dir, recursive = TRUE, force = TRUE)
Returns the number of cells expressing a given ADT
get_adt_feature_info(x) ## S3 method for class 'ADTCounts' get_adt_feature_info(x)get_adt_feature_info(x) ## S3 method for class 'ADTCounts' get_adt_feature_info(x)
x |
The object from which to get the ADT feature information |
A data.table with the ADT feature information
# number of cells expressing each protein adt <- generate_single_cell_test_data_adt() cell_info <- stats::setNames( seq_len(nrow(adt$counts)), rownames(adt$counts) ) adt_clr <- new_adt_counts_clr(adt$counts, cell_info = cell_info) head(get_adt_feature_info(adt_clr))# number of cells expressing each protein adt <- generate_single_cell_test_data_adt() cell_info <- stats::setNames( seq_len(nrow(adt$counts)), rownames(adt$counts) ) adt_clr <- new_adt_counts_clr(adt$counts, cell_info = cell_info) head(get_adt_feature_info(adt_clr))
Get the main ADT feature names
get_adt_names(x) ## S3 method for class 'ADTCounts' get_adt_names(x)get_adt_names(x) ## S3 method for class 'ADTCounts' get_adt_names(x)
x |
An object to get the gene names from. |
The primary ADT feature identifiers stored in the class.
# protein identifiers held by an `ADTCounts` adt <- generate_single_cell_test_data_adt() cell_info <- stats::setNames( seq_len(nrow(adt$counts)), rownames(adt$counts) ) adt_clr <- new_adt_counts_clr(adt$counts, cell_info = cell_info) head(get_adt_names(adt_clr))# protein identifiers held by an `ADTCounts` adt <- generate_single_cell_test_data_adt() cell_info <- stats::setNames( seq_len(nrow(adt$counts)), rownames(adt$counts) ) adt_clr <- new_adt_counts_clr(adt$counts, cell_info = cell_info) head(get_adt_names(adt_clr))
Returns sample information in terms of ADT capture, number of features, etc.
get_adt_sample_info(x) ## S3 method for class 'ADTCounts' get_adt_sample_info(x)get_adt_sample_info(x) ## S3 method for class 'ADTCounts' get_adt_sample_info(x)
x |
The object from which to get the ADT sample information |
A data.table with the ADT sample information
# per-cell ADT capture: non-zero proteins and library size adt <- generate_single_cell_test_data_adt() cell_info <- stats::setNames( seq_len(nrow(adt$counts)), rownames(adt$counts) ) adt_clr <- new_adt_counts_clr(adt$counts, cell_info = cell_info) head(get_adt_sample_info(adt_clr))# per-cell ADT capture: non-zero proteins and library size adt <- generate_single_cell_test_data_adt() cell_info <- stats::setNames( seq_len(nrow(adt$counts)), rownames(adt$counts) ) adt_clr <- new_adt_counts_clr(adt$counts, cell_info = cell_info) head(get_adt_sample_info(adt_clr))
Returns the available embedding as names from the class. This function is used for the single cell-related classes and methods.
get_available_embeddings(x, ...) ## S7 method for class <bixverse::MetaCells> get_available_embeddings(x, ...) ## S3 method for class 'ScCache' get_available_embeddings(x, ...) ## S7 method for class <bixverse::SingleCells> get_available_embeddings(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_available_embeddings(x, ...)get_available_embeddings(x, ...) ## S7 method for class <bixverse::MetaCells> get_available_embeddings(x, ...) ## S3 method for class 'ScCache' get_available_embeddings(x, ...) ## S7 method for class <bixverse::SingleCells> get_available_embeddings(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_available_embeddings(x, ...)
x |
An object to get embedding from |
... |
Other parameters. |
Get the names of the available embeddings.
# what is in the cache to plot against sc <- demo_single_cells() get_available_embeddings(sc) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# what is in the cache to plot against sc <- demo_single_cells() get_available_embeddings(sc) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Returns the fit at best_k, or at a k you name, without refitting. The
sweep keeps every model it fitted.
get_best_model(x, k = NULL) ## S3 method for class 'LdaKSweepResult' get_best_model(x, k = NULL)get_best_model(x, k = NULL) ## S3 method for class 'LdaKSweepResult' get_best_model(x, k = NULL)
x |
|
k |
Optional integer. The topic count to extract. If |
best_k is never below five, see lda_k_sweep(). Pass k
explicitly if the raw metrics point somewhere the selection could not go.
An LdaResult.
# pull the selected fit out of a sweep without refitting set.seed(42L) corpus <- matrix(rbinom(200L * 40L, 1L, 0.05), nrow = 200L, ncol = 40L) corpus[1:100, 1:10] <- rbinom(1000L, 1L, 0.6) corpus[101:200, 11:20] <- rbinom(1000L, 1L, 0.6) colnames(corpus) <- sprintf("term_%02d", 1:40) sweep_res <- lda_k_sweep(corpus > 0, k_range = 5:7, .verbose = FALSE) dim(get_best_model(sweep_res))# pull the selected fit out of a sweep without refitting set.seed(42L) corpus <- matrix(rbinom(200L * 40L, 1L, 0.05), nrow = 200L, ncol = 40L) corpus[1:100, 1:10] <- rbinom(1000L, 1L, 0.6) corpus[101:200, 11:20] <- rbinom(1000L, 1L, 0.6) colnames(corpus) <- sprintf("term_%02d", 1:40) sweep_res <- lda_k_sweep(corpus > 0, k_range = 5:7, .verbose = FALSE) dim(get_best_model(sweep_res))
Extracts the run with the lowest reconstruction loss and returns it as a
single-run NmfResult for downstream methods.
get_best_run(x) ## S3 method for class 'StabilisedNmfResult' get_best_run(x)get_best_run(x) ## S3 method for class 'StabilisedNmfResult' get_best_run(x)
x |
|
An NmfResult containing the W/H of the best run.
# the restart with the lowest reconstruction loss sc <- demo_single_cells() res <- stabilised_nmf_sc(sc, k = 5L, n_runs = 5L, .verbose = FALSE) get_best_run(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the restart with the lowest reconstruction loss sc <- demo_single_cells() res <- stabilised_nmf_sc(sc, k = 5L, n_runs = 5L, .verbose = FALSE) get_best_run(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter function for the feature factors of the contrastive PCA
get_c_pca_factors(object)get_c_pca_factors(object)
object |
The underlying class, see |
The sample scores of the contrastive PCA run. If not found, returns a warning and NULL.
# sample scores of the contrastive components cpca_data <- synthetic_c_pca_data() target <- t(cpca_data$target) background <- t(cpca_data$background) meta <- data.table::data.table(sample_id = rownames(target)) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, .verbose = FALSE) obj <- contrastive_pca_processing(obj, background, .verbose = FALSE) obj <- contrastive_pca(obj, alpha = 2.5, no_pcs = 5L) head(get_c_pca_factors(obj)[, 1:3])# sample scores of the contrastive components cpca_data <- synthetic_c_pca_data() target <- t(cpca_data$target) background <- t(cpca_data$background) meta <- data.table::data.table(sample_id = rownames(target)) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, .verbose = FALSE) obj <- contrastive_pca_processing(obj, background, .verbose = FALSE) obj <- contrastive_pca(obj, alpha = 2.5, no_pcs = 5L) head(get_c_pca_factors(obj)[, 1:3])
Getter function for the feature loadings of the contrastive PCA
get_c_pca_loadings(object)get_c_pca_loadings(object)
object |
The underlying class, see |
The feature loadings of the contrastive PCA run. If not found, returns a warning and NULL.
# feature loadings of the contrastive components cpca_data <- synthetic_c_pca_data() target <- t(cpca_data$target) background <- t(cpca_data$background) meta <- data.table::data.table(sample_id = rownames(target)) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, .verbose = FALSE) obj <- contrastive_pca_processing(obj, background, .verbose = FALSE) obj <- contrastive_pca(obj, alpha = 2.5, no_pcs = 5L) head(get_c_pca_loadings(obj)[, 1:3])# feature loadings of the contrastive components cpca_data <- synthetic_c_pca_data() target <- t(cpca_data$target) background <- t(cpca_data$background) meta <- data.table::data.table(sample_id = rownames(target)) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, .verbose = FALSE) obj <- contrastive_pca_processing(obj, background, .verbose = FALSE) obj <- contrastive_pca(obj, alpha = 2.5, no_pcs = 5L) head(get_c_pca_loadings(obj)[, 1:3])
Returns the index for a given gene based on the internal gene mapping. This is used for the single cell-related classes and methods.
get_cell_indices(x, cell_ids, rust_index) ## S7 method for class <bixverse::MetaCells> get_cell_indices(x, cell_ids, rust_index) ## S3 method for class 'ScMap' get_cell_indices(x, cell_ids, rust_index) ## S7 method for class <bixverse::SingleCells> get_cell_indices(x, cell_ids, rust_index) ## S7 method for class <bixverse::SingleCellsSubset> get_cell_indices(x, cell_ids, rust_index)get_cell_indices(x, cell_ids, rust_index) ## S7 method for class <bixverse::MetaCells> get_cell_indices(x, cell_ids, rust_index) ## S3 method for class 'ScMap' get_cell_indices(x, cell_ids, rust_index) ## S7 method for class <bixverse::SingleCells> get_cell_indices(x, cell_ids, rust_index) ## S7 method for class <bixverse::SingleCellsSubset> get_cell_indices(x, cell_ids, rust_index)
x |
An object to get the gene index from. |
cell_ids |
String vector. The cell ids to search for. |
rust_index |
Bool. Shall rust-based indexing be returned. |
The indices of the cells
# R-based positions of two cells sc <- demo_single_cells(prepped = FALSE) get_cell_indices(sc, c("cell_001", "cell_002"), rust_index = FALSE) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# R-based positions of two cells sc <- demo_single_cells(prepped = FALSE) get_cell_indices(sc, c("cell_001", "cell_002"), rust_index = FALSE) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Returns the cell indices (R-based) and the cell names (usually barcodes) from the object for further downstream usage.
get_cell_info(x, filtered = TRUE) ## S3 method for class 'ScMap' get_cell_info(x, filtered = TRUE) ## S7 method for class <bixverse::SingleCells> get_cell_info(x, filtered = TRUE)get_cell_info(x, filtered = TRUE) ## S3 method for class 'ScMap' get_cell_info(x, filtered = TRUE) ## S7 method for class <bixverse::SingleCells> get_cell_info(x, filtered = TRUE)
x |
An object to get the cell info from |
filtered |
Boolean. If |
A named vector with elements -> cell_idx, names -> cell_names.
# cell indices carrying the barcodes as names sc <- demo_single_cells(prepped = FALSE) head(get_cell_info(sc), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# cell indices carrying the barcodes as names sc <- demo_single_cells(prepped = FALSE) head(get_cell_info(sc), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Returns the cell names (usually barcodes).
get_cell_names(x, filtered = FALSE) ## S3 method for class 'ScMap' get_cell_names(x, filtered = FALSE) ## S7 method for class <bixverse::SingleCells> get_cell_names(x, filtered = FALSE) ## S7 method for class <bixverse::SingleCellsSubset> get_cell_names(x, filtered = FALSE)get_cell_names(x, filtered = FALSE) ## S3 method for class 'ScMap' get_cell_names(x, filtered = FALSE) ## S7 method for class <bixverse::SingleCells> get_cell_names(x, filtered = FALSE) ## S7 method for class <bixverse::SingleCellsSubset> get_cell_names(x, filtered = FALSE)
x |
An object to get the cell names from. |
filtered |
Boolean. Shall, if found only the cell names of the
|
The cell names (barcodes)
# barcodes of the cells that passed quality control sc <- demo_single_cells(prepped = FALSE) head(get_cell_names(sc, filtered = TRUE), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# barcodes of the cells that passed quality control sc <- demo_single_cells(prepped = FALSE) head(get_cell_names(sc, filtered = TRUE), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Resolves the three files a Cell Ranger MTX directory holds and wraps them
into the parameter list load_mtx() wants. Handles the v2 naming
(genes.tsv) and the v3 one (features.tsv), and the .csv variant that
write_cellranger_output() can emit.
get_cell_ranger_params(dir_data, cells_as_rows = FALSE, has_hdr = FALSE)get_cell_ranger_params(dir_data, cells_as_rows = FALSE, has_hdr = FALSE)
dir_data |
String. The directory with the Cell Ranger outputs |
cells_as_rows |
Boolean. Are the cells the rows of the matrix? Cell
Ranger writes genes x cells, so this defaults to |
has_hdr |
Boolean. Do the barcode and feature files carry a header row?
Cell Ranger writes none, so this defaults to |
A list based on params_sc_mtx_io().
# round trip through the package's own writer dir <- tempfile("cellranger") dir.create(dir) data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 50L, n_genes = 40L) ) write_cellranger_output( f_path = dir, counts = data$counts, obs = data$obs, var = data$var, rows = "genes", format_type = "tsv", .verbose = FALSE ) str(get_cell_ranger_params(dir, has_hdr = TRUE)) unlink(dir, recursive = TRUE, force = TRUE)# round trip through the package's own writer dir <- tempfile("cellranger") dir.create(dir) data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 50L, n_genes = 40L) ) write_cellranger_output( f_path = dir, counts = data$counts, obs = data$obs, var = data$var, rows = "genes", format_type = "tsv", .verbose = FALSE ) str(get_cell_ranger_params(dir, has_hdr = TRUE)) unlink(dir, recursive = TRUE, force = TRUE)
Fetches the CellPhoneDB v5.0.0 database from the
cellphonedb-data repository
and flattens it to one row per interaction. Each partner is resolved to
the gene symbols of its subunits: one gene for a single protein, several
for a complex. The download is cached in dir.
get_cellphonedb_db(dir = tempdir(), .verbose = TRUE)get_cellphonedb_db(dir = tempdir(), .verbose = TRUE)
dir |
String. Directory to store and look for |
.verbose |
Boolean. Controls verbosity of the download. |
A data.table with one row per interaction and the columns:
interaction_id - The CellPhoneDB interaction identifier.
partner_a, partner_b - The partner names: the gene symbol for a single protein, the complex name otherwise.
genes_a, genes_b - List columns with the gene symbols of the subunits.
is_complex_a, is_complex_b - Is the partner a complex.
directionality - E.g. "Ligand-Receptor".
classification - The signalling classification.
Troulé et al., Nat Protoc, 2025.
Returns the indices of the cells that survived quality control. These are stored 0-indexed for Rust, so add one before using them in R.
get_cells_to_keep(x) ## S3 method for class 'ScMap' get_cells_to_keep(x) ## S7 method for class <bixverse::SingleCells> get_cells_to_keep(x) ## S7 method for class <bixverse::SingleCellsSubset> get_cells_to_keep(x)get_cells_to_keep(x) ## S3 method for class 'ScMap' get_cells_to_keep(x) ## S7 method for class <bixverse::SingleCells> get_cells_to_keep(x) ## S7 method for class <bixverse::SingleCellsSubset> get_cells_to_keep(x)
x |
An object from which to get the cells to keep from. These are 0-indexed. |
Integer vector with 0-indices of the cells to keep.
# 0-based for Rust, so add one before indexing in R sc <- demo_single_cells(prepped = FALSE) head(get_cells_to_keep(sc) + 1, 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# 0-based for Rust, so add one before indexing in R sc <- demo_single_cells(prepped = FALSE) head(get_cells_to_keep(sc) + 1, 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Get k-means centroids from a fast cluster result
get_centroids_sc(x) ## S3 method for class 'SingleCellFastClusters' get_centroids_sc(x)get_centroids_sc(x) ## S3 method for class 'SingleCellFastClusters' get_centroids_sc(x)
x |
|
# the k-means centroids the graph clustering was built on sc <- demo_single_cells() res <- fast_cluster_sc( sc, resolutions = 1.0, n_centroids = 30L, return_kmeans = TRUE, .verbose = FALSE ) dim(get_centroids_sc(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the k-means centroids the graph clustering was built on sc <- demo_single_cells() res <- fast_cluster_sc( sc, resolutions = 1.0, n_centroids = 30L, return_kmeans = TRUE, .verbose = FALSE ) dim(get_centroids_sc(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Extract the TF to gene data from the ScenicGrn object
get_cistarget_res(x) ## S3 method for class 'ScenicGrn' get_cistarget_res(x)get_cistarget_res(x) ## S3 method for class 'ScenicGrn' get_cistarget_res(x)
x |
|
data.table with TF to gene information
# CisTarget against a synthetic ranking database sc <- demo_single_cells() tfs <- sprintf("gene_%02d", 1:5) grn <- scenic_grn_sc( sc, tf_ids = tfs, scenic_params = params_scenic( min_counts = 1L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) grn <- identify_tf_to_genes( grn, method = "top_k", k_tfs = 3L, .verbose = FALSE ) genes <- get_gene_names(sc) targets <- split(get_tf_to_gene(grn)$gene, get_tf_to_gene(grn)$tf) rankings <- matrix( vapply( tfs, function(tf) { as.integer(rank(!(genes %in% targets[[tf]]), ties.method = "first")) }, integer(length(genes)) ), nrow = length(genes), dimnames = list(genes, sprintf("motif_%s", tfs)) ) annot <- data.table::data.table( motif = sprintf("motif_%s", tfs), TF = tfs, annotationSource = factor( c(rep("directAnnotation", 4), "inferredBy_MotifSimilarity") ) ) grn <- tf_to_genes_motif_enrichment( grn, motif_rankings = rankings, annot_data = annot, cis_target_params = params_cistarget( auc_threshold = 1, nes_threshold = 1, high_conf_cats = "directAnnotation", low_conf_cats = "inferredBy_MotifSimilarity" ), .verbose = FALSE ) get_cistarget_res(grn)[, c("gs_name", "motif", "nes")] unlink(sc@dir_data, recursive = TRUE, force = TRUE)# CisTarget against a synthetic ranking database sc <- demo_single_cells() tfs <- sprintf("gene_%02d", 1:5) grn <- scenic_grn_sc( sc, tf_ids = tfs, scenic_params = params_scenic( min_counts = 1L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) grn <- identify_tf_to_genes( grn, method = "top_k", k_tfs = 3L, .verbose = FALSE ) genes <- get_gene_names(sc) targets <- split(get_tf_to_gene(grn)$gene, get_tf_to_gene(grn)$tf) rankings <- matrix( vapply( tfs, function(tf) { as.integer(rank(!(genes %in% targets[[tf]]), ties.method = "first")) }, integer(length(genes)) ), nrow = length(genes), dimnames = list(genes, sprintf("motif_%s", tfs)) ) annot <- data.table::data.table( motif = sprintf("motif_%s", tfs), TF = tfs, annotationSource = factor( c(rep("directAnnotation", 4), "inferredBy_MotifSimilarity") ) ) grn <- tf_to_genes_motif_enrichment( grn, motif_rankings = rankings, annot_data = annot, cis_target_params = params_cistarget( auc_threshold = 1, nes_threshold = 1, high_conf_cats = "directAnnotation", low_conf_cats = "inferredBy_MotifSimilarity" ), .verbose = FALSE ) get_cistarget_res(grn)[, c("gs_name", "motif", "nes")] unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Helper function to get a correlation-based igraph from the class
get_cor_graph(object, epsilon, .verbose)get_cor_graph(object, epsilon, .verbose)
object |
The class, see |
epsilon |
Float. The epsilon parameter for the RBF function, in this case the bump function. |
.verbose |
Boolean. Controls verbosity of the function. |
A list with the following elements:
graph - The igraph
params - A list that contains the parameters of the graph generation and general graph information (node, edge numbers).
# igraph from the stored correlations at a fixed epsilon mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) graph_res <- get_cor_graph(obj, epsilon = 2, .verbose = FALSE) graph_res$params# igraph from the stored correlations at a fixed epsilon mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) graph_res <- get_cor_graph(obj, epsilon = 2, .verbose = FALSE) graph_res$params
Helper method that creates data.tables with cell indices which were used in the given analysis + the values that are to be added to the obs table in the DuckDB.
get_data(x, columns = NULL, ...) ## S3 method for class 'CellQc' get_data(x, ...) ## S3 method for class 'ScListRes' get_data(x, ...) ## S3 method for class 'ScMatrixRes' get_data(x, columns = NULL, ...) ## S3 method for class 'ScrubletRes' get_data(x, ...) ## S3 method for class 'BoostRes' get_data(x, ...) ## S3 method for class 'ScDblFinderRes' get_data(x, ...) ## S3 method for class 'SingleCellFastClusters' get_data(x, ...) ## S3 method for class 'NmfResult' get_data(x, ...) ## S3 method for class 'ConsensusNmfResult' get_data(x, ...)get_data(x, columns = NULL, ...) ## S3 method for class 'CellQc' get_data(x, ...) ## S3 method for class 'ScListRes' get_data(x, ...) ## S3 method for class 'ScMatrixRes' get_data(x, columns = NULL, ...) ## S3 method for class 'ScrubletRes' get_data(x, ...) ## S3 method for class 'BoostRes' get_data(x, ...) ## S3 method for class 'ScDblFinderRes' get_data(x, ...) ## S3 method for class 'SingleCellFastClusters' get_data(x, ...) ## S3 method for class 'NmfResult' get_data(x, ...) ## S3 method for class 'ConsensusNmfResult' get_data(x, ...)
x |
An object to get the data from. |
columns |
Optional string. For some of the functions you can decide to only extract specific columns. |
... |
Other parameters |
Returns a data.table with a cell_idx column for the cells included in the analysis and additional columns to be added to the obs table.
# the cell indices and cluster memberships a run produced sc <- demo_single_cells() res <- fast_cluster_sc(sc, resolutions = 1.0, .verbose = FALSE) head(get_data(res), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the cell indices and cluster memberships a run produced sc <- demo_single_cells() res <- fast_cluster_sc(sc, resolutions = 1.0, .verbose = FALSE) head(get_data(res), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter function to extract the Effect size results from the
BulkDge() class.
get_dge_effect_sizes(object)get_dge_effect_sizes(object)
object |
|
Returns the effect size results. (If found.)
# Hedge's G effect sizes per contrast syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_dge_hedges( object, contrast_column = "case_control", .verbose = FALSE ) head(get_dge_effect_sizes(object))# Hedge's G effect sizes per contrast syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_dge_hedges( object, contrast_column = "case_control", .verbose = FALSE ) head(get_dge_effect_sizes(object))
Getter function to extract the Limma Voom results from the
BulkDge() class.
get_dge_limma_voom(object)get_dge_limma_voom(object)
object |
|
Returns the Limma Voom results. (If found.)
# topTable results for every contrast that was fitted syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_dge_limma( object, contrast_column = "case_control", .verbose = FALSE ) head(get_dge_limma_voom(object))# topTable results for every contrast that was fitted syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_dge_limma( object, contrast_column = "case_control", .verbose = FALSE ) head(get_dge_limma_voom(object))
Builds an edgeR DGEList from the filtered counts, library sizes and
normalisation factors stored in the BulkDge() class. bixverse
does not need edgeR itself, so this requires edgeR to be installed.
Normalisation factors are one until normalise_bulk_dge() has
run.
get_dge_list(object)get_dge_list(object)
object |
|
An edgeR DGEList, or NULL with a warning if
qc_bulk_dge() has not been run.
# the edgeR DGEList built during QC syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) if (requireNamespace("edgeR", quietly = TRUE)) { dim(get_dge_list(object)) }# the edgeR DGEList built during QC syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) if (requireNamespace("edgeR", quietly = TRUE)) { dim(get_dge_list(object)) }
Getter function to extract the QC plots from the BulkDge()
class. These are added when you run for example
qc_bulk_dge() and normalise_bulk_dge(). You can
either leave the plot choice as NULL and provide input when prompted, or
you provide the name. The possible plots that might be in the class
p1_nb_genes_cohort Proportion of non-zero genes for the samples in the
respective cohorts (added after using qc_bulk_dge()).
p2_outliers An outlier plot based on the data from p1, added after
using qc_bulk_dge().
p3_voom_normalization Initial Voom normalisation plot after filtering
lowly expressed genes. Added after using normalise_bulk_dge().
p4_boxplot_normalization Expression levels after normalisation. Added
after using normalise_bulk_dge().
p5_pca_case_control A PCA plot with the chosen case control category.
Added if calculate_pca_bulk_dge() is run.
p6_batch_correction_plot A PCA plot pre and post batch correction
with the case-control category overlayed. Added if
batch_correction_bulk_dge() is run.
get_dge_qc_plot(object, plot_choice = NULL)get_dge_qc_plot(object, plot_choice = NULL)
object |
|
plot_choice |
Optional string or integer. Index or name of the plate. |
Returns the DGEList stored in the class.
# pull a named QC plot back off the object syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) get_dge_qc_plot(object, plot_choice = "p1_nb_genes_cohort")# pull a named QC plot back off the object syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) get_dge_qc_plot(object, plot_choice = "p1_nb_genes_cohort")
Returns one diagnostic by key, or the whole named list if which is
NULL. Contents are method-specific: CoReMo stores stability and
cluster_quality; Leiden stores resolution_used and modularity; ICA
stores ica_meta and stability_scores; DGRDL stores
feature_laplacian, sample_laplacian, and the grid-search table; NMF
stores final_loss, n_iter, converged, and (for stabilised runs)
losses, best_idx.
get_diagnostics(object, which = NULL)get_diagnostics(object, which = NULL)
object |
A |
which |
String or |
The requested diagnostic, the named list, or NULL (with
warning) if which is not among the stored diagnostic keys.
# convergence diagnostics of an NMF fit syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 500L, .verbose = FALSE) object <- nmf_bulk(object, k = 3L, .verbose = FALSE) res <- S7::prop(object, "final_results") names(get_diagnostics(res)) get_diagnostics(res, "converged")# convergence diagnostics of an NMF fit syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 500L, .verbose = FALSE) object <- nmf_bulk(object, k = 3L, .verbose = FALSE) res <- S7::prop(object, "final_results") names(get_diagnostics(res)) get_diagnostics(res, "converged")
Helper function to get a differential correlation-based igraph from the class
get_diffcor_graph(object, min_cor = 0.2, fdr_threshold = 0.05, .verbose = TRUE)get_diffcor_graph(object, min_cor = 0.2, fdr_threshold = 0.05, .verbose = TRUE)
object |
The class, see |
min_cor |
Float. The minimum absolute correlation that needs to be present in either data set. |
fdr_threshold |
Float. The maximum FDR that is tolerated for the generation of the graph. |
.verbose |
Boolean. Controls the verbosity of the function. |
A list with the following elements:
graph - The igraph
params - A list that contains the parameters of the graph generation and general graph information (node, edge numbers).
# igraph from the differential correlations sig <- synthetic_signal_matrix() mat <- t(sig$mat) target <- mat[sig$group %in% c("group1", "group2"), ] background <- mat[sig$group == "group3", ] meta <- data.table::data.table(sample_id = rownames(target)) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- diffcor_module_processing( obj, background, cor_method = "pearson", .verbose = FALSE ) graph_res <- get_diffcor_graph(obj, .verbose = FALSE) graph_res$params# igraph from the differential correlations sig <- synthetic_signal_matrix() mat <- t(sig$mat) target <- mat[sig$group %in% c("group1", "group2"), ] background <- mat[sig$group == "group3", ] meta <- data.table::data.table(sample_id = rownames(target)) obj <- BulkCoExp(target, meta) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- diffcor_module_processing( obj, background, cor_method = "pearson", .verbose = FALSE ) graph_res <- get_diffcor_graph(obj, .verbose = FALSE) graph_res$params
Get the differential abundance results
get_differential_abundance_res(x) ## S3 method for class 'miloR' get_differential_abundance_res(x)get_differential_abundance_res(x) ## S3 method for class 'miloR' get_differential_abundance_res(x)
x |
An object from which to get the differential abundance results from. |
The differential abundance results stored in the object if found.
# neighbourhood level differential abundance table sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) milo <- get_miloR_abundances_sc( sc, sample_id_col = "sample_id", miloR_params = params_sc_miloR(k_refine = 10L), .verbose = FALSE ) design_df <- data.frame( grp = rep(c("a", "b"), each = 3), row.names = sprintf("sample_%i", 1:6) ) milo <- test_nhoods(milo, design = ~grp, design_df = design_df) head(get_differential_abundance_res(milo)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# neighbourhood level differential abundance table sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) milo <- get_miloR_abundances_sc( sc, sample_id_col = "sample_id", miloR_params = params_sc_miloR(k_refine = 10L), .verbose = FALSE ) design_df <- data.frame( grp = rep(c("a", "b"), each = 3), row.names = sprintf("sample_%i", 1:6) ) milo <- test_nhoods(milo, design = ~grp, design_df = design_df) head(get_differential_abundance_res(milo)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Returns the diffusion Z-scores if you ran
permute_seed_nodes().
get_diffusion_perms(object)get_diffusion_perms(object)
object |
The underlying class |
The diffusion Z scores if found. Otherwise NULL.
# Z-scores from node-degree adjusted permutations set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes(object, c(node_1 = 1, node_3 = 1), "max") object <- permute_seed_nodes(object, perm_iters = 100L, .verbose = FALSE) head(get_diffusion_perms(object))# Z-scores from node-degree adjusted permutations set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes(object, c(node_1 = 1, node_3 = 1), "max") object <- permute_seed_nodes(object, perm_iters = 100L, .verbose = FALSE) head(get_diffusion_perms(object))
Returns the diffusion vector if you ran tied_diffusion()
or diffuse_seed_nodes().
get_diffusion_vector(object)get_diffusion_vector(object)
object |
The underlying class |
The diffusion vector if found. If you did not run either diffusion
functions, it will return NULL and a warning.
# diffusion scores after a single seed node diffusion set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes(object, c(node_1 = 1, node_3 = 1), "max") head(get_diffusion_vector(object))# diffusion scores after a single seed node diffusion set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes(object, c(node_1 = 1, node_3 = 1), "max") head(get_diffusion_vector(object))
General wrapper function that can be used to pull out any embedding stored in the class. This function is used for the single cell-related classes and methods.
get_embedding(x, embd_name, ...) ## S7 method for class <bixverse::MetaCells> get_embedding(x, embd_name, ...) ## S3 method for class 'ScCache' get_embedding(x, embd_name, ...) ## S7 method for class <bixverse::SingleCells> get_embedding(x, embd_name, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_embedding(x, embd_name, ...)get_embedding(x, embd_name, ...) ## S7 method for class <bixverse::MetaCells> get_embedding(x, embd_name, ...) ## S3 method for class 'ScCache' get_embedding(x, embd_name, ...) ## S7 method for class <bixverse::SingleCells> get_embedding(x, embd_name, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_embedding(x, embd_name, ...)
x |
An object to get embedding from |
embd_name |
String. The name of the embedding to return. The function will throw an error if the embedding does not exist. |
... |
Other parameters. |
Get the specified embeddings from the object (if found).
# any stored embedding, by name sc <- demo_single_cells() dim(get_embedding(sc, "pca")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# any stored embedding, by name sc <- demo_single_cells() dim(get_embedding(sc, "pca")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter function to extract the epsilon param ~ power law goodness of fit
data from the BulkCoExp() class.
get_epsilon_res(object)get_epsilon_res(object)
object |
|
Returns the epsilon data. (If found. Otherwise NULL).
# scale-free fit of the affinity matrix across epsilons syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 200L, .verbose = FALSE) object <- cor_module_processing( object, cor_method = "pearson", .verbose = FALSE ) object <- cor_module_check_epsilon( object, rbf_func = "gaussian", .verbose = FALSE ) head(get_epsilon_res(object))# scale-free fit of the affinity matrix across epsilons syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 200L, .verbose = FALSE) object <- cor_module_processing( object, cor_method = "pearson", .verbose = FALSE ) object <- cor_module_check_epsilon( object, rbf_func = "gaussian", .verbose = FALSE ) head(get_epsilon_res(object))
Returns one factor matrix by key, or the whole named list of factor
matrices if which is NULL. Keys are method-specific; see the
factors argument of new_bulk_module_result() for the shared vocabulary.
get_factors(object, which = NULL)get_factors(object, which = NULL)
object |
A |
which |
String or |
A matrix, the named list of matrices, or NULL (with warning)
if which is not among the stored factor keys.
# gene loadings and sample activities of an NMF fit syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 500L, .verbose = FALSE) object <- nmf_bulk(object, k = 3L, .verbose = FALSE) res <- S7::prop(object, "final_results") names(get_factors(res)) dim(get_factors(res, "gene_loadings"))# gene loadings and sample activities of an NMF fit syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 500L, .verbose = FALSE) object <- nmf_bulk(object, k = 3L, .verbose = FALSE) res <- S7::prop(object, "final_results") names(get_factors(res)) dim(get_factors(res, "gene_loadings"))
Get the feature matrix used for the classifier
get_feature_mat(x, ...) ## S3 method for class 'ScDblFinderRes' get_feature_mat(x, ...)get_feature_mat(x, ...) ## S3 method for class 'ScDblFinderRes' get_feature_mat(x, ...)
x |
An object to get the feature matrix from. This will only include the values of the observed cells. |
... |
Additional parameters to forward to the method. |
# the features the scDblFinder classifier was trained on sc <- demo_single_cells(prepped = FALSE) res <- scdblfinder_sc( sc, scdblfinder_params = params_scdblfinder( pca = list(no_pcs = 10L), n_genes = 25L, cxds_genes = 25L ), return_features = TRUE, .verbose = FALSE ) dim(get_feature_mat(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the features the scDblFinder classifier was trained on sc <- demo_single_cells(prepped = FALSE) res <- scdblfinder_sc( sc, scdblfinder_params = params_scdblfinder( pca = list(no_pcs = 10L), n_genes = 25L, cxds_genes = 25L ), return_features = TRUE, .verbose = FALSE ) dim(get_feature_mat(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter function to extract the FPKM-normalised counts from the
BulkDge() class.
get_fpkm_counts(object)get_fpkm_counts(object)
object |
|
Returns the FPKM-normalised counts. (If found.)
# FPKM counts, which normalise_bulk_dge() only computes on request syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) gene_lengths <- stats::setNames( rep(2000, nrow(syn$counts)), rownames(syn$counts) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", calc_fpkm = TRUE, gene_lengths = gene_lengths, .verbose = FALSE ) get_fpkm_counts(object)[1:3, 1:3]# FPKM counts, which normalise_bulk_dge() only computes on request syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) gene_lengths <- stats::setNames( rep(2000, nrow(syn$counts)), rownames(syn$counts) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", calc_fpkm = TRUE, gene_lengths = gene_lengths, .verbose = FALSE ) get_fpkm_counts(object)[1:3, 1:3]
Returns the index for a given gene based on the internal gene mapping. This is used for the single cell-related classes and methods.
get_gene_indices(x, gene_ids, rust_index) ## S7 method for class <bixverse::MetaCells> get_gene_indices(x, gene_ids, rust_index) ## S3 method for class 'ScMap' get_gene_indices(x, gene_ids, rust_index) ## S7 method for class <bixverse::SingleCells> get_gene_indices(x, gene_ids, rust_index) ## S7 method for class <bixverse::SingleCellsSubset> get_gene_indices(x, gene_ids, rust_index)get_gene_indices(x, gene_ids, rust_index) ## S7 method for class <bixverse::MetaCells> get_gene_indices(x, gene_ids, rust_index) ## S3 method for class 'ScMap' get_gene_indices(x, gene_ids, rust_index) ## S7 method for class <bixverse::SingleCells> get_gene_indices(x, gene_ids, rust_index) ## S7 method for class <bixverse::SingleCellsSubset> get_gene_indices(x, gene_ids, rust_index)
x |
An object to get the gene index from. |
gene_ids |
String vector. The gene ids to search for. |
rust_index |
Bool. Shall Rust-based indexing be returned. |
The indices of the genes
# R-based positions of two genes sc <- demo_single_cells(prepped = FALSE) get_gene_indices(sc, c("gene_01", "gene_02"), rust_index = FALSE) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# R-based positions of two genes sc <- demo_single_cells(prepped = FALSE) get_gene_indices(sc, c("gene_01", "gene_02"), rust_index = FALSE) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Get the gene lengths
get_gene_lengths(x, species = c("human", "mouse", "rat"), ...) ## S7 method for class <bixverse::BulkCoExp> get_gene_lengths(x, species = c("human", "mouse", "rat"), ...)get_gene_lengths(x, species = c("human", "mouse", "rat"), ...) ## S7 method for class <bixverse::BulkCoExp> get_gene_lengths(x, species = c("human", "mouse", "rat"), ...)
x |
Object to extract gene lengths from. Can be a matrix or BulkCoExp object. |
species |
String. One of |
... |
Additional parameters passed to methods. |
## Not run: # median transcript length per Ensembl gene, queried from Ensembl counts <- matrix( 1:4, nrow = 2, dimnames = list(c("ENSG00000141510", "ENSG00000012048"), c("s1", "s2")) ) get_gene_lengths(counts, species = "human") ## End(Not run)## Not run: # median transcript length per Ensembl gene, queried from Ensembl counts <- matrix( 1:4, nrow = 2, dimnames = list(c("ENSG00000141510", "ENSG00000012048"), c("s1", "s2")) ) get_gene_lengths(counts, species = "human") ## End(Not run)
Get the main gene names (for example symbols or Ensembl identifiers).
get_gene_names(x) ## S3 method for class 'ScMap' get_gene_names(x) ## S7 method for class <bixverse::SingleCells> get_gene_names(x) ## S7 method for class <bixverse::SingleCellsSubset> get_gene_names(x)get_gene_names(x) ## S3 method for class 'ScMap' get_gene_names(x) ## S7 method for class <bixverse::SingleCells> get_gene_names(x) ## S7 method for class <bixverse::SingleCellsSubset> get_gene_names(x)
x |
An object to get the gene names from. |
The primary gene identifiers stored in the class.
# the primary gene identifiers held by the object sc <- demo_single_cells(prepped = FALSE) head(get_gene_names(sc), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the primary gene identifiers held by the object sc <- demo_single_cells(prepped = FALSE) head(get_gene_names(sc), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Get the gene names based on the gene idx
get_gene_names_from_idx(x, gene_idx, rust_based = TRUE) ## S3 method for class 'ScMap' get_gene_names_from_idx(x, gene_idx, rust_based = TRUE) ## S7 method for class <bixverse::SingleCells> get_gene_names_from_idx(x, gene_idx, rust_based = TRUE) ## S7 method for class <bixverse::SingleCellsSubset> get_gene_names_from_idx(x, gene_idx, rust_based = TRUE)get_gene_names_from_idx(x, gene_idx, rust_based = TRUE) ## S3 method for class 'ScMap' get_gene_names_from_idx(x, gene_idx, rust_based = TRUE) ## S7 method for class <bixverse::SingleCells> get_gene_names_from_idx(x, gene_idx, rust_based = TRUE) ## S7 method for class <bixverse::SingleCellsSubset> get_gene_names_from_idx(x, gene_idx, rust_based = TRUE)
x |
An object to get the gene names from. |
gene_idx |
Integer. The original gene indices for which to return the gene names. |
rust_based |
Boolean. Is it Rust-based, i.e., 0-index or R-based, i.e., 1-indexed. |
# Rust indices translated back into gene identifiers sc <- demo_single_cells(prepped = FALSE) get_gene_names_from_idx(sc, gene_idx = 0:2, rust_based = TRUE) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# Rust indices translated back into gene identifiers sc <- demo_single_cells(prepped = FALSE) get_gene_names_from_idx(sc, gene_idx = 0:2, rust_based = TRUE) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function loads in gene ontology data stored in the package and processes
it into the format for GeneOntologyElim(). Wraps
load_go_human_data() and process_go_data() into one.
get_go_data_human(filter_relationships = TRUE, .verbose = TRUE)get_go_data_human(filter_relationships = TRUE, .verbose = TRUE)
filter_relationships |
Boolean. Shall the ontology be filtered to
only |
.verbose |
Boolean. Controls verbosity of the function. |
A data.table
# human GO data ready for GeneOntologyElim() go_dt <- get_go_data_human(.verbose = FALSE) dim(go_dt)# human GO data ready for GeneOntologyElim() go_dt <- get_go_data_human(.verbose = FALSE) dim(go_dt)
Getter function to extract the grid search results. If not found will return
NULL.
get_grid_search_res(object)get_grid_search_res(object)
object |
The class, see |
data.table with the grid search results (if found. Otherwise NULL.)
# the grid search table syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) obj <- BulkCoExp(syn$data, syn$meta_data) obj <- preprocess_bulk_coexp(obj, hvg = NULL, .verbose = FALSE) obj <- dgrdl_grid_search( obj, neighbours_vec = c(3L, 5L), dict_size_vec = c(4L, 6L), seed_vec = 123L, .verbose = FALSE ) get_grid_search_res(obj)# the grid search table syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) obj <- BulkCoExp(syn$data, syn$meta_data) obj <- preprocess_bulk_coexp(obj, hvg = NULL, .verbose = FALSE) obj <- dgrdl_grid_search( obj, neighbours_vec = c(3L, 5L), dict_size_vec = c(4L, 6L), seed_vec = 123L, .verbose = FALSE ) get_grid_search_res(obj)
Get the H (cell activations) matrix
get_h(x) ## S3 method for class 'NmfResult' get_h(x) ## S3 method for class 'StabilisedNmfResult' get_h(x) ## S3 method for class 'ConsensusNmfResult' get_h(x)get_h(x) ## S3 method for class 'NmfResult' get_h(x) ## S3 method for class 'StabilisedNmfResult' get_h(x) ## S3 method for class 'ConsensusNmfResult' get_h(x)
x |
An object holding NMF results. |
# per cell activations of a five factor NMF sc <- demo_single_cells() res <- nmf_sc(sc, k = 5L, .verbose = FALSE) dim(get_h(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# per cell activations of a five factor NMF sc <- demo_single_cells() res <- nmf_sc(sc, k = 5L, .verbose = FALSE) dim(get_h(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Distinguishes sparse (CSR/CSC) from dense storage. For dense X the
orientation is inferred by comparing the matrix dims against the obs and
var lengths (DENSE_ROW = cells x genes, DENSE_COL = genes x cells).
Ties (no_obs == no_var) fall back to the AnnData convention (DENSE_ROW).
get_h5ad_dimensions(f_path)get_h5ad_dimensions(f_path)
f_path |
File path to the |
A list with dims (named integer c(obs, var)) and type (one of
"CSR", "CSC", "DENSE_ROW", "DENSE_COL").
# dimensions and storage layout without reading the counts data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, data$counts, data$obs, data$var, .verbose = FALSE) get_h5ad_dimensions(f_path) unlink(f_path)# dimensions and storage layout without reading the counts data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, data$counts, data$obs, data$var, .verbose = FALSE) get_h5ad_dimensions(f_path) unlink(f_path)
Get the hotspot gene membership table
get_hotspot_membership(x) ## S3 method for class 'Hotspot' get_hotspot_membership(x)get_hotspot_membership(x) ## S3 method for class 'Hotspot' get_hotspot_membership(x)
x |
The object from which to retrieve the hotspot gene membership |
# the gene to module table hotspot clustered out sc <- demo_single_cells() hs <- hotspot_gene_cor_sc(sc, .verbose = FALSE) hs <- generate_hotspot_membership(hs) head(get_hotspot_membership(hs)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the gene to module table hotspot clustered out sc <- demo_single_cells() hs <- hotspot_gene_cor_sc(sc, .verbose = FALSE) hs <- generate_hotspot_membership(hs) head(get_hotspot_membership(hs)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Returns the HVG indices. Pending class type this are 1-based (for R) or 0-based for Rust.
get_hvg(x) ## S7 method for class <bixverse::MetaCells> get_hvg(x) ## S3 method for class 'ScMap' get_hvg(x) ## S7 method for class <bixverse::SingleCells> get_hvg(x) ## S7 method for class <bixverse::SingleCellsSubset> get_hvg(x)get_hvg(x) ## S7 method for class <bixverse::MetaCells> get_hvg(x) ## S3 method for class 'ScMap' get_hvg(x) ## S7 method for class <bixverse::SingleCells> get_hvg(x) ## S7 method for class <bixverse::SingleCellsSubset> get_hvg(x)
x |
An object to get HVG from. |
Indices of the stored HVG genes.
# stored 0-based, so map them back through the gene names sc <- demo_single_cells() get_gene_names_from_idx(sc, head(get_hvg(sc), 3)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# stored 0-based, so map them back through the gene names sc <- demo_single_cells() get_gene_names_from_idx(sc, head(get_hvg(sc), 3)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Like find_hvg_sc() but does not mutate object. Returns a data.table
with per-gene HVG statistics plus is_hvg/hvg_rank for the top hvg_no
genes. Useful for computing HVGs on a subset of cells (e.g. a specific
cell type) for downstream methods like NMF, without overwriting the HVGs
stored on the object.
get_hvg_data_sc( object, cell_ids = NULL, hvg_no = 3000L, hvg_params = params_sc_hvg(), streaming = NULL, .verbose = TRUE )get_hvg_data_sc( object, cell_ids = NULL, hvg_no = 3000L, hvg_params = params_sc_hvg(), streaming = NULL, .verbose = TRUE )
object |
|
cell_ids |
Optional character. Cell ids (or meta cell ids) to restrict
the HVG calculation to. If |
hvg_no |
Integer. Number of top HVGs to flag. Defaults to |
hvg_params |
List, see |
streaming |
Optional Boolean. Stream the data. Ignored for |
.verbose |
Boolean or integer. Verbosity. |
data.table with gene_idx, gene_id, the HVG statistics returned
by the Rust HVG function, an is_hvg boolean and an hvg_rank integer
(NA for non-HVGs).
# HVG statistics without touching the object sc <- demo_single_cells(prepped = FALSE) dt <- get_hvg_data_sc(sc, hvg_no = 20L, .verbose = FALSE) head(dt[(is_hvg), c("gene_id", "hvg_rank")], 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# HVG statistics without touching the object sc <- demo_single_cells(prepped = FALSE) dt <- get_hvg_data_sc(sc, hvg_no = 20L, .verbose = FALSE) head(dt[(is_hvg), c("gene_id", "hvg_rank")], 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter function to extract the ICA component data in terms of stability,
convergence and maximum absolute correlation between the component loadings
(max_abs_loading_cor). If not
found will return NULL.
get_ica_stability_res(object)get_ica_stability_res(object)
object |
The class, see |
data.table with the ICA parameter data (if found. Otherwise NULL.)
# the stability table behind the ncomp choice mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) obj <- ica_evaluate_comp( obj, ica_type = "logcosh", ncomp_params = params_ica_ncomp(custom_seq = seq(2L, 20L, by = 2L)), .verbose = FALSE ) head(get_ica_stability_res(obj))# the stability table behind the ncomp choice mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) obj <- ica_evaluate_comp( obj, ica_type = "logcosh", ncomp_params = params_ica_ncomp(custom_seq = seq(2L, 20L, by = 2L)), .verbose = FALSE ) head(get_ica_stability_res(obj))
Get the index cells
get_index_cells(x) ## S3 method for class 'miloR' get_index_cells(x)get_index_cells(x) ## S3 method for class 'miloR' get_index_cells(x)
x |
An object from which get the index cells. |
The indices of the cells in the neighbourhood.
# the cells the neighbourhoods were centred on sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) milo <- get_miloR_abundances_sc( sc, sample_id_col = "sample_id", miloR_params = params_sc_miloR(k_refine = 10L), .verbose = FALSE ) head(get_index_cells(milo)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the cells the neighbourhoods were centred on sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) milo <- get_miloR_abundances_sc( sc, sample_id_col = "sample_id", miloR_params = params_sc_miloR(k_refine = 10L), .verbose = FALSE ) head(get_index_cells(milo)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function will identify the index of the inflection point of an arbitrary
series x ~ y via the biggest increase in the first derivative. Useful to
identify key points in elbow plots.
get_inflection_point(x, y, span = 0.5)get_inflection_point(x, y, span = 0.5)
x, y
|
The x and y values. |
span |
The span parameter for the loess function. |
A list containing:
inflection_idx - Index of the inflection point
gradient_change - Absolute change in the first derivative
# elbow of a decaying curve that flattens out x <- 1:20 y <- c(exp(-x[1:10] / 2), rep(0.005, 10)) get_inflection_point(x, y)$inflection_idx# elbow of a decaying curve that flattens out x <- 1:20 y <- c(exp(-x[1:10] / 2), rep(0.005, 10)) get_inflection_point(x, y)$inflection_idx
Get the ligand-target influence matrix
get_influence(x) ## S3 method for class 'LigandTargetInfluence' get_influence(x)get_influence(x) ## S3 method for class 'LigandTargetInfluence' get_influence(x)
x |
An object holding ligand-target influence results. |
# the ligands x genes regulatory potential matrix ppi <- data.table::data.table( from = c("L1", "SIG1", "L2", "SIG2"), to = c("SIG1", "TF1", "SIG2", "TF2"), weight = 1.0 ) grn <- data.table::data.table( from = rep(c("TF1", "TF2"), each = 3), to = c("G1", "G2", "G3", "G4", "G5", "G6"), weight = 1.0 ) inf <- generate_ligand_target_influence( ligand_seeds = list(L1 = "L1", L2 = "L2"), ppi_network = ppi, grn_network = grn, params = params_ligand_target(ltf_cutoff = 0) ) dim(get_influence(inf))# the ligands x genes regulatory potential matrix ppi <- data.table::data.table( from = c("L1", "SIG1", "L2", "SIG2"), to = c("SIG1", "TF1", "SIG2", "TF2"), weight = 1.0 ) grn <- data.table::data.table( from = rep(c("TF1", "TF2"), each = 3), to = c("G1", "G2", "G3", "G4", "G5", "G6"), weight = 1.0 ) inf <- generate_ligand_target_influence( ligand_seeds = list(L1 = "L1", L2 = "L2"), ppi_network = ppi, grn_network = grn, params = params_ligand_target(ltf_cutoff = 0) ) dim(get_influence(inf))
Get k-means cluster assignments from a fast cluster result
get_kmeans_clusters(x) ## S3 method for class 'SingleCellFastClusters' get_kmeans_clusters(x)get_kmeans_clusters(x) ## S3 method for class 'SingleCellFastClusters' get_kmeans_clusters(x)
x |
|
# the centroid each cell was assigned to sc <- demo_single_cells() res <- fast_cluster_sc( sc, resolutions = 1.0, n_centroids = 30L, return_kmeans = TRUE, .verbose = FALSE ) head(get_kmeans_clusters(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the centroid each cell was assigned to sc <- demo_single_cells() res <- fast_cluster_sc( sc, resolutions = 1.0, n_centroids = 30L, return_kmeans = TRUE, .verbose = FALSE ) head(get_kmeans_clusters(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter for an integer matrix of samples x distances. Useful in combination
with get_knn_mat().
get_knn_dist(x, ...) ## S7 method for class <bixverse::MetaCells> get_knn_dist(x, ...) ## S3 method for class 'ScCache' get_knn_dist(x, ...) ## S7 method for class <bixverse::SingleCells> get_knn_dist(x, ...) ## S3 method for class 'SingleCellNearestNeighbour' get_knn_dist(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_knn_dist(x, ...)get_knn_dist(x, ...) ## S7 method for class <bixverse::MetaCells> get_knn_dist(x, ...) ## S3 method for class 'ScCache' get_knn_dist(x, ...) ## S7 method for class <bixverse::SingleCells> get_knn_dist(x, ...) ## S3 method for class 'SingleCellNearestNeighbour' get_knn_dist(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_knn_dist(x, ...)
x |
An object to get the kNN distances from. |
... |
Other parameters. |
# cells x neighbour distances sc <- demo_single_cells() dim(get_knn_dist(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# cells x neighbour distances sc <- demo_single_cells() dim(get_knn_dist(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter for an integer matrix of samples x neighbours.
get_knn_mat(x, ...) ## S7 method for class <bixverse::MetaCells> get_knn_mat(x, ...) ## S3 method for class 'ScCache' get_knn_mat(x, ...) ## S7 method for class <bixverse::SingleCells> get_knn_mat(x, ...) ## S3 method for class 'SingleCellNearestNeighbour' get_knn_mat(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_knn_mat(x, ...)get_knn_mat(x, ...) ## S7 method for class <bixverse::MetaCells> get_knn_mat(x, ...) ## S3 method for class 'ScCache' get_knn_mat(x, ...) ## S7 method for class <bixverse::SingleCells> get_knn_mat(x, ...) ## S3 method for class 'SingleCellNearestNeighbour' get_knn_mat(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_knn_mat(x, ...)
x |
An object to get the kNN matrix from. |
... |
Other parameters. |
# cells x neighbours sc <- demo_single_cells() dim(get_knn_mat(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# cells x neighbours sc <- demo_single_cells() dim(get_knn_mat(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Returns the SingleCellNearestNeighbour from the object. This function is
used for the single cell-related classes and methods.
get_knn_obj(x, ...) ## S7 method for class <bixverse::MetaCells> get_knn_obj(x, ...) ## S3 method for class 'ScCache' get_knn_obj(x, ...) ## S7 method for class <bixverse::SingleCells> get_knn_obj(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_knn_obj(x, ...)get_knn_obj(x, ...) ## S7 method for class <bixverse::MetaCells> get_knn_obj(x, ...) ## S3 method for class 'ScCache' get_knn_obj(x, ...) ## S7 method for class <bixverse::SingleCells> get_knn_obj(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_knn_obj(x, ...)
x |
An object to get the KNN class from. |
... |
Other parameters. |
The SingleCellNearestNeighbour object.
# the cached kNN, cells x neighbours sc <- demo_single_cells() dim(get_knn_mat(get_knn_obj(sc))) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the cached kNN, cells x neighbours sc <- demo_single_cells() dim(get_knn_mat(get_knn_obj(sc))) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Returns the ScMagic layer written by run_magic_sc(). This
function is used for the single cell-related classes and methods.
get_magic(x, ...) ## S3 method for class 'ScCache' get_magic(x, ...) ## S7 method for class <bixverse::SingleCells> get_magic(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_magic(x, ...)get_magic(x, ...) ## S3 method for class 'ScCache' get_magic(x, ...) ## S7 method for class <bixverse::SingleCells> get_magic(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_magic(x, ...)
x |
An object to get the imputed layer from. |
... |
Other parameters. |
The ScMagic object, or NULL when nothing was imputed.
# the imputed layer, only the genes MAGIC was asked for sc <- demo_single_cells() sc <- run_magic_sc(sc, features = get_gene_names(sc)[1:5], .verbose = FALSE) dim(get_magic(sc)$data) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the imputed layer, only the genes MAGIC was asked for sc <- demo_single_cells() sc <- run_magic_sc(sc, features = get_gene_names(sc)[1:5], .verbose = FALSE) dim(get_magic(sc)$data) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Returns the statistics that the summaries of
get_marker_summary() are built from, i.e., one row per gene,
reference group and rival. Useful to find out which rival a gene fails
against.
get_marker_comparisons(x) ## S3 method for class 'ScSpecificMarkers' get_marker_comparisons(x)get_marker_comparisons(x) ## S3 method for class 'ScSpecificMarkers' get_marker_comparisons(x)
x |
|
A copy of the per comparison data.table.
# the per rival statistics the summaries are built from sc <- demo_single_cells() res <- find_specific_markers_sc( sc, column_of_interest = "cell_grp", .verbose = FALSE ) head(get_marker_comparisons(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the per rival statistics the summaries are built from sc <- demo_single_cells() res <- find_specific_markers_sc( sc, column_of_interest = "cell_grp", .verbose = FALSE ) head(get_marker_comparisons(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Returns the summaries a marker is judged on: the AUROC of the reference
against its rivals reduced to one row per gene and reference group. Rank on
median_auroc for a marker that survives a single closely related rival, or
on min_auroc when it has to beat every rival unambiguously.
get_marker_summary(x) ## S3 method for class 'ScSpecificMarkers' get_marker_summary(x)get_marker_summary(x) ## S3 method for class 'ScSpecificMarkers' get_marker_summary(x)
x |
|
A copy of the summary data.table.
# per gene summaries across every rival group sc <- demo_single_cells() res <- find_specific_markers_sc( sc, column_of_interest = "cell_grp", .verbose = FALSE ) head(get_marker_summary(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# per gene summaries across every rival group sc <- demo_single_cells() res <- find_specific_markers_sc( sc, column_of_interest = "cell_grp", .verbose = FALSE ) head(get_marker_summary(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Like calc_meta_cell_purity() but does not mutate object. Returns a
data.table with the purity per meta cell instead of stamping the columns onto
the observation table. Useful for comparing several label columns, or for
sweeping meta cell resolutions, without permanently annotating the object.
get_meta_cell_purity( object, original_cell_type, add_additional_info = c("none", "top_label", "top_two_labels"), add_entropy = FALSE )get_meta_cell_purity( object, original_cell_type, add_additional_info = c("none", "top_label", "top_two_labels"), add_entropy = FALSE )
object |
|
original_cell_type |
Character vector. The original cell type
annotations of the object the meta cells came from. Either in the row order
of its full (unfiltered) obs table, i.e. |
add_additional_info |
String. Which label information to add on top of
the purity. One of |
add_entropy |
Boolean. Shall the normalised Shannon entropy of the
label distribution be added as a diversity measure. Defaults to |
Ties for the top (or second) label resolve to whichever label sorts first,
as the labels are factorised internally. The entropy is the Shannon entropy
of the label distribution within a meta cell, divided by
log(<number of distinct labels in original_cell_type>), so it sits in
[0, 1] and stays comparable between meta cells. It is 0 if there is only
a single label in the data.
A data.table in observation table row order:
meta_cell_idx - Index of the meta cell. Always returned.
meta_cell_id - Identifier of the meta cell. Always returned.
mc_purity - Fraction of the meta cell's cells that carry the most abundant label. Always returned.
mc_top_label - Name of the most abundant label. Returned for
add_additional_info %in% c("top_label", "top_two_labels").
mc_second_label - Name of the second most abundant label, NA for a
pure meta cell. Returned for add_additional_info = "top_two_labels".
mc_second_frac - Fraction of the meta cell's cells carrying that
second label, 0 for a pure meta cell. Returned for
add_additional_info = "top_two_labels".
mc_entropy - Normalised Shannon entropy of the label distribution,
see details. Returned for add_entropy = TRUE.
# the same numbers as a table, leaving the object untouched sc <- demo_single_cells() mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 50L), .verbose = FALSE ) purity <- get_meta_cell_purity( mc, original_cell_type = sc[["cell_grp"]]$cell_grp, add_additional_info = "top_label" ) head(purity) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the same numbers as a table, leaving the object untouched sc <- demo_single_cells() mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 50L), .verbose = FALSE ) purity <- get_meta_cell_purity( mc, original_cell_type = sc[["cell_grp"]]$cell_grp, add_additional_info = "top_label" ) head(purity) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter function to extract the metadata from the BulkCoExp() or
BulkDge().
get_metadata(object, ...)get_metadata(object, ...)
object |
The underlying object, either |
... |
Additional arguments to parse to the functions. |
Returns the metadata stored in the class.
# sample metadata back out of a BulkDge syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) head(get_metadata(object))# sample metadata back out of a BulkDge syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) head(get_metadata(object))
This function implements the miloR differential abundance testing on top
of the kNN graph. The general idea of the approach is to use the kNN graph
generated from the single cell data, generate representative neighbourhoods
and calculate differential abundances within these neighbourhoods. For
further details on the method, please refer to Dann, et al. This function
will take a SingleCells class, run the neighbourhood detection,
count the occurrences of a sample and return a sc_miloR class for
subsequent differential abundance testing and further annotations.
get_miloR_abundances_sc( object, sample_id_col, embd_to_use = "pca", no_embd_to_use = NULL, miloR_params = params_sc_miloR(), seed = 42L, .verbose = TRUE )get_miloR_abundances_sc( object, sample_id_col, embd_to_use = "pca", no_embd_to_use = NULL, miloR_params = params_sc_miloR(), seed = 42L, .verbose = TRUE )
object |
|
sample_id_col |
Character. The column in the obs table representing the sample identifier to count. |
embd_to_use |
Character. The embedding to use for the refinement
procedure. Please use the same here as you used to generate the neighbours!
Defaults to |
no_embd_to_use |
Optional integer. If you only want to use a subset of the embedding. |
miloR_params |
A list, please see
|
seed |
Integer. Seed for reproducibility |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A miloR class with the following elements:
nhoods - Sparse matrix of cells x neighbourhoods.
sample_counts - Matrix of neighbourhoods x samples with the cell counts per neighbourhood and sample.
spatial_dist - Numeric. The kth nearest neighbour distance per neighbourhood index cell.
nhood_overlap - The overlap between the neighbourhoods.
params - List. The parameters used, plus the embedding and the index cells.
Dann, et al., Nat Biotechnol, 2022
# neighbourhood counts across six synthetic samples sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) milo <- get_miloR_abundances_sc( sc, sample_id_col = "sample_id", miloR_params = params_sc_miloR(k_refine = 10L), .verbose = FALSE ) dim(milo$sample_counts) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# neighbourhood counts across six synthetic samples sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) milo <- get_miloR_abundances_sc( sc, sample_id_col = "sample_id", miloR_params = params_sc_miloR(k_refine = 10L), .verbose = FALSE ) dim(milo$sample_counts) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
The neighbourhood test moved into Rust, so there is no
DGEGLM to hand
back any more. Everything the fit was consulted for now sits in the results
table, see get_differential_abundance_res().
get_model_fit(x) ## S3 method for class 'miloR' get_model_fit(x)get_model_fit(x) ## S3 method for class 'miloR' get_model_fit(x)
x |
An object from which to get the fitted model from. |
NULL, with a deprecation warning.
Returns the data.table of gene to module_id assignments. The exact
columns depend on the method that produced the result (CoReMo adds
sign and stability; NMF/ICA/DGRDL add loading, sign and the
thresholding score; Leiden adds only module_id).
gene is not a unique key for the matrix factorisation methods. ICA, NMF
and DGRDL assign membership by keeping the tails of each component's loading
distribution, so a gene loading strongly on three components appears in three
rows, and a gene in no tail appears in none. That is the point of a
factorisation. The partition-based methods (CoReMo, Leiden) do emit one row
per gene. Do not assume uniqueness without checking method.
get_modules(object)get_modules(object)
object |
A |
data.table with at minimum gene and module_id columns. One row
per (gene, module) pair.
# gene to module assignments from an NMF fit syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 500L, .verbose = FALSE) object <- nmf_bulk(object, k = 3L, .verbose = FALSE) res <- S7::prop(object, "final_results") head(get_modules(res))# gene to module assignments from an NMF fit syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 500L, .verbose = FALSE) object <- nmf_bulk(object, k = 3L, .verbose = FALSE) res <- S7::prop(object, "final_results") head(get_modules(res))
Getter function to extract the gene loadings matrix (features x k) from a
bulk NMF fit stored in BulkCoExp(). If no NMF fit is present,
returns NULL with a warning.
get_nmf_gene_loadings(object)get_nmf_gene_loadings(object)
object |
The class, see |
A features x k numeric matrix (if found) or NULL.
# gene loadings of a four-factor fit syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- nmf_bulk(obj, k = 4L, .verbose = FALSE) dim(get_nmf_gene_loadings(obj))# gene loadings of a four-factor fit syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- nmf_bulk(obj, k = 4L, .verbose = FALSE) dim(get_nmf_gene_loadings(obj))
Getter function to extract the gene-to-module data.table from a bulk NMF fit. Each row is one gene assigned to its top-loading module.
get_nmf_modules(object)get_nmf_modules(object)
object |
The class, see |
A data.table with gene, module_id, loading, sign columns
(if found) or NULL.
# gene to module assignments of a four-factor fit syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- nmf_bulk(obj, k = 4L, .verbose = FALSE) head(get_nmf_modules(obj))# gene to module assignments of a four-factor fit syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- nmf_bulk(obj, k = 4L, .verbose = FALSE) head(get_nmf_modules(obj))
Getter function to extract the sample activity matrix (samples x k) from a
bulk NMF fit. If no NMF fit is present, returns NULL with a warning.
get_nmf_sample_activity(object)get_nmf_sample_activity(object)
object |
The class, see |
A samples x k numeric matrix (if found) or NULL.
# sample activity of a four-factor fit syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- nmf_bulk(obj, k = 4L, .verbose = FALSE) dim(get_nmf_sample_activity(obj))# sample activity of a four-factor fit syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- nmf_bulk(obj, k = 4L, .verbose = FALSE) dim(get_nmf_sample_activity(obj))
Getter function for the diagnostics of a multi-run NMF fit. For a
stabilised_nmf_bulk() fit that is the per-run losses,
convergence flags and the best-run index. For a
consensus_nmf_bulk() fit it is the cluster stability, the
relative errors and the per-component clustering table. Returns NULL for a
single-run fit.
get_nmf_stability(object)get_nmf_stability(object)
object |
The class, see |
For a stabilised fit, a list with losses, converged and
best_idx. For a consensus fit, a list with stability, rel_error,
rel_run_errors, clusters, cluster_sizes, n_dropped and
n_empty_clusters. NULL if neither was run.
# clustering diagnostics of a consensus fit syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- consensus_nmf_bulk(obj, k = 4L, n_runs = 10L, .verbose = FALSE) get_nmf_stability(obj)$cluster_sizes# clustering diagnostics of a consensus fit syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- consensus_nmf_bulk(obj, k = 4L, n_runs = 10L, .verbose = FALSE) get_nmf_stability(obj)$cluster_sizes
This function will return all ancestors and descendants based on a provided data.table with parent-child terms
get_ontology_ancestry(parent_child_dt)get_ontology_ancestry(parent_child_dt)
parent_child_dt |
data.table. The data.table with column parent and child. |
A list with
ancestors A list with all ancestor terms.
descendants A list with all descendant terms.
# ancestors and descendants of every term in a toy ontology onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f") ) ancestry <- get_ontology_ancestry(onto) ancestry$ancestors[["f"]]# ancestors and descendants of every term in a toy ontology onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f") ) ancestry <- get_ontology_ancestry(onto) ancestry$ancestors[["f"]]
Getter function to extract the outputs from the BulkCoExp() or
BulkDge().
get_outputs(object, ...)get_outputs(object, ...)
object |
The underlying object, either |
... |
Additional arguments to parse to the functions. |
Returns the outputs stored in the class.
# everything the QC and normalisation steps stashed on the object syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) names(get_outputs(object))# everything the QC and normalisation steps stashed on the object syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) names(get_outputs(object))
Extracts parameters from the BixverseBaseClass class (or
child classes) and has options to return (pretty) JSONs. This generic
also gets inherited by other classes and can be used to extract parameters.
Also, can dispatch to specific methods for certain S3 classes.
get_params(object, to_json = FALSE, pretty_json = FALSE) get_params.LdaResult(object, ...) get_params.LdaKSweepResult(object, ...) get_params.Hotspot(object, to_json = FALSE, pretty_json = FALSE) get_params.miloR(object, to_json = FALSE, pretty_json = FALSE) get_params.ScenicGrn(object, to_json = FALSE, pretty_json = FALSE) get_params.NmfResult(object, to_json = FALSE, pretty_json = FALSE) get_params.StabilisedNmfResult(object, to_json = FALSE, pretty_json = FALSE) get_params.ConsensusNmfResult(object, to_json = FALSE, pretty_json = FALSE) get_params.DialogueResult(object, ...) get_params.ScNebula(object, to_json = FALSE, pretty_json = FALSE)get_params(object, to_json = FALSE, pretty_json = FALSE) get_params.LdaResult(object, ...) get_params.LdaKSweepResult(object, ...) get_params.Hotspot(object, to_json = FALSE, pretty_json = FALSE) get_params.miloR(object, to_json = FALSE, pretty_json = FALSE) get_params.ScenicGrn(object, to_json = FALSE, pretty_json = FALSE) get_params.NmfResult(object, to_json = FALSE, pretty_json = FALSE) get_params.StabilisedNmfResult(object, to_json = FALSE, pretty_json = FALSE) get_params.ConsensusNmfResult(object, to_json = FALSE, pretty_json = FALSE) get_params.DialogueResult(object, ...) get_params.ScNebula(object, to_json = FALSE, pretty_json = FALSE)
object |
A class within bixverse that inherits from
|
to_json |
Shall the params be returned as a JSON string. |
pretty_json |
Shall the params be returned as a pretty JSON string. |
... |
Unused, present so the S3 methods sharing this page match the generic. |
Depending on parameters either the R list or a (pretty) JSON string.
# parameters stored in a freshly created class object <- SimilarityNetworkFusion(snf_params = params_snf(k = 3L)) names(get_params(object)) get_params(object, to_json = TRUE, pretty_json = TRUE)# parameters stored in a freshly created class object <- SimilarityNetworkFusion(snf_params = params_snf(k = 3L)) names(get_params(object)) get_params(object, to_json = TRUE, pretty_json = TRUE)
Returns the PCA factors (sample-based scores). This function is used for the single cell-related classes and methods.
get_pca_factors(x, ...) ## S7 method for class <bixverse::MetaCells> get_pca_factors(x, ...) ## S3 method for class 'ScCache' get_pca_factors(x, ...) ## S7 method for class <bixverse::SingleCells> get_pca_factors(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_pca_factors(x, ...)get_pca_factors(x, ...) ## S7 method for class <bixverse::MetaCells> get_pca_factors(x, ...) ## S3 method for class 'ScCache' get_pca_factors(x, ...) ## S7 method for class <bixverse::SingleCells> get_pca_factors(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_pca_factors(x, ...)
x |
An object to get PCA factors from. |
... |
Other parameters. |
The PCA factors from the object (if found).
# cells x PCs sc <- demo_single_cells() dim(get_pca_factors(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# cells x PCs sc <- demo_single_cells() dim(get_pca_factors(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Returns the PCA loadings (feature-based scores). This function is used for the single cell-related classes and methods.
get_pca_loadings(x, ...) ## S7 method for class <bixverse::MetaCells> get_pca_loadings(x, ...) ## S3 method for class 'ScCache' get_pca_loadings(x, ...) ## S7 method for class <bixverse::SingleCells> get_pca_loadings(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_pca_loadings(x, ...)get_pca_loadings(x, ...) ## S7 method for class <bixverse::MetaCells> get_pca_loadings(x, ...) ## S3 method for class 'ScCache' get_pca_loadings(x, ...) ## S7 method for class <bixverse::SingleCells> get_pca_loadings(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_pca_loadings(x, ...)
x |
An object to get PCA loadings from. |
... |
Other parameters. |
The PCA feature loadings from the object (if found).
# HVGs x PCs sc <- demo_single_cells() dim(get_pca_loadings(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# HVGs x PCs sc <- demo_single_cells() dim(get_pca_loadings(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Returns the PCA singular values (can be useful to assess cumulative variance explained). This function is used for the single cell-related classes and methods.
get_pca_singular_val(x, ...) ## S7 method for class <bixverse::MetaCells> get_pca_singular_val(x, ...) ## S3 method for class 'ScCache' get_pca_singular_val(x, ...) ## S7 method for class <bixverse::SingleCells> get_pca_singular_val(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_pca_singular_val(x, ...)get_pca_singular_val(x, ...) ## S7 method for class <bixverse::MetaCells> get_pca_singular_val(x, ...) ## S3 method for class 'ScCache' get_pca_singular_val(x, ...) ## S7 method for class <bixverse::SingleCells> get_pca_singular_val(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_pca_singular_val(x, ...)
x |
An object to get PCA singular values from. |
... |
Other parameters. |
The PCA singular values from the object (if found).
# singular values, largest first sc <- demo_single_cells() head(get_pca_singular_val(sc), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# singular values, largest first sc <- demo_single_cells() head(get_pca_singular_val(sc), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This is a wrapper function to generate pseudo-bulked counts from a list of cell identifiers. You have the option to return sparse or dense matrices and if you wish to sum raw counts or calculate the mean of the normalised counts.
get_pseudobulked_sc( object, cell_list, return_format = c("dense", "sparse"), assay = c("raw", "norm"), .verbose = TRUE )get_pseudobulked_sc( object, cell_list, return_format = c("dense", "sparse"), assay = c("raw", "norm"), .verbose = TRUE )
object |
|
cell_list |
Named list. |
return_format |
String. One of |
assay |
String. One of |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
Pending on your setting in return_format a dense matrix or sparse CSR matrix with aggregated cells x genes.
# sum raw counts per planted cell type sc <- demo_single_cells() obs <- get_sc_obs(sc) res <- get_pseudobulked_sc( sc, cell_list = split(obs$cell_id, obs$cell_grp), .verbose = FALSE ) dim(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# sum raw counts per planted cell type sc <- demo_single_cells() obs <- get_sc_obs(sc) res <- get_pseudobulked_sc( sc, cell_list = split(obs$cell_id, obs$cell_grp), .verbose = FALSE ) dim(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Pulls out the RBH results if you ran
generate_rbh_graph()
get_rbh_res(object)get_rbh_res(object)
object |
The underlying class |
The data.table with the RBH result if found, otherwise NULL.
# reciprocal best hits between modules of two data sets set.seed(123) modules <- data.table::data.table( origin = rep(c("set_a", "set_b"), each = 20), module = rep(c("m1", "m2", "m3", "m4"), each = 10), gene = unlist(replicate(4, sample(letters, 10), simplify = FALSE)) ) object <- RbhGraph( modules, rbh_type = "set", dataset_col = "origin", module_col = "module", value_col = "gene" ) object <- generate_rbh_graph(object, minimum_similarity = 0) head(get_rbh_res(object))# reciprocal best hits between modules of two data sets set.seed(123) modules <- data.table::data.table( origin = rep(c("set_a", "set_b"), each = 20), module = rep(c("m1", "m2", "m3", "m4"), each = 10), gene = unlist(replicate(4, sample(letters, 10), simplify = FALSE)) ) object <- RbhGraph( modules, rbh_type = "set", dataset_col = "origin", module_col = "module", value_col = "gene" ) object <- generate_rbh_graph(object, minimum_similarity = 0) head(get_rbh_res(object))
Returns the ScResidualFit written by fit_residuals_sc(). This
function is used for the single cell-related classes and methods.
Warns and returns NULL when nothing was fitted, so it is safe to use as a
presence probe. The functions that compute with the fit assert instead.
get_residual_fit(x, ...) ## S7 method for class <bixverse::MetaCells> get_residual_fit(x, ...) ## S3 method for class 'ScCache' get_residual_fit(x, ...) ## S7 method for class <bixverse::SingleCells> get_residual_fit(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_residual_fit(x, ...)get_residual_fit(x, ...) ## S7 method for class <bixverse::MetaCells> get_residual_fit(x, ...) ## S3 method for class 'ScCache' get_residual_fit(x, ...) ## S7 method for class <bixverse::SingleCells> get_residual_fit(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_residual_fit(x, ...)
x |
An object to get the fitted model from. |
... |
Other parameters. |
The ScResidualFit object, or NULL when nothing was fitted.
# the fitted model and the genes it covers sc <- demo_single_cells() sc <- fit_residuals_sc(sc, method = "analytic_pearson", .verbose = FALSE) length(get_residual_fit(sc)$genes) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the fitted model and the genes it covers sc <- demo_single_cells() sc <- fit_residuals_sc(sc, method = "analytic_pearson", .verbose = FALSE) length(get_residual_fit(sc)$genes) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter function to get the resolution results (if available).
get_resolution_res(object)get_resolution_res(object)
object |
The class, see |
If resolution results were found, returns the data.table. Otherwise, throws a warning and returns NULL.
# cluster counts and modularity across the Leiden resolution sweep syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 200L, .verbose = FALSE) object <- cor_module_processing( object, cor_method = "pearson", .verbose = FALSE ) object <- cor_module_graph_check_res( object, parallel = FALSE, .verbose = FALSE ) head(get_resolution_res(object))# cluster counts and modularity across the Leiden resolution sweep syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 200L, .verbose = FALSE) object <- cor_module_processing( object, cor_method = "pearson", .verbose = FALSE ) object <- cor_module_graph_check_res( object, parallel = FALSE, .verbose = FALSE ) head(get_resolution_res(object))
Get the final results from BixverseBaseClass class (or child
classes).
get_results(object) get_results.DialogueResult(object, ...)get_results(object) get_results.DialogueResult(object, ...)
object |
The underlying |
... |
Unused, present so the S3 methods sharing this page match the generic. |
Returns the final results if any have been stored in the class.
# communities found after a network diffusion set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes(object, c(node_1 = 1, node_3 = 1), "max") object <- permute_seed_nodes(object, perm_iters = 100L, .verbose = FALSE) object <- community_detection( object, community_params = params_community_detection( min_seed_nodes = 0L, min_nodes = 2L ) ) head(get_results(object))# communities found after a network diffusion set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes(object, c(node_1 = 1, node_3 = 1), "max") object <- permute_seed_nodes(object, perm_iters = 100L, .verbose = FALSE) object <- community_detection( object, community_params = params_community_detection( min_seed_nodes = 0L, min_nodes = 2L ) ) head(get_results(object))
Returns a data.table with available features in the obs table and in the count matrices.
get_sc_available_features(object)get_sc_available_features(object)
object |
|
A data.table with available features.
# what can be queried from the obs table and the counts sc <- demo_single_cells(prepped = FALSE) head(get_sc_available_features(sc), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# what can be queried from the obs table and the counts sc <- demo_single_cells(prepped = FALSE) head(get_sc_available_features(sc), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
One row per cached artefact, saying whether it still agrees with the current
cell set and with the artefacts it was derived from. Artefacts computed
before provenance stamping existed report stamped = FALSE and are never
flagged stale, because nothing is known about them either way.
get_sc_cache_status(object)get_sc_cache_status(object)
object |
|
A data.table with the columns
modality - The modality the artefact lives in.
artefact - One of pca, embedding, knn, snn, magic.
name - The embedding name, NA for the others.
stamped - Whether the artefact carries a provenance stamp.
stale - Whether it disagrees with the current state.
reason - Why it is stale, NA otherwise.
id - The artefact's stamp id.
from - List column of the parent stamp ids.
# what the object holds and whether it still agrees with the cells sc <- demo_single_cells() get_sc_cache_status(sc) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# what the object holds and whether it still agrees with the cells sc <- demo_single_cells() get_sc_cache_status(sc) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter the counts
get_sc_counts( object, assay = c("raw", "norm"), return_format = c("cell", "gene"), cell_indices = NULL, gene_indices = NULL, use_cells_to_keep = TRUE, modality = c("rna", "adt"), .verbose = TRUE )get_sc_counts( object, assay = c("raw", "norm"), return_format = c("cell", "gene"), cell_indices = NULL, gene_indices = NULL, use_cells_to_keep = TRUE, modality = c("rna", "adt"), .verbose = TRUE )
object |
|
assay |
String. Which slot to return. One of |
return_format |
String. One of |
cell_indices |
Optional cell indices. |
gene_indices |
Optional gene indices. |
use_cells_to_keep |
Boolean. Shall cells to keep be found in the class,
shall the counts be reduced to these. Not relevant for |
modality |
String. The modality to return. One of |
.verbose |
Boolean. Controls verbosity of the function. |
The counts table
# raw counts for the first ten genes, cell-centric (CSR) sc <- demo_single_cells(prepped = FALSE) counts <- get_sc_counts(sc, gene_indices = 1:10, .verbose = FALSE) dim(counts) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# raw counts for the first ten genes, cell-centric (CSR) sc <- demo_single_cells(prepped = FALSE) counts <- get_sc_counts(sc, gene_indices = 1:10, .verbose = FALSE) dim(counts) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter the obs table
get_sc_obs(object, indices = NULL, cols = NULL, filtered = FALSE)get_sc_obs(object, indices = NULL, cols = NULL, filtered = FALSE)
object |
|
indices |
Optional integer vector. The integer positions of the cells to return. |
cols |
Optional string vector. The columns from the obs table to return. |
filtered |
Boolean. Whether to return all cells or filtered to |
The obs table
# the obs table, restricted to a few columns sc <- demo_single_cells(prepped = FALSE) head(get_sc_obs(sc, cols = c("cell_id", "cell_grp", "lib_size")), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the obs table, restricted to a few columns sc <- demo_single_cells(prepped = FALSE) head(get_sc_obs(sc, cols = c("cell_id", "cell_grp", "lib_size")), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter the var table
get_sc_var(object, indices = NULL, cols = NULL, modality = c("rna", "adt"))get_sc_var(object, indices = NULL, cols = NULL, modality = c("rna", "adt"))
object |
|
indices |
Optional integer vector. The integer positions of the genes to return. |
cols |
Optional string vector. The columns from the var table to return. |
modality |
String. The modality to return. One of |
The vars table
# the per gene statistics the HVG step wrote sc <- demo_single_cells() head(get_sc_var(sc, cols = c("gene_id", "mean", "var_std")), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the per gene statistics the HVG step wrote sc <- demo_single_cells() head(get_sc_var(sc, cols = c("gene_id", "mean", "var_std")), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Get scores
get_scores(x, ...) ## S3 method for class 'ScDblFinderRes' get_scores(x, ..., score_type = c("weighted", "cxds_scores")) ## S3 method for class 'ScTypeResults' get_scores(x, ...) ## S3 method for class 'ScTypeCellResults' get_scores(x, ...)get_scores(x, ...) ## S3 method for class 'ScDblFinderRes' get_scores(x, ..., score_type = c("weighted", "cxds_scores")) ## S3 method for class 'ScTypeResults' get_scores(x, ...) ## S3 method for class 'ScTypeCellResults' get_scores(x, ...)
x |
An object to get scores from. |
... |
Additional arguments passed to methods. |
score_type |
Either |
The score matrix or data.table held by the object, depending on the
class. Methods exist for ScTypeResults, ScTypeCellResults and
ScDblFinderRes.
A numeric matrix of cells x cell types.
A numeric vector with the winning score per cell.
# the ScType score matrix of cell types by cluster sc <- demo_single_cells() markers <- data.table::data.table( cell_type = rep(sprintf("cell_type_%i", 1:3), each = 10), gene_id = sprintf("gene_%02d", 1:30) ) cell_markers <- prepare_cell_markers(obj = sc, marker_df = markers) res <- calc_sc_type_scores(sc, cell_marker_list = cell_markers, .verbose = FALSE) dim(get_scores(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the ScType score matrix of cell types by cluster sc <- demo_single_cells() markers <- data.table::data.table( cell_type = rep(sprintf("cell_type_%i", 1:3), each = 10), gene_id = sprintf("gene_%02d", 1:30) ) cell_markers <- prepare_cell_markers(obj = sc, marker_df = markers) res <- calc_sc_type_scores(sc, cell_marker_list = cell_markers, .verbose = FALSE) dim(get_scores(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Get the similarity matrix
get_sim_matrix(object, as_data_table = FALSE, .verbose = TRUE)get_sim_matrix(object, as_data_table = FALSE, .verbose = TRUE)
object |
|
as_data_table |
Boolean. Shall the data be returned as a long data.table. |
.verbose |
Boolean. Controls verbosity of the function. |
Returns the semantic similarity data.table from the class
# the Wang similarity matrix back out of the class onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) onto_obj <- OntologySim(onto, .verbose = FALSE) onto_obj <- calculate_wang_sim_onto( onto_obj, weights = c(part_of = 0.8, is_a = 0.6), .verbose = FALSE ) round(get_sim_matrix(onto_obj, .verbose = FALSE), 3)# the Wang similarity matrix back out of the class onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) onto_obj <- OntologySim(onto, .verbose = FALSE) onto_obj <- calculate_wang_sim_onto( onto_obj, weights = c(part_of = 0.8, is_a = 0.6), .verbose = FALSE ) round(get_sim_matrix(onto_obj, .verbose = FALSE), 3)
Get an individual affinity matrix
get_snf_adjcacency_mat(object, name)get_snf_adjcacency_mat(object, name)
object |
The underlying class |
name |
String. The name of the individual data modality affinity matrix to return. |
Returns adjcacency matrix if found.
# affinity matrix of a single modality set.seed(42) continuous <- matrix(rnorm(120), nrow = 12, ncol = 10) rownames(continuous) <- sprintf("sample_%02i", 1:12) colnames(continuous) <- sprintf("feature_%i", 1:10) object <- SimilarityNetworkFusion( data = continuous, data_name = "continuous", snf_params = params_snf(k = 3L) ) dim(get_snf_adjcacency_mat(object, name = "continuous"))# affinity matrix of a single modality set.seed(42) continuous <- matrix(rnorm(120), nrow = 12, ncol = 10) rownames(continuous) <- sprintf("sample_%02i", 1:12) colnames(continuous) <- sprintf("feature_%i", 1:10) object <- SimilarityNetworkFusion( data = continuous, data_name = "continuous", snf_params = params_snf(k = 3L) ) dim(get_snf_adjcacency_mat(object, name = "continuous"))
Get the final SNF matrix
get_snf_final_mat(object)get_snf_final_mat(object)
object |
The underlying class |
Returns the SNF adjacency/similarity matrix.
# fused similarity matrix across two modalities set.seed(42) continuous <- matrix(rnorm(120), nrow = 12, ncol = 10) rownames(continuous) <- sprintf("sample_%02i", 1:12) colnames(continuous) <- sprintf("feature_%i", 1:10) clinical <- data.table::data.table( sample_id = rownames(continuous), sex = factor(sample(c("M", "F"), 12, replace = TRUE)), stage = factor(sample(c("I", "II", "III"), 12, replace = TRUE)) ) object <- SimilarityNetworkFusion( data = continuous, data_name = "continuous", snf_params = params_snf(k = 3L) ) object <- add_snf_data_modality(object, clinical, data_name = "clinical") object <- run_snf(object) dim(get_snf_final_mat(object))# fused similarity matrix across two modalities set.seed(42) continuous <- matrix(rnorm(120), nrow = 12, ncol = 10) rownames(continuous) <- sprintf("sample_%02i", 1:12) colnames(continuous) <- sprintf("feature_%i", 1:10) clinical <- data.table::data.table( sample_id = rownames(continuous), sex = factor(sample(c("M", "F"), 12, replace = TRUE)), stage = factor(sample(c("I", "II", "III"), 12, replace = TRUE)) ) object <- SimilarityNetworkFusion( data = continuous, data_name = "continuous", snf_params = params_snf(k = 3L) ) object <- add_snf_data_modality(object, clinical, data_name = "clinical") object <- run_snf(object) dim(get_snf_final_mat(object))
Get the SNF params
get_snf_params(object)get_snf_params(object)
object |
The underlying class |
Returns the stored SNF params
# the SNF parameters stored in an empty class object <- SimilarityNetworkFusion(snf_params = params_snf(k = 3L)) get_snf_params(object)$k# the SNF parameters stored in an empty class object <- SimilarityNetworkFusion(snf_params = params_snf(k = 3L)) get_snf_params(object)$k
Returns the shared nearest neighbour graph from the object. This function is used for the single cell-related classes and methods.
get_snn_graph(x, ...) ## S7 method for class <bixverse::MetaCells> get_snn_graph(x, ...) ## S3 method for class 'ScCache' get_snn_graph(x, ...) ## S7 method for class <bixverse::SingleCells> get_snn_graph(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_snn_graph(x, ...)get_snn_graph(x, ...) ## S7 method for class <bixverse::MetaCells> get_snn_graph(x, ...) ## S3 method for class 'ScCache' get_snn_graph(x, ...) ## S7 method for class <bixverse::SingleCells> get_snn_graph(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> get_snn_graph(x, ...)
x |
An object to get the sNN graph from. |
... |
Other parameters. |
The igraph that has the shared nearest neighbours.
# the sNN graph the clustering methods run on sc <- demo_single_cells() igraph::vcount(get_snn_graph(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the sNN graph the clustering methods run on sc <- demo_single_cells() igraph::vcount(get_snn_graph(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Returns the mean silhouette of the consensus clusters, the relative
reconstruction errors and the per-component clustering table. A stability
near 1 means every restart found the same programmes; a low one means the
factorisation is not reproducible at this k.
get_stability(x) ## S3 method for class 'ConsensusNmfResult' get_stability(x)get_stability(x) ## S3 method for class 'ConsensusNmfResult' get_stability(x)
x |
An object holding consensus NMF results. |
A list with stability, rel_error, rel_run_errors, clusters,
cluster_sizes, n_dropped and n_empty_clusters.
# how much the restarts agreed on the consensus factors sc <- demo_single_cells() res <- consensus_nmf_sc( sc, k = 5L, n_runs = 5L, nmf_consensus_params = params_nmf_consensus(density_threshold = 2), .verbose = FALSE ) get_stability(res)$stability unlink(sc@dir_data, recursive = TRUE, force = TRUE)# how much the restarts agreed on the consensus factors sc <- demo_single_cells() res <- consensus_nmf_sc( sc, k = 5L, n_runs = 5L, nmf_consensus_params = params_nmf_consensus(density_threshold = 2), .verbose = FALSE ) get_stability(res)$stability unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Getter for the HVG gene names of a Symphony reference
get_symphony_hvg_names(object)get_symphony_hvg_names(object)
object |
|
Character vector of HVG gene names in reference loading order.
# the HVG names in reference loading order ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) head(get_symphony_hvg_names(symphony_ref)) unlink(ref@dir_data, recursive = TRUE, force = TRUE)# the HVG names in reference loading order ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) head(get_symphony_hvg_names(symphony_ref)) unlink(ref@dir_data, recursive = TRUE, force = TRUE)
Getter for the stored labels of a Symphony reference
get_symphony_labels(object)get_symphony_labels(object)
object |
|
A data.table of reference cell labels in z_corr row order, or
NULL if no labels are stored.
# the cell labels snapshotted at build time ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) head(get_symphony_labels(symphony_ref)) unlink(ref@dir_data, recursive = TRUE, force = TRUE)# the cell labels snapshotted at build time ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) head(get_symphony_labels(symphony_ref)) unlink(ref@dir_data, recursive = TRUE, force = TRUE)
Getter for the PCA loadings of a Symphony reference
get_symphony_loadings(object)get_symphony_loadings(object)
object |
|
The PCA gene loadings matrix (n_hvgs x d).
# the PCA gene loadings the query gets projected through ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) dim(get_symphony_loadings(symphony_ref)) unlink(ref@dir_data, recursive = TRUE, force = TRUE)# the PCA gene loadings the query gets projected through ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) dim(get_symphony_loadings(symphony_ref)) unlink(ref@dir_data, recursive = TRUE, force = TRUE)
Getter for the corrected embedding of a Symphony reference
get_symphony_z_corr(object)get_symphony_z_corr(object)
object |
|
The post-Harmony corrected embedding (N x d).
# the Harmony corrected reference embedding ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) dim(get_symphony_z_corr(symphony_ref)) unlink(ref@dir_data, recursive = TRUE, force = TRUE)# the Harmony corrected reference embedding ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) dim(get_symphony_z_corr(symphony_ref)) unlink(ref@dir_data, recursive = TRUE, force = TRUE)
Extract the TF to gene data from the ScenicGrn object
get_tf_to_gene(x) ## S3 method for class 'ScenicGrn' get_tf_to_gene(x)get_tf_to_gene(x) ## S3 method for class 'ScenicGrn' get_tf_to_gene(x)
x |
|
data.table with TF to gene information
# the TF to gene links that survived the importance filter sc <- demo_single_cells() grn <- scenic_grn_sc( sc, tf_ids = sprintf("gene_%02d", 1:5), scenic_params = params_scenic( min_counts = 1L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) grn <- identify_tf_to_genes( grn, method = "top_k", k_tfs = 3L, .verbose = FALSE ) head(get_tf_to_gene(grn)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the TF to gene links that survived the importance filter sc <- demo_single_cells() grn <- scenic_grn_sc( sc, tf_ids = sprintf("gene_%02d", 1:5), scenic_params = params_scenic( min_counts = 1L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) grn <- identify_tf_to_genes( grn, method = "top_k", k_tfs = 3L, .verbose = FALSE ) head(get_tf_to_gene(grn)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
The interpretability surface of a topic model: for each topic, the terms carrying the most probability mass. Note that these are probabilities within a topic, so they are comparable down a topic but not across topics of different breadth.
get_top_terms(x, n = 20L) ## S3 method for class 'LdaResult' get_top_terms(x, n = 20L)get_top_terms(x, n = 20L) ## S3 method for class 'LdaResult' get_top_terms(x, n = 20L)
x |
|
n |
Integer. Number of terms to return per topic. |
A data.table with topic, rank, term and probability, sorted
by topic and then rank.
# the five highest probability terms of each topic set.seed(42L) corpus <- matrix(rbinom(200L * 40L, 1L, 0.05), nrow = 200L, ncol = 40L) corpus[1:100, 1:10] <- rbinom(1000L, 1L, 0.6) corpus[101:200, 11:20] <- rbinom(1000L, 1L, 0.6) colnames(corpus) <- sprintf("term_%02d", 1:40) lda_res <- run_lda(corpus > 0, k = 2L, .verbose = FALSE) get_top_terms(lda_res, n = 5L)# the five highest probability terms of each topic set.seed(42L) corpus <- matrix(rbinom(200L * 40L, 1L, 0.05), nrow = 200L, ncol = 40L) corpus[1:100, 1:10] <- rbinom(1000L, 1L, 0.6) corpus[101:200, 11:20] <- rbinom(1000L, 1L, 0.6) colnames(corpus) <- sprintf("term_%02d", 1:40) lda_res <- run_lda(corpus > 0, k = 2L, .verbose = FALSE) get_top_terms(lda_res, n = 5L)
Getter function to extract the TPM-normalised counts from the
BulkDge() class.
get_tpm_counts(object)get_tpm_counts(object)
object |
|
Returns the TPM-normalised counts. (If found.)
# TPM counts, which normalise_bulk_dge() only computes on request syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) gene_lengths <- stats::setNames( rep(2000, nrow(syn$counts)), rownames(syn$counts) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", calc_tpm = TRUE, gene_lengths = gene_lengths, .verbose = FALSE ) get_tpm_counts(object)[1:3, 1:3]# TPM counts, which normalise_bulk_dge() only computes on request syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) gene_lengths <- stats::setNames( rep(2000, nrow(syn$counts)), rownames(syn$counts) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", calc_tpm = TRUE, gene_lengths = gene_lengths, .verbose = FALSE ) get_tpm_counts(object)[1:3, 1:3]
Get the W (gene loadings) matrix
get_w(x) ## S3 method for class 'NmfResult' get_w(x) ## S3 method for class 'StabilisedNmfResult' get_w(x) ## S3 method for class 'ConsensusNmfResult' get_w(x)get_w(x) ## S3 method for class 'NmfResult' get_w(x) ## S3 method for class 'StabilisedNmfResult' get_w(x) ## S3 method for class 'ConsensusNmfResult' get_w(x)
x |
An object holding NMF results. |
# gene loadings of a five factor NMF sc <- demo_single_cells() res <- nmf_sc(sc, k = 5L, .verbose = FALSE) dim(get_w(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# gene loadings of a five factor NMF sc <- demo_single_cells() res <- nmf_sc(sc, k = 5L, .verbose = FALSE) dim(get_w(res)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This method takes the GeneOntologyElim and a target gene set and performs an GSE enrichment leveraging ontological information. It starts at the lowest levels of the ontology and tests if there is significant enrichment for any GO terms. If the threshold of the p-value is below the elimination threshold, the genes from this term will be removed from all its ancestors. The function then proceeds to the next level of the ontology and repeats the process. The gene universe will be automatically set to every gene represented in the ontology.
gse_go_elim_method( object, target_genes, minimum_overlap = 3L, fdr_threshold = 0.05, elim_threshold = 0.05, min_genes = NULL )gse_go_elim_method( object, target_genes, minimum_overlap = 3L, fdr_threshold = 0.05, elim_threshold = 0.05, min_genes = NULL )
object |
The underlying class, see |
target_genes |
String. The target genes you wish to apply the GSEA over. |
minimum_overlap |
Integer. Threshold for the minimal overlap. |
fdr_threshold |
Float. Threshold for maximum fdr to include in the output. |
elim_threshold |
Float. Threshold from which p-value onwards the elimination on the ancestors shall be conducted. |
min_genes |
Integer. Minimum number of genes that have to be included in
the gene ontology term. If NULL, it will default to the number of minimum
genes stored in |
data.table with enrichment results.
# human GO terms with at least 25 genes go_obj <- GeneOntologyElim( get_go_data_human(.verbose = FALSE), min_genes = 25L ) target_genes <- unique(unlist(S7::prop(go_obj, "go_to_genes")[1:5])) head(gse_go_elim_method(go_obj, target_genes = target_genes), 3)# human GO terms with at least 25 genes go_obj <- GeneOntologyElim( get_go_data_human(.verbose = FALSE), min_genes = 25L ) target_genes <- unique(unlist(S7::prop(go_obj, "go_to_genes")[1:5])) head(gse_go_elim_method(go_obj, target_genes = target_genes), 3)
This method takes the GeneOntologyElim and a list of target gene sets and performs an GSE enrichment leveraging ontological information. It starts at the lowest levels of the ontology and tests if there is significant enrichment for any GO terms. If the threshold of the p-value is below the elimination threshold, the genes from this term will be removed from all its ancestors. The function then proceeds to the next level of the ontology and repeats the process. The class will leverage Rust threading to parallelise the process. The gene universe will be automatically set to every gene represented in the ontology.
gse_go_elim_method_list( object, target_gene_list, minimum_overlap = 3L, fdr_threshold = 0.05, elim_threshold = 0.05, min_genes = NULL )gse_go_elim_method_list( object, target_gene_list, minimum_overlap = 3L, fdr_threshold = 0.05, elim_threshold = 0.05, min_genes = NULL )
object |
The underlying class, see |
target_gene_list |
List. The target genes list you wish to apply the gene set enrichment analysis over. |
minimum_overlap |
Integer. Threshold for the minimal overlap. |
fdr_threshold |
Float. Threshold for maximum fdr to include in the output. |
elim_threshold |
Float. Threshold from which p-value onwards the elimination on the ancestors shall be conducted. |
min_genes |
Integer. Minimum number of genes that have to be included in
the gene ontology term. If NULL, it will default to the number of minimum
genes stored in |
data.table with enrichment results.
# human GO terms with at least 25 genes go_obj <- GeneOntologyElim( get_go_data_human(.verbose = FALSE), min_genes = 25L ) target_genes <- unique(unlist(S7::prop(go_obj, "go_to_genes")[1:5])) res <- gse_go_elim_method_list( go_obj, target_gene_list = list( set_a = target_genes, set_b = rev(target_genes)[1:50] ) ) head(res, 3)# human GO terms with at least 25 genes go_obj <- GeneOntologyElim( get_go_data_human(.verbose = FALSE), min_genes = 25L ) target_genes <- unique(unlist(S7::prop(go_obj, "go_to_genes")[1:5])) res <- gse_go_elim_method_list( go_obj, target_gene_list = list( set_a = target_genes, set_b = rev(target_genes)[1:50] ) ) head(res, 3)
Takes a set of target genes, a list of gene sets and calculates a p-value (hypergeometric test) and odds ratio (OR) against all the gene sets. Also applies a multiple hypothesis correction (BH) to the p-values.
gse_hypergeometric( target_genes, gene_set_list, gene_universe = NULL, threshold = 0.05, minimum_overlap = 3L, .verbose = FALSE )gse_hypergeometric( target_genes, gene_set_list, gene_universe = NULL, threshold = 0.05, minimum_overlap = 3L, .verbose = FALSE )
target_genes |
Character vector. GeneID(s) of the target genes. |
gene_set_list |
Named list of character vectors. Names should represent the gene sets, pathways, and the elements the genes within the respective gene set. |
gene_universe |
Optional character vector. If you would like to specify
specifically the gene universe. If set to NULL, the function will default to
all represented genes in the |
threshold |
Float between 0 and 1 to filter on the fdr. Default: 0.05. If 1 everything is returned. |
minimum_overlap |
Number of minimum overlap between the target genes and the respective gene set. |
.verbose |
Boolean. Controls verbosity of the function. |
data.table with enrichment results.
# hypergeometric test of a target set against a small universe gene_universe <- sprintf("gene_%03i", 1:200) gene_sets <- list( set_a = gene_universe[1:20], set_b = gene_universe[15:40], set_c = gene_universe[100:130] ) target <- gene_universe[c(1:12, 150:158)] gse_hypergeometric(target, gene_sets, gene_universe, threshold = 1)# hypergeometric test of a target set against a small universe gene_universe <- sprintf("gene_%03i", 1:200) gene_sets <- list( set_a = gene_universe[1:20], set_b = gene_universe[15:40], set_c = gene_universe[100:130] ) target <- gene_universe[c(1:12, 150:158)] gse_hypergeometric(target, gene_sets, gene_universe, threshold = 1)
Takes a set of list of target genes, a list of gene sets and calculates a p-value (hypergeometric test) and odds ratio (OR) against all the gene sets. Also applies a multiple hypothesis correction (BH) to the p-values.
gse_hypergeometric_list( target_genes_list, gene_set_list, gene_universe = NULL, threshold = 0.05, minimum_overlap = 3L, .verbose = FALSE )gse_hypergeometric_list( target_genes_list, gene_set_list, gene_universe = NULL, threshold = 0.05, minimum_overlap = 3L, .verbose = FALSE )
target_genes_list |
Named list of character vectors. Names should represent the identifiers of the target genes and the elements the genes. |
gene_set_list |
Named list of character vectors. Names should represent the gene sets, pathways, and the elements the genes within the respective gene set. |
gene_universe |
Optional character vector. If you would like to specify
specifically the gene universe. If set to NULL, the function will default to
all represented genes in the |
threshold |
Float between 0 and 1 to filter on the fdr. Default: 0.05. If NULL everything is returned. |
minimum_overlap |
Number of minimum overlap between the target genes and the respective gene set. |
.verbose |
Boolean. Controls verbosity of the function. |
data.table with enrichment results.
# two target sets tested against the same gene sets in one call gene_universe <- sprintf("gene_%03i", 1:200) gene_sets <- list( set_a = gene_universe[1:20], set_b = gene_universe[100:130] ) targets <- list( hit_a = gene_universe[c(1:12, 150:158)], hit_b = gene_universe[c(100:112, 5:8)] ) gse_hypergeometric_list(targets, gene_sets, gene_universe, threshold = 1)# two target sets tested against the same gene sets in one call gene_universe <- sprintf("gene_%03i", 1:200) gene_sets <- list( set_a = gene_universe[1:20], set_b = gene_universe[100:130] ) targets <- list( hit_a = gene_universe[c(1:12, 150:158)], hit_b = gene_universe[c(100:112, 5:8)] ) gse_hypergeometric_list(targets, gene_sets, gene_universe, threshold = 1)
A version of Harmony by Korsunsky et al., implemented in Rust. Performs
batch correction on PCA embeddings and stores the result as a "harmony"
embedding in the object.
harmony_sc( object, batch_column, additional_batch_columns = NULL, modality = c("rna", "adt"), harmony_params = params_sc_harmony(), seed = 42L, .verbose = TRUE )harmony_sc( object, batch_column, additional_batch_columns = NULL, modality = c("rna", "adt"), harmony_params = params_sc_harmony(), seed = 42L, .verbose = TRUE )
object |
|
batch_column |
String. Column name in the object containing the primary batch labels. |
additional_batch_columns |
Optional character vector. Additional batch
columns to regress out. If |
modality |
String. One of |
harmony_params |
List. Output of |
seed |
Integer. For reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The object with a "harmony" embedding added. If no PCA embeddings
are found, returns the object unchanged with a warning.
# Harmony correction of the PCA embedding sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) sc <- harmony_sc(sc, batch_column = "batch_index", .verbose = FALSE) dim(get_embedding(sc, "harmony")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# Harmony correction of the PCA embedding sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) sc <- harmony_sc(sc, batch_column = "batch_index", .verbose = FALSE) dim(get_embedding(sc, "harmony")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
A version of Harmony v2 by Patikas et al., 2026, implemented in Rust.
Performs batch correction on PCA embeddings and stores the result as a
"harmony_v2" embedding in the object.
harmony_v2_sc( object, batch_column, additional_batch_columns = NULL, modality = c("rna", "adt"), harmony_params = params_sc_harmony_v2(), seed = 42L, .verbose = TRUE )harmony_v2_sc( object, batch_column, additional_batch_columns = NULL, modality = c("rna", "adt"), harmony_params = params_sc_harmony_v2(), seed = 42L, .verbose = TRUE )
object |
|
batch_column |
String. Column name in the object containing the primary batch labels. |
additional_batch_columns |
Optional character vector. Additional batch
columns to regress out. If |
modality |
String. One of |
harmony_params |
List. Output of |
seed |
Integer. For reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The object with a "harmony_v2" embedding added. If no PCA
embeddings are found, returns the object unchanged with a warning.
# the reimplemented Harmony, writing its own embedding sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) sc <- harmony_v2_sc( sc, batch_column = "batch_index", .verbose = FALSE ) dim(get_embedding(sc, "harmony_v2")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the reimplemented Harmony, writing its own embedding sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) sc <- harmony_v2_sc( sc, batch_column = "batch_index", .verbose = FALSE ) dim(get_embedding(sc, "harmony_v2")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Calculate the effect size
hedges_g_dge( meta_data, main_contrast, normalised_counts, contrast_list = NULL, .verbose = TRUE )hedges_g_dge( meta_data, main_contrast, normalised_counts, contrast_list = NULL, .verbose = TRUE )
meta_data |
data.table. The meta information about the experiment in which the contrast info can be found. |
main_contrast |
String. Which column contains the main groups you want
to calculate the Hedge's G effect for. Every permutation of the groups
will be tested if |
normalised_counts |
Numeric Matrix. The normalised count matrix. |
contrast_list |
String vector or NULL. Optional string vector of
contrast formatted as |
.verbose |
Boolean. Controls the verbosity of the function. |
A data.table with the effect sizes and standard errors based on the Hedge's G effect size for the groups.
# Hedge's G on log CPM counts for the case vs control contrast syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) norm_counts <- rs_cpm(syn$counts, lib_size = NULL, log = TRUE, prior_count = 2) res <- hedges_g_dge( meta_data = meta, main_contrast = "case_control", normalised_counts = norm_counts, .verbose = FALSE ) head(res)# Hedge's G on log CPM counts for the case vs control contrast syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) norm_counts <- rs_cpm(syn$counts, lib_size = NULL, log = TRUE, prior_count = 2) res <- hedges_g_dge( meta_data = meta, main_contrast = "case_control", normalised_counts = norm_counts, .verbose = FALSE ) head(res)
This method implements the HotSpot approach (see DeTomaso, et al.) to calculate the auto-correlation of a given gene in the kNN graph based on the chosen embedding. This can be used to identify genes that have strong local correlations and vary across the kNN graph.
hotspot_autocor_sc( object, embd_to_use = "pca", use_knn = TRUE, hotspot_params = params_sc_hotspot(), no_embd_to_use = NULL, cells_to_take = NULL, genes_to_take = NULL, streaming = NULL, random_seed = 42L, .verbose = TRUE )hotspot_autocor_sc( object, embd_to_use = "pca", use_knn = TRUE, hotspot_params = params_sc_hotspot(), no_embd_to_use = NULL, cells_to_take = NULL, genes_to_take = NULL, streaming = NULL, random_seed = 42L, .verbose = TRUE )
object |
|
embd_to_use |
String. The embedding to use. Defaults to |
use_knn |
Boolean. Shall the internal kNN be used. If set to yes, you
need to ensure consistency. If you provide |
hotspot_params |
List with hotspot parameters, see
|
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
cells_to_take |
Optional string vector. If you want to only use
selected cells. If |
genes_to_take |
Optional string vector. If you wish to limit the
search to a subset of genes. If |
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
random_seed |
Integer. Used for reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
Should a gene not be found in sufficient cells, the gene will be automatically filtered out from the results. This can occur for example if you have filtered out the cells that contain a given gene. The underlying genes are still available, but the cells that might contain them are not included.
Whether the neighbour distances need squaring before the kernel sees them
follows from the metric. With use_knn = TRUE it is taken from the metric
stored on the cached kNN graph, otherwise from ann_dist in
hotspot_params.
A data.table with the auto-correlations on a per gene basis and various statistics.
# local auto-correlation of every gene on the cached kNN graph sc <- demo_single_cells() res <- hotspot_autocor_sc(sc, .verbose = FALSE) head(res, 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# local auto-correlation of every gene on the cached kNN graph sc <- demo_single_cells() res <- hotspot_autocor_sc(sc, .verbose = FALSE) head(res, 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This method implements the HotSpot approach (see DeTomaso, et al.) to calculate the local gene-gene correlations and their Z-scores.
hotspot_gene_cor_sc( object, embd_to_use = "pca", use_knn = TRUE, hotspot_params = params_sc_hotspot(), no_embd_to_use = NULL, cells_to_take = NULL, genes_to_take = NULL, streaming = NULL, working_mem_gb = 4, random_seed = 42L, .verbose = TRUE )hotspot_gene_cor_sc( object, embd_to_use = "pca", use_knn = TRUE, hotspot_params = params_sc_hotspot(), no_embd_to_use = NULL, cells_to_take = NULL, genes_to_take = NULL, streaming = NULL, working_mem_gb = 4, random_seed = 42L, .verbose = TRUE )
object |
|
embd_to_use |
String. The embedding to use. Defaults to |
use_knn |
Boolean. Shall the internal kNN be used. If set to yes, you
need to ensure consistency. If you provide |
hotspot_params |
List with hotspot parameters, see
|
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
cells_to_take |
Optional string vector. If you want to only use
selected cells. If |
genes_to_take |
Optional string vector. If you wish to limit the
search to a subset of genes. If |
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
working_mem_gb |
Numeric. Approximate working memory (GB) the streaming
pair path may use for resident gene panels. Ignored when |
random_seed |
Integer. Used for reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
Should a gene not be found in sufficient cells, the pairs with this gene will be set to 0. Please ensure prior to running the function that you are only calculating gene-gene auto-correlations that occur in sufficient cells.
Whether the neighbour distances need squaring before the kernel sees them
follows from the metric. With use_knn = TRUE it is taken from the metric
stored on the cached kNN graph, otherwise from ann_dist in
hotspot_params.
A sc_hotspot class that can be used for subsequent analysis.
# local gene-gene correlations over a subset of the genes sc <- demo_single_cells() res <- hotspot_gene_cor_sc( sc, genes_to_take = get_gene_names(sc)[1:20], .verbose = FALSE ) res unlink(sc@dir_data, recursive = TRUE, force = TRUE)# local gene-gene correlations over a subset of the genes sc <- demo_single_cells() res <- hotspot_gene_cor_sc( sc, genes_to_take = get_gene_names(sc)[1:20], .verbose = FALSE ) res unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function allows to iterate over a vector of ncomp to identify which ncomp parameter to choose for your data set. The idea is to generate stability profiles over the different ncomps and identify a 'sweet spot' of good stability, low redundancy (maximum absolute correlation between the component loadings) and good convergence of the identified independent components
ica_evaluate_comp( object, ica_type = c("logcosh", "exp"), iter_params = params_ica_randomisation(), ncomp_params = params_ica_ncomp(), ica_params = params_ica_general(), random_seed = 42L, .verbose = TRUE )ica_evaluate_comp( object, ica_type = c("logcosh", "exp"), iter_params = params_ica_randomisation(), ncomp_params = params_ica_ncomp(), ica_params = params_ica_general(), random_seed = 42L, .verbose = TRUE )
object |
The class, see |
ica_type |
String, element of |
iter_params |
List. This list controls the randomisation parameters for
the ICA runs, see
|
ncomp_params |
List. Parameters for the ncomp to iterate through, see
|
ica_params |
List. The ICA parameters, see
|
random_seed |
Integer. For reproducibility. |
.verbose |
Boolean. Controls verbosity. |
BulkCoExp with the added information of stability of the components
and other data to plot to choose the right ncomp.
# stability across a small grid of component counts mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) obj <- ica_evaluate_comp( obj, ica_type = "logcosh", ncomp_params = params_ica_ncomp(custom_seq = seq(2L, 20L, by = 2L)), .verbose = FALSE ) head(get_ica_stability_res(obj))# stability across a small grid of component counts mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) obj <- ica_evaluate_comp( obj, ica_type = "logcosh", ncomp_params = params_ica_ncomp(custom_seq = seq(2L, 20L, by = 2L)), .verbose = FALSE ) head(get_ica_stability_res(obj))
This function can be used after having run ica_evaluate_comp().
It will calculate the inflection point, based on the first derivative of a
loess function fitted ncomp ~ combined_score with the combined score being
a product of the median stability, 1 - max_abs_loading_cor and proportion
of convergence
and add these info to the object. Should the loess function raise a warning
(e.g., singularity), the class will be returned as is and manual
determination of optimal ncomp is warranted. Additionally, you have the
option to plot the loess function for additional control over the span
parameter (defaults to TRUE).
ica_optimal_ncomp(object, span = 0.2, show_plot = TRUE, .verbose = TRUE)ica_optimal_ncomp(object, span = 0.2, show_plot = TRUE, .verbose = TRUE)
object |
The class, see |
span |
Float. A value between 0.1 and 1.0 for the loess function
defining the span. Defaults to |
show_plot |
Boolean. Shall a plot be shown of the loess function fitted.
Defaults to |
.verbose |
Boolean. Controls the verbosity of the function. |
BulkCoExp with optimal ncomp based on the inflection point method.
# inflection point of the combined score mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) obj <- ica_evaluate_comp( obj, ica_type = "logcosh", ncomp_params = params_ica_ncomp(custom_seq = seq(2L, 20L, by = 2L)), .verbose = FALSE ) obj <- ica_optimal_ncomp( obj, span = 0.4, show_plot = FALSE, .verbose = FALSE ) obj@params$ica_stability_assessment$optimal_ncomp# inflection point of the combined score mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) obj <- ica_evaluate_comp( obj, ica_type = "logcosh", ncomp_params = params_ica_ncomp(custom_seq = seq(2L, 20L, by = 2L)), .verbose = FALSE ) obj <- ica_optimal_ncomp( obj, span = 0.4, show_plot = FALSE, .verbose = FALSE ) obj@params$ica_stability_assessment$optimal_ncomp
This is the generic function for doing the necessary preprocessing for running independent component analysis.
ica_processing(object, fast_svd = TRUE, random_seed = 123L, .verbose = TRUE)ica_processing(object, fast_svd = TRUE, random_seed = 123L, .verbose = TRUE)
object |
The class, see |
fast_svd |
Boolean. Shall randomised SVD be used for the whitening. This is faster and usually causes little precision loss. |
random_seed |
Integer. Seed for the randomised SVD. Only relevant, if
fast_svd = |
.verbose |
Boolean. Controls verbosity of the function. |
BulkCoExp with the needed data for ICA in the
properties of the class.
# whitening ahead of the ICA runs mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) dim(obj@processed_data$K)# whitening ahead of the ICA runs mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) dim(obj@processed_data$K)
This function runs stabilised ICA with the defined number of components.
ica_stabilised_results( object, no_comp = NULL, ica_type = c("logcosh", "exp"), iter_params = params_ica_randomisation(), ica_params = params_ica_general(), membership_params = params_module_membership(), random_seed = 42L, consistent_sign = TRUE, .verbose = TRUE )ica_stabilised_results( object, no_comp = NULL, ica_type = c("logcosh", "exp"), iter_params = params_ica_randomisation(), ica_params = params_ica_general(), membership_params = params_module_membership(), random_seed = 42L, consistent_sign = TRUE, .verbose = TRUE )
object |
The class, see |
no_comp |
Optional integer. Number of components you wish to use for the
ICA run. If you have run |
ica_type |
String, element of |
iter_params |
List. This list controls the randomisation parameters for
the ICA runs, see
|
ica_params |
List. The ICA parameters, see
|
membership_params |
List. Controls how the component loadings are turned
into module membership, see |
random_seed |
Integer. For reproducibility. |
consistent_sign |
Boolean. If set to |
.verbose |
Boolean. Controls verbosity. |
BulkCoExp with the the source matrix S, mixing matrix A and other
parameters added to the slots. The diagnostics carry
loading_condition_number (condition number of S S') and
max_abs_loading_cor (largest absolute correlation between two component
loadings). High values mean near-duplicate components and an unstable
sample activity A; reduce no_comp.
# stabilised five-component ICA and the resulting modules mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) obj <- ica_stabilised_results( obj, no_comp = 5L, ica_type = "logcosh", .verbose = FALSE ) head(get_modules(get_results(obj)))# stabilised five-component ICA and the resulting modules mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) obj <- ica_stabilised_results( obj, no_comp = 5L, ica_type = "logcosh", .verbose = FALSE ) head(get_modules(get_results(obj)))
Generates the TF to gene associations from the importance matrix produced by the tree-based regression models. Two filtering strategies are available:
Threshold (method = "threshold"): For each gene, computes
mean + n_sd * SD of the importance scores across all TFs and retains
only pairs exceeding that threshold. This adapts to the per-gene
importance distribution and is less sensitive to differences between
learners.
Top-k (method = "top_k"): Selects the top k_tfs TFs per
gene and/or the top k_genes genes per TF. Both margins can be combined
(union). At least one of k_tfs or k_genes must be provided.
Both methods accept an optional min_importance floor.
identify_tf_to_genes( x, method = c("threshold", "top_k"), k_tfs = NULL, k_genes = NULL, n_sd = 2, min_importance = NULL, .verbose = TRUE ) ## S3 method for class 'ScenicGrn' identify_tf_to_genes( x, method = c("threshold", "top_k"), k_tfs = NULL, k_genes = NULL, n_sd = 2, min_importance = NULL, .verbose = TRUE )identify_tf_to_genes( x, method = c("threshold", "top_k"), k_tfs = NULL, k_genes = NULL, n_sd = 2, min_importance = NULL, .verbose = TRUE ) ## S3 method for class 'ScenicGrn' identify_tf_to_genes( x, method = c("threshold", "top_k"), k_tfs = NULL, k_genes = NULL, n_sd = 2, min_importance = NULL, .verbose = TRUE )
x |
|
method |
Character. Either |
k_tfs |
Optional integer. Top TFs per gene (only used when
|
k_genes |
Optional integer. Top genes per TF (only used when
|
n_sd |
Numeric. Number of standard deviations above the per-gene mean
to use as the threshold (only used when |
min_importance |
Optional numeric in [0, 1]. Absolute minimum importance score for inclusion. |
.verbose |
Boolean. Controls verbosity of the function. |
The ScenicGrn object with a tf_to_gene_results data.table
added.
# keep the three strongest TFs per gene sc <- demo_single_cells() grn <- scenic_grn_sc( sc, tf_ids = sprintf("gene_%02d", 1:5), scenic_params = params_scenic( min_counts = 1L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) grn <- identify_tf_to_genes( grn, method = "top_k", k_tfs = 3L, .verbose = FALSE ) head(get_tf_to_gene(grn)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# keep the three strongest TFs per gene sc <- demo_single_cells() grn <- scenic_grn_sc( sc, tf_ids = sprintf("gene_%02d", 1:5), scenic_params = params_scenic( min_counts = 1L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) grn <- identify_tf_to_genes( grn, method = "top_k", k_tfs = 3L, .verbose = FALSE ) head(get_tf_to_gene(grn)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
bixverse ships a skill that teaches coding agents how to use the package:
the workflows, the params_*() convention, the ordering constraints and the
traps. This copies it out of the installed package to where your agent looks
for it.
The skill is versioned with the package, so re-run this after upgrading bixverse to keep the agent in sync with the code.
install_agent_skill( dest = NULL, agent = c("claude", "codex", "generic"), overwrite = FALSE )install_agent_skill( dest = NULL, agent = c("claude", "codex", "generic"), overwrite = FALSE )
dest |
String or |
agent |
String. One of |
overwrite |
Boolean. Replace an existing installation. Defaults to
|
The reference files are plain markdown and work anywhere. Only the entry file differs between agents:
"claude" writes SKILL.md with the YAML frontmatter Claude Code
uses to decide when to load the skill. Discovery is automatic.
"codex" and "generic" write AGENTS.md with the frontmatter
stripped, since no other agent reads it. There is no auto-discovery, so
point the agent at the directory yourself.
The path the skill was written to, invisibly.
## Not run: # Claude Code, into ~/.claude/skills, auto-discovered install_agent_skill() # Codex, into ~/.codex/skills as AGENTS.md install_agent_skill(agent = "codex") # anywhere else install_agent_skill(dest = "~/my-agent/context", agent = "generic") # refresh after a package upgrade install_agent_skill(overwrite = TRUE) ## End(Not run)## Not run: # Claude Code, into ~/.claude/skills, auto-discovered install_agent_skill() # Codex, into ~/.codex/skills as AGENTS.md install_agent_skill(agent = "codex") # anywhere else install_agent_skill(dest = "~/my-agent/context", agent = "generic") # refresh after a package upgrade install_agent_skill(overwrite = TRUE) ## End(Not run)
In case of a kNN graph with a subset of nodes having labels, this function can be used to propagate the labels through the graph. This function uses the label spreading version of the algorithm.
knn_graph_label_propagation( from, to, labels, weights = NULL, label_prop_params = params_label_propagation() )knn_graph_label_propagation( from, to, labels, weights = NULL, label_prop_params = params_label_propagation() )
from |
Integer vector. Source node indices for each edge. |
to |
Integer vector. Target node indices for each edge. Must be the
same length as |
labels |
Character vector. The labels with |
weights |
Optional numeric vector. The weights between the nodes. Must
be the same length as |
label_prop_params |
List. Named list of label propagation parameters as
generated by |
A list with the following elements:
assignment_probs - Matrix with the assignment probabilities.
final_labels - Final labels in the graph.
# spread two labels through a five node kNN graph from <- as.integer(c(1, 1, 2, 3, 4)) to <- as.integer(c(2, 3, 3, 4, 5)) labels <- c("A", NA, "B", NA, NA) res <- knn_graph_label_propagation(from = from, to = to, labels = labels) res$final_labels# spread two labels through a five node kNN graph from <- as.integer(c(1, 1, 2, 3, 4)) to <- as.integer(c(2, 3, 3, 4, 5)) labels <- c("A", NA, "B", NA, NA) res <- knn_graph_label_propagation(from = from, to = to, labels = labels) res$final_labels
Fits one model per requested topic count and scores each with three model
selection metrics, then combines them into a single rescaled score. Use it
to pick k, then pull the winning model out with
get_best_model().
lda_k_sweep( x, k_range, lda_params = params_lda(), top_topics_coh = 5L, seed = 42L, .verbose = TRUE )lda_k_sweep( x, k_range, lda_params = params_lda(), top_topics_coh = 5L, seed = 42L, .verbose = TRUE )
x |
Documents x terms matrix. Either a |
k_range |
Integer vector. The topic counts to evaluate. Every entry must be at least 2. |
lda_params |
List, see |
top_topics_coh |
Integer. Number of top-scoring topics averaged into the reported coherence. |
seed |
Integer. Seed for the variational initialisation. |
.verbose |
Boolean or integer. Verbosity. |
The three metrics disagree by design and are meant to be looked at together.
Arun is a symmetric KL between the singular values of the topic-term matrix
and the document length distribution (lower is better). Cao Juan is the mean
pairwise cosine similarity between topics, so it punishes a k that has
started splitting one signal in two (lower is better). Mimno is UMass
coherence (higher is better), averaged over the top-scoring topics only,
because coherence over all topics is dragged down by the ones that never
specialised. combined_score is the min-max rescaled compromise, and
best_k is its argmax.
One thing to know before reading best_k: any k below five is struck out
of the selection entirely, because coherence saturates on small topic counts
and would otherwise always win. That is upstream behaviour, inherited from
pycisTopic. So best_k can never come back below five, however good the raw
metrics of a smaller k look, and a sweep over 2:6 is really a choice
between five and six. The excluded rows carry NA in combined_score, keep
their metrics, and trigger a warning. If you suspect a handful of topics,
read arun_2010 and cao_juan_2009 off the table yourself and pass the k
you want to get_best_model().
Cost is one full fit per k. The corpus is built once and shared, and each
fit is already parallel over documents, so the topic counts run sequentially
rather than nesting thread pools.
An LdaKSweepResult, which is a data.table with one row per k.
Arun, et al., PAKDD, 2010; Cao, et al., Neurocomputing, 2009; Mimno, et al., EMNLP, 2011
# small sweep above the coherence topic count floor set.seed(42L) corpus <- matrix(rbinom(200L * 40L, 1L, 0.05), nrow = 200L, ncol = 40L) corpus[1:100, 1:10] <- rbinom(1000L, 1L, 0.6) corpus[101:200, 11:20] <- rbinom(1000L, 1L, 0.6) colnames(corpus) <- sprintf("term_%02d", 1:40) sweep_res <- lda_k_sweep(corpus > 0, k_range = 5:7, .verbose = FALSE) sweep_res# small sweep above the coherence topic count floor set.seed(42L) corpus <- matrix(rbinom(200L * 40L, 1L, 0.05), nrow = 200L, ncol = 40L) corpus[1:100, 1:10] <- rbinom(1000L, 1L, 0.6) corpus[101:200, 11:20] <- rbinom(1000L, 1L, 0.6) colnames(corpus) <- sprintf("term_%02d", 1:40) sweep_res <- lda_k_sweep(corpus > 0, k_range = 5:7, .verbose = FALSE) sweep_res
Modularity-based Leiden (Traag et al.) or Louvain (Blondel et al.) community
detection, run in Rust. Mirrors
igraph::cluster_leiden(objective_function = "modularity"), but the result
depends on seed only (not on the global RNG or the number of threads).
Communities are numbered by size, 1 being the largest.
leiden_clustering( graph, resolution = 1, algorithm = c("leiden", "louvain"), beta = 0.01, n_iterations = 2L, seed = 42L )leiden_clustering( graph, resolution = 1, algorithm = c("leiden", "louvain"), beta = 0.01, n_iterations = 2L, seed = 42L )
graph |
igraph. Undirected. If it has an edge attribute |
resolution |
Numeric. Modularity resolution parameter. Higher values give more communities. |
algorithm |
String. One of |
beta |
Numeric. Randomness of the Leiden refinement merges. Ignored for Louvain. |
n_iterations |
Integer. Number of full iterations, each starting from the previous partition. |
seed |
Integer. Random seed. |
Integer vector with the community of each node (1-based, 1 is
the largest community).
Traag, Waltman & van Eck, Sci Rep, 2019; Blondel, et al., J Stat Mech, 2008
g <- igraph::make_graph("Zachary") table(leiden_clustering(g))g <- igraph::make_graph("Zachary") table(leiden_clustering(g))
For each gene set, ranks ligands by how well their influence vector aligns with set membership across a background. Returns AUROC, AUPR, AUPR corrected against the random baseline, Pearson, and Spearman per ligand and per gene set.
The influence matrix is restricted to background columns before
scoring, so AUROC / AUPR reflect ranking within the background (typical
NicheNet practice: background = genes expressed in the receiver cells,
gene set = the DEGs).
ligand_activity_scores(ligand_influence, gene_sets, background = NULL)ligand_activity_scores(ligand_influence, gene_sets, background = NULL)
ligand_influence |
A |
gene_sets |
Either a character vector (one gene set) or a list of character vectors. Names of the list are propagated to the output. |
background |
Character vector or |
A data.table with one row per (gene set, ligand) pair and
columns gene_set, ligand, auroc, aupr, aupr_corrected,
pearson, spearman.
# rank the ligands against the TF1 target set ppi <- data.table::data.table( from = c("L1", "SIG1", "L2", "SIG2"), to = c("SIG1", "TF1", "SIG2", "TF2"), weight = 1.0 ) grn <- data.table::data.table( from = rep(c("TF1", "TF2"), each = 3), to = c("G1", "G2", "G3", "G4", "G5", "G6"), weight = 1.0 ) inf <- generate_ligand_target_influence( ligand_seeds = list(L1 = "L1", L2 = "L2"), ppi_network = ppi, grn_network = grn, params = params_ligand_target(ltf_cutoff = 0) ) ligand_activity_scores( ligand_influence = inf, gene_sets = list(set_A = c("G1", "G2", "G3")) )# rank the ligands against the TF1 target set ppi <- data.table::data.table( from = c("L1", "SIG1", "L2", "SIG2"), to = c("SIG1", "TF1", "SIG2", "TF2"), weight = 1.0 ) grn <- data.table::data.table( from = rep(c("TF1", "TF2"), each = 3), to = c("G1", "G2", "G3", "G4", "G5", "G6"), weight = 1.0 ) inf <- generate_ligand_target_influence( ligand_seeds = list(L1 = "L1", L2 = "L2"), ppi_network = ppi, grn_network = grn, params = params_ligand_target(ltf_cutoff = 0) ) ligand_activity_scores( ligand_influence = inf, gene_sets = list(set_A = c("G1", "G2", "G3")) )
Helper function that can load the parameters to access the on-disk stored data into the class.
load_existing(object, .verbose = TRUE)load_existing(object, .verbose = TRUE)
object |
|
.verbose |
Boolean. Controls verbosity of the function. |
The object with added information on the data on disk.
# a fresh handle over a directory written earlier sc <- demo_single_cells(prepped = FALSE) dir <- sc@dir_data sc <- load_existing(SingleCells(dir_data = dir), .verbose = FALSE) dim(sc) unlink(dir, recursive = TRUE, force = TRUE)# a fresh handle over a directory written earlier sc <- demo_single_cells(prepped = FALSE) dir <- sc@dir_data sc <- load_existing(SingleCells(dir_data = dir), .verbose = FALSE) dim(sc) unlink(dir, recursive = TRUE, force = TRUE)
This function loads in gene ontology data stored in the package. This is for humans only.
load_go_human_data()load_go_human_data()
A list containing:
go_info - data.table. The gene ontology identifier, name and namespace can be found in this one
gene_ontology - data.table. The relationships between different gene ontology terms.
go_to_genes - data.table. The gene ontology term to gene (ensembl id) relationships.
# the human gene ontology tables shipped with the package go_data <- load_go_human_data() names(go_data) head(go_data$go_info)# the human gene ontology tables shipped with the package go_data <- load_go_human_data() names(go_data) head(go_data$go_info)
SingleCells
This function takes an h5ad file and loads the obs and var data into the
DuckDB of the SingleCells class and the counts into a Rust-binarised
format for rapid access. During the reading in of the counts, the log CPM
transformation will occur automatically.
load_h5ad( object, h5_path, sc_qc_param = params_sc_min_quality(), raw_count_slot = c("auto", "X", "raw.X", "layers.counts"), cell_id_col = NULL, h5ad_streaming = TRUE, csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )load_h5ad( object, h5_path, sc_qc_param = params_sc_min_quality(), raw_count_slot = c("auto", "X", "raw.X", "layers.counts"), cell_id_col = NULL, h5ad_streaming = TRUE, csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
object |
|
h5_path |
File path to the h5ad object you wish to load in. |
sc_qc_param |
List. Output of
|
raw_count_slot |
Where raw counts live. |
cell_id_col |
Optional string. If a specific column in the h5ad obs data is representing the cell identifiers, you can specify it here. |
h5ad_streaming |
Boolean. Stream the h5ad counts into the cell-based
binary in batches instead of materialising the filtered matrix first.
Recommended for large files. Defaults to |
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean. Controls the verbosity of the function. |
It will populate the files on disk and return the class with updated shape information.
# round trip through a sparse h5ad file data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, data$counts, data$obs, data$var, .verbose = FALSE) dir_data <- tempfile("sc_h5ad") dir.create(dir_data, recursive = TRUE) sc <- load_h5ad( object = SingleCells(dir_data = dir_data), h5_path = f_path, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(c(f_path, dir_data), recursive = TRUE, force = TRUE)# round trip through a sparse h5ad file data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, data$counts, data$obs, data$var, .verbose = FALSE) dir_data <- tempfile("sc_h5ad") dir.create(dir_data, recursive = TRUE) sc <- load_h5ad( object = SingleCells(dir_data = dir_data), h5_path = f_path, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(c(f_path, dir_data), recursive = TRUE, force = TRUE)
SingleCells
This function takes an h5ad file where only normalised counts are available
in the X slot and loads the obs and var data into the DuckDB of the
SingleCells class and the counts into a Rust-binarised format for rapid
access. Raw counts are reconstructed from the normalised values using the
library sizes stored in a specified obs column.
The reconstruction assumes the normalisation was:
norm = log1p(x / lib_size * target_size)
load_h5ad_norm( object, h5_path, obs_lib_size_col, target_size, sc_qc_param = params_sc_min_quality(), cell_id_col = NULL, csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )load_h5ad_norm( object, h5_path, obs_lib_size_col, target_size, sc_qc_param = params_sc_min_quality(), cell_id_col = NULL, csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
object |
|
h5_path |
File path to the h5ad object you wish to load in. |
obs_lib_size_col |
String. Name of the obs column containing the total
counts per cell or spot (e.g. |
target_size |
Numeric. The target size used in the original
normalisation (e.g. |
sc_qc_param |
List. Output of |
cell_id_col |
Optional string. If a specific column in the h5ad obs data represents the cell identifiers, you can specify it here. |
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean. |
It will populate the files on disk and return the class with updated shape information.
# an h5ad holding log1p(x / lib_size * 1e4), raw counts reconstructed on read data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) lib_size <- Matrix::rowSums(data$counts) norm_counts <- data$counts norm_counts@x <- log1p( norm_counts@x / rep(lib_size, diff(norm_counts@p)) * 1e4 ) obs <- data.table::copy(data$obs)[, total_counts := lib_size] f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, norm_counts, obs, data$var, .verbose = FALSE) dir_data <- tempfile("sc_h5ad_norm") dir.create(dir_data, recursive = TRUE) sc <- load_h5ad_norm( object = SingleCells(dir_data = dir_data), h5_path = f_path, obs_lib_size_col = "total_counts", target_size = 1e4, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(c(f_path, dir_data), recursive = TRUE, force = TRUE)# an h5ad holding log1p(x / lib_size * 1e4), raw counts reconstructed on read data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) lib_size <- Matrix::rowSums(data$counts) norm_counts <- data$counts norm_counts@x <- log1p( norm_counts@x / rep(lib_size, diff(norm_counts@p)) * 1e4 ) obs <- data.table::copy(data$obs)[, total_counts := lib_size] f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, norm_counts, obs, data$var, .verbose = FALSE) dir_data <- tempfile("sc_h5ad_norm") dir.create(dir_data, recursive = TRUE) sc <- load_h5ad_norm( object = SingleCells(dir_data = dir_data), h5_path = f_path, obs_lib_size_col = "total_counts", target_size = 1e4, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(c(f_path, dir_data), recursive = TRUE, force = TRUE)
SingleCells
This is a helper function to load in mtx files and corresponding plain text files. It will automatically filter out low quality cells and only keep high quality cells. Under the hood DucKDB and high performance Rust binary files are being used to store the counts.
load_mtx( object, sc_mtx_io_param = params_sc_mtx_io(), sc_qc_param = params_sc_min_quality(), mtx_streaming = TRUE, csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )load_mtx( object, sc_mtx_io_param = params_sc_mtx_io(), sc_qc_param = params_sc_min_quality(), mtx_streaming = TRUE, csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
object |
|
sc_mtx_io_param |
List. Please generate this one via
|
sc_qc_param |
List. Output of |
mtx_streaming |
Boolean. Shall the .mtx file ingestion itself be
streamed (via temp-file bucketing). Recommended for large mtx files.
Defaults to |
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean. |
The class with updated shape information.
# read back a CellRanger style .mtx trio data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) dir_src <- tempfile("cellranger") dir.create(dir_src, recursive = TRUE) write_cellranger_output( dir_src, data$counts, data$obs, data$var, rows = "cells", format_type = "csv", .verbose = FALSE ) dir_data <- tempfile("sc_mtx") dir.create(dir_data, recursive = TRUE) sc <- load_mtx( object = SingleCells(dir_data = dir_data), sc_mtx_io_param = params_sc_mtx_io( path_mtx = file.path(dir_src, "matrix.mtx"), path_obs = file.path(dir_src, "barcodes.csv"), path_var = file.path(dir_src, "features.csv"), cells_as_rows = TRUE, has_hdr = TRUE ), sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(c(dir_src, dir_data), recursive = TRUE, force = TRUE)# read back a CellRanger style .mtx trio data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) dir_src <- tempfile("cellranger") dir.create(dir_src, recursive = TRUE) write_cellranger_output( dir_src, data$counts, data$obs, data$var, rows = "cells", format_type = "csv", .verbose = FALSE ) dir_data <- tempfile("sc_mtx") dir.create(dir_data, recursive = TRUE) sc <- load_mtx( object = SingleCells(dir_data = dir_data), sc_mtx_io_param = params_sc_mtx_io( path_mtx = file.path(dir_src, "matrix.mtx"), path_obs = file.path(dir_src, "barcodes.csv"), path_var = file.path(dir_src, "features.csv"), cells_as_rows = TRUE, has_hdr = TRUE ), sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(c(dir_src, dir_data), recursive = TRUE, force = TRUE)
SingleCells
Takes a pre-scan result from prescan_h5ad_files() and loads
all files into a single experiment with global gene QC and sequential
cell indexing.
load_multi_h5ad( object, prescan_result, sc_qc_param = params_sc_min_quality(), cell_id_col = NULL, csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )load_multi_h5ad( object, prescan_result, sc_qc_param = params_sc_min_quality(), cell_id_col = NULL, csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
object |
|
prescan_result |
Output of |
sc_qc_param |
List. Output of |
cell_id_col |
Optional string. Column name for cell identifiers in obs. |
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean. |
The class with updated shape and populated DuckDB.
# two files into one experiment, cells tagged by exp_id files <- c(a = tempfile(fileext = ".h5ad"), b = tempfile(fileext = ".h5ad")) for (i in seq_along(files)) { data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data( n_cells = 200L, n_genes = 40L ), seed = i ) write_h5ad_sc(files[i], data$counts, data$obs, data$var, .verbose = FALSE) } tasks <- prescan_h5ad_files(h5_paths = files, .verbose = FALSE) dir_data <- tempfile("sc_multi_h5ad") dir.create(dir_data, recursive = TRUE) sc <- load_multi_h5ad( object = SingleCells(dir_data = dir_data), prescan_result = tasks, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) table(sc[["exp_id"]]) unlink(c(files, dir_data), recursive = TRUE, force = TRUE)# two files into one experiment, cells tagged by exp_id files <- c(a = tempfile(fileext = ".h5ad"), b = tempfile(fileext = ".h5ad")) for (i in seq_along(files)) { data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data( n_cells = 200L, n_genes = 40L ), seed = i ) write_h5ad_sc(files[i], data$counts, data$obs, data$var, .verbose = FALSE) } tasks <- prescan_h5ad_files(h5_paths = files, .verbose = FALSE) dir_data <- tempfile("sc_multi_h5ad") dir.create(dir_data, recursive = TRUE) sc <- load_multi_h5ad( object = SingleCells(dir_data = dir_data), prescan_result = tasks, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) table(sc[["exp_id"]]) unlink(c(files, dir_data), recursive = TRUE, force = TRUE)
SingleCells
Takes the result of prescan_mtx_dirs() and loads all inputs
into a single experiment with global gene QC and sequential cell indexing.
The feature space is the intersection of input gene IDs.
load_multi_mtx( object, prescan_result, sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )load_multi_mtx( object, prescan_result, sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
object |
|
prescan_result |
Output of |
sc_qc_param |
List. Output of |
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean. |
The class with updated shape and populated DuckDB.
# two CellRanger directories into one experiment dirs <- c(tempfile("cr_a"), tempfile("cr_b")) for (i in seq_along(dirs)) { dir.create(dirs[i], recursive = TRUE) data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data( n_cells = 200L, n_genes = 40L ), seed = i ) write_cellranger_output( dirs[i], data$counts, data$obs, data$var, rows = "cells", format_type = "csv", .verbose = FALSE ) } scan_res <- prescan_mtx_dirs( dirs = dirs, exp_ids = c("a", "b"), cells_as_rows = TRUE, has_hdr = TRUE, .verbose = FALSE ) dir_data <- tempfile("sc_multi_mtx") dir.create(dir_data, recursive = TRUE) sc <- load_multi_mtx( object = SingleCells(dir_data = dir_data), prescan_result = scan_res, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink( c(dirs, dir_data, scan_res$temp_files), recursive = TRUE, force = TRUE )# two CellRanger directories into one experiment dirs <- c(tempfile("cr_a"), tempfile("cr_b")) for (i in seq_along(dirs)) { dir.create(dirs[i], recursive = TRUE) data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data( n_cells = 200L, n_genes = 40L ), seed = i ) write_cellranger_output( dirs[i], data$counts, data$obs, data$var, rows = "cells", format_type = "csv", .verbose = FALSE ) } scan_res <- prescan_mtx_dirs( dirs = dirs, exp_ids = c("a", "b"), cells_as_rows = TRUE, has_hdr = TRUE, .verbose = FALSE ) dir_data <- tempfile("sc_multi_mtx") dir.create(dir_data, recursive = TRUE) sc <- load_multi_mtx( object = SingleCells(dir_data = dir_data), prescan_result = scan_res, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink( c(dirs, dir_data, scan_res$temp_files), recursive = TRUE, force = TRUE )
SingleCells
Takes the result of prescan_tenx_h5_files() and loads all
inputs into a single experiment with global gene QC and sequential cell
indexing. The feature space is determined by the prescan
(intersection or union of gene ids).
load_multi_tenx_h5( object, prescan_result, sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )load_multi_tenx_h5( object, prescan_result, sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
object |
|
prescan_result |
Output of |
sc_qc_param |
List. Output of |
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean. |
The class with updated shape and populated DuckDB.
# two 10x h5 files into one experiment files <- c(a = tempfile(fileext = ".h5"), b = tempfile(fileext = ".h5")) for (i in seq_along(files)) { data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data( n_cells = 200L, n_genes = 40L ), seed = i ) write_tenx_h5_sc( f_path = files[i], counts = data$counts, barcodes = data$obs$cell_id, features = data.table::data.table( id = data$var$gene_id, name = data$var$ensembl_id, feature_type = "Gene Expression" ) ) } scan_res <- prescan_tenx_h5_files(h5_paths = files, .verbose = FALSE) dir_data <- tempfile("sc_multi_tenx") dir.create(dir_data, recursive = TRUE) sc <- load_multi_tenx_h5( object = SingleCells(dir_data = dir_data), prescan_result = scan_res, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(c(files, dir_data), recursive = TRUE, force = TRUE)# two 10x h5 files into one experiment files <- c(a = tempfile(fileext = ".h5"), b = tempfile(fileext = ".h5")) for (i in seq_along(files)) { data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data( n_cells = 200L, n_genes = 40L ), seed = i ) write_tenx_h5_sc( f_path = files[i], counts = data$counts, barcodes = data$obs$cell_id, features = data.table::data.table( id = data$var$gene_id, name = data$var$ensembl_id, feature_type = "Gene Expression" ) ) } scan_res <- prescan_tenx_h5_files(h5_paths = files, .verbose = FALSE) dir_data <- tempfile("sc_multi_tenx") dir.create(dir_data, recursive = TRUE) sc <- load_multi_tenx_h5( object = SingleCells(dir_data = dir_data), prescan_result = scan_res, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(c(files, dir_data), recursive = TRUE, force = TRUE)
This function loads in data directly from R objects. The counts matrix must
be a dgRMatrix (rows = cells, columns = genes).
load_r_data( object, counts, obs, var, sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )load_r_data( object, counts, obs, var, sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
object |
|
counts |
Sparse matrix. The cells represent the rows, the genes the
columns. Needs to be a |
obs |
data.table. Cell metadata. Must have one row per cell in the
same order as |
var |
data.table. Feature metadata. Must have one row per gene in
the same order as |
sc_qc_param |
List. Output of
|
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean. Controls the verbosity of the function. |
It will populate the files on disk and return the class with updated shape information.
# straight from a dgRMatrix in memory onto disk data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) dir_data <- tempfile("sc_r_data") dir.create(dir_data, recursive = TRUE) sc <- load_r_data( object = SingleCells(dir_data = dir_data), counts = data$counts, obs = data$obs, var = data$var, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(dir_data, recursive = TRUE, force = TRUE)# straight from a dgRMatrix in memory onto disk data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) dir_data <- tempfile("sc_r_data") dir.create(dir_data, recursive = TRUE) sc <- load_r_data( object = SingleCells(dir_data = dir_data), counts = data$counts, obs = data$obs, var = data$var, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(dir_data, recursive = TRUE, force = TRUE)
SingleCellExperiment
Brings a Bioconductor SingleCellExperiment into a SingleCells object.
colData becomes the obs table, rowData becomes the var table, and the
chosen assay goes through the same Rust quality control and normalisation
every other loader uses.
The assay has to hold raw counts. Plenty of objects in the wild ship only
logcounts, and a negative binomial cannot model those, so pick the right
one rather than letting the default find whatever is there.
reducedDims and altExps are not carried over. Run the embedding on this
side, and use SingleCellsMultiModal() for ADT.
load_sce( object, sce, assay_name = "counts", sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )load_sce( object, sce, assay_name = "counts", sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
object |
|
sce |
|
assay_name |
String. Which assay holds the raw counts. Defaults to
|
sc_qc_param |
List. Output of
|
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean. Controls the verbosity of the function. |
It will populate the files on disk and return the class with updated shape information.
# colData becomes obs, rowData becomes var, the counts assay gets normalised data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) sce <- SingleCellExperiment::SingleCellExperiment( assays = list(counts = as(Matrix::t(data$counts), "CsparseMatrix")), colData = data.frame(data$obs, row.names = data$obs$cell_id), rowData = data.frame(data$var, row.names = data$var$gene_id) ) dir_data <- tempfile("sc_sce") dir.create(dir_data, recursive = TRUE) sc <- load_sce( object = SingleCells(dir_data = dir_data), sce = sce, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(dir_data, recursive = TRUE, force = TRUE)# colData becomes obs, rowData becomes var, the counts assay gets normalised data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) sce <- SingleCellExperiment::SingleCellExperiment( assays = list(counts = as(Matrix::t(data$counts), "CsparseMatrix")), colData = data.frame(data$obs, row.names = data$obs$cell_id), rowData = data.frame(data$var, row.names = data$var$gene_id) ) dir_data <- tempfile("sc_sce") dir.create(dir_data, recursive = TRUE) sc <- load_sce( object = SingleCells(dir_data = dir_data), sce = sce, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(dir_data, recursive = TRUE, force = TRUE)
SingleCells
This function takes a Seurat object and generates a SingleCells class
from it. The raw counts are extracted, written to the Rust binary format,
and the metadata is loaded into the DuckDB.
load_seurat( object, seurat, sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )load_seurat( object, seurat, sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
object |
|
seurat |
|
sc_qc_param |
List. Output of
|
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean. Controls the verbosity of the function. |
It will populate the files on disk and return the class with updated shape information.
# a Seurat object holds genes x cells, the loader transposes it data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) seurat_obj <- Seurat::CreateSeuratObject( counts = Matrix::t(data$counts), meta.data = data.frame(data$obs, row.names = data$obs$cell_id) ) dir_data <- tempfile("sc_seurat") dir.create(dir_data, recursive = TRUE) sc <- load_seurat( object = SingleCells(dir_data = dir_data), seurat = seurat_obj, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(dir_data, recursive = TRUE, force = TRUE)# a Seurat object holds genes x cells, the loader transposes it data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) seurat_obj <- Seurat::CreateSeuratObject( counts = Matrix::t(data$counts), meta.data = data.frame(data$obs, row.names = data$obs$cell_id) ) dir_data <- tempfile("sc_seurat") dir.create(dir_data, recursive = TRUE) sc <- load_seurat( object = SingleCells(dir_data = dir_data), seurat = seurat_obj, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(dir_data, recursive = TRUE, force = TRUE)
SingleCells
Loads the gene-expression modality from a CellRanger v2/v3 h5 file. The
counts go into the Rust-binarised format (with log normalisation applied on
read) and the barcodes/features into the DuckDB. Non-gene modalities (e.g.
Antibody Capture) are filtered out via feature_type.
load_tenx_h5( object, h5_path, sc_qc_param = params_sc_min_quality(), feature_type = "Gene Expression", csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )load_tenx_h5( object, h5_path, sc_qc_param = params_sc_min_quality(), feature_type = "Gene Expression", csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
object |
|
h5_path |
File path to the 10x h5 file. |
sc_qc_param |
List. Output of |
feature_type |
String. Modality to keep. Defaults to
|
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean. |
The class with updated shape information.
# read back a CellRanger v3 h5, gene expression only data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5") write_tenx_h5_sc( f_path = f_path, counts = data$counts, barcodes = data$obs$cell_id, features = data.table::data.table( id = data$var$gene_id, name = data$var$ensembl_id, feature_type = "Gene Expression" ) ) dir_data <- tempfile("sc_tenx") dir.create(dir_data, recursive = TRUE) sc <- load_tenx_h5( object = SingleCells(dir_data = dir_data), h5_path = f_path, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(c(f_path, dir_data), recursive = TRUE, force = TRUE)# read back a CellRanger v3 h5, gene expression only data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5") write_tenx_h5_sc( f_path = f_path, counts = data$counts, barcodes = data$obs$cell_id, features = data.table::data.table( id = data$var$gene_id, name = data$var$ensembl_id, feature_type = "Gene Expression" ) ) dir_data <- tempfile("sc_tenx") dir.create(dir_data, recursive = TRUE) sc <- load_tenx_h5( object = SingleCells(dir_data = dir_data), h5_path = f_path, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(c(f_path, dir_data), recursive = TRUE, force = TRUE)
Projects query cells through the reference's PCA loadings and applies the cached MoE batch correction. Gene matching is by name against the reference's stored HVG names; unmatched HVGs become zero columns. Results are attached to the query as embeddings.
map_symphony_query( reference, query, batch_column = NULL, additional_batch_columns = NULL, params = params_symphony_map(), .verbose = TRUE )map_symphony_query( reference, query, batch_column = NULL, additional_batch_columns = NULL, params = params_symphony_map(), .verbose = TRUE )
reference |
|
query |
|
batch_column |
Optional string. If |
additional_batch_columns |
Optional character vector. |
params |
List. Output of |
.verbose |
Boolean or integer. |
The query object with embeddings "symphony" (z_corr),
"symphony_pca" (z_pca) and "symphony_r" (soft cluster assignments,
transposed to N_q x K).
# project a second data set into the reference embedding ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, .verbose = FALSE ) query <- demo_single_cells(prepped = FALSE, seed = 7L) query <- map_symphony_query(symphony_ref, query = query, .verbose = FALSE) dim(get_embedding(query, "symphony")) unlink(c(ref@dir_data, query@dir_data), recursive = TRUE, force = TRUE)# project a second data set into the reference embedding ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, .verbose = FALSE ) query <- demo_single_cells(prepped = FALSE, seed = 7L) query <- map_symphony_query(symphony_ref, query = query, .verbose = FALSE) dim(get_embedding(query, "symphony")) unlink(c(ref@dir_data, query@dir_data), recursive = TRUE, force = TRUE)
This function implements MELD for estimating sample-associated density on a
cell manifold. The general idea is to smooth a binary sample indicator matrix
over the kNN graph via spectral filtering, yielding per-cell likelihood
estimates for each sample condition. For further details on the method,
please refer to Burkhardt, et al. This function will take a SingleCells
class and return the smoothed density estimates per cell and condition.
meld_sc( object, sample_id_col, embd_to_use = "pca", no_embd_to_use = NULL, meld_params = params_meld(), seed = 42L, .verbose = TRUE )meld_sc( object, sample_id_col, embd_to_use = "pca", no_embd_to_use = NULL, meld_params = params_meld(), seed = 42L, .verbose = TRUE )
object |
|
sample_id_col |
Character. The column in the obs table representing the sample identifier. |
embd_to_use |
Character. The embedding to use for kNN graph
construction. Please use the same here as you used to generate the
neighbours. Defaults to |
no_embd_to_use |
Optional integer. If you only want to use a subset of the embedding dimensions. |
meld_params |
A list, please see
|
seed |
Integer. Seed for reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run
times. |
A list with:
raw_scores - The raw MELD scores
norm_scores - Negative values were clamped to 0 and the rows L1 normalised. This yields probability-like values.
Burkhardt, et al. Nat. Biotechnol., 2021.
# per cell sample likelihoods over the kNN graph sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) res <- meld_sc(sc, sample_id_col = "sample_id", .verbose = FALSE) head(res$norm_scores[, 1:3]) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# per cell sample likelihoods over the kNN graph sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) res <- meld_sc(sc, sample_id_col = "sample_id", .verbose = FALSE) head(res$norm_scores[, 1:3]) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Row-binds several MetaCells() objects into a single one. The
typical use case is sample-pure meta cells: generate meta cells per patient
(see meta_cells_per_group()), then merge them so that methods like SCENIC,
AUCell or NMF can run across the full set.
The normalised counts are carried over as generated. They are normalised per meta cell, so row-binding leaves them valid. Caches (PCA, kNN, sNN, embeddings) are per source and are dropped; recompute them on the merged object.
merge_meta_cells( inputs, source_ids = NULL, feature_space = c("intersect", "union"), prefix_ids = TRUE, .verbose = TRUE )merge_meta_cells( inputs, source_ids = NULL, feature_space = c("intersect", "union"), prefix_ids = TRUE, .verbose = TRUE )
inputs |
List of |
source_ids |
Optional character vector of the same length as |
feature_space |
String. One of |
prefix_ids |
Boolean. Prefix the meta cell identifiers with the source
identifier. If |
.verbose |
Boolean. Controls verbosity of the function. |
A single MetaCells object with all meta cells of the inputs and
is_merged set to TRUE. The observation table gains a source_id column;
original_cell_idx stays in the index space of its own source, which is why
methods that resolve it against the source single cell data
(calc_diffusion_coordinates(), calc_manifold_metrics()) refuse to run on
the result. other_data holds the source identifiers under sources.
# meta cells generated per source, then pooled for downstream methods sc_1 <- demo_single_cells(seed = 1L) sc_2 <- demo_single_cells(seed = 2L) params <- params_sc_bt_metacells(target_no_metacells = 25L) merged <- merge_meta_cells( list( donor_1 = generate_bt_meta_cells_sc(sc_1, params, .verbose = FALSE), donor_2 = generate_bt_meta_cells_sc(sc_2, params, .verbose = FALSE) ), .verbose = FALSE ) merged unlink(c(sc_1@dir_data, sc_2@dir_data), recursive = TRUE, force = TRUE)# meta cells generated per source, then pooled for downstream methods sc_1 <- demo_single_cells(seed = 1L) sc_2 <- demo_single_cells(seed = 2L) params <- params_sc_bt_metacells(target_no_metacells = 25L) merged <- merge_meta_cells( list( donor_1 = generate_bt_meta_cells_sc(sc_1, params, .verbose = FALSE), donor_2 = generate_bt_meta_cells_sc(sc_2, params, .verbose = FALSE) ), .verbose = FALSE ) merged unlink(c(sc_1@dir_data, sc_2@dir_data), recursive = TRUE, force = TRUE)
SingleCells experiments into oneMerges N existing SingleCells objects into a freshly constructed target
object. The feature space of the result is the intersection of the input
gene sets. Each input's cells_to_keep filter is honoured (i.e. only cells
with to_keep = TRUE in the input's obs are carried over).
If renormalise = FALSE, the stored data_norm values are copied through
unchanged. This is valid only when all inputs were normalised against the
same target_size. If the gene intersection is much smaller than the
individual input gene sets, the inherited data_norm becomes a lossy
approximation (it was computed against the pre-intersection library size). In
that case set renormalise = TRUE to recompute data_norm against the
surviving raw counts using sc_qc_param$target_size.
Obs columns are intersected across inputs. The result obs gains an exp_id
column. Inputs that already have an exp_id column are rejected. The
sc_cache and sc_map of the target are populated fresh; any PCA, kNN, sNN
or HVG state on the inputs is not carried over and must be re-run.
merge_sc_experiments( target, inputs, exp_ids, renormalise = FALSE, sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )merge_sc_experiments( target, inputs, exp_ids, renormalise = FALSE, sc_qc_param = params_sc_min_quality(), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
target |
A freshly constructed |
inputs |
List of |
exp_ids |
Character vector of experiment identifiers, one per input. Must be unique. |
renormalise |
Boolean. Whether to recompute |
sc_qc_param |
List. Output of |
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean. |
The populated target SingleCells.
# two synthetic experiments merged over their shared gene space sc_a <- demo_single_cells(prepped = FALSE, seed = 1L) sc_b <- demo_single_cells(prepped = FALSE, seed = 2L) merged_dir <- tempfile("bixverse_merged") dir.create(merged_dir) merged <- merge_sc_experiments( target = SingleCells(dir_data = merged_dir), inputs = list(sc_a, sc_b), exp_ids = c("exp_a", "exp_b"), .verbose = FALSE ) table(unlist(merged[["exp_id"]])) unlink( c(sc_a@dir_data, sc_b@dir_data, merged_dir), recursive = TRUE, force = TRUE )# two synthetic experiments merged over their shared gene space sc_a <- demo_single_cells(prepped = FALSE, seed = 1L) sc_b <- demo_single_cells(prepped = FALSE, seed = 2L) merged_dir <- tempfile("bixverse_merged") dir.create(merged_dir) merged <- merge_sc_experiments( target = SingleCells(dir_data = merged_dir), inputs = list(sc_a, sc_b), exp_ids = c("exp_a", "exp_b"), .verbose = FALSE ) table(unlist(merged[["exp_id"]])) unlink( c(sc_a@dir_data, sc_b@dir_data, merged_dir), recursive = TRUE, force = TRUE )
Takes columns computed on one or more SingleCellsSubset()
objects and writes them into the parent's obs table in the DuckDB, joining
on cell_idx. The join is a left join, so every parent cell that is not
part of any of the provided subsets ends up as NA.
Pass a single subset or a list of them, e.g. the output of
apply_pipeline_per_group(). All subsets are written in a single join, and
the function refuses to run if two subsets claim the same cell.
merge_subset_obs( object, subsets, cols = NULL, new_names = NULL, prefix_values = FALSE, overwrite = FALSE, .verbose = TRUE )merge_subset_obs( object, subsets, cols = NULL, new_names = NULL, prefix_values = FALSE, overwrite = FALSE, .verbose = TRUE )
object |
|
subsets |
A |
cols |
Optional character vector. Columns in the subset obs tables to
merge. If |
new_names |
Optional character vector. Names to give the merged columns
in the parent obs table. Same length as |
prefix_values |
Boolean. Prefix every merged value with the subset's
group, i.e. |
overwrite |
Boolean. Allow merged columns to replace existing columns
of the same name in the parent obs table. Defaults to |
.verbose |
Boolean. Controls verbosity. |
The parent SingleCells object with the merged columns added to
the obs table in the DuckDB.
add_sc_new_obs() for the equivalent on result objects that carry
their own cell_idx, such as fast_cluster_sc().
# write a column computed on the subset back onto the parent sc <- demo_single_cells(prepped = FALSE) subset_obj <- SingleCellsSubset( sc_object = sc, grouping_column = "cell_grp", group = "cell_type_1" ) subset_obj[["sub_label"]] <- rep("a", dim(subset_obj)[1]) sc <- merge_subset_obs(sc, subset_obj, cols = "sub_label", .verbose = FALSE) table(get_sc_obs(sc)$sub_label, useNA = "ifany") unlink(sc@dir_data, recursive = TRUE, force = TRUE)# write a column computed on the subset back onto the parent sc <- demo_single_cells(prepped = FALSE) subset_obj <- SingleCellsSubset( sc_object = sc, grouping_column = "cell_grp", group = "cell_type_1" ) subset_obj[["sub_label"]] <- rep("a", dim(subset_obj)[1]) sc <- merge_subset_obs(sc, subset_obj, cols = "sub_label", .verbose = FALSE) table(get_sc_obs(sc)$sub_label, useNA = "ifany") unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Splits object by group_col, optionally runs pipeline on each subset,
generates meta cells per group and merges the results into a single
MetaCells() object. This gives you meta cells that never mix
cells from two patients/samples, while still being one object you can run
SCENIC, AUCell or NMF over.
The meta cell generators need an embedding, so pipeline will normally be
step_hvg_sc() %>>% step_pca_sc() %>>% step_neighbours_sc() unless every
subset already carries one.
meta_cells_per_group( object, group_col, method = c("bootstrapped", "seacells", "supercells"), mc_params = list(), pipeline = NULL, groups = NULL, feature_space = c("intersect", "union"), .verbose = TRUE )meta_cells_per_group( object, group_col, method = c("bootstrapped", "seacells", "supercells"), mc_params = list(), pipeline = NULL, groups = NULL, feature_space = c("intersect", "union"), .verbose = TRUE )
object |
|
group_col |
String. Column in obs used to split. |
method |
String. One of |
mc_params |
Named list. Arguments passed on to the generator, e.g.
|
pipeline |
Optional |
groups |
Optional character vector. Restrict to these group values; if
|
feature_space |
String. One of |
.verbose |
Boolean. Controls verbosity of the function. |
A merged MetaCells() object with a source_id column
in its observation table.
# meta cells that never mix two cell groups sc <- demo_single_cells(prepped = FALSE) prep <- step_hvg_sc(hvg_no = 30L, .verbose = FALSE) %>>% step_pca_sc(no_pcs = 10L, .verbose = FALSE) %>>% step_neighbours_sc(.verbose = FALSE) meta_cells_per_group( object = sc, group_col = "cell_grp", method = "bootstrapped", mc_params = list( sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 10L), .verbose = FALSE ), pipeline = prep, .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# meta cells that never mix two cell groups sc <- demo_single_cells(prepped = FALSE) prep <- step_hvg_sc(hvg_no = 30L, .verbose = FALSE) %>>% step_pca_sc(no_pcs = 10L, .verbose = FALSE) %>>% step_neighbours_sc(.verbose = FALSE) meta_cells_per_group( object = sc, group_col = "cell_grp", method = "bootstrapped", mc_params = list( sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 10L), .verbose = FALSE ), pipeline = prep, .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This is the bixverse-based meta cell class. It is generated by running
meta-cell generating functions on top of the main class. Due to the
aggregation of the count matrices to a way smaller number of meta cells,
the data is held in memory for this class specifically. Additional methods
that make specific usage of meta cells (especially co-expression module
detection ones will be made available).
MetaCells( meta_cell_data, var_data, meta_cell_method, obs_ids = NULL, cells_to_keep = NULL )MetaCells( meta_cell_data, var_data, meta_cell_method, obs_ids = NULL, cells_to_keep = NULL )
meta_cell_data |
Named list. Output of meta-cell generation Rust functions. Will contain the aggregated raw and normalised counts, plus additional information on the origin of the meta cells. |
var_data |
data.table with the variable/feature informations. |
meta_cell_method |
String describing the origin of the metacell. |
obs_ids |
Optional character vector of length |
cells_to_keep |
Optional integer vector. The source's
|
Returns the MetaCells class for further operations.
The meta cell observation table.
The meta cell variable table.
List with the raw and normalised counts.
Class with embeddings, kNN/sNN graphs, etc. Shared with
SingleCells().
List with original assignment information.
Dimensions of the new meta cell matrices.
Potential other data returned from the meta-cell generating methods.
Boolean. TRUE for objects returned by
merge_meta_cells(). Methods that need to resolve
original_cell_idx against the source single cell data use this to bail
out early.
# the class is produced by the aggregation functions, not built by hand sc <- demo_single_cells() mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 50L), .verbose = FALSE ) mc unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the class is produced by the aggregation functions, not built by hand sc <- demo_single_cells() mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 50L), .verbose = FALSE ) mc unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Implements the approach from Tirosh et al into bixverse, i.e., the AddModuleScore functionality from Seurat. For each module (gene set), computes the average expression of genes in the set minus the average expression of randomly selected control genes from the same expression bins. Genes are binned based on their average expression across cells to ensure controls are expression-matched.
module_scores_sc( object, gs_list, n_bins = 24L, n_ctrl = 100L, seed = 42L, streaming = NULL, .verbose = TRUE )module_scores_sc( object, gs_list, n_bins = 24L, n_ctrl = 100L, seed = 42L, streaming = NULL, .verbose = TRUE )
object |
|
gs_list |
Named list. The elements have the gene identifiers of the respective gene sets. |
n_bins |
Integer. Number of bins to use. Defaults to |
n_ctrl |
Integer. Number of control genes to use per gene for each gene
set. Defaults to |
seed |
Integer. The random seed. |
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
Returns a ScMatrixRes with the module scores.
Tirosh et al, Science (2016)
# score the three planted marker programmes per cell sc <- demo_single_cells() gs_list <- list( type_1 = sprintf("gene_%02d", 1:10), type_2 = sprintf("gene_%02d", 11:20), type_3 = sprintf("gene_%02d", 21:30) ) res <- module_scores_sc(sc, gs_list = gs_list, n_ctrl = 5L, .verbose = FALSE) dim(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# score the three planted marker programmes per cell sc <- demo_single_cells() gs_list <- list( type_1 = sprintf("gene_%02d", 1:10), type_2 = sprintf("gene_%02d", 11:20), type_3 = sprintf("gene_%02d", 21:30) ) res <- module_scores_sc(sc, gs_list = gs_list, n_ctrl = 5L, .verbose = FALSE) dim(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Turns a gene x k loading matrix from a matrix factorisation (ICA, NMF,
DGRDL) into a membership table by keeping the tails of each component's
loading distribution.
Two thresholding rules, selected via membership_params:
"zscore" - standardisation per component, keeping abs(z) > cutoff. No distributional assumption beyond rough symmetry.
"fdr" - two-sided p-values against a Normal null fitted per
component, Benjamini-Hochberg adjusted, keeping padj < fdr.
The standardisation itself is controlled by membership_params$scaling:
"robust" centres and scales by the median and MAD, "standard" by the
mean and standard deviation. The latter is stricter and keeps fewer genes
on skewed loadings, which is common for NMF.
modules_from_loadings(loadings, membership_params = params_module_membership())modules_from_loadings(loadings, membership_params = params_module_membership())
loadings |
Numeric matrix. |
membership_params |
List. See
|
A data.table with columns gene, module_id, loading, sign and
the per-component score (z or padj depending on the method). One row per
surviving (gene, component) pair, ordered by component then by descending
absolute loading.
# turn NMF gene loadings into module membership syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- nmf_bulk(obj, k = 4L, .verbose = FALSE) head(modules_from_loadings(get_nmf_gene_loadings(obj)))# turn NMF gene loadings into module membership syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- nmf_bulk(obj, k = 4L, .verbose = FALSE) head(modules_from_loadings(get_nmf_gene_loadings(obj)))
Fits NEBULA's negative binomial gamma mixed model over aggregated counts.
The arithmetic is nebula_sc() verbatim, only the counts come out
of memory rather than the streamed store, and the fit is much cheaper
because there are far fewer rows.
What changes is the interpretation, not the numerics. The cell-level overdispersion becomes the spread between aggregates within a subject rather than between cells, so it is smaller and it absorbs whatever the aggregation smoothed away. The subject-level term keeps its meaning. Read the two as a variance decomposition over meta cells and do not compare them against a single cell run.
nebula_mc( object, subject_col, design, coef = NULL, contrast = NULL, genes_to_use = NULL, cells_to_use = NULL, offset = NULL, nebula_params = params_nebula(), .verbose = TRUE )nebula_mc( object, subject_col, design, coef = NULL, contrast = NULL, genes_to_use = NULL, cells_to_use = NULL, offset = NULL, nebula_params = params_nebula(), .verbose = TRUE )
object |
|
subject_col |
String. The column in the obs table holding the subject (donor) identifier. This is what the random effect is over. |
design |
Formula. The experimental design, evaluated against the obs
table, e.g. |
coef |
Optional integer or character. Which coefficient of the design the Wald test reports, as a 1-based column position or a column name. Defaults to the last column. |
contrast |
Optional numeric vector. One weight per design column.
Mutually exclusive with |
genes_to_use |
Optional character vector. The genes to fit. Defaults to every gene in the object. |
cells_to_use |
Optional character vector. Meta cell identifiers
( |
offset |
Optional numeric vector. Strictly positive scaling factor per
meta cell, aligned to the meta cells that survive the design. Defaults to
|
nebula_params |
A list, see |
.verbose |
Boolean or integer. Controls verbosity and returns run
times. |
A ScNebula class, see new_sc_nebula_res().
He, et al., Commun Biol, 2021
# differential expression over meta cells with a donor random effect sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 2000L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 100L), .verbose = FALSE ) # the meta cell obs carries no donor labels, so vote them over sample_id <- sc[["sample_id"]]$sample_id mc[["sample_id"]] <- vapply( mc[[]]$original_cell_idx, function(idx) names(which.max(table(sample_id[idx]))), character(1) ) mc[["condition"]] <- ifelse( mc[["sample_id"]]$sample_id %in% c("sample_1", "sample_2", "sample_3"), "ctr", "trt" ) res <- nebula_mc( mc, subject_col = "sample_id", design = ~condition, .verbose = FALSE ) head(res$results) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# differential expression over meta cells with a donor random effect sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 2000L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) mc <- generate_bt_meta_cells_sc( sc, sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 100L), .verbose = FALSE ) # the meta cell obs carries no donor labels, so vote them over sample_id <- sc[["sample_id"]]$sample_id mc[["sample_id"]] <- vapply( mc[[]]$original_cell_idx, function(idx) names(which.max(table(sample_id[idx]))), character(1) ) mc[["condition"]] <- ifelse( mc[["sample_id"]]$sample_id %in% c("sample_1", "sample_2", "sample_3"), "ctr", "trt" ) res <- nebula_mc( mc, subject_col = "sample_id", design = ~condition, .verbose = FALSE ) head(res$results) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Fits NEBULA's negative binomial gamma mixed model, which is the test to reach for when the cells are not independent because they came from a handful of donors. The variance is split into a subject-level random effect and a cell-level overdispersion, so the donor structure is modelled rather than ignored, and the nominal false discovery rate holds.
The design is a formula evaluated against the obs table, so anything in there can go into the model. Cells with a missing design or subject value are dropped with a warning.
Genes are streamed and fitted in batches, which is exact: NEBULA is
gene-independent. A fit is milliseconds to seconds per gene though, so
running it over the full gene axis is rarely what you want. Restrict
genes_to_use to the highly variable genes or a candidate set.
nebula_sc( object, subject_col, design, coef = NULL, contrast = NULL, genes_to_use = NULL, cells_to_use = NULL, offset = NULL, nebula_params = params_nebula(), .verbose = TRUE )nebula_sc( object, subject_col, design, coef = NULL, contrast = NULL, genes_to_use = NULL, cells_to_use = NULL, offset = NULL, nebula_params = params_nebula(), .verbose = TRUE )
object |
|
subject_col |
String. The column in the obs table holding the subject (donor) identifier. This is what the random effect is over. Not the same thing as a sample or a batch, unless they happen to coincide. |
design |
Formula. The experimental design, evaluated against the obs
table, e.g. |
coef |
Optional integer or character. Which coefficient of the design the Wald test reports, as a 1-based column position or a column name. Defaults to the last column. |
contrast |
Optional numeric vector. One weight per design column.
Mutually exclusive with |
genes_to_use |
Optional character vector. The genes to fit. Defaults to every gene in the object, which is usually too many. |
cells_to_use |
Optional character vector. Names of the cells to fit, e.g. one cell type or one condition. Defaults to every cell that passed quality control. Cells that cannot be matched are dropped with a warning. |
offset |
Optional numeric vector. Strictly positive scaling factor per
cell, aligned to the cells that survive the design. Defaults to |
nebula_params |
A list, see
|
.verbose |
Boolean or integer. Controls verbosity and returns run
times. |
A ScNebula class, see new_sc_nebula_res(), with
results - data.table. One row per gene that survived NEBULA's expression filter, with the Wald test and both overdispersions.
coefficients - Numeric matrix of genes x coefficients.
se - Numeric matrix of genes x coefficients.
params - List. The parameters the run used.
He, et al., Commun Biol, 2021
# mixed model DGE with the sample as the random effect sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) obs <- get_sc_obs(sc) ctr <- sprintf("sample_%i", 1:3) condition <- ifelse(obs$sample_id %in% ctr, "ctr", "trt") sc <- set_sc_new_obs_col(sc, col_name = "condition", new_data = condition) res <- nebula_sc( sc, subject_col = "sample_id", design = ~condition, .verbose = FALSE ) head(res$results) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# mixed model DGE with the sample as the random effect sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) obs <- get_sc_obs(sc) ctr <- sprintf("sample_%i", 1:3) condition <- ifelse(obs$sample_id %in% ctr, "ctr", "trt") sc <- set_sc_new_obs_col(sc, col_name = "condition", new_data = condition) res <- nebula_sc( sc, subject_col = "sample_id", design = ~condition, .verbose = FALSE ) head(res$results) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This class helps to do diffusion of seed nodes in a single or tied version over a network, measure the ability of these diffusion vectors to recall against a gold standard set of nodes and do community detection within the subset of the network that received the the most heat from the initial seed genes.
NetworkDiffusions(edge_data_frame, weighted, directed)NetworkDiffusions(edge_data_frame, weighted, directed)
edge_data_frame |
data.table that contains the edge information. It is expected to have the columns 'from' and 'to'. |
weighted |
Boolean. Is the graph weighted. If set to TRUE, the
|
directed |
Boolean. Shall the graph be stored as directed. |
Returns the NetworkDiffusions class for further operations.
igraph. The underlying graph.
Numeric vector. Contains contains the single or tied diffusion results.
A (nested) list that will store all the parameters of the applied function.
data.table. Contains final results.
# unweighted, undirected diffusion class from an edge table set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) class(object)[1]# unweighted, undirected diffusion class from an edge table set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) class(object)[1]
ADTCounts classThis function generates a new ADTCounts class which uses CLR normalisation
under the hood. You have the choice between Seurat-style CLR (no negative
values) and normal CLR (allows negative values).
new_adt_counts_clr( raw_counts, cell_info, seurat_clr = FALSE, clean_clr_counts = TRUE, percentile = 0.01 )new_adt_counts_clr( raw_counts, cell_info, seurat_clr = FALSE, clean_clr_counts = TRUE, percentile = 0.01 )
raw_counts |
Numeric matrix. The raw ADT counts. |
cell_info |
Named integer vector. Output of |
seurat_clr |
Boolean. Shall a Seurat-style CLR be applied. |
clean_clr_counts |
Boolean. Shall the per-protein 1st percentile be removed from the CLR normalised counts. |
percentile |
Numeric. The percentile to remove to reduce background effects. |
ADTCounts that contains the raw and normalised ADT counts.
# CLR normalisation of synthetic ADT counts adt <- generate_single_cell_test_data_adt() cell_info <- stats::setNames( seq_len(nrow(adt$counts)), rownames(adt$counts) ) new_adt_counts_clr(adt$counts, cell_info = cell_info)# CLR normalisation of synthetic ADT counts adt <- generate_single_cell_test_data_adt() cell_info <- stats::setNames( seq_len(nrow(adt$counts)), rownames(adt$counts) ) new_adt_counts_clr(adt$counts, cell_info = cell_info)
ADTCounts class via DSB normalisationThis function generates a new ADTCounts class using DSB normalisation from
Mulè et al., instead of CLR. When empty_drops is provided, the per-protein
background is estimated from empty droplets. Without empty_drops, the
function falls back to a 2-component k-means on the log-transformed cell
counts ("ModelNegativeADTnorm").
new_adt_counts_dsb( raw_counts, cell_info, empty_drops = NULL, isotype_names = NULL, dsb_params = params_sc_dsb(), scale_factor = c("standardise", "mean_subtract"), seed = 42L, .verbose = TRUE )new_adt_counts_dsb( raw_counts, cell_info, empty_drops = NULL, isotype_names = NULL, dsb_params = params_sc_dsb(), scale_factor = c("standardise", "mean_subtract"), seed = 42L, .verbose = TRUE )
raw_counts |
Numeric matrix. Cells x proteins matrix of raw ADT counts with cell barcodes as row names and protein names as column names. |
cell_info |
Named integer vector. Output of |
empty_drops |
Optional numeric matrix. Cells x proteins matrix of empty-droplet ADT counts. If provided, used to estimate per-protein ambient background. |
isotype_names |
Optional string vector. Column names in |
dsb_params |
List. Output of |
scale_factor |
String. One of |
seed |
Integer. Random seed for k-means initialisation. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
ADTCounts containing the raw counts and the DSB-normalised
counts.
Mulè et al., Nat Commun, 2022
# DSB without empty droplets, i.e. the k-means fallback, using the isotypes adt <- generate_single_cell_test_data_adt() cell_info <- stats::setNames( seq_len(nrow(adt$counts)), rownames(adt$counts) ) new_adt_counts_dsb( adt$counts, cell_info = cell_info, isotype_names = adt$var$protein_id[adt$var$is_isotype], .verbose = FALSE )# DSB without empty droplets, i.e. the k-means fallback, using the isotypes adt <- generate_single_cell_test_data_adt() cell_info <- stats::setNames( seq_len(nrow(adt$counts)), rownames(adt$counts) ) new_adt_counts_dsb( adt$counts, cell_info = cell_info, isotype_names = adt$var$protein_id[adt$var$is_isotype], .verbose = FALSE )
Wrapper class that stores kNN data for subsequent usage in various other functions, methods and helpers.
new_sc_knn(knn_data, used_cells)new_sc_knn(knn_data, used_cells)
knn_data |
Named list with the following items:
|
used_cells |
Character vector. The cells used to generate the kNN graph with the distances. |
Generates the SingleCellNearestNeighbour class.
# rebuild the kNN wrapper from an existing graph sc <- demo_single_cells() knn <- generate_knn_sc(sc, .validate_index = FALSE, .verbose = FALSE) new_sc_knn( knn_data = list( indices = get_knn_mat(knn), dist = get_knn_dist(knn), dist_metric = "euclidean" ), used_cells = get_cell_names(sc) ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# rebuild the kNN wrapper from an existing graph sc <- demo_single_cells() knn <- generate_knn_sc(sc, .validate_index = FALSE, .verbose = FALSE) new_sc_knn( knn_data = list( indices = get_knn_mat(knn), dist = get_knn_dist(knn), dist_metric = "euclidean" ), used_cells = get_cell_names(sc) ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Fits a single HALS-NMF V ~ W H to the bulk expression matrix with a fixed
number of components k. V is samples x features (matches the layout of
raw_data / processed_data on BulkCoExp()). The result is
rearranged so that:
gene_loadings (features x k) captures per-module gene contributions.
sample_activity (samples x k) captures per-module sample activity.
Module membership is derived by keeping the upper tail of each component's
gene loadings, see params_module_membership(). A gene can belong
to several modules, and a gene in no tail belongs to none.
nmf_bulk( object, k, preprocessing = c("none", "sd", "sqrt_sd"), nmf_hals_params = params_nmf_hals(), membership_params = params_module_membership(), seed = 42L, .verbose = TRUE )nmf_bulk( object, k, preprocessing = c("none", "sd", "sqrt_sd"), nmf_hals_params = params_nmf_hals(), membership_params = params_module_membership(), seed = 42L, .verbose = TRUE )
object |
The class, see |
k |
Integer. Number of latent factors (modules). |
preprocessing |
String. One of |
nmf_hals_params |
List. Output of
|
membership_params |
List. Controls how the gene loadings are turned into
module membership, see |
seed |
Integer. Random seed for the NMF initialisation. Defaults to
|
.verbose |
Boolean or integer |
The class with final_results populated (see description) and the
fit parameters stored under params$nmf_fit, plus
params$detection_method = "nmf-based".
Cichocki & Phan, IEICE Trans., 2009.
# a single four-factor HALS-NMF fit syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- nmf_bulk(obj, k = 4L, .verbose = FALSE) head(get_nmf_modules(obj))# a single four-factor HALS-NMF fit syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- nmf_bulk(obj, k = 4L, .verbose = FALSE) head(get_nmf_modules(obj))
Runs the consensus clustering step across a range of k and reports
stability against reconstruction error, without keeping any factors. Pick
the k where stability is still high and the error curve has not yet
flattened out, then run consensus_nmf_bulk() there.
nmf_k_sweep_bulk( object, k_range, n_runs = 30L, preprocessing = c("none", "sd", "sqrt_sd"), nmf_hals_params = params_nmf_hals(), nmf_consensus_params = params_nmf_consensus(), seed = 42L, .verbose = TRUE )nmf_k_sweep_bulk( object, k_range, n_runs = 30L, preprocessing = c("none", "sd", "sqrt_sd"), nmf_hals_params = params_nmf_hals(), nmf_consensus_params = params_nmf_consensus(), seed = 42L, .verbose = TRUE )
object |
The class, see |
k_range |
Integer vector. The ranks to evaluate. Every entry must be at least 2. |
n_runs |
Integer. Number of random restarts per |
preprocessing |
String. One of |
nmf_hals_params |
List. Output of |
nmf_consensus_params |
List. Output of
|
seed |
Integer. Base random seed. |
.verbose |
Boolean or integer |
This is a diagnostic, so it leaves the object alone and hands you the result
back directly. plot() on the returned object gives you the two curves.
Cost is length(k_range) * n_runs full NMF fits, so keep both modest on a
first pass.
An NmfKSweepResult, which is a data.table with one row per k.
Kotliar et al., eLife, 2019
# small sweep, keep both the grid and the restarts modest syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) sweep_res <- nmf_k_sweep_bulk( obj, k_range = 3:5, n_runs = 5L, .verbose = FALSE ) sweep_res# small sweep, keep both the grid and the restarts modest syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) sweep_res <- nmf_k_sweep_bulk( obj, k_range = 3:5, n_runs = 5L, .verbose = FALSE ) sweep_res
Runs the consensus clustering step across a range of k and reports
stability against reconstruction error, without keeping any factors. Pick
the k where stability is still high and the error curve has not yet
flattened out, then run consensus_nmf_sc() there.
nmf_k_sweep_sc( object, k_range, cell_ids = NULL, gene_ids = NULL, preprocessing = "none", use_second_layer = TRUE, nmf_hals_params = params_nmf_hals(), nmf_consensus_params = params_nmf_consensus(), n_runs = 30L, seed = 42L, .verbose = TRUE )nmf_k_sweep_sc( object, k_range, cell_ids = NULL, gene_ids = NULL, preprocessing = "none", use_second_layer = TRUE, nmf_hals_params = params_nmf_hals(), nmf_consensus_params = params_nmf_consensus(), n_runs = 30L, seed = 42L, .verbose = TRUE )
object |
|
k_range |
Integer vector. The ranks to evaluate. Every entry must be at least 2. |
cell_ids |
Optional character. Cell ids (or meta cell ids) to restrict
the NMF to. If |
gene_ids |
Optional character. Gene ids to restrict the NMF to. If
|
preprocessing |
String. One of |
use_second_layer |
Boolean. If |
nmf_hals_params |
List, see |
nmf_consensus_params |
List, see |
n_runs |
Integer. Number of random restarts. Must be at least 2. |
seed |
Integer. Base random seed. Restart |
.verbose |
Boolean or integer. Verbosity. |
This is a diagnostic, so it leaves the object alone and hands the result back
directly. plot() on it gives you the two curves.
Cost is length(k_range) * n_runs full NMF fits. On the SingleCells path
the counts are read from disk once and reused across every k, but the fits
themselves are not free, so keep both modest on a first pass.
An NmfKSweepResult, which is a data.table with one row per k.
Kotliar et al., eLife, 2019
# stability against reconstruction error across three ranks sc <- demo_single_cells() res <- nmf_k_sweep_sc( sc, k_range = 2:4, n_runs = 5L, nmf_consensus_params = params_nmf_consensus(density_threshold = 2), .verbose = FALSE ) res unlink(sc@dir_data, recursive = TRUE, force = TRUE)# stability against reconstruction error across three ranks sc <- demo_single_cells() res <- nmf_k_sweep_sc( sc, k_range = 2:4, n_runs = 5L, nmf_consensus_params = params_nmf_consensus(density_threshold = 2), .verbose = FALSE ) res unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Runs a single HALS NMF on a chosen subset of cells and genes. For
SingleCells, the counts are streamed from disk via the Rust binary
files; for MetaCells, the in-memory sparse counts are used.
nmf_sc( object, k, cell_ids = NULL, gene_ids = NULL, preprocessing = "none", use_second_layer = TRUE, nmf_hals_params = params_nmf_hals(), seed = 42L, .verbose = TRUE )nmf_sc( object, k, cell_ids = NULL, gene_ids = NULL, preprocessing = "none", use_second_layer = TRUE, nmf_hals_params = params_nmf_hals(), seed = 42L, .verbose = TRUE )
object |
|
k |
Integer. Number of latent factors to return. |
cell_ids |
Optional character. Cell ids (or meta cell ids) to restrict
the NMF to. If |
gene_ids |
Optional character. Gene ids to restrict the NMF to. If
|
preprocessing |
String. One of |
use_second_layer |
Boolean. If |
nmf_hals_params |
List, see |
seed |
Integer. Random seed for initialisation. |
.verbose |
Boolean or integer. Verbosity. |
An NmfResult object.
# three factors on the highly variable genes sc <- demo_single_cells() res <- nmf_sc(sc, k = 3L, .verbose = FALSE) res unlink(sc@dir_data, recursive = TRUE, force = TRUE)# three factors on the highly variable genes sc <- demo_single_cells() res <- nmf_sc(sc, k = 3L, .verbose = FALSE) res unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Calculates the normalisation factors and applies voom on the filtered
counts from qc_bulk_dge(), both in Rust via the edge-rs
crate. Can additionally calculate TPM and FPKM values for plotting purposes.
normalise_bulk_dge( object, group_col, norm_method = c("TMM", "TMMwsp", "RLE", "upperquartile", "none"), calc_tpm = FALSE, calc_fpkm = FALSE, gene_lengths = NULL, .verbose = TRUE )normalise_bulk_dge( object, group_col, norm_method = c("TMM", "TMMwsp", "RLE", "upperquartile", "none"), calc_tpm = FALSE, calc_fpkm = FALSE, gene_lengths = NULL, .verbose = TRUE )
object |
The underlying class, see |
group_col |
String. The column in the metadata that will contain the contrast groups. Needs to be part of the metadata stored in the class. |
norm_method |
String. One of
|
calc_tpm |
Boolean. Output TPM calculation (default = FALSE). |
calc_fpkm |
Boolean. Output FPKM calculation (default = FALSE). |
gene_lengths |
Optional named numeric. If you want to calculate TPM or FPKM you need to provide this one. The names need to be the same identifier as used in the counts. |
.verbose |
Boolean. Controls the verbosity of the function. |
Returns the class with the processed_data data slot populated and
applied parameters added to the params slot.
# TMM library size normalisation followed by voom syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) get_outputs(object)$normalised_counts[1:3, 1:3]# TMM library size normalisation followed by voom syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) get_outputs(object)$normalised_counts[1:3, 1:3]
This class is used to store any ontology and apply different methods to it. Currently implemented are different types of term similarities, i.e., based on semantic similarities and the Wang similarity.
OntologySim(parent_child_dt, .verbose = TRUE)OntologySim(parent_child_dt, .verbose = TRUE)
parent_child_dt |
A data.table that contains the ontological information
in terms of parent child relationships. Need to contain the
|
.verbose |
Boolean. Controls the verbosity of the class |
Returns the class for subsequent usage.
data.table. Contains the parent-child relationships. (For Wang similarity also the relationship type.)
List. Contains various intermediary results used for some methods.
List. Contains the potentially calculated similarity matrix in form of an R6 class. Getters to access the data are provided.
A (nested) list that will store all the parameters of the applied function.
Final results stored in the class.
# wrap a toy parent-child ontology into the class onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) OntologySim(onto, .verbose = FALSE)# wrap a toy parent-child ontology into the class onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) OntologySim(onto, .verbose = FALSE)
The function takes in a vector of scores between 0 and 1 and calculates a harmonic sum, based on the approach OpenTargets takes to do their gene - disease evidence scores, see: https://platform-docs.opentargets.org/associations.
ot_harmonic_score(x)ot_harmonic_score(x)
x |
Numeric vector. Needs to be between 0 and 1. |
Harmonic, normalised sum of the provided scores.
# harmonic sum over four evidence scores ot_harmonic_score(c(1, 0.8, 0.5, 0.1))# harmonic sum over four evidence scores ot_harmonic_score(c(1, 0.8, 0.5, 0.1))
Controls filtering, ordering and binning in
extract_binary_heatmap_data(). Features are clustered on the Jaccard
distance, samples on the Hamming distance within their group. Groups larger
than max_cluster_n are ordered by barycentre instead, which skips the
distance matrix. More samples than max_cols get binned within their group
into fraction-on columns.
params_binary_heatmap( min_frac_on = 0.01, max_frac_on = 0.99, cluster_features = TRUE, cluster_samples = TRUE, max_cols = 2000L, max_cluster_n = 2000L )params_binary_heatmap( min_frac_on = 0.01, max_frac_on = 0.99, cluster_features = TRUE, cluster_samples = TRUE, max_cols = 2000L, max_cluster_n = 2000L )
min_frac_on |
Numeric. Features on in a smaller fraction of samples are
dropped. Defaults to |
max_frac_on |
Numeric. Features on in a larger fraction of samples are
dropped. Defaults to |
cluster_features |
Boolean. Shall the features be clustered within their
group. Defaults to |
cluster_samples |
Boolean. Shall the samples be ordered within their
group. Defaults to |
max_cols |
Integer. Maximum number of columns before samples get binned.
Defaults to |
max_cluster_n |
Integer. Groups with more samples than this are ordered
by barycentre instead of hierarchical clustering. Defaults to |
A named list with the following elements:
min_frac_on - Numeric. Features on in a smaller fraction of samples
are dropped. Defaults to 0.01.
max_frac_on - Numeric. Features on in a larger fraction of samples are
dropped. Defaults to 0.99.
cluster_features - Boolean. Shall the features be clustered within
their group. Defaults to TRUE.
cluster_samples - Boolean. Shall the samples be ordered within their
group. Defaults to TRUE.
max_cols - Integer. Maximum number of columns before samples get
binned. Defaults to 2000L.
max_cluster_n - Integer. Groups with more samples than this are
ordered by barycentre instead of hierarchical clustering. Defaults to
2000L.
Wrapper function to generate blitzGSEA parameters
params_blitzgsea( min_size = 5L, max_size = 500L, permutations = 2000L, anchors = 40L, symmetric = FALSE, centre = TRUE, ks_test = TRUE, seed = 42 )params_blitzgsea( min_size = 5L, max_size = 500L, permutations = 2000L, anchors = 40L, symmetric = FALSE, centre = TRUE, ks_test = TRUE, seed = 42 )
min_size |
Integer. Minimum number of genes per gene set. Defaults to
|
max_size |
Integer. Maximum number of genes per gene set. Defaults to
|
permutations |
Integer. Random gene sets drawn per anchor size during
calibration. Below |
anchors |
Integer. Number of log-spaced anchor sizes requested. Sizes
that collide after rounding are collapsed, so the realised grid is usually a
little smaller. Defaults to |
symmetric |
Boolean. Pool both tails into one gamma instead of fitting
them separately. Defaults to |
centre |
Boolean. Centre the signature on its mean before scoring. The
enrichment score is not invariant to an offset, so the calibration and the
scoring have to agree on this. Defaults to |
ks_test |
Boolean. Run the Kolmogorov-Smirnov goodness-of-fit diagnostic
at every anchor. Costs a sort per anchor. Defaults to |
seed |
Numeric. Random seed for the calibration. Defaults to |
A named list with the following elements:
min_size - Integer. Minimum number of genes per gene set. Defaults to
5L.
max_size - Integer. Maximum number of genes per gene set. Defaults to
500L.
permutations - Integer. Random gene sets drawn per anchor size during
calibration. Below 1000L the two tails are pooled into a single gamma
regardless of symmetric. Defaults to 2000L.
anchors - Integer. Number of log-spaced anchor sizes requested. Sizes
that collide after rounding are collapsed, so the realised grid is usually a
little smaller. Defaults to 40L.
symmetric - Boolean. Pool both tails into one gamma instead of fitting
them separately. Defaults to FALSE.
centre - Boolean. Centre the signature on its mean before scoring. The
enrichment score is not invariant to an offset, so the calibration and the
scoring have to agree on this. Defaults to TRUE.
ks_test - Boolean. Run the Kolmogorov-Smirnov goodness-of-fit
diagnostic at every anchor. Costs a sort per anchor. Defaults to TRUE.
seed - Numeric. Random seed for the calibration. Defaults to 42.0.
Lachmann, et al., Bioinformatics, 2022
Wrapper function for Boost parameters
params_boost( boost_rate = 0.25, replace = FALSE, resolution = 1, n_iters = 20L, p_thresh = 1e-07, voter_thresh = 0.9, fast_cluster = FALSE, normalisation = list(), hvg = list(), pca = list(), knn = list(k = 0L), fast_cluster_params = list() )params_boost( boost_rate = 0.25, replace = FALSE, resolution = 1, n_iters = 20L, p_thresh = 1e-07, voter_thresh = 0.9, fast_cluster = FALSE, normalisation = list(), hvg = list(), pca = list(), knn = list(k = 0L), fast_cluster_params = list() )
boost_rate |
Numeric. Boosting rate for the algorithm. Must be between 0
and 1. Defaults to |
replace |
Boolean. Whether to use replacement during boosting. Defaults
to |
resolution |
Numeric. Resolution parameter for graph-based clustering.
Higher values lead to more clusters. Defaults to |
n_iters |
Integer. Number of iterations to run the algorithm. Defaults
to |
p_thresh |
Numeric. P-value threshold for significance testing. Defaults
to |
voter_thresh |
Numeric. Voter threshold across iterations. Proportion of
iterations a cell must be assigned to a cluster to be considered a member.
Must be between 0 and 1. Defaults to |
fast_cluster |
Boolean. Shall fast Louvain clustering be applied, i.e.,
k-means clustering and use the centroids for kNN graph generation and Louvain
clustering with then backpropagating the membership based on centroid
proximity. Defaults to |
normalisation |
List. Optional overrides for normalisation parameters.
See |
hvg |
List. Optional overrides for highly variable gene selection
parameters. See |
pca |
List. Optional overrides for PCA parameters. See
|
knn |
List. Optional overrides for kNN parameters. See
|
fast_cluster_params |
List. Optional overrides for the fast clustering
parameters. Only relevant if |
A named list with the following elements:
The elements of params_norm_doublets_defaults(), overridden by
normalisation, spliced in at this position.
The elements of params_hvg_defaults(), overridden by hvg, spliced
in at this position.
The elements of params_pca_defaults(), overridden by pca, spliced
in at this position.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
The elements of params_fast_cluster_default(), overridden by
fast_cluster_params, spliced in at this position.
boost_rate - Numeric. Boosting rate for the algorithm. Must be between
0 and 1. Defaults to 0.25.
replace - Boolean. Whether to use replacement during boosting.
Defaults to FALSE.
resolution - Numeric. Resolution parameter for graph-based clustering.
Higher values lead to more clusters. Defaults to 1.0.
fast_cluster - Boolean. Shall fast Louvain clustering be applied,
i.e., k-means clustering and use the centroids for kNN graph generation and
Louvain clustering with then backpropagating the membership based on
centroid proximity. Defaults to FALSE.
n_iters - Integer. Number of iterations to run the algorithm. Defaults
to 20L.
p_thresh - Numeric. P-value threshold for significance testing.
Defaults to 1e-07.
voter_thresh - Numeric. Voter threshold across iterations. Proportion
of iterations a cell must be assigned to a cluster to be considered a
member. Must be between 0 and 1. Defaults to 0.9.
Parameters for PCA with missing values via Bayesian PCA, see
run_bpca(). Defaults follow pcaMethods.
params_bpca( n_pcs = 2L, max_iter = 100L, tol = 1e-04, centre = TRUE, scale = FALSE )params_bpca( n_pcs = 2L, max_iter = 100L, tol = 1e-04, centre = TRUE, scale = FALSE )
n_pcs |
Integer. Number of principal components. Defaults to |
max_iter |
Integer. Maximum number of variational steps. Defaults to
|
tol |
Numeric. Change in |
centre |
Boolean. Shall the observed column means be subtracted first.
Defaults to |
scale |
Boolean. Shall the columns be divided by their observed standard
deviation first (pcaMethods' |
A named list with the following elements:
n_pcs - Integer. Number of principal components. Defaults to 2L.
max_iter - Integer. Maximum number of variational steps. Defaults to
100L.
tol - Numeric. Change in log10(tau) over ten steps below which the
fit stops. Defaults to 1e-04.
centre - Boolean. Shall the observed column means be subtracted first.
Defaults to TRUE.
scale - Boolean. Shall the columns be divided by their observed
standard deviation first (pcaMethods' "uv"). Defaults to FALSE.
Oba, et al., Bioinformatics, 2003; Stacklies, et al., Bioinformatics, 2007
Parameters for simulate_dropouts(). Dropout falls
out of the library size rather than an explicit per-gene dropout curve: a
size factor s_j ~ LogNormal(0, capture_efficiency_sigma) is drawn per
sample, giving a target library size of target_library_size * s_j, and each
gene is binomially thinned towards that target.
params_bulk_sparsity( strategy = "seq_depth", target_library_size = 20000, capture_efficiency_sigma = 0.5, seed = 123L )params_bulk_sparsity( strategy = "seq_depth", target_library_size = 20000, capture_efficiency_sigma = 0.5, seed = 123L )
strategy |
String. Which dropout strategy to apply. Currently only
|
target_library_size |
Numeric. Reference library size per sample.
Defaults to |
capture_efficiency_sigma |
Numeric. Standard deviation of the LogNormal
size-factor distribution. Larger values spread the library sizes further
apart. Defaults to |
seed |
Integer. Seed for reproducibility purposes. Defaults to |
A named list with the following elements:
strategy - String. Which dropout strategy to apply. Currently only
"seq_depth". One of "seq_depth". Defaults to "seq_depth".
target_library_size - Numeric. Reference library size per sample.
Defaults to 20000.0.
capture_efficiency_sigma - Numeric. Standard deviation of the
LogNormal size-factor distribution. Larger values spread the library sizes
further apart. Defaults to 0.5.
seed - Integer. Seed for reproducibility purposes. Defaults to 123L.
Zappia, et al., Genome Biol, 2017
auc_threshold and max_rank are two different cutoffs and are
easy to confuse. The first truncates the recovery curve used to score motif
enrichment, the second sets how deep into the ranking the background curve
(mean + 2 SD across all motifs) is built, and therefore where the leading
edge cuts. RcisTarget uses maxRank = 5000 and nMean = 100; the defaults
here follow it. auc_threshold follows pySCENIC at 5%, RcisTarget itself
uses 3%.
params_cistarget( auc_threshold = 0.05, nes_threshold = 3, max_rank = 5000L, n_mean = 100L, rcc_method = c("approx", "icistarget"), high_conf_cats = c("directAnnotation", "inferredBy_Orthology"), low_conf_cats = c("inferredBy_MotifSimilarity", "inferredBy_MotifSimilarity_n_Orthology") )params_cistarget( auc_threshold = 0.05, nes_threshold = 3, max_rank = 5000L, n_mean = 100L, rcc_method = c("approx", "icistarget"), high_conf_cats = c("directAnnotation", "inferredBy_Orthology"), low_conf_cats = c("inferredBy_MotifSimilarity", "inferredBy_MotifSimilarity_n_Orthology") )
auc_threshold |
Numeric. Numeric between 0 and 1. Proportion of genes to
use for AUC threshold calculation. Default is 0.05 (5% of genes). Defaults to
|
nes_threshold |
Numeric. Normalised Enrichment Score threshold for
significant motifs. Default is 3.0. Defaults to |
max_rank |
Integer. Depth of the recovery curves used to derive the
background and the leading edge. Clamped to the number of genes in the
ranking database. Default is 5000, the RcisTarget value. Defaults to |
n_mean |
Integer. Window for the rolling mean smoothing the background
recovery curve. Only read when |
rcc_method |
String. Method for recovery curve calculation. Either
"approx" (approximate, faster) or "icistarget" (exact, slower). One of
|
high_conf_cats |
String vector. Annotation categories considered high
confidence. Default includes direct annotations and orthology-based
inferences. Defaults to |
low_conf_cats |
String vector. Annotation categories considered lower
confidence. Default includes motif similarity-based inferences. Defaults to
|
A named list with the following elements:
auc_threshold - Numeric. Numeric between 0 and 1. Proportion of genes
to use for AUC threshold calculation. Default is 0.05 (5% of genes).
Defaults to 0.05.
nes_threshold - Numeric. Normalised Enrichment Score threshold for
significant motifs. Default is 3.0. Defaults to 3.0.
max_rank - Integer. Depth of the recovery curves used to derive the
background and the leading edge. Clamped to the number of genes in the
ranking database. Default is 5000, the RcisTarget value. Defaults to
5000L.
n_mean - Integer. Window for the rolling mean smoothing the background
recovery curve. Only read when rcc_method = "approx". Default is 100, the
RcisTarget value. Defaults to 100L.
rcc_method - String. Method for recovery curve calculation. Either
"approx" (approximate, faster) or "icistarget" (exact, slower). One of
c("approx", "icistarget"). Defaults to "approx".
high_conf_cats - String vector. Annotation categories considered high
confidence. Default includes direct annotations and orthology-based
inferences. Defaults to c("directAnnotation", "inferredBy_Orthology").
low_conf_cats - String vector. Annotation categories considered lower
confidence. Default includes motif similarity-based inferences. Defaults to
c("inferredBy_MotifSimilarity", "inferredBy_MotifSimilarity_n_Orthology").
Wrapper function to generate community detection parameters
params_community_detection( max_nodes = 300L, min_nodes = 10L, min_seed_nodes = 2L, initial_res = 0.5, threshold_type = c("prop_based", "pval_based"), network_threshold = 0.5, pval_threshold = 0.1 )params_community_detection( max_nodes = 300L, min_nodes = 10L, min_seed_nodes = 2L, initial_res = 0.5, threshold_type = c("prop_based", "pval_based"), network_threshold = 0.5, pval_threshold = 0.1 )
max_nodes |
Integer. Maximum number of nodes in a given community.
Defaults to |
min_nodes |
Integer. Minimum number of nodes in a given community.
Defaults to |
min_seed_nodes |
Integer. Minimum number of seed nodes within a
community. Defaults to |
initial_res |
Numeric. Initial resolution parameter to start with.
Defaults to |
threshold_type |
String. You can chose to include a certain proportion
of the network with the highest diffusion scores, or use p-values based on
permutations. One of |
network_threshold |
Numeric. The proportion of the network to include.
Used if |
pval_threshold |
Numeric. The maximum p-value for nodes to be included.
Used if |
A named list with the following elements:
max_nodes - Integer. Maximum number of nodes in a given community.
Defaults to 300L.
min_nodes - Integer. Minimum number of nodes in a given community.
Defaults to 10L.
min_seed_nodes - Integer. Minimum number of seed nodes within a
community. Defaults to 2L.
initial_res - Numeric. Initial resolution parameter to start with.
Defaults to 0.5.
threshold_type - String. You can chose to include a certain proportion
of the network with the highest diffusion scores, or use p-values based on
permutations. One of c("prop_based", "pval_based"). Defaults to
"prop_based".
network_threshold - Numeric. The proportion of the network to include.
Used if threshold_type = "prop_based". Defaults to 0.5.
pval_threshold - Numeric. The maximum p-value for nodes to be
included. Used if threshold_type = "pval_based". Defaults to 0.1.
Wrapper function for graph generation
params_cor_graph( epsilon = 2, min_cor = 0.2, fdr_threshold = 0.05, verbose = TRUE )params_cor_graph( epsilon = 2, min_cor = 0.2, fdr_threshold = 0.05, verbose = TRUE )
epsilon |
Numeric. Defines the epsilon parameter for the radial basis
function. Defaults to |
min_cor |
Numeric. Minimum absolute correlation that needs to be
observed in either data set. Only relevant for differential correlation-based
graphs. Defaults to |
fdr_threshold |
Numeric. Maximum FDR for the differential correlation
p-value. Defaults to |
verbose |
Boolean. Controls verbosity of the graph generation function.
Defaults to |
A named list with the following elements:
epsilon - Numeric. Defines the epsilon parameter for the radial basis
function. Defaults to 2.0.
min_cor - Numeric. Minimum absolute correlation that needs to be
observed in either data set. Only relevant for differential
correlation-based graphs. Defaults to 0.2.
fdr_threshold - Numeric. Maximum FDR for the differential correlation
p-value. Defaults to 0.05.
verbose - Boolean. Controls verbosity of the graph generation
function. Defaults to TRUE.
Wrapper function to generate CoReMo parameters
params_coremo( epsilon = 2, k_min = 2L, k_max = 150L, min_size = NULL, junk_module_threshold = 0.05, rbf_func = c("gaussian", "inverse_quadratic", "bump"), cor_method = c("spearman", "pearson") )params_coremo( epsilon = 2, k_min = 2L, k_max = 150L, min_size = NULL, junk_module_threshold = 0.05, rbf_func = c("gaussian", "inverse_quadratic", "bump"), cor_method = c("spearman", "pearson") )
epsilon |
Numeric. Epsilon parameter for the chosen RBF function, see
|
k_min |
Integer. Minimum and maximum number of cuts to use for the
hierarchical clustering. Defaults to |
k_max |
Integer. Minimum and maximum number of cuts to use for the
hierarchical clustering. Defaults to |
min_size |
Integer or |
junk_module_threshold |
Numeric. Threshold for the minimum correlation
to be observed in a module. Defaults to |
rbf_func |
String. Type of RBF you wish to apply to down-weigh weak
correlations. One of |
cor_method |
String. The type of correlation to use. One of
|
A named list with the following elements:
epsilon - Numeric. Epsilon parameter for the chosen RBF function, see
rbf_func. The higher, the more aggressively low correlations will be
shrunk. Defaults to 2.0.
k_min - Integer. Minimum and maximum number of cuts to use for the
hierarchical clustering. Defaults to 2L.
k_max - Integer. Minimum and maximum number of cuts to use for the
hierarchical clustering. Defaults to 150L.
min_size - Integer or NULL. Minimum size of the clusters. Smaller
clusters will be combined together. Defaults to NULL.
junk_module_threshold - Numeric. Threshold for the minimum correlation
to be observed in a module. Defaults to 0.05.
rbf_func - String. Type of RBF you wish to apply to down-weigh weak
correlations. One of c("gaussian", "inverse_quadratic", "bump"). Defaults
to "gaussian".
cor_method - String. The type of correlation to use. One of
c("spearman", "pearson"). Defaults to "spearman".
Wrapper function to generate DGRDL parameters
params_dgrdl( sparsity = 5L, dict_size = 5L, alpha = 1, beta = 1, max_iter = 20L, k_neighbours = 5L, admm_iter = 5L, rho = 1 )params_dgrdl( sparsity = 5L, dict_size = 5L, alpha = 1, beta = 1, max_iter = 20L, k_neighbours = 5L, admm_iter = 5L, rho = 1 )
sparsity |
Integer. Sparsity constraint (max non-zero coefficients per
signal) Defaults to |
dict_size |
Integer. Dictionary size Defaults to |
alpha |
Numeric. Sample context regularisation weight. Defaults to
|
beta |
Numeric. Feature effect regularisation weight. Defaults to |
max_iter |
Integer. Maximum number of iterations for the main algorithm.
Defaults to |
k_neighbours |
Integer. Number of neighbours in the KNN graph. Defaults
to |
admm_iter |
Integer. ADMM iterations for sparse coding. Defaults to
|
rho |
Numeric. ADMM step size. Defaults to |
A named list with the following elements:
sparsity - Integer. Sparsity constraint (max non-zero coefficients per
signal) Defaults to 5L.
dict_size - Integer. Dictionary size Defaults to 5L.
alpha - Numeric. Sample context regularisation weight. Defaults to
1.0.
beta - Numeric. Feature effect regularisation weight. Defaults to
1.0.
max_iter - Integer. Maximum number of iterations for the main
algorithm. Defaults to 20L.
k_neighbours - Integer. Number of neighbours in the KNN graph.
Defaults to 5L.
admm_iter - Integer. ADMM iterations for sparse coding. Defaults to
5L.
rho - Numeric. ADMM step size. Defaults to 1.0.
Stage two of DIALOGUE: for every ordered pair of cell types and every candidate gene, a random-intercept mixed model over samples asking whether a cell's own programme score tracks the partner cell type's expression of that gene in the same sample.
params_dialogue_hlm( min_cells_per_sample = 2L, use_tme_qc = TRUE, use_cell_quality = TRUE, satterthwaite = TRUE )params_dialogue_hlm( min_cells_per_sample = 2L, use_tme_qc = TRUE, use_cell_quality = TRUE, satterthwaite = TRUE )
min_cells_per_sample |
Integer. Minimum cells a sample must contribute,
in both cell types of a pair, before it takes part in that pair's models.
Defaults to |
use_tme_qc |
Boolean. Include the partner cell type's mean quality in
that sample as a fixed effect. Upstream's |
use_cell_quality |
Boolean. Include the responding cell's own quality as
a fixed effect. Upstream's |
satterthwaite |
Boolean. Compute Satterthwaite denominator degrees of
freedom, as |
This stage dominates the runtime, and satterthwaite is the knob that
decides how badly. Turning it off falls back to the residual count for the
denominator degrees of freedom, which is far cheaper and barely differs once
a cell type has thousands of cells.
use_cell_quality conditions on the cell's own quality covariate, which
stage one has already regressed out of the scores by ordinary least squares.
The default conditions on it twice, because upstream does.
A named list with the following elements:
min_cells_per_sample - Integer. Minimum cells a sample must
contribute, in both cell types of a pair, before it takes part in that
pair's models. Defaults to 2L.
use_tme_qc - Boolean. Include the partner cell type's mean quality in
that sample as a fixed effect. Upstream's tme.qc. Defaults to TRUE.
use_cell_quality - Boolean. Include the responding cell's own quality
as a fixed effect. Upstream's cellQ. Defaults to TRUE.
satterthwaite - Boolean. Compute Satterthwaite denominator degrees of
freedom, as lmerTest does. Defaults to TRUE.
Jerby-Arnon & Regev, Nature Biotechnology, 2022
Stage one of DIALOGUE: the penalised matrix decomposition that turns the per-cell-type features into multicellular programmes, and the provisional gene signatures that come off it.
params_dialogue_pmd( k = 2L, n_permutations = 100L, extra_sparse = FALSE, abn_c = 15L, p_anova = 0.05, centre = TRUE, cap = 0.01, spatial = FALSE, n_genes = 200L, min_ci = 0.05, averaging = c("median", "mean"), mcp_assignment_p = 0.1, seed = 1234L )params_dialogue_pmd( k = 2L, n_permutations = 100L, extra_sparse = FALSE, abn_c = 15L, p_anova = 0.05, centre = TRUE, cap = 0.01, spatial = FALSE, n_genes = 200L, min_ci = 0.05, averaging = c("median", "mean"), mcp_assignment_p = 0.1, seed = 1234L )
k |
Integer. Number of multicellular programmes to extract. Must be at
least 1. Defaults to |
n_permutations |
Integer. Permutations backing the empirical p-value per
programme. Must be at least 2. Defaults to |
extra_sparse |
Boolean. Tune the L1 bound by permutation instead of
fixing it at |
abn_c |
Integer. Minimum cells a sample must contribute, within a cell
type, before it counts towards the feature-level ANOVA. Defaults to |
p_anova |
Numeric. BH-adjusted ANOVA cutoff for keeping a feature. Must
be in |
centre |
Boolean. Centre and scale the sample-level feature matrix, then
winsorise it. Defaults to |
cap |
Numeric. Winsorising tail fraction applied to each column. Must be
in |
spatial |
Boolean. Spatial data: skip the ANOVA feature filter entirely.
Niches are small, so a feature need not vary across them to be real. Defaults
to |
n_genes |
Integer. Genes taken per programme per direction when building
a signature. Defaults to |
min_ci |
Numeric. Minimum absolute correlation for a gene to enter a
signature. Must be in |
averaging |
String. How cell-level features are collapsed per sample.
One of |
mcp_assignment_p |
Numeric. Empirical p below which a cell type pair
counts as connected when deciding which cell types a programme spans. Must be
in |
seed |
Integer. Seed for the permutation null. Defaults to |
The defaults follow upstream's DLG.get.param. Two knobs are worth thinking
about before anything else. k is how many programmes you are asking for,
and there is no sweep to help you pick it. n_permutations sets the
resolution of the empirical p-value: with the default of 100 the smallest
p you can observe is 0.01, so lower it for a quick look and leave it alone
for anything you intend to believe.
averaging is exposed and honoured here. Upstream takes the same argument
and then ignores it, hard-coding column medians, so "median" is what every
published DIALOGUE run actually used.
A named list with the following elements:
k - Integer. Number of multicellular programmes to extract. Must be at
least 1. Defaults to 2L.
n_permutations - Integer. Permutations backing the empirical p-value
per programme. Must be at least 2. Defaults to 100L.
extra_sparse - Boolean. Tune the L1 bound by permutation instead of
fixing it at sqrt(p_1) / 2. Costs ten more fits per permutation. Defaults
to FALSE.
abn_c - Integer. Minimum cells a sample must contribute, within a cell
type, before it counts towards the feature-level ANOVA. Defaults to 15L.
p_anova - Numeric. BH-adjusted ANOVA cutoff for keeping a feature.
Must be in (0, 1]. Defaults to 0.05.
centre - Boolean. Centre and scale the sample-level feature matrix,
then winsorise it. Defaults to TRUE.
cap - Numeric. Winsorising tail fraction applied to each column. Must
be in [0, 0.5). Defaults to 0.01.
spatial - Boolean. Spatial data: skip the ANOVA feature filter
entirely. Niches are small, so a feature need not vary across them to be
real. Defaults to FALSE.
n_genes - Integer. Genes taken per programme per direction when
building a signature. Defaults to 200L.
min_ci - Numeric. Minimum absolute correlation for a gene to enter a
signature. Must be in [0, 1]. Defaults to 0.05.
averaging - String. How cell-level features are collapsed per sample.
One of c("median", "mean"). Defaults to "median".
mcp_assignment_p - Numeric. Empirical p below which a cell type pair
counts as connected when deciding which cell types a programme spans. Must
be in (0, 1]. Defaults to 0.1.
seed - Integer. Seed for the permutation null. Defaults to 1234L.
Jerby-Arnon & Regev, Nature Biotechnology, 2022
Stage three of DIALOGUE: the cross-partner meta-analysis that decides which genes survive, and the non-negative refit of the programme scores onto them.
params_dialogue_refine( support_p = 0.1, min_support_fraction = 0.333333333333333, min_stratum = 5L, early_stop_cor = 0.95, permissive_p = 0.001, strict_p = 0.05 )params_dialogue_refine( support_p = 0.1, min_support_fraction = 0.333333333333333, min_stratum = 5L, early_stop_cor = 0.95, permissive_p = 0.001, strict_p = 0.05 )
support_p |
Numeric. Adjusted p below which one partner counts as
supporting a gene. Must be in |
min_support_fraction |
Numeric. Minimum supporting fraction for a
stratum to enter the staged fit. Must be in |
min_stratum |
Integer. Minimum genes in a stratum before it is worth
fitting. Defaults to |
early_stop_cor |
Numeric. Correlation between the original score and the
running fit at which the staged fit stops early. Must be in |
permissive_p |
Numeric. Fisher-combined p for the permissive gene list,
where a gene is carried by partner support rather than by a positive
coefficient. Must be in |
strict_p |
Numeric. Fisher-combined p for the strict gene list, which
also demands that every partner supports the gene. Must be in |
Two gene lists come out. The permissive one asks only for a Fisher-combined
p below permissive_p; the strict one is looser on the p-value but also
demands that every partner supports the gene. They are not nested by
threshold, they are nested by evidence, and the strict list is the one to
quote.
A named list with the following elements:
support_p - Numeric. Adjusted p below which one partner counts as
supporting a gene. Must be in (0, 1]. Defaults to 0.1.
min_support_fraction - Numeric. Minimum supporting fraction for a
stratum to enter the staged fit. Must be in [0, 1]. Defaults to
0.3333333333333333.
min_stratum - Integer. Minimum genes in a stratum before it is worth
fitting. Defaults to 5L.
early_stop_cor - Numeric. Correlation between the original score and
the running fit at which the staged fit stops early. Must be in (0, 1].
Defaults to 0.95.
permissive_p - Numeric. Fisher-combined p for the permissive gene
list, where a gene is carried by partner support rather than by a positive
coefficient. Must be in (0, 1]. Defaults to 0.001.
strict_p - Numeric. Fisher-combined p for the strict gene list, which
also demands that every partner supports the gene. Must be in (0, 1].
Defaults to 0.05.
Jerby-Arnon & Regev, Nature Biotechnology, 2022
Parameters for the edgeR quasi-likelihood chain, implemented in
Rust via the edge-rs crate and gated against edgeR 4.8.2. Defaults are
edgeR's own. The legacy switch picks between two genuinely different
pipelines. The current route estimates its own dispersion from the most
abundant genes and skips estimateDisp(), which is where most of the runtime
went and is edgeR 4's own recommendation. The legacy route shrinks the raw
residual deviance, needs a dispersion handed to it, and is the only one where
the Poisson bound bites.
params_edger_ql( norm_method = c("TMM", "TMMwsp", "RLE", "upperquartile", "none"), filter = TRUE, min_mean = 0, robust = FALSE, legacy = FALSE )params_edger_ql( norm_method = c("TMM", "TMMwsp", "RLE", "upperquartile", "none"), filter = TRUE, min_mean = 0, robust = FALSE, legacy = FALSE )
norm_method |
String. Library size normalisation. |
filter |
Boolean. Run |
min_mean |
Numeric. Drop features whose mean count across samples is
below this. Applied on top of |
robust |
Boolean. Robust empirical Bayes squeezing, giving outlier
features their own smaller prior degrees of freedom. Defaults to |
legacy |
Boolean. Take edgeR's pre-4.0 quasi-likelihood pipeline.
Defaults to |
A named list with the following elements:
norm_method - String. Library size normalisation. "none" leaves
every factor at one, which is what Milo's logMS amounts to. One of
c("TMM", "TMMwsp", "RLE", "upperquartile", "none"). Defaults to "TMM".
filter - Boolean. Run filterByExpr() before fitting. Turn this off
for anything that is not gene expression, e.g. Milo neighbourhood counts,
where the heuristic means nothing. Defaults to TRUE.
min_mean - Numeric. Drop features whose mean count across samples is
below this. Applied on top of filter. Defaults to 0.0.
robust - Boolean. Robust empirical Bayes squeezing, giving outlier
features their own smaller prior degrees of freedom. Defaults to FALSE.
legacy - Boolean. Take edgeR's pre-4.0 quasi-likelihood pipeline.
Defaults to FALSE.
Chen, Lun and Smyth, F1000Research, 2016
Helper function to generate default parameters for the fast clustering for the doublet detection methods
params_fast_cluster_default()params_fast_cluster_default()
A named list with the following elements:
km_type - String. The type of k-means clustering. One of
c("minibatch", "standard"). Defaults to "minibatch".
n_centroids - Integer or NULL. The number of centroids to use.
NULL uses sqrt(N_cells) * 4 centroids. Defaults to NULL.
kmeans_iters - Integer. Number of maximum k-means iterations. Defaults
to 100L.
batch_size - Integer. Max batch size, capped at N_cells / 2
depending on the data set. Defaults to 4098L.
Wrapper function to generate resolution parameters for Leiden or Louvain clustering.
params_graph_resolution(min_res = 0.1, max_res = 10, number_res = 15L)params_graph_resolution(min_res = 0.1, max_res = 10, number_res = 15L)
min_res |
Numeric. Minimum resolution to test. Defaults to |
max_res |
Numeric. Maximum resolution to test. Defaults to |
number_res |
Integer. Number of resolutions to test between the
|
A named list with the following elements:
min_res - Numeric. Minimum resolution to test. Defaults to 0.1.
max_res - Numeric. Maximum resolution to test. Defaults to 10.0.
number_res - Integer. Number of resolutions to test between the
max_res and min_res. Defaults to 15L.
Wrapper function to generate GSEA parameters
params_gsea( min_size = 5L, max_size = 500L, gsea_param = 1, sample_size = 101L, eps = 1e-50 )params_gsea( min_size = 5L, max_size = 500L, gsea_param = 1, sample_size = 101L, eps = 1e-50 )
min_size |
Integer. Minimum number of genes per gene set. Defaults to
|
max_size |
Integer. Maximum number of genes per gene set. Defaults to
|
gsea_param |
Numeric. GSEA parameter. Defaults to |
sample_size |
Integer. Number of samples to iterate through for the
multi-level implementation of fgsea. Defaults to |
eps |
Numeric. Boundary for calculating the p-value. Used for the multi-
level implementation of fgsea. Defaults to |
A named list with the following elements:
min_size - Integer. Minimum number of genes per gene set. Defaults to
5L.
max_size - Integer. Maximum number of genes per gene set. Defaults to
500L.
gsea_param - Numeric. GSEA parameter. Defaults to 1.0.
sample_size - Integer. Number of samples to iterate through for the
multi-level implementation of fgsea. Defaults to 101L.
eps - Numeric. Boundary for calculating the p-value. Used for the
multi- level implementation of fgsea. Defaults to 1e-50.
Wrapper function to generate GSVA parameters
params_gsva( tau = 1, min_size = 5L, max_size = 500L, max_diff = TRUE, abs_rank = FALSE )params_gsva( tau = 1, min_size = 5L, max_size = 500L, max_diff = TRUE, abs_rank = FALSE )
tau |
Numeric. Tau parameter, usual recommendation is to use |
min_size |
Integer. Minimum number of genes per gene set. Defaults to
|
max_size |
Integer. Maximum number of genes per gene set. Defaults to
|
max_diff |
Boolean. Scoring mode for GSVA, if |
abs_rank |
Boolean. If |
A named list with the following elements:
tau - Numeric. Tau parameter, usual recommendation is to use 1.0
here. Larger values emphasise the tails more. Defaults to 1.0.
min_size - Integer. Minimum number of genes per gene set. Defaults to
5L.
max_size - Integer. Maximum number of genes per gene set. Defaults to
500L.
max_diff - Boolean. Scoring mode for GSVA, if TRUE = difference; if
FALSE = larger absolute value. Defaults to TRUE.
abs_rank - Boolean. If TRUE = pos - neg, FALSE = pos + neg.
Defaults to FALSE.
Helper function to generate HVG defaults
params_hvg_defaults()params_hvg_defaults()
A named list with the following elements:
min_gene_var_pctl - Numeric. Which percentile of the highly variable
genes to include. Defaults to 0.7.
hvg_method - String. Which method to use to identify HVG. "scran"
runs with the default trend parameters, see params_hvg_scran_defaults().
One of c("vst", "meanvarbin", "dispersion", "scran"). Defaults to "vst".
loess_span - Numeric. In case of "vst" the span of the loess
function. Defaults to 0.3.
clip_max - Numeric or NULL. The maximum clipping value (optional).
Defaults to NULL.
n_bins - Integer. The number of bins to use for the "meanvarbin" and
"dispersion" HVG detection. Defaults to 20L.
binning_strategy - String. Which binning strategy to use for
"meanvarbin" and "dispersion". One of c("equal_width", "equal_frequency"). Defaults to "equal_width".
Trend parameters for method = "scran" in params_sc_hvg().
They mirror scrapper's fitVarianceTrend() defaults before scrapper 1.8,
which switched use.min.width to TRUE. The lowess span comes from
loess_span in params_sc_hvg().
params_hvg_scran_defaults()params_hvg_scran_defaults()
A named list with the following elements:
mean_filter - Boolean. Shall genes below min_mean be left out of the
trend fit. Defaults to TRUE.
min_mean - Numeric. Minimum mean log-expression for a gene to enter
the trend fit. Defaults to 0.1.
transform - Boolean. Shall the variances be fourth-root transformed
before the fit. Defaults to TRUE.
use_min_width - Boolean. Shall the lowess window be defined by
min_width and min_window_count instead of the span. Defaults to FALSE.
min_width - Numeric. Minimum window width, only used with
use_min_width = TRUE. Defaults to 1.0.
min_window_count - Integer. Minimum number of genes per window, only
used with use_min_width = TRUE. Defaults to 200L.
Wrapper function for standard ICA parameters
params_ica_general(maxit = 200L, alpha = 1, max_tol = 1e-04, verbose = FALSE)params_ica_general(maxit = 200L, alpha = 1, max_tol = 1e-04, verbose = FALSE)
maxit |
Integer. Maximum number of iterations for ICA. Defaults to
|
alpha |
Numeric. The alpha parameter for the logcosh version of ICA.
Should be between 1 to 2. Defaults to |
max_tol |
Numeric. Should be |
verbose |
Boolean. Controls verbosity of the function. Defaults to
|
A named list with the following elements:
maxit - Integer. Maximum number of iterations for ICA. Defaults to
200L.
alpha - Numeric. The alpha parameter for the logcosh version of ICA.
Should be between 1 to 2. Defaults to 1.0.
max_tol - Numeric. Should be 0 < max_tol < 1. Maximum tolerance of
the algorithm. Defaults to 1e-04.
verbose - Boolean. Controls verbosity of the function. Defaults to
FALSE.
Wrapper function to provide parameters through which ncomps to iterate through.
params_ica_ncomp(max_no_comp = 75L, steps = 5L, custom_seq = NULL)params_ica_ncomp(max_no_comp = 75L, steps = 5L, custom_seq = NULL)
max_no_comp |
Integer. Maximum number of ncomp to test. Defaults to
|
steps |
Integer. In which steps to move from 5 onwards. Defaults to
|
custom_seq |
Integer vector or |
A named list with the following elements:
max_no_comp - Integer. Maximum number of ncomp to test. Defaults to
75L.
steps - Integer. In which steps to move from 5 onwards. Defaults to
5L.
custom_seq - Integer vector or NULL. If you wish to provide a custom
version of no_comp to iterate through. If NULL, you will iterate through
c(2, 3, 4, 5, 5 + step, ... max_no_comp - step, max_no_comp) Defaults to
NULL.
Wrapper function for ICA randomisation
params_ica_randomisation( cross_validate = FALSE, random_init = 50L, folds = 10L )params_ica_randomisation( cross_validate = FALSE, random_init = 50L, folds = 10L )
cross_validate |
Boolean. Do you want to apply a cross-validation type
approach and split the data into |
random_init |
Integer. Number of random initialisations to use. Defaults
to |
folds |
Integer. Number of folds to use if |
A named list with the following elements:
cross_validate - Boolean. Do you want to apply a cross-validation type
approach and split the data into folds folds to assess within data
stability of the component. Defaults to FALSE.
random_init - Integer. Number of random initialisations to use.
Defaults to 50L.
folds - Integer. Number of folds to use if cross_validate is set to
TRUE. To note, you will be running random_init * folds ICA runs.
Defaults to 10L.
Helper function to generate defaults for the k-mean clustering were more control is needed.
params_kmeans_defaults()params_kmeans_defaults()
A named list with the following elements:
k_means_iter - Integer. The number of iterations to use for the
clustering. Defaults to 30L.
k_means_init - String. The initialisation. One of c("parallel", "random"). Defaults to "parallel".
gemm - Boolean or NULL. Controls which CPU implementation is used by
the method. GEMM is faster with large dimensionality. Defaults to FALSE.
hamerly - Boolean or NULL. Shall a faster exact method be used
leveraging the triangle inequality. Faster on large data sets with large
numbers of centroids. Defaults to TRUE.
This function generates various sensible default parameters for all of the different approximate nearest neighbours that are available within this package.
params_knn_defaults()params_knn_defaults()
A named list with the following elements:
k - Integer. Number of neighbours. Defaults to 15L.
knn_method - String. Which method to use for the approximate nearest
neighbour search. One of c("kmknn", "hnsw", "annoy", "nndescent", "ivf", "exhaustive"). Defaults to "kmknn".
ann_dist - String. Which distance metric to use for the approximate
nearest neighbour search. One of c("euclidean", "cosine"). Defaults to
"euclidean".
n_trees - Integer. Annoy param: number of trees to generate for Annoy.
Defaults to 50L.
search_budget - Integer or NULL. Annoy param: optional search budget
per tree for Annoy. If not provided, it will default to n_tree * k * 20L.
Defaults to NULL.
delta - Numeric. NNDescent param: early termination criterium for
NNDescent. Defaults to 0.001.
diversify_prob - Numeric. NNDescent param: diversification probability
for the NNDescent index. This will diversify the index at the end and
identify potentially better edges. Defaults to 0.0.
ef_budget - Integer or NULL. NNDescent param: optional query budget
parameter. Can accelerate querying, but at the cost of Recall. Defaults to
NULL.
extract_knn - Boolean. NNDescent param: hand back the graph the
descent already built instead of beam searching it. Skips the query pass
entirely, so it is much faster, at the cost of some recall. Rows the descent
never filled come back padded with duplicate edges. Ignored by every other
method. Defaults to FALSE.
m - Integer. HNSW param: number of connections between layers for
HNSW. Defaults to 16L.
ef_construction - Integer. HNSW param: size of dynamic candidate list
during construction. Defaults to 200L.
ef_search - Integer. HNSW param: size of candidate list (higher =
better recall, slower). Defaults to 100L.
n_list - Integer or NULL. IVF param: number of clusters/centroids to
generate. NULL generates sqrt(n) lists. Defaults to NULL.
n_probe - Integer or NULL. IVF param: number of clusters/centroids
to query. NULL queries sqrt(n_lists) clusters. Defaults to NULL.
Wrapper function to generate label propagation parameters
params_label_propagation( alpha = 0.9, iter = 100L, tolerance = 1e-06, symmetrise = TRUE, symmetry_strategy = c("average", "avg", "min", "max"), max_hops = NULL )params_label_propagation( alpha = 0.9, iter = 100L, tolerance = 1e-06, symmetrise = TRUE, symmetry_strategy = c("average", "avg", "min", "max"), max_hops = NULL )
alpha |
Numeric. Controls the spreading strength. Higher values anchor
labelled nodes more strongly to their original label. Defaults to |
iter |
Integer. Maximum number of iterations to run. Defaults to |
tolerance |
Numeric. Convergence threshold. Stops early if the maximum
change across all nodes falls below this value. Defaults to |
symmetrise |
Boolean. Whether to symmetrise the graph. Defaults to
|
symmetry_strategy |
String. Strategy to resolve weight conflicts when
symmetrising. Only relevant when |
max_hops |
Integer or |
A named list with the following elements:
alpha - Numeric. Controls the spreading strength. Higher values anchor
labelled nodes more strongly to their original label. Defaults to 0.9.
iter - Integer. Maximum number of iterations to run. Defaults to
100L.
tolerance - Numeric. Convergence threshold. Stops early if the maximum
change across all nodes falls below this value. Defaults to 1e-06.
symmetrise - Boolean. Whether to symmetrise the graph. Defaults to
TRUE.
symmetry_strategy - String. Strategy to resolve weight conflicts when
symmetrising. Only relevant when symmetrise = TRUE and edge weights are
provided. One of c("average", "avg", "min", "max"). Defaults to
"average".
max_hops - Integer or NULL. If provided, restricts label spreading
to nodes within this many hops of any labelled node. Nodes beyond this limit
remain all-zeroes. Defaults to NULL.
Solver options for the variational Bayes latent Dirichlet
allocation, see run_lda().
params_lda( alpha = 50, alpha_by_topic = TRUE, eta = 0.1, eta_by_topic = FALSE, max_iter = 150L, tol = 0.001, inner_max_iter = 100L, inner_tol = 0.001, check_every = 10L, learning = c("batch", "online"), batch_size = 1024L, n_epochs = 10L )params_lda( alpha = 50, alpha_by_topic = TRUE, eta = 0.1, eta_by_topic = FALSE, max_iter = 150L, tol = 0.001, inner_max_iter = 100L, inner_tol = 0.001, check_every = 10L, learning = c("batch", "online"), batch_size = 1024L, n_epochs = 10L )
alpha |
Numeric. Dirichlet prior on the document-topic distributions.
Defaults to |
alpha_by_topic |
Boolean. Shall |
eta |
Numeric. Dirichlet prior on the topic-term distributions. Defaults
to |
eta_by_topic |
Boolean. Shall |
max_iter |
Integer. Maximum outer iterations. Ignored by the online
variant, which counts epochs instead. Defaults to |
tol |
Numeric. Relative change in the bound below which the solver
stops. Defaults to |
inner_max_iter |
Integer. Maximum fixed-point iterations of the
per-document E-step. Defaults to |
inner_tol |
Numeric. Relative L1 change in the variational parameters
below which the per-document E-step stops. Defaults to |
check_every |
Integer. Iterations between bound evaluations. Defaults to
|
learning |
String. Batch or online variational inference. One of
|
batch_size |
Integer. Documents per mini-batch. Online only. Defaults to
|
n_epochs |
Integer. Passes over the corpus. Online only. Defaults to
|
The defaults follow pycisTopic, so the knobs mean the same thing on both
sides. alpha_by_topic = TRUE turns alpha into the Griffiths and
Steyvers 50 / k heuristic that cisTopic defaults to; set it to FALSE if
you want alpha taken literally.
learning = "batch" sweeps every document once per iteration and is
monotone in the bound. "online" takes decaying steps from shuffled
mini-batches, which reaches a usable fit in far fewer passes on a large
corpus at the cost of that guarantee. batch_size and n_epochs are only
read by the online variant.
A named list with the following elements:
alpha - Numeric. Dirichlet prior on the document-topic distributions.
Defaults to 50.0.
alpha_by_topic - Boolean. Shall alpha be divided by the topic count.
Defaults to TRUE.
eta - Numeric. Dirichlet prior on the topic-term distributions.
Defaults to 0.1.
eta_by_topic - Boolean. Shall eta be divided by the topic count.
Defaults to FALSE.
max_iter - Integer. Maximum outer iterations. Ignored by the online
variant, which counts epochs instead. Defaults to 150L.
tol - Numeric. Relative change in the bound below which the solver
stops. Defaults to 0.001.
inner_max_iter - Integer. Maximum fixed-point iterations of the
per-document E-step. Defaults to 100L.
inner_tol - Numeric. Relative L1 change in the variational parameters
below which the per-document E-step stops. Defaults to 0.001.
check_every - Integer. Iterations between bound evaluations. Defaults
to 10L.
learning - String. Batch or online variational inference. One of
c("batch", "online"). Defaults to "batch".
batch_size - Integer. Documents per mini-batch. Online only. Defaults
to 1024L.
n_epochs - Integer. Passes over the corpus. Online only. Defaults to
10L.
Hoffman, Blei and Bach, NIPS, 2010; Bravo Gonzalez-Blas, et al., Nat Methods, 2019
Parameters for ligand to target influence computation
params_ligand_target( lr_sig_hub = 0, gr_hub = 0, ltf_cutoff = 0.99, damping_factor = 0.5, tol = 1e-12, max_iter = 1000L, topology_correction = FALSE, secondary_targets = FALSE )params_ligand_target( lr_sig_hub = 0, gr_hub = 0, ltf_cutoff = 0.99, damping_factor = 0.5, tol = 1e-12, max_iter = 1000L, topology_correction = FALSE, secondary_targets = FALSE )
lr_sig_hub |
Numeric. Numeric in |
gr_hub |
Numeric. Numeric in |
ltf_cutoff |
Numeric. Numeric in |
damping_factor |
Numeric. Numeric in |
tol |
Numeric. Numeric > 0. Convergence tolerance (L1 change) for the
propagation step. With |
max_iter |
Integer. Integer >= 1. Maximum iterations for the propagation
step. Defaults to |
topology_correction |
Boolean. Apply topology correction. Defaults to
|
secondary_targets |
Boolean. Run a second round through targets.
Defaults to |
A named list with the following elements:
lr_sig_hub - Numeric. Numeric in [0, 1]. Hub correction strength for
the ligand-receptor / signalling layer. 0 disables correction. Defaults to
0.0.
gr_hub - Numeric. Numeric in [0, 1]. Hub correction strength for the
gene regulatory layer. 0 disables correction. Defaults to 0.0.
ltf_cutoff - Numeric. Numeric in [0, 1]. Quantile cutoff applied to
the intermediate ligand-to-TF matrix. Defaults to 0.99.
damping_factor - Numeric. Numeric in [0, 1]. PageRank-style damping
factor. Defaults to 0.5.
tol - Numeric. Numeric > 0. Convergence tolerance (L1 change) for the
propagation step. With ltf_cutoff > 0 the output is only stable once the
propagation has converged well below the score gaps around the quantile, so
keep this tight. Defaults to 1e-12.
max_iter - Integer. Integer >= 1. Maximum iterations for the
propagation step. Defaults to 1000L.
topology_correction - Boolean. Apply topology correction. Defaults to
FALSE.
secondary_targets - Boolean. Run a second round through targets.
Defaults to FALSE.
Parameters for the limma linear model chain, implemented in Rust
via the edge-rs crate and gated against limma 3.66.0. Defaults are limma's
own, except for filter: inside BulkDge() the genes were already
filtered by qc_bulk_dge(). route = "voom" is voomLmFit():
precision weights from the mean-variance trend, then weighted least squares.
route = "trend" is limma-trend: log-CPM straight into lmFit(), with the
trend absorbed by eBayes(trend = TRUE). The empirical Bayes trend follows
the route.
params_limma_voom( route = c("voom", "trend"), norm_method = c("TMM", "TMMwsp", "RLE", "upperquartile", "none"), filter = FALSE, min_mean = 0, robust = FALSE, prior_count = NULL, adaptive_span = TRUE, span = 0.5, proportion = 0.01 )params_limma_voom( route = c("voom", "trend"), norm_method = c("TMM", "TMMwsp", "RLE", "upperquartile", "none"), filter = FALSE, min_mean = 0, robust = FALSE, prior_count = NULL, adaptive_span = TRUE, span = 0.5, proportion = 0.01 )
route |
String. Whether to run the voom or the limma-trend route. One of
|
norm_method |
String. Library size normalisation. One of |
filter |
Boolean. Run |
min_mean |
Numeric. Drop genes whose mean count across samples is below
this. Applied on top of |
robust |
Boolean. Robust empirical Bayes, |
prior_count |
Numeric or |
adaptive_span |
Boolean. Derive the lowess span from the number of
genes, as limma does since 3.56. Only used by voom. Defaults to |
span |
Numeric. Lowess span for the voom trend, only read if
|
proportion |
Numeric. Assumed proportion of differentially expressed
genes, only used for the B-statistic. Defaults to |
A named list with the following elements:
route - String. Whether to run the voom or the limma-trend route. One
of c("voom", "trend"). Defaults to "voom".
norm_method - String. Library size normalisation. One of c("TMM", "TMMwsp", "RLE", "upperquartile", "none"). Defaults to "TMM".
filter - Boolean. Run filterByExpr() before fitting. Defaults to
FALSE.
min_mean - Numeric. Drop genes whose mean count across samples is
below this. Applied on top of filter. Defaults to 0.0.
robust - Boolean. Robust empirical Bayes, eBayes(robust = TRUE).
Defaults to FALSE.
prior_count - Numeric or NULL. Count added before the log. NULL
takes the route's own default, 0.5 for voom and 2 for trend. Defaults to
NULL.
adaptive_span - Boolean. Derive the lowess span from the number of
genes, as limma does since 3.56. Only used by voom. Defaults to TRUE.
span - Numeric. Lowess span for the voom trend, only read if
adaptive_span = FALSE. Defaults to 0.5.
proportion - Numeric. Assumed proportion of differentially expressed
genes, only used for the B-statistic. Defaults to 0.01.
Law, et al., Genome Biol, 2014; Smyth, Stat Appl Genet Mol Biol, 2004
Constructor for MELD parameters
params_meld( beta = 60, offset = 0, order = 1, filter = c("heat", "laplacian"), chebyshev_order = 50L, lap_type = c("combinatorial", "normalised"), normalise_indicators = TRUE, knn = list() )params_meld( beta = 60, offset = 0, order = 1, filter = c("heat", "laplacian"), chebyshev_order = 50L, lap_type = c("combinatorial", "normalised"), normalise_indicators = TRUE, knn = list() )
beta |
Numeric. Smoothing strength; larger values produce smoother
densities. Must be strictly positive. Defaults to |
offset |
Numeric. Shift of the filter centre in the rescaled spectrum.
Must be in |
order |
Numeric. Filter falloff sharpness; larger values approach a
square low-pass. Must be strictly positive. Defaults to |
filter |
String. Filter family to use. One of |
chebyshev_order |
Integer. Number of Chebyshev coefficients (polynomial
terms). Must be >= 2. Defaults to |
lap_type |
String. Type of Laplacian to use for spectral filtering. One
of |
normalise_indicators |
Boolean. If |
knn |
List. Optional overrides for kNN parameters. See
|
A named list with the following elements:
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
beta - Numeric. Smoothing strength; larger values produce smoother
densities. Must be strictly positive. Defaults to 60.0.
offset - Numeric. Shift of the filter centre in the rescaled spectrum.
Must be in [0, 1]. Defaults to 0.0.
order - Numeric. Filter falloff sharpness; larger values approach a
square low-pass. Must be strictly positive. Defaults to 1.0.
filter - String. Filter family to use. One of c("heat", "laplacian"). Defaults to "heat".
chebyshev_order - Integer. Number of Chebyshev coefficients
(polynomial terms). Must be >= 2. Defaults to 50L.
lap_type - String. Type of Laplacian to use for spectral filtering.
One of c("combinatorial", "normalised"). Defaults to "combinatorial".
normalise_indicators - Boolean. If TRUE, each column of the
indicator matrix is divided by its column sum before filtering, making
cross-condition densities comparable regardless of cells-per-condition.
Defaults to TRUE.
Controls how a gene x k loading matrix from ICA, NMF or DGRDL
is turned into module membership. Genes are kept where they sit in the tail
of a component's loading distribution, which means membership is not
exclusive: a gene loading strongly on three components belongs to three
modules. Genes in no tail belong to nothing, which is the background category
an argmax assignment cannot give you.
params_module_membership( method = c("zscore", "fdr"), cutoff = 3, fdr = 0.05, tails = c("auto", "upper", "both"), scaling = c("robust", "standard") )params_module_membership( method = c("zscore", "fdr"), cutoff = 3, fdr = 0.05, tails = c("auto", "upper", "both"), scaling = c("robust", "standard") )
method |
String. |
cutoff |
Numeric. Absolute z threshold for |
fdr |
Numeric. Adjusted p-value threshold for |
tails |
String. |
scaling |
String. |
A named list with the following elements:
method - String. "zscore" standardises each component and keeps
abs(z) > cutoff. "fdr" converts to two-sided p-values against a Normal
null fitted the same way, Benjamini-Hochberg adjusts, and keeps padj < fdr. One of c("zscore", "fdr"). Defaults to "zscore".
cutoff - Numeric. Absolute z threshold for method = "zscore".
Defaults to 3.0.
fdr - Numeric. Adjusted p-value threshold for method = "fdr".
Defaults to 0.05.
tails - String. "auto" uses an upper-tail-only test when every
loading is non-negative (the NMF case) and a two-sided one otherwise.
"upper" and "both" force the choice. One of c("auto", "upper", "both"). Defaults to "auto".
scaling - String. "robust" centres and scales each component by its
median and MAD. "standard" uses the mean and standard deviation instead,
which is stricter and less forgiving of skewed loadings (e.g. NMF). One of
c("robust", "standard"). Defaults to "robust".
Biton, et al., Cell Rep, 2014
Parameters for the NEBULA negative binomial gamma mixed model,
implemented in Rust via the edge-rs crate and ported from the nebula
package's own C++. Defaults are the R package's own. NEBULA splits the
variance into a subject-level random effect and a cell-level overdispersion.
Run it on meta cells and the cell-level term becomes the spread between
aggregates within a subject rather than between cells, so it is smaller and
absorbs whatever the aggregation smoothed away. The subject-level term keeps
its meaning either way.
params_nebula( nebula_method = c("ln", "hl"), min_sigma = 1e-04, min_phi = 1e-04, max_sigma = 10, max_phi = 1000, cutoff_cell = 20, kappa = 800, cpc = 0.005, mincp = 5L, min_subjects = 0L, reml = FALSE, eps = 1e-06, gene_batch_size = 1000L, shrink_dispersion = TRUE )params_nebula( nebula_method = c("ln", "hl"), min_sigma = 1e-04, min_phi = 1e-04, max_sigma = 10, max_phi = 1000, cutoff_cell = 20, kappa = 800, cpc = 0.005, mincp = 5L, min_subjects = 0L, reml = FALSE, eps = 1e-06, gene_batch_size = 1000L, shrink_dispersion = TRUE )
nebula_method |
String. Which variant to run. NEBULA downgrades |
min_sigma |
Numeric. Lower bound on the subject-level overdispersion.
Defaults to |
min_phi |
Numeric. Lower bound on the cell-level overdispersion.
Defaults to |
max_sigma |
Numeric. Upper bound on the subject-level overdispersion.
Defaults to |
max_phi |
Numeric. Upper bound on the cell-level overdispersion.
Defaults to |
cutoff_cell |
Numeric. Refit both overdispersions when the product of
the cells per subject and the estimated |
kappa |
Numeric. Threshold on NEBULA's |
cpc |
Numeric. Drop a gene whose mean count per cell is at most this.
Defaults to |
mincp |
Integer. Drop a gene expressed in fewer than this many cells.
Defaults to |
min_subjects |
Integer. Drop a gene that fewer than this many subjects
express, a subject expressing it when its own mean count per cell is above
|
reml |
Boolean. Estimate the overdispersions by restricted maximum
likelihood. The R package only honours this for |
eps |
Numeric. Absolute stopping tolerance for the optimiser. Defaults
to |
gene_batch_size |
Integer. Genes read and fitted per batch. Bounds how
much of the store is resident at once and changes nothing about the answer,
since NEBULA is gene-independent. Defaults to |
shrink_dispersion |
Boolean. Shrink the cell-level overdispersions
towards an empirical Bayes prior once the sweep is done. Defaults to |
A named list with the following elements:
nebula_method - String. Which variant to run. NEBULA downgrades "ln"
to "hl" below 30 cells per subject, as the R package does. One of c("ln", "hl"). Defaults to "ln".
min_sigma - Numeric. Lower bound on the subject-level overdispersion.
Defaults to 1e-04.
min_phi - Numeric. Lower bound on the cell-level overdispersion.
Defaults to 1e-04.
max_sigma - Numeric. Upper bound on the subject-level overdispersion.
Defaults to 10.0.
max_phi - Numeric. Upper bound on the cell-level overdispersion.
Defaults to 1000.0.
cutoff_cell - Numeric. Refit both overdispersions when the product of
the cells per subject and the estimated phi falls below this. Defaults to
20.0.
kappa - Numeric. Threshold on NEBULA's kappa_obs above which the
subject-level overdispersion from stage one is trusted as is. Defaults to
800.0.
cpc - Numeric. Drop a gene whose mean count per cell is at most this.
Defaults to 0.005.
mincp - Integer. Drop a gene expressed in fewer than this many cells.
Defaults to 5L.
min_subjects - Integer. Drop a gene that fewer than this many subjects
express, a subject expressing it when its own mean count per cell is above
cpc. cpc and mincp pool every cell, so one subject can carry a gene
through on its own. 0 switches the check off, as in the nebula package.
Defaults to 0L.
reml - Boolean. Estimate the overdispersions by restricted maximum
likelihood. The R package only honours this for NBLMM, which the Rust port
does not implement, so this arm has not been validated against an R
reference. Leave it off unless you know why you want it. Defaults to
FALSE.
eps - Numeric. Absolute stopping tolerance for the optimiser. Defaults
to 1e-06.
gene_batch_size - Integer. Genes read and fitted per batch. Bounds how
much of the store is resident at once and changes nothing about the answer,
since NEBULA is gene-independent. Defaults to 1000L.
shrink_dispersion - Boolean. Shrink the cell-level overdispersions
towards an empirical Bayes prior once the sweep is done. Defaults to TRUE.
He, et al., Commun Biol, 2021
Controls the clustering step of consensus NMF: the components of
every restart are pooled, outliers are dropped by local density, and the
survivors are k-means clustered into k groups whose median becomes the
consensus factor.
params_nmf_consensus( consensus_target = c("h", "w"), n_neighbours = 0L, density_threshold = 0.5, kmeans_iters = 100L, kmeans_n_init = 3L )params_nmf_consensus( consensus_target = c("h", "w"), n_neighbours = 0L, density_threshold = 0.5, kmeans_iters = 100L, kmeans_n_init = 3L )
consensus_target |
String. |
n_neighbours |
Integer. Neighbours used for the local density estimate.
|
density_threshold |
Numeric. Components whose mean cosine distance to
their neighbours exceeds this are dropped as unstable. Cosine distance cannot
exceed 2, so any value |
kmeans_iters |
Integer. Maximum k-means iterations. Defaults to |
kmeans_n_init |
Integer. Number of k-means restarts. Defaults to |
A named list with the following elements:
consensus_target - String. "h" clusters the gene programmes (the
spectra), which is what cNMF does and what you almost always want. "w"
clusters in sample space instead, which on single cell data means cell
space: it pools a dense (k * n_runs) x n_cells matrix and runs an
exhaustive cosine search over it, so it gets expensive fast. One of c("h", "w"). Defaults to "h".
n_neighbours - Integer. Neighbours used for the local density
estimate. 0L picks ceiling(0.3 * n_runs) for you. Defaults to 0L.
density_threshold - Numeric. Components whose mean cosine distance to
their neighbours exceeds this are dropped as unstable. Cosine distance
cannot exceed 2, so any value >= 2 disables the filter entirely. Defaults
to 0.5.
kmeans_iters - Integer. Maximum k-means iterations. Defaults to
100L.
kmeans_n_init - Integer. Number of k-means restarts. Defaults to 3L.
Kotliar et al., eLife, 2019
Wrapper function for NMF (HALS) parameters
params_nmf_hals( max_iter = 250L, tol = 1e-04, eps = 1e-10, check_every = 10L, nmf_init = c("nndsvd", "svd", "random") )params_nmf_hals( max_iter = 250L, tol = 1e-04, eps = 1e-10, check_every = 10L, nmf_init = c("nndsvd", "svd", "random") )
max_iter |
Integer. Maximum number of HALS iterations. Defaults to
|
tol |
Numeric. Convergence tolerance on the relative change in
reconstruction loss. Defaults to |
eps |
Numeric. Numerical floor for non-negativity / division safety.
Defaults to |
check_every |
Integer. Convergence check interval in iterations.
Defaults to |
nmf_init |
String. |
A named list with the following elements:
max_iter - Integer. Maximum number of HALS iterations. Defaults to
250L.
tol - Numeric. Convergence tolerance on the relative change in
reconstruction loss. Defaults to 1e-04.
eps - Numeric. Numerical floor for non-negativity / division safety.
Defaults to 1e-10.
check_every - Integer. Convergence check interval in iterations.
Defaults to 10L.
nmf_init - String. "nndsvd" and "svd" both map to deterministic
NNDSVD initialisation; "random" uses random non-negative draws. For
stabilised (multi-run) NMF this field is ignored and random init is always
used. One of c("nndsvd", "svd", "random"). Defaults to "nndsvd".
Helper function to generate normalisation defaults for doublet detection.
params_norm_doublets_defaults()params_norm_doublets_defaults()
A named list with the following elements:
log_transform - Boolean. Shall the counts be log-normalised. Defaults
to TRUE.
mean_center - Boolean. Shall mean centring be applied. Defaults to
FALSE.
normalise_variance - Boolean. Shall the variance be normalised.
Defaults to FALSE.
target_size - Numeric. Target library size. Defaults to 1000000.0.
Helper function to generate default parameters for PCA
params_pca_defaults()params_pca_defaults()
A named list with the following elements:
no_pcs - Integer. Number of PCs to consider. Defaults to 30L.
svd_solver - String. Which solver to use. "randomised" (default) is
a randomised SVD, approximate in the trailing components. "covariance"
builds the gene x gene cross-product and eigendecomposes it. "exact" is
Lanczos on the sparse path and a full SVD on the dense one. See
params_sc_pca() for the trade-offs. One of c("randomised", "covariance", "exact"). Defaults to "randomised".
Parameters for PCA with missing values via probabilistic PCA,
see run_ppca(). Defaults follow pcaMethods.
params_ppca( n_pcs = 2L, max_iter = 1000L, tol = 1e-05, seed = 42L, centre = TRUE, scale = FALSE )params_ppca( n_pcs = 2L, max_iter = 1000L, tol = 1e-05, seed = 42L, centre = TRUE, scale = FALSE )
n_pcs |
Integer. Number of principal components. Defaults to |
max_iter |
Integer. Maximum number of EM iterations. Defaults to
|
tol |
Numeric. Relative change in the objective below which EM stops.
Defaults to |
seed |
Integer. Seed for the random initial loadings. Defaults to |
centre |
Boolean. Shall the observed column means be subtracted first.
Defaults to |
scale |
Boolean. Shall the columns be divided by their observed standard
deviation first (pcaMethods' |
A named list with the following elements:
n_pcs - Integer. Number of principal components. Defaults to 2L.
max_iter - Integer. Maximum number of EM iterations. Defaults to
1000L.
tol - Numeric. Relative change in the objective below which EM stops.
Defaults to 1e-05.
seed - Integer. Seed for the random initial loadings. Defaults to
42L.
centre - Boolean. Shall the observed column means be subtracted first.
Defaults to TRUE.
scale - Boolean. Shall the columns be divided by their observed
standard deviation first (pcaMethods' "uv"). Defaults to FALSE.
Tipping and Bishop, J R Stat Soc B, 1999; Stacklies, et al., Bioinformatics, 2007
The closed-form alternative to scTransform: one shared dispersion instead of a fitted model per gene. Much cheaper, and on most data sets it ranks genes about as well.
params_sc_apr(theta = 100, min_cells = 5L, clip_min = NULL, clip_max = NULL)params_sc_apr(theta = 100, min_cells = 5L, clip_min = NULL, clip_max = NULL)
theta |
Numeric. The shared negative binomial dispersion. |
min_cells |
Integer. Minimum number of cells a gene must be detected in
to be retained. |
clip_min |
Numeric or |
clip_max |
Numeric or |
A named list with the following elements:
theta - Numeric. The shared negative binomial dispersion. Inf gives
the Poisson limit. Defaults to 100.0.
min_cells - Integer. Minimum number of cells a gene must be detected
in to be retained. 0L keeps everything. Defaults to 5L.
clip_min - Numeric or NULL. Lower residual clipping bound. NULL
uses -sqrt(n_cells). Must be given together with clip_max. Defaults to
NULL.
clip_max - Numeric or NULL. Upper residual clipping bound. NULL
uses sqrt(n_cells). Must be given together with clip_min. Defaults to
NULL.
Lause, Berens and Kobak, Genome Biology, 2021.
The three statistics consume the same within-cell ranking, but
weight it very differently. "wilcox" is a pure function of the gene set's
rank sum, so a gene at rank 2 and one at rank 200 count for almost the same
thing. "recovery" and "ap" are top-heavy. For SCENIC use "recovery",
the default. "wilcox" is a bixverse addition and its flatter score does not
separate into on/off populations, so it binarises badly. Ranking ties are
averaged (midranks) rather than broken at random the way AUCell does it,
which is why there is no need for SCENIC's trick of setting the cutoff to the
1st percentile of genes detected per cell. Undetected genes all collapse onto
one rank well outside any sensible max_rank. Note the recovery AUC is
normalised by max_rank * length(gene_set), matching pySCENIC and AUCell's
normAUC = FALSE. Modern AUCell divides by the attainable maximum instead,
so absolute values differ by a per-gene-set constant. Cell ordering within a
regulon is unaffected.
params_sc_aucell( auc_type = c("recovery", "wilcox", "ap"), max_rank = NULL, standardise = FALSE )params_sc_aucell( auc_type = c("recovery", "wilcox", "ap"), max_rank = NULL, standardise = FALSE )
auc_type |
String. Which statistic to calculate. |
max_rank |
Numeric or |
standardise |
Boolean. Shall each gene set's scores be z-scored across
the cells. This is what makes |
A named list with the following elements:
auc_type - String. Which statistic to calculate. "wilcox" is the AUC
derived from the Mann-Whitney U statistic over the full ranking, with the
null at 0.5 for any gene set size. "recovery" is the recovery-curve AUC
under a rank cutoff, i.e. the actual AUCell statistic of Aibar, et al.
"ap" is average precision, the most top-heavy of the three, but its null
tracks the gene set prevalence so raw values are not comparable across gene
sets of different size unless standardise is on. One of c("recovery", "wilcox", "ap"). Defaults to "recovery".
max_rank - Numeric or NULL. Rank cutoff for "recovery", counted
from the top of the within-cell ranking. If NULL, resolves to the top 5%
of the gene universe, following Aibar, et al. Ignored by the other two
statistics. Defaults to NULL.
standardise - Boolean. Shall each gene set's scores be z-scored across
the cells. This is what makes "ap" comparable across gene sets of
different size. Defaults to FALSE.
Aibar, et al., Nat Methods, 2017
Wrapper function for the BBKNN parameters
params_sc_bbknn( neighbours_within_batch = 3L, set_op_mix_ratio = 1, local_connectivity = 1, trim = NULL, knn = list() )params_sc_bbknn( neighbours_within_batch = 3L, set_op_mix_ratio = 1, local_connectivity = 1, trim = NULL, knn = list() )
neighbours_within_batch |
Integer. Number of neighbours to consider per
batch. Defaults to |
set_op_mix_ratio |
Numeric. Mixing ratio between union (1.0) and
intersection (0.0). Defaults to |
local_connectivity |
Numeric. UMAP connectivity computation parameter,
how many nearest neighbours of each cell are assumed to be fully connected.
Defaults to |
trim |
Integer or |
knn |
List. Optional overrides for kNN parameters. See
|
A named list with the following elements:
neighbours_within_batch - Integer. Number of neighbours to consider
per batch. Defaults to 3L.
set_op_mix_ratio - Numeric. Mixing ratio between union (1.0) and
intersection (0.0). Defaults to 1.0.
local_connectivity - Numeric. UMAP connectivity computation parameter,
how many nearest neighbours of each cell are assumed to be fully connected.
Defaults to 1.0.
trim - Integer or NULL. Trim the neighbours of each cell to these
many top connectivities. May help with population independence and improve
the tidiness of clustering. If NULL, it defaults to 10 * neighbours_within_batch. Defaults to NULL.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
Parameters for bonsai_sc(). Sanity turns the raw
counts into posterior log fold changes with error bars, Bonsai builds a tree
over the cells from those, and the tree is laid out in 2D for plotting. The
Sanity defaults come from sanity-sc-rs, the search defaults from bonsai-rs.
params_sc_bonsai( variance_rule = c("marginalise", "posterior_mean", "max_posterior", "fixed"), fixed_variance = NULL, variance_bins = 161L, variance_min = 0.001, variance_max = 50, min_signal_to_noise = 1, start = c("linkage", "greedy_merge"), spr_search = c("approximate", "exact"), nni_search = c("approximate", "exact"), seed = 42L, reroot = TRUE, layout = c("equal_angle", "equal_daylight", "dendrogram"), hyperbolic = FALSE )params_sc_bonsai( variance_rule = c("marginalise", "posterior_mean", "max_posterior", "fixed"), fixed_variance = NULL, variance_bins = 161L, variance_min = 0.001, variance_max = 50, min_signal_to_noise = 1, start = c("linkage", "greedy_merge"), spr_search = c("approximate", "exact"), nni_search = c("approximate", "exact"), seed = 42L, reroot = TRUE, layout = c("equal_angle", "equal_daylight", "dendrogram"), hyperbolic = FALSE )
variance_rule |
String. How Sanity treats each gene's variance in log
fold change. |
fixed_variance |
Numeric or |
variance_bins |
Integer. Bins in the log-spaced variance grid. Defaults
to |
variance_min |
Numeric. Smallest variance on the grid. Defaults to
|
variance_max |
Numeric. Largest variance on the grid. Defaults to
|
min_signal_to_noise |
Numeric. Genes whose signal-to-noise ratio falls
below this are left out of the tree. Defaults to |
start |
String. Initial topology. |
spr_search |
String. Subtree pruning and regrafting. |
nni_search |
String. Nearest-neighbour interchange. |
seed |
Integer. Seed for the linkage, SPR and NNI searches. Defaults to
|
reroot |
Boolean. Reroot the finished tree for display. Changes the
drawing, not the likelihood. Defaults to |
layout |
String. 2D layout. |
hyperbolic |
Boolean. Project the layout onto the hyperbolic disk.
Defaults to |
A named list with the following elements:
variance_rule - String. How Sanity treats each gene's variance in log
fold change. "marginalise" integrates over the variance grid,
"posterior_mean" and "max_posterior" collapse it to one value first
(cheaper, less accurate error bars), and "fixed" uses fixed_variance for
every gene. One of c("marginalise", "posterior_mean", "max_posterior", "fixed"). Defaults to "marginalise".
fixed_variance - Numeric or NULL. The variance for variance_rule = "fixed". Ignored otherwise. Defaults to NULL.
variance_bins - Integer. Bins in the log-spaced variance grid.
Defaults to 161L.
variance_min - Numeric. Smallest variance on the grid. Defaults to
0.001.
variance_max - Numeric. Largest variance on the grid. Defaults to
50.0.
min_signal_to_noise - Numeric. Genes whose signal-to-noise ratio falls
below this are left out of the tree. Defaults to 1.0.
start - String. Initial topology. "linkage" is a Ward linkage over a
neighbour graph: faster and a better tree on real data. "greedy_merge" is
the paper's star merge, kept for comparisons with the published method. One
of c("linkage", "greedy_merge"). Defaults to "linkage".
spr_search - String. Subtree pruning and regrafting. "approximate"
stays within a few nats of "exact" on bonsai-rs's benchmarks at up to
twice the speed. One of c("approximate", "exact"). Defaults to
"approximate".
nni_search - String. Nearest-neighbour interchange. "approximate"
found the same tree as "exact" on every bonsai-rs benchmark, and faster.
One of c("approximate", "exact"). Defaults to "approximate".
seed - Integer. Seed for the linkage, SPR and NNI searches. Defaults
to 42L.
reroot - Boolean. Reroot the finished tree for display. Changes the
drawing, not the likelihood. Defaults to TRUE.
layout - String. 2D layout. "equal_daylight" refines equal angle but
falls back to it above 2,048 nodes, so it only matters for small trees. Can
be changed later with relayout_bonsai(). One of
c("equal_angle", "equal_daylight", "dendrogram"). Defaults to
"equal_angle".
hyperbolic - Boolean. Project the layout onto the hyperbolic disk.
Defaults to FALSE.
de Groot, et al., Nat. Biotechnol., 2026; Breda, et al., Nat. Biotechnol., 2021.
Parameters controlling which cells
run_gene_trends_sc() assigns to each branch. The threshold on a
fate's probability is an expanding quantile over the pseudotime-sorted cells,
made monotone with a cumulative maximum, so a fate's bar can only rise as
differentiation proceeds. The defaults are the reference ones.
params_sc_branch_selection(q = 0.01, eps = 0.01, resolution = 500L)params_sc_branch_selection(q = 0.01, eps = 0.01, resolution = 500L)
q |
Numeric. Upper-tail quantile of the fate probability used as the
threshold. Defaults to |
eps |
Numeric. Slack subtracted from the threshold before the
comparison. Defaults to |
resolution |
Integer. Number of pseudotime buckets, capped at the cell
count. Defaults to |
A named list with the following elements:
q - Numeric. Upper-tail quantile of the fate probability used as the
threshold. Defaults to 0.01.
eps - Numeric. Slack subtracted from the threshold before the
comparison. Defaults to 0.01.
resolution - Integer. Number of pseudotime buckets, capped at the cell
count. Defaults to 500L.
Setty, et al., Nat. Biotechnol., 2019.
This function generates parameters for the bootstrapped meta cell generation based on hdWGCNA, see Morabito, et al., Cell Rep. Methods, 2023.
params_sc_bt_metacells( max_shared = 15L, target_no_metacells = 1000L, max_iter = 5000L, knn = list() )params_sc_bt_metacells( max_shared = 15L, target_no_metacells = 1000L, max_iter = 5000L, knn = list() )
max_shared |
Integer. Maximum number of allowed shared neighbours for
the meta cell to be considered. Defaults to |
target_no_metacells |
Integer. Target number of meta-cells to generate.
Defaults to |
max_iter |
Integer. Maximum number of iterations for the algorithm.
Defaults to |
knn |
List. Optional overrides for kNN parameters. See
|
A named list with the following elements:
max_shared - Integer. Maximum number of allowed shared neighbours for
the meta cell to be considered. Defaults to 15L.
target_no_metacells - Integer. Target number of meta-cells to
generate. Defaults to 1000L.
max_iter - Integer. Maximum number of iterations for the algorithm.
Defaults to 5000L.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
Defaults match CellPhoneDB v5: 1000 permutations and a 10% expression threshold.
params_sc_cellphonedb( n_perm = 1000L, threshold = 0.1, seed = 42L, perm_batch = NULL )params_sc_cellphonedb( n_perm = 1000L, threshold = 0.1, seed = 42L, perm_batch = NULL )
n_perm |
Integer. Number of cluster label permutations for the p-values.
Defaults to |
threshold |
Numeric. Both partners need a fraction of expressing cells
strictly above this value in their cluster. Defaults to |
seed |
Integer. Seed for the permutations. Defaults to |
perm_batch |
Integer or |
A named list with the following elements:
n_perm - Integer. Number of cluster label permutations for the
p-values. Defaults to 1000L.
threshold - Numeric. Both partners need a fraction of expressing cells
strictly above this value in their cluster. Defaults to 0.1.
seed - Integer. Seed for the permutations. Defaults to 42L.
perm_batch - Integer or NULL. Permutations that share one pass over
the gene data. NULL uses the Rust default (16). Defaults to NULL.
Mirrors the CellSweep reference implementation's defaults. The
pseudocounts (celltype_lambda, ambient_lambda, bulk_lambda) are given
on the scale you see here and divided by the gene count internally. Two of
these are worth knowing about before you touch anything else.
freeze_ambient_profile = TRUE keeps the ambient profile at its
empty-droplet estimate, which is the recommended path and the only one where
alpha_cap, the repulsion terms and cell-type reassignment are live. And
freeze_empties only accepts TRUE: the reference gives empty droplets a
cell-type component they have no label for, which indexes past the end of the
profile matrix and wraps onto the last cell type.
params_sc_cellsweep( freeze_empties = TRUE, freeze_ambient_profile = TRUE, init_alpha = 0.9, init_beta = 0.1, alpha_cap = 0.9, repulsion_strength = 1e-04, max_frac_gene_repulsion = 0.2, celltype_lambda = 50, ambient_lambda = 50, bulk_lambda = 10, eps = 1e-12, log_eps = as.numeric("1e-300"), max_iter = 2000L, del0_ll_tol = 0.001, min_ll_tol = 1e-06, tol_p = 1e-04, tol_f = 1e-04, norm_from_rounded = FALSE, seed = 42L )params_sc_cellsweep( freeze_empties = TRUE, freeze_ambient_profile = TRUE, init_alpha = 0.9, init_beta = 0.1, alpha_cap = 0.9, repulsion_strength = 1e-04, max_frac_gene_repulsion = 0.2, celltype_lambda = 50, ambient_lambda = 50, bulk_lambda = 10, eps = 1e-12, log_eps = as.numeric("1e-300"), max_iter = 2000L, del0_ll_tol = 0.001, min_ll_tol = 1e-06, tol_p = 1e-04, tol_f = 1e-04, norm_from_rounded = FALSE, seed = 42L )
freeze_empties |
Boolean. Keep the contamination fraction of empty
droplets pinned at 1. Only |
freeze_ambient_profile |
Boolean. Keep the ambient profile at its
empty-droplet estimate rather than re-estimating it as a mixture over the
cell-type profiles. Defaults to |
init_alpha |
Numeric. Starting ambient fraction for every real barcode.
With |
init_beta |
Numeric. Starting bulk contamination fraction. Set below
|
alpha_cap |
Numeric. Ceiling on the per-cell ambient fraction before the
log-likelihood converges. Barcodes wanting to exceed it are excluded from the
cell-type profile update and allowed to switch cell type. Defaults to |
repulsion_strength |
Numeric. Strength of the repulsion pushing
cell-type profiles away from the ambient profile. Scales with cluster mass,
so it is inert on small data and only bites at realistic cell counts.
Defaults to |
max_frac_gene_repulsion |
Numeric. Ceiling on the fraction of any single
profile entry that repulsion may remove. Defaults to |
celltype_lambda |
Numeric. Pseudocount smoothing the cell-type profile
update. Higher values give smoother profiles. Defaults to |
ambient_lambda |
Numeric. Pseudocount smoothing the ambient profile
estimate. Defaults to |
bulk_lambda |
Numeric. Pseudocount smoothing the bulk profile estimate.
Defaults to |
eps |
Numeric. Floor on denominators. Defaults to |
log_eps |
Numeric. Floor on the argument of |
max_iter |
Integer. Hard cap on EM iterations. Defaults to |
del0_ll_tol |
Numeric. Log-likelihood change, as a fraction of the first
EM step's change, below which stage one ends and parameter convergence starts
being checked. Defaults to |
min_ll_tol |
Numeric. Floor on the adaptive tolerance, relative to the
current log-likelihood. Stops |
tol_p |
Numeric. Convergence threshold on the maximum row-wise L1 change
in the cell-type profiles. Defaults to |
tol_f |
Numeric. Convergence threshold on the change in the total
contamination fraction. Defaults to |
norm_from_rounded |
Boolean. Derive the normalised layer from the
integerised counts rather than the denoised floats. Consistent across the two
layers at the cost of the sub-integer signal, which is where CellSweep is
most informative. Defaults to |
seed |
Integer. Seed for the stochastic rounding of the denoised counts.
Defaults to |
A named list with the following elements:
freeze_empties - Boolean. Keep the contamination fraction of empty
droplets pinned at 1. Only TRUE is supported, see the description.
Defaults to TRUE.
freeze_ambient_profile - Boolean. Keep the ambient profile at its
empty-droplet estimate rather than re-estimating it as a mixture over the
cell-type profiles. Defaults to TRUE.
init_alpha - Numeric. Starting ambient fraction for every real
barcode. With freeze_ambient_profile = TRUE the final result barely
depends on it, so it sits at alpha_cap. Defaults to 0.9.
init_beta - Numeric. Starting bulk contamination fraction. Set below
init_alpha on purpose: bulk and ambient are not fully separable, so this
biases unassignable contamination towards ambient. Defaults to 0.1.
alpha_cap - Numeric. Ceiling on the per-cell ambient fraction before
the log-likelihood converges. Barcodes wanting to exceed it are excluded
from the cell-type profile update and allowed to switch cell type. Defaults
to 0.9.
repulsion_strength - Numeric. Strength of the repulsion pushing
cell-type profiles away from the ambient profile. Scales with cluster mass,
so it is inert on small data and only bites at realistic cell counts.
Defaults to 1e-04.
max_frac_gene_repulsion - Numeric. Ceiling on the fraction of any
single profile entry that repulsion may remove. Defaults to 0.2.
celltype_lambda - Numeric. Pseudocount smoothing the cell-type profile
update. Higher values give smoother profiles. Defaults to 50.0.
ambient_lambda - Numeric. Pseudocount smoothing the ambient profile
estimate. Defaults to 50.0.
bulk_lambda - Numeric. Pseudocount smoothing the bulk profile
estimate. Defaults to 10.0.
eps - Numeric. Floor on denominators. Defaults to 1e-12.
log_eps - Numeric. Floor on the argument of log. Defaults to
1e-300.
max_iter - Integer. Hard cap on EM iterations. Defaults to 2000L.
del0_ll_tol - Numeric. Log-likelihood change, as a fraction of the
first EM step's change, below which stage one ends and parameter convergence
starts being checked. Defaults to 0.001.
min_ll_tol - Numeric. Floor on the adaptive tolerance, relative to the
current log-likelihood. Stops del0_ll_tol chasing floating point noise.
Defaults to 1e-06.
tol_p - Numeric. Convergence threshold on the maximum row-wise L1
change in the cell-type profiles. Defaults to 1e-04.
tol_f - Numeric. Convergence threshold on the change in the total
contamination fraction. Defaults to 1e-04.
norm_from_rounded - Boolean. Derive the normalised layer from the
integerised counts rather than the denoised floats. Consistent across the
two layers at the cost of the sub-integer signal, which is where CellSweep
is most informative. Defaults to FALSE.
seed - Integer. Seed for the stochastic rounding of the denoised
counts. Defaults to 42L.
Default parameters for DSB ADT normalisation
params_sc_dsb( denoise_counts = TRUE, use_isotype_controls = TRUE, pseudocount = 10, quantile_low = NULL, quantile_high = NULL )params_sc_dsb( denoise_counts = TRUE, use_isotype_controls = TRUE, pseudocount = 10, quantile_low = NULL, quantile_high = NULL )
denoise_counts |
Boolean. Run Step II (cell-to-cell technical noise
removal). Defaults to |
use_isotype_controls |
Boolean. Include isotype controls in the noise
matrix in Step II. Requires |
pseudocount |
Numeric. Pseudocount added before the log transform. The
DSB paper recommends |
quantile_low |
Numeric or |
quantile_high |
Numeric or |
A named list with the following elements:
denoise_counts - Boolean. Run Step II (cell-to-cell technical noise
removal). Defaults to TRUE.
use_isotype_controls - Boolean. Include isotype controls in the noise
matrix in Step II. Requires isotype_indices to be passed at call time.
Defaults to TRUE.
pseudocount - Numeric. Pseudocount added before the log transform. The
DSB paper recommends 10 with empty droplets and 1 without. Defaults to
10.0.
quantile_low - Numeric or NULL. Optional numeric in [0, 1). Lower
quantile for per-protein output clipping. If NULL (and quantile_high is
also NULL), no clipping is applied. Defaults to NULL.
quantile_high - Numeric or NULL. Optional numeric in (0, 1]. Upper
quantile for per-protein output clipping. If NULL (and quantile_low is
also NULL), no clipping is applied. Defaults to NULL.
[0, 1). Lower quantile for per-protein output clipping. If NULL(andquantile_highis alsoNULL), no clipping is applied. Defaults to NULL. \item quantile_high - Numeric or NULL. Optional numeric in (0, 1]: R:0,%201)%60.%20Lower%0A%20quantile%20for%20per-protein%20output%20clipping.%20If%20%60NULL%60%20(and%20%60quantile_high%60%20is%0A%20also%20%60NULL%60),%20no%20clipping%20is%20applied.%20Defaults%20to%20%60NULL%60.%0A%20%5C%5Citem%20quantile_high%20-%20Numeric%20or%20%60NULL%60.%20Optional%20numeric%20in%20%60(0,%201
CellSweep trains its ambient profile on the empty droplets, so
it needs them called before it runs. Four ways to do that, in descending
order of how much you should trust them. "supplied" takes an existing
logical column from the obs table. This is the common case: if you have
CellRanger's filtered barcode list you already know which barcodes are empty,
and nothing here will beat that. "umi_cutoff" calls everything below an
absolute library size empty. "expected_cells" turns a cell count into that
cutoff via the sorted library sizes. "knee" finds the cutoff from the
curvature of the rank / log-count curve; it is experimental in the reference
too, and smoothing puts the curvature minimum a few ranks ahead of the actual
cliff, so it sweeps up the real barcodes nearest the transition.
params_sc_empty_droplets( method = c("supplied", "umi_cutoff", "expected_cells", "knee"), is_empty_column = NULL, umi_cutoff = NULL, expected_cells = NULL )params_sc_empty_droplets( method = c("supplied", "umi_cutoff", "expected_cells", "knee"), is_empty_column = NULL, umi_cutoff = NULL, expected_cells = NULL )
method |
String. How the empty droplets are called, see the description.
One of |
is_empty_column |
String or |
umi_cutoff |
Integer or |
expected_cells |
Integer or |
A named list with the following elements:
method - String. How the empty droplets are called, see the
description. One of c("supplied", "umi_cutoff", "expected_cells", "knee").
Defaults to "supplied".
is_empty_column - String or NULL. Name of the logical obs column
holding the mask. Required for method = "supplied", ignored otherwise.
Defaults to NULL.
umi_cutoff - Integer or NULL. Barcodes with a library size strictly
below this are empty. Required for method = "umi_cutoff", ignored
otherwise. Defaults to NULL.
expected_cells - Integer or NULL. Number of real cells expected in
the run. Required for method = "expected_cells", ignored otherwise.
Defaults to NULL.
Fast single cell clustering parameters
params_sc_fast_cluster( kmeans_iters = 100L, batch_size = 4096L, drift_threshold = 1e-04, lr_alpha = 1, full_snn = FALSE, pruning = NULL, snn_similarity = c("jaccard", "rank"), louvain_iters = 10L, cell_mediated = FALSE, n_anchors = 5L, knn = list(k = 5L) )params_sc_fast_cluster( kmeans_iters = 100L, batch_size = 4096L, drift_threshold = 1e-04, lr_alpha = 1, full_snn = FALSE, pruning = NULL, snn_similarity = c("jaccard", "rank"), louvain_iters = 10L, cell_mediated = FALSE, n_anchors = 5L, knn = list(k = 5L) )
kmeans_iters |
Integer. Number of iterations for k-means clustering.
Defaults to |
batch_size |
Integer. Batch size for mini batch k-means clustering.
Defaults to |
drift_threshold |
Numeric. The drift for the mini batch k-means
clustering. If the centroid drift is below this, the mini batch k-means
terminates. Defaults to |
lr_alpha |
Numeric. Learning rate alpha parameter for mini batch
k-means. Defaults to |
full_snn |
Boolean. Shall the full shared nearest neighbour graph be
generated that generates edges between all cells instead of between only
neighbours. Defaults to |
pruning |
Numeric or |
snn_similarity |
String. The Jaccard similarity calculates the Jaccard
between the neighbours, whereas the rank method calculates edge weights based
on the ranking of shared neighbours. For the rank method, the weight is
determined by finding the shared neighbour with the lowest combined rank
across both cells, where lower-ranked (closer) shared neighbours result in
higher edge weights Both methods produce weights normalised to the range |
louvain_iters |
Integer. Number of iterations for Louvain clustering.
Defaults to |
cell_mediated |
Boolean. Shall the cell-mediated centroid graph from
Secuer be built instead of the kNN/sNN graph on the centroids. Centroids are
then linked by the cells that sit near both, so two centroids that are close
in space but have no cells between them stay apart. If |
n_anchors |
Integer. Number of nearest centroids each cell links to in
the cell-mediated graph. Only used if |
knn |
List. Optional overrides for kNN parameters. See
|
A named list with the following elements:
kmeans_iters - Integer. Number of iterations for k-means clustering.
Defaults to 100L.
batch_size - Integer. Batch size for mini batch k-means clustering.
Defaults to 4096L.
drift_threshold - Numeric. The drift for the mini batch k-means
clustering. If the centroid drift is below this, the mini batch k-means
terminates. Defaults to 1e-04.
lr_alpha - Numeric. Learning rate alpha parameter for mini batch
k-means. Defaults to 1.0.
louvain_iters - Integer. Number of iterations for Louvain clustering.
Defaults to 10L.
full_snn - Boolean. Shall the full shared nearest neighbour graph be
generated that generates edges between all cells instead of between only
neighbours. Defaults to FALSE.
pruning - Numeric or NULL. Weights below this threshold will be set
to 0 in the generation of the sNN graph. A node that loses every edge keeps
its strongest kNN edge. If not provided, defaults to 1 / ceil(k * 0.8).
Defaults to NULL.
snn_similarity - String. The Jaccard similarity calculates the Jaccard
between the neighbours, whereas the rank method calculates edge weights
based on the ranking of shared neighbours. For the rank method, the weight
is determined by finding the shared neighbour with the lowest combined rank
across both cells, where lower-ranked (closer) shared neighbours result in
higher edge weights Both methods produce weights normalised to the range
[0, 1]. One of c("jaccard", "rank"). Defaults to "jaccard".
cell_mediated - Boolean. Shall the cell-mediated centroid graph from
Secuer be built instead of the kNN/sNN graph on the centroids. Centroids are
then linked by the cells that sit near both, so two centroids that are close
in space but have no cells between them stay apart. If TRUE, snn,
full_snn, pruning and snn_similarity are ignored. See Wei, et al.,
PLoS Comput Biol, 2022. Defaults to FALSE.
n_anchors - Integer. Number of nearest centroids each cell links to in
the cell-mediated graph. Only used if cell_mediated = TRUE. Defaults to
5L.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
Wrapper function for the fastMNN parameters
params_sc_fastmnn( ndist = 3, cos_norm = TRUE, no_pcs = 30L, sparse_svd = TRUE, knn = list(k = 20L), pca = params_sc_pca() )params_sc_fastmnn( ndist = 3, cos_norm = TRUE, no_pcs = 30L, sparse_svd = TRUE, knn = list(k = 20L), pca = params_sc_pca() )
ndist |
Numeric. Number of median distances for the tricube kernel
bandwidth. Defaults to |
cos_norm |
Boolean. Apply cosine normalisation before computing
distances. Defaults to |
no_pcs |
Integer. Number of PCs to use for MNN calculations. Defaults to
|
sparse_svd |
Boolean. Shall the sparse SVD be used. Defaults to |
knn |
List. Optional overrides for kNN parameters. See
|
pca |
List. Parameters to feed through to the optional recalculation of
the PCA, see |
A named list with the following elements:
ndist - Numeric. Number of median distances for the tricube kernel
bandwidth. Defaults to 3.0.
cos_norm - Boolean. Apply cosine normalisation before computing
distances. Defaults to TRUE.
no_pcs - Integer. Number of PCs to use for MNN calculations. Defaults
to 30L.
sparse_svd - Boolean. Shall the sparse SVD be used. Defaults to
TRUE.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
The elements of params_sc_pca(), overridden by pca, spliced in at
this position.
Parameters controlling the landmark Gaussian process that
run_gene_trends_sc() fits per branch. The kernel is a Matern-5/2
one and the prediction grid doubles as the landmark set. The defaults come
from the reference and are prior-dominated. Palantir's pseudotime is min-max
scaled to [0, 1], so a length_scale of 1.0 spans the entire domain and
a sigma of 1.0 sits at roughly the signal scale of log-normalised
expression. The posterior will flatten genuine transient structure and
resolve almost any gene into a smooth monotone or single-peaked curve. That
is a presentation choice, not inference. Shorten length_scale before
believing a bump.
params_sc_gene_trends( resolution = 500L, weighting = c("hard_mask", "fate_probability"), length_scale = 1, sigma = 1, jitter = 1e-06, max_jitter_retries = 3L, chunk_size = 2048L )params_sc_gene_trends( resolution = 500L, weighting = c("hard_mask", "fate_probability"), length_scale = 1, sigma = 1, jitter = 1e-06, max_jitter_retries = 3L, chunk_size = 2048L )
resolution |
Integer. Grid points per branch. Kept at the default even
when a branch holds fewer cells, as the reference does. Defaults to |
weighting |
String. With |
length_scale |
Numeric. Matern-5/2 length scale. Defaults to |
sigma |
Numeric. Noise standard deviation. Defaults to |
jitter |
Numeric. Added to the landmark covariance diagonal before the
Cholesky. Defaults to |
max_jitter_retries |
Integer. Times the jitter is raised and the
Cholesky retried before giving up. Defaults to |
chunk_size |
Integer. Training points held at once when accumulating the
cross-covariance. Defaults to |
A named list with the following elements:
resolution - Integer. Grid points per branch. Kept at the default even
when a branch holds fewer cells, as the reference does. Defaults to 500L.
weighting - String. With "hard_mask" every selected cell enters its
branch's fit with equal weight, which is what the reference does. With
"fate_probability" every cell enters every fit weighted by its fate
probability, which is more defensible: a cell at 0.6 is not a member. One of
c("hard_mask", "fate_probability"). Defaults to "hard_mask".
length_scale - Numeric. Matern-5/2 length scale. Defaults to 1.0.
sigma - Numeric. Noise standard deviation. Defaults to 1.0.
jitter - Numeric. Added to the landmark covariance diagonal before the
Cholesky. Defaults to 1e-06.
max_jitter_retries - Integer. Times the jitter is raised and the
Cholesky retried before giving up. Defaults to 3L.
chunk_size - Integer. Training points held at once when accumulating
the cross-covariance. Defaults to 2048L.
Setty, et al., Nat. Biotechnol., 2019.
Default parameters for Harmony batch correction
params_sc_harmony( k = NULL, sigma = 0.1, theta = 2, lambda = 1, block_size = 0.2, max_iter_kmeans = 20L, max_iter_harmony = 10L, epsilon_kmeans = 0.001, epsilon_harmony = 0.01, window_size = 3L, kmeans = list() )params_sc_harmony( k = NULL, sigma = 0.1, theta = 2, lambda = 1, block_size = 0.2, max_iter_kmeans = 20L, max_iter_harmony = 10L, epsilon_kmeans = 0.001, epsilon_harmony = 0.01, window_size = 3L, kmeans = list() )
k |
Integer or |
sigma |
Numeric vector. Per-cluster diversity weights. Either a single
value (broadcast to all clusters) or a vector of length k. Defaults to |
theta |
Numeric vector. Per-variable diversity penalties. Either a
single value (broadcast to all variables) or a vector of length equal to the
number of batch variables. Defaults to |
lambda |
Numeric vector. Ridge regression penalty for the linear model.
Typically a single value that is broadcast to every batch column of the
design matrix; the intercept is not penalised. Defaults to |
block_size |
Numeric. Fraction of cells to update per block during
optimisation (0.0-1.0). Lower values reduce memory usage but increase
computation time. Defaults to |
max_iter_kmeans |
Integer. Maximum number of k-means iterations per
Harmony round. Defaults to |
max_iter_harmony |
Integer. Maximum number of Harmony outer iterations.
Defaults to |
epsilon_kmeans |
Numeric. Convergence threshold for k-means clustering.
Stops when the relative change in cluster assignments falls below this value.
Defaults to |
epsilon_harmony |
Numeric. Convergence threshold for Harmony. Stops when
the relative change in the objective function falls below this value.
Defaults to |
window_size |
Integer. Number of previous iterations to consider when
checking convergence. Defaults to |
kmeans |
List. Optional overrides for the k-means clustering algorithm
Possible parameters are |
A named list with the following elements:
k - Integer or NULL. Number of clusters for k-means clustering. If
not provided, it will be automatically determined as min(round(N / 30), 100). Defaults to NULL.
sigma - Numeric vector. Per-cluster diversity weights. Either a single
value (broadcast to all clusters) or a vector of length k. Defaults to
0.1.
theta - Numeric vector. Per-variable diversity penalties. Either a
single value (broadcast to all variables) or a vector of length equal to the
number of batch variables. Defaults to 2.0.
lambda - Numeric vector. Ridge regression penalty for the linear
model. Typically a single value that is broadcast to every batch column of
the design matrix; the intercept is not penalised. Defaults to 1.0.
block_size - Numeric. Fraction of cells to update per block during
optimisation (0.0-1.0). Lower values reduce memory usage but increase
computation time. Defaults to 0.2.
max_iter_kmeans - Integer. Maximum number of k-means iterations per
Harmony round. Defaults to 20L.
max_iter_harmony - Integer. Maximum number of Harmony outer
iterations. Defaults to 10L.
epsilon_kmeans - Numeric. Convergence threshold for k-means
clustering. Stops when the relative change in cluster assignments falls
below this value. Defaults to 0.001.
epsilon_harmony - Numeric. Convergence threshold for Harmony. Stops
when the relative change in the objective function falls below this value.
Defaults to 0.01.
window_size - Integer. Number of previous iterations to consider when
checking convergence. Defaults to 3L.
The elements of params_kmeans_defaults(), overridden by kmeans,
spliced in at this position.
Default parameters for Harmony v2 batch correction
params_sc_harmony_v2( k = NULL, sigma = 0.1, theta = 2, lambda = 1, block_size = 0.2, max_iter_kmeans = 4L, max_iter_harmony = 10L, epsilon_kmeans = 0.001, epsilon_harmony = 0.01, window_size = 3L, alpha = 0.2, tau = 0, batch_proportion_cutoff = 1e-05, use_dynamic_lambda = TRUE, kmeans = list() )params_sc_harmony_v2( k = NULL, sigma = 0.1, theta = 2, lambda = 1, block_size = 0.2, max_iter_kmeans = 4L, max_iter_harmony = 10L, epsilon_kmeans = 0.001, epsilon_harmony = 0.01, window_size = 3L, alpha = 0.2, tau = 0, batch_proportion_cutoff = 1e-05, use_dynamic_lambda = TRUE, kmeans = list() )
k |
Integer or |
sigma |
Numeric vector. Per-cluster diversity weights. Either a single
value (broadcast to all clusters) or a vector of length k. Defaults to |
theta |
Numeric vector. Per-variable diversity penalties. Either a
single value (broadcast to all variables) or a vector of length equal to the
number of batch variables. Defaults to |
lambda |
Numeric vector. Ridge regression penalty for the linear model.
Typically a single value that is broadcast to every batch column of the
design matrix; the intercept is not penalised. Ignored when
|
block_size |
Numeric. Fraction of cells to update per block during
optimisation (0.0-1.0). Lower values reduce memory usage but increase
computation time. Defaults to |
max_iter_kmeans |
Integer. Maximum number of k-means iterations per
Harmony round. Defaults to |
max_iter_harmony |
Integer. Maximum number of Harmony outer iterations.
Defaults to |
epsilon_kmeans |
Numeric. Convergence threshold for k-means clustering.
Stops when the relative change in cluster assignments falls below this value.
Defaults to |
epsilon_harmony |
Numeric. Convergence threshold for Harmony. Stops when
the relative change in the objective function falls below this value.
Defaults to |
window_size |
Integer. Number of previous iterations to consider when
checking convergence. Defaults to |
alpha |
Numeric. Scaling factor for dynamic lambda estimation. Must be
in (0, 1). Only relevant when |
tau |
Numeric. Scaling factor for theta based on batch size. A value of
0 disables batch-size scaling of theta. Defaults to |
batch_proportion_cutoff |
Numeric. Cutoff for pruning batches with small
proportions during ridge regression. Defaults to |
use_dynamic_lambda |
Boolean. If |
kmeans |
List. Optional overrides for the k-means clustering algorithm
Possible parameters are |
A named list with the following elements:
k - Integer or NULL. Number of clusters for k-means clustering. If
not provided, it will be automatically determined as min(round(N / 30), 100). Defaults to NULL.
sigma - Numeric vector. Per-cluster diversity weights. Either a single
value (broadcast to all clusters) or a vector of length k. Defaults to
0.1.
theta - Numeric vector. Per-variable diversity penalties. Either a
single value (broadcast to all variables) or a vector of length equal to the
number of batch variables. Defaults to 2.0.
lambda - Numeric vector. Ridge regression penalty for the linear
model. Typically a single value that is broadcast to every batch column of
the design matrix; the intercept is not penalised. Ignored when
use_dynamic_lambda = TRUE. Defaults to 1.0.
block_size - Numeric. Fraction of cells to update per block during
optimisation (0.0-1.0). Lower values reduce memory usage but increase
computation time. Defaults to 0.2.
max_iter_kmeans - Integer. Maximum number of k-means iterations per
Harmony round. Defaults to 4L.
max_iter_harmony - Integer. Maximum number of Harmony outer
iterations. Defaults to 10L.
epsilon_kmeans - Numeric. Convergence threshold for k-means
clustering. Stops when the relative change in cluster assignments falls
below this value. Defaults to 0.001.
epsilon_harmony - Numeric. Convergence threshold for Harmony. Stops
when the relative change in the objective function falls below this value.
Defaults to 0.01.
window_size - Integer. Number of previous iterations to consider when
checking convergence. Defaults to 3L.
alpha - Numeric. Scaling factor for dynamic lambda estimation. Must be
in (0, 1). Only relevant when use_dynamic_lambda = TRUE. Defaults to
0.2.
tau - Numeric. Scaling factor for theta based on batch size. A value
of 0 disables batch-size scaling of theta. Defaults to 0.0.
batch_proportion_cutoff - Numeric. Cutoff for pruning batches with
small proportions during ridge regression. Defaults to 1e-05.
use_dynamic_lambda - Boolean. If TRUE, lambda is estimated per
cluster as alpha times the expected counts, as in R harmony v2, instead of
using the fixed lambda value. Defaults to TRUE.
The elements of params_kmeans_defaults(), overridden by kmeans,
spliced in at this position.
weighted_graph controls how the kNN distances become edge
weights. The default of FALSE follows the reference implementation: the
distances only decide who is a neighbour and every retained edge weighs one.
Set it to TRUE for the Gaussian kernel, whose width is the ceil(k / neighborhood_factor)-th neighbour distance.
params_sc_hotspot( model = c("danb", "normal", "bernoulli"), normalise = TRUE, weighted_graph = FALSE, neighborhood_factor = 3, knn = list() )params_sc_hotspot( model = c("danb", "normal", "bernoulli"), normalise = TRUE, weighted_graph = FALSE, neighborhood_factor = 3, knn = list() )
model |
String. Model to use for modelling the GEX. One of |
normalise |
Boolean. Shall the data be normalised. Defaults to |
weighted_graph |
Boolean. Shall the Gaussian kernel be applied to the
neighbour distances. Defaults to |
neighborhood_factor |
Numeric. Kernel width is the |
knn |
List. Optional overrides for kNN parameters. See
|
A named list with the following elements:
model - String. Model to use for modelling the GEX. One of c("danb", "normal", "bernoulli"). Defaults to "danb".
normalise - Boolean. Shall the data be normalised. Defaults to TRUE.
weighted_graph - Boolean. Shall the Gaussian kernel be applied to the
neighbour distances. Defaults to FALSE.
neighborhood_factor - Numeric. Kernel width is the ceil(k / neighborhood_factor)-th neighbour distance. Only read when weighted_graph = TRUE. Defaults to 3.0.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
DeTomaso and Yosef, Cell Systems, 2021
Wrapper function for HVG detection parameters.
params_sc_hvg( method = c("vst", "meanvarbin", "dispersion", "scran", "residual"), loess_span = 0.3, num_bin = 20L, bin_method = c("equal_width", "equal_freq"), scran = list() )params_sc_hvg( method = c("vst", "meanvarbin", "dispersion", "scran", "residual"), loess_span = 0.3, num_bin = 20L, bin_method = c("equal_width", "equal_freq"), scran = list() )
method |
String. |
loess_span |
Numeric. The span parameter for the loess function that is
used to standardise the variance for |
num_bin |
Integer. Not yet implemented. Defaults to |
bin_method |
String. The binning method. One of |
scran |
List. Optional overrides for the |
A named list with the following elements:
method - String. "scran" fits a weighted lowess trend to the
variance of the log-expression against its mean and ranks genes by the
residual, as in scran's modelGeneVar(). "residual" ranks genes by the
residual variance of a model fitted with fit_residuals_sc(), and
needs that fit on the object first. It also treats hvg_no as a per-group
count and returns the union across groups, so a grouped fit can select more
than hvg_no genes. One of c("vst", "meanvarbin", "dispersion", "scran", "residual"). Defaults to "vst".
loess_span - Numeric. The span parameter for the loess function that
is used to standardise the variance for method = "vst", and the lowess
span of the trend for method = "scran". Defaults to 0.3.
num_bin - Integer. Not yet implemented. Defaults to 20L.
bin_method - String. The binning method. One of c("equal_width", "equal_freq"). Defaults to "equal_width".
The elements of params_hvg_scran_defaults(), overridden by scran,
spliced in at this position.
Parameters for single cell kNN searches
params_sc_knn( k = 15L, knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "ivf", "exhaustive"), ann_dist = c("euclidean", "cosine"), n_trees = 50L, search_budget = NULL, delta = 0.001, diversify_prob = 0, ef_budget = NULL, extract_knn = FALSE, m = 16L, ef_construction = 200L, ef_search = 100L, n_list = NULL, n_probe = NULL )params_sc_knn( k = 15L, knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "ivf", "exhaustive"), ann_dist = c("euclidean", "cosine"), n_trees = 50L, search_budget = NULL, delta = 0.001, diversify_prob = 0, ef_budget = NULL, extract_knn = FALSE, m = 16L, ef_construction = 200L, ef_search = 100L, n_list = NULL, n_probe = NULL )
k |
Integer. Number of neighbours. Defaults to |
knn_method |
String. Which method to use for the approximate nearest
neighbour search. One of |
ann_dist |
String. Distance metric to use. One of |
n_trees |
Integer. Annoy param: number of trees. Defaults to |
search_budget |
Integer or |
delta |
Numeric. NNDescent param: early termination criterion. Defaults
to |
diversify_prob |
Numeric. NNDescent param: diversification probability
applied at the end of index construction. Defaults to |
ef_budget |
Integer or |
extract_knn |
Boolean. NNDescent param: hand back the graph the descent
already built instead of beam searching it. Skips the query pass entirely, so
it is much faster, at the cost of some recall. Rows the descent never filled
come back padded with duplicate edges. Ignored by every other method.
Defaults to |
m |
Integer. HNSW param: number of connections between layers. Defaults
to |
ef_construction |
Integer. HNSW param: size of the dynamic candidate
list during construction. Defaults to |
ef_search |
Integer. HNSW param: size of the candidate list at query
time. Higher values improve recall at the cost of speed. Defaults to |
n_list |
Integer or |
n_probe |
Integer or |
A named list with the following elements:
k - Integer. Number of neighbours. Defaults to 15L.
knn_method - String. Which method to use for the approximate nearest
neighbour search. One of c("kmknn", "hnsw", "annoy", "nndescent", "ivf", "exhaustive"). Defaults to "kmknn".
ann_dist - String. Distance metric to use. One of c("euclidean", "cosine"). Defaults to "euclidean".
n_trees - Integer. Annoy param: number of trees. Defaults to 50L.
search_budget - Integer or NULL. Annoy param: optional search budget
per tree. If NULL, defaults to n_trees * k * 20L internally. Defaults to
NULL.
delta - Numeric. NNDescent param: early termination criterion.
Defaults to 0.001.
diversify_prob - Numeric. NNDescent param: diversification probability
applied at the end of index construction. Defaults to 0.0.
ef_budget - Integer or NULL. NNDescent param: optional query budget.
Higher values improve recall at the cost of speed. Defaults to NULL.
extract_knn - Boolean. NNDescent param: hand back the graph the
descent already built instead of beam searching it. Skips the query pass
entirely, so it is much faster, at the cost of some recall. Rows the descent
never filled come back padded with duplicate edges. Ignored by every other
method. Defaults to FALSE.
m - Integer. HNSW param: number of connections between layers.
Defaults to 16L.
ef_construction - Integer. HNSW param: size of the dynamic candidate
list during construction. Defaults to 200L.
ef_search - Integer. HNSW param: size of the candidate list at query
time. Higher values improve recall at the cost of speed. Defaults to 100L.
n_list - Integer or NULL. IVF param: number of clusters to generate.
If NULL, defaults to sqrt(n) internally. Defaults to NULL.
n_probe - Integer or NULL. IVF param: number of clusters to query.
If NULL, defaults to sqrt(n_list) internally. Defaults to NULL.
Parameters controlling the MAGIC imputation run by
run_magic_sc(). The defaults mirror the reference implementation.
params_sc_magic( n_steps = 3L, clip_threshold = 0.01, gene_batch_size = 1000L, layer = c("norm", "raw"), allow_large = FALSE )params_sc_magic( n_steps = 3L, clip_threshold = 0.01, gene_batch_size = 1000L, layer = c("norm", "raw"), allow_large = FALSE )
n_steps |
Integer. Diffusion steps applied to the counts. Zero is legal
and hands back the un-imputed values, which is a cheap way to compare the
two. Defaults to |
clip_threshold |
Numeric. Imputed values below this are zeroed after the
last step. Defaults to |
gene_batch_size |
Integer. Genes streamed off the binary store per
block. Bounds the scratch memory and is clamped to the number of requested
genes. Defaults to |
layer |
String. Which stored layer to impute. The operator preserves
per-cell mass, so imputed values sit on the scale of whatever went in:
imputing raw counts and imputing log-normalised counts are different
operations rather than the same one rescaled. One of |
allow_large |
Boolean. Skip the output size guard. The dense output is
capped at 1e9 elements, i.e. 4 GB of |
A named list with the following elements:
n_steps - Integer. Diffusion steps applied to the counts. Zero is
legal and hands back the un-imputed values, which is a cheap way to compare
the two. Defaults to 3L.
clip_threshold - Numeric. Imputed values below this are zeroed after
the last step. Defaults to 0.01.
gene_batch_size - Integer. Genes streamed off the binary store per
block. Bounds the scratch memory and is clamped to the number of requested
genes. Defaults to 1000L.
layer - String. Which stored layer to impute. The operator preserves
per-cell mass, so imputed values sit on the scale of whatever went in:
imputing raw counts and imputing log-normalised counts are different
operations rather than the same one rescaled. One of c("norm", "raw").
Defaults to "norm".
allow_large - Boolean. Skip the output size guard. The dense output is
capped at 1e9 elements, i.e. 4 GB of f32. Defaults to FALSE.
van Dijk, et al., Cell, 2018.
Wrapper function for parameters for MiloR
params_sc_miloR( prop = 0.2, k_refine = 20L, refinement_strategy = c("index", "approximate", "bruteforce"), index_type = c("nndescent", "ivf", "hnsw", "annoy", "exhaustive"), knn = list() )params_sc_miloR( prop = 0.2, k_refine = 20L, refinement_strategy = c("index", "approximate", "bruteforce"), index_type = c("nndescent", "ivf", "hnsw", "annoy", "exhaustive"), knn = list() )
prop |
Numeric. Proportion of cells to sample as neighbourhood indices.
Defaults to |
k_refine |
Integer. Number of neighbours to use for refinement.
Defaults to |
refinement_strategy |
String. Strategy for refining sampled indices.
One of |
index_type |
String. Type of kNN index to use. One of
|
knn |
List. Optional overrides for kNN parameters. See
|
A list with the MiloR parameters.
Wrapper function to generate QC metric params for single cell
params_sc_min_quality( min_unique_genes = 100L, min_lib_size = 250L, min_cells = 10L, target_size = 10000 )params_sc_min_quality( min_unique_genes = 100L, min_lib_size = 250L, min_cells = 10L, target_size = 10000 )
min_unique_genes |
Integer. Minimum number of unique genes per cell/spot
to be included. Defaults to |
min_lib_size |
Integer. Minimum library size per cell/spot to be
included. Defaults to |
min_cells |
Integer. Minimum number of cells a gene has to be expressed
to be included. Defaults to |
target_size |
Numeric. The target size for the normalisation. Defaults
to |
A named list with the following elements:
min_unique_genes - Integer. Minimum number of unique genes per
cell/spot to be included. Defaults to 100L.
min_lib_size - Integer. Minimum library size per cell/spot to be
included. Defaults to 250L.
min_cells - Integer. Minimum number of cells a gene has to be
expressed to be included. Defaults to 10L.
target_size - Numeric. The target size for the normalisation. Defaults
to 10000.0.
Wrapper function to provide data for mtx-based loading
params_sc_mtx_io(path_mtx, path_obs, path_var, cells_as_rows, has_hdr)params_sc_mtx_io(path_mtx, path_obs, path_var, cells_as_rows, has_hdr)
path_mtx |
Any. Path to the .mtx file Required. |
path_obs |
Any. Path to the file containing cell/barcode info. Required. |
path_var |
Any. Path to the file containing gene/variable info. Required. |
cells_as_rows |
Boolean. Do cells represent the rows or columns. Required. |
has_hdr |
Boolean. Do the plain text files have headers. Required. |
A named list with the following elements:
path_mtx - Any. Path to the .mtx file Required.
path_obs - Any. Path to the file containing cell/barcode info. Required.
path_var - Any. Path to the file containing gene/variable info. Required.
cells_as_rows - Boolean. Do cells represent the rows or columns. Required.
has_hdr - Boolean. Do the plain text files have headers. Required.
Wrapper function for parameters for neighbour identification in single cell
params_sc_neighbours( full_snn = TRUE, pruning = 0.0833333333333333, snn_similarity = c("jaccard", "rank"), knn = list() )params_sc_neighbours( full_snn = TRUE, pruning = 0.0833333333333333, snn_similarity = c("jaccard", "rank"), knn = list() )
full_snn |
Boolean. Shall the full shared nearest neighbour graph be
generated that generates edges between all cells instead of between only
neighbours. Defaults to |
pruning |
Numeric. Weights below this threshold will be set to 0 in the
generation of the sNN graph. Seurat uses for example |
snn_similarity |
String. The Jaccard similarity calculates the Jaccard
between the neighbours, whereas the rank method calculates edge weights based
on the ranking of shared neighbours. For the rank method, the weight is
determined by finding the shared neighbour with the lowest combined rank
across both cells, where lower-ranked (closer) shared neighbours result in
higher edge weights Both methods produce weights normalised to the range |
knn |
List. Optional overrides for kNN parameters. See
|
A named list with the following elements:
full_snn - Boolean. Shall the full shared nearest neighbour graph be
generated that generates edges between all cells instead of between only
neighbours. Defaults to TRUE.
pruning - Numeric. Weights below this threshold will be set to 0 in
the generation of the sNN graph. Seurat uses for example 1/15 with k = 20. As the default k is set to 15, we set it to 1/12. Track this against
k rather than leaving it: the threshold is a share of the neighbourhood,
so the same value prunes far harder at a larger k. A cell that loses every
edge to pruning keeps its strongest kNN edge, so it does not drop out as a
one-cell cluster. Over-pruning still fails quietly otherwise: small groups
of cells that only share neighbours with each other split off as tiny
communities. Defaults to 0.08333333333333333.
snn_similarity - String. The Jaccard similarity calculates the Jaccard
between the neighbours, whereas the rank method calculates edge weights
based on the ranking of shared neighbours. For the rank method, the weight
is determined by finding the shared neighbour with the lowest combined rank
across both cells, where lower-ranked (closer) shared neighbours result in
higher edge weights Both methods produce weights normalised to the range
[0, 1]. One of c("jaccard", "rank"). Defaults to "jaccard".
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
Parameters controlling the Palantir trajectory inference. The
kNN graph you hand to run_palantir_sc() feeds the diffusion
kernel. The geodesics are measured over a second kNN graph that Palantir
builds internally on the multiscale space, and knn is what controls that
one. It is a different knob from k in the kNN parameter block, which sizes
the backend index. Palantir overrides k and ann_dist for its internal
search, so only knn_method and the backend tuning parameters have an
effect.
params_sc_palantir( n_dcs = 10L, n_eigs = NULL, knn = 30L, num_waypoints = 1200L, scale_components = TRUE, use_early_cell_as_start = TRUE, max_iterations = 25L, branch_prob_threshold = 0.01, lanczos_basis_size = NULL, lanczos_max_restarts = 16L, lanczos_tol = 1e-08, knn_params = list() )params_sc_palantir( n_dcs = 10L, n_eigs = NULL, knn = 30L, num_waypoints = 1200L, scale_components = TRUE, use_early_cell_as_start = TRUE, max_iterations = 25L, branch_prob_threshold = 0.01, lanczos_basis_size = NULL, lanczos_max_restarts = 16L, lanczos_tol = 1e-08, knn_params = list() )
n_dcs |
Integer. Diffusion components to extract before the multiscale
scaling. Defaults to |
n_eigs |
Integer or |
knn |
Integer. Neighbours for the geodesic graph over the multiscale
space, in the reference's self-inclusive convention. Defaults to |
num_waypoints |
Integer. Target waypoint count for the max-min sampler.
Defaults to |
scale_components |
Boolean. Min-max scale each multiscale component to
|
use_early_cell_as_start |
Boolean. Use the provided early cell directly
rather than snapping it to the nearest diffusion-map boundary cell. Defaults
to |
max_iterations |
Integer. Iteration cap for the pseudotime refinement.
Defaults to |
branch_prob_threshold |
Numeric. Fate probabilities below this are
zeroed. Defaults to |
lanczos_basis_size |
Integer or |
lanczos_max_restarts |
Integer. Maximum restart cycles for the Lanczos
solver. Defaults to |
lanczos_tol |
Numeric. Relative residual tolerance for the Lanczos
solver. Defaults to |
knn_params |
List. Optional overrides for the kNN parameters of the
internal multiscale search. See |
A named list with the following elements:
n_dcs - Integer. Diffusion components to extract before the multiscale
scaling. Defaults to 10L.
n_eigs - Integer or NULL. Eigenvectors to retain, not components:
the trivial leading eigenvector is counted here and then dropped, so 3L
leaves two multiscale components. If NULL, the count is picked from the
largest eigengap, as the reference does. Defaults to NULL.
knn - Integer. Neighbours for the geodesic graph over the multiscale
space, in the reference's self-inclusive convention. Defaults to 30L.
num_waypoints - Integer. Target waypoint count for the max-min
sampler. Defaults to 1200L.
scale_components - Boolean. Min-max scale each multiscale component to
[0, 1] before any distance is taken. Defaults to TRUE.
use_early_cell_as_start - Boolean. Use the provided early cell
directly rather than snapping it to the nearest diffusion-map boundary cell.
Defaults to TRUE.
max_iterations - Integer. Iteration cap for the pseudotime refinement.
Defaults to 25L.
branch_prob_threshold - Numeric. Fate probabilities below this are
zeroed. Defaults to 0.01.
lanczos_basis_size - Integer or NULL. Krylov basis vectors held at
once during the diffusion-map eigendecomposition. If NULL, derived from
the requested component count. Defaults to NULL.
lanczos_max_restarts - Integer. Maximum restart cycles for the Lanczos
solver. Defaults to 16L.
lanczos_tol - Numeric. Relative residual tolerance for the Lanczos
solver. Defaults to 1e-08.
The elements of params_knn_defaults(), overridden by knn_params,
spliced in at this position.
Setty, et al., Nat. Biotechnol., 2019.
Wrapper for PCA specifically designed for single cells
params_sc_pca( mean_center = TRUE, normalise_variance = TRUE, randomised = NULL, clr = FALSE, size_factor = 10000, svd_solver = c("covariance", "randomised", "exact") )params_sc_pca( mean_center = TRUE, normalise_variance = TRUE, randomised = NULL, clr = FALSE, size_factor = 10000, svd_solver = c("covariance", "randomised", "exact") )
mean_center |
Boolean. Shall the data be mean centred Defaults to
|
normalise_variance |
Boolean. Shall the data have normalised variance
Defaults to |
randomised |
Boolean or |
clr |
Boolean. Shall the CLR-type |
size_factor |
Numeric. The used size factor during I/O. It needs to be
the same as during I/O to have correct results when using the |
svd_solver |
String. Which solver to use. |
A named list with the following elements:
mean_center - Boolean. Shall the data be mean centred Defaults to
TRUE.
normalise_variance - Boolean. Shall the data have normalised variance
Defaults to TRUE.
randomised - Boolean or NULL. Deprecated, use svd_solver. TRUE
maps to "randomised", FALSE to "exact". Defaults to NULL.
clr - Boolean. Shall the CLR-type PFlogPF be applied, see
Booeshaghi, et al. Defaults to FALSE.
size_factor - Numeric. The used size factor during I/O. It needs to be
the same as during I/O to have correct results when using the PFlogPF
transformation. Defaults to 10000.0.
svd_solver - String. Which solver to use. "covariance" builds the
gene x gene cross-product and eigendecomposes it: exact, and the fastest
option for a few thousand HVGs, but its cost grows with the square of the
gene number in memory and the cube in time. "randomised" is a randomised
SVD, approximate in the trailing components. "exact" is a full SVD on the
dense path and Lanczos on the sparse one. One of c("covariance", "randomised", "exact"). Defaults to "covariance".
Defaults are sctransform's own with vst.flavor = "v2" applied.
Only the step-1 fit scales with n_genes and n_cells: those bound the
subsample the negative binomial models are fitted on, and every later pass
streams gene by gene. Raising them costs fitting time, not memory.
params_sc_sctransform( n_genes = 2000L, n_cells = 2000L, min_cells = 5L, bw_adjust = 3, gmean_eps = 1, outlier_th = 10, poisson_diff_theta = 0.001, clip_min = NULL, clip_max = NULL )params_sc_sctransform( n_genes = 2000L, n_cells = 2000L, min_cells = 5L, bw_adjust = 3, gmean_eps = 1, outlier_th = 10, poisson_diff_theta = 0.001, clip_min = NULL, clip_max = NULL )
n_genes |
Integer. Genes in the step-1 subsample. Defaults to |
n_cells |
Integer. Cells in the step-1 subsample. Defaults to |
min_cells |
Integer. Minimum number of cells a gene must be detected in
to be modelled. Defaults to |
bw_adjust |
Numeric. Bandwidth multiplier for the kernel regression that
regularises the parameters. Defaults to |
gmean_eps |
Numeric. Offset in the geometric mean. Defaults to |
outlier_th |
Numeric. Threshold, in median absolute deviations, past
which a step-1 fit is treated as an outlier. Defaults to |
poisson_diff_theta |
Numeric. Below this, the fitted dispersion is taken
as the Poisson limit. Defaults to |
clip_min |
Numeric or |
clip_max |
Numeric or |
A named list with the following elements:
n_genes - Integer. Genes in the step-1 subsample. Defaults to 2000L.
n_cells - Integer. Cells in the step-1 subsample. Defaults to 2000L.
min_cells - Integer. Minimum number of cells a gene must be detected
in to be modelled. Defaults to 5L.
bw_adjust - Numeric. Bandwidth multiplier for the kernel regression
that regularises the parameters. Defaults to 3.0.
gmean_eps - Numeric. Offset in the geometric mean. Defaults to 1.0.
outlier_th - Numeric. Threshold, in median absolute deviations, past
which a step-1 fit is treated as an outlier. Defaults to 10.0.
poisson_diff_theta - Numeric. Below this, the fitted dispersion is
taken as the Poisson limit. Defaults to 0.001.
clip_min - Numeric or NULL. Lower residual clipping bound. NULL
uses -sqrt(n_cells). Must be given together with clip_max. Defaults to
NULL.
clip_max - Numeric or NULL. Upper residual clipping bound. NULL
uses sqrt(n_cells). Must be given together with clip_min. Defaults to
NULL.
Choudhary and Satija, Genome Biology, 2022.
Wrapper function for the SEACells parameters
params_sc_seacells( n_sea_cells, max_fw_iters = 50L, convergence_epsilon = 0.001, max_iter = 100L, min_iter = 10L, greedy_threshold = 20000L, graph_building = "union", pruning = TRUE, pruning_threshold = 1e-07, n_landmarks = NULL, knn = list() )params_sc_seacells( n_sea_cells, max_fw_iters = 50L, convergence_epsilon = 0.001, max_iter = 100L, min_iter = 10L, greedy_threshold = 20000L, graph_building = "union", pruning = TRUE, pruning_threshold = 1e-07, n_landmarks = NULL, knn = list() )
n_sea_cells |
Integer. Number of SEA cells to detect. Required. |
max_fw_iters |
Integer. Maximum iterations for the Franke-Wolfe
algorithm. Defaults to |
convergence_epsilon |
Numeric. Convergence threshold. Algorithm stops
when RSS change < epsilon * RSS(0). Defaults to |
max_iter |
Integer. Maximum iterations to run SEACells for. Defaults to
|
min_iter |
Integer. Minimum iterations to run SEACells for. Defaults to
|
greedy_threshold |
Integer. Maximum number of cells before defaulting to
rapid random selection of archetypes. Defaults to |
graph_building |
String. Graph building method. Defaults to |
pruning |
Boolean. Shall tiny values be pruned during Franke-Wolfe
updates. Defaults to |
pruning_threshold |
Numeric. If |
n_landmarks |
Integer or |
knn |
List. Optional overrides for kNN parameters. See
|
A named list with the following elements:
n_sea_cells - Integer. Number of SEA cells to detect. Required.
max_fw_iters - Integer. Maximum iterations for the Franke-Wolfe
algorithm. Defaults to 50L.
convergence_epsilon - Numeric. Convergence threshold. Algorithm stops
when RSS change < epsilon * RSS(0). Defaults to 0.001.
max_iter - Integer. Maximum iterations to run SEACells for. Defaults
to 100L.
min_iter - Integer. Minimum iterations to run SEACells for. Defaults
to 10L.
greedy_threshold - Integer. Maximum number of cells before defaulting
to rapid random selection of archetypes. Defaults to 20000L.
graph_building - String. Graph building method. Defaults to "union".
pruning - Boolean. Shall tiny values be pruned during Franke-Wolfe
updates. Defaults to TRUE.
pruning_threshold - Numeric. If pruning = TRUE values below which
threshold shall be pruned. Defaults to 1e-07.
n_landmarks - Integer or NULL. If provided, it will use the Nystroem
extension during the archetype finding. Useful for larger data sets.
Defaults to NULL.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
Wrapper function for the Seurat CCA parameters
params_sc_seurat_cca( num_cc = 30L, dims = 30L, k_anchor = 5L, k_filter = 200L, k_score = 30L, k_weight = 100L, n_top_features = 200L, l2_norm = TRUE, sd = 1, knn = list(), pca = params_sc_pca() )params_sc_seurat_cca( num_cc = 30L, dims = 30L, k_anchor = 5L, k_filter = 200L, k_score = 30L, k_weight = 100L, n_top_features = 200L, l2_norm = TRUE, sd = 1, knn = list(), pca = params_sc_pca() )
num_cc |
Integer. Number of canonical correlation dimensions to compute
for the anchor space. The effective rank used is |
dims |
Integer. Number of dimensions used for the anchor kNN queries and
the size of the returned embedding. Defaults to |
k_anchor |
Integer. Neighbourhood size for the mutual nearest neighbour
anchor search. Defaults to |
k_filter |
Integer. Neighbourhood size for the gene-space anchor filter.
Defaults to |
k_score |
Integer. Neighbourhood size for the shared-neighbour anchor
scoring. Defaults to |
k_weight |
Integer. Neighbourhood size for the kernel weights applied
during the correction. Defaults to |
n_top_features |
Integer. Number of top-loading genes used for the
gene-space anchor filter. Defaults to |
l2_norm |
Boolean. Shall the canonical correlation embedding be
L2-normalised per cell. Defaults to |
sd |
Numeric. Bandwidth divisor of the Gaussian kernel used for the
anchor weights. Defaults to |
knn |
List. Optional overrides for kNN parameters. See
|
pca |
List. Parameters to feed through to the optional recalculation of
the PCA, see |
A named list with the following elements:
num_cc - Integer. Number of canonical correlation dimensions to
compute for the anchor space. The effective rank used is max(num_cc, dims). Defaults to 30L.
dims - Integer. Number of dimensions used for the anchor kNN queries
and the size of the returned embedding. Defaults to 30L.
k_anchor - Integer. Neighbourhood size for the mutual nearest
neighbour anchor search. Defaults to 5L.
k_filter - Integer. Neighbourhood size for the gene-space anchor
filter. Defaults to 200L.
k_score - Integer. Neighbourhood size for the shared-neighbour anchor
scoring. Defaults to 30L.
k_weight - Integer. Neighbourhood size for the kernel weights applied
during the correction. Defaults to 100L.
n_top_features - Integer. Number of top-loading genes used for the
gene-space anchor filter. Defaults to 200L.
l2_norm - Boolean. Shall the canonical correlation embedding be
L2-normalised per cell. Defaults to TRUE.
sd - Numeric. Bandwidth divisor of the Gaussian kernel used for the
anchor weights. Defaults to 1.0.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
The elements of params_sc_pca(), overridden by pca, spliced in at
this position.
Stuart, et al., Cell, 2019
Wrapper function for the Seurat rPCA parameters
params_sc_seurat_rpca( dims = 30L, k_anchor = 5L, k_score = 30L, k_weight = 100L, l2_norm = TRUE, sd = 1, knn = list(), pca = params_sc_pca() )params_sc_seurat_rpca( dims = 30L, k_anchor = 5L, k_score = 30L, k_weight = 100L, l2_norm = TRUE, sd = 1, knn = list(), pca = params_sc_pca() )
dims |
Integer. Number of dimensions used for the per-batch PCA
projections, the anchor kNN queries and the size of the returned embedding.
Defaults to |
k_anchor |
Integer. Neighbourhood size for the mutual nearest neighbour
anchor search. Defaults to |
k_score |
Integer. Neighbourhood size for the shared-neighbour anchor
scoring. Defaults to |
k_weight |
Integer. Neighbourhood size for the kernel weights applied
during the correction. Defaults to |
l2_norm |
Boolean. Shall the projected embeddings be L2-normalised per
cell. Defaults to |
sd |
Numeric. Bandwidth divisor of the Gaussian kernel used for the
anchor weights. Defaults to |
knn |
List. Optional overrides for kNN parameters. See
|
pca |
List. Parameters to feed through to the optional recalculation of
the PCA, see |
A named list with the following elements:
dims - Integer. Number of dimensions used for the per-batch PCA
projections, the anchor kNN queries and the size of the returned embedding.
Defaults to 30L.
k_anchor - Integer. Neighbourhood size for the mutual nearest
neighbour anchor search. Defaults to 5L.
k_score - Integer. Neighbourhood size for the shared-neighbour anchor
scoring. Defaults to 30L.
k_weight - Integer. Neighbourhood size for the kernel weights applied
during the correction. Defaults to 100L.
l2_norm - Boolean. Shall the projected embeddings be L2-normalised per
cell. Defaults to TRUE.
sd - Numeric. Bandwidth divisor of the Gaussian kernel used for the
anchor weights. Defaults to 1.0.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
The elements of params_sc_pca(), overridden by pca, spliced in at
this position.
Stuart, et al., Cell, 2019
Wrapper function for parameters for SuperCell generation
params_sc_supercell( walk_length = 3L, graining_factor = 20, use_kernel = TRUE, k_ith = NULL, max_support = NULL, knn = list() )params_sc_supercell( walk_length = 3L, graining_factor = 20, use_kernel = TRUE, k_ith = NULL, max_support = NULL, knn = list() )
walk_length |
Integer. Walk length for the Walktrap algorithm. Defaults
to |
graining_factor |
Numeric. Graining level of data (proportion of number
of single cells in the initial dataset to the number of metacells in the
final dataset). (One meta cell per 20 cells.) Defaults to |
use_kernel |
Boolean. Shall a kernel function akin to MAGIC be applied
akin to the approach in SuperCell2, see Hérault, et al., bioRxiv, 2026 and
van Dijk, et al., Cell, 2018. Defaults to |
k_ith |
Integer or |
max_support |
Integer or |
knn |
List. Optional overrides for kNN parameters. See
|
A named list with the following elements:
walk_length - Integer. Walk length for the Walktrap algorithm.
Defaults to 3L.
graining_factor - Numeric. Graining level of data (proportion of
number of single cells in the initial dataset to the number of metacells in
the final dataset). (One meta cell per 20 cells.) Defaults to 20.0.
use_kernel - Boolean. Shall a kernel function akin to MAGIC be applied
akin to the approach in SuperCell2, see Hérault, et al., bioRxiv, 2026 and
van Dijk, et al., Cell, 2018. Defaults to TRUE.
k_ith - Integer or NULL. The k-ith neighbour to use for the kernel.
Defaults to NULL.
max_support - Integer or NULL. Caps each cell's walk-probability
vector to its top entries by mass, bounding memory at ~max_support * n_cells on large data. Makes the result an approximation. NULL (default)
keeps the walks exact. Defaults to NULL.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
Shapes the fixture with a planted ambient profile that
generate_cellsweep_test_data() builds. The defaults give 600 real
barcodes over 3 cell types plus 2000 empty droplets, on 200 genes.
params_sc_synthetic_cellsweep( n_real = 600L, n_empty = 2000L, n_genes = 200L, n_celltypes = 3L, n_markers = 20L, marker_weight = 25, ambient_dominance = 0.6, alpha_mean = 0.3, alpha_sd = 0.12, real_lib_size = 3000L, empty_lib_size = 120L )params_sc_synthetic_cellsweep( n_real = 600L, n_empty = 2000L, n_genes = 200L, n_celltypes = 3L, n_markers = 20L, marker_weight = 25, ambient_dominance = 0.6, alpha_mean = 0.3, alpha_sd = 0.12, real_lib_size = 3000L, empty_lib_size = 120L )
n_real |
Integer. Number of real barcodes. Cell types are assigned
round-robin over them. Defaults to |
n_empty |
Integer. Number of empty droplets. The ambient profile is
estimated off these, so at least 30 and preferably a lot more. Defaults to
|
n_genes |
Integer. Number of genes. Defaults to |
n_celltypes |
Integer. Number of cell types. Defaults to |
n_markers |
Integer. Width of each cell type's marker block. The blocks
are contiguous and disjoint, so |
marker_weight |
Numeric. Enrichment of a marker gene over background in
its own cell type's profile. Must exceed 1. Defaults to |
ambient_dominance |
Numeric. Fraction of the soup coming from the first
cell type. The remainder is flat background. Defaults to |
alpha_mean |
Numeric. Mean planted ambient fraction across real
barcodes. Defaults to |
alpha_sd |
Numeric. Spread of the planted ambient fraction. Defaults to
|
real_lib_size |
Integer. Expected library size of a real barcode.
Defaults to |
empty_lib_size |
Integer. Expected library size of an empty droplet.
Defaults to |
The soup is the first cell type plus flat background rather than a mixture of every cell type profile. A soup sitting in the span of the cell type profiles makes the contamination fraction unidentifiable, and the fixture would then be testing the repulsion term rather than the model.
A named list with the following elements:
n_real - Integer. Number of real barcodes. Cell types are assigned
round-robin over them. Defaults to 600L.
n_empty - Integer. Number of empty droplets. The ambient profile is
estimated off these, so at least 30 and preferably a lot more. Defaults to
2000L.
n_genes - Integer. Number of genes. Defaults to 200L.
n_celltypes - Integer. Number of cell types. Defaults to 3L.
n_markers - Integer. Width of each cell type's marker block. The
blocks are contiguous and disjoint, so n_markers * n_celltypes has to fit
into n_genes. Defaults to 20L.
marker_weight - Numeric. Enrichment of a marker gene over background
in its own cell type's profile. Must exceed 1. Defaults to 25.0.
ambient_dominance - Numeric. Fraction of the soup coming from the
first cell type. The remainder is flat background. Defaults to 0.6.
alpha_mean - Numeric. Mean planted ambient fraction across real
barcodes. Defaults to 0.3.
alpha_sd - Numeric. Spread of the planted ambient fraction. Defaults
to 0.12.
real_lib_size - Integer. Expected library size of a real barcode.
Defaults to 3000L.
empty_lib_size - Integer. Expected library size of an empty droplet.
Defaults to 120L.
For the generation of synthetic single cell data mostly for testing or showcasing purposes. The default configurations generates 1000 cells x 100 genes with genes 1:10 being cell markers for cell type 1, genes 11:20 for cell type 2 and genes 21:30 for cell type.
params_sc_synthetic_data( n_cells = 1000L, n_genes = 100L, n_batches = 1L, marker_genes = list(cell_type_1 = list(marker_genes = 0:9), cell_type_2 = list(marker_genes = 10:19), cell_type_3 = list(marker_genes = 20:29)), batch_effect_strength = c("strong", "medium", "weak"), n_samples = NULL, sample_bias = NULL )params_sc_synthetic_data( n_cells = 1000L, n_genes = 100L, n_batches = 1L, marker_genes = list(cell_type_1 = list(marker_genes = 0:9), cell_type_2 = list(marker_genes = 10:19), cell_type_3 = list(marker_genes = 20:29)), batch_effect_strength = c("strong", "medium", "weak"), n_samples = NULL, sample_bias = NULL )
n_cells |
Integer. Number of cells. Defaults to |
n_genes |
Integer. Number of genes. Defaults to |
n_batches |
Integer. Number of batches. Defaults to |
marker_genes |
Any. A nested list that indicates which gene indices are
markers for which cell. Defaults to |
batch_effect_strength |
String. The strength of the batch effect to add.
One of |
n_samples |
Integer or |
sample_bias |
Any. One of |
A named list with the following elements:
n_cells - Integer. Number of cells. Defaults to 1000L.
n_genes - Integer. Number of genes. Defaults to 100L.
marker_genes - Any. A nested list that indicates which gene indices
are markers for which cell. Defaults to list(cell_type_1 = list(marker_genes = 0:9), cell_type_2 = list(marker_genes = 10:19), cell_type_3 = list(marker_genes = 20:29)).
n_batches - Integer. Number of batches. Defaults to 1L.
batch_effect_strength - String. The strength of the batch effect to
add. One of c("strong", "medium", "weak"). Defaults to "strong".
n_samples - Integer or NULL. Shall sample membership be added to the
synthetic data. If you want sample information you need to provide
n_samples and sample_bias. Defaults to NULL.
sample_bias - Any. One of c("even", "slightly_uneven", "very_uneven") Defaults to NULL.
For the generation of synthetic single cell data mostly for testing or showcasing purposes. In this case, ADT counts to test multi-modal integration. The default configurations generates 1000 cells x 15 proteins with probes 1:3 being cell markers for cell type 1, genes 4:6 for cell type 2 and genes 7:9 for cell type. Columns 13:15 represents isotype controls.
params_sc_synthetic_data_adt( n_cells = 1000L, n_proteins = 15L, n_batches = 1L, marker_genes = list(cell_type_1 = list(marker_genes = 0:2), cell_type_2 = list(marker_genes = 3:5), cell_type_3 = list(marker_genes = 6:8)), isotype_controls = 12:14, batch_effect_strength = c("strong", "medium", "weak") )params_sc_synthetic_data_adt( n_cells = 1000L, n_proteins = 15L, n_batches = 1L, marker_genes = list(cell_type_1 = list(marker_genes = 0:2), cell_type_2 = list(marker_genes = 3:5), cell_type_3 = list(marker_genes = 6:8)), isotype_controls = 12:14, batch_effect_strength = c("strong", "medium", "weak") )
n_cells |
Integer. Number of cells. Defaults to |
n_proteins |
Integer. Number of proteins Defaults to |
n_batches |
Integer. Number of batches. Defaults to |
marker_genes |
Any. A nested list that indicates which gene indices are
markers for which cell. Defaults to |
isotype_controls |
Integer vector. The columns that defines the isotype
controls. (0-indexed!) Defaults to |
batch_effect_strength |
String. The strength of the batch effect to add.
One of |
A named list with the following elements:
n_cells - Integer. Number of cells. Defaults to 1000L.
n_proteins - Integer. Number of proteins Defaults to 15L.
marker_genes - Any. A nested list that indicates which gene indices
are markers for which cell. Defaults to list(cell_type_1 = list(marker_genes = 0:2), cell_type_2 = list(marker_genes = 3:5), cell_type_3 = list(marker_genes = 6:8)).
n_batches - Integer. Number of batches. Defaults to 1L.
isotype_controls - Integer vector. The columns that defines the
isotype controls. (0-indexed!) Defaults to 12:14.
batch_effect_strength - String. The strength of the batch effect to
add. One of c("strong", "medium", "weak"). Defaults to "strong".
Shapes the fixture with a planted multicellular programme that
generate_dialogue_test_data() builds. The defaults give 14
samples x 25 cells x 3 cell types = 1050 cells over 400 genes.
params_sc_synthetic_dialogue( n_samples = 14L, cells_per_sample = 25L, n_cell_types = 3L, n_features = 8L, n_sample_features = 5L, n_genes = 400L, n_planted = 8L )params_sc_synthetic_dialogue( n_samples = 14L, cells_per_sample = 25L, n_cell_types = 3L, n_features = 8L, n_sample_features = 5L, n_genes = 400L, n_planted = 8L )
n_samples |
Integer. Samples the experiment spans. DIALOGUE needs at
least 5. Defaults to |
cells_per_sample |
Integer. Cells per sample per cell type. Defaults to
|
n_cell_types |
Integer. Number of cell types. Must be at least 2.
Defaults to |
n_features |
Integer. Feature columns per cell type. Must be at least 2.
Defaults to |
n_sample_features |
Integer. Feature columns carrying a per-sample
component. The first of those is the shared programme, the rest are
cell-type-specific nuisance; anything past this count is pure noise and
exists so the ANOVA filter has something to reject. Defaults to |
n_genes |
Integer. Number of genes. Defaults to |
n_planted |
Integer. Planted genes per cell type. The blocks are
contiguous, so |
n_genes defaults higher here than on the Rust side, which plants the same
structure into 90. R runs the counts through the normal ingestion path, and
on a small panel the planted genes are a large enough share of the library
that log-normalisation divides the signal back out: at 90 genes the library
size tracks the programme at an r of 0.75 and background genes pick up a
spurious correlation of their own. At 400 the planted block is a few percent
of the library and the contrast survives. The Rust tests feed the planted
layer straight in, so they never meet this.
A named list with the following elements:
n_samples - Integer. Samples the experiment spans. DIALOGUE needs at
least 5. Defaults to 14L.
cells_per_sample - Integer. Cells per sample per cell type. Defaults
to 25L.
n_cell_types - Integer. Number of cell types. Must be at least 2.
Defaults to 3L.
n_features - Integer. Feature columns per cell type. Must be at least
2. Defaults to 8L.
n_sample_features - Integer. Feature columns carrying a per-sample
component. The first of those is the shared programme, the rest are
cell-type-specific nuisance; anything past this count is pure noise and
exists so the ANOVA filter has something to reject. Defaults to 5L.
n_genes - Integer. Number of genes. Defaults to 400L.
n_planted - Integer. Planted genes per cell type. The blocks are
contiguous, so n_planted * n_cell_types has to fit into n_genes.
Defaults to 8L.
Jerby-Arnon & Regev, Nature Biotechnology, 2022
Wrapper function for parameters for VISION with auto-correlation
params_sc_vision(n_perm = 500L, n_cluster = 5L, knn = list())params_sc_vision(n_perm = 500L, n_cluster = 5L, knn = list())
n_perm |
Integer. Number of random gene sets to generate per cluster.
Defaults to |
n_cluster |
Integer. Number of clusters for the random gene set
clustering generation. Defaults to |
knn |
List. Optional overrides for kNN parameters. See
|
A named list with the following elements:
n_perm - Integer. Number of random gene sets to generate per cluster.
Defaults to 500L.
n_cluster - Integer. Number of clusters for the random gene set
clustering generation. Defaults to 5L.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
Wrapper function for WNN parameters
params_sc_wnn( k_nn = 20L, knn_range = 200L, sigma_method = c("snn_farthest", "sigma_idx"), sigma_idx = 19L, snn_type = c("full_connection", "limited"), s_nn = 20L, sd_scale = 1, kernel_power = 1, cross_const = 1e-04, sigma_floor = 1e-08, knn = list() )params_sc_wnn( k_nn = 20L, knn_range = 200L, sigma_method = c("snn_farthest", "sigma_idx"), sigma_idx = 19L, snn_type = c("full_connection", "limited"), s_nn = 20L, sd_scale = 1, kernel_power = 1, cross_const = 1e-04, sigma_floor = 1e-08, knn = list() )
k_nn |
Integer. Final number of multimodal neighbours per cell. Defaults
to |
knn_range |
Integer. Candidate pool size per modality. Each cell's kNN
input must contain at least this many neighbours. Defaults to |
sigma_method |
String. Bandwidth method. One of |
sigma_idx |
Integer. |
snn_type |
String. sNN type. The limited version only considers edges
that exist in the kNN. One of |
s_nn |
Integer. |
sd_scale |
Numeric. Multiplier on sigma. Defaults to |
kernel_power |
Numeric. Kernel exponent power. Defaults to |
cross_const |
Numeric. Cross-modality kernel stabiliser. Defaults to
|
sigma_floor |
Numeric. Minimum sigma value (avoids division by zero).
Defaults to |
knn |
List. Optional overrides for kNN parameters. See
|
A named list with the following elements:
k_nn - Integer. Final number of multimodal neighbours per cell.
Defaults to 20L.
knn_range - Integer. Candidate pool size per modality. Each cell's kNN
input must contain at least this many neighbours. Defaults to 200L.
sigma_method - String. Bandwidth method. One of c("snn_farthest", "sigma_idx"). Defaults to "snn_farthest".
sigma_idx - Integer. "sigma_idx" only: 0-based kNN index for
bandwidth.e. k_nn - 1). Defaults to 19L.
snn_type - String. sNN type. The limited version only considers edges
that exist in the kNN. One of c("full_connection", "limited"). Defaults to
"full_connection".
s_nn - Integer. "snn_farthest" only: kNN size used to build the SNN
graph. Defaults to 20L.
sd_scale - Numeric. Multiplier on sigma. Defaults to 1.0.
kernel_power - Numeric. Kernel exponent power. Defaults to 1.0.
cross_const - Numeric. Cross-modality kernel stabiliser. Defaults to
1e-04.
sigma_floor - Numeric. Minimum sigma value (avoids division by zero).
Defaults to 1e-08.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
Constructor for the scDblFinder parameters. This method combines cluster-aware doublet simulation with a gradient-boosted classifier trained on engineered features.
params_scdblfinder( n_genes = 1352L, doublet_ratio = 1, heterotypic_bias = 1, cluster_resolution = 1, cluster_iters = 10L, fast_cluster = FALSE, n_iterations = 3L, gbm_n_trees = 200L, max_depth = 4L, learning_rate = 0.3, min_samples_leaf = 20L, subsample_rate = 0.75, cv_folds = 5L, cv_early_stop = 2L, se_fraction = 1, include_pcs = 19L, expected_doublet_rate = NULL, cxds_genes = NULL, manual_threshold = NULL, normalisation = list(mean_center = TRUE), pca = list(), knn = list(k = 0L), fast_cluster_params = list() )params_scdblfinder( n_genes = 1352L, doublet_ratio = 1, heterotypic_bias = 1, cluster_resolution = 1, cluster_iters = 10L, fast_cluster = FALSE, n_iterations = 3L, gbm_n_trees = 200L, max_depth = 4L, learning_rate = 0.3, min_samples_leaf = 20L, subsample_rate = 0.75, cv_folds = 5L, cv_early_stop = 2L, se_fraction = 1, include_pcs = 19L, expected_doublet_rate = NULL, cxds_genes = NULL, manual_threshold = NULL, normalisation = list(mean_center = TRUE), pca = list(), knn = list(k = 0L), fast_cluster_params = list() )
n_genes |
Integer. Number of top-expressed genes to use as features.
Defaults to |
doublet_ratio |
Numeric. Ratio of simulated doublets to observed cells.
Defaults to |
heterotypic_bias |
Numeric. Fraction of simulated pairs forced to come
from different clusters (0-1). Defaults to |
cluster_resolution |
Numeric. Resolution for the initial Louvain
clustering. Defaults to |
cluster_iters |
Integer. Number of Louvain iterations per clustering
step. Defaults to |
fast_cluster |
Boolean. Shall fast Louvain clustering be applied, i.e.,
k-means clustering and use the centroids for kNN graph generation and Louvain
clustering with then backpropagating the membership based on centroid
proximity. Defaults to |
n_iterations |
Integer. Number of refinement iterations. Typically 2-3.
Defaults to |
gbm_n_trees |
Integer. Maximum number of boosting rounds for the GBM
classifier. Defaults to |
max_depth |
Integer. Maximum tree depth. Shallow trees (3-5) work best.
Defaults to |
learning_rate |
Numeric. Shrinkage applied to each tree. Defaults to
|
min_samples_leaf |
Integer. Minimum training samples per leaf. Defaults
to |
subsample_rate |
Numeric. Fraction of samples used per tree. Defaults to
|
cv_folds |
Integer. Number of cross-validation folds for boosting round
selection. Defaults to |
cv_early_stop |
Integer. Early stopping patience per CV fold. Defaults
to |
se_fraction |
Numeric. Multiplier on the standard error for the SE rule
used in round selection. Defaults to |
include_pcs |
Any. Number of leading principal components to include as
classifier features. Defaults to |
expected_doublet_rate |
Numeric or |
cxds_genes |
Integer or |
manual_threshold |
Numeric or |
normalisation |
List. Optional overrides for normalisation parameters.
See |
pca |
List. Optional overrides for PCA parameters. See
|
knn |
List. Optional overrides for kNN parameters. See
|
fast_cluster_params |
List. Optional overrides for the fast clustering
parameters. Only relevant if |
A named list with the following elements:
The elements of params_norm_doublets_defaults(), overridden by
normalisation, spliced in at this position.
The elements of params_pca_defaults(), overridden by pca, spliced
in at this position.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
The elements of params_fast_cluster_default(), overridden by
fast_cluster_params, spliced in at this position.
n_genes - Integer. Number of top-expressed genes to use as features.
Defaults to 1352L.
doublet_ratio - Numeric. Ratio of simulated doublets to observed
cells. Defaults to 1.0.
heterotypic_bias - Numeric. Fraction of simulated pairs forced to come
from different clusters (0-1). Defaults to 1.0.
cluster_resolution - Numeric. Resolution for the initial Louvain
clustering. Defaults to 1.0.
cluster_iters - Integer. Number of Louvain iterations per clustering
step. Defaults to 10L.
fast_cluster - Boolean. Shall fast Louvain clustering be applied,
i.e., k-means clustering and use the centroids for kNN graph generation and
Louvain clustering with then backpropagating the membership based on
centroid proximity. Defaults to FALSE.
n_iterations - Integer. Number of refinement iterations. Typically
2-3. Defaults to 3L.
gbm_n_trees - Integer. Maximum number of boosting rounds for the GBM
classifier. Defaults to 200L.
max_depth - Integer. Maximum tree depth. Shallow trees (3-5) work
best. Defaults to 4L.
learning_rate - Numeric. Shrinkage applied to each tree. Defaults to
0.3.
min_samples_leaf - Integer. Minimum training samples per leaf.
Defaults to 20L.
subsample_rate - Numeric. Fraction of samples used per tree. Defaults
to 0.75.
cv_folds - Integer. Number of cross-validation folds for boosting
round selection. Defaults to 5L.
cv_early_stop - Integer. Early stopping patience per CV fold. Defaults
to 2L.
se_fraction - Numeric. Multiplier on the standard error for the SE
rule used in round selection. Defaults to 1.0.
include_pcs - Any. Number of leading principal components to include
as classifier features. Defaults to 19L.
expected_doublet_rate - Numeric or NULL. Expected doublet rate as a
percentage. If not provided, will be calculated internally. Defaults to
NULL.
manual_threshold - Numeric or NULL. Manual score threshold. If
NULL (default), expected-rate thresholding is used. Defaults to NULL.
cxds_genes - Integer or NULL. Number of CXDS genes to consider. If
not provided, defaults to 500L. Defaults to NULL.
Constructor for SCENIC parameters
params_scenic( min_counts = 50L, min_cells = 0.03, learner_type = c("randomforest", "extratrees", "grnboost2"), gene_batch_strategy = c("correlated", "random"), gene_batch_size = NULL, n_pcs = 50L, n_subsample = 100000L, learner_params = list() )params_scenic( min_counts = 50L, min_cells = 0.03, learner_type = c("randomforest", "extratrees", "grnboost2"), gene_batch_strategy = c("correlated", "random"), gene_batch_size = NULL, n_pcs = 50L, n_subsample = 100000L, learner_params = list() )
min_counts |
Integer. Minimum total counts a gene needs to be included
in the analysis. Defaults to |
min_cells |
Numeric. Minimum proportion of cells (between 0 and 1) that
must express a gene for it to be considered. Defaults to |
learner_type |
String. Regression learner to use. One of
|
gene_batch_strategy |
String. Strategy for grouping target genes into
batches. Only used for |
gene_batch_size |
Integer or |
n_pcs |
Integer. Number of PCs to use for the correlated gene batch
strategy. Defaults to |
n_subsample |
Integer. Cell subsampling threshold for the correlated
gene batch strategy. If the number of cells meets or exceeds this value,
|
learner_params |
List. Optional overrides for the regression learner
parameters. For |
A named list with the following elements:
min_counts - Integer. Minimum total counts a gene needs to be included
in the analysis. Defaults to 50L.
min_cells - Numeric. Minimum proportion of cells (between 0 and 1)
that must express a gene for it to be considered. Defaults to 0.03.
learner_type - String. Regression learner to use. One of
c("randomforest", "extratrees", "grnboost2"). Defaults to
"randomforest".
gene_batch_strategy - String. Strategy for grouping target genes into
batches. Only used for "randomforest" and "extratrees" learners; ignored
for "grnboost2". One of c("correlated", "random"). Defaults to
"correlated".
gene_batch_size - Integer or NULL. Number of genes per batch. If
NULL (default), the batch size is determined automatically. Ignored for
"grnboost2". Defaults to NULL.
n_pcs - Integer. Number of PCs to use for the correlated gene batch
strategy. Defaults to 50L.
n_subsample - Integer. Cell subsampling threshold for the correlated
gene batch strategy. If the number of cells meets or exceeds this value,
n_subsample cells are randomly selected prior to running randomised SVD.
Defaults to 100000L.
The elements of the base list, overridden by learner_params, spliced
in at this position.
Each regulon gets its own threshold. A two-component Gaussian
mixture is fitted and compared against a single Gaussian by BIC; if the
mixture wins, the threshold is the kernel density minimum between the two
component means, otherwise it falls back to mean + 2 * sd. This follows
pySCENIC. AUCell fits six candidates and then lets the density trough
override all of them whenever one exists, so the two land in much the same
place. Turn bw_adjust up if shallow wobbles in the density are being picked
up as troughs. AUCell effectively runs at 2.
params_scenic_binarise(bw_adjust = 1, n_grid = 512L, n_bins = 512L)params_scenic_binarise(bw_adjust = 1, n_grid = 512L, n_bins = 512L)
bw_adjust |
Numeric. Multiplier on the Silverman bandwidth of the kernel
density estimate. Higher values smooth more. Defaults to |
n_grid |
Integer. Number of points at which the density is evaluated
between the two component means. Defaults to |
n_bins |
Integer. Number of histogram bins used to approximate the
density. Defaults to |
A named list with the following elements:
bw_adjust - Numeric. Multiplier on the Silverman bandwidth of the
kernel density estimate. Higher values smooth more. Defaults to 1.0.
n_grid - Integer. Number of points at which the density is evaluated
between the two component means. Defaults to 512L.
n_bins - Integer. Number of histogram bins used to approximate the
density. Defaults to 512L.
Aibar, et al., Nat Methods, 2017
Default parameters for the SCENIC ExtraTrees regression learner
params_scenic_extra_trees_defaults()params_scenic_extra_trees_defaults()
A named list with the following elements:
n_trees - Any. TODO Defaults to 500L.
min_samples_leaf - Any. TODO Defaults to 50L.
n_features_split - Any. TODO Defaults to 0L.
n_thresholds - Any. TODO Defaults to 1L.
max_depth - Any. TODO Defaults to 8L.
subsample_frac - Any. TODO Defaults to NULL.
Default parameters for the SCENIC GradientBoosting (GRNBoost2) regression learner
params_scenic_gradient_boosting_defaults()params_scenic_gradient_boosting_defaults()
A named list with the following elements:
n_trees_max - Any. TODO Defaults to 1000L.
learning_rate - Any. TODO Defaults to 0.01.
max_depth - Any. TODO Defaults to 3L.
min_samples_leaf - Any. TODO Defaults to 50L.
early_stop_window - Any. TODO Defaults to 25L.
subsample_rate - Any. TODO Defaults to 0.9.
n_features_split - Any. TODO Defaults to 0L.
Default parameters for the SCENIC RandomForest regression learner
params_scenic_random_forest_defaults()params_scenic_random_forest_defaults()
A named list with the following elements:
n_trees - Any. TODO Defaults to 250L.
min_samples_leaf - Any. TODO Defaults to 50L.
n_features_split - Any. TODO Defaults to 0L.
subsample_rate - Any. TODO Defaults to 0.632.
bootstrap - Any. TODO Defaults to FALSE.
max_depth - Any. TODO Defaults to 8L.
subsample_frac - Any. TODO Defaults to NULL.
Constructor for the various Scrublet parameters. In this case,
the default for the kNN graph generation was set to "hnsw" as this
algorithm showed the best performance in different empirical benchmarks.
params_scrublet( sim_doublet_ratio = 1.5, expected_doublet_rate = 0.1, stdev_doublet_rate = 0.02, n_bins_histogram = 100L, manual_threshold = NULL, normalisation = list(), hvg = list(), pca = list(), knn = list(k = 0L) )params_scrublet( sim_doublet_ratio = 1.5, expected_doublet_rate = 0.1, stdev_doublet_rate = 0.02, n_bins_histogram = 100L, manual_threshold = NULL, normalisation = list(), hvg = list(), pca = list(), knn = list(k = 0L) )
sim_doublet_ratio |
Numeric. Number of doublets to simulate relative to
the number of observed cells. For example, 2.0 simulates twice as many
doublets as there are cells. Defaults to |
expected_doublet_rate |
Numeric. Expected doublet rate for the
experiment, typically 0.05-0.10 depending on cell loading. Must be between 0
and 1. Defaults to |
stdev_doublet_rate |
Numeric. Uncertainty in the expected doublet rate.
Defaults to |
n_bins_histogram |
Integer. Number of bins for histogram-based automatic
threshold detection. Typically 50-100. Defaults to |
manual_threshold |
Numeric or |
normalisation |
List. Optional overrides for normalisation parameters.
See |
hvg |
List. Optional overrides for highly variable gene selection
parameters. See |
pca |
List. Optional overrides for PCA parameters. See
|
knn |
List. Optional overrides for kNN parameters. See
|
A named list with the following elements:
The elements of params_norm_doublets_defaults(), overridden by
normalisation, spliced in at this position.
The elements of params_hvg_defaults(), overridden by hvg, spliced
in at this position.
The elements of params_pca_defaults(), overridden by pca, spliced
in at this position.
The elements of params_knn_defaults(), overridden by knn, spliced
in at this position.
sim_doublet_ratio - Numeric. Number of doublets to simulate relative
to the number of observed cells. For example, 2.0 simulates twice as many
doublets as there are cells. Defaults to 1.5.
expected_doublet_rate - Numeric. Expected doublet rate for the
experiment, typically 0.05-0.10 depending on cell loading. Must be between 0
and 1. Defaults to 0.1.
stdev_doublet_rate - Numeric. Uncertainty in the expected doublet
rate. Defaults to 0.02.
n_bins_histogram - Integer. Number of bins for histogram-based
automatic threshold detection. Typically 50-100. Defaults to 100L.
manual_threshold - Numeric or NULL. Manual doublet score threshold.
If NULL (default), threshold is automatically detected from simulated
doublet score distribution. Defaults to NULL.
Controls the per-cell path of assign_sc_type(): how the raw
ScType scores are rescaled, how hard the scores get smoothed over the sNN
graph, and where the cut-offs for an Unknown call and for a mixed cluster
sit.
params_sctype_cells( alpha = 0.5, iterations = 2L, tolerance = 1e-04, calibration = c("none", "column_z"), score_floor = 0.25, purity_threshold = 0.9 )params_sctype_cells( alpha = 0.5, iterations = 2L, tolerance = 1e-04, calibration = c("none", "column_z"), score_floor = 0.25, purity_threshold = 0.9 )
alpha |
Numeric. Numeric in |
iterations |
Integer. Integer >= 0. Number of smoothing iterations. |
tolerance |
Numeric. Numeric > 0. Convergence tolerance for the
smoothing. Defaults to |
calibration |
String. |
score_floor |
Numeric. Numeric >= 0. Minimum score for a cell to get a
call instead of |
purity_threshold |
Numeric. Numeric in |
A named list with the following elements:
alpha - Numeric. Numeric in [0, 1]. Self-retention during smoothing.
Each iteration computes alpha * original + (1 - alpha) * neighbour_average. Defaults to 0.5.
iterations - Integer. Integer >= 0. Number of smoothing iterations.
0 disables smoothing. Defaults to 2L.
tolerance - Numeric. Numeric > 0. Convergence tolerance for the
smoothing. Defaults to 1e-04.
calibration - String. "column_z" standardises each cell type's score
column across cells, which removes the bias towards cell types whose marker
sets happen to produce larger scores. One of c("none", "column_z").
Defaults to "none".
score_floor - Numeric. Numeric >= 0. Minimum score for a cell to get a
call instead of NA. Defaults to 0.25.
purity_threshold - Numeric. Numeric in [0, 1]. Cluster purity above
which the hybrid assignment keeps the cluster-level call. Defaults to 0.9.
Zhou et al., NIPS, 2004.
Wrapper function to generate SNF parameters
params_snf( k = 20L, t = 20L, mu = 0.5, alpha = 1, normalise = TRUE, distance_metric = c("euclidean", "manhattan", "canberra", "cosine") )params_snf( k = 20L, t = 20L, mu = 0.5, alpha = 1, normalise = TRUE, distance_metric = c("euclidean", "manhattan", "canberra", "cosine") )
k |
Integer. Number of neighbours to consider. Defaults to |
t |
Integer. Number of iterations for the SNF algorithm. Defaults to
|
mu |
Numeric. Normalisation factor for the Gaussian kernel width.
Defaults to |
alpha |
Numeric. Normalisation parameter controlling the fusion
strength. Defaults to |
normalise |
Boolean. Shall continuous values be Z-scored. Defaults to
|
distance_metric |
String. Which distance metric to use for the
continuous calculations. In case of pure categorical, Hamming will be used,
for mixed data types Gower distance is used. One of |
A named list with the following elements:
k - Integer. Number of neighbours to consider. Defaults to 20L.
t - Integer. Number of iterations for the SNF algorithm. Defaults to
20L.
mu - Numeric. Normalisation factor for the Gaussian kernel width.
Defaults to 0.5.
alpha - Numeric. Normalisation parameter controlling the fusion
strength. Defaults to 1.0.
distance_metric - String. Which distance metric to use for the
continuous calculations. In case of pure categorical, Hamming will be used,
for mixed data types Gower distance is used. One of c("euclidean", "manhattan", "canberra", "cosine"). Defaults to "euclidean".
normalise - Boolean. Shall continuous values be Z-scored. Defaults to
TRUE.
Wrapper function to generate ssGSEA parameters
params_ssgsea(alpha = 0.25, min_size = 5L, max_size = 500L, normalise = TRUE)params_ssgsea(alpha = 0.25, min_size = 5L, max_size = 500L, normalise = TRUE)
alpha |
Numeric. The exponent defining the weight of the tail in the
random walk performed by ssGSEA. Defaults to |
min_size |
Integer. Minimum number of genes per gene set. Defaults to
|
max_size |
Integer. Maximum number of genes per gene set. Defaults to
|
normalise |
Boolean. Shall the scores be normalised. Defaults to |
A named list with the following elements:
alpha - Numeric. The exponent defining the weight of the tail in the
random walk performed by ssGSEA. Defaults to 0.25.
min_size - Integer. Minimum number of genes per gene set. Defaults to
5L.
max_size - Integer. Maximum number of genes per gene set. Defaults to
500L.
normalise - Boolean. Shall the scores be normalised. Defaults to
TRUE.
Default parameters for Symphony query mapping
params_symphony_map(sigma = 0.1, lambda = 1)params_symphony_map(sigma = 0.1, lambda = 1)
sigma |
Numeric. Soft-clustering fuzziness for query -> reference
centroid assignment. Symphony R default is 0.1. Defaults to |
lambda |
Numeric. Ridge penalty on batch coefficients. Symphony R
hardcodes 1.0. Defaults to |
A named list with the following elements:
sigma - Numeric. Soft-clustering fuzziness for query -> reference
centroid assignment. Symphony R default is 0.1. Defaults to 0.1.
lambda - Numeric. Ridge penalty on batch coefficients. Symphony R
hardcodes 1.0. Defaults to 1.0.
Parameters for synthetic_bulk_cor_matrix(). Counts
come from a negative binomial with a mean-dispersion trend; co-expression
modules are planted by putting each module's genes on a shared latent factor.
The generator picks how loadings and factors are drawn, which is what makes
a given dataset a fair or unfair benchmark for a given method:
"hub_modular" - LogNormal loadings on a Normal factor. Some genes end
up far more connected than others, so this is the WGCNA-style default.
"modular" - Beta(5, 2) loadings on a Normal factor. Homogeneous
within-module correlation and no hubs.
"non_negative_factor" -
LogNormal loadings on a Gamma factor. The activity matrix is non-negative by
construction, so NMF has a ground truth it can actually reach.
"non_gaussian_factor" - LogNormal loadings on a Laplace factor.
Non-Gaussian sources satisfy ICA identifiability.
params_synthetic_bulk_rnaseq( num_samples = 100L, num_genes = 1000L, module_sizes = c(100L, 100L, 100L), generator = c("hub_modular", "modular", "non_negative_factor", "non_gaussian_factor"), seed = 123L, mean_exp_gamma_shape = 5, mean_exp_gamma_scale = 10, disp_intercept = 0.2, disp_slope = 0.3, noise_std = 0.1, factor_std = 0.5, factor_shape = 2, factor_scale = 0.3, loading_mu = 0, loading_sigma = 0.7, hub_percentile = 0.1 )params_synthetic_bulk_rnaseq( num_samples = 100L, num_genes = 1000L, module_sizes = c(100L, 100L, 100L), generator = c("hub_modular", "modular", "non_negative_factor", "non_gaussian_factor"), seed = 123L, mean_exp_gamma_shape = 5, mean_exp_gamma_scale = 10, disp_intercept = 0.2, disp_slope = 0.3, noise_std = 0.1, factor_std = 0.5, factor_shape = 2, factor_scale = 0.3, loading_mu = 0, loading_sigma = 0.7, hub_percentile = 0.1 )
num_samples |
Integer. Number of samples (columns) to simulate. Defaults
to |
num_genes |
Integer. Number of genes (rows) to simulate. Defaults to
|
module_sizes |
Integer vector. Sizes of the co-expression modules. The
sum must be smaller or equal to |
generator |
String. Which topology and distribution family to plant.,
see the description. One of |
seed |
Integer. Seed for reproducibility purposes. Defaults to |
mean_exp_gamma_shape |
Numeric. Shape and scale of the Gamma the
per-gene mean expression is drawn from. Defaults to |
mean_exp_gamma_scale |
Numeric. Shape and scale of the Gamma the
per-gene mean expression is drawn from. Defaults to |
disp_intercept |
Numeric. Intercept and slope of the negative binomial
dispersion trend |
disp_slope |
Numeric. Intercept and slope of the negative binomial
dispersion trend |
noise_std |
Numeric. Per-gene per-sample noise standard deviation on the
latent log-signal. Smaller values track the module factor more tightly and
give stronger within-module correlation. Defaults to |
factor_std |
Numeric. Standard deviation of the Normal factor. Only used
by |
factor_shape |
Numeric. Shape and scale of the Gamma factor for
|
factor_scale |
Numeric. Shape and scale of the Gamma factor for
|
loading_mu |
Numeric. Location and scale of the LogNormal the loadings
are drawn from. Unused by |
loading_sigma |
Numeric. Location and scale of the LogNormal the
loadings are drawn from. Unused by |
hub_percentile |
Numeric. Top fraction of module genes flagged as hubs
by loading rank. Must be in |
noise_std and factor_std default to 0.1 and 0.5, not to the
bixverse-rs values of 0.3 and 0.3. At the crate defaults the
"modular" generator plants modules too weakly to detect at 1000 genes by
100 samples: the within-module minus cross-module mean absolute Spearman gap
comes out around 0.06, against 0.17 to 0.23 for the other three. The
values here put all four generators in the 0.30 to 0.39 band, so a
comparison across generators reflects the method rather than the signal
strength it happened to be handed. Pass the crate values explicitly if you
want a harder problem.
A named list with the following elements:
num_samples - Integer. Number of samples (columns) to simulate.
Defaults to 100L.
num_genes - Integer. Number of genes (rows) to simulate. Defaults to
1000L.
module_sizes - Integer vector. Sizes of the co-expression modules. The
sum must be smaller or equal to num_genes. Genes are assigned in
contiguous blocks from the first gene onwards, any remainder is background.
Use integer(0) for no modules. Must be an integer vector, see the note
below. Defaults to c(100L, 100L, 100L).
generator - String. Which topology and distribution family to plant.,
see the description. One of c("hub_modular", "modular", "non_negative_factor", "non_gaussian_factor"). Defaults to "hub_modular".
seed - Integer. Seed for reproducibility purposes. Defaults to 123L.
mean_exp_gamma_shape - Numeric. Shape and scale of the Gamma the
per-gene mean expression is drawn from. Defaults to 5.0.
mean_exp_gamma_scale - Numeric. Shape and scale of the Gamma the
per-gene mean expression is drawn from. Defaults to 10.0.
disp_intercept - Numeric. Intercept and slope of the negative binomial
dispersion trend disp = 1 / (a + b * mean). This is what gives you
heteroskedasticity: lowly expressed genes show higher variance. Defaults to
0.2.
disp_slope - Numeric. Intercept and slope of the negative binomial
dispersion trend disp = 1 / (a + b * mean). This is what gives you
heteroskedasticity: lowly expressed genes show higher variance. Defaults to
0.3.
noise_std - Numeric. Per-gene per-sample noise standard deviation on
the latent log-signal. Smaller values track the module factor more tightly
and give stronger within-module correlation. Defaults to 0.1.
factor_std - Numeric. Standard deviation of the Normal factor. Only
used by "hub_modular" and "modular"; the other two generators draw their
factor from factor_shape/factor_scale instead. Defaults to 0.5.
factor_shape - Numeric. Shape and scale of the Gamma factor for
"non_negative_factor". factor_scale doubles as the Laplace scale for
"non_gaussian_factor". Defaults to 2.0.
factor_scale - Numeric. Shape and scale of the Gamma factor for
"non_negative_factor". factor_scale doubles as the Laplace scale for
"non_gaussian_factor". Defaults to 0.3.
loading_mu - Numeric. Location and scale of the LogNormal the loadings
are drawn from. Unused by "modular", which draws Beta(5, 2). Defaults to
0.0.
loading_sigma - Numeric. Location and scale of the LogNormal the
loadings are drawn from. Unused by "modular", which draws Beta(5, 2).
Defaults to 0.7.
hub_percentile - Numeric. Top fraction of module genes flagged as hubs
by loading rank. Must be in (0, 1]. Defaults to 0.1.
Zhang & Horvath, Stat Appl Genet Mol Biol, 2005
Use MAD outlier detection on per-cell QC metrics
per_cell_qc_outlier( metric, threshold = 3, direction = c("twosided", "below", "above") )per_cell_qc_outlier( metric, threshold = 3, direction = c("twosided", "below", "above") )
metric |
Numerical vector. The QC metric to check for. |
threshold |
Numeric. How many MADs in either direction to consider for outlier detection. |
direction |
String. One of |
A list with:
outlier - Boolean vector indicating which cell is an outlier
metrics - The applied thresholds.
# one badly undersequenced cell, flagged on the lower tail only set.seed(42L) lib_size <- c(rnorm(99, 1000, 100), 50) res <- per_cell_qc_outlier(lib_size, direction = "below") sum(res$outlier) res$metrics# one badly undersequenced cell, flagged on the lower tail only set.seed(42L) lib_size <- c(rnorm(99, 1000, 100), 50) res <- per_cell_qc_outlier(lib_size, direction = "below") sum(res$outlier) res$metrics
This function generate node-degree adjusted permutations of a given diffusion score and adds Z-scores to the object. The function will automatically determine if the original diffusion was a single or tied diffusion and construct permutations accordingly.
permute_seed_nodes( object, perm_iters = 1000L, random_seed = 10101L, .verbose = TRUE )permute_seed_nodes( object, perm_iters = 1000L, random_seed = 10101L, .verbose = TRUE )
object |
|
perm_iters |
Integer. Number of permutations to test for. Defaults to
|
random_seed |
Integer. Random seed for determinism. |
.verbose |
Boolean. Controls verbosity. |
The class with added diffusion score based on a single set of seed genes. Additionally, the seed genes are stored in the class.
# 100 node-degree adjusted permutations of a single diffusion set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes(object, c(node_1 = 1, node_3 = 1), "max") object <- permute_seed_nodes(object, perm_iters = 100L, .verbose = FALSE) head(get_diffusion_perms(object))# 100 node-degree adjusted permutations of a single diffusion set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- diffuse_seed_nodes(object, c(node_1 = 1, node_3 = 1), "max") object <- permute_seed_nodes(object, perm_iters = 100L, .verbose = FALSE) head(get_diffusion_perms(object))
Wrapper around manifoldsR::phate() for the SingleCells and MetaCells
classes. PHATE (Potential of Heat-diffusion for Affinity-based Trajectory
Embedding) produces a low-dimensional embedding that preserves both local and
global structure by operating on a diffusion process over the data manifold.
Unlike UMAP or t-SNE, PHATE is explicitly designed to reveal continuous
progressions and branching structure, making it the preferred choice for data
with developmental or trajectory-like organisation.
When use_knn = TRUE (the default), the kNN graph already stored on the
object is reused; otherwise neighbours are computed from the chosen
embedding. The algorithm then constructs a diffusion operator, raises it to a
power (the diffusion time t, see manifoldsR::params_phate()) that
denoises the manifold, and computes potential distances that are finally
embedded via metric MDS.
Because PHATE inherently smooths over the kNN graph, it pairs naturally
with MetaCells: the combination yields a particularly clean view of
continuous biological processes on denoised data.
phate_sc( object, use_knn = TRUE, embd_to_use = "pca", slot_name = "phate", no_embd_to_use = NULL, modality = c("rna", "adt", "wnn"), n_dim = 2L, k = 5L, knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"), nn_params = manifoldsR::params_nn(), phate_params = manifoldsR::params_phate(), seed = 42L, .verbose = TRUE )phate_sc( object, use_knn = TRUE, embd_to_use = "pca", slot_name = "phate", no_embd_to_use = NULL, modality = c("rna", "adt", "wnn"), n_dim = 2L, k = 5L, knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"), nn_params = manifoldsR::params_nn(), phate_params = manifoldsR::params_phate(), seed = 42L, .verbose = TRUE )
object |
|
use_knn |
Boolean. Use the kNN graph found in the object. Defaults to
|
embd_to_use |
String. The embedding to use for PHATE. Must be available in the object. |
slot_name |
String. The name of this embedding within the object.
Defaults to |
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
modality |
String. On which modality to run PHATE. One of
|
n_dim |
Integer. Number of PHATE dimensions. Currently only |
k |
Integer. Number of nearest neighbours for graph construction.
Defaults to |
knn_method |
String. Approximate nearest neighbour algorithm. One of
|
nn_params |
Named list. See |
phate_params |
Named list. See |
seed |
Integer. For reproducibility. |
.verbose |
Boolean. Controls verbosity. |
The object with a "phate" embedding added.
# PHATE embedding off the cached kNN graph sc <- demo_single_cells() sc <- phate_sc(sc, .verbose = FALSE) dim(get_embedding(sc, "phate")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# PHATE embedding off the cached kNN graph sc <- demo_single_cells() sc <- phate_sc(sc, .verbose = FALSE) dim(get_embedding(sc, "phate")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Helper plot function for boxplot of normalised data
plot_boxplot_normalization(samples, norm_counts, group_col)plot_boxplot_normalization(samples, norm_counts, group_col)
samples |
data.table with sample information with perc_detected_genes and a column specifying the cohort. |
norm_counts |
Numeric matrix. The normalised log2 expression, genes x
samples, with the samples in the same order as the rows of |
group_col |
String. The grouping column. |
ggplot object, i.e., box plot with expression per sample.
# normalised expression per sample, coloured by cohort syn <- synthetic_bulk_cor_matrix() samples <- data.table::data.table( sample_id = colnames(syn$counts), cohort = rep(c("case", "control"), each = 50) ) norm_counts <- rs_cpm(syn$counts, lib_size = NULL, log = TRUE, prior_count = 0.5) plot_boxplot_normalization(samples, norm_counts, group_col = "cohort")# normalised expression per sample, coloured by cohort syn <- synthetic_bulk_cor_matrix() samples <- data.table::data.table( sample_id = colnames(syn$counts), cohort = rep(c("case", "control"), each = 50) ) norm_counts <- rs_cpm(syn$counts, lib_size = NULL, log = TRUE, prior_count = 0.5) plot_boxplot_normalization(samples, norm_counts, group_col = "cohort")
Plots the epsilon results (if they can be found in the class). The x-axis reflects the different epsilon parameters for the radial basis function, and the y-axis the R2 value that the resulting networks follows a power law distribution (i.e., scale free topology).
plot_epsilon_res(object)plot_epsilon_res(object)
object |
The class, see |
If epsilon results were found, returns the ggplot. Otherwise, throws a warning and returns NULL.
# epsilon versus scale-free fit mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_check_epsilon( obj, rbf_func = "gaussian", .verbose = FALSE ) plot_epsilon_res(obj)# epsilon versus scale-free fit mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_check_epsilon( obj, rbf_func = "gaussian", .verbose = FALSE ) plot_epsilon_res(obj)
Plots the median-absolute deviation of the genes and applied thresholds.
Expects that preprocess_bulk_coexp() was run and will throw an
error otherwise.
plot_hvgs(object, bins = 50L)plot_hvgs(object, bins = 50L)
object |
The underlying class, see |
bins |
Integer. Number of bins to plot. |
# MAD distribution with the selected genes highlighted syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 200L, .verbose = FALSE) plot_hvgs(object)# MAD distribution with the selected genes highlighted syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 200L, .verbose = FALSE) plot_hvgs(object)
Helper function to plot various parameters with the no of components. You have:
Median stability - The median stability of the clusters at this no of components given random initialisations.
% Converged - The percentage of ICA runs that converged at this no of components.
IC redundancy - 1 - max_abs_loading_cor, one minus the largest
absolute correlation between two component loadings at this level.
Combined score - The product of the three other scores.
If found, the function will also add the optimal number of components based
on ica_optimal_ncomp() (if a loess function could be fitted).
plot_ica_ncomp_params(object)plot_ica_ncomp_params(object)
object |
The class, see |
The plot with no comp ~ vs. various parameters.
# stability, convergence and orthogonality against ncomp mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) obj <- ica_evaluate_comp( obj, ica_type = "logcosh", ncomp_params = params_ica_ncomp(custom_seq = seq(2L, 20L, by = 2L)), .verbose = FALSE ) plot_ica_ncomp_params(obj)# stability, convergence and orthogonality against ncomp mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) obj <- ica_evaluate_comp( obj, ica_type = "logcosh", ncomp_params = params_ica_ncomp(custom_seq = seq(2L, 20L, by = 2L)), .verbose = FALSE ) plot_ica_ncomp_params(obj)
Helper function to plot the individual stability profiles over the tested ncomps.
plot_ica_stability_individual(object)plot_ica_stability_individual(object)
object |
The class, see |
A ggplot with the per-component stability profiles over the tested number of components.
# per-component stability profiles across the tested ncomps mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) obj <- ica_evaluate_comp( obj, ica_type = "logcosh", ncomp_params = params_ica_ncomp(custom_seq = seq(2L, 20L, by = 2L)), .verbose = FALSE ) plot_ica_stability_individual(obj)# per-component stability profiles across the tested ncomps mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- ica_processing(obj, .verbose = FALSE) obj <- ica_evaluate_comp( obj, ica_type = "logcosh", ncomp_params = params_ica_ncomp(custom_seq = seq(2L, 20L, by = 2L)), .verbose = FALSE ) plot_ica_stability_individual(obj)
Plots the optimal k vs. median of median R2 graph to identify the optimal number of cuts.
plot_optimal_cuts(object)plot_optimal_cuts(object)
object |
The class, see |
If optimal cuts results were found, returns the ggplot. Otherwise, throws a warning and returns NULL.
# k cuts versus median R2, with the chosen cut marked mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_coremo_clustering(obj, .verbose = FALSE) plot_optimal_cuts(obj)# k cuts versus median R2, with the chosen cut marked mat <- t(synthetic_signal_matrix()$mat) obj <- BulkCoExp(mat, data.table::data.table(sample_id = rownames(mat))) obj <- preprocess_bulk_coexp(obj, hvg = 0.3, .verbose = FALSE) obj <- cor_module_processing(obj, cor_method = "spearman", .verbose = FALSE) obj <- cor_module_coremo_clustering(obj, .verbose = FALSE) plot_optimal_cuts(obj)
Helper plot function for pca with contrasts
plot_pca(pca_dt, grps)plot_pca(pca_dt, grps)
pca_dt |
data.table. data.table with PCA and contrast information. |
grps |
String. Name of the column in |
ggplot object for the pca
# PC1 against PC2, coloured by the named grouping column syn <- synthetic_bulk_cor_matrix() pcs <- stats::prcomp(t(log1p(syn$counts)))$x[, 1:2] pca_dt <- data.table::data.table( PC_1 = pcs[, 1], PC_2 = pcs[, 2], cohort = rep(c("case", "control"), each = 50) ) plot_pca(pca_dt, grps = "cohort")# PC1 against PC2, coloured by the named grouping column syn <- synthetic_bulk_cor_matrix() pcs <- stats::prcomp(t(log1p(syn$counts)))$x[, 1:2] pca_dt <- data.table::data.table( PC_1 = pcs[, 1], PC_2 = pcs[, 2], cohort = rep(c("case", "control"), each = 50) ) plot_pca(pca_dt, grps = "cohort")
Plot the PCA data
plot_pca_res( object, cols_to_plot = c("contrast_info", "sample_source"), pcs_to_plot = c("PC_1", "PC_2"), ... )plot_pca_res( object, cols_to_plot = c("contrast_info", "sample_source"), pcs_to_plot = c("PC_1", "PC_2"), ... )
object |
The underlying class, see |
cols_to_plot |
String vector. The columns within the meta-data to plot.
Defaults to |
pcs_to_plot |
String vector of length 2.
Will default to |
... |
additional parameters |
A plot if the PCA information was found. NULL if no PCA was found.
# PCA faceted over the metadata columns you care about syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_pca_bulk_dge(object, no_hvg_genes = 500L) plot_pca_res(object, cols_to_plot = "case_control")# PCA faceted over the metadata columns you care about syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) object <- normalise_bulk_dge( object, group_col = "case_control", .verbose = FALSE ) object <- calculate_pca_bulk_dge(object, no_hvg_genes = 500L) plot_pca_res(object, cols_to_plot = "case_control")
Boxplot of the number of detected genes per sample, split by cohort. Used in the bulk DGE pre-processing report to spot samples with poor library complexity.
plot_preprocessing_genes(samples, group_col)plot_preprocessing_genes(samples, group_col)
samples |
data.table with sample information with nb_detected_genes and a column specifying the cohort. |
group_col |
String specifying the column with cohort information |
ggplot object, i.e. boxplot with number of genes by cohort
# detected genes per sample, split by cohort syn <- synthetic_bulk_cor_matrix() samples <- data.table::data.table( cohort = rep(c("case", "control"), each = 50), nb_detected_genes = colSums(syn$counts > 0) ) plot_preprocessing_genes(samples, group_col = "cohort")# detected genes per sample, split by cohort syn <- synthetic_bulk_cor_matrix() samples <- data.table::data.table( cohort = rep(c("case", "control"), each = 50), nb_detected_genes = colSums(syn$counts > 0) ) plot_preprocessing_genes(samples, group_col = "cohort")
Beeswarm plot of the percentage of detected genes per sample, with the outlier cutoffs drawn in. Used in the bulk DGE pre-processing report.
plot_preprocessing_outliers(samples, group_col, min_perc, max_perc)plot_preprocessing_outliers(samples, group_col, min_perc, max_perc)
samples |
data.table with sample information with perc_detected_genes and a column specifying the cohort. |
group_col |
String specifying the column with cohort information |
min_perc |
Numeric. Lower cutoff to identify outliers. |
max_perc |
Numeric. Upper cutoff to identify outliers |
ggplot object, i.e., beeswarm plot with outlier indication
# percentage of detected genes with the outlier cutoffs drawn in syn <- synthetic_bulk_cor_matrix() samples <- data.table::data.table( cohort = rep(c("case", "control"), each = 50), perc_detected_genes = colMeans(syn$counts > 0) * 100 ) plot_preprocessing_outliers( samples, group_col = "cohort", min_perc = 60, max_perc = 95 )# percentage of detected genes with the outlier cutoffs drawn in syn <- synthetic_bulk_cor_matrix() samples <- data.table::data.table( cohort = rep(c("case", "control"), each = 50), perc_detected_genes = colMeans(syn$counts > 0) * 100 ) plot_preprocessing_outliers( samples, group_col = "cohort", min_perc = 60, max_perc = 95 )
This function plots the distance to affinity relationship after applying a given radial basis function (RBF) with a given epsilon value.
plot_rbf_impact(rbf_type, epsilon)plot_rbf_impact(rbf_type, epsilon)
rbf_type |
String. The RBF type. One of
|
epsilon |
Numerical. The epsilon parameter. |
A plot depicting the distance to affinity relationship after applying the RBF function.
# distance to affinity under a Gaussian RBF with epsilon 2 plot_rbf_impact(rbf_type = "gaussian", epsilon = 2)# distance to affinity under a Gaussian RBF with epsilon 2 plot_rbf_impact(rbf_type = "gaussian", epsilon = 2)
Plots the resolution results (if they can be found in the class). The x-axis reflects the different resolutions and the y axis the modularity observed with that resolution.
plot_resolution_res(object, print_head = TRUE, ...)plot_resolution_res(object, print_head = TRUE, ...)
object |
The class, either |
print_head |
Boolean. Print the Top5 resolution parameters and their
meta data. Only applicable for |
... |
Additional arguments to parse to the functions. |
Plots the result, if the results were found in the class. Otherwise, throws a warning and returns NULL.
# modularity across the resolutions tested on an RBH graph set.seed(123) modules <- data.table::data.table( origin = rep(c("set_a", "set_b"), each = 20), module = rep(c("m1", "m2", "m3", "m4"), each = 10), gene = unlist(replicate(4, sample(letters, 10), simplify = FALSE)) ) object <- RbhGraph( modules, rbh_type = "set", dataset_col = "origin", module_col = "module", value_col = "gene" ) object <- generate_rbh_graph(object, minimum_similarity = 0) object <- find_rbh_communities(object, parallel = FALSE, .verbose = FALSE) plot_resolution_res(object)# modularity across the resolutions tested on an RBH graph set.seed(123) modules <- data.table::data.table( origin = rep(c("set_a", "set_b"), each = 20), module = rep(c("m1", "m2", "m3", "m4"), each = 10), gene = unlist(replicate(4, sample(letters, 10), simplify = FALSE)) ) object <- RbhGraph( modules, rbh_type = "set", dataset_col = "origin", module_col = "module", value_col = "gene" ) object <- generate_rbh_graph(object, minimum_similarity = 0) object <- find_rbh_communities(object, parallel = FALSE, .verbose = FALSE) plot_resolution_res(object)
Helper plot function for Voom normalisation
plot_voom_normalization(norm_counts)plot_voom_normalization(norm_counts)
norm_counts |
Numeric matrix. The voom log2-CPM values, genes x samples. |
ggplot object, i.e., voom normalisation plot.
# mean variance trend after voom syn <- synthetic_bulk_cor_matrix() grp <- rep(c("case", "control"), each = 50) norm_counts <- rs_cpm(syn$counts, lib_size = NULL, log = TRUE, prior_count = 0.5) plot_voom_normalization(norm_counts)# mean variance trend after voom syn <- synthetic_bulk_cor_matrix() grp <- rep(c("case", "control"), each = 50) norm_counts <- rs_cpm(syn$counts, lib_size = NULL, log = TRUE, prior_count = 0.5) plot_voom_normalization(norm_counts)
The three selection metrics and the combined score against k. Look for the
k where the combined score peaks, then sanity check that Cao Juan has not
already started climbing, which is the signal that topics are duplicating.
## S3 method for class 'LdaKSweepResult' plot(x, ...)## S3 method for class 'LdaKSweepResult' plot(x, ...)
x |
|
... |
Ignored. |
A ggplot2 object with one panel per metric.
# the three selection metrics and the combined score against k set.seed(42L) corpus <- matrix(rbinom(200L * 40L, 1L, 0.05), nrow = 200L, ncol = 40L) corpus[1:100, 1:10] <- rbinom(1000L, 1L, 0.6) corpus[101:200, 11:20] <- rbinom(1000L, 1L, 0.6) colnames(corpus) <- sprintf("term_%02d", 1:40) sweep_res <- lda_k_sweep(corpus > 0, k_range = 5:7, .verbose = FALSE) plot(sweep_res)# the three selection metrics and the combined score against k set.seed(42L) corpus <- matrix(rbinom(200L * 40L, 1L, 0.05), nrow = 200L, ncol = 40L) corpus[1:100, 1:10] <- rbinom(1000L, 1L, 0.6) corpus[101:200, 11:20] <- rbinom(1000L, 1L, 0.6) colnames(corpus) <- sprintf("term_%02d", 1:40) sweep_res <- lda_k_sweep(corpus > 0, k_range = 5:7, .verbose = FALSE) plot(sweep_res)
Stability and relative reconstruction error against k, the standard cNMF
diagnostic. Pick the k where stability is still high and the error curve
has not yet flattened out. Values of k whose consensus step failed have no
stability point.
## S3 method for class 'NmfKSweepResult' plot(x, ...)## S3 method for class 'NmfKSweepResult' plot(x, ...)
x |
|
... |
Additional params. Currently unused. |
A ggplot2 object with the two curves side by side.
Kotliar et al., eLife, 2019
# stability against reconstruction error across k sc <- demo_single_cells() res <- nmf_k_sweep_sc( sc, k_range = 2:4, n_runs = 3L, nmf_consensus_params = params_nmf_consensus(density_threshold = 2), .verbose = FALSE ) plot(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# stability against reconstruction error across k sc <- demo_single_cells() res <- nmf_k_sweep_sc( sc, k_range = 2:4, n_runs = 3L, nmf_consensus_params = params_nmf_consensus(density_threshold = 2), .verbose = FALSE ) plot(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function calculates needed information for semantic similiary calculations
pre_process_sim_onto(object, .verbose = TRUE)pre_process_sim_onto(object, .verbose = TRUE)
object |
|
.verbose |
Boolean. Controls verbosity of the function. |
The class with added pre-processed data for semantic similarities to the properties.
# ancestors, descendants and information content added to the class onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) onto_obj <- OntologySim(onto, .verbose = FALSE) onto_obj <- pre_process_sim_onto(onto_obj, .verbose = FALSE) names(S7::prop(onto_obj, "outputs"))# ancestors, descendants and information content added to the class onto <- data.table::data.table( parent = c("a", "b", "b", "b", "c"), child = c("b", "c", "d", "e", "f"), type = c("part_of", "part_of", "part_of", "is_a", "is_a") ) onto_obj <- OntologySim(onto, .verbose = FALSE) onto_obj <- pre_process_sim_onto(onto_obj, .verbose = FALSE) names(S7::prop(onto_obj, "outputs"))
This function is a helper to generate a list of cell type to cell marker
annotations that can be fed into for example [REF].
prepare_cell_markers(obj, marker_df)prepare_cell_markers(obj, marker_df)
obj |
A single cell class, i.e. one of |
marker_df |
A data.table with the columns |
A list of cell type to marker associations ready for subsequent usage. Genes not found in the object will be automatically removed.
# a marker table turned into the indexed list the scorers want sc <- demo_single_cells(prepped = FALSE) markers <- data.table::data.table( cell_type = c("type_a", "type_a", "type_b"), gene_id = get_gene_names(sc)[1:3] ) names(prepare_cell_markers(sc, markers)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# a marker table turned into the indexed list the scorers want sc <- demo_single_cells(prepped = FALSE) markers <- data.table::data.table( cell_type = c("type_a", "type_a", "type_b"), gene_id = get_gene_names(sc)[1:3] ) names(prepare_cell_markers(sc, markers)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Function to do general pre-processing on top of the BulkCoExp().
Options to do scaling, HVG selection, etc.
preprocess_bulk_coexp( object, hvg = NULL, mad_threshold = NULL, scaling = FALSE, scaling_type = c("normal", "robust"), .verbose = TRUE )preprocess_bulk_coexp( object, hvg = NULL, mad_threshold = NULL, scaling = FALSE, scaling_type = c("normal", "robust"), .verbose = TRUE )
object |
The underlying class, see |
hvg |
Integer or float. If an integer, the top |
mad_threshold |
Float. Instead of of selecting number or proportion of genes, you can also provide a mad_threshold. |
scaling |
Boolean. Shall the data be scaled. |
scaling_type |
String. You have the option to use normal scaling or robust scaling. |
.verbose |
Boolean. Controls the verbosity of the function. |
Returns the class with the processed_data data slot populated and
applied parameters added to the params slot.
# keep the 200 most variable genes for module detection syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 200L, .verbose = FALSE) object# keep the 200 most variable genes for module detection syn <- synthetic_bulk_cor_matrix() mat <- log1p(t(syn$counts)) meta <- data.table::data.table(sample_id = rownames(mat)) object <- BulkCoExp(raw_data = mat, meta_data = meta) object <- preprocess_bulk_coexp(object, hvg = 200L, .verbose = FALSE) object
This is a deprecated method and will raise an error. Please use
qc_bulk_dge() and normalise_bulk_dge() instead.
preprocess_bulk_dge( object, group_col, norm_method = c("TMM", "TMMwsp", "RLE", "upperquartile", "none"), outlier_threshold = 2, min_prop = 0.2, .verbose = TRUE )preprocess_bulk_dge( object, group_col, norm_method = c("TMM", "TMMwsp", "RLE", "upperquartile", "none"), outlier_threshold = 2, min_prop = 0.2, .verbose = TRUE )
object |
The underlying class, see |
group_col |
String. The column in the metadata that will contain the contrast groups. Needs to be part of the metadata stored in the class. |
norm_method |
String. One of
|
outlier_threshold |
Float. Number of standard deviations in terms of
percentage genes detected you allow before removing a sample. Defaults to |
min_prop |
Float. Minimum proportion of samples in which the gene has to be identified in. |
.verbose |
Boolean. Controls the verbosity of the function. |
Throws an error, as it is deprecated.
Pre-scan multiple h5ad files for multi-sample loading
prescan_h5ad_files( h5_paths, gene_universe = c("intersection", "union"), var_index = "_index", raw_count_slot = c("auto", "X", "raw.X", "layers.counts"), .verbose = TRUE )prescan_h5ad_files( h5_paths, gene_universe = c("intersection", "union"), var_index = "_index", raw_count_slot = c("auto", "X", "raw.X", "layers.counts"), .verbose = TRUE )
h5_paths |
Character vector of file paths to h5ad files. If names are provided, these will be used as experimental identifiers. |
gene_universe |
One of "intersection" or "union". |
var_index |
String. The name within the h5ad var part in which the
variable names are stored. Defaults to |
raw_count_slot |
Where raw counts live. |
.verbose |
Boolean. Controls verbosity of the function. |
A list with:
universe - Character vector of gene names in the universe
file_tasks - List of per-file task structures, each containing: exp_id, h5_path, cs_type, no_cells, no_genes, gene_local_to_universe (integer vector, NA for genes not in universe, 0-indexed into universe)
# build the gene universe across two files before a multi-file load data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) files <- c(a = tempfile(fileext = ".h5ad"), b = tempfile(fileext = ".h5ad")) for (f in files) { write_h5ad_sc(f, data$counts, data$obs, data$var, .verbose = FALSE) } tasks <- prescan_h5ad_files(h5_paths = files, .verbose = FALSE) tasks$universe_size unlink(files)# build the gene universe across two files before a multi-file load data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) files <- c(a = tempfile(fileext = ".h5ad"), b = tempfile(fileext = ".h5ad")) for (f in files) { write_h5ad_sc(f, data$counts, data$obs, data$var, .verbose = FALSE) } tasks <- prescan_h5ad_files(h5_paths = files, .verbose = FALSE) tasks$universe_size unlink(files)
Walks each input directory, reads the features file to build the
intersection of gene IDs across inputs (matched by the first column,
typically Ensembl gene IDs), decompresses any .mtx.gz files into a
temporary directory, and builds the file tasks expected by
load_multi_mtx().
Each input directory must contain the standard 10x trio: a .mtx (or
.mtx.gz) file, a barcodes file, and a features/genes file. File names are
matched by extension; if a directory contains multiple matching files an
error is raised.
prescan_mtx_dirs( dirs, exp_ids, cells_as_rows = FALSE, has_hdr = FALSE, .verbose = TRUE )prescan_mtx_dirs( dirs, exp_ids, cells_as_rows = FALSE, has_hdr = FALSE, .verbose = TRUE )
dirs |
Character vector of input directories. Length >= 2. |
exp_ids |
Character vector of experiment identifiers, one per directory. Must be unique. |
cells_as_rows |
Boolean. Applied uniformly to all inputs. Defaults
to |
has_hdr |
Boolean. Whether the barcodes/features files have a
header row. Applied uniformly. Defaults to |
.verbose |
Boolean. Controls verbosity of the function. |
A list with:
universe - Character vector of gene IDs in the intersection, in the order they will appear in the final var table.
universe_size - Length of the universe.
file_tasks - List of per-input task lists for Rust and DuckDB.
temp_files - Character vector of temp files created during
decompression; the caller should unlink() these after use.
# two CellRanger style directories reduced to their shared gene universe data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) dirs <- c(tempfile("cr_a"), tempfile("cr_b")) for (d in dirs) { dir.create(d, recursive = TRUE) write_cellranger_output( d, data$counts, data$obs, data$var, rows = "cells", format_type = "csv", .verbose = FALSE ) } scan_res <- prescan_mtx_dirs( dirs = dirs, exp_ids = c("a", "b"), cells_as_rows = TRUE, has_hdr = TRUE, .verbose = FALSE ) scan_res$universe_size unlink(c(dirs, scan_res$temp_files), recursive = TRUE, force = TRUE)# two CellRanger style directories reduced to their shared gene universe data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) dirs <- c(tempfile("cr_a"), tempfile("cr_b")) for (d in dirs) { dir.create(d, recursive = TRUE) write_cellranger_output( d, data$counts, data$obs, data$var, rows = "cells", format_type = "csv", .verbose = FALSE ) } scan_res <- prescan_mtx_dirs( dirs = dirs, exp_ids = c("a", "b"), cells_as_rows = TRUE, has_hdr = TRUE, .verbose = FALSE ) scan_res$universe_size unlink(c(dirs, scan_res$temp_files), recursive = TRUE, force = TRUE)
Walks each input file, reads the per-file feature ids, restricts to the
chosen feature_type (V3 only; ignored for V2), builds the intersection
or union universe of gene ids, and returns the file tasks expected by
load_multi_tenx_h5().
V2 and V3 files can be mixed in a single batch. Gene matching is done on
the feature id (ensembl-style) since gene symbols collide.
prescan_tenx_h5_files( h5_paths, feature_type = "Gene Expression", gene_universe = c("intersection", "union"), .verbose = TRUE )prescan_tenx_h5_files( h5_paths, feature_type = "Gene Expression", gene_universe = c("intersection", "union"), .verbose = TRUE )
h5_paths |
Character vector of file paths to 10x h5 files. If names are provided, these will be used as experimental identifiers. |
feature_type |
String. Modality to keep across all files (V3 only;
ignored for V2). Defaults to |
gene_universe |
One of |
.verbose |
Boolean. Controls verbosity. |
A list with:
universe - Character vector of gene ids in the universe.
universe_size - Length of the universe.
file_tasks - Named list of per-file task structures, each
containing exp_id, h5_path, version, no_cells, no_genes,
feature_type and gene_local_to_universe (integer vector, NA
for features outside the universe / non-target modality, 0-indexed).
# gene universe across two 10x h5 files data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) features <- data.table::data.table( id = data$var$gene_id, name = data$var$ensembl_id, feature_type = "Gene Expression" ) files <- c(a = tempfile(fileext = ".h5"), b = tempfile(fileext = ".h5")) for (f in files) { write_tenx_h5_sc(f, data$counts, data$obs$cell_id, features) } scan_res <- prescan_tenx_h5_files(h5_paths = files, .verbose = FALSE) scan_res$universe_size unlink(files)# gene universe across two 10x h5 files data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) features <- data.table::data.table( id = data$var$gene_id, name = data$var$ensembl_id, feature_type = "Gene Expression" ) files <- c(a = tempfile(fileext = ".h5"), b = tempfile(fileext = ".h5")) for (f in files) { write_tenx_h5_sc(f, data$counts, data$obs$cell_id, features) } scan_res <- prescan_tenx_h5_files(h5_paths = files, .verbose = FALSE) scan_res$universe_size unlink(files)
For every (sender, ligand, receiver, receptor) tuple drawn from the LR network, compute a weighted prioritisation score combining:
ligand DE in the sender (de_ligand)
receptor DE in the receiver (de_receptor)
ligand activity in the receiver (activity_scaled)
ligand expression specificity across senders (exprs_ligand)
receptor expression specificity across receivers (exprs_receptor)
ligand condition specificity (ligand_condition_specificity)
receptor condition specificity (receptor_condition_specificity)
Components with weight zero are skipped (their joins are not performed).
DE tables are supplied by the user. find_markers_sc and
find_all_markers_sc give a quick Wilcox; pseudo-bulk + DESeq2 / edgeR is
generally preferable for statistical inference.
prioritise_interactions( celltype_de, expression_info, ligand_activities, lr_network, senders_oi, receivers_oi, condition_de = NULL, weights = NULL, scenario = c("case_control", "one_condition") )prioritise_interactions( celltype_de, expression_info, ligand_activities, lr_network, senders_oi, receivers_oi, condition_de = NULL, weights = NULL, scenario = c("case_control", "one_condition") )
celltype_de |
A |
expression_info |
A |
ligand_activities |
A |
lr_network |
A |
senders_oi |
Character vector of sender cluster IDs. |
receivers_oi |
Character vector of receiver cluster IDs. |
condition_de |
Optional |
weights |
Optional named numeric vector. If |
scenario |
|
A data.table with one row per surviving
(sender, ligand, receiver, receptor) tuple, sorted by descending
prioritisation_score, with a prioritisation_rank column.
# one sender, one receiver, with the signal planted on L1 -> R1 lr_network <- data.table::data.table( ligand = c("L1", "L2"), receptor = c("R1", "R2") ) celltype_de <- data.table::CJ( cluster_id = c("sender", "receiver"), gene = c("L1", "L2", "R1", "R2") ) celltype_de[, lfc := c(3, 0.1, 0.1, 0.1, 0.1, 0.1, 2.5, 0.1)] celltype_de[, pval := c(1e-10, 0.5, 0.5, 0.5, 0.5, 0.5, 1e-9, 0.5)] expression_info <- celltype_de[, .(cluster_id, gene, avg_expr = 0.05)] expression_info[cluster_id == "sender" & gene == "L1", avg_expr := 5] expression_info[cluster_id == "receiver" & gene == "R1", avg_expr := 5] res <- prioritise_interactions( celltype_de = celltype_de, expression_info = expression_info, ligand_activities = data.table::data.table( ligand = c("L1", "L2"), aupr_corrected = c(0.75, -0.125) ), lr_network = lr_network, senders_oi = "sender", receivers_oi = "receiver", scenario = "one_condition" ) head(res[, .(sender, ligand, receiver, receptor, prioritisation_score)])# one sender, one receiver, with the signal planted on L1 -> R1 lr_network <- data.table::data.table( ligand = c("L1", "L2"), receptor = c("R1", "R2") ) celltype_de <- data.table::CJ( cluster_id = c("sender", "receiver"), gene = c("L1", "L2", "R1", "R2") ) celltype_de[, lfc := c(3, 0.1, 0.1, 0.1, 0.1, 0.1, 2.5, 0.1)] celltype_de[, pval := c(1e-10, 0.5, 0.5, 0.5, 0.5, 0.5, 1e-9, 0.5)] expression_info <- celltype_de[, .(cluster_id, gene, avg_expr = 0.05)] expression_info[cluster_id == "sender" & gene == "L1", avg_expr := 5] expression_info[cluster_id == "receiver" & gene == "R1", avg_expr := 5] res <- prioritise_interactions( celltype_de = celltype_de, expression_info = expression_info, ligand_activities = data.table::data.table( ligand = c("L1", "L2"), aupr_corrected = c(0.75, -0.125) ), lr_network = lr_network, senders_oi = "sender", receivers_oi = "receiver", scenario = "one_condition" ) head(res[, .(sender, ligand, receiver, receptor, prioritisation_score)])
Helper function that takes in different files containing gene ontology data and puts them together for various gene set enrichment methods using the ontological information
process_go_data(go_info, go_genes, go_relationships)process_go_data(go_info, go_genes, go_relationships)
go_info |
data.table. Contains |
go_genes |
data.table. Contains |
go_relationships |
data.table. Contains |
data.table ready for usage in GeneOntologyElim().
# assemble the packaged human GO data by hand go_data <- load_go_human_data() relationships <- data.table::setnames( data.table::copy(go_data$gene_ontology), old = c("from", "to"), new = c("parent", "child") ) go_dt <- process_go_data( go_info = go_data$go_info, go_genes = go_data$go_to_genes, go_relationships = relationships[relationship %in% c("is_a", "part_of")] ) dim(go_dt)# assemble the packaged human GO data by hand go_data <- load_go_human_data() relationships <- data.table::setnames( data.table::copy(go_data$gene_ontology), old = c("from", "to"), new = c("parent", "child") ) go_dt <- process_go_data( go_info = go_data$go_info, go_genes = go_data$go_to_genes, go_relationships = relationships[relationship %in% c("is_a", "part_of")] ) dim(go_dt)
Sums the raw counts per sample and treats the result as a bulk experiment. That is the whole method, and it is the one that holds its nominal false discovery rate when the cells within a sample are not independent, which they never are.
This is a convenience wrapper: it pseudo-bulks with
get_pseudobulked_sc() and hands the aggregate to
run_edger_ql(). Reach for the two separately if you want to
inspect or reuse the aggregated matrix.
pseudobulk_dge_sc( object, cell_list, design, coef = NULL, contrast = NULL, edger_params = params_edger_ql(), .verbose = TRUE )pseudobulk_dge_sc( object, cell_list, design, coef = NULL, contrast = NULL, edger_params = params_edger_ql(), .verbose = TRUE )
object |
|
cell_list |
Named list of character vectors. The cell identifiers per
pseudo-bulk sample. The names become the sample identifiers, and the rows of
|
design |
Numeric matrix. The design matrix of samples x coefficients,
including the intercept. Rows aligned to |
coef |
Optional integer or character. See |
contrast |
Optional numeric vector or matrix. See
|
edger_params |
A list, see |
.verbose |
Boolean or integer. Controls verbosity and returns run
times. |
A data.table, see run_edger_ql(). The feature_id
column holds the gene identifiers.
Squair, et al., Nat Commun, 2021; Chen, Lun and Smyth, F1000Research, 2016
# six pseudo-bulk samples out of a demo single cell object sc <- demo_single_cells(prepped = FALSE) cells <- get_sc_obs(sc)$cell_id cell_list <- split( cells, rep(sprintf("sample_%i", 1:6), length.out = length(cells)) ) design <- stats::model.matrix(~ rep(c("ctrl", "case"), each = 3)) res <- pseudobulk_dge_sc(sc, cell_list, design, .verbose = FALSE) head(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# six pseudo-bulk samples out of a demo single cell object sc <- demo_single_cells(prepped = FALSE) cells <- get_sc_obs(sc)$cell_id cell_list <- split( cells, rep(sprintf("sample_%i", 1:6), length.out = length(cells)) ) design <- stats::model.matrix(~ rep(c("ctrl", "case"), each = 3)) res <- pseudobulk_dge_sc(sc, cell_list, design, .verbose = FALSE) head(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function will do QC on the bulk data and remove outlier samples that show substantially lower expression of genes compared to the rest of the data and remove lowly expressed genes.
qc_bulk_dge( object, group_col, outlier_threshold = 2, min_prop = 0.2, min_count = 10, .verbose = TRUE )qc_bulk_dge( object, group_col, outlier_threshold = 2, min_prop = 0.2, min_count = 10, .verbose = TRUE )
object |
The underlying class, see |
group_col |
String. The column in the metadata that will contain the contrast groups. Needs to be part of the metadata stored in the class. |
outlier_threshold |
Float. Number of standard deviations in terms of
percentage genes detected you allow before removing a sample. Defaults to |
min_prop |
Float. Minimum proportion of samples in which the gene has to be identified in. |
min_count |
Float. Minimum number of counts (cpm) to be detected in min_prop of the samples (in cohorts defined by groups_coll) |
.verbose |
Boolean. Controls the verbosity of the function. |
Returns the class with the processed_data data slot populated and
applied parameters added to the params slot.
# drop outlier samples and lowly expressed genes syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) dim(get_outputs(object)$raw_counts_filtered)# drop outlier samples and lowly expressed genes syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- qc_bulk_dge(object, group_col = "case_control", .verbose = FALSE) dim(get_outputs(object)$raw_counts_filtered)
This class can be used to generate reciprocal best hit graphs between gene modules from different origins.
RbhGraph( module_results, rbh_type = c("cor", "set"), dataset_col = NULL, module_col = NULL, value_col = NULL )RbhGraph( module_results, rbh_type = c("cor", "set"), dataset_col = NULL, module_col = NULL, value_col = NULL )
module_results |
data.table or list. If data.table (you set the class
to |
rbh_type |
String. One of |
dataset_col |
The column (name) which indicates from which data set/ method the gene module was derived. Only needed if you want to use set similarities. |
module_col |
The column (name) which stores the names of the modules. Only needed if you want to use set similarities. |
value_col |
The column (name) which stores the genes that are part of the modules. Only needed if you want to use set similarities. |
Returns the RbhGraph class for further operations.
Nested list of the modules generated by different methods.
igraph. The reciprocal best hit graph.
data.table. Contains the RBH graph data as an edge df.
A (nested) list that will store all the parameters of the applied function.
data.table. Contains final results.
# set similarity based RBH class over four gene modules set.seed(123) modules <- data.table::data.table( origin = rep(c("set_a", "set_b"), each = 20), module = rep(c("m1", "m2", "m3", "m4"), each = 10), gene = unlist(replicate(4, sample(letters, 10), simplify = FALSE)) ) object <- RbhGraph( modules, rbh_type = "set", dataset_col = "origin", module_col = "module", value_col = "gene" ) class(object)[1]# set similarity based RBH class over four gene modules set.seed(123) modules <- data.table::data.table( origin = rep(c("set_a", "set_b"), each = 20), module = rep(c("m1", "m2", "m3", "m4"), each = 10), gene = unlist(replicate(4, sample(letters, 10), simplify = FALSE)) ) object <- RbhGraph( modules, rbh_type = "set", dataset_col = "origin", module_col = "module", value_col = "gene" ) class(object)[1]
Useful for exploring the data stored in an h5ad file.
read_h5ad_metadata(f_path)read_h5ad_metadata(f_path)
f_path |
File path to the |
A list with:
obs - data.table of cell-level metadata
var - data.table of gene-level metadata
dims - named integer vector c(obs, var)
type - "CSR" or "CSC"
# obs and var tables straight out of the file data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, data$counts, data$obs, data$var, .verbose = FALSE) meta <- read_h5ad_metadata(f_path) head(meta$var, 3) unlink(f_path)# obs and var tables straight out of the file data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, data$counts, data$obs, data$var, .verbose = FALSE) meta <- read_h5ad_metadata(f_path) head(meta$var, 3) unlink(f_path)
Read summary statistics from the X slot of an h5ad file
read_h5ad_x_summary(f_path, n_sample = 10000L)read_h5ad_x_summary(f_path, n_sample = 10000L)
f_path |
File path to the |
n_sample |
Number of non-zero values to sample for the preview. NULL reads all. |
A list with:
stats - named vector: min, max, mean, median, and fraction of values that are whole numbers
is_integer_valued - logical; TRUE if >99% of sampled non-zero values are whole numbers
type - "CSR" or "CSC"
dims - named integer vector c(obs, var)
sample - numeric vector of sampled non-zero values
# check whether /X holds raw counts before loading it data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, data$counts, data$obs, data$var, .verbose = FALSE) summary_x <- read_h5ad_x_summary(f_path) summary_x$stats summary_x$is_integer_valued unlink(f_path)# check whether /X holds raw counts before loading it data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, data$counts, data$obs, data$var, .verbose = FALSE) summary_x <- read_h5ad_x_summary(f_path) summary_x$stats summary_x$is_integer_valued unlink(f_path)
This function loads in the motif2tf information that you can get from
https://resources.aertslab.org/cistarget/motif2tf/.
The function will generate a data.table that can be subsequently used.
read_motif_annotation_file(annot_file)read_motif_annotation_file(annot_file)
annot_file |
String. Path to the motif2tf file that you downloaded. |
data.table with the motif to transcription factor information.
## Not run: # motif to transcription factor table from the downloaded reference paths <- download_cistarget_hg38() annot <- read_motif_annotation_file(paths$motif_annotations) head(annot) ## End(Not run)## Not run: # motif to transcription factor table from the downloaded reference paths <- download_cistarget_hg38() annot <- read_motif_annotation_file(paths$motif_annotations) head(annot) ## End(Not run)
This function loads in the .feather files with the motif to target gene
rankings. These can be found here:
https://resources.aertslab.org/cistarget/databases/
read_motif_ranking(ranking_file)read_motif_ranking(ranking_file)
ranking_file |
String. The file path to the .feather file |
An integer matrix that has been transposed for easier use in the underlying Rust code.
## Not run: # transposed motif rankings from the downloaded feather file paths <- download_cistarget_hg38() rankings <- read_motif_ranking(paths$rankings) dim(rankings) ## End(Not run)## Not run: # transposed motif rankings from the downloaded feather file paths <- download_cistarget_hg38() rankings <- read_motif_ranking(paths$rankings) dim(rankings) ## End(Not run)
Multi-file counterpart to read_tenx_h5_adt(). Reads the same
modality from each input, stacks the cells (rows), and prefixes each
barcode with its exp_id so the result matches the cell_id convention
used by load_multi_tenx_h5(). The feature space is either
the intersection or union of features across inputs; missing features
in the union case are filled with zero.
read_multi_tenx_h5_adt( h5_paths, feature_type = "Antibody Capture", gene_universe = c("intersection", "union") )read_multi_tenx_h5_adt( h5_paths, feature_type = "Antibody Capture", gene_universe = c("intersection", "union") )
h5_paths |
Character vector of file paths to 10x h5 files. If names
are provided, these will be used as |
feature_type |
String. The feature type to return. Defaults to
|
gene_universe |
One of |
A dense matrix of cells x features with exp_id_barcode
rownames and feature names as colnames.
# stack the ADT layer of two files, barcodes prefixed by exp_id data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) adt <- generate_single_cell_test_data_adt( params_sc_synthetic_data_adt(n_cells = 200L) ) features <- data.table::data.table( id = c(data$var$gene_id, colnames(adt$counts)), name = c(data$var$ensembl_id, colnames(adt$counts)), feature_type = rep( c("Gene Expression", "Antibody Capture"), c(ncol(data$counts), ncol(adt$counts)) ) ) counts <- cbind(data$counts, as(adt$counts, "RsparseMatrix")) files <- c(a = tempfile(fileext = ".h5"), b = tempfile(fileext = ".h5")) for (f in files) { write_tenx_h5_sc(f, counts, data$obs$cell_id, features) } adt_counts <- read_multi_tenx_h5_adt(files) dim(adt_counts) head(rownames(adt_counts), 2) unlink(files)# stack the ADT layer of two files, barcodes prefixed by exp_id data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) adt <- generate_single_cell_test_data_adt( params_sc_synthetic_data_adt(n_cells = 200L) ) features <- data.table::data.table( id = c(data$var$gene_id, colnames(adt$counts)), name = c(data$var$ensembl_id, colnames(adt$counts)), feature_type = rep( c("Gene Expression", "Antibody Capture"), c(ncol(data$counts), ncol(adt$counts)) ) ) counts <- cbind(data$counts, as(adt$counts, "RsparseMatrix")) files <- c(a = tempfile(fileext = ".h5"), b = tempfile(fileext = ".h5")) for (f in files) { write_tenx_h5_sc(f, counts, data$obs$cell_id, features) } adt_counts <- read_multi_tenx_h5_adt(files) dim(adt_counts) head(rownames(adt_counts), 2) unlink(files)
Helper function to load in ADT counts from h5. Leverages Rust under the hood for faster filtering and reading.
read_tenx_h5_adt(f_path, feature_type = "Antibody Capture")read_tenx_h5_adt(f_path, feature_type = "Antibody Capture")
f_path |
String. File to the h5 file from which to read the ADT counts. |
feature_type |
String. The feature type to return. Defaults here to
|
A dense matrix of cells x features
# pull the antibody capture layer out of a multi-modal 10x file data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) adt <- generate_single_cell_test_data_adt( params_sc_synthetic_data_adt(n_cells = 200L) ) f_path <- tempfile(fileext = ".h5") write_tenx_h5_sc( f_path = f_path, counts = cbind(data$counts, as(adt$counts, "RsparseMatrix")), barcodes = data$obs$cell_id, features = data.table::data.table( id = c(data$var$gene_id, colnames(adt$counts)), name = c(data$var$ensembl_id, colnames(adt$counts)), feature_type = rep( c("Gene Expression", "Antibody Capture"), c(ncol(data$counts), ncol(adt$counts)) ) ) ) adt_counts <- read_tenx_h5_adt(f_path) dim(adt_counts) unlink(f_path)# pull the antibody capture layer out of a multi-modal 10x file data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) adt <- generate_single_cell_test_data_adt( params_sc_synthetic_data_adt(n_cells = 200L) ) f_path <- tempfile(fileext = ".h5") write_tenx_h5_sc( f_path = f_path, counts = cbind(data$counts, as(adt$counts, "RsparseMatrix")), barcodes = data$obs$cell_id, features = data.table::data.table( id = c(data$var$gene_id, colnames(adt$counts)), name = c(data$var$ensembl_id, colnames(adt$counts)), feature_type = rep( c("Gene Expression", "Antibody Capture"), c(ncol(data$counts), ncol(adt$counts)) ) ) ) adt_counts <- read_tenx_h5_adt(f_path) dim(adt_counts) unlink(f_path)
Useful for exploring the data stored in a 10x CellRanger v2/v3 h5 file, including the breakdown of feature types in a multi-modal file.
read_tenx_h5_metadata(f_path)read_tenx_h5_metadata(f_path)
f_path |
File path to the 10x |
A list with:
obs - data.table of barcodes
var - data.table of features (id, name, and feature_type for v3)
dims - named integer vector c(obs, var)
version - "v2" or "v3"
feature_types - named integer vector of feature_type counts (v3 only, otherwise NULL)
# barcodes, features and the feature type breakdown data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5") write_tenx_h5_sc( f_path = f_path, counts = data$counts, barcodes = data$obs$cell_id, features = data.table::data.table( id = data$var$gene_id, name = data$var$ensembl_id, feature_type = "Gene Expression" ) ) meta <- read_tenx_h5_metadata(f_path) meta$dims meta$feature_types unlink(f_path)# barcodes, features and the feature type breakdown data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5") write_tenx_h5_sc( f_path = f_path, counts = data$counts, barcodes = data$obs$cell_id, features = data.table::data.table( id = data$var$gene_id, name = data$var$ensembl_id, feature_type = "Gene Expression" ) ) meta <- read_tenx_h5_metadata(f_path) meta$dims meta$feature_types unlink(f_path)
Lays the finished tree out again without searching. The tree is renumbered on the Rust side, so the inferred ancestors can come back with different node numbers; the leaves, and so the cells, keep theirs.
relayout_bonsai( x, layout = c("equal_angle", "equal_daylight", "dendrogram"), hyperbolic = FALSE )relayout_bonsai( x, layout = c("equal_angle", "equal_daylight", "dendrogram"), hyperbolic = FALSE )
x |
A |
layout |
String. One of |
hyperbolic |
Boolean. Project the layout onto the hyperbolic disk. |
The BonsaiTree with the new coordinates.
# the same tree as a dendrogram sc <- demo_single_cells( prepped = FALSE, syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 50L) ) tree <- bonsai_sc(sc, .verbose = FALSE) tree <- relayout_bonsai(tree, layout = "dendrogram") plot(tree) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the same tree as a dendrogram sc <- demo_single_cells( prepped = FALSE, syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 50L) ) tree <- bonsai_sc(sc, .verbose = FALSE) tree <- relayout_bonsai(tree, layout = "dendrogram") plot(tree) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Return the ADT feature names removing the isotypes
remove_adt_isotypes(feature_names, pattern = "isotype")remove_adt_isotypes(feature_names, pattern = "isotype")
feature_names |
Character. ADT feature names (matrix colnames or
from |
pattern |
String. Case-insensitive regex. Defaults to |
Character vector of ADT features, but anything with "isotype".
# the same features with the isotype controls dropped features <- c("CD3", "CD19", "IgG1_isotype", "IgG2a_isotype") remove_adt_isotypes(features)# the same features with the isotype controls dropped features <- c("CD3", "CD19", "IgG1_isotype", "IgG2a_isotype") remove_adt_isotypes(features)
Remove the MAGIC imputed layer
remove_magic(x, ...) ## S3 method for class 'ScCache' remove_magic(x, ...) ## S7 method for class <bixverse::SingleCells> remove_magic(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> remove_magic(x, ...)remove_magic(x, ...) ## S3 method for class 'ScCache' remove_magic(x, ...) ## S7 method for class <bixverse::SingleCells> remove_magic(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> remove_magic(x, ...)
x |
An object from which to remove the imputed layer. |
... |
Other parameters. |
The object with the imputed layer dropped.
# drop the imputed layer again sc <- demo_single_cells() sc <- run_magic_sc(sc, features = get_gene_names(sc)[1:5], .verbose = FALSE) sc <- remove_magic(sc) is.null(get_magic(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# drop the imputed layer again sc <- demo_single_cells() sc <- run_magic_sc(sc, features = get_gene_names(sc)[1:5], .verbose = FALSE) sc <- remove_magic(sc) is.null(get_magic(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Remove the fitted residual model
remove_residual_fit(x, ...) ## S7 method for class <bixverse::MetaCells> remove_residual_fit(x, ...) ## S3 method for class 'ScCache' remove_residual_fit(x, ...) ## S7 method for class <bixverse::SingleCells> remove_residual_fit(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> remove_residual_fit(x, ...)remove_residual_fit(x, ...) ## S7 method for class <bixverse::MetaCells> remove_residual_fit(x, ...) ## S3 method for class 'ScCache' remove_residual_fit(x, ...) ## S7 method for class <bixverse::SingleCells> remove_residual_fit(x, ...) ## S7 method for class <bixverse::SingleCellsSubset> remove_residual_fit(x, ...)
x |
An object from which to remove the fitted model. |
... |
Other parameters. |
The object with the fitted model dropped.
# drop the fitted model again sc <- demo_single_cells() sc <- fit_residuals_sc(sc, method = "analytic_pearson", .verbose = FALSE) sc <- remove_residual_fit(sc) is.null(get_residual_fit(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# drop the fitted model again sc <- demo_single_cells() sc <- fit_residuals_sc(sc, method = "analytic_pearson", .verbose = FALSE) sc <- remove_residual_fit(sc) is.null(get_residual_fit(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function allows to remove certain samples from the object
remove_samples(object, samples_to_remove, ...)remove_samples(object, samples_to_remove, ...)
object |
The underlying object, either |
samples_to_remove |
Character vector. The sample identifiers to remove. |
... |
Additional arguments to parse to the functions. |
Returns the object with the samples removed. This will regenerated the object from the start and remove any data in it.
# drop the first two samples and rebuild the object syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table(sample_id = colnames(syn$counts)) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- remove_samples(object, c("sample_1", "sample_2")) dim(S7::prop(object, "raw_counts"))# drop the first two samples and rebuild the object syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table(sample_id = colnames(syn$counts)) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- remove_samples(object, c("sample_1", "sample_2")) dim(S7::prop(object, "raw_counts"))
For each metric in rescue_thresholds, cells whose value is within
[lower, upper] are un-flagged from MAD. Hard-threshold flags are not
cleared; a warning is issued when overlaps occur. Calling rescue_cells
overwrites any prior rescue set for the metrics named.
rescue_cells(x, rescue_thresholds)rescue_cells(x, rescue_thresholds)
x |
A |
rescue_thresholds |
Named list of numeric vectors with |
Updated CellQc.
# un-flag MAD outliers that still sit above a sane library size set.seed(42L) metrics <- list( lib_size = c(rnorm(99, 1000, 100), 50), pct_mt = runif(100, 0, 20) ) qc <- run_cell_qc( metrics, cells_to_keep = 0:99, directions = c(lib_size = "below", pct_mt = "above") ) rescue_cells(qc, list(lib_size = c(lower = 500)))# un-flag MAD outliers that still sit above a sane library size set.seed(42L) metrics <- list( lib_size = c(rnorm(99, 1000, 100), 50), pct_mt = runif(100, 0, 20) ) qc <- run_cell_qc( metrics, cells_to_keep = 0:99, directions = c(lib_size = "below", pct_mt = "above") ) rescue_cells(qc, list(lib_size = c(lower = 500)))
Restores every cell found in the binary count file and wipes the cache,
taking the object back to a pristine state. Filtering only ever flips a
to_keep flag in the DuckDB, so nothing was deleted and nothing is lost by
resetting.
Wiping the cache is not optional: a PCA or a kNN computed on a filtered
subset does not describe the full cell set, and keeping it would recreate
exactly the mismatch this guards against. With force = FALSE you are asked
to confirm before that happens.
reset_cells_to_keep(object, force = FALSE) ## S7 method for class <bixverse::MetaCells> reset_cells_to_keep(object, force = FALSE) ## S7 method for class <bixverse::SingleCells> reset_cells_to_keep(object, force = FALSE) ## S7 method for class <bixverse::SingleCellsMultiModal> reset_cells_to_keep(object, force = FALSE) ## S7 method for class <bixverse::SingleCellsSubset> reset_cells_to_keep(object, force = FALSE)reset_cells_to_keep(object, force = FALSE) ## S7 method for class <bixverse::MetaCells> reset_cells_to_keep(object, force = FALSE) ## S7 method for class <bixverse::SingleCells> reset_cells_to_keep(object, force = FALSE) ## S7 method for class <bixverse::SingleCellsMultiModal> reset_cells_to_keep(object, force = FALSE) ## S7 method for class <bixverse::SingleCellsSubset> reset_cells_to_keep(object, force = FALSE)
object |
|
force |
Boolean. Skip the confirmation prompt. Defaults to |
The object with every cell restored and an empty cache. Unchanged if the confirmation was declined.
# a filter taken back off, cache wiped with it sc <- demo_single_cells(prepped = FALSE) sc <- set_cells_to_keep(sc, get_cell_names(sc)[1:100]) sc <- reset_cells_to_keep(sc, force = TRUE) length(get_cells_to_keep(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# a filter taken back off, cache wiped with it sc <- demo_single_cells(prepped = FALSE) sc <- set_cells_to_keep(sc, get_cell_names(sc)[1:100]) sc <- reset_cells_to_keep(sc, force = TRUE) length(get_cells_to_keep(sc)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Robust scaling, i.e., removes the median and scales data based on the interquartile range (IQR). Useful if outliers are expected. NAs will be ignored.
robust_scale(x)robust_scale(x)
x |
Numeric vector. |
x, robustly scaled.
# median-centred, IQR-scaled vector set.seed(123) head(robust_scale(rnorm(10)))# median-centred, IQR-scaled vector set.seed(123) head(robust_scale(rnorm(10)))
Fits a Loess regression of
y on x. Points where either value is
non-finite are dropped from the fit.
rs_2d_loess(x, y, span, degree)rs_2d_loess(x, y, span, degree)
x |
Numeric. The x values to fit. |
y |
Numeric. The y values to fit. |
span |
Numeric. The span parameter. Needs to be in |
degree |
Integer. Either 1 (linear) or 2 (quadratic). Other values will cause an error. |
A list with the following items
predicted - The predicted values, 0 for dropped points.
residuals - The residuals for every data point, 0 for dropped
points.
valid_idx - 1-based indices of the points included in the fit.
Computes the average silhouette width on batch labels using pairwise
distances in the embedding space. Values near 0 indicate good batch
mixing, values near 1 indicate batch separation.
rs_batch_silhouette_width(embedding, batch_vector, max_cells, verbose, seed)rs_batch_silhouette_width(embedding, batch_vector, max_cells, verbose, seed)
embedding |
Numeric matrix. The embedding to assess (e.g. PCA or corrected embedding). Rows are cells, columns are dimensions. |
batch_vector |
Integer vector. The batch per cell. The codes need not be 0-based or contiguous. |
max_cells |
Integer or NULL. If not NULL, subsample to this many cells for performance. If NULL, all cells are used. |
verbose |
Boolean. Controls verbosity of the function. |
seed |
Integer. Seed for subsampling reproducibility. |
A list with the following items
per_cell - Per-cell silhouette scores
mean_asw - Mean silhouette width
median_asw - Median silhouette width
Draws random gene sets across a log-spaced grid of anchor sizes, fits gamma tails to the resulting enrichment scores and smooths the fitted parameters across sizes. Nothing about any gene set library enters here, so one calibration serves every library scored against that signature.
rs_blitzgsea_calibrate(stats, blitz_params)rs_blitzgsea_calibrate(stats, blitz_params)
stats |
Numeric vector. The gene level statistic. Needs to be sorted in descending nature. |
blitz_params |
List. The blitzGSEA parameters, see
|
List with the following elements
anchor_sizes Numeric vector. The anchor set sizes, ascending.
shape_pos Numeric vector. Smoothed positive-tail gamma shape.
scale_pos Numeric vector. Smoothed positive-tail gamma scale.
shape_neg Numeric vector. Smoothed negative-tail gamma shape.
scale_neg Numeric vector. Smoothed negative-tail gamma scale.
pos_ratio Numeric vector. Smoothed fraction of positive null scores at each anchor.
ks_pos Float. Mean goodness-of-fit p-value for the positive tail.
ks_neg Float. Mean goodness-of-fit p-value for the negative tail.
centred Boolean. Whether the signature was centred.
Each gene set costs one enrichment score plus one gamma tail evaluation. The gene sets are expected to have been filtered to the desired size bounds and intersected with the signature already.
rs_blitzgsea_score(stats, pathways, null_model, blitz_params)rs_blitzgsea_score(stats, pathways, null_model, blitz_params)
stats |
Numeric vector. The gene level statistic. Needs to be sorted in descending nature and be the same signature the null was calibrated on. |
pathways |
List. One integer vector of index positions per gene set, indexed to R's 1-indexing. Order and duplicates do not matter. |
null_model |
List. The calibrated null from
|
blitz_params |
List. The blitzGSEA parameters, see
|
List with the following elements
es Numeric vector. Enrichment scores for the gene sets.
nes Numeric vector. Normalised enrichment scores.
pvals Numeric vector. Two-sided p-values from the gamma approximation.
sidak Numeric vector. Sidak-adjusted p-values.
fdr Numeric vector. Benjamini-Hochberg adjusted p-values.
size Integer vector. Gene set size after intersection.
leading_edge List of integer vectors with the leading edge index positions, indexed to R's 1-indexing.
edgeR's
calcNormFactors, via the edge-rs crate.
rs_calc_norm_factors(counts, lib_size, norm_method)rs_calc_norm_factors(counts, lib_size, norm_method)
counts |
Integer or double matrix. Raw counts of genes x samples. |
lib_size |
Numeric vector or NULL. Library size per sample. Pass the
pre-filter column sums after filtering genes, as edgeR keeps them. NULL uses
the column sums of |
norm_method |
String. One of
|
Numeric vector of normalisation factors, one per sample.
Robinson and Oshlack, Genome Biol, 2010
Average silhouette width on cell type labels, rescaled to
[0, 1] via
(s + 1) / 2. Higher values mean cell types stay separated.
rs_cell_type_asw(embedding, labels, max_cells, verbose, seed)rs_cell_type_asw(embedding, labels, max_cells, verbose, seed)
embedding |
Numeric matrix. Cells x dimensions. |
labels |
Integer vector. The cell type per cell. The codes need not be 0-based or contiguous. |
max_cells |
Integer or NULL. If not NULL, subsample to this many cells for performance. If NULL, all cells are used. |
verbose |
Boolean. Controls verbosity of the function. |
seed |
Integer. Seed for subsampling reproducibility. |
A list with the following items
per_cell - Per-cell rescaled silhouette scores
mean_asw - Mean rescaled silhouette width
median_asw - Median rescaled silhouette width
Luecken, et al., Nat Methods, 2022
Core Rust function for motif enrichment analysis using recovery curves.
Gene sets are processed in parallel; only motifs with an NES at or above
nes_threshold are returned.
rs_cistarget( rankings, gs_list, auc_threshold, nes_threshold, max_rank, method, n_mean, verbose )rs_cistarget( rankings, gs_list, auc_threshold, nes_threshold, max_rank, method, n_mean, verbose )
rankings |
Integer matrix with motif rankings for genes (genes in rows, motifs in columns). Lower ranks indicate higher regulatory potential. |
gs_list |
List of integer vectors. Each element contains 1-based indices of genes in the gene set (matching row indices in rankings). |
auc_threshold |
Integer. Absolute number of top-ranked genes to use for the AUC calculation (e.g., for 5% of 10000 genes, use 500). |
nes_threshold |
Numeric. Normalised Enrichment Score threshold for filtering significant motifs. |
max_rank |
Integer. Maximum rank to consider for the recovery curves
(at most |
method |
String. Recovery curve calculation method, one of
|
n_mean |
Integer. Window size for the smoothing in the approximate method. |
verbose |
Boolean. Report progress per decile of gene sets. |
List of lists, one per gene set, each containing
motif_idx - 1-based column index of the motif in rankings.
nes - Normalised enrichment score.
auc - Area under the recovery curve.
rank_at_max - Rank at which the leading edge is reached.
n_enriched - Number of genes in the leading edge.
leading_edge - List of 1-based row indices of the leading edge genes, one element per motif.
Compare two kNN graphs and return the distance ratios and overlaps of
k-nearest neighbours between them.
rs_compare_knn(knn_data_a, knn_data_b)rs_compare_knn(knn_data_a, knn_data_b)
knn_data_a |
Named list. This contains the kNN data (including distances) of the first kNN graph. This one will be treated as the ground truth |
knn_data_b |
Named list. This contains the kNN data (including distances) of the second kNN graph. |
A list with the following elements:
all_matches - Integer vector. Number of shared neighbours per sample.
all_ratios - Numerical vector. Ratio of the summed distances
(b / a) per sample. Samples whose summed distance in a is ~0 are skipped,
so this can be shorter than all_matches.
final_recall - The mean recall across all samples, with a as the ground truth.
final_ratio - The mean distance ratio across the retained samples.
This function can be used to get constrained personalised
page-rank scores akin to Ruiz, et al. You can provide optionally
sink_nodes (node types that will force a reset) and/or sink_edges
(edge types that will force a reset).
rs_constrained_page_rank( node_names, node_types, from, to, weights, edge_type, personalised, sink_nodes, sink_edges )rs_constrained_page_rank( node_names, node_types, from, to, weights, edge_type, personalised, sink_nodes, sink_edges )
node_names |
String vector. Name of the graph nodes. |
node_types |
String vector. The node types. |
from |
String vector. The names of the |
to |
String vector. The names of the |
weights |
Numerical vector. The edge weights from the edge list. |
edge_type |
String vector. The edge types. |
personalised |
Numerical vector. The reset values. They must sum to 1
and be of same length of |
sink_nodes |
Optional string vector. Should these node types be seen as sinks, i.e., the reset occurs when this node is reached. |
sink_edges |
Optional string vector. Shall an automatic reset occur when this edge type is traversed. |
The personalised constrained page rank values.
Ruiz, et al., Nat Commun, 2021
This function can be used to get constrained personalised page-rank scores
akin to Ruiz, et al. You can provide optionally
sink_nodes (node types
that will force a reset) and/or sink_edges (edge types that will force a
reset). This version can take in a list of personalisation vectors and
returns a list as result.
rs_constrained_page_rank_list( personalisation_list, node_names, node_types, from, to, weights, edge_type, sink_nodes, sink_edges )rs_constrained_page_rank_list( personalisation_list, node_names, node_types, from, to, weights, edge_type, sink_nodes, sink_edges )
personalisation_list |
List. The list with the personalisation vectors. The sum must equal to 1, otherwise the function panics! |
node_names |
String vector. Name of the graph nodes. |
node_types |
String vector. The node types. |
from |
String vector. The names of the |
to |
String vector. The names of the |
weights |
Numerical vector. The edge weights from the edge list. |
edge_type |
String vector. The edge types. |
sink_nodes |
Optional string vector. Should these node types be seen as sinks, i.e., the reset occurs when this node is reached. |
sink_edges |
Optional string vector. Shall an automatic reset occur when this edge type is traversed. |
A list of the personalised (constrained) page rank values.
Ruiz, et al., Nat Commun, 2021
This function calculates the contrastive PCA given a target covariance
matrix and the background covariance matrix you wish to subtract. The alpha
parameter controls how much of the background covariance you wish to remove.
You have the options to return the feature loadings and you can specify
the number of cPCAs to return.
rs_contrastive_pca( target_covar, background_covar, target_mat, alpha, n_pcs, return_loadings )rs_contrastive_pca( target_covar, background_covar, target_mat, alpha, n_pcs, return_loadings )
target_covar |
The co-variance matrix of the target data set. |
background_covar |
The co-variance matrix of the background data set. |
target_mat |
The original values of the target matrix. Rows = samples, columns = features. |
alpha |
How much of the background co-variance should be removed. |
n_pcs |
How many contrastive PCs to return |
return_loadings |
Shall the loadings be returned from the contrastive PCA |
A list containing:
factors - The factors of the contrastive PCA, i.e. target_mat
multiplied by the loadings. Samples x n_pcs.
loadings - The loadings (top eigenvectors) of the contrastive PCA.
Features x n_pcs. Will be NULL if return_loadings = FALSE.
Calculates the pairwise correlations of the columns.
rs_cor(x, spearman)rs_cor(x, spearman)
x |
R matrix with doubles. |
spearman |
Shall the Spearman correlation be calculated instead of Pearson. |
The correlation matrix.
Calculates the correlation matrix of the columns, but returns the upper
triangle only as a flat vector.
rs_cor_upper_triangle(x, spearman, shift)rs_cor_upper_triangle(x, spearman, shift)
x |
R matrix with doubles. |
spearman |
Shall the Spearman correlation be calculated instead of Pearson. |
shift |
Boolean. If |
The upper triangle of the correlation matrix iterating through the
rows, with or without the diagonal depending on shift.
Calculates the correlation between the columns of two matrices. The number
of rows need to be the same!
rs_cor2(x, y, spearman)rs_cor2(x, y, spearman)
x |
R matrix with doubles. |
y |
R matrix with doubles. |
spearman |
Shall the Spearman correlation be calculated instead of Pearson. |
The correlation matrix of dimension ncol(x) by ncol(y).
Calculates the pairwise cosine similarity matrix of the columns.
rs_cos(x)rs_cos(x)
x |
R matrix with doubles. |
The cosine similarity matrix.
Calculates in a single matrix pass the total number of zeroes, the row
zeroes and column zeroes.
rs_count_zeroes(x)rs_count_zeroes(x)
x |
Numeric matrix. The matrix for which to calculate the total zeroes, column and row zeroes. |
A list with:
total_zeroes - Total zeroes in the matrix.
row_zeroes - Vector with number of zeroes per row.
col_zeroes - Vector with number of zeroes per column.
Calculates the correlation matrix from a co-variance matrix.
rs_cov2cor(x)rs_cov2cor(x)
x |
R matrix with doubles that is the co-variance matrix |
The correlation matrix.
Calculates the pairwise co-variance of the columns.
rs_covariance(x)rs_covariance(x)
x |
R matrix with doubles. |
The co-variance matrix.
edgeR's
cpm on a plain count matrix, via the edge-rs crate.
rs_cpm(counts, lib_size, log, prior_count)rs_cpm(counts, lib_size, log, prior_count)
counts |
Integer or double matrix. Raw counts of genes x samples. |
lib_size |
Numeric vector or NULL. Library size per sample, e.g.
|
log |
Boolean. Return log2-CPM. |
prior_count |
Numeric. Prior count added before the log. Ignored if
|
Numeric matrix of (log2-)CPM values, genes x samples.
This function generates a vector from the upper triangle of a given
symmetric matrix, iterating through the rows. You have the option to
remove the diagonal with setting
shift = TRUE.
rs_dense_to_upper_triangle(x, shift)rs_dense_to_upper_triangle(x, shift)
x |
Numeric matrix. The symmetric matrix to flatten. |
shift |
Boolean. If |
Numeric vector with the upper triangle values.
This function calculates the differential correlation based on the Fisher
method. For speed purposes, the function will only calculate the
differential correlation on the upper triangle of the two correlation
matrices.
rs_differential_cor(x_a, x_b, spearman)rs_differential_cor(x_a, x_b, spearman)
x_a |
Numeric matrix a, samples x features. |
x_b |
Numeric matrix b, samples x features. Needs the same number of
columns as |
spearman |
Boolean. Shall the Spearman correlation be calculated instead of Pearson. |
A list containing, one entry per upper-triangle feature pair:
r_a - The correlation coefficients in the upper triangle of matrix a.
r_b - The correlation coefficients in the upper triangle of matrix b.
z_score - The z-scores of the difference in correlation coefficients.
p_val - The z-scores transformed to two-sided p-values.
This function allows to calculate pairwise between all columns the specified
distance metric.
rs_dist(x, distance_type)rs_dist(x, distance_type)
x |
Numerical matrix. The matrix for which to calculate the pairwise column distances. |
distance_type |
String. One of
|
The calculated distance matrix.
Runs optional
filterByExpr -> calcNormFactors -> glmQLFit ->
glmQLFTest, implemented in Rust via the edge-rs crate and gated against
edgeR 4.8.2. legacy = TRUE adds estimateDisp before the fit. The tested
axis does not have to be genes: Milo's neighbourhood counts are tested with
the same call, with filter = FALSE.
rs_edger_ql(counts, design, edger_params)rs_edger_ql(counts, design, edger_params)
counts |
Numeric matrix. Raw counts of features x samples. Must not be normalised or log-transformed. |
design |
Numeric matrix. The design matrix of samples x coefficients, including the intercept. Needs at least two columns, since the null model has to retain one. |
edger_params |
Named list. The edgeR parameters, see
|
A list with the following elements, all but features_to_keep
with one entry per kept feature
features_to_keep - Boolean. Which features survived the filters.
Spans the full feature axis of counts.
log_fc - Log2 fold changes of the tested coefficient or contrast.
log_cpm - Average log2 counts per million.
f_stat - The quasi-likelihood F statistic.
p_values - Raw p-values.
fdr - Benjamini-Hochberg adjusted p-values.
Chen, Lun and Smyth, F1000Research, 2016
This function calculates rapidly AUCs based on an approximation: the
fraction of
iters random (positive, negative) pairs, drawn with
replacement, where the positive score is larger.
rs_fast_auc(pos_scores, neg_scores, iters, seed)rs_fast_auc(pos_scores, neg_scores, iters, seed)
pos_scores |
The scores of your hits. |
neg_scores |
The scores of your non-hits. |
iters |
Number of iterations to run the function for. Recommended size: 10000L. |
seed |
Seed. |
The approximate AUC.
This function serves as a wrapper over the fast ICA implementations in Rust.
It assumes a whitened matrix and also an initialised w_init.
rs_fast_ica(whiten, w_init, ica_type, ica_params)rs_fast_ica(whiten, w_init, ica_type, ica_params)
whiten |
Numerical matrix. The whitened matrix. |
w_init |
Numerical matrix. The initial unmixing matrix. ncols need to be equal to nrows of whiten. |
ica_type |
String. One of |
ica_params |
A list containing:
If the list is empty or the expected elements are not found, default values are used. |
A list with the following items:
mixing - The mixing matrix for subsequent usage.
converged - Boolean. Did the best tolerance reached fall below
max_tol.
Rust implementation that will be faster if you have a terrifying amount of
p-values to adjust.
rs_fdr_adjustment(pvals)rs_fdr_adjustment(pvals)
pvals |
Numeric vector. The p-values you wish to adjust. |
The Benjamini-Hochberg adjusted p-values.
edgeR's
filterByExpr, via the edge-rs crate.
rs_filter_by_expr( counts, group, lib_size, min_count, min_total_count, min_prop )rs_filter_by_expr( counts, group, lib_size, min_count, min_total_count, min_prop )
counts |
Integer or double matrix. Raw counts of genes x samples. |
group |
Integer vector or NULL. 1-based group code per sample (e.g.
|
lib_size |
Numeric vector or NULL. Library size per sample. NULL uses the column sums. |
min_count |
Numeric. Minimum count in the median-sized library. |
min_total_count |
Numeric. Minimum total count across all samples. |
min_prop |
Numeric. Fraction of the smallest group size beyond
edgeR's |
Boolean vector, one per gene. TRUE if the gene is kept.
Chen, Lun and Smyth, F1000Research, 2016
Selects the cells belonging to each branch from the fate probabilities, then
fits a landmark Gaussian process with a Matern-5/2 kernel per branch. This
is the Mellon-based estimator of the reference, not the legacy GAM.
Whatever expression matrix is handed over is what gets fitted. Nothing here imputes; that decision belongs to the caller.
The defaults are prior-dominated. Palantir's pseudotime is min-max scaled to
[0, 1], so a length_scale of 1.0 spans the whole domain and a sigma
of 1.0 sits at roughly the signal scale of log-normalised expression. That
resolves almost any gene into a smooth monotone or single-peaked curve.
Shorten length_scale before believing a bump.
rs_gene_trends( expression, pseudotime, branch_probs, branch_params, gene_trend_params )rs_gene_trends( expression, pseudotime, branch_probs, branch_params, gene_trend_params )
expression |
Numerical matrix of cells x genes. All cells, in the same
row order as |
pseudotime |
Numerical vector. Pseudotime per cell. |
branch_probs |
Numerical matrix of cells x fates with the fate probabilities. Rows need not sum to one. |
branch_params |
List. Parameter list, see
|
gene_trend_params |
List. Parameter list, see
|
A list with
trends - List of numerical matrices, one per branch, each of resolution x genes.
grids - List of numerical vectors with the pseudotime grid per branch, running from the branch minimum to its maximum.
branch_cells - List of integer vectors with the cell indices (0-indexed!) selected for each branch.
n_cells - Integer vector with the cell count per branch.
jitter_used - Numerical vector with the jitter each branch's Cholesky needed.
Setty, et al., Nat. Biotechnol., 2019.
Helper function to generate the ligand to target influence matrix for the
NicheNet like approach.
rs_generate_ligand_target_influence( ligand_seeds, ppi_network, grn_network, n_nodes, params )rs_generate_ligand_target_influence( ligand_seeds, ppi_network, grn_network, n_nodes, params )
ligand_seeds |
List of integer vectors. The 0-indexed seed node(s) per ligand or ligand combination. |
ppi_network |
Named list. Contains the PPI network with the ligand
to receptor to signalling to TFs. Must contain |
grn_network |
Named list. Contains the gene regulatory network with the
TF to target gene network. Must contain |
n_nodes |
Integer. Number of total nodes. |
params |
Named list. The ligand-target diffusion parameters. |
A dense matrix of ligands x n_nodes, rows in ligand_seeds
order, with the ligand to target influence scores.
rs_geom_elim_fgsea_simple( stats, levels, go_obj, gsea_params, elim_threshold, iters, seed )rs_geom_elim_fgsea_simple( stats, levels, go_obj, gsea_params, elim_threshold, iters, seed )
stats |
Named numerical vector. Needs to be sorted. The gene level statistics. |
levels |
A character vector representing the levels to iterate through. The order will be the one the iterations are happening in. |
go_obj |
The |
gsea_params |
List. The GSEA parameters, see
|
elim_threshold |
p-value below which the elimination procedure shall be applied to the ancestors. |
iters |
Integer. Number of random permutations for the fgsea simple method to use |
seed |
Integer. For reproducibility purposes. |
List with the following elements
go_id The name of the tested gene ontology identifier.
es The enrichment scores for the pathway
nes The normalised enrichment scores for the pathway. Can be
NA.
size The pathway sizes (after elimination!).
pvals The p-values for this pathway based on permutation testing
n_more_extreme Number of times the enrichment score was bigger or smaller than the permutation (pending sign).
le_zero Number of times the permutation was less than zero.
ge_zero Number of times the permutation was greater than zero.
leading_edge A list of the 1-based index positions (in stats)
of the leading edge genes for this given GO term.
Calculates the pairwise Gower distance between the rows. Continuous columns
contribute the range-normalised absolute difference, categorical columns a
simple mismatch.
rs_gower_dist(x, is_cat)rs_gower_dist(x, is_cat)
x |
Numerical matrix. Converted matrix of continuous and categorical variables as numerical values. Rows = samples, columns = features. |
is_cat |
Logical vector of length |
The Gower distance matrix between the rows, values in [0, 1].
For each cell type, the fraction of its cells in the largest connected
component of the kNN graph restricted to that cell type. 1 means every
cell type forms one connected piece. Edge direction is ignored.
rs_graph_connectivity(knn_mat, labels)rs_graph_connectivity(knn_mat, labels)
knn_mat |
Integer matrix. The rows represent the cells and the columns the neighbour indices (0-indexed!). |
labels |
Integer vector. The cell type per cell. The codes need not be 0-based or contiguous. |
A list with the following items
per_label - Connectivity per cell type, in order of first
appearance in labels.
mean - Mean connectivity.
median - Median connectivity.
Luecken, et al., Nat Methods, 2022
This function implements a Rust version of the gene ontology enrichment with
elimination: the starting point are the leaves of the ontology and
hypergeometric tests will first be conducted there. Should the
hypergeometric test p-value be below a certain threshold, the genes of that
gene ontology term will be removed from all ancestors.
rs_gse_geom_elim( target_genes, levels, go_obj, gene_universe_length, min_genes, elim_threshold, min_overlap, fdr_threshold )rs_gse_geom_elim( target_genes, levels, go_obj, gene_universe_length, min_genes, elim_threshold, min_overlap, fdr_threshold )
target_genes |
A character vector representing the target gene set. |
levels |
A character vector representing the levels to iterate through. The order will be the one the iterations are happening in. |
go_obj |
The |
gene_universe_length |
The length of the gene universe. |
min_genes |
number of minimum genes for the gene ontology term to be tested. |
elim_threshold |
p-value below which the elimination procedure shall be applied to the ancestors. |
min_overlap |
Optional minimum overlap threshold. |
fdr_threshold |
Optional fdr threshold. |
A list containing (for the terms passing the thresholds):
go_ids - The gene ontology identifier.
pvals - The calculated p-values.
fdr - The calculated FDRs.
odds_ratios - The calculated odds ratios.
hits - The size of the overlap.
gene_set_lengths - The length of the gene sets.
This function implements a Rust version of the gene ontology enrichment with
elimination: the starting point are the leaves of the ontology and
hypergeometric tests will first be conducted there. Should the
hypergeometric test p-value be below a certain threshold, the genes of that
gene ontology term will be removed from all ancestors. This function is
designed to
leverage Rust-based threading for parallel processing of a list of target
genes.
rs_gse_geom_elim_list( target_genes_list, levels, go_obj, gene_universe_length, min_genes, elim_threshold, min_overlap, fdr_threshold )rs_gse_geom_elim_list( target_genes_list, levels, go_obj, gene_universe_length, min_genes, elim_threshold, min_overlap, fdr_threshold )
target_genes_list |
A list of target genes against which to run the method. |
levels |
A character vector representing the levels to iterate through. The order will be the one the iterations are happening in. |
go_obj |
The |
gene_universe_length |
The length of the gene universe. |
min_genes |
number of minimum genes for the gene ontology term to be tested. |
elim_threshold |
p-value below which the elimination procedure shall be applied to the ancestors. |
min_overlap |
Optional minimum overlap threshold. |
fdr_threshold |
Optional fdr threshold. |
A list containing (results of all target sets concatenated):
go_ids - The gene ontology identifier.
pvals - The calculated p-values.
fdr - The calculated FDRs.
odds_ratios - The calculated odds ratios.
hits - The size of the overlap.
gene_set_lengths - The length of the gene sets.
no_test - The number of tests for that target set that passed the thresholds.
Rust-based implementation of the popular GSVA algorithm. Has further
performance optimisations compared to the original implementation.
rs_gsva(exp, gs_list, tau, kernel, max_diff, abs_rank, timings)rs_gsva(exp, gs_list, tau, kernel, max_diff, abs_rank, timings)
exp |
Numerical matrix. The expression matrix with rows = genes, and columns = samples |
gs_list |
List. A list containing the 0-based indices of the pathway
genes. See |
tau |
Float. Tau parameter, usual recommendation is to use |
kernel |
String. One of |
max_diff |
Boolean. Scoring mode: |
abs_rank |
Boolean. If |
timings |
Boolean. Prints timings from the algorithm. |
Returns a matrix of gene set ES scores x samples.
Loads in h5ad data within Rust and automatically converts the data into
CSR with cells x genes.
rs_h5ad_data(f_path, cs_type, nrows, ncols, cell_quality, slot, verbose)rs_h5ad_data(f_path, cs_type, nrows, ncols, cell_quality, slot, verbose)
f_path |
File path. The path to the h5ad file. |
cs_type |
String. One of |
nrows |
Integer. Number of rows in the file. |
ncols |
Integer. Number of columns in the file. |
cell_quality |
List. Specifying the cell quality. Please refer
to |
slot |
String. In which slot the raw data can be found. One of
|
verbose |
Boolean. Controls verbosity of the function |
A list with the CSR data (cells x genes) of the cells and genes
passing cell_quality:
data - The counts of the sparse matrix.
indices - The 0-based gene indices of the sparse matrix.
indptr - The index pointers of the sparse matrix.
no_genes - No of genes in the sparse matrix (i.e., ncol).
no_cells - No of cells in the sparse matrix (i.e., nrow).
Calculates the pairwise Hamming distance between the columns, i.e. the
fraction of rows in which two columns differ.
rs_hamming_dist(x)rs_hamming_dist(x)
x |
Integer matrix. The integers represent the factor data. |
The Hamming distance matrix between the columns, values in
[0, 1].
This function implements the Harmony algorithm from Korsunsky et al., 2019.
rs_harmony(pca, harmony_params, batch_labels, seed, verbose)rs_harmony(pca, harmony_params, batch_labels, seed, verbose)
pca |
Numerical matrix, i.e., the PCA matrix you want to correct. |
harmony_params |
List. The parameters for the Harmony algorithm. |
batch_labels |
List. Each element needs to be a 0-indexed integer vector, one per batch variable you wish to regress out. |
seed |
Integer. Seed for reproducibility purposes. |
verbose |
Integer. |
Numerical matrix, cells x dimensions, with the batch-corrected Harmony embedding.
This function implements the version 2 Harmony algorithm from Patikas, et
al., 2026.
rs_harmony_v2(pca, harmony_params, batch_labels, seed, verbose)rs_harmony_v2(pca, harmony_params, batch_labels, seed, verbose)
pca |
Numerical matrix, i.e., the PCA matrix you want to correct. |
harmony_params |
List. The parameters for the Harmony (v2) algorithm. |
batch_labels |
List. Each element needs to be a 0-indexed integer vector, one per batch variable you wish to regress out. |
seed |
Integer. Seed for reproducibility purposes. |
verbose |
Integer. |
Numerical matrix, cells x dimensions, with the batch-corrected Harmony (v2) embedding.
Calculates the Hedge's G effect for two sets of matrices. The function
assumes that rows = samples and columns = features.
rs_hedges_g(mat_a, mat_b, small_sample_correction)rs_hedges_g(mat_a, mat_b, small_sample_correction)
mat_a |
The matrix of samples and features in grp A for which to calculate the Hedge's G effect. |
mat_b |
The matrix of samples and features in grp B for which to calculate the Hedge's G effect. |
small_sample_correction |
Shall the small sample correction be applied. |
A list with the following items:
effect_sizes - Hedge's G effect size per feature.
standard_errors - Standard error of the effect size per feature.
Given a set of target genes, this is a Rust implementation of an
hypergeometric test testing for overenrichment of the target genes in the
gene sets.
rs_hypergeom_test( target_genes, gene_sets, gene_universe, min_overlap, fdr_threshold )rs_hypergeom_test( target_genes, gene_sets, gene_universe, min_overlap, fdr_threshold )
target_genes |
String vector. Represents the target gene set. |
gene_sets |
List. Contains the strings that represent the gene sets to test against. |
gene_universe |
String vector. The features representing the gene universe from which the target genes and gene sets are sampled from. |
min_overlap |
Optional integer. Shall a filter be applied on the minimum of overlapping genes. |
fdr_threshold |
Optional float. Shall a filter be applied for the maximum tolerated FDR. |
A list containing (only for the gene sets passing the filters):
pvals - The p-values from the hypergeometric test
odds_ratios - The calculated odds ratios
hits - The size of the overlap
gene_set_lengths - The length of the gene sets.
fdr - The FDR calculated across all gene sets (before filtering).
to_keep - 1-based indices of the gene sets that passed the (optional) thresholds.
Given a list of target gene sets, this function will test for each of the
individual target gene sets the hypergeometric enrichment against the
specified gene sets.
rs_hypergeom_test_list( target_genes_list, gene_sets, gene_universe, min_overlap, fdr_threshold )rs_hypergeom_test_list( target_genes_list, gene_sets, gene_universe, min_overlap, fdr_threshold )
target_genes_list |
A list of character vectors, each representing a target gene set. |
gene_sets |
A list of strings that represent the gene sets to test against. |
gene_universe |
A character vector representing the gene universe from which the target genes and gene sets are sampled from. |
min_overlap |
Optional integer. Shall a filter be applied on the minimum of overlapping genes. |
fdr_threshold |
Optional float. Shall a filter be applied for the maximum tolerated FDR. |
A list containing (results of all target sets concatenated, only for the tests passing the filters):
pvals - The p-values from the hypergeometric test.
fdr - The FDRs for each target set calculated across all gene sets.
odds_ratios - The calculated odds ratios
hits - The size of the overlap between the target gene set and individual gene sets.
gene_set_lengths - The length of the gene sets.
to_keep - 1-based indices (into gene_sets) of the tests that
passed.
tests_passed - How many tests passed the filter criteria for that target set.
This function implements a stabilised ICA like algorithm in Rust. Briefly,
it generates random w_init matrices (total number being no_random_init) and
runs ICA given the x_processed and k data over these. The function returns
combined S from the different runs and a boolean vector indicating if this
specific run converged.
rs_ica_iters(x1, k, no_comp, no_random_init, ica_type, random_seed, ica_params)rs_ica_iters(x1, k, no_comp, no_random_init, ica_type, random_seed, ica_params)
x1 |
Numerical matrix. The processed matrix (but not yet whitened!) |
k |
Numerical matrix. The whitening matrix. Needs at least |
no_comp |
Integer. Number of independent components to return. |
no_random_init |
Integer. Number of random initialisations to test. |
ica_type |
String. One of |
random_seed |
Integer. Seed for randomisations. Run |
ica_params |
A list containing:
If the list is empty or the expected elements are not found, default values are used. |
A list containing:
s_combined - The combined matrices for S. Dimensions are nrows = features; and ncols = ncomp * no_random_init.
converged - Boolean vector indicating if the respective run reached convergence. Length = no_random_init
This function will split the data into
no_folds and apply ICA with
no_random_init random initialisations over each fold.
rs_ica_iters_cv( x, no_comp, no_folds, no_random_init, ica_type, random_seed, ica_params )rs_ica_iters_cv( x, no_comp, no_folds, no_random_init, ica_type, random_seed, ica_params )
x |
Numeric matrix. The processed data (no whitening function has been applied yet.) |
no_comp |
Integer. Number of components to test for. |
no_folds |
Integer. Number of folds to use for the cross-validation. |
no_random_init |
Integer. Number of random initialisations per fold. |
ica_type |
String. One of |
random_seed |
Integer. For reproducibility. |
ica_params |
A list containing:
If the list is empty or the expected elements are not found, default values are used. |
A list containing:
s_combined - The combined matrices for S. Dimensions are nrows = features; and ncols = no_comp * no_random_init * no_folds.
converged - Boolean vector indicating if the respective run reached convergence. Length = no_random_init * no_folds
Helper function to quickly calculate the Jaccard similarity between
matching rows of the two matrices. Each row is treated as a set of
integers (duplicates removed).
rs_jaccard_row_integers(data_1, data_2)rs_jaccard_row_integers(data_1, data_2)
data_1 |
Integer matrix. The first matrix to compare. |
data_2 |
Integer matrix. The second matrix to compare. Needs the same
number of rows as |
The Jaccard similarity averaged over the rows.
The function takes in a kNN matrix and a batch vector indicating which
cell belongs to which batch. For the neighbourhood of each cell, a
chi-square test checks whether the batch proportions differ from the
overall batch proportions (with Yates' correction for two batches). Good
mixing means few cells with significant differences; bad mixing means
many.
rs_kbet(knn_mat, batch_vector, verbose)rs_kbet(knn_mat, batch_vector, verbose)
knn_mat |
Integer matrix. The rows represent the cells and the columns the neighbour indices (0-indexed!). |
batch_vector |
Integer vector. The batch per cell. The codes need not be 0-based or contiguous. |
verbose |
Boolean. Controls verbosity of the function. |
A list with the following items
pval - Per-cell p-values from the chi-square test.
chi_square_stats - Per-cell chi-square statistics.
mean_chi_square - The mean chi-square value.
median_chi_square - The median chi-square value.
The function is a helper function to do kNN label propagation. This can
be useful for semi-supervised tasks. It implements the label spreading
method.
rs_knn_label_propagation( from, to, one_hot_encoding, label_mask, weights, label_prop_params )rs_knn_label_propagation( from, to, one_hot_encoding, label_mask, weights, label_prop_params )
from |
Integer vector. 1-based source node indices for each edge. |
to |
Integer vector. 1-based target node indices for each edge. Must be
the same length as |
one_hot_encoding |
Integer matrix. Each row represents a sample, the columns the one-hot encodings. Everything 0 denotes the unlabelled data. |
label_mask |
Boolean vector. Which of the samples do not have a label.
Needs to be same length as |
weights |
Optional numeric vector. Edge weights for each pair in
|
label_prop_params |
List. Named list of parameters with the following optional fields (defaults in parentheses):
|
The matrix with the probabilities of being of a certain class.
Same shape as one_hot_encoding.
Helper function to leverage Rust to transform a kNN matrix into a flat
edge list.
rs_knn_mat_to_edge_list(knn_mat, one_index)rs_knn_mat_to_edge_list(knn_mat, one_index)
knn_mat |
Integer matrix. Rows represent the samples and the columns the 0-based indices of the k-nearest neighbours. |
one_index |
Boolean. Shall 1-based indices be returned. |
A flat vector representing the edge list, alternating from and
to, i.e. c(from_1, to_1, from_2, to_2, ...).
Modularity-based Leiden (or Louvain, with
refine = FALSE) on an
undirected weighted graph. Mirrors
igraph::cluster_leiden(objective_function = "modularity"). The result
depends on the seed only, not on the number of threads.
rs_leiden_clustering(from, to, weights, n_nodes, leiden_params)rs_leiden_clustering(from, to, weights, n_nodes, leiden_params)
from |
Integer vector. 1-based source node of each edge. |
to |
Integer vector. 1-based target node of each edge. Same length as
|
weights |
Numeric vector. Edge weights, same length as |
n_nodes |
Integer. Number of nodes in the graph. |
leiden_params |
List. Named list with the optional fields (defaults in parentheses):
|
Integer vector with the 1-based community of each node; 1 is the
largest community.
rs_ligand_activity_scores(ligand_influence, in_gene_sets)rs_ligand_activity_scores(ligand_influence, in_gene_sets)
ligand_influence |
A ligand x background genes matrix that measures the ligand to target gene influence. |
in_gene_sets |
List of logical vectors, one per gene set, each of
length |
A list with one element per gene set, each a list of per-ligand
vectors (NaN where the metric is undefined):
auroc - The Area Under the Receiver Operating Characteristic.
aupr - The Area Under the Precision-Recall curve.
aupr_corrected - The corrected AUPR.
pearson - The Pearson correlations.
spearman - The Spearman correlations.
Runs optional
filterByExpr -> calcNormFactors -> voomLmFit (or
limma-trend) -> contrasts.fit -> eBayes -> topTable for one
coefficient or contrast, implemented in Rust via the edge-rs crate and
gated against limma 3.66.0.
rs_limma_voom(counts, design, lib_size, limma_params)rs_limma_voom(counts, design, lib_size, limma_params)
counts |
Integer or double matrix. Raw counts of genes x samples. Must not be normalised or log-transformed. |
design |
Numeric matrix. The design matrix of samples x coefficients, including the intercept. Must be full rank. |
lib_size |
Numeric vector or NULL. Library size per sample. NULL uses
the column sums of |
limma_params |
Named list. The limma parameters, see
|
A list with the following elements, all but features_to_keep
with one entry per kept gene, in input order
features_to_keep - Boolean. Which genes survived the filters. Spans
the full gene axis of counts.
log_fc - Log2 fold changes of the tested coefficient or contrast.
ci_lower - Lower end of the 95% confidence interval on log_fc.
ci_upper - Upper end of the 95% confidence interval on log_fc.
ave_expr - Average log2 counts per million.
t_stat - Moderated t statistic.
p_values - Raw p-values.
fdr - Benjamini-Hochberg adjusted p-values.
b_stat - Log-odds of differential expression.
Law, et al., Genome Biol, 2014; Smyth, Stat Appl Genet Mol Biol, 2004
Computes the Local Inverse Simpson's Index on the kNN graph: the effective
number of labels in each cell's neighbourhood. On batch labels this is
iLISI (higher is better mixing), on cell type labels cLISI (lower is better
separation). Both come back rescaled to
[0, 1], higher is better, as in
scIB.
rs_lisi(knn_mat, knn_dist, labels, perplexity, verbose)rs_lisi(knn_mat, knn_dist, labels, perplexity, verbose)
knn_mat |
Integer matrix. The rows represent the cells and the columns the neighbour indices (0-indexed!). |
knn_dist |
Numeric matrix or NULL. The kNN distances, same shape as
|
labels |
Integer vector. The label (batch or cell type) per cell. The codes need not be 0-based or contiguous. |
perplexity |
Numeric or NULL. Perplexity for the weighted version.
NULL defaults to 30; values above k are clamped to k. Ignored if
|
verbose |
Boolean. Controls verbosity of the function. |
A list with the following items
per_cell - Per-cell LISI scores
mean_lisi - Mean LISI
median_lisi - Median LISI
n_labels - Number of distinct labels
ilisi_norm - Median LISI rescaled as iLISI, (median - 1) / (n - 1)
clisi_norm - Median LISI rescaled as cLISI, (n - median) / (n - 1)
Korsunsky, et al., Nat Methods, 2019; Luecken, et al., Nat Methods, 2022
rs_mad_outlier(x, threshold, direction)rs_mad_outlier(x, threshold, direction)
x |
Numerical vector to test. |
threshold |
Numeric. Number of (unscaled) MADs from the median in either direction that is acceptable. |
direction |
String. One of |
Should you provide an empty vector, the function will return an empty boolean and a threshold of 0.
A list with the following items:
outlier - Boolean vector if element is an outlier
threshold - Applied margin, i.e. threshold * MAD.
The function will take in a list of gene set indices (0-indexed!) and
calculate an AUCell type statistic. Three options here: the recovery-curve
AUC of Aibar, et al. (the actual AUCell statistic), an AUC derived from the
Mann-Whitney statistic, or average precision. This version works on
MetaCell counts which are stored in memory directly. Genes are ranked
within each meta cell on the second layer, an
f32 cast of the supplied
counts.
rs_mc_aucell(sparse_data, gs_list, aucell_params, verbose)rs_mc_aucell(sparse_data, gs_list, aucell_params, verbose)
sparse_data |
A named list that needs to have |
gs_list |
List. List with the gene set indices (0-indexed!) of the genes of interest. |
aucell_params |
List. The AUCell parameters, see
|
verbose |
Integer. |
A matrix of meta cells x gene sets with the values representing the AUC.
The meta cell entry point into DIALOGUE, see
rs_dialogue_sc() for what the
method does. This is a shim rather than a second implementation: everything
DIALOGUE asks of the expression matrix is per-gene, so the in-memory matrix
is wrapped as a gene-major reader and the same core runs.
Only the normalised layer is ever read, so sparse_data has to carry the
normalised counts. The data layer is cast to integers on the way in and
then goes unused.
Meta cells are already aggregates, so the sample a meta cell belongs to has to be unambiguous: build them within samples, not across them. The random intercept in stage two is over samples, and a meta cell straddling two of them has no well-defined level.
rs_mc_dialogue( sparse_data, cell_type_indices, features, sample_ids, cell_quality, gene_indices, dialogue_params, verbose )rs_mc_dialogue( sparse_data, cell_type_indices, features, sample_ids, cell_quality, gene_indices, dialogue_params, verbose )
sparse_data |
A named list that needs to have |
cell_type_indices |
List of integer vectors. 0-indexed(!) positions of the meta cells belonging to each cell type. At least two cell types are needed. |
features |
List of numeric matrices, one per cell type, shaped
|
sample_ids |
Integer vector. 0-indexed(!) sample code per meta cell, over all meta cells rather than per cell type. |
cell_quality |
Numeric vector. Quality covariate per meta cell, indexed
the same way as |
gene_indices |
Integer vector. 0-indexed(!) positions of the genes to consider when building signatures. |
dialogue_params |
Named list. Contains the DIALOGUE parameters across
all three stages, see |
verbose |
Integer. |
A list, identical in shape to rs_dialogue_sc().
Jerby-Arnon & Regev, Nature Biotechnology, 2022
This function implements the HotSpot auto-correlation functionality and
will return to what extent a given gene shows auto-correlation in the
kNN-graph over the meta cells. For details see DeTomaso, et al. This version
works on MetaCell counts which are stored in memory directly. There is no
streaming variant: streaming bounds disk re-reads, which is not a problem
an in-memory matrix has.
rs_mc_hotspot_autocor( sparse_data, embd, knn_data, hotspot_params, cells_to_keep, genes_to_use, verbose, seed )rs_mc_hotspot_autocor( sparse_data, embd, knn_data, hotspot_params, cells_to_keep, genes_to_use, verbose, seed )
sparse_data |
A named list that needs to have |
embd |
Numerical matrix. The embedding matrix from which to generate
the kNN graph. Needs one row per entry of |
knn_data |
Optional list. This contains pre-computed kNN data
( |
hotspot_params |
List. The HotSpot parameter list. The kNN parameters
are only read when no |
cells_to_keep |
Integer vector. 0-index vector indicating which meta cells to include in the analysis. Ensure that this is of same order/length as the embedding matrix. |
genes_to_use |
Integer vector. 0-index vector indicating which genes to include. |
verbose |
Integer. |
seed |
Integer. Random seed for reproducibility. |
A list with the following elements.
gene_idx - 0-based integer indicating the gene index.
gaerys_c - Geary's C calculation for the autocorrelation coefficient.
z_score - Z-score of the auto-correlation.
pval - P-value derived from the Z-score.
fdr - False discovery rate based on the p-value.
DeTomaso, et al., Cell Systems, 2021
This function implements the HotSpot gene <> gene local correlation
functionality from HotSpot, see DeTomaso, et al. This version works on
MetaCell counts which are stored in memory directly.
Three dense metacells x genes blocks are live at once, so keep
genes_to_use to the panel actually of interest rather than the whole
transcriptome.
rs_mc_hotspot_gene_cor( sparse_data, embd, knn_data, hotspot_params, cells_to_keep, genes_to_use, verbose, seed )rs_mc_hotspot_gene_cor( sparse_data, embd, knn_data, hotspot_params, cells_to_keep, genes_to_use, verbose, seed )
sparse_data |
A named list that needs to have |
embd |
Numerical matrix. The embedding matrix from which to generate
the kNN graph. Needs one row per entry of |
knn_data |
Optional list. This contains pre-computed kNN data
( |
hotspot_params |
List. The HotSpot parameter list. The kNN parameters
are only read when no |
cells_to_keep |
Integer vector. 0-index vector indicating which meta cells to include in the analysis. Ensure that this is of same order/length as the embedding matrix. |
genes_to_use |
Integer vector. 0-index vector indicating which genes to include. |
verbose |
Integer. |
seed |
Integer. Random seed for reproducibility. |
A list with the following elements.
cor - The genes x genes local correlation matrix, in the order of
genes_to_use.
z - The Z-scores of these local correlations, same shape.
DeTomaso, et al., Cell Systems, 2021
Calculates highly variable genes for MetaCells or more
generally speaking sparse data. This is happening in-memory compared to the
(usually much) larger single cell data sets.
"meanvarbin" and
"dispersion" compute the same statistics; they differ only in how the R
side selects from them.
rs_mc_hvg( sparse_data, hvg_method, loess_span, binning, n_bins, clip_max, scran_params, verbose )rs_mc_hvg( sparse_data, hvg_method, loess_span, binning, n_bins, clip_max, scran_params, verbose )
sparse_data |
A named list that needs to have |
hvg_method |
String. Which HVG detection method to use. Options
are |
loess_span |
Numeric. The span parameter for the loess function
( |
binning |
String. The binning strategy for the |
n_bins |
Integer. Number of bins for the |
clip_max |
Optional clipping number. Defaults to |
scran_params |
List. Trend parameters for |
verbose |
Integer. |
A list with the HVG statistics. If hvg_method == "vst":
mean - The average expression of the gene.
var - The variance of the gene.
var_exp - The expected variance of the gene.
var_std - The standardised variance of the gene.
If hvg_method == "scran", all on the log2 scale:
scran_mean - The mean log-expression of the gene.
scran_var - The variance of the log-expression of the gene.
scran_fitted - The variance the trend expects at the gene's mean.
scran_residual - scran_var - scran_fitted. Genes are ranked on
this.
For "meanvarbin" and "dispersion":
mean - The average expression of the gene.
dispersion - The dispersion of the gene.
dispersion_scaled - The scaled dispersion per bin per gene.
bin - The bin of the gene.
Calculates PCA for MetaCells or more generally speaking sparse
data. This is happening in-memory compared to the (usually much) larger
single cell data sets. The matrix is densified, optionally CLR transformed
and scaled according to
pca_params before the SVD.
rs_mc_pca(sparse_data, no_pcs, pca_params, clr_offsets, seed, verbose)rs_mc_pca(sparse_data, no_pcs, pca_params, clr_offsets, seed, verbose)
sparse_data |
A named list that needs to have |
no_pcs |
Integer. Number of PCs to return. |
pca_params |
Named list. Contains the parameters to use for this PCA
run, see |
clr_offsets |
Optional numeric. One offset per meta cell for the
|
seed |
Integer. Random seed for the randomised SVD. |
verbose |
Integer. |
A list with the following items
scores - The samples projected on the PCA space (solved via sparse SVD).
loadings - The loadings of the features for the PCA (solved via sparse SVD).
singular_values - The singular values for the PCA (solved via sparse SVD).
Booeshaghi, et al., bioRxive, 2026.
Assumes that the sparse data is pre-filtered for the cells and genes you
wish to include: every column is a target gene. The regressors read the
second layer, which here is an
f32 cast of the supplied counts.
rs_mc_scenic(sparse_data, tf_indices, scenic_params, seed, verbose)rs_mc_scenic(sparse_data, tf_indices, scenic_params, seed, verbose)
sparse_data |
A named list that needs to have |
tf_indices |
Integer vector. 0-indexed(!) column positions of the
transcription factors within |
scenic_params |
Named list. Contains all of the parameters needed for
SCENIC, see |
seed |
Integer. Controls reproducibility of the function. |
verbose |
Integer. |
A genes x TFs importance matrix, rows in column order of
sparse_data, columns in the order of tf_indices.
The function will take in a list of gene sets that contains lists of
"pos"
and "neg" gene indices (0-indexed). You don't have to provide the "neg",
but it can be useful to classify the delta of two stats (EMT, Th1; Th2) etc.
This version works on MetaCell counts which are stored in memory directly.
rs_mc_vision(sparse_data, gs_list, verbose)rs_mc_vision(sparse_data, gs_list, verbose)
sparse_data |
A named list that needs to have |
gs_list |
Nested list. Each sublist contains the (0-indexed!) positive and negative gene indices of that specific gene set. |
verbose |
Integer. |
A matrix of meta cells x vision scores per gene set.
DeTomaso, et al., Nat. Commun., 2019
The function will take in a list of gene sets that contains lists of
"pos"
and "neg" gene indices (0-indexed). You don't have to provide the "neg",
but it can be useful to classify the delta of two stats (EMT, Th1; Th2) etc.
Additionally, it will take a random gene list and calculate an
auto-correlation score based on Geary's C to identify pathways that show
significant patterns on the kNN graph generated on the provided embedding.
This version works on MetaCell counts which are stored in memory directly.
rs_mc_vision_with_autocorrelation( sparse_data, embd, knn_data, gs_list, random_gs_list, vision_params, cluster_membership, verbose, seed )rs_mc_vision_with_autocorrelation( sparse_data, embd, knn_data, gs_list, random_gs_list, vision_params, cluster_membership, verbose, seed )
sparse_data |
A named list that needs to have |
embd |
Numerical matrix. The embedding matrix to use to generate the
kNN graph. Needs to be of the same order/length as the meta cells in
|
knn_data |
Optional list. This contains pre-computed kNN data
( |
gs_list |
Nested list. Each sublist contains the (0-indexed!) positive and negative gene indices of that specific gene set. |
random_gs_list |
Double-nested list. The outer list represents the clusters of gene sets and the inner list represents the permutations within that cluster. |
vision_params |
List. The kNN parameters, only read when no
|
cluster_membership |
Integer vector. 1-indexed(!) position in
|
verbose |
Integer. |
seed |
Integer. Random seed for reproducibility. |
A list with the following items:
autocor_res - List with auto_cor (1 - Geary's C), p_val and
fdr, one entry per gene set.
vision_mat - A matrix of meta cells x vision scores per gene set.
DeTomaso, et al., Nat. Commun., 2019
Ranks the contrasts column-wise and runs the mitch MANOVA/ANOVA tests per
pathway.
rs_mitch_calc(x, pathway_list, min_size)rs_mitch_calc(x, pathway_list, min_size)
x |
Numerical matrix. Each column represents one of the contrasts you wish to test for and the rows represent the gene statistics per contrast. Needs row names (the genes). |
pathway_list |
Named list. Each element represents one of the pathways to test for. |
min_size |
Integer. Minimum size of the gene set to be tested for. |
A list with the following elements:
pathway_names - The name of the pathway.
pathway_sizes - The size of the pathway.
manova_pval - The p-value of the MANOVA test.
manova_fdr - The Benjamini-Hochberg adjusted manova_pval.
anova_pvals - The p-values of the ANOVA test on top of the MANOVA
results. Total length = ncol(x) * number of pathways, pathway-major.
scores - The scores for each pathway set, contrast. Same length
and layout as anova_pvals.
s_dist - Calculated distances from the hypotenuse.
sd - SDs of the scores.
Calculates the pairwise mutual information across all columns in the data.
rs_mutual_info(x, n_bins, strategy, normalise)rs_mutual_info(x, n_bins, strategy, normalise)
x |
R matrix with doubles for which to calculate the mutual information |
n_bins |
Optional integer. Number of bins to use. If |
strategy |
String. Binning strategy. One of
|
normalise |
Boolean. Shall the normalised mutual information be calculated via joint entropy. |
The symmetric mutual information matrix. The diagonal holds the
column entropies, or 0 if normalise = TRUE.
The arithmetic is the single cell one verbatim, only the counts come from
memory rather than the streamed store. What changes is the interpretation:
the cell-level overdispersion becomes the spread between aggregates within
a subject, not between cells, so it is smaller and absorbs whatever the
aggregation smoothed away. The subject-level term keeps its meaning. Do not
compare the two against a single cell run.
rs_nebula_mc( sparse_data, metacells_to_keep, gene_indices, subject_ids, design, offset, nebula_params, verbose )rs_nebula_mc( sparse_data, metacells_to_keep, gene_indices, subject_ids, design, offset, nebula_params, verbose )
sparse_data |
A named list that needs to have |
metacells_to_keep |
Integer vector. 0-indexed(!) positions of the meta cells to analyse, in any order. Must not hold duplicates. |
gene_indices |
Integer vector. 0-indexed(!) positions of the genes to fit. |
subject_ids |
Integer vector. 0-indexed(!) subject label per meta
cell. One entry per row of |
design |
Numeric matrix. Predictors of meta cells x coefficients, rows
aligned to |
offset |
Optional numeric vector. Strictly positive scaling factor per
selected meta cell. |
nebula_params |
Named list. The NEBULA parameters, see
|
verbose |
Integer. |
A list with the same elements rs_nebula_sc() returns.
He, et al., Commun Biol, 2021
This function calculates the specified semantic similarity between all
unique pairs of
terms (upper triangle, no diagonal) and returns them in
long format.
rs_onto_semantic_sim(terms, sim_type, ancestor_list, ic_list)rs_onto_semantic_sim(terms, sim_type, ancestor_list, ic_list)
terms |
Vector of strings. The terms in the ontology you wish to screen. |
sim_type |
String. Must be one of |
ancestor_list |
R list with names being the term and the elements in the list the names of the ancestors. |
ic_list |
R list with the names being the term and the elements the information content of this given term. Needs to be a single float! |
A list with:
term1 - name of the first term.
term2 - name of the second term.
sims - similarity between the two terms.
This function calculates the specified semantic similarity between all
terms in
ic_list (only calculating the upper triangle) and returns it
either as the flat upper triangle or as the full matrix.
rs_onto_semantic_sim_mat(sim_type, ancestor_list, ic_list, flat_matrix)rs_onto_semantic_sim_mat(sim_type, ancestor_list, ic_list, flat_matrix)
sim_type |
String. Must be one of |
ancestor_list |
R list with names being the term and the elements in the list the names of the ancestors. |
ic_list |
R list with the names being the term and the elements the information content of this given term. Needs to be a single float! |
flat_matrix |
Boolean. Shall only the upper triangle (row-wise, diagonal excluded) be returned. |
A list with:
sim_mat - the semantic similarity matrix (flat or as matrix). The
diagonal of the full matrix is 1.
names - the row and column names for the calculated matrix, i.e.
the names of ic_list sorted alphabetically.
This function calculates the Wang similarities between all unique pairs of
a given set of terms. Terms not found in the ontology get
NaN.
rs_onto_sim_wang(terms, parents, children, w)rs_onto_sim_wang(terms, parents, children, w)
terms |
String vector. The terms you wish to calculate the similarities for. |
parents |
String vector. The names of the parents. |
children |
String vector. The names of the children. The length of
|
w |
Numerics. The weights between the parents and children. Need to be values between 0 and 1. |
A list with:
term1 - name of the first term.
term2 - name of the second term.
sims - similarity between the two terms.
This function calculates the Wang similarity matrix for a given ontology.
rs_onto_sim_wang_mat(parents, children, w, flat_matrix)rs_onto_sim_wang_mat(parents, children, w, flat_matrix)
parents |
String vector. The names of the parents. |
children |
String vector. The names of the children. The length of
|
w |
Numerics. The weights between the parents and children. Need to be values between 0 and 1. |
flat_matrix |
Boolean. Shall only the upper triangle (row-wise, diagonal excluded) be returned. |
A list with:
sim_mat - the Wang similarity matrix (flat or as matrix.)
names - the row and column names for the calculated matrix.
Sorts
x in decreasing order and sums x[i] / i^2, normalised by the same
sum over a vector of ones of equal length.
rs_ot_harmonic_sum(x)rs_ot_harmonic_sum(x)
x |
The numeric vector (should be between 0 and 1) for which to calculate the harmonic sum |
The normalised harmonic sum according to the OT calculation.
Personalised page rank with at most 1000 iterations.
rs_page_rank( node_names, from, to, weights, personalised, undirected, damping_factor, tol )rs_page_rank( node_names, from, to, weights, personalised, undirected, damping_factor, tol )
node_names |
String vector. Name of the graph nodes. |
from |
String vector. The names of the |
to |
String vector. The names of the |
weights |
Optional weight vector. If NULL, defaults to 1.0 as weight for all edges. |
personalised |
Numerical vector. The reset values. They must sum to 1
and be of same length of |
undirected |
Boolean. Is this an undirected graph. |
damping_factor |
Numeric in |
tol |
Numeric > 0. Convergence threshold on the L1 change between iterations. |
The personalised page rank values.
Helper function to calculate in parallel on the same network the
personalised page rank as fast as possible. Can be used for permutation
type approaches.
rs_page_rank_parallel( node_names, from, to, weights, diffusion_scores, undirected, damping_factor, tol )rs_page_rank_parallel( node_names, from, to, weights, diffusion_scores, undirected, damping_factor, tol )
node_names |
String vector. Name of the graph nodes. |
from |
String vector. The names of the |
to |
String vector. The names of the |
weights |
Optional weight vector. If NULL, defaults to 1.0 as weight for all edges. |
diffusion_scores |
List. The personalised vectors for the page rank
reset values. Each element must sum to 1 and be of same length of
|
undirected |
Boolean. Is this an undirected graph. |
damping_factor |
Numeric in |
tol |
Numeric > 0. Convergence threshold on the L1 change between iterations. |
A matrix of the scores with each row representing an element in the
diffusion_scores list (in order), and each column representing the value
of the personalised page rank diffusion for this node.
Regresses each embedding dimension on the batch labels (one-way ANOVA) and
weights the per-dimension R-squared by the variance each dimension carries.
Compare the value on the uncorrected PCA with the one on the corrected
embedding,
(pre - post) / pre, rather than reading it on its own.
rs_pcr(embedding, batch_vector)rs_pcr(embedding, batch_vector)
embedding |
Numeric matrix. Cells x dimensions, ideally a PCA. |
batch_vector |
Integer vector. The batch per cell. The codes need not be 0-based or contiguous. |
A list with the following items
var_explained - Variance per embedding dimension.
r_squared - R-squared of batch per embedding dimension.
pcr - Variance-weighted R-squared of batch.
Büttner, et al., Nat Methods, 2019; Luecken, et al., Nat Methods, 2022
rs_phyper(q, m, n, k)rs_phyper(q, m, n, k)
q |
Number of white balls drawn out of urn. |
m |
Number of white balls in the urn. |
n |
Number of black balls in the urn. |
k |
The number of balls drawn out of the urn. |
P-value, i.e. P(X > q) (equivalent to lower.tail = FALSE).
Calculates the pointwise mutual information (can be also normalised) across
all columns of the data. This can be used to identify (dis)similar samples
based on Boolean characteristics.
rs_pointwise_mutual_info(x, normalise)rs_pointwise_mutual_info(x, normalise)
x |
Logical matrix. The columns represent features and the rows represent samples |
normalise |
Shall the normalised pointwise mutual information be returned. |
The (normalised) pointwise mutual information matrix between the
columns. Pairs without co-occurrence are -Inf.
Runs the singular value decomposition over the matrix x. Assumes that
samples = rows, and columns = features.
rs_prcomp(x, scale, top_pcs)rs_prcomp(x, scale, top_pcs)
x |
Numeric matrix. Rows = samples, columns = features. |
scale |
Boolean. Shall the columns be variance normalised. (Mean centring will automatically occur.) |
top_pcs |
Optional integer. Only return the top PCs (under the hood
all of them will be calculated). |
A list with:
scores - The product of x (centred and potentially scaled) with v.
v - v matrix of the SVD.
s - Standard deviations of the PCs, i.e. singular values divided
by sqrt(nrow(x) - 1).
scaled - Boolean. Was the matrix scaled.
Prepares the data for subsequent usage in ICA. Incorrect use can cause
kernel crashes. Wrapper around the Rust functions with type checks are
provided in the package.
rs_prepare_whitening(x, fast_svd, seed, rank, oversampling, n_power_iter)rs_prepare_whitening(x, fast_svd, seed, rank, oversampling, n_power_iter)
x |
Numeric matrix to whiten. The columns are centred and the whitening happens over the columns. |
fast_svd |
Boolean. Shall a randomised SVD be used. This is way faster on larger data sets. |
seed |
Integer. Only relevant with fast_svd is set to |
rank |
Integer. How many ranks to use for the fast SVD approximation.
If you supply |
oversampling |
Integer. Oversampling parameter to make the
approximation more precise. If you supply |
n_power_iter |
Integer. Number of power iterations for the randomised
SVD. If you supply |
A list containing:
x - The column-centred input, transposed.
k - The whitening matrix K. With fast_svd = TRUE it can carry
more than rank rows; the caller trims it.
Runs a randomised singular value decomposition over a matrix. This
implementation is faster than the full SVD on large data sets, with slight
loss in precision.
rs_random_svd(x, scale, rank, seed, oversampling, n_power_iter)rs_random_svd(x, scale, rank, seed, oversampling, n_power_iter)
x |
Numeric matrix. Rows = samples, columns = features. |
scale |
Boolean. Shall the columns be variance normalised. (Mean centring will automatically occur.) |
rank |
Integer. The rank to use. |
seed |
Integer. Random seed for reproducibility. |
oversampling |
Optional integer. Defaults to |
n_power_iter |
Optional integer. Number of power iterations (each with
a QR decomposition). Defaults to |
A list with:
scores - u matrix of the SVD multiplied by the singular values.
v - v matrix of the SVD.
s - Singular values of the SVD.
scaled - Boolean. Was the matrix scaled.
Applies a range normalisation on an R vector.
rs_range_norm(x, max_val, min_val)rs_range_norm(x, max_val, min_val)
x |
Numerical vector. The data to normalise. |
max_val |
Numeric. The upper bound value to normalise into. If set to 1, the function will be equal to a min-max normalisation. |
min_val |
Numeric. The lower bound value to normalise into. If set to 0, the function will equal a min-max normalisation. |
Normalised values. A constant vector returns max_val for every
element.
Ranks the columns of a matrix with the tie method.
rs_rank_matrix_col(exp)rs_rank_matrix_col(exp)
exp |
Numerical matrix. The expression matrix (rows = genes, columns = samples). |
Returns a matrix of ranks with the same shape as exp.
Unit-normalised column ranks computed against a set of stable genes. Use
with the
stable = TRUE setting of the singscore functions.
rs_rank_matrix_col_stable(exp, stable_gene_indices)rs_rank_matrix_col_stable(exp, stable_gene_indices)
exp |
Numerical matrix. The expression matrix (rows = genes, columns = samples). |
stable_gene_indices |
Integer vector of stable genes. One-indexed. |
Returns a matrix of normalised ranks with the same shape as exp.
Applies a radial basis function (RBF) to a given distance vector. Has the
option to apply a Gaussian, Bump or Inverse Quadratic RBF.
rs_rbf_function(x, epsilon, rbf_type)rs_rbf_function(x, epsilon, rbf_type)
x |
Numeric vector. The distances you wish to apply the RBF onto. |
epsilon |
Float. Epsilon parameter for the RBF. |
rbf_type |
String. Needs to be from
|
The affinities after the Kernel was applied.
Applies a radial basis function (RBF) to a given distance matrix. Has the
option to apply a Gaussian, Bump or Inverse Quadratic RBF.
rs_rbf_function_mat(x, epsilon, rbf_type)rs_rbf_function_mat(x, epsilon, rbf_type)
x |
Numeric Matrix. The distances you wish to apply the RBF onto. |
epsilon |
Float. Epsilon parameter for the RBF. |
rbf_type |
String. Needs to be from
|
The affinities after the Kernel was applied.
This function takes list of (named) matrices which represent for example
matrix factorisation results you wish to identify (k-th) reciprocal best
hits (RBH) for. The rows need to represent the features and the columns the
parts you wish to calculate the RBH for.
rs_rbh_cor(module_matrices, k_best, spearman, min_similarity)rs_rbh_cor(module_matrices, k_best, spearman, min_similarity)
module_matrices |
A named list of matrices with row and column names. Rows represent features and columns the modules you wish to calculate the correlations for. Only features shared between two matrices are used. |
k_best |
Integer. Number of best neighbours to consider. If set to
|
spearman |
Boolean. Shall Spearman correlation be used. |
min_similarity |
Numeric. Only hits with an absolute correlation strictly above this are returned. |
A list containing:
origin - The name of the origin of the gene modules.
target - The name of the target of the gene modules.
comparisons - Integer vector indicating how many RBH hits were identified in this comparison
origin_modules - Names of the gene modules from the origin.
target_modules - Names of the gene modules from the target.
similarity - The absolute correlations between the two respective gene modules.
This function takes a nested list that contains gene modules/sets derived
from various methods and identifies (k-th) reciprocal best hits between
gene modules/sets across every pair of origins.
rs_rbh_sets(module_list, k_best, overlap_coefficient, min_similarity)rs_rbh_sets(module_list, k_best, overlap_coefficient, min_similarity)
module_list |
A nested named list. The outer list should contain the origin of the gene modules, the inner list the names of the gene modules and the respective genes in them. |
k_best |
Integer. Number of best neighbours to consider. If set to
|
overlap_coefficient |
Boolean. Shall the overlap coefficient instead of the Jaccard similarity be used. |
min_similarity |
Numeric. Only hits with a similarity strictly above this are returned. |
A list containing:
origin - The name of the origin of the gene modules.
target - The name of the target of the gene modules.
comparisons - Integer vector indicating how many RBH hits were identified in this comparison
origin_modules - Names of the gene modules from the origin.
target_modules - Names of the gene modules from the target.
similarity - The similarities between the two respective gene modules.
limma's
removeBatchEffect, via the edge-rs crate. Batch gets
sum-to-zero contrasts, is fitted jointly with the design of interest and
only the batch part is subtracted. Meant for plotting and unsupervised
work; for testing put batch into the design.
rs_remove_batch_effect(x, batch, design)rs_remove_batch_effect(x, batch, design)
x |
Numeric matrix. Log-expression values of genes x samples. |
batch |
Integer vector. Batch label per sample, e.g.
|
design |
Numeric matrix or NULL. The design of interest, samples x coefficients, whose effects are protected. NULL is an intercept only. |
Numeric matrix of corrected values, genes x samples.
Smyth, Stat Appl Genet Mol Biol, 2004
This function is a wrapper over the Rust-based generation of the approximate
nearest neighbours.
rs_sc_knn(embd, knn_params, validate_index, verbose, seed)rs_sc_knn(embd, knn_params, validate_index, verbose, seed)
embd |
Numerical matrix. The embedding matrix to use to generate the kNN graph. |
knn_params |
List. The kNN parameters defined by
|
validate_index |
Boolean. If you want to validate the index via an exhaustive search in a subset of cells. |
verbose |
Integer. |
seed |
Integer. Seed for reproducibility purposes. |
An integer matrix of N x k with the neighbour indices (0-indexed!), N being the number of cells and k the number of neighbours. Rows the search left short are padded by repeating their last neighbour.
This function is a wrapper over the Rust-based generation of the approximate
nearest neighbours.
rs_sc_knn_w_dist(embd, knn_params, validate_index, verbose, seed)rs_sc_knn_w_dist(embd, knn_params, validate_index, verbose, seed)
embd |
Numerical matrix. The embedding matrix to use to generate the kNN graph. |
knn_params |
List. The kNN parameters defined by
|
validate_index |
Boolean. If you want to validate the index via an exhaustive search in a subset of cells. |
verbose |
Integer. |
seed |
Integer. Seed for reproducibility purposes. |
A list with:
indices - An integer matrix (cells x k) representing the indices (0-indexed!) of the approximate nearest neighbours.
dist - A numerical matrix (cells x k) representing the distances to the nearest neighbours.
dist_metric - String representing the used distance metric.
Rows the search left short are padded by repeating their last neighbour and distance.
This function takes a kNN matrix and generates the inputs for an SNN
graph based on it.
rs_sc_snn(knn_mat, snn_method, limited_graph, pruning, verbose)rs_sc_snn(knn_mat, snn_method, limited_graph, pruning, verbose)
knn_mat |
Integer matrix. Rows represent cells and the columns represent the neighbours (0-indexed!). |
snn_method |
String. Which method to use to calculate the similarity.
Choice of |
limited_graph |
Boolean. Shall the sNNs only be calculated between direct neighbours in the graph, or between all possible combinations. |
pruning |
Float. Below which similarity value to prune the weight to 0. A cell that loses every edge keeps its strongest kNN edge. |
verbose |
Integer. |
A list with the following items:
edges - Integer vector with the flattened sNN edge pairs
(1-indexed!), ready for igraph::add_edges().
weights - sNN weights of the pairs above.
This function calculates the Jaccard similarity or overlap coefficient
between two string vectors.
rs_set_similarity(s_1, s_2, overlap_coefficient)rs_set_similarity(s_1, s_2, overlap_coefficient)
s_1 |
Character vector. The first set. |
s_2 |
Character vector. The second set. |
overlap_coefficient |
Boolean. Use the overlap coefficient instead of the Jaccard similarity. |
The Jaccard similarity or overlap coefficient.
This function calculates the set similarity via Jaccard or overlap
coefficient across all permutations of one list.
rs_set_similarity_list(list, overlap_coefficient)rs_set_similarity_list(list, overlap_coefficient)
list |
A named R list. |
overlap_coefficient |
Boolean. Use the overlap coefficient instead of the Jaccard similarity. |
A list with the following items, one entry per unordered pair:
from - Name of element i
to - Name of element j
sim - Similarity between the two elements
This function calculates the Jaccard similarity or overlap coefficient
between all elements of two lists.
rs_set_similarity_list2(s_1_list, s_2_list, overlap_coefficient)rs_set_similarity_list2(s_1_list, s_2_list, overlap_coefficient)
s_1_list |
R list. The first list of string elements you want to compare against. |
s_2_list |
R list. The second list of string elements you want to compare against. |
overlap_coefficient |
Boolean. Use the overlap coefficient instead of the Jaccard similarity. |
A matrix of the similarities between the elements. The rows
represent s_1_list and the columns s_2_list.
Rust-based implementation of singscore over many gene sets with optional
paired down sets.
rs_singscore_multi( ranks, up_list, down_list, center_score, known_direction, stable )rs_singscore_multi( ranks, up_list, down_list, center_score, known_direction, stable )
ranks |
Numerical matrix. The ranked expression matrix. |
up_list |
List. Up gene sets as 0-based indices. See
|
down_list |
List or NULL. Paired down gene sets as 0-based indices,
same length and ordering as |
center_score |
Boolean. Centre scores around 0. Disabled internally
when |
known_direction |
Boolean. Whether the up-set direction is known. |
stable |
Boolean. If |
A named list with
scores - Numerical matrix with the scores
dispersions - Numerical matrix with the dispersions
Both matrices are of shape gene sets x samples.
For
n_permutations iterations, draws random gene indices of the same
total size as the real gene set(s), scores them with the same options as
the real call, and builds a per-sample null distribution. Returns empirical
one-tailed p-values: max(1 / n_permutations, mean(null > observed)).
rs_singscore_permutation_test( ranks, up_set, down_set, center_score, known_direction, stable, n_permutations, seed )rs_singscore_permutation_test( ranks, up_set, down_set, center_score, known_direction, stable, n_permutations, seed )
ranks |
Numerical matrix. The ranked expression matrix. |
up_set |
Integer vector. One-indexed (shifted internally). |
down_set |
Integer vector or NULL. One-indexed (shifted internally). |
center_score, known_direction, stable
|
Booleans. Should match the
values used for the real |
n_permutations |
Integer. Number of random draws (B). |
seed |
Integer. RNG seed. |
A named list with observed_scores (length n_samples),
null_distribution (B x n_samples matrix), and p_values
(length n_samples).
Rust-based implementation of singscore for a single up-regulated gene set
with an optional paired down-regulated set.
rs_singscore_single( ranks, up_set, down_set, center_score, known_direction, stable )rs_singscore_single( ranks, up_set, down_set, center_score, known_direction, stable )
ranks |
Numerical matrix. The ranked expression matrix (rows = genes,
columns = samples). Produce with column-wise ranks (standard) or with
|
up_set |
Integer vector. One-indexed gene indices of the up set. |
down_set |
Integer vector or NULL. One-indexed gene indices of the optional down set. |
center_score |
Boolean. Centre scores around 0. Disabled internally
when |
known_direction |
Boolean. Whether the up-set direction is known.
Becomes irrelevant when |
stable |
Boolean. If |
A named list with total_score, total_dispersion, up_score,
up_dispersion, down_score and down_dispersion. The last four are
NULL unless down_set is provided.
This function iteratively fuses the affinity matrices together.
rs_snf(aff_mat_list, k, t, alpha)rs_snf(aff_mat_list, k, t, alpha)
aff_mat_list |
A list of numerical matrices. The affinity matrices to fuse together. |
k |
Integer. Number of neighbours to consider. |
t |
Integer. Number of iterations for the algorithm. |
alpha |
Float. Normalisation parameter controlling the fusion strength. |
The final affinity matrix after the fusion.
This is the Rust implementation of dual graph regularised dictionary
learning in the implementation of Pan, et al., Cell Systems, 2022.
rs_sparse_dict_dgrdl(x, dgrdl_params, seed, verbose)rs_sparse_dict_dgrdl(x, dgrdl_params, seed, verbose)
x |
Numerical matrix. Rows = samples, columns = features. |
dgrdl_params |
A list with the parameters for the algorithm. Missing items fall back to defaults. Expects the following items.
|
seed |
Integer. Seed for the initialisation of the algorithm. |
verbose |
Boolean. Controls the verbosity of the function and reports timing of individual steps. |
A list with the following elements:
dictionary - The dictionary of samples x dict_size.
coefficients - The feature loadings of size dict_size x features.
feature_laplacian - The kNN graph Laplacian of the features as a
CSR list with data, indptr, indices, nrow, ncol and cs_type.
sample_laplacian - The kNN graph Laplacian of the samples, same format.
This is the Rust implementation of dual graph regularised dictionary
learning in the implementation of Pan, et al., Cell Systems, 2022. This
helper function is designed to run a grid search over the data.
rs_sparse_dict_dgrdl_grid_search( x, dgrdl_params, seeds, dict_sizes, k_neighbours_vec, verbose )rs_sparse_dict_dgrdl_grid_search( x, dgrdl_params, seeds, dict_sizes, k_neighbours_vec, verbose )
x |
Numerical matrix. Rows = samples, columns = features. |
dgrdl_params |
A list with the parameters for the algorithm. Missing items fall back to defaults. Expects the following items.
|
seeds |
Integer vector. The random seeds to include in the grid search. |
dict_sizes |
Integer vector. The dictionary sizes to test in the grid search. |
k_neighbours_vec |
Integer vector. The number of neighbours for the kNN graph generation to test in the grid search. |
verbose |
Boolean. Controls verbosity of the function. |
A list with the following elements, one entry per tested combination:
seed - The tested seeds.
dict_size - The tested dictionary sizes.
k_neighbours - The tested numbers of neighbours.
reconstruction_errs - The reconstruction errors (squared Frobenius norm) for these hyperparameters.
feature_laplacian_objective - The objective values of the feature Laplacian term for these hyperparameters.
sample_laplacian_objective - The objective values of the sample Laplacian term for these hyperparameters.
Milo's spatial FDR. Neighbourhoods overlap, so the tests are not
independent and a plain BH is anti-conservative. Each p-value is weighted
by the reciprocal of its connectivity and the step-up runs on those
weights. Non-finite p-values are carried through untouched and take no part
in the adjustment.
rs_spatial_fdr(p_values, connectivity)rs_spatial_fdr(p_values, connectivity)
p_values |
Numeric vector. One raw p-value per tested neighbourhood. |
connectivity |
Numeric vector. The matching connectivity per neighbourhood, either the k-th neighbour distances or the neighbourhood overlaps. A zero connectivity gets a weight of one, as in the upstream. |
The adjusted p-values, in the input order.
Dann, et al., Nat Biotechnol, 2022
This version can take in data as is and will calculate the distance matrix
internally and convert it into similarities with a Gaussian RBF.
rs_spectral_clustering( data, distance_type, epsilon, k_neighbours, n_clusters, max_iters, seed )rs_spectral_clustering( data, distance_type, epsilon, k_neighbours, n_clusters, max_iters, seed )
data |
Numerical matrix. The data to cluster. Rows = samples, columns = features. |
distance_type |
String. One of
|
epsilon |
Numerical. The epsilon parameter for the Gaussian Radial Basis function |
k_neighbours |
Integer. Number of neighbours to consider in the kNN graph generation |
n_clusters |
Integer. Number of clusters to identify |
max_iters |
Integer. Number of iterations for k-means clustering |
seed |
Integer. Seed for reproducibility |
A vector with the 1-based cluster membership of the samples.
Spectral clustering on a pre-calculated similarity matrix.
rs_spectral_clustering_sim( similarities, k_neighbours, n_clusters, max_iters, seed )rs_spectral_clustering_sim( similarities, k_neighbours, n_clusters, max_iters, seed )
similarities |
Numerical matrix representing the similarities. Needs to be symmetric! |
k_neighbours |
Integer. Number of neighbours to consider in the kNN graph generation |
n_clusters |
Integer. Number of clusters to identify |
max_iters |
Integer. Number of iterations for k-means clustering |
seed |
Integer. Seed for reproducibility |
A vector with the 1-based cluster membership of the samples.
Rust-based implementation of the popular single sample GSEA algorithm. Has
further performance optimisations compared to the original implementation.
rs_ssgsea(exp, gs_list, alpha, normalise, timings)rs_ssgsea(exp, gs_list, alpha, normalise, timings)
exp |
Numerical matrix. The expression matrix with rows = genes, and columns = samples |
gs_list |
List. A list containing the 0-based indices of the pathway
genes. See |
alpha |
Float. The alpha parameter to adjust the weights. |
normalise |
Boolean. Shall the scores be normalised. |
timings |
Boolean. Prints timings from the algorithm. |
Returns a matrix of gene set ES scores x samples.
Helper function to calculate in parallel on the same network the tied
diffusions as fast as possible. Can be used for permutation.
rs_tied_diffusion_parallel( node_names, from, to, weights, diffusion_scores_1, diffusion_scores_2, summarisation_fun, undirected, damping_factor, tol )rs_tied_diffusion_parallel( node_names, from, to, weights, diffusion_scores_1, diffusion_scores_2, summarisation_fun, undirected, damping_factor, tol )
node_names |
String vector. Name of the graph nodes. |
from |
String vector. The names of the |
to |
String vector. The names of the |
weights |
Optional weight vector. If NULL, defaults to 1.0 as weight for all edges. |
diffusion_scores_1 |
List. The first set of personalised vectors for
the page rank reset values. Each element must sum to 1 and be of same length
of |
diffusion_scores_2 |
List. The second set of personalised vectors for
the page rank reset values. Each element must sum to 1 and be of same length
of |
summarisation_fun |
String. One of |
undirected |
Boolean. Is this an undirected graph. |
damping_factor |
Numeric in |
tol |
Numeric > 0. Convergence threshold on the L1 change between iterations. |
A matrix of the scores with each row representing a tied diffusion
of the diffusion_scores_1 and diffusion_scores_2 lists (in order), and
each column representing the value of the tied diffusion for this node.
Calculates the topological overlap measure for a given affinity matrix x.
Has the option to calculate the signed and unsigned version.
rs_tom(x, tom_type, signed)rs_tom(x, tom_type, signed)
x |
Numerical matrix. Affinity matrix. |
tom_type |
String. One of |
signed |
Boolean. Shall the signed TOM be calculated, i.e. the
connectivity be taken over absolute affinities. If set to |
Returns the TOM matrix.
This function takes a flattened vector of the upper triangle from a
symmetric matrix (think correlation matrix) and reconstructs the full
dense matrix for you.
rs_upper_triangle_to_dense(data, shift, n)rs_upper_triangle_to_dense(data, shift, n)
data |
Numeric vector. The vector of for example correlation coefficients that you want to use to go back to a dense matrix. |
shift |
Boolean. If |
n |
Integer. Original dimension (i.e., ncol/nrow) of the matrix to be reconstructed. |
The dense R matrix.
This function takes the values from an upper triangle matrix, the shift
and the nrows/ncols and returns the full symmetric matrix as a compressed
sparse list.
rs_upper_triangle_to_sparse(value, shift, n, cs_type)rs_upper_triangle_to_sparse(value, shift, n, cs_type)
value |
Numeric vector. The upper triangle values. |
shift |
Boolean. Was the matrix shifted up ( |
n |
Integer. The number of columns/rows in the symmetric matrix. |
cs_type |
String. One of |
A list containing:
data - Numeric vector with the non-zero values.
indptr - Integer vector with the index pointers.
indices - Integer vector with the 0-based indices.
nrow - Number of rows.
ncol - Number of columns.
cs_type - "csr" or "csc".
limma's
voom on counts that are already filtered: log2-CPM against the
supplied (effective) library sizes, the mean-variance trend and the
precision weights. No filtering and no normalisation happen in here; pass
lib.size * norm.factors as lib_size to get voom on a normalised
DGEList.
rs_voom_normalise(counts, design, lib_size, span, adaptive_span)rs_voom_normalise(counts, design, lib_size, span, adaptive_span)
counts |
Integer or double matrix. Raw counts of genes x samples. |
design |
Numeric matrix. The design matrix of samples x coefficients. Must be full rank. |
lib_size |
Numeric vector. The effective library size per sample. |
span |
Numeric. Lowess span, only used if |
adaptive_span |
Boolean. Derive the span from the number of genes, as limma does since 3.56. |
A list with the following elements
e - Numeric matrix. The log2-CPM values, genes x samples. limma's
E.
weights - Numeric matrix. The precision weights, genes x samples.
trend_x - The mean-variance trend abscissae (average log2 count).
trend_y - The mean-variance trend ordinates (sqrt standard deviation).
amean - Average log2-CPM per gene.
Law, et al., Genome Biol, 2014
This provides a Rust-based implementation of the WNN algorithm from Hao,
et al. Both embeddings are L2-normalised per cell, a kNN graph with
knn_range neighbours is built per modality, and the per-cell modality
weights are then used to fuse both into one kNN graph.
rs_wnn(modality_emb_one, modality_emb_two, wnn_params, seed, verbose)rs_wnn(modality_emb_one, modality_emb_two, wnn_params, seed, verbose)
modality_emb_one |
Numerical matrix of the first modality, cells x dimensions. For example the PCA (or other embeddings) from the transcriptomics. |
modality_emb_two |
Numerical matrix of the second modality, same cells in the same row order. For example the PCA (or other embeddings) from the ADT counts. |
wnn_params |
Named list. The weighted nearest neighbour parameters. |
seed |
Integer. For reproducibility purposes. |
verbose |
Integer. |
A list with
indices - Integer matrix of cells x neighbours with the indices (0-indexed!) of the weighted nearest neighbours.
dist - Numerical matrix with the distances to these neighbours.
dist_metric - String. Always "kernelised pseudo-distance".
modality_one_weights - Numerical vector with the per-cell weights of the first modality.
modality_two_weights - Numerical vector with the per-cell weights of the second modality.
Hao et al., Cell, 2021
Port of
bpca() from pcaMethods. Variational Bayes with an ARD prior per
component, so components that the data do not support shrink towards zero
rather than fitting noise. Missing entries are imputed from the fit.
run_bpca(x, bpca_params = params_bpca(), .verbose = TRUE)run_bpca(x, bpca_params = params_bpca(), .verbose = TRUE)
x |
Numeric matrix. Rows = samples, columns = features. |
bpca_params |
List, see |
.verbose |
Boolean or integer. Verbosity. |
Deterministic, the start comes from an SVD. BPCA loadings are not
orthonormal, and r2_cum is computed on the observed entries only, as in
pcaMethods. Signs of the components are arbitrary.
A list with:
scores - Samples x n_pcs.
loadings - Features x n_pcs, not orthonormal.
r2_cum - Cumulative R^2 per component on the observed entries.
centre - Column centres that were subtracted.
scale - Column scales that were divided out.
completed - x with the missing entries imputed. Observed entries
are untouched.
noise_var - Residual variance, 1 / tau.
n_iter - Variational steps run.
converged - Whether tol was reached before max_iter.
Oba, et al., Bioinformatics, 2003; Stacklies, et al., Bioinformatics, 2007
set.seed(42L) x <- matrix(rnorm(50L * 10L), nrow = 50L, ncol = 10L) x[sample(length(x), 50L)] <- NA bpca_res <- run_bpca(x, .verbose = FALSE) head(bpca_res$scores)set.seed(42L) x <- matrix(rnorm(50L * 10L), nrow = 50L, ncol = 10L) x[sample(length(x), 50L)] <- NA bpca_res <- run_bpca(x, .verbose = FALSE) head(bpca_res$scores)
Helper function to run initial quality control on cells.
run_cell_qc( metrics, cells_to_keep, directions = NULL, threshold = 3, groups = NULL, hard_thresholds = NULL, mad = TRUE )run_cell_qc( metrics, cells_to_keep, directions = NULL, threshold = 3, groups = NULL, hard_thresholds = NULL, mad = TRUE )
metrics |
Named list of numeric vectors. |
cells_to_keep |
Integer. 0-indexed cell positions. |
directions |
Named character vector, one of |
threshold |
Numeric. MADs for MAD outlier detection. |
groups |
Optional grouping vector. |
hard_thresholds |
Optional named list of numeric vectors with |
mad |
Logical. If |
A CellQc object.
# MAD outlier detection over two metrics at once set.seed(42L) metrics <- list( lib_size = c(rnorm(99, 1000, 100), 50), pct_mt = runif(100, 0, 20) ) run_cell_qc( metrics, cells_to_keep = 0:99, directions = c(lib_size = "below", pct_mt = "above") )# MAD outlier detection over two metrics at once set.seed(42L) metrics <- list( lib_size = c(rnorm(99, 1000, 100), 50), pct_mt = runif(100, 0, 20) ) run_cell_qc( metrics, cells_to_keep = 0:99, directions = c(lib_size = "below", pct_mt = "above") )
Thin wrapper around run_cell_qc with MAD disabled.
run_cell_qc_fixed(metrics, cells_to_keep, hard_thresholds, groups = NULL)run_cell_qc_fixed(metrics, cells_to_keep, hard_thresholds, groups = NULL)
metrics |
Named list of numeric vectors. |
cells_to_keep |
Integer. 0-indexed cell positions. |
hard_thresholds |
Required. See |
groups |
Optional grouping vector. |
# a hard upper bound, no MAD anywhere set.seed(42L) qc <- run_cell_qc_fixed( metrics = list(pct_mt = runif(100, 0, 30)), cells_to_keep = 0:99, hard_thresholds = list(pct_mt = c(upper = 15)) ) sum(qc$combined)# a hard upper bound, no MAD anywhere set.seed(42L) qc <- run_cell_qc_fixed( metrics = list(pct_mt = runif(100, 0, 30)), cells_to_keep = 0:99, hard_thresholds = list(pct_mt = c(upper = 15)) ) sum(qc$combined)
The bixverse implementation of the RCisTarget workflow, one of the
algorithms used in SCENIC, see Aibar, et al. You will need motif to target
gene rankings, see read_motif_ranking() and the motif to TF
annotations, see read_motif_annotation_file().
run_cistarget( gs_list, rankings, annot_data, cis_target_params = params_cistarget(), .verbose = TRUE )run_cistarget( gs_list, rankings, annot_data, cis_target_params = params_cistarget(), .verbose = TRUE )
gs_list |
Named list of character vectors. Each element is a gene set
containing gene identifiers that must match row names in |
rankings |
Integer matrix. Motif rankings for genes. Row names are gene identifiers, column names are motif identifiers. Lower values indicate higher regulatory potential. |
annot_data |
data.table. Motif annotation database mapping motifs to
transcription factors. Must contain columns: |
cis_target_params |
List. Output of
|
.verbose |
Boolean. Controls verbosity of the function. |
data.table with enriched motifs and corresponding statistics and high & low confidence TFs for each gene set.
Aibar, et al., Nat Methods, 2017
# motif enrichment against a tiny synthetic ranking database rankings <- matrix( c(1L, 5L, 4L, 2L, 2L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 3L, 1L, 5L, 4L, 5L, 4L, 3L, 3L), nrow = 5, byrow = TRUE, dimnames = list(sprintf("gene_%i", 1:5), sprintf("motif_%i", 1:4)) ) annot <- data.table::data.table( motif = sprintf("motif_%i", 1:4), TF = sprintf("TF%i", 1:4), annotationSource = factor(c( "directAnnotation", "inferredBy_Orthology", "inferredBy_MotifSimilarity", "inferredBy_MotifSimilarity_n_Orthology" )) ) res <- run_cistarget( gs_list = list(set_a = c("gene_1", "gene_2", "gene_3")), rankings = rankings, annot_data = annot, cis_target_params = params_cistarget( auc_threshold = 1, nes_threshold = 0.2 ), .verbose = FALSE ) res[, c("gs_name", "motif", "nes")]# motif enrichment against a tiny synthetic ranking database rankings <- matrix( c(1L, 5L, 4L, 2L, 2L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 3L, 1L, 5L, 4L, 5L, 4L, 3L, 3L), nrow = 5, byrow = TRUE, dimnames = list(sprintf("gene_%i", 1:5), sprintf("motif_%i", 1:4)) ) annot <- data.table::data.table( motif = sprintf("motif_%i", 1:4), TF = sprintf("TF%i", 1:4), annotationSource = factor(c( "directAnnotation", "inferredBy_Orthology", "inferredBy_MotifSimilarity", "inferredBy_MotifSimilarity_n_Orthology" )) ) res <- run_cistarget( gs_list = list(set_a = c("gene_1", "gene_2", "gene_3")), rankings = rankings, annot_data = annot, cis_target_params = params_cistarget( auc_threshold = 1, nes_threshold = 0.2 ), .verbose = FALSE ) res[, c("gs_name", "motif", "nes")]
Runs filterByExpr() -> calcNormFactors() -> glmQLFit() ->
glmQLFTest() in Rust via the edge-rs crate, gated against edgeR 4.8.2.
The tested axis does not have to be genes. Anything with a counts matrix of
features x samples goes through here, which is why the Milo neighbourhood
test calls the same function with filter = FALSE.
By default this skips estimateDisp() and lets the fit estimate its own
dispersion from the most abundant features. That is edgeR 4's own
recommendation and where most of the runtime went. Set legacy = TRUE in
params_edger_ql() for the pre-4.0 pipeline, which does run
estimateDisp().
run_edger_ql( counts, design, coef = NULL, contrast = NULL, edger_params = params_edger_ql() )run_edger_ql( counts, design, coef = NULL, contrast = NULL, edger_params = params_edger_ql() )
counts |
Numeric matrix. Raw counts of features x samples, with rownames. Must not be normalised or log-transformed. |
design |
Numeric matrix. The design matrix of samples x coefficients,
including the intercept. Usually the output of |
coef |
Optional integer or character. Which coefficient(s) of |
contrast |
Optional numeric vector or matrix. Weights over the
coefficients, one entry (or row) per column of |
edger_params |
A list, see
|
A data.table with one row per feature that survived the filters:
feature_id - The rowname of counts.
log_fc - Log2 fold change of the tested coefficient or contrast.
log_cpm - Average log2 counts per million.
f_stat - The quasi-likelihood F statistic.
p_value - Raw p-value.
fdr - Benjamini-Hochberg adjusted p-value.
Chen, Lun and Smyth, F1000Research, 2016
# quasi-likelihood F test on the second design column syn <- synthetic_bulk_cor_matrix() grp <- factor(rep(c("ctrl", "case"), each = 50), levels = c("ctrl", "case")) design <- stats::model.matrix(~grp) res <- run_edger_ql(counts = syn$counts, design = design) head(res)# quasi-likelihood F test on the second design column syn <- synthetic_bulk_cor_matrix() grp <- factor(rep(c("ctrl", "case"), each = 50), levels = c("ctrl", "case")) design <- stats::model.matrix(~grp) res <- run_edger_ql(counts = syn$counts, design = design) head(res)
Assigns cells to branches from their fate probabilities, then fits a landmark Gaussian process with a Matern-5/2 kernel per branch, which is the Mellon-based estimator of the reference rather than the legacy GAM. Out comes one smooth curve per gene and branch, on a common pseudotime grid.
Whatever expression is handed over is what gets fitted. With
use_magic = FALSE that is the normalised counts; with TRUE it is the
imputed layer run_magic_sc() wrote. Imputing first smooths
twice, which is a defensible presentation choice and not usually a necessary
one.
run_gene_trends_sc( object, palantir_res, features = NULL, use_magic = FALSE, branch_params = params_sc_branch_selection(), gene_trend_params = params_sc_gene_trends(), .verbose = TRUE )run_gene_trends_sc( object, palantir_res, features = NULL, use_magic = FALSE, branch_params = params_sc_branch_selection(), gene_trend_params = params_sc_gene_trends(), .verbose = TRUE )
object |
One of |
palantir_res |
|
features |
Optional character vector. The genes to fit. If |
use_magic |
Boolean. Fit the MAGIC imputed layer rather than the
normalised counts. Needs |
branch_params |
List. See |
gene_trend_params |
List. See |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A GeneTrendsRes S3 object with:
trends - Long data.table with branch, pseudotime, gene and
expression, ready to facet.
branch_cells - Named list of the cell names selected for each branch.
branches - Character vector with the branch names, taken from the column names of the fate probability matrix.
params - List with the parameters the run used.
run_info - List with n_cells and jitter_used per branch and the
grid resolution.
Palantir's pseudotime is min-max scaled to [0, 1], so the reference's
length_scale of 1.0 spans the entire domain and its sigma of 1.0
sits at roughly the signal scale of log-normalised expression. The posterior
is prior-dominated: it flattens genuine transient structure and resolves
almost any gene into a smooth monotone or single-peaked curve. Shorten
length_scale in params_sc_gene_trends() before believing a
bump.
Setty, et al., Nat. Biotechnol., 2019.
# GP trends for five genes along the Palantir branches sc <- demo_single_cells() palantir <- run_palantir_sc( sc, early_cell = get_knn_obj(sc)$used_cells[1], palantir_params = params_sc_palantir( knn = 15L, num_waypoints = 100L, n_eigs = 3L, use_early_cell_as_start = TRUE, knn_params = list(knn_method = "exhaustive") ), .verbose = FALSE ) res <- run_gene_trends_sc( sc, palantir_res = palantir, features = get_gene_names(sc)[1:5], gene_trend_params = params_sc_gene_trends(resolution = 50L), .verbose = FALSE ) head(res$trends) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# GP trends for five genes along the Palantir branches sc <- demo_single_cells() palantir <- run_palantir_sc( sc, early_cell = get_knn_obj(sc)$used_cells[1], palantir_params = params_sc_palantir( knn = 15L, num_waypoints = 100L, n_eigs = 3L, use_early_cell_as_start = TRUE, knn_params = list(knn_method = "exhaustive") ), .verbose = FALSE ) res <- run_gene_trends_sc( sc, palantir_res = palantir, features = get_gene_names(sc)[1:5], gene_trend_params = params_sc_gene_trends(resolution = 50L), .verbose = FALSE ) head(res$trends) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Topic model over a documents x terms count matrix, fitted by variational
Bayes. The intended input is a binary matrix: this is the model behind
cisTopic, where binarised cells x regions scATAC becomes cells x topics for
clustering and topics x regions for region set discovery. Nothing here is
ATAC-specific, so binarised regulon activity from
binarise_regulon_activity() works the same way, as does any
count matrix.
run_lda(x, k, lda_params = params_lda(), seed = 42L, .verbose = TRUE)run_lda(x, k, lda_params = params_lda(), seed = 42L, .verbose = TRUE)
x |
Documents x terms matrix. Either a |
k |
Integer. Number of topics. |
lda_params |
List, see |
seed |
Integer. Seed for the variational initialisation. |
.verbose |
Boolean or integer. Verbosity. |
Binarising first is what makes a topic model the right tool. LDA on raw single cell counts is dominated by library size, which is precisely the effect cisTopic sidesteps.
Use lda_k_sweep() if you do not already know k.
An LdaResult object.
Hoffman, Blei and Bach, NIPS, 2010; Bravo Gonzalez-Blas, et al., Nat Methods, 2019
# two planted term blocks recovered as two topics set.seed(42L) corpus <- matrix(rbinom(200L * 40L, 1L, 0.05), nrow = 200L, ncol = 40L) corpus[1:100, 1:10] <- rbinom(1000L, 1L, 0.6) corpus[101:200, 11:20] <- rbinom(1000L, 1L, 0.6) colnames(corpus) <- sprintf("term_%02d", 1:40) lda_res <- run_lda(corpus > 0, k = 2L, .verbose = FALSE) lda_res# two planted term blocks recovered as two topics set.seed(42L) corpus <- matrix(rbinom(200L * 40L, 1L, 0.05), nrow = 200L, ncol = 40L) corpus[1:100, 1:10] <- rbinom(1000L, 1L, 0.6) corpus[101:200, 11:20] <- rbinom(1000L, 1L, 0.6) colnames(corpus) <- sprintf("term_%02d", 1:40) lda_res <- run_lda(corpus > 0, k = 2L, .verbose = FALSE) lda_res
Runs the limma-voom workflow (calcNormFactors() -> voomLmFit() ->
contrasts.fit() -> eBayes() -> topTable()) in Rust via the edge-rs
crate, gated against limma 3.66.0. The design is
~ 0 + main_contrast + co_variates and every requested contrast is tested
separately. limma and edgeR are not needed.
run_limma_voom( meta_data, main_contrast, counts, contrast_list = NULL, co_variates = NULL, limma_params = params_limma_voom(), lib_size = NULL, .verbose = TRUE )run_limma_voom( meta_data, main_contrast, counts, contrast_list = NULL, co_variates = NULL, limma_params = params_limma_voom(), lib_size = NULL, .verbose = TRUE )
meta_data |
data.table. The meta information about the experiment in
which the contrast info (and potential co-variates) can be found. Rows need
to be in the same order as the columns of |
main_contrast |
String. Which column contains the main groups you want to test differential gene expression with the Limma-Voom workflow for. |
counts |
Numeric matrix. Raw counts of genes x samples, with gene identifiers as row names. |
contrast_list |
String vector or NULL. Optional string vector of
contrast formatted as |
co_variates |
String or NULL. Optional co-variates you wish to consider during model fitting. |
limma_params |
List. The limma parameters, see
|
lib_size |
Optional numeric vector. Library size per sample, in the
column order of |
.verbose |
Boolean. Controls verbosity of the function. |
A data.table with the columns of limma's topTable(confint = TRUE)
(gene_id, logFC, CI.L, CI.R, AveExpr, t, P.Value,
adj.P.Val, B) plus contrast, sorted by p-value within each contrast.
Law, et al., Genome Biol, 2014
# voom fit and topTable results for the single case vs control contrast syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) res <- run_limma_voom( meta_data = meta, main_contrast = "case_control", counts = syn$counts, .verbose = FALSE ) head(res)# voom fit and topTable results for the single case vs control contrast syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) res <- run_limma_voom( meta_data = meta, main_contrast = "case_control", counts = syn$counts, .verbose = FALSE ) head(res)
MAGIC smooths counts over the kNN graph: a row-stochastic diffusion operator
is built from the graph stored on the object and applied n_steps times, so
every cell becomes a weighted average of its neighbourhood. It makes sparse
marker genes readable on an embedding and gives gene trends something less
speckled to fit. For details, please refer to van Dijk, et al.
Deliberately restricted to a gene subset. The output is dense and the binary store is never touched, so pick the genes you want to plot or fit and impute those. Genome-wide imputation is not offered.
Each call replaces the whole layer. Read it back with
get_magic(), or hand it to any of the extractors with
layer = "magic".
run_magic_sc( object, features, modality = c("rna", "adt", "wnn"), magic_params = params_sc_magic(), .verbose = TRUE )run_magic_sc( object, features, modality = c("rna", "adt", "wnn"), magic_params = params_sc_magic(), .verbose = TRUE )
object |
One of |
features |
Character vector. The genes to impute. Anything not found in the object is dropped with a warning. |
modality |
String. One of |
magic_params |
List. See |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The object with an ScMagic layer in its cache.
Neighbourhoods overlap, so smoothing over them makes nearby cells resemble each other in every gene at once. Gene-gene correlation after imputation is inflated, badly, and the inflation is a property of the graph rather than of the biology. Do not feed imputed values into Hotspot, SCENIC, differential correlation or CoReMo: those methods measure exactly the quantity MAGIC manufactures. Visualisation and trend fitting are what it is for.
The operator preserves per-cell mass, so imputed values sit on the scale of
whatever went in. Imputing raw counts and imputing log-normalised counts are
different operations rather than the same one rescaled, see the layer
element of params_sc_magic().
van Dijk, et al., Cell, 2018.
# diffusion imputation of five genes for plotting sc <- demo_single_cells() sc <- run_magic_sc( sc, features = get_gene_names(sc)[1:5], .verbose = FALSE ) dim(get_magic(sc)$data) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# diffusion imputation of five genes for plotting sc <- demo_single_cells() sc <- run_magic_sc( sc, features = get_gene_names(sc)[1:5], .verbose = FALSE ) dim(get_magic(sc)$data) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
PAGA abstracts the single cell kNN graph into a cluster-level graph, where the edge weight between two clusters measures how much more connected they are than expected under a random null model. It also returns the maximum spanning forest of that graph, which is the backbone typically plotted. For details, please refer to Wolf, et al.
Only the kNN indices are used, no distances and no expression data, so the
method is safe on any of the available graphs, including the WNN one. Run
find_neighbours_sc() and a clustering first.
run_paga_sc( object, cluster_col, modality = c("rna", "adt", "wnn"), .verbose = TRUE )run_paga_sc( object, cluster_col, modality = c("rna", "adt", "wnn"), .verbose = TRUE )
object |
One of |
cluster_col |
String. The obs column holding the cluster assignments. Empty factor levels are retained. |
modality |
String. One of |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A PagaRes S3 object with:
connectivities - Symmetric sparse matrix of clusters x clusters with
a zero diagonal. Values lie in (0, 1].
connectivities_tree - Maximum spanning forest of connectivities,
carrying the original connectivity values on the retained edges.
sizes - data.table with cluster and n_cells.
params - List with cluster_col and modality.
Wolf, et al., Genome Biol., 2019.
# cluster level graph abstraction of the Leiden clusters sc <- demo_single_cells() sc <- find_clusters_sc(sc, res = 1.0) res <- run_paga_sc(sc, cluster_col = "leiden_clustering", .verbose = FALSE) res$sizes unlink(sc@dir_data, recursive = TRUE, force = TRUE)# cluster level graph abstraction of the Leiden clusters sc <- demo_single_cells() sc <- find_clusters_sc(sc, res = 1.0) res <- run_paga_sc(sc, cluster_col = "leiden_clustering", .verbose = FALSE) res$sizes unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Palantir models differentiation as a Markov chain over a diffusion map of the cells. It returns a pseudotime per cell, the probability of each cell reaching each terminal state, and the differentiation entropy derived from those probabilities. For details, please refer to Setty, et al.
The kNN graph stored on the object feeds the diffusion kernel, so run
find_neighbours_sc() first. The geodesics themselves are measured
over a second kNN graph that Palantir builds internally on the multiscale
space, controlled by the knn element of
params_sc_palantir().
For SingleCellsMultiModal the "wnn" graph works, but its distances are
kernel-derived and not a metric, so the diffusion map on top is a heuristic
on a heuristic. You will get a warning saying as much.
run_palantir_sc( object, early_cell, terminal_states = NULL, modality = c("rna", "adt", "wnn"), palantir_params = params_sc_palantir(), seed = 42L, .verbose = TRUE )run_palantir_sc( object, early_cell, terminal_states = NULL, modality = c("rna", "adt", "wnn"), palantir_params = params_sc_palantir(), seed = 42L, .verbose = TRUE )
object |
One of |
early_cell |
String. Name of the cell to start the trajectory from. Must be one of the cells the kNN graph was built over. |
terminal_states |
Optional character vector. Names of the terminal state
cells. If |
modality |
String. One of |
palantir_params |
List. See |
seed |
Integer. For reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A PalantirRes S3 object with:
pseudotime - data.table with cell_id, pseudotime (min-max scaled
to [0, 1]; the start cell is not pinned to 0, and a start cell far from 0
means the refinement disagreed with the anchor) and entropy.
branch_probs - Numeric matrix of cells x terminal states with the fate probabilities. Rows need not sum to one, as sub-threshold values are zeroed without renormalisation.
terminal_states - Character vector with the terminal state cell
names, carrying the labels of a named terminal_states argument as its
own names. Sets the column order of branch_probs.
waypoints - Character vector with the waypoint cell names. The first element is the start cell.
start_cell - String. The start cell that was actually used.
multiscale - Numeric matrix of cells x components with the multiscale diffusion components.
run_info - List with iterations, converged, eigen_converged,
eigen_residual, repair_edges, stranded_waypoints, n_waypoints and
modality. A eigen_converged of FALSE means the diffusion eigensolve
ran out of restarts and the embedding is under-resolved.
Setty, et al., Nat. Biotechnol., 2019.
# pseudotime from an arbitrary start cell on the demo kNN graph sc <- demo_single_cells() res <- run_palantir_sc( sc, early_cell = get_knn_obj(sc)$used_cells[1], palantir_params = params_sc_palantir( knn = 15L, num_waypoints = 100L, n_eigs = 3L, use_early_cell_as_start = TRUE, knn_params = list(knn_method = "exhaustive") ), .verbose = FALSE ) head(res$pseudotime) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# pseudotime from an arbitrary start cell on the demo kNN graph sc <- demo_single_cells() res <- run_palantir_sc( sc, early_cell = get_knn_obj(sc)$used_cells[1], palantir_params = params_sc_palantir( knn = 15L, num_waypoints = 100L, n_eigs = 3L, use_early_cell_as_start = TRUE, knn_params = list(knn_method = "exhaustive") ), .verbose = FALSE ) head(res$pseudotime) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Port of
ppca() from pcaMethods. Fits the principal subspace by EM on the
observed entries only, then imputes the missing entries from it. Use it to
get a PCA or a completed matrix out of data with sporadic NAs, for
example proteomics or metabolomics intensities.
run_ppca(x, ppca_params = params_ppca(), .verbose = TRUE)run_ppca(x, ppca_params = params_ppca(), .verbose = TRUE)
x |
Numeric matrix. Rows = samples, columns = features. |
ppca_params |
List, see |
.verbose |
Boolean or integer. Verbosity. |
The initial loadings are drawn from ppca_params$seed with the Rust RNG, so
results agree with pcaMethods at convergence, not iterate by iterate. PPCA
loadings are orthonormal. Signs of the components are arbitrary.
A list with:
scores - Samples x n_pcs.
loadings - Features x n_pcs, orthonormal.
r2_cum - Cumulative R^2 per component on the completed matrix.
centre - Column centres that were subtracted.
scale - Column scales that were divided out.
completed - x with the missing entries imputed. Observed entries
are untouched.
noise_var - Residual variance outside the subspace.
n_iter - EM iterations run.
converged - Whether tol was reached before max_iter.
Roweis, NIPS, 1998; Tipping and Bishop, J R Stat Soc B, 1999; Stacklies, et al., Bioinformatics, 2007
set.seed(42L) x <- matrix(rnorm(50L * 10L), nrow = 50L, ncol = 10L) x[sample(length(x), 50L)] <- NA ppca_res <- run_ppca(x, .verbose = FALSE) head(ppca_res$scores)set.seed(42L) x <- matrix(rnorm(50L * 10L), nrow = 50L, ncol = 10L) x[sample(length(x), 50L)] <- NA ppca_res <- run_ppca(x, .verbose = FALSE) head(ppca_res$scores)
This function will run the SNF algorithm on top of the adjacency matrices
found in the object. You can also optionally specify which adjacency matrices
to use via the to_include parameter.
run_snf(object, to_include = NULL, params = NULL)run_snf(object, to_include = NULL, params = NULL)
object |
The underlying class, see |
to_include |
Optional string, if you wish to only use a subset of the
generated adjacency matrices. If |
params |
Optional List. If you wish to overwite the already set up
parameters for SNF, see |
The class with added adjacency matrix based on the SNF algorithm.
# fuse a continuous and a categorical modality set.seed(42) continuous <- matrix(rnorm(120), nrow = 12, ncol = 10) rownames(continuous) <- sprintf("sample_%02i", 1:12) colnames(continuous) <- sprintf("feature_%i", 1:10) clinical <- data.table::data.table( sample_id = rownames(continuous), sex = factor(sample(c("M", "F"), 12, replace = TRUE)), stage = factor(sample(c("I", "II", "III"), 12, replace = TRUE)) ) object <- SimilarityNetworkFusion( data = continuous, data_name = "continuous", snf_params = params_snf(k = 3L) ) object <- add_snf_data_modality(object, clinical, data_name = "clinical") object <- run_snf(object) dim(get_snf_final_mat(object))# fuse a continuous and a categorical modality set.seed(42) continuous <- matrix(rnorm(120), nrow = 12, ncol = 10) rownames(continuous) <- sprintf("sample_%02i", 1:12) colnames(continuous) <- sprintf("feature_%i", 1:10) clinical <- data.table::data.table( sample_id = rownames(continuous), sex = factor(sample(c("M", "F"), 12, replace = TRUE)), stage = factor(sample(c("I", "II", "III"), 12, replace = TRUE)) ) object <- SimilarityNetworkFusion( data = continuous, data_name = "continuous", snf_params = params_snf(k = 3L) ) object <- add_snf_data_modality(object, clinical, data_name = "clinical") object <- run_snf(object) dim(get_snf_final_mat(object))
SingleCells object to h5adWrites the counts, the DuckDB obs/var tables and whatever is cached in memory (PCA, embeddings, sNN graph) to a spec-compliant h5ad file, so that the experiment can be handed to ScanPy or shared. The counts are streamed cell batch by cell batch and never fully materialised in R.
Only the cells that are currently kept are written, i.e. the export matches
what object[] and object[[]] return. Counts are stored as float32,
which is the ScanPy convention.
A SingleCellsMultiModal object inherits this method and exports its RNA
modality; the ADT layer is not written.
save_h5ad( object, h5_path, assay = c("raw", "norm"), chunk_size = 10000L, overwrite = TRUE, .verbose = TRUE )save_h5ad( object, h5_path, assay = c("raw", "norm"), chunk_size = 10000L, overwrite = TRUE, .verbose = TRUE )
object |
|
h5_path |
File path to write the h5ad file to. |
assay |
String. One of |
chunk_size |
Integer. Number of cells per streaming batch. Defaults to
|
overwrite |
Boolean. Shall an existing file be overwritten. Defaults to
|
.verbose |
Boolean. Controls the verbosity of the function. |
Returns the path to the written file, invisibly.
Helper function that stores the memory-bound data to disk for checkpointing
or when you close the session for quick recovery of prior work. You have the
option to save as ".rds" or ".qs2" (you need to have the package "qs2"
installed for this option!).
save_sc_exp_to_disk(object, type = c("qs2", "rds"))save_sc_exp_to_disk(object, type = c("qs2", "rds"))
object |
|
type |
String. One of |
NULL, invisibly. Called for the side effect of writing the
in-memory maps and caches next to the counts. It does not return the
object, so do not assign the result.
# checkpoint the in-memory map and cache next to the counts sc <- demo_single_cells() save_sc_exp_to_disk(sc, type = "rds") list.files(sc@dir_data) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# checkpoint the in-memory map and cache next to the counts sc <- demo_single_cells() save_sc_exp_to_disk(sc, type = "rds") list.files(sc@dir_data) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Converts the bixverse single cell kNN class into the manifoldsR
NearestNeighbours class for use in UMAP, tSNE, etc. Note that the
bixverse class stores 0-indexed indices whereas manifoldsR expects
1-indexed, so the conversion handles that.
sc_knn_to_nearest_neighbours(x)sc_knn_to_nearest_neighbours(x)
x |
|
A NearestNeighbours class compatible with manifoldsR.
# hand the single cell kNN graph over to manifoldsR sc <- demo_single_cells() knn <- generate_knn_sc(sc, .validate_index = FALSE, .verbose = FALSE) sc_knn_to_nearest_neighbours(knn) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# hand the single cell kNN graph over to manifoldsR sc <- demo_single_cells() knn <- generate_knn_sc(sc, .validate_index = FALSE, .verbose = FALSE) sc_knn_to_nearest_neighbours(knn) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Linear container of ScSteps. Append steps with %>>% and execute with
apply_pipeline(). Pipelines are inert until applied; steps can be
inspected via pipeline$steps.
sc_pipeline()sc_pipeline()
An empty ScPipeline object.
# an empty container, filled with `%>>%` sc_pipeline() %>>% step_hvg_sc(hvg_no = 30L) %>>% step_pca_sc(no_pcs = 10L)# an empty container, filled with `%>>%` sc_pipeline() %>>% step_hvg_sc(hvg_no = 30L) %>>% step_pca_sc(no_pcs = 10L)
Cluster-aware doublet detection using engineered features and a gradient-boosted classifier. See Germain et al., F1000Research, 2022.
scdblfinder_sc( object, scdblfinder_params = params_scdblfinder(), return_features = FALSE, cells_to_use = NULL, group_by = NULL, streaming = NULL, seed = 42L, .verbose = TRUE )scdblfinder_sc( object, scdblfinder_params = params_scdblfinder(), return_features = FALSE, cells_to_use = NULL, group_by = NULL, streaming = NULL, seed = 42L, .verbose = TRUE )
object |
|
scdblfinder_params |
List. Parameters from
|
return_features |
Boolean. Shall the features used to train the classifier be returned. |
cells_to_use |
Optional string. Names of the cells to use for the run of the boosted doublet detection. Useful when you wish to run doublet detection on individual batches within your data. The object returned will be specifically using these cells. |
group_by |
Optional grouping variable. Useful if you want to run the method on a per-sample basis. |
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
seed |
Integer. Seed for reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
An S3 object of class ScDblFinderRes containing:
Logical vector of doublet calls.
Numeric vector of classifier probabilities.
Numeric vector of the cxds scores.
Numeric vector of the weighted scores.
The threshold used for calling.
Integer vector of final cluster assignments.
Fraction of cells called as doublets.
with cell_indices stored as an attribute.
# cluster aware doublet calls from the gradient boosted classifier sc <- demo_single_cells(prepped = FALSE) scdblfinder_sc( sc, scdblfinder_params = params_scdblfinder( pca = list(no_pcs = 10L), n_genes = 25L, cxds_genes = 25L ), .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# cluster aware doublet calls from the gradient boosted classifier sc <- demo_single_cells(prepped = FALSE) scdblfinder_sc( sc, scdblfinder_params = params_scdblfinder( pca = list(no_pcs = 10L), n_genes = 25L, cxds_genes = 25L ), .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Filters genes by minimum total counts and minimum expressed-cell fraction using the SCENIC inclusion criteria. Returns a character vector of gene identifiers passing both filters.
scenic_gene_filter_sc( object, scenic_params = params_scenic(), cells_to_take = NULL, .verbose = TRUE )scenic_gene_filter_sc( object, scenic_params = params_scenic(), cells_to_take = NULL, .verbose = TRUE )
object |
|
scenic_params |
List. SCENIC parameters, see
|
cells_to_take |
Optional string vector. Cell identifiers to restrict
to. If |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A character vector of gene identifiers passing the SCENIC inclusion criteria.
# genes clearing the SCENIC count and prevalence thresholds sc <- demo_single_cells() genes <- scenic_gene_filter_sc( sc, scenic_params = params_scenic(min_counts = 100L, min_cells = 0.05), .verbose = FALSE ) length(genes) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# genes clearing the SCENIC count and prevalence thresholds sc <- demo_single_cells() genes <- scenic_gene_filter_sc( sc, scenic_params = params_scenic(min_counts = 100L, min_cells = 0.05), .verbose = FALSE ) length(genes) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Runs SCENIC GRN inference on the provided genes using the specified
transcription factors as predictors. Returns a ScenicGrn object
containing the TF-gene importance matrix for further processing.
scenic_grn_sc( object, tf_ids, scenic_params = params_scenic(), genes_to_take = NULL, cells_to_take = NULL, streaming = NULL, random_seed = 42L, .verbose = TRUE )scenic_grn_sc( object, tf_ids, scenic_params = params_scenic(), genes_to_take = NULL, cells_to_take = NULL, streaming = NULL, random_seed = 42L, .verbose = TRUE )
object |
|
tf_ids |
Character vector. Transcription factor gene identifiers to use as predictors. Must be a subset of gene identifiers present in the object. |
scenic_params |
List. SCENIC parameters, see
|
genes_to_take |
Optional character vector. Target gene identifiers.
If |
cells_to_take |
Optional string vector. Cell identifiers to restrict
to. If |
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
random_seed |
Integer. Used for reproducibility. Defaults to |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
TF identifiers that are not found in the object's gene list are silently
dropped with a warning indicating how many were removed. TF indices are
intersected with the target gene indices so that TFs not passing the gene
filter are excluded from the predictor set but remain as potential targets
if present in genes_to_take. You have the option to generate the TF-gene
importance values with three distinct methods. For the random_forest and
the extratrees version, a batching strategy is applied in the default
settings. Correlated genes are identified and clustered together via
k-means clustering on the feature loadings of the PCA. These are then
divided into batches of gene_batch_size and the regression learners
are leveraging multi-target regression to fit all genes in the batch in one
go. This massively accelerates the algorithm and the importance values per
gene-TF pair are calculated then individually. Due to the batching by
similar gene, the signal dilution is limited. If you wish to run the
traditional approach, you can set gene_batch_size to 1L or use the
grnboost2 learner that can only fit one gene at a given time.
A ScenicGrn object.
# TF to gene importances from a small random forest sc <- demo_single_cells() res <- scenic_grn_sc( sc, tf_ids = get_gene_names(sc)[1:5], scenic_params = params_scenic( min_counts = 100L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) res unlink(sc@dir_data, recursive = TRUE, force = TRUE)# TF to gene importances from a small random forest sc <- demo_single_cells() res <- scenic_grn_sc( sc, tf_ids = get_gene_names(sc)[1:5], scenic_params = params_scenic( min_counts = 100L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) res unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Score clusters based on ScType
score_clusters(x, cluster_labels) ## S3 method for class 'ScTypeResults' score_clusters(x, cluster_labels)score_clusters(x, cluster_labels) ## S3 method for class 'ScTypeResults' score_clusters(x, cluster_labels)
x |
|
cluster_labels |
Integer vector. Cluster assignment, of length of the scored cells. |
A data.table with cluster_id, cell_type, scores and n_cells.
# aggregate the per cell ScType scores onto a clustering sc <- demo_single_cells() markers <- data.table::data.table( cell_type = rep(sprintf("cell_type_%i", 1:3), each = 10), gene_id = sprintf("gene_%02d", 1:30) ) cell_markers <- prepare_cell_markers(obj = sc, marker_df = markers) scores <- calc_sc_type_scores( sc, cell_marker_list = cell_markers, .verbose = FALSE ) clusters <- fast_cluster_sc( sc, resolutions = 1.0, n_centroids = 30L, .verbose = FALSE ) head(score_clusters(scores, cluster_labels = get_data(clusters)$res_1)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# aggregate the per cell ScType scores onto a clustering sc <- demo_single_cells() markers <- data.table::data.table( cell_type = rep(sprintf("cell_type_%i", 1:3), each = 10), gene_id = sprintf("gene_%02d", 1:30) ) cell_markers <- prepare_cell_markers(obj = sc, marker_df = markers) scores <- calc_sc_type_scores( sc, cell_marker_list = cell_markers, .verbose = FALSE ) clusters <- fast_cluster_sc( sc, resolutions = 1.0, n_centroids = 30L, .verbose = FALSE ) head(score_clusters(scores, cluster_labels = get_data(clusters)$res_1)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function implements the doublet detection from Scrublet, see Wolock, et al. Briefly, arteficial doublets are being generated from the data via random combination of initial cells. Highly variable genes (HVG) are being identified and the observed cells are being projected on a PCA space. Subsequently, the simulated doublets are being projected on the same PCA space given the same HVGs; kNN graphs are being generated and a kNN classifier is used to assign a probability that a given cell in the original data is a doublet. For more details, please check the publication.
scrublet_sc( object, scrublet_params = params_scrublet(), seed = 42L, streaming = NULL, cells_to_use = NULL, group_by = NULL, return_combined_pca = FALSE, return_pairs = FALSE, .verbose = TRUE )scrublet_sc( object, scrublet_params = params_scrublet(), seed = 42L, streaming = NULL, cells_to_use = NULL, group_by = NULL, return_combined_pca = FALSE, return_pairs = FALSE, .verbose = TRUE )
object |
|
scrublet_params |
A list with the final scrublet parameters, see
|
seed |
Integer. Random seed. |
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
cells_to_use |
Optional string. Names of the cells to use for the generation of the Scrublet. Useful when you wish to run doublet detection on individual batches within your data. The object returned will be specifically using these cells. |
group_by |
Optional grouping variable. Useful if you want to run the method on a per-sample basis. |
return_combined_pca |
Boolean. Shall the PCA of the observed cells and simulated doublets be returned. |
return_pairs |
Boolean. Shall the pairs be returned. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A scrublet_res class that has with the following items:
predicted_doublets - Boolean vector indicating which observed cells predicted as doublets (TRUE = doublet, FALSE = singlet).
doublet_scores_obs - Numerical vector with the likelihood of being a doublet for the observed cells.
doublet_scores_sim - Numerical vector with the likelihood of being a doublet for the simulated cells.
doublet_errors_obs - Numerical vector with the standard errors of the scores for the observed cells.
z_scores - Z-scores for the observed cells. Represents:
score - threshold / error.
threshold - Used threshold.
detected_doublet_rate - Fraction of cells that are called as doublet.
detectable_doublet_fraction - Fraction of simulated doublets with scores above the threshold.
overall_doublet_rate - Estimated overall doublet rate. Should roughly match the expected doublet rate.
pca - Optional PCA embeddings across the original cells and simulated doublets.
pair_1 - Optional index of the parent cell 1 of the simulated doublets.
pair_2 - Optional index of the parent cell 2 of the simulated doublets.
Wollock, et al., Cell Syst, 2020
# simulated doublet scoring on 500 synthetic cells sc <- demo_single_cells(prepped = FALSE) scrublet_sc( sc, scrublet_params = params_scrublet( pca = list(no_pcs = 10L), hvg = list(min_gene_var_pctl = 0.0), n_bins = 20L ), .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# simulated doublet scoring on 500 synthetic cells sc <- demo_single_cells(prepped = FALSE) scrublet_sc( sc, scrublet_params = params_scrublet( pca = list(no_pcs = 10L), hvg = list(min_gene_var_pctl = 0.0), n_bins = 20L ), .verbose = FALSE ) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Reverses the residual transform with every latent variable, the library size included, held at its median. The depth structure goes, the per-sample intercept stays.
The result is a new store on disk, not a layer on object. Its gene axis is
the model's, so it is narrower than the source and the indices do not line
up, which is why the observation and variable tables are rebuilt rather than
copied.
sct_corrected_counts_sc( object, dir_out = NULL, build_cell_store = TRUE, overwrite = FALSE, gene_batch_size = NULL, .verbose = TRUE )sct_corrected_counts_sc( object, dir_out = NULL, build_cell_store = TRUE, overwrite = FALSE, gene_batch_size = NULL, .verbose = TRUE )
object |
|
dir_out |
String or |
build_cell_store |
Boolean. Also write the |
overwrite |
Boolean. Overwrite an existing store in |
gene_batch_size |
Integer or |
.verbose |
Boolean or Integer. Controls verbosity. |
With build_cell_store = TRUE a new SingleCells over the
corrected counts, otherwise the path of the gene-major file, invisibly.
# corrected counts as a fresh object sc <- demo_single_cells(prepped = FALSE) sc <- fit_residuals_sc(sc, .verbose = FALSE) corrected <- sct_corrected_counts_sc(sc, .verbose = FALSE) dim(corrected) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# corrected counts as a fresh object sc <- demo_single_cells(prepped = FALSE) sc <- fit_residuals_sc(sc, .verbose = FALSE) corrected <- sct_corrected_counts_sc(sc, .verbose = FALSE) dim(corrected) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Writes the 2D position of every leaf, cell or metacell, into the object's embeddings, so the usual embedding plots can use it. The tree has to have been built over the object's current cells (or all of its metacells), in the same order.
set_bonsai_embedding(object, tree, name = "bonsai")set_bonsai_embedding(object, tree, name = "bonsai")
object |
|
tree |
A |
name |
String. Name of the embedding. |
The object with the embedding added.
# Bonsai leaf coordinates next to the other embeddings sc <- demo_single_cells( prepped = FALSE, syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 50L) ) tree <- bonsai_sc(sc, .verbose = FALSE) sc <- set_bonsai_embedding(sc, tree) get_available_embeddings(sc) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# Bonsai leaf coordinates next to the other embeddings sc <- demo_single_cells( prepped = FALSE, syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 50L) ) tree <- bonsai_sc(sc, .verbose = FALSE) sc <- set_bonsai_embedding(sc, tree) get_available_embeddings(sc) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Set/add the MAGIC imputed layer
set_magic(x, magic, ...) ## S3 method for class 'ScCache' set_magic(x, magic, ...) ## S7 method for class <bixverse::SingleCells> set_magic(x, magic, ...) ## S7 method for class <bixverse::SingleCellsSubset> set_magic(x, magic, ...)set_magic(x, magic, ...) ## S3 method for class 'ScCache' set_magic(x, magic, ...) ## S7 method for class <bixverse::SingleCells> set_magic(x, magic, ...) ## S7 method for class <bixverse::SingleCellsSubset> set_magic(x, magic, ...)
x |
An object to add the imputed layer to. |
magic |
|
... |
Other parameters. |
The object with the imputed layer attached.
# the imputed layer taken out and put back sc <- demo_single_cells() sc <- run_magic_sc(sc, features = get_gene_names(sc)[1:5], .verbose = FALSE) magic <- get_magic(sc) sc <- set_magic(remove_magic(sc), magic) dim(get_magic(sc)$data) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the imputed layer taken out and put back sc <- demo_single_cells() sc <- run_magic_sc(sc, features = get_gene_names(sc)[1:5], .verbose = FALSE) magic <- get_magic(sc) sc <- set_magic(remove_magic(sc), magic) dim(get_magic(sc)$data) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Set/add the fitted residual model
set_residual_fit(x, residual_fit, ...) ## S7 method for class <bixverse::MetaCells> set_residual_fit(x, residual_fit, ...) ## S3 method for class 'ScCache' set_residual_fit(x, residual_fit, ...) ## S7 method for class <bixverse::SingleCells> set_residual_fit(x, residual_fit, ...) ## S7 method for class <bixverse::SingleCellsSubset> set_residual_fit(x, residual_fit, ...)set_residual_fit(x, residual_fit, ...) ## S7 method for class <bixverse::MetaCells> set_residual_fit(x, residual_fit, ...) ## S3 method for class 'ScCache' set_residual_fit(x, residual_fit, ...) ## S7 method for class <bixverse::SingleCells> set_residual_fit(x, residual_fit, ...) ## S7 method for class <bixverse::SingleCellsSubset> set_residual_fit(x, residual_fit, ...)
x |
An object to add the fitted model to. |
residual_fit |
|
... |
Other parameters. |
The object with the fitted model attached.
# the fitted model taken out and put back sc <- demo_single_cells() sc <- fit_residuals_sc(sc, method = "analytic_pearson", .verbose = FALSE) fit <- get_residual_fit(sc) sc <- set_residual_fit(remove_residual_fit(sc), fit) get_residual_fit(sc)$method unlink(sc@dir_data, recursive = TRUE, force = TRUE)# the fitted model taken out and put back sc <- demo_single_cells() sc <- fit_residuals_sc(sc, method = "analytic_pearson", .verbose = FALSE) fit <- get_residual_fit(sc) sc <- set_residual_fit(remove_residual_fit(sc), fit) get_residual_fit(sc)$method unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Add a new column to the obs table
set_sc_new_obs_col(object, col_name, new_data)set_sc_new_obs_col(object, col_name, new_data)
object |
|
col_name |
String. The name of the column to add. |
new_data |
Atomic vector. The data to add to the column. Needs to be
of same length as |
The class with updated obs table in the DuckDB
# one new column, in the order of the cells that passed quality control sc <- demo_single_cells(prepped = FALSE) sc <- set_sc_new_obs_col( sc, col_name = "arm", new_data = rep(c("ctrl", "treated"), length.out = dim(sc)[1]) ) table(get_sc_obs(sc)$arm) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# one new column, in the order of the cells that passed quality control sc <- demo_single_cells(prepped = FALSE) sc <- set_sc_new_obs_col( sc, col_name = "arm", new_data = rep(c("ctrl", "treated"), length.out = dim(sc)[1]) ) table(get_sc_obs(sc)$arm) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Add multiple new columns to the obs table
set_sc_new_obs_col_multiple(object, new_data)set_sc_new_obs_col_multiple(object, new_data)
object |
|
new_data |
Named list. The names will be the column names and the
elements will be added to the obs table. Needs to be of same length as
|
The class with updated obs table in the DuckDB
# several columns in one go sc <- demo_single_cells(prepped = FALSE) n_cells <- dim(sc)[1] sc <- set_sc_new_obs_col_multiple( sc, new_data = list( arm = rep(c("ctrl", "treated"), length.out = n_cells), donor = rep(sprintf("donor_%i", 1:4), length.out = n_cells) ) ) head(get_sc_obs(sc)[, c("arm", "donor")], 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# several columns in one go sc <- demo_single_cells(prepped = FALSE) n_cells <- dim(sc)[1] sc <- set_sc_new_obs_col_multiple( sc, new_data = list( arm = rep(c("ctrl", "treated"), length.out = n_cells), donor = rep(sprintf("donor_%i", 1:4), length.out = n_cells) ) ) head(get_sc_obs(sc)[, c("arm", "donor")], 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Add a new column to the var table
set_sc_new_var_cols(object, data_list)set_sc_new_var_cols(object, data_list)
object |
|
data_list |
Named list with the data to add. |
The class with updated var table in the DuckDB
# flag a couple of genes in the var table sc <- demo_single_cells(prepped = FALSE) sc <- set_sc_new_var_cols( sc, data_list = list( is_marker = get_gene_names(sc) %in% c("gene_01", "gene_02") ) ) head(get_sc_var(sc), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# flag a couple of genes in the var table sc <- demo_single_cells(prepped = FALSE) sc <- set_sc_new_var_cols( sc, data_list = list( is_marker = get_gene_names(sc) %in% c("gene_01", "gene_02") ) ) head(get_sc_var(sc), 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Renames the columns in the obs or var table of single cell-related classes.
setnames_sc(object, table = c("obs", "var"), old, new)setnames_sc(object, table = c("obs", "var"), old, new)
object |
|
table |
String. One of |
old |
Character vector. The old column names. |
new |
Character vector. The new column names. |
Invisible self
# rename a column in the obs table sc <- demo_single_cells(prepped = FALSE) sc <- setnames_sc(sc, table = "obs", old = "cell_grp", new = "cell_type") head(get_sc_obs(sc)$cell_type, 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# rename a column in the obs table sc <- demo_single_cells(prepped = FALSE) sc <- setnames_sc(sc, table = "obs", old = "cell_grp", new = "cell_type") head(get_sc_obs(sc)$cell_type, 3) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function implements the canonical correlation analysis (CCA) anchor integration from Stuart, et al. For each pair of batches a shared CCA embedding is computed, mutual nearest neighbours in that embedding become anchors, these are filtered in gene space, scored by shared neighbours and finally used to apply a kernel-weighted correction on the union PCA embedding. Batches are merged in the order of their pairwise anchor counts.
This port deviates from Seurat in two places. It skips the per-gene
ScaleData step and works from per-cell standardised log-normalised HVG
expression, and it never materialises the N1 x N2 cross-product (the
canonical correlations come from a matrix-free randomised SVD). The
correction runs on the embedding, not on full log-expression. In practice
the anchor structure comes out close to identical at a fraction of the
memory.
seurat_cca_sc( object, batch_column, batch_hvg_genes, cca_params = params_sc_seurat_cca(), use_precomputed_pca = FALSE, seed = 42L, .verbose = TRUE )seurat_cca_sc( object, batch_column, batch_hvg_genes, cca_params = params_sc_seurat_cca(), use_precomputed_pca = FALSE, seed = 42L, .verbose = TRUE )
object |
|
batch_column |
String. The column with the batch information in the obs data of the class. |
batch_hvg_genes |
Integer vector. These are the highly variable genes,
identified by a batch-aware method. Please refer to
|
cca_params |
A list, please see
|
use_precomputed_pca |
Boolean. Should the PCA in the object be used if found. If you decide to do this, make sure that you have run the PCA on the batch-aware HVG ideally. Note that CCA still needs the PCA loadings for the gene-space filter, so this saves less work than it does for fastMNN. |
seed |
Integer. Random seed. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The object with the added "cca" embedding.
Stuart, et al., Cell, 2019
# CCA anchor integration over batch aware highly variable genes sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) hvg <- find_hvg_batch_aware_sc( sc, hvg_no = 20L, batch_column = "batch_index", .verbose = FALSE ) sc <- seurat_cca_sc( sc, batch_column = "batch_index", batch_hvg_genes = hvg$hvg_gene_idx, cca_params = params_sc_seurat_cca(num_cc = 10L, dims = 10L), .verbose = FALSE ) dim(get_embedding(sc, "cca")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# CCA anchor integration over batch aware highly variable genes sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) hvg <- find_hvg_batch_aware_sc( sc, hvg_no = 20L, batch_column = "batch_index", .verbose = FALSE ) sc <- seurat_cca_sc( sc, batch_column = "batch_index", batch_hvg_genes = hvg$hvg_gene_idx, cca_params = params_sc_seurat_cca(num_cc = 10L, dims = 10L), .verbose = FALSE ) dim(get_embedding(sc, "cca")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function implements the reciprocal PCA (rPCA) anchor integration from
Stuart, et al. It runs the same anchor pipeline as
seurat_cca_sc() but builds a cheaper per-pair anchor space: each
batch keeps its own PCA basis and the other batch's HVG expression is
projected into it. Cross-batch mutual nearest neighbours are then found in
these projected bases.
rPCA is faster than CCA and corrects less aggressively, which makes it the safer choice when batches share most of their cell types. As in Seurat, no gene-space anchor filter is applied, that step is CCA-only.
seurat_rpca_sc( object, batch_column, batch_hvg_genes, rpca_params = params_sc_seurat_rpca(), use_precomputed_pca = FALSE, seed = 42L, .verbose = TRUE )seurat_rpca_sc( object, batch_column, batch_hvg_genes, rpca_params = params_sc_seurat_rpca(), use_precomputed_pca = FALSE, seed = 42L, .verbose = TRUE )
object |
|
batch_column |
String. The column with the batch information in the obs data of the class. |
batch_hvg_genes |
Integer vector. These are the highly variable genes,
identified by a batch-aware method. Please refer to
|
rpca_params |
A list, please see
|
use_precomputed_pca |
Boolean. Should the PCA in the object be used if found. This only applies to the union PCA that gets corrected, the per-batch PCAs are always recomputed because rPCA needs them. |
seed |
Integer. Random seed. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The object with the added "rpca" embedding.
Stuart, et al., Cell, 2019
# reciprocal PCA anchor integration, the cheaper sibling of CCA sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) hvg <- find_hvg_batch_aware_sc( sc, hvg_no = 20L, batch_column = "batch_index", .verbose = FALSE ) sc <- seurat_rpca_sc( sc, batch_column = "batch_index", batch_hvg_genes = hvg$hvg_gene_idx, rpca_params = params_sc_seurat_rpca(dims = 10L), .verbose = FALSE ) dim(get_embedding(sc, "rpca")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# reciprocal PCA anchor integration, the cheaper sibling of CCA sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 600L, n_genes = 50L, n_batches = 3L ) ) hvg <- find_hvg_batch_aware_sc( sc, hvg_no = 20L, batch_column = "batch_index", .verbose = FALSE ) sc <- seurat_rpca_sc( sc, batch_column = "batch_index", batch_hvg_genes = hvg$hvg_gene_idx, rpca_params = params_sc_seurat_rpca(dims = 10L), .verbose = FALSE ) dim(get_embedding(sc, "rpca")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This class is designed to generate and store the adjacency matrices for subsequent usage in similarity network fusion. There are methods to add different types of adjacency matrices (pending the type of data, it can be continuous, categorical and/or mixed). Subsequently, there are methods to generate the final network.
SimilarityNetworkFusion( data = NULL, data_name = NULL, snf_params = params_snf() )SimilarityNetworkFusion( data = NULL, data_name = NULL, snf_params = params_snf() )
data |
Optional data to already transform into adjacency data. Any data supplied will be assumed to be samples x features. The provided data taype can be a data.table (for categorical and/or mixed types) or a matrix (for continous types). If you provide a data.table, the function will assume the first column are the sample identifiers. Please ensure that setting. |
data_name |
Optional string. Name of the data modality. |
snf_params |
List. The SNF parameters, see
The parameters will be internally stored for subsequent usage in other functions. |
Returns the SimilarityNetworkFusion class for further
operations.
A list containing the processed adjacency matrices.
Matrix. The final adjacency of the fused network.
A (nested) list that will store all the parameters of the applied function.
data.table. Contains final results.
# continuous modality turned into an affinity matrix set.seed(42) continuous <- matrix(rnorm(120), nrow = 12, ncol = 10) rownames(continuous) <- sprintf("sample_%02i", 1:12) colnames(continuous) <- sprintf("feature_%i", 1:10) object <- SimilarityNetworkFusion( data = continuous, data_name = "continuous", snf_params = params_snf(k = 3L) ) dim(get_snf_adjcacency_mat(object, "continuous"))# continuous modality turned into an affinity matrix set.seed(42) continuous <- matrix(rnorm(120), nrow = 12, ncol = 10) rownames(continuous) <- sprintf("sample_%02i", 1:12) colnames(continuous) <- sprintf("feature_%i", 1:10) object <- SimilarityNetworkFusion( data = continuous, data_name = "continuous", snf_params = params_snf(k = 3L) ) dim(get_snf_adjcacency_mat(object, "continuous"))
This function provides an interface to simplify overenrichment results based on ontological information of the pathway origin (typical use case is to simplify gene ontology results). To do so, the function will calculate the Wang similarity and keep within a set of highly similar terms the one with the lowest fdr. Should there be terms with the same fdr, the function will keep the most specific term within the ontology.
simplify_hypergeom_res(res, parent_child_dt, weights, min_sim = 0.7)simplify_hypergeom_res(res, parent_child_dt, weights, min_sim = 0.7)
res |
data.table. The enrichment results. Needs to have the columns
|
parent_child_dt |
data.table. The data.table with column parent and child. You also need to have a type column for the Wang similarity to provide the weights for the relationships. |
weights |
Named numeric. The relationship of type to weight for this
specific edge. For example |
min_sim |
Float between 0 and 1. The minimum similarity that the terms need to have. |
data.table with enrichment results.
# collapse redundant terms of a toy ontology by Wang similarity onto <- data.table::data.table( parent = c("a", "b", "b", "c"), child = c("b", "c", "d", "e"), type = c("is_a", "is_a", "part_of", "is_a") ) res <- data.table::data.table( gene_set_name = c("b", "c", "d", "e"), fdr = c(0.001, 0.001, 0.4, 0.02) ) simplify_hypergeom_res( res, onto, weights = c("is_a" = 0.8, "part_of" = 0.6), min_sim = 0.6 )# collapse redundant terms of a toy ontology by Wang similarity onto <- data.table::data.table( parent = c("a", "b", "b", "c"), child = c("b", "c", "d", "e"), type = c("is_a", "is_a", "part_of", "is_a") ) res <- data.table::data.table( gene_set_name = c("b", "c", "d", "e"), fdr = c(0.001, 0.001, 0.4, 0.02) ) simplify_hypergeom_res( res, onto, weights = c("is_a" = 0.8, "part_of" = 0.6), min_sim = 0.6 )
This function induces Splatter-style sequencing-depth sparsity on the data.
Per sample a size factor s_j ~ LogNormal(0, capture_efficiency_sigma) is
drawn, giving a target library size of target_library_size * s_j. Each gene
is then binomially thinned to approach that target, so dropout falls out of
the library size rather than an explicit per-gene dropout curve. Retention
probability is capped at 1, meaning samples already below their target are
left alone rather than upsampled.
simulate_dropouts(object, sparsity_params = params_bulk_sparsity())simulate_dropouts(object, sparsity_params = params_bulk_sparsity())
object |
The |
sparsity_params |
List. The sparsification parameters, see
|
synthetic_bulk_data with added sparse data.
Zappia, et al., Genome Biol, 2017
# thin the counts down to a shallower library size syn <- synthetic_bulk_cor_matrix() syn <- simulate_dropouts(syn, params_bulk_sparsity()) mean(syn$counts == 0) mean(syn$sparse_counts == 0)# thin the counts down to a shallower library size syn <- synthetic_bulk_cor_matrix() syn <- simulate_dropouts(syn, params_bulk_sparsity()) mean(syn$counts == 0) mean(syn$sparse_counts == 0)
A class for handling single cell count data stored on disk in two
complementary binary representations: a CSR-like layout (
f_path_cells)
for fast cell-wise access and a CSC-like layout (f_path_genes) for fast
gene-wise access. Both raw counts and log-normalised counts are stored
side by side. Provides methods for ingesting data from R, h5ad, mtx
and 10x CellRanger h5 sources (including multi-file workflows),
converting between layouts, retrieving slices of the matrix, merging
existing binary objects and writing CellSweep-denoised counts.
f_path_cells |
( |
f_path_genes |
( |
n_cells |
( |
n_genes |
( |
A new instance of the SingleCellCountData class.
new
Create a new instance of the class
f_path_cells(character)
Path to the .bin file for the cell-based representation.
f_path_genes(character)
Path to the .bin file for the gene-based representation.
A new SingleCellCountData instance with n_cells and
n_genes initialised to zero.
get_shape
Get the shape of the matrix
An integer vector c(n_cells, n_genes).
set_from_file
Populate n_cells and n_genes from the cells binary file
Reads the header of the file at f_path_cells and updates the
n_cells and n_genes fields accordingly. Useful when reconnecting
to an existing object on disk.
Invisible NULL.
r_data_to_file
Write a CSR matrix from R to the cells binary file
r_data(list)
A named list convertible into CompressedSparseData2. Must contain "indptr", "indices" (0-indexed), "data", "nrow", "ncol" and "cs_type" ("csr" or "csc").
qc_params(list)
Quality control parameters parseable into MinCellQuality.
verbose(logical)
Controls verbosity of the function.
Ingest a sparse matrix passed in from R, apply per-cell QC, and write
the result to f_path_cells.
A list with cell_indices and gene_indices (0-indexed,
surviving QC), lib_size and nnz.
h5ad_to_file
Write an h5ad file to the cells binary file
cs_type(character)
Storage layout of the h5ad data. One of "CSC" or "CSR".
h5_path(character)
Path to the h5ad file.
no_cells(integer)
Number of cells in the h5ad file.
no_genes(integer)
Number of genes in the h5ad file.
qc_params(list)
Quality control parameters parseable into MinCellQuality.
slot(character)
Where to find the raw counts. One of "X", "raw.X" (or "raw", for CellXGene data) or "layers.counts". Unmatched values fall back to "X".
verbose(logical)
Controls verbosity of the function.
A list with cell_indices and gene_indices (0-indexed,
surviving QC), lib_size and nnz.
norm_h5ad_to_file
Write an h5ad file with normalised counts to the cells binary file
cs_type(character)
Storage layout of the h5 data. One of "CSC" or "CSR".
h5_path(character)
Path to the h5 file.
no_cells(integer)
Number of cells in the h5 file.
no_genes(integer)
Number of genes in the h5 file.
obs_lib_size_col(character)
Name of the obs column containing total counts per cell (e.g. "nCount_RNA").
target_size(numeric)
Target size used in the original normalisation (e.g. 1e4).
qc_params(list)
Quality control parameters parseable into MinCellQuality.
verbose(logical)
Controls verbosity of the function.
For data sets where only normalised counts are available in X.
Reads library sizes from a specified obs column to reconstruct raw
counts before writing.
A list with cell_indices and gene_indices (0-indexed,
surviving QC), lib_size and nnz.
h5ad_to_file_streaming
Write an h5ad file to disk using streaming
cs_type(character)
Storage layout of the h5 data. One of "CSC" or "CSR".
h5_path(character)
Path to the h5 file.
no_cells(integer)
Number of cells in the h5 file.
no_genes(integer)
Number of genes in the h5 file.
qc_params(list)
Quality control parameters parseable into MinCellQuality.
slot(character)
Where to find the raw counts. One of "X", "raw.X" (or "raw", for CellXGene data) or "layers.counts". Unmatched values fall back to "X".
verbose(logical)
Controls verbosity of the function.
Slower but lighter on memory than h5ad_to_file; streams the input
where possible.
A list with cell_indices and gene_indices (0-indexed,
surviving QC), lib_size and nnz.
multi_h5ad_to_file
Load multiple h5ad files into a single binary
file_tasks(list)
A list of lists, each produced by the R prescan function. Each inner list must contain exp_id, h5_path, cs_type, no_cells, no_genes and gene_local_to_universe (0-indexed integer vector, NA for unmapped genes), plus an optional raw_slot.
universe_size(integer)
Total number of genes in the universe.
qc_params(list)
Quality control parameters (min_unique_genes, min_lib_size, min_cells, target_size).
verbose(logical)
Controls verbosity.
A list with global_gene_indices, total_cells,
total_genes and per_file (a list of lists with exp_id,
cell_indices (file-local, 0-indexed), lib_size, nnz).
mtx_to_file
Write an mtx file to the cells binary file
mtx_path(character)
Path to the mtx file.
qc_params(list)
Quality control parameters parseable into MinCellQuality.
cells_as_rows(logical)TRUE if cells are rows in the mtx file, FALSE if cells are columns.
verbose(logical)
Controls verbosity of the function.
A list with cell_indices and gene_indices (0-indexed,
surviving QC), lib_size and nnz.
mtx_to_file_streaming
Write an mtx file to the cells binary file using streaming
mtx_path(character)
Path to the mtx file.
qc_params(list)
Quality control parameters parseable into MinCellQuality.
cells_as_rows(logical)TRUE if cells are rows in the mtx file, FALSE if cells are columns.
verbose(logical)
Controls verbosity of the function.
A list with cell_indices and gene_indices (0-indexed,
surviving QC), lib_size and nnz.
multi_mtx_to_file
Load multiple mtx files into a single binary
file_tasks(list)
A list of lists, each containing exp_id, mtx_path, cells_as_rows and gene_local_to_universe (integer vector, NA for unmapped genes).
universe_size(integer)
Number of genes in the intersection universe.
qc_params(list)
Quality control parameters parseable into MinCellQuality.
verbose(logical)
Controls verbosity.
A list with global_gene_indices, total_cells,
total_genes and per_file (a list of lists with exp_id,
cell_indices (file-local, 0-indexed), lib_size, nnz).
tenx_h5_to_file_streaming
Write a 10x CellRanger h5 file to the cells binary file using streaming
h5_path(character)
Path to the 10x h5 file.
version(character)
One of "auto", "v2" or "v3". "auto", and any unmatched value, detects the layout from the file.
no_cells(integer)
Number of cells (columns) in the file.
no_genes(integer)
Number of features (rows), including all modalities.
qc_params(list)
Quality control parameters parseable into MinCellQuality.
feature_type(character or NULL)
Target modality for v3. Defaults to "Gene Expression" when NULL.
verbose(logical)
Controls verbosity of the function.
Ingests the gene-expression modality from a CellRanger v2/v3 h5 file.
Other modalities (e.g. Antibody Capture) are filtered out via
feature_type.
A list with cell_indices and gene_indices (0-indexed,
surviving QC), lib_size and nnz.
multi_tenx_h5_to_file
Load multiple 10x CellRanger h5 files into a single binary
file_tasks(list)
A list of lists, each produced by the R prescan function. Each inner list must contain exp_id, h5_path, version ("v2" or "v3"), no_cells, no_genes, gene_local_to_universe (integer vector, NA for unmapped / non-gene features) and feature_type (optional string, defaults to "Gene Expression").
universe_size(integer)
Total number of genes in the universe.
qc_params(list)
Quality control parameters (min_unique_genes, min_lib_size, min_cells, target_size).
verbose(logical)
Controls verbosity.
A list with global_gene_indices, total_cells, total_genes
and per_file (a list of lists with exp_id, cell_indices
(file-local, 0-indexed), lib_size, nnz).
return_full_mat
Return the full count matrix
assay(character)
One of "raw" or "norm". Selects whether raw counts or log-normalised counts are returned.
cell_based(logical)
If TRUE, the data is returned in CSR layout (cells as rows). If FALSE, the data is returned in CSC layout (genes as columns).
verbose(logical)
Controls verbosity of the function.
A list with indptr, indices (0-indexed), data,
no_cells and no_genes, parseable into a sparse matrix in R. data
is integer for "raw" and double for "norm".
get_cells_by_indices
Return cells by index positions
indices(integer)
The cell indices to return (1-indexed).
assay(character)
One of "raw" or "norm".
Leverages the CSR-stored data for fast cell retrieval.
A list with indptr, indices (0-indexed gene positions),
data, no_cells (number of returned cells) and no_genes,
parseable into a CSR matrix in R.
generate_gene_based_data
Generate gene-based data from the cells binary file
max_mem_gb(numeric or NULL)
Memory for the conversion buffers in GB, at 10 bytes per non-zero. NULL converts in a single phase.
verbose(logical)
Controls verbosity of the function.
Reads the .bin file at f_path_cells and writes a gene-friendly
(CSC) representation to f_path_genes. A parallel counting-sort
transpose: one pass counts the non-zeros per gene, then genes are
converted in phases that fit max_mem_gb. Each phase re-reads the
cell file, so a tighter budget trades time for memory.
Invisible NULL.
archive
Archive the cell-based binary for cold storage
f_path_archive(character)
Path of the archive to write.
level(integer)
zstd compression level, 1 to 22.
verbose(logical)
Controls verbosity of the function.
Writes a zstd-compressed archive of f_path_cells. The gene-based
file is not archived; restore_archive() rebuilds it. Normalised
values are only stored for cells where they cannot be recomputed from
the raw counts.
A list with n_cells, nnz, n_norm_stored and
archive_bytes.
restore_archive
Restore both binaries from an archive
f_path_archive(character)
Path to the archive.
max_mem_gb(numeric or NULL)
Memory for the gene file conversion buffers in GB. NULL converts in a single phase.
verbose(logical)
Controls verbosity of the function.
Rebuilds f_path_cells from the archive, then generates
f_path_genes from it.
Invisible NULL.
get_genes_by_indices
Return genes by index positions
indices(integer)
The gene indices to return (1-indexed).
assay(character)
One of "raw" or "norm".
Leverages the CSC-stored data for fast gene retrieval.
A list with indptr, indices (0-indexed cell positions),
data, no_cells and no_genes (number of returned genes),
parseable into a CSC matrix in R.
get_nnz_genes
Get the number of cells expressing each gene
gene_indices(integer or NULL)
Optional 1-indexed gene indices. If NULL, results are returned for all genes.
An integer vector of NNZ counts for the requested genes.
merge_sc_files
Merge multiple existing bin files into the cells binary file
merge_tasks(list)
A list of lists. Each inner list must contain exp_id, bin_cells_path, cells_to_keep (0-indexed integer vector) and gene_local_to_universe (0-indexed integer vector, NA for genes absent from the universe).
universe_size(integer)
Number of genes in the intersection universe.
renormalise(logical)
If TRUE, recompute data_norm against target_size using each cell's surviving raw counts. If FALSE, pass data_norm through untouched; the caller must guarantee all inputs were normalised against the same target_size.
target_size(numeric)
Target library size for renormalisation. Ignored when renormalise = FALSE.
verbose(logical)
Controls verbosity.
A list with total_cells, total_genes and per_file (a
list of lists with exp_id, lib_size, nnz).
cellsweep
Run CellSweep and write the denoised barcodes into the cells binary
One independent EM fit per sample, since the ambient profile is a property of a single emulsion. Only the real barcodes are written: the empty droplets exist to train the ambient profile, and barcodes that are neither empty nor annotated are not part of the model.
Writes the cell-based file only; regenerate the gene-based companion afterwards, as for a merge.
f_path_source(character)
Path to the raw counts_cells.bin, which must still contain the empty droplets.
samples(list)
A list of lists. Each inner list must contain sample_id, real_cells and empty_cells (0-indexed integer vectors of store indices), celltype_idx (0-indexed integer vector, one entry per real_cells entry) and n_celltypes.
cellsweep_params(list)
The CellSweep model parameters. Missing entries fall back to the reference implementation's defaults.
target_size(numeric)
Library size the normalised layer is scaled to.
verbose(integer)0 silent, 1 per-sample progress, 2 per-EM-iteration.
A list with cell_order (0-indexed source indices in output
order), lib_size, nnz and fits (one list per sample with
sample_id, alpha, z_hat (1-indexed), beta, ambient,
celltype_profiles, n_celltypes, log_likelihood, n_iter and
converged).
This is the bixverse-based SingleCells class. Under the hood it uses a
DuckDB for obs and vars storing, and a Rust-based binarised file format to
store the raw and normalised counts. In both cases, the idea is not to hold
any data that is not needed at a given point of time in memory, but leverage
speedy on-disk computations and streaming engines powered by Rust and DuckDB
to run the analysis. This version is specifically designed for single cell
RNAseq. If you want to use multi-modal data, please refer to
SingleCellsMultiModal() - this class can store multiple layers of 'omics.
SingleCells(dir_data)SingleCells(dir_data)
dir_data |
String. This is the directory in which the experimental files will be |
Returns the SingleCells class for further operations.
This contains an R6 class with DuckDB pointers and wrappers to interact with the table-like data for this experiment.
This contains an R6-like environment that points to Rust functions that can work on the counts more specifically.
Path to the directory in which the data will be saved on disk.
Class with cached data. Contains less memory-heavy objects such as embeddings, kNN information or sNN graphs.
Class containing various mapping information such as HVG indices, cells to keep, etc.
Dimensions of the original data.
# demo_single_cells() wraps the construction and the ingestion sc <- demo_single_cells(prepped = FALSE) sc unlink(sc@dir_data, recursive = TRUE, force = TRUE)# demo_single_cells() wraps the construction and the ingestion sc <- demo_single_cells(prepped = FALSE) sc unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This is the bixverse-based SingleCells class for multiple modalities. Under
the hood it uses a DuckDB for obs and vars storing, and a Rust-based
binarised file format to store the raw and normalised counts for single cell
RNAseq. In both cases, the idea is not to hold any data that is not needed at
a given point of time in memory, but leverage speedy on-disk computations and
streaming engines powered by Rust and DuckDB to run the analysis.
SingleCellsMultiModal(dir_data)SingleCellsMultiModal(dir_data)
dir_data |
String. This is the directory in which the experimental files will be |
Returns the SingleCellsMultiModal class for further operations.
This contains an R6 class with DuckDB pointers and wrappers to interact with the table-like data for this experiment.
This contains an R6-like environment that points to Rust functions that can work on the RNAseq counts more specifically.
...
Future feature: to store ATAC Seq counts in the future.
Path to the directory in which the data will be saved on disk.
Class with cached data. Contains less memory-heavy objects such as embeddings, kNN information or sNN graphs for the single cell RNAseq.
Class with cached data. Contains less memory-heavy objects such as embeddings, kNN information or sNN graphs for the single cell Antibody-Derived Tags.
Class with cached data. Contains less memory-heavy objects such as embeddings, kNN information or sNN graphs for the single cell chromatin accessability.
Class containing various mapping information such as HVG indices, cells to keep, etc.
List that contains additional data and results, such as for example the WNN graph.
Dimensions of the original data.
# an empty multi-modal handle with the RNA modality ingested into it rna <- generate_single_cell_test_data() dir <- tempfile("bixverse_mm") dir.create(dir) object <- load_r_data( SingleCellsMultiModal(dir_data = dir), counts = rna$counts, obs = rna$obs, var = rna$var, sc_qc_param = params_sc_min_quality(min_unique_genes = 5L), .verbose = FALSE ) object unlink(dir, recursive = TRUE, force = TRUE)# an empty multi-modal handle with the RNA modality ingested into it rna <- generate_single_cell_test_data() dir <- tempfile("bixverse_mm") dir.create(dir) object <- load_r_data( SingleCellsMultiModal(dir_data = dir), counts = rna$counts, obs = rna$obs, var = rna$var, sc_qc_param = params_sc_min_quality(min_unique_genes = 5L), .verbose = FALSE ) object unlink(dir, recursive = TRUE, force = TRUE)
Subset view onto a SingleCells() object, restricted to cells
belonging to a single level of a grouping variable. The Rust count
connection is shared with the parent (no data copy). obs_table and
var_table are held in memory; sc_map is rebuilt to point only at the
subset cells but stays in the original index space so Rust calls remain
valid without further translation.
SingleCellsSubset(sc_object, grouping_column, group)SingleCellsSubset(sc_object, grouping_column, group)
sc_object |
A |
grouping_column |
String. Column in the obs table that defines the grouping. |
group |
String. Level of |
A SingleCellsSubset object.
Shared Rust pointer to the on-disk counts.
Directory holding the binary count files.
Subset obs (rows for the chosen group only).
cell_idx keeps the original 1-indexed position in the parent.
Variable/feature table (unchanged from parent).
Column in obs used to define the subset.
Value of grouping_column represented by this subset.
Fresh ScCache for subset-specific PCA, kNN, sNN,
embeddings.
ScMap restricted to the subset cells. cell_mapping
stays 1-indexed and cells_to_keep_idx stays 0-indexed, both in the
original parent index space.
Integer vector. 1-indexed original cell
positions, in subset row order. subset_to_original[i] is the parent
position of subset row i.
c(n_cells_subset, n_genes).
# a view onto one cell type, sharing the parent's counts on disk sc <- demo_single_cells(prepped = FALSE) subset_obj <- SingleCellsSubset( sc_object = sc, grouping_column = "cell_grp", group = "cell_type_1" ) dim(subset_obj) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# a view onto one cell type, sharing the parent's counts on disk sc <- demo_single_cells(prepped = FALSE) subset_obj <- SingleCellsSubset( sc_object = sc, grouping_column = "cell_grp", group = "cell_type_1" ) dim(subset_obj) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Runs n_runs HALS-NMF fits with random initialisations and returns the run
with the lowest final reconstruction loss as the primary result, along with
the per-run losses and convergence flags. Useful when the objective is
known to have multiple local minima. Uses f64 precision.
stabilised_nmf_bulk( object, k, n_runs = 30L, preprocessing = c("none", "sd", "sqrt_sd"), nmf_hals_params = params_nmf_hals(), membership_params = params_module_membership(), seed = 42L, .verbose = TRUE )stabilised_nmf_bulk( object, k, n_runs = 30L, preprocessing = c("none", "sd", "sqrt_sd"), nmf_hals_params = params_nmf_hals(), membership_params = params_module_membership(), seed = 42L, .verbose = TRUE )
object |
The class, see |
k |
Integer. Number of latent factors (modules) per run. |
n_runs |
Integer. Number of random restarts. |
preprocessing |
String. One of |
nmf_hals_params |
List. Output of |
membership_params |
List. Controls how the gene loadings are turned into
module membership, see |
seed |
Integer. Base random seed. |
.verbose |
Boolean or integer |
The class with final_results populated from the best run (lowest
final loss) plus stability diagnostics (losses, converged, best_idx,
w_all_runs, h_per_run).
# ten random restarts, best run kept syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- stabilised_nmf_bulk(obj, k = 4L, n_runs = 10L, .verbose = FALSE) get_nmf_stability(obj)$losses# ten random restarts, best run kept syn <- generate_gene_module_data(n_samples = 24L, n_genes = 60L) # NMF needs a non-negative matrix mat <- syn$data - min(syn$data) obj <- BulkCoExp(mat, syn$meta_data) obj <- preprocess_bulk_coexp( obj, hvg = NULL, scaling = FALSE, .verbose = FALSE ) obj <- stabilised_nmf_bulk(obj, k = 4L, n_runs = 10L, .verbose = FALSE) get_nmf_stability(obj)$losses
Runs n_runs HALS NMF with random initialisations seeded by seed + i.
The nmf_init field in nmf_hals_params is ignored; random init is
always used.
stabilised_nmf_sc( object, k, cell_ids = NULL, gene_ids = NULL, preprocessing = "none", use_second_layer = TRUE, nmf_hals_params = params_nmf_hals(), n_runs = 30L, seed = 42L, .verbose = TRUE )stabilised_nmf_sc( object, k, cell_ids = NULL, gene_ids = NULL, preprocessing = "none", use_second_layer = TRUE, nmf_hals_params = params_nmf_hals(), n_runs = 30L, seed = 42L, .verbose = TRUE )
object |
|
k |
Integer. Number of latent factors to return. |
cell_ids |
Optional character. Cell ids (or meta cell ids) to restrict
the NMF to. If |
gene_ids |
Optional character. Gene ids to restrict the NMF to. If
|
preprocessing |
String. One of |
use_second_layer |
Boolean. If |
nmf_hals_params |
List, see |
n_runs |
Integer. Number of random restarts. |
seed |
Integer. Random seed for initialisation. |
.verbose |
Boolean or integer. Verbosity. |
A StabilisedNmfResult object.
# five random restarts, the best one reported sc <- demo_single_cells() res <- stabilised_nmf_sc(sc, k = 3L, n_runs = 5L, .verbose = FALSE) res unlink(sc@dir_data, recursive = TRUE, force = TRUE)# five random restarts, the best one reported sc <- demo_single_cells() res <- stabilised_nmf_sc(sc, k = 3L, n_runs = 5L, .verbose = FALSE) res unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Wraps bbknn_sc() as an ScStep.
step_bbknn_sc( batch_column, no_neighbours_to_keep = 5L, embd_to_use = "pca", no_embd_to_use = NULL, bbknn_params = params_sc_bbknn(), seed = 42L, .verbose = TRUE )step_bbknn_sc( batch_column, no_neighbours_to_keep = 5L, embd_to_use = "pca", no_embd_to_use = NULL, bbknn_params = params_sc_bbknn(), seed = 42L, .verbose = TRUE )
batch_column |
String. The column with the batch information in the obs data of the class. |
no_neighbours_to_keep |
Integer. Maximum number of neighbours to keep
from the BBKNN algorithm. Due to generating neighbours for each batch, there
might be a large number of generated neighbours. This will only keep the top
|
embd_to_use |
String. The embedding to use. Atm, the only option is
|
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
bbknn_params |
A list, please see
|
seed |
Integer. Random seed. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
An ScStep.
# BBKNN replaces the graph outright, so nothing follows it step_pca_sc(no_pcs = 10L) %>>% step_bbknn_sc(batch_column = "batch_index")# BBKNN replaces the graph outright, so nothing follows it step_pca_sc(no_pcs = 10L) %>>% step_bbknn_sc(batch_column = "batch_index")
Wraps find_clusters_sc() as an ScStep.
step_clusters_sc( cluster_algorithm = c("leiden", "louvain"), res = 1, name = "leiden_clustering", modality = c("rna", "adt", "wnn"), seed = 42L )step_clusters_sc( cluster_algorithm = c("leiden", "louvain"), res = 1, name = "leiden_clustering", modality = c("rna", "adt", "wnn"), seed = 42L )
cluster_algorithm |
String. One of |
res |
Numeric. The modularity resolution parameter, see
|
name |
String. The name to add to the obs table in the DuckDB. |
modality |
String. On which modality to run the UMAP. One of
|
seed |
Integer. For reproducibility. |
An ScStep.
# Leiden over the graph the neighbours step wrote step_neighbours_sc() %>>% step_clusters_sc(res = 0.5)# Leiden over the graph the neighbours step wrote step_neighbours_sc() %>>% step_clusters_sc(res = 0.5)
Wraps fast_mnn_sc() as an ScStep.
step_fast_mnn_sc( batch_column, batch_hvg_genes, fastmnn_params = params_sc_fastmnn(), use_precomputed_pca = FALSE, seed = 42L, .verbose = TRUE )step_fast_mnn_sc( batch_column, batch_hvg_genes, fastmnn_params = params_sc_fastmnn(), use_precomputed_pca = FALSE, seed = 42L, .verbose = TRUE )
batch_column |
String. The column with the batch information in the obs data of the class. |
batch_hvg_genes |
Integer vector. These are the highly variable genes,
identified by a batch-aware method. Please refer to
|
fastmnn_params |
A list, please see
|
use_precomputed_pca |
Boolean. Should the PCA in the object be used if found. If you decide to do this, make sure that you have run the PCA on the batch-aware HVG ideally. |
seed |
Integer. Random seed. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
An ScStep.
# fastMNN wants the batch aware genes handed to it up front step_fast_mnn_sc( batch_column = "batch_index", batch_hvg_genes = 0:29L ) %>>% step_neighbours_sc(embd_to_use = "mnn")# fastMNN wants the batch aware genes handed to it up front step_fast_mnn_sc( batch_column = "batch_index", batch_hvg_genes = 0:29L ) %>>% step_neighbours_sc(embd_to_use = "mnn")
Wraps harmony_sc() as an ScStep.
step_harmony_sc( batch_column, additional_batch_columns = NULL, modality = c("rna", "adt"), harmony_params = params_sc_harmony(), seed = 42L, .verbose = TRUE )step_harmony_sc( batch_column, additional_batch_columns = NULL, modality = c("rna", "adt"), harmony_params = params_sc_harmony(), seed = 42L, .verbose = TRUE )
batch_column |
String. Column name in the object containing the primary batch labels. |
additional_batch_columns |
Optional character vector. Additional batch
columns to regress out. If |
modality |
String. One of |
harmony_params |
List. Output of |
seed |
Integer. For reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
An ScStep.
# correct the PCA, then build the graph on the corrected embedding step_harmony_sc(batch_column = "batch_index") %>>% step_neighbours_sc(embd_to_use = "harmony")# correct the PCA, then build the graph on the corrected embedding step_harmony_sc(batch_column = "batch_index") %>>% step_neighbours_sc(embd_to_use = "harmony")
Wraps harmony_v2_sc() as an ScStep.
step_harmony_v2_sc( batch_column, additional_batch_columns = NULL, modality = c("rna", "adt"), harmony_params = params_sc_harmony_v2(), seed = 42L, .verbose = TRUE )step_harmony_v2_sc( batch_column, additional_batch_columns = NULL, modality = c("rna", "adt"), harmony_params = params_sc_harmony_v2(), seed = 42L, .verbose = TRUE )
batch_column |
String. Column name in the object containing the primary batch labels. |
additional_batch_columns |
Optional character vector. Additional batch
columns to regress out. If |
modality |
String. One of |
harmony_params |
List. Output of |
seed |
Integer. For reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
An ScStep.
# the v2 implementation writes its own embedding name step_harmony_v2_sc(batch_column = "batch_index") %>>% step_neighbours_sc(embd_to_use = "harmony_v2")# the v2 implementation writes its own embedding name step_harmony_v2_sc(batch_column = "batch_index") %>>% step_neighbours_sc(embd_to_use = "harmony_v2")
Wraps find_hvg_sc() as an ScStep.
step_hvg_sc( hvg_no = 2000L, hvg_params = params_sc_hvg(), streaming = NULL, .verbose = TRUE )step_hvg_sc( hvg_no = 2000L, hvg_params = params_sc_hvg(), streaming = NULL, .verbose = TRUE )
hvg_no |
Integer. Number of highly variable genes to include. Defaults
to |
hvg_params |
List, see
|
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
An ScStep.
# a step is inert until the pipeline is applied step_hvg_sc(hvg_no = 30L)# a step is inert until the pipeline is applied step_hvg_sc(hvg_no = 30L)
Wraps the meta cell generators as an ScStep. Unlike the other steps this
one changes the class of the object: it takes a SingleCells or
SingleCellsSubset and returns a MetaCells(). Steps that follow
it need MetaCells methods, which validate_pipeline() checks up front.
Combined with apply_pipeline_per_group() this gives you per-group
pre-processing (HVG, PCA, batch correction within a patient, kNN) followed by
source-pure meta cells, which you then hand to merge_meta_cells().
step_metacells_sc(method = c("bootstrapped", "seacells", "supercells"), ...)step_metacells_sc(method = c("bootstrapped", "seacells", "supercells"), ...)
method |
String. One of |
... |
Arguments passed on to the generator, e.g.
|
An ScStep.
# per group pre-processing that ends on source-pure meta cells sc <- demo_single_cells(prepped = FALSE) pipeline <- step_hvg_sc(hvg_no = 30L, .verbose = FALSE) %>>% step_pca_sc(no_pcs = 10L, .verbose = FALSE) %>>% step_neighbours_sc(.verbose = FALSE) %>>% step_metacells_sc( "bootstrapped", sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 10L), .verbose = FALSE ) per_group <- apply_pipeline_per_group(pipeline, sc, group_col = "cell_grp") merge_meta_cells(per_group, .verbose = FALSE) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# per group pre-processing that ends on source-pure meta cells sc <- demo_single_cells(prepped = FALSE) pipeline <- step_hvg_sc(hvg_no = 30L, .verbose = FALSE) %>>% step_pca_sc(no_pcs = 10L, .verbose = FALSE) %>>% step_neighbours_sc(.verbose = FALSE) %>>% step_metacells_sc( "bootstrapped", sc_meta_cell_params = params_sc_bt_metacells(target_no_metacells = 10L), .verbose = FALSE ) per_group <- apply_pipeline_per_group(pipeline, sc, group_col = "cell_grp") merge_meta_cells(per_group, .verbose = FALSE) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Wraps find_neighbours_sc() as an ScStep.
step_neighbours_sc( embd_to_use = "pca", no_embd_to_use = NULL, modality = c("rna", "adt"), neighbours_params = params_sc_neighbours(), seed = 42L, .verbose = TRUE )step_neighbours_sc( embd_to_use = "pca", no_embd_to_use = NULL, modality = c("rna", "adt"), neighbours_params = params_sc_neighbours(), seed = 42L, .verbose = TRUE )
embd_to_use |
String. The embedding to use. Whichever you chose, it needs to be part of the object. |
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
modality |
String. One of |
neighbours_params |
List. Output of
|
seed |
Integer. For reproducibility. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
An ScStep.
# neighbours on the PCA embedding step_pca_sc(no_pcs = 10L) %>>% step_neighbours_sc()# neighbours on the PCA embedding step_pca_sc(no_pcs = 10L) %>>% step_neighbours_sc()
Wraps calculate_pca_sc() as an ScStep.
step_pca_sc( no_pcs = 30L, pca_params = params_sc_pca(), sparse_svd = NULL, hvg = NULL, seed = 42L, .verbose = TRUE )step_pca_sc( no_pcs = 30L, pca_params = params_sc_pca(), sparse_svd = NULL, hvg = NULL, seed = 42L, .verbose = TRUE )
no_pcs |
Integer. Number of PCs to calculate. |
pca_params |
Named list. Controls the parameters to be used for the
PCA calculation which is single cell-specific, see |
sparse_svd |
Optional boolean. Solve on the sparse data with implicit
centring and scaling instead of materialising the dense scaled matrix.
|
hvg |
Optional integer. If you want to provide your own HVG genes.
Otherwise, the function will default to what is found in
|
seed |
Integer. Controls reproducibility. Only relevant for
|
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
An ScStep.
# PCA restricted to whatever the HVG step selected step_hvg_sc(hvg_no = 30L) %>>% step_pca_sc(no_pcs = 10L)# PCA restricted to whatever the HVG step selected step_hvg_sc(hvg_no = 30L) %>>% step_pca_sc(no_pcs = 10L)
SingleCells (alias)Convenience alias for load_h5ad(h5ad_streaming = TRUE). Kept for
backwards compatibility. Prefer calling load_h5ad() directly.
stream_h5ad( object, h5_path, sc_qc_param = params_sc_min_quality(), raw_count_slot = c("auto", "X", "raw.X", "layers.counts"), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )stream_h5ad( object, h5_path, sc_qc_param = params_sc_min_quality(), raw_count_slot = c("auto", "X", "raw.X", "layers.counts"), csc_mem_gb = NULL, streaming = deprecated(), batch_size = deprecated(), max_genes_in_memory = deprecated(), cell_batch_size = deprecated(), .verbose = TRUE )
object |
|
h5_path |
File path to the h5ad object. |
sc_qc_param |
List. Output of |
raw_count_slot |
Where raw counts live. |
csc_mem_gb |
Optional numeric. Memory in GB for the buffers of the
cell-to-gene (CSR to CSC) conversion, at 10 bytes per non-zero. |
streaming, batch_size, max_genes_in_memory, cell_batch_size
|
|
.verbose |
Boolean. |
The class with updated shape information.
# same as load_h5ad(h5ad_streaming = TRUE) data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, data$counts, data$obs, data$var, .verbose = FALSE) dir_data <- tempfile("sc_stream") dir.create(dir_data, recursive = TRUE) sc <- stream_h5ad( object = SingleCells(dir_data = dir_data), h5_path = f_path, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(c(f_path, dir_data), recursive = TRUE, force = TRUE)# same as load_h5ad(h5ad_streaming = TRUE) data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, data$counts, data$obs, data$var, .verbose = FALSE) dir_data <- tempfile("sc_stream") dir.create(dir_data, recursive = TRUE) sc <- stream_h5ad( object = SingleCells(dir_data = dir_data), h5_path = f_path, sc_qc_param = params_sc_min_quality( min_unique_genes = 5L, min_lib_size = 25L, min_cells = 5L ), .verbose = FALSE ) dim(sc) unlink(c(f_path, dir_data), recursive = TRUE, force = TRUE)
Holds a Symphony reference: PCA loadings, per-HVG scaling stats, soft
cluster centroids and the cached compression terms (Nr, C) needed to map
queries without the reference cells. The Harmony-corrected reference
embedding (z_corr) is retained for downstream label transfer. Reference
cell labels (e.g. cell type annotations) can be snapshotted at build time
via label_columns in build_symphony_ref() or attached post-hoc via
add_symphony_labels(). For details on the method, refer to Kang et al.
SymphonyReference( hvg_gene_names, gene_means, gene_sds, loadings, z_corr, centroids, nr, c_cache, no_pcs, harmony_backend, batch_vars, slim = FALSE, z_orig = NULL, r = NULL, labels = NULL )SymphonyReference( hvg_gene_names, gene_means, gene_sds, loadings, z_corr, centroids, nr, c_cache, no_pcs, harmony_backend, batch_vars, slim = FALSE, z_orig = NULL, r = NULL, labels = NULL )
hvg_gene_names |
Character vector of HVG gene names in reference loading order. |
gene_means |
Per-HVG mean of the normalised reference data. |
gene_sds |
Per-HVG standard deviation of the normalised reference data. |
loadings |
PCA gene loadings matrix (n_hvgs x d). |
z_corr |
Post-Harmony corrected embedding (N x d). |
centroids |
Cosine-normalised reference centroids (K x d). |
nr |
Reference cluster sizes; row-sums of |
c_cache |
Cached |
no_pcs |
Number of principal components. |
harmony_backend |
Which Harmony variant was used ( |
batch_vars |
Names of the batch variables used during reference construction. |
slim |
Logical; if |
z_orig |
Pre-Harmony PCA scores (N x d). |
r |
Soft cluster assignments (K x N). |
labels |
Optional |
Returns the SymphonyReference class for further operations.
Kang et al., Nat. Commun., 2021
# a reference built off a two batch synthetic data set ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) symphony_ref unlink(ref@dir_data, recursive = TRUE, force = TRUE)# a reference built off a two batch synthetic data set ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) symphony_ref unlink(ref@dir_data, recursive = TRUE, force = TRUE)
Function generates synthetic bulkRNAseq data with heteroskedasticity (lowly
expressed genes show higher variance) and optional co-expression modules for
testing module detection methods. The ground truth comes back with the
counts, so a recovered module or eigengene can be scored against what was
actually simulated. Which generator to pick and what each parameter does is
documented in params_synthetic_bulk_rnaseq().
synthetic_bulk_cor_matrix(synthetic_params = params_synthetic_bulk_rnaseq())synthetic_bulk_cor_matrix(synthetic_params = params_synthetic_bulk_rnaseq())
synthetic_params |
List. The synthetic data parameters, see
|
A synthetic_bulk_data class containing:
counts - The count matrix. Rows are genes, columns are samples.
sparse_counts - A slot for sparse counts that can be added later, see
simulate_dropouts().
module_data - Per-gene ground truth: the gene identifier, its
membership (0 for background), its loading on the module factor and
whether it is a hub gene.
module_factors - The latent factors the modules were built on. Rows are modules, columns are samples. This is what a module eigengene or an ICA/NMF component is trying to recover.
The parameters used are stored on the synthetic_params attribute.
# three co-expression modules on a 1000 x 100 count matrix syn <- synthetic_bulk_cor_matrix(params_synthetic_bulk_rnaseq()) dim(syn$counts) head(syn$module_data) dim(syn$module_factors)# three co-expression modules on a 1000 x 100 count matrix syn <- synthetic_bulk_cor_matrix(params_synthetic_bulk_rnaseq()) dim(syn$counts) head(syn$module_data) dim(syn$module_factors)
Generates three elements: target matrix, background matrix and labels for the target matrix.
synthetic_c_pca_data(seed = 10101L)synthetic_c_pca_data(seed = 10101L)
seed |
Integer. Initial random seed for generation of the synthetic data. Default: 10101L. |
A cpca_synthetic_data class with the following elements:
target - The target matrix.
background - The background matrix.
target_labels - The target labels
# target and background matrices for a contrastive PCA run cpca_data <- synthetic_c_pca_data() dim(cpca_data$target) dim(cpca_data$background) table(cpca_data$target_labels)# target and background matrices for a contrastive PCA run cpca_data <- synthetic_c_pca_data() dim(cpca_data$target) dim(cpca_data$background) table(cpca_data$target_labels)
This is a function to create a synthetic, pseudo gene expression matrix with a pre-defined number of total genes, groups and number of genes differentially expressed in the groups. The function will create in a standard setting a 1000 x 90 matrix, and add optionally a small group into the mixture that is slightly more difficult to detect
synthetic_signal_matrix( no_grps = 3, per_group = 30, total_genes = 1000L, no_genes_up = 100L, add_small_group = TRUE, size_small_grp = 5L, seed = 10101L )synthetic_signal_matrix( no_grps = 3, per_group = 30, total_genes = 1000L, no_genes_up = 100L, add_small_group = TRUE, size_small_grp = 5L, seed = 10101L )
no_grps |
Integer. Number of initial larger groups. Default: 3L. |
per_group |
Integer. Number of samples per larger groups. Default: 30L. |
total_genes |
Integer. Number of total genes in the matrix. Default: 1000L. |
no_genes_up |
Integer. Number of genes per group that are differentially expressed. Default: 100L. |
add_small_group |
Boolean. Add a smaller group that overlaps with group 1? Default: TRUE. |
size_small_grp |
Integer. Size of the smaller group. |
seed |
Integer. Initial random seed for generation of the synthetic data. Default: 10101L. |
A synthetic_matrix_simple class containing:
mat - The random matrix
diff - List of differentially expressed genes per group
group - Look-up vector for sample to group mapping
# default 1000 x 90 matrix with three groups and a small fourth one synthetic_gex <- synthetic_signal_matrix() dim(synthetic_gex$mat) table(synthetic_gex$group) lengths(synthetic_gex$diff)# default 1000 x 90 matrix with three groups and a small fourth one synthetic_gex <- synthetic_signal_matrix() dim(synthetic_gex$mat) table(synthetic_gex$group) lengths(synthetic_gex$diff)
Performs differential abundance testing on single-cell neighbourhoods with
edgeR's quasi-likelihood negative binomial framework, implemented in Rust
via the edge-rs crate. A generalised linear model is fitted to the
neighbourhood counts, one coefficient or contrast is tested, and the spatial
FDR correction accounts for the fact that neighbourhoods overlap and their
tests are therefore not independent.
filterByExpr() is off here and cannot be turned on. It is a gene
expression heuristic and means nothing for a neighbourhood. Use min_mean
if you want to drop sparsely populated neighbourhoods.
test_nhoods( x, design, design_df, coef = NULL, contrast = NULL, norm_method = c("TMM", "TMMwsp", "RLE", "upperquartile", "logMS"), min_mean = 0, robust = TRUE, legacy = TRUE, fdr_weighting = c("k-distance", "graph-overlap", "none") ) ## S3 method for class 'miloR' test_nhoods( x, design, design_df, coef = NULL, contrast = NULL, norm_method = c("TMM", "TMMwsp", "RLE", "upperquartile", "logMS"), min_mean = 0, robust = TRUE, legacy = TRUE, fdr_weighting = c("k-distance", "graph-overlap", "none") )test_nhoods( x, design, design_df, coef = NULL, contrast = NULL, norm_method = c("TMM", "TMMwsp", "RLE", "upperquartile", "logMS"), min_mean = 0, robust = TRUE, legacy = TRUE, fdr_weighting = c("k-distance", "graph-overlap", "none") ) ## S3 method for class 'miloR' test_nhoods( x, design, design_df, coef = NULL, contrast = NULL, norm_method = c("TMM", "TMMwsp", "RLE", "upperquartile", "logMS"), min_mean = 0, robust = TRUE, legacy = TRUE, fdr_weighting = c("k-distance", "graph-overlap", "none") )
x |
|
design |
Formula for the experimental design, e.g. |
design_df |
data.frame. The metadata used to build the model matrix. Its rownames need to cover the sample names of the neighbourhood counts. |
coef |
Optional integer or character. Which coefficient(s) of the design to drop from the null model, given as 1-based column positions or column names. Defaults to the last column, as edgeR does. |
contrast |
Optional numeric vector or matrix. Weights over the design
columns. Mutually exclusive with |
norm_method |
String. Library size normalisation. One of
|
min_mean |
Numeric. Minimum mean count across samples. Neighbourhoods
below it are dropped. Defaults to |
robust |
Logical. Robust estimation of the quasi-likelihood dispersion.
Defaults to |
legacy |
Logical. Take edgeR's pre-4.0 quasi-likelihood pipeline, which
runs |
fdr_weighting |
String. Spatial FDR weighting scheme. One of
|
The miloR object with the differential abundance results added.
Dann, et al., Nat Biotechnol, 2022; Chen, Lun and Smyth, F1000Research, 2016
# differential abundance of neighbourhoods across two sample groups sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) milo <- get_miloR_abundances_sc( sc, sample_id_col = "sample_id", miloR_params = params_sc_miloR(k_refine = 10L), .verbose = FALSE ) design_df <- data.frame( grp = rep(c("a", "b"), each = 3), row.names = sprintf("sample_%i", 1:6) ) milo <- test_nhoods(milo, design = ~grp, design_df = design_df) head(get_differential_abundance_res(milo)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# differential abundance of neighbourhoods across two sample groups sc <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_samples = 6L, sample_bias = "even" ) ) milo <- get_miloR_abundances_sc( sc, sample_id_col = "sample_id", miloR_params = params_sc_miloR(k_refine = 10L), .verbose = FALSE ) design_df <- data.frame( grp = rep(c("a", "b"), each = 3), row.names = sprintf("sample_%i", 1:6) ) milo <- test_nhoods(milo, design = ~grp, design_df = design_df) head(get_differential_abundance_res(milo)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function will calculate the correlations between the identified TF to
gene pairs. You need to have run identify_tf_to_genes()!
Following SCENIC, the correlation is turned into a three-level sign at
rho_threshold: +1 for activating links, -1 for repressing ones and 0
for everything in the band between. mode then decides which of those you
keep. The default keeps the activating links only, which is what SCENIC does
with onlyPositiveCorr = TRUE.
tf_to_genes_correlations( x, object, rho_threshold = 0.03, mode = c("activating", "repressing", "both"), remove_self = TRUE, spearman = TRUE, cor_filter = NULL, .verbose = TRUE ) ## S3 method for class 'ScenicGrn' tf_to_genes_correlations( x, object, rho_threshold = 0.03, mode = c("activating", "repressing", "both"), remove_self = TRUE, spearman = TRUE, cor_filter = NULL, .verbose = TRUE )tf_to_genes_correlations( x, object, rho_threshold = 0.03, mode = c("activating", "repressing", "both"), remove_self = TRUE, spearman = TRUE, cor_filter = NULL, .verbose = TRUE ) ## S3 method for class 'ScenicGrn' tf_to_genes_correlations( x, object, rho_threshold = 0.03, mode = c("activating", "repressing", "both"), remove_self = TRUE, spearman = TRUE, cor_filter = NULL, .verbose = TRUE )
x |
|
object |
|
rho_threshold |
Float. Absolute correlation above which a TF to gene
link counts as activating or repressing. Defaults to |
mode |
String. Which links to keep. One of
|
remove_self |
Boolean. Shall self loops (where TF controls its own
expression) be removed. Defaults to |
spearman |
Boolean. Shall Spearman correlation be used. Defaults to
|
cor_filter |
Deprecated. Use |
.verbose |
Boolean. Controls verbosity of the function. |
Adds a pairwise_cor and a cor_sign column to the TF to gene
results.
Aibar, et al., Nat Methods, 2017
# sign the TF to gene links and keep the activating ones sc <- demo_single_cells() grn <- scenic_grn_sc( sc, tf_ids = sprintf("gene_%02d", 1:5), scenic_params = params_scenic( min_counts = 1L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) grn <- identify_tf_to_genes( grn, method = "top_k", k_tfs = 3L, .verbose = FALSE ) grn <- tf_to_genes_correlations(grn, object = sc, .verbose = FALSE) head(get_tf_to_gene(grn)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# sign the TF to gene links and keep the activating ones sc <- demo_single_cells() grn <- scenic_grn_sc( sc, tf_ids = sprintf("gene_%02d", 1:5), scenic_params = params_scenic( min_counts = 1L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) grn <- identify_tf_to_genes( grn, method = "top_k", k_tfs = 3L, .verbose = FALSE ) grn <- tf_to_genes_correlations(grn, object = sc, .verbose = FALSE) head(get_tf_to_gene(grn)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function will run the motif enrichment based on RCisTarget (or in this
case the internal implementation via run_cistarget()). You need to provide
the expected rankings and TF annotations (for details, please see
run_cistarget()). Briefly, this function will run CisTarget and add the
results to the ScenicGrn object and add additionally a column "in_motif",
for a given TF to gene set to say if it was part of the motifs associated
with this TF (or not). You have the option to limit this to only the
the high confidence TFs (default), or also include the low confidence TFs
(i.e., links from TF to motif that are less certain).
tf_to_genes_motif_enrichment( x, motif_rankings, annot_data, cis_target_params = params_cistarget(), gene_id_to_symbol = NULL, only_high_conf_tf = TRUE, .verbose = TRUE ) ## S3 method for class 'ScenicGrn' tf_to_genes_motif_enrichment( x, motif_rankings, annot_data, cis_target_params = params_cistarget(), gene_id_to_symbol = NULL, only_high_conf_tf = TRUE, .verbose = TRUE )tf_to_genes_motif_enrichment( x, motif_rankings, annot_data, cis_target_params = params_cistarget(), gene_id_to_symbol = NULL, only_high_conf_tf = TRUE, .verbose = TRUE ) ## S3 method for class 'ScenicGrn' tf_to_genes_motif_enrichment( x, motif_rankings, annot_data, cis_target_params = params_cistarget(), gene_id_to_symbol = NULL, only_high_conf_tf = TRUE, .verbose = TRUE )
x |
|
motif_rankings |
Integer matrix. Motif rankings for genes. Row names are gene identifiers, column names are motif identifiers. Lower values indicate higher regulatory potential. |
annot_data |
data.table. Motif annotation database mapping motifs to
transcription factors. Must contain columns: |
cis_target_params |
List. Output of
|
gene_id_to_symbol |
Named character vector. Mapping from gene
identifiers used internally (e.g., Ensembl IDs) to the identifiers used
in the motif rankings (e.g., HGNC symbols). Names are internal IDs,
values are ranking IDs. If |
only_high_conf_tf |
Boolean. Shall only the high confidence TF to
motif association be used. Defaults to |
.verbose |
Boolean. Controls verbosity of the function. |
Adds a data.table with the first tf to gene results to the class.
# CisTarget against a synthetic ranking database sc <- demo_single_cells() tfs <- sprintf("gene_%02d", 1:5) grn <- scenic_grn_sc( sc, tf_ids = tfs, scenic_params = params_scenic( min_counts = 1L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) grn <- identify_tf_to_genes( grn, method = "top_k", k_tfs = 3L, .verbose = FALSE ) genes <- get_gene_names(sc) targets <- split(get_tf_to_gene(grn)$gene, get_tf_to_gene(grn)$tf) rankings <- matrix( vapply( tfs, function(tf) { as.integer(rank(!(genes %in% targets[[tf]]), ties.method = "first")) }, integer(length(genes)) ), nrow = length(genes), dimnames = list(genes, sprintf("motif_%s", tfs)) ) annot <- data.table::data.table( motif = sprintf("motif_%s", tfs), TF = tfs, annotationSource = factor( c(rep("directAnnotation", 4), "inferredBy_MotifSimilarity") ) ) grn <- tf_to_genes_motif_enrichment( grn, motif_rankings = rankings, annot_data = annot, cis_target_params = params_cistarget( auc_threshold = 1, nes_threshold = 1, high_conf_cats = "directAnnotation", low_conf_cats = "inferredBy_MotifSimilarity" ), .verbose = FALSE ) head(get_tf_to_gene(grn)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# CisTarget against a synthetic ranking database sc <- demo_single_cells() tfs <- sprintf("gene_%02d", 1:5) grn <- scenic_grn_sc( sc, tf_ids = tfs, scenic_params = params_scenic( min_counts = 1L, learner_params = list(n_trees = 20L) ), .verbose = FALSE ) grn <- identify_tf_to_genes( grn, method = "top_k", k_tfs = 3L, .verbose = FALSE ) genes <- get_gene_names(sc) targets <- split(get_tf_to_gene(grn)$gene, get_tf_to_gene(grn)$tf) rankings <- matrix( vapply( tfs, function(tf) { as.integer(rank(!(genes %in% targets[[tf]]), ties.method = "first")) }, integer(length(genes)) ), nrow = length(genes), dimnames = list(genes, sprintf("motif_%s", tfs)) ) annot <- data.table::data.table( motif = sprintf("motif_%s", tfs), TF = tfs, annotationSource = factor( c(rep("directAnnotation", 4), "inferredBy_MotifSimilarity") ) ) grn <- tf_to_genes_motif_enrichment( grn, motif_rankings = rankings, annot_data = annot, cis_target_params = params_cistarget( auc_threshold = 1, nes_threshold = 1, high_conf_cats = "directAnnotation", low_conf_cats = "inferredBy_MotifSimilarity" ), .verbose = FALSE ) head(get_tf_to_gene(grn)) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function takes two sets of diffusion vector and leverages tied diffusion to identify an intersection of influential nodes. If the network is undirected, the method will run two personalised page rank diffusions based on the diffusion vectors and generate the score aggregation
tied_diffusion( object, diffusion_vector_1, diffusion_vector_2, summarisation = c("max", "mean", "harmonic_sum"), score_aggregation = c("min", "max", "mean"), .verbose = FALSE )tied_diffusion( object, diffusion_vector_1, diffusion_vector_2, summarisation = c("max", "mean", "harmonic_sum"), score_aggregation = c("min", "max", "mean"), .verbose = FALSE )
object |
|
diffusion_vector_1 |
Named numeric. The first named vector with values to use for the reset parameter in the personalised page-rank diffusion. Names should represent node names of the graph. |
diffusion_vector_2 |
Named numeric. The second named vector with values to use for the reset parameter in the personalised page-rank diffusion. Names should represent node names of the graph. |
summarisation |
String. If there are duplicated names in the
|
score_aggregation |
String. How to summarise the tied scores. |
.verbose |
Boolean. Controls verbosity of the function. |
The class with added diffusion score based on a two sets of seed genes. Additionally, the seed genes are stored in the class.
# tied diffusion between two sets of seed nodes set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- tied_diffusion( object, diffusion_vector_1 = c(node_1 = 1, node_3 = 1), diffusion_vector_2 = c(node_2 = 1, node_6 = 1), summarisation = "max", score_aggregation = "min" ) head(get_diffusion_vector(object))# tied diffusion between two sets of seed nodes set.seed(42) g <- igraph::sample_pa(15, directed = FALSE) edges <- data.table::setDT(igraph::as_data_frame(g))[, `:=`( from = sprintf("node_%i", from), to = sprintf("node_%i", to) )] object <- NetworkDiffusions(edges, weighted = FALSE, directed = FALSE) object <- tied_diffusion( object, diffusion_vector_1 = c(node_1 = 1, node_3 = 1), diffusion_vector_2 = c(node_2 = 1, node_6 = 1), summarisation = "max", score_aggregation = "min" ) head(get_diffusion_vector(object))
Helper function to transform strings to snake_case
to_snake_case(x, ignore_na = FALSE)to_snake_case(x, ignore_na = FALSE)
x |
String. The string (vector) you wish to transform to snake_case. |
ignore_na |
Boolean. Shall the function just ignore |
Returns the string in snake_case format.
# normalise mixed naming conventions to_snake_case(c("Gene Name", "someRNAValue", "Foo-Bar"))# normalise mixed naming conventions to_snake_case(c("Gene Name", "someRNAValue", "Foo-Bar"))
This is a helper function that calculates proportions of reads to the Top N genes by expression in a given cell. High values here can indicate low complexity, quality cells. The values will be automatically added to the obs table.
top_genes_perc_sc( object, top_n_vals = c(25L, 50L, 100L), streaming = NULL, .verbose = TRUE )top_genes_perc_sc( object, top_n_vals = c(25L, 50L, 100L), streaming = NULL, .verbose = TRUE )
object |
|
top_n_vals |
Integer. The Top N thresholds to test. |
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
It will add the columns based on the names in the gene_set_list to
the obs table.
# share of a cell's reads taken by its top 5 and top 10 genes sc <- demo_single_cells(prepped = FALSE) sc <- top_genes_perc_sc(sc, top_n_vals = c(5L, 10L), .verbose = FALSE) head(unlist(sc[["top_5_genes_percentage"]])) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# share of a cell's reads taken by its top 5 and top 10 genes sc <- demo_single_cells(prepped = FALSE) sc <- top_genes_perc_sc(sc, top_n_vals = c(5L, 10L), .verbose = FALSE) head(unlist(sc[["top_5_genes_percentage"]])) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
kNN majority vote on the Harmony-corrected embeddings. The reference's
z_corr is the searchable index; the query's "symphony" embedding is
what gets queried. Reference labels come from the reference's stored
labels slot — populate it via the label_columns argument to
build_symphony_ref() or post-hoc via add_symphony_labels().
Distances on z_corr are typically Euclidean — set knn_params$ann_dist
to "euclidean" unless you have a reason to use cosine.
transfer_labels_symphony( reference, query, label_column, knn_params = params_sc_knn(), seed = 42L, .verbose = TRUE )transfer_labels_symphony( reference, query, label_column, knn_params = params_sc_knn(), seed = 42L, .verbose = TRUE )
reference |
|
query |
|
label_column |
String. Name of a column in the reference's stored labels. |
knn_params |
List. Output of |
seed |
Integer. |
.verbose |
Boolean or integer. |
A data.table with columns predicted_<label_column> and
confidence_<label_column>, in get_cells_to_keep(query) order.
# kNN label transfer from the reference onto a mapped query ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) query <- demo_single_cells(prepped = FALSE, seed = 7L) query <- map_symphony_query(symphony_ref, query = query, .verbose = FALSE) labels <- transfer_labels_symphony( symphony_ref, query = query, label_column = "cell_grp", .verbose = FALSE ) head(labels) unlink(c(ref@dir_data, query@dir_data), recursive = TRUE, force = TRUE)# kNN label transfer from the reference onto a mapped query ref <- demo_single_cells( syn_data_params = params_sc_synthetic_data( n_cells = 500L, n_genes = 50L, n_batches = 2L ) ) symphony_ref <- build_symphony_ref( ref, batch_column = "batch_index", hvg = get_hvg(ref) + 1L, harmony_params = params_sc_harmony(k = 10L), no_pcs = 10L, label_columns = "cell_grp", .verbose = FALSE ) query <- demo_single_cells(prepped = FALSE, seed = 7L) query <- map_symphony_query(symphony_ref, query = query, .verbose = FALSE) labels <- transfer_labels_symphony( symphony_ref, query = query, label_column = "cell_grp", .verbose = FALSE ) head(labels) unlink(c(ref@dir_data, query@dir_data), recursive = TRUE, force = TRUE)
Wrapper around manifoldsR::tsne() for the SingleCells and MetaCells
classes. t-SNE produces a low-dimensional embedding that emphasises local
neighbourhood structure. Distances between well-separated clusters should not
be over-interpreted quantitatively, but the common claim that t-SNE discards
global structure while UMAP preserves it is largely an artefact of default
initialisations rather than a property of the loss functions themselves.
When use_knn = TRUE, the kNN graph already stored on the object is reused.
With use_knn = FALSE (the default), neighbours are computed from the chosen
embedding.
Three approximation strategies are available via approx_type: "bh"
(Barnes-Hut) is the classical O(n log n) approximation and works well
across a wide range of dataset sizes; "fft" (interpolation-based, as in
FIt-SNE) scales better to very large datasets; "fft_3k" is the
three-kernel variant of the latter, with one forward and three inverse FFTs
per epoch instead of four each. The FFT options are only available on Unix
systems. perplexity controls the bandwidth of the Gaussian kernel used to
compute affinities within the neighbour set (typical values 5-50). When a
pre-computed kNN is supplied via use_knn = TRUE, perplexity no longer
drives neighbour retrieval but still shapes the affinity distribution over
the retrieved neighbours; values too close to the kNN size will produce poor
results. With tSNE in particular the rule of thumb is to set k to
3 * perplexity. When k <= perplexity the algorithm does not behave
properly anymore, thus, will throw an error.
tsne_sc( object, use_knn = FALSE, embd_to_use = "pca", slot_name = "tsne", no_embd_to_use = NULL, modality = c("rna", "adt", "wnn"), n_dim = 2L, perplexity = 10, approx_type = c("bh", "fft", "fft_3k"), knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"), nn_params = manifoldsR::params_nn(), tsne_params = manifoldsR::params_tsne(), seed = 42L, .verbose = TRUE )tsne_sc( object, use_knn = FALSE, embd_to_use = "pca", slot_name = "tsne", no_embd_to_use = NULL, modality = c("rna", "adt", "wnn"), n_dim = 2L, perplexity = 10, approx_type = c("bh", "fft", "fft_3k"), knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"), nn_params = manifoldsR::params_nn(), tsne_params = manifoldsR::params_tsne(), seed = 42L, .verbose = TRUE )
object |
|
use_knn |
Boolean. Use the kNN graph found in the object. Defaults to
|
embd_to_use |
String. The embedding to use for t-SNE. Must be available in the object. |
slot_name |
String. The name of this embedding within the object.
Defaults to |
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
modality |
String. On which modality to run the t-SNE. One of
|
n_dim |
Integer. Number of t-SNE dimensions. Currently only |
perplexity |
Numeric. Perplexity parameter. Typical values between 5
and 50. Defaults to |
approx_type |
String. Approximation method. One of |
knn_method |
String. Approximate nearest neighbour algorithm. One of
|
nn_params |
Named list. See |
tsne_params |
Named list. See |
seed |
Integer. For reproducibility. |
.verbose |
Boolean. Controls verbosity. |
The object with a "tsne" embedding added.
# Barnes-Hut t-SNE on the PCA factors sc <- demo_single_cells() sc <- tsne_sc(sc, .verbose = FALSE) dim(get_embedding(sc, "tsne")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# Barnes-Hut t-SNE on the PCA factors sc <- demo_single_cells() sc <- tsne_sc(sc, .verbose = FALSE) dim(get_embedding(sc, "tsne")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Wrapper around manifoldsR::umap() for the SingleCells and MetaCells
classes. UMAP produces a low-dimensional embedding that emphasises local
neighbourhood structure while being computationally efficient via its
negative-sampling-based optimisation. It is the de facto default for
visualising single-cell data, though claims that it preserves global
structure substantially better than t-SNE are not well supported; with
matched initialisation (e.g. PCA or Laplacian Eigenmaps), the two methods
behave similarly on global geometry, and both should be interpreted primarily
as views of local structure.
When use_knn = TRUE (the default), the kNN graph already stored on the
object (via find_neighbours_sc()) is reused, which avoids
recomputing nearest neighbours and keeps the UMAP consistent with any
downstream sNN-based clustering. If no kNN is present, neighbours are
computed from the chosen embedding on the fly.
Key parameters to tune: k controls the balance between local and global
structure (larger values produce more global layouts), while min_dist
and spread together control how tightly points are packed in the
embedding. For MetaCells, smaller k values are often appropriate given
the reduced number of points.
umap_sc( object, use_knn = TRUE, embd_to_use = "pca", slot_name = "umap", no_embd_to_use = NULL, modality = c("rna", "adt", "wnn"), n_dim = 2L, k = 15L, min_dist = 0.5, spread = 1, knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"), nn_params = manifoldsR::params_nn(), umap_params = manifoldsR::params_umap(), seed = 42L, .verbose = TRUE )umap_sc( object, use_knn = TRUE, embd_to_use = "pca", slot_name = "umap", no_embd_to_use = NULL, modality = c("rna", "adt", "wnn"), n_dim = 2L, k = 15L, min_dist = 0.5, spread = 1, knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"), nn_params = manifoldsR::params_nn(), umap_params = manifoldsR::params_umap(), seed = 42L, .verbose = TRUE )
object |
|
use_knn |
Boolean. Use the kNN graph found in the object. Defaults to
|
embd_to_use |
String. The embedding to use for UMAP. Must be available in the object. |
slot_name |
String. The name of this embedding within the object.
Defaults to |
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
modality |
String. On which modality to run the UMAP. One of
|
n_dim |
Integer. Number of UMAP dimensions. Defaults to |
k |
Integer. Number of nearest neighbours. Defaults to |
min_dist |
Numeric. Minimum distance between embedded points. Defaults
to |
spread |
Numeric. Effective scale of embedded points. Defaults to |
knn_method |
String. Approximate nearest neighbour algorithm. One of
|
nn_params |
Named list. See |
umap_params |
Named list. See |
seed |
Integer. For reproducibility. |
.verbose |
Boolean. Controls verbosity. |
The object with a "umap" embedding added.
# UMAP off the cached kNN graph sc <- demo_single_cells() sc <- umap_sc(sc, .verbose = FALSE) dim(get_embedding(sc, "umap")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# UMAP off the cached kNN graph sc <- demo_single_cells() sc <- umap_sc(sc, .verbose = FALSE) dim(get_embedding(sc, "umap")) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This function will update the values in a given metadata column based on what you are providing in terms of replacement.
update_metadata_values(object, column, replacement, ...)update_metadata_values(object, column, replacement, ...)
object |
The underlying object, either |
column |
Character vector. The columns for which to replace the values. |
replacement |
Named character vector. The values with which to replace the data. |
... |
Additional arguments to parse to the functions. |
Returns the object with the respective metadata updated.
# relabel the levels of a metadata column syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- update_metadata_values( object, column = "case_control", replacement = c(case = "disease", control = "healthy") ) unique(get_metadata(object)$case_control)# relabel the levels of a metadata column syn <- synthetic_bulk_cor_matrix() meta <- data.table::data.table( sample_id = colnames(syn$counts), case_control = rep(c("case", "control"), each = 50) ) object <- BulkDge(raw_counts = syn$counts, meta_data = meta) object <- update_metadata_values( object, column = "case_control", replacement = c(case = "disease", control = "healthy") ) unique(get_metadata(object)$case_control)
Transform an upper triangle-stored matrix to a sparse one
upper_triangle_to_sparse(upper_triangle_vals, shift, n, type = c("csc", "csr"))upper_triangle_to_sparse(upper_triangle_vals, shift, n, type = c("csc", "csr"))
upper_triangle_vals |
Numerical vector. The values of the upper triangle stored in a row major format. |
shift |
Boolean. Did you exclude the diagonal. |
n |
Integer. Number of columns and rows of the symmetric matrix. |
type |
String. One of |
The sparse matrix.
# 3 x 3 symmetric matrix from its off-diagonal upper triangle mat <- upper_triangle_to_sparse(c(0.5, 0.2, 0.8), shift = TRUE, n = 3L) dim(mat)# 3 x 3 symmetric matrix from its off-diagonal upper triangle mat <- upper_triangle_to_sparse(c(0.5, 0.2, 0.8), shift = TRUE, n = 3L) dim(mat)
The class allows to store the upper triangular matrix of a symmetric matrix (think correlation matrix, distance matrix, etc.) in an memory-efficient form and return a data.table or dense (or sparse) R matrix if need be.
upper_triangular_sym_mat$new()Initialises the R6 class.
upper_triangular_sym_mat$new(values, features, shift)
valuesNumerical vector. The correlation coefficients of the upper triangular correlation matrix stored as a row-major vector
featuresString vector. The features of the correlation matrix.
shiftBollean Was a shift applied during the calculation of the
upper triangular matrix. If FALSE, the diagonal was included, if
TRUE, the diagonal was removed.
Returns the initialised class.
upper_triangular_sym_mat$print()Print the class
upper_triangular_sym_mat$print()
Returns the initialised class
upper_triangular_sym_mat$get_data_table()Returns the data in form of a data.table.
upper_triangular_sym_mat$get_data_table(factor = FALSE, .verbose = TRUE)
factorBoolean. Shall the string columns be transformed into factors. Reduces size of the object; however, takes longer to generate.
.verboseBoolean. Controls verbosity.
A data.table with three columns:
feature_a: The name of the first feature in the correlation matrix.
feature_b: The name of the second feature in the correlation matrix.
cor: The correlation coefficients between these two features.
upper_triangular_sym_mat$get_sym_matrix()Return the full correlation matrix.
upper_triangular_sym_mat$get_sym_matrix(.verbose = TRUE)
.verboseBoolean. Controls verbosity.
Returns the correlation matrix as a dense R matrix.
upper_triangular_sym_mat$get_sparse_matrix()Return a sparse version of the correlation matrix
upper_triangular_sym_mat$get_sparse_matrix(.verbose = TRUE)
.verboseBoolean. Controls verbosity
The sparse matrix.
upper_triangular_sym_mat$get_data()Return the correlation data and shift
upper_triangular_sym_mat$get_data()
A list with
cor_data - Numeric vector. The values.
features - String. The feature names.
n_features - Integer. Number of initial features.
shift - Integer. The applied shift.
upper_triangular_sym_mat$clone()The objects of this class are cloneable with this method.
upper_triangular_sym_mat$clone(deep = FALSE)
deepWhether to make a deep clone.
# store the off-diagonal of a correlation matrix and get it back set.seed(42) cor_mat <- cor(matrix(rnorm(80), nrow = 20, ncol = 4)) object <- upper_triangular_sym_mat$new( values = cor_mat[lower.tri(cor_mat)], features = sprintf("gene_%i", 1:4), shift = TRUE ) object$get_data_table(.verbose = FALSE) dim(object$get_sym_matrix(.verbose = FALSE))# store the off-diagonal of a correlation matrix and get it back set.seed(42) cor_mat <- cor(matrix(rnorm(80), nrow = 20, ncol = 4)) object <- upper_triangular_sym_mat$new( values = cor_mat[lower.tri(cor_mat)], features = sprintf("gene_%i", 1:4), shift = TRUE ) object$get_data_table(.verbose = FALSE) dim(object$get_sym_matrix(.verbose = FALSE))
Walks the pipeline and tracks which class each step would receive. Steps
declare the classes their generic has methods for, and most steps hand back
what they were given; step_metacells_sc() does not, it turns a
SingleCells/SingleCellsSubset into a MetaCells. Without this you would
only find out about a mismatch when S7 fails to dispatch, i.e. after the
expensive steps already ran.
validate_pipeline(pipeline, class)validate_pipeline(pipeline, class)
pipeline |
|
class |
String. Class of the object the pipeline would start on. One of
|
Invisibly, the class the pipeline would return.
# the meta cell step changes what the next step would receive p <- step_hvg_sc() %>>% step_metacells_sc("bootstrapped") print(validate_pipeline(p, "SingleCells"))# the meta cell step changes what the next step would receive p <- step_hvg_sc() %>>% step_metacells_sc("bootstrapped") print(validate_pipeline(p, "SingleCells"))
Calculates an VISION-type scores for pathways based on DeTomaso, et al. Compared to other score types, you can also calculate delta-type scores between positive and negative gene indices, think epithelial vs mesenchymal gene signature, etc.
vision_sc(object, gs_list, streaming = NULL, .verbose = TRUE)vision_sc(object, gs_list, streaming = NULL, .verbose = TRUE)
object |
|
gs_list |
Named nested list. Every element must itself be a list with
at least a |
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
The VISION scores in form of a matrix that is cells x gene sets
or as ScMatrixRes pending the input.
DeTomaso, et al., Nat. Commun., 2019
# a signed signature alongside a plain one sc <- demo_single_cells() gs_list <- list( programme_a = list( pos = get_gene_names(sc)[1:10], neg = get_gene_names(sc)[11:20] ), programme_b = list(pos = get_gene_names(sc)[21:30]) ) res <- vision_sc(sc, gs_list = gs_list, .verbose = FALSE) dim(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)# a signed signature alongside a plain one sc <- demo_single_cells() gs_list <- list( programme_a = list( pos = get_gene_names(sc)[1:10], neg = get_gene_names(sc)[11:20] ), programme_b = list(pos = get_gene_names(sc)[21:30]) ) res <- vision_sc(sc, gs_list = gs_list, .verbose = FALSE) dim(res) unlink(sc@dir_data, recursive = TRUE, force = TRUE)
Calculates VISION-type scores for pathways based on DeTomaso, et al. Compared to other score types, you can also calculate delta-type scores between positive and negative gene indices, think epithelial vs mesenchymal gene signature, etc. Additionally, this function also calculates the auto- correlation values, answering the question if a given signature shows non- random enrichment on the kNN graph. The kNN graph (and distance measures) will be generated on-the-fly based on the embedding you wish to use.
vision_w_autocor_sc( object, gs_list, embd_to_use, no_embd_to_use = NULL, use_knn = TRUE, vision_params = params_sc_vision(), streaming = NULL, random_seed = 42L, .verbose = TRUE )vision_w_autocor_sc( object, gs_list, embd_to_use, no_embd_to_use = NULL, use_knn = TRUE, vision_params = params_sc_vision(), streaming = NULL, random_seed = 42L, .verbose = TRUE )
object |
|
gs_list |
Named nested list. Every element must itself be a list with
at least a |
embd_to_use |
String. The embedding to use. Whichever you chose, it needs to be part of the object. |
no_embd_to_use |
Optional integer. Number of embedding dimensions to
use. If |
use_knn |
Boolean. Shall the internal kNN be used. If set to yes, you need to ensure consistency. |
vision_params |
List with vision parameters, see
|
streaming |
Optional Boolean. Shall the data be streamed in. Useful for
larger data sets where you wish to avoid loading in the whole data. If
|
random_seed |
Integer. The random seed. |
.verbose |
Boolean or integer. Controls verbosity and returns run times.
|
A list with the following elements:
vision_matrix - Matrix of cells x signatures with the VISION pathway scores as values.
auto_cor_dt - data.table with the auto-correlation results per
gene set, i.e., auto_cor (1 - Gaery's C), p_val and fdr.
DeTomaso, et al., Nat. Commun., 2019
# scores plus whether they sit non-randomly on the kNN graph sc <- demo_single_cells() gs_list <- list( programme_a = list(pos = get_gene_names(sc)[1:10]), programme_b = list(pos = get_gene_names(sc)[21:30]) ) res <- vision_w_autocor_sc( sc, gs_list = gs_list, embd_to_use = "pca", vision_params = params_sc_vision(n_perm = 50L, n_cluster = 3L), .verbose = FALSE ) res$auto_cor_dt unlink(sc@dir_data, recursive = TRUE, force = TRUE)# scores plus whether they sit non-randomly on the kNN graph sc <- demo_single_cells() gs_list <- list( programme_a = list(pos = get_gene_names(sc)[1:10]), programme_b = list(pos = get_gene_names(sc)[21:30]) ) res <- vision_w_autocor_sc( sc, gs_list = gs_list, embd_to_use = "pca", vision_params = params_sc_vision(n_perm = 50L, n_cluster = 3L), .verbose = FALSE ) res$auto_cor_dt unlink(sc@dir_data, recursive = TRUE, force = TRUE)
This is a helper to write synthetic data to cell ranger like output, i.e., an .mtx file, an barcodes.csv (or .tsv) and a features.csv (or .tsv).
write_cellranger_output( f_path, counts, obs, var, format_type = c("csv", "tsv"), rows = c("cells", "genes"), overwrite = TRUE, .verbose = TRUE )write_cellranger_output( f_path, counts, obs, var, format_type = c("csv", "tsv"), rows = c("cells", "genes"), overwrite = TRUE, .verbose = TRUE )
f_path |
String. The filepath to which to save the data |
counts |
Sparse matrix. Needs to be of class |
obs |
data.table. The observations. Needs to have
|
var |
data.table. The variable data. Needs to have
|
format_type |
String. One of |
rows |
String. One of |
overwrite |
Boolean. Shall any found h5ad file be overwritten. |
.verbose |
Boolean. Controls verbosity of the function. |
Returns invisible
# the 10x trio: an .mtx plus barcode and feature tables data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) dir_out <- tempfile("cellranger") dir.create(dir_out, recursive = TRUE) write_cellranger_output( f_path = dir_out, counts = data$counts, obs = data$obs, var = data$var, rows = "cells", format_type = "csv", .verbose = FALSE ) list.files(dir_out) unlink(dir_out, recursive = TRUE, force = TRUE)# the 10x trio: an .mtx plus barcode and feature tables data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) dir_out <- tempfile("cellranger") dir.create(dir_out, recursive = TRUE) write_cellranger_output( f_path = dir_out, counts = data$counts, obs = data$obs, var = data$var, rows = "cells", format_type = "csv", .verbose = FALSE ) list.files(dir_out) unlink(dir_out, recursive = TRUE, force = TRUE)
This is a helper to write synthetic data to h5ad file. This version will write the data into the common compressed sparse data format.
write_h5ad_sc(f_path, counts, obs, var, overwrite = TRUE, .verbose = TRUE)write_h5ad_sc(f_path, counts, obs, var, overwrite = TRUE, .verbose = TRUE)
f_path |
String. The filepath to which to save the data |
counts |
Sparse matrix. Needs to be of class |
obs |
data.table. The observations. Needs to have
|
var |
data.table. The variable data. Needs to have
|
overwrite |
Boolean. Shall any found h5ad file be overwritten. |
.verbose |
Boolean. Controls verbosity of the function. |
Returns invisible
# round trip synthetic counts through a sparse h5ad data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, data$counts, data$obs, data$var, .verbose = FALSE) get_h5ad_dimensions(f_path)$dims unlink(f_path)# round trip synthetic counts through a sparse h5ad data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc(f_path, data$counts, data$obs, data$var, .verbose = FALSE) get_h5ad_dimensions(f_path)$dims unlink(f_path)
Helper to write synthetic data to h5ad with /X stored as a
dense 2D dataset (cells x genes). Useful for exercising the dense ingestion
path.
write_h5ad_sc_dense( f_path, counts, obs, var, overwrite = TRUE, .verbose = TRUE )write_h5ad_sc_dense( f_path, counts, obs, var, overwrite = TRUE, .verbose = TRUE )
f_path |
String. The filepath to which to save the data |
counts |
Matrix or sparse matrix; sparse input is densified. |
obs |
data.table. The observations. Needs |
var |
data.table. The variable data. Needs |
overwrite |
Boolean. Shall any found h5ad file be overwritten. |
.verbose |
Boolean. Controls verbosity of the function. |
Returns invisible
# the same data with a dense /X, which reads back as DENSE_ROW data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc_dense( f_path, data$counts, data$obs, data$var, .verbose = FALSE ) get_h5ad_dimensions(f_path)$type unlink(f_path)# the same data with a dense /X, which reads back as DENSE_ROW data <- generate_single_cell_test_data( syn_data_params = params_sc_synthetic_data(n_cells = 200L, n_genes = 40L) ) f_path <- tempfile(fileext = ".h5ad") write_h5ad_sc_dense( f_path, data$counts, data$obs, data$var, .verbose = FALSE ) get_h5ad_dimensions(f_path)$type unlink(f_path)