Metadata-Version: 2.5
Name: sioff
Version: 0.3.0
Summary: siOFF — siRNA off-target discovery pipeline for analyzing RIsearch3 predictions
Project-URL: Homepage, https://github.com/RTH-tools/siOFF
Project-URL: Documentation, https://rth-tools.github.io/siOFF/
Project-URL: Repository, https://github.com/RTH-tools/siOFF
Project-URL: Issues, https://github.com/RTH-tools/siOFF/issues
Project-URL: Changelog, https://github.com/RTH-tools/siOFF/blob/main/CHANGELOG.md
Author-email: Lorenzo Favaro <lorenzo@rth.dk>
License-Expression: BUSL-1.1
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
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-Python: >=3.11
Requires-Dist: biopython
Requires-Dist: loguru
Requires-Dist: ncls>=0.0.63
Requires-Dist: numpy>=2.4.1
Requires-Dist: omegaconf>=2.3.0
Requires-Dist: polars>=2.0.0
Requires-Dist: pyarrow
Requires-Dist: pyyaml
Requires-Dist: rich>=14.3.0
Requires-Dist: risearch
Requires-Dist: typer
Requires-Dist: viennarna==2.7.2
Provides-Extra: lts-cpu
Requires-Dist: polars[rtcompat]; extra == 'lts-cpu'
Requires-Dist: risearch[lts-cpu]; extra == 'lts-cpu'
Description-Content-Type: text/markdown

# siOFF

