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

The following examples show how to use org.openscience.cdk.interfaces.IAtomContainer#atoms() . 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: MolContainer.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public synchronized String getMoleculeID(IAtomContainer mol) throws Exception {
    IAtomContainer queryMol = removeHydrogensExceptSingleAndPreserveAtomID(mol);
    percieveAtomTypesAndConfigureAtoms(queryMol);
    CDKHydrogenAdder instance = CDKHydrogenAdder.getInstance(queryMol.getBuilder());
    for (IAtom atom : queryMol.atoms()) {
        try {
            instance.addImplicitHydrogens(queryMol, atom);
        } catch (CDKException e) {
            LOGGER.error("WARNING: Error in adding H to the molecule");
        }
    }

    aromatizeMolecule(queryMol);

    for (Map.Entry<String, IAtomContainer> map : molContainer.entrySet()) {
        String key = map.getKey();
        IAtomContainer tMol = map.getValue();
        if (isIdentical(queryMol, tMol, true)) {
            return key;
        }
    }
    //System.LOGGER.debug("Error: Unable to Find AtomContainer ID!!!");
    return null;
}
 
Example 2
Source File: SmilesMoleculeLabeller.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 *
 * @param container
 * @return
 */
@Override
public IAtomContainer getCanonicalMolecule(IAtomContainer container) {
    try {
        IAtomContainer clone = container.clone();
        for (IAtom a : container.atoms()) {
            if (a.getID() != null) {
                int index = container.indexOf(a);
                clone.getAtom(index).setID(a.getID());
            }
        }

        if (container.getID() != null) {
            clone.setID(container.getID());
        }

        int[] canonicalPermutation = getCanonicalPermutation(clone);
        getCanonicalPermutation(clone, canonicalPermutation);
        return clone;

    } catch (CloneNotSupportedException ex) {
        LOGGER.error(SEVERE, null, ex);
    }
    return null;
}
 
Example 3
Source File: ExtAtomContainerManipulator.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Convenience method to perceive atom types for all <code>IAtom</code>s in
 * the <code>IAtomContainer</code>, using the
 * <code>CDKAtomTypeMatcher</code>. If the matcher finds atom matching atom
 * type, the <code>IAtom</code> will be configured to have the same
 * properties as the <code>IAtomType</code>. If no matching atom type is
 * found, no configuration is performed.
 *
 * @param container
 * @throws CDKException
 */
public static void percieveAtomTypesAndConfigureAtoms(IAtomContainer container) throws CDKException {
    CDKAtomTypeMatcher matcher = CDKAtomTypeMatcher.getInstance(container.getBuilder());
    for (IAtom atom : container.atoms()) {
        if (!(atom instanceof IPseudoAtom)) {
            try {
                IAtomType matched = matcher.findMatchingAtomType(container, atom);
                if (matched != null) {
                    AtomTypeManipulator.configure(atom, matched);
                }
            } catch (CDKException e) {
                LOGGER.error(Level.WARNING,
                        "Failed to find Matching AtomType! {0}{1}", new Object[]{atom.getSymbol(), e.getMessage()});
            }
        }
    }
}
 
Example 4
Source File: EdgeProductGraph.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates the subgraph of g1 containing all the edges from the edge product
 * in the vertices of this EdgeProductGraph
 *
 * @param edgeProductVertices if (and only if) these vertices induce a
 * complete subgraph in this EdgeProductGraph, then the result will be the a
 * common subgraph of g1 and g2.
 * @return a subgraph of g1
 * @throws java.lang.CloneNotSupportedException
 */
public IAtomContainer toQuerySubgraph(Set<Vertex> edgeProductVertices) throws CloneNotSupportedException {

    IAtomContainer ac = ExtAtomContainerManipulator.cloneWithIDs(source);

    //Add the left Edge (including vertices) from all the EdgeProducts in vertices
    Set<IAtom> atomsMapped = new HashSet<>();
    edgeProductVertices.stream().map((ep) -> ep.getQueryBondIndex()).map((bondIndex) -> ac.getBond(bondIndex)).map((bond) -> {
        atomsMapped.add(bond.getBegin());
        return bond;
    }).forEachOrdered((bond) -> {
        atomsMapped.add(bond.getEnd());
    });
    Set<IAtom> atomsToBeRemoved = new HashSet<>();
    for (IAtom a : ac.atoms()) {
        atomsToBeRemoved.add(a);
    }

    atomsToBeRemoved.removeAll(atomsMapped);
    atomsToBeRemoved.forEach((a) -> {
        ac.removeAtom(a);
    });

    return ac;
}
 
