Skip to content

Instantly share code, notes, and snippets.

@egonw
Created September 4, 2021 10:54
Show Gist options
  • Save egonw/c450fbf4ec20d589ccaf44c402d3b71f to your computer and use it in GitHub Desktop.
Save egonw/c450fbf4ec20d589ccaf44c402d3b71f to your computer and use it in GitHub Desktop.
Convert a file with SMILES to InChIKeys
@Grab(group='org.openscience.cdk', module='cdk-bundle', version='2.5')
// Copyright (C) 2021 Egon Willighagen <0000-0001-7542-0286@orcid.org>
//
// MIT license
//
// If you use this script, please cite the CDK 2.0 article:
// https://jcheminf.biomedcentral.com/articles/10.1186/s13321-017-0220-4
import org.openscience.cdk.interfaces.*;
import org.openscience.cdk.io.*;
import org.openscience.cdk.io.iterator.*;
import org.openscience.cdk.*;
import org.openscience.cdk.silent.*;
import org.openscience.cdk.smiles.*;
import org.openscience.cdk.tools.manipulator.*;
import org.openscience.cdk.inchi.*;
import net.sf.jniinchi.INCHI_RET;
def cli = new CliBuilder(usage: 'inchis.groovy')
cli.h(longOpt: 'help', 'print this message')
cli.f(longOpt: 'input-file', args:1, argName:'filename', 'Name of the SDF file')
def options = cli.parse(args)
if (options.help) {
cli.usage()
System.exit(0)
}
smiFile = "jcheminf.smi"
if (options.f) {
sdfFile = options.f
}
factory = InChIGeneratorFactory.getInstance();
sp = new SmilesParser(
SilentChemObjectBuilder.getInstance()
)
new File(smiFile).eachLine { smi ->
IAtomContainer mol = sp.parseSmiles(smi)
generator = factory.getInChIGenerator(mol);
inchi = ""
if (generator.getReturnStatus() == INCHI_RET.OKAY ||
generator.getReturnStatus() == INCHI_RET.WARNING) {
inchi = generator.getInchiKey()
println "$inchi"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment