/
InChIGenerator.java
435 lines (392 loc) · 16.7 KB
/
InChIGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* $Revision$ $Author$ $Date$
*
* Copyright (C) 2006-2007 Sam Adams <sea36@users.sf.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.openscience.cdk.inchi;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.vecmath.Point2d;
import javax.vecmath.Point3d;
import net.sf.jniinchi.INCHI_BOND_STEREO;
import net.sf.jniinchi.INCHI_BOND_TYPE;
import net.sf.jniinchi.INCHI_KEY;
import net.sf.jniinchi.INCHI_PARITY;
import net.sf.jniinchi.INCHI_RADICAL;
import net.sf.jniinchi.INCHI_RET;
import net.sf.jniinchi.INCHI_STEREOTYPE;
import net.sf.jniinchi.JniInchiAtom;
import net.sf.jniinchi.JniInchiBond;
import net.sf.jniinchi.JniInchiException;
import net.sf.jniinchi.JniInchiInput;
import net.sf.jniinchi.JniInchiOutput;
import net.sf.jniinchi.JniInchiOutputKey;
import net.sf.jniinchi.JniInchiStereo0D;
import net.sf.jniinchi.JniInchiWrapper;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.config.IsotopeFactory;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IAtomParity;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
/**
* <p>This class generates the IUPAC International Chemical Identifier (InChI) for
* a CDK IAtomContainer. It places calls to a JNI wrapper for the InChI C++ library.
*
* <p>If the atom container has 3D coordinates for all of its atoms then they
* will be used, otherwise 2D coordinates will be used if available.
*
* <p><i>Spin multiplicities and some aspects of stereochemistry are not
* currently handled completely.</i>
*
* <h3>Example usage</h3>
*
* <code>// Generate factory - throws CDKException if native code does not load</code><br>
* <code>InChIGeneratorFactory factory = new InChIGeneratorFactory();</code><br>
* <code>// Get InChIGenerator</code><br>
* <code>InChIGenerator gen = factory.getInChIGenerator(container);</code><br>
* <code></code><br>
* <code>INCHI_RET ret = gen.getReturnStatus();</code><br>
* <code>if (ret == INCHI_RET.WARNING) {</code><br>
* <code> // InChI generated, but with warning message</code><br>
* <code> System.out.println("InChI warning: " + gen.getMessage());</code><br>
* <code>} else if (ret != INCHI_RET.OKAY) {</code><br>
* <code> // InChI generation failed</code><br>
* <code> throw new CDKException("InChI failed: " + ret.toString()</code><br>
* <code> + " [" + gen.getMessage() + "]");</code><br>
* <code>}</code><br>
* <code></code><br>
* <code>String inchi = gen.getInchi();</code><br>
* <code>String auxinfo = gen.getAuxInfo();</code><br>
* <p><tt><b>
* TODO: distinguish between singlet and undefined spin multiplicity<br/>
* TODO: double bond and allene parities<br/>
* TODO: problem recognising bond stereochemistry<br/>
* </b></tt>
*
* @author Sam Adams
*
* @cdk.module inchi
* @cdk.githash
*/
@TestClass("org.openscience.cdk.inchi.InChIGeneratorTest")
public class InChIGenerator {
protected JniInchiInput input;
protected JniInchiOutput output;
/**
* AtomContainer instance refers to.
*/
protected IAtomContainer atomContainer;
/**
* <p>Constructor. Generates InChI from CDK AtomContainer.
*
* <p>Reads atoms, bonds etc from atom container and converts to format
* InChI library requires, then calls the library.
*
* @param atomContainer AtomContainer to generate InChI for.
* @throws org.openscience.cdk.exception.CDKException if there is an
* error during InChI generation
*/
@TestMethod("testGetInchiFromChlorineAtom,testGetInchiFromLithiumIontestGetInchiFromChlorine37Atom")
protected InChIGenerator(IAtomContainer atomContainer) throws CDKException {
try {
input = new JniInchiInput("");
generateInchiFromCDKAtomContainer(atomContainer);
} catch (JniInchiException jie) {
throw new CDKException("InChI generation failed: " + jie.getMessage(), jie);
}
}
/**
* <p>Constructor. Generates InChI from CDK AtomContainer.
*
* <p>Reads atoms, bonds etc from atom container and converts to format
* InChI library requires, then calls the library.
*
* @param atomContainer AtomContainer to generate InChI for.
* @param options Space delimited string of options to pass to InChI library.
* Each option may optionally be preceded by a command line
* switch (/ or -).
* @throws CDKException
*/
protected InChIGenerator(IAtomContainer atomContainer, String options) throws CDKException {
try {
input = new JniInchiInput(options);
generateInchiFromCDKAtomContainer(atomContainer);
} catch (JniInchiException jie) {
throw new CDKException("InChI generation failed: " + jie.getMessage(), jie);
}
}
/**
* <p>Constructor. Generates InChI from CDK AtomContainer.
*
* <p>Reads atoms, bonds etc from atom container and converts to format
* InChI library requires, then calls the library.
*
* @param atomContainer AtomContainer to generate InChI for.
* @param options List of INCHI_OPTION.
* @throws CDKException
*/
protected InChIGenerator(IAtomContainer atomContainer, List options) throws CDKException {
try {
input = new JniInchiInput(options);
generateInchiFromCDKAtomContainer(atomContainer);
} catch (JniInchiException jie) {
throw new CDKException("InChI generation failed: " + jie.getMessage(), jie);
}
}
/**
* <p>Reads atoms, bonds etc from atom container and converts to format
* InChI library requires, then places call for the library to generate
* the InChI.
*
* @param atomContainer AtomContainer to generate InChI for.
* @throws CDKException
*/
private void generateInchiFromCDKAtomContainer(IAtomContainer atomContainer) throws CDKException {
this.atomContainer = atomContainer;
Iterator<IAtom> atoms = atomContainer.atoms().iterator();
// Check for 3d coordinates
boolean all3d = true;
boolean all2d = true;
while (atoms.hasNext()) {
IAtom atom = (IAtom)atoms.next();
if (atom.getPoint3d() == null) {
all3d = false;
}
if (atom.getPoint2d() == null) {
all2d = false;
}
}
// Process atoms
IsotopeFactory ifact = null;
try {
ifact = IsotopeFactory.getInstance(atomContainer.getBuilder());
} catch (Exception e) {
// Do nothing
}
Map<IAtom, JniInchiAtom> atomMap = new HashMap<IAtom, JniInchiAtom>();
atoms = atomContainer.atoms().iterator();
while (atoms.hasNext()) {
IAtom atom = atoms.next();
// Get coordinates
// Use 3d if possible, otherwise 2d or none
double x, y, z;
if (all3d) {
Point3d p = atom.getPoint3d();
x = p.x;
y = p.y;
z = p.z;
} else if (all2d) {
Point2d p = atom.getPoint2d();
x = p.x;
y = p.y;
z = 0.0;
} else {
x = 0.0;
y = 0.0;
z = 0.0;
}
// Chemical element symbol
String el = atom.getSymbol();
// Generate InChI atom
JniInchiAtom iatom = input.addAtom(new JniInchiAtom(x, y, z, el));
atomMap.put(atom, iatom);
// Check if charged
int charge = atom.getFormalCharge();
if (charge != 0) {
iatom.setCharge(charge);
}
// Check whether isotopic
Integer isotopeNumber = atom.getMassNumber();
if (isotopeNumber != CDKConstants.UNSET && ifact != null) {
IAtom isotope = atomContainer.getBuilder().newInstance(IAtom.class,el);
ifact.configure(isotope);
if (isotope.getMassNumber().intValue() == isotopeNumber.intValue()) {
isotopeNumber = 0;
}
}
if (isotopeNumber != CDKConstants.UNSET) {
iatom.setIsotopicMass(isotopeNumber);
}
// Check for implicit hydrogens
// atom.getHydrogenCount() returns number of implict hydrogens, not
// total number
// Ref: Posting to cdk-devel list by Egon Willighagen 2005-09-17
Integer implicitH = atom.getHydrogenCount();
if (implicitH == CDKConstants.UNSET) implicitH = 0;
if (implicitH != 0) {
iatom.setImplicitH(implicitH);
}
// Check if radical
int count = atomContainer.getConnectedSingleElectronsCount(atom);
if (count == 0) {
// TODO - how to check whether singlet or undefined multiplicity
} else if (count == 1) {
iatom.setRadical(INCHI_RADICAL.DOUBLET);
} else if (count == 2) {
iatom.setRadical(INCHI_RADICAL.TRIPLET);
} else {
throw new CDKException("Unrecognised radical type");
}
}
// Process bonds
Iterator<IBond> bonds = atomContainer.bonds().iterator();
while (bonds.hasNext()) {
IBond bond = bonds.next();
// Assumes 2 centre bond
JniInchiAtom at0 = (JniInchiAtom) atomMap.get(bond.getAtom(0));
JniInchiAtom at1 = (JniInchiAtom) atomMap.get(bond.getAtom(1));
// Get bond order
INCHI_BOND_TYPE order;
IBond.Order bo = bond.getOrder();
if (bond.getFlag(CDKConstants.ISAROMATIC)) {
order = INCHI_BOND_TYPE.ALTERN;
} else if (bo == CDKConstants.BONDORDER_SINGLE) {
order = INCHI_BOND_TYPE.SINGLE;
} else if (bo == CDKConstants.BONDORDER_DOUBLE) {
order = INCHI_BOND_TYPE.DOUBLE;
} else if (bo == CDKConstants.BONDORDER_TRIPLE) {
order = INCHI_BOND_TYPE.TRIPLE;
} else {
throw new CDKException("Failed to generate InChI: Unsupported bond type");
}
// Create InChI bond
JniInchiBond ibond = new JniInchiBond(at0, at1, order);
input.addBond(ibond);
// Check for bond stereo definitions
IBond.Stereo stereo = bond.getStereo();
// No stereo definition
if (stereo == IBond.Stereo.NONE) {
ibond.setStereoDefinition(INCHI_BOND_STEREO.NONE);
}
// Bond ending (fat end of wedge) below the plane
else if (stereo == IBond.Stereo.DOWN) {
ibond.setStereoDefinition(INCHI_BOND_STEREO.SINGLE_1DOWN);
}
// Bond ending (fat end of wedge) above the plane
else if (stereo == IBond.Stereo.UP) {
ibond.setStereoDefinition(INCHI_BOND_STEREO.SINGLE_1UP);
}
// Bond starting (pointy end of wedge) below the plane
else if (stereo == IBond.Stereo.DOWN_INVERTED) {
ibond.setStereoDefinition(INCHI_BOND_STEREO.SINGLE_2DOWN);
}
// Bond starting (pointy end of wedge) above the plane
else if (stereo == IBond.Stereo.UP_INVERTED) {
ibond.setStereoDefinition(INCHI_BOND_STEREO.SINGLE_2UP);
}
// Bond with undefined stereochemistry
else if (stereo == CDKConstants.UNSET) {
if (order == INCHI_BOND_TYPE.SINGLE) {
ibond.setStereoDefinition(INCHI_BOND_STEREO.SINGLE_1EITHER);
} else if (order == INCHI_BOND_TYPE.DOUBLE) {
ibond.setStereoDefinition(INCHI_BOND_STEREO.DOUBLE_EITHER);
}
}
}
// Process atom parities (tetrahedral InChI Stereo0D Parities)
atoms = atomContainer.atoms().iterator();
while (atoms.hasNext()) {
IAtom atom = atoms.next();
IAtomParity parity = AtomContainerManipulator.getAtomParity(atomContainer, atom);
if (parity != null) {
IAtom[] surroundingAtoms = parity.getSurroundingAtoms();
int sign = parity.getParity();
JniInchiAtom atC = (JniInchiAtom) atomMap.get(atom);
JniInchiAtom at0 = (JniInchiAtom) atomMap.get(surroundingAtoms[0]);
JniInchiAtom at1 = (JniInchiAtom) atomMap.get(surroundingAtoms[1]);
JniInchiAtom at2 = (JniInchiAtom) atomMap.get(surroundingAtoms[2]);
JniInchiAtom at3 = (JniInchiAtom) atomMap.get(surroundingAtoms[3]);
INCHI_PARITY p = INCHI_PARITY.UNKNOWN;
if (sign > 0) {
p = INCHI_PARITY.EVEN;
} else if (sign < 0) {
p = INCHI_PARITY.ODD;
} else {
throw new CDKException("Atom parity of zero");
}
input.addStereo0D(new JniInchiStereo0D(atC, at0, at1, at2, at3,
INCHI_STEREOTYPE.TETRAHEDRAL, p));
}
}
try {
output = JniInchiWrapper.getInchi(input);
} catch (JniInchiException jie) {
throw new CDKException("Failed to generate InChI: " + jie.getMessage(), jie);
}
}
/**
* Gets return status from InChI process. OKAY and WARNING indicate
* InChI has been generated, in all other cases InChI generation
* has failed.
*/
@TestMethod("testGetInchiFromLandDAlanine3D,testGetInchiEandZ12Dichloroethene2D")
public INCHI_RET getReturnStatus() {
return(output.getReturnStatus());
}
/**
* Gets generated InChI string.
*/
@TestMethod("testGetInchiEandZ12Dichloroethene2D,testGetInchiFromEthyne,testGetInchiFromEthene")
public String getInchi() {
return(output.getInchi());
}
/**
* Gets generated InChIKey string.
*/
@TestMethod("testGetInchiFromEthane")
public String getInchiKey() throws CDKException {
JniInchiOutputKey key;
try {
key = JniInchiWrapper.getInChIKey(output.getInchi());
if (key.getReturnStatus() == INCHI_KEY.OK) {
return key.getKey();
} else {
throw new CDKException("Error while creating InChIKey: " +
key.getReturnStatus());
}
} catch (JniInchiException exception) {
throw new CDKException("Error while creating InChIKey: " +
exception.getMessage(), exception);
}
}
/**
* Gets auxillary information.
*/
public String getAuxInfo() {
return(output.getAuxInfo());
}
/**
* Gets generated (error/warning) messages.
*/
public String getMessage() {
return(output.getMessage());
}
/**
* Gets generated log.
*/
public String getLog() {
return(output.getLog());
}
}