Hello! I found your library very helpful in parsing SMILES.
Would you be interested in adding MF and MW as additional attributes?
Something along these lines:
from collections import default_dict
from pysmiles import read_smiles
AW = {
'C': 12.0107,
'H': 1.00794,
# etc.
}
class MolecularFormula:
def __init__(self, smiles: str):
self.smiles = smiles
self.mf = defaultdict(lambda: 0)
try:
mol = read_smiles(
smiles,
explicit_hydrogen=False,
reinterpret_aromatic=False,
)
nodes = mol.nodes()
for i in range(mol.number_of_nodes()):
self.mf[nodes[i]['element']] += 1
self.mf['H'] += nodes[i]['hcount']
self.mw = 0
for k, v in self.mf.items():
self.mw += AW[k] * v
self.mw = round(self.mw, 2)
except Exception as e:
# log or raise
self.mw = 0
def __repr__(self):
return ''.join([str(k)+str(v) for k,v in self.mf.items()])
Hello! I found your library very helpful in parsing SMILES.
Would you be interested in adding MF and MW as additional attributes?
Something along these lines: