"""
Test script for Qt Legacy Molecular Viewer
This script tests the molecular viewer functionality without requiring a display.
"""
import os
import sys
from pathlib import Path
from molib.core.constants import MoLibConstant
# Add the examples directory to the path so we can import the PDB loader
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "utils"))
from pdb_loader import PDBLoader
[docs]
def test_pdb_loading():
"""Test PDB loading and C-alpha extraction"""
print("๐งฌ Testing PDB Loading and C-alpha Extraction")
print("=" * 50)
# Path to the 2VUG.pdb file
pdb_path = os.path.join(os.path.dirname(__file__), "data", "2VUG.pdb")
if not os.path.exists(pdb_path):
print(f"โ Error: PDB file not found at {pdb_path}")
return False
try:
# Load PDB structure
print(f"๐ Loading PDB file: {pdb_path}")
pdb_loader = PDBLoader(pdb_path)
structure = pdb_loader.structure
print(f"โ
Successfully loaded PDB structure")
print(f" Title: {structure.title}")
print(f" Total atoms: {len(structure.atoms)}")
print(f" Total bonds: {len(structure.bonds)}")
print(f" Chains: {structure.chains}")
print(f" Residues: {len(structure.residues)}")
# Extract C-alpha atoms
calpha_atoms = [
atom
for atom in structure.atoms
if atom.name.strip() == MoLibConstant.PEPTIDE_CHAIN_ATOMNAME
]
print(f"\n๐ฌ C-alpha Analysis:")
print(f" C-alpha atoms found: {len(calpha_atoms)}")
if calpha_atoms:
# Group by chain
chain_counts = {}
for atom in calpha_atoms:
chain_id = atom.chain_id
chain_counts[chain_id] = chain_counts.get(chain_id, 0) + 1
print(f" C-alpha atoms per chain:")
for chain_id, count in chain_counts.items():
color = (
"Green" if chain_id == "A" else "Blue" if chain_id == "B" else "White"
)
print(f" Chain {chain_id}: {count} atoms ({color})")
# Show first few C-alpha atoms
print(f"\n๐ First 5 C-alpha atoms:")
for i, atom in enumerate(calpha_atoms[:5]):
print(
f" {i+1}. Chain {atom.chain_id}, Residue {atom.res_seq} ({atom.res_name}): "
f"({atom.x:.2f}, {atom.y:.2f}, {atom.z:.2f})"
)
# Test bond generation
print(f"\n๐ Bond Analysis:")
calpha_bonds = []
# Group atoms by chain
chain_atoms = {}
for i, atom in enumerate(calpha_atoms):
chain_id = atom.chain_id
if chain_id not in chain_atoms:
chain_atoms[chain_id] = []
chain_atoms[chain_id].append((i, atom))
# Create bonds between consecutive C-alpha atoms in each chain
for chain_id, atoms in chain_atoms.items():
# Sort by residue sequence number
atoms.sort(key=lambda x: x[1].res_seq)
for i in range(len(atoms) - 1):
idx1, atom1 = atoms[i]
idx2, atom2 = atoms[i + 1]
# Only create bonds between consecutive residues
if atom2.res_seq == atom1.res_seq + 1:
calpha_bonds.append((idx1, idx2))
print(f" C-alpha bonds generated: {len(calpha_bonds)}")
# Show bond statistics per chain
chain_bonds = {}
for bond in calpha_bonds:
atom1 = calpha_atoms[bond[0]]
chain_id = atom1.chain_id
chain_bonds[chain_id] = chain_bonds.get(chain_id, 0) + 1
print(f" C-alpha bonds per chain:")
for chain_id, count in chain_bonds.items():
color = (
"Green" if chain_id == "A" else "Blue" if chain_id == "B" else "White"
)
print(f" Chain {chain_id}: {count} bonds ({color})")
print(f"\nโ
All tests passed!")
return True
except Exception as e:
print(f"โ Error during testing: {e}")
import traceback
traceback.print_exc()
return False
[docs]
def test_molecular_viewer_import():
"""Test that the molecular viewer can be imported"""
print("\n๐ง Testing Molecular Viewer Import")
print("=" * 50)
try:
# Try to import the molecular viewer
from legacy_qt_molecular_viewer import (
LegacyMolecularViewerWindow,
QtLegacyMolecularViewer,
)
print("โ
Successfully imported QtLegacyMolecularViewer")
print("โ
Successfully imported LegacyMolecularViewerWindow")
return True
except ImportError as e:
print(f"โ Import error: {e}")
return False
except Exception as e:
print(f"โ Unexpected error: {e}")
return False
[docs]
def main():
"""Run all tests"""
print("๐งฌ Qt Legacy Molecular Viewer Test Suite")
print("=" * 60)
tests = [
("PDB Loading and C-alpha Extraction", test_pdb_loading),
("Molecular Viewer Import", test_molecular_viewer_import),
]
passed = 0
total = len(tests)
for test_name, test_func in tests:
print(f"\n๐งช Running: {test_name}")
if test_func():
passed += 1
print(f"โ
{test_name} - PASSED")
else:
print(f"โ {test_name} - FAILED")
print(f"\n๐ Test Results: {passed}/{total} tests passed")
if passed == total:
print("๐ All tests passed! The molecular viewer is ready to use.")
print("\n๐ To run the molecular viewer:")
print(" python legacy_qt_molecular_viewer.py")
print("")
print("๐ฎ Controls:")
print(" โข Left mouse: Rotate")
print(" โข Mouse wheel: Zoom")
print(" โข R key: Reset view")
print(" โข W key: Toggle wireframe/filled spheres")
print(" โข ESC: Exit")
else:
print("โ ๏ธ Some tests failed. Please check the error above.")
return passed == total
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)