Skip to content

Commit

Permalink
Merge pull request #192 from cdk/01feb16_2
Browse files Browse the repository at this point in the history
Rendering patches merged.
  • Loading branch information
egonw committed Feb 12, 2016
2 parents a838988 + a19eabd commit 51f0ffc
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ public final class DepictionGenerator {
* system font.
*/
public DepictionGenerator() {
this(new Font(getDefaultOsFont(), Font.PLAIN, 22));
this(new Font(getDefaultOsFont(), Font.PLAIN, 13));
setParam(BasicSceneGenerator.BondLength.class, 26.1d);
setParam(StandardGenerator.HashSpacing.class, 26 / 8d);
setParam(StandardGenerator.WaveSpacing.class, 26 / 8d);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@
import org.openscience.cdk.ringsearch.RingSearch;
import org.openscience.cdk.stereo.ExtendedTetrahedral;
import org.openscience.cdk.tools.LoggingToolFactory;
import uk.ac.ebi.beam.Bond;

import javax.vecmath.Point2d;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.openscience.cdk.interfaces.IBond.Order.DOUBLE;
import static org.openscience.cdk.interfaces.IBond.Order.SINGLE;
Expand Down Expand Up @@ -179,7 +182,7 @@ public int compare(Integer i, Integer j) {
}

// Unspecified double bond, indicated with an up/down wavy bond
for (IBond bond : findUnspecifiedDoubleBonds()) {
for (IBond bond : findUnspecifiedDoubleBonds(g)) {
labelUnspecified(bond);
}
}
Expand Down Expand Up @@ -583,7 +586,7 @@ private void labelUnspecified(IBond doubleBond) {
*
* @return set of double bonds
*/
private List<IBond> findUnspecifiedDoubleBonds() {
private List<IBond> findUnspecifiedDoubleBonds(int[][] adjList) {
List<IBond> unspecifiedDoubleBonds = new ArrayList<>();
for (IBond bond : container.bonds()) {
// non-double bond, ignore it
Expand Down Expand Up @@ -611,12 +614,97 @@ private List<IBond> findUnspecifiedDoubleBonds() {

if (!hasOnlyPlainBonds(beg, bond) || !hasOnlyPlainBonds(end, bond))
continue;


if (hasLinearEqualPaths(adjList, beg, end) || hasLinearEqualPaths(adjList, end, beg))
continue;

unspecifiedDoubleBonds.add(bond);
}
return unspecifiedDoubleBonds;
}

private boolean hasLinearEqualPaths(int[][] adjList, int start, int prev) {
int a = -1;
int b = -1;
for (int w : adjList[start]) {
if (w == prev) continue;
else if (a == -1) a = w;
else if (b == -1) b = w;
else return false; // ???
}
if (b < 0)
return false;
Set<IAtom> visit = new HashSet<>();
IAtom aAtom = container.getAtom(a);
IAtom bAtom = container.getAtom(b);
visit.add(container.getAtom(start));
if (aAtom.isInRing() || bAtom.isInRing())
return false;
IAtom aNext = aAtom;
IAtom bNext = bAtom;
while (aNext != null && bNext != null) {
aAtom = aNext;
bAtom = bNext;
visit.add(aAtom);
visit.add(bAtom);
aNext = null;
bNext = null;

// different atoms
if (notEqual(aAtom.getAtomicNumber(), bAtom.getAtomicNumber()))
return false;
if (notEqual(aAtom.getFormalCharge(), bAtom.getFormalCharge()))
return false;
if (notEqual(aAtom.getMassNumber(), bAtom.getMassNumber()))
return false;

int hCntA = aAtom.getImplicitHydrogenCount();
int hCntB = bAtom.getImplicitHydrogenCount();
int cntA = 0, cntB = 0;
for (int w : adjList[atomToIndex.get(aAtom)]) {
IAtom atom = container.getAtom(w);
if (visit.contains(atom))
continue;
// hydrogen
if (atom.getAtomicNumber() == 1 && adjList[w].length == 1) {
hCntA++;
continue;
}
aNext = cntA == 0 ? atom : null;
cntA++;
}
for (int w : adjList[atomToIndex.get(bAtom)]) {
IAtom atom = container.getAtom(w);
if (visit.contains(atom))
continue;
// hydrogen
if (atom.getAtomicNumber() == 1 && adjList[w].length == 1) {
hCntB++;
continue;
}
bNext = cntB == 0 ? atom : null;
cntB++;
}

// hydrogen counts are different
if (hCntA != hCntB)
return false;

// differing in co
if (cntA != cntB || (cntA > 1 && cntB > 1))
return false;
}

if (aNext != null || bNext != null)
return false;

// traversed the path till the end
return true;
}

private boolean notEqual(Integer a, Integer b) {
return a == null ? b != null : !a.equals(b);
}

/**
* Check that an atom (v:index) is only adjacent to plain single bonds (may be a bold or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.junit.Test;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.graph.Cycles;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IBond;
Expand Down Expand Up @@ -444,6 +445,156 @@ public void dontMarkRingBondsInBezeneAsUnspecified() {
}
}

/**
* {@code SMILES: *CN=C(N)N}
*/
@Test
public void dontMarkGuanidineAsUnspecified() {
IAtomContainer m = new AtomContainer();
m.addAtom(atom("R", 0, 0.00, 0.00));
m.addAtom(atom("C", 2, 1.30, -0.75));
m.addAtom(atom("N", 0, 2.60, -0.00));
m.addAtom(atom("C", 0, 3.90, -0.75));
m.addAtom(atom("N", 2, 5.20, -0.00));
m.addAtom(atom("N", 2, 3.90, -2.25));
m.addBond(0, 1, IBond.Order.SINGLE);
m.addBond(1, 2, IBond.Order.SINGLE);
m.addBond(2, 3, IBond.Order.DOUBLE);
m.addBond(3, 4, IBond.Order.SINGLE);
m.addBond(3, 5, IBond.Order.SINGLE);
NonplanarBonds.assign(m);
for (IBond bond : m.bonds())
assertThat(bond.getStereo(), is(IBond.Stereo.NONE));
}

/**
* {@code SMILES: *CN=C(CCC)CCC[H]}
*/
@Test
public void dontUnspecifiedDueToHRepresentation() {
IAtomContainer m = new AtomContainer();
m.addAtom(atom("R", 0, 0.00, 0.00));
m.addAtom(atom("C", 2, 1.30, -0.75));
m.addAtom(atom("N", 0, 2.60, -0.00));
m.addAtom(atom("C", 0, 3.90, -0.75));
m.addAtom(atom("C", 2, 3.90, -2.25));
m.addAtom(atom("C", 2, 2.60, -3.00));
m.addAtom(atom("C", 3, 2.60, -4.50));
m.addAtom(atom("C", 2, 5.20, -0.00));
m.addAtom(atom("C", 2, 6.50, -0.75));
m.addAtom(atom("C", 2, 7.79, -0.00));
m.addAtom(atom("H", 0, 9.09, -0.75));
m.addBond(0, 1, IBond.Order.SINGLE);
m.addBond(1, 2, IBond.Order.SINGLE);
m.addBond(2, 3, IBond.Order.DOUBLE);
m.addBond(3, 4, IBond.Order.SINGLE);
m.addBond(4, 5, IBond.Order.SINGLE);
m.addBond(5, 6, IBond.Order.SINGLE);
m.addBond(3, 7, IBond.Order.SINGLE);
m.addBond(7, 8, IBond.Order.SINGLE);
m.addBond(8, 9, IBond.Order.SINGLE);
m.addBond(9, 10, IBond.Order.SINGLE);
NonplanarBonds.assign(m);
for (IBond bond : m.bonds())
assertThat(bond.getStereo(), is(IBond.Stereo.NONE));
}

/**
* {@code SMILES: *CN=C(CCC)CCC}
*/
@Test
public void dontMarkUnspecifiedForLinearEqualChains() {
IAtomContainer m = new AtomContainer();
m.addAtom(atom("R", 0, 0.00, -0.00));
m.addAtom(atom("C", 2, 1.30, -0.75));
m.addAtom(atom("N", 0, 2.60, -0.00));
m.addAtom(atom("C", 0, 3.90, -0.75));
m.addAtom(atom("C", 2, 5.20, -0.00));
m.addAtom(atom("C", 2, 6.50, -0.75));
m.addAtom(atom("C", 3, 7.79, -0.00));
m.addAtom(atom("C", 2, 3.90, -2.25));
m.addAtom(atom("C", 2, 5.20, -3.00));
m.addAtom(atom("C", 3, 5.20, -4.50));
m.addBond(0, 1, IBond.Order.SINGLE);
m.addBond(1, 2, IBond.Order.SINGLE);
m.addBond(2, 3, IBond.Order.DOUBLE);
m.addBond(3, 4, IBond.Order.SINGLE);
m.addBond(4, 5, IBond.Order.SINGLE);
m.addBond(5, 6, IBond.Order.SINGLE);
m.addBond(3, 7, IBond.Order.SINGLE);
m.addBond(7, 8, IBond.Order.SINGLE);
m.addBond(8, 9, IBond.Order.SINGLE);
NonplanarBonds.assign(m);
for (IBond bond : m.bonds())
assertThat(bond.getStereo(), is(IBond.Stereo.NONE));
}

/**
* {@code SMILES: *CN=C1CCCCC1}
*/
@Test
public void markUnspecifiedForCyclicLigands() {
IAtomContainer m = new AtomContainer();
m.addAtom(atom("R", 0, -4.22, 3.05));
m.addAtom(atom("C", 2, -2.92, 2.30));
m.addAtom(atom("N", 0, -1.62, 3.05));
m.addAtom(atom("C", 0, -0.32, 2.30));
m.addAtom(atom("C", 2, -0.33, 0.80));
m.addAtom(atom("C", 2, 0.97, 0.05));
m.addAtom(atom("C", 2, 2.27, 0.80));
m.addAtom(atom("C", 2, 2.27, 2.30));
m.addAtom(atom("C", 2, 0.97, 3.05));
m.addBond(0, 1, IBond.Order.SINGLE);
m.addBond(1, 2, IBond.Order.SINGLE);
m.addBond(2, 3, IBond.Order.DOUBLE);
m.addBond(3, 4, IBond.Order.SINGLE);
m.addBond(4, 5, IBond.Order.SINGLE);
m.addBond(5, 6, IBond.Order.SINGLE);
m.addBond(6, 7, IBond.Order.SINGLE);
m.addBond(7, 8, IBond.Order.SINGLE);
m.addBond(3, 8, IBond.Order.SINGLE);
Cycles.markRingAtomsAndBonds(m);
NonplanarBonds.assign(m);
int wavyCount = 0;
for (IBond bond : m.bonds())
if (bond.getStereo() == IBond.Stereo.UP_OR_DOWN)
wavyCount++;
assertThat(wavyCount, is(1));
}

/**
* {@code SMILES: *CN=C(CCC)CCN}
*/
@Test
public void unspecifiedMarkedOnDifferentLigands() {
IAtomContainer m = new AtomContainer();
m.addAtom(atom("R", 0, 0.00, -0.00));
m.addAtom(atom("C", 2, 1.30, -0.75));
m.addAtom(atom("N", 0, 2.60, -0.00));
m.addAtom(atom("C", 0, 3.90, -0.75));
m.addAtom(atom("C", 2, 5.20, -0.00));
m.addAtom(atom("C", 2, 6.50, -0.75));
m.addAtom(atom("C", 3, 7.79, -0.00));
m.addAtom(atom("C", 2, 3.90, -2.25));
m.addAtom(atom("C", 2, 5.20, -3.00));
m.addAtom(atom("N", 2, 5.20, -4.50));
m.addBond(0, 1, IBond.Order.SINGLE);
m.addBond(1, 2, IBond.Order.SINGLE);
m.addBond(2, 3, IBond.Order.DOUBLE);
m.addBond(3, 4, IBond.Order.SINGLE);
m.addBond(4, 5, IBond.Order.SINGLE);
m.addBond(5, 6, IBond.Order.SINGLE);
m.addBond(3, 7, IBond.Order.SINGLE);
m.addBond(7, 8, IBond.Order.SINGLE);
m.addBond(8, 9, IBond.Order.SINGLE);
NonplanarBonds.assign(m);
int wavyCount = 0;
for (IBond bond : m.bonds())
if (bond.getStereo() == IBond.Stereo.UP_OR_DOWN)
wavyCount++;
assertThat(wavyCount, is(1));
}

static IAtom atom(String symbol, int hCount, double x, double y) {
IAtom a = new Atom(symbol);
a.setImplicitHydrogenCount(hCount);
Expand Down

0 comments on commit 51f0ffc

Please sign in to comment.