Metadata-Version: 2.4
Name: rigel-rnaseq
Version: 0.8.0
Summary: Bayesian RNA-seq transcript quantification tool
Author: Matthew Iyer
License-Expression: GPL-3.0-or-later
License-File: LICENSE
License-File: THIRD_PARTY_NOTICES.txt
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Project-URL: Homepage, https://github.com/mkiyer/rigel
Project-URL: Repository, https://github.com/mkiyer/rigel
Project-URL: Documentation, https://github.com/mkiyer/rigel/blob/main/docs/MANUAL.md
Project-URL: Changelog, https://github.com/mkiyer/rigel/blob/main/CHANGELOG.md
Project-URL: Bug Tracker, https://github.com/mkiyer/rigel/issues
Requires-Python: >=3.12
Requires-Dist: pysam>=0.22
Requires-Dist: numpy>=1.26
Requires-Dist: pandas>=2.1
Requires-Dist: pyarrow>=14.0
Requires-Dist: pyyaml>=6.0
Requires-Dist: scipy>=1.12
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Requires-Dist: pytest-cov>=5.0; extra == "dev"
Requires-Dist: rigel-rnaseq[report]; extra == "dev"
Provides-Extra: report
Requires-Dist: vl-convert-python>=1.7; extra == "report"
Description-Content-Type: text/markdown

<p align="center">
  <img src="https://raw.githubusercontent.com/mkiyer/rigel/main/docs/images/rigel_banner.png" alt="Rigel" width="100%"/>
</p>

<p align="center">
  <strong>Bayesian RNA-seq quantification of annotated transcripts, nascent RNA and genomic DNA in one model</strong>
</p>

<p align="center">
  <a href="https://github.com/mkiyer/rigel/actions"><img src="https://img.shields.io/github/actions/workflow/status/mkiyer/rigel/ci.yml?branch=main&label=CI" alt="CI"></a>
  <a href="https://pypi.org/project/rigel-rnaseq/"><img src="https://img.shields.io/pypi/v/rigel-rnaseq" alt="PyPI"></a>
  <a href="https://anaconda.org/bioconda/rigel"><img src="https://img.shields.io/conda/vn/bioconda/rigel" alt="Bioconda"></a>
  <a href="https://pypi.org/project/rigel-rnaseq/"><img src="https://img.shields.io/pypi/pyversions/rigel-rnaseq" alt="Python"></a>
  <a href="https://github.com/mkiyer/rigel/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-GPL--3.0-blue" alt="License"></a>
</p>

---

## Overview

<p align="center">
  <img src="https://raw.githubusercontent.com/mkiyer/rigel/main/docs/images/rigel_overview.png" alt="Rigel Overview" width="85%"/>
</p>

Rigel quantifies RNA-seq alignments while explicitly modeling three sources of
signal in the same library:

- RNA from the annotated transcripts (every reference transcript, single-exon included)
- Nascent RNA: RNA that has not spliced, over the span of a multi-exon gene
- Genomic DNA contamination (gDNA)

A single-pass native BAM scan feeds a calibration stage that separates RNA from gDNA
genome-wide, and a locus-level EM then assigns the RNA to transcripts. Nascent RNA is
represented as one synthetic span per merged span — transcripts whose start and end sites each lie within
`--end-tolerance` on one strand — shared by every transcript in it, so isoforms that start and
end at about the same coordinates do not multiply nascent states.

### Key features

- Joint quantification of annotated transcripts, nascent RNA and gDNA in one locus-level model
- Single-pass C++ BAM scanner using htslib, with memory-bounded buffering and spill-to-disk support
- Automatic strand-model training from annotated spliced fragments; protocol auto-detection (`R1-sense` / `R1-antisense`)
- gDNA/RNA calibration that solves each genomic region's unspliced mass into sense RNA, antisense RNA and gDNA, with a gDNA-density prior fitted from the library itself
- Calibrated per-locus gDNA priors feeding the EM
- MAP-EM and Variational Bayes EM (VBEM, default) solver modes with SQUAREM acceleration
- Post-EM assignment: `sample` (default: whole counts, each fragment to one transcript or gDNA) or `fractional` (posterior weights)
- Parallel BAM scanning and parallel locus EM controlled through one `--threads` setting
- Feather and TSV outputs plus optional annotated BAM output with per-fragment assignment tags

---

## Installation

### Bioconda

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

### PyPI

