Search in sources :

Example 26 with InChIGenerator

use of org.openscience.cdk.inchi.InChIGenerator in project ambit-mirror by ideaconsult.

the class FilterTautomers method duplicationFilterBasedOnInChI.

List<IAtomContainer> duplicationFilterBasedOnInChI(List<IAtomContainer> tautomers) throws Exception {
    InChIGeneratorFactory igf = InChIGeneratorFactory.getInstance();
    List<IAtomContainer> uniqueTautomers = new ArrayList<IAtomContainer>();
    Set<String> tKeys = new TreeSet<String>();
    for (int i = 0; i < tautomers.size(); i++) {
        // IAtomContainer clone =
        // getCloneForInChIGeneration(tautomers.get(i)); //the clone
        // contains explicit H atoms
        IAtomContainer clone = tautomers.get(i);
        InChIGenerator ig = igf.getInChIGenerator(clone, options);
        INCHI_RET returnCode = ig.getReturnStatus();
        if (INCHI_RET.ERROR == returnCode) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, ig.getMessage());
        }
        String inchiKey = ig.getInchiKey();
        if (!tKeys.contains(inchiKey)) {
            tKeys.add(inchiKey);
            tautomers.get(i).setProperty(Property.opentox_InChI, ig.getInchi());
            tautomers.get(i).setProperty(Property.opentox_InChIKey, ig.getInchiKey());
            uniqueTautomers.add(tautomers.get(i));
        }
    }
    return uniqueTautomers;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory) INCHI_RET(net.sf.jniinchi.INCHI_RET)

Example 27 with InChIGenerator

use of org.openscience.cdk.inchi.InChIGenerator in project ambit-mirror by ideaconsult.

the class StructureStandardizer method process.

