Source code for schrodinger.application.combiglide.utils
"""
This module contains the utility functions for CombiGlide.
"""
# Copyright Schrodinger, LLC. All rights reserved.
import re
[docs]def get_spec_dict(
        filename,
        delimiter=':',
        pat_replace={},  # noqa: M511
        ignore_case=True):
    r"""
    Reads the file, tokenizes the each line with delimiter, and returns the
    dict. It ignores the lines which start with '#'.
    :type filename: string
    :param filename: The name of the input file.
    :type delimiter: str
    :param delimiter: line is divided from delimiter to create the <key,value>
        of dict.
    :type pat_replace: dict
    :param pat_replace: this dict can be used to replace pattern, if specified.
        For example: pat_replace={'#.*':'', '\s+':' '} would do replace all
        characters starting from '#' to end of the line with '' and replace
        multiple spaces with one space.
    :type ignore_case: bool
    :param ignore_case: it is used to create the lowercase keys to make case
            insensitive search
    Illustration::
        A call to get_spec_dict(filename, pat_replace={'#.*':'', '\s+':' '})
        with following file..
        file content = "
             #CombGen input file
             #Core molecule
             Core: cg_singledock_run1_tproj38171a23586-core.mae
             #Attachment AcCl
             Chain: AcCl 1 1 1f 1t
             Frag: AcCl 1 cg_acid_chlorides_Acid_Cl_C_C
             #Attachment Hyd
             Chain: Hyd 1 1 2f 2t
             Frag: Hyd 1 cg_hydrazines_Hydrazine_C_N
             #Attachment NCO
             Chain: NCO 1 1 3f 3t
             Frag: NCO 1 cg_isocyanates_Isocyanate_C_N
        "
        would return spec_dict as follow...
        {'frag': ['AcCl', '1', 'cg_acid_chlorides_Acid_Cl_C_C',
                 'Hyd', '1', 'cg_hydrazines_Hydrazine_C_N',
                 'NCO', '1', 'cg_isocyanates_Isocyanate_C_N'],
         'core': ['cg_singledock_run1_tproj38171a23586-core.mae'],
         'chain': ['AcCl', '1', '1', '1f', '1t',
                   'Hyd', '1', '1', '2f', '2t',
                   'NCO', '1', '1', '3f', '3t']}
    """
    spec_dict = {}
    file = open(filename)
    for line in file:
        line = line.strip()
        if line and not line.startswith('#'):
            key, val = line.split(delimiter)
            key = key.strip()
            if ignore_case:
                key = key.lower()
            for pat, replace in pat_replace.items():
                val = re.sub(pat, replace, val)
            val = re.split(' ', val.strip())
            if key in spec_dict:
                spec_dict[key] += val
            else:
                spec_dict[key] = val
    file.close()
    return spec_dict 
# EOF