Reading and writing¶
pefftacular.read_peff
¶
read_peff(
source: str | Path | IO[str],
) -> tuple[FileHeader, list[SequenceEntry]]
Convenience: parse an entire PEFF file into header + list of entries.
A path may be plain or gzip/bzip2/xz compressed. Undecodable (non-UTF-8) or
corrupt compressed input raises PeffParseError chained to the cause.
Source code in src/pefftacular/_parser.py
pefftacular.PeffReader
¶
Lazy reader for PEFF files.
Use it as a context manager, like fastatacular.FastaReader: a path is opened
in __enter__ (UTF-8, a leading BOM is skipped; gzip, bzip2 and xz files are
decompressed, detected from the magic bytes or the suffix) and closed in __exit__; a
stream you pass in is read but never closed. Accessing header or iterating
outside the with block raises :class:RuntimeError. Entries can be
iterated once; a second iteration over the same reader yields nothing.
Example::
with PeffReader("proteins.peff") as reader:
header = reader.header
for entry in reader:
...
Source code in src/pefftacular/_parser.py
to_records
¶
Read the remaining entries as flat dicts (see :func:pefftacular.to_records).
Source code in src/pefftacular/_parser.py
pefftacular.write_peff
¶
write_peff(
header: FileHeader,
entries: Iterable[SequenceEntry],
dest: str | Path | IO[str],
*,
verify: bool = True,
) -> None
Write a complete PEFF file.
Every entry is validated and formatted before anything is written to dest:
on a PeffWriteError a path is not created or truncated and nothing is
written to a handle. entries is consumed once, as a stream; formatted text
is spooled to a temporary file past 32 MiB instead of being held in memory.
With verify=True (the default) each formatted entry is parsed back and
compared with the entry, so a value holding PEFF syntax (\Key= text, an
unbalanced paren) raises instead of writing a file that reads back
differently. verify=False skips that read-back (about 3/4 of the write
time); use it for entries that came from read_peff / PeffReader
unchanged, or that you have already written once. The basic checks (empty or
malformed prefix, id or sequence, line breaks, duplicate keys) always run.