Skip to content

shelx

parse_shelx_file(filename)

Read a SHELX formatted .res file. Parameters


filename: str path to the shelx .res file to read

Returns

dict dictionary of parsed shelx data

Source code in chmpy/fmt/shelx.py
def parse_shelx_file(filename):
    """Read a SHELX formatted .res file.
    Parameters
    ----------
    filename: str
        path to the shelx .res file to read

    Returns
    -------
    dict
        dictionary of parsed shelx data
    """
    return parse_shelx_file_content(Path(filename).read_text())

parse_shelx_file_content(file_content)

Read a SHELX formatted crystal structure from a string Parameters


file_content: str text contents of the SHELX .res file to read

Returns

dict dictionary of parsed shelx data

Source code in chmpy/fmt/shelx.py
def parse_shelx_file_content(file_content):
    """Read a SHELX formatted crystal structure from
    a string
    Parameters
    ----------
    file_content: str
        text contents of the SHELX .res file to read

    Returns
    -------
    dict
        dictionary of parsed shelx data
    """
    contents = file_content.split("\n")
    shelx_dict = {"SYMM": [SymmetryOperation.from_string_code("x,y,z")], "ATOM": []}
    for line_number, line in enumerate(contents, start=1):
        try:
            line = line.strip()
            if not line:
                continue
            key = line[:4].upper()
            if key == "END":
                break
            elif key == "SYMM":
                shelx_dict[key].append(SHELX_LINE_KEYS[key](line))
            elif key not in SHELX_LINE_KEYS:
                shelx_dict["ATOM"].append(_parse_atom_line(shelx_dict["SFAC"], line))
            else:
                f = SHELX_LINE_KEYS[key]
                if f is None:
                    continue
                shelx_dict[key] = f(line)
        except Exception as e:
            raise ValueError(f"Error parsing shelx string: line {line_number}") from e
    return shelx_dict

to_res_contents(shelx_data)

Parameters

shelx_data: dict dictionary of data to write into a SHELX .res format

Returns

str the string encoded contents of this shelx_data

Source code in chmpy/fmt/shelx.py
def to_res_contents(shelx_data):
    """
    Parameters
    ----------
    shelx_data: dict
        dictionary of data to write into a SHELX .res format

    Returns
    -------
    str
        the string encoded contents of this shelx_data
    """
    SHELX_FORMATTERS = {
        "TITL": lambda x: f"TITL {x}",
        "CELL": _cell_string,
        "LATT": lambda x: f"LATT {x}",
        "SYMM": lambda symm: "\n".join(f"SYMM {x}" for x in symm),
        "SFAC": lambda x: "SFAC " + " ".join(x),
        "ATOM": _atom_lines,
    }
    sections = []
    for key in SHELX_FORMATTERS:
        sections.append(SHELX_FORMATTERS[key](shelx_data[key]))
    return "\n".join(sections)