On Feb 23, 2009, at 4:48 PM, Greg Landrum wrote:
Somehow it has happened that the RDKit does not have a function to
generate the chemical formula for a molecule, so one would need to
write it from scratch. Here's a simple (and relatively untested) way
of doing this:

Ideally it would generate the Hill formula,

  ks = cnts.keys()
  ks.sort()
  res=''
  for k in ks:
    res+=k
    if cnts[k]>1:
      res+=str(cnts[k])

would be more like:

  ks = cnts.keys()
  # Alphabetize everything
  ks.sort()

  # Put into Hill order (C then H then everything else)
  if "C" in cnts:
    ks.remove("C")
    ks.insert(0, "C")
    if "H" in cnts:
      ks.remove("H")
      ks.insert(1, "H")

  ...

There are other solutions which are more efficient for large N, but there's only about 100 elements in the world, and only a handful in most compounds.


                                Andrew
                                [email protected]



Reply via email to