File: conftest.py

package info (click to toggle)
kalign 1%3A3.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,060 kB
  • sloc: ansic: 16,129; python: 10,759; cpp: 636; sh: 65; makefile: 57
file content (282 lines) | stat: -rw-r--r-- 8,806 bytes parent folder | download
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
"""
Shared pytest configuration and fixtures for Kalign Python tests.
"""

import pytest
import tempfile
import os
from pathlib import Path
from typing import List, Dict, Any

# Rich table support for benchmarks
try:
    from rich.console import Console
    from rich.table import Table
    from rich.text import Text

    RICH_AVAILABLE = True
except ImportError:
    RICH_AVAILABLE = False


# Test data constants
DNA_SEQUENCES_SIMPLE = ["ATCGATCG", "ATCGTCG", "ATCGATCG"]

DNA_SEQUENCES_WITH_GAPS = [
    "ATCGATCGATCG",
    "ATCGTCGATCG",
    "ATCGATCATCG",
    "ATCGATCGAGATCG",
]

RNA_SEQUENCES_SIMPLE = ["AUCGAUCG", "AUCGUCG", "AUCGAUCG"]

PROTEIN_SEQUENCES_SIMPLE = ["MKTAYIAK", "MKTAYK", "MKTAYIAK"]

PROTEIN_SEQUENCES_COMPLEX = [
    "MKTAYIAKQRQISFVKSHFSRQLEERLGLIEVQAPILSRVGDGTQDNLSGAEKAVQVKVKALPDAQFEVVHSLAKWKRQTLGQHDFSAGEGLYTHMKALRPDEDRLSPLHSVYVDQWDWERVMGDGERQFSTLKSTVEAIWAGIKATEAAVSEEFGLAPFLPDQIHFVHSQELLSRYPDLDAKGRERAIAKDLGAVFLVGIGGKLSDGHRHDVRAPDYDDWUAIFRRVVSAEFQRQPVHQSYLNTVLGSQGKL",
    "MKTAYIAKQRQISFVKSHFSRQLEERLGLIEVQAPILSRVGDGTQDNLSGAEKAVQVKVKALPDAQFEVVHSLAKWKRQTLGQHDFSAGEGLYTHMKALRPDEDRLSPLHSVYVDQWDWERVMGDGERQFSTLKSTVEAIWAGIKATEAAVSEEFGLAPFLPDQIHFVHSQELLSRYPDLDAKGRERAIAKDLGAVFLVGIGGKLSDGHRHDVRAPDYDDWUAIFRRVVSAEFQRQPVHQSYLNTVLGSQGKL",
    "MKTAYIAKQRQISFVKSHFSRQLEERLGLIEVQAPILSRVGDGTQDNLSGAEKAVQVKVKALPDAQFEVVHSLAKWKRQTLGQHDFSAGEGLYTHMKALRPDEDRLSPLHSVYVDQWDWERVMGDGERQFSTLKSTVEAIWAGIKATEAAVSEEFGLAPFLPDQIHFVHSQELLSRYPDLDAKGRERAIAKDLGAVFLVGIGGKLSDGHRHDVRAPDYDDWUAIFRRVVSAEFQRQPVHQSYLNTVLGSQGKL",
]

INVALID_DNA_SEQUENCES = [
    "ATCG123",  # Contains numbers
    "ATCGXYZ",  # Invalid characters
    "ATCG@#$",  # Special characters
]

MIXED_CASE_SEQUENCES = ["AtCgAtCg", "atcgatcg", "ATCGATCG"]


@pytest.fixture
def dna_simple():
    """Simple DNA sequences for basic testing."""
    return DNA_SEQUENCES_SIMPLE.copy()


@pytest.fixture
def dna_with_gaps():
    """DNA sequences that will likely produce gaps."""
    return DNA_SEQUENCES_WITH_GAPS.copy()


@pytest.fixture
def rna_simple():
    """Simple RNA sequences for basic testing."""
    return RNA_SEQUENCES_SIMPLE.copy()


@pytest.fixture
def protein_simple():
    """Simple protein sequences for basic testing."""
    return PROTEIN_SEQUENCES_SIMPLE.copy()


@pytest.fixture
def protein_complex():
    """Complex protein sequences for advanced testing."""
    return PROTEIN_SEQUENCES_COMPLEX.copy()


@pytest.fixture
def invalid_dna():
    """Invalid DNA sequences for error testing."""
    return INVALID_DNA_SEQUENCES.copy()


@pytest.fixture
def mixed_case():
    """Mixed case sequences for case handling testing."""
    return MIXED_CASE_SEQUENCES.copy()


@pytest.fixture
def temp_dir():
    """Create a temporary directory for file operations."""
    with tempfile.TemporaryDirectory() as tmpdir:
        yield Path(tmpdir)


@pytest.fixture
def sample_fasta_file(temp_dir):
    """Create a sample FASTA file for testing."""
    fasta_content = """>seq1
ATCGATCGATCG
>seq2
ATCGTCGATCG
>seq3
ATCGATCATCG
"""
    fasta_file = temp_dir / "sample.fasta"
    fasta_file.write_text(fasta_content)
    return str(fasta_file)


