Repository navigation
Expand file tree
/
Copy pathtest_regression.py
More file actions
executable file
·282 lines (233 loc) · 9.76 KB
/
Copy pathtest_regression.py
File metadata and controls
executable file
·282 lines (233 loc) · 9.76 KB
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
#!/usr/bin/env python3
"""
Regression test for VaxLab-report
This script runs evaluate_only.py and report_only.py with a known input
and checks that the output matches expected results.
"""
import os
import sys
import json
import tempfile
import shutil
import subprocess
from pathlib import Path
# Use test file included in repository
TEST_INPUT_FILE = "test_data.fasta"
def run_command(cmd, cwd=None):
"""Run a command and return the result"""
try:
# Check if we're in a conda environment or if conda is available
conda_env = os.environ.get('CONDA_DEFAULT_ENV')
if conda_env:
# Already in a conda environment, run command directly
full_cmd = cmd
else:
# Try to detect conda and activate environment
conda_exe = shutil.which('conda')
if conda_exe:
# Use detected conda
full_cmd = f'eval "$(conda shell.bash hook)" && conda activate base && {cmd}'
else:
# No conda found, run command directly (assume dependencies are available)
full_cmd = cmd
result = subprocess.run(
full_cmd,
shell=True,
capture_output=True,
text=True,
cwd=cwd,
timeout=120, # 2 minute timeout
executable='/bin/bash' # Use bash explicitly
)
return result.returncode, result.stdout, result.stderr
except subprocess.TimeoutExpired:
return -1, "", "Command timed out"
def test_evaluate_only():
"""Test evaluate_only.py"""
print("Testing evaluate_only.py...")
# Check if test input file exists
if not os.path.exists(TEST_INPUT_FILE):
print(f"❌ Test input file not found: {TEST_INPUT_FILE}")
return False
with tempfile.TemporaryDirectory() as tmpdir:
# Copy test input file
input_file = os.path.join(tmpdir, "test_input.fasta")
shutil.copy2(TEST_INPUT_FILE, input_file)
# Run evaluate_only.py
cmd = f"python vaxlab_report/evaluate_only.py -i {input_file} -o {tmpdir} --preset parameters.json"
returncode, _, stderr = run_command(cmd)
if returncode != 0:
print(f"❌ evaluate_only.py failed with return code {returncode}")
print(f"stderr: {stderr}")
return False
# Check if required output files exist
required_files = [
"evaluation_result_cds.json",
"evaluation_result_mrna.json",
"checkpoints_cds.tsv",
"checkpoints_mrna.tsv",
"evaluate_only_log.txt"
]
for filename in required_files:
filepath = os.path.join(tmpdir, filename)
if not os.path.exists(filepath):
print(f"❌ Missing output file: {filename}")
return False
# Validate JSON structure
for json_file in ["evaluation_result_cds.json", "evaluation_result_mrna.json"]:
try:
with open(os.path.join(tmpdir, json_file), 'r') as f:
data = json.load(f)
# Check required keys
required_keys = ["input_sequence_id", "evaluation_status", "global_metrics", "local_metrics"]
for key in required_keys:
if key not in data:
print(f"❌ Missing key '{key}' in {json_file}")
return False
# Check that we have some metrics
if not data["global_metrics"]:
print(f"❌ No global metrics in {json_file}")
return False
except json.JSONDecodeError as e:
print(f"❌ Invalid JSON in {json_file}: {e}")
return False
print("✅ evaluate_only.py test passed")
return True
def test_report_only():
"""Test report_only.py"""
print("Testing report_only.py...")
# Check if test input file exists
if not os.path.exists(TEST_INPUT_FILE):
print(f"❌ Test input file not found: {TEST_INPUT_FILE}")
return False
with tempfile.TemporaryDirectory() as tmpdir:
# Copy test input file
input_file = os.path.join(tmpdir, "test_input.fasta")
shutil.copy2(TEST_INPUT_FILE, input_file)
# First run evaluate_only to create required JSON files
cmd = f"python vaxlab_report/evaluate_only.py -i {input_file} -o {tmpdir} --preset parameters.json"
returncode, _, stderr = run_command(cmd)
if returncode != 0:
print(f"❌ Setup failed: evaluate_only.py returned {returncode}")
print(f"stderr: {stderr}")
return False
# Run report_only.py
cmd = f"python vaxlab_report/report_only.py -i {input_file} -o {tmpdir}"
returncode, _, stderr = run_command(cmd)
if returncode != 0:
print(f"❌ report_only.py failed with return code {returncode}")
print(f"stderr: {stderr}")
return False
# Check if report.html was created
report_file = os.path.join(tmpdir, "report.html")
if not os.path.exists(report_file):
print("❌ Missing output file: report.html")
return False
# Basic HTML validation
with open(report_file, 'r') as f:
html_content = f.read()
required_elements = [
"<html>",
"</html>",
"VaxLab mRNA Optimization Result",
"Global Sequence Metrics",
"Local Sequence Metrics"
]
for element in required_elements:
if element not in html_content:
print(f"❌ Missing HTML element: {element}")
return False
print("✅ report_only.py test passed")
return True
def test_with_forna():
"""Test report_only.py with --forna option"""
print("Testing report_only.py with --forna option...")
# Check if test input file exists
if not os.path.exists(TEST_INPUT_FILE):
print(f"❌ Test input file not found: {TEST_INPUT_FILE}")
return False
with tempfile.TemporaryDirectory() as tmpdir:
# Copy test input file
input_file = os.path.join(tmpdir, "test_input.fasta")
shutil.copy2(TEST_INPUT_FILE, input_file)
# First run evaluate_only
cmd = f"python vaxlab_report/evaluate_only.py -i {input_file} -o {tmpdir} --preset parameters.json"
returncode, _, stderr = run_command(cmd)
if returncode != 0:
print(f"❌ Setup failed: evaluate_only.py returned {returncode}")
return False
# Test both forna options
for forna_option in ['qbio', 'tbi']:
cmd = f"python vaxlab_report/report_only.py -i {input_file} -o {tmpdir} --forna {forna_option}"
returncode, _, stderr = run_command(cmd)
if returncode != 0:
print(f"❌ report_only.py with --forna {forna_option} failed with return code {returncode}")
print(f"stderr: {stderr}")
return False
# Check HTML contains forna URL
report_file = os.path.join(tmpdir, "report.html")
with open(report_file, 'r') as f:
html_content = f.read()
if forna_option == 'qbio':
if "pub-forna.qbio.io" not in html_content:
print(f"❌ qbio Forna URL not found in HTML")
return False
elif forna_option == 'tbi':
if "nibiru.tbi.univie.ac.at" not in html_content:
print(f"❌ TBI Forna URL not found in HTML")
return False
print("✅ Forna options test passed")
return True
def main():
"""Run all regression tests"""
print("🧪 Running VaxLab-report regression tests...\n")
# Change to repository root
repo_root = Path(__file__).parent
os.chdir(repo_root)
# Check if required files exist
required_files = [
"vaxlab_report/evaluate_only.py",
"vaxlab_report/report_only.py",
"parameters.json"
]
for filepath in required_files:
if not os.path.exists(filepath):
print(f"❌ Required file not found: {filepath}")
sys.exit(1)
# Run tests
tests = [
test_evaluate_only,
test_report_only,
test_with_forna
]
passed = 0
total = len(tests)
for test_func in tests:
try:
if test_func():
passed += 1
else:
print(f"❌ Test {test_func.__name__} failed")
except Exception as e:
print(f"❌ Test {test_func.__name__} crashed: {e}")
print() # Empty line between tests
# Results
print(f"📊 Test Results: {passed}/{total} tests passed")
if passed == total:
print("🎉 All regression tests passed!")
print()
print("🔍 Test Summary:")
print(" ✅ evaluate_only.py - CDS and mRNA evaluation")
print(" ✅ report_only.py - HTML report generation")
print(" ✅ Forna options - Structure visualization (qbio/tbi)")
print(" ✅ File outputs - All expected files validated")
print(" ✅ JSON structure - Evaluation results properly formatted")
print()
print("📦 VaxLab-report is ready for production use!")
sys.exit(0)
else:
print("💥 Some tests failed!")
print("Please check the error messages above and fix any issues before deployment.")
sys.exit(1)
if __name__ == "__main__":
main()