| from __future__ import annotations | |
| from pathlib import Path | |
| from typing import Dict | |
| def read_fasta(path: str | Path) -> Dict[str, str]: | |
| """Read FASTA entries into mapping of record id to sequence.""" | |
| source = Path(path) | |
| records: Dict[str, str] = {} | |
| current_id = None | |
| current_seq: list[str] = [] | |
| for line in source.read_text(encoding="utf-8").splitlines(): | |
| line = line.strip() | |
| if not line: | |
| continue | |
| if line.startswith(">"): # FASTA header | |
| if current_id is not None: | |
| records[current_id] = "".join(current_seq) | |
| current_id = line[1:].split()[0] | |
| current_seq = [] | |
| else: | |
| current_seq.append(line) | |
| if current_id is not None: | |
| records[current_id] = "".join(current_seq) | |
| return records | |