Example 5
Source File: MappingGraph.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private AtomContainerAtomPair getByID(IAtomContainerSet moleculeSet, String id) {
//        System.out.println("getting id " + id);
        for (IAtomContainer ac : moleculeSet.atomContainers()) {
            for (IAtom atom : ac.atoms()) {
                String atomID = atom.getID();
                if (atomID != null && atomID.equals(id)) {
                    int index = ac.indexOf(atom);
                    return new AtomContainerAtomPair(ac, atom, index);
                }
            }
        }
        return null;
    }
 
Example 6
Source File: Chirality2DTool.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param atomContainer
 * @param getNoneAssesments
 * @return
 */
@Override
public Map<IAtom, IStereoAndConformation> getTetrahedralChiralities(
        IAtomContainer atomContainer, boolean getNoneAssesments) {
    Map<IAtom, IStereoAndConformation> chiralities = new HashMap<>();
    WedgeStereoLifter lifter = new WedgeStereoLifter();
    for (IAtom atom : atomContainer.atoms()) {
        IStereoAndConformation chirality = getChirality2D(lifter, atom, atomContainer);
        if (getNoneAssesments || chirality != NONE) {
            chiralities.put(atom, chirality);
        }
    }
    return chiralities;
}
 
Example 7
Source File: BlockMapper.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private AtomContainerAtomPair getByID(IAtomContainerSet moleculeSet, String id) {
//        System.out.println("getting id " + id);
        for (IAtomContainer ac : moleculeSet.atomContainers()) {
            for (IAtom atom : ac.atoms()) {
                String atomID = atom.getID();
                if (atomID != null && atomID.equals(id)) {
                    return new AtomContainerAtomPair(ac, atom);
                }
            }
        }
        return null;
    }
 
Example 8
Source File: MappingReferenceResolver.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Fill a map of string ids to atoms using the atom ID and a unique label
 * for this moleculeSet.
 *
 * @param moleculeSet
 * @param map
 * @param label
 */
private static void fillMap(IAtomContainerSet moleculeSet, Map<String, IAtom> map, String label) {
    for (IAtomContainer atomContainer : moleculeSet.atomContainers()) {
        for (IAtom atom : atomContainer.atoms()) {
            String id = label + atom.getID();
            map.put(id, atom);
        }
    }
}
 
Example 9
Source File: AbstractReactionLabeller.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void fixAtomMapping(IAtomContainer canonicalForm) {
    for (IAtom a : canonicalForm.atoms()) {
        String v = (String) a.getProperty(ATOM_ATOM_MAPPING);
        if (v != null) {
            a.setProperty(ATOM_ATOM_MAPPING, valueOf(v));
        }
    }
}
 
Example 10
Source File: GraphMatching.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private synchronized IAtom getAtomByID(IAtomContainer ac, String ID) {
    for (IAtom atom : ac.atoms()) {
        if (atom.getID().equalsIgnoreCase(ID)) {
            return atom;
        }
    }
    return null;
}
 
Example 11
Source File: StereoCenteralityTool.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static IAtom getAtomByID(String id, IAtomContainer ac) {
    for (IAtom a : ac.atoms()) {
        if (a.getID().equals(id)) {
            return a;
        }
    }
    return null;
}
 
Example 12
Source File: MDLV2000Writer.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private int getNumberOfDimensions(IAtomContainer mol) {
    for (IAtom atom : mol.atoms()) {
        if (atom.getPoint3d() != null && !forceWriteAs2DCoords.isSet()) {
            return 3;
        } else if (atom.getPoint2d() != null) {
            return 2;
        }
    }
    return 0;
}
 
Example 13
Source File: ReactionAtomIDTransformation.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param reaction
 * @return
 */
