Coverage for /builds/kinetik161/ase/ase/io/sdf.py: 100.00%
17 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-10 11:04 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-10 11:04 +0000
1"""Reads chemical data in SDF format (wraps the molfile format).
3See https://en.wikipedia.org/wiki/Chemical_table_file#SDF
4"""
5from typing import TextIO
7from ase.atoms import Atoms
8from ase.utils import reader
11def get_num_atoms_sdf_v2000(first_line: str) -> int:
12 """Parse the first line extracting the number of atoms.
14 The V2000 dialect uses a fixed field length of 3, which means there
15 won't be space between the numbers if there are 100+ atoms, and
16 the format doesn't support 1000+ atoms at all.
18 http://biotech.fyicenter.com/1000024_SDF_File_Format_Specification.html
19 """
20 return int(first_line[0:3]) # first three characters
23@reader
24def read_sdf(file_obj: TextIO) -> Atoms:
25 """Read the sdf data and compose the corresponding Atoms object."""
26 lines = file_obj.readlines()
27 # first three lines header
28 del lines[:3]
30 num_atoms = get_num_atoms_sdf_v2000(lines.pop(0))
31 positions = []
32 symbols = []
33 for line in lines[:num_atoms]:
34 x, y, z, symbol = line.split()[:4]
35 symbols.append(symbol)
36 positions.append((float(x), float(y), float(z)))
37 return Atoms(symbols=symbols, positions=positions)