Skip to content

Commit 6e01e55

Browse files
johnmayegonw
authored andcommitted
Allow reading of cumulated double bonds from InChI's
1 parent 5471fae commit 6e01e55

File tree

3 files changed

+91
-18
lines changed

3 files changed

+91
-18
lines changed

base/core/src/main/java/org/openscience/cdk/stereo/ExtendedCisTrans.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.openscience.cdk.interfaces.IBond;
2929
import org.openscience.cdk.interfaces.IStereoElement;
3030

31+
import java.util.ArrayList;
3132
import java.util.List;
3233

3334
import static org.openscience.cdk.interfaces.ITetrahedralChirality.Stereo;
@@ -92,6 +93,45 @@ public static IAtom[] findTerminalAtoms(IAtomContainer container, IBond focus) {
9293
return null;
9394
}
9495

96+
/**
97+
* Locate the central double-bond in a chain of cumulated double bonds.
98+
*
99+
* <pre>
100+
* A = C = C = B
101+
* ^
102+
* A = C = C = C = C = B
103+
* ^
104+
* </pre>
105+
*
106+
* @param mol molecule
107+
* @param atom at atom from either end of the cumulated chains
108+
* @return the central bond, or null if not found
109+
*/
110+
public static IBond findCentralBond(IAtomContainer mol, IAtom atom) {
111+
List<IBond> bonds = new ArrayList<>();
112+
IAtom prevAtom = atom;
113+
IBond prevBond = null;
114+
boolean found;
115+
do {
116+
found = false;
117+
for (IBond bond : mol.getConnectedBondsList(prevAtom)) {
118+
if (prevBond == bond)
119+
continue;
120+
if (bond.getOrder() == IBond.Order.DOUBLE) {
121+
bonds.add(bond);
122+
found = true;
123+
prevBond = bond;
124+
prevAtom = bond.getOther(prevAtom);
125+
break;
126+
}
127+
}
128+
} while (found);
129+
int nbonds = bonds.size();
130+
if ((nbonds&0x1) == 0)
131+
return null; // is even => false
132+
return bonds.get(nbonds/2);
133+
}
134+
95135
/**
96136
* {@inheritDoc}
97137
*/

storage/inchi/src/main/java/org/openscience/cdk/inchi/InChIToStructure.java

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.openscience.cdk.interfaces.IStereoElement;
4848
import org.openscience.cdk.interfaces.ITetrahedralChirality;
4949
import org.openscience.cdk.stereo.DoubleBondStereochemistry;
50+
import org.openscience.cdk.stereo.ExtendedCisTrans;
5051
import org.openscience.cdk.stereo.ExtendedTetrahedral;
5152
import org.openscience.cdk.tools.periodictable.PeriodicTable;
5253

@@ -156,6 +157,14 @@ protected InChIToStructure(String inchi, IChemObjectBuilder builder, List<String
156157
generateAtomContainerFromInchi(builder);
157158
}
158159

160+
/**
161+
* Flip the storage order of atoms in a bond.
162+
* @param bond the bond
163+
*/
164+
private void flip(IBond bond) {
165+
bond.setAtoms(new IAtom[]{bond.getEnd(), bond.getBegin()});
166+
}
167+
159168
/**
160169
* Gets structure from InChI, and converts InChI library data structure
161170
* into an IAtomContainer.
@@ -355,6 +364,7 @@ else if (stereo == INCHI_BOND_STEREO.SINGLE_1EITHER || stereo == INCHI_BOND_STER
355364
assert stereoElement != null;
356365
molecule.addStereoElement(stereoElement);
357366
} else if (stereo0d.getStereoType() == INCHI_STEREOTYPE.DOUBLEBOND) {
367+
boolean extended = false;
358368
JniInchiAtom[] neighbors = stereo0d.getNeighbors();
359369

360370
// from JNI InChI doc
@@ -369,28 +379,35 @@ else if (stereo == INCHI_BOND_STEREO.SINGLE_1EITHER || stereo == INCHI_BOND_STER
369379
IAtom b = inchiCdkAtomMap.get(neighbors[2]);
370380
IAtom y = inchiCdkAtomMap.get(neighbors[3]);
371381

372-
// XXX: AtomContainer is doing slow lookup
373382
IBond stereoBond = molecule.getBond(a, b);
374-
stereoBond.setAtoms(new IAtom[]{a, b}); // ensure a is first atom
375-
376-
Conformation conformation = null;
377-
378-
switch (stereo0d.getParity()) {
379-
case ODD:
380-
conformation = Conformation.TOGETHER;
381-
break;
382-
case EVEN:
383-
conformation = Conformation.OPPOSITE;
384-
break;
383+
if (stereoBond == null) {
384+
extended = true;
385+
IBond tmp = null;
386+
// A = C = C = B
387+
stereoBond = ExtendedCisTrans.findCentralBond(molecule, a);
388+
if (stereoBond == null)
389+
continue; // warn on invalid input?
390+
IAtom[] ends = ExtendedCisTrans.findTerminalAtoms(molecule, stereoBond);
391+
assert ends != null;
392+
if (ends[0] != a)
393+
flip(stereoBond);
394+
} else {
395+
flip(stereoBond);
385396
}
386397

387-
// unspecified not stored
388-
if (conformation == null) continue;
398+
int config = IStereoElement.TOGETHER;
399+
if (stereo0d.getParity() == INCHI_PARITY.EVEN)
400+
config = IStereoElement.OPPOSITE;
389401

390-
molecule.addStereoElement(new DoubleBondStereochemistry(stereoBond, new IBond[]{molecule.getBond(x, a),
391-
molecule.getBond(b, y)}, conformation));
392-
} else {
393-
// TODO - other types of atom parity - double bond, etc
402+
if (extended) {
403+
molecule.addStereoElement(new ExtendedCisTrans(stereoBond,
404+
new IBond[]{molecule.getBond(x, a),
405+
molecule.getBond(b, y)}, config));
406+
} else {
407+
molecule.addStereoElement(new DoubleBondStereochemistry(stereoBond,
408+
new IBond[]{molecule.getBond(x, a),
409+
molecule.getBond(b, y)}, config));
410+
}
394411
}
395412
}
396413
}

storage/inchi/src/test/java/org/openscience/cdk/inchi/InChIToStructureTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,20 @@ public void isotope() throws Exception{
265265
assertThat(mol.getAtomCount(), is(1));
266266
assertThat(mol.getAtom(0).getMassNumber(), is(121));
267267
}
268+
269+
@Test
270+
public void testExtendedCisTrans() throws Exception {
271+
IAtomContainer mol = InChIGeneratorFactory.getInstance()
272+
.getInChIToStructure("InChI=1/C4BrClFI/c5-3(8)1-2-4(6)7/b4-3-",
273+
SilentChemObjectBuilder.getInstance()).getAtomContainer();
274+
assertNotNull(mol);
275+
int nExtendedCisTrans = 0;
276+
for (IStereoElement se : mol.stereoElements()) {
277+
if (se.getConfig() != IStereoElement.CU)
278+
nExtendedCisTrans++;
279+
else
280+
Assert.fail("Expected onl extended cis/trans");
281+
}
282+
Assert.assertEquals(1, nExtendedCisTrans);
283+
}
268284
}

0 commit comments

Comments
 (0)