# What is the best way to convert SMILES strings to file names?

I would like to convert SMILES strings into file names. There are many characters to avoid in file names, i.e. \/:*?"<>| or maybe even []()^ #%&!@:+={}'~ (see discussion) for convenience. I would like to escape such characters that appear in SMILES. However, I could not see a list of allowed characters for SMILES to decide on how to escape them. There might be also some existing convention to do this for databases, so I am all ears for any help.

An alternative is using InChIkeys but since they are not human readable, I want to avoid that option.

Edit: I am mainly interested in small organic molecules with less than 20 atoms.

• Are the SMILES strings not going to make awfully long file names for anything but the most trivial of molecules? Long file names might be allowed now, but can cause issues with some software / systems May 12 '17 at 18:28
• I am mainly interested in small organic molecules, (less than 20 atoms) so length won't be a problem. Thanks for pointing it out, I'll edit the question to specify it. May 12 '17 at 18:45
• So you like to filter out, char wise, all but 0-9 and A-Z, and collapse this string -- with the examples on the site given by you and on en.wikipedia.org/wiki/…, it sounds like a suitable question for the code golfers (codegolf.stackexchange.com). Awk, grep ... May 12 '17 at 18:58
• InChI keys are more appropriate, since they are in fact hashes. But there is always many ways how to repesent single structure in SMILES string, and there are also multiple ways how to normalize those strings. May 12 '17 at 20:53
• Maybe a better question to ask is: why are you doing this?
– Zhe
May 12 '17 at 21:36

I personally find SMILES quite unreadable, even for only small compounds. I think they are even less readable when performing substitutions. Given the vast amount of special characters they use, I do not think they are a good way to create a naming scheme.

An alternative is using InChIkeys but since they are not human readable, I want to avoid that option.

While that is true, InChI strings are human readable, especially for small molecules. In principle only the layers need to be separated by something other than / (underscores would do that).

Other problematic characters are *;?+() which do not fit in [0-9a-zA-Z-.,_], and you would have to find ways around them when using in scripts.

I am mainly interested in small organic molecules with less than 20 atoms.

If you are really only interested in molecules with less than 20 atoms, general nomenclature should be the easiest to comprehend, and should yield reasonably short file names. For these you probably only have to worry about spaces.

• The question is about SMILES. Dec 5 '17 at 15:15
• @NoelO'Boyle Exactly, that's why I am writing that SMILES is a bad way to generate filenames and then offer alternatives. Dec 5 '17 at 15:16

From my experience, the only characters that have been a problem are '/', '\', and '#'. I replace them with 'z', 'x', '^' respectively. It's not a convention but just something I've come up with. If you were to use the file name in a command, '(', ')', '\$' would also need to be escaped.

I've used base64 encoding in the past for escaping SMILES strings for use as filenames.

• I might be wrong here, but that is equally unreadable as an InChIkey, and it doesn't even have a fixed length. Otherwise I don't understand your answer (which is more like a comment to begin with). Dec 5 '17 at 9:41
• I read the original question as the OP looking for a way of escaping the SMILES string for use as a filename. For sure, base64 encoding is not human readable. Dec 5 '17 at 15:12

Use the PubChem Compound ID (CID) to name your files. You'll be able to name your files with numbers containing, at most, 9 digits. With over 100 million compounds in PubChem, your compounds are likely to be present.

Download the CID-SMILES.gz file from the PubChem FTP Server. It contains 2 columns: CID and the isomeric SMILES, separated by a tab.