| |
| """Batch download misfolding target PDB structures for offline use. |
| |
| Downloads all conformations from the misfolding knowledge base. |
| Stores PDB files in data/misfolding_targets/. |
| Generates a target_manifest.json for the pipeline. |
| |
| Usage: |
| python fetch_targets.py # download all targets |
| python fetch_targets.py --dry-run # check what would be downloaded |
| python fetch_targets.py --max 3 # max 3 conformations per target |
| """ |
|
|
| import sys |
| import os |
| import io |
| import json |
| import time |
| import argparse |
| from pathlib import Path |
|
|
| if sys.platform == 'win32': |
| sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') |
|
|
| PROJECT_DIR = Path(__file__).parent.parent |
| sys.path.insert(0, str(PROJECT_DIR)) |
|
|
| from misfolding_knowledge_base import MISFOLDING_TARGETS |
| from misfolding_data_loader import prepare_misfolding_target, DATA_DIR |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser( |
| description='Download misfolding disease target PDB structures' |
| ) |
| parser.add_argument('--dry-run', action='store_true', |
| help='Check what would be downloaded without downloading') |
| parser.add_argument('--max', type=int, default=3, dest='max_confs', |
| help='Max conformations per target (default: 3)') |
| parser.add_argument('--force', action='store_true', |
| help='Force re-download even if cached') |
| args = parser.parse_args() |
|
|
| total = 0 |
| success = 0 |
| failed = [] |
|
|
| print('=' * 70) |
| print('Misfolding Disease Target PDB Downloader') |
| print(f'Targets: {len(MISFOLDING_TARGETS)}') |
| print(f'Max conformations/target: {args.max_confs}') |
| print(f'Output dir: {DATA_DIR}') |
| if args.dry_run: |
| print('MODE: DRY RUN (no downloads)') |
| print('=' * 70) |
|
|
| manifest = {} |
|
|
| for disease_key, target in sorted(MISFOLDING_TARGETS.items()): |
| print(f'\n{"─" * 60}') |
| print(f'[{disease_key}]') |
| print(f' Disease: {target["disease_cn"]}') |
| print(f' Protein: {target["protein"]} ({target["gene"]})') |
|
|
| confs = target.get('conformations', {}) |
| conf_keys = list(confs.keys())[:args.max_confs] |
|
|
| if not confs: |
| print(f' ⚠ No conformations defined — skipping') |
| continue |
|
|
| manifest[disease_key] = { |
| 'disease_cn': target['disease_cn'], |
| 'protein': target['protein'], |
| 'gene': target['gene'], |
| 'idp_flag': target.get('idp_flag', False), |
| 'conformations': {}, |
| } |
|
|
| for conf_key in conf_keys: |
| conf = confs[conf_key] |
| total += 1 |
| pdb_id = conf['pdb_id'] |
| print(f' [{conf_key}] PDB: {pdb_id} ({conf.get("type", "?")})') |
|
|
| if args.dry_run: |
| print(f' → Would download from RCSB') |
| manifest[disease_key]['conformations'][conf_key] = { |
| 'pdb_id': pdb_id, 'status': 'dry_run' |
| } |
| success += 1 |
| continue |
|
|
| if args.force: |
| |
| import misfolding_data_loader |
| cache = misfolding_data_loader._load_cache() |
| cache_key = f'{disease_key}_{conf_key}' |
| if cache_key in cache: |
| del cache[cache_key] |
| misfolding_data_loader._save_cache(cache) |
| print(f' (forced re-download)') |
|
|
| result = prepare_misfolding_target(disease_key, target, conf_key) |
|
|
| if result['success']: |
| print(f' ✓ Downloaded → {result["pdb_path"]}') |
| print(f' Chain: {result["chain_id"]} | Resolution: {result["resolution"]}') |
| if result['idp_warning']: |
| print(f' ⚠ IDP warning: {result["idp_warning"][:80]}...') |
| success += 1 |
| manifest[disease_key]['conformations'][conf_key] = { |
| 'pdb_id': pdb_id, |
| 'pdb_path': result['pdb_path'], |
| 'chain_id': result['chain_id'], |
| 'resolution': result['resolution'], |
| 'status': 'ready', |
| } |
| else: |
| print(f' ✗ Failed: {result["error"]}') |
| failed.append(f'{disease_key}/{conf_key}') |
| manifest[disease_key]['conformations'][conf_key] = { |
| 'pdb_id': pdb_id, 'status': 'failed', |
| 'error': result['error'], |
| } |
|
|
| if not args.dry_run: |
| time.sleep(0.4) |
|
|
| |
| manifest_path = DATA_DIR / 'target_manifest.json' |
| with open(manifest_path, 'w', encoding='utf-8') as f: |
| json.dump(manifest, f, indent=2, ensure_ascii=False) |
|
|
| |
| print(f'\n{"=" * 70}') |
| print(f'SUMMARY') |
| print(f' Total conformations: {total}') |
| print(f' Success: {success}/{total}') |
| if failed: |
| print(f' Failed ({len(failed)}):') |
| for f in failed: |
| print(f' - {f}') |
| print(f' Manifest: {manifest_path}') |
| print(f'{"=" * 70}') |
|
|
| return 0 if not failed else 1 |
|
|
|
|
| if __name__ == '__main__': |
| sys.exit(main()) |
|
|