@Override
public IReaction transform(IReaction reaction) {
    for (IAtomContainer atomContainer : getAllAtomContainers(reaction)) {
        for (IAtom atom : atomContainer.atoms()) {
            Object prop = atom.getProperty(ATOM_ATOM_MAPPING);
            if (prop != null) {
                int mappingID = (Integer) prop;
                atom.setID(valueOf(mappingID));
            }
        }
    }

    return reaction;
}
 
Example 14
Source File: GraphMatcher.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static IAtom getAtomByID(IAtomContainer ac, IAtom atom) {
    if (atom.getID() == null) {
        return null;
    }
    for (IAtom a : ac.atoms()) {
        if (a.getID().equals(atom.getID())) {
            return a;
        }
    }
    return null;
}
 
Example 15
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 16
Source File: AbstractReactionLabeller.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param reaction
 * @return
 */
protected Map<IChemObject, Integer> makeIndexMap(IReaction reaction) {
    Map<IChemObject, Integer> indexMap
            = new HashMap<>();
    List<IAtomContainer> all
            = getAllAtomContainers(reaction);
    int globalIndex = 0;
    for (IAtomContainer ac : all) {
        for (IAtom atom : ac.atoms()) {
            indexMap.put(atom, globalIndex);
            globalIndex++;
        }
    }
    return indexMap;
}
 
Example 17
Source File: ExtAtomContainerManipulator.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void printAtoms(IAtomContainer mol) {
    System.out.print("Atom: ");
    for (IAtom a : mol.atoms()) {

        System.out.print(a.getSymbol());
        System.out.print("[" + a.getFormalCharge() + "]");
        if (a.getID() != null) {
            System.out.print("[" + a.getID() + "]");
        }

    }
    System.out.println();
    System.out.println();
}
 
Example 18
Source File: GeometryTools.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Determines the coverage of this {@link IAtomContainer}'s 3D coordinates.
 * If all atoms are non-null and have 3D coordinates this method will return
 * {@link CoordinateCoverage#FULL}. If one or more atoms does have 3D
 * coordinates and any others atoms are null or are missing 3D coordinates
 * this method will return {@link CoordinateCoverage#PARTIAL}. If all atoms
 * are null or are all missing 3D coordinates this method will return
 * {@link CoordinateCoverage#NONE}. If the provided container is null
 * {@link CoordinateCoverage#NONE} is also returned.
 *
 * @param container the container to inspect
 *
 * @return
 * {@link CoordinateCoverage#FULL}, {@link CoordinateCoverage#PARTIAL} or
 * {@link CoordinateCoverage#NONE} depending on the number of 3D coordinates
 * present
 *
 * @see CoordinateCoverage
 * @see #has3DCoordinates(org.openscience.cdk.interfaces.IAtomContainer)
 * @see
 * #get2DCoordinateCoverage(org.openscience.cdk.interfaces.IAtomContainer)
 * @see org.openscience.cdk.interfaces.IAtom#getPoint3d()
 */
public static CoordinateCoverage get3DCoordinateCoverage(IAtomContainer container) {

    if (container == null || container.getAtomCount() == 0) {
        return CoordinateCoverage.NONE;
    }

    int count = 0;

    for (IAtom atom : container.atoms()) {
        count += atom != null && atom.getPoint3d() != null ? 1 : 0;
    }

    return count == 0 ? CoordinateCoverage.NONE : count == container.getAtomCount() ? CoordinateCoverage.FULL
            : CoordinateCoverage.PARTIAL;

}
 
Example 19
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 20
Source File: ExtAtomContainerManipulator.java    From ReactionDecoder with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Set all null hydrogen counts to 0. Generally hydrogen counts are present
 * and if not we add them. However the molecule being tested can't include
 * hydrogen counts as then fingerprints don't line up (substructure
 * filtering). The previous behaviour of the SMARTS matching was to treat
 * null hydrogens as 0 - the new behaviour is to complain about it.
 *
 * @param mol molecule to zero out hydrogen counts
 */
public static void setNullHCountToZero(IAtomContainer mol) {
    for (IAtom a : mol.atoms()) {
        if (a.getImplicitHydrogenCount() == null) {
            a.setImplicitHydrogenCount(0);
        }
    }
}