diff --git a/src/main/org/openscience/cdk/libio/jena/CDK.java b/src/main/org/openscience/cdk/libio/jena/CDK.java index 9c871d7e83b..ecf05f47134 100644 --- a/src/main/org/openscience/cdk/libio/jena/CDK.java +++ b/src/main/org/openscience/cdk/libio/jena/CDK.java @@ -47,6 +47,7 @@ private static final Property property(String local) { public static final Resource ChemObject = resource("ChemObject"); public static final Resource Element = resource("Element"); public static final Resource AtomType = resource("AtomType"); + public static final Resource Isotope = resource("Isotope"); // IBond.Order public static final Resource SingleBond = resource("SingleBond"); @@ -77,5 +78,9 @@ private static final Property property(String local) { public static final Property hasAtomTypeName = property("hasAtomTypeName"); public static final Property hasMaxBondOrder = property("hasMaxBondOrder"); public static final Property hasFormalCharge = property("hasFormalCharge"); + public static final Property hasMassNumber = property("hasMassNumber"); + public static final Property hasExactMass = property("hasExactMass"); + public static final Property hasNaturalAbundance = + property("hasNaturalAbundance"); } \ No newline at end of file diff --git a/src/main/org/openscience/cdk/libio/jena/Convertor.java b/src/main/org/openscience/cdk/libio/jena/Convertor.java index 43495a4108e..9b3a34db740 100644 --- a/src/main/org/openscience/cdk/libio/jena/Convertor.java +++ b/src/main/org/openscience/cdk/libio/jena/Convertor.java @@ -25,12 +25,14 @@ import java.util.HashMap; import java.util.Map; +import org.openscience.cdk.CDKConstants; import org.openscience.cdk.interfaces.IAtom; import org.openscience.cdk.interfaces.IAtomType; import org.openscience.cdk.interfaces.IBond; import org.openscience.cdk.interfaces.IChemObject; import org.openscience.cdk.interfaces.IChemObjectBuilder; import org.openscience.cdk.interfaces.IElement; +import org.openscience.cdk.interfaces.IIsotope; import org.openscience.cdk.interfaces.IMolecule; import org.openscience.cdk.interfaces.IAtomType.Hybridization; import org.openscience.cdk.interfaces.IBond.Order; @@ -127,7 +129,7 @@ private static void deserializeElementFields( private static void serializeAtomTypeFields(Model model, Resource rdfObject, IAtomType type) { - serializeElementFields(model, rdfObject, type); + serializeIsotopeFields(model, rdfObject, type); if (type.getHybridization() != null) { Hybridization hybrid = type.getHybridization(); if (hybrid == Hybridization.S) { @@ -169,9 +171,32 @@ private static void serializeAtomTypeFields(Model model, } } + private static void serializeIsotopeFields(Model model, Resource rdfObject, + IIsotope isotope) { + serializeElementFields(model, rdfObject, isotope); + if (isotope.getMassNumber() != CDKConstants.UNSET) { + model.add( + rdfObject, CDK.hasMassNumber, + isotope.getMassNumber().toString() + ); + } + if (isotope.getExactMass() != CDKConstants.UNSET) { + model.add( + rdfObject, CDK.hasExactMass, + isotope.getExactMass().toString() + ); + } + if (isotope.getNaturalAbundance() != CDKConstants.UNSET) { + model.add( + rdfObject, CDK.hasNaturalAbundance, + isotope.getNaturalAbundance().toString() + ); + } + } + private static void deserializeAtomTypeFields( Resource rdfObject, IAtomType element) { - deserializeElementFields(rdfObject, element); + deserializeIsotopeFields(rdfObject, element); Statement hybrid = rdfObject.getProperty(CDK.hasHybridization); if (hybrid != null) { Resource rdfHybrid = (Resource)hybrid.getObject(); @@ -211,6 +236,21 @@ private static void deserializeAtomTypeFields( element.setFormalCharge(formalCharge.getInt()); } + private static void deserializeIsotopeFields(Resource rdfObject, + IIsotope isotope) { + deserializeElementFields(rdfObject, isotope); + Statement massNumber = rdfObject.getProperty(CDK.hasMassNumber); + if (massNumber != null) + isotope.setMassNumber(massNumber.getInt()); + Statement exactMass = rdfObject.getProperty(CDK.hasExactMass); + if (exactMass != null) + isotope.setExactMass(exactMass.getDouble()); + Statement naturalAbundance = + rdfObject.getProperty(CDK.hasNaturalAbundance); + if (naturalAbundance != null) + isotope.setNaturalAbundance(naturalAbundance.getDouble()); + } + public static Order resource2Order(Resource rdfOrder) { if (rdfOrder.equals(CDK.SingleBond)) { return Order.SINGLE; diff --git a/src/test/org/openscience/cdk/libio/jena/ConvertorTest.java b/src/test/org/openscience/cdk/libio/jena/ConvertorTest.java index 4a21ede1d4f..981ba3320b6 100644 --- a/src/test/org/openscience/cdk/libio/jena/ConvertorTest.java +++ b/src/test/org/openscience/cdk/libio/jena/ConvertorTest.java @@ -122,6 +122,39 @@ private void roundtripBond_Order(IBond.Order order) { Assert.assertEquals("Unexpected diff: " + diff, 0, diff.length()); } + @Test public void roundtripIsotope_ExactMass() { + IMolecule mol = new NNMolecule(); + IAtom object = new NNAtom("C"); + object.setExactMass(0.3); + mol.addAtom(object); + Model model = Convertor.molecule2Model(mol); + IMolecule rtMol = Convertor.model2Molecule(model, builder); + String diff = AtomContainerDiff.diff(mol, rtMol); + Assert.assertEquals("Unexpected diff: " + diff, 0, diff.length()); + } + + @Test public void roundtripIsotope_MassNumber() { + IMolecule mol = new NNMolecule(); + IAtom object = new NNAtom("C"); + object.setMassNumber(13); + mol.addAtom(object); + Model model = Convertor.molecule2Model(mol); + IMolecule rtMol = Convertor.model2Molecule(model, builder); + String diff = AtomContainerDiff.diff(mol, rtMol); + Assert.assertEquals("Unexpected diff: " + diff, 0, diff.length()); + } + + @Test public void roundtripIsotope_NaturalAbundance() { + IMolecule mol = new NNMolecule(); + IAtom object = new NNAtom("C"); + object.setNaturalAbundance(0.95); + mol.addAtom(object); + Model model = Convertor.molecule2Model(mol); + IMolecule rtMol = Convertor.model2Molecule(model, builder); + String diff = AtomContainerDiff.diff(mol, rtMol); + Assert.assertEquals("Unexpected diff: " + diff, 0, diff.length()); + } + @Test public void roundtripAtomType_S() { roundtripAtomType_Hybridization(Hybridization.S); }