/** * Find the atoms that match this atom type. * * @param container structure representation * @return indices of atoms that matched this type */ private Set<Integer> matches(IAtomContainer container) { Set<Integer> matchedIdx = new HashSet<Integer>(); for (int[] mapping : pattern.matchAll(container)) { matchedIdx.add(mapping[0]); } return matchedIdx; } }
@Override public BitSet load(IAtomContainer target) throws Exception { BitSet hits = new BitSet(); for (int[] mapping : Pattern.findSubstructure(query) .matchAll(target)) { hits.set(mapping[0]); } return hits; } });
public IAtomContainer removeMolecule(IAtomContainer molecule) throws CDKException { for (int i = 0; i < templates.size(); i++) { if (VentoFoggia.findIdentical(templates.get(i), anonAtomMatcher, anonBondMatcher) .matches(molecule)) { elemPatterns.remove(i); anonPatterns.remove(i); return templates.remove(i); } } return null; }
@Test public void napthaleneSubsearch() throws Exception { int[] match = Ullmann.findSubstructure(TestMoleculeFactory.makeNaphthalene()).match( TestMoleculeFactory.makeBenzene()); assertThat(match, is(new int[0])); int count = FluentIterable.from( Ullmann.findSubstructure(TestMoleculeFactory.makeNaphthalene()).matchAll( TestMoleculeFactory.makeBenzene())).size(); assertThat(count, is(0)); } }
@Test public void toAtomMap() throws Exception { IAtomContainer query = smi("CC"); IAtomContainer target = smi("CC"); Iterable<Map<IAtom, IAtom>> iterable = Pattern.findIdentical(query).matchAll(target).toAtomMap(); Iterator<Map<IAtom, IAtom>> iterator = iterable.iterator(); assertTrue(iterator.hasNext()); Map<IAtom, IAtom> m1 = iterator.next(); assertThat(m1.get(query.getAtom(0)), is(target.getAtom(0))); assertThat(m1.get(query.getAtom(1)), is(target.getAtom(1))); assertTrue(iterator.hasNext()); Map<IAtom, IAtom> m2 = iterator.next(); assertThat(m2.get(query.getAtom(0)), is(target.getAtom(1))); assertThat(m2.get(query.getAtom(1)), is(target.getAtom(0))); assertFalse(iterator.hasNext()); }
/** * Internal constructor. * * @param smarts pattern * @param builder the builder * @throws IOException the pattern could not be parsed */ private SmartsPattern(final String smarts, IChemObjectBuilder builder) throws IOException { this.query = new QueryAtomContainer(null); if (!Smarts.parse(query, smarts)) throw new IOException("Could not parse SMARTS: " + smarts); this.pattern = Pattern.findSubstructure(query); }
/** * Determine if there is a mapping of this pattern in the {@code target}. * Depending on the implementation stereochemistry may be checked * (recommended). * * <blockquote><pre> * Pattern pattern = ...; // create pattern * for (IAtomContainer m : ms) { * if (pattern.matches(m)) { * // found mapping! * } * } * </pre></blockquote> * * @param target the container to search for the pattern in * @return the mapping from the pattern to the target */ public boolean matches(IAtomContainer target) { return match(target).length > 0; }
@Test public void benzeneNonIdentical() throws Exception { int[] match = VentoFoggia.findIdentical(TestMoleculeFactory.makeBenzene()).match( TestMoleculeFactory.makeNaphthalene()); assertThat(match, is(new int[0])); int count = FluentIterable.from( VentoFoggia.findIdentical(TestMoleculeFactory.makeBenzene()).matchAll( TestMoleculeFactory.makeNaphthalene())).size(); assertThat(count, is(0)); }
@Test public void toBondMap() throws Exception { IAtomContainer query = smi("CCC"); IAtomContainer target = smi("CCC"); Iterable<Map<IBond, IBond>> iterable = Pattern.findIdentical(query).matchAll(target).toBondMap(); Iterator<Map<IBond, IBond>> iterator = iterable.iterator(); assertTrue(iterator.hasNext()); Map<IBond, IBond> m1 = iterator.next(); assertThat(m1.get(query.getBond(0)), is(target.getBond(0))); assertThat(m1.get(query.getBond(1)), is(target.getBond(1))); assertTrue(iterator.hasNext()); Map<IBond, IBond> m2 = iterator.next(); assertThat(m2.get(query.getBond(0)), is(target.getBond(1))); assertThat(m2.get(query.getBond(1)), is(target.getBond(0))); assertFalse(iterator.hasNext()); }
private void addTemplateMol(IAtomContainer mol) { templates.add(mol); QueryAtomContainer query = QueryAtomContainerCreator.createAnyAtomAnyBondContainer(mol, false); queries.add(query); for (int i = 0; i < mol.getAtomCount(); i++) { query.getAtom(i).setPoint3d(new Point3d(mol.getAtom(i).getPoint3d())); } patterns.add(Pattern.findSubstructure(query)); }
/** * Find the atoms that match this atom type. * * @param container structure representation * @return indices of atoms that matched this type */ private Set<Integer> matches(IAtomContainer container) { Set<Integer> matchedIdx = new HashSet<Integer>(); for (int[] mapping : pattern.matchAll(container)) { matchedIdx.add(mapping[0]); } return matchedIdx; } }
@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 benzeneSubsearch() throws Exception { int[] match = Ullmann.findSubstructure(TestMoleculeFactory.makeBenzene()).match( TestMoleculeFactory.makeNaphthalene()); assertThat(match, is(new int[]{2, 7, 6, 5, 4, 3})); int count = FluentIterable.from( Ullmann.findSubstructure(TestMoleculeFactory.makeBenzene()).matchAll( TestMoleculeFactory.makeNaphthalene())).size(); assertThat(count, is(6)); // note: aromatic one would be 24 }
@Test public void toAtomBondMap() throws Exception { IAtomContainer query = smi("CCC"); IAtomContainer target = smi("CCC"); Iterable<Map<IChemObject, IChemObject>> iterable = Pattern.findIdentical(query).matchAll(target).toAtomBondMap(); Iterator<Map<IChemObject, IChemObject>> iterator = iterable.iterator(); assertTrue(iterator.hasNext()); Map<IChemObject, IChemObject> m1 = iterator.next(); assertThat(m1.get(query.getAtom(0)), is((IChemObject)target.getAtom(0))); assertThat(m1.get(query.getAtom(1)), is((IChemObject)target.getAtom(1))); assertThat(m1.get(query.getAtom(2)), is((IChemObject)target.getAtom(2))); assertThat(m1.get(query.getBond(0)), is((IChemObject)target.getBond(0))); assertThat(m1.get(query.getBond(1)), is((IChemObject)target.getBond(1))); assertTrue(iterator.hasNext()); Map<IChemObject, IChemObject> m2 = iterator.next(); assertThat(m2.get(query.getAtom(0)), is((IChemObject)target.getAtom(2))); assertThat(m2.get(query.getAtom(1)), is((IChemObject)target.getAtom(1))); assertThat(m2.get(query.getAtom(2)), is((IChemObject)target.getAtom(0))); assertThat(m2.get(query.getBond(0)), is((IChemObject)target.getBond(1))); assertThat(m2.get(query.getBond(1)), is((IChemObject)target.getBond(0))); assertFalse(iterator.hasNext()); }
public IAtomContainer removeMolecule(IAtomContainer molecule) throws CDKException { for (int i = 0; i < templates.size(); i++) { if (VentoFoggia.findIdentical(templates.get(i), anonAtomMatcher, anonBondMatcher) .matches(molecule)) { elemPatterns.remove(i); anonPatterns.remove(i); return templates.remove(i); } } return null; }
/** * Internal constructor. * * @param smarts pattern * @param builder the builder */ private SmartsPattern(final String smarts, IChemObjectBuilder builder) { this.query = new QueryAtomContainer(builder); if (!Smarts.parse(query, smarts)) throw new IllegalArgumentException("Could not parse SMARTS: " + smarts + "\n" + Smarts.getLastErrorMesg() + "\n" + Smarts.getLastErrorLocation()); this.pattern = Pattern.findSubstructure(query); }
/** * Find all mappings of this pattern in the {@code target} reaction. * * <blockquote><pre> * Pattern pattern = Pattern.findSubstructure(query); * for (IReaction r : rs) { * for (int[] mapping : pattern.matchAll(r)) { * // found mapping * } * } * </pre></blockquote> * * The reaction is inlined into a molecule and vs mapped id's correspond * to the absolute atom index in the reaction when considered as reactants, agents, * products {@link ReactionManipulator#toMolecule}. * * @param target the reaction to search for the pattern in * @return the mapping from the pattern to the target * @see Mappings * @see ReactionManipulator#toMolecule(IReaction) */ public final Mappings matchAll(IReaction target) { return matchAll(ReactionManipulator.toMolecule(target)); }
@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 }
@Test public void benzeneIdentical() throws Exception { int[] match = VentoFoggia.findIdentical(TestMoleculeFactory.makeBenzene()).match( TestMoleculeFactory.makeBenzene()); assertThat(match, is(new int[]{0, 1, 2, 3, 4, 5})); int count = FluentIterable.from( VentoFoggia.findIdentical(TestMoleculeFactory.makeBenzene()) .matchAll(TestMoleculeFactory.makeBenzene())).size(); assertThat(count, is(6)); // note: aromatic one would be 12 }