Search in sources :

Example 16 with InChIGenerator

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

the class TestTautomers method testInChIGenerator.

/*
	 * public void testAdenine() throws CDKException, CloneNotSupportedException
	 * { InChITautomerGenerator tautomerGenerator = new
	 * InChITautomerGenerator(); IChemObjectBuilder builder =
	 * SilentChemObjectBuilder.getInstance(); IAtomContainer mol =
	 * builder.newInstance(IAtomContainer.class); IAtom a1 =
	 * builder.newInstance(IAtom.class, "N"); mol.addAtom(a1); IAtom a2 =
	 * builder.newInstance(IAtom.class, "N"); mol.addAtom(a2); IAtom a3 =
	 * builder.newInstance(IAtom.class, "N"); mol.addAtom(a3); IAtom a4 =
	 * builder.newInstance(IAtom.class, "N"); mol.addAtom(a4); IAtom a5 =
	 * builder.newInstance(IAtom.class, "N"); mol.addAtom(a5); IAtom a6 =
	 * builder.newInstance(IAtom.class, "C"); mol.addAtom(a6); IAtom a7 =
	 * builder.newInstance(IAtom.class, "C"); mol.addAtom(a7); IAtom a8 =
	 * builder.newInstance(IAtom.class, "C"); mol.addAtom(a8); IAtom a9 =
	 * builder.newInstance(IAtom.class, "C"); mol.addAtom(a9); IAtom a10 =
	 * builder.newInstance(IAtom.class, "C"); mol.addAtom(a10); IAtom a11 =
	 * builder.newInstance(IAtom.class, "H"); mol.addAtom(a11); IAtom a12 =
	 * builder.newInstance(IAtom.class, "H"); mol.addAtom(a12); IAtom a13 =
	 * builder.newInstance(IAtom.class, "H"); mol.addAtom(a13); IAtom a14 =
	 * builder.newInstance(IAtom.class, "H"); mol.addAtom(a14); IAtom a15 =
	 * builder.newInstance(IAtom.class, "H"); mol.addAtom(a15); IBond b1 =
	 * builder.newInstance(IBond.class, a1, a6, IBond.Order.SINGLE);
	 * mol.addBond(b1); IBond b2 = builder.newInstance(IBond.class, a1, a9,
	 * IBond.Order.SINGLE); mol.addBond(b2); IBond b3 =
	 * builder.newInstance(IBond.class, a1, a11, IBond.Order.SINGLE);
	 * mol.addBond(b3); IBond b4 = builder.newInstance(IBond.class, a2, a7,
	 * IBond.Order.SINGLE); mol.addBond(b4); IBond b5 =
	 * builder.newInstance(IBond.class, a2, a9, IBond.Order.DOUBLE);
	 * mol.addBond(b5); IBond b6 = builder.newInstance(IBond.class, a3, a7,
	 * IBond.Order.DOUBLE); mol.addBond(b6); IBond b7 =
	 * builder.newInstance(IBond.class, a3, a10, IBond.Order.SINGLE);
	 * mol.addBond(b7); IBond b8 = builder.newInstance(IBond.class, a4, a8,
	 * IBond.Order.SINGLE); mol.addBond(b8); IBond b9 =
	 * builder.newInstance(IBond.class, a4, a10, IBond.Order.DOUBLE);
	 * mol.addBond(b9); IBond b10 = builder.newInstance(IBond.class, a5, a8,
	 * IBond.Order.SINGLE); mol.addBond(b10); IBond b11 =
	 * builder.newInstance(IBond.class, a5, a14, IBond.Order.SINGLE);
	 * mol.addBond(b11); IBond b12 = builder.newInstance(IBond.class, a5, a15,
	 * IBond.Order.SINGLE); mol.addBond(b12); IBond b13 =
	 * builder.newInstance(IBond.class, a6, a7, IBond.Order.SINGLE);
	 * mol.addBond(b13); IBond b14 = builder.newInstance(IBond.class, a6, a8,
	 * IBond.Order.DOUBLE); mol.addBond(b14); IBond b15 =
	 * builder.newInstance(IBond.class, a9, a12, IBond.Order.SINGLE);
	 * mol.addBond(b15); IBond b16 = builder.newInstance(IBond.class, a10, a13,
	 * IBond.Order.SINGLE); mol.addBond(b16);
	 * 
	 * AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
	 * 
	 * List<IAtomContainer> tautomers = tautomerGenerator.getTautomers(mol); //
	 * Assert.assertEquals(8, tautomers.size());
	 * System.out.println("tautomers.size() = " + tautomers.size());
	 * 
	 * TestStrVisualizer tsv = new TestStrVisualizer(tautomers,
	 * "InChI - algorithm");
	 * 
	 * }
	 */
public void testInChIGenerator(String smi) throws Exception {
    logger.log(Level.INFO, "Testing: " + smi);
    IAtomContainer mol = SmartsHelper.getMoleculeFromSmiles(smi, true);
    InChIGeneratorFactory igf = InChIGeneratorFactory.getInstance();
    InChIGenerator ig = igf.getInChIGenerator(mol);
    String inchi = ig.getInchi();
    String inchiKey = ig.getInchiKey();
    logger.log(Level.INFO, "inchi = " + inchi);
    logger.log(Level.INFO, "inchiKey = " + inchiKey);
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory)

Example 17 with InChIGenerator

