Loading data¶
From 10x output¶
read_10x
¶
read_10x(data_dir: Union[str, Path], var_names: str = 'gene_symbols', make_unique: bool = True) -> tuple[csc_matrix, list[str], list[str]]
Read 10X Genomics output directory.
Mirrors R's Read10X(). Supports both v2 (genes.tsv) and v3 (features.tsv.gz) directory layouts.
Parameters:
-
data_dir(Union[str, Path]) –path to the 10X output directory
-
var_names(str, default:'gene_symbols') –'gene_symbols' (default) or 'gene_ids'
-
make_unique(bool, default:True) –append suffix to duplicate gene names
Returns:
-
(matrix, feature_names, cell_names)– -
matrix is (features × cells) csc_matrix.–
Source code in truecell/io.py
Bundled datasets¶
truecell.datasets stands in for R's SeuratData. Each loader downloads on first
call into ~/.truecell_data/ and returns a ready Truecell object; the whole set is
roughly 770 MB cached. These are the datasets the tutorials
run on, which is what makes each tutorial reproducible from a clean machine.
pbmc3k
¶
pbmc3k(data_dir: Optional[str] = None, force_download: bool = False) -> tuple[csc_matrix, list[str], list[str]]
Download (if needed) and load the PBMC 3k dataset.
Returns (counts_matrix, gene_names, cell_barcodes). matrix is (genes × cells) csc_matrix with raw counts.
Parameters:
-
data_dir(Optional[str], default:None) –directory to cache the raw files (defaults to ~/.truecell_data/pbmc3k)
-
force_download(bool, default:False) –re-download even if files exist
Source code in truecell/datasets.py
pbmc8k
¶
pbmc8k(data_dir: Optional[str] = None, force_download: bool = False) -> tuple[csc_matrix, list[str], list[str]]
Download (if needed) and load the 10x Genomics PBMC 8k dataset.
~8,400 peripheral blood mononuclear cells (GRCh38, v2 chemistry). Larger
than pbmc3k and used by the advanced subclustering tutorial.
Returns (counts_matrix, gene_names, cell_barcodes); matrix is (genes x cells) csc_matrix with raw counts.
Parameters:
-
data_dir(Optional[str], default:None) –directory to cache the raw files (defaults to ~/.truecell_data/pbmc8k)
-
force_download(bool, default:False) –re-download even if files exist
Source code in truecell/datasets.py
cbmc_citeseq
¶
cbmc_citeseq(data_dir: Optional[str] = None, force_download: bool = False, species_prefix: str = _CBMC_SPECIES_PREFIX)
Download (if needed) and load the CBMC CITE-seq dataset (GSE100866).
~8,600 cord-blood mononuclear cells profiled for both RNA and 13 surface
proteins (ADT). The RNA matrix mixes human and mouse spike-in genes; this
loader keeps the human genes and strips the HUMAN_ prefix (mirroring
Seurat's CollapseSpeciesExpressionMatrix). RNA and ADT are aligned to their
shared cell barcodes.
Returns:
-
((rna_counts, rna_genes, adt_counts, adt_proteins, cell_names), where)– -
``rna_counts`` is a (genes x cells) ``csc_matrix`` and ``adt_counts`` is a– -
(proteins x cells) ``csc_matrix`` in the same cell order.–
Source code in truecell/datasets.py
pbmc_hashing
¶
Download (if needed) and load the PBMC Cell-Hashing dataset (GSE108313).
The 8-HTO experiment from Stoeckius et al. (2018), as used by Seurat's
hashing vignette. Returns raw RNA counts aligned to the HTO counts on their
shared cell barcodes; the three non-hashtag QC rows (bad_struct,
no_match, total_reads) are dropped from the HTO matrix, leaving the
8 hashtags (BatchA–BatchH).
The RNA reference is a combined human+mouse genome (both MT- and mt-
genes are present), which is deliberate: cross-species doublets validate the
HTO doublet calls.
Returns:
-
((rna_counts, rna_genes, hto_counts, hto_names, cell_names), where)– -
``rna_counts`` is a (genes x cells) ``csc_matrix`` of raw counts and– -
``hto_counts`` is an (8 x cells) ``csc_matrix`` in the same cell order.–
Source code in truecell/datasets.py
ifnb
¶
Load the IFNB-stimulated PBMC dataset (Kang et al. 2018), via SeuratData.
~14,000 human PBMCs, half stimulated with interferon-beta and half control —
the standard benchmark for batch integration (correcting the stim/ctrl shift
while preserving cell type). Curated as SeuratData's ifnb, so it is loaded
through the R export bridge (see _load_seuratdata_export).
Returns (counts, genes, cells, meta); meta carries stim
(CTRL/STIM — the batch) and seurat_annotations (the cell types).
Source code in truecell/datasets.py
panc8
¶
Load the human pancreatic-islet dataset panc8 (8 techs), via SeuratData.
~14,900 cells profiled across five/eight technologies (CEL-seq, CEL-seq2, Fluidigm C1, SMART-seq2, inDrop) — a cross-technology integration and reference-mapping benchmark. Loaded through the R export bridge.
Returns (counts, genes, cells, meta); meta carries tech (the
batch / technology) and celltype (the reference annotation).
Source code in truecell/datasets.py
thp1_eccite
¶
Download (if needed) and load the THP-1 ECCITE-seq dataset (GSE153056).
The pooled-CRISPR screen from Papalexi et al. (2021) used by Seurat's
Mixscape vignette. Returns RNA + ADT counts and the per-cell metadata
(guide assignment, targeted gene, replicate, cell-cycle phase), all aligned
to their shared barcodes. The gene / guide_ID / NT columns of the
metadata are the perturbation labels run_mixscape needs; NT marks the
non-targeting controls.
Returns:
-
((rna_counts, rna_genes, adt_counts, adt_names, meta, cell_names), where)– -
``rna_counts`` is a (genes x cells) ``csc_matrix`` of raw counts,– -
``adt_counts`` is a (proteins x cells) ``csc_matrix`` in the same cell– -
order, and ``meta`` is a ``pandas.DataFrame`` indexed by cell barcode– -
(``guide_ID``, ``gene``, ``NT``, ``crispr``, ``replicate``, ``Phase``,– -
``S.Score``, ``G2M.Score``, ...).–
Source code in truecell/datasets.py
xenium_mouse_brain
¶
Download (if needed) the 10x Xenium mouse-brain coronal subset.
Fetches only the analysis components (cell_feature_matrix/ + cells),
~20 MB, into data_dir (default ~/.truecell_data/xenium_mouse_brain) and
returns the folder path — ready to pass to truecell.load_xenium.
This is the public section featured in Seurat's Xenium spatial vignette, so
the same analysis runs in R (LoadXenium) and Python (load_xenium).
Source code in truecell/datasets.py
visium_mouse_brain
¶
Download (if needed) the 10x Visium mouse-brain sagittal-anterior section.
Fetches the Space Ranger bundle (~64 MB) into data_dir (default
~/.truecell_data/visium_mouse_brain) and returns the folder path — ready to
pass to truecell.load_visium, and to R's Read10X_Image /
Load10X_Spatial, so the same slide runs in both languages.
Source code in truecell/datasets.py
AnnData interoperability¶
as_anndata
¶
Convert a Seurat object to anndata.AnnData.
Mapping
active_assay counts/data layer → adata.X (+ adata.layers for extras) meta_data → adata.obs assay.meta_features / meta_data → adata.var reductions["pca"].embeddings → adata.obsm["X_pca"] reductions["pca"].loadings → adata.varm["PCs"] graphs → adata.obsp misc → adata.uns
Source code in truecell/compat/anndata.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
from_anndata
¶
from_anndata(adata, assay: str = 'RNA', spatial_key: str = 'spatial', fov_key: str = 'fov') -> 'Truecell'
Convert an anndata.AnnData to a Seurat object.
Mapping
adata.X → Assay5 'counts' layer (transposed → features × cells) adata.layers → additional Assay5 layers adata.obs → seurat.meta_data adata.var → assay.meta_data adata.obsm["X_pca"] → seurat.reductions["pca"].cell_embeddings adata.obsm[spatial_key] → seurat.images (Centroids/FOV, split by obs[fov_key]) adata.varm["PCs"] → seurat.reductions["pca"].feature_loadings adata.obsp["connectivities"] → seurat.graphs adata.uns → seurat.misc
spatial_key (default "spatial") is treated as physical coordinates
and reconstructed into seurat.images — NOT as a dimensional reduction —
so get_tissue_coordinates and the spatial-analysis functions work. If
obs[fov_key] exists it splits the cells into one image per FOV.
Source code in truecell/compat/anndata.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |