In this article we illustrate how to extract the normalized expression matrix from the objects downloaded from the Trailmaker Insights module, both for Seurat (.rds) and AnnData (.h5ad) projects.
This approach is particularly useful when you need the full normalized matrix for downstream analysis outside of Trailmaker.
If, instead, you need the matrix for only a subset of the dataset, exporting directly from the Plots and Tables module in Trailmaker is typically more convenient.
Extracting the normalized matrix from a Seurat object
1. Load the Seurat object in R:
library(Seurat)
scdata <- readRDS("your_seurat_object.rds")
Trailmaker stores:
- Raw counts in: scdata@assays$RNA@counts
- Normalized data in: scdata@assays$RNA@data
- Scaled data in: scdata@assays$RNA@scale.data
The normalized expression matrix is stored in the data slot.
2. Extract the normalized matrix:
normalized_matrix <- GetAssayData(scdata, assay = "RNA", slot = "data")Alternatively:
normalized_matrix <- scdata@assays$RNA@dataThis returns a sparse matrix (genes × cells).
3. Optional - Convert to a dense matrix:
normalized_matrix_dense <- as.matrix(normalized_matrix)Depending on the downstream usage, you might want to convert to a dense matrix. Be aware that converting very large datasets to dense format can consume significant memory.
4. Export to CSV:
write.csv(as.matrix(normalized_matrix), file = "path/to/output_folder/normalized_expression_matrix.csv", quote = F)
Extracting normalized matrix from an AnnData object
1. Load the AnnData object in Python:
import scanpy as sc
adata = sc.read_h5ad("your_AnnData_object.h5ad")Trailmaker stores:
- Raw counts in: adata.X
- Alternative representations (e.g., normalized, scaled) in: adata.layers
The normalized expression matrix is stored in adata.layers.
2. Extract the normalized matrix:
normalized_matrix = adata.layers["normalized"]3. Optional - Convert to a dense matrix:
import pandas as pd
normalized_df = pd.DataFrame(
normalized_matrix.toarray(),
index=adata.obs["barcode"],
columns = adata.var_names
)
Depending on the downstream usage, you might want to convert to a dense matrix. Be aware that converting very large datasets to dense format can consume significant memory.
4. Export to CSV:
normalized_df.to_csv("path/to/output_folder/normalized_expression.csv")