Skip to content

Commit 5b644f9

Browse files
committed
Added double bond stereo
Change-Id: I7a3ece179bc3b9136976634108d18cd69a20645e
1 parent 76577b4 commit 5b644f9

2 files changed

Lines changed: 88 additions & 2 deletions

File tree

src/main/org/openscience/cdk/inchi/InChIGenerator.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.openscience.cdk.interfaces.IAtomContainer;
5555
import org.openscience.cdk.interfaces.IAtomParity;
5656
import org.openscience.cdk.interfaces.IBond;
57+
import org.openscience.cdk.interfaces.IDoubleBondStereochemistry;
5758
import org.openscience.cdk.interfaces.IStereoElement;
5859
import org.openscience.cdk.interfaces.ITetrahedralChirality;
5960
import org.openscience.cdk.interfaces.ITetrahedralChirality.Stereo;
@@ -283,6 +284,7 @@ private void generateInchiFromCDKAtomContainer(IAtomContainer atomContainer) thr
283284

284285

285286
// Process bonds
287+
Map<IBond, JniInchiBond> bondMap = new HashMap<IBond, JniInchiBond>();
286288
Iterator<IBond> bonds = atomContainer.bonds().iterator();
287289
while (bonds.hasNext()) {
288290
IBond bond = bonds.next();
@@ -308,6 +310,7 @@ private void generateInchiFromCDKAtomContainer(IAtomContainer atomContainer) thr
308310

309311
// Create InChI bond
310312
JniInchiBond ibond = new JniInchiBond(at0, at1, order);
313+
bondMap.put(bond, ibond);
311314
input.addBond(ibond);
312315

313316
// Check for bond stereo definitions
@@ -393,6 +396,52 @@ else if (stereo == CDKConstants.UNSET) {
393396
JniInchiStereo0D jniStereo = new JniInchiStereo0D(atC, at0, at1, at2, at3,
394397
INCHI_STEREOTYPE.TETRAHEDRAL, p);
395398
input.addStereo0D(jniStereo);
399+
} else if (stereoElem instanceof IDoubleBondStereochemistry) {
400+
IDoubleBondStereochemistry dbStereo = (IDoubleBondStereochemistry)stereoElem;
401+
IBond[] surroundingBonds = dbStereo.getBonds();
402+
if (surroundingBonds[0] == null || surroundingBonds[1] == null)
403+
throw new CDKException("Cannot generate an InChI with incomplete double bond info");
404+
org.openscience.cdk.interfaces.IDoubleBondStereochemistry.
405+
Conformation stereoType = dbStereo.getStereo();
406+
407+
IBond stereoBond = dbStereo.getStereoBond();
408+
JniInchiAtom at0 = null;
409+
JniInchiAtom at1 = null;
410+
JniInchiAtom at2 = null;
411+
JniInchiAtom at3 = null;
412+
// TODO: I should check for two atom bonds... or maybe that should happen when you
413+
// create a double bond stereochemistry
414+
if (stereoBond.contains(surroundingBonds[0].getAtom(0))) {
415+
// first atom is A
416+
at1 = (JniInchiAtom) atomMap.get(surroundingBonds[0].getAtom(0));
417+
at0 = (JniInchiAtom) atomMap.get(surroundingBonds[0].getAtom(1));
418+
} else {
419+
// first atom is X
420+
at0 = (JniInchiAtom) atomMap.get(surroundingBonds[0].getAtom(0));
421+
at1 = (JniInchiAtom) atomMap.get(surroundingBonds[0].getAtom(1));
422+
}
423+
if (stereoBond.contains(surroundingBonds[1].getAtom(0))) {
424+
// first atom is B
425+
at2 = (JniInchiAtom) atomMap.get(surroundingBonds[1].getAtom(0));
426+
at3 = (JniInchiAtom) atomMap.get(surroundingBonds[1].getAtom(1));
427+
} else {
428+
// first atom is Y
429+
at2 = (JniInchiAtom) atomMap.get(surroundingBonds[1].getAtom(1));
430+
at3 = (JniInchiAtom) atomMap.get(surroundingBonds[1].getAtom(0));
431+
}
432+
INCHI_PARITY p = INCHI_PARITY.UNKNOWN;
433+
if (stereoType == org.openscience.cdk.interfaces.IDoubleBondStereochemistry.Conformation.TOGETHER) {
434+
p = INCHI_PARITY.ODD;
435+
} else if (stereoType == org.openscience.cdk.interfaces.IDoubleBondStereochemistry.Conformation.OPPOSITE) {
436+
p = INCHI_PARITY.EVEN;
437+
} else {
438+
throw new CDKException("Unknown double bond stereochemistry");
439+
}
440+
441+
JniInchiStereo0D jniStereo = new JniInchiStereo0D(
442+
null, at0, at1, at2, at3, INCHI_STEREOTYPE.DOUBLEBOND, p
443+
);
444+
input.addStereo0D(jniStereo);
396445
}
397446
}
398447

