Open liquidcarbon opened 3 years ago
Happy to hear you find the library helpful :)
I'm not quite sure whether the MolecularFormula
is worth adding to the library. If anything I'd make a Molecule
(subclass of nx.Graph
) and give that a molecular_formula
attribute/property. But I'm not sure it's worth the hassle/complication. Generating the MF should be pretty straightforward anyway (collections.Counter(nx.get_node_attributes(mol, 'element').values())
, and sum(nx.get_node_attributes(mol, 'hcount').values())
, will get you 90% of the way).
Adding a function that calculates a molecular_weight
would not be too much work, and may be valuable to numerous people. However, I'd have to find a periodic table library that's easy to install somewhere. No point in maintaining that as well...
Thanks for the tip on nx.get_node_attributes
! My implementation appears to be about 25% faster than through nx
.
For periodic table you only need a dictionary of atomic weights (if you ignore isotopes, which I would). You get one like so:
ELEMENTS_URL = \
'https://raw.githubusercontent.com/bokeh/bokeh/branch-2.4/bokeh/sampledata/_data/elements.csv'
df = pd.read_csv(ELEMENTS_URL)
df = df[~df['atomic mass'].str.contains('\[')] # ignore radioactive elements
AW = df.set_index('symbol')['atomic mass'].astype(float).to_dict()
My implementation appears to be about 25% faster than through nx.
I loop over the molecule twice, once for the mass, and once for the hcount, rather than getting both at the same time.
I find pulling data from a network connection rather impolite for a library though, so I'd much rather add a dependency on a lightweight periodic table module.
Of course, I'm not suggesting to execute it every time someone imports. This is just a way to retrieve data. I hard-coded the dictionary into my module that does MW calculation.
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: