1 0
import logging
2 0
import re
3

4 0
import yaml
5

6 0
logger = logging.getLogger(__name__)
7

8

9 0
def read_yaml(file):
10
    """
11
    Read `Taproom <https://github.com/slochower/host-guest-benchmarks>`_ -style YAML-formatted instructions for
12
    preparing host-guest systems.
13

14
    Parameters
15
    ----------
16
    file: os.PathLike
17
        A YAML-formatted file.
18

19
    Returns
20
    -------
21
    yaml_data: dict
22
        Dictionary containing simulation setup parameters.
23

24
    """
25

26 0
    with open(file, "r") as f:
27 0
        yaml_data = yaml.safe_load(f)
28 0
    logger.debug(yaml_data)
29

30 0
    if "aliases" in yaml_data.keys():
31 0
        logger.debug("Dealiasing atom masks...")
32 0
        yaml_data = de_alias(yaml_data)
33

34 0
    return yaml_data
35

36

37 0
def multiple_replace(dct, text):
38
    """
39
    Create a regular expression to do multiple find and replace.
40
    """
41

42
    # Create a regular expression from the dictionary keys
43 0
    regex = re.compile("(%s)" % "|".join(map(re.escape, dct.keys())))
44

45
    # For each match, look-up corresponding value in dictionary
46 0
    return regex.sub(lambda mo: dct[mo.string[mo.start() : mo.end()]], text)
47

48

49 0
def de_alias(yaml_data):
50
    """
51
    Replace aliased atoms in a ``taproom`` recipe.
52
    """
53

54 0
    mapping_list = yaml_data["aliases"]
55 0
    mapping_dictionary = {}
56

57 0
    for atom_pair in mapping_list:
58 0
        mapping_dictionary.update(atom_pair)
59 0
    logger.debug(f"Found mapping: {mapping_dictionary}")
60

61 0
    for restraint_type, restraint_type_list in yaml_data["restraints"].items():
62 0
        for restraint in restraint_type_list:
63 0
            atoms = restraint["restraint"]["atoms"]
64 0
            mapped_atoms = multiple_replace(mapping_dictionary, atoms)
65 0
            logger.info(f"{atoms} → {mapped_atoms}")
66 0
            restraint["restraint"]["atoms"] = mapped_atoms
67

68 0
    if "symmetry_correction" in yaml_data.keys():
69 0
        for restraint in yaml_data["symmetry_correction"]["restraints"]:
70 0
            atoms = restraint["atoms"]
71 0
            mapped_atoms = multiple_replace(mapping_dictionary, atoms)
72 0
            logger.info(f"{atoms} → {mapped_atoms}")
73 0
            restraint["atoms"] = mapped_atoms
74

75 0
    return yaml_data

Read our documentation on viewing source code .

Loading