src/test/org/openscience/cdk/inchi/InChIGeneratorTest.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
import org.openscience.cdk.*;
2626
import org.openscience.cdk.interfaces.IAtom;
2727
import org.openscience.cdk.interfaces.IAtomContainer;
28+
import org.openscience.cdk.interfaces.IBond;
29+
import org.openscience.cdk.interfaces.IDoubleBondStereochemistry;
2830
import org.openscience.cdk.interfaces.ITetrahedralChirality;
2931
import org.openscience.cdk.interfaces.IBond.Order;
3032
import org.openscience.cdk.interfaces.ITetrahedralChirality.Stereo;
33+
import org.openscience.cdk.stereo.DoubleBondStereochemistry;
3134
import org.openscience.cdk.stereo.TetrahedralChirality;
3235

3336
import javax.vecmath.Point2d;
@@ -277,8 +280,7 @@ protected InChIGeneratorFactory getFactory() throws Exception {
277280
Assert.assertEquals(genZ.getReturnStatus(), INCHI_RET.OKAY);
278281
Assert.assertEquals("InChI=1/C2H2Cl2/c3-1-2-4/h1-2H/b2-1-", genZ.getInchi());
279282
}
280-
281-
283+
282284
/**
283285
* Tests 3D coordinates are correctly passed to InChI.
284286
*
@@ -680,4 +682,39 @@ public void testTetrahedralStereo() throws Exception {
680682
genL.getInchi()
681683
);
682684
}
685+
686+
@Test public void testDoubleBondStereochemistry() throws Exception {
687+
// (E)-1,2-dichloroethene
688+
IAtomContainer acE = new AtomContainer();
689+
IAtom a1E = new Atom("C");
690+
IAtom a2E = new Atom("C");
691+
IAtom a3E = new Atom("Cl");
692+
IAtom a4E = new Atom("Cl");
693+
a1E.setImplicitHydrogenCount(1);
694+
a2E.setImplicitHydrogenCount(1);
695+
acE.addAtom(a1E);
696+
acE.addAtom(a2E);
697+
acE.addAtom(a3E);
698+
acE.addAtom(a4E);
699+
700+
acE.addBond(new Bond(a1E, a2E, CDKConstants.BONDORDER_DOUBLE));
701+
acE.addBond(new Bond(a1E, a3E, CDKConstants.BONDORDER_SINGLE));
702+
acE.addBond(new Bond(a2E, a4E, CDKConstants.BONDORDER_SINGLE));
703+
704+
IBond[] ligands = new IBond[2];
705+
ligands[0] = acE.getBond(1);
706+
ligands[1] = acE.getBond(2);
707+
IDoubleBondStereochemistry stereo = new DoubleBondStereochemistry(
708+
acE.getBond(0), ligands, org.openscience.cdk.interfaces.IDoubleBondStereochemistry.Conformation.OPPOSITE
709+
);
710+
acE.addStereoElement(stereo);
711+
712+
InChIGenerator genE = getFactory().getInChIGenerator(acE);
713+
Assert.assertEquals(INCHI_RET.OKAY, genE.getReturnStatus());
714+
System.out.println(genE.getMessage());
715+
Assert.assertEquals(
716+
"InChI=1S/C2H2Cl2/c3-1-2-4/h1-2H/b2-1+",
717+
genE.getInchi()
718+
);
719+
}
683720
}

0 commit comments

Comments
 (0)