Skip to content

Commit

Permalink
Merge pull request #846 from marco-foscato/master
Browse files Browse the repository at this point in the history
restore space as delimiter for string-based definition of InChI options
  • Loading branch information
johnmay committed Mar 7, 2022
2 parents bfce5f0 + b3222a8 commit 2726726
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected InChIGenerator(IAtomContainer atomContainer, boolean ignoreAromaticBon
* InChI library requires, then calls the library.
*
* @param atomContainer AtomContainer to generate InChI for.
* @param optStr Space delimited string of options to pass to InChI library.
* @param optStr Space or comma delimited string of options to pass to InChI library.
* Each option may optionally be preceded by a command line
* switch (/ or -).
* @param ignoreAromaticBonds if aromatic bonds should be treated as bonds of type single and double
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public InChIGenerator getInChIGenerator(IAtomContainer container) throws CDKExce
* Gets InChI generator for CDK IAtomContainer.
*
* @param container AtomContainer to generate InChI for.
* @param options String of options for InChI generation.
* @param options Space or comma delimited string of options for InChI generation.
* @return the InChI generator object
* @throws CDKException if the generator cannot be instantiated
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void processString(String optstr) {
break;
case 'W': // timeout
pos++;
int next = optstr.indexOf(',', pos);
int next = getIndexOfEither(optstr,',',' ', pos);
if (next < 0)
next = optstr.length();
String substring = optstr.substring(pos, next);
Expand All @@ -70,19 +70,39 @@ private void processString(String optstr) {
}
break;
default:
next = optstr.indexOf(',', pos);
next = getIndexOfEither(optstr,',',' ', pos);
if (next < 0)
next = optstr.length();
InchiFlag flag = optMap.get(optstr.substring(pos, next));
if (flag != null)
options.withFlag(flag);
else
logger.warn("Ignore unrecognized InChI flag:" + flag);
logger.warn("Ignore unrecognized InChI flag:" + optstr.substring(pos, next));
pos = next;
}
}
}


/**
*
* @param str the string to search into.
* @param chA a character to search for (Unicode code point).
* @param chB a character to search for (Unicode code point).
* @param fromIndex the index to start the search from.
* @return the index of the first occurrence of either of the characters in the character
* sequence represented by the string object that is greater than or equal to fromIndex,
* or -1 if the character does not occur.
*/
private static int getIndexOfEither(String str, char chA, char chB, int fromIndex) {
int iA = str.indexOf(chA, fromIndex);
int iB = str.indexOf(chB, fromIndex);
if (iA<0)
return iB;
if (iB<0)
return iA;
return Math.min(iA, iB);
}

static InchiOptions parseString(String str) {
if (str == null)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import org.openscience.cdk.templates.TestMoleculeFactory;
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;

import io.github.dan2097.jnainchi.InchiFlag;
import io.github.dan2097.jnainchi.InchiStatus;
import net.sf.jniinchi.INCHI_OPTION;
import net.sf.jniinchi.INCHI_RET;
import org.junit.Assert;
Expand Down Expand Up @@ -98,6 +100,35 @@ public void testGetInChIGenerator_IAtomContainer_NullString() throws Exception {
Assert.assertEquals(gen.getReturnStatus(), INCHI_RET.OKAY);
Assert.assertEquals("InChI=1S/ClH/h1H", gen.getInchi());
}

/**
* We must get the same result from using space or comma delimited string
*/
@Test
public void testGetInChIGenerator_IAtomContainer_StringSeparators() throws Exception {
SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
IAtomContainer ac = sp.parseSmiles("C[C@H](Cl)N");
String spaceSeparated = "";
String commaSeparated = "";
String commaAndSpaceSeparated = "";
InchiFlag[] opts = new InchiFlag[] {InchiFlag.SNon, InchiFlag.FixedH};
for (int i=0; i<opts.length; i++)
{
spaceSeparated = spaceSeparated + " " + opts[i];
commaSeparated = commaSeparated + "," + opts[i];
commaAndSpaceSeparated = commaAndSpaceSeparated + ", " + opts[i];
}

InChIGenerator genSpace = InChIGeneratorFactory.getInstance().getInChIGenerator(ac, spaceSeparated);
InChIGenerator genComma = InChIGeneratorFactory.getInstance().getInChIGenerator(ac, commaSeparated);
InChIGenerator genBoth = InChIGeneratorFactory.getInstance().getInChIGenerator(ac, commaAndSpaceSeparated);

Assert.assertEquals(genSpace.getStatus(), InchiStatus.SUCCESS);
Assert.assertEquals(genComma.getStatus(), InchiStatus.SUCCESS);
Assert.assertEquals(genBoth.getStatus(), InchiStatus.SUCCESS);
Assert.assertEquals(genBoth.getInchi(),genSpace.getInchi());
Assert.assertEquals(genComma.getInchi(),genSpace.getInchi());
}

/**
* Because we are setting an options, we get a non-standard InChI.
Expand Down

0 comments on commit 2726726

Please sign in to comment.