Chemistry examples using com-hfg objects.

The com.hfg.chem package contains classes for working with elements, isotopes, molecules and aqueous solutions. Here are some simple examples.

  1. Constructing a molecule from a chemical formula
  2. Constructing a molecule from a SMILES string
  3. Working with a stock solution and a concentration type change
  4. Working with a stock solution and a powdered reagent

Example 1: Constructing a molecule from a chemical formula

  1.  
  2. Molecule molecule = new Molecule("Aluminum sulfate hexadecahydrate").setChemicalFormula("Al2(SO4)3•16H2O");
  3.  
  4. // A Molecule can be queried for its elemental composition and mass
  5. Assert.assertEquals(2, molecule.getElementalComposition().get(Element.ALUMINIUM).intValue());
  6. Assert.assertEquals(32, molecule.getElementalComposition().get(Element.HYDROGEN).intValue());
  7. Assert.assertEquals(28, molecule.getElementalComposition().get(Element.OXYGEN).intValue());
  8. Assert.assertEquals(3, molecule.getElementalComposition().get(Element.SULFUR).intValue());
  9. Assert.assertEquals(629.9872995728, molecule.getMonoisotopicMass(), 0.0000001);
  10. Assert.assertEquals(630.3953572, molecule.getAverageMass(), 0.00001);
  11.  
  12. // This also works with subscript characters:
  13. molecule = new Molecule("Aluminum sulfate hexadecahydrate").setChemicalFormula("Al₂(SO₄)₃•16H₂O");
  14.  
  15. // Another hydrate example:
  16. molecule = new Molecule().setChemicalFormula("C₂H₃NaO₂ * 3 H₂O");
  17.  
  18. Assert.assertEquals(2, molecule.getElementalComposition().get(Element.CARBON).intValue());
  19. Assert.assertEquals(9, molecule.getElementalComposition().get(Element.HYDROGEN).intValue());
  20. Assert.assertEquals(5, molecule.getElementalComposition().get(Element.OXYGEN).intValue());
  21. Assert.assertEquals(1, molecule.getElementalComposition().get(Element.SODIUM).intValue());
  22. Assert.assertEquals(136.0347680708, molecule.getMonoisotopicMass(), 0.0000001);
  23. Assert.assertEquals(136.0796302, molecule.getAverageMass(), 0.00001);
  24.  
  25. // Isotopes in square brackets are also recognized:
  26. molecule = new Molecule().setChemicalFormula("H2[18O]");
  27. molecule = new Molecule().setChemicalFormula("H₂[¹⁸O]");
  28.  
  29. Assert.assertEquals(2, molecule.getElementalComposition().get(Element.HYDROGEN).intValue());
  30. Assert.assertEquals(1, molecule.getElementalComposition().get(Isotope.valueOf(Element.OXYGEN, 18)).intValue());
  31. Assert.assertNull(molecule.getElementalComposition().get(Element.OXYGEN));
  32. Assert.assertEquals(20.01480967686, molecule.getMonoisotopicMass(), 0.0000001);
  33. Assert.assertEquals(20.01503961286, molecule.getAverageMass(), 0.00001);
  34.  
  35. // Linear formula example:
  36. molecule = new Molecule().setChemicalFormula("H−C≡N"); // Cyanide
  37.  
  38. Assert.assertEquals(1, molecule.getElementalComposition().get(Element.CARBON).intValue());
  39. Assert.assertEquals(1, molecule.getElementalComposition().get(Element.HYDROGEN).intValue());
  40. Assert.assertEquals(1, molecule.getElementalComposition().get(Element.NITROGEN).intValue());
  41. Assert.assertEquals(27.010899037199998, molecule.getMonoisotopicMass(), 0.0000001);
  42. Assert.assertEquals(27.02534, molecule.getAverageMass(), 0.00001);
  43.  
  44. // Trapped atom notation example:
  45. molecule = new Molecule().setChemicalFormula("La@C60");
  46.  
  47. Assert.assertEquals(60, molecule.getElementalComposition().get(Element.CARBON).intValue());
  48. Assert.assertEquals(1, molecule.getElementalComposition().get(Element.LANTHANUM).intValue());
  49. Assert.assertEquals(858.9063563, molecule.getMonoisotopicMass(), 0.0000001);
  50. Assert.assertEquals(859.54747, molecule.getAverageMass(), 0.00001);
  51.  

Example 2: Constructing a molecule from a SMILES string

  1.  
  2. // A SMILES string can be used to instantiate a Molecule via a collection of Atoms
  3. String smilesString = "CCc1nc(N2CC3C(c4ccccc4)(C2)NC(=N)N(C)S3(=O)=O)nc(OC)c1F";
  4.  
  5. SMILES_Parser parser = new SMILES_Parser();
  6.  
  7. Molecule mol = parser.parse(smilesString);
  8.  
  9. Assert.assertEquals("C19H23N6O3FS", mol.getChemicalFormula());

Example 3: Working with a stock solution and a concentration type change

What amount of a 3 M glucose stock is needed to make 1.5 kg of 500 mg/kg glucose solution?
  1.  
  2. // Define the solution to be made
  3. AqueousSolution solution = new AqueousSolution()
  4. .setTargetQuantity(new Quantity(1.5, "kg"))
  5. .defineComponent(Monosaccharide.Glucose, new Quantity(500, "mg/kg"));
  6.  
  7. // Define a 3 M glucose stock solution
  8. AqueousSolution stockSolution = new AqueousSolution()
  9. .defineComponent(Monosaccharide.Glucose, new Quantity(3, "M"));
  10.  
  11. // Calculate how much of the glucose stock solution is needed to create the final solution
  12. Quantity amountToAdd = solution.calcQuantityOfStockSolutionNeeded(stockSolution);
  13.  
  14. Assert.assertEquals("1.39 mL", amountToAdd.toString("%.2f"));
  15.  

Example 4: Working with a stock solution and a powdered reagent

What amount of a 500 ml 540 g/L glucose stock and mass of glutamine are needed to make 10 L of 4.5 g/L glucose; 2 mM glutamine solution?
  1.  
  2. // Define the solution to be made
  3. AqueousSolution solution = new AqueousSolution()
  4. .setTargetQuantity(new Quantity(10, "L"))
  5. .defineComponent(Monosaccharide.Glucose, new Quantity(4.5, "g/L"))
  6. .defineComponent(AminoAcid.GLUTAMINE, new Quantity(2, "mM"));
  7.  
  8. // Define a 3M glucose stock solution
  9. AqueousSolution glucoseStockSolution = new AqueousSolution()
  10. .setActualQuantity(new Quantity(500, "ml"))
  11. .defineComponent(Monosaccharide.Glucose, new Quantity(540, "g/L")); // 3 M
  12.  
  13. // What volume of the glucose stock solution do we need?
  14. Quantity amountOfGlucoseToAdd = solution.calcQuantityOfStockSolutionNeeded(glucoseStockSolution);
  15. Assert.assertEquals("83.3 mL", amountOfGlucoseToAdd.toString("%.1f"));
  16.  
  17. // What mass of glutamine powder do we need?
  18. Quantity amountOfGlutamineToAdd = solution.calcMassOfComponentNeeded(AminoAcid.GLUTAMINE);
  19. Assert.assertEquals("2.56 g", amountOfGlutamineToAdd.toString("%.2f"));
  20.  
  21. // Update the amount of stock solution remaining
  22. glucoseStockSolution.subtract(amountOfGlucoseToAdd);
  23. Assert.assertEquals("416.7 mL", glucoseStockSolution.getActualQuantity().toString("%.1f"));
  24.  

Return to Main Page