Metadata-Version: 2.5
Name: sc_apl_py
Version: 0.1.1
Summary: Association Plots (APL) for the Scverse ecosystem and AnnData objects
Project-URL: Homepage, https://github.com/keller-mark/apl-py
Project-URL: Repository, https://github.com/keller-mark/apl-py
Project-URL: Issues, https://github.com/keller-mark/apl-py/issues
Author: Mark Keller
License-Expression: GPL-3.0-or-later
License-File: LICENSE
Requires-Python: >=3.12
Requires-Dist: anndata>=0.13.4
Requires-Dist: numpy>=2.5.3
Requires-Dist: pandas>=3.0.6
Requires-Dist: scipy>=1.18.1
Provides-Extra: examples
Requires-Dist: adjusttext>=1.4.0; extra == 'examples'
Requires-Dist: igraph>=1.0.0; extra == 'examples'
Requires-Dist: marimo>=0.25.0; extra == 'examples'
Requires-Dist: matplotlib>=3.11.2; extra == 'examples'
Requires-Dist: scanpy>=1.12.4; extra == 'examples'
Provides-Extra: plot
Requires-Dist: matplotlib>=3.11.2; extra == 'plot'
Description-Content-Type: text/markdown

# sc-apl-py

[![PyPI](https://img.shields.io/pypi/v/sc_apl_py)](https://pypi.org/project/sc_apl_py)

Python implementation of Association Plots ([APL](https://github.com/VingronLab/APL)) (Gralinska and Vingron, JRSSSC 2023 [paper](https://doi.org/10.1093/jrsssc/qlad039))

## Installation

```sh
pip install sc_apl_py
```

The core package depends only on anndata, numpy, scipy and pandas. The
plotting functions in `apl_py.pl` need matplotlib, which comes with the `plot`
extra:

```sh
pip install "sc_apl_py[plot]"
```

## Usage

```python
import scanpy as sc
import sc_apl_py as apl

adata = sc.datasets.pbmc3k()
sc.pp.filter_genes(adata, min_cells=3)
sc.pp.normalize_total(adata)
sc.pp.log1p(adata)                 # APL's vignette runs CA on logcounts
# ... clustering, e.g. sc.tl.leiden(adata)

apl.tl.ca(adata, top=5000)         # correspondence analysis
n = apl.tl.pick_dims(adata, "elbow_rule")
apl.tl.association_coords(adata, "2", groupby="leiden", n_comps=n)
apl.tl.apl_score(adata)            # S-alpha scores

apl.get.apl_df(adata)              # ranked genes: x, y, score, rank
apl.pl.association_plot(adata)
apl.pl.scree(adata)
apl.pl.biplot(adata, color="leiden")
sc.pl.embedding(adata, basis="ca", color="leiden")
```

`apl.tl.apl(adata, "2", groupby="leiden")` runs the whole workflow (`runAPL`).

`apl.tl.apl_groups(adata, "leiden")` ranks genes for every cluster at once and
returns a long table (`group`, `gene`, `x`, `y`, `score`, `rank`); the
cutoff angle for each group is stored in `uns["apl_groups"]["alpha"]`.

Use `apl.tl.ca(adata, mask_var="highly_variable", top=None)` to run CA on
genes chosen by `sc.pp.highly_variable_genes` instead of APL's selection by
chi-square variance. For plain arrays, `apl.correspondence_analysis(X)`
returns a `CAResult`, and `apl.association_coords(ca, group)` works on it.

### Where results are stored

| Result | Location |
|---|---|
| Cell standard coordinates | `obsm["X_ca"]` |
| Gene principal coordinates | `varm["ca_princ"]` (NaN for unused genes) |
| Masses / inertias | `obs`/`var` columns `ca_mass`, `ca_inertia` |
| Singular values, parameters | `uns["ca"]` |
| AP coordinates | `obs`/`var` columns `apl_x`, `apl_y`; group in `obs["apl_group"]` |
| S-alpha score | `var["apl_score"]`; cutoff angle in `uns["apl"]` |

Everything is stored in plain AnnData fields, so it round-trips through
`.h5ad`, and `key_added` lets you keep APs for several clusters side by side.

## R → Python

Orientation is transposed: R's APL uses genes × cells, AnnData uses cells ×
genes, so R's "rows" are `var` and its "columns" are `obs`.

| R (APL) | Python |
|---|---|
| `cacomp` | `apl.tl.ca` / `apl.correspondence_analysis` |
| `var_rows` | `apl.variable_scores` (used via `top=`) |
| `pick_dims` | `apl.tl.pick_dims`, `apl.pl.scree` |
| `subset_dims` | `n_comps=` on AP functions, `CAResult.subset_dims` |
| `apl_coords` | `apl.tl.association_coords` |
| `apl_score` | `apl.tl.apl_score` |
| `apl` | `apl.pl.association_plot` |
| `ca_biplot` | `apl.pl.biplot` |
| `runAPL` | `apl.tl.apl` |
| `as.cacomp` | `apl.get.ca` |

Differences from R:

- The SVD uses SciPy: LAPACK for small problems, ARPACK otherwise. For the
  default (unclipped Pearson) residuals, the residual matrix is used as an
  implicit operator and never densified.
- R's `clip` argument has no effect in APL (Pearson residuals are bounded by
  1), so it is dropped; `cutoff` still clips.
- Negative-binomial residuals follow APL, which differs from scanpy's
  `normalize_pearson_residuals`: APL computes them on the frequency table.
- Not ported: `apl_topGO`/`plot_enrichment` (use `var["apl_score"]` with a
  prerank tool such as gseapy or decoupler), `ca_3Dplot`, plotly output, and
  caching of permuted data (`store_perm`).

## Examples

The `examples` directory contains [marimo](https://marimo.io) notebooks that
demonstrate the plotting functions:

- `plotting_basics.py`: simulated data (runs offline). Covers the scree plot,
  biplots and Association Plot variants, with a cluster picker.
- `pbmc3k.py`: a scanpy workflow on 10x PBMC 3k (downloads about 6 MB), with a
  comparison to Wilcoxon marker genes and rankings for every cluster.

```sh
uv run --extra examples marimo edit examples/plotting_basics.py
```

## Development

```bash
uv sync
uv run pytest
```

Tests check AP coordinates against the reference outputs from APL's R test
suite (`tests/data`).

## AI usage disclosure

Claude Code was used to port code from R to Python.
