Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion stlearn/tl/cci/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)
from .ligand_receptors import LRPairs, genes_of, lr_pairs, split_name
from .neighbours import calc_distance, calc_neighbours, stored_neighbours
from .permutation import perform_spot_testing
from .permutation import Null, perform_spot_testing
from .scoring import get_lrs_scores


Expand Down Expand Up @@ -91,6 +91,7 @@ def run(
random_state: int = 0,
verbose: bool = True,
cell_prop_cutoff: float = 0.2,
null: Null = "codetected",
) -> anndata.AnnData:
"""Performs stLearn LR analysis.

Expand Down Expand Up @@ -140,6 +141,14 @@ def run(
cell_prop_cutoff: float
With use_label, a cell type is in a spot when its proportion is above this,
as in run_cci.
null: str
Which of an LR's random gene pairs a spot's score is tested against.
"codetected" (default): those that score above 0 at the spot, detected
there as the LR is, so p = (n_greater + 1) / (n_codetected + 1); for
sparse data (single cells, Visium HD bins) without gridding. "random":
all n_pairs of them, the original test, p = max(n_greater, 1) / n_pairs,
where the pairs not detected at a spot score 0 and make any score there
significant. Not used with neg_binom.
Returns
-------
AnnData
Expand Down Expand Up @@ -280,6 +289,7 @@ def run(
save_bg=save_bg,
neg_binom=neg_binom,
random_state=random_state,
null=null,
)
features = tests.lr_features.loc[tested.names()]
var = pandas.DataFrame(
Expand Down
41 changes: 38 additions & 3 deletions stlearn/tl/cci/permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from dataclasses import dataclass
from functools import partial
from typing import Literal, get_args

import numpy as np
import numpy.typing as npt
Expand All @@ -19,6 +20,10 @@
from .perm_utils import _column_quantiles, background_pairs, get_lr_features
from .scoring import LRScorer, spots_per_chunk

Null = Literal["codetected", "random"]
"""What a spot's score is compared with: its random pairs that scored there too
("codetected"), or all of them, the zeros included ("random")."""


@dataclass
class SpotTests:
Expand Down Expand Up @@ -59,6 +64,7 @@ def perform_spot_testing(
1,
),
random_state: int = 0,
null: Null = "codetected",
) -> SpotTests:
"""Tests each spot's LR scores against random gene pairs with similar
expression to the LR pair; backgrounds are only generated for spots
Expand All @@ -67,8 +73,16 @@ def perform_spot_testing(
for each of `lrs`, in the order of lrs.names(), as get_lrs_scores gives them.
The p-values come back sparse, an entry for each.

`null` is which of a spot's n_pairs random pairs its score is compared with
(see empirical_pvals):

The backgrounds saved with save_bg, and the negative binomial fitted with
neg_binom, are of every random pair under either.

Returns lr_features in expression-rank order.
"""
if null not in get_args(Null):
raise ValueError(f"null must be one of {get_args(Null)}; got {null!r}.")
quantiles = np.array(quantiles)
scores = scored_entries(lr_scores)
names, ligands, receptors = lrs.names(), lrs.ligands(), lrs.receptors()
Expand Down Expand Up @@ -153,10 +167,8 @@ def perform_spot_testing(
chunk = spots_per_chunk(2 * n_genes, n_pairs)
for a in range(0, len(spot_indices), chunk):
background = score(spot_indices[a : a + chunk])
n_greater = (background >= lr_j_scores[a : a + chunk, None]).sum(1)
n_greater = np.maximum(n_greater, 1) # pseudocount
pvals.data[start + a : start + a + len(background)] = (
n_greater / n_pairs
empirical_pvals(lr_j_scores[a : a + chunk], background, null)
)
# Collect memory before the next chunk is made
del background
Expand Down Expand Up @@ -186,6 +198,29 @@ def perform_spot_testing(
return SpotTests(pvals, lr_feats, backgrounds, bg_spot_indices)


def empirical_pvals(
scores: npt.NDArray[np.float64],
background: npt.NDArray[np.float64],
null: Null = "codetected",
) -> npt.NDArray[np.float64]:
"""A p-value for each spot's score (all above 0) from its row of background:
the scores of the LR's random pairs at the spot.

"random" is a null using any random pairs, where there must be at least one and
they must be scored to be at least the spot's own score.

"codetected" is a null using pairs that score above 0 and where one gene is
expressed at the spot and the other in its neighbours. Formulae is:
(n_greater + 1) / (n_codetected + 1)
"""
n_greater = (background >= scores[:, None]).sum(1)
if null == "random":
return np.maximum(n_greater, 1) / background.shape[1]
else:
n_codetected = (background > 0).sum(1)
return (n_greater + 1) / (n_codetected + 1)


def score_background(
scorer: LRScorer,
pairs: npt.NDArray[np.int64],
Expand Down
16 changes: 3 additions & 13 deletions tests/tl/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,20 @@

import anndata
import numpy as np
import pandas as pd
from anndata import AnnData

import stlearn as st
from stlearn.tl.cci import LRPairs
from tests.utils import spot_table


def _make_cci_adata(dtype=np.float64, n_side=8, n_extra_genes=40, seed=0):
rng = np.random.default_rng(seed)
n_spots = n_side * n_side

lr_genes = ["Lig0", "Rec0", "Lig1", "Rec1"] # two L-R pairs
extra_genes = [f"gene{i}" for i in range(n_extra_genes)]
var_names = lr_genes + extra_genes

X = rng.poisson(3.0, size=(n_spots, len(var_names))).astype(dtype)

X = rng.poisson(3.0, size=(n_spots, len(lr_genes) + n_extra_genes))
rows, cols = np.divmod(np.arange(n_spots), n_side)
obs = pd.DataFrame(
{"imagerow": rows.astype(float), "imagecol": cols.astype(float)},
index=[f"spot{i}" for i in range(n_spots)],
)
var = pd.DataFrame(index=var_names)
return AnnData(X=X, obs=obs, var=var)
return spot_table(X, rows, cols, lr_genes, sparse=False, dtype=dtype)


LRS = LRPairs.from_names(["Lig0_Rec0", "Lig1_Rec1"], species="human")
Expand Down
9 changes: 8 additions & 1 deletion tests/tl/test_cci_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ class TestCCILabels(unittest.TestCase):
def setUpClass(cls):
adata, lrs = read_test_data_with_lr_pairs()
cls.lr_results = st.tl.cci.run(
adata, lrs, min_spots=5, distance=None, n_pairs=200, n_cpus=1, verbose=False
adata,
lrs,
min_spots=5,
distance=None,
n_pairs=200,
n_cpus=1,
verbose=False,
null="random",
)
# Three bands of cell types across the tissue.
bands = pd.qcut(adata.obs["imagecol"], 3, labels=CELL_TYPE_LABELS)
Expand Down
13 changes: 4 additions & 9 deletions tests/tl/test_het.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from stlearn.tl.cci import lr_result
from stlearn.tl.cci.het import count, get_data_for_counting
from stlearn.tl.cci.neighbours import calc_neighbours
from tests.utils import CCI_LRS, cci_table
from tests.utils import CCI_LRS, cci_table, spot_table

LRS = st.tl.cci.LRPairs.from_names(
["Lig0_Rec0", "Lig1_Rec1"], species="human", source="test"
Expand All @@ -25,15 +25,10 @@ def _make_adata(n_side=6, n_extra_genes=40, seed=0):
"""Spots on a unit grid, with cell type proportions in uns["cell_type"]."""
rng = np.random.default_rng(seed)
n_spots = n_side * n_side
var_names = list(LRS.genes()) + [f"gene{i}" for i in range(n_extra_genes)]
X = rng.poisson(3.0, size=(n_spots, len(var_names))).astype(np.float64)

lr_genes = list(LRS.genes())
X = rng.poisson(3.0, size=(n_spots, len(lr_genes) + n_extra_genes))
rows, cols = np.divmod(np.arange(n_spots), n_side)
obs = pd.DataFrame(
{"imagerow": rows.astype(float), "imagecol": cols.astype(float)},
index=[f"spot{i}" for i in range(n_spots)],
)
adata = AnnData(X=X, obs=obs, var=pd.DataFrame(index=var_names))
adata = spot_table(X, rows, cols, lr_genes, sparse=False, dtype=np.float64)
adata.uns["spatial"] = {
"lib": {
"scalefactors": {"spot_diameter_fullres": 5.0, "tissue_hires_scalef": 0.1},
Expand Down
Loading
Loading