Dear Colleagues, I hope you are all safe and well at this extraordinary time.
This is a follow-up to messages I sent to the list in late 2013 and early 2014 regarding an old DOS-based program 'SMOG' (see CCL archives http://www.ccl.net/cca/software/MS-DOS/SMOG/index.shtml and http://pubs.acs.org/doi/abs/10.1021/ci950393z) that generates isomers given a molecular formula and a set of structural constraints. Although the output is in the form of a connection table it was not understood by other applications I was aware of and I thought OpenBabel might work. However, at the time this was not an option and Noel kindly provided me with a Python script (smog.py - attached as a txt file) that took the SMOG output and generated SMILES. I believe there was an intention to include this in future versions of Open Babel but I appreciate there are many calls on time and I don't think this has yet happened. Fast forwarding seven years or so, during which my attention was taken by other duties, I have now returned to working on the additional SMOG output I have available and I naively thought I would be able to install current versions of Open Babel and Python, fire up the script and everything would work as before. Unfortunately, things didn't go quite to plan and the following is what happened. I Installed Anaconda Powershell and OpenBabel 3.0.0 and, I think, the bindings ('pip install -U openbabel') correctly. I ran through the commands given in the 'Test the installation' box on https://open-babel.readthedocs.io/en/latest/UseTheLibrary/PythonInstall.html and obtained a structure in a separate window so it seemed that things were working okay. I then ran smog.py as attached and got the following: (base) PS C:\users\anthony\anaconda2> python smog.py myfile.res myoutputfile.smi Traceback (most recent call last): File "smog.py", line 5, in <module> import pybel ImportError: No module named pybel Having looked at https://open-babel.readthedocs.io/en/latest/UseTheLibrary/PythonInstall.html again and also the 'Updating to Open Babel 3.0 from 2.x' page (https://open-babel.readthedocs.io/en/latest/UseTheLibrary/migration.html), I changed line 5 in smog.py from 'import pybel' to 'from openbabel import pybel' and ran smog.py again with the following result: (base) PS C:\users\anthony\anaconda2> python smog.py myfile.res myoutputfile.smi Traceback (most recent call last): File "smog.py", line 60, in <module> for mol in readSMOG(options['inputfile']): File "smog.py", line 41, in readSMOG yield smog2mol(lines) File "smog.py", line 16, in smog2mol atm.SetAtomicNum(ob.cvar.etab.GetAtomicNum(element)) AttributeError: Unknown C global variable 'etab' At this point I am well beyond my capabilities but I assume there have been changes since version 2.x'ish that I used in the past and the global variable 'etab' and is absent. I did find 'etab' defined on line 468 at https://github.com/openbabel/documentation/blob/master/pybel.py (dated 14 Oct 2012) but I don't know what to do with this information! I guess this is a similar issue to one posted to the list at the end of May and I'm hoping that someone can provide some assistance and, if it's possible, show me how I can get the smog.py working again. Maybe this is my chance to learn Python after all the times I've said I would... My apologies for the lengthy post and many thanks for any assistance received, Anthony C. Anthony Lewis Petroleum & Environmental Geochemistry Group & Centre for Chemical Sciences, School of Geography, Earth and Environmental Sciences, Plymouth University, Plymouth, Devon PL4 8AA, U.K. tel: +44 (0)1752 584554 email: cale...@plymouth.ac.uk<mailto:cale...@plymouth.ac.uk> web: http://www.pegg.org.uk<http://www.pegg.org.uk/> http://www.plymouth.ac.uk/chemistry ________________________________ [http://www.plymouth.ac.uk/images/email_footer.gif]<http://www.plymouth.ac.uk/worldclass> This email and any files with it are confidential and intended solely for the use of the recipient to whom it is addressed. If you are not the intended recipient then copying, distribution or other use of the information contained is strictly prohibited and you should not rely on it. If you have received this email in error please let the sender know immediately and delete it from your system(s). Internet emails are not necessarily secure. While we take every care, University of Plymouth accepts no responsibility for viruses and it is your responsibility to scan emails and their attachments. University of Plymouth does not accept responsibility for any changes made after it was sent. Nothing in this email or its attachments constitutes an order for goods or services unless accompanied by an official order form.
from __future__ import print_function import os import sys import pybel ob = pybel.ob def smog2mol(lines): mol = ob.OBMol() for line in lines: atmnum = int(line[:3]) element = line[6:10].strip() bonds = line[23:] if atmnum > 0: atm = ob.OBAtom() atm.SetAtomicNum(ob.cvar.etab.GetAtomicNum(element)) mol.AddAtom(atm) for i, b in enumerate(bonds): if int(b): mol.AddBond(mol.NumAtoms(), i+1, int(b)) else: for i, n in enumerate(bonds): for j in range(int(n)): atm = ob.OBAtom() atm.SetAtomicNum(ob.cvar.etab.GetAtomicNum(element)) mol.AddAtom(atm) mol.AddBond(mol.NumAtoms(), i+1, 1) return mol def readSMOG(filename): with open(filename) as f: for line in f: if line.startswith("Isomer"): lines = [] while True: temp = f.next().strip() if not temp: break lines.append(temp) yield smog2mol(lines) def printhelp(): print("python smog.py inputfile outputfile") sys.exit(1) def handleargs(args): options = {} if len(args) != 3: printhelp() options['inputfile'] = sys.argv[1] if not os.path.isfile(sys.argv[1]): printhelp() options['outputfile'] = sys.argv[2] return options if __name__ == "__main__": options = handleargs(sys.argv) output = pybel.Outputfile("smi", options['outputfile'], overwrite=True) for mol in readSMOG(options['inputfile']): m = pybel.Molecule(mol) m.removeh() output.write(m) output.close()
_______________________________________________ OpenBabel-discuss mailing list OpenBabel-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbabel-discuss