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

The following examples show how to use org.openscience.cdk.interfaces.IAtomContainer#bonds() . 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: GeometryTools.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Calculates the normalization factor in order to get an average bond
 * length of 1.5. It takes only into account Bond's with two atoms. See
 * comment for center(IAtomContainer atomCon, Dimension areaDim, HashMap
 * renderingCoordinates) for details on coordinate sets
 *
 * @param container Description of the Parameter
 * @return The normalizationFactor value
 */
public static double getNormalizationFactor(IAtomContainer container) {
    double bondlength = 0.0;
    double ratio;
    /*
     * Desired bond length for storing structures in MDL mol files This
     * should probably be set externally (from system wide settings)
     */
    double desiredBondLength = 1.5;
    // loop over all bonds and determine the mean bond distance
    int counter = 0;
    for (IBond bond : container.bonds()) {
        // only consider two atom bonds into account
        if (bond.getAtomCount() == 2) {
            counter++;
            IAtom atom1 = bond.getBegin();
            IAtom atom2 = bond.getEnd();
            bondlength += Math.sqrt(Math.pow(atom1.getPoint2d().x - atom2.getPoint2d().x, 2)
                    + Math.pow(atom1.getPoint2d().y - atom2.getPoint2d().y, 2));
        }
    }
    bondlength = bondlength / counter;
    ratio = desiredBondLength / bondlength;
    return ratio;
}
 
Example 2
Source File: Utility.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 *
 * @param ringBond
 * @param singleRings
 * @return
 */
public static int getNeighbourBondOrderCountFromRing(IBond ringBond, IRingSet singleRings) {
    int minValue = 9999;
    for (IAtomContainer ring : singleRings.atomContainers()) {
        int value = 0;
        if (ring.contains(ringBond.getAtom(0)) && ring.contains(ringBond.getAtom(1))) {
            for (IBond bond : ring.bonds()) {
                if (bond.contains(ringBond.getAtom(0)) || bond.contains(ringBond.getAtom(1))) {
                    value += bond.getOrder().numeric();
                }
            }
        }
        if (value < minValue) {
            minValue = value;
        }
    }
    return minValue;
}
 
Example 3
Source File: CDKConnectionTable.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param container
 */
public CDKConnectionTable(IAtomContainer container) {
    for (int i = 0; i < container.getAtomCount(); i++) {
        container.getAtom(i).setProperty("number", i + 1);
    }
    for (IBond bond : container.bonds()) {
        addConnection(bond.getAtom(0),
                bond.getAtom(1),
                getOrder(bond.getOrder()), // might need to check for aromatic
                getDepth(bond.getStereo()));

    }
}
 
Example 4
Source File: AtomContainerPrinter.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param atomContainer
 * @return
 */
public String toString(IAtomContainer atomContainer) {
    StringBuilder sb = new StringBuilder();
    for (IAtom atom : atomContainer.atoms()) {
        sb.append(atom.getSymbol());
    }
    sb.append(" ");
    List<Edge> edges = new ArrayList<>();
    for (IBond bond : atomContainer.bonds()) {
        if (bond.getAtomCount() < 2) {
            edges.add(new Edge(-1, -1, -1, "!", "!"));
            continue;
        }
        IAtom a0 = bond.getAtom(0);
        IAtom a1 = bond.getAtom(1);
        int a0N = atomContainer.indexOf(a0);
        int a1N = atomContainer.indexOf(a1);
        String a0S = a0.getSymbol();
        String a1S = a1.getSymbol();
        int o = bond.getOrder().numeric();
        if (a0N < a1N) {
            edges.add(new Edge(a0N, a1N, o, a0S, a1S));
        } else {
            edges.add(new Edge(a1N, a0N, o, a1S, a0S));
        }
    }
    sort(edges);
    sb.append(edges.toString());
    return sb.toString();
}
 
Example 5
Source File: GeometryTools.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * An average of all 3D bond length values is produced, using point3ds in
 * atoms. Atom's with no coordinates are disregarded.
 *
 * @param container The AtomContainer for which the average bond length is
 * to be calculated
 * @return the average bond length
 */
public static double getBondLengthAverage3D(IAtomContainer container) {
    double bondLengthSum = 0;
    int bondCounter = 0;
    for (IBond bond : container.bonds()) {
        IAtom atom1 = bond.getBegin();
        IAtom atom2 = bond.getEnd();
        if (atom1.getPoint3d() != null && atom2.getPoint3d() != null) {
            bondCounter++;
            bondLengthSum += atom1.getPoint3d().distance(atom2.getPoint3d());
        }
    }
    return bondLengthSum / bondCounter;
}
 
