From 613dc0196247cf5bd2c7c844bb5bac99772c914f Mon Sep 17 00:00:00 2001 From: ffmulks <63452434+ffmulks@users.noreply.github.com> Date: Fri, 4 Feb 2022 14:32:22 -0800 Subject: [PATCH] Funct read_xyz_file expanded Now can also work with Tinker xyz files containing connectivity information (simply dropping it), with xyz files without title line, and with files with leading atomic number before atomic symbols. --- xyz2mol.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/xyz2mol.py b/xyz2mol.py index c9cd5c2..415a6d0 100644 --- a/xyz2mol.py +++ b/xyz2mol.py @@ -529,8 +529,16 @@ def get_proto_mol(atoms): return mol -def read_xyz_file(filename, look_for_charge=True): +def read_xyz_file(filename): """ + Takes xyz file. Also accept xyz files without title lines and + xyz files containing connectivity info (Tinker format) as well + as ones which have leading atomic numbers before the atomic symbols. + + args: + filename for xyz file + returns: + RDKit mol object """ atomic_symbols = [] @@ -542,12 +550,26 @@ def read_xyz_file(filename, look_for_charge=True): for line_number, line in enumerate(file): if line_number == 0: num_atoms = int(line) - elif line_number == 1: + elif line_number == 1 and len(line.split()) < 4: title = line if "charge=" in line: charge = int(line.split("=")[1]) + elif line_number == 1 and len(line.split()) >= 4 and not \ + ((line.split()[0].strip('-')[0].isdigit() and \ + line.split()[2].strip('-')[0].isdigit() and \ + line.split()[3].strip('-')[0].isdigit()) or \ + (line.split()[1].strip('-')[0].isdigit() and \ + line.split()[2].strip('-')[0].isdigit() and \ + line.split()[3].strip('-')[0].isdigit())): + title = line + if "charge=" in line: + charge = int(line.split("=")[1]) + elif line.split()[0].isnumeric(): + atomic_symbol, x, y, z = line.split()[1:5] + atomic_symbols.append(atomic_symbol) + xyz_coordinates.append([float(x), float(y), float(z)]) else: - atomic_symbol, x, y, z = line.split() + atomic_symbol, x, y, z = line.split()[0:4] atomic_symbols.append(atomic_symbol) xyz_coordinates.append([float(x), float(y), float(z)]) @@ -555,7 +577,6 @@ def read_xyz_file(filename, look_for_charge=True): return atoms, charge, xyz_coordinates - def xyz2AC(atoms, xyz, charge, use_huckel=False): """