
Single cell count data handler
SingleCellCountData.Rd
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.
Arguments
- f_path_cells
(
character)
Path to the.binfile for the cell-based (CSR-like) representation.- f_path_genes
(
character)
Path to the.binfile for the gene-based (CSC-like) representation.- n_cells
(
integer)
Number of cells represented in the data.- n_genes
(
integer)
Number of genes represented in the data.
Methods
Method new
Create a new instance of the class
Method set_from_file
Populate n_cells and n_genes from the cells binary file
Method r_data_to_file
Write a CSR matrix from R to the cells binary file
Arguments
r_data(
list)
A named list convertible intoCompressedSparseData2. Must contain"indptr","indices"(0-indexed),"data","nrow","ncol"and"cs_type"("csr"or"csc").qc_params(
list)
Quality control parameters parseable intoMinCellQuality.verbose(
logical)
Controls verbosity of the function.
Method h5ad_to_file
Write an h5ad file to the cells binary file
Arguments
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 intoMinCellQuality.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.
Method norm_h5ad_to_file
Write an h5ad file with normalised counts to the cells binary file
Arguments
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 theobscolumn 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 intoMinCellQuality.verbose(
logical)
Controls verbosity of the function.
Method h5ad_to_file_streaming
Write an h5ad file to disk using streaming
Arguments
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 intoMinCellQuality.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.
Method multi_h5ad_to_file
Load multiple h5ad files into a single binary
Arguments
file_tasks(
list)
A list of lists, each produced by the R prescan function. Each inner list must containexp_id,h5_path,cs_type,no_cells,no_genesandgene_local_to_universe(0-indexed integer vector,NAfor unmapped genes), plus an optionalraw_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.
Method mtx_to_file
Write an mtx file to the cells binary file
Method mtx_to_file_streaming
Write an mtx file to the cells binary file using streaming
Method multi_mtx_to_file
Load multiple mtx files into a single binary
Arguments
file_tasks(
list)
A list of lists, each containingexp_id,mtx_path,cells_as_rowsandgene_local_to_universe(integer vector,NAfor unmapped genes).universe_size(
integer)
Number of genes in the intersection universe.qc_params(
list)
Quality control parameters parseable intoMinCellQuality.verbose(
logical)
Controls verbosity.
Method tenx_h5_to_file_streaming
Write a 10x CellRanger h5 file to the cells binary file using streaming
Arguments
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 intoMinCellQuality.feature_type(
characterorNULL)
Target modality for v3. Defaults to"Gene Expression"whenNULL.verbose(
logical)
Controls verbosity of the function.
Method multi_tenx_h5_to_file
Load multiple 10x CellRanger h5 files into a single binary
Arguments
file_tasks(
list)
A list of lists, each produced by the R prescan function. Each inner list must containexp_id,h5_path,version("v2"or"v3"),no_cells,no_genes,gene_local_to_universe(integer vector,NAfor unmapped / non-gene features) andfeature_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.
Method return_full_mat
Return the full count matrix
Arguments
assay(
character)
One of"raw"or"norm". Selects whether raw counts or log-normalised counts are returned.cell_based(
logical)
IfTRUE, the data is returned in CSR layout (cells as rows). IfFALSE, the data is returned in CSC layout (genes as columns).verbose(
logical)
Controls verbosity of the function.
Method get_cells_by_indices
Return cells by index positions
Method generate_gene_based_data
Generate gene-based data from the cells binary file
description
Reads the .bin file at f_path_cells and writes a gene-friendly
(CSC) representation to f_path_genes. The conversion happens fully
in memory and may cause memory pressure on large data sets; see
generate_gene_based_data_streaming or
generate_gene_based_data_memory_bounded for lighter alternatives.
Method generate_gene_based_data_streaming
Generate gene-based data with streaming
Arguments
batch_size(
integer)
Number of cells processed per batch. Larger values increase memory pressure but reduce overhead.verbose(
logical)
Controls verbosity of the function.
Method generate_gene_based_data_memory_bounded
Generate gene-based data with memory-bounded accumulation
Arguments
max_genes_in_memory(
integer)
Maximum number of genes to accumulate at once (e.g.2000).cell_batch_size(
integer)
Number of cells to process at once (e.g.100000).verbose(
logical)
Controls verbosity.
Method get_genes_by_indices
Return genes by index positions
Method get_nnz_genes
Get the number of cells expressing each gene
Method merge_sc_files
Merge multiple existing bin files into the cells binary file
Arguments
merge_tasks(
list)
A list of lists. Each inner list must containexp_id,bin_cells_path,cells_to_keep(0-indexed integer vector) andgene_local_to_universe(0-indexed integer vector,NAfor genes absent from the universe).universe_size(
integer)
Number of genes in the intersection universe.renormalise(
logical)
IfTRUE, recomputedata_normagainsttarget_sizeusing each cell's surviving raw counts. IfFALSE, passdata_normthrough untouched; the caller must guarantee all inputs were normalised against the sametarget_size.target_size(
numeric)
Target library size for renormalisation. Ignored whenrenormalise = FALSE.verbose(
logical)
Controls verbosity.
Method 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.
Arguments
f_path_source(
character)
Path to the rawcounts_cells.bin, which must still contain the empty droplets.samples(
list)
A list of lists. Each inner list must containsample_id,real_cellsandempty_cells(0-indexed integer vectors of store indices),celltype_idx(0-indexed integer vector, one entry perreal_cellsentry) andn_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)0silent,1per-sample progress,2per-EM-iteration.