Permalink
Browse files

Added (de)serialization of the IIsotope fields to RDF

  • Loading branch information...
1 parent 153fa7f commit 3db0b71849cbc2bfa9a1930378ae4c3731001167 @egonw egonw committed Oct 31, 2009
View
5 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");
}
View
44 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;
View
33 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);
}

0 comments on commit 3db0b71

Please sign in to comment.