Search in sources :

Example 21 with InChIGenerator

use of org.openscience.cdk.inchi.InChIGenerator in project MassBank-web by MassBank.

the class AddMetaData method doLink.

/**
 * Automatically process CH$LINK section.
 */
public static String doLink(Record record) {
    // add InChI-Key if missing
    boolean hasInchiKey = record.CH_LINK().containsKey("INCHIKEY");
    String INCHIKEY = record.CH_LINK().get("INCHIKEY");
    if (!hasInchiKey) {
        String ch_iupac = record.CH_IUPAC();
        if (!"N/A".equals(ch_iupac)) {
            try {
                // Get InChIToStructure
                InChIToStructure intostruct = InChIGeneratorFactory.getInstance().getInChIToStructure(ch_iupac, SilentChemObjectBuilder.getInstance());
                INCHI_RET ret = intostruct.getReturnStatus();
                if (ret == INCHI_RET.WARNING) {
                    // Structure generated, but with warning message
                    logger.warn("InChI warning: " + intostruct.getMessage());
                    logger.warn(record.ACCESSION());
                } else if (ret != INCHI_RET.OKAY) {
                    // Structure generation failed
                    logger.error("Can not parse InChI string in \"CH$IUPAC\" field. Structure generation failed: " + ret.toString() + " [" + intostruct.getMessage() + "] for " + ch_iupac + ".");
                }
                // Structure generation succeeded
                IAtomContainer m = intostruct.getAtomContainer();
                // prepare an InChIGenerator
                InChIGenerator inchiGen = InChIGeneratorFactory.getInstance().getInChIGenerator(m);
                ret = inchiGen.getReturnStatus();
                if (ret == INCHI_RET.WARNING) {
                    // InChI generated, but with warning message
                    logger.warn("InChI warning: " + inchiGen.getMessage());
                    logger.warn(record.ACCESSION());
                } else if (ret != INCHI_RET.OKAY) {
                    // InChI generation failed
                    logger.error("Can not create InChiKey from InChI string in \"CH$IUPAC\" field. Error: " + ret.toString() + " [" + inchiGen.getMessage() + "] for " + ch_iupac + ".");
                } else {
                    LinkedHashMap<String, String> ch_link = record.CH_LINK();
                    ch_link.put("INCHIKEY", inchiGen.getInchiKey());
                    record.CH_LINK(ch_link);
                    INCHIKEY = inchiGen.getInchiKey();
                    hasInchiKey = true;
                }
            } catch (CDKException e) {
                logger.error("Can not parse InChI string in \"CH$IUPAC\" field. Error: \"" + e.getMessage() + "\" for \"" + ch_iupac + "\".");
            }
        }
    }
    if (hasInchiKey) {
        // add COMPTOX if missing
        if (!record.CH_LINK().containsKey("COMPTOX")) {
            try {
                String COMPTOXID = getComptoxID(INCHIKEY);
                LinkedHashMap<String, String> ch_link = record.CH_LINK();
                ch_link.put("COMPTOX", COMPTOXID);
                record.CH_LINK(ch_link);
            } catch (JSONException | IOException e) {
                logger.warn("Could not fetch COMPTOX id.");
                logger.trace(e.getMessage());
            }
        } else {
            try {
                String COMPTOXID = getComptoxID(INCHIKEY);
                if (!COMPTOXID.equals(record.CH_LINK().get("COMPTOX"))) {
                    logger.error("Wrong COMPTOX database identifier in record file.");
                }
            } catch (JSONException | IOException e) {
                logger.warn("Could not fetch COMPTOX id for comparision.");
                logger.trace(e.getMessage());
            }
        }
        // add Chemspider if missing
        if (!record.CH_LINK().containsKey("CHEMSPIDER")) {
            try {
                String CHEMSPIDERID = getChemspiderID(INCHIKEY);
                LinkedHashMap<String, String> ch_link = record.CH_LINK();
                ch_link.put("CHEMSPIDER", CHEMSPIDERID);
                record.CH_LINK(ch_link);
            } catch (JSONException | IOException e) {
                logger.warn("Could not fetch CHEMSPIDER id.");
                logger.trace(e.getMessage());
            }
        } else {
            try {
                String CHEMSPIDERID = getChemspiderID(INCHIKEY);
                if (!CHEMSPIDERID.equals(record.CH_LINK().get("CHEMSPIDER"))) {
                    logger.error("Wrong CHEMSPIDER database identifier in record file.");
                }
            } catch (JSONException | IOException e) {
                logger.warn("Could not fetch CHEMSPIDER id for comparision.");
                logger.trace(e.getMessage());
            }
        }
    }
    return record.toString();
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) CDKException(org.openscience.cdk.exception.CDKException) InChIToStructure(org.openscience.cdk.inchi.InChIToStructure) JSONException(org.json.JSONException) IOException(java.io.IOException) INCHI_RET(net.sf.jniinchi.INCHI_RET) LinkedHashMap(java.util.LinkedHashMap)

