Metadata-Version: 2.5
Name: gelpy
Version: 1.1.0
Summary: Gel electrophoresis image analysis, agent-native: a caching run-folder pipeline with a CLI.
Project-URL: homepage, https://github.com/ckarfusehr/GelPy
Project-URL: bug-tracker, https://github.com/ckarfusehr/GelPy/issues
Author-email: Christoph Karfusehr <c.karfusehr@gmail.com>
License-File: LICENSE
Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.10
Requires-Dist: matplotlib>=3.8.4
Requires-Dist: numpy>=2.0
Requires-Dist: pillow>=9.1
Requires-Dist: pydantic>=2.9
Requires-Dist: scikit-image>=0.23.1
Requires-Dist: scikit-learn>=1.4.2
Requires-Dist: scipy>=1.13.0
Requires-Dist: tifffile>=2024.4.24
Requires-Dist: typer>=0.19.0
Provides-Extra: dev
Requires-Dist: mkdocs-material>=9.5; extra == 'dev'
Requires-Dist: nbclient>=0.9; extra == 'dev'
Requires-Dist: nbformat>=5.9; extra == 'dev'
Requires-Dist: pandas>=2.2.2; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: pyyaml>=6.0; extra == 'dev'
Requires-Dist: tomli>=2.0; (python_version < '3.11') and extra == 'dev'
Provides-Extra: mcp
Requires-Dist: mcp<3,>=2.2; extra == 'mcp'
Provides-Extra: notebook
Requires-Dist: ipykernel>=6.19.2; extra == 'notebook'
Requires-Dist: pandas>=2.2.2; extra == 'notebook'
Requires-Dist: seaborn>=0.12.2; extra == 'notebook'
Description-Content-Type: text/markdown

# gelpy

