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¶
Both functions live on truecell.compat.anndata, not on the top-level package.
AnnData, Scanpy and SpatialData covers what carries across, spatial
data included, and how to build a SpatialData object from the result.
as_anndata
¶
Convert a Truecell 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 images → adata.obsm[spatial_key], adata.obs[fov_key], adata.uns[spatial_key]
Space goes where Scanpy and Squidpy look for it, and where from_anndata
reads it back:
obsm[spatial_key]holds each cell's (x, y), row for row withobs_names, from the first image that places the cell: itsCentroidspoint, or, in an image with no centroids, the centroid of itsSegmentationring (as_centroids). A cell no image places is NaN. Visium spots stay in full-resolution pixels, column first, the orderscanpy.read_visiumwrites.obs[fov_key]names the image each cell came from, as a categorical in the order ofseurat.images. Ameta_datacolumn already under that name is written the same way when it names the images, as a platform's own FOV column does, and kept as it is, with a warning, when it does not.uns[spatial_key][name]holds eachVisiumV2's tissue image, under its resolution, and its scale factors, underscalefactors_json.json's keys.
AnnData has nowhere to put cell polygons or molecules, so those stay behind, as does a second image of cells another image already placed, such as a crop.
An AnnData layer is as wide as var, so a layer with fewer features than the
assay also stays behind, with a warning. That is scale.data after
scale_data()'s default, which scales only the variable features; scaling every
feature first, scale_data(obj, features=obj.feature_names()), keeps it.
Source code in truecell/compat/anndata.py
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 124 125 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 | |
from_anndata
¶
from_anndata(adata, assay: str = 'RNA', spatial_key: str = 'spatial', fov_key: str = 'fov', image_resolution: str = 'lowres') -> 'Truecell'
Convert an anndata.AnnData to a Truecell 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.uns[spatial_key] → the tissue image and scale factors of a VisiumV2 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, in
category order when the column is categorical. A cell with NaN coordinates
stays in the object and out of the images.
An image named after a library in uns[spatial_key], the layout
scanpy.read_visium and as_anndata write, becomes a VisiumV2 with
that library's tissue image and scale factors. With no obs[fov_key]
column, a single library names the single image. image_resolution picks
which of the library's images to keep, falling back to the other, as
load_visium does. The image and scale factors leave misc; anything
else the library holds, such as Scanpy's metadata, stays there.
Source code in truecell/compat/anndata.py
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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |