Skip to content

Commit

Permalink
Added a method to generate non-standard InChIs
Browse files Browse the repository at this point in the history
  • Loading branch information
egonw committed Aug 16, 2012
1 parent 44def77 commit 3f033f3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Expand Up @@ -39,6 +39,15 @@ public void testGenerate() throws Exception {
Assert.assertEquals("InChI=1S/CH4/h1H4", inchiObj.getValue());
}

@Test
public void testGenerateWithOptions() throws Exception {
IMolecule mol = cdk.fromSMILES("OC=O");
Assert.assertNotNull("Input structure is unexpectedly null", mol);
InChI inchiObj = inchi.generate(mol, "FixedH");
Assert.assertNotNull(inchiObj);
Assert.assertEquals("InChI=1/CH2O2/c2-1-3/h1H,(H,2,3)/f/h2H", inchiObj.getValue());
}

@Test
public void testGenerateNoStereo() throws Exception {
IMolecule mol = cdk.fromSMILES("ClC(Br)(F)(O)");
Expand Down
Expand Up @@ -10,6 +10,8 @@
******************************************************************************/
package net.bioclipse.inchi.business;

import java.util.List;

import net.bioclipse.core.PublishedClass;
import net.bioclipse.core.PublishedMethod;
import net.bioclipse.core.Recorded;
Expand Down Expand Up @@ -38,6 +40,17 @@ public interface IInChIManager extends IBioclipseManager {
public BioclipseJob<InChI> generate(IMolecule molecule,
BioclipseJobUpdateHook<InChI> h );

@Recorded
@PublishedMethod(
params = "IMolecule molecule, String options",
methodSummary = "Generates the InChI and InChIKey for the " +
"given molecule, using the given options. This options String consists of " +
"one or more, space-delimited options, such as FixedH.")
@TestMethods("testGenerateWithOptions")
public InChI generate(IMolecule molecule, String options) throws Exception;
public BioclipseJob<InChI> generate(IMolecule molecule, String options,
BioclipseJobUpdateHook<InChI> h );

@Recorded
@PublishedMethod(
methodSummary = "Loads the InChI library.")
Expand Down
Expand Up @@ -87,6 +87,50 @@ public void generate( IMolecule molecule,
}
}

public void generate( IMolecule molecule, String options,
IReturner<InChI> returner,
IProgressMonitor monitor)
throws Exception {
monitor.beginTask("Calculating InChI", IProgressMonitor.UNKNOWN);
// return early if InChI library could not be loaded
if (!isAvailable()) {
returner.completeReturn(InChI.FAILED_TO_CALCULATE);
return;
}

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);
INCHI_RET status = gen.getReturnStatus();
if(monitor.isCanceled())
throw new OperationCanceledException();
if (status == INCHI_RET.OKAY ||
status == INCHI_RET.WARNING) {
monitor.done();
InChI inchi = new InChI();
inchi.setValue(gen.getInchi());
inchi.setKey(gen.getInchiKey());
returner.completeReturn( inchi );
} else {
throw new InvalidParameterException(
"Error while generating InChI (" + status + "): " +
gen.getMessage()
);
}
} else {
throw new InvalidParameterException(
"Given molecule must be a CDKMolecule"
);
}
}

public String load() {
if (factory == null) {
try {
Expand Down

0 comments on commit 3f033f3

Please sign in to comment.