Package {scFlex}


Type: Package
Title: Flexible Conversion Between Single-Cell Data Objects
Version: 0.1.0
Description: Provides conversion among 'Seurat', 'SingleCellExperiment', 'AnnData', and 'Loom' single-cell data representations while preserving expression matrices, cell and feature metadata, and dimensionality reductions when supported by the target format. The package performs alignment and validity checks during conversion and reports unsupported or unavailable components rather than silently reconstructing them.
License: MIT + file LICENSE
Encoding: UTF-8
Depends: R (≥ 4.2.0)
Imports: Matrix, methods, reticulate (≥ 1.41.0), SeuratObject
Suggests: hdf5r, Seurat, SingleCellExperiment, SummarizedExperiment, S4Vectors, testthat (≥ 3.0.0)
Config/testthat/edition: 3
URL: https://github.com/mohamednhassan/scFlex, https://mohamednhassan.github.io/scFlex/
BugReports: https://github.com/mohamednhassan/scFlex/issues
Config/roxygen2/version: 8.1.0
NeedsCompilation: no
Packaged: 2026-09-18 14:21:27 UTC; mhassan
Author: Mohamed N. Hassan [aut, cre]
Maintainer: Mohamed N. Hassan <m.nhassan@hotmail.com>
Repository: CRAN
Date/Publication: 2026-09-29 13:30:07 UTC

scFlex: Preservation-Aware Single-Cell Interoperability

Description

Tools for conversion among Seurat, SingleCellExperiment, AnnData, and Loom representations.


Collapse values for display

Description

Collapses a vector into a comma-separated string and returns "None" for null or empty input.

Usage

collapse_or_none(x)

Arguments

x

A vector-like object to collapse.

Value

A single character string.


Convert AnnData to Loom

Description

Reads an AnnData H5AD file and writes a Loom file using loompy, preserving supported matrices, cell and feature attributes, and reductions where representable.

Usage

convert_anndata_to_loom(input, output)

Arguments

input

Path to an input .h5ad file.

output

Path to the output .loom file.

Details

Loom is a more limited interchange format than Seurat, SingleCellExperiment, or AnnData, so only components representable by the current mapping are preserved.

Value

The output path, returned invisibly.


Convert AnnData to SingleCellExperiment

Description

Converts an AnnData H5AD file to a SingleCellExperiment object while preserving counts, normalized expression when available, cell metadata, feature metadata, and dimensional reductions.

Usage

convert_anndata_to_sce(input, output)

Arguments

input

Path to an AnnData H5AD file.

output

Path where the converted SingleCellExperiment RDS file should be written.

Details

An explicit layers["counts"] matrix is preferred as the counts assay. Fractional values are accepted in an explicit counts layer as long as all values are finite and non-negative. If no explicit counts layer is present, X is used as counts only when its values are finite, non-negative, and integer-like.

Normalized expression is optional. If layers["logcounts"] is present it is stored as the SCE logcounts assay. Otherwise, X is used as logcounts when it was not already used as the counts matrix.

Value

Invisibly returns the output file path.


Convert AnnData to Seurat

Description

Reads an AnnData H5AD file and writes a Seurat RDS object while preserving supported counts, normalized expression, metadata, feature metadata, and dimensional reductions.

Usage

convert_anndata_to_seurat(input, output)

Arguments

input

Path to an input .h5ad file.

output

Path to the output Seurat .rds file.

Value

The output path, returned invisibly.


Convert Loom to AnnData

Description

Reads a Loom file through AnnData and writes the resulting object as H5AD while preserving components supported by the Loom-to-AnnData reader.

Usage

convert_loom_to_anndata(input, output)

Arguments

input

Path to an input .loom file.

output

Path to the output .h5ad file.

Details

Loom is a more limited interchange format than Seurat, SingleCellExperiment, or AnnData, so only components representable by the current mapping are preserved.

Value

The output path, returned invisibly.


Convert Loom to SingleCellExperiment

Description

Reads a Loom file through AnnData and creates a SingleCellExperiment while preserving supported count data, normalized data, metadata, feature metadata, and reductions.

Usage

convert_loom_to_sce(input, output)

Arguments

input

Path to an input .loom file.

output

Path to the output SingleCellExperiment .rds file.

Details

