Aniket Schneider
Aniket Schneider

Reputation: 934

Can I capture warning messages during format conversion in pybel?

I have a list of molecules in SMILES format that I am converting to InChIKey format. However, a number of them are generating warnings. I would like to be able to generate a file showing the warnings generated in the process of converting each molecule, so that a team member can review them. Is it possible to capture these messages programmatically, instead of just logging them?

A snippet illustrating what I'd like to be able to do:

import pybel

smiles = pybel.readfile('smi', 'smiles.txt')
converted = []
for mol in smiles:
    smiles_str = mol.write('smiles')
    inchikey_str = mol.write('inchikey')
    warnings = None # Is there something I can do here to capture the warnings?
    converted.append((smiles_str, inchikey_str, warnings))

Upvotes: 2

Views: 125

Answers (1)

M.Vu
M.Vu

Reputation: 488

After researching many sources such as chemistry and github and stackoverflow, I have not found a way to add codes to the exist script in order to redirect pybel warnings to a log file. However, I can provide you a possible solution here by using script. By modifying your code:

import pybel

smiles = pybel.readfile('smi', 'smiles.txt')
converted = []
for mol in smiles:
    print(mol)
    smiles_str = mol.write('smiles')
    inchikey_str = mol.write('inchikey')

with a command run:

script -c "python smiles2sdf.py" log.txt

It will output everything shown on terminal during executing to log.txt. This feature makes us be careful to choose what will be appeared on screen.

Further information about this package is here.

Hope this help.

p.s: I also want to capture pybel warnings programmatically. If there is a possible method to do that, please provide me. Thank you very much.

Upvotes: 1

Related Questions