Example 6
Source File: SimpleHighlighter.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
     *
     * @param highlightContainer
     * @param color
     * @param g
     */
    public void drawHighlightContainer(
            IAtomContainer highlightContainer, Color color, Graphics2D g) {
        Color actualColor;
        if (params.highlightsAbove) {
            actualColor = getTranslucentColor(color);
        } else {
            actualColor = color;
        }
//        System.out.println(color + " " + color.getAlpha() + " " + actualColor + actualColor.getAlpha());
        g.setColor(actualColor);
        double r = params.highlightRadius;
        double d = r * 2;
        for (IAtom atom : highlightContainer.atoms()) {
            Point2d p = atom.getPoint2d();
            g.fill(new Ellipse2D.Double(p.x - r, p.y - r, d, d));
        }

        Stroke stroke = g.getStroke();
        g.setStroke(new BasicStroke(params.highlightBondStroke));
        for (IBond bond : highlightContainer.bonds()) {
            Point2d p0 = bond.getAtom(0).getPoint2d();
            Point2d p1 = bond.getAtom(1).getPoint2d();
            drawLine(p0, p1, g);
        }
        g.setStroke(stroke);
    }
 
Example 7
Source File: SimpleHighlighter.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Set the highlights for all atoms and bonds in the highlight container to
 * this color.
 *
 * @param highlightContainer
 * @param color
 */
@Override
public void addHighlights(IAtomContainer highlightContainer, Color color) {
    registerColor(color);
    for (IAtom atom : highlightContainer.atoms()) {
        atomColorMap.put(atom, color);
    }
    for (IBond bond : highlightContainer.bonds()) {
        bondColorMap.put(bond, color);
    }
}
 
Example 8
Source File: DirectBondDrawer.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean ringIsAromatic(IAtomContainer ring) {
    for (IAtom atom : ring.atoms()) {
        if (!atom.getFlag(ISAROMATIC)) {
            return false;
        }
    }
    for (IBond b : ring.bonds()) {
        if (!b.getFlag(ISAROMATIC)) {
            return false;
        }
    }
    return true;
}
 