Raw counts are selected conservatively from an explicit counts layer when available or from X when it is non-negative and integer-like. The function does not guess raw counts from arbitrary named layers. Loom is a more limited interchange format than Seurat, SingleCellExperiment, or AnnData, so only components representable by the current mapping are preserved.

Value

The output path, returned invisibly on success. If suitable raw count data cannot be identified, the function may return NULL invisibly without writing an output object.


Convert Loom to Seurat

Description

Reads a Loom file through AnnData and creates a Seurat object while preserving supported count data, metadata, feature metadata, and reductions.

Usage

convert_loom_to_seurat(input, output)

Arguments

input

Path to an input .loom file.

output

Path to the output Seurat .rds file.

Details

Raw counts are selected conservatively from an explicit counts layer when available or from X when it is non-negative and integer-like. The function does not guess raw counts from arbitrary named layers. Loom is a more limited interchange format than Seurat, SingleCellExperiment, or AnnData, so only components representable by the current mapping are preserved.

Value

The output path, returned invisibly on success. If suitable raw count data cannot be identified, the function may return NULL invisibly without writing an output object.


Convert between supported single-cell formats

Description

Dispatches a file conversion between Seurat, SingleCellExperiment, AnnData, and Loom using the corresponding scFlex conversion function.

Usage

convert_sc(input, source, destination, output)

Arguments

input

Character string giving the path to the input file.

source

Character string naming the source format. Supported values are "seurat", "sce", "anndata", and "loom". Matching is case-insensitive.

destination

Character string naming the destination format. Supported values are "seurat", "sce", "anndata", and "loom". Matching is case-insensitive.

output

Character string giving the output file path.

Details

Source and destination must differ. Unsupported format names or unimplemented conversion paths produce an error.

Value

The result returned by the selected conversion function, invisibly. Current converters normally return the output path invisibly; some conversions may return NULL invisibly when required count data cannot be identified.


Convert SingleCellExperiment to AnnData

Description

Reads a SingleCellExperiment RDS object and writes an H5AD AnnData file, mapping supported assays, metadata, feature metadata, and reduced dimensions.

Usage

convert_sce_to_anndata(input, output)

Arguments

input

Path to an input SingleCellExperiment .rds file.

output

Path to the output .h5ad file.

Value

The output path, returned invisibly.


Convert SingleCellExperiment to Loom

Description

Reads a SingleCellExperiment RDS object and writes a Loom file using loompy, preserving supported assays, attributes, and reductions where representable.

Usage

convert_sce_to_loom(input, output)

Arguments

input

Path to an input SingleCellExperiment .rds file.

output

Path to the output .loom file.

Details

Loom is a more limited interchange format than Seurat, SingleCellExperiment, or AnnData, so only components representable by the current mapping are preserved.

Value

The output path, returned invisibly.


Convert SingleCellExperiment to Seurat

Description

Reads a SingleCellExperiment RDS object and writes a Seurat RDS object while preserving supported counts, normalized expression, metadata, feature metadata, and dimensional reductions.

Usage

convert_sce_to_seurat(input, output)

Arguments

input

Path to an input SingleCellExperiment .rds file.

output

Path to the output Seurat .rds file.

Value

The output path, returned invisibly.


Convert a classic Seurat Assay to Assay5

Description

Converts a selected classic Seurat Assay to a Seurat v5 Assay5 while preserving the supported assay data and object structure.

Usage

convert_seu_classic_to_v5(input, output, assay = "RNA")

Arguments

input

Path to an input Seurat .rds file.

output

Path to the output Seurat .rds file.

assay

Name of the assay to convert. Defaults to "RNA".

Value

The output path, returned invisibly.


Convert a Seurat Assay5 assay to a classic Assay

Description

Converts a selected Seurat v5 Assay5 assay to a classic Seurat Assay while preserving the supported assay data and object structure.

Usage

convert_seu_v5_to_classic(input, output, assay = "RNA")

Arguments

input

Path to an input Seurat .rds file.

output

Path to the output Seurat .rds file.

assay

Name of the assay to convert. Defaults to "RNA".

Value

The output path, returned invisibly.


Convert Seurat to AnnData

Description

Reads a Seurat RDS object and writes an H5AD AnnData file while preserving supported counts, normalized expression, metadata, feature metadata, and dimensional reductions.

Usage

convert_seurat_to_anndata(input, output, assay = "RNA")

