Metadata-Version: 2.4
Name: epirefine
Version: 0.13.0
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Requires-Dist: numpy>=1.26
Requires-Dist: scipy>=1.11
Requires-Dist: polars>=1.27
Requires-Dist: pytest>=8 ; extra == 'dev'
Requires-Dist: pytest-xdist>=3 ; extra == 'dev'
Provides-Extra: dev
License-File: LICENSE
License-File: rust/THIRD_PARTY_LICENSES.txt
Summary: Refine metagenome bins from nanopore methylation: flag contaminants and place unbinned contigs, with calibrated probabilities
Keywords: metagenomics,binning,bin refinement,nanopore,DNA methylation,Nanomotif,MAG
Author-email: Søren Heidelbach <shei@bio.aau.dk>
Maintainer-email: Søren Heidelbach <shei@bio.aau.dk>
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Changelog, https://github.com/SorenHeidelbach/epirefine/blob/main/CHANGELOG.md
Project-URL: Documentation, https://github.com/SorenHeidelbach/epirefine/blob/main/docs/README.md
Project-URL: Homepage, https://github.com/SorenHeidelbach/epirefine
Project-URL: Issues, https://github.com/SorenHeidelbach/epirefine/issues
Project-URL: Repository, https://github.com/SorenHeidelbach/epirefine

# EpiRefine

