Datasets:
Formats:
parquet
Languages:
English
Size:
10M - 100M
Tags:
biology
chemistry
drug-discovery
clinical-trials
protein-protein-interaction
gene-essentiality
License:
File size: 11,862 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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | """Tests for STRING v12.0 negative PPI ETL."""
from pathlib import Path
import pytest
from negbiodb_ppi.etl_string import (
compute_protein_degrees,
extract_zero_score_pairs,
load_linked_pairs,
load_string_mapping,
run_string_etl,
)
from negbiodb_ppi.ppi_db import get_connection, run_ppi_migrations
MIGRATIONS_DIR = Path(__file__).resolve().parent.parent / "migrations_ppi"
@pytest.fixture
def ppi_db(tmp_path):
db_path = tmp_path / "test_ppi.db"
run_ppi_migrations(db_path, MIGRATIONS_DIR)
return db_path
# --- Mapping file fixtures ---
MAPPING_LINES = """\
9606\tP00001|PROT1_HUMAN\t9606.ENSP00000000001\t100.0\t500.0
9606\tP00002|PROT2_HUMAN\t9606.ENSP00000000002\t100.0\t480.0
9606\tP00003|PROT3_HUMAN\t9606.ENSP00000000003\t100.0\t460.0
9606\tP00004|PROT4_HUMAN\t9606.ENSP00000000004\t100.0\t440.0
9606\tP00005|PROT5_HUMAN\t9606.ENSP00000000005\t100.0\t420.0
9606\tP00006|PROT6_HUMAN\t9606.ENSP00000000006\t100.0\t400.0
"""
# 6 proteins, links forming a triangle (P1-P2, P1-P3, P2-P3) + P4-P5, P4-P6, P5-P6
# Each of P1-P6 has degree >= 2
LINKS_LINES = """\
protein1 protein2 combined_score
9606.ENSP00000000001 9606.ENSP00000000002 900
9606.ENSP00000000001 9606.ENSP00000000003 800
9606.ENSP00000000002 9606.ENSP00000000003 700
9606.ENSP00000000004 9606.ENSP00000000005 600
9606.ENSP00000000004 9606.ENSP00000000006 500
9606.ENSP00000000005 9606.ENSP00000000006 400
"""
@pytest.fixture
def mapping_file(tmp_path):
p = tmp_path / "human.uniprot_2_string.2018.tsv"
p.write_text(MAPPING_LINES)
return p
@pytest.fixture
def links_file(tmp_path):
p = tmp_path / "9606.protein.links.v12.0.txt"
p.write_text(LINKS_LINES)
return p
class TestLoadStringMapping:
def test_basic(self, mapping_file):
m = load_string_mapping(mapping_file)
assert m["9606.ENSP00000000001"] == "P00001"
assert m["9606.ENSP00000000006"] == "P00006"
assert len(m) == 6
def test_empty_file(self, tmp_path):
p = tmp_path / "empty.tsv"
p.write_text("")
assert load_string_mapping(p) == {}
def test_comment_lines(self, tmp_path):
p = tmp_path / "commented.tsv"
p.write_text("# header comment\n9606\tP00001|X\t9606.ENSP1\t100\t500\n")
m = load_string_mapping(p)
assert len(m) == 1
def test_invalid_uniprot_skipped(self, tmp_path):
p = tmp_path / "bad.tsv"
p.write_text("9606\tinvalid|X\t9606.ENSP1\t100\t500\n")
assert load_string_mapping(p) == {}
def test_short_line_skipped(self, tmp_path):
p = tmp_path / "short.tsv"
p.write_text("9606\tP00001|X\n") # Only 2 columns
assert load_string_mapping(p) == {}
def test_gzipped(self, tmp_path):
import gzip
p = tmp_path / "mapping.tsv.gz"
with gzip.open(p, "wt") as f:
f.write("9606\tP00001|X\t9606.ENSP1\t100\t500\n")
m = load_string_mapping(p)
assert m["9606.ENSP1"] == "P00001"
class TestComputeProteinDegrees:
def test_basic_ensp(self, links_file):
degrees = compute_protein_degrees(links_file)
assert degrees["9606.ENSP00000000001"] == 2
assert degrees["9606.ENSP00000000004"] == 2
def test_with_uniprot_mapping(self, links_file, mapping_file):
mapping = load_string_mapping(mapping_file)
degrees = compute_protein_degrees(links_file, mapping)
assert degrees["P00001"] == 2
assert degrees["P00002"] == 2
assert degrees["P00004"] == 2
assert len(degrees) == 6
def test_empty_file(self, tmp_path):
p = tmp_path / "empty.protein.links.txt"
p.write_text("protein1 protein2 combined_score\n")
assert compute_protein_degrees(p) == {}
def test_header_skipped(self, links_file):
degrees = compute_protein_degrees(links_file)
assert "protein1" not in degrees
class TestLoadLinkedPairs:
def test_basic(self, links_file, mapping_file):
mapping = load_string_mapping(mapping_file)
pairs = load_linked_pairs(links_file, mapping)
assert len(pairs) == 6
# All canonical ordered
for a, b in pairs:
assert a < b
def test_unmapped_skipped(self, tmp_path):
links = tmp_path / "links.txt"
links.write_text(
"protein1 protein2 combined_score\n"
"9606.ENSP1 9606.ENSP2 900\n"
)
# Empty mapping → no pairs
assert load_linked_pairs(links, {}) == set()
def test_self_pair_skipped(self, tmp_path):
links = tmp_path / "links.txt"
links.write_text(
"protein1 protein2 combined_score\n"
"9606.ENSP1 9606.ENSP1 900\n"
)
mapping = {"9606.ENSP1": "P00001"}
assert load_linked_pairs(links, mapping) == set()
class TestExtractZeroScorePairs:
def test_basic_subtraction(self):
linked = {("P00001", "P00002"), ("P00001", "P00003")}
degrees = {"P00001": 5, "P00002": 5, "P00003": 5}
result = extract_zero_score_pairs(linked, degrees, min_degree=5)
# 3 choose 2 = 3 pairs, minus 2 linked = 1 pair
assert result == [("P00002", "P00003")]
def test_degree_filter(self):
linked = set()
degrees = {"P00001": 10, "P00002": 10, "P00003": 1} # P3 low degree
result = extract_zero_score_pairs(linked, degrees, min_degree=5)
# Only P1 and P2 qualify → 1 pair
assert result == [("P00001", "P00002")]
def test_universe_filter(self):
linked = set()
degrees = {"P00001": 10, "P00002": 10, "P00003": 10}
universe = {"P00001", "P00002"} # P3 not in universe
result = extract_zero_score_pairs(
linked, degrees, min_degree=5, protein_universe=universe
)
assert result == [("P00001", "P00002")]
def test_max_pairs_cap(self):
linked = set()
# 5 proteins → 10 pairs
degrees = {f"P0000{i}": 10 for i in range(1, 6)}
result = extract_zero_score_pairs(
linked, degrees, min_degree=5, max_pairs=3
)
assert len(result) == 3
def test_max_pairs_deterministic(self):
linked = set()
degrees = {f"P0000{i}": 10 for i in range(1, 6)}
r1 = extract_zero_score_pairs(
linked, degrees, min_degree=5, max_pairs=3, random_seed=42
)
r2 = extract_zero_score_pairs(
linked, degrees, min_degree=5, max_pairs=3, random_seed=42
)
assert r1 == r2
def test_empty_candidates(self):
result = extract_zero_score_pairs(set(), {}, min_degree=5)
assert result == []
def test_invalid_uniprot_filtered(self):
linked = set()
degrees = {"P00001": 10, "invalid": 10, "P00002": 10}
result = extract_zero_score_pairs(linked, degrees, min_degree=5)
# Only P00001, P00002 pass validate_uniprot
assert result == [("P00001", "P00002")]
def test_results_sorted(self):
linked = set()
degrees = {f"P0000{i}": 10 for i in range(1, 5)}
result = extract_zero_score_pairs(linked, degrees, min_degree=5)
assert result == sorted(result)
class TestRunStringEtl:
@pytest.fixture
def string_data_dir(self, tmp_path):
data_dir = tmp_path / "string"
data_dir.mkdir()
(data_dir / "human.uniprot_2_string.2018.tsv").write_text(MAPPING_LINES)
(data_dir / "9606.protein.links.v12.0.txt").write_text(LINKS_LINES)
return data_dir
def test_basic_etl(self, ppi_db, string_data_dir):
stats = run_string_etl(
db_path=ppi_db,
data_dir=string_data_dir,
min_degree=2,
max_pairs=500_000,
)
assert stats["mapping_entries"] == 6
assert stats["linked_pairs"] == 6
# 6 choose 2 = 15, minus 6 linked = 9
assert stats["negative_pairs_derived"] == 9
assert stats["negative_pairs_inserted"] == 9
def test_all_bronze_tier(self, ppi_db, string_data_dir):
run_string_etl(db_path=ppi_db, data_dir=string_data_dir, min_degree=2)
conn = get_connection(ppi_db)
try:
tiers = conn.execute(
"SELECT DISTINCT confidence_tier FROM ppi_negative_results"
).fetchall()
assert tiers == [("bronze",)]
evidence = conn.execute(
"SELECT DISTINCT evidence_type FROM ppi_negative_results"
).fetchall()
assert evidence == [("low_score_negative",)]
finally:
conn.close()
def test_canonical_ordering(self, ppi_db, string_data_dir):
run_string_etl(db_path=ppi_db, data_dir=string_data_dir, min_degree=2)
conn = get_connection(ppi_db)
try:
rows = conn.execute(
"SELECT protein1_id, protein2_id FROM ppi_negative_results"
).fetchall()
for p1, p2 in rows:
assert p1 < p2
finally:
conn.close()
def test_experiment_record(self, ppi_db, string_data_dir):
run_string_etl(db_path=ppi_db, data_dir=string_data_dir, min_degree=2)
conn = get_connection(ppi_db)
try:
exp = conn.execute(
"SELECT source_db, source_experiment_id FROM ppi_experiments "
"WHERE source_db = 'string'"
).fetchone()
assert exp is not None
assert exp[1] == "string-v12.0-zero-score"
finally:
conn.close()
def test_dataset_version(self, ppi_db, string_data_dir):
run_string_etl(db_path=ppi_db, data_dir=string_data_dir, min_degree=2)
conn = get_connection(ppi_db)
try:
dv = conn.execute(
"SELECT name, version FROM dataset_versions WHERE name = 'string'"
).fetchone()
assert dv[0] == "string"
assert dv[1] == "v12.0"
finally:
conn.close()
def test_proteins_inserted(self, ppi_db, string_data_dir):
run_string_etl(db_path=ppi_db, data_dir=string_data_dir, min_degree=2)
conn = get_connection(ppi_db)
try:
count = conn.execute("SELECT COUNT(*) FROM proteins").fetchone()[0]
assert count == 6
finally:
conn.close()
def test_missing_mapping_file(self, ppi_db, tmp_path):
empty_dir = tmp_path / "empty_string"
empty_dir.mkdir()
with pytest.raises(FileNotFoundError, match="mapping"):
run_string_etl(db_path=ppi_db, data_dir=empty_dir)
def test_missing_links_file(self, ppi_db, tmp_path):
no_links = tmp_path / "no_links"
no_links.mkdir()
(no_links / "human.uniprot_2_string.2018.tsv").write_text(MAPPING_LINES)
with pytest.raises(FileNotFoundError, match="links"):
run_string_etl(db_path=ppi_db, data_dir=no_links)
def test_protein_universe_restricts(self, ppi_db, string_data_dir):
# Only allow P00001 and P00004 — they're not linked to each other
stats = run_string_etl(
db_path=ppi_db,
data_dir=string_data_dir,
min_degree=2,
protein_universe={"P00001", "P00004"},
)
# 2 choose 2 = 1 pair, minus 0 linked between them = 1
assert stats["negative_pairs_derived"] == 1
assert stats["negative_pairs_inserted"] == 1
def test_stats_keys(self, ppi_db, string_data_dir):
stats = run_string_etl(
db_path=ppi_db, data_dir=string_data_dir, min_degree=2
)
expected_keys = {
"mapping_entries",
"linked_pairs",
"proteins_with_degree",
"well_studied_proteins",
"negative_pairs_derived",
"negative_pairs_inserted",
}
assert set(stats.keys()) == expected_keys
|