use of org.openscience.cdk.inchi.InChIGenerator in project cdk by cdk.

the class InChINumbersTools method auxInfo.

/**
 * Obtain the InChI auxiliary info for the provided structure using
 * using the specified InChI options.
 *
 * @param  container the structure to obtain the numbers of
 * @param  flags varargs ... the JNA InChI flags
 * @return auxiliary info
 * @throws CDKException the inchi could not be generated
 */
static String auxInfo(IAtomContainer container, InchiFlag... flags) throws CDKException {
    InChIGeneratorFactory factory = InChIGeneratorFactory.getInstance();
    boolean org = factory.getIgnoreAromaticBonds();
    factory.setIgnoreAromaticBonds(true);
    InChIGenerator gen = factory.getInChIGenerator(container, flags);
    // an option on the singleton so we should reset for others
    factory.setIgnoreAromaticBonds(org);
    if (gen.getStatus() == InchiStatus.ERROR)
        throw new CDKException("Could not generate InChI Numbers: " + gen.getMessage());
    return gen.getAuxInfo();
}
Also used : InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) CDKException(org.openscience.cdk.exception.CDKException) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory)

Example 18 with InChIGenerator

use of org.openscience.cdk.inchi.InChIGenerator in project bacting by egonw.

the class InChIManager method generate.

/**
 * Generates an InChI for the given {@link IMolecule}.
 *
 * @param molecule the {@link IMolecule} to create the InChI for
 * @return         an {@link InChI} object
 * @throws Exception
 */
public InChI generate(IMolecule molecule) throws Exception {
    if (!isAvailable()) {
        return InChI.FAILED_TO_CALCULATE;
    }
    Object adapted = molecule.getAdapter(IAtomContainer.class);
    if (adapted != null) {
        IAtomContainer container = (IAtomContainer) adapted;
        IAtomContainer clone = (IAtomContainer) container.clone();
        // remove aromaticity flags
        for (IAtom atom : clone.atoms()) atom.setFlag(CDKConstants.ISAROMATIC, false);
        for (IBond bond : clone.bonds()) bond.setFlag(CDKConstants.ISAROMATIC, false);
        InChIGenerator gen = factory.getInChIGenerator(clone);
        InchiStatus status = gen.getStatus();
        if (status == InchiStatus.SUCCESS || status == InchiStatus.WARNING) {
            InChI inchi = new InChI();
            inchi.setValue(gen.getInchi());
            inchi.setKey(gen.getInchiKey());
            return inchi;
        } else {
            throw new InvalidParameterException("Error while generating InChI (" + status + "): " + gen.getMessage());
        }
    } else {
        throw new InvalidParameterException("Given molecule must be a CDKMolecule");
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) InchiStatus(io.github.dan2097.jnainchi.InchiStatus) IBond(org.openscience.cdk.interfaces.IBond) InChI(net.bioclipse.inchi.InChI) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 19 with InChIGenerator

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

the class Record method CH_IUPAC.

public String CH_IUPAC() throws CDKException {
    if (ch_iupac.isEmpty())
        return "N/A";
    InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(ch_iupac);
    INCHI_RET ret = gen.getReturnStatus();
    if (ret == INCHI_RET.WARNING) {
        // Structure generated, but with warning message
        System.out.println("InChI warning: " + gen.getMessage());
    } else if (ret != INCHI_RET.OKAY) {
        // Structure generation failed
        throw new IllegalStateException("Structure generation failed: " + ret.toString() + " [" + gen.getMessage() + "]");
    }
    return gen.getInchi();
}
Also used : InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) INCHI_RET(net.sf.jniinchi.INCHI_RET)

Example 20 with InChIGenerator

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

the class AddMetaData method doAddInchikey.

/**
 * Automatically add CH$LINK: INCHIKEY
 * 	if no CH$IUPAC entry is available - do nothing
 *  if CH$IUPAC entry is available and no CH$LINK: INCHIKEY - add InChIKey
 *  if CH$IUPAC entry is available and CH$LINK: INCHIKEY - correct InChIKey
 */
public static String doAddInchikey(Record record) {
    String ch_iupac = record.CH_IUPAC();
    if ("N/A".equals(ch_iupac))
        return record.toString();
    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 + ".");
            return record.toString();
        }
        // 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 + ".");
            return record.toString();
        }
        String INCHIKEY = inchiGen.getInchiKey();
        if (!record.CH_LINK().containsKey("INCHIKEY")) {
            LinkedHashMap<String, String> ch_link = record.CH_LINK();
            ch_link.put("INCHIKEY", INCHIKEY);
            // sort
            ch_link = ch_link.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (u, v) -> u, LinkedHashMap::new));
            record.CH_LINK(ch_link);
        } else {
            if (!INCHIKEY.equals(record.CH_LINK().get("INCHIKEY"))) {
                logger.error("Wrong INCHIKEY identifier in record file.");
            }
        }
    } catch (CDKException e) {
        logger.error("Can not parse InChI string in \"CH$IUPAC\" field. Error: \"" + e.getMessage() + "\" for \"" + ch_iupac + "\".");
    }
    return record.toString();
}
Also used : Entry(java.util.Map.Entry) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) CDKException(org.openscience.cdk.exception.CDKException) InChIToStructure(org.openscience.cdk.inchi.InChIToStructure) INCHI_RET(net.sf.jniinchi.INCHI_RET) LinkedHashMap(java.util.LinkedHashMap)

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