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


    Molecule molecule = new Molecule("Aluminum sulfate hexadecahydrate").setChemicalFormula("Al2(SO4)3•16H2O");

    // A Molecule can be queried for its elemental composition and mass
    Assert.assertEquals(2, molecule.getElementalComposition().get(Element.ALUMINIUM).intValue());
    Assert.assertEquals(32, molecule.getElementalComposition().get(Element.HYDROGEN).intValue());
    Assert.assertEquals(28, molecule.getElementalComposition().get(Element.OXYGEN).intValue());
    Assert.assertEquals(3, molecule.getElementalComposition().get(Element.SULFUR).intValue());
    Assert.assertEquals(629.9872995728, molecule.getMonoisotopicMass(), 0.0000001);
    Assert.assertEquals(630.3953572, molecule.getAverageMass(), 0.00001);

    // This also works with subscript characters:
    molecule = new Molecule("Aluminum sulfate hexadecahydrate").setChemicalFormula("Al₂(SO₄)₃•16H₂O");

    // Another hydrate example:
    molecule = new Molecule().setChemicalFormula("C₂H₃NaO₂ * 3 H₂O");

    Assert.assertEquals(2, molecule.getElementalComposition().get(Element.CARBON).intValue());
    Assert.assertEquals(9, molecule.getElementalComposition().get(Element.HYDROGEN).intValue());
    Assert.assertEquals(5, molecule.getElementalComposition().get(Element.OXYGEN).intValue());
    Assert.assertEquals(1, molecule.getElementalComposition().get(Element.SODIUM).intValue());
    Assert.assertEquals(136.0347680708, molecule.getMonoisotopicMass(), 0.0000001);
    Assert.assertEquals(136.0796302, molecule.getAverageMass(), 0.00001);

    // Isotopes in square brackets are also recognized:
    molecule = new Molecule().setChemicalFormula("H2[18O]");
    molecule = new Molecule().setChemicalFormula("H₂[¹⁸O]");

    Assert.assertEquals(2, molecule.getElementalComposition().get(Element.HYDROGEN).intValue());
    Assert.assertEquals(1, molecule.getElementalComposition().get(Isotope.valueOf(Element.OXYGEN, 18)).intValue());
    Assert.assertNull(molecule.getElementalComposition().get(Element.OXYGEN));
    Assert.assertEquals(20.01480967686, molecule.getMonoisotopicMass(), 0.0000001);
    Assert.assertEquals(20.01503961286, molecule.getAverageMass(), 0.00001);

    // Linear formula example:
    molecule = new Molecule().setChemicalFormula("H−C≡N"); // Cyanide

    Assert.assertEquals(1, molecule.getElementalComposition().get(Element.CARBON).intValue());
    Assert.assertEquals(1, molecule.getElementalComposition().get(Element.HYDROGEN).intValue());
    Assert.assertEquals(1, molecule.getElementalComposition().get(Element.NITROGEN).intValue());
    Assert.assertEquals(27.010899037199998, molecule.getMonoisotopicMass(), 0.0000001);
    Assert.assertEquals(27.02534, molecule.getAverageMass(), 0.00001);

    // Trapped atom notation example:
    molecule = new Molecule().setChemicalFormula("La@C60");

    Assert.assertEquals(60, molecule.getElementalComposition().get(Element.CARBON).intValue());
    Assert.assertEquals(1, molecule.getElementalComposition().get(Element.LANTHANUM).intValue());
    Assert.assertEquals(858.9063563, molecule.getMonoisotopicMass(), 0.0000001);
    Assert.assertEquals(859.54747, molecule.getAverageMass(), 0.00001);

Example 2: Constructing a molecule from a SMILES string


    // A SMILES string can be used to instantiate a Molecule via a collection of Atoms
    String smilesString = "CCc1nc(N2CC3C(c4ccccc4)(C2)NC(=N)N(C)S3(=O)=O)nc(OC)c1F";

    SMILES_Parser parser = new SMILES_Parser();

    Molecule mol = parser.parse(smilesString);

    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?

    // Define the solution to be made
    AqueousSolution solution = new AqueousSolution()
          .setTargetQuantity(new Quantity(1.5, "kg"))
          .defineComponent(Monosaccharide.Glucose, new Quantity(500, "mg/kg"));

    // Define a 3 M glucose stock solution
    AqueousSolution stockSolution = new AqueousSolution()
          .defineComponent(Monosaccharide.Glucose, new Quantity(3, "M"));

    // Calculate how much of the glucose stock solution is needed to create the final solution
    Quantity amountToAdd = solution.calcQuantityOfStockSolutionNeeded(stockSolution);

    Assert.assertEquals("1.39 mL", amountToAdd.toString("%.2f"));

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?

    // Define the solution to be made
    AqueousSolution solution = new AqueousSolution()
          .setTargetQuantity(new Quantity(10, "L"))
          .defineComponent(Monosaccharide.Glucose, new Quantity(4.5, "g/L"))
          .defineComponent(AminoAcid.GLUTAMINE, new Quantity(2, "mM"));

    // Define a 3M glucose stock solution
    AqueousSolution glucoseStockSolution = new AqueousSolution()
          .setActualQuantity(new Quantity(500, "ml"))
          .defineComponent(Monosaccharide.Glucose, new Quantity(540, "g/L")); // 3 M

    // What volume of the glucose stock solution do we need?
    Quantity amountOfGlucoseToAdd = solution.calcQuantityOfStockSolutionNeeded(glucoseStockSolution);
    Assert.assertEquals("83.3 mL", amountOfGlucoseToAdd.toString("%.1f"));

    // What mass of glutamine powder do we need?
    Quantity amountOfGlutamineToAdd = solution.calcMassOfComponentNeeded(AminoAcid.GLUTAMINE);
    Assert.assertEquals("2.56 g", amountOfGlutamineToAdd.toString("%.2f"));

    // Update the amount of stock solution remaining
    glucoseStockSolution.subtract(amountOfGlucoseToAdd);
    Assert.assertEquals("416.7 mL", glucoseStockSolution.getActualQuantity().toString("%.1f"));


Return to Main Page