@Override
public IAtomContainer process(IAtomContainer mol) throws Exception {
    IAtomContainer processed = mol;
    try {
        if (processed.getAtomCount() == 0) {
            processed.setProperty(ERROR_TAG, "Empty structure");
            throw new EmptyMoleculeException();
        }
        String err = processed.getProperty(ERROR_TAG);
        if (err == null)
            processed.setProperty(ERROR_TAG, "");
        if (neutralise) {
            if (neutraliser == null)
                neutraliser = new NeutraliseProcessor(logger);
            processed = neutraliser.process(processed);
            fragments.setAtomtypeasproperties(neutraliser.isAtomtypeasproperties());
            fragments.setSparseproperties(neutraliser.isSparseproperties());
        }
        if (splitFragments && (processed != null))
            processed = fragments.process(processed);
        if (processed != null) {
            if (generate2D && (StructureTypeProcessor.has2DCoordinates(processed) <= 1)) {
                sdg.setMolecule(processed, false);
                sdg.generateCoordinates(new Vector2d(0, 1));
                processed = sdg.getMolecule();
            }
            if (clearIsotopes) {
                if (isotopesProcessor == null) {
                    isotopesProcessor = new IsotopesProcessor(logger);
                }
                processed = isotopesProcessor.process(processed);
            }
            int newse = 0;
            int oldse = 0;
            if (generateTautomers)
                try {
                    // todo
                    processed = tautomers.process(processed);
                } catch (Exception x) {
                    StringWriter w = new StringWriter();
                    x.printStackTrace(new PrintWriter(w));
                    logger.log(Level.WARNING, "MSG_TAUTOMERGEN", new Object[] { x, w.toString() });
                    err = processed.getProperty(ERROR_TAG);
                    processed.setProperty(ERROR_TAG, String.format("Tautomer %s %s %s", err == null ? "" : err, x.getClass().getName(), x.getMessage()));
                }
            if (implicitHydrogens)
                try {
                    processed = AtomContainerManipulator.suppressHydrogens(processed);
                } catch (Exception x) {
                    if (processed != null) {
                        err = processed.getProperty(ERROR_TAG);
                        processed.setProperty(ERROR_TAG, String.format("%s %s %s", err == null ? "" : err, x.getClass().getName(), x.getMessage()));
                    }
                }
            if (generateStereofrom2D)
                try {
                    StereoElementFactory stereo = StereoElementFactory.using2DCoordinates(processed);
                    for (IStereoElement se : processed.stereoElements()) oldse++;
                    List<IStereoElement> stereoElements = stereo.createAll();
                    for (IStereoElement se : stereoElements) {
                        newse++;
                    }
                    if ((oldse > 0) && (newse > 0))
                        processed.setProperty(stereo.getClass().getName(), String.format("StereoElements %s --> %s", oldse, newse));
                    processed.setStereoElements(stereoElements);
                } catch (Exception x) {
                    if (processed != null) {
                        err = processed.getProperty(ERROR_TAG);
                        processed.setProperty(ERROR_TAG, String.format("%s %s %s", err == null ? "" : err, x.getClass().getName(), x.getMessage()));
                    }
                }
            if (generateInChI) {
                if (processed.getProperty(Property.opentox_InChI) == null)
                    try {
                        if (igf == null)
                            igf = InChIGeneratorFactory.getInstance();
                        InChIGenerator gen = igf.getInChIGenerator(processed, generateTautomers ? tautomers.tautomerManager.tautomerFilter.getInchiOptions() : options);
                        switch(gen.getReturnStatus()) {
                            case OKAY:
                                {
                                    processed.setProperty(Property.opentox_InChI, gen.getInchi());
                                    processed.setProperty(Property.opentox_InChIKey, gen.getInchiKey());
                                    break;
                                }
                            case WARNING:
                                {
                                    processed.setProperty(Property.opentox_InChI, gen.getInchi());
                                    processed.setProperty(Property.opentox_InChIKey, gen.getInchiKey());
                                    logger.log(Level.FINE, "MSG_INCHIGEN", new Object[] { gen.getReturnStatus(), gen.getMessage() });
                                    break;
                                }
                            default:
                                {
                                    processed.setProperty(Property.opentox_InChI, "");
                                    processed.setProperty(Property.opentox_InChIKey, "");
                                    err = processed.getProperty(ERROR_TAG);
                                    processed.setProperty(ERROR_TAG, String.format("InChI Generation %s %s %s", err == null ? "" : err, gen.getReturnStatus(), gen.getMessage()));
                                    logger.log(Level.WARNING, "MSG_INCHIGEN", new Object[] { gen.getReturnStatus(), gen.getMessage() });
                                    break;
                                }
                        }
                    // gen.getReturnStatus().
                    } catch (Exception x) {
                        StringWriter w = new StringWriter();
                        x.printStackTrace(new PrintWriter(w));
                        logger.log(Level.WARNING, "MSG_INCHIGEN", new Object[] { x, w.toString() });
                        err = processed.getProperty(ERROR_TAG);
                        processed.setProperty(ERROR_TAG, String.format("InChI Generation %s %s %s", err == null ? "" : err, x.getClass().getName(), x.getMessage()));
                    }
            }
            if (getSmilesGenerator() != null) {
                Property p_smiles = Property.getSMILESInstance();
                try {
                    processed.setProperty(p_smiles, getSmilesGenerator().create(processed));
                } catch (Exception x) {
                    StringWriter w = new StringWriter();
                    x.printStackTrace(new PrintWriter(w));
                    logger.log(Level.WARNING, "MSG_SMILESGEN", new Object[] { x, processed.getProperty(Property.opentox_InChIKey) });
                    logger.log(Level.FINE, "MSG_SMILESGEN", new Object[] { x, w.toString() });
                    String inchi_err = "";
                    if (processed.getProperty(p_smiles) == null)
                        try {
                            Object inchi = processed.getProperty(Property.opentox_InChI);
                            String last_resort_smiles = inchi == null ? "" : InChI2SMILES(inchi.toString());
                            processed.setProperty(p_smiles, last_resort_smiles);
                            inchi_err = "SMILES generated from InChI as fallback";
                        } catch (Exception xx) {
                            inchi_err = x.getMessage();
                            processed.setProperty(p_smiles, "");
                        }
                    err = processed.getProperty(ERROR_TAG);
                    String msg = String.format("%s %s %s %s", err == null ? "" : err, x.getClass().getName(), x.getMessage(), inchi_err);
                    processed.setProperty(ERROR_TAG, msg);
                    logger.log(Level.WARNING, msg);
                }
            }
            renameTags(processed, tags);
        } else {
            logger.log(Level.WARNING, "Null molecule after processing", mol.getProperties());
        }
    } catch (EmptyMoleculeException x) {
        processed.setProperty(Property.opentox_InChI, "");
        processed.setProperty(Property.opentox_InChIKey, "");
        logger.log(Level.SEVERE, x.getMessage() + " " + mol.getProperties().toString(), x);
    } catch (Exception x) {
        logger.log(Level.SEVERE, x.getMessage() + " " + mol.getProperties().toString(), x);
        x.printStackTrace();
    } finally {
        if (mol != null && processed != null) {
            Iterator<Entry<Object, Object>> p = mol.getProperties().entrySet().iterator();
            // molecule
            while (p.hasNext()) {
                Entry<Object, Object> entry = p.next();
                Object value = processed.getProperty(entry.getKey());
                if (value == null || "".equals(value.toString().trim()))
                    processed.setProperty(entry.getKey(), entry.getValue());
            }
        }
    // System.out.println(processed.getProperties());
    }
    return processed;
}
Also used : EmptyMoleculeException(ambit2.base.exceptions.EmptyMoleculeException) NeutraliseProcessor(ambit2.smarts.processors.NeutraliseProcessor) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) StereoElementFactory(org.openscience.cdk.stereo.StereoElementFactory) EmptyMoleculeException(ambit2.base.exceptions.EmptyMoleculeException) Entry(java.util.Map.Entry) Vector2d(javax.vecmath.Vector2d) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) List(java.util.List) Property(ambit2.base.data.Property) IStereoElement(org.openscience.cdk.interfaces.IStereoElement) IsotopesProcessor(ambit2.core.processors.IsotopesProcessor) PrintWriter(java.io.PrintWriter)

