code: public static IMolecularFormula getMolecularFormula(IAtomContainer atomContainer, IMolecularFormula formula) {
          int charge = 0;
          int hcnt   = 0;
          for (IAtom iAtom : atomContainer.atoms()) {
              formula.addIsotope(iAtom);
              if (iAtom.getFormalCharge() != null)
                  charge += iAtom.getFormalCharge();
              if (iAtom.getImplicitHydrogenCount() != null)
                  hcnt += iAtom.getImplicitHydrogenCount();
              }
          if (hcnt != 0) {
              IAtom hAtom = atomContainer.getBuilder().newInstance(IAtom.class, "H");
              formula.addIsotope(hAtom, hcnt);
          }
          formula.setCharge(charge);
          return formula;
      }
docs: Method that actually does the work of convert the atomContainer
  to IMolecularFormula given a IMolecularFormula.
  <p> The hydrogens must be implicit.
  
  @param  atomContainer     IAtomContainer object
  @param  formula           IMolecularFormula molecularFormula to put the new Isotopes
  @return                   the filled AtomContainer
  @see                      #getMolecularFormula(IAtomContainer)