File size: 5,471 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
"""Tests for PPI protein mapper: UniProt validation & canonical pair ordering."""

from pathlib import Path

import pytest

from negbiodb_ppi.ppi_db import get_connection, run_ppi_migrations
from negbiodb_ppi.protein_mapper import (
    canonical_pair,
    get_or_insert_protein,
    validate_uniprot,
)

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


@pytest.fixture
def ppi_db(tmp_path):
    """Create a temporary PPI database with all migrations applied."""
    db_path = tmp_path / "test_ppi.db"
    run_ppi_migrations(db_path, MIGRATIONS_DIR)
    return db_path


class TestValidateUniprot:
    """Test UniProt accession validation."""

    def test_valid_swissprot_p(self):
        assert validate_uniprot("P12345") == "P12345"

    def test_valid_swissprot_q(self):
        assert validate_uniprot("Q9UHC1") == "Q9UHC1"

    def test_valid_swissprot_o(self):
        assert validate_uniprot("O15553") == "O15553"

    def test_valid_trembl_short(self):
        assert validate_uniprot("A0A024") == "A0A024"

    def test_valid_trembl_long(self):
        assert validate_uniprot("A0A0K9P0T2") == "A0A0K9P0T2"

    def test_isoform_stripped(self):
        assert validate_uniprot("P12345-2") == "P12345"

    def test_isoform_long_stripped(self):
        assert validate_uniprot("Q9UHC1-14") == "Q9UHC1"

    def test_whitespace_stripped(self):
        assert validate_uniprot("  P12345  ") == "P12345"

    def test_invalid_numeric_only(self):
        assert validate_uniprot("12345") is None

    def test_invalid_too_short(self):
        assert validate_uniprot("P1") is None

    def test_invalid_all_alpha(self):
        assert validate_uniprot("ABCDEF") is None

    def test_invalid_empty(self):
        assert validate_uniprot("") is None

    def test_invalid_none(self):
        assert validate_uniprot(None) is None

    def test_invalid_lowercase(self):
        assert validate_uniprot("p12345") is None

    def test_invalid_special_chars(self):
        assert validate_uniprot("P123!5") is None


class TestCanonicalPair:
    """Test canonical pair ordering."""

    def test_already_ordered(self):
        assert canonical_pair("A00001", "B00002") == ("A00001", "B00002")

    def test_reversed(self):
        assert canonical_pair("B00002", "A00001") == ("A00001", "B00002")

    def test_same_accession(self):
        assert canonical_pair("P12345", "P12345") == ("P12345", "P12345")

    def test_real_accessions(self):
        assert canonical_pair("Q9UHC1", "P12345") == ("P12345", "Q9UHC1")


class TestGetOrInsertProtein:
    """Test protein insertion/retrieval."""

    def test_insert_new_protein(self, ppi_db):
        conn = get_connection(ppi_db)
        try:
            pid = get_or_insert_protein(conn, "P12345")
            conn.commit()
            assert pid == 1

            row = conn.execute(
                "SELECT uniprot_accession, organism FROM proteins "
                "WHERE protein_id = ?",
                (pid,),
            ).fetchone()
            assert row[0] == "P12345"
            assert row[1] == "Homo sapiens"
        finally:
            conn.close()

    def test_return_existing_protein(self, ppi_db):
        conn = get_connection(ppi_db)
        try:
            pid1 = get_or_insert_protein(conn, "P12345")
            conn.commit()
            pid2 = get_or_insert_protein(conn, "P12345")
            assert pid1 == pid2

            count = conn.execute(
                "SELECT COUNT(*) FROM proteins"
            ).fetchone()[0]
            assert count == 1
        finally:
            conn.close()

    def test_insert_with_gene_symbol(self, ppi_db):
        conn = get_connection(ppi_db)
        try:
            pid = get_or_insert_protein(conn, "Q9UHC1", gene_symbol="BRCA1")
            conn.commit()

            row = conn.execute(
                "SELECT gene_symbol FROM proteins WHERE protein_id = ?",
                (pid,),
            ).fetchone()
            assert row[0] == "BRCA1"
        finally:
            conn.close()

    def test_insert_with_sequence(self, ppi_db):
        conn = get_connection(ppi_db)
        try:
            seq = "MTEYKLVVV"
            pid = get_or_insert_protein(conn, "P01116", sequence=seq)
            conn.commit()

            row = conn.execute(
                "SELECT amino_acid_sequence, sequence_length FROM proteins "
                "WHERE protein_id = ?",
                (pid,),
            ).fetchone()
            assert row[0] == seq
            assert row[1] == len(seq)
        finally:
            conn.close()

    def test_insert_without_sequence_null_length(self, ppi_db):
        conn = get_connection(ppi_db)
        try:
            pid = get_or_insert_protein(conn, "P12345")
            conn.commit()

            row = conn.execute(
                "SELECT sequence_length FROM proteins WHERE protein_id = ?",
                (pid,),
            ).fetchone()
            assert row[0] is None
        finally:
            conn.close()

    def test_multiple_proteins(self, ppi_db):
        conn = get_connection(ppi_db)
        try:
            pid1 = get_or_insert_protein(conn, "P12345")
            pid2 = get_or_insert_protein(conn, "Q9UHC1")
            conn.commit()
            assert pid1 != pid2

            count = conn.execute(
                "SELECT COUNT(*) FROM proteins"
            ).fetchone()[0]
            assert count == 2
        finally:
            conn.close()