How do we represent a molecule in text form?

How do we represent a molecule in text form?

A molecule, as a computer scientist I see it as a connected graph of atoms. A connected graph means that beginning at any atom, there is a path to any other atom in the graph.

The interesting question is, how do we represent these as text?

Specific questions are:

1) Is $\ce{H2O}$ different than $\ce{OH2}$? That is, does order matter, or what does order mean?

2) What does this mean $\ce{NH4NO3}$? Why do we write N two times? Is this the same thing $\ce{NNH4O3}$?

The representation of a molecule in text form actually has a name: the chemical formula (pl. formulae).

The formula we use for everyday purposes is an empirical formula. That article should give you more information, but in a nutshell, it is a simple way of identifying a compound by the ratio of its constituent elements. Since ionic compounds and other compounds such as macromolecules can have huge absolute numbers of atoms which are technically bonded but in practice are subdivided into their simplest components, empirical formulae are useful for unambiguously identifying most compounds. Note, however, that we run into trouble when talking about isomers or other compounds with the same elemental ratio but different structures.

For these compounds, we must disambiguate by using a structural formula, which is basically a graph of the molecule. Translating this into text can be done many different ways, using a formal system for specifying branches and stereochemistry. The three most widely used formats for chemical database applications are SMILES, PDB, and CML. You can get more information on all these from Open Babel (alternately OpenBabel).

There is also a middle ground, using a formula similar to SMILES, variously called a molecular formula or condensed formula. It implies structure by counting every atom and splitting individual elements apart to show functional groups like hydroxyl ($\ce{-OH}$) or amino ($\ce{-NH2}$) bonded to the main structure.

I think this is what your question is really about. Yes, in a condensed formula, the order of elements and functional groups matters, because you are trying to communicate information about a compound's structure with a minimum of overhead. It wouldn't make sense to say that butyraldehyde is $\ce{C2H4CHOCH3}$ because the formyl group is at the end. Therefore we say butyraldehyde is $\ce{CH3(CH2)2CHO}$ to better represent the compound's structure. This helps distinguish isomers (but not stereoisomers). Even using the Hill system, one must still be careful.

I hope this clears things up for you on how chemists use different formulae in different situations, and how a condensed formula works.

There are many text representations of molecules. SMILES is commonly used but has issues in representing stereochemistry, to the MDL (now Accelrys) CTAB format, to InChi.

Certainly a molecule can be represented as a graph, in which the atom are nodes and the bonds edges.

In terms of the molecular formulae you asked about, for a simple molecule like water, a chemist would recognise H2O and OH2 as the same thing, but things quickly get out of hand when more complex molecules are represented in this fashion, and order becomes important. Any chemist would recognise NH4NO3 as ammonium nitrate, but may fail to immediately see it when represented as N2H4O3. We write the N twice because it makes the context easier to determine - ie that this is a salt, comprised of the ammonium anion and the nitrate cation.

I'm afraid that there are a number of contextual complexities when approaching cheminformatics from the viewpoint of a computer scientist - a knowledge of chemistry is just as important :)

• I disagree with the comment that SMILES has issues in representing stereochemistry. By definition, SMILES exactly represents stereochemistry. That doesn't mean all software correctly handles it, but SMILES can specify all types of stereo information. – Geoff Hutchison Sep 15 '14 at 17:53
• Agreed, however there exists contextual complexity that SMILES cannot handle. For example, the drug thalidomide, which exists as a racemic mixture. How to represent that ? If drawn 'flat', ie no stereo representation, is the racemate assumed ? If so, was it meant ? If so, was it 50:50 ? 60:40 ? What about chirality brought about by conformational constraint ? I guess my comment added more complexity than was required by the answer to the OP :) – Drew Gibson Oct 9 '14 at 19:49
• I think my comment was mainly aimed at indicating your answer needed to be more specific. You can represent stereochemistry in SMILES. I completely agree though that there are vast areas of chemical complexity which are hard to represent computationally, and EE and racemates are good examples. (I'm not sure there exists any format which can represent all of chemistry. Organometallics are a specific personal gripe.) – Geoff Hutchison Oct 9 '14 at 19:58

Correct me If I am wrong and also I am not sure that this answers your full question or not as it only provides an answer to your specific questions.

Here are some rules for writing chemical formulas. Kindly read them first.

1) Is $\ce{H2O}$ different than $\ce{OH2}$? That is, does order matter, or what does order mean?

Literally in these type of molecules, it doesn't matter that you write it as $\ce{H2O}$ or $\ce{OH2}$ because it will have no effect on its structure. These type of molecules follow Hill System

2) What does this mean $\ce{NH4NO3}$? Why do we write N two times? Is this the same thing $\ce{NNH4O3}$?

In this case, $\ce{NH4+}$ (Ammonium) and $\ce{NO3-}$ (Nitrate) are two different ions which makes $\ce{NH4NO3}$. In these type of compounds, cation is written first and then the anion. In the formation of Ammonium Nitrate, $\ce{NH3}$ reacts with $\ce{HNO3}$ and then cation & anion formation takes place and reaction proceeds.

So basically these two N in ammonium nitrate came from two different molecules. By 'from different molecules' I mean that each N is bonded with different atoms, this is why we need to write it twice.

And I don't think that $\ce{NNH4O3}$ is the same thing. (I am not sure about it so kindly correct me if I am wrong here. and leave a comment with explaination.)

Note: As commented by @F'x, the order of elements in a chemical formula is a complex matter. See this and this, there are some good answers as mentioned by @F'x. I also found them useful.

• Regarding your first answer (“cation is written first”): there is no cation in H2O. The order of elements in formula is a complex matter: see there, there and this other excellent answer – F'x Sep 21 '13 at 19:47
• thanks for the beautiful knowledge. I also never thought about CH3 and H2O. I didn't knew about the information provided in answer. :) and I have edited my answer now. Kindly review it and edit if anything is wrong.! – ashu Sep 21 '13 at 20:08