Skip to content

Commit

Permalink
Improve alignment of mapped reaction layout (don't crash).
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Jul 20, 2018
1 parent dd41d4c commit c5c4567
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@
import javax.vecmath.Point2d;
import javax.vecmath.Vector2d;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -288,12 +290,58 @@ public final void generateCoordinates(final IReaction reaction) throws CDKExcept
}
}
}
// XXX: can do better

afix.clear();
for (IBond bond : bfix) {
afix.put(bond.getBegin(), null);
afix.put(bond.getEnd(), null);
}

int[] parts2 = new int[mol.getAtomCount()];
int numParts = 0;
Deque<IAtom> queue = new ArrayDeque<>();
for (IAtom atom : afix.keySet()) {
if (parts2[mol.indexOf(atom)] != 0)
continue;
parts2[mol.indexOf(atom)] = ++numParts;
for (IBond bond : mol.getConnectedBondsList(atom)) {
if (bfix.contains(bond))
queue.add(bond.getOther(atom));
}
while (!queue.isEmpty()) {
atom = queue.poll();
if (parts2[mol.indexOf(atom)] != 0)
continue;
parts2[mol.indexOf(atom)] = numParts;
for (IBond bond : mol.getConnectedBondsList(atom)) {
if (bfix.contains(bond))
queue.add(bond.getOther(atom));
}
}
}

if (numParts > 1) {
int best = 0;
int bestSize = 0;
for (int part = 1; part <= numParts; part++) {
int size = 0;
for (int i = 0; i < parts2.length; i++) {
if (parts2[i] == part)
++size;
}
if (size > bestSize) {
bestSize = size;
best = part;
}
}

for (IAtom atom : new ArrayList<>(afix.keySet())) {
if (parts2[mol.indexOf(atom)] != best) {
afix.remove(atom);
bfix.removeAll(mol.getConnectedBondsList(atom));
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.IChemSequence;
import org.openscience.cdk.interfaces.IDoubleBondStereochemistry;
import org.openscience.cdk.interfaces.IReaction;
import org.openscience.cdk.interfaces.IStereoElement;
import org.openscience.cdk.io.CMLReader;
import org.openscience.cdk.io.IChemObjectReader.Mode;
Expand All @@ -52,6 +53,7 @@
import org.openscience.cdk.smiles.SmilesParser;
import org.openscience.cdk.stereo.StereoElementFactory;
import org.openscience.cdk.templates.TestMoleculeFactory;
import org.openscience.cdk.tools.manipulator.ReactionManipulator;

import javax.vecmath.Point2d;
import javax.vecmath.Vector2d;
Expand Down Expand Up @@ -1270,4 +1272,20 @@ public void setBondLength() {
StructureDiagramGenerator sdg = new StructureDiagramGenerator();
sdg.setBondLength(2);
}

/**
* Reaction from US20050272744A1 [0195], a bond is broken and made. The
* reaction layout should not crash.
*/
@Test
public void alignReactionBondBrokenAndMade() throws CDKException {
String smiles = "[CH3:18][NH:19][CH3:20].[cH:14]1[cH:13][cH:12][c:11]([cH:16][cH:15]1)[CH2:10][O:9][C:1](=[O:17])[NH:2][C@H:3]2[CH2:8][C:6](=[O:7])[O:5][CH2:4]2>C1CCOC1>[CH3:18][N:19]([CH3:20])[C:6](=[O:7])[CH2:8][C@H:3]([CH2:4][OH:5])[NH:2][C:1](=[O:17])[O:9][CH2:10][c:11]1[cH:12][cH:13][cH:14][cH:15][cH:16]1";
SmilesParser smipar = new SmilesParser(SilentChemObjectBuilder.getInstance());
IReaction reaction = smipar.parseReactionSmiles(smiles);
StructureDiagramGenerator sdg = new StructureDiagramGenerator();
sdg.setAlignMappedReaction(true);
sdg.generateCoordinates(reaction);
for (IAtom atom : ReactionManipulator.toMolecule(reaction).atoms())
assertNotNull(atom.getPoint2d());
}
}

0 comments on commit c5c4567

Please sign in to comment.