[![DOI](https://zenodo.org/badge/648061579.svg)](https://zenodo.org/doi/10.5281/zenodo.13848775)

A Python package for simplified, reproducible, and easily shareable gel electrophoresis
analysis. Point it at a gel image and it gives you per-lane band intensities in a browsable
folder of plain files — no hand-drawn ROIs, no CSV shuffling, and no analysis that only runs
on the machine it was written on.

**gelpy is maintained again, and version 1.0 is a rewrite.** I archived this project in 2024
for lack of time. What brought it back is that the tedious half of the work — driving the
tool, checking each lane, deciding what to refit — is work a coding agent can now do
properly, provided the tool is built for one. So gelpy is now CLI-first and agent-native: it
speaks JSON, every command is explicit and replayable, and the analysis lives in a run folder
you can hand to someone else.

> **1.0 is not compatible with 0.x.** The `Gel` object API is gone, not deprecated, and old
> pickled gel objects cannot be loaded. If you have 0.x notebooks, see
> [docs/guide/migrating-from-0.x.md](docs/guide/migrating-from-0.x.md) for the command that
> replaces each method, and [CHANGELOG.md](CHANGELOG.md) for the full list of breaks.

## What it does

- Places lanes **from the image**, not from pixel coordinates you typed in, and renders an
  overlay so you can check before trusting anything downstream
- Removes the image background with a robust IRLS surface fit — no hand-placed stripes
- Extracts per-lane line profiles
- Fits bands with **no per-lane peak-detection tuning**: the peak count is chosen by AICc, the
  shape is chosen per component (gaussian, EMG, bi-gaussian, pseudo-Voigt), and every band is
  classified and counted
- Tells you **how much to trust each lane**: a per-lane confidence flag with machine-readable
  reasons, separate from whether the fit converged
- Writes the whole analysis to a run folder: `recipe.json`, `results.csv`/`.json`,
  `report.md`, and every figure

The pipeline is content-hash cached: re-running an identical command recomputes nothing, and
refitting one lane recomputes only that lane. The generated [walkthrough](docs/walkthrough/)
times the first fit and a re-run on the example gel.

## See it work

[**docs/walkthrough/**](docs/walkthrough/) is the whole workflow on a real 20-lane DNA origami
folding screen: the commands, their output, the figures and the numbers. The page is
*generated by running the analysis*, so it cannot drift out of date. It also covers the
awkward parts: which lanes came back low-confidence, and why the number you want is a sum over
a migration window rather than a single row.

## Install

```bash
conda create -n gelpy python=3.11
conda activate gelpy
pip install -e .            # from a checkout
pip install -e ".[dev]"     # plus the test and documentation suites
pip install -e ".[mcp]"     # plus the MCP server (gelpy mcp)
```

This installs a `gelpy` command (also `python -m gelpy`). Check it:

```bash
gelpy --help
gelpy models list --kind fit
```

## Use it

Two ways, and they do the same thing.

**Ask an agent.** With the skill installed (below), one prompt does the analysis, checks the
lane overlay, inspects each fit, and tells you which lanes it would not trust:

```text
Analyze gels/my_gel.tif. It is a 20-lane agarose gel; the sample
sheet is in gels/labels.txt. I want the per-lane band fractions and I want to
know which lanes you would not trust.
```

More prompts, and what each one makes the agent do, in
[examples/agent_prompts.md](examples/agent_prompts.md).

**Or run it yourself.** The commands the agent runs are the commands you would:

```bash
RUN=runs/gel; IMAGE=gels/my_gel.tif

gelpy init "$RUN" "$IMAGE" --n-lanes 20
gelpy lanes detect "$RUN" --n-lanes 20 --apply --labels-file gels/labels.txt
# now check $RUN/render/lane_detect.png: if the lanes are wrong, every
# number below is wrong
gelpy background remove "$RUN" --model robust_plane
gelpy fit run "$RUN" --plot
gelpy status "$RUN"
gelpy results show "$RUN" --format table
```

Every command takes the run directory explicitly, so there is no hidden "current run" and your
shell history is a complete record of the analysis. `init` stops with `run_exists` on a folder
that already holds a run: repeat it with `--force` to replace that run, or pick a new folder.

Each step above is also a tool that reads one JSON object and writes one (`gelpy tool list --json`
describes them, `gelpy tool call NAME` runs one). `gelpy mcp` serves the same tools to an MCP
client (`pip install "gelpy[mcp]"`), and `gelpy report html "$RUN"` writes the run as a static web
page. [docs/guide/driving-from-an-agent.md](docs/guide/driving-from-an-agent.md) has the tool
contract and [docs/guide/html-report.md](docs/guide/html-report.md) the report.

## Documentation

[**docs/**](docs/) is the map. The short version:

- [docs/guide/analyze-a-gel.md](docs/guide/analyze-a-gel.md) — the workflow, in order
- [docs/guide/fit-engine.md](docs/guide/fit-engine.md) — what the fitter does and how far to
  trust it
- [docs/guide/results.md](docs/guide/results.md) — what every column means
- [docs/guide/known-limitations.md](docs/guide/known-limitations.md) — **read this before you
  publish numbers**
- [docs/reference/](docs/reference/) — every flag, contract and error code, generated from the
  code
- [docs/guide/extending.md](docs/guide/extending.md) — adding a peak shape, fit strategy or
  background model

The documentation is checked rather than trusted: the reference is generated from the code and
diffed by a test, every command block in the guide and the walkthrough is executed against the
example gel, and key facts are asserted by named tests. This exists because, during the
rewrite, the documentation was wrong in 38 verified places and nobody noticed for two months.

## Getting the skill

The `gelpy-analyze` skill lives in [plugin/](plugin/) and carries the judgment part: verify
the overlay, don't trust `status: "ok"`, escalate a low-confidence lane. The simplest install
needs no marketplace: copy `plugin/skills/gelpy-analyze/` into `~/.claude/skills/` (every
project) or into `.claude/skills/` of the project you analyze gels in. To update, delete the
old copy before copying the new one; copying over it keeps files the new version dropped. To
serve the skill from a Claude Code plugin marketplace instead,
[plugin/README.md](plugin/README.md) has both arrangements and the command that vendors it.

The verified setup is Claude Code (terminal or desktop app) in a directory with gelpy
installed and your images on disk. I have not tested other agent environments.

## What it deliberately does not do

- **Molecular weights.** No ladder calibration, so `area` is in intensity units, not mass.
- **Sub-pixel lane placement.** The grid phase is accurate to about 0.02 of the lane pitch on
  a real gel; that is invisible against a 180 px profile strip, but it is not sub-pixel.
- **Rescue a gel that fills a fraction of the crop.** The lane-grid prior assumes the lanes
  span roughly the whole image. That case is detected and flagged, not silently fudged — set
  the outer lane centers by hand instead.
- **Decide what your bands mean.** It gives you areas, classes and uncertainties. Which
  species is which is your call.

[docs/guide/known-limitations.md](docs/guide/known-limitations.md) has the full list, with
measurements.

## Contributing

Same invitation as before: **try gelpy, break it, and tell me.** A gel it handles badly is
more useful to me than a bug report about tidy code, so an issue with the image attached is
the most valuable thing you can send. If you want to write code,
[docs/guide/architecture.md](docs/guide/architecture.md) explains the layout and
[docs/guide/extending.md](docs/guide/extending.md) explains how to add a model without
touching anything else.

Before opening a pull request:

```bash
pytest tests/ -q                # the suite must pass
python tools/docs.py check      # documentation must agree with the code
```

## Citing

If gelpy contributed to work you are publishing, please cite it via the
[Zenodo DOI](https://zenodo.org/doi/10.5281/zenodo.13848775).

## License

GPLv3 or later. See [LICENSE](LICENSE).
