File size: 5,554 Bytes
9fa8514
 
 
 
 
 
b447b99
 
 
9fa8514
 
 
 
 
 
 
 
 
 
 
 
5aabf0b
9fa8514
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aabf0b
9fa8514
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e37e18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87bb202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36f317a
87bb202
 
 
 
 
 
 
 
 
 
 
 
36f317a
 
 
87bb202
 
 
 
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
"""Regression tests for Vina pose parsing and partial interaction failures.

These tests exercise the PDBQT record/model parser independently of chemistry.
They deliberately mock the chemistry routine: they validate parser behavior,
not the scientific validity of interaction classifications.
"""
import asyncio

from app.routers import docking as module


def _atom(record="ATOM", serial=1, atom="C1"):
    return (
        f"{record:<6}{serial:5d} {atom:<4s} LIG A   1    "
        f"{1.0:8.3f}{2.0:8.3f}{3.0:8.3f}  1.00  0.00"
    )


def test_vina_atom_records_are_parsed_as_pose_atoms(monkeypatch):
    observed = []

    def fake_compute(protein, ligand, ligand_smiles=None):
        observed.append(ligand)
        return {"hbonds": [], "hydrophobic": [], "pi_stacking": [], "salt_bridges": []}

    monkeypatch.setattr(module, "_compute_interactions", fake_compute)
    result = module._summarize_pose_interactions("protein", _atom("ATOM") + "\n")

    assert result == [{"model": 1, "hbonds": 0, "hydrophobic": 0, "pi_stacking": 0, "salt_bridges": 0}]
    assert len(observed) == 1
    assert observed[0].startswith("ATOM")


def test_heteroatom_records_are_also_parsed(monkeypatch):
    observed = []
    monkeypatch.setattr(
        module, "_compute_interactions",
        lambda protein, ligand, ligand_smiles=None: observed.append(ligand) or {
            "hbonds": [], "hydrophobic": [], "pi_stacking": [], "salt_bridges": []
        },
    )
    result = module._summarize_pose_interactions("protein", _atom("HETATM") + "\n")
    assert len(result) == 1
    assert observed[0].startswith("HETATM")


def test_multiple_models_remain_separate(monkeypatch):
    observed = []
    monkeypatch.setattr(
        module, "_compute_interactions",
        lambda protein, ligand: observed.append(ligand) or {
            "hbonds": [], "hydrophobic": [], "pi_stacking": [], "salt_bridges": []
        },
    )
    pdbqt = "MODEL 1\n" + _atom("ATOM", 1) + "\nENDMDL\nMODEL 2\n" + _atom("ATOM", 2) + "\nENDMDL\n"
    result = module._summarize_pose_interactions("protein", pdbqt)
    assert [row["model"] for row in result] == [1, 2]
    assert len(observed) == 2


def test_single_pose_without_model_delimiters_is_supported(monkeypatch):
    monkeypatch.setattr(
        module, "_compute_interactions",
        lambda protein, ligand: {"hbonds": [], "hydrophobic": [], "pi_stacking": [], "salt_bridges": []},
    )
    result = module._summarize_pose_interactions("protein", _atom("ATOM") + "\n")
    assert len(result) == 1
    assert result[0]["model"] == 1


def test_empty_models_are_not_reported_as_zero_contact_poses(monkeypatch):
    monkeypatch.setattr(
        module, "_compute_interactions",
        lambda *args: (_ for _ in ()).throw(AssertionError("chemistry must not run for an empty pose")),
    )
    pdbqt = "MODEL 1\nENDMDL\nMODEL 2\n" + _atom("ATOM") + "\nENDMDL\n"
    result = module._summarize_pose_interactions("protein", pdbqt)
    assert [row["model"] for row in result] == [2]



def test_stored_job_pose_extractor_preserves_model_ids_affinities_and_atom_records():
    pdbqt = (
        "MODEL 1\n"
        "REMARK VINA RESULT:    -5.132      0.000      0.000\n"
        + _atom("ATOM", 1) + "\nENDMDL\n"
        "MODEL 2\n"
        "REMARK VINA RESULT:    -4.981      1.234      2.345\n"
        + _atom("ATOM", 2) + "\nENDMDL\n"
    )
    models = module._extract_vina_pose_models(pdbqt)
    assert [m["model"] for m in models] == [1, 2]
    assert [m["affinity_kcal_mol"] for m in models] == [-5.132, -4.981]
    assert all(len(m["records"]) == 1 for m in models)



def test_exact_job_reproduction_returns_all_poses_without_mutating_artifact(monkeypatch):
    original = {
        "pdb_id": "4HHB",
        "smiles": "CCO",
        "vina_version": "1.2.7",
        "vina_exhaustiveness": 32,
        "vina_num_modes": 2,
        "vina_seed": 42,
        "box_center": [0.0, 0.0, 0.0],
        "box_size": [20.0, 20.0, 20.0],
        "receptor_pdb": "ATOM\n",
        "result_pdbqt": (
            "MODEL 1\nREMARK VINA RESULT: -5.132 0.0 0.0\n" + _atom("ATOM", 1) + "\nENDMDL\n"
            "MODEL 2\nREMARK VINA RESULT: -4.981 0.0 0.0\n" + _atom("ATOM", 2) + "\nENDMDL\n"
        ),
        "interaction_analysis": {"status": "UNAVAILABLE", "reason": "ValueError"},
        "receptor_prep_evidence": {"input_other_hetero_records_excluded": 174, "input_water_records_excluded": 221},
    }
    monkeypatch.setattr(module, "_load_docking_result", lambda job_id, user_id: original)
    monkeypatch.setattr(
        module, "_compute_interactions",
        lambda protein, ligand, smiles: {
            "status": "COMPUTED", "engine": "RDKit", "engine_version": "test",
            "counts": {"hbonds": 1, "hydrophobic": 0, "pi_stacking": 0, "salt_bridges": 0},
            "hbonds": [{"type": "hbond"}], "hydrophobic": [], "pi_stacking": [], "salt_bridges": [],
            "protein_parse": {"residues_skipped": []}, "limitations": [],
        },
    )
    result = asyncio.run(module.recalculate_stored_docking_interactions("job-1", "user-1"))
    assert result["status"] == "DEGRADED"
    assert result["receptor_coverage"]["upstream_hetero_records_excluded"] == 174
    assert result["receptor_coverage"]["upstream_water_records_excluded"] == 221
    assert result["pose_count"] == 2
    assert [p["affinity_kcal_mol"] for p in result["pose_results"]] == [-5.132, -4.981]
    assert result["reproduction"]["original_artifact_mutated"] is False
    assert original["interaction_analysis"]["status"] == "UNAVAILABLE"