Example 22 with InChIGenerator

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

the class InchiProcessorTest method testProcessStereo.

@Test
public void testProcessStereo() throws Exception {
    InChIGeneratorFactory factory = InChIGeneratorFactory.getInstance();
    SmilesParser p = new SmilesParser(SilentChemObjectBuilder.getInstance());
    InputStream in = getClass().getClassLoader().getResourceAsStream("ambit2/core/data/stereo/stereo.sdf");
    IIteratingChemObjectReader<IAtomContainer> reader = FileInputState.getReader(in, "stereo.sdf");
    int count = 0;
    try {
        while (reader.hasNext()) {
            IAtomContainer o = reader.next();
            InChIGenerator gen = factory.getInChIGenerator(o);
            INCHI_RET ret = gen.getReturnStatus();
            if (ret != INCHI_RET.OKAY) {
                System.err.println(String.format("InChI failed: %s [%s]", ret.toString(), gen.getMessage()));
            }
            String inchi = gen.getInchi();
            System.out.println(inchi);
            Assert.assertEquals(o.getProperty("InChI"), inchi);
            Object smiles = o.getProperty("SMILES");
            Assert.assertNotNull(smiles);
            System.out.println(smiles);
            IAtomContainer smilesMol = p.parseSmiles(smiles.toString());
            gen = factory.getInChIGenerator(smilesMol);
            System.out.println(gen.getReturnStatus() + " " + gen.getMessage());
            System.out.println("from SMILES " + gen.getInchi());
            count++;
            Assert.assertEquals(inchi, gen.getInchi());
        }
        Assert.assertEquals(4, count);
    } finally {
        reader.close();
    }
}
Also used : SmilesParser(org.openscience.cdk.smiles.SmilesParser) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) InputStream(java.io.InputStream) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory) IBitFingerprint(org.openscience.cdk.fingerprint.IBitFingerprint) INCHI_RET(net.sf.jniinchi.INCHI_RET) Test(org.junit.Test)

Example 23 with InChIGenerator

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

the class ParserTest method testStereo.

public void testStereo(SmilesGenerator g) throws Exception {
    SmilesParser p = new SmilesParser(SilentChemObjectBuilder.getInstance());
    InChIGeneratorFactory factory = InChIGeneratorFactory.getInstance();
    IIteratingChemObjectReader<IAtomContainer> reader = null;
    int error = 0;
    try {
        InputStream in = ParserTest.class.getClassLoader().getResourceAsStream("ambit2/dbcli/test/stereo.sdf");
        Assert.assertNotNull(in);
        reader = new InteractiveIteratingMDLReader(in, SilentChemObjectBuilder.getInstance());
        Assert.assertNotNull(reader);
        StructureStandardizer standardizer = new StructureStandardizer();
        standardizer.setGenerateSMILES(true);
        standardizer.setGenerateSMILES_Canonical(false);
        standardizer.setNeutralise(true);
        standardizer.setSplitFragments(true);
        standardizer.setGenerateTautomers(false);
        standardizer.setImplicitHydrogens(true);
        standardizer.setClearIsotopes(true);
        while (reader.hasNext()) {
            IAtomContainer m1 = reader.next();
            AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(m1);
            try {
                String smi = g.create(m1);
                IAtomContainer m2 = p.parseSmiles(smi);
                AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(m2);
                String newsmiles = g.create(m2);
                Assert.assertEquals(smi, newsmiles);
                InChIGenerator gen1 = factory.getInChIGenerator(m1);
                InChIGenerator gen2 = factory.getInChIGenerator(m2);
                Assert.assertEquals(gen1.getInchi(), gen2.getInchi());
                Assert.assertEquals(gen1.getInchiKey(), gen2.getInchiKey());
                Assert.assertEquals(g.create(m1), g.create(m2));
                System.out.println(smi);
                printstereo(m1);
                System.out.println(newsmiles);
                printstereo(m2);
                IAtomContainer m1_transformed = standardizer.process(m1);
                AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(m1_transformed);
                IAtomContainer m2_transformed = standardizer.process(m2);
                AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(m2_transformed);
                System.out.println(g.create(m1_transformed));
                printstereo(m1_transformed);
                System.out.println(g.create(m2_transformed));
                printstereo(m2_transformed);
                gen1 = factory.getInChIGenerator(m1_transformed);
                gen2 = factory.getInChIGenerator(m2_transformed);
                Assert.assertEquals(gen1.getInchi(), gen2.getInchi());
                Assert.assertEquals(gen1.getInchiKey(), gen2.getInchiKey());
            } catch (Exception x) {
                error++;
                System.out.println(x.getMessage());
            }
        }
    } finally {
        if (reader != null)
            reader.close();
    }
    Assert.assertEquals(0, error);
}
Also used : SmilesParser(org.openscience.cdk.smiles.SmilesParser) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) InputStream(java.io.InputStream) InteractiveIteratingMDLReader(ambit2.core.io.InteractiveIteratingMDLReader) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory) StructureStandardizer(ambit2.tautomers.processor.StructureStandardizer)

