Java Code Examples for org.openscience.cdk.interfaces.IAtomContainer#getBond()

The following examples show how to use org.openscience.cdk.interfaces.IAtomContainer#getBond() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: McGregorChecks.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 *
 * @param corresponding_atom
 * @param new_symbol
 * @param neighbor_bondnum
 * @param atomContainer
 * @param c_bond_neighbors
 * @return bond index
 */
protected static int changeCharBonds(int corresponding_atom, String new_symbol, int neighbor_bondnum,
        IAtomContainer atomContainer, List<String> c_bond_neighbors) {
    for (int atomIndex = 0; atomIndex < neighbor_bondnum; atomIndex++) {
        IBond bond = atomContainer.getBond(atomIndex);
        if ((atomContainer.indexOf(bond.getAtom(0)) == corresponding_atom)
                && (c_bond_neighbors.get(atomIndex * 4 + 2).compareToIgnoreCase("X") == 0)) {
            c_bond_neighbors.set(atomIndex * 4 + 2, c_bond_neighbors.get(atomIndex * 4 + 0));
            c_bond_neighbors.set(atomIndex * 4 + 0, new_symbol);
        }

        if ((atomContainer.indexOf(bond.getAtom(1)) == corresponding_atom)
                && (c_bond_neighbors.get(atomIndex * 4 + 3).compareToIgnoreCase("X") == 0)) {
            c_bond_neighbors.set(atomIndex * 4 + 3, c_bond_neighbors.get(atomIndex * 4 + 1));
            c_bond_neighbors.set(atomIndex * 4 + 1, new_symbol);
        }

    }

    return 0;
}
 
Example 2
Source File: MoleculeHandler.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void setIntConnectionTable() {

        IAtomContainer ac = (IAtomContainer) getAtomContainer();

        for (int i = 0; i < bondNumber; i++) {
            IBond bond = ac.getBond(i);
            /*This will fetch the connected ATOM as integer and its Bond order ex: 2 as double, 1 as single */
            // System.out.println(ac.indexOf(bond.getAtom(0))+" "+ac.indexOf(bond.getAtom(1))+" "+(int)bond.getOrder());
            intTable.add((ac.indexOf(bond.getAtom(0)) + 1));//Plus one because Java Indexing is one less
            intTable.add((ac.indexOf(bond.getAtom(1)) + 1));//Plus one because Java indexing is one less
            if (this.bm) {
                intTable.add((int) bond.getOrder().numeric());
            } else {
                intTable.add(1);
            }

            /*This will fetch the Connected ATOM Symbol*/
//            System.out.println(bond.getAtom(0).getSymbol() + " " + bond.getAtom(1).getSymbol()
//                    + " , bond: " + (int) bond.getOrder().numeric() + " Stored: " + intTable.get(i * 3 + 2));
        }
    }
 
Example 3
Source File: GeometryTools.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Calculate the median bond length of an atom container.
 *
 * @param container structure representation
 * @return median bond length
 * @throws java.lang.IllegalArgumentException unset coordinates or no bonds
 */
public static double getBondLengthMedian(final IAtomContainer container) {
    if (container.getBondCount() == 0) {
        throw new IllegalArgumentException("Container has no bonds.");
    }
    double[] lengths = new double[container.getBondCount()];
    for (int i = 0; i < container.getBondCount(); i++) {
        final IBond bond = container.getBond(i);
        final IAtom atom1 = bond.getBegin();
        final IAtom atom2 = bond.getEnd();
        if (atom1.getPoint2d() == null || atom2.getPoint2d() == null) {
            throw new IllegalArgumentException("An atom has no 2D coordinates.");
        }
        lengths[i] = getLength2D(bond);
    }
    Arrays.sort(lengths);
    return lengths[lengths.length / 2];
}
 
Example 4
Source File: RBlastSmilesGenerator.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Append the symbol for the bond order between <code>a1</code> and
 * <code>a2</code> to the <code>line</code>.
 *
 * @param line the StringBuffer that the bond symbol is appended to.
 * @param a1 Atom participating in the bond.
 * @param a2 Atom participating in the bond.
 * @param atomContainer the GraphAtomContainer that the SMILES string is
 * generated for.
 * @param useAromaticity true=aromaticity or sp2 will trigger lower case
 * letters, wrong=only sp2
 */
private void parseBond(StringBuffer line, IAtom a1, IAtom a2, IAtomContainer atomContainer, boolean useAromaticity) {
    //LOGGER.debug("in parseBond()");
    if (useAromaticity && a1.getFlag(ISAROMATIC) && a2.getFlag(ISAROMATIC)) {
        return;
    }
    if (atomContainer.getBond(a1, a2) == null) {
        return;
    }
    IBond.Order type = atomContainer.getBond(a1, a2).getOrder();
    if (null != type) {
        switch (type) {
            case SINGLE:
                break;
            case DOUBLE:
                line.append("=");
                break;
            case TRIPLE:
                line.append("#");
                break;
            // //LOGGER.debug("Unknown bond type");
            default:
                break;
        }
    }
}
 
Example 5
Source File: ValencyCalculator.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 *
 * @param m
 * @param atom
 * @param skipHydrogen
 * @return
 * @throws CDKException
 */
public static Integer getFreeValenceElectrons(IAtomContainer m, IAtom atom, boolean skipHydrogen) throws CDKException {
    initialize();
    Integer totalConnectedBondOrder = 0;
    List<IAtom> connectedAtoms = m.getConnectedAtomsList(atom);
    int counterH = 0;
    for (IAtom connAtom : connectedAtoms) {
        if (skipHydrogen && connAtom.getSymbol().equalsIgnoreCase("H")) {
            counterH++;
        }
        IBond bond = m.getBond(atom, connAtom);
        totalConnectedBondOrder += convertBondOrder(bond);
    }
    Integer charge = Objects.equals(atom.getFormalCharge(), UNSET) ? 0 : atom.getFormalCharge();
    return skipHydrogen ? (getValenceElectron(atom) - totalConnectedBondOrder + counterH - charge)
            : (getValenceElectron(atom) - totalConnectedBondOrder - charge);
}
 
Example 6
Source File: BaseMapping.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 */
@Override
public synchronized boolean isStereoMisMatch() {
    boolean flag = false;
    IAtomContainer reactant = getQuery();
    IAtomContainer product = getTarget();
    int stereoMisMatchScore = 0;
    if (getMappingCount() > 0) {
        AtomAtomMapping firstAtomMCS = getMCSList().iterator().next();
        for (IAtom indexI : firstAtomMCS.getMappingsByAtoms().keySet()) {
            IAtom indexJ = firstAtomMCS.getMappingsByAtoms().get(indexI);
            for (IAtom indexIPlus : firstAtomMCS.getMappingsByAtoms().keySet()) {
                IAtom indexJPlus = firstAtomMCS.getMappingsByAtoms().get(indexIPlus);
                if (!indexI.equals(indexIPlus) && !indexJ.equals(indexJPlus)) {

                    IAtom sourceAtom1 = indexI;
                    IAtom sourceAtom2 = indexIPlus;
                    IBond rBond = reactant.getBond(sourceAtom1, sourceAtom2);

                    IAtom targetAtom1 = indexJ;
                    IAtom targetAtom2 = indexJPlus;
                    IBond pBond = product.getBond(targetAtom1, targetAtom2);

                    if ((rBond != null && pBond != null)
                            && (rBond.getStereo() != pBond.getStereo())) {
                        stereoMisMatchScore++;
                    }
                }
            }
        }
    }
    if (stereoMisMatchScore > 0) {
        flag = true;
    }
    return flag;
}
 
Example 7
Source File: McGregor.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private synchronized void extendMapping(IAtomContainer source, int xIndex, int yIndex, McgregorHelper mcGregorHelper, List<Integer> additional_mapping, List<Integer> currentMapping) {

        int atom1_moleculeA = mcGregorHelper.getiBondNeighborAtomsA().get(xIndex * 3 + 0);
        int atom2_moleculeA = mcGregorHelper.getiBondNeighborAtomsA().get(xIndex * 3 + 1);
        int atom1_moleculeB = mcGregorHelper.getiBondNeighborAtomsB().get(yIndex * 3 + 0);
        int atom2_moleculeB = mcGregorHelper.getiBondNeighborAtomsB().get(yIndex * 3 + 1);

        IAtom R1_A = source.getAtom(atom1_moleculeA);
        IAtom R2_A = source.getAtom(atom2_moleculeA);
        IBond reactantBond = source.getBond(R1_A, R2_A);

        IAtom P1_B = target.getAtom(atom1_moleculeB);
        IAtom P2_B = target.getAtom(atom2_moleculeB);
        IBond productBond = target.getBond(P1_B, P2_B);

//      Bond Order Check Introduced by Asad
        if (AtomBondMatcher.matchAtomAndBond(reactantBond, productBond, atomMatcher, bondMatcher, true)) {

            for (int indexZ = 0; indexZ < mcGregorHelper.getMappedAtomCount(); indexZ++) {

                int Mapped_Atom_1 = currentMapping.get(indexZ * 2 + 0);
                int Mapped_Atom_2 = currentMapping.get(indexZ * 2 + 1);

                if ((Mapped_Atom_1 == atom1_moleculeA) && (Mapped_Atom_2 == atom1_moleculeB)) {
                    additional_mapping.add(atom2_moleculeA);
                    additional_mapping.add(atom2_moleculeB);
                } else if ((Mapped_Atom_1 == atom1_moleculeA) && (Mapped_Atom_2 == atom2_moleculeB)) {
                    additional_mapping.add(atom2_moleculeA);
                    additional_mapping.add(atom1_moleculeB);
                } else if ((Mapped_Atom_1 == atom2_moleculeA) && (Mapped_Atom_2 == atom1_moleculeB)) {
                    additional_mapping.add(atom1_moleculeA);
                    additional_mapping.add(atom2_moleculeB);
                } else if ((Mapped_Atom_1 == atom2_moleculeA) && (Mapped_Atom_2 == atom2_moleculeB)) {
                    additional_mapping.add(atom1_moleculeA);
                    additional_mapping.add(atom1_moleculeB);
                }
            }//for loop
        }
    }
 
Example 8
Source File: MoleculeHandler.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void setCharConnectionTable() {
    IAtomContainer ac = (IAtomContainer) getAtomContainer();
    for (int i = 0; i < bondNumber; i++) {
        IBond bond = ac.getBond(i);
        /*This will fetch the Connected ATOM Symbol*/
        String atom1 = bond.getAtom(0).getSymbol();
        String atom2 = bond.getAtom(1).getSymbol();
        charTable.add(atom1);
        charTable.add(atom2);
    }
}
 
Example 9
Source File: BEMatrix.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param a
 * @param b
 * @return
 * @throws CDKException
 */
public double getBondOrder(IAtom a, IAtom b) throws CDKException {
    double bondOrder = 0;
    IBond bond;
    for (int i = 0; i < myMoleculeSet.getAtomContainerCount(); i++) {
        IAtomContainer m = myMoleculeSet.getAtomContainer(i);
        bond = m.getBond(a, b);
        if (bond != null) {
            return convertBondOrder(bond);
        }
    }
    return bondOrder;
}
 
Example 10
Source File: RBlastSmilesGenerator.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Says if an atom is the end of a double bond configuration
 *
 * @param atom The atom which is the end of configuration
 * @param container The atomContainer the atom is in
 * @param parent The atom we came from
 * @param doubleBondConfiguration The array indicating where double bond
 * configurations are specified (this method ensures that there is actually
 * the possibility of a double bond configuration)
 * @return false=is not end of configuration, true=is
 */
