/** * Convenience method to count the number of unique atom mappings. Note * mappings are lazily generated and checking the count and then iterating * over the mappings currently performs two searches. If the mappings are * also needed, it is more efficient to check the mappings and count * manually. * * The method is simply invokes <pre>mappings.uniqueAtoms().count()</pre>. * * @return number of matches */ public int countUnique() { return uniqueAtoms().count(); }
/** * Test whether the pattern matches at the provided atom. * * @param root the root atom of the molecule * @return the pattern matches */ public boolean matchesRoot(IAtom root) { return matchRoot(root).atLeast(1); }
/** *{@inheritDoc} */ @Override public int[] match(IAtomContainer container) { return matchAll(container).first(); }
/** * Efficiently determine if there are at least 'n' matches * * <blockquote><pre> * Mappings mappings = ...; * * if (mappings.atLeast(5)) * // set bit flag etc. * * // are the at least 5 unique matches? * if (mappings.uniqueAtoms().atLeast(5)) * // set bit etc. * </pre></blockquote> * * @param n number of matches * @return there are at least 'n' matches */ public boolean atLeast(int n) { return limit(n).count() == n; }
@Test @SuppressWarnings("unchecked") public void count() throws Exception { Iterable<int[]> iterable = mock(Iterable.class); Iterator<int[]> iterator = mock(Iterator.class); when(iterable.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, true, true, true, true, false); when(iterator.next()).thenReturn(new int[0]); Mappings ms = new Mappings(mock(IAtomContainer.class), mock(IAtomContainer.class), iterable); assertThat(ms.count(), is(5)); }
if (pattern.matchAll(container).uniqueAtoms().atLeast(key.count + 1)) fp.set(i);
for (Map<IAtom, IAtom> atoms : ptrn.matchAll(mol).uniqueAtoms().toAtomMap()) { for (Map.Entry<IAtom, IAtom> e : atoms.entrySet()) { e.getValue().setPoint2d(new Point2d(e.getKey().getPoint2d()));
.uniqueAtoms() .toAtomBondMap()) { boolean overlaps = false; IAtomContainer matched = molecule.getBuilder()
void assertMatch(IAtomContainer query, IAtomContainer target, int count) { assertThat(query.getTitle() + " should match " + target.getTitle() + " " + count + " times", create(query).matchAll(target).count(), is(count)); }
/** * Get the uniue matching pharmacophore groups. * * The method should be called after performing the match, otherwise the return value is null. * The method returns a List of List's. Each List represents the pharmacophore groups in the * target molecule that matched the query. Each pharmacophore group contains the indices of the * atoms (in the target molecule) that correspond to the group. * * This is analogous to the USA form of return value from a SMARTS match. * * @return a List of a List of pharmacophore groups in the target molecule that match the query * @see org.openscience.cdk.pharmacophore.PharmacophoreAtom */ public List<List<PharmacophoreAtom>> getUniqueMatchingPharmacophoreAtoms() { if (pharmacophoreMolecule == null || mappings == null) return null; return getPCoreAtoms(mappings.uniqueAtoms()); }
private List<List<PharmacophoreAtom>> getPCoreAtoms(Mappings mappings) { List<List<PharmacophoreAtom>> atoms = new ArrayList<>(); // XXX: re-subsearching the query for (Map<IAtom,IAtom> map : mappings.toAtomMap()) { List<PharmacophoreAtom> pcoreatoms = new ArrayList<>(); for (IAtom atom : map.values()) pcoreatoms.add((PharmacophoreAtom) AtomRef.deref(atom)); atoms.add(pcoreatoms); } return atoms; }
@Test @SuppressWarnings("unchecked") public void limit() throws Exception { Iterable<int[]> iterable = mock(Iterable.class); Iterator<int[]> iterator = mock(Iterator.class); when(iterable.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, true, true, true, true, false); when(iterator.next()).thenReturn(new int[0]); Mappings ms = new Mappings(mock(IAtomContainer.class), mock(IAtomContainer.class), iterable); assertThat(ms.limit(2).count(), is(2)); verify(iterator, atMost(2)).next(); // was only called twice }
@Test public void toSubstructures() throws Exception { IAtomContainer query = smi("O1CC1"); IAtomContainer target = smi("C1OC1CCC"); Iterable<IAtomContainer> iterable = Pattern.findSubstructure(query) .matchAll(target) .uniqueAtoms() .toSubstructures(); Iterator<IAtomContainer> iterator = iterable.iterator(); assertTrue(iterator.hasNext()); IAtomContainer submol = iterator.next(); assertThat(submol, is(not(query))); // note that indices are mapped from query to target assertThat(submol.getAtom(0), is(target.getAtom(1))); // oxygen assertThat(submol.getAtom(1), is(target.getAtom(0))); // C assertThat(submol.getAtom(2), is(target.getAtom(2))); // C assertThat(submol.getBond(0), is(target.getBond(0))); // C-O bond assertThat(submol.getBond(1), is(target.getBond(2))); // O-C bond assertThat(submol.getBond(2), is(target.getBond(1))); // C-C bond assertFalse(iterator.hasNext()); }
@Test @SuppressWarnings("unchecked") public void atLeast() throws Exception { Iterable<int[]> iterable = mock(Iterable.class); Iterator<int[]> iterator = mock(Iterator.class); when(iterable.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, true, true, true, true, false); when(iterator.next()).thenReturn(new int[0]); Mappings ms = new Mappings(mock(IAtomContainer.class), mock(IAtomContainer.class), iterable); assertTrue(ms.atLeast(2)); verify(iterator, atMost(2)).next(); // was only called twice }
/** * Atom-atom mapping of the input molecule to the bare container constructed from the InChI connection table. * This makes it possible to map the positions of the mobile hydrogens in the InChI back to the input molecule. * @param inchiMolGraph molecule (bare) as defined in InChI * @param mol user input molecule * @throws CDKException */ private void mapInputMoleculeToInchiMolgraph(IAtomContainer inchiMolGraph, IAtomContainer mol) throws CDKException { Iterator<Map<IAtom, IAtom>> iter = org.openscience.cdk.isomorphism.VentoFoggia.findIdentical(inchiMolGraph, AtomMatcher.forElement(), BondMatcher.forAny()) .matchAll(mol) .limit(1) .toAtomMap() .iterator(); if (iter.hasNext()) { for (Map.Entry<IAtom,IAtom> e : iter.next().entrySet()) { IAtom src = e.getKey(); IAtom dst = e.getValue(); String position = src.getID(); dst.setID(position); LOGGER.debug("Mapped InChI ", src.getSymbol(), " ", src.getID(), " to ", dst.getSymbol(), " " + dst.getID()); } } else { throw new IllegalArgumentException(CANSMI.create(inchiMolGraph) + " " + CANSMI.create(mol)); } }
public int countSubstructure(String smarts) throws CDKException { SmartsPattern ptrn = cache.get(smarts); if (ptrn == null) { ptrn = SmartsPattern.create(smarts); ptrn.setPrepare(false); cache.put(smarts, ptrn); } return ptrn.matchAll(mol).countUnique(); } }
@Test @SuppressWarnings("unchecked") public void first() throws Exception { Iterable<int[]> iterable = mock(Iterable.class); Iterator<int[]> iterator = mock(Iterator.class); when(iterable.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, true, false); int[] p1 = new int[0]; int[] p2 = new int[0]; when(iterator.next()).thenReturn(p1, new int[][]{p2}); Mappings ms = new Mappings(mock(IAtomContainer.class), mock(IAtomContainer.class), iterable); assertThat(ms.first(), is(sameInstance(p1))); }
@Test @SuppressWarnings("unchecked") public void filter() throws Exception { Iterable<int[]> iterable = mock(Iterable.class); Iterator<int[]> iterator = mock(Iterator.class); when(iterable.iterator()).thenReturn(iterator); int[] p1 = {0, 1, 2}; int[] p2 = {0, 2, 1}; int[] p3 = {0, 3, 4}; int[] p4 = {0, 4, 3}; when(iterator.hasNext()).thenReturn(true, true, true, true, false); when(iterator.next()).thenReturn(p1, p2, p3, p4); Mappings ms = new Mappings(mock(IAtomContainer.class), mock(IAtomContainer.class), iterable); Predicate<int[]> f = mock(Predicate.class); when(f.apply(p1)).thenReturn(false); when(f.apply(p2)).thenReturn(true); when(f.apply(p3)).thenReturn(false); when(f.apply(p4)).thenReturn(true); assertThat(ms.filter(f).toArray(), is(new int[][]{p2, p4})); }
private void assertMatch(String sma, String smiles, int numHits, int uniqNumHits) throws Exception { Pattern ptrn = SmartsPattern.create(sma); Mappings mappings = ptrn.matchAll(smi(smiles)); assertThat(mappings.count(), is(numHits)); assertThat(mappings.countUnique(), is(uniqNumHits)); }
@Test @SuppressWarnings("unchecked") public void countUnique() throws Exception { Iterable<int[]> iterable = mock(Iterable.class); Iterator<int[]> iterator = mock(Iterator.class); when(iterable.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, true, true, true, false); int[] p1 = {0, 1, 2}; int[] p2 = {0, 2, 1}; int[] p3 = {0, 3, 4}; int[] p4 = {0, 4, 3}; when(iterator.next()).thenReturn(p1, p2, p3, p4); Mappings ms = new Mappings(mock(IAtomContainer.class), mock(IAtomContainer.class), iterable); assertThat(ms.countUnique(), is(2)); }