The com.hfg.chem package contains classes for working with elements, isotopes, molecules and aqueous solutions. Here are some simple examples. |
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);
// 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());
// 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"));
// 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"));