private boolean isEndOfDoubleBond(IAtomContainer container, IAtom atom, IAtom parent, boolean[] doubleBondConfiguration) {
    IBond bond = container.getBond(atom, parent);
    if (bond != null
            || doubleBondConfiguration.length <= container.indexOf(bond)
            || !doubleBondConfiguration[container.indexOf(bond)]) {
        return false;
    }
    // TO-DO: We make the silent assumption of unset hydrogen count equals zero hydrogen count here.
    int lengthAtom = container.getConnectedBondsCount(atom) + ((Objects.equals(atom.getImplicitHydrogenCount(), UNSET)) ? 0 : atom.getImplicitHydrogenCount());
    // TO-DO: We make the silent assumption of unset hydrogen count equals zero hydrogen count here.
    int lengthParent = container.getConnectedBondsCount(parent) + ((Objects.equals(parent.getImplicitHydrogenCount(), UNSET)) ? 0 : parent.getImplicitHydrogenCount());
    if (container.getBond(atom, parent) != null) {
        if (container.getBond(atom, parent).getOrder() == IBond.Order.DOUBLE
                && (lengthAtom == 3 || (lengthAtom == 2 && atom.getSymbol().equals("N")))
                && (lengthParent == 3 || (lengthParent == 2 && parent.getSymbol().equals("N")))) {
            List<IAtom> atoms = container.getConnectedAtomsList(atom);
            IAtom one = null;
            IAtom two = null;
            IAtom atomi = null;
            for (int i = 0; i < atoms.size(); i++) {
                atomi = container.getAtom(i);
                if (atomi != parent && one == null) {
                    one = atomi;
                } else if (atomi != parent && one != null) {
                    two = atomi;
                }
            }
            String[] morgannumbers = getMorganNumbersWithElementSymbol(container);
            if ((one != null && two == null && atom.getSymbol().equals("N") && abs(giveAngleBothMethods(parent, atom, one, true)) > PI / 10) || (!atom.getSymbol().equals("N") && one != null && two != null && !morgannumbers[container.indexOf(one)].equals(morgannumbers[container.indexOf(two)]))) {
                return (true);
            } else {
                return (false);
            }
        }
    }
    return (false);
}
 
Example 11
Source File: RBlastSmilesGenerator.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Says if an atom is the start of a double bond configuration
 *
 * @param a The atom which is the start of configuration
 * @param container The atomContainer the atom is in
 * @param parent The atom we came from
 * @param doubleBondConfiguration The array indicating where double bond
 * configurations are specified (this method ensures that there is actually
 * the possibility of a double bond configuration)
 * @return false=is not start of configuration, true=is
 */
private boolean isStartOfDoubleBond(IAtomContainer container, IAtom a, IAtom parent, boolean[] doubleBondConfiguration) {
    // TO-DO: We make the silent assumption of unset hydrogen count equals zero hydrogen count here.
    int lengthAtom = container.getConnectedBondsCount(a) + ((Objects.equals(a.getImplicitHydrogenCount(), UNSET)) ? 0 : a.getImplicitHydrogenCount());
    if (lengthAtom != 3 && (lengthAtom != 2 && !a.getSymbol().equals("N"))) {
        return (false);
    }
    List<IAtom> atoms = container.getConnectedAtomsList(a);
    IAtom one = null;
    IAtom two = null;
    boolean doubleBond = false;
    IAtom nextAtom = null;
    for (IAtom atomi : atoms) {
        if (atomi != parent && container.getBond(atomi, a).getOrder() == IBond.Order.DOUBLE
                && isEndOfDoubleBond(container, atomi, a, doubleBondConfiguration)) {
            doubleBond = true;
            nextAtom = atomi;
        }
        if (atomi != nextAtom && one == null) {
            one = atomi;
        } else if (atomi != nextAtom && one != null) {
            two = atomi;
        }
    }
    String[] morgannumbers = getMorganNumbersWithElementSymbol(container);

    IBond bond = container.getBond(a, nextAtom);
    if (bond == null) {
        return false;
    }
    if (one != null && ((!a.getSymbol().equals("N") && two != null
            && !morgannumbers[container.indexOf(one)].equals(morgannumbers[container.indexOf(two)])
            && doubleBond && doubleBondConfiguration[container.indexOf(bond)])
            || (doubleBond && a.getSymbol().equals("N") && abs(giveAngleBothMethods(nextAtom, a, parent, true)) > PI / 10))) {
        return (true);
    } else {
        return (false);
    }
}
 
Example 12
Source File: McGregorChecks.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 *
 * @param source
 * @param target
 * @param neighborBondNumA
 * @param neighborBondNumB
 * @param i_bond_neighbor_atoms_A
 * @param i_bond_neighbor_atoms_B
 * @param cBondNeighborsA
 * @param cBondNeighborsB
 * @param modifiedARCS
 * @return List
 */
