Metadata-Version: 2.5
Name: pySIFTS
Version: 0.1.1
Summary: SIFTS PDB-to-UniProt mapping, entity dissection, and an interactive sequence view
Project-URL: Homepage, https://github.com/MaybeBio/pySIFTS
Author-email: Joe Hoye Dow <luxunisgod123@gmail.com>
License: GPL-3.0-or-later
License-File: LICENSE
Requires-Python: >=3.9
Requires-Dist: requests>=2.31
Requires-Dist: typer>=0.12
Description-Content-Type: text/markdown

<div align="center">

# 🧬 pySIFTS

**SIFTS PDB ↔ UniProt mapping, entity dissection, and an interactive**
**sequence-coverage viewer — for any RCSB entry or local structure file.**

<p>
From a PDB ID — or a local <code>.cif</code> / <code>.pdb</code> — to a residue-level
SIFTS mapping, per-chain coverage statistics, and a genome-browser-style viewer:
<code>entity</code> → <code>sifts</code> → <code>view</code> → <code>all</code>,
with every network artifact cached and every output deterministic.
</p>

<img src="./figs/viewer.svg" alt="The pySIFTS viewer: chain tabs, per-chain tracks with observed, unobserved and unmapped residues, and an all-chains overview strip" width="82%" />

[![License: GPL-3.0](https://img.shields.io/badge/License-GPL--3.0-blue.svg)](LICENSE)
[![Python](https://img.shields.io/badge/Python-3.9%2B-3776AB.svg?logo=python&logoColor=white)](#installation)
[![PyPI version](https://img.shields.io/pypi/v/pySIFTS.svg)](https://pypi.org/project/pySIFTS/)
[![PyPI downloads](https://img.shields.io/pypi/dm/pySIFTS.svg)](https://pypi.org/project/pySIFTS/)
[![Commands](https://img.shields.io/badge/Commands-4-0366d6.svg)](#commands)
[![GitHub stars](https://img.shields.io/github/stars/MaybeBio/pySIFTS.svg?style=social)](https://github.com/MaybeBio/pySIFTS)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com)

<p>
  <a href="#installation">Installation</a> ·
  <a href="#usage">Usage</a> ·
  <a href="#-features">Features</a> ·
  <a href="#commands">Commands</a> ·
  <a href="#-artifacts">Artifacts</a> ·
  <a href="#-the-viewer">Viewer</a>
</p>

</div>

> **If this project helps you, please consider giving it a Star ⭐. Thank you!**

---

## 🚀 Features

- **Entity dissection** (`entity`) — polymer entities, chains, `kind` (protein / nucleic acid / other), UniProt accessions, and chimeric-chain detection, from the RCSB REST API or a local file.
- **Residue-level SIFTS mapping** (`sifts`) — the authoritative 7-column PDB↔UniProt table, written as TSV alongside per-chain coverage statistics.
- **Full-length coordinates** — UniProt FASTA anchors each track to the *complete* sequence, so residues the structure never resolved are visible as `unobserved`, not silently dropped. SIFTS alone only lists structurally touched positions.
- **Interactive 2D viewer** (`view`) — per-chain track tabs over an all-chains overview strip; hover, wheel zoom, drag pan, click-to-pin, and keyboard navigation. Serve it locally or export it as one self-contained HTML file.
- **One-shot pipeline** (`all`) — all four artifacts for one entry in a single run; no server started.
- **Offline-friendly** — RCSB JSON, SIFTS XML/TSV, and UniProt FASTA are cached; `--force` re-downloads on demand.
- **Framework-free front end** — two `<canvas>` elements plus plain DOM, no build step, no bundled runtime.

---

# Prerequisites

> Nothing to install beyond Python itself — all data comes from public REST endpoints.

| Source | Used for | Endpoint |
|---|---|---|
| RCSB PDB | entry metadata, polymer entities, chains, accessions | `https://data.rcsb.org/rest/v1/core/…` |
| EBI SIFTS | residue-level PDB ↔ UniProt mapping | `https://www.ebi.ac.uk/pdbe/files/sifts/<pdb>.xml.gz` |
| UniProt | full-length FASTA sequences | `https://rest.uniprot.org/uniprotkb/<acc>.fasta` |

A network connection is required on a cold cache only. Downloads are retried and
written atomically; cached results are reused on every later run.

# Installation

```bash
pip install pySIFTS
```

Requires **Python 3.9+**. The static viewer assets (`index.html`, `app.js`,
`style.css`) ship inside the wheel — there is no separate download or build step.

<details>
<summary>From source</summary>

```bash
git clone https://github.com/MaybeBio/pySIFTS.git
cd pySIFTS
pip install -e .
```

</details>

# Usage

```bash
❯ pySIFTS --help

 Usage: pySIFTS [OPTIONS] COMMAND [ARGS]...

 SIFTS PDB-to-UniProt mapping and sequence coverage viewer

╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --version          Show the version and exit.                                │
│ --help             Show this message and exit.                               │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────╮
│ entity  Dissect the polymer entities and chains of a PDB entry.              │
│ sifts   Write the SIFTS mapping and per-chain coverage statistics.           │
│ view    Serve the interactive sequence view, or export it as one HTML file.  │
│ all     Write all four artifacts for one entry (no server is started).       │
╰──────────────────────────────────────────────────────────────────────────────╯
```

## Quick start

```bash
# Four artifacts → ./pySIFTS_out/4ins/
pySIFTS all 4ins --out ./pySIFTS_out

# Explore the same entry in the browser
pySIFTS view 4ins

# Or ship a single HTML file — no server needed
pySIFTS view 4ins --html 4ins.html
```

```mermaid
flowchart LR
    Q["PDB ID<br/>or local .cif / .pdb"] --> E["RCSB entry +<br/>polymer entities"]
    Q --> S["SIFTS XML<br/>→ TSV rows"]
    E --> J["entity.json"]
    S --> T["tsv"]
    S --> C["chains.csv<br/>per-chain stats"]
    U["UniProt FASTA"] --> V["view.json"]
    S --> V
    V --> W["local server<br/>or single HTML"]
```

---

## Commands

### 1️⃣ `entity` — dissect the structure

```bash
❯ pySIFTS entity 7w1m

┏━━━━━━┳━━━━━━━━┳━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ PDB  ┃ Entity ┃ Asym ┃ Auth Asym ┃ UniProt ┃ Kind         ┃ Chimeric ┃
┡━━━━━━╇━━━━━━━━╇━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━┩
│ 7W1M │ 1      │ A    │ A         │ Q14683  │ protein      │ no       │
│ 7W1M │ 2      │ B    │ B         │ Q9UQE7  │ protein      │ no       │
│ 7W1M │ 3      │ C    │ C         │ O60216  │ protein      │ no       │
│ 7W1M │ 4      │ D    │ D         │ Q8WVM7  │ protein      │ no       │
│ 7W1M │ 5      │ E    │ E         │ Q6KC79  │ protein      │ no       │
│ 7W1M │ 6      │ F    │ F         │         │ nucleic_acid │ no       │
│ 7W1M │ 7      │ G    │ G         │         │ nucleic_acid │ no       │
│ 7W1M │ 8      │ H    │ H         │ P49711  │ protein      │ no       │
└──────┴────────┴──────┴───────────┴─────────┴──────────────┴──────────┘
```

- `--json` prints the full `entity.json` payload instead of the table.
- `--out DIR` also writes `<DIR>/<pdb>/<pdb>.entity.json`.
- One row per **(chain, accession)** pair: a chimeric chain yields one row per
  accession, never a zipped approximation.

### 2️⃣ `sifts` — mapping + coverage statistics

```bash
❯ pySIFTS sifts 4ins --out ./pySIFTS_out

┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
┃ Chain ┃ UniProt ┃ Mapped ┃ Observed ┃ Missing ┃ Covered ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
│ A     │ P01315  │ 21     │ 21       │ 0       │ 88-108  │
│ B     │ P01315  │ 30     │ 30       │ 0       │ 25-54   │
│ C     │ P01315  │ 21     │ 21       │ 0       │ 88-108  │
│ D     │ P01315  │ 30     │ 30       │ 0       │ 25-54   │
└───────┴─────────┴────────┴──────────┴─────────┴─────────┘
```

Writes two files alongside that table:

```text
4ins/
├── 4ins.tsv          # 7-column residue-level mapping, no header
└── 4ins.chains.csv   # one row per (chain, UniProt) pair
```

The TSV columns, in order:

| # | Column | Meaning |
|---|---|---|
| 0 | `PDB_ID` | entry ID, lowercase |
| 1 | `PDB_chain` | author chain ID (`auth_asym_id`) |
| 2 | `PDB_res` | three-letter residue name |
| 3 | `PDB_pos` | PDB residue number, or `null` when unobserved |
| 4 | `Uniprot_ID` | UniProt accession, lowercase |
| 5 | `Uniprot_res` | one-letter amino acid |
| 6 | `Uniprot_pos` | position on the full UniProt sequence |

`chains.csv` carries `pdb_id, auth_asym_id, asym_id, entity_id, uniprot_id,
kind, chimeric, mapped, observed, missing, covered_uni` — where `covered_uni`
lists the observed UniProt ranges as `a-b;c-d`.

> [!NOTE]
> A SIFTS row with `PDB_pos = null` still belongs to a real UniProt position.
> pySIFTS keeps it as **unobserved** rather than discarding it, which is why the
> coverage statistics count it in `mapped` and `missing` but not in `observed`.
> For example, 7w1m chain H reports 322 observed and 405 unobserved positions.

### 3️⃣ `view` — the interactive sequence view

```bash
# Serve on a random localhost port and open a browser
pySIFTS view 4ins

# Fixed port, no browser (e.g. on a remote host)
pySIFTS view 4ins --port 8765 --no-browser

# One self-contained file — assets inlined, no server
pySIFTS view 4ins --html 4ins.html
```

See [The viewer](#-the-viewer) for the interaction model.

### 4️⃣ `all` — everything at once

```bash
pySIFTS all 4ins --out ./pySIFTS_out --force
```

Runs `entity` → `sifts` → `view` payload in one pass and writes all four
artifacts. It never starts a server; use `view --html` when you also want the
standalone page.

---

# 📦 Artifacts

| File | Contents |
|---|---|
| `<pdb>.tsv` | 7-column residue-level SIFTS mapping, no header |
| `<pdb>.entity.json` | RCSB entry and polymer-entity payloads, chains, accessions |
| `<pdb>.chains.csv` | One row per (chain, UniProt) pair with mapped/observed/missing/covered statistics |
| `<pdb>.view.json` | Viewer payload: per-chain tracks with a dense per-residue state array |

Output defaults to `./pySIFTS_out`; files go to
`<out>/<pdb_lower>/<pdb_lower>.<ext>`. All writes are atomic (temp file +
`os.replace`), so an interrupted run never leaves a half-written artifact.

## Residue states

Each protein track is dense over the **full UniProt length** (when the UniProt
FASTA is available; otherwise it falls back to the SIFTS span and warns). Every
position carries one of three states:

| State | Color | Meaning |
|---|---|---|
| `0` | 🟦 `#2f6feb` | **observed** — in SIFTS and has PDB coordinates |
| `1` | ⬜ `#d7dbe3` | **unobserved** — in SIFTS but no coordinates |
| `2` | ◻️ `#f7f8fa` | **unmapped** — absent from SIFTS entirely |

Non-protein tracks (DNA/RNA/other) render from the entity's own one-letter
sequence and carry no per-residue state.

---

# 🖥️ The viewer

A genome-browser-style layout: **chain tabs** across the top, the active chain's
tracks below, and a pinned **all-chains overview strip** at the bottom. A
chimeric chain gets one track per UniProt accession, stacked; the overview strip
always summarizes the whole chain.

| Interaction | Effect |
|---|---|
| Click a chain tab or strip row | Activate that chain |
| Drag a track | Pan the visible window |
| Wheel over a track | Zoom around the cursor (overview keeps the window in frame) |
| Hover a residue | Tooltip with PDB and UniProt position |
| Click a residue | Pin the selection; click again or press `Esc` to clear |
| `←` / `→` | Pan the active track by 25% of the window |
| `+` / `-` | Zoom in / out by 2× |
| `Home` | Reset the active track to the full sequence |

## 3D seam

The 2D viewer is the only implemented view, but it emits two DOM events on
`document` so a future 3D panel can follow the selection without touching the
2D code:

```js
new CustomEvent('pysifts:select', { detail: { authChain, authResnum, uniPos } })
new CustomEvent('pysifts:window', { detail: { authChain, uniStart, uniEnd } })
```

`authResnum` and `uniPos` may be `null`. In serve mode the structure bytes are
available at `GET /api/structure` when the query was a local file; otherwise
that route returns 404. HTML exports set `structure` to `null` and show a
`static export · 3D disabled` banner.

---

# ⚙️ Caching, options, and exit codes

Commands share these options (`--force` where listed):

| Option | Meaning |
|---|---|
| `--cache-dir DIR` | Cache location (default `~/.cache/pysifts`) |
| `--quiet`, `-q` | Suppress warnings |
| `--force` | Bypass cached SIFTS XML/TSV and UniProt FASTA (`sifts`, `view`, `all`) |

RCSB entity JSON is cheap to reuse and is not affected by `--force`.

Exit codes: `0` success · `1` domain error (e.g. SIFTS mapping unavailable) ·
`2` usage error.

## Development

```bash
pip install -e . pytest
pytest                 # offline suite
pytest -m network      # hits RCSB / EBI / UniProt (cold cache, ~45 s)
```

---

# License

GPL-3.0-or-later. See [LICENSE](LICENSE).
