Load your CIF file in PLATON and run CALC-GEO
In the *.LIS
file generated, you'll find all the tables that you need, namely
- Bond Lengths (Angstrom)
- Bond/Valence Angles (Degrees)
- Torsion/Dihedral Angles (Deg.)
You can still use the regex
module of Python to parse the data,\s*(?P<atoms>([A-Z][a-z]?\(\d*\w\)\s*){4})\s*(?P<value>-?\d*\.\d\(\d+\))
will split entries in the torsion table into two named groups, which you can further process and write to a CSV file.
EDIT
I think that OpenBabel is an excellent tool! In this case, however, I suggest to use the approach above, rather than parsing the OpenBabel report file for the following reasons:
- In a couple of cases, I noticed that the report generator of OpenBabel had some problems with CIF files that were checked both using the internal check routines of PLATON, as well as CheckCIF, an online tool provided by the IUCr, and were found to be valid.
- In the table for the dihedral angles, the OpenBabel report does not use descriptors like
C(5), H(10A)
to denote atoms in a dihedral, but uses the atom indices. If you would want to write the decriptors to a CSV file, you would have to look them up at another table.
If you can live with the indices, the following Python snippet should suffice:
#!/usr/bin/env python3
import csv
import argparse
def get_torsions_from_report(report_path):
'''Read all rows with he indices to four consecutive atoms and
the corresponding dihedral angle from an OpenBabel report file.'''
try:
torsion_list = []
with open(report_path) as infile:
for line in infile:
if line.startswith('TORSION ANGLES'):
break
for line in infile:
if line.strip():
torsion_list.append(line.split())
return torsion_list
except IOError:
print(e)
def write_torsions_to_csv(csv_path, torsion_list):
'''Write torsion data to CSV.'''
with open(csv_path, 'wt') as fout:
writer = csv.writer(fout)
writer.writerow(('Atom1', 'Atom 2', 'Atom 3', 'Atom 4', 'Torsion'))
writer.writerows(torsion_list)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Read torsions
from OpenBabel report and write them to a CSV file.')
parser.add_argument('-i', dest="infile", required=True,
help="OpenBabel report file")
parser.add_argument('-o', dest="outfile", required=True,
help="CSV file")
args = parser.parse_args()
torsions = get_torsions_from_report(args.infile)
write_torsions_to_csv(args.outfile, torsions)