adata.Load: Seurat to AnnData Conversion and Management Tools

View source: R/Anndata.R

adata.LoadR Documentation

Seurat to AnnData Conversion and Management Tools

Description

This documentation encompasses a suite of functions designed to facilitate the conversion and management of data between Seurat objects and AnnData structures. The functions cover the entire workflow from initial conversion of Seurat objects to Loom or AnnData formats, adding supplementary data like dimension reductions and metadata, and saving or loading these objects for further analysis.

Usage

adata.Load(adata.path, conda_env = "seuratextend")

adata.Save(adata.path, conda_env = "seuratextend")

adata.AddDR(
  seu,
  dr = NULL,
  prefix = "X_",
  scv.graph = FALSE,
  load.adata = NULL,
  save.adata = NULL,
  conda_env = "seuratextend"
)

adata.AddMetadata(
  seu,
  col,
  load.adata = NULL,
  save.adata = NULL,
  conda_env = "seuratextend"
)

adata.LoadLoom(loompath, save.adata = NULL, conda_env = "seuratextend")

Seu2Adata(
  seu,
  add.normdata = TRUE,
  save.adata = NULL,
  conda_env = "seuratextend"
)

Seu2Loom(
  seu,
  filename,
  add.normdata = FALSE,
  add.metadata = TRUE,
  layers = NULL,
  overwrite = FALSE
)

Arguments

adata.path

Path to the AnnData (.h5ad) file where the data is to be saved or from which data is to be loaded.

conda_env

Name of the Conda environment where the necessary Python libraries are installed. This environment is used to run Python code from R, bridging Seurat and AnnData functionalities. Default: 'seuratextend'.

seu

The Seurat object from which data is being converted or managed.

dr

Specifies which dimension reductions from the Seurat object should be transferred to the AnnData object. If NULL, all available reductions are included. Default: NULL.

prefix

A prefix to add to the dimension reduction names when importing them into the AnnData object's 'obsm' attribute. This helps in differentiating various data types and maintaining order within the dataset. Default: 'X_'.

scv.graph

Optional; a boolean indicating whether to calculate the velocity graph after importing dimension reduction data. This is useful for kinetic analyses in single-cell studies. Default: FALSE.

load.adata

Optional; path to a previously saved AnnData object to be loaded. Default: NULL. Useful for resuming analyses or integrating additional data without repeating preprocessing steps.

save.adata

Optional; specifies the file path where the AnnData object should be saved after processing. The file will be saved in the .h5ad format. This parameter facilitates easy storage of processed data.

col

The name of the metadata column in the Seurat object that needs to be transferred to the AnnData object.

loompath

Path to the loom file that needs to be loaded into an AnnData object.

add.normdata

Boolean indicating whether to also include the normalized gene expression matrix in the loom file.

filename

Filename for the loom file to be created or accessed. It specifies where the loom file will be stored or is stored, which is essential for data conversion tasks.

add.metadata

Boolean indicating whether to also include the meta.data in the loom file.

layers

Optional; a list of additional matrices to include as layers in the loom file, such as velocyto's spliced/unspliced matrices. Each matrix must match the transposed dimensions of the Seurat object (row for cells and columns for genes). Default: NULL.

Details

These functions are designed to ensure seamless interoperability between R, used primarily for Seurat, and Python, used for AnnData via the reticulate package:

- 'Seu2Adata' directly converts a Seurat object into an AnnData object, optionally adding normalized data and saving the result.

- 'Seu2Loom' converts a Seurat object into a Loom file, allowing for intermediate storage or further conversion.

- 'adata.LoadLoom' initiates an AnnData object from a Loom file, useful for integrating with Python-based analyses.

- 'adata.AddDR' appends dimension reduction results from Seurat to an AnnData object, essential for preserving comprehensive analytical context.

- 'adata.AddMetadata' transfers additional metadata from Seurat to AnnData, facilitating enriched data analyses.

- 'adata.Save' provides functionality to save AnnData objects to disk, securing data for future use.

- 'adata.Load' retrieves AnnData objects from disk, ensuring continuity of analysis across sessions.

These tools are integral for researchers employing hybrid workflows that leverage the strengths of both Seurat and Scanpy/AnnData environments, supporting a wide range of computational tasks from data preprocessing to advanced analyses.

Value

Depending on the function called, this suite returns either a modified Seurat object or an AnnData object, or it may perform save/load operations without returning an object. Specifically:

- 'Seu2Adata' and 'Seu2Loom' convert Seurat objects to AnnData and Loom formats, respectively.

- 'adata.LoadLoom' loads a Loom file into an AnnData object.

- 'adata.AddDR' and 'adata.AddMetadata' enrich an AnnData object with additional dimension reduction data and metadata from a Seurat object.

- 'adata.Save' and 'adata.Load' handle saving to and loading from disk operations for AnnData objects.

Examples

library(Seurat)
library(SeuratExtend)
library(reticulate)

# Convert Seurat object to loom file and save it locally
pbmc_loom_path <- file.path(tempdir(), "pbmc3k_small.loom")
Seu2Loom(pbmc, filename = pbmc_loom_path, add.normdata = TRUE)

# Load the loom file into an AnnData object
adata.LoadLoom(loompath = pbmc_loom_path)

# Use reticulate to execute Python code and print the AnnData object details
py_run_string("print(adata)")

# Add dimension reduction data from Seurat to AnnData
adata.AddDR(pbmc)
py_run_string("print(adata)")

# Directly convert Seurat object to AnnData object including all processing steps
Seu2Adata(pbmc)

# Update AnnData with new metadata from Seurat
pbmc$cluster2 <- pbmc$cluster
adata.AddMetadata(pbmc, col = "cluster2")
py_run_string("print(adata)")

# Save the AnnData object to a local file
pbmc_adata_path <- file.path(tempdir(), "pbmc3k_small.h5ad")
adata.Save(pbmc_adata_path)

# Load an existing AnnData object from file
adata.Load(pbmc_adata_path)


huayc09/SeuratExtend documentation built on July 15, 2024, 6:22 p.m.