-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxyz2sdf.py
executable file
·47 lines (26 loc) · 1.01 KB
/
xyz2sdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python2
import sys
import copy
import pybel
import openbabel
from correct_sdf import *
if __name__ == "__main__":
nbo_filename = sys.argv[1]
xyz_filename = sys.argv[2]
bonds, charges, total_charge = get_bonds_nbo(nbo_filename)
mol = pybel.readfile("xyz", xyz_filename).next()
mol = delete_bonds_from_mol(mol)
mol_corrected = add_bonds_to_mol(mol, bonds)
total_charge_check = 0
for i, atom in enumerate(mol):
nuc = atom.OBAtom.GetAtomicNum()
formal_charge = nuc - charges[i]
total_charge_check += formal_charge
atom.OBAtom.SetFormalCharge(formal_charge)
atom.OBAtom.SetSpinMultiplicity(0)
# print i+1, nuc, charges[i], atom.formalcharge
# print total_charge, total_charge_check
assert (total_charge == total_charge_check)
mol.OBMol.SetTotalCharge(total_charge)
outfilename = xyz_filename.rstrip(".xyz") + "_corrected.sdf"
mol.write("sdf", outfilename, overwrite=True)