Source code for iodata.formats.sdf

# IODATA is an input and output module for quantum chemistry.
# Copyright (C) 2011-2019 The IODATA Development Team
#
# This file is part of IODATA.
#
# IODATA is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# IODATA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>
# --
"""SDF file format.

Usually, the different frames in a trajectory describe different geometries of the same
molecule, with atoms in the same order. The ``load_many`` and ``dump_many`` functions
below can also handle an SDF file with different molecules, e.g. a molecular database.

The SDF format is somewhat documented on the following page:
http://www.nonlinear.com/progenesis/sdf-studio/v0.9/faq/sdf-file-format-guidance.aspx

This format is one of the chemical table file formats:
https://en.wikipedia.org/wiki/Chemical_table_file
"""

from collections.abc import Iterator
from typing import TextIO

import numpy as np

from ..docstrings import (
    document_dump_many,
    document_dump_one,
    document_load_many,
    document_load_one,
)
from ..iodata import IOData
from ..periodic import num2sym, sym2num
from ..prepare import prepare_single_line_title
from ..utils import LineIterator, LoadError, PrepareDumpError, angstrom

__all__ = ()


PATTERNS = ["*.sdf"]


def _parse_counts(line: str, lit: LineIterator) -> tuple[int, int]:
    """Parse the counts line, which has fixed-width fields."""
    if line.rsplit(maxsplit=1)[-1].upper() != "V2000":
        raise LoadError("Only V2000 SDF files are supported.", lit)
    try:
        return int(line[0:3]), int(line[3:6])
    except ValueError as exc:
        raise LoadError("Could not read the numbers of atoms and bonds.", lit) from exc


def _parse_atom(line: str, lit: LineIterator) -> tuple[float, float, float, int]:
    """Parse an atom line, which has fixed-width fields."""
    try:
        x = float(line[0:10])
        y = float(line[10:20])
        z = float(line[20:30])
    except ValueError as exc:
        raise LoadError("Could not read the coordinates of an atom.", lit) from exc
    symbol = line[31:34].strip()
    atnum = sym2num.get(symbol.title())
    if atnum is None:
        raise LoadError(f"Unknown element symbol: '{symbol}'.", lit)
    return x, y, z, atnum


def _parse_bond(line: str, lit: LineIterator) -> tuple[int, int, int]:
    """Parse a bond line, which has fixed-width fields."""
    try:
        return int(line[0:3]), int(line[3:6]), int(line[6:9])
    except ValueError as exc:
        raise LoadError("Could not read a bond.", lit) from exc


[docs] @document_load_one("SDF", ["atcoords", "atnums", "bonds", "title"]) def load_one(lit: LineIterator) -> dict: """Do not edit this docstring. It will be overwritten.""" title = next(lit).strip() # The next two lines are general comments next(lit) next(lit) # The V2000 format has fixed-width fields, which may touch each other, e.g. for # molecules with 100 or more atoms. They are read by column, not by splitting # at whitespace. natom, nbond = _parse_counts(next(lit), lit) atcoords = np.empty((natom, 3), float) atnums = np.empty(natom, int) for iatom in range(natom): x, y, z, atnum = _parse_atom(next(lit), lit) atcoords[iatom] = np.array([x, y, z]) * angstrom atnums[iatom] = atnum bonds = np.empty((nbond, 3), int) for ibond in range(nbond): iatom0, iatom1, bondtype = _parse_bond(next(lit), lit) bonds[ibond, 0] = iatom0 - 1 bonds[ibond, 1] = iatom1 - 1 # Bond types 1 to 8 (inclusive) are defined in the SDF format. # Anything outside that range is not modified, just not to lose any # information, but could be potentially meaningless. bonds[ibond, 2] = bondtype while True: try: words = next(lit) except StopIteration as exc: raise LoadError("Molecule specification did not end properly with $$$$.", lit) from exc # The last line of the file may have no line break and lines may have trailing spaces. if words.rstrip() == "$$$$": break return { "title": title, "atcoords": atcoords, "atnums": atnums, "bonds": bonds, }
[docs] @document_load_many("SDF", ["atcoords", "atnums", "bonds", "title"]) def load_many(lit: LineIterator) -> Iterator[dict]: """Do not edit this docstring. It will be overwritten.""" # SDF files with more molecules are a simple concatenation of individual SDF files,' # making it travial to load many frames. try: while True: yield load_one(lit) except StopIteration: return
[docs] def prepare_dump(data: IOData, allow_changes: bool, filename: str) -> IOData: """Check the compatibility of the IOData object with the SDF format. Parameters ---------- data The IOData instance to be checked. allow_changes Whether conversion of the IOData object to a compatible form is allowed or not. filename The file to be written to, only used for error messages. Returns ------- data The given ``IOData`` object, or a copy with a title on a single line. Raises ------ PrepareDumpError If the title has more than one line and ``allow_changes`` is False, if the molecule has more atoms or bonds than the three-digit fields of the SDF V2000 format can hold, or if a coordinate does not fit in the fields of ten characters (from -9999.9999 Å to 99999.9999 Å). """ if data.natom > 999: raise PrepareDumpError("The SDF format supports at most 999 atoms.", filename) if data.bonds is not None and len(data.bonds) > 999: raise PrepareDumpError("The SDF format supports at most 999 bonds.", filename) if data.natom > 0: # Round as in the dumper, so values just outside the range are also caught. atcoords = np.round(data.atcoords / angstrom, 4) if atcoords.min() <= -1e4 or atcoords.max() >= 1e5: raise PrepareDumpError( "The SDF format only supports coordinates from -9999.9999 Å to 99999.9999 Å.", filename, ) return prepare_single_line_title(data, allow_changes, filename, "SDF")
[docs] @document_dump_one("SDF", ["atcoords", "atnums"], ["title", "bonds"]) def dump_one(f: TextIO, data: IOData): """Do not edit this docstring. It will be overwritten.""" print(data.title or "Created with IOData", file=f) print("", file=f) print("", file=f) nbond = 0 if data.bonds is None else len(data.bonds) print(f"{data.natom:3d}{nbond:3d} 0 0 0 0 0 0 0999 V2000", file=f) for iatom in range(data.natom): n = num2sym[data.atnums[iatom]] x, y, z = data.atcoords[iatom] / angstrom print(f"{x:10.4f}{y:10.4f}{z:10.4f} {n:<3s} 0 0 0 0 0 0 0 0 0 0 0 0", file=f) if data.bonds is not None: for iatom, jatom, bondtype in data.bonds: print(f"{iatom + 1:3d}{jatom + 1:3d}{bondtype:3d} 0 0 0 0", file=f) print("M END", file=f) print("$$$$", file=f)
[docs] @document_dump_many("SDF", ["atcoords", "atnums"], ["title", "bonds"]) def dump_many(f: TextIO, datas: Iterator[IOData]): """Do not edit this docstring. It will be overwritten.""" # Similar to load_many, this is relatively easy. for data in datas: dump_one(f, data)