Permalink
Browse files

Added (de)serialization of the IPseudoAtom fields to RDF

  • Loading branch information...
1 parent 4f9d7ec commit ce159ccf0a1d60382cb0e811dc3ef99447251fff @egonw egonw committed Nov 1, 2009
@@ -43,6 +43,7 @@ private static final Property property(String local) {
public static final Resource Molecule = resource("Molecule");
public static final Resource Atom = resource("Atom");
+ public static final Resource PseudoAtom = resource("PseudoAtom");
public static final Resource Bond = resource("Bond");
public static final Resource ChemObject = resource("ChemObject");
public static final Resource Element = resource("Element");
@@ -72,6 +73,7 @@ private static final Property property(String local) {
public static final Property bindsAtom = property("bindsAtom");
public static final Property hasOrder = property("hasOrder");
public static final Property symbol = property("symbol");
+ public static final Property hasLabel = property("hasLabel");
public static final Property identfier = property("identifier");
public static final Property hasAtomicNumber = property("hasAtomicNumber");
public static final Property hasHybridization = property("hasHybridization");
@@ -35,6 +35,7 @@
import org.openscience.cdk.interfaces.IElement;
import org.openscience.cdk.interfaces.IIsotope;
import org.openscience.cdk.interfaces.IMolecule;
+import org.openscience.cdk.interfaces.IPseudoAtom;
import org.openscience.cdk.interfaces.IAtomType.Hybridization;
import org.openscience.cdk.interfaces.IBond.Order;
@@ -72,10 +73,14 @@ public static Model molecule2Model(IMolecule molecule) {
createIdentifier(model, atom)
);
cdkToRDFAtomMap.put(atom, rdfAtom);
- model.add(rdfAtom, RDF.type, CDK.Atom);
- model.add(rdfAtom, CDK.symbol, atom.getSymbol());
model.add(subject, CDK.hasAtom, rdfAtom);
- serializeAtomTypeFields(model, rdfAtom, atom);
+ if (atom instanceof IPseudoAtom) {
+ model.add(rdfAtom, RDF.type, CDK.PseudoAtom);
+ serializePseudoAtomFields(model, rdfAtom, (IPseudoAtom)atom);
+ } else {
+ model.add(rdfAtom, RDF.type, CDK.Atom);
+ serializeAtomFields(model, rdfAtom, atom);
+ }
}
for (IBond bond : molecule.bonds()) {
Resource rdfBond = model.createResource(
@@ -97,6 +102,21 @@ public static Model molecule2Model(IMolecule molecule) {
return model;
}
+ private static void serializePseudoAtomFields(Model model,
+ Resource rdfAtom, IPseudoAtom atom) {
+ serializeAtomFields(model, rdfAtom, atom);
+ if (atom.getLabel() != CDKConstants.UNSET)
+ model.add(rdfAtom, CDK.hasLabel, atom.getLabel());
+ }
+
+ private static void serializeAtomFields(Model model, Resource rdfAtom,
+ IAtom atom) {
+ serializeAtomTypeFields(model, rdfAtom, atom);
+ model.add(rdfAtom, RDF.type, CDK.Atom);
+ if (atom.getSymbol() != CDKConstants.UNSET)
+ model.add(rdfAtom, CDK.symbol, atom.getSymbol());
+ }
+
private static void serializeElectronContainerFields(Model model,
Resource rdfBond, IElectronContainer bond) {
serializeChemObjectFields(model, rdfBond, bond);
@@ -318,10 +338,19 @@ public static IMolecule model2Molecule(Model model,
StmtIterator atoms = rdfMol.listProperties(CDK.hasAtom);
while (atoms.hasNext()) {
Resource rdfAtom = atoms.nextStatement().getResource();
- IAtom atom = builder.newAtom();
- rdfToCDKAtomMap.put(rdfAtom, atom);
+ IAtom atom;
+ if (rdfAtom.hasProperty(RDF.type, CDK.PseudoAtom)) {
+ atom = builder.newPseudoAtom();
+ atom.setStereoParity(0);
+ Statement label = rdfAtom.getProperty(CDK.hasLabel);
+ if (label != null)
+ ((IPseudoAtom)atom).setLabel(label.getString());
+ } else {
+ atom = builder.newAtom();
+ }
Statement symbol = rdfAtom.getProperty(CDK.symbol);
if (symbol != null) atom.setSymbol(symbol.getString());
+ rdfToCDKAtomMap.put(rdfAtom, atom);
deserializeAtomTypeFields(rdfAtom, atom);
mol.addAtom(atom);
}
@@ -26,10 +26,12 @@
import org.junit.Test;
import org.openscience.cdk.CDKTestCase;
+import org.openscience.cdk.PseudoAtom;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IMolecule;
+import org.openscience.cdk.interfaces.IPseudoAtom;
import org.openscience.cdk.interfaces.IAtomType.Hybridization;
import org.openscience.cdk.interfaces.IBond.Order;
import org.openscience.cdk.nonotify.NNAtom;
@@ -122,6 +124,17 @@ private void roundtripBond_Order(IBond.Order order) {
Assert.assertEquals("Unexpected diff: " + diff, 0, diff.length());
}
+ @Test public void roundtripPseudoAtom() {
+ IMolecule mol = new NNMolecule();
+ IPseudoAtom object = new PseudoAtom("FunnyAtom");
+ mol.addAtom(object);
+ Model model = Convertor.molecule2Model(mol);
+ model.write(System.out, "N3");
+ IMolecule rtMol = Convertor.model2Molecule(model, builder);
+ String diff = AtomContainerDiff.diff(mol, rtMol);
+ Assert.assertEquals("Unexpected diff: " + diff, 0, diff.length());
+ }
+
@Test public void roundtripAtomType() {
IMolecule mol = new NNMolecule();
IAtom object = new NNAtom("C");

0 comments on commit ce159cc

Please sign in to comment.