protected static List<Integer> setArcs(IAtomContainer source,
        IAtomContainer target,
        int neighborBondNumA,
        int neighborBondNumB,
        List<Integer> i_bond_neighbor_atoms_A,
        List<Integer> i_bond_neighbor_atoms_B,
        List<String> cBondNeighborsA,
        List<String> cBondNeighborsB,
        List<Integer> modifiedARCS,
        AtomMatcher atomMatcher,
        BondMatcher bondMatcher) {

    for (int row = 0; row < neighborBondNumA; row++) {
        for (int column = 0; column < neighborBondNumB; column++) {

            String G1A = cBondNeighborsA.get(row * 4 + 0);
            String G2A = cBondNeighborsA.get(row * 4 + 1);
            String G1B = cBondNeighborsB.get(column * 4 + 0);
            String G2B = cBondNeighborsB.get(column * 4 + 1);

            if (McGregorChecks.isAtomMatch(G1A, G2A, G1B, G2B)) {

                int Index_I = i_bond_neighbor_atoms_A.get(row * 3 + 0);
                int Index_IPlus1 = i_bond_neighbor_atoms_A.get(row * 3 + 1);

                IAtom R1_A = source.getAtom(Index_I);
                IAtom R2_A = source.getAtom(Index_IPlus1);
                IBond reactantBond = source.getBond(R1_A, R2_A);

                int Index_J = i_bond_neighbor_atoms_B.get(column * 3 + 0);
                int Index_JPlus1 = i_bond_neighbor_atoms_B.get(column * 3 + 1);

                IAtom P1_B = target.getAtom(Index_J);
                IAtom P2_B = target.getAtom(Index_JPlus1);
                IBond productBond = target.getBond(P1_B, P2_B);
                if (AtomBondMatcher.matchAtomAndBond(reactantBond, productBond, atomMatcher, bondMatcher, true)) {
                    modifiedARCS.set(row * neighborBondNumB + column, 1);
                }
            }
        }
    }
    return modifiedARCS;
}
 
Example 13
Source File: TargetProcessor.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void process(
        IAtomContainer target,
        List<Integer> unmapped_atoms_molB,
        int mappingSize,
        List<Integer> i_bond_setB,
        List<String> c_bond_setB,
        List<Integer> mapped_atoms,
        int counter) {

    int unmapped_numB = unmapped_atoms_molB.size();
    boolean bond_considered = false;
    boolean normal_bond = true;

    for (int atomIndex = 0; atomIndex < target.getBondCount(); atomIndex++) {

        Integer indexI = target.indexOf(target.getBond(atomIndex).getAtom(0));
        Integer indexJ = target.indexOf(target.getBond(atomIndex).getAtom(1));
        IBond bond = target.getBond(atomIndex);
        Integer order = null;
        if (!(bond instanceof IQueryBond)) {
            order = (bond.getOrder().numeric());
        } else {
            IQueryBond queryBond = (IQueryBond) bond;
            order = queryBond.getOrder() != null ? (queryBond.getOrder().numeric()) : null;
        }

        for (int b = 0; b < unmapped_numB; b++) {
            if (unmapped_atoms_molB.get(b).equals(indexI)) {
                normal_bond = unMappedAtomsEqualsIndexI(target, mappingSize, atomIndex, counter, mapped_atoms, indexI, indexJ, order);
                bond_considered = true;
            } else if (unmapped_atoms_molB.get(b).equals(indexJ)) {
                normal_bond = unMappedAtomsEqualsIndexJ(target, mappingSize, atomIndex, counter, mapped_atoms, indexI, indexJ, order);
                bond_considered = true;
            }

            if (normal_bond && bond_considered) {
                markNormalBonds(atomIndex, i_bond_setB, c_bond_setB, indexI, indexJ, order);
                normal_bond = true;
                break;
            }

        }
        bond_considered = false;
    }

}
 