Arguments

input

Path to an input Seurat .rds file.

output

Path to the output .h5ad file.

assay

Name of the Seurat assay to convert. Defaults to "RNA".

Value

The output path, returned invisibly.


Convert Seurat to Loom

Description

Reads a Seurat RDS object and writes a Loom file using loompy, preserving supported expression matrices, attributes, and reductions where representable.

Usage

convert_seurat_to_loom(input, output)

Arguments

input

Path to an input Seurat .rds file.

output

Path to the output .loom file.

Details

Loom is a more limited interchange format than Seurat, SingleCellExperiment, or AnnData, so only components representable by the current mapping are preserved.

Value

The output path, returned invisibly.


Convert Seurat to SingleCellExperiment

Description

Reads a Seurat RDS object and writes a SingleCellExperiment RDS object while preserving supported counts, normalized expression, metadata, feature metadata, and dimensional reductions.

Usage

convert_seurat_to_sce(input, output)

Arguments

input

Path to an input Seurat .rds file.

output

Path to the output SingleCellExperiment .rds file.

Value

The output path, returned invisibly.


Inspect an AnnData object

Description

Internal helper that reads an H5AD file and reports its dimensions, primary matrix, layers, cell-level matrices, graphs, feature-level matrices, unstructured metadata, raw data, metadata columns, and a sample of cell names.

Usage

inspect_anndata(path_to_file)

Arguments

path_to_file

Path to an H5AD file.

Value

Invisibly returns a list containing information about the AnnData object.


Inspect a Loom file

Description

Internal helper that reads the HDF5 structure of a Loom file and reports its dimensions, layers, cell and feature attributes, graphs, and a sample of cell names when available.

Usage

inspect_loom(obj)

Arguments

obj

Path to a Loom file.

Value

Invisibly returns a list containing information about the Loom file.


Inspect an R single-cell object

Description

Internal helper that identifies whether an object read from an RDS file is a Seurat or SingleCellExperiment object and dispatches it to the corresponding inspection function.

Usage

inspect_rds_object(obj)

Arguments

obj

An R object read from an RDS file.

Value

Invisibly returns a list containing information about the object.


Inspect a single-cell object

Description

Inspects the structure and contents of a supported single-cell data file without performing a conversion. Supported inputs include Seurat and SingleCellExperiment objects stored as RDS files, AnnData H5AD files, and Loom files.

Usage

inspect_sc(path_to_file)

Arguments

path_to_file

Path to an RDS, H5AD, or Loom file.

Details

The function reports basic information such as the number of cells and features, available assays or layers, dimensional reductions, metadata, and a sample of cell names. Additional format-specific information is reported when available.

Value

Invisibly returns a list containing information about the inspected object. The contents of the list depend on the input format.

Examples

if (requireNamespace("Seurat", quietly = TRUE)) {
  counts <- matrix(
    c(1, 0, 3, 0, 2, 1),
    nrow = 2,
    dimnames = list(
      c("Gene1", "Gene2"),
      c("Cell1", "Cell2", "Cell3")
    )
  )
  obj <- Seurat::CreateSeuratObject(counts = counts)
  path <- tempfile(fileext = ".rds")
  saveRDS(obj, path)
  inspect_sc(path)
  unlink(path)
}


Inspect a SingleCellExperiment object

Description

Internal helper that reports the structure of a SingleCellExperiment object, including its dimensions, assays, reduced dimensions, alternative experiments, cell metadata, feature metadata, and a sample of cell names.

Usage

inspect_sce(obj)

Arguments

obj

A SingleCellExperiment object.

Value

Invisibly returns a list containing information about the SingleCellExperiment object.


Inspect a Seurat object

Description

Internal helper that reports the structure of a Seurat object, including its dimensions, assays, default assay structure, reductions, graphs, neighbors, metadata columns, and a sample of cell names.

Usage

inspect_seurat(obj)

Arguments

obj

A Seurat object.

Value

Invisibly returns a list containing information about the Seurat object.


Sanitize cell metadata for AnnData

Description

Checks cell metadata columns and simplifies supported one-column matrix or data-frame columns before conversion to AnnData obs.

Usage

sanitize_obs(obs)

Arguments

obs

A data frame containing cell-level metadata with cells as row names.

Value

A sanitized data frame with the original row names preserved.