Skip to content

Commit

Permalink
Added (de)serialization of the IIsotope fields to RDF
Browse files Browse the repository at this point in the history
  • Loading branch information
egonw committed Oct 17, 2010
1 parent 153fa7f commit 3db0b71
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/main/org/openscience/cdk/libio/jena/CDK.java
Expand Up @@ -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");
Expand Down Expand Up @@ -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");

}
44 changes: 42 additions & 2 deletions src/main/org/openscience/cdk/libio/jena/Convertor.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions src/test/org/openscience/cdk/libio/jena/ConvertorTest.java
Expand Up @@ -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);
}
Expand Down

0 comments on commit 3db0b71

Please sign in to comment.