# Enrichment and plotting

This page covers the reporting layer after differential expression or marker
analysis. Enrichment results are only as reliable as the gene universe and the
ranking supplied to the test.

## Prepare gene sets

`sctk` uses a long two-column network: one row per term–gene association.

```python
import sctk

net = sctk.tl.read_gmt("pathways.gmt")
net.head()
```

By default, the columns are named `source` (term) and `target` (gene). If a
resource uses different names, standardize it explicitly:

```python
net = sctk.tl.rename_net_columns(
    resource,
    source="geneset",
    target="genesymbol",
)
```

Remote resources obtained through `get_resource()` are cached locally as
Parquet. Set `SCTK_CACHE_DIR` when the default user cache location is not
writable or should live with the project.

## Over-representation analysis

ORA accepts one gene list or a mapping of named gene lists:

```python
selected = de.index[(de["padj"] < 0.05) & (de["log2FoldChange"] > 1)]
tested = de.index[de["baseMean"].notna()]

ora = sctk.tl.run_ora(
    selected,
    net,
    background=tested,
    tmin=5,
)
```

The background must be the genes that could have entered the selected list,
usually all genes that passed the differential-expression filtering step. Do
not silently use the whole genome when only a subset was tested.

For several contrasts:

```python
ora = sctk.tl.run_ora(
    {
        "treated_up": treated_up,
        "treated_down": treated_down,
    },
    net,
    background=tested,
)
```

## Preranked GSEA

Use a signed statistic that ranks every tested gene. A Wald statistic or
moderated *t* statistic is preferable to ranking by adjusted p-value.

```python
ranking = de["stat"].dropna()

gsea = sctk.tl.run_gsea(
    ranking,
    net,
    min_size=15,
    max_size=500,
    permutations=1000,
    threads=8,
    seed=0,
)
```

`run_gsea()` accepts one ranking per call. With a DataFrame, select the ranking
column explicitly:

```python
gsea = sctk.tl.run_gsea(de, net, stat="stat")
```

Keep gene identifiers consistent between the ranking and gene-set network.
Duplicated gene identifiers are reduced to the first occurrence, so resolve
symbol mappings before enrichment when possible.

## Plot enrichment results

```python
ax = sctk.pl.enrichment_dotplot(ora, top=20)
ax = sctk.pl.enrichment_barplot(ora, top=15)
ax = sctk.pl.plot_ora_bar(ora, top_n=10, title="Treated versus control")
```

The generic dot and bar helpers infer common result-column names, but explicit
column arguments are safer after joining or renaming result tables.

## Plot AnnData results

```python
sctk.pl.set_colors(adata, "cell_type")
sctk.pl.embedding(
    adata,
    basis="umap",
    color=["cell_type", "sample"],
)

sctk.pl.dotplot(
    adata,
    marker_dict,
    groupby="cell_type",
    layer="log1p_norm",
)

sctk.pl.composition(
    adata,
    groupby="cell_type",
    splitby="sample",
    percent=True,
    show_counts=True,
)
```

Composition plots describe the observed cells. They do not test differential
abundance and should not be treated as a substitute for a replicated
sample-level model.

## Reporting checklist

- Report the gene universe, gene-set database/version, and identifier type.
- For ORA, state the threshold used to form the selected list.
- For GSEA, state the ranking statistic and permutation count.
- Show effect direction as well as significance.
- Save the complete result table, not only the plotted top terms.
- Treat highly overlapping terms as related evidence rather than independent
  discoveries.