Example 14
Source File: CDKMCS.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * This makes a map of matching atoms out of a map of matching bonds as
 * produced by the get(Subgraph|Ismorphism)Map methods.
 *
 * @param l The list produced by the getMap method.
 * @param g1 first molecule. Must not be an {@link IQueryAtomContainer}.
 * @param g2 second molecule. May be an {@link IQueryAtomContainer}.
 * @return The mapping found projected on g1. This is a {@link List} of
 * {@link CDKRMap} objects containing Ids of matching atoms.
 */
private static List<CDKRMap> makeAtomsMapOfBondsMap(List<CDKRMap> l, IAtomContainer g1, IAtomContainer g2) {
    if (l == null) {
        return (l);
    }
    List<CDKRMap> result = new ArrayList<>();
    for (int i = 0; i < l.size(); i++) {
        IBond bond1 = g1.getBond(l.get(i).getId1());
        IBond bond2 = g2.getBond(l.get(i).getId2());
        IAtom[] atom1 = BondManipulator.getAtomArray(bond1);
        IAtom[] atom2 = BondManipulator.getAtomArray(bond2);
        for (int j = 0; j < 2; j++) {
            List<IBond> bondsConnectedToAtom1j = g1.getConnectedBondsList(atom1[j]);
            for (int k = 0; k < bondsConnectedToAtom1j.size(); k++) {
                if (bondsConnectedToAtom1j.get(k) != bond1) {
                    IBond testBond = bondsConnectedToAtom1j.get(k);
                    for (int m = 0; m < l.size(); m++) {
                        IBond testBond2;
                        if (l.get(m).getId1() == g1.indexOf(testBond)) {
                            testBond2 = g2.getBond(l.get(m).getId2());
                            for (int n = 0; n < 2; n++) {
                                List<IBond> bondsToTest = g2.getConnectedBondsList(atom2[n]);
                                if (bondsToTest.contains(testBond2)) {
                                    CDKRMap map;
                                    if (j == n) {
                                        map = new CDKRMap(g1.indexOf(atom1[0]), g2.indexOf(atom2[0]));
                                    } else {
                                        map = new CDKRMap(g1.indexOf(atom1[1]), g2.indexOf(atom2[0]));
                                    }
                                    if (!result.contains(map)) {
                                        result.add(map);
                                    }
                                    CDKRMap map2;
                                    if (j == n) {
                                        map2 = new CDKRMap(g1.indexOf(atom1[1]), g2.indexOf(atom2[1]));
                                    } else {
                                        map2 = new CDKRMap(g1.indexOf(atom1[0]), g2.indexOf(atom2[1]));
                                    }
                                    if (!result.contains(map2)) {
                                        result.add(map2);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return result;
}
 
Example 15
Source File: MappingHandler.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * This makes a map of matching atoms out of a map of matching bonds as
 * produced by the get(Subgraph|Ismorphism)Map methods.
 *
 * @param l The list produced by the getMap method.
 * @param g1 first molecule. Must not be an {@link IQueryAtomContainer}.
 * @param g2 second molecule. May be an {@link IQueryAtomContainer}.
 * @param shouldMatchRings
 * @param matchAtomTypes
 * @return The mapping found projected on g1. This is a {@link List} of
 * {@link RMap} objects containing Ids of matching atoms.
 */
public static Map<Integer, Integer> makeAtomsMapOfBondsMap(
        Map<Integer, Integer> l, IAtomContainer g1, IAtomContainer g2,
        AtomMatcher am, BondMatcher bm) {
    if (l == null) {
        return (new TreeMap<>());
    }
    Map<Integer, Integer> result = new TreeMap<>();
    for (Map.Entry<Integer, Integer> map : l.entrySet()) {
        IBond bond1 = g1.getBond(map.getKey());
        IBond bond2 = g2.getBond(map.getValue());
        IAtom[] atom1 = BondManipulator.getAtomArray(bond1);
        IAtom[] atom2 = BondManipulator.getAtomArray(bond2);
        for (int j = 0; j < 2; j++) {
            List<IBond> bondsConnectedToAtom1j = g1.getConnectedBondsList(atom1[j]);
            for (int k = 0; k < bondsConnectedToAtom1j.size(); k++) {
                if (!bondsConnectedToAtom1j.get(k).equals(bond1)) {
                    IBond testBond = (IBond) bondsConnectedToAtom1j.get(k);
                    for (Map.Entry<Integer, Integer> m : l.entrySet()) {
                        IBond testBond2;
                        if (m.getKey() == g1.indexOf(testBond)) {
                            testBond2 = g2.getBond(m.getValue());
                            for (int n = 0; n < 2; n++) {
                                List<IBond> bondsToTest = g2.getConnectedBondsList(atom2[n]);
                                if (bondsToTest.contains(testBond2)) {

                                    if (j == n) {
                                        if (!result.containsKey(g1.indexOf(atom1[0]))
                                                && !result.containsValue(g2.indexOf(atom2[0]))) {
                                            result.put(g1.indexOf(atom1[0]), g2.indexOf(atom2[0]));
                                        }
                                    } else {
                                        if (!result.containsKey(g1.indexOf(atom1[1]))
                                                && !result.containsValue(g2.indexOf(atom2[0]))) {
                                            result.put(g1.indexOf(atom1[1]), g2.indexOf(atom2[0]));
                                        }
                                    }

                                    if (j == n) {
                                        if (!result.containsKey(g1.indexOf(atom1[1]))
                                                && !result.containsValue(g2.indexOf(atom2[1]))) {
                                            result.put(g1.indexOf(atom1[1]), g2.indexOf(atom2[1]));
                                        }
                                    } else {
                                        if (!result.containsKey(g1.indexOf(atom1[0]))
                                                && !result.containsValue(g2.indexOf(atom2[1]))) {
                                            result.put(g1.indexOf(atom1[0]), g2.indexOf(atom2[1]));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (result.isEmpty() && l.size() == 1) {
        result = SingleMappingCase(l, g1, g2, am, bm);
    }
    return result;
}
 
Example 16
Source File: MappingHandler.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static Map<Integer, Integer> SingleMappingCase(
        Map<Integer, Integer> l, IAtomContainer g1, IAtomContainer g2,
        AtomMatcher am, BondMatcher bm) {
    Map<Integer, Integer> result = new TreeMap<>();
    if (l.size() == 1) {
        IBond bond1 = g1.getBond(l.keySet().iterator().next());
        IBond bond2 = g2.getBond(l.values().iterator().next());

        if (bond1 instanceof IQueryBond) {
            if (((IQueryBond) bond1).matches(bond2)) {
                IQueryAtom atom1 = (IQueryAtom) (bond1.getAtom(0));
                IQueryAtom atom2 = (IQueryAtom) (bond1.getAtom(1));
                if (atom1.matches(bond2.getAtom(0)) && atom2.matches(bond2.getAtom(1))) {
                    result.put(g1.indexOf(bond1.getAtom(0)), g2.indexOf(bond2.getAtom(0)));
                    result.put(g1.indexOf(bond1.getAtom(1)), g2.indexOf(bond2.getAtom(1)));
                }
                if (atom1.matches(bond2.getAtom(1)) && atom2.matches(bond2.getAtom(0))) {
                    result.put(g1.indexOf(bond1.getAtom(0)), g2.indexOf(bond2.getAtom(1)));
                    result.put(g1.indexOf(bond1.getAtom(1)), g2.indexOf(bond2.getAtom(0)));
                }

            }
        } else {

            IAtom a1 = bond1.getBegin();
            IAtom a2 = bond1.getEnd();
            IAtom b1 = bond2.getBegin();
            IAtom b2 = bond2.getEnd();

            if (AtomBondMatcher.matches(a1, b1, am)
                    && AtomBondMatcher.matches(a2, b2, am)) {
                result.put(g1.indexOf(bond1.getAtom(0)), g2.indexOf(bond2.getAtom(0)));
                result.put(g1.indexOf(bond1.getAtom(1)), g2.indexOf(bond2.getAtom(1)));
            }
            if (AtomBondMatcher.matches(a1, b2, am)
                    && AtomBondMatcher.matches(a2, b1, am)) {
                result.put(g1.indexOf(bond1.getAtom(0)), g2.indexOf(bond2.getAtom(1)));
                result.put(g1.indexOf(bond1.getAtom(1)), g2.indexOf(bond2.getAtom(0)));
            }
        }
    }

    return result;
}