Example 28 with InChIGenerator

use of org.openscience.cdk.inchi.InChIGenerator in project ambit-mirror by ideaconsult.

the class CanonicTautomer method getMinInchiTautomer.

public IAtomContainer getMinInchiTautomer(List<IAtomContainer> tautomers) throws Exception {
    if (tautomers.isEmpty())
        return null;
    if (tautomers.size() == 1)
        return tautomers.get(0);
    InChIGeneratorFactory igf = InChIGeneratorFactory.getInstance();
    IAtomContainer minTaut = tautomers.get(0);
    InChIGenerator ig0 = igf.getInChIGenerator(minTaut, tman.tautomerFilter.getInchiOptions());
    String minInchiKey = ig0.getInchiKey();
    for (int i = 1; i < tautomers.size(); i++) {
        InChIGenerator ig = igf.getInChIGenerator(tautomers.get(i), tman.tautomerFilter.getInchiOptions());
        String inchiKey = ig.getInchiKey();
        if (minInchiKey.compareTo(inchiKey) > 0)
            minInchiKey = inchiKey;
    }
    return minTaut;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory)

Example 29 with InChIGenerator

use of org.openscience.cdk.inchi.InChIGenerator in project ambit-mirror by ideaconsult.

the class ZwitterionManager method getInchiKey.

String getInchiKey(IAtomContainer mol) throws Exception {
    InChIGenerator ig = igf.getInChIGenerator(mol, options);
    INCHI_RET returnCode = ig.getReturnStatus();
    if (INCHI_RET.ERROR == returnCode) {
        System.out.println("inchi error " + ig.getMessage());
        return null;
    }
    return ig.getInchiKey();
}
Also used : InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) INCHI_RET(net.sf.jniinchi.INCHI_RET)

Example 30 with InChIGenerator

use of org.openscience.cdk.inchi.InChIGenerator in project ambit-mirror by ideaconsult.

the class CanonicTautomerTestCases method testCanonic.

