Search in sources :

Example 1 with IAtomContainer

use of org.openscience.cdk.interfaces.IAtomContainer in project MassBank-web by MassBank.

the class StructureToSvgStringGenerator method structureFromInChI.

public static IAtomContainer structureFromInChI(String inchi) {
    IAtomContainer mol = null;
    try {
        // get atom container
        InChIGeneratorFactory inchiFactory = InChIGeneratorFactory.getInstance();
        InChIToStructure inchi2structure = inchiFactory.getInChIToStructure(inchi, DefaultChemObjectBuilder.getInstance());
        mol = inchi2structure.getAtomContainer();
    } catch (CDKException e) {
        System.out.println("Warning: " + e.getLocalizedMessage());
    }
    return mol;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) CDKException(org.openscience.cdk.exception.CDKException) InChIToStructure(org.openscience.cdk.inchi.InChIToStructure) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory)

Example 2 with IAtomContainer

use of org.openscience.cdk.interfaces.IAtomContainer in project MassBank-web by MassBank.

the class StructureToSvgStringGenerator method createClickablePreviewImage.

public static ClickablePreviewImageData createClickablePreviewImage(String accession, String inchi, String smiles, String tmpFileFolder, String tmpUrlFolder, int sizeSmall, int sizeMedium, int sizeBig) {
    if (inchi != null && (inchi.equals("NA") || inchi.equals("N/A")))
        inchi = null;
    if (smiles != null && (smiles.equals("NA") || smiles.equals("N/A")))
        smiles = null;
    // generate SVG string from structure (inchi / smiles)
    boolean inchiThere = inchi != null;
    boolean smilesThere = smiles != null;
    IAtomContainer mol = null;
    if (mol == null && smilesThere)
        mol = StructureToSvgStringGenerator.structureFromSMILES(smiles);
    if (mol == null && inchiThere)
        mol = StructureToSvgStringGenerator.structureFromInChI(inchi);
    if (mol == null)
        return null;
    String svg = StructureToSvgStringGenerator.drawToSvg(mol);
    if (svg == null)
        return null;
    // path to temp file as local file and as url
    final SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd_HHmmss_SSS");
    String accession2 = accession.replaceAll("[^0-9a-zA-Z]", "_");
    String fileNameSmall = sdf.format(new Date()) + "_" + accession2 + "_small.svg";
    String fileNameMedium = sdf.format(new Date()) + "_" + accession2 + "_medium.svg";
    String fileNameBig = sdf.format(new Date()) + "_" + accession2 + "_big.svg";
    String tmpFileSmall = (new File(tmpFileFolder + fileNameSmall)).getPath();
    String tmpFileMedium = (new File(tmpFileFolder + fileNameMedium)).getPath();
    String tmpFileBig = (new File(tmpFileFolder + fileNameBig)).getPath();
    String tmpUrlSmall = tmpUrlFolder + "/" + fileNameSmall;
    String tmpUrlMedium = tmpUrlFolder + "/" + fileNameMedium;
    String tmpUrlBig = tmpUrlFolder + "/" + fileNameBig;
    // adapt size of svg image
    String svgSmall = StructureToSvgStringGenerator.resizeSvg(svg, sizeSmall, sizeSmall);
    String svgMedium = StructureToSvgStringGenerator.resizeSvg(svg, sizeMedium, sizeMedium);
    String svgBig = StructureToSvgStringGenerator.resizeSvg(svg, sizeBig, sizeBig);
    return new ClickablePreviewImageData(tmpFileSmall, tmpFileMedium, tmpFileBig, tmpUrlSmall, tmpUrlMedium, tmpUrlBig, svgSmall, svgMedium, svgBig);
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Date(java.util.Date)

Example 3 with IAtomContainer

use of org.openscience.cdk.interfaces.IAtomContainer in project Smiles2Monomers by yoann-dufresne.

the class MolTreeWidth method calculateTreeWidth.

public int calculateTreeWidth(IMolecule m) {
    NGraph<InputData> g;
    AtomContainerToNgraph converter = new AtomContainerToNgraph();
    IAtomContainer mol = m;
    if (!ConnectivityChecker.isConnected(mol)) {
        IMoleculeSet comps = ConnectivityChecker.partitionIntoMolecules(mol);
        int maxIndex = 0;
        int maxCount = -9999;
        for (int i = 0; i < comps.getAtomContainerCount(); i++) {
            if (comps.getAtomContainer(i).getAtomCount() > maxCount) {
                maxCount = comps.getAtomContainer(i).getAtomCount();
                maxIndex = i;
            }
        }
        mol = comps.getAtomContainer(maxIndex);
    }
    g = converter.convert(mol);
    Stopwatch stopwatch = new Stopwatch(new JavaNanoTime());
    Permutation<InputData> p = new LexBFS<InputData>();
    PermutationToTreeDecomposition<InputData> pttd = new PermutationToTreeDecomposition<InputData>(p);
    stopwatch.reset();
    stopwatch.start();
    pttd.setInput(g);
    pttd.run();
    stopwatch.stop();
    return pttd.getUpperBound();
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IMoleculeSet(org.openscience.cdk.interfaces.IMoleculeSet) LexBFS(nl.uu.cs.treewidth.algorithm.LexBFS) Stopwatch(nl.uu.cs.treewidth.timing.Stopwatch) JavaNanoTime(nl.uu.cs.treewidth.timing.JavaNanoTime) PermutationToTreeDecomposition(nl.uu.cs.treewidth.algorithm.PermutationToTreeDecomposition) InputData(nl.uu.cs.treewidth.input.GraphInput.InputData)

Example 4 with IAtomContainer

use of org.openscience.cdk.interfaces.IAtomContainer in project MassBank-web by MassBank.

the class StructureToSvgStringGenerator method structureFromSMILES.

public static IAtomContainer structureFromSMILES(String smiles) {
    IAtomContainer mol = null;
    try {
        // get atom container
        SmilesParser smipar = new SmilesParser(DefaultChemObjectBuilder.getInstance());
        mol = smipar.parseSmiles(smiles);
    } catch (CDKException e) {
        System.out.println("Warning: " + e.getLocalizedMessage());
    }
    return mol;
}
Also used : SmilesParser(org.openscience.cdk.smiles.SmilesParser) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) CDKException(org.openscience.cdk.exception.CDKException)

Example 5 with IAtomContainer

use of org.openscience.cdk.interfaces.IAtomContainer in project Smiles2Monomers by yoann-dufresne.

the class SmilesGenerator method createSMILESWithoutCheckForMultipleMolecules.

/**
 *  Generate canonical SMILES from the <code>molecule</code>. This method
 *  canonicaly lables the molecule but dose not perform any checks on the
 *  chemical validity of the molecule. Does not care about multiple molecules.
 *  IMPORTANT: A precomputed Set of All Rings (SAR) can be passed to this
 *  SmilesGenerator in order to avoid recomputing it. Use setRings() to
 *  assign the SAR.
 *
 * @param  molecule                 The molecule to evaluate.
 * @param  chiral                   true=SMILES will be chiral, false=SMILES
 *      will not be chiral.
 * @param  doubleBondConfiguration  Should E/Z configurations be read at these positions? If the flag at position X is set to true,
 *                                  an E/Z configuration will be written from coordinates around bond X, if false, it will be ignored.
 *                                  If flag is true for a bond which does not constitute a valid double bond configuration, it will be
 *                                  ignored (meaning setting all to true will create E/Z indication will be pu in the smiles wherever
 *                                  possible, but note the coordinates might be arbitrary).
 * @exception  CDKException         At least one atom has no Point2D;
 *      coordinates are needed for creating the chiral smiles. This excpetion
 *      can only be thrown if chiral smiles is created, ignore it if you want a
 *      non-chiral smiles (createSMILES(AtomContainer) does not throw an
 *      exception).
 *@see                             org.openscience.cdk.graph.invariant.CanonicalLabeler#canonLabel(IAtomContainer)
 * @return the SMILES representation of the molecule
 */
@TestMethod("testCreateSMILESWithoutCheckForMultipleMolecules_withDetectAromaticity,testCreateSMILESWithoutCheckForMultipleMolecules_withoutDetectAromaticity")
public synchronized String createSMILESWithoutCheckForMultipleMolecules(IAtomContainer molecule, boolean chiral, boolean[] doubleBondConfiguration) throws CDKException {
    if (molecule.getAtomCount() == 0) {
        return "";
    }
    canLabler.canonLabel(molecule);
    brokenBonds.clear();
    ringMarker = 0;
    IAtom start = null;
    for (int i = 0; i < molecule.getAtomCount(); i++) {
        IAtom atom = molecule.getAtom(i);
        if (chiral && atom.getPoint2d() == null) {
            throw new CDKException("Atom number " + i + " has no 2D coordinates, but 2D coordinates are needed for creating chiral smiles");
        }
        // logger.debug("Setting all VISITED flags to false");
        atom.setFlag(CDKConstants.VISITED, false);
        if ((Long) atom.getProperty("CanonicalLable") == 1) {
            start = atom;
        }
    }
    // detect aromaticity
    if (useAromaticityFlag || chiral) {
        if (rings == null) {
            if (ringFinder == null) {
                ringFinder = new AllRingsFinder();
            }
            rings = ringFinder.findAllRings(molecule);
        }
        AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
        CDKHueckelAromaticityDetector.detectAromaticity(molecule);
    }
    if (chiral && rings.getAtomContainerCount() > 0) {
        List<?> v = RingPartitioner.partitionRings(rings);
        // logger.debug("RingSystems: " + v.size());
        for (int i = 0; i < v.size(); i++) {
            int counter = 0;
            Iterator<IAtomContainer> containers = RingSetManipulator.getAllAtomContainers((IRingSet) v.get(i)).iterator();
            while (containers.hasNext()) {
                IAtomContainer allrings = (IAtomContainer) containers.next();
                for (int k = 0; k < allrings.getAtomCount(); k++) {
                    if (!BondTools.isStereo(molecule, allrings.getAtom(k)) && hasWedges(molecule, allrings.getAtom(k)) != null) {
                        IBond bond = molecule.getBond(allrings.getAtom(k), hasWedges(molecule, allrings.getAtom(k)));
                        if (bond.getStereo() == IBond.Stereo.UP) {
                            allrings.getAtom(k).setProperty(RING_CONFIG, UP);
                        } else {
                            allrings.getAtom(k).setProperty(RING_CONFIG, DOWN);
                        }
                        counter++;
                    }
                }
                if (counter == 1) {
                    for (int k = 0; k < allrings.getAtomCount(); k++) {
                        IBond bond = molecule.getBond(allrings.getAtom(k), hasWedges(molecule, allrings.getAtom(k)));
                        if (bond != null) {
                            if (bond.getStereo() == IBond.Stereo.UP) {
                                allrings.getAtom(k).setProperty(RING_CONFIG, UP);
                            } else {
                                allrings.getAtom(k).setProperty(RING_CONFIG, DOWN);
                            }
                        }
                    }
                }
            }
        }
    }
    StringBuffer l = new StringBuffer();
    createSMILES(start, l, molecule, chiral, doubleBondConfiguration, useAromaticityFlag);
    rings = null;
    // remove all CanonicalLable/InvariancePair props
    for (int k = 0; k < molecule.getAtomCount(); k++) {
        molecule.getAtom(k).removeProperty("CanonicalLable");
        molecule.getAtom(k).removeProperty("InvariancePair");
    }
    return l.toString();
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) CDKException(org.openscience.cdk.exception.CDKException) IRingSet(org.openscience.cdk.interfaces.IRingSet) IBond(org.openscience.cdk.interfaces.IBond) AllRingsFinder(org.openscience.cdk.ringsearch.AllRingsFinder) IAtom(org.openscience.cdk.interfaces.IAtom) TestMethod(org.openscience.cdk.annotations.TestMethod)

Aggregations

IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)5 CDKException (org.openscience.cdk.exception.CDKException)3 File (java.io.File)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 LexBFS (nl.uu.cs.treewidth.algorithm.LexBFS)1 PermutationToTreeDecomposition (nl.uu.cs.treewidth.algorithm.PermutationToTreeDecomposition)1 InputData (nl.uu.cs.treewidth.input.GraphInput.InputData)1 JavaNanoTime (nl.uu.cs.treewidth.timing.JavaNanoTime)1 Stopwatch (nl.uu.cs.treewidth.timing.Stopwatch)1 TestMethod (org.openscience.cdk.annotations.TestMethod)1 InChIGeneratorFactory (org.openscience.cdk.inchi.InChIGeneratorFactory)1 InChIToStructure (org.openscience.cdk.inchi.InChIToStructure)1 IAtom (org.openscience.cdk.interfaces.IAtom)1 IBond (org.openscience.cdk.interfaces.IBond)1 IMoleculeSet (org.openscience.cdk.interfaces.IMoleculeSet)1 IRingSet (org.openscience.cdk.interfaces.IRingSet)1 AllRingsFinder (org.openscience.cdk.ringsearch.AllRingsFinder)1 SmilesParser (org.openscience.cdk.smiles.SmilesParser)1