Example 9
Source File: SignatureRootFinder.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static boolean adjacent(RootSystem rsI, RootSystem rsJ, IAtomContainer atomContainer) {
    for (int idxI = 0; idxI < rsI.getRoots().size(); idxI++) {
        IAtom atomI = rsI.getRoots().get(idxI);
        for (int idxJ = 0; idxJ < rsJ.getRoots().size(); idxJ++) {
            IAtom atomJ = rsJ.getRoots().get(idxJ);
            for (IBond bond : atomContainer.bonds()) {
                if (bond.contains(atomI) && bond.contains(atomJ)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 10
Source File: BlockReactionCanoniser.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private IAtomContainerSet permuteP(IAtomContainerSet original, BlockMapping mapping) {
    IAtomContainerSet permutedContainers = builder.newInstance(IAtomContainerSet.class);
    int[] productPermutation = mapping.getPermutationOfProducts();
    // System.out.println("PRODUCTS --------------------");

    List<IAtomContainer> unusedContainers = new ArrayList<>();
    for (IAtomContainer ac : original.atomContainers()) {
        unusedContainers.add(ac);
    }
    for (int i = 0; i < original.getAtomContainerCount(); i++) {
        int pi = productPermutation[i];
        IAtomContainer container = original.getAtomContainer(pi);
        BlockList blockList = mapping.getBlockListForProduct(container);
        if (blockList == null) {
            out.println("blocklist null for " + i
                    + new AtomContainerPrinter().toString(container)
                    + " in " + java.util.Arrays.toString(productPermutation));
            continue;
        }
        unusedContainers.remove(container);

        IAtomContainer permutedContainer = builder.newInstance(IAtomContainer.class);
        permutedContainer.setID(container.getID());
        // System.out.println("AC " + i + " " + blockList);
        IAtom[] atoms = getPermutedAtomsForProduct(blockList, container);
        permutedContainer.setAtoms(atoms);
        for (IBond bond : container.bonds()) {
            permutedContainer.addBond(bond);
        }
        permutedContainers.addAtomContainer(permutedContainer);
    }
    unusedContainers.stream().forEach((unusedContainer) -> {
        permutedContainers.addAtomContainer(unusedContainer);
    });
    return permutedContainers;
}
 
Example 11
Source File: BlockReactionCanoniser.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private IAtomContainerSet permuteR(IAtomContainerSet original, BlockMapping mapping) {
    int[] reactantPermutation = mapping.getPermutationOfReactants();
    IAtomContainerSet permutedContainers = builder.newInstance(IAtomContainerSet.class);
    // System.out.println("REACTANTS --------------------");

    List<IAtomContainer> unusedContainers = new ArrayList<>();
    for (IAtomContainer ac : original.atomContainers()) {
        unusedContainers.add(ac);
    }
    for (int i = 0; i < original.getAtomContainerCount(); i++) {
        int pi = reactantPermutation[i];
        IAtomContainer container = original.getAtomContainer(pi);
        BlockList blockList = mapping.getBlockListForReactant(container);

        if (blockList == null) {
            out.println("blocklist null for " + i
                    + new AtomContainerPrinter().toString(container)
                    + " in " + java.util.Arrays.toString(reactantPermutation));
            continue;
        }

        unusedContainers.remove(container);

        IAtomContainer permutedContainer = builder.newInstance(IAtomContainer.class);
        permutedContainer.setID(container.getID());
        // System.out.println("AC " + i + " " + blockList);

        IAtom[] atoms = getPermutedAtomsForReactant(blockList, container);
        // System.out.println("AC " + i + " " + blockList);
        permutedContainer.setAtoms(atoms);
        for (IBond bond : container.bonds()) {
            permutedContainer.addBond(bond);
        }
        permutedContainers.addAtomContainer(permutedContainer);
    }
    unusedContainers.stream().forEach((unusedContainer) -> {
        permutedContainers.addAtomContainer(unusedContainer);
    });
    return permutedContainers;
}
 
Example 12
Source File: CaseHandler.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean findAndChipBondPhophate(IAtomContainer container) {
    boolean flag = false;

    for (IBond bond : container.bonds()) {
        IAtom atomE = bond.getAtom(0);
        IAtom atomP = bond.getAtom(1);
        if ((atomE.getSymbol().equals("O") && atomP.getSymbol().equals("P"))
                || (atomE.getSymbol().equals("P") && atomP.getSymbol().equals("O"))) {
            if (bond.getOrder().equals(SINGLE)) {
                IAtom oxygen = atomE.getSymbol().equals("O") ? atomE : atomP;
                List<IBond> neighbourBonds = container.getConnectedBondsList(oxygen);
                if (neighbourBonds.size() == 2) {
                    neighbourBonds.stream().filter((b) -> (b.getAtom(0).getSymbol().equals("O")
                            || b.getAtom(0).getSymbol().equals("P"))).filter((b) -> (b.getAtom(1).getSymbol().equals("O")
                            || b.getAtom(1).getSymbol().equals("P"))).map((b) -> {
                        container.removeBond(b);
                        return b;
                    }).filter((b) -> (DEBUG)).map((b) -> {
                        out.println("bondToBeChipped " + b.getAtom(0).getSymbol());
                        return b;
                    }).map((b) -> {
                        out.println("bondToBeChipped " + b.getAtom(1).getSymbol());
                        return b;
                    }).forEach((_item) -> {
                        out.println("removeBond o-p ");
                    });
                    return true;
                }
            }
        }
    }

    return flag;
}
 
Example 13
Source File: CaseHandler.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean findAndChipBond(IAtomContainer container, IAtomContainer referenceContainer) {
    boolean flag = false;

    if (container != null) {
        for (IBond bond : container.bonds()) {
            if ((bond.getAtom(0).getSymbol().equalsIgnoreCase("O")
                    && bond.getAtom(1).getSymbol().equalsIgnoreCase("C"))
                    || (bond.getAtom(0).getSymbol().equalsIgnoreCase("C")
                    && bond.getAtom(1).getSymbol().equalsIgnoreCase("O"))) {
                if (!bond.getAtom(0).getFlag(ISAROMATIC)
                        && !bond.getAtom(1).getFlag(ISAROMATIC)) {
                    if (referenceContainer.contains(bond)) {
                        IAtom atom = bond.getAtom(0).getSymbol().equalsIgnoreCase("C") ? bond.getAtom(0) : bond.getAtom(1);
                        List<IBond> neighbourhoodBonds = referenceContainer.getConnectedBondsList(atom);
                        flag = false;
                        for (IBond neighbourhoodBond : neighbourhoodBonds) {
                            if (neighbourhoodBond.contains(atom) && !neighbourhoodBond.getFlag(ISINRING)) {
                                if ((neighbourhoodBond.getAtom(0).getSymbol().equalsIgnoreCase("O")
                                        && neighbourhoodBond.getAtom(1).getSymbol().equalsIgnoreCase("C"))
                                        || (neighbourhoodBond.getAtom(0).getSymbol().equalsIgnoreCase("C")
                                        && neighbourhoodBond.getAtom(1).getSymbol().equalsIgnoreCase("O"))) {
                                    flag = true;
                                }
                            }
                        }

                        if (flag) {
                            referenceContainer.removeBond(bond);
                            break;
                        }
                    }
                }
            }
        }
    }
    return flag;
}
 
Example 14
Source File: RBlastReaction.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void setupBondChangeLists(IAtomContainer atomContainer, boolean isReactant) {
    for (IBond bond : atomContainer.bonds()) {
        if (bond.getProperty(BOND_CHANGE_INFORMATION) != null) {
            ECBLAST_BOND_CHANGE_FLAGS bondChangeType = (ECBLAST_BOND_CHANGE_FLAGS) bond.getProperty(BOND_CHANGE_INFORMATION);
            if (null != bondChangeType) {
                switch (bondChangeType) {
                    case BOND_CLEAVED:
                        bondsCleavedInReactant.add(bond);
                        break;
                    case BOND_FORMED:
                        bondsFormedInProduct.add(bond);
                        break;
                    case BOND_ORDER:
                        if (isReactant) {
                            bondsOrderChangedInReactant.add(bond);
                        } else {
                            bondsOrderChangedInProduct.add(bond);
                        }
                        break;
                    case BOND_STEREO:
                        if (isReactant) {
                            bondsStereoChangedInReactant.add(bond);
                        } else {
                            bondsStereoChangedInProduct.add(bond);
                        }
                        break;
                    case BOND_FORMED_OR_CLEAVED:
                        if (isReactant) {
                            bondsCleavedInReactant.add(bond);
                        } else {
                            bondsFormedInProduct.add(bond);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
 
Example 15
Source File: ExtAtomContainerManipulator.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * This method is a workaround by assigning dative bonds to single
 *
 * @param mol
 */
public static void fixDativeBonds(IAtomContainer mol) {
    if (!(mol instanceof IQueryAtomContainer)) {
        for (IBond bond : mol.bonds()) {
            if (bond.getOrder() == IBond.Order.UNSET) {
                bond.setOrder(IBond.Order.SINGLE);
            }
        }
    }
}
 
Example 16
Source File: BondChangeAnnotator.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 *
 * @param affectedBondReactants
 * @param affectedBondProducts
 * @return
 */
public int isKekuleEffect(IBond affectedBondReactants, IBond affectedBondProducts) {
    if (affectedBondReactants != null && affectedBondProducts != null) {
        if (affectedBondReactants.getFlag(ISINRING)
                == affectedBondProducts.getFlag(ISINRING)) {

            if ((!affectedBondReactants.getFlag(ISAROMATIC)
                    && affectedBondProducts.getFlag(ISAROMATIC))
                    || (affectedBondReactants.getFlag(ISAROMATIC)
                    && !affectedBondProducts.getFlag(ISAROMATIC))) {
                IRingSet smallestRingSetR = getSmallestRingSet(affectedBondReactants, queryRingSet);
                IRingSet smallestRingSetP = getSmallestRingSet(affectedBondProducts, targetRingSet);

                int changeCounter = 0;
                for (IAtomContainer rRing : smallestRingSetR.atomContainers()) {
                    for (IAtomContainer pRing : smallestRingSetP.atomContainers()) {
                        if (rRing.getAtomCount() == pRing.getAtomCount()) {
                            for (IBond rBond : rRing.bonds()) {
                                for (IBond pBond : pRing.bonds()) {
                                    if (rBond.getAtom(0).getID().equals(pBond.getAtom(0).getID())
                                            && rBond.getAtom(1).getID().equals(pBond.getAtom(1).getID())) {
                                        if (!rBond.getOrder().equals(pBond.getOrder())) {
                                            changeCounter++;
                                        }
                                    }
                                    if (rBond.getAtom(0).getID().equals(pBond.getAtom(1).getID())
                                            && rBond.getAtom(1).getID().equals(pBond.getAtom(0).getID())) {
                                        if (!rBond.getOrder().equals(pBond.getOrder())) {
                                            changeCounter++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (changeCounter == 1) {
                    return 0;
                } else {
                    return 1;
                }
            }
        }
    }

    return -1;
}
 
Example 17
Source File: SubgraphMoleculeSignature.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
     *
     * @param fullContainer
     * @param subgraphAtoms
     * @param dummy
     */
    public SubgraphMoleculeSignature(
            IAtomContainer fullContainer, List<IAtom> subgraphAtoms, int dummy) {
        this.atomContainer
                = fullContainer.getBuilder().newInstance(IAtomContainer.class);

//        for (IAtom atom : subgraphAtoms) {
//            this.atomContainer.addAtom(atom);
//        }
        // sort the atoms
        IAtom[] sortedAtoms = new IAtom[fullContainer.getAtomCount()];
        List<String> debugList = new ArrayList<>();
        subgraphAtoms.stream().forEach((atom) -> {
            int atomNumber = fullContainer.indexOf(atom);
            sortedAtoms[atomNumber] = atom;
            debugList.add(atom.getSymbol() + atomNumber);
        });
        sort(debugList);
//        System.out.println("atoms of " + fullContainer.getID() + " " + debugList);

        List<Integer> debugList2 = new ArrayList<>();
        for (int i = 0; i < sortedAtoms.length; i++) {
            if (sortedAtoms[i] != null) {
                this.atomContainer.addAtom(sortedAtoms[i]);
                debugList2.add(i);
            }
        }
//        System.out.println("atoms (2) of " + atomContainer.getID() + " " + debugList2);

        for (IBond bond : fullContainer.bonds()) {
            IAtom a0 = bond.getAtom(0);
            IAtom a1 = bond.getAtom(1);
            int a0n = fullContainer.indexOf(a0);
            int a1n = fullContainer.indexOf(a1);
            if (subgraphAtoms.contains(a0) && subgraphAtoms.contains(a1)) {
                this.atomContainer.addBond(bond);
//                System.out.println("adding bond " + a0n + " " + a1n);
            } else {
//                System.out.println("rejecting bond " + a0n + " " + a1n);
            }
        }

        this.vertexCount = subgraphAtoms.size();
        useAdjLists = false;
    }
 
Example 18
Source File: StereoCenteralityTool.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
     *
     * @param ac
     * @param perceptor
     * @return
     */
    public static Map<IAtom, IStereoAndConformation> getChirality2D(IAtomContainer ac, CDKPerceptor perceptor) {
        Map<IAtom, IStereoAndConformation> chiralityMap = new HashMap<>();
//        perceptor.perceive(ac);

        /*
         * time out function added
         */
        Boolean flag = false;
        ExecutorService executor = Executors.newCachedThreadPool();
        Callable<Boolean> task = () -> {
            perceptor.perceive(ac);
            return true;
        };
        Future<Boolean> future = executor.submit(task);
        try {
            flag = future.get(30, TimeUnit.SECONDS);
        } catch (TimeoutException | InterruptedException | ExecutionException ex) {
            flag = false;
        } // handle the interrupts
        // handle other exceptions
        finally {
            future.cancel(true); // may or may not desire this
        }

        if (flag == false) {
            LOGGER.error(Level.WARNING, null, "Time out hit in computing stereo centers");
        }

        for (IAtom atom : ac.atoms()) {
            if (Tetrahedral.R.equals(atom.getProperty("descriptor"))) {
                chiralityMap.put(atom, IStereoAndConformation.R);
            } else if (Tetrahedral.S.equals(atom.getProperty("descriptor"))) {
                chiralityMap.put(atom, IStereoAndConformation.S);
            } else if (Planar.E.equals(atom.getProperty("descriptor"))) {
                chiralityMap.put(atom, IStereoAndConformation.E);
            } else if (Planar.Z.equals(atom.getProperty("descriptor"))) {
                chiralityMap.put(atom, IStereoAndConformation.Z);
            } else if (Trigonal.Re.equals(atom.getProperty("descriptor"))) {
                chiralityMap.put(atom, IStereoAndConformation.P);
            } else if (Trigonal.Si.equals(atom.getProperty("descriptor"))) {
                chiralityMap.put(atom, IStereoAndConformation.M);
            } else {
                chiralityMap.put(atom, IStereoAndConformation.NONE);
            }
        }
        for (IBond bond : ac.bonds()) {
            if (Planar.E.equals(bond.getProperty("descriptor"))) {
                chiralityMap.put(bond.getAtom(0), IStereoAndConformation.E);
                chiralityMap.put(bond.getAtom(1), IStereoAndConformation.E);
            } else if (Planar.Z.equals(bond.getProperty("descriptor"))) {
                chiralityMap.put(bond.getAtom(0), IStereoAndConformation.Z);
                chiralityMap.put(bond.getAtom(1), IStereoAndConformation.Z);
            } else if (Trigonal.Re.equals(bond.getProperty("descriptor"))) {
                chiralityMap.put(bond.getAtom(0), IStereoAndConformation.P);
                chiralityMap.put(bond.getAtom(1), IStereoAndConformation.P);
            } else if (Trigonal.Si.equals(bond.getProperty("descriptor"))) {
                chiralityMap.put(bond.getAtom(0), IStereoAndConformation.M);
                chiralityMap.put(bond.getAtom(1), IStereoAndConformation.M);
            }
        }

        chiralityMap.keySet().stream().forEach((atom) -> {
            atom.setProperty("Stereo", chiralityMap.get(atom));
        });
        return chiralityMap;
    }
 
Example 19
Source File: CDKRMapHandler.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * This makes atom map1 of matching atoms out of atom map1 of matching bonds
 * as produced by the get(Subgraph|Ismorphism)Map methods. Added by Asad
 * since CDK one doesn't pick up the correct changes
 *
 * @param list The list produced by the getMap method.
 * @param sourceGraph first molecule. Must not be an IQueryAtomContainer.
 * @param targetGraph second molecule. May be an IQueryAtomContainer.
 * @return The mapping found projected on sourceGraph. This is atom List of
 * CDKRMap objects containing Ids of matching atoms.
 */
private synchronized List<List<CDKRMap>> makeAtomsMapOfBondsMapSingleBond(List<CDKRMap> list, IAtomContainer sourceGraph, IAtomContainer targetGraph) {
    if (list == null) {
        return null;
    }
    Map<IBond, IBond> bondMap = new HashMap<>(list.size());
    list.stream().forEach((solBondMap) -> {
        int id1 = solBondMap.getId1();
        int id2 = solBondMap.getId2();
        IBond qBond = sourceGraph.getBond(id1);
        IBond tBond = targetGraph.getBond(id2);
        bondMap.put(qBond, tBond);
    });
    List<CDKRMap> result1 = new ArrayList<>();
    List<CDKRMap> result2 = new ArrayList<>();
    for (IBond qbond : sourceGraph.bonds()) {
        if (bondMap.containsKey(qbond)) {
            IBond tbond = bondMap.get(qbond);
            CDKRMap map00 = null;
            CDKRMap map01 = null;
            CDKRMap map10 = null;
            CDKRMap map11 = null;

            if ((qbond.getAtom(0).getSymbol().equals(tbond.getAtom(0).getSymbol()))
                    && (qbond.getAtom(1).getSymbol().equals(tbond.getAtom(1).getSymbol()))) {
                map00 = new CDKRMap(sourceGraph.indexOf(qbond.getAtom(0)),
                        targetGraph.indexOf(tbond.getAtom(0)));
                map11 = new CDKRMap(sourceGraph.indexOf(qbond.getAtom(1)),
                        targetGraph.indexOf(tbond.getAtom(1)));
                if (!result1.contains(map00)) {
                    result1.add(map00);
                }
                if (!result1.contains(map11)) {
                    result1.add(map11);
                }
            }
            if ((qbond.getAtom(0).getSymbol().equals(tbond.getAtom(1).getSymbol()))
                    && (qbond.getAtom(1).getSymbol().equals(tbond.getAtom(0).getSymbol()))) {
                map01 = new CDKRMap(sourceGraph.indexOf(qbond.getAtom(0)),
                        targetGraph.indexOf(tbond.getAtom(1)));
                map10 = new CDKRMap(sourceGraph.indexOf(qbond.getAtom(1)),
                        targetGraph.indexOf(tbond.getAtom(0)));
                if (!result2.contains(map01)) {
                    result2.add(map01);
                }
                if (!result2.contains(map10)) {
                    result2.add(map10);
                }
            }
        }
    }
    List<List<CDKRMap>> result = new ArrayList<>();
    if (result1.size() == result2.size()) {
        result.add(result1);
        result.add(result2);
    } else if (result1.size() > result2.size()) {
        result.add(result1);
    } else {
        result.add(result2);
    }
    return result;
}