File size: 6,850 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
"""Tests for BindingDB ETL pipeline."""

from pathlib import Path

import pandas as pd
import pytest

from negbiodb.db import connect, create_database
from negbiodb.etl_bindingdb import (
    _extract_inactive_rows_from_chunk,
    _parse_relation_value,
    run_bindingdb_etl,
)

MIGRATIONS_DIR = Path(__file__).resolve().parent.parent / "migrations"


@pytest.fixture
def migrated_db(tmp_path):
    db_path = tmp_path / "test.db"
    create_database(db_path, MIGRATIONS_DIR)
    return db_path


class TestBindingDBHelpers:

    def test_parse_relation_value(self):
        assert _parse_relation_value(">10000") == (">", 10000.0)
        assert _parse_relation_value("<=500") == ("<=", 500.0)
        assert _parse_relation_value("12345") == ("=", 12345.0)
        assert _parse_relation_value(None) == ("=", None)

    def test_extract_inactive_rows_human_only(self):
        chunk = pd.DataFrame(
            {
                "Ligand SMILES": ["c1ccccc1", "c1ccccc1", "CCO", "CCN"],
                "UniProt (SwissProt) Primary ID of Target Chain": [
                    "P00533",
                    "P00533",
                    "P12345",
                    "P12345",
                ],
                "Target Source Organism According to Curator or DataSource": [
                    "Homo sapiens",
                    "Homo sapiens",
                    "Mus musculus",
                    "Homo sapiens",
                ],
                "Ki (nM)": [">10000", "500", ">15000", None],
                "Kd (nM)": [None, None, None, ">12000"],
                "BindingDB Reactant_set_id": [1, 2, 3, 4],
                "Publication Year": [2010, 2011, 2012, 2013],
            }
        )

        rows = _extract_inactive_rows_from_chunk(
            chunk, inactivity_threshold_nm=10000, human_only=True
        )
        assert len(rows) == 2
        assert {r["activity_type"] for r in rows} == {"Ki", "Kd"}
        assert all(r["species_tested"] == "Homo sapiens" for r in rows)

    def test_extract_inactive_rows_with_non_human_enabled(self):
        chunk = pd.DataFrame(
            {
                "Ligand SMILES": ["CCO"],
                "UniProt (SwissProt) Primary ID of Target Chain": ["P12345"],
                "Target Source Organism According to Curator or DataSource": [
                    "Mus musculus"
                ],
                "Ki (nM)": [">15000"],
                "BindingDB Reactant_set_id": [3],
                "Publication Year": [2012],
            }
        )

        rows = _extract_inactive_rows_from_chunk(
            chunk, inactivity_threshold_nm=10000, human_only=False
        )
        assert len(rows) == 1
        assert rows[0]["species_tested"] == "Mus musculus"

    def test_extract_inactive_rows_requires_organism_when_human_only(self):
        chunk = pd.DataFrame(
            {
                "Ligand SMILES": ["c1ccccc1"],
                "UniProt (SwissProt) Primary ID of Target Chain": ["P00533"],
                "Ki (nM)": [">10000"],
            }
        )
        rows = _extract_inactive_rows_from_chunk(
            chunk, inactivity_threshold_nm=10000, human_only=True
        )
        assert rows == []


class TestRunBindingDBETL:

    def test_run_bindingdb_etl_small_dataset(self, migrated_db, tmp_path):
        tsv_path = tmp_path / "BindingDB_All.tsv"
        pd.DataFrame(
            {
                "Ligand SMILES": ["c1ccccc1", "c1ccccc1", "CCO", "CCN"],
                "UniProt (SwissProt) Primary ID of Target Chain": [
                    "P00533",
                    "P00533",
                    "P12345",
                    "P12345",
                ],
                "Target Source Organism According to Curator or DataSource": [
                    "Homo sapiens",
                    "Homo sapiens",
                    "Homo sapiens",
                    "Homo sapiens",
                ],
                "Ki (nM)": [">10000", ">20000", None, "500"],
                "IC50 (nM)": [None, None, "15000", "500"],
                "BindingDB Reactant_set_id": [1, 2, 3, 4],
                "Publication Year": [2010, 2011, 2020, 2021],
            }
        ).to_csv(tsv_path, sep="\t", index=False)

        stats = run_bindingdb_etl(
            db_path=migrated_db,
            bindingdb_tsv_path=tsv_path,
            chunksize=2,
        )

        assert stats["rows_read"] == 4
        assert stats["rows_filtered_inactive"] == 3
        assert stats["results_inserted"] == 3

        with connect(migrated_db) as conn:
            n_results = conn.execute(
                "SELECT COUNT(*) FROM negative_results WHERE source_db='bindingdb'"
            ).fetchone()[0]
            assert n_results == 3

            n_pairs = conn.execute(
                "SELECT COUNT(*) FROM compound_target_pairs"
            ).fetchone()[0]
            assert n_pairs == 2

            species = {
                row[0]
                for row in conn.execute(
                    "SELECT DISTINCT species_tested FROM negative_results WHERE source_db='bindingdb'"
                ).fetchall()
            }
            assert species == {"Homo sapiens"}

    def test_run_bindingdb_etl_respects_threshold_and_human_toggle(
        self, migrated_db, tmp_path, monkeypatch
    ):
        import negbiodb.etl_bindingdb as mod

        tsv_path = tmp_path / "BindingDB_All.tsv"
        pd.DataFrame(
            {
                "Ligand SMILES": ["c1ccccc1", "CCO"],
                "UniProt (SwissProt) Primary ID of Target Chain": ["P00533", "P12345"],
                "Target Source Organism According to Curator or DataSource": [
                    "Homo sapiens",
                    "Mus musculus",
                ],
                "Ki (nM)": ["15000", "25000"],
                "BindingDB Reactant_set_id": [1, 2],
                "Publication Year": [2010, 2011],
            }
        ).to_csv(tsv_path, sep="\t", index=False)

        monkeypatch.setattr(
            mod,
            "load_config",
            lambda: {
                "inactivity_threshold_nm": 10000,
                "downloads": {"bindingdb": {"dest_dir": "unused"}},
                "bindingdb_etl": {
                    "chunksize": 100000,
                    "inactive_threshold_nm": 20000,
                    "human_only": False,
                },
            },
        )

        stats = run_bindingdb_etl(
            db_path=migrated_db,
            bindingdb_tsv_path=tsv_path,
            chunksize=10,
        )
        assert stats["results_inserted"] == 1

        with connect(migrated_db) as conn:
            row = conn.execute(
                "SELECT inactivity_threshold, species_tested FROM negative_results "
                "WHERE source_db='bindingdb'"
            ).fetchone()
            assert row == (20000.0, "Mus musculus")