Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Correct filtering of unique atom and bond matches. The filter now use…
…s a new predicate for each successive iterations.

Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Feb 2, 2014
1 parent af67de8 commit 602a40e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main/org/openscience/cdk/isomorphism/Mappings.java
Expand Up @@ -28,6 +28,7 @@
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.graph.GraphUtil;
Expand Down Expand Up @@ -280,7 +281,15 @@ public Mappings stereochemistry() {
*/
@TestMethod("uniqueAtoms")
public Mappings uniqueAtoms() {
return filter(new UniqueAtomMatches());
// we need the unique predicate to be reset for each new iterator -
// otherwise multiple iterations are always filtered (seen before)
return new Mappings(query, target,
new Iterable<int[]>() {
@Override public Iterator<int[]> iterator() {
return Iterators.filter(iterable.iterator(),
new UniqueAtomMatches());
}
});
}

/**
Expand All @@ -292,7 +301,16 @@ public Mappings uniqueAtoms() {
*/
@TestMethod("uniqueBonds")
public Mappings uniqueBonds() {
return filter(new UniqueBondMatches(GraphUtil.toAdjList(query)));
// we need the unique predicate to be reset for each new iterator -
// otherwise multiple iterations are always filtered (seen before)
final int[][] g = GraphUtil.toAdjList(query);
return new Mappings(query, target,
new Iterable<int[]>() {
@Override public Iterator<int[]> iterator() {
return Iterators.filter(iterable.iterator(),
new UniqueBondMatches(g));
}
});
}

/**
Expand Down
Expand Up @@ -70,6 +70,24 @@ public class MappingPredicatesTest {
is(3));
}

@Test public void uniqueAtoms_multipleIterations() throws Exception {
IAtomContainer ethane = smi("CC");
IAtomContainer ethanol = smi("CCO");
Mappings mappings = Pattern.findSubstructure(ethane)
.matchAll(ethanol);
assertThat(mappings.countUnique(), is(1));
assertThat(mappings.countUnique(), is(1)); // re-iteration
}

@Test public void uniqueBonds_multipleIterations() throws Exception {
IAtomContainer ethane = smi("CC");
IAtomContainer ethanol = smi("CCO");
Mappings mappings = Pattern.findSubstructure(ethane)
.matchAll(ethanol);
assertThat(mappings.uniqueBonds().count(), is(1));
assertThat(mappings.uniqueBonds().count(), is(1)); // re-iteration
}

IChemObjectBuilder bldr = SilentChemObjectBuilder.getInstance();
SmilesParser smipar = new SmilesParser(bldr);

Expand Down

0 comments on commit 602a40e

Please sign in to comment.