File size: 828 Bytes
c289d87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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