"""Reproduce the GBA1 public-structure summary from downloaded source files.

Place this script beside structure.pdb and source-uniprot.json, then run
python3 analyze_gba1.py. Python 3 standard library only.
"""
from pathlib import Path
import json
import statistics

root = Path(__file__).resolve().parent
sequence = json.loads((root / 'source-uniprot.json').read_text())['sequence']
residues = {}
for line in (root / 'structure.pdb').read_text().splitlines():
    if line.startswith('ATOM  ') and line[12:16].strip() == 'CA':
        key = (line[21:22], int(line[22:26]))
        residues.setdefault(key, float(line[60:66]))
scores = list(residues.values())
assert scores and sequence['length'] > 0
summary = {
    'sequence_length': sequence['length'],
    'coordinate_residue_count': len(scores),
    'coverage_percent': round(100 * len(scores) / sequence['length'], 2),
    'mean_ca_plddt': round(statistics.mean(scores), 2),
    'low_confidence_residue_count_below_50': sum(value < 50 for value in scores),
    'below_70_residue_count': sum(value < 70 for value in scores),
    'at_least_90_residue_count': sum(value >= 90 for value in scores),
}
(root / 'reproduced-summary.json').write_text(json.dumps(summary, indent=2))
print(json.dumps(summary, indent=2))
