Skip to content

Commit

Permalink
Added roundtripping of IAtom.atomtypeName and .maxBondOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
egonw committed Oct 17, 2010
1 parent 047b60e commit 8b2e4c2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/org/openscience/cdk/libio/jena/CDK.java
Expand Up @@ -43,6 +43,7 @@ private static final Property property(String local) {
public static final Resource Bond = resource("Bond");
public static final Resource ChemObject = resource("ChemObject");
public static final Resource Element = resource("Element");
public static final Resource AtomType = resource("AtomType");

// IBond.Order
public static final Resource SingleBond = resource("SingleBond");
Expand Down Expand Up @@ -70,5 +71,7 @@ private static final Property property(String local) {
public static final Property identfier = property("identifier");
public static final Property hasAtomicNumber = property("hasAtomicNumber");
public static final Property hasHybridization = property("hasHybridization");
public static final Property hasAtomTypeName = property("hasAtomTypeName");
public static final Property hasMaxBondOrder = property("hasMaxBondOrder");

}
45 changes: 45 additions & 0 deletions src/main/org/openscience/cdk/libio/jena/Convertor.java
Expand Up @@ -33,6 +33,7 @@
import org.openscience.cdk.interfaces.IElement;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.interfaces.IAtomType.Hybridization;
import org.openscience.cdk.interfaces.IBond.Order;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
Expand Down Expand Up @@ -154,6 +155,15 @@ private static void serializeAtomTypeFields(Model model,
model.add(rdfObject, CDK.hasHybridization, CDK.SP3D5);
}
}
if (type.getAtomTypeName() != null) {
model.add(rdfObject, CDK.hasAtomTypeName, type.getAtomTypeName());
}
if (type.getMaxBondOrder() != null) {
model.add(
rdfObject, CDK.hasMaxBondOrder,
order2Resource(type.getMaxBondOrder())
);
}
}

private static void deserializeAtomTypeFields(
Expand Down Expand Up @@ -184,6 +194,41 @@ private static void deserializeAtomTypeFields(
element.setHybridization(Hybridization.SP3D5);
}
}
Statement name = rdfObject.getProperty(CDK.hasAtomTypeName);
if (name != null) {
element.setAtomTypeName(name.getString());
}
Statement order = rdfObject.getProperty(CDK.hasMaxBondOrder);
if (order != null) {
Resource maxOrder = (Resource)order.getResource();
element.setMaxBondOrder(resource2Order(maxOrder));
}
}

public static Order resource2Order(Resource rdfOrder) {
if (rdfOrder.equals(CDK.SingleBond)) {
return Order.SINGLE;
} else if (rdfOrder.equals(CDK.DoubleBond)) {
return Order.DOUBLE;
} else if (rdfOrder.equals(CDK.TripleBond)) {
return Order.TRIPLE;
} else if (rdfOrder.equals(CDK.QuadrupleBond)) {
return Order.QUADRUPLE;
}
return null;
}

public static Resource order2Resource(Order order) {
if (order == Order.SINGLE) {
return CDK.SingleBond;
} else if (order == Order.DOUBLE) {
return CDK.DoubleBond;
} else if (order == Order.TRIPLE) {
return CDK.TripleBond;
} else if (order == Order.QUADRUPLE) {
return CDK.QuadrupleBond;
}
return null;
}

private static String createIdentifier(Model model, IChemObject object) {
Expand Down
35 changes: 35 additions & 0 deletions src/test/org/openscience/cdk/libio/jena/ConvertorTest.java
Expand Up @@ -31,6 +31,7 @@
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.interfaces.IAtomType.Hybridization;
import org.openscience.cdk.interfaces.IBond.Order;
import org.openscience.cdk.nonotify.NNAtom;
import org.openscience.cdk.nonotify.NNMolecule;
import org.openscience.cdk.nonotify.NoNotificationChemObjectBuilder;
Expand Down Expand Up @@ -109,6 +110,17 @@ private void roundtripBond_Order(IBond.Order order) {
Assert.assertEquals("Unexpected diff: " + diff, 0, diff.length());
}

@Test public void roundtripAtomType() {
IMolecule mol = new NNMolecule();
IAtom object = new NNAtom("C");
object.setAtomTypeName("C.sp3");
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 Expand Up @@ -151,4 +163,27 @@ private void roundtripAtomType_Hybridization(Hybridization hybrid) {
Assert.assertEquals("Unexpected diff: " + diff, 0, diff.length());
}

@Test public void testAtomType_MaxBondOrder_SINGLE() {
roundtripAtomType_MaxBondOrder(Order.SINGLE);
}
@Test public void testAtomType_MaxBondOrder_DOUBLE() {
roundtripAtomType_MaxBondOrder(Order.DOUBLE);
}
@Test public void testAtomType_MaxBondOrder_TRIPLE() {
roundtripAtomType_MaxBondOrder(Order.TRIPLE);
}
@Test public void testAtomType_MaxBondOrder_QUAD() {
roundtripAtomType_MaxBondOrder(Order.QUADRUPLE);
}

private void roundtripAtomType_MaxBondOrder(Order order) {
IMolecule mol = new NNMolecule();
IAtom object = new NNAtom("C");
object.setMaxBondOrder(order);
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());
}
}

0 comments on commit 8b2e4c2

Please sign in to comment.