[![CI](https://github.com/RTH-tools/siOFF/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/RTH-tools/siOFF/actions/workflows/ci.yml?query=branch%3Amain)
[![Release](https://img.shields.io/github/v/release/RTH-tools/siOFF?include_prereleases&sort=semver)](https://github.com/RTH-tools/siOFF/releases)
[![PyPI](https://img.shields.io/pypi/v/sioff)](https://pypi.org/project/sioff/)
[![License](https://img.shields.io/badge/license-BUSL--1.1-blue)](https://github.com/RTH-tools/siOFF/blob/main/LICENSE)
[![Python](https://img.shields.io/python/required-version-toml?tomlFilePath=https%3A%2F%2Fraw.githubusercontent.com%2FRTH-tools%2FsiOFF%2Fmain%2Fpyproject.toml)](https://github.com/RTH-tools/siOFF/blob/main/pyproject.toml)

siOFF — siRNA off-target discovery pipeline.

Documentation (usage guide, changelog and API reference): <https://rth-tools.github.io/siOFF/>

A bioinformatics pipeline for **siRNA off-target discovery and probability quantification**. Integrates RNA-RNA interaction predictions with transcriptome annotations, RNA accessibility profiling, and thermodynamic modeling to rank off-target binding sites.

siOFF is a from-scratch re-implementation of the siRNA off-target discovery
pipeline originally distributed with
[RIsearch2](https://rth.dk/resources/risearch)
([Alkan *et al.*, Nucleic Acids Research 2017](https://doi.org/10.1093/nar/gkw1325)).
It keeps the same thermodynamic model and still reads legacy RIsearch2
prediction files, but replaces the original C toolchain with a single
Python package built on Polars, adds in-process bindings for
[RIsearch 3](https://github.com/RTH-tools/risearch) (Rust/PyO3, on
[PyPI](https://pypi.org/project/risearch/) and
[crates.io](https://crates.io/crates/risearch)),
Parquet-based accessibility profiles, and a Slurm-aware orchestrator.

The pipeline has three stages, mapped one-to-one onto CLI commands:

```mermaid
flowchart LR
    subgraph inputs [Inputs]
        Q([siRNA FASTA])
        G([Genome / transcriptome FASTA])
        A([Annotation GTF/GFF3<br>with expression])
    end

    subgraph s1 [1 · Predict]
        R["sioff search<br>(RIsearch RNA-RNA interactions)"]
    end

    subgraph s2 [2 · Fold]
        F["sioff accessibility<br>(RNAplfold opening energies)"]
    end

    subgraph s3 [3 · Score]
        O["sioff off-targets<br>(annotate · weight · partition function)"]
    end

    T([Ranked off-target table<br>+ per-siRNA summaries])

    Q --> R
    G --> R
    G --> F
    R -- binding sites --> O
    F -- opening energies --> O
    A -- expression --> O
    O --> T
```

Rounded nodes are data, rectangles are pipeline stages. Stages 1 and 2 are
independent and can run in parallel; stage 3 combines their outputs. Stage 1
can be skipped entirely if you already have
[RIsearch](https://github.com/RTH-tools/risearch) prediction files —
`sioff off-targets` reads them directly.

---

## Installation

### Prerequisites

- **Python ≥ 3.11** (tested on 3.11–3.15)
- **[uv](https://docs.astral.sh/uv/)** (recommended) or pip
- `risearch` and ViennaRNA are installed automatically as Python dependencies
  (pinned `3.0.0a4` and `2.7.2`) — no system-level install needed. Prebuilt
  `risearch` wheels cover Linux x86_64 and Apple Silicon macOS; other platforms
  build it from source, which needs a **Rust toolchain** ([rustup](https://rustup.rs)).
  On Python 3.15, ViennaRNA has no prebuilt wheel yet and is compiled from
  source during install (needs a C/C++ compiler; takes several minutes).

### Install

Install from [PyPI](https://pypi.org/project/sioff/):

```bash
pip install sioff          # or: uv pip install sioff

# Verify
sioff --help
```

The PyPI distribution name and the import name are both **`sioff`**. One install
gives you the complete pipeline: `off-targets` / `accessibility` on pre-computed
RIsearch predictions **and** the in-process `index` / `search` commands (plus
the `--sirna-fasta` in-process mode of `off-targets`).

Older CPU without AVX2, or setting up a development clone? See
[Advanced installation](#advanced-installation).

---

## Quick start: run the bundled example

The repository ships a small, internally consistent example dataset in
[`examples/data/`](examples/data): a 6 kb toy genome, 5 siRNAs, an annotation
with expression values, pre-computed RIsearch predictions, and pre-computed
accessibility profiles.

The example data is part of the repository, not of the PyPI package, so clone
the repository to get it and run the commands below from the clone's root:

```bash
git clone https://github.com/RTH-tools/siOFF.git
cd siOFF
```

The installed `sioff` works from there (in a [development
install](#development-install), prefix the commands with `uv run`). This run
never calls `risearch` and takes a few seconds:

```bash
# Pre-computed predictions file
sioff off-targets \
  -r examples/data/predictions.out \
  -t examples/data/annotation.gtf \
  -a examples/data/accessibility \
  -o example_run/off_targets.tsv
```
This produces `example_run/off_targets.tsv` and prints a summary like

```
✓ Processed 86 predictions → example_run/off_targets.tsv
✓ Wrote 5 .summary files to example_run
```

The same run expressed as a YAML config (the config is the complete, commented
reference for every option):

```bash
sioff -c example_yaml/off-targets.example.yaml
```

Both produce the same table; the YAML run writes to
`example_yaml/sioff_example_out/`.

### What you get

`example_run/` contains the main table `off_targets.tsv` — one row per
annotated binding site, in one block per siRNA, each ranked by off-target
probability — plus one
`<sirna_id>.summary` file per siRNA with its partition-function statistics.
The key columns:

| Column | Description |
|--------|-------------|
| `sirna_id` | siRNA the site belongs to |
| `chrom`, `start`, `end`, `strand` | Binding site location: first and last nucleotide, counted from 1 |
| `gene_id`, `transcript_id` | Overlapping annotation feature |
| `energy` | Duplex hybridization energy from RIsearch (kcal/mol) |
| `opening_energy` | Accessibility penalty — cost to unfold the site (kcal/mol) |
| `dG_total` | `energy + opening_energy` |
| `exp_value` | Expression of the overlapping transcript (e.g. RPKM) |
| `P_off_target` | Off-target probability (per-siRNA partition function) |

For example, `siRNA_4` has two equally strong binding sites in the toy data.
The `gene_6` transcript is six times more expressed, but its site costs
1.7 kcal/mol more to open, so the more accessible site in `gene_5` takes most
of the probability:

```
sirna_id  gene_id  energy  opening_energy  dG_total  exp_value  P_off_target
siRNA_4   gene_5   -34.15  5.0             -29.15    100        0.724
siRNA_4   gene_6   -34.15  6.7             -27.45    600        0.276
```

The full column list is documented in [Output](#output).

## Full example: recompute everything from FASTA

The quick start consumed pre-computed predictions and accessibility profiles.
This example rebuilds both from the raw example FASTA files — the same three
stages you will run on your own data.

```bash
# 1 · Fold: accessibility profiles, one packed memory-mappable .npy cache per strand
# run with --verbose to see the steps involved
sioff accessibility \
  -f examples/data/genome.fa \
  -o example_run/accessibility

# 2 · Predict + 3 · Score in one command: RIsearch runs in-process
#     on the siRNA FASTA, then sites are annotated and scored
# run with --verbose to see the steps involved
sioff off-targets \
  -s examples/data/sirnas.fa \
  --target-fasta examples/data/genome.fa \
  -t examples/data/annotation.gtf \
  -a example_run/accessibility \
  -o example_run/off_targets.tsv
```

This produces the **same table as the quick start** — the bundled
`predictions.out` and `accessibility/` were generated exactly this way (see
[`examples/generate_example_data.py`](examples/generate_example_data.py)).

For repeated searches against the same target, build the index once and reuse
it:

```bash
sioff index examples/data/genome.fa -o example_run/genome.idx

sioff off-targets \
  -s examples/data/sirnas.fa \
  -idx example_run/genome.idx \
  --target-fasta examples/data/genome.fa \
  -t examples/data/annotation.gtf \
  -a example_run/accessibility \
  -o example_run/off_targets.tsv
```

Standalone prediction (stage 1 by itself) is also available as
`sioff search query.fa target.idx -t target.fa` — see the
[CLI reference](#cli-reference).

---

## Running on your own data

### What you need

1. **siRNA guide strands** — FASTA, one entry per siRNA (RNA or DNA alphabet).
2. **Target sequences** — genome or transcriptome FASTA. This decides the
   analysis mode: `--type gw` (genome-wide, default) intersects predictions
   with the annotation by genomic coordinates; `--type tw`
   (transcriptome-wide) matches the prediction's target ID against
   `transcript_id` directly. There the annotation only has to say how much of
   each transcript there is: its `transcript` rows are used by default
   (`--feature`), and a transcript given as several rows (its exons) still
   counts each hit once; those rows must carry the same expression.
3. **Annotation with expression** — GTF or GFF3. Expression is
   read from an attribute of your choice (`--expression-metric`, default
   `RPKM`) — annotate your GTF with RPKM/TPM values from your expression data
   first. Sites that don't overlap any feature are dropped. A site counts once
   per transcript: a site touching two exons of one transcript counts that
   transcript once, and every transcript containing the site contributes its
   own expression. The value must therefore be each transcript's own
   abundance (isoform-level TPM/RPKM), so that the transcripts of a gene add
   up to the gene: a gene-level value copied onto every transcript counts the
   gene once per isoform. BED is not accepted: with a single ID column it
   cannot say which rows belong to the same transcript.
   Hits and features are 1-based closed intervals, so a site that shares a
   single position with an exon overlaps it. Exons with expression ≤ 0 are
   dropped after loading: they would add weight 0 to every partition function.
4. Either **pre-computed [RIsearch](https://github.com/RTH-tools/risearch)
   predictions** (a TSV / `.out.gz` / Parquet file, or a folder of them) **or**
   a siRNA FASTA plus target FASTA so siOFF can run RIsearch itself in-process.
   A target is named by the first word of its FASTA header, which is what
   the annotation and the accessibility profiles are matched on: the
   `risearch` command line writes the whole header line as the target name
   (`rna-XM_1.2 CDS=163-1434`), and siOFF cuts it at the first whitespace.
5. Optional, but required for `--alpha`/`--gamma` other than 1: the **siRNA
   table** (`--sirna-table`), which names each siRNA's intended target (it
   enters the partition function as the on-target term) and gives its guide
   sequence (for the perfect-match energy `E_min` needs, see
   [Thermodynamic Model](#thermodynamic-model)). See
   [Step 3](#step-3--off-target-analysis).

### Step 1 — RIsearch predictions

If you already have RIsearch output, skip this step and pass the file (or a
folder of files) to `-r`.

Otherwise, generate the predictions with RIsearch in two commands: build the
target index once (suffix-array construction is the expensive part; reuse
`genome.idx` for every later search against this genome), then search your
siRNAs against it:

```bash
sioff index genome.fa -o genome.idx
sioff search sirnas.fa genome.idx -t genome.fa -o predictions.tsv
```

`predictions.tsv` is what Step 3 reads with `-r`. The defaults are seed length
6, energy threshold −10 kcal/mol, extension 20 nt and the Turner 2004 energy
matrix; change them with `-s`, `-E`, `-e` and `-z` (see
[`sioff search`](#search)).

Alternatively, skip the separate file: `off-targets` runs the same search
in-process when given `-s/--sirna-fasta sirnas.fa --target-fasta genome.fa`
(plus `-idx genome.idx` to reuse the index) instead of `-r`; `-s` is only that
search's query and cannot be combined with `-r`, as in the
[full example](#full-example-recompute-everything-from-fasta).

### Step 2 — Accessibility profiles

```bash
sioff accessibility -f genome.fa -o accessibility/ -j 8
```

Writes RNAplfold opening energies for every FASTA record (window `-W 80`, span
`-L 40`, unpaired length `-u 30` by default — change them consistently if
your protocol differs). The default `--output-format npy` writes the uint8
cache that `off-targets` memory-maps: `accessibility.{plus,minus}.packed.npy`
(value × 10 per position and `u`, all records back to back) plus an
`accessibility.{plus,minus}.packed.npz` index holding the record offsets and
exactly the few values a byte cannot (above 25.5 kcal/mol). That is four files
whatever the number of FASTA records, or two with `--strand +`, and
`off-targets -a` reads them with no conversion step. `--output-format parquet`
still writes one `{chrom}.accessibility.parquet` per record (columns
`position`, `strand`, `u1..u{u}`), for other tools.
Each sequence is folded as given (`+`) and as its reverse complement (`-`).
For a transcriptome FASTA, whose records are already in sense orientation,
`--strand +` skips the reverse complement and halves the work; run
`off-targets` with `--sense-only` on such profiles, or every antisense hit
keeps the default opening energy (10 kcal/mol, with a warning).
Records fold in parallel (`-j` processes). With the default npy output,
short records such as transcripts are grouped into batches of about 2 Mb and
a long chromosome is a batch of its own; `--output-format parquet` folds one
record per task. This is the
most compute-intensive stage: budget hours and tens of GB of memory for a
mammalian genome — run it on a cluster (see
[Orchestrated runs](#orchestrated-multi-step-runs-local--slurm)) and reuse
the profiles across all subsequent analyses of that genome.

Accessibility is optional: without `-a`, opening energies are 0 and ranking
uses hybridization energy and expression only.

### Step 3 — Off-target analysis

```bash
sioff off-targets \
  -r predictions.tsv \
  -t annotation.gtf \
  -a accessibility/ \
  --expression-metric RPKM \
  --type gw \
  --sirna-table sirna_table.tsv \
  -o results/off_targets.tsv
```

`-r` takes a file or a folder (a file is a folder of one). Every input is split
into per-siRNA parts, scored in parallel with `-j` processes, and written as one
block of rows per siRNA, sorted by `P_off_target` descending. The split writes
every input again under `TMPDIR`, so for large runs `TMPDIR` needs free space
about the size of the input; a Parquet file that holds one siRNA is the
exception, scored in place, so a folder of per-siRNA Parquet files needs none.

`--sirna-table` is a tab-separated file with a header; columns are found by
name, `#` lines are skipped:

| Column | Meaning |
|--------|---------|
| `sirna_id` | The siRNA, as named in the predictions |
| `on_target_id` | Transcript or gene ID of its intended target; may be empty (no on-target) |
| `sequence` | Guide strand, the one that was searched (`U` or `T`) |

[`examples/data/sirna_table.tsv`](examples/data/sirna_table.tsv) is a worked
example. The table is required when `--alpha`/`--gamma` set a pair other than
1,1; at the baseline it only supplies the on-target.

`-o` takes a `.tsv`, `.csv` or `.parquet` name (the suffix is replaced by the one
of `--output-format`); any other name gets the extension appended, so
`-o out` writes `out.tsv` with the `.summary` files in `out/`. The run
overwrites an earlier table at that path, or removes it when it writes no
rows; an argument error leaves it in place.

Useful knobs: `--alpha/--gamma/--theta` for parameter sweeps (semicolon-
separated values, each combination adds a `P_off_target:...` column),
`-j` for worker count, `--output-format parquet` for large runs,
`--detailed-report` for the raw per-interaction weights, `--profile` for a
timing and memory breakdown. Prefer the YAML
config for reproducibility — [`example_yaml/off-targets.example.yaml`](example_yaml/off-targets.example.yaml)
documents every option; run it with `sioff -c my-config.yaml`.

#### Accessibility cache

`off-targets` memory-maps the uint8 `accessibility.{plus,minus}.packed.npy`
cache, so every worker shares one page-cache copy. `sioff accessibility`
writes it by default. A Parquet `-a` is packed into it on the first run, next
to the Parquet or in `--accessibility-cache-dir` (for example node-local
scratch, or when `-a` is read-only), and rebuilt (with a warning) when its
Parquet changes, as after re-folding into the same directory. A complete
v0.2.0 per-record cache (`{chrom}_{plus,minus}.access.npy`) next to the
Parquet is still read, and a directory of such files is still accepted as
`-a`. The cache takes one byte per position and `u` on each strand, about
186 GB for hg38 at `-u 30`; use
`--accessibility-cache-dir` to put it on a disk with room. A cache directory
that holds a complete cache needs no Parquet in `-a`. `off-targets` stops
with an error when neither `-a` nor `--accessibility-cache-dir` holds any
profile, and when it cannot write the cache.
A failed worker never passes silently: the successful siRNAs are written, then
`off-targets` reports `N/M tasks failed (input files or siRNAs) ...; first
error: ...` and exits 1.

### Orchestrated multi-step runs (local + Slurm)

`scripts/run_pipeline.py` runs all stages in dependency order from one config —
`index` and `accessibility` in parallel, `off-targets` after `accessibility`.
It ships in the repo (`scripts/`), not as an installed console script.

```bash
# Dry-run (print commands without executing)
python scripts/run_pipeline.py --config example_yaml/run-pipeline.example.yaml --dry-run

# Run locally (sequential, logs to logs/<timestamp>/)
python scripts/run_pipeline.py --config example_yaml/run-pipeline.example.yaml

# Run a subset of steps
python scripts/run_pipeline.py --config example_yaml/run-pipeline.example.yaml --steps accessibility,off-targets

# Submit to Slurm (add --dry-run to preview the sbatch dependency chain)
python scripts/run_pipeline.py --config example_yaml/run-pipeline.example.yaml --slurm
```

Slurm resources come from the config's `slurm:` key (per-step overrides
allowed); the CLI flags `--partition`, `--time`, `--mem`, `--cpus-per-task` and
`--account` override it for all steps. A top-level `transcriptomes:` list fans
one launch out over several genomes/transcriptomes, one Slurm job per entry,
each with its own predictions, annotation and output, so the per-siRNA
partition functions never mix. The commented
[`example_yaml/run-pipeline.example.yaml`](example_yaml/run-pipeline.example.yaml)
is the full reference for both; paths resolve relative to the config file.

---

## CLI Reference

### Global options

Available on `sioff` itself, before any subcommand.

| Flag | Description |
|------|-------------|
| `-c / --config` | Path to a YAML config file; runs the command named in it. Top-level only — `sioff -c cfg.yaml`, not `sioff off-targets -c cfg.yaml` |
| `-v / --verbose` | Enable DEBUG-level logging |
| `--version` | Print the installed version and exit (long form only — `-v` is `--verbose`) |

### `off-targets`

| Flag | Description |
|------|-------------|
| `-r / --risearch-file` | Pre-computed predictions: a file (TSV, `.out.gz`, Parquet) or a folder of them; either way split per siRNA and scored in parallel |
| `-s / --sirna-fasta` | siRNA FASTA — the query of the in-process RIsearch run; cannot be combined with `-r` |
| `--target-fasta / --genome` | Target FASTA for in-process RIsearch |
| `-idx / --target-index` | Pre-built RIsearch index (speeds up repeated runs) |
| `-t / --transcriptome` | GTF or GFF3 annotation file |
| `-a / --accessibility-dir` | Directory of accessibility profiles from the `accessibility` command: the packed `accessibility.*.packed.npy` cache (its default) or per-record Parquet files, packed on the first run |
| `--accessibility-cache-dir` | Where the packed cache is read from / built (default: the accessibility dir) |
| `--feature` | Feature type (column 3) of the annotation rows to use (default: `exon`, or `transcript` with `--type tw`); a type that no row has is an error |
| `--expression-metric` | GTF attribute for expression weighting (default: `RPKM`) |
| `--type` | `gw` (genome-wide) or `tw` (transcriptome-wide, default: `gw`) |
| `--sense-only` | Keep only `+`-strand predictions (use it with profiles from `accessibility --strand +`) |
| `--alpha / --gamma / --theta` | Parameter sweep values (semicolon-separated, e.g. `0.8;1.0`) |
| `--sirna-table` | TSV `sirna_id`, `on_target_id`, `sequence` (header required): the on-target and the perfect-match energy for `E_min`; required for `--alpha`/`--gamma` other than 1,1 |
| `-z / --matrix` | Energy parameter set (`t04`, `slh04`, `s95-rna-dna`, `s95-dna-rna` or a DSM TSV path) for the in-process search and the perfect-match energy of `E_min`; use the set the predictions were searched with (default: `t04`) |
| `--detailed-report` | Add the raw weights, partition functions and `E_min` to the table |
| `--summary-only` | Write only the `.summary` files |
| `-T / --temperature` | Temperature °C of everything the run computes (default: 37.0): RT of the partition function, the `-f` fold, the in-process search (`-s`) and the perfect-match energy of `E_min` (the last two take whole degrees, 0–50). Inputs computed earlier keep their own temperature: fold the `-a` profiles and search the `-r` predictions at this temperature too |
| `--output-format` | `tsv` (default), `csv` or `parquet` |
| `--profile` | Print the run's timing and memory breakdown |
| `-j / --workers` | Parallel worker processes (default: CPU count) |
| `-o / --output` | Output table; see [Step 3](#step-3--off-target-analysis) for how the name becomes the file name |

### `accessibility`

| Flag | Description |
|------|-------------|
| `-f / --fasta` | Genome or transcriptome FASTA |
| `-o / --output` | Output directory (`accessibility.{plus,minus}.packed.npy` + `.packed.npz` per strand, or one `{chrom}.accessibility.parquet` per record with `--output-format parquet`) |
| `--output-format` | `npy` (default; the cache `off-targets` memory-maps) or `parquet` |
| `--strand` | `both` (default) or `+`: fold the sequences as given only (transcriptome FASTA; pair with `off-targets --sense-only`) |
| `-W / --window` | RNAplfold window size W (default: 80) |
| `-L / --span` | Max base-pair span L (default: 40) |
| `-u / --unpaired` | Unpaired probability length u (default: 30) |
| `-T / --temperature` | Folding temperature °C (default: 37.0) |
| `-j / --workers` | Parallel folding processes; with the npy output, records are folded in batches of about 2 Mb (default: 1) |

### `index`

| Flag | Description |
|------|-------------|
| `TARGET` | Target FASTA file to index (positional) |
| `-o / --output` | Output index path (default: `<target>.idx`) |

### `search`

| Flag | Description |
|------|-------------|
| `QUERY INDEX` | Query siRNA FASTA and pre-built `.idx` (positional) |
| `-t / --target` | Target FASTA used to build the index |
| `-s / --seed` | Seed spec, RIsearch syntax: `l`, `n:m` or `n:m/l` (default: 6) |
| `--no-gu-seed` | Forbid G:U wobble pairs inside the seed (RIsearch `--no-seed-wobble`; legacy `--noGUseed`) |
| `-z / --matrix` | Energy parameter set: `t04` (default), `slh04`, `s95-rna-dna`, `s95-dna-rna`, or the path of a custom DSM TSV table (`q1 q2 t1 t2 delta_g_kcal_per_mol`) |
| `-e / --max-extension` | Max extension length on each side (default: 20) |
| `-E / --energy` | Energy threshold in kcal/mol (default: −10.0) |
| `--mismatches` | Mismatches allowed inside the seed (RIsearch `--mismatch-max`; default: 0). A mismatched seed holding an uninterrupted run of the full seed length is dropped, as the perfect seed covers it |
| `--mismatch-prefix` | Matches required at the seed's 5′ end before any mismatch (default: 1, as risearch's Python API; its CLI defaults to the mismatch count) |
| `--mismatch-suffix` | Matches required at the seed's 3′ end after any mismatch (default: 0) |
| `-T / --temperature` | Energy-model temperature in °C, 0–50 (default: 37; a custom DSM table ignores it) |
| `-d / --penalty` | kcal/mol added per extended nucleotide, included in the reported energy (default: 0.0). RIsearch2's C `-d` took dacal/mol: its recommended 30 is 0.3 here |
| `--seed-energy` | Accepted by risearch for compatibility; no effect in risearch 3.0.0a4/a5 (default: 0.0) |
| `--no-max-prune` | Also extend non-maximal seeds (redundant hits) |
| `--no-dedup` | Report every seed's hit instead of one best hit per duplex |
| `--alignment` | Add the columns `q_start`, `q_end` (first and last paired query nucleotide, counted from 1) and `alignment` (pairing string, query 5′→3′: `P` pair, `W` G:U, `U` mismatch, `T`/`Q` extra target/query base) |
| `-j / --threads` | Worker threads (default: `RAYON_NUM_THREADS`, else every core) |
| `--sense-only` | Keep only `+`-strand hits, for a target already in sense orientation (a transcriptome) |
| `-o / --output` | Output TSV file (default: stdout) |

Every option is passed to `risearch.search` explicitly with siOFF's default, so
a change of upstream default cannot move the results; the defaults give the
same hits as before these options existed. `--sense-only` is siOFF's own: risearch
indexes and searches both strands and has no option to search one, so the `-`
hits are dropped after the search. The output shrinks; the search's time and
peak memory do not.

---

## Python API

siOFF can be used as a library — `import sioff`, then call the commands as plain functions; they return their results in-memory.

The example below runs on the bundled example data; like the
[Quick start](#quick-start-run-the-bundled-example), run it from the root of a
clone of the repository.

```python
from pathlib import Path

import sioff

data = Path("examples/data")

# Off-target analysis on a pre-computed predictions file → polars.DataFrame
df = sioff.off_targets(
    risearch_file=data / "predictions.out",
    gtf_file=data / "annotation.gtf",
    accessibility_dir=data / "accessibility",
)
print(
    df.sort("P_off_target", descending=True)
    .select("sirna_id", "gene_id", "energy", "dG_total", "P_off_target")
    .head()
)

# Pre-compute accessibility profiles, one per FASTA record → dict[chrom -> polars.DataFrame]
profiles = sioff.accessibility(genome=data / "genome.fa")

# Build a RIsearch index → Path (risearch bindings, in-process)
Path("example_run").mkdir(exist_ok=True)
idx = sioff.index(data / "genome.fa", output="example_run/genome.idx")

# Run a RIsearch search → polars.DataFrame (risearch bindings, in-process)
hits = sioff.search(data / "sirnas.fa", idx, target=data / "genome.fa")

# ... and feed hits and profiles straight into the analysis, no intermediate files
df = sioff.off_targets(
    predictions=hits,
    accessibility=profiles,
    gtf_file=data / "annotation.gtf",
)
```

**Notes**

- `sioff.off_targets` takes its predictions from one of `predictions=` (a `polars.DataFrame` in the `sioff.search` schema: `sirna_id, chrom, start, end, strand, energy`), `risearch_file=` (a RIsearch output file, or a folder of them) or `sirna_fasta=` + `target_fasta=` (run RIsearch in-process), and returns one `polars.DataFrame` for every input (an empty result is a frame with no columns). Pass `stream=True` for a **generator** of one `polars.DataFrame` per siRNA instead, so a large folder never sits in memory at once; argument errors surface at the first `next()`, and `.close()` stops it early. The on-target table is `sirna_table=`. No form writes files — the API layer returns results in memory, and writing output is the CLI's job.
- `sioff.accessibility` likewise writes nothing: it returns `dict[chrom -> polars.DataFrame]`, which `sioff.off_targets(accessibility=...)` takes directly (packed like `-a`, so the opening energies are quantised to 0.1 kcal/mol). Use the `accessibility` CLI command (or `sioff -c <config>`) if you want profiles on disk (the packed `accessibility.*.packed.npy` cache, or one `{chrom}.accessibility.parquet` per record with `--output-format parquet`).
- On bad input the API functions raise ordinary Python exceptions — `FileNotFoundError` or `ValueError` — not `typer.Exit`. `typer.Exit` is raised only by the CLI layer for its own argument validation.
- `sioff.index` / `sioff.search` call the `risearch` bindings in-process — the same dependency the CLI's `index`/`search` commands use.

---

## Thermodynamic Model

Off-target probability is computed from a Boltzmann partition function over all predicted binding sites for each siRNA:

```
W_i  = Expression_i × exp(−ΔG_total_i / RT)

       ΔG_total = ΔG_hybridization + ΔG_opening

Z_s  = Σ W_i  (all off-targets of siRNA s)  +  W_on-target

P(off-target_i | siRNA_s) = W_i / Z_s
```

- **ΔG_hybridization**: duplex interaction energy from RIsearch. The nearest-neighbour parameter set is selectable with `sioff search -z/--matrix` (default Turner 2004).
- **ΔG_opening**: Accessibility penalty — cost to unfold the target region, retrieved from pre-computed `RNA.pfl_fold_up` profiles.
- **Expression weighting**: annotation-derived RPKM/TPM values scale each site's contribution.
- **Per-siRNA normalization**: Partition functions are computed independently per siRNA; mixing them is biologically incorrect.
- **Precision**: the weights `W_i` and `Z_s` are computed in double precision (raw, not shifted), so a very strong duplex cannot overflow them; `P_off_target` is written as float32.
- **E_min** (used by `--alpha`/`--gamma`): the lower of the perfect-match energy of the siRNA's table sequence and the lowest energy among the hits left after `--sense-only` and the annotation intersection. The perfect-match energy is computed with `-z/--matrix`, so pass the set the predictions were searched with. The baseline α=γ=1 and `--theta` do not depend on it. α/γ ≠ 1 results differ from siOFF 0.2.0, except for folder runs (`-r <dir>`) that passed `-s`: E_min now always includes the perfect-match energy from `--sirna-table`, which only those runs had, and folder runs without `-s` no longer take it from every hit in the file, so the hits the intersection drops and, with `--sense-only`, minus-strand hits no longer count.

### Parameter sweeps

`--alpha`/`--gamma` clamp extremely favorable energies; `--theta` scales energy differences around a −10 kcal/mol reference. All combinations are computed in a single Polars `group_by` pass, producing separate `P_off_target:alpha=X,gamma=Y` columns.

Pairs with α > γ are skipped and listed before scoring. An α or γ value that no kept pair uses is an error, not silently dropped.

---

## Output

### TSV (default)

| Column | Description |
|--------|-------------|
| `chrom` | Chromosome or transcript ID |
| `start`, `end` | Binding site coordinates: first and last nucleotide, counted from 1 along the target as written |
| `strand` | Strand orientation |
| `energy` | Hybridization energy (kcal/mol) |
| `opening_energy` | Accessibility penalty (kcal/mol) |
| `dG_total` | Combined free energy |
| `exp_value` | Expression level |
| `P_off_target` | Off-target probability (baseline α=γ=θ=1) |
| `P_off_target:alpha=X,gamma=Y` | Per-parameter-set probabilities |

Rows come in one block per siRNA, each sorted by `P_off_target` descending, in
the `--output-format` you chose. One `<sirna_id>.summary` file per siRNA is written next to the output with the
partition-function statistics (`P`, `Z`, `Zoff`, with and without
accessibility). `--summary-only` skips the per-prediction table — the old
pipeline's default behaviour.

`--detailed-report` adds the raw per-interaction columns to the table:
`boltzmann_weight` (and `boltzmann_weight_noacc`) and `Z_sirna` (and
`Z_sirna_noacc`), each repeated with a `:alpha=X,gamma=Y` or `:theta=T`
suffix per extra parameter set, and `E_min` once. `E_min` follows the rule
under [Thermodynamic Model](#thermodynamic-model) for a siRNA with a
`--sirna-table` row; for one without (no table, which only the baseline
allows, or a missing row) it is the strongest hit left. It cannot be
combined with `--summary-only`.

**Precision of the table.** The energies (`energy*`, `opening_energy`,
`dG_total*`, `E_min`), `exp_value` and every `P_off_target*` are single
precision (Float32); `boltzmann_weight*` and `Z_sirna*` are double precision.
Each weight is `exp_value × exp(−dG_total / RT)` (`energy` in place of
`dG_total` for `_noacc`) with `RT = 0.001987 × (T + 273.15)` kcal/mol,
evaluated on the single-precision values. To recompute a weight exactly, use
`--output-format parquet`, which stores the column types, or read those
columns of the TSV/CSV as Float32: read as double precision, the printed
decimals differ from the stored values by up to about 3e-6 relative in the
weight.

---

## Advanced installation

### CPUs without AVX2

On a CPU without AVX2 (roughly pre-2013 Intel, e.g. Sandy/Ivy Bridge Xeons)
the default polars runtime crashes with an illegal instruction; polars warns
"Missing required CPU features" at import. Install the compat runtime instead:

```bash
pip install "sioff[lts-cpu]"   # SSE4-only polars runtime, for siOFF and risearch alike
```

With both runtimes installed polars picks the compat one; set
`POLARS_PREFER_PKG=32` to force the AVX2 runtime on a machine that has it.

### Development install

From a clone:

```bash
git clone https://github.com/RTH-tools/siOFF.git
cd siOFF
uv sync                    # runtime deps + dev tooling (ruff, pyrefly, pytest)
uv run sioff --help
```

`uv sync` also installs the compat polars runtime (the `lts` dependency group,
on by default because the development server has no AVX2). On a modern machine
use `uv sync --no-group lts` for the AVX2 runtime; CI does the same.

See [RIsearch](#risearch) for how the in-process engine is pinned.

---

## RIsearch

The in-process `index` / `search` engine is
[`risearch`](https://github.com/RTH-tools/risearch)
([PyPI](https://pypi.org/project/risearch/),
[crates.io](https://crates.io/crates/risearch),
[docs](https://rth-tools.github.io/risearch/)): the RIsearch core rewritten in
Rust with PyO3 bindings, called in-process with no subprocess or intermediate
files. `off-targets` and `accessibility` on pre-computed predictions never
import it.

`risearch` is unpinned, so the latest release is installed automatically.
While only alpha builds exist on PyPI, resolvers fall back to pre-releases;
once a stable release is published, the latest stable is preferred.

The previous generation of this pipeline, built on RIsearch2, is at
[rth.dk/resources/risearch](https://rth.dk/resources/risearch); siOFF reads its
output files unchanged.

---

## Citation

If you use siOFF in published work, please cite:

> Roncelli S, Favaro L, Anthon C, Gorodkin J. *RIsearch and siOFF: An integrated,
> high-performance framework for RNA-RNA interaction and siRNA off-target
> prediction.* In preparation.

Machine-readable metadata is in [`CITATION.cff`](CITATION.cff).

For the original method and the previous pipeline, cite:

> Alkan F, Wenzel A, Palasca O, Kerpedjiev P, Rudebeck AF, Stadler PF,
> Hofacker IL, Gorodkin J. *RIsearch2: suffix array-based large-scale
> prediction of RNA-RNA interactions and siRNA off-target discovery.*
> Nucleic Acids Research 2017;45(8):e60.
> [doi:10.1093/nar/gkw1325](https://doi.org/10.1093/nar/gkw1325)

Note that citation is not merely requested but a **term of the BUSL-1.1
licence** for any production use of siOFF or `risearch` — see [License](#license).

---

## License

siOFF (`sioff`) is released under the **Business Source License 1.1
(BUSL-1.1)** — see [LICENSE](LICENSE) — the same licence as its `risearch`
dependency. BUSL-1.1 is *source-available, not open source* and is not
OSI-approved. In brief:

- **Research and production use are free**, including commercial use, with one
  exception below.
- **No hosted services.** You may not use it to provide SaaS, PaaS or any other
  hosted or cloud-based service to third parties — commercial *or*
  non-commercial — except where such services are provided exclusively to
  accredited academic institutions, or individuals affiliated with them, for
  non-commercial research or educational purposes.
- **Citation is a licence term.** Any production use must cite the work; see
  [Citation](#citation).
- **Converts to Apache-2.0 on 14 September 2030.**
- Licensor: RTH, University of Copenhagen. Commercial licensing enquiries:
  <software@rth.dk>.

The summary above is provided for orientation only; [LICENSE](LICENSE) is the
authoritative text.
