Skip to content

Commit

Permalink
Don't bond the same two atoms more than once.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Feb 4, 2016
1 parent fcac95e commit 2701e82
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Expand Up @@ -870,11 +870,19 @@ public int compare(IAtomContainer a, IAtomContainer b) {
final IChemObjectBuilder bldr = frags.get(0).getBuilder();

// make the bonds
final List<IBond> ionicBonds = new ArrayList<>();
final List<IBond> ionicBonds = new ArrayList<>(cations.size());
for (int i = 0; i < cations.size(); i++) {
final IAtom beg = cations.get(i);
final IAtom end = anions.get(i);
ionicBonds.add(bldr.newInstance(IBond.class, beg, end));

boolean unique = true;
for (IBond bond : ionicBonds)
if (bond.getAtom(0).equals(beg) && bond.getAtom(1).equals(end) ||
bond.getAtom(1).equals(beg) && bond.getAtom(0).equals(end))
unique = false;

if (unique)
ionicBonds.add(bldr.newInstance(IBond.class, beg, end));
}

// we could merge the fragments here using union-find structures
Expand Down
Expand Up @@ -948,6 +948,17 @@ public void chembl12276() throws Exception {
closeTo(SDG.getBondLength(), 0.001));
}

@Test
public void calciumOxide() throws Exception {
SmilesParser sp = new SmilesParser(SilentChemObjectBuilder.getInstance());
IAtomContainer mol = sp.parseSmiles("[Ca+2].[O-2]");
layout(mol);
for (IAtom atom : mol.atoms())
assertNotNull(atom.getPoint2d());
assertThat(mol.getAtom(0).getPoint2d().distance(mol.getAtom(1).getPoint2d()),
closeTo(SDG.getBondLength(), 0.001));
}

// An extreme test case suggest by Roger Sayle showing Humpty Dumpty reassembly
@Test
public void multipleSalts() throws Exception {
Expand Down

0 comments on commit 2701e82

Please sign in to comment.