"""Helper methods for reading input files.""" | |
import re | |
from itertools import filterfalse, tee | |
from pathlib import Path | |
def partition_template_pdb_from_file( | |
custom_templates: list[str | Path], | |
) -> tuple[list[str], list[Path]]: | |
"""Partitions custom templates files from PDB codes and removes duplicates. | |
Inspired from `partition` function in itertools cookbook: | |
https://docs.python.org/dev/library/itertools.html#itertools-recipes | |
Args: | |
custom_templates (list[str | Path]): List of custom templates | |
Returns: | |
The list of PDB codes and the list of custom files. | |
""" | |
def pred(x): | |
return re.match(r"^[a-zA-Z0-9]{4}$", str(x)) | |
t1, t2 = tee(custom_templates) | |
pdb_codes = filter(pred, t1) | |
custom_files = filterfalse(pred, t2) | |
return (pdb_codes, custom_files) | |