```bash
pip install rigel-rnaseq

# with the optional HTML QC report ('rigel report'):
pip install 'rigel-rnaseq[report]'
```

The PyPI package name is `rigel-rnaseq` because `rigel` is already taken on
PyPI. The import name and CLI stay `rigel`.

The `[report]` extra pulls in `vl-convert-python` (on conda:
`conda install -c conda-forge vl-convert-python`); without it `rigel report` still builds a
report, with every chart omitted.

### From source

```bash
git clone https://github.com/mkiyer/rigel.git
cd rigel

mamba env create -f mamba_env.yaml
conda activate rigel

pip install --no-build-isolation -e .
```

### Requirements

- Python 3.12+
- Wheels are published for Linux (x86_64 and aarch64, manylinux_2_28) and macOS 15+ on Apple silicon; anywhere
  else pip builds from source, which needs a C++17-capable compiler, CMake ≥ 3.15, pkg-config and htslib
  (`mamba_env.yaml` installs all of them)
- Runtime dependencies (from `pyproject.toml`): `pysam>=0.22`, `numpy>=1.26`, `pandas>=2.1`,
  `pyarrow>=14.0`, `pyyaml>=6.0`, `scipy>=1.12`

On macOS, install Xcode Command Line Tools first:

```bash
xcode-select --install
```

---

## Quick start

### 1. Build an index

```bash
samtools faidx genome.fa          # the FASTA needs a .fai index
rigel index \
    --fasta genome.fa \
    --gtf annotation.gtf \
    --no-mappability \
    -o index/
```

`rigel index` requires either `--alignable-zarr PATH` (the store the companion `alignable` tool
builds for the same genome and aligner, whose unobservable regions Rigel reads; recommended for real
genomes) or `--no-mappability` to opt out explicitly, when the reference's N runs are its unobservable
regions.

### 2. Quantify a BAM

```bash
rigel quant \
    --bam sample.bam \
    --index index/ \
    -o results/ \
    --tsv
```

Input BAM requirements:

- Paired-end (the scan stops at the first unpaired record)
- Name-sorted or collated
- A read pair's multiplicity is its usable alignments; `NH` is read only to find a truncated output
- For a stranded library, a splice-junction strand tag: `XS` (HISAT2, or STAR with
  `--outSAMstrandField intronMotif`) or `ts` (minimap2). Without one no junction trains the strand
  model and the library is quantified as unstranded; `--sj-strand-tag auto` reads whichever tag each
  spliced record carries
