Metadata-Version: 2.4
Name: fastamodeller
Version: 1.0.0
Summary: Generate random DNA, RNA and protein sequences in FASTA format from a JSON configuration.
Author-email: Daniel Pérez Rodríguez <dperez@iim.csic.es>
Maintainer-email: Daniel Pérez Rodríguez <dperez@iim.csic.es>
License-Expression: MIT
Project-URL: Homepage, https://github.com/Dannyzimmer/fastaModeller
Project-URL: Repository, https://github.com/Dannyzimmer/fastaModeller
Project-URL: Issues, https://github.com/Dannyzimmer/fastaModeller/issues
Keywords: fasta,dna,rna,protein,bioinformatics,sequence-generator
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
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: Topic :: Scientific/Engineering :: Bio-Informatics
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: click>=8.1
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: ruff>=0.6; extra == "dev"
Requires-Dist: mypy>=1.10; extra == "dev"
Requires-Dist: build>=1.2; extra == "dev"
Requires-Dist: twine>=5; extra == "dev"
Dynamic: license-file

# fastaModeller

[![PyPI](https://img.shields.io/pypi/v/fastamodeller.svg)](https://pypi.org/project/fastamodeller/)
[![Python](https://img.shields.io/pypi/pyversions/fastamodeller.svg)](https://pypi.org/project/fastamodeller/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

Generate random DNA, RNA or protein sequences in FASTA format from a JSON
configuration file.

`fastaModeller` is a command-line tool, but the same engine is available as a
small Python library.

## Features

- Random sequences with a **final length** between `min_len` and `max_len`
  (inserts and repeats are included in the final length).
- Configurable residue composition for DNA, RNA and protein alphabets.
- IUPAC ambiguity symbols (`N`, `R`, `Y`, ...) and protein `B`, `Z`, `X`, `J`.
- Tandem repeat motifs with per-sequence likelihood.
- Inserts that can be split into fragments, separated by random gaps and
  mutated.
- Reproducible generation from a seed. Each batch derives an independent RNG
  from the master seed, so adding or reordering batches does not alter others.
- Streaming FASTA output (sequences are not retained in memory) and an
  incremental report.
- Reports include composition, GC content for nucleic acids, ambiguous
  fraction, insert mutations/gaps and repeat counts.

## Installation

```bash
python -m pip install fastamodeller
```

Or install it as an isolated command-line application:

```bash
pipx install fastamodeller
```

Python 3.10 or newer is required.

## Command line

```bash
# Write FASTA to stdout; the seed notice goes to stderr.
fastamodeller generate config.json

# Write files explicitly.
fastamodeller generate config.json -o output.fasta -r report.txt

# Only print the report.
fastamodeller generate config.json --only-report

# Override the seed (0 is a valid seed).
fastamodeller generate config.json --seed 0

# Read the configuration from stdin.
fastamodeller generate - -o output.fasta -r report.txt -q < config.json

# Validate without generating.
fastamodeller validate config.json

# Create a template.
fastamodeller template ./examples
```

`python -m fastamodeller` is equivalent to the `fastamodeller` command.
The alias `fastaModeller` is also installed for backwards compatibility.

Use `-q/--quiet` to suppress the seed notice. The seed is always included in
the report.

## Configuration format

```json
{
    "molecule": "dna",
    "id_padding": 4,
    "seq_wrap": 70,
    "seed": false,
    "sequences": [
        {
            "base_id": "TEST",
            "generate": 10,
            "min_len": 500,
            "max_len": 1000,
            "proportion": {"A": 0.20, "T": 0.25, "C": 0.20, "G": 0.35},
            "repeats": [
                {
                    "likelihood": 0.5,
                    "pattern": "ATCATT",
                    "pattern_min_reps": 10,
                    "pattern_max_reps": 50
                }
            ],
            "inserts": [
                {
                    "total": 4,
                    "min_split": 0,
                    "max_split": 5,
                    "ave_gap": 5,
                    "sd_gap": 2,
                    "mutation_rate": 0.05,
                    "sequence": "CCCCCCTTAAACACTTATACGGTAG"
                }
            ]
        }
    ]
}
```

### Top-level fields

| Field | Meaning |
|---|---|
| `molecule` | `dna` (default), `rna` or `protein`. One molecule per file. |
| `id_padding` | Width of the numeric suffix in FASTA identifiers. |
| `seq_wrap` | Line width. `false` or `0` disables wrapping. |
| `seed` | Optional integer for reproducibility. Omit or use `false` for a random seed. |
| `sequences` | List of sequence groups (batches). Required and non-empty. |

### Sequence fields

- `base_id`: unique prefix for FASTA identifiers (`TEST0001`, ...).
- `generate`: number of sequences in the batch.
- `min_len` / `max_len`: bounds for the **final** sequence length. If the
  inserts and repeats of a given sequence exceed `max_len`, generation raises a
  clear error rather than silently exceeding the limit.
- `proportion`: relative weight per residue. If omitted, a uniform composition
  over the canonical alphabet is used.
- `repeats`: for each sequence, the repeat is included with probability
  `likelihood`; its tandem copy count is sampled between
  `pattern_min_reps` and `pattern_max_reps`.
- `inserts`: `total` copies are distributed across the batch, so every
  requested copy is used. Each copy may be split into fragments
  (`min_split`/`max_split`), separated by normally distributed gaps
  (`ave_gap`/`sd_gap`) and mutated per residue (`mutation_rate`).

### Alphabets

- DNA: `A C G T`, plus IUPAC ambiguity `R Y S W K M B D H V N`.
- RNA: `A C G U`, plus the same ambiguity codes.
- Protein: the 20 standard amino acids, plus `B Z X J U O`.

Ambiguity symbols are accepted in inserts, repeats and compositions. To emit
them in the random filler, include them in `proportion`.

## Python API

```python
from fastamodeller import ConfigLoader, generate_to_files

config = ConfigLoader("config.json").load()
result = generate_to_files(
    config,
    fasta_path="output.fasta",
    report_path="report.txt",
)
print(result.seed)
```

The CLI is the recommended integration path for other applications.

## Integration with SeqModeller

SeqModeller can invoke the CLI without depending on the old `libs/` package:

```python
import json
import subprocess
import sys

result = subprocess.run(
    [
        sys.executable,
        "-m",
        "fastamodeller",
        "generate",
        "-",
        "-o",
        str(fasta_path),
        "-r",
        str(report_path),
        "-q",
    ],
    input=json.dumps(config),
    text=True,
    capture_output=True,
)

if result.returncode != 0:
    raise RuntimeError(result.stderr.strip())
```

Exit codes: `0` success, `1` configuration or generation error, `2` command
usage error. Errors are written to stderr. FASTA, report and configuration
files are written atomically.

## Architecture

```text
fastamodeller/
  config/      # immutable models, alphabets, composition, loader, validation
  generation/  # RNG, mutation, repeats, inserts, assembly, service
  reporting/   # incremental statistics and text formatting
  output/      # atomic FASTA/report/config writers
  cli.py       # Click commands
```

Generation depends on small protocols (composition sampling, mutation,
sequence types) and receives them from the configuration. Adding a new molecule
is a matter of registering a `SequenceType` and its composition; the generation
and reporting code contains no DNA-specific conditions.

## Development

```bash
python -m pip install -e ".[dev]"
python -m pytest
python -m ruff check fastamodeller tests fastaModeller.py deploy.py
python -m mypy fastamodeller/config fastamodeller/generation fastamodeller/reporting fastamodeller/output fastamodeller/cli.py
```

To build and inspect a release locally:

```bash
python -m build
python -m twine check dist/*
```

Releases are published to PyPI from GitHub Actions when a GitHub release is
created. The workflow uses PyPI trusted publishing and requires the `pypi`
environment configured in the repository.

## License

MIT. See [LICENSE](LICENSE).
