Integration and reference mapping¶
Two different jobs that share machinery. Integration removes a batch effect between datasets you intend to analyse together. Reference mapping leaves the reference untouched and projects a query into it, carrying labels across.
integrate_layers is the v5 dispatcher — method="harmony" | "cca" | "rpca" —
and runs integrate_embeddings, the embedding-space algorithm, as Seurat v5
does. The v4 pair (find_integration_anchors + integrate_data, which corrects
expression rather than embeddings) is still available directly and is still what
you want if you are reproducing a v4 analysis. These were the same function once,
which was a bug: the v5 name ran the v4 algorithm.
Both anchor paths are compared against Seurat's own anchors, not just against the clustering they produce, in Anchor internals.
Batch correction¶
integrate_layers
¶
integrate_layers(seurat, method: str = 'harmony', orig_reduction: str = 'pca', new_reduction: Optional[str] = None, group_by: Optional[Union[str, list[str]]] = None, assay: Optional[str] = None, **kwargs) -> None
Integrate layers/batches (Seurat v5 IntegrateLayers dispatch API).
Mirrors IntegrateLayers(obj, method = HarmonyIntegration,
orig.reduction = "pca"). A thin dispatcher over the individual
integration routines.
Parameters:
-
method(str, default:'harmony') –'harmony', 'cca', or 'rpca'.
-
orig_reduction(str, default:'pca') –reduction to integrate (default 'pca'). Every method corrects this reduction and writes a new one of the same shape — the anchor methods included, which is what makes them interchangeable with Harmony here.
-
new_reduction(Optional[str], default:None) –storage key for the integrated reduction (defaults to '{method}')
-
group_by(Optional[Union[str, list[str]]], default:None) –batch column identifying the layers/batches to integrate (required for every method)
Source code in truecell/integration.py
run_harmony
¶
run_harmony(seurat, group_by: Union[str, list[str]], reduction: str = 'pca', dims: Optional[Union[list[int], range]] = None, reduction_name: str = 'harmony', reduction_key: str = 'harmony_', theta: Optional[Union[float, list[float]]] = None, lambda_: Optional[Union[float, list[float]]] = None, sigma: float = 0.1, nclust: Optional[int] = None, max_iter_harmony: int = 10, assay: Optional[str] = None, seed: int = 0) -> None
Run Harmony batch correction on an existing reduction.
Mirrors R's RunHarmony(obj, group.by.vars = "batch"). Takes the cell
embeddings of reduction (PCA by default), removes batch effects with
harmonypy, and stores the corrected embeddings as a new DimReduc under
reduction_name — same shape as the input, so it can be passed straight
to find_neighbors(reduction="harmony") / run_umap(reduction="harmony").
Parameters:
-
group_by(Union[str, list[str]]) –metadata column(s) identifying the batch(es) to correct
-
reduction(str, default:'pca') –source reduction to correct (default 'pca')
-
dims(Optional[Union[list[int], range]], default:None) –which dimensions of
reductionto use (0-indexed; default all available) -
reduction_name(str, default:'harmony') –storage key for the corrected reduction
-
theta(Optional[Union[float, list[float]]], default:None) –diversity clustering penalty (harmonypy default when None)
-
lambda_(Optional[Union[float, list[float]]], default:None) –ridge regression penalty (harmonypy default when None)
-
sigma(float, default:0.1) –soft-clustering width
-
nclust(Optional[int], default:None) –number of Harmony clusters (harmonypy default when None)
-
max_iter_harmony(int, default:10) –maximum Harmony iterations
-
seed(int, default:0) –random seed for reproducibility
Source code in truecell/integration.py
Anchors, directly¶
find_integration_anchors
¶
find_integration_anchors(objects: list, anchor_features: Optional[list[str]] = None, reduction: str = 'cca', dims: int = 30, k_anchor: int = 5, k_filter: int = 200, k_score: int = 30, reference: int = 0, layer: str = 'scale.data', seed: int = 42) -> IntegrationAnchors
Find anchors linking each dataset to the reference (Seurat's FindIntegrationAnchors).
Mirrors FindIntegrationAnchors(object.list, reduction = "cca"). Anchors
are mutual nearest neighbours in a shared CCA (or reciprocal-PCA) space,
scored by neighbourhood consistency and filtered against the original
expression space.
Parameters:
-
objects(list) –list of Truecell objects (each normalized + with variable features / scaled data).
objects[reference]is treated as the reference every other dataset is anchored to. -
anchor_features(Optional[list[str]], default:None) –features to integrate on (default: variable features shared across all objects).
-
reduction(str, default:'cca') –"cca"or"rpca". -
dims(int, default:30) –number of shared dimensions to use.
-
k_anchor(int, default:5) –neighbours for the mutual-nearest-neighbour search.
-
k_filter(int, default:200) –neighbourhood size for the feature-space anchor filter (set to 0 or None to skip filtering).
-
k_score(int, default:30) –neighbourhood size for anchor scoring.
-
reference(int, default:0) –index of the reference dataset in
objects. -
layer(str, default:'scale.data') –layer to draw the shared-space expression from.
-
seed(int, default:42) –random seed for the PCA/neighbour steps.
Returns:
Source code in truecell/anchors.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
integrate_embeddings
¶
integrate_embeddings(anchors: IntegrationAnchors, reduction, new_reduction: str = 'integrated_dr', dims_to_integrate: Optional[list[int]] = None, k_weight: int = 100, sd_weight: float = 1.0) -> DimReduc
Batch-correct an existing reduction (Seurat's IntegrateEmbeddings).
The v5 counterpart to integrate_data, and a genuinely different
algorithm rather than a wrapper over it. IntegrateData corrects
expression and leaves you to re-scale and re-run PCA; IntegrateEmbeddings
corrects the embedding itself, so the output lives in the input
reduction's basis and keeps its loadings.
Seurat implements it by transposing the embedding into a fake assay whose
"features" are the dimensions (drtointegrate-1 …) and pushing that
through the very same anchor machinery, which is why this shares
_anchor_weights with integrate_data. The one substantive
difference is the weight space: RunIntegration's dims = NULL branch
hands FindWeights the drtointegrate matrix itself, so neighbours are
measured in the uncorrected embedding — not in the fresh per-pair PCA
that the expression path builds.
Parameters:
-
anchors(IntegrationAnchors) – -
reduction–a
DimReduccovering every cell inanchors.objects— the reduction to correct. -
new_reduction(str, default:'integrated_dr') –key for the returned reduction.
-
dims_to_integrate(Optional[list[int]], default:None) –which dimensions to correct (0-indexed; default all).
-
k_weight(int, default:100) –anchors used to weight each query cell's correction.
-
sd_weight(float, default:1.0) –bandwidth of the anchor kernel; enters as
(2/sd)².
Returns:
-
DimReduc–The corrected embedding, cells in merged order (reference first).
Source code in truecell/anchors.py
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 | |
integrate_data
¶
integrate_data(anchors: IntegrationAnchors, new_assay: str = 'integrated', k_weight: int = 100, sd_weight: float = 1.0, add_cell_ids: Optional[list[str]] = None, seed: int = 42) -> 'object'
Batch-correct query datasets onto the reference (Seurat's IntegrateData).
Mirrors IntegrateData(anchors). For every query dataset, each cell is
corrected by a distance-weighted sum of anchor correction vectors
(expr_ref − expr_query); the reference is left unchanged. The corrected
expression of the anchor features is stored as the data layer of a new
"integrated" assay on a merged object, which becomes the active assay.
Downstream: scale_data + run_pca on the integrated assay yields an
embedding that clusters by cell type rather than by batch.
Parameters:
-
anchors(IntegrationAnchors) – -
new_assay(str, default:'integrated') –name for the corrected assay (default
"integrated"). -
k_weight(int, default:100) –anchors used to weight each query cell's correction. Counts anchors, not anchor cells — see
_anchor_weights. -
sd_weight(float, default:1.0) –bandwidth of the anchor kernel; enters as
(2/sd_weight)². -
add_cell_ids(Optional[list[str]], default:None) –optional per-object prefixes for the merged cell names.
-
seed(int, default:42) –random seed for the per-pair weight PCA.
Returns:
-
Truecell–A merged object carrying the
new_assayassay (active) alongside the original assay.
Source code in truecell/anchors.py
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 | |
IntegrationAnchors
¶
IntegrationAnchors(anchors: DataFrame, objects: list, reference: int, reduction: str, anchor_features: list[str], dims: int, weight_embeddings: dict[int, ndarray])
Anchors linking a reference dataset to one or more query datasets.
Slots
anchors— DataFrame with columnsdataset1, cell1, dataset2, cell2, score.dataset1is always the reference; the cell columns hold within-dataset 0-based row indices.objects— the list of Truecell objects passed tofind_integration_anchors(order preserved).reference— index intoobjectsof the reference dataset.reduction—"cca"or"rpca"— how the shared space was built.anchor_features— the features the anchors (and correction) run on.dims— number of shared dimensions used.weight_embeddings—{query_index: (n_query_cells × dims) array}— each query dataset's cells in the shared anchor space. Kept for inspection;integrate_datadoes not weight with it, because Seurat weights in a fresh PCA of the merged pair instead.
Source code in truecell/anchors.py
Reference mapping¶
find_transfer_anchors
¶
find_transfer_anchors(reference, query, anchor_features: Optional[list[str]] = None, reduction: str = 'pcaproject', dims: int = 30, k_anchor: int = 5, k_filter: int = 200, k_score: int = 30, layer: str = 'scale.data', seed: int = 42) -> TransferAnchors
Find transfer anchors from a reference to a query (Seurat's FindTransferAnchors).
Mirrors FindTransferAnchors(reference, query, reduction = "pcaproject").
Anchors are mutual nearest neighbours between the reference and the query in
a shared space — by default the reference's own PCA, into which the query is
projected — scored by neighbourhood consistency and filtered against the
original expression space.
Parameters:
-
reference–annotated reference Truecell object (normalized + with variable features / scaled data).
-
query–query Truecell object to annotate (same preprocessing).
-
anchor_features(Optional[list[str]], default:None) –features to anchor on (default: variable features shared by reference and query).
-
reduction(str, default:'pcaproject') –"pcaproject"(project the query into the reference's PCA; the default and most robust for annotation) or"cca"(a jointly-learned space, for harder cross-modality/species cases). The reference PCA is computed on the shared anchor features, so the reference need not already carry apcareduction. -
dims(int, default:30) –number of shared dimensions to use.
-
k_anchor(int, default:5) –neighbours for the mutual-nearest-neighbour search.
-
k_filter(int, default:200) –neighbourhood size for the feature-space anchor filter (set to 0 or None to skip filtering).
-
k_score(int, default:30) –neighbourhood size for anchor scoring.
-
layer(str, default:'scale.data') –layer to draw the shared-space expression from.
-
seed(int, default:42) –random seed for the PCA/neighbour steps.
Returns:
Source code in truecell/transfer.py
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 | |
transfer_data
¶
transfer_data(anchors: TransferAnchors, refdata: Union[str, ndarray, list, Series], k_weight: int = 50, sd_weight: float = 1.0, refdata_features: Optional[list[str]] = None) -> DataFrame
Transfer labels or expression from reference to query (Seurat's TransferData).
Mirrors TransferData(anchorset, refdata = "celltype"). Each query cell
gets a weight over the anchors — a distance-weighted, anchor-score-scaled
Gaussian kernel in the shared space (the same weighting IntegrateData
uses) — and the reference information is carried across those weights.
refdata selects the mode:
- classification — a metadata column name (
str) or a 1-D array of per-reference-cell labels. Returns a DataFrame indexed by query cell withpredicted.id, oneprediction.score.<class>column per reference class (each query cell's rows sum to 1), andprediction.score.max. - imputation — a 2-D
features × reference-cellsmatrix. Returns a DataFrame of predicted query expression (features × query cells); name the rows withrefdata_features.
Parameters:
-
anchors(TransferAnchors) –a
TransferAnchorsfromfind_transfer_anchors. -
refdata(Union[str, ndarray, list, Series]) –reference labels (str column / 1-D array) or a 2-D
features × reference-cellsmatrix to impute. -
k_weight(int, default:50) –anchors used to weight each query cell.
-
sd_weight(float, default:1.0) –bandwidth multiplier for the Gaussian anchor kernel.
-
refdata_features(Optional[list[str]], default:None) –row names for the imputation output (2-D
refdata).
Returns:
-
DataFrame–
Source code in truecell/transfer.py
TransferAnchors
¶
TransferAnchors(anchors: DataFrame, reference, query, reduction: str, anchor_features: list[str], dims: int, query_embedding: ndarray)
Anchors linking a fixed reference to a query, for label/data transfer.
Slots
anchors— DataFrame with columnscell1, cell2, score.cell1is a within-reference 0-based cell index,cell2a within-query one; the reference is never moved.reference— the reference Truecell object (annotated atlas).query— the query Truecell object (to be annotated).reduction—"pcaproject"or"cca"— how the shared space was built.anchor_features— the features the anchors run on.dims— number of shared dimensions used.query_embedding—(n_query_cells × dims)— the query cells in the shared space, used bytransfer_datato weight anchors.
Source code in truecell/transfer.py
map_query
¶
map_query(anchors: TransferAnchors, refdata: Optional[Union[str, ndarray, list, Series]] = None, reference_reduction: str = 'pca', reduction_model: str = 'umap', reduction_name: str = 'ref.umap', reduction_key: str = 'refUMAP_', k_weight: int = 50, sd_weight: float = 1.0, refdata_features: Optional[list[str]] = None, layer: str = 'scale.data') -> Optional[DataFrame]
Annotate and place a query in a reference (Seurat's MapQuery).
Mirrors MapQuery(anchorset, query, reference, refdata = "celltype"). The
single call that turns transfer anchors into a mapped query:
transfer_datacarriesrefdataacross the anchors, and — for a categorical label — writespredicted.id/prediction.score.*straight ontoquery.meta_data.project_umapprojects the query into the reference's UMAP, stored asquery.reductions[reduction_name].
Both steps mutate the query object (the anchors' query) in place.
Parameters:
-
anchors(TransferAnchors) –a
TransferAnchorsfromfind_transfer_anchors. -
refdata(Optional[Union[str, ndarray, list, Series]], default:None) –reference labels to transfer (metadata column name or a per-reference-cell array) or a 2-D
features × reference-cellsmatrix to impute. PassNoneto skip transfer and only project the UMAP. -
reference_reduction(str, default:'pca') –reference reduction whose loadings project the query into the UMAP's input space (default
"pca"). -
reduction_model(str, default:'umap') –reference reduction holding the fitted UMAP model.
-
reduction_name(str, default:'ref.umap') –storage key for the projection in
query.reductions. -
reduction_key(str, default:'refUMAP_') –prefix for the projected dimension names.
-
k_weight(int, default:50) –anchor-weighting knobs passed to
transfer_data. -
sd_weight(int, default:50) –anchor-weighting knobs passed to
transfer_data. -
refdata_features(Optional[list[str]], default:None) –row names for imputation output (2-D
refdata). -
layer(str, default:'scale.data') –layer to draw the query's expression from.
Returns:
-
DataFrame or None–The transferred predictions (classification) or imputed expression (imputation);
Nonewhenrefdatais not given.
Source code in truecell/mapping.py
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 | |
project_umap
¶
project_umap(query, reference, reduction: str = 'pca', umap_reduction: str = 'umap', dims: Optional[Union[list[int], range]] = None, reduction_name: str = 'ref.umap', reduction_key: str = 'refUMAP_', layer: str = 'scale.data') -> DimReduc
Project a query into a reference's UMAP (Seurat's ProjectUMAP).
Mirrors ProjectUMAP(query, reference, reduction.model = "umap"). The
query is first projected into the reference's PCA (through the reference's
loadings), then run through the reference's fitted UMAP model in
transform-only mode — so the query cells land in the reference's existing
embedding rather than in a fresh, unrelated one. Stores the result as
query.reductions[reduction_name] and returns it.
The reference must already carry both a PCA reduction (reduction, for the
loadings) and a UMAP reduction (umap_reduction) fitted with a returnable
model — i.e. run_umap(reference), which stashes the umap-learn model
in reduction.misc["umap_model"].
Parameters:
-
query–query Truecell object (normalized + scaled on the reference's PCA features).
-
reference–reference Truecell object carrying the fitted PCA + UMAP.
-
reduction(str, default:'pca') –reference reduction whose loadings project the query (default
"pca"). -
umap_reduction(str, default:'umap') –reference reduction holding the fitted UMAP model (default
"umap"). -
dims(Optional[Union[list[int], range]], default:None) –which PCA dimensions to feed the UMAP model (0-indexed). Defaults to the dimensions the model was trained on.
-
reduction_name(str, default:'ref.umap') –storage key for the projection in
query.reductions. -
reduction_key(str, default:'refUMAP_') –prefix for the projected dimension names.
-
layer(str, default:'scale.data') –layer to draw the query's expression from.
Returns:
-
DimReduc–The query's cells in the reference UMAP; also stored on
query.