Skip to content

Commit

Permalink
Handle depiction corner case, do not put wavy bonds on unspecified do…
Browse files Browse the repository at this point in the history
…uble bonds that aren't Sp2 atoms (e.g. Cis/Trans).
  • Loading branch information
johnmay committed Apr 20, 2017
1 parent c0db57f commit 972f30a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Expand Up @@ -636,6 +636,60 @@ private void labelUnspecified(IBond doubleBond) {
doubleBond.setStereo(E_OR_Z);
}


/**
* Checks if the atom can be involved in a double-bond.
* @param idx atom idx
* @return the atom at index (idx) is valid for a double bond
* @see <a href="http://www.inchi-trust.org/download/104/InChI_TechMan.pdf">Double bond stereochemistry, InChI Technical Manual</a>
*/
private boolean isCisTransEndPoint(int idx){
IAtom atom = container.getAtom(idx);
// error: uninit atom
if (atom.getAtomicNumber() == null ||
atom.getFormalCharge() == null ||
atom.getImplicitHydrogenCount() == null)
return false;
final int chg = atom.getFormalCharge();
final int btypes = getBondTypes(idx);
switch (atom.getAtomicNumber()) {
case 6: // C
case 14: // Si
case 32: // Ge
// double, single, single
return chg == 0 && btypes == 0x0102;
case 7: // N
if (chg == 0) // double, single
return btypes == 0x0101;
if (chg == +1) // double, single, single
return btypes == 0x0102;
default:
return false;
}
}

/**
* Generate a bond type code for a given atom. The bond code
* can be quickly tested to count the number of single, double,
* or 'other' bonds.
*
* @param idx the atom idx
* @return bond code
*/
private int getBondTypes(int idx) {
int btypes = container.getAtom(idx).getImplicitHydrogenCount();
for (int end : graph[idx]) {
IBond bond = edgeToBond.get(idx, end);
if (bond.getOrder() == SINGLE)
btypes += 0x00_0001;
else if (bond.getOrder() == DOUBLE)
btypes += 0x00_0100;
else // other bond types
btypes += 0x01_0000;
}
return btypes;
}

/**
* Locates double bonds to mark as unspecified stereochemistry.
*
Expand Down Expand Up @@ -666,7 +720,10 @@ private List<IBond> findUnspecifiedDoubleBonds(int[][] adjList) {
// is actually a tetrahedral centre
if (tetrahedralElements[beg] != null || tetrahedralElements[end] != null)
continue;


if (!isCisTransEndPoint(beg) || !isCisTransEndPoint(end))
continue;

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

Expand Down
Expand Up @@ -613,6 +613,21 @@ public void testosterone() throws CDKException {
assertThat(wedgeCount, is(7));
}

/**
* {@code SMILES: CN(C)(C)=CC}
*/
@Test
public void noWavyBondForCisTransNv5() throws CDKException {
SmilesParser smipar = new SmilesParser(SilentChemObjectBuilder.getInstance());
IAtomContainer mol = smipar.parseSmiles("CN(C)(C)=CC");
StructureDiagramGenerator sdg = new StructureDiagramGenerator();
sdg.generateCoordinates(mol);
for (IBond bond : mol.bonds()) {
assertThat(bond.getStereo(), is(not(IBond.Stereo.UP_OR_DOWN)));
assertThat(bond.getStereo(), is(not(IBond.Stereo.UP_OR_DOWN_INVERTED)));
}
}

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 972f30a

Please sign in to comment.