Search in sources :

Example 1 with IQueryAtom

use of org.openscience.cdk.isomorphism.matchers.IQueryAtom in project ambit-mirror by ideaconsult.

the class SLN2ChemObject method slnContainerToQueryAtomContainer.

public IQueryAtomContainer slnContainerToQueryAtomContainer(SLNContainer slnContainer) {
    clearAllErrorsAndWarnings();
    IQueryAtomContainer container = new QueryAtomContainer(SilentChemObjectBuilder.getInstance());
    Map<SLNAtom, IQueryAtom> convertedAtoms = new HashMap<SLNAtom, IQueryAtom>();
    for (int i = 0; i < slnContainer.getAtomCount(); i++) {
        SLNAtom slnAtom = (SLNAtom) slnContainer.getAtom(i);
        IQueryAtom atom = slnAtomToQueryAtom(slnAtom);
        if (currentConversionWarning != null)
            conversionWarnings.add(currentConversionWarning + " for atom: " + (i + 1));
        if (atom == null) {
            conversionErrors.add(currentConversionError + " for atom: " + (i + 1));
            continue;
        }
        container.addAtom(atom);
        convertedAtoms.put(slnAtom, atom);
    }
    for (int i = 0; i < slnContainer.getBondCount(); i++) {
        SLNBond slnBbond = (SLNBond) slnContainer.getBond(i);
        IQueryBond bond = slnBondToQueryBond(slnBbond);
        if (currentConversionWarning != null)
            conversionWarnings.add(currentConversionWarning + " for bond: " + (i + 1));
        if (bond == null) {
            conversionErrors.add(currentConversionError + " for bond: " + (i + 1));
            continue;
        }
        IAtom[] newAtoms = new IAtom[2];
        newAtoms[0] = convertedAtoms.get(slnBbond.getAtom(0));
        newAtoms[1] = convertedAtoms.get(slnBbond.getAtom(1));
        if (newAtoms[0] == null || newAtoms[1] == null)
            // one of the atoms is not converted
            continue;
        bond.setAtoms(newAtoms);
        container.addBond(bond);
    }
    return container;
}
Also used : HashMap(java.util.HashMap) IQueryAtom(org.openscience.cdk.isomorphism.matchers.IQueryAtom) SLNBond(ambit2.sln.SLNBond) SLNAtom(ambit2.sln.SLNAtom) IQueryBond(org.openscience.cdk.isomorphism.matchers.IQueryBond) IQueryAtomContainer(org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer) QueryAtomContainer(org.openscience.cdk.isomorphism.matchers.QueryAtomContainer) IQueryAtomContainer(org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 2 with IQueryAtom

use of org.openscience.cdk.isomorphism.matchers.IQueryAtom in project ambit-mirror by ideaconsult.

the class SmartsParser method parseSpecialSymbol.

void parseSpecialSymbol() {
    switch(smarts.charAt(curChar)) {
        case // Any atom
        '*':
            IQueryAtom curAtom = new AnyAtom(SilentChemObjectBuilder.getInstance());
            curChar++;
            addAtom(curAtom);
            break;
        // Bond expression symbols - bond types and logical operations
        case '-':
        case '=':
        case '#':
        case '~':
        case '/':
        case '\\':
        case ':':
        case '@':
        case '!':
        case '&':
        case ',':
        case ';':
            parseBondExpression();
            break;
        case // Atom index which > 9 (has two digits)
        '%':
            parseAtomIndex();
            break;
        case '(':
            if (prevAtom == null) {
                if (FlagCLG) {
                    if (curComponent > 0) {
                        newError("Incorrect nested componet brackets", curChar + 1, "");
                    } else {
                        brackets.push(prevAtom);
                        maxCompNumber++;
                        curComponent = maxCompNumber;
                    }
                } else
                    newError("Component Level Grouping is off: incorrect openning brackect", curChar + 1, "");
            } else
                brackets.push(prevAtom);
            curChar++;
            break;
        case ')':
            if (brackets.empty()) {
                // System.out.println("curChar = " + curChar);
                newError("Incorrect closing brackect", curChar + 1, "");
                return;
            }
            ;
            // Not empty brackets stack guarantees that curChar > 0
            if (smarts.charAt(curChar - 1) == '(') {
                newError("Empty branch/substituent ", curChar + 1, "");
                // This prevents generation of another error
                brackets.pop();
                // "There are unclosed brackets"
                return;
            }
            ;
            prevAtom = brackets.pop();
            if (prevAtom == null)
                curComponent = 0;
            curChar++;
            break;
        case '[':
            parseAtomExpression();
            break;
        case ']':
            newError("Incorrect closing bracket ']' ", curChar + 1, "");
            break;
        case '.':
            if (FlagCLG) {
                curChar++;
                prevAtom = null;
                curBond = null;
                curBondType = SmartsConst.BT_UNDEFINED;
            } else
                newError("Zero bond order (disclosure) is not allowed. Component Level Grouping is off.", curChar + 1, "");
            break;
        default:
            newError("Incorrect symbol", curChar + 1, "");
            break;
    }
}
Also used : IQueryAtom(org.openscience.cdk.isomorphism.matchers.IQueryAtom) AnyAtom(org.openscience.cdk.isomorphism.matchers.smarts.AnyAtom)

Example 3 with IQueryAtom

use of org.openscience.cdk.isomorphism.matchers.IQueryAtom in project ambit-mirror by ideaconsult.

the class IsomorphismTester method setQueryAtomSequence.

void setQueryAtomSequence(IQueryAtom firstAt) {
    IQueryAtom firstAtom;
    QuerySequenceElement seqEl;
    TopLayer topLayer;
    // List<IQueryAtom> curAddedAtoms = new List<IQueryAtom>();
    int n;
    if (firstAt == null)
        firstAtom = (IQueryAtom) query.getFirstAtom();
    else
        firstAtom = firstAt;
    sequence.clear();
    sequencedAtoms.clear();
    sequencedBondAt1.clear();
    sequencedBondAt2.clear();
    // Set first sequence atom
    sequencedAtoms.add(firstAtom);
    seqEl = new QuerySequenceElement();
    seqEl.center = firstAtom;
    topLayer = (TopLayer) firstAtom.getProperty(TopLayer.TLProp);
    n = topLayer.atoms.size();
    seqEl.atoms = new IQueryAtom[n];
    seqEl.bonds = new IQueryBond[n];
    for (int i = 0; i < n; i++) {
        sequencedAtoms.add((IQueryAtom) topLayer.atoms.get(i));
        seqEl.atoms[i] = (IQueryAtom) topLayer.atoms.get(i);
        seqEl.bonds[i] = (IQueryBond) topLayer.bonds.get(i);
        addSeqBond(seqEl.center, seqEl.atoms[i]);
    }
    sequence.add(seqEl);
    // Sequencing the entire query structure
    Stack<QuerySequenceElement> stack = new Stack<QuerySequenceElement>();
    stack.push(seqEl);
    while (!stack.empty()) {
        // curAddedAtoms.clear();
        QuerySequenceElement curSeqAt = stack.pop();
        for (int i = 0; i < curSeqAt.atoms.length; i++) {
            topLayer = (TopLayer) curSeqAt.atoms[i].getProperty(TopLayer.TLProp);
            if (topLayer.atoms.size() == 1)
                // it is terminal atom and no further sequencing should be done
                continue;
            int[] a = getSeqAtomsInLayer(topLayer);
            n = 0;
            for (int k = 0; k < a.length; k++) if (a[k] == 0)
                n++;
            if (n > 0) {
                seqEl = new QuerySequenceElement();
                seqEl.center = curSeqAt.atoms[i];
                seqEl.atoms = new IQueryAtom[n];
                seqEl.bonds = new IQueryBond[n];
                sequence.add(seqEl);
                stack.add(seqEl);
            }
            int j = 0;
            for (int k = 0; k < a.length; k++) {
                if (a[k] == 0) {
                    seqEl.atoms[j] = (IQueryAtom) topLayer.atoms.get(k);
                    seqEl.bonds[j] = (IQueryBond) topLayer.bonds.get(k);
                    addSeqBond(seqEl.center, seqEl.atoms[j]);
                    sequencedAtoms.add(seqEl.atoms[j]);
                    // curAddedAtoms.add(seqEl.atoms[j]);
                    j++;
                } else {
                    if (curSeqAt.center == topLayer.atoms.get(k))
                        continue;
                    // is already sequenced
                    if (getSeqBond(curSeqAt.atoms[i], topLayer.atoms.get(k)) != -1)
                        continue;
                    // topLayer.atoms.get(k) atom is already sequenced.
                    // Therefore sequnce element of 'bond' type is registered.
                    // newSeqEl is not added in the stack (this is not needed for this bond)
                    QuerySequenceElement newSeqEl = new QuerySequenceElement();
                    newSeqEl.center = null;
                    newSeqEl.atoms = new IQueryAtom[2];
                    newSeqEl.bonds = new IQueryBond[1];
                    newSeqEl.atoms[0] = curSeqAt.atoms[i];
                    newSeqEl.atoms[1] = (IQueryAtom) topLayer.atoms.get(k);
                    addSeqBond(newSeqEl.atoms[0], newSeqEl.atoms[1]);
                    newSeqEl.bonds[0] = (IQueryBond) topLayer.bonds.get(k);
                    sequence.add(newSeqEl);
                }
            }
        }
    }
    for (int i = 0; i < sequence.size(); i++) sequence.get(i).setAtomNums(query);
}
Also used : IQueryAtom(org.openscience.cdk.isomorphism.matchers.IQueryAtom) Stack(java.util.Stack)

Example 4 with IQueryAtom

use of org.openscience.cdk.isomorphism.matchers.IQueryAtom in project ambit-mirror by ideaconsult.

the class SmartsIsomorphismTester method setQueryAtomSequence.

void setQueryAtomSequence(IQueryAtom firstAt) {
    IQueryAtom firstAtom;
    QuerySequenceElement seqEl;
    TopLayer topLayer;
    int n;
    if (firstAt == null)
        firstAtom = (IQueryAtom) query.getFirstAtom();
    else
        firstAtom = firstAt;
    sequence.clear();
    sequencedAtoms.clear();
    sequencedBondAt1.clear();
    sequencedBondAt2.clear();
    // Set first sequence atom
    sequencedAtoms.add(firstAtom);
    seqEl = new QuerySequenceElement();
    seqEl.center = firstAtom;
    topLayer = (TopLayer) firstAtom.getProperty(TopLayer.TLProp);
    n = topLayer.atoms.size();
    seqEl.atoms = new IQueryAtom[n];
    seqEl.bonds = new IQueryBond[n];
    for (int i = 0; i < n; i++) {
        sequencedAtoms.add((IQueryAtom) topLayer.atoms.get(i));
        seqEl.atoms[i] = (IQueryAtom) topLayer.atoms.get(i);
        seqEl.bonds[i] = (IQueryBond) topLayer.bonds.get(i);
        addSeqBond(seqEl.center, seqEl.atoms[i]);
    }
    sequence.add(seqEl);
    // Sequencing the entire query structure
    Stack<QuerySequenceElement> stack = new Stack<QuerySequenceElement>();
    stack.push(seqEl);
    while (!stack.empty()) {
        // curAddedAtoms.clear();
        QuerySequenceElement curSeqAt = stack.pop();
        for (int i = 0; i < curSeqAt.atoms.length; i++) {
            topLayer = (TopLayer) curSeqAt.atoms[i].getProperty(TopLayer.TLProp);
            if (topLayer.atoms.size() == 1)
                // it is terminal atom and no further sequencing should be done
                continue;
            int[] a = getSeqAtomsInLayer(topLayer);
            n = 0;
            for (int k = 0; k < a.length; k++) if (a[k] == 0)
                n++;
            if (n > 0) {
                seqEl = new QuerySequenceElement();
                seqEl.center = curSeqAt.atoms[i];
                seqEl.atoms = new IQueryAtom[n];
                seqEl.bonds = new IQueryBond[n];
                sequence.add(seqEl);
                stack.add(seqEl);
            }
            int j = 0;
            for (int k = 0; k < a.length; k++) {
                if (a[k] == 0) {
                    seqEl.atoms[j] = (IQueryAtom) topLayer.atoms.get(k);
                    seqEl.bonds[j] = (IQueryBond) topLayer.bonds.get(k);
                    addSeqBond(seqEl.center, seqEl.atoms[j]);
                    sequencedAtoms.add(seqEl.atoms[j]);
                    // curAddedAtoms.add(seqEl.atoms[j]);
                    j++;
                } else {
                    if (curSeqAt.center == topLayer.atoms.get(k))
                        continue;
                    // is already sequenced
                    if (getSeqBond(curSeqAt.atoms[i], topLayer.atoms.get(k)) != -1)
                        continue;
                    // topLayer.atoms.get(k) atom is already sequenced.
                    // Therefore sequnce element of 'bond' type is registered.
                    // newSeqEl is not added in the stack (this is not needed for this bond)
                    QuerySequenceElement newSeqEl = new QuerySequenceElement();
                    newSeqEl.center = null;
                    newSeqEl.atoms = new IQueryAtom[2];
                    newSeqEl.bonds = new IQueryBond[1];
                    newSeqEl.atoms[0] = curSeqAt.atoms[i];
                    newSeqEl.atoms[1] = (IQueryAtom) topLayer.atoms.get(k);
                    addSeqBond(newSeqEl.atoms[0], newSeqEl.atoms[1]);
                    newSeqEl.bonds[0] = (IQueryBond) topLayer.bonds.get(k);
                    sequence.add(newSeqEl);
                }
            }
        }
    }
    for (int i = 0; i < sequence.size(); i++) sequence.get(i).setAtomNums(query);
}
Also used : QuerySequenceElement(ambit2.smarts.QuerySequenceElement) IQueryAtom(org.openscience.cdk.isomorphism.matchers.IQueryAtom) TopLayer(ambit2.smarts.TopLayer) Stack(java.util.Stack)

Example 5 with IQueryAtom

use of org.openscience.cdk.isomorphism.matchers.IQueryAtom in project cdk by cdk.

the class AtomMatcherTest method queryMatch.

@Test
public void queryMatch() throws Exception {
    AtomMatcher matcher = AtomMatcher.forQuery();
    IQueryAtom atom1 = mock(IQueryAtom.class);
    IAtom atom2 = mock(IAtom.class);
    IAtom atom3 = mock(IAtom.class);
    when(atom1.matches(atom2)).thenReturn(true);
    when(atom1.matches(atom3)).thenReturn(false);
    assertTrue(matcher.matches(atom1, atom2));
    assertFalse(matcher.matches(atom1, atom3));
}
Also used : IQueryAtom(org.openscience.cdk.isomorphism.matchers.IQueryAtom) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test)

Aggregations

IQueryAtom (org.openscience.cdk.isomorphism.matchers.IQueryAtom)44 IAtom (org.openscience.cdk.interfaces.IAtom)26 IQueryAtomContainer (org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer)14 IBond (org.openscience.cdk.interfaces.IBond)12 IQueryBond (org.openscience.cdk.isomorphism.matchers.IQueryBond)10 CDKException (org.openscience.cdk.exception.CDKException)9 List (java.util.List)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 QueryAtomContainer (org.openscience.cdk.isomorphism.matchers.QueryAtomContainer)4 AnyAtom (org.openscience.cdk.isomorphism.matchers.smarts.AnyAtom)4 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)3 RMap (org.openscience.cdk.isomorphism.mcss.RMap)3 SLNAtom (ambit2.sln.SLNAtom)2 BitSet (java.util.BitSet)2 LinkedHashMap (java.util.LinkedHashMap)2 Stack (java.util.Stack)2 IChemObjectBuilder (org.openscience.cdk.interfaces.IChemObjectBuilder)2 SmartsStereoMatch (org.openscience.cdk.isomorphism.SmartsStereoMatch)2