Datasets:
Formats:
parquet
Languages:
English
Size:
10M - 100M
Tags:
biology
chemistry
drug-discovery
clinical-trials
protein-protein-interaction
gene-essentiality
License:
File size: 21,590 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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 | """Tests for NegBioDB Clinical Trial domain database layer."""
import sqlite3
import tempfile
from pathlib import Path
import pytest
from negbiodb_ct.ct_db import (
create_ct_database,
get_connection,
refresh_all_ct_pairs,
run_ct_migrations,
)
MIGRATIONS_DIR = Path(__file__).resolve().parent.parent / "migrations_ct"
@pytest.fixture
def ct_db(tmp_path):
"""Create a temporary CT database with all migrations applied."""
db_path = tmp_path / "test_ct.db"
run_ct_migrations(db_path, MIGRATIONS_DIR)
return db_path
class TestCTMigrations:
"""Test CT schema creation and migrations."""
def test_create_ct_database(self, tmp_path):
db_path = tmp_path / "test.db"
result = create_ct_database(db_path, MIGRATIONS_DIR)
assert result == db_path
assert db_path.exists()
def test_migration_001_applied(self, ct_db):
conn = get_connection(ct_db)
try:
versions = conn.execute(
"SELECT version FROM schema_migrations"
).fetchall()
assert ("001",) in versions
finally:
conn.close()
def test_migration_002_applied(self, ct_db):
conn = get_connection(ct_db)
try:
versions = conn.execute(
"SELECT version FROM schema_migrations"
).fetchall()
assert ("002",) in versions
finally:
conn.close()
def test_idempotent_migrations(self, ct_db):
"""Running migrations twice should not fail."""
applied = run_ct_migrations(ct_db, MIGRATIONS_DIR)
assert applied == [] # Nothing new to apply
def test_all_tables_exist(self, ct_db):
conn = get_connection(ct_db)
try:
tables = {
row[0]
for row in conn.execute(
"SELECT name FROM sqlite_master WHERE type='table'"
).fetchall()
}
expected = {
"schema_migrations",
"dataset_versions",
"interventions",
"conditions",
"intervention_targets",
"clinical_trials",
"trial_interventions",
"trial_conditions",
"trial_publications",
"combination_components",
"trial_failure_results",
"intervention_condition_pairs",
"trial_failure_context",
}
assert expected.issubset(tables), f"Missing: {expected - tables}"
finally:
conn.close()
class TestCTSchema:
"""Test CT schema constraints and foreign keys."""
def test_interventions_type_check(self, ct_db):
conn = get_connection(ct_db)
try:
conn.execute(
"INSERT INTO interventions (intervention_type, intervention_name) "
"VALUES ('drug', 'Imatinib')"
)
conn.commit()
with pytest.raises(sqlite3.IntegrityError):
conn.execute(
"INSERT INTO interventions (intervention_type, intervention_name) "
"VALUES ('invalid_type', 'Bad')"
)
finally:
conn.close()
def test_clinical_trial_unique_constraint(self, ct_db):
conn = get_connection(ct_db)
try:
conn.execute(
"INSERT INTO clinical_trials "
"(source_db, source_trial_id, overall_status) "
"VALUES ('clinicaltrials_gov', 'NCT00000001', 'Terminated')"
)
conn.commit()
with pytest.raises(sqlite3.IntegrityError):
conn.execute(
"INSERT INTO clinical_trials "
"(source_db, source_trial_id, overall_status) "
"VALUES ('clinicaltrials_gov', 'NCT00000001', 'Completed')"
)
finally:
conn.close()
def test_same_trial_id_different_source(self, ct_db):
"""Same trial ID from different registries should work."""
conn = get_connection(ct_db)
try:
conn.execute(
"INSERT INTO clinical_trials "
"(source_db, source_trial_id, overall_status) "
"VALUES ('clinicaltrials_gov', 'NCT00000001', 'Terminated')"
)
conn.execute(
"INSERT INTO clinical_trials "
"(source_db, source_trial_id, overall_status) "
"VALUES ('eu_ctr', 'NCT00000001', 'Terminated')"
)
conn.commit()
count = conn.execute(
"SELECT COUNT(*) FROM clinical_trials"
).fetchone()[0]
assert count == 2
finally:
conn.close()
def test_failure_category_check(self, ct_db):
conn = get_connection(ct_db)
try:
# Insert required FK rows
conn.execute(
"INSERT INTO interventions (intervention_type, intervention_name) "
"VALUES ('drug', 'TestDrug')"
)
conn.execute(
"INSERT INTO conditions (condition_name) VALUES ('TestDisease')"
)
conn.commit()
# Valid category
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, failure_category, confidence_tier, "
" source_db, source_record_id, extraction_method) "
"VALUES (1, 1, 'efficacy', 'gold', 'aact', 'NCT001', 'database_direct')"
)
conn.commit()
# Invalid category
with pytest.raises(sqlite3.IntegrityError):
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, failure_category, confidence_tier, "
" source_db, source_record_id, extraction_method) "
"VALUES (1, 1, 'invalid', 'gold', 'aact', 'NCT002', 'database_direct')"
)
finally:
conn.close()
def test_confidence_tier_check(self, ct_db):
conn = get_connection(ct_db)
try:
conn.execute(
"INSERT INTO interventions (intervention_type, intervention_name) "
"VALUES ('drug', 'TestDrug')"
)
conn.execute(
"INSERT INTO conditions (condition_name) VALUES ('TestDisease')"
)
conn.commit()
with pytest.raises(sqlite3.IntegrityError):
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, failure_category, confidence_tier, "
" source_db, source_record_id, extraction_method) "
"VALUES (1, 1, 'efficacy', 'platinum', 'aact', 'NCT001', 'database_direct')"
)
finally:
conn.close()
def test_foreign_key_enforcement(self, ct_db):
"""FK violations should raise errors."""
conn = get_connection(ct_db)
try:
with pytest.raises(sqlite3.IntegrityError):
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, failure_category, confidence_tier, "
" source_db, source_record_id, extraction_method) "
"VALUES (999, 999, 'efficacy', 'gold', 'aact', 'NCT001', 'database_direct')"
)
finally:
conn.close()
class TestRefreshCTPairs:
"""Test intervention_condition_pairs aggregation."""
def _insert_test_data(self, conn):
"""Insert minimal test data for aggregation testing."""
conn.execute(
"INSERT INTO interventions (intervention_type, intervention_name) "
"VALUES ('drug', 'DrugA')"
)
conn.execute(
"INSERT INTO interventions (intervention_type, intervention_name) "
"VALUES ('drug', 'DrugB')"
)
conn.execute(
"INSERT INTO conditions (condition_name) VALUES ('DiseaseX')"
)
conn.execute(
"INSERT INTO conditions (condition_name) VALUES ('DiseaseY')"
)
conn.execute(
"INSERT INTO clinical_trials "
"(source_db, source_trial_id, overall_status, trial_phase) "
"VALUES ('clinicaltrials_gov', 'NCT001', 'Terminated', 'phase_3')"
)
conn.execute(
"INSERT INTO clinical_trials "
"(source_db, source_trial_id, overall_status, trial_phase) "
"VALUES ('clinicaltrials_gov', 'NCT002', 'Completed', 'phase_2')"
)
# DrugA + DiseaseX: 2 trials (gold + silver)
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, trial_id, failure_category, "
" confidence_tier, source_db, source_record_id, extraction_method, "
" publication_year, highest_phase_reached) "
"VALUES (1, 1, 1, 'efficacy', 'gold', 'aact', 'R001', 'database_direct', 2020, 'phase_3')"
)
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, trial_id, failure_category, "
" confidence_tier, source_db, source_record_id, extraction_method, "
" publication_year, highest_phase_reached) "
"VALUES (1, 1, 2, 'efficacy', 'silver', 'aact', 'R002', 'database_direct', 2022, 'phase_2')"
)
# DrugA + DiseaseY: 1 trial (bronze)
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, failure_category, "
" confidence_tier, source_db, source_record_id, extraction_method, "
" publication_year, highest_phase_reached) "
"VALUES (1, 2, 'safety', 'bronze', 'aact', 'R003', 'nlp_classified', 2021, 'phase_2')"
)
# DrugB + DiseaseX: 1 trial (copper)
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, failure_category, "
" confidence_tier, source_db, source_record_id, extraction_method, "
" publication_year, highest_phase_reached) "
"VALUES (2, 1, 'enrollment', 'copper', 'aact', 'R004', 'text_mining', 2023, 'phase_1')"
)
conn.commit()
def test_refresh_pair_count(self, ct_db):
conn = get_connection(ct_db)
try:
self._insert_test_data(conn)
count = refresh_all_ct_pairs(conn)
conn.commit()
assert count == 3 # 3 unique intervention-condition pairs
finally:
conn.close()
def test_best_confidence_aggregation(self, ct_db):
conn = get_connection(ct_db)
try:
self._insert_test_data(conn)
refresh_all_ct_pairs(conn)
conn.commit()
row = conn.execute(
"SELECT best_confidence, num_trials FROM intervention_condition_pairs "
"WHERE intervention_id = 1 AND condition_id = 1"
).fetchone()
assert row[0] == "gold" # best of gold + silver
assert row[1] == 2 # 2 distinct trials
finally:
conn.close()
def test_degree_computation(self, ct_db):
conn = get_connection(ct_db)
try:
self._insert_test_data(conn)
refresh_all_ct_pairs(conn)
conn.commit()
# DrugA tested on 2 conditions → intervention_degree = 2
row = conn.execute(
"SELECT intervention_degree FROM intervention_condition_pairs "
"WHERE intervention_id = 1 AND condition_id = 1"
).fetchone()
assert row[0] == 2
# DiseaseX tested with 2 drugs → condition_degree = 2
row = conn.execute(
"SELECT condition_degree FROM intervention_condition_pairs "
"WHERE intervention_id = 1 AND condition_id = 1"
).fetchone()
assert row[0] == 2
finally:
conn.close()
def test_refresh_is_idempotent(self, ct_db):
conn = get_connection(ct_db)
try:
self._insert_test_data(conn)
count1 = refresh_all_ct_pairs(conn)
conn.commit()
count2 = refresh_all_ct_pairs(conn)
conn.commit()
assert count1 == count2
finally:
conn.close()
def test_highest_phase_ordering(self, ct_db):
"""CASE-based phase ordering should pick phase_3 over not_applicable."""
conn = get_connection(ct_db)
try:
conn.execute(
"INSERT INTO interventions (intervention_type, intervention_name) "
"VALUES ('drug', 'DrugX')"
)
conn.execute(
"INSERT INTO conditions (condition_name) VALUES ('DiseaseZ')"
)
conn.execute(
"INSERT INTO clinical_trials "
"(source_db, source_trial_id, overall_status) "
"VALUES ('clinicaltrials_gov', 'NCT100', 'Terminated')"
)
conn.execute(
"INSERT INTO clinical_trials "
"(source_db, source_trial_id, overall_status) "
"VALUES ('clinicaltrials_gov', 'NCT101', 'Terminated')"
)
# Result 1: not_applicable phase
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, trial_id, failure_category, "
" confidence_tier, source_db, source_record_id, extraction_method, "
" highest_phase_reached) "
"VALUES (1, 1, 1, 'efficacy', 'bronze', 'aact', 'P001', "
"'nlp_classified', 'not_applicable')"
)
# Result 2: early_phase_1
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, trial_id, failure_category, "
" confidence_tier, source_db, source_record_id, extraction_method, "
" highest_phase_reached) "
"VALUES (1, 1, 2, 'efficacy', 'bronze', 'aact', 'P002', "
"'nlp_classified', 'early_phase_1')"
)
conn.commit()
refresh_all_ct_pairs(conn)
conn.commit()
row = conn.execute(
"SELECT highest_phase_reached FROM intervention_condition_pairs "
"WHERE intervention_id = 1 AND condition_id = 1"
).fetchone()
# early_phase_1 (rank 2) should beat not_applicable (rank 1)
assert row[0] == "early_phase_1"
finally:
conn.close()
class TestMigration002Fixes:
"""Test migration 002 schema additions."""
def test_unique_constraint_prevents_duplicates(self, ct_db):
"""Same (intervention, condition, trial, source, record) should fail."""
conn = get_connection(ct_db)
try:
conn.execute(
"INSERT INTO interventions (intervention_type, intervention_name) "
"VALUES ('drug', 'DrugU')"
)
conn.execute(
"INSERT INTO conditions (condition_name) VALUES ('DiseaseU')"
)
conn.commit()
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, failure_category, confidence_tier, "
" source_db, source_record_id, extraction_method) "
"VALUES (1, 1, 'efficacy', 'gold', 'aact', 'U001', 'database_direct')"
)
conn.commit()
with pytest.raises(sqlite3.IntegrityError):
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, failure_category, confidence_tier, "
" source_db, source_record_id, extraction_method) "
"VALUES (1, 1, 'safety', 'silver', 'aact', 'U001', 'database_direct')"
)
finally:
conn.close()
def test_unique_constraint_allows_different_sources(self, ct_db):
"""Same intervention+condition but different source_record_id is OK."""
conn = get_connection(ct_db)
try:
conn.execute(
"INSERT INTO interventions (intervention_type, intervention_name) "
"VALUES ('drug', 'DrugV')"
)
conn.execute(
"INSERT INTO conditions (condition_name) VALUES ('DiseaseV')"
)
conn.commit()
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, failure_category, confidence_tier, "
" source_db, source_record_id, extraction_method) "
"VALUES (1, 1, 'efficacy', 'gold', 'aact', 'V001', 'database_direct')"
)
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, failure_category, confidence_tier, "
" source_db, source_record_id, extraction_method) "
"VALUES (1, 1, 'efficacy', 'silver', 'aact', 'V002', 'database_direct')"
)
conn.commit()
count = conn.execute(
"SELECT COUNT(*) FROM trial_failure_results"
).fetchone()[0]
assert count == 2
finally:
conn.close()
def test_interventions_has_inchikey_columns(self, ct_db):
conn = get_connection(ct_db)
try:
conn.execute(
"INSERT INTO interventions "
"(intervention_type, intervention_name, inchikey, inchikey_connectivity) "
"VALUES ('drug', 'InChITest', 'XLYOFNOQVPJJNP-UHFFFAOYSA-N', "
"'XLYOFNOQVPJJNP')"
)
conn.commit()
row = conn.execute(
"SELECT inchikey, inchikey_connectivity FROM interventions "
"WHERE intervention_name = 'InChITest'"
).fetchone()
assert row[0] == "XLYOFNOQVPJJNP-UHFFFAOYSA-N"
assert row[1] == "XLYOFNOQVPJJNP"
finally:
conn.close()
def test_interventions_molecular_type_check(self, ct_db):
conn = get_connection(ct_db)
try:
# Valid molecular_type
conn.execute(
"INSERT INTO interventions "
"(intervention_type, intervention_name, molecular_type) "
"VALUES ('drug', 'SmallMol', 'small_molecule')"
)
conn.commit()
# Invalid molecular_type
with pytest.raises(sqlite3.IntegrityError):
conn.execute(
"INSERT INTO interventions "
"(intervention_type, intervention_name, molecular_type) "
"VALUES ('drug', 'Bad', 'invalid_type')"
)
finally:
conn.close()
def test_result_interpretation_check(self, ct_db):
conn = get_connection(ct_db)
try:
conn.execute(
"INSERT INTO interventions (intervention_type, intervention_name) "
"VALUES ('drug', 'InterpDrug')"
)
conn.execute(
"INSERT INTO conditions (condition_name) VALUES ('InterpDisease')"
)
conn.commit()
# Valid interpretation
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, failure_category, confidence_tier, "
" source_db, source_record_id, extraction_method, result_interpretation) "
"VALUES (1, 1, 'efficacy', 'gold', 'aact', 'I001', "
"'database_direct', 'definitive_negative')"
)
conn.commit()
# Invalid interpretation
with pytest.raises(sqlite3.IntegrityError):
conn.execute(
"INSERT INTO trial_failure_results "
"(intervention_id, condition_id, failure_category, confidence_tier, "
" source_db, source_record_id, extraction_method, result_interpretation) "
"VALUES (1, 1, 'efficacy', 'gold', 'aact', 'I002', "
"'database_direct', 'invalid_interp')"
)
finally:
conn.close()
def test_termination_type_check(self, ct_db):
conn = get_connection(ct_db)
try:
# Valid termination_type
conn.execute(
"INSERT INTO clinical_trials "
"(source_db, source_trial_id, overall_status, termination_type) "
"VALUES ('clinicaltrials_gov', 'NCT900', 'Terminated', 'clinical_failure')"
)
conn.commit()
# Invalid termination_type
with pytest.raises(sqlite3.IntegrityError):
conn.execute(
"INSERT INTO clinical_trials "
"(source_db, source_trial_id, overall_status, termination_type) "
"VALUES ('clinicaltrials_gov', 'NCT901', 'Terminated', 'invalid')"
)
finally:
conn.close()
|