Expression, signatures, and annotation#

This module supports expression summaries and provisional annotation. Marker scores should be interpreted together with differential markers, known negative markers, sample consistency, and biological context.

Check marker availability#

marker_sets = {
    "T_cell": ["CD3D", "CD3E", "TRBC1"],
    "B_cell": ["CD79A", "MS4A1", "CD37"],
    "Myeloid": ["LYZ", "FCER1G", "CTSS"],
}

markers = sctk.tl.markers_in_data(adata, marker_sets)

Missing markers are common when identifiers differ or features were filtered. Do not silently interpret a signature supported by only one weak marker.

Gene-set scoring#

Simple mean:

scores = sctk.tl.score_gene_sets(
    adata,
    marker_sets,
    layer="log1p_norm",
    method="mean",
    min_genes=2,
    inplace=False,
)

Scanpy control-gene score:

scores = sctk.tl.score_gene_sets(
    adata,
    marker_sets,
    layer="log1p_norm",
    method="scanpy",
    min_genes=2,
    ctrl_size=50,
    inplace=False,
)

Use mean for transparent summaries of an already standardized matrix. Use scanpy for log-normalized cell-state signatures when expression-matched control genes are desirable.

Provisional marker annotation#

annotation = sctk.tl.annotate_by_markers(
    adata,
    marker_sets,
    layer="log1p_norm",
    method="scanpy",
    min_genes=2,
    min_margin=0.1,
    label_key="marker_annotation",
    unknown_label="unknown",
    inplace=False,
)

annotation[
    ["marker_annotation", "marker_annotation_score", "marker_annotation_margin"]
].head()

min_margin is the required difference between the best and second-best score. Ambiguous cells remain unknown. Recommended review:

  1. summarize labels by Leiden cluster and sample;

  2. inspect positive and negative markers;

  3. compare against differential markers;

  4. check whether condition or batch drives the label;

  5. curate a final cell_type column separately.

Ranked marker tables#

After Scanpy marker testing:

sc.tl.rank_genes_groups(
    adata,
    groupby="cell_type",
    layer="log1p_norm",
    method="wilcoxon",
)

table = sctk.tl.rank_genes_table(
    adata,
    group="T_cell",
    n_genes=30,
)

top_markers = sctk.tl.marker_dict_from_rank_genes(
    adata,
    groupby="cell_type",
    n_genes=10,
)

Cell-level marker tests are useful for annotation but do not replace sample-level differential expression between experimental conditions.

Grouped mean expression#

mean_expression = sctk.tl.grouped_obs_mean(
    adata,
    groupby="cell_type",
    layer="log1p_norm",
)

The result has genes as rows and groups as columns.

Zero-inflated genes#

For a count layer, detected versus undetected is often more interpretable than a median split:

key, thresholds = sctk.tl.split_cells_by_gene(
    adata,
    "CA12",
    layer="counts",
    method="detected",
    threshold=0,
    groupby="sample",
    min_cells_per_group=25,
    drop_invalid_groups=True,
)

Available methods:

Method

Appropriate use

detected

zero-inflated count expression

median

balanced continuous values

quantile

a specified upper/lower fraction

otsu

approximately bimodal continuous distributions

gmm

two Gaussian-like components with sufficient cells

When groupby is supplied, thresholds are estimated within each group and returned as a Series. Invalid groups remain missing when drop_invalid_groups=True.

Sample-level detection#

To compare samples, calculate one fraction per sample:

detection = sctk.tl.grouped_gene_detection_rate(
    adata,
    "CA12",
    groupby="sample",
    layer="counts",
)

sample_groups, cutoffs = sctk.tl.split_groups_by_fraction(
    detection["positive_fraction"],
    method="tertile",
)

This avoids treating thousands of cells from the same sample as independent replicates. Quantile grouping is exploratory; model a continuous fraction when possible instead of forcing categories.

Extreme tails#

key, (low, high) = sctk.tl.split_cells_by_gene_extremes(
    adata,
    "state_score",
    low_quantile=0.1,
    high_quantile=0.9,
)

Extreme-tail comparisons discard information and can amplify technical effects. Use them for visualization or hypothesis generation, not as the default statistical design.