I'm new to this rdkit, below is the code that I'm using to get the chemical image from the formula,
from rdkit import Chem
m = Chem.MolFromSmiles('OCC1OC(C(C(C1O)O)O)[C]1(C)(CO)CC(=O)C=C(C1CCC(=O)C)C')
m
if the code is correct, it displays the structre. The above code displays the error saying
"[15:23:55] Explicit valence for atom # 11 C, 5, is greater than permitted"
I tried adding try catch but, it does not help.
I need to catch this exception/Message and display it to the user.
How can we do this?Issue Image Here
try:
from rdkit import Chem
m = Chem.MolFromSmiles('OCC1OC(C(C(C1O)O)O)[C]1(C)(CO)CC(=O)C=C(C1CCC(=O)C)C')
m
except Exception as e:
print(e)
It is not an Error, it is a Warning. Your code does not break doing that assignment. So there is no Error to catch.
But your solution lays within the return of Chem.MolFromSmiles
. If it fails to build a mol object of your SMILES it returns None
, whereas it returns the mol object when it manages to "deal with the SMILES properly".
For sure you can raise an error yourself!
from rdkit import Chem
m = Chem.MolFromSmiles('OCC1OC(C(C(C1O)O)O)[C]1(C)(CO)CC(=O)C=C(C1CCC(=O)C)C')
if m is None:
print("hey user, there was an error!")
# or even raise an error:
if m is None:
raise ValueError("Could not interpret SMILES to mol object")
I know my answer is lacking. My solution is not forwarding the warning to the user. I feel like it is quite hard to do. If there is a solution it might be found utilizing logging
or warnings
package. I would also be interested in that but couldn't manage so far myself.