Example 24 with InChIGenerator

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

the class StartingMaterialsDataBase method createStartingMaterialsFile.

public static void createStartingMaterialsFile(File sourceFile, File outputFile, Map<String, Integer> columnIndices, StartMaterialStandartization standartization) throws Exception {
    // Setup input columns indices
    Integer idCol = columnIndices.get("id");
    Integer smilesCol = columnIndices.get("smiles");
    Integer mwCol = columnIndices.get("mw");
    if (idCol == null || smilesCol == null)
        throw new Exception("Incorrect columns indices!");
    int maxIndex = idCol;
    if (maxIndex < smilesCol)
        maxIndex = smilesCol;
    if (mwCol != null)
        if (maxIndex < mwCol)
            maxIndex = mwCol;
    // Inchi generator
    List<INCHI_OPTION> inchiOpt = getBacisInchiOptions();
    InChIGeneratorFactory inchiGF = InChIGeneratorFactory.getInstance();
    // Setup file reader and writer
    RandomAccessFile sf = ReactionWriteUtils.createReader(sourceFile);
    FileWriter outf = ReactionWriteUtils.createWriter(outputFile.getAbsolutePath());
    String splitter = "\t";
    long length = sf.length();
    int lineNum = 0;
    try {
        while (sf.getFilePointer() < length) {
            String line = sf.readLine();
            lineNum++;
            line = line.trim();
            if (line.equals(""))
                continue;
            String[] tokens = line.split(splitter);
            if (maxIndex >= tokens.length)
                // error some of the column are missing
                continue;
            String id = tokens[idCol].trim();
            String smiles = tokens[smilesCol].trim();
            String inchiKey = null;
            try {
                IAtomContainer mol = SmartsHelper.getMoleculeFromSmiles(smiles);
                InChIGenerator ig = inchiGF.getInChIGenerator(mol, inchiOpt);
                INCHI_RET returnCode = ig.getReturnStatus();
                if (INCHI_RET.ERROR == returnCode) {
                // Error
                }
                inchiKey = ig.getInchiKey();
            } catch (Exception e) {
                // SEVERE error;
                continue;
            }
            String s = String.format("%s\t%s\t%s", id, smiles, inchiKey);
            if (mwCol != null)
                s = s + "\t" + tokens[mwCol].trim();
            s = s + "\n";
            System.out.println("Line " + lineNum + "   " + s);
            outf.write(s);
            outf.flush();
        }
    } catch (Exception e) {
    }
    ReactionWriteUtils.closeReader(sf);
    ReactionWriteUtils.closeWriter(outf);
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) INCHI_OPTION(net.sf.jniinchi.INCHI_OPTION) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) FileWriter(java.io.FileWriter) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory) INCHI_RET(net.sf.jniinchi.INCHI_RET) RandomAccessFile(java.io.RandomAccessFile)

Example 25 with InChIGenerator

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

the class StartingMaterialsDataBase method initlizeFromSmilesArray.

void initlizeFromSmilesArray(String[] smiles) throws Exception {
    if (smiles == null)
        return;
    // Inchi generator
    List<INCHI_OPTION> inchiOpt = getBacisInchiOptions();
    InChIGeneratorFactory inchiGF = InChIGeneratorFactory.getInstance();
    for (int i = 0; i < smiles.length; i++) {
        String inchiKey = null;
        try {
            IAtomContainer mol = SmartsHelper.getMoleculeFromSmiles(smiles[i]);
            InChIGenerator ig = inchiGF.getInChIGenerator(mol, inchiOpt);
            INCHI_RET returnCode = ig.getReturnStatus();
            if (INCHI_RET.ERROR == returnCode) {
            // Error
            }
            inchiKey = ig.getInchiKey();
        } catch (Exception e) {
            // SEVERE error;
            continue;
        }
        if (inchiKey != null) {
            StartMaterialData smd = new StartMaterialData();
            smd.id = "SMI-" + (i + 1);
            smd.smiles = smiles[i];
            materials.put(inchiKey, smd);
        }
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) INCHI_OPTION(net.sf.jniinchi.INCHI_OPTION) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory) INCHI_RET(net.sf.jniinchi.INCHI_RET)

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