Coverage for /builds/ase/ase/ase/io/sdf.py : 100.00%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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."""
13 return int(first_line[0:3]) # first three characters
14 # http://biotech.fyicenter.com/1000024_SDF_File_Format_Specification.html
17@reader
18def read_sdf(file_obj: TextIO) -> Atoms:
19 """Read the sdf data and compose the corresponding Atoms object."""
20 lines = file_obj.readlines()
21 # first three lines header
22 del lines[:3]
24 num_atoms = get_num_atoms_sdf_v2000(lines.pop(0))
25 positions = []
26 symbols = []
27 for line in lines[:num_atoms]:
28 x, y, z, symbol = line.split()[:4]
29 symbols.append(symbol)
30 positions.append((float(x), float(y), float(z)))
31 return Atoms(symbols=symbols, positions=positions)