Metadata-Version: 2.4
Name: TAtouScan
Version: 0.3.0
Summary:  A command-line tool for identifying toxin-antitoxin (TA) systems in genomes and metagenomes. 
Author: Jean Mainguy
Maintainer: Jean Mainguy
License: MIT License
        
        Copyright (c) 2025 Jean Mainguy
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Repository, https://github.com/JeanMainguy/TAtouScan
Project-URL: Changelog, https://github.com/JeanMainguy/TAtouScan/blob/main/CHANGELOG.md
Project-URL: Documentation, https://tatouscan.readthedocs.io
Keywords: Bioinformatics,Prokaryote,Genomics,Toxin antitoxin,Metagenomics
Classifier: Environment :: Console
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: POSIX :: Linux
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
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: typer>=0.15.0
Requires-Dist: rich
Requires-Dist: pyhmmer<0.12.0,>=0.11.0
Provides-Extra: build-reference
Requires-Dist: pyfastx<3.0.0,>=2.0.0; extra == "build-reference"
Requires-Dist: pandas; extra == "build-reference"
Requires-Dist: plotly; extra == "build-reference"
Requires-Dist: scipy; extra == "build-reference"
Provides-Extra: doc
Requires-Dist: sphinx==6.2.1; extra == "doc"
Requires-Dist: sphinx_rtd_theme==1.2.2; extra == "doc"
Requires-Dist: readthedocs-sphinx-search==0.3.1; extra == "doc"
Requires-Dist: sphinx-autobuild==2021.3.14; extra == "doc"
Requires-Dist: myst-parser==1.0.0; extra == "doc"
Requires-Dist: docutils==0.18.1; extra == "doc"
Requires-Dist: nbsphinx==0.9.5; extra == "doc"
Requires-Dist: nbsphinx_link==1.3.0; extra == "doc"
Requires-Dist: sphinx-book-theme==1.0.1; extra == "doc"
Requires-Dist: sphinxcontrib.mermaid; extra == "doc"
Requires-Dist: sphinx_togglebutton==0.3.2; extra == "doc"
Provides-Extra: dev
Requires-Dist: pytest>=8.0.0; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: black>=8.0.0; extra == "dev"
Dynamic: license-file

