/
InChIToStructure.java
322 lines (285 loc) · 12.3 KB
/
InChIToStructure.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
/* $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.List;
import java.util.Map;
import net.sf.jniinchi.INCHI_BOND_STEREO;
import net.sf.jniinchi.INCHI_BOND_TYPE;
import net.sf.jniinchi.INCHI_PARITY;
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.JniInchiInputInchi;
import net.sf.jniinchi.JniInchiOutputStructure;
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.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.interfaces.IChemObjectBuilder;
import org.openscience.cdk.tools.periodictable.PeriodicTable;
/**
* <p>This class generates a CDK IAtomContainer from an InChI string. It places
* calls to a JNI wrapper for the InChI C++ library.
*
* <p>The generated IAtomContainer will have all 2D and 3D coordinates set to 0.0,
* but may have atom parities set. Double bond and allene stereochemistry are
* not currently recorded.
*
* <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 InChIToStructure</code><br>
* <code>InChIToStructure intostruct = factory.getInChIToStructure(</code><br>
* <code> inchi, DefaultChemObjectBuilder.getInstance()</code><br>
* <code>);</code><br>
* <code></code><br>
* <code>INCHI_RET ret = intostruct.getReturnStatus();</code><br>
* <code>if (ret == INCHI_RET.WARNING) {</code><br>
* <code> // Structure generated, but with warning message</code><br>
* <code> System.out.println("InChI warning: " + intostruct.getMessage());</code><br>
* <code>} else if (ret != INCHI_RET.OKAY) {</code><br>
* <code> // Structure generation failed</code><br>
* <code> throw new CDKException("Structure generation failed failed: " + ret.toString()</code><br>
* <code> + " [" + intostruct.getMessage() + "]");</code><br>
* <code>}</code><br>
* <code></code><br>
* <code>IAtomContainer container = intostruct.getAtomContainer();</code><br>
* <p><tt><b>
*
* @author Sam Adams
*
* @cdk.module inchi
* @cdk.githash
*/
@TestClass("org.openscience.cdk.inchi.InChIToStructureTest")
public class InChIToStructure {
protected JniInchiInputInchi input;
protected JniInchiOutputStructure output;
protected IAtomContainer molecule;
/**
* Constructor. Generates CDK AtomContainer from InChI.
* @param inchi
* @throws CDKException
*/
@TestMethod("testConstructor_String_IChemObjectBuilder")
protected InChIToStructure(String inchi, IChemObjectBuilder builder) throws CDKException {
try {
input = new JniInchiInputInchi(inchi, "");
} catch (JniInchiException jie) {
throw new CDKException("Failed to convert InChI to molecule: " + jie.getMessage(), jie);
}
generateAtomContainerFromInchi(builder);
}
/**
* Constructor. Generates CMLMolecule from InChI.
* @param inchi
* @param options
* @throws CDKException
*/
protected InChIToStructure(String inchi, IChemObjectBuilder builder, String options) throws CDKException {
try {
input = new JniInchiInputInchi(inchi, options);
} catch (JniInchiException jie) {
throw new CDKException("Failed to convert InChI to molecule: " + jie.getMessage(), jie);
}
generateAtomContainerFromInchi(builder);
}
/**
* Constructor. Generates CMLMolecule from InChI.
* @param inchi
* @param options
* @throws CDKException
*/
protected InChIToStructure(String inchi, IChemObjectBuilder builder, List<String> options) throws CDKException {
try {
input = new JniInchiInputInchi(inchi, options);
} catch (JniInchiException jie) {
throw new CDKException("Failed to convert InChI to molecule: " + jie.getMessage());
}
generateAtomContainerFromInchi(builder);
}
/**
* Gets structure from InChI, and converts InChI library data structure
* into an IAtomContainer.
*
* @throws CDKException
*/
@TestMethod("testGetAtomContainer_IChemObjectBuilder")
protected void generateAtomContainerFromInchi(IChemObjectBuilder builder) throws CDKException {
try {
output = JniInchiWrapper.getStructureFromInchi(input);
} catch (JniInchiException jie) {
throw new CDKException("Failed to convert InChI to molecule: " + jie.getMessage(), jie);
}
//molecule = new AtomContainer();
molecule = builder.newInstance(IAtomContainer.class);
Map<JniInchiAtom, IAtom> inchiCdkAtomMap = new HashMap<JniInchiAtom, IAtom>();
for (int i = 0; i < output.getNumAtoms(); i ++) {
JniInchiAtom iAt = output.getAtom(i);
IAtom cAt = builder.newInstance(IAtom.class);
inchiCdkAtomMap.put(iAt, cAt);
cAt.setID("a" + i);
cAt.setSymbol(iAt.getElementType());
cAt.setAtomicNumber(PeriodicTable.getAtomicNumber(cAt.getSymbol()));
// Ignore coordinates - all zero
int charge = iAt.getCharge();
if (charge != 0) {
cAt.setFormalCharge(charge);
}
// hydrogenCount contains number of implict hydrogens, not
// total number
// Ref: Posting to cdk-devel list by Egon Willighagen 2005-09-17
int numH = iAt.getImplicitH();
if (numH != 0) {
cAt.setImplicitHydrogenCount(numH);
}
molecule.addAtom(cAt);
}
for (int i = 0; i < output.getNumBonds(); i ++) {
JniInchiBond iBo = output.getBond(i);
IBond cBo = builder.newInstance(IBond.class);
IAtom atO = inchiCdkAtomMap.get(iBo.getOriginAtom());
IAtom atT = inchiCdkAtomMap.get(iBo.getTargetAtom());
IAtom[] atoms = new IAtom[2];
atoms[0] = atO;
atoms[1] = atT;
cBo.setAtoms(atoms);
INCHI_BOND_TYPE type = iBo.getBondType();
if (type == INCHI_BOND_TYPE.SINGLE) {
cBo.setOrder(CDKConstants.BONDORDER_SINGLE);
} else if (type == INCHI_BOND_TYPE.DOUBLE) {
cBo.setOrder(CDKConstants.BONDORDER_DOUBLE);
} else if (type == INCHI_BOND_TYPE.TRIPLE) {
cBo.setOrder(CDKConstants.BONDORDER_TRIPLE);
} else if (type == INCHI_BOND_TYPE.ALTERN) {
cBo.setFlag(CDKConstants.ISAROMATIC, true);
} else {
throw new CDKException("Unknown bond type: " + type);
}
INCHI_BOND_STEREO stereo = iBo.getBondStereo();
// No stereo definition
if (stereo == INCHI_BOND_STEREO.NONE) {
cBo.setStereo(IBond.Stereo.NONE);
}
// Bond ending (fat end of wedge) below the plane
else if (stereo == INCHI_BOND_STEREO.SINGLE_1DOWN) {
cBo.setStereo(IBond.Stereo.DOWN);
}
// Bond ending (fat end of wedge) above the plane
else if (stereo == INCHI_BOND_STEREO.SINGLE_1UP) {
cBo.setStereo(IBond.Stereo.UP);
}
// Bond starting (pointy end of wedge) below the plane
else if (stereo == INCHI_BOND_STEREO.SINGLE_2DOWN) {
cBo.setStereo(IBond.Stereo.DOWN_INVERTED);
}
// Bond starting (pointy end of wedge) above the plane
else if (stereo == INCHI_BOND_STEREO.SINGLE_2UP) {
cBo.setStereo(IBond.Stereo.UP_INVERTED);
}
// Bond with undefined stereochemistry
else if (stereo == INCHI_BOND_STEREO.SINGLE_1EITHER
|| stereo == INCHI_BOND_STEREO.DOUBLE_EITHER) {
cBo.setStereo((IBond.Stereo)CDKConstants.UNSET);
}
molecule.addBond(cBo);
}
for (int i = 0; i < output.getNumStereo0D(); i ++) {
JniInchiStereo0D stereo0d = output.getStereo0D(i);
if (stereo0d.getStereoType() == INCHI_STEREOTYPE.TETRAHEDRAL) {
JniInchiAtom central = stereo0d.getCentralAtom();
JniInchiAtom[] neighbours = stereo0d.getNeighbors();
IAtom atC = (IAtom) inchiCdkAtomMap.get(central);
IAtom at0 = (IAtom) inchiCdkAtomMap.get(neighbours[0]);
IAtom at1 = (IAtom) inchiCdkAtomMap.get(neighbours[1]);
IAtom at2 = (IAtom) inchiCdkAtomMap.get(neighbours[2]);
IAtom at3 = (IAtom) inchiCdkAtomMap.get(neighbours[3]);
int sign;
if (stereo0d.getParity() == INCHI_PARITY.ODD) {
sign = -1;
} else if (stereo0d.getParity() == INCHI_PARITY.EVEN) {
sign = +1;
} else {
// CDK Only supports parities of + or -
continue;
}
IAtomParity parity = builder.newInstance(IAtomParity.class,atC, at0, at1, at2, at3, sign);
molecule.addStereoElement(parity);
} else {
// TODO - other types of atom parity - double bond, etc
}
}
}
/**
* Returns generated molecule.
* @return An AtomContainer object
*/
@TestMethod("testGetAtomContainer")
public IAtomContainer getAtomContainer() {
return(molecule);
}
/**
* Gets return status from InChI process. OKAY and WARNING indicate
* InChI has been generated, in all other cases InChI generation
* has failed.
*/
@TestMethod("testGetReturnStatus_EOF")
public INCHI_RET getReturnStatus() {
return(output.getReturnStatus());
}
/**
* Gets generated (error/warning) messages.
*/
@TestMethod("testGetMessage")
public String getMessage() {
return(output.getMessage());
}
/**
* Gets generated log.
*/
@TestMethod("testGetLog")
public String getLog() {
return(output.getLog());
}
/**
* <p>Returns warning flags, see INCHIDIFF in inchicmp.h.
*
* <p>[x][y]:
* <br>x=0 => Reconnected if present in InChI otherwise Disconnected/Normal
* <br>x=1 => Disconnected layer if Reconnected layer is present
* <br>y=1 => Main layer or Mobile-H
* <br>y=0 => Fixed-H layer
*/
@TestMethod("testGetWarningFlags")
public long[][] getWarningFlags() {
return(output.getWarningFlags());
}
}