int testCanonic(String targetSmi, String expectedCanTautSmi) throws Exception {
    IAtomContainer mol = SmartsHelper.getMoleculeFromSmiles(targetSmi);
    IAtomContainer expectedCanTaut = SmartsHelper.getMoleculeFromSmiles(expectedCanTautSmi);
    tman.FlagRegisterOnlyBestRankTautomers = false;
    tman.setStructure(mol);
    List<IAtomContainer> tautomers = tman.generateTautomersIncrementaly();
    IAtomContainer t1 = tman.getCanonicTautomer(tautomers);
    if (FlagPrintTautomers) {
        InChIGenerator ig0 = igf.getInChIGenerator(expectedCanTaut, tman.tautomerFilter.getInchiOptions());
        String inchKey0 = ig0.getInchiKey();
        System.out.println("target = " + targetSmi);
        System.out.println("eexpected can. tautomer inchi = " + inchKey0);
        for (int i = 0; i < tautomers.size(); i++) {
            InChIGenerator ig = igf.getInChIGenerator(tautomers.get(i), tman.tautomerFilter.getInchiOptions());
            String inchKey = ig.getInchiKey();
            Double rank = tautomers.get(i).getProperty(TautomerConst.TAUTOMER_RANK);
            Double cactvs_rank = tautomers.get(i).getProperty(TautomerConst.CACTVS_ENERGY_RANK);
            System.out.println("   " + SmartsHelper.moleculeToSMILES(tautomers.get(i), false) + "  " + rank + "  " + cactvs_rank + "  " + inchKey);
        }
    }
    tman.FlagRegisterOnlyBestRankTautomers = true;
    // This is needed again to reset the tautomer manager
    tman.setStructure(mol);
    tautomers = tman.generateTautomersIncrementaly();
    IAtomContainer t2 = tman.getCanonicTautomer(tautomers);
    if (FlagPrintTautomers) {
        System.out.println("best found");
        for (int i = 0; i < tautomers.size(); i++) {
            InChIGenerator ig = igf.getInChIGenerator(tautomers.get(i), tman.tautomerFilter.getInchiOptions());
            String inchKey = ig.getInchiKey();
            Double rank = tautomers.get(i).getProperty(TautomerConst.TAUTOMER_RANK);
            Double cactvs_rank = tautomers.get(i).getProperty(TautomerConst.CACTVS_ENERGY_RANK);
            System.out.println("   " + SmartsHelper.moleculeToSMILES(tautomers.get(i), false) + "  " + rank + "  " + cactvs_rank + "  " + inchKey);
        }
        System.out.println();
    }
    // Checking canonical tauomers by inchi key
    InChIGenerator ig = igf.getInChIGenerator(expectedCanTaut, tman.tautomerFilter.getInchiOptions());
    String expInchiKey = ig.getInchiKey();
    ig = igf.getInChIGenerator(t1, tman.tautomerFilter.getInchiOptions());
    String key1 = ig.getInchiKey();
    ig = igf.getInChIGenerator(t2, tman.tautomerFilter.getInchiOptions());
    String key2 = ig.getInchiKey();
    assertEquals("Expected canonic tautomer (all registered): " + expectedCanTautSmi, expInchiKey, key1);
    assertEquals("Expected canonic tautomer (best registered): " + expectedCanTautSmi, expInchiKey, key2);
    return 0;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator)

Aggregations

InChIGenerator (org.openscience.cdk.inchi.InChIGenerator)38 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)22 InChIGeneratorFactory (org.openscience.cdk.inchi.InChIGeneratorFactory)17 INCHI_RET (net.sf.jniinchi.INCHI_RET)14 CDKException (org.openscience.cdk.exception.CDKException)14 ArrayList (java.util.ArrayList)9 CDKHydrogenAdder (org.openscience.cdk.tools.CDKHydrogenAdder)7 INCHI_OPTION (net.sf.jniinchi.INCHI_OPTION)6 SmilesParser (org.openscience.cdk.smiles.SmilesParser)6 Test (org.junit.Test)5 IBond (org.openscience.cdk.interfaces.IBond)5 EmptyMoleculeException (ambit2.base.exceptions.EmptyMoleculeException)3 InchiProcessor (ambit2.core.processors.structure.InchiProcessor)3 HDByteMolecularFormula (de.ipbhalle.metfraglib.molecularformula.HDByteMolecularFormula)3 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 FileReader (java.io.FileReader)3 IAtom (org.openscience.cdk.interfaces.IAtom)3 InchiStatus (io.github.dan2097.jnainchi.InchiStatus)2 InputStream (java.io.InputStream)2