[![PyPI version](https://badge.fury.io/py/TAtouScan.svg?cache-control=no-cache)](https://badge.fury.io/py/TAtouScan)

# TAtouScan

**TAtouScan** is a command-line tool designed to identify **toxin-antitoxin (TA) systems** in genomes and metagenomes. 


## Installation

TAtouScan is available on [PyPI](https://pypi.org/project/TAtouScan/). It is recommended to install it in a virtual environment:

```bash
python -m venv tatouscan-env
source tatouscan-env/bin/activate
pip install TAtouScan
```

Alternatively, using `conda` for environment management:

```bash
conda create -n tatouscan python=3.12
conda activate tatouscan
pip install TAtouScan
```
> [!NOTE]
> TAtouScan is not yet available via `bioconda`. The above method combines `conda` for environment management and `pip` for installation.


### Download the TAtouScan Database

TAtouScan requires a database directory containing HMM profiles and reference statistics.
The database is distributed on Zenodo: [doi.org/10.5281/zenodo.15305312](https://doi.org/10.5281/zenodo.15305312)


Download the current version (0.3.0) and extract it with:
```bash
wget https://zenodo.org/records/24379358/files/tatouscan_db.tar.gz
tar -xzf tatouscan_db.tar.gz
```

The database directory must contain the following five files:

```
tatouscan_db/
  version.txt            # database version, checked at startup
  ta.hmm                 # HMM profiles (HMMER3 format)
  hmm_info.tsv           # profile metadata (name, type, source, Pfam annotation)
  family_statistics.tsv  # per-family reference statistics for scoring
  known_pairs.tsv        # known toxin–antitoxin family co-occurrences
```

> [!IMPORTANT]
> TAtouScan checks the database version at startup and stops if the database is too old.
> TAtouScan ≥ 0.3.0 requires database version ≥ 0.3.0. Databases released before 0.3.0 (without
> `version.txt`) are not supported anymore: download the latest version from
> [Zenodo](https://doi.org/10.5281/zenodo.15305312).

### Pfam annotation of the HMM profiles

To ease interpretation, each HMM profile is annotated with Pfam (release 38.2). The consensus
sequence of each profile (`hmmemit -c`) is searched against Pfam-A with `hmmscan`, and Pfam
families with a full-sequence E-value ≤ 1e-3 are kept. This adds the following columns to `hmm_info.tsv`
(empty when the profile has no significant Pfam hit):

| Column | Description |
|--------|-------------|
| `pfam_accession` / `pfam_name` / `pfam_description` | Closest Pfam family (lowest E-value) |
| `pfam_evalue` / `pfam_coverage` | E-value of the closest family and fraction of the consensus sequence it covers |
| `pfam_hits` | All significant Pfam families ordered by E-value, as `NAME(ACCESSION):E-value:positions` separated by `;` |

Several Pfam families are often reported for one profile: either several domains of a multi-domain
protein (different positions) or closely related families of the same clan (overlapping positions).

The approach was validated on the TASmania profiles, comparing with their original Pfam annotation
(Pfam 31, profile–profile comparison with PRC). For the 547 profiles whose TASmania annotation is
significant (p ≤ 1e-3), the same Pfam family is recovered every time: as the closest hit for 541 profiles
and among the other significant hits for the remaining 6.


## Usage

After installation and downloading the database, run TAtouScan with:

- a **GFF** file with gene annotations
- a **FAA** file with the corresponding protein sequences
- the **database directory** downloaded above

```bash
tatouscan --gff <genes.gff> --faa <proteins.faa> --db tatouscan_db/
```

By default, results are written to a directory called `tatouscan_results/`. Use `--outdir` to specify a different location:

```bash
tatouscan --gff <genes.gff> --faa <proteins.faa> --db tatouscan_db/ --outdir my_results/
```

Two TSV files are produced inside the output directory:

| File | Description |
|------|-------------|
| `tatouscan_results.tsv` | One row per predicted toxin or antitoxin gene |
| `tatouscan_results_pairs.tsv` | One row per predicted TA pair (two-gene systems only) |



## HMM Database Composition

The HMM database used by TAtouScan is composed of profiles collected from multiple sources, including curated databases and literature. The file `hmm_info.tsv` provides metadata for each profile, indicating its origin and whether it corresponds to a **toxin** or an **antitoxin**.

### Breakdown of the database:

- **682 profiles** were obtained from the [TASmania project](https://doi.org/10.1371/journal.pcbi.1006946):  
  > Akarsu H, Bordes P, Mansour M, Bigot D-J, Genevaux P, Falquet L (2019). *TASmania: A bacterial Toxin-Antitoxin Systems database*. PLoS Comput Biol 15(4): e1006946.  
  > https://doi.org/10.1371/journal.pcbi.1006946

- **3,168 profiles** were generated from sequences in the [TADB 3.0 database](https://bioinfo-mml.sjtu.edu.cn/TADB3/):  
  These sequences were first clustered, and each cluster was then aligned using multiple sequence alignment. HMM profiles were built from the resulting alignments.

  > Guan J, Chen Y, Goh YX, Wang M, Tai C, Deng Z, Song J, Ou HY (2024).  
  > *TADB 3.0: an updated database of bacterial toxin-antitoxin loci and associated mobile genetic elements.*  
  > Nucleic Acids Research, 52(D1): D784–D790.  
  > https://doi.org/10.1093/nar/gkad962

- Additional HMM profiles were manually collected from other sources in the literature.


## Output

TAtouScan writes two TSV files into the output directory.

By default, only the most informative columns are written. Add `--detailed` to include per-source HMM breakdowns and raw Z-score columns.

### `tatouscan_results.tsv` — per-gene results

One row per predicted toxin or antitoxin gene.

| Column | Description |
|--------|-------------|
| `contig_name` | Contig where the gene is located |
| `gene_id` | Gene identifier (from the input GFF) |
| `start` / `end` | Genomic coordinates |
| `strand` | `+` or `-` |
| `length_aa` | Protein length in amino acids |
| `product` | Predicted gene product (if available) |
| `ta_system_id` | ID shared by both genes of a pair (`None` for single-gene predictions) |
| `is_single_gene` | `True` if no paired partner was found |
| `gene_type` | `Toxin` or `Antitoxin` |
| `hmm_name` / `hmm_score` / `hmm_evalue` | Best HMM hit across all database sources |
| `hmm_source` | Database the best hit comes from (`TADB3`, `TASmania`, or other) |
| `hmm_description` | Profile description |
| `hmm_pfam_name` | Closest Pfam family of the best HMM profile (see [Pfam annotation](#pfam-annotation-of-the-hmm-profiles)) |
| `hmm_pfam_families` | Names of all significant Pfam families of the best HMM profile, ordered by E-value (`NAME;NAME;…`) |
| `pair_is_known` | `1` if this toxin–antitoxin family combination is known in TADB3, `0` if not, `None` if family could not be identified |
| `score` | Unified match score in `(0, 1]` (see [Scoring](#scoring)) |

Scoring columns are `None` for single-gene predictions.

### `tatouscan_results_pairs.tsv` — per-pair results

One row per predicted toxin–antitoxin pair. For systems with more than one toxin or antitoxin, all valid combinations are written as separate rows.

| Column | Description |
|--------|-------------|
| `ta_system_id` | Shared system ID (matches the per-gene file) |
| `contig_name` | Contig where the pair is located |
| `toxin_gene_id` | Toxin gene identifier |
| `toxin_strand` | `+` or `-` |
| `toxin_product` | Predicted gene product |
| `toxin_length_aa` | Toxin protein length in amino acids |
| `toxin_hmm_name` / `_score` / `_evalue` / `_source` / `_description` | Best HMM hit for the toxin |
| `toxin_hmm_pfam_name` / `_families` | Pfam annotation of the toxin best HMM profile |
| `antitoxin_gene_id` | Antitoxin gene identifier |
| `antitoxin_strand` | `+` or `-` |
| `antitoxin_product` | Predicted gene product |
| `antitoxin_length_aa` | Antitoxin protein length in amino acids |
| `antitoxin_hmm_name` / `_score` / `_evalue` / `_source` / `_description` | Best HMM hit for the antitoxin |
| `antitoxin_hmm_pfam_name` / `_families` | Pfam annotation of the antitoxin best HMM profile |
| `intergenic_distance` | Distance in nucleotides between the two genes (negative = overlap) |
| `pair_is_known` | `1` / `0` / `None` (see above) |
| `score` | Unified match score in `(0, 1]` |

### Detailed output

With `--detailed`, all default columns are kept and the following additional columns are written to both files (prefixed with `toxin_` / `antitoxin_` in the pairs file):

- **Pfam details of the best HMM profile**: `hmm_pfam_accession`, `hmm_pfam_description`, and `hmm_pfam_hits` (all significant Pfam families with E-values and positions on the profile consensus, `NAME(ACCESSION):E-value:positions;…`)
- **Per-source HMM hits**: `TASmania_hmm_name/score/evalue/description/pfam_name`, `TADB3_hmm_name/score/evalue/description/pfam_name`, `Other_hmm_name/score/evalue/description/pfam_name`
- **Raw Z-scores**: `toxin_size_z`, `at_size_z`, `intergenic_distance_z`, `matched_family`, `n_reference_pairs`

The pairs file also adds `toxin_start/end` and `antitoxin_start/end` in detailed mode.

---

## Scoring

Every predicted TA **pair** is compared against reference statistics derived from known TADB3 type-II systems. The score measures how closely the predicted pair resembles a genuine TA system of its family.

### What is compared

Three structural features are measured for each predicted pair and compared against the reference distribution for the matched family:

| Feature | Definition |
|---------|------------|
| `toxin_size` | Toxin protein length (amino acids) |
| `at_size` | Antitoxin protein length (amino acids) |
| `intergenic_distance` | Distance in nucleotides between the two genes (negative = overlap) |

The **toxin family** is determined from its best TADB3 HMM hit. If no TADB3 hit exists or the family has fewer than 20 reference pairs, global statistics computed across all families are used as a fallback.

### Robust Z-scores

For each feature, a Z-score measures how far the predicted value deviates from the family reference:

$$z = \frac{x - \text{median}}{\text{MAD} / 0.6745}$$

Median and MAD (median absolute deviation) are used instead of mean and standard deviation because size distributions in TA families are often skewed. This makes the scores robust to outliers.

### Unified score

All Z-scores are combined into a single **score** in the range $(0, 1]$:

$$\text{score} = \exp\!\left(-\frac{1}{n}\sum_i |z_i|\right)$$

The mean is taken over all available terms: the three structural Z-scores plus a **compatibility term** ($z_{\text{compat}}$) based on whether this toxin–antitoxin family combination has been observed in TADB3:

- `pair_is_known = 1` → $z_{\text{compat}} = 0$ (no penalty)
- `pair_is_known = 0` → $z_{\text{compat}} = 2$ (unknown combination lowers the score)
- `pair_is_known = None` → compatibility term excluded from the mean

**Score interpretation:**

| Score | Meaning |
|-------|---------|
| ~1.0 | Features match the family reference almost exactly, known combination |
| ~0.7 | Moderate structural match, known combination |
| ~0.4 | Moderate structural match, but family combination not seen in TADB3 |
| < 0.2 | Large structural deviations or unknown combination — treat with caution |

A high score supports a genuine TA pair; a low score does not exclude it, but suggests the prediction should be reviewed.



## License

This project is licensed under the [MIT License](LICENSE).