@pytest.fixture
def sample_protein_fasta_file(temp_dir):
    """Create a sample protein FASTA file for testing."""
    fasta_content = """>protein1
MKTAYIAKQRQISFVKSHFSRQLEERLGLIEVQAPILSRVGDGTQDNLSGAEKAVQVKVKALPDAQFEVVHSLAKWKRQTLGQHDFSAGEGLYTHMKALRPDEDRLSPLHSVY
>protein2
MKTAYIAKQRQISFVKSHFSRQLEERLGLIEVQAPILSRVGDGTQDNLSGAEKAVQVKVKALPDAQFEVVHSLAKWKRQTLGQHDFSAGEGLYTHMKALRPDEDRLSPLH
>protein3
MKTAYIAKQRQISFVKSHFSRQLEERLGLIEVQAPILSRVGDGTQDNLSGAEKAVQVKVKALPDAQFEVVHSLAKWKRQTLGQHDFSAGEGLYTHMKALRPDEDRL
"""
    fasta_file = temp_dir / "proteins.fasta"
    fasta_file.write_text(fasta_content)
    return str(fasta_file)


@pytest.fixture
def invalid_fasta_file(temp_dir):
    """Create an invalid FASTA file for error testing."""
    invalid_content = """This is not a valid FASTA file
It has no proper headers
And random content
"""
    invalid_file = temp_dir / "invalid.fasta"
    invalid_file.write_text(invalid_content)
    return str(invalid_file)


@pytest.fixture
def nonexistent_file():
    """Return path to a file that doesn't exist."""
    return "/path/that/does/not/exist.fasta"


@pytest.fixture
def test_data_dir():
    """Get the test data directory path."""
    return Path(__file__).parent / "test_data"


# Parametrize fixtures for common test scenarios
@pytest.fixture(
    params=[
        ("dna", DNA_SEQUENCES_SIMPLE),
        ("rna", RNA_SEQUENCES_SIMPLE),
        ("protein", PROTEIN_SEQUENCES_SIMPLE),
    ]
)
def sequence_type_data(request):
    """Parametrized fixture providing different sequence types."""
    seq_type, sequences = request.param
    return seq_type, sequences.copy()


@pytest.fixture(params=[1, 2, 4])
def thread_count(request):
    """Parametrized fixture for testing different thread counts."""
    return request.param


@pytest.fixture(
    params=[
        ("auto", None),
        ("dna", "dna"),
        ("rna", "rna"),
        ("protein", "protein"),
        ("divergent", "divergent"),
        ("internal", "internal"),
    ]
)
def sequence_type_spec(request):
    """Parametrized fixture for testing sequence type specifications."""
    return request.param


# Pytest configuration
def pytest_configure(config):
    """Configure pytest with custom markers."""
    config.addinivalue_line("markers", "slow: mark test as slow running")
    config.addinivalue_line("markers", "integration: mark test as integration test")
    config.addinivalue_line("markers", "performance: mark test as performance test")
    config.addinivalue_line("markers", "file_io: mark test as requiring file I/O")


def pytest_collection_modifyitems(config, items):
    """Add markers to tests based on their names."""
    for item in items:
        # Mark slow tests
        if "slow" in item.nodeid or "large" in item.nodeid:
            item.add_marker(pytest.mark.slow)

        # Mark integration tests
        if "integration" in item.nodeid:
            item.add_marker(pytest.mark.integration)

        # Mark performance tests
        if "performance" in item.nodeid or "benchmark" in item.nodeid:
            item.add_marker(pytest.mark.performance)

        # Mark file I/O tests
        if "file" in item.nodeid:
            item.add_marker(pytest.mark.file_io)


# Helper functions for tests
def assert_valid_alignment(sequences: List[str], aligned: List[str]) -> None:
    """Assert that an alignment result is valid."""
    # Same number of sequences
    assert len(aligned) == len(sequences), "Number of sequences changed"

    # All aligned sequences have same length
    if aligned:
        expected_length = len(aligned[0])
        for i, seq in enumerate(aligned):
            assert len(seq) == expected_length, f"Sequence {i} has different length"

    # No empty sequences (unless input was empty)
    for seq in aligned:
        if any(
            original for original in sequences if original
        ):  # If any input was non-empty
            assert seq, "Aligned sequence is empty"


def assert_alignment_preserves_characters(
    original: List[str], aligned: List[str]
) -> None:
    """Assert that alignment preserves the original characters (just adds gaps)."""
    for orig, align in zip(original, aligned):
        # Remove gaps from aligned sequence
        degapped = align.replace("-", "")
        assert degapped == orig, f"Original sequence {orig} not preserved in {align}"


def calculate_identity(seq1: str, seq2: str) -> float:
    """Calculate sequence identity percentage between two sequences."""
    if len(seq1) != len(seq2):
        raise ValueError("Sequences must have same length")

    matches = sum(1 for a, b in zip(seq1, seq2) if a == b and a != "-" and b != "-")
    valid_positions = sum(1 for a, b in zip(seq1, seq2) if a != "-" and b != "-")

    return (matches / valid_positions * 100) if valid_positions > 0 else 0.0


# Expected results for regression testing
EXPECTED_ALIGNMENTS = {
    "dna_simple": {
        "sequences": DNA_SEQUENCES_SIMPLE,
        "expected_length": 8,  # This might need adjustment after testing
        "min_identity": 75.0,  # Minimum expected identity percentage
    },
    "protein_simple": {
        "sequences": PROTEIN_SEQUENCES_SIMPLE,
        "expected_length": 8,  # This might need adjustment after testing
        "min_identity": 85.0,  # Minimum expected identity percentage
    },
}


@pytest.fixture
def expected_results():
    """Expected alignment results for regression testing."""
    return EXPECTED_ALIGNMENTS.copy()


# Note: pytest_benchmark_update_json hook removed to avoid plugin validation errors
# when pytest-benchmark is not installed. If you need benchmark display functionality,
# install pytest-benchmark: pip install pytest-benchmark