Metadata-Version: 2.4
Name: peplocus
Version: 0.6.0
Summary: Fast peptide-to-genome mapping with splice-junction support and optional one-edit/adjacent-swap rescue
Author: Qussai Abbas
License-Expression: MIT
Project-URL: Homepage, https://github.com/qussai96/PepLocus
Project-URL: Source, https://github.com/qussai96/PepLocus
Project-URL: Issues, https://github.com/qussai96/PepLocus/issues
Keywords: proteogenomics,peptides,genome,splicing,BED12
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.23
Requires-Dist: biopython>=1.80
Requires-Dist: pyahocorasick>=2.0
Requires-Dist: pysam>=0.22
Dynamic: license-file

# PepLocus

Map mass-spectrometry peptides directly to eukaryotic genome assemblies, including splice-junction peptides. PepLocus searches all six reading frames, always treats I/L as equivalent, and can use GTF/GFF annotations or RNA-seq BAM evidence.

![PepLocus overview](https://raw.githubusercontent.com/qussai96/PepLocus/main/PepLocus.png)

## Install

```bash
pip install peplocus
```

## Run

Map a peptide list to an assembly:

```bash
peplocus -a assembly.fa -p peptides.tsv --out results
```

Add annotation and/or RNA-seq evidence to find splice-junction placements:

```bash
peplocus -a assembly.fa -p peptides.tsv -g models.gtf -b rnaseq.bam --out results_spliced
```

`-g` accepts GTF or GFF. Either `-g` or `-b` can be used alone. A BAM index is not required.

Optional one-edit rescue searches peptides with no exact match for one amino-acid substitution, insertion or deletion:

```bash
peplocus -a assembly.fa -p peptides.tsv -g models.gtf --one-edit --out results_rescue
```

The default is **8 workers** (`--threads`). Rescue uses a minimum query length of **9 aa**; short candidates can be highly ambiguous. Use `--min-rescue-length 12` for a more conservative search. `--discover-junctions` optionally searches for bounded canonical single-intron matches without supplied evidence.

Peptides can be a plain list, FASTA, CSV or TSV; FragPipe's `Peptide Sequence` column is recognised automatically. Assembly FASTA may be plain or gzip-compressed. Use a fresh output directory for each run.

```bash
peplocus -h
```

## Results

| File | Contents |
|---|---|
| `all_mappings.bed` | Exact and I/L-equivalent placements |
| `tryptic_mappings.bed` | Placements with compatible tryptic or possible protein-terminal contexts |
| `mappings.tsv` | Coordinates, sequences, evidence, cleavage contexts and mapping counts |
| `peptides.tsv` | Per-peptide exact and rescue counts and status |
| `unmapped.tsv` | Peptides with no exact match, including labeled rescue candidates |
| `summary.json` | Parameters, counts, timings and search limits |

BED files use standard BED12 coordinates, with peptide names in biological N-to-C orientation on both strands. Rescue candidates are kept separately in `rescue/`, with their own all/tryptic BED files and detailed TSV. A reusable `junctions.tsv` is also saved when splice evidence is supplied.

Contiguous matching is exhaustive for the chosen translation table. Splice mapping depends on available junctions and search limits. Placements show sequence compatibility; one-edit candidates are not confirmed variants, and tryptic compatibility alone does not establish biological origin.

[Report an issue](https://github.com/qussai96/PepLocus/issues) · [MIT license](https://github.com/qussai96/PepLocus/blob/main/LICENSE)

## Optional adjacent-swap rescue (0.6.0)

`--swap` searches for **one exchange of two neighbouring amino acids**, for
example `PEPTDIE` against a translated `PEPTIDE`. It is opt-in. The default
minimum query length is **9 aa**, shared with one-edit rescue.

```bash
# Adjacent swaps only
peplocus -a assembly.fa -p peptides.tsv -g models.gtf --swap --out swap_results

# Substitution/insertion/deletion rescue only
peplocus -a assembly.fa -p peptides.tsv -g models.gtf --one-edit --out edit_results

# Both methods, sharing target translation and splice traversal
peplocus -a assembly.fa -p peptides.tsv -g models.gtf --one-edit --swap --out combined_results

# More conservative length cutoff for either enabled rescue method
peplocus -a assembly.fa -p peptides.tsv --swap --min-rescue-length 12 --out longer_results
```

`--rescue-one-edit` remains an alias for `--one-edit`. Neither flag is enabled
by default. The Python API accepts `swap=True` and `one_edit=True`;
`rescue_one_edit=True` remains supported.

### Eligibility and search scope

All exact stages finish first. A query with **any exact/I/L-equivalent
placement**, even a nontryptic one, is excluded from both rescue methods.
The query must also pass `--min-rescue-length` (default 9). Swapping identical
residues, including I/L-equivalent pairs, is an exact match, not a swap event.

With both flags enabled, each eligible query is searched by both methods. A
one-edit candidate does not suppress a swap candidate elsewhere. The results
are the union of their distinct genomic placements. Each alignment contains
**one substitution, one insertion, one deletion, or one adjacent swap**; a
swap plus another edit is not allowed. Existing exact results do not change.

Rescue covers the six contiguous reading frames and the bounded splice paths
supplied by GTF/GFF, BAM or reusable junction tables. It can span an exon
junction or a split codon on either strand. It does not discover a new splice
junction through an approximate match. Existing splice-path caps still apply.
There are no additional swap seed, candidate or location caps, and all
qualifying placements are reported rather than selecting a preferred hit.

### Mixed search algorithm

PepLocus builds one reusable swap index for the eligible query set:

- **Below 12 aa:** generate every distinct full-length adjacent-swap variant
  and search them together with the C-backed Aho–Corasick matcher. A query of
  length L contributes at most L−1 nontrivial variants.
- **At least 12 aa:** use two exact anchors separated by one unused residue.
  For a query of length L, let m = floor((L−1)/2); the anchors are `query[:m]`
  and `query[m+1:]`. An adjacent pair cannot overlap both anchors across that
  gap, so at least one anchor survives every allowed swap. Fixed anchor
  offsets identify candidate starts, and the complete query is verified.

The 12-aa switch is an internal implementation choice, **not the minimum
rescue length**. It avoids short-anchor candidate explosions while reducing
index memory for longer queries. The search verifies exactly two adjacent
differences with exchanged residues and rejects exact matches, ambiguous
translated residues, nonadjacent changes and additional edits. Matches never
cross a stop or unresolved codon. Both strategies are complete for a single
adjacent swap within the searched target sequences; chunk ownership and
location deduplication prevent repeated records at overlapping chunk edges.

When `--one-edit` and `--swap` are combined, one rescue stage shares translated
targets, eligible queries, worker processes, splice paths and the output store.
The two operation indexes are scanned separately within that traversal. The
existing one-edit anchor search is unchanged when swap rescue is disabled.

### Outputs and alignment convention

No new top-level output family is added. All approximate candidates stay in
`rescue/all_mappings.bed`, `rescue/tryptic_mappings.bed` and
`rescue/mappings.tsv`. Root `peptides.tsv` and `unmapped.tsv` retain the exact
status and summarize the combined rescue multiplicity. Ambiguity is counted
across **all enabled rescue methods**, separately for all and tryptic tracks.
The default coordinate-verification step re-extracts and translates every
reported target and validates its operation against the query.

A swap row has:

- `match_type` and `edit_type`: `adjacent_transposition`;
- `edit_distance`: **2**, using ordinary Levenshtein distance after I/L normalization; this is one
  transposition under the Damerau convention;
- `edit_query_position`: the **1-based position of the first swapped residue**;
- `query_aa` and `reference_aa`: the two residues in query and target order;
- `alignment_cigar`: two substitutions (`2X`), with I/L-equivalent exact flanks as applicable;
- `edit_position_min` and `edit_position_max`: that same first position.

One-edit rows retain `match_type=one_edit`, distance 1 and the existing
substitution/insertion/deletion fields. Coordinates always cover the
translated reference bases. In `summary.json`, runs using swaps have an
`approximate_rescue` stage with enabled operations, counters, timings and
separate all/tryptic counts; one-edit-only runs retain the `one_edit_rescue`
stage for compatibility.

### Digestion and interpretation

Query and reference cleavage contexts are classified separately. A swap near
a peptide end can create or remove a K/R cleavage site or change the proline
exception. The tryptic rescue subset uses the **query in the actual reference
flanks**; reference compatibility and cleavage changes remain explicit in the
mapping table. A reference-side change does not silently discard a candidate.
Internal missed cleavages are not capped. Methionine-based contexts indicate
plausibility, not a proven translation start.

A swap match establishes sequence compatibility, not a confirmed sequencing
error, DNA variant or gene-model correction. Short and low-complexity queries
can match by chance. The 9-aa default favors sensitivity; a 12-aa cutoff is
more conservative. Keep rescue candidates separate when estimating exact
coverage, identification error or independent support for annotations.

### Temporary storage

Private exact/rescue SQLite databases use `SLURM_TMPDIR` when set, otherwise
Python's temporary directory (`TMPDIR` or the system default). This keeps
random database I/O off a shared output filesystem. On a cluster, point these
variables to node-local scratch with sufficient space for the match store.
Verified outputs are still published to `--out`, and temporary databases are
removed when the run closes. No database is written beside the source inputs.