- With STAR, align with `--sjdbScore 0 --outFilterMultimapScoreRange 2`, so an unspliced reading competes
  with an annotated splice (the manual's input requirements say why)

### 3. Inspect outputs

Outputs are Feather files; `--tsv` above also writes `.tsv` mirrors (or convert afterward with
`rigel export results/ --format tsv`).

```bash
head results/quant.tsv
head results/gene_quant.tsv
head results/nascent_quant.tsv
head results/loci.tsv
cat results/summary.json
```

### 4. Build a QC report (optional)

```bash
rigel report results/ -o results/report.html
```

Produces a single self-contained HTML QC file from the files `rigel quant` already wrote, so
reports can be built later and in bulk. Its charts need the `[report]` extra (see
[Installation](#pypi)).

The five subcommands are `index`, `quant`, `sim` (a small synthetic scenario from a YAML
file), `export` (Feather to TSV or Parquet) and `report`. `rigel --version` prints the version;
`rigel -v <subcommand>` (`--verbose`) enables DEBUG-level logging. See
[docs/MANUAL.md](https://github.com/mkiyer/rigel/blob/main/docs/MANUAL.md) for every flag.

---

## Output files

| File | Description |
|------|-------------|
| `quant.feather` / `quant.tsv` | Transcript-level abundance table (one row per annotated transcript; nascent RNA is in `nascent_quant`) with `count`, `count_unambig`, `count_em`, `count_spliced`, `tpm`, `tpm_total_rna`, effective lengths, and per-transcript QC columns |
| `gene_quant.feather` / `gene_quant.tsv` | Gene-level aggregates derived from transcript estimates |
| `nascent_quant.feather` / `nascent_quant.tsv` | Nascent RNA: one row per synthetic span Rigel adds to model the unspliced RNA of multi-exon transcripts (read overlapping spans per gene) |
| `loci.feather` / `loci.tsv` | Per-locus EM summary |
| `summary.json` | Library protocol, strand specificity, per-category fragment-length summary statistics, the calibration scalars, alignment counts, and global quantification totals |
| `fragment_lengths.feather` | Raw per-bin fragment-length histograms, tidy `(category, length, count)` |
| `calibration_track.feather` / `.bedgraph` | Per-region gDNA solution; the bedGraph is a genome-browser track (IGV / UCSC) |
| `config.yaml` | Resolved run configuration (parameters, I/O paths). Rerun with `rigel quant --config config.yaml` |
| `report.html` | Optional self-contained QC report, built by `rigel report` (see step 4) |
| `locus_stats.feather` | Optional per-locus statistics, emitted only with `--emit-locus-stats` |
| `--annotated-bam PATH` | Optional annotated BAM with per-fragment assignment tags (a second BAM pass); the same records as the input, collated |

`tpm` is normalized over annotated transcripts only; `tpm_total_rna` normalizes over all RNA
(annotated and nascent) and is comparable to `nascent_quant`'s `tpm_total_rna`.

---

## How it works

Rigel runs one native BAM pass feeding three stages: **scan**, **calibrate**, and **quantify**.

```
FASTA + GTF ──▶ rigel index ──▶ regions / boundaries partition + transcript tables
BAM ──▶ 1. scan       C++ single pass: resolve fragments, train the strand and fragment-length
                      models, buffer the ambiguous fragments, tally per-region/boundary mass
    ──▶ 2. calibrate  split each region's unspliced mass into sense RNA / antisense RNA / gDNA
                      (strand tilt + neighbour messages + a gDNA-density prior fitted from the
                      library) ──▶ two Dirichlet scalars per locus
    ──▶ 3. quantify   per-locus EM (VBEM or MAP-EM, SQUAREM, OpenMP across loci) with one
                      component per transcript row plus one gDNA component ──▶ counts, TPM
```

A spliced fragment is certified RNA, so calibration's problem is the unspliced mass. Its
answer enters the EM as a prior that a decisive locus likelihood can override. The design and
its rulings are in [docs/DESIGN.md](https://github.com/mkiyer/rigel/blob/main/docs/DESIGN.md); the derivations are in
[docs/EQUATIONS.md](https://github.com/mkiyer/rigel/blob/main/docs/EQUATIONS.md).

---

## Documentation

| Document | Description |
|----------|-------------|
| [docs/MANUAL.md](https://github.com/mkiyer/rigel/blob/main/docs/MANUAL.md) | The user manual: CLI reference, defaults, configuration rules, output schema |
| [docs/DESIGN.md](https://github.com/mkiyer/rigel/blob/main/docs/DESIGN.md) | What is built, and the rulings behind it |
| [docs/EQUATIONS.md](https://github.com/mkiyer/rigel/blob/main/docs/EQUATIONS.md) | The derivations the implementation depends on |
| [docs/SUCCESS.md](https://github.com/mkiyer/rigel/blob/main/docs/SUCCESS.md) | How performance is measured: the accumulator, then calibration against an oracle |
| [docs/TESTING.md](https://github.com/mkiyer/rigel/blob/main/docs/TESTING.md) | The benchmark panels, how to build them, and what the test suite can judge |
| [docs/ROADMAP.md](https://github.com/mkiyer/rigel/blob/main/docs/ROADMAP.md) | The ranked view of what is next for the 0.8.0 release |
| [Issues](https://github.com/mkiyer/rigel/issues) | Open problems, and what was refused (closed as not planned); the [0.8.0 milestone](https://github.com/mkiyer/rigel/milestone/1) is what the release must fix or state |
| [docs/TRAPS.md](https://github.com/mkiyer/rigel/blob/main/docs/TRAPS.md) | Mistakes already made, as named rules |
| [docs/PUBLISHING.md](https://github.com/mkiyer/rigel/blob/main/docs/PUBLISHING.md) | Release workflow for PyPI and Bioconda |

---

## Citing Rigel

If you use Rigel in research, cite the repository for now:

> Iyer MK. Rigel: Bayesian RNA-seq quantification with joint mRNA, nascent RNA,
> and genomic DNA modeling. 2026. https://github.com/mkiyer/rigel

---

## License

Rigel is distributed under the [GNU General Public License v3.0](https://github.com/mkiyer/rigel/blob/main/LICENSE).

---

## Development

```bash
conda activate rigel
pip install --no-build-isolation -e ".[dev]"   # rebuild after any src/rigel/native/ change
python -m pytest tests/ -q
ruff check src/ tests/ scripts/ && ruff format src/ tests/
```

