Search in sources :

Example 36 with InChIGenerator

use of org.openscience.cdk.inchi.InChIGenerator in project MetFragRelaunched by ipb-halle.

the class GetRankOfCandidateCSV method calculateInchiKey.

/**
 * @param con
 * @return
 * @throws CDKException
 */
public static String calculateInchiKey(IAtomContainer con) throws CDKException {
    AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(con);
    CDKHydrogenAdder hAdder = CDKHydrogenAdder.getInstance(con.getBuilder());
    for (int i = 0; i < con.getAtomCount(); i++) {
        try {
            hAdder.addImplicitHydrogens(con, con.getAtom(i));
        } catch (CDKException e) {
            continue;
        }
    }
    AtomContainerManipulator.convertImplicitToExplicitHydrogens(con);
    InChIGeneratorFactory factory = InChIGeneratorFactory.getInstance();
    InChIGenerator gen = factory.getInChIGenerator(con);
    return gen.getInchiKey().split("-")[0];
}
Also used : InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) CDKException(org.openscience.cdk.exception.CDKException) CDKHydrogenAdder(org.openscience.cdk.tools.CDKHydrogenAdder) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory)

Example 37 with InChIGenerator

use of org.openscience.cdk.inchi.InChIGenerator in project MetFragRelaunched by ipb-halle.

the class GetRankOfCandidatePSV method calculateInchiKey.

/**
 * @param con
 * @return
 * @throws CDKException
 */
public static String calculateInchiKey(IAtomContainer con) throws CDKException {
    AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(con);
    CDKHydrogenAdder hAdder = CDKHydrogenAdder.getInstance(con.getBuilder());
    for (int i = 0; i < con.getAtomCount(); i++) {
        try {
            hAdder.addImplicitHydrogens(con, con.getAtom(i));
        } catch (CDKException e) {
            continue;
        }
    }
    AtomContainerManipulator.convertImplicitToExplicitHydrogens(con);
    InChIGeneratorFactory factory = InChIGeneratorFactory.getInstance();
    InChIGenerator gen = factory.getInChIGenerator(con);
    return gen.getInchiKey().split("-")[0];
}
Also used : InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) CDKException(org.openscience.cdk.exception.CDKException) CDKHydrogenAdder(org.openscience.cdk.tools.CDKHydrogenAdder) InChIGeneratorFactory(org.openscience.cdk.inchi.InChIGeneratorFactory)

Example 38 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}, using the given options.
 * This options String consists of one or more, space-delimited options, such as FixedH.
 *
 * @param molecule the {@link IMolecule} to create the InChI for
 * @return         an {@link InChI} object
 * @throws Exception
 */
public InChI generate(IMolecule molecule, String options) 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, options);
        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)

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