use of org.openscience.cdk.inchi.InChIGenerator in project MetFragRelaunched by ipb-halle.
the class InChIDeuteriumGeneration method main.
public static void main(String[] args) throws Exception {
boolean withAromaticRings = false;
boolean combinatorial = true;
/*
* read input inchis
*/
ArrayList<String> inchis = new ArrayList<String>();
ArrayList<Integer> numberToAddDeuteriums = new ArrayList<Integer>();
ArrayList<String> identifiers = new ArrayList<String>();
BufferedReader breader = new BufferedReader(new FileReader(new File(args[0])));
String line = "";
int identifier = 1;
while ((line = breader.readLine()) != null) {
String[] tmp = line.trim().split("\\s+");
inchis.add(tmp[0].trim());
if (tmp.length >= 2)
numberToAddDeuteriums.add(Integer.parseInt(tmp[1].trim()));
if (tmp.length == 3)
identifiers.add(tmp[2].trim());
else
identifiers.add(identifier + "");
identifier++;
}
breader.close();
/*
* generate deuterated version of inchi
*/
InChITautomerGenerator tg = new InChITautomerGenerator();
java.io.BufferedWriter bwriter = null;
if (args.length == 2) {
bwriter = new java.io.BufferedWriter(new java.io.FileWriter(new java.io.File(args[1])));
}
CDKHydrogenAdder adder = CDKHydrogenAdder.getInstance(DefaultChemObjectBuilder.getInstance());
for (int j = 0; j < inchis.size(); j++) {
System.out.println(inchis.get(j));
/*
* build the atom container
*/
IAtomContainer its = InChIGeneratorFactory.getInstance().getInChIToStructure(inchis.get(j), SilentChemObjectBuilder.getInstance()).getAtomContainer();
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(its);
try {
adder.addImplicitHydrogens(its);
} catch (Exception e) {
System.out.println("missed " + inchis.get(j) + " " + identifiers.get(j));
continue;
}
AtomContainerManipulator.convertImplicitToExplicitHydrogens(its);
int numberDeuteriums = 0;
int numberDeuteriumsEasilyExchanged = 0;
int numberDeuteriumsAromaticExchanged = 0;
int[] toExchange = searchForDeuteriumExchangeablePositions(new String[] { "O", "N", "S" }, its);
if (!combinatorial || (numberToAddDeuteriums.size() == 0 || toExchange.length <= numberToAddDeuteriums.get(j))) {
for (int i = 0; i < toExchange.length; i++) {
int numberExchanged = setAllExplicitDeuteriums(its, toExchange[i]);
numberDeuteriums += numberExchanged;
numberDeuteriumsEasilyExchanged += numberExchanged;
}
} else if (toExchange.length > numberToAddDeuteriums.get(j)) {
ArrayList<IAtomContainer> deuteratedStrutures = new ArrayList<IAtomContainer>();
ArrayList<Integer> numberDeuteriumsVec = new ArrayList<Integer>();
ArrayList<Integer> numberDeuteriumsEasilyExchangedVec = new ArrayList<Integer>();
// get all possible combinations of exchanges with given number
// of exchangeable hydrogens
int[][] combs = getCombinations(toExchange, numberToAddDeuteriums.get(j));
for (int k = 0; k < combs.length; k++) {
numberDeuteriumsEasilyExchanged = 0;
numberDeuteriums = 0;
InChIToStructure itsNew = InChIGeneratorFactory.getInstance().getInChIToStructure(inchis.get(j), SilentChemObjectBuilder.getInstance());
IAtomContainer con = itsNew.getAtomContainer();
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(con);
java.util.List<IAtomContainer> tautos = tg.getTautomers(con);
SmilesGenerator sg = new SmilesGenerator(SmiFlavor.Generic);
for (IAtomContainer tautomer : tautos) {
System.out.println(sg.create(tautomer));
}
IAtomContainer jiosNew = itsNew.getAtomContainer();
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(jiosNew);
adder.addImplicitHydrogens(jiosNew);
AtomContainerManipulator.convertImplicitToExplicitHydrogens(jiosNew);
// System.out.println(Arrays.toString(combs[k]));
for (int l = 0; l < combs[k].length; l++) {
addExplicitDeuterium(jiosNew, combs[k][l]);
numberDeuteriumsEasilyExchanged++;
numberDeuteriums++;
}
// System.out.println("JniInchiOutputStructure");
// printInfo(jiosNew);
numberDeuteriumsVec.add(numberDeuteriums);
numberDeuteriumsEasilyExchangedVec.add(numberDeuteriumsEasilyExchanged);
deuteratedStrutures.add(jiosNew);
}
for (int k = 0; k < deuteratedStrutures.size(); k++) {
IAtomContainer con = deuteratedStrutures.get(k);
InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(con);
String inchi = gen.getInchi();
String inchiKey = gen.getInchiKey();
HDByteMolecularFormula formula = null;
try {
formula = new HDByteMolecularFormula(inchi.split("/")[1]);
} catch (Exception e) {
System.err.println(identifiers.get(j));
e.printStackTrace();
System.exit(1);
}
formula.setNumberHydrogens((short) formula.getNumberHydrogens());
formula.setNumberDeuterium((short) (int) numberDeuteriumsVec.get(k));
double mass = formula.getMonoisotopicMass();
System.out.println(identifiers.get(j) + "-" + (k + 1) + "|" + inchi + "|" + formula.toString() + "|" + mass + "|" + inchiKey.split("-")[0] + "|" + inchiKey.split("-")[1] + "|" + numberDeuteriumsEasilyExchangedVec.get(k) + "|0|" + (toExchange.length - numberToAddDeuteriums.get(j)) + "|" + (mass + Constants.getMonoisotopicMassOfAtom("D") - Constants.ELECTRON_MASS) + "|" + (mass - Constants.getMonoisotopicMassOfAtom("D") + Constants.ELECTRON_MASS));
}
continue;
}
if (withAromaticRings || (numberToAddDeuteriums.size() != 0 && numberToAddDeuteriums.get(j) > numberDeuteriums)) {
int numberMissing = numberToAddDeuteriums.get(j) - numberDeuteriums;
numberDeuteriums += numberMissing;
numberDeuteriumsAromaticExchanged += numberMissing;
}
HDByteMolecularFormula formula = null;
try {
formula = new HDByteMolecularFormula(MolecularFormulaManipulator.getString(MolecularFormulaManipulator.getMolecularFormula(its)));
} catch (Exception e) {
System.err.println(identifiers.get(j));
e.printStackTrace();
System.exit(1);
}
formula.setNumberHydrogens((short) formula.getNumberHydrogens());
formula.setNumberDeuterium((short) numberDeuteriums);
// Identifier|InChI|MolecularFormula|MonoisotopicMass|InChIKey1|InChIKey2|OSN-Deuteriums|AromaticDeuteriums
if (numberToAddDeuteriums.size() == 0 || numberDeuteriums == numberToAddDeuteriums.get(j)) {
InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(its);
String inchi = gen.getInchi();
String inchiKey = gen.getInchiKey();
double mass = formula.getMonoisotopicMass();
double negMass = mass - Constants.getMonoisotopicMassOfAtom("D") + Constants.ELECTRON_MASS;
if (numberDeuteriumsEasilyExchanged + numberDeuteriumsAromaticExchanged == 0)
negMass = mass - Constants.getMonoisotopicMassOfAtom("H") + Constants.ELECTRON_MASS;
System.out.println(identifiers.get(j) + "|" + inchi + "|" + formula.toString() + "|" + mass + "|" + inchiKey.split("-")[0] + "|" + inchiKey.split("-")[1] + "|" + numberDeuteriumsEasilyExchanged + "|" + numberDeuteriumsAromaticExchanged + "|0" + "|" + (mass + Constants.getMonoisotopicMassOfAtom("D") - Constants.ELECTRON_MASS) + "|" + negMass);
if (bwriter != null) {
bwriter.write(identifiers.get(j) + "|" + inchi + "|" + formula.toString() + "|" + mass + "|" + inchiKey.split("-")[0] + "|" + inchiKey.split("-")[1] + "|" + numberDeuteriumsEasilyExchanged + "|" + numberDeuteriumsAromaticExchanged + "|0" + "|" + (mass + Constants.getMonoisotopicMassOfAtom("D") - Constants.ELECTRON_MASS) + "|" + negMass);
bwriter.newLine();
}
} else if (!combinatorial)
System.err.println("discarded to many easy exchangeable hydrogens " + numberDeuteriums + " " + identifiers.get(j));
/*
* String[] masses = getAllFormulas(formula);
*
*
* for(int i = 0; i < masses.length; i++) { System.out.print(masses[i] + " "); }
* System.out.println();
*/
}
if (bwriter != null)
bwriter.close();
}
use of org.openscience.cdk.inchi.InChIGenerator in project MetFragRelaunched by ipb-halle.
the class SDFDeuteriumGeneration method main.
public static void main(String[] args) throws Exception {
boolean withAromaticRings = false;
boolean combinatorial = true;
/*
* read input inchis
*/
ArrayList<String> inchis = new ArrayList<String>();
ArrayList<Integer> numberToAddDeuteriums = new ArrayList<Integer>();
ArrayList<String> identifiers = new ArrayList<String>();
BufferedReader breader = new BufferedReader(new FileReader(new File(args[0])));
String line = "";
int identifier = 1;
while ((line = breader.readLine()) != null) {
String[] tmp = line.trim().split("\\s+");
inchis.add(tmp[0].trim());
if (tmp.length >= 2)
numberToAddDeuteriums.add(Integer.parseInt(tmp[1].trim()));
if (tmp.length == 3)
identifiers.add(tmp[2].trim());
else
identifiers.add(identifier + "");
identifier++;
}
breader.close();
/*
* generate deuterated version of inchi
*/
IAtomContainerSet set = new AtomContainerSet();
SmilesGenerator sg = new SmilesGenerator(SmiFlavor.Generic);
CDKHydrogenAdder adder = CDKHydrogenAdder.getInstance(DefaultChemObjectBuilder.getInstance());
for (int j = 0; j < inchis.size(); j++) {
/*
* build the inchi atom container
*/
IAtomContainer its = InChIGeneratorFactory.getInstance().getInChIToStructure(inchis.get(j), SilentChemObjectBuilder.getInstance()).getAtomContainer();
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(its);
try {
adder.addImplicitHydrogens(its);
} catch (Exception e) {
System.out.println("missed " + inchis.get(j) + " " + identifiers.get(j));
continue;
}
AtomContainerManipulator.convertImplicitToExplicitHydrogens(its);
int numberDeuteriums = 0;
int numberDeuteriumsEasilyExchanged = 0;
int numberDeuteriumsAromaticExchanged = 0;
int[] toExchange = searchForDeuteriumExchangeablePositions(new String[] { "O", "N", "S" }, its);
if (!combinatorial || (numberToAddDeuteriums.size() == 0 || toExchange.length <= numberToAddDeuteriums.get(j))) {
for (int i = 0; i < toExchange.length; i++) {
int numberExchanged = setAllExplicitDeuteriums(its, i);
numberDeuteriums += numberExchanged;
numberDeuteriumsEasilyExchanged += numberExchanged;
}
} else if (toExchange.length > numberToAddDeuteriums.get(j)) {
ArrayList<IAtomContainer> deuteratedStrutures = new ArrayList<IAtomContainer>();
ArrayList<Integer> numberDeuteriumsVec = new ArrayList<Integer>();
ArrayList<Integer> numberDeuteriumsEasilyExchangedVec = new ArrayList<Integer>();
// get all possible combinations of exchanges with given number
// of exchangeable hydrogens
int[][] combs = getCombinations(toExchange, numberToAddDeuteriums.get(j));
for (int k = 0; k < combs.length; k++) {
numberDeuteriumsEasilyExchanged = 0;
numberDeuteriums = 0;
IAtomContainer itsNew = InChIGeneratorFactory.getInstance().getInChIToStructure(inchis.get(j), SilentChemObjectBuilder.getInstance()).getAtomContainer();
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(itsNew);
adder.addImplicitHydrogens(itsNew);
AtomContainerManipulator.convertImplicitToExplicitHydrogens(itsNew);
for (int l = 0; l < combs[k].length; l++) {
addExplicitDeuterium(itsNew, combs[k][l]);
numberDeuteriumsEasilyExchanged++;
numberDeuteriums++;
}
numberDeuteriumsVec.add(numberDeuteriums);
numberDeuteriumsEasilyExchangedVec.add(numberDeuteriumsEasilyExchanged);
deuteratedStrutures.add(itsNew);
}
for (int k = 0; k < deuteratedStrutures.size(); k++) {
String inchi = inchis.get(j);
IAtomContainer con = deuteratedStrutures.get(k);
HDByteMolecularFormula formula = null;
try {
formula = new HDByteMolecularFormula(inchi.split("/")[1]);
} catch (Exception e) {
System.err.println(identifiers.get(j));
e.printStackTrace();
System.exit(1);
}
formula.setNumberHydrogens((short) formula.getNumberHydrogens());
formula.setNumberDeuterium((short) (int) numberDeuteriumsVec.get(k));
InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(con);
String inchiKey = gen.getInchiKey();
con.setProperty(VariableNames.IDENTIFIER_NAME, identifiers.get(j) + "-" + (k + 1));
con.setProperty(VariableNames.INCHI_NAME, inchi);
// con.setProperty(VariableNames.SMILES_NAME, sg.create(deuteratedStrutures.get(k)));
con.setProperty(VariableNames.MOLECULAR_FORMULA_NAME, formula.toString());
con.setProperty(VariableNames.MONOISOTOPIC_MASS_NAME, formula.getMonoisotopicMass());
con.setProperty(VariableNames.INCHI_KEY_1_NAME, inchiKey.split("-")[0]);
con.setProperty(VariableNames.INCHI_KEY_2_NAME, inchiKey.split("-")[1]);
con.setProperty("OSN-Deuteriums", numberDeuteriumsEasilyExchangedVec.get(k));
con.setProperty("AromaticDeuteriums", 0);
con.setProperty("MissedDeuteriums", (toExchange.length - numberToAddDeuteriums.get(j)));
set.addAtomContainer(con);
System.out.println(identifiers.get(j) + "-" + (k + 1) + "|" + inchi + "|" + sg.create(deuteratedStrutures.get(k)) + "|" + formula.toString() + "|" + formula.getMonoisotopicMass() + "|" + inchiKey.split("-")[0] + "|" + inchiKey.split("-")[1] + "|" + numberDeuteriumsEasilyExchangedVec.get(k) + "|0|" + (toExchange.length - numberToAddDeuteriums.get(j)));
}
continue;
}
if (withAromaticRings || (numberToAddDeuteriums.size() != 0 && numberToAddDeuteriums.get(j) > numberDeuteriums)) {
int numberMissing = numberToAddDeuteriums.get(j) - numberDeuteriums;
numberDeuteriums += numberMissing;
numberDeuteriumsAromaticExchanged += numberMissing;
}
String inchi = inchis.get(j);
InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(its);
String inchiKey = gen.getInchiKey();
HDByteMolecularFormula formula = null;
try {
formula = new HDByteMolecularFormula(inchi.split("/")[1]);
} catch (Exception e) {
System.err.println(identifiers.get(j));
e.printStackTrace();
System.exit(1);
}
formula.setNumberHydrogens((short) formula.getNumberHydrogens());
formula.setNumberDeuterium((short) numberDeuteriums);
if (numberToAddDeuteriums.size() == 0 || numberDeuteriums == numberToAddDeuteriums.get(j)) {
System.out.println(identifiers.get(j) + "|" + inchi + "|" + sg.create(its) + "|" + formula.toString() + "|" + formula.getMonoisotopicMass() + "|" + inchiKey.split("-")[0] + "|" + inchiKey.split("-")[1] + "|" + numberDeuteriumsEasilyExchanged + "|" + numberDeuteriumsAromaticExchanged + "|0");
its.setProperty(VariableNames.IDENTIFIER_NAME, identifiers.get(j));
its.setProperty(VariableNames.INCHI_NAME, inchi);
// its.setProperty(VariableNames.SMILES_NAME, sg.create(its));
its.setProperty(VariableNames.MOLECULAR_FORMULA_NAME, formula.toString());
its.setProperty(VariableNames.MONOISOTOPIC_MASS_NAME, formula.getMonoisotopicMass());
its.setProperty(VariableNames.INCHI_KEY_1_NAME, inchiKey.split("-")[0]);
its.setProperty(VariableNames.INCHI_KEY_2_NAME, inchiKey.split("-")[1]);
its.setProperty("OSN-Deuteriums", numberDeuteriumsEasilyExchanged);
its.setProperty("AromaticDeuteriums", numberDeuteriumsAromaticExchanged);
its.setProperty("MissedDeuteriums", 0);
set.addAtomContainer(its);
} else if (!combinatorial)
System.err.println("discarded to many easy exchangeable hydrogens " + numberDeuteriums + " " + identifiers.get(j));
}
try {
SDFWriter writer = new SDFWriter(new java.io.FileWriter(new java.io.File(args[1])));
writer.write(set);
writer.close();
} catch (java.io.IOException e) {
e.printStackTrace();
} catch (CDKException e) {
e.printStackTrace();
}
}
use of org.openscience.cdk.inchi.InChIGenerator in project ambit-mirror by ideaconsult.
the class CompoundLookup method atomcontainer2inchi.
public String[] atomcontainer2inchi(IAtomContainer c, String origin) throws ResourceException {
try {
Kekulization.kekulize(c);
// inchi can't process aromatic bonds...
for (IBond bond : c.bonds()) bond.setFlag(CDKConstants.ISAROMATIC, false);
// now generate unique inchi, as SMILES are not
searchType = _searchtype.inchi;
InChIGeneratorFactory f = InChIGeneratorFactory.getInstance();
InChIGenerator gen = f.getInChIGenerator(c);
INCHI_RET status = gen.getReturnStatus();
if (INCHI_RET.OKAY.equals(status) || INCHI_RET.WARNING.equals(status))
return new String[] { gen.getInchi(), gen.getInchiKey() };
else
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, String.format("Error converting to InChI: %s %s %s %s", status, gen.getMessage(), gen.getLog(), origin));
} catch (ResourceException x) {
throw x;
} catch (InvalidSmilesException x) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x.getMessage(), x);
} catch (CDKException x) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x.getMessage(), x);
}
}
use of org.openscience.cdk.inchi.InChIGenerator in project ambit-mirror by ideaconsult.
the class StructureNormalizer method process.
public IStructureRecord process(IStructureRecord structure) throws AmbitException {
if ((structure.getFormat() != null) && MOL_TYPE.NANO.name().equals(structure.getFormat())) {
try {
// fields
return structure;
/*
* Class clazz =
* FileInputState.class.getClassLoader().loadClass(
* "net.idea.ambit2.nano.NanoStructureNormalizer"); Method
* method = clazz.getMethod("normalizeNano",
* IStructureRecord.class); return (IStructureRecord)
* method.invoke(null, structure);
*/
} catch (Exception x) {
if (x instanceof AmbitException)
throw (AmbitException) x;
else
throw new AmbitException(x);
}
}
// else process as chemical
IAtomContainer molecule = molReader.process(structure);
if ((molecule != null) && (molecule.getProperties() != null))
structure.addRecordProperties(molecule.getProperties());
if ((molecule == null) || (molecule.getAtomCount() == 0))
structure.setType(STRUC_TYPE.NA);
else {
structure.setType(strucType.process(molecule));
if (build2D)
try {
IAtomContainer newmol = build2d(molecule, structure.getType());
if (newmol != null) {
molecule = newmol;
StringWriter w = new StringWriter();
SDFWriter writer = new SDFWriter(w);
try {
writer.write(molecule);
} catch (Exception x) {
logger.log(Level.WARNING, x.getMessage());
} finally {
writer.close();
}
structure.setType(strucType.process(molecule));
structure.setContent(w.toString());
structure.setFormat(MOL_TYPE.SDF.name());
}
} catch (Exception x) {
}
}
try {
if ((molecule != null) && (molecule.getAtomCount() > 0)) {
structure.setSmiles(smilesKey.process(molecule));
if ("".equals(structure.getSmiles()))
structure.setSmiles(null);
}
} catch (Exception x) {
logger.log(Level.SEVERE, x.getMessage());
structure.setSmiles(null);
}
try {
if (structure.getInchi() == null) {
String inchi = null;
String key = null;
if ((molecule == null) || (molecule.getAtomCount() == 0)) {
inchi = inchiKey.process(structure);
} else
try {
if (inchiProcessor == null)
inchiProcessor = new InchiProcessor();
boolean kekulize = false;
for (IBond bond : molecule.bonds()) if (bond.getFlag(CDKConstants.ISAROMATIC)) {
kekulize = true;
break;
}
IAtomContainer kekulized = (IAtomContainer) molecule;
if (kekulize)
try {
// inchi can't process aromatic bonds...
kekulized = (IAtomContainer) molecule.clone();
// the new kekulizer probably do not needs atom typing
// AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(kekulized);
Kekulization.kekulize(kekulized);
for (IBond bond : kekulized.bonds()) bond.setFlag(CDKConstants.ISAROMATIC, false);
} catch (Exception x) {
logger.log(Level.FINE, x.getMessage(), x);
;
}
InChIGenerator gen = inchiProcessor.process(kekulized);
if (INCHI_RET.OKAY.equals(gen.getReturnStatus()) || INCHI_RET.WARNING.equals(gen.getReturnStatus())) {
inchi = gen.getInchi();
key = gen.getInchiKey();
} else {
inchi = null;
key = String.format("ERROR:%s:%s", gen.getReturnStatus(), gen.getMessage());
}
} catch (Exception x) {
inchi = null;
key = null;
}
if ("".equals(inchi))
inchi = null;
structure.setInchi(inchi);
structure.setInchiKey(key);
}
} catch (Exception x) {
structure.setInchi(null);
structure.setInchiKey("ERROR:" + x.getMessage());
}
if ((molecule != null) && (molecule.getAtomCount() > 0))
try {
IMolecularFormula formula = MolecularFormulaManipulator.getMolecularFormula(molecule);
structure.setFormula(formula == null ? null : MolecularFormulaManipulator.getString(formula));
} catch (Exception x) {
structure.setFormula(null);
}
if (DXParser.hasDxProperties(structure)) {
if (dxParser == null)
dxParser = new DXParser();
structure = dxParser.process(structure);
}
return structure;
}
use of org.openscience.cdk.inchi.InChIGenerator in project ambit-mirror by ideaconsult.
the class InchiProcessorTest method testProcessExplicitH.
@Test
public void testProcessExplicitH() throws Exception {
List<INCHI_OPTION> options = new ArrayList<INCHI_OPTION>();
options.add(INCHI_OPTION.FixedH);
options.add(INCHI_OPTION.SAbs);
options.add(INCHI_OPTION.SAsXYZ);
options.add(INCHI_OPTION.SPXYZ);
options.add(INCHI_OPTION.FixSp3Bug);
options.add(INCHI_OPTION.AuxNone);
SmilesParser p = new SmilesParser(SilentChemObjectBuilder.getInstance());
IAtomContainer mol = p.parseSmiles("CN1C=NC2=C1C(=O)N(C(=O)N2C)C");
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
CDKHydrogenAdder.getInstance(mol.getBuilder()).addImplicitHydrogens(mol);
IAtomContainer molH = mol.clone();
MoleculeTools.convertImplicitToExplicitHydrogens(molH);
InChIGeneratorFactory factory = InChIGeneratorFactory.getInstance();
InChIGenerator gen = factory.getInChIGenerator(mol, options);
Assert.assertEquals(INCHI_RET.OKAY, gen.getReturnStatus());
InChIGenerator genH = factory.getInChIGenerator(mol, options);
Assert.assertEquals(INCHI_RET.OKAY, genH.getReturnStatus());
Assert.assertEquals(gen.getInchi(), genH.getInchi());
}
Aggregations