# Python program that returns elements from a SMILE string

Given a SMILES string for a molecule, I want to know the elements in the molecule, I then send this to basis_set_exchange to get a "custom" basis set for use in Gaussian.

This seems like something that RDKit can do, or PubChem.

I do know that given a molecular formula, like "H2O" molmass can return the elements. So, it would also suffice to have a program that simply takes a SMILES string and returns a molecular formula, then use molmass to get the elements.

However, this seems like such a trivial exercise, it must already be done. I don't want to make my own script for converting a SMILES to its elements just because I don't want to have to worry about corner cases or reinvent a wheel.

• +1 because, Charlie Crown is back! By the way, did you ever visit the Gaussian chat room? Mar 27 '21 at 5:09
• Now I did, I liked the historical context in that question. Personally, I am going to try psi4 since it is python friendly Mar 27 '21 at 5:13
• is smiles ID supposed to mean smiles string? I've never heard of a smiles ID Mar 27 '21 at 6:06
• Probably, I may be misusing the term ID in this case. "CCCCCO" is the SMILES for pentanol, for instance Mar 27 '21 at 6:09
• haha, sorry didn't mean to make you feel bad ;) was just confused. Mar 27 '21 at 6:13

## RDKit

This is pretty easy to do in RDKit. If you want the molecular formula, you can just use CalcMolFormula():

from rdkit import Chem
from rdkit.Chem.rdMolDescriptors import CalcMolFormula

# e.g. cysteine
mol = Chem.MolFromSmiles("C([C@@H](C(=O)O)N)S")
formula = CalcMolFormula(mol)


It is also very easy to get all of the unique heteroatoms:

from rdkit import Chem

# e.g. cysteine
mol = Chem.MolFromSmiles("C([C@@H](C(=O)O)N)S")
hetatms = {atom.GetSymbol() for atom in mol.GetAtoms()}


## Pybel

import pybel
global __ATOM_LIST__
__ATOM_LIST__ = \
['h',  'he',
'li', 'be', 'b',  'c',  'n',  'o',  'f',  'ne',
'na', 'mg', 'al', 'si', 'p',  's',  'cl', 'ar',
'k',  'ca', 'sc', 'ti', 'v ', 'cr', 'mn', 'fe', 'co', 'ni', 'cu',
'zn', 'ga', 'ge', 'as', 'se', 'br', 'kr',
'rb', 'sr', 'y',  'zr', 'nb', 'mo', 'tc', 'ru', 'rh', 'pd', 'ag',
'cd', 'in', 'sn', 'sb', 'te', 'i',  'xe',
'cs', 'ba', 'la', 'ce', 'pr', 'nd', 'pm', 'sm', 'eu', 'gd', 'tb', 'dy',
'ho', 'er', 'tm', 'yb', 'lu', 'hf', 'ta', 'w',  're', 'os', 'ir', 'pt',
'au', 'hg', 'tl', 'pb', 'bi', 'po', 'at', 'rn',
'fr', 'ra', 'ac', 'th', 'pa', 'u',  'np', 'pu']


• Oof, looks good but pybel doesn't have readstring... turns out I need to install pip install openbabel not pip install pybel...turns out openbabel doesn't want to install, turns out it may be python2? python... so much ductape :) will try in the morning, need sleep Mar 27 '21 at 7:14
• You want from openbabel import pybel for OB v3. Mar 28 '21 at 17:32
• As far as the "other pybel" - there's not much that can be done. I use conda, so conda install -c conda-forge openbabel Mar 28 '21 at 17:34