cgohlke / molmass

Molecular mass calculations
https://pypi.org/project/molmass
BSD 3-Clause "New" or "Revised" License
56 stars 13 forks source link

How to process the formula containing '-'? #12

Closed JosieHong closed 1 year ago

JosieHong commented 1 year ago

I tried to convert this SMILES string to a formula by following codes:

from rdkit import Chem
from rdkit.Chem.rdMolDescriptors import CalcMolFormula
from molmass import Formula

s = '[O-]C(CCNC1=CC(N2CCC3=CC=CC=C3CC2)=NC(C4=CC=CN=C4)=N1)=O'
f = CalcMolFormula(Chem.MolFromSmiles(s))
f = Formula(f)

Then I met the following error:

molmass.molmass.FormulaError: unexpected character -'
C22H22N5O2-
..........^

Is there a way to process this type of formula, which contains '-'? Thanks!

cgohlke commented 1 year ago

Please try the latest version of molmass, v2022.12.9:

>>> from rdkit import Chem
>>> from rdkit.Chem.rdMolDescriptors import CalcMolFormula
>>> from molmass import Formula
>>>
>>> s = '[O-]C(CCNC1=CC(N2CCC3=CC=CC=C3CC2)=NC(C4=CC=CN=C4)=N1)=O'
>>> f = CalcMolFormula(Chem.MolFromSmiles(s))
>>> Formula(f)
Formula('[C22H22N5O2]-')
cgohlke commented 1 year ago

Molmass v2023.4.10 supports rdkit-style ionic charges. See also #11.