[![tests](https://github.com/SorenHeidelbach/epirefine/actions/workflows/tests.yml/badge.svg)](https://github.com/SorenHeidelbach/epirefine/actions/workflows/tests.yml)
[![PyPI](https://img.shields.io/pypi/v/epirefine.svg)](https://pypi.org/project/epirefine/)
[![Python](https://img.shields.io/pypi/pyversions/epirefine.svg)](https://pypi.org/project/epirefine/)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/SorenHeidelbach/epirefine/blob/main/LICENSE)

**Refine metagenome bins with nanopore methylation.** EpiRefine takes the bins from any binner and checks every
contig against the methylation fingerprint of its bin. It removes contigs that don't belong (contamination) and
puts unbinned contigs into the bin whose methylation they share. Every decision comes with a probability,
calibrated on pieces cut from the sample's own bins. Very long contigs remain overconfident (see the
[FAQ](https://github.com/SorenHeidelbach/epirefine/blob/main/docs/faq.md#should-i-trust-every-flag)).

![What EpiRefine does to a bin](https://raw.githubusercontent.com/SorenHeidelbach/epirefine/main/docs/images/epirefine_overview.png)

Bacteria methylate short DNA motifs everywhere in the cell: on the chromosome and usually on its plasmids and
phages as well. Nanopore reads record that methylation. The set of methylated motifs is therefore a signature of
the genome a contig comes from. It is measured separately from the sequence composition and coverage that binners
use, so it can catch errors they miss. It is not fully independent of them: calls need at least 10x
coverage and 5 kb, and in bins without methylated motifs the remaining signal follows GC content.

**Contents:** [Install](#install) · [Quick start](#quick-start) · [What you get](#what-you-get) ·
[Documentation](#documentation) · [When to trust it](#when-to-trust-it) · [Citing](#citing)

## Install

```bash
pip install epirefine
```

Python 3.10 or newer; Linux and macOS. Check it works with `epirefine --version`.

EpiRefine is also on Bioconda:

```bash
conda install -c conda-forge -c bioconda epirefine
```

Bioconda can lag behind PyPI by a version. `epirefine --version` shows which version you have; for the newest
release, install with pip (also inside a conda environment).

The wheels for Linux (x86_64, aarch64; glibc 2.17 or newer, and musl on x86_64) and macOS (Apple silicon and Intel)
carry compiled kernels, which EpiRefine uses by default. On any other platform pip installs the pure-Python wheel
and EpiRefine runs its numpy code: the same decisions, more slowly. The log's third line says which code runs
(`kernels: compiled (...)` or `kernels: numpy (<reason>)`). A run on numpy without being asked says so in a
warning, and `params.json` records it under `kernels`. `EPIREFINE_RS=0` forces the numpy code. pip never compiles
anything, because the pure wheel fits every platform. Building from source happens only when asked for
(`pip install --no-binary epirefine epirefine`, which needs Rust and a C compiler). A separately installed
`epirefine_rs` (the unpublished kernel package of earlier development versions) is not used.

## Quick start

You need four files for the sample:

| Input | What it is | Made with |
|---|---|---|
| `assembly.fasta` | the assembled contigs | any long-read assembler (e.g. myloasm or metaFlye) |
| `contig_bin.tsv` | which bin each binned contig is in: `contig<TAB>bin`, no header | any binner |
| `pileup.bed` | reads called modified / unmodified at every position | `modkit pileup` |
| `bin-motifs.tsv` | the methylated motifs found in the bins | `nanomotif motif_discovery` ([Nanomotif](https://github.com/MicrobialDarkMatter/nanomotif); the benchmarks of 0.12 used version 1.3.0) |

Then run:

```bash
epirefine run \
    --assembly assembly.fasta \
    --contig-bin contig_bin.tsv \
    --pileup pileup.bed \
    --motifs bin-motifs.tsv \
    --out epirefine_out \
    --workers 16
```

New to nanopore methylation? The [getting-started guide](https://github.com/SorenHeidelbach/epirefine/blob/main/docs/getting-started.md)
walks through making the pileup and the motif file, running EpiRefine, and writing the refined bins as FASTA.

## What you get

The refined binning is in **`epirefine_out/new_contig_bin.tsv`** (`contig<TAB>bin`): the bins without their
contaminants and with the included contigs added. Every contig's decision, with its probabilities, is in
**`contig_decisions.tsv`**:

| Status | Meaning |
|---|---|
| `kept` | binned, and its methylation fits its bin |
| `contaminant` | binned, but its methylation contradicts its bin: removed |
| `moved` | a contaminant that clearly belongs to another bin: moved there |
| `mixed` | a long contig that is partly its bin's and partly another genome's: kept, with a map of which stretch fits which bin |
| `included` | unbinned, and its methylation fits one bin: added to it |
| `unbinned` | unbinned, and no bin fits well enough: left out |
| `redundant` | unbinned; would be included, but its sequence is already in the bin |
| `low_coverage`, `too_short`, `no_methylation_evidence`, `sole_contig_of_bin` | not judged: too few reads; too short; its bin cannot be told from a genome without a bin (or too few sites), which is not a sign of contamination; or nothing to compare it with. A binned contig with one of these stays in its bin |

`bin_contamination.tsv`, `include_contigs.tsv` and `decontaminated_contig_bin.tsv` list the flags, the inclusion
candidates and the bins without their contaminants in a fixed column format for downstream scripts. All files are
described in [docs/outputs.md](https://github.com/SorenHeidelbach/epirefine/blob/main/docs/outputs.md).

## Documentation

| Guide | For |
|---|---|
| [Getting started](https://github.com/SorenHeidelbach/epirefine/blob/main/docs/getting-started.md) | a first run, step by step, from reads to refined bins |
| [Options](https://github.com/SorenHeidelbach/epirefine/blob/main/docs/usage.md) | every command-line option, its default and when to change it |
| [Outputs](https://github.com/SorenHeidelbach/epirefine/blob/main/docs/outputs.md) | every output file and column |
| [How it works](https://github.com/SorenHeidelbach/epirefine/blob/main/docs/how-it-works.md) | the model in plain terms, from read counts to decisions |
| [FAQ and troubleshooting](https://github.com/SorenHeidelbach/epirefine/blob/main/docs/faq.md) | common questions, runtime, memory, odd results |
| [Changelog](https://github.com/SorenHeidelbach/epirefine/blob/main/CHANGELOG.md) | what changed in each version |

## When to trust it

- **Most of its calls are right, though less often on new data than on the data it was developed on.** On the six
  monoculture test and validation communities, 311 of 316 flags (98.4%) were true contaminants and 1,096 of 1,180
  placements (92.9%) went into the right bin. Of the placements, 98.4% were right in the test set and 89.6% in the
  validation set, where contigs of genomes without a bin were placed into bins with few methylated motifs. It found
  311 of 363 planted contaminants (85.7%). These communities are spent: they were looked at during development, so
  they are no longer an unseen evaluation (version 0.10.1; 0.11.0 gives the same results). With the same
  version, on the development benchmarks every flag and 99.8% of placements were right. With 0.12.0, on the six
  training communities (development) it found 94.8% of the contaminants with a flag precision of 100% and put back
  89.6% of the removed contigs ([benchmarks/results.tsv](https://github.com/SorenHeidelbach/epirefine/blob/main/benchmarks/results.tsv)).
  No evaluation on data held out from development has been run yet. The manuscript will report it.
- **It makes no call rather than guess.** Contigs below 10x coverage or shorter than 5 kb get no call (the defaults
  since 0.13; 5x and 1 kb before). Neither do contigs that would stay where they are when their bin cannot be told
  from a genome without a bin, or when they have too few sites (`no_methylation_evidence`, which is not a sign of
  contamination). On a complex fecal sample (a development sample) the 10x and 5 kb limits leave 13% of the contigs
  and 48% of the assembled sequence to be judged. If the calibration finds no usable temperature (it ends at the cap
  of its grid), the run makes no call at all and says so in the log.
- **Results depend on the motif table.** They also depend on whether genomes without a bin share a bin's
  methylation: their contigs are then placed in that bin, and more short contigs get no call. See
  [known limitations](https://github.com/SorenHeidelbach/epirefine/blob/main/docs/faq.md#known-limitations).
- **Methylation cannot separate genomes that methylate the same motifs**, such as very close strains. EpiRefine
  then keeps both bins in its credible set or leaves the contig where it is.

More in the [FAQ](https://github.com/SorenHeidelbach/epirefine/blob/main/docs/faq.md).

## Citing

A manuscript is in preparation. Until then, please cite this repository and the version you used
(`epirefine --version`). `CITATION.cff` holds the citation data (GitHub's "Cite this repository"). EpiRefine takes
its motifs from [Nanomotif](https://github.com/MicrobialDarkMatter/nanomotif) (motif discovery) and its read counts
from [modkit](https://github.com/nanoporetech/modkit) (the pileup).

## Contributing

Bug reports and questions: [GitHub issues](https://github.com/SorenHeidelbach/epirefine/issues). To work on the
code:

```bash
git clone https://github.com/SorenHeidelbach/epirefine.git && cd epirefine
uv venv && uv pip install -e ".[dev]"
.venv/bin/pytest -q
```

The editable install compiles the kernels in `rust/` and needs a Rust toolchain and a C compiler (with a conda
toolchain, set `AR=gcc-ar`). `EPIREFINE_RS=0` runs the numpy code in such an install.

Development rules are in [CLAUDE.md](https://github.com/SorenHeidelbach/epirefine/blob/main/CLAUDE.md). The
benchmark used for every release is in `benchmarks/`, with results per version in
[benchmarks/results.tsv](https://github.com/SorenHeidelbach/epirefine/blob/main/benchmarks/results.tsv). Notes on
the statistical model and performance are in
[docs/development/](https://github.com/SorenHeidelbach/epirefine/tree/main/docs/development).

Licence: [MIT](https://github.com/SorenHeidelbach/epirefine/blob/main/LICENSE).

