snapatac2.read_10x_mtx#

snapatac2.read_10x_mtx(path, file=None, prefix=None)[source]#

Read a 10x Genomics MTX directory into an AnnData object.

Use this function to load a directory containing a matrix file, feature file, and barcode file in the 10x MTX layout. Pass file to create a backed AnnData object on disk instead of returning a fully in-memory object.

Anti-Patterns#

  • Do NOT pass the path to matrix.mtx directly; pass the directory that contains the matrix, feature, and barcode files.

  • Do NOT include multiple matching matrix, feature, or barcode files with the same prefix in the directory; exactly one of each file type must match.

param path:

Directory containing the 10x MTX files. The directory must contain one matching file from each group:

  1. Matrix: “matrix.mtx” or “matrix.mtx.gz”.

  2. Features: “genes.tsv”, “genes.tsv.gz”, “features.tsv”, or “features.tsv.gz”.

  3. Barcodes: “barcodes.tsv” or “barcodes.tsv.gz”.

type path:

Path

param file:

Output h5ad filename for a backed AnnData object. If None, return an in-memory AnnData object.

type file:

Path | None

param prefix:

Optional filename prefix before the matrix, feature, and barcode names. For files named patientA_matrix.mtx, patientA_genes.tsv, and patientA_barcodes.tsv, pass prefix="patientA_".

type prefix:

str | None

returns:

AnnData object with observations from the barcode file, variables from the feature file, and .X containing the transposed sparse count matrix.

rtype:

AnnData

Examples

>>> from pathlib import Path
>>> import snapatac2 as snap
>>> adata = snap.read_10x_mtx(Path("filtered_feature_bc_matrix"))
>>> adata.n_obs >= 0
True