Package 'ABCatlasR'

Title: Release-Aware Access to Allen Brain Cell Atlas Data
Description: Provides an R-native, release-aware interface to the Allen Brain Cell (ABC) Atlas <https://brain-map.org/bkp/explore/abc-atlas>. Discovers supported dataset routes, assembles validated cell and gene metadata, reports the expression files and disk space a request needs, and reads the expression of selected cells and genes from H5AD files directly in R. Expression can be returned as a sparse matrix bundle, a SingleCellExperiment, or a SpatialExperiment object. The package preserves source representation and provenance and distinguishes raw counts from upstream transformed or imputed expression. Atlas files are downloaded on first use and cached locally, so an internet connection is needed the first time each file is accessed.
Authors: Uday Rangaswamy [aut] (ORCID: <https://orcid.org/0009-0007-2758-9213>), Mahdi Komeili [aut, cre] (ORCID: <https://orcid.org/0009-0000-3760-5327>), Federico Caretti [aut] (ORCID: <https://orcid.org/0000-0002-6963-5245>), Idris Kouadri Boudjelthia [aut] (ORCID: <https://orcid.org/0000-0002-3139-4861>), Maria Musarra Pizzo [aut], Amir Fardin [aut] (ORCID: <https://orcid.org/0000-0002-9472-408X>), Andres Hernandez-Clavijo [aut], Remo Sanges [aut] (ORCID: <https://orcid.org/0000-0002-5047-9713>)
Maintainer: Mahdi Komeili <[email protected]>
License: MIT + file LICENSE
Version: 0.99.0
Built: 2026-11-25 20:11:31 UTC
Source: https://github.com/BiocStaging/ABCatlasR

Help Index


Connect to a local ABC Atlas cache

Description

Opens the files of one ABC Atlas release in a local folder and returns an object that finds, downloads and reads them. load_data() and fetch_data() use it internally; call it yourself only to work with individual ABC Atlas files.

Usage

abc_cache_connect(
  download_base = NULL,
  offline = FALSE,
  release = .abc_default_release
)

Arguments

download_base

Folder for ABC Atlas files. If NULL (default), the folder given by abc_download_dir() is used.

offline

If TRUE, use only files already in the folder and never connect to the internet. The release must have been used online once before. If FALSE (default) and the ABC Atlas server cannot be reached, the connection continues offline with the files already in the folder.

release

ABC Atlas release in YYYYMMDD form. Defaults to 20261130, the release validated against this package. NULL selects the release as described in Details.

Details

A file is downloaded the first time it is needed and checked against the checksum the ABC Atlas publishes for it. With offline = TRUE, only files already in the folder are used: nothing is downloaded or written.

By default the release this package was validated against is used. With release = NULL, the release is chosen as the Python abc_atlas_access package chooses it: the one last used with this folder, with a warning if a newer one exists, or else the newest. Files are stored with the same layout as in the Python package, so the two can share a folder.

Value

An abc_cache object: a list of class "abc_cache" with these elements:

download_base

The folder.

manifest_name, version

The release file list in use and its release date.

offline

Whether downloads are switched off.

get_file_path(directory, file_name)

Function that returns the local path of a file, downloading it if needed.

get_metadata_dataframe(directory, file_name, col_classes, ...)

Function that reads a metadata CSV file into a data frame; ... is passed to data.table::fread().

Examples

example_dir <- system.file(
    "extdata", "ex",
    package = "ABCatlasR"
)

cache <- abc_cache_connect(
    example_dir,
    offline = TRUE,
    release = "20250101"
)
cache

Find the folder where ABC Atlas files are stored

Description

Returns the folder where ABC Atlas files are downloaded and kept for reuse. It is the first of these that is set:

  1. the explicit path argument;

  2. getOption("ABCatlasR.download_base");

  3. the ABC_ATLAS_CACHE environment variable;

  4. a per-user cache directory, tools::R_user_dir("ABCatlasR", "cache").

The folder is created if it does not exist yet.

Usage

abc_download_dir(path = NULL)

Arguments

path

Folder to use. If NULL or empty, the other settings above are checked in order.

Value

The full path of the folder.

Examples

# Use a temporary cache directory.
cache_dir <- file.path(tempdir(), "ABCatlasR-example")
abc_download_dir(cache_dir)

Fetch ABC Atlas expression for selected cells and genes

Description

Downloads the expression files that hold the selected cells, unless they were downloaded before, and reads the expression of those cells and genes into R. plan_fetch() can show the files and their sizes first, but is not needed.

Usage

fetch_data(
  data,
  cell_ids,
  genes = NULL,
  data_type = NULL,
  return_type = c("matrix", "sce", "spatial"),
  offline = FALSE
)

Arguments

data

The object returned by load_data(). It provides the metadata, the release and the folder.

cell_ids

Labels of the cells to fetch, from the cell_label column of data$cell_metadata. The cells are returned in this order. To fetch every cell, use data$cell_metadata$cell_label.

genes

Genes to fetch, given as gene identifiers or gene symbols. Each name is looked up in gene_identifier, then in source_gene_identifier where the dataset has it, then in gene_symbol. A symbol that belongs to several genes is reported so that you can choose by identifier. If NULL (default), every gene in data$gene_data is returned.

data_type

Type of expression values: "raw" for counts or "log2" for the log2-transformed values the ABC Atlas publishes. The spatial HMBA datasets instead offer "raw", "batch_normalized" (counts normalized for differences between batches) and "ln" (the natural log of the batch-normalized values). list_datasets() shows the types of each dataset. If NULL (default), "raw" is used where the dataset publishes it, otherwise "log2". Values are returned as published; they are never converted from one type to another.

return_type

Form of the result: "matrix" (default) for a sparse matrix with its cell and gene tables, "sce" for a SingleCellExperiment, or "spatial" for a SpatialExperiment with the cells' spatial coordinates. "spatial" needs a dataset with spatial coordinates (see list_datasets()). For Seurat or Monocle 3, see "Converting to other formats" below.

offline

If TRUE, use only files already in the folder and never connect to the internet; a missing file is an error instead of a download. If FALSE (default) and the ABC Atlas server cannot be reached, the files already in the folder are used, as with TRUE.

Details

Before downloading, the free space on the disk holding the folder is checked, and a download that does not fit is refused. On the disk the operating system runs from, at least 2 GB must also remain free afterwards, so the system keeps room for virtual memory. Nothing is checked when every file was downloaded before. The same check runs before every other download, including the metadata read by load_data().

Value

The expression, in the form chosen by return_type.

For return_type = "matrix", an abc_matrix object: a list of class c("abc_matrix", "list") with these elements:

expression

Sparse dgCMatrix (genes x cells). Rows are named by gene_identifier, columns by cell_label, in the order requested.

cell_metadata

Data frame with one row per selected cell, in the order of the matrix columns.

gene_data

Data frame with one row per selected gene, in the order of the matrix rows.

data_type

The type of expression values, as in the data_type argument.

provenance

List recording where the data come from: dataset, release, data_type, modality, spatial, expression_directories, n_cells and n_genes.

For "sce", a SingleCellExperiment, and for "spatial", a SpatialExperiment. The expression is in the counts assay for raw data and otherwise in an assay named after the data type (for example log2 or ln), the cell and gene metadata are in colData and rowData, and the provenance list is in metadata(x)$ABCatlasR. A SpatialExperiment also holds the spatial coordinates in spatialCoords, and its provenance records their columns in coordinate_columns.

Converting to other formats

This package does not create Seurat or Monocle 3 objects. Fetch raw counts with return_type = "matrix" and convert them with the package you use; both expect counts, so use data_type = "raw".

x <- fetch_data(abc, cell_ids = cells, genes = genes, data_type = "raw")

# Seurat (package SeuratObject). Seurat replaces "_" in feature names.
counts <- x$expression
rownames(counts) <- gsub("_", "-", rownames(counts))
seu <- SeuratObject::CreateSeuratObject(
    counts = counts,
    meta.data = x$cell_metadata
)

# Monocle 3 (package monocle3, installed from GitHub).
gene_metadata <- x$gene_data
gene_metadata$gene_short_name <- gene_metadata$gene_symbol
cds <- monocle3::new_cell_data_set(
    x$expression,
    cell_metadata = x$cell_metadata,
    gene_metadata = gene_metadata
)

Examples

# A small synthetic dataset shipped with the package.
example_dir <- system.file("extdata", "ex", package = "ABCatlasR")
abc <- readRDS(file.path(example_dir, "example-data.rds"))
abc$download_base <- example_dir

cells <- c("c4", "c1")
x <- fetch_data(
    abc,
    cell_ids = cells,
    genes = c("G1", "G4"),
    offline = TRUE
)
x$expression

List the ABC Atlas datasets you can load

Description

Returns one row per dataset the package supports, with the name to pass to load_data(). The list is part of the package, so this needs no internet connection and downloads nothing.

Usage

list_datasets()

Value

A data frame of class c("abc_dataset_catalog", "data.frame") with one row per dataset and these columns:

dataset

Dataset name, used as dataset in load_data().

modality

Kind of data, for example "spatial_transcriptomics".

spatial

Whether the cells have spatial coordinates, so that return_type = "spatial" can be used.

imputed

Whether the expression values are imputed rather than measured.

default_data_type, available_data_types

The type of expression values used by default and all the types the dataset offers (see the data_type argument of fetch_data()).

cell_directory, gene_directory, taxonomy, expression_directories

The ABC Atlas folders the dataset is read from.

Examples

datasets <- list_datasets()
datasets[datasets$spatial, ]

Load the cell and gene metadata of an ABC Atlas dataset

Description

Reads the metadata of every cell and gene in one dataset, together with its annotation tables such as cell types, donors and spatial positions. The files are downloaded the first time and reused afterwards. Gene expression is not downloaded here; see fetch_data().

Usage

load_data(
  dataset,
  download_base = NULL,
  offline = FALSE,
  release = .abc_default_release
)

Arguments

dataset

Dataset name, for example "WHB", "AgingMouse" or "MERFISH"; list_datasets() shows all of them. The start of a name is enough if it matches only one dataset.

download_base

Folder for ABC Atlas files. If NULL (default), the folder given by abc_download_dir() is used.

offline

If TRUE, use only files already in the folder and never connect to the internet. If FALSE (default) and the ABC Atlas server cannot be reached, the files already in the folder are used, as with TRUE.

release

ABC Atlas release in YYYYMMDD form. Defaults to 20261130, the release validated against this package. Metadata are read from exactly that release. Release 20261031 renamed the folders of the HMBA datasets; releases from before that are read with the earlier folder names. If a release lacks a folder the dataset needs, loading stops with a message.

Details

Choose cells and genes from the returned tables, then pass their labels and names to fetch_data(). The returned object also records the release and the folder, so plan_fetch() and fetch_data() use the same ones.

Value

An abc_data object: a list of class c("abc_data", "list") with these elements:

cell_metadata

Data frame with one row per cell; the row names are the cell_label values. It combines the published cell metadata with the annotation tables that exist for the dataset: cell type names and colours, donor, library and spatial information. Attributes on the data frame (abc_dataset, abc_modality, abc_spatial, abc_default_data_type, abc_available_data_types and abc_expression_directories) tell plan_fetch() and fetch_data() where the expression is stored, so pass the object on as returned.

gene_data

Data frame with one row per gene. The gene_identifier column identifies each gene uniquely; gene_symbol is included where the ABC Atlas publishes it.

unique_values

Named list with the different values found in the main metadata columns, to help choose cells.

dataset

Dataset name, as listed by list_datasets().

download_base

Folder used for the files.

release

ABC Atlas release the metadata come from.

Examples

# Cache the downloaded metadata in a temporary folder.
abc <- load_data(
    "HMBA_PatchSeq",
    download_base = file.path(tempdir(), "ABCatlasR")
)
abc
dim(abc$cell_metadata)

Report the expression files a request needs

Description

Shows which expression files fetch_data() would need for the selected cells: their sizes, which were downloaded before and how much is still to download, and whether that fits on the disk. Nothing is downloaded. Using plan_fetch() is optional; fetch_data() does not need it.

Usage

plan_fetch(data, cell_ids = NULL, data_type = NULL, offline = FALSE)

Arguments

data

The object returned by load_data().

cell_ids

Labels of the cells, from the cell_label column of data$cell_metadata. If NULL (default), every cell of the dataset is included.

data_type

Type of expression values, as in fetch_data(): "raw", "log2", and for the spatial HMBA datasets "batch_normalized" or "ln". If NULL (default), "raw" is used where the dataset publishes it, otherwise "log2".

offline

If TRUE, use only files already in the folder and never connect to the internet. If FALSE (default) and the ABC Atlas server cannot be reached, the files already in the folder are used, as with TRUE.

Details

Expression files are downloaded whole, so the sizes do not depend on the genes. They depend on the cells, because some datasets spread their cells over several files, and on data_type.

Value

An abc_fetch_plan object, which prints as a report: a list of class "abc_fetch_plan" with these elements:

dataset, release, data_type

The dataset, ABC Atlas release and type of expression values.

n_cells

Number of selected cells.

files

Data frame with one row per expression file: dataset_label, feature_matrix_label, data_type, n_cells (selected cells in the file), size (bytes), relative_path, local_path, cached (downloaded before), partial_bytes (bytes of an interrupted download that will be resumed) and additional_download (bytes still to download).

total_size, total_partial, total_download

Sums of size, partial_bytes and additional_download, in bytes.

storage

The disk space check fetch_data() makes before downloading: available (free bytes, or NA if unknown), download, system_disk, headroom (space that must stay free), required and sufficient.

Examples

# A small synthetic dataset shipped with the package.
example_dir <- system.file("extdata", "ex", package = "ABCatlasR")
abc <- readRDS(file.path(example_dir, "example-data.rds"))
abc$download_base <- example_dir

plan_fetch(abc, offline = TRUE)

# Exact cells: only the files holding them are listed.
plan_fetch(abc, cell_ids = c("c1", "c2"), offline = TRUE)