Datasets:
Formats:
parquet
Languages:
English
Size:
10M - 100M
Tags:
biology
chemistry
drug-discovery
clinical-trials
protein-protein-interaction
gene-essentiality
License:
File size: 7,275 Bytes
6d1bbc7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | """Tests for scripts_ppi/fetch_sequences.py — UniProt sequence fetching."""
import json
import sqlite3
import sys
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts_ppi"))
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "src"))
from fetch_sequences import fetch_uniprot_batch, update_protein_sequences
@pytest.fixture
def ppi_db(tmp_path):
"""Create a minimal PPI database with 3 proteins (no sequences)."""
db_path = tmp_path / "test_ppi.db"
conn = sqlite3.connect(str(db_path))
conn.execute(
"""CREATE TABLE proteins (
protein_id INTEGER PRIMARY KEY AUTOINCREMENT,
uniprot_accession TEXT NOT NULL UNIQUE,
uniprot_entry_name TEXT,
gene_symbol TEXT,
amino_acid_sequence TEXT,
sequence_length INTEGER,
organism TEXT DEFAULT 'Homo sapiens',
taxonomy_id INTEGER DEFAULT 9606,
subcellular_location TEXT,
created_at TEXT DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
updated_at TEXT DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))
)"""
)
conn.executemany(
"INSERT INTO proteins (uniprot_accession) VALUES (?)",
[("P12345",), ("Q9UHC1",), ("P99999",)],
)
conn.commit()
conn.close()
return db_path
def _mock_uniprot_response():
"""Build a mock UniProt JSON response."""
return {
"results": [
{
"primaryAccession": "P12345",
"sequence": {"value": "MKTAYIAKQRQISFVKSHFSRQ"},
"genes": [{"geneName": {"value": "BRCA1"}}],
"comments": [
{
"commentType": "SUBCELLULAR LOCATION",
"subcellularLocations": [
{"location": {"value": "Nucleus"}}
],
}
],
},
{
"primaryAccession": "Q9UHC1",
"sequence": {"value": "MAAPWRRGARL"},
"genes": [{"geneName": {"value": "MLH1"}}],
"comments": [],
},
# P99999 deliberately missing — simulates 404/obsolete
]
}
class TestFetchUniprotBatch:
@patch("fetch_sequences.requests.get")
def test_basic_fetch(self, mock_get):
mock_resp = MagicMock()
mock_resp.status_code = 200
mock_resp.json.return_value = _mock_uniprot_response()
mock_resp.raise_for_status = MagicMock()
mock_get.return_value = mock_resp
result = fetch_uniprot_batch(["P12345", "Q9UHC1", "P99999"])
assert "P12345" in result
assert result["P12345"]["sequence"] == "MKTAYIAKQRQISFVKSHFSRQ"
assert result["P12345"]["gene_symbol"] == "BRCA1"
assert result["P12345"]["subcellular_location"] == "Nucleus"
assert "Q9UHC1" in result
assert result["Q9UHC1"]["gene_symbol"] == "MLH1"
assert result["Q9UHC1"]["subcellular_location"] is None
# P99999 not in response → not in result
assert "P99999" not in result
def test_empty_list(self):
result = fetch_uniprot_batch([])
assert result == {}
@patch("fetch_sequences.requests.get")
def test_retry_on_failure(self, mock_get):
import requests as req
mock_fail = MagicMock()
mock_fail.raise_for_status.side_effect = req.HTTPError("503")
mock_ok = MagicMock()
mock_ok.json.return_value = {"results": []}
mock_ok.raise_for_status = MagicMock()
mock_get.side_effect = [mock_fail, mock_ok]
with patch("fetch_sequences.RETRY_BACKOFF", 0.01):
result = fetch_uniprot_batch(["P12345"])
assert result == {}
assert mock_get.call_count == 2
class TestUpdateProteinSequences:
@patch("fetch_sequences.fetch_uniprot_batch")
def test_update_sequences(self, mock_fetch, ppi_db):
mock_fetch.return_value = {
"P12345": {
"sequence": "MKTAYIAKQRQISFVKSHFSRQ",
"gene_symbol": "BRCA1",
"subcellular_location": "Nucleus",
},
"Q9UHC1": {
"sequence": "MAAPWRRGARL",
"gene_symbol": "MLH1",
"subcellular_location": None,
},
# P99999 missing → fails
}
summary = update_protein_sequences(ppi_db, batch_size=500, delay=0)
assert summary["total"] == 3
assert summary["fetched"] == 2
assert summary["failed"] == 1
conn = sqlite3.connect(str(ppi_db))
rows = conn.execute(
"SELECT uniprot_accession, amino_acid_sequence, sequence_length, gene_symbol "
"FROM proteins ORDER BY uniprot_accession"
).fetchall()
conn.close()
# P12345
assert rows[0][1] == "MKTAYIAKQRQISFVKSHFSRQ"
assert rows[0][2] == 22
assert rows[0][3] == "BRCA1"
# P99999 still NULL
assert rows[1][1] is None
# Q9UHC1
assert rows[2][1] == "MAAPWRRGARL"
assert rows[2][2] == 11
@patch("fetch_sequences.fetch_uniprot_batch")
def test_checkpoint_resume(self, mock_fetch, ppi_db, tmp_path):
checkpoint = tmp_path / "checkpoint.json"
# First run: fetch P12345 only (batch_size=1, limit 1 batch)
mock_fetch.return_value = {
"P12345": {
"sequence": "MKTA",
"gene_symbol": None,
"subcellular_location": None,
},
}
update_protein_sequences(
ppi_db, batch_size=1, delay=0, checkpoint_path=checkpoint
)
assert checkpoint.exists()
with open(checkpoint) as f:
ckpt = json.load(f)
# At least P12345 should be in completed
assert "P12345" in ckpt["completed"]
@patch("fetch_sequences.fetch_uniprot_batch")
def test_preserves_existing_gene_symbol(self, mock_fetch, ppi_db):
"""COALESCE should not overwrite existing gene_symbol."""
conn = sqlite3.connect(str(ppi_db))
conn.execute(
"UPDATE proteins SET gene_symbol = 'EXISTING' WHERE uniprot_accession = 'P12345'"
)
conn.commit()
conn.close()
mock_fetch.return_value = {
"P12345": {
"sequence": "MKTA",
"gene_symbol": "NEW_GENE",
"subcellular_location": None,
},
"Q9UHC1": {
"sequence": "MAAP",
"gene_symbol": "MLH1",
"subcellular_location": None,
},
"P99999": {
"sequence": "ABCD",
"gene_symbol": None,
"subcellular_location": None,
},
}
update_protein_sequences(ppi_db, batch_size=500, delay=0)
conn = sqlite3.connect(str(ppi_db))
row = conn.execute(
"SELECT gene_symbol FROM proteins WHERE uniprot_accession = 'P12345'"
).fetchone()
conn.close()
# COALESCE keeps existing value
assert row[0] == "EXISTING"
|