Debian Codegraph @

jna-inchi-smiles/src/main/java/io/github/dan2097/jnainchi/SmilesToInchi.java

raw
1 /**
2 * JNA-InChI - Library for calling InChI from Java
3 * Copyright © 2018 Daniel Lowe
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 package io.github.dan2097.jnainchi;
19
20 import java.io.IOException;
21 import java.util.Arrays;
22 import java.util.List;
23
24 import uk.ac.ebi.beam.Atom;
25 import uk.ac.ebi.beam.Bond;
26 import uk.ac.ebi.beam.Configuration;
27 import uk.ac.ebi.beam.Edge;
28 import uk.ac.ebi.beam.Graph;
29
30 public class SmilesToInchi {
31
32 /**
33 * Convert a SMILES string to InChI using the default options
34 * (hence StdInChI will be the output)
35 * Throws an IOException if there is an issue with the SMILES string
36 * or IllegalArgumentException if given null input
37 * @param smiles
38 * @return
39 * @throws IOException
40 */
41 public static InchiOutput toInchi(String smiles) throws IOException {
42 return toInchi(smiles, InchiOptions.DEFAULT_OPTIONS);
43 }
44
45 /**
46 * Convert a SMILES string to InChI using the given options
47 * Throws an IOException if there is an issue with the SMILES string
48 * or IllegalArgumentException if given null input
49 * @param smiles
50 * @param options
51 * @return
52 * @throws IOException
53 */
54 public static InchiOutput toInchi(String smiles, InchiOptions options) throws IOException {
55 if (smiles == null) {
56 throw new IllegalArgumentException("SMILES should not be null");
57 }
58 if (options == null) {
59 throw new IllegalArgumentException("options must not be null");
60 }
61 Graph g = Graph.fromSmiles(smiles);
62 InchiInput input = graphToInput(g);
63 return JnaInchi.toInchi(input, options);
64 }
65
66 private static InchiInput graphToInput(Graph g) throws IOException {
67 g = g.kekule();
68 InchiInput input = new InchiInput();
69 for (int i = 0, len = g.order(); i < len; i++) {
70 Atom smiAtom = g.atom(i);
71 String elementSymbol = smiAtom.element().symbol();
72 if ("*".equals(elementSymbol)) {
73 elementSymbol = "Zz";
74 }
75 InchiAtom a = new InchiAtom(elementSymbol);
76 a.setCharge(smiAtom.charge());
77 a.setImplicitHydrogen(g.implHCount(i));
78 if (smiAtom.isotope() != -1) {
79 a.setIsotopicMass(smiAtom.isotope());
80 }
81 input.addAtom(a);
82 }
83 for (int i = 0, len = g.order(); i < len; i++) {
84 InchiAtom a = input.getAtom(i);
85 Configuration stereoConfig = g.configurationOf(i);
86 switch (stereoConfig.type()) {
87 case Tetrahedral: {
88 int[] neighbours = g.neighbors(i);
89 int implicitHydrogen = g.implHCount(i);
90 InchiAtom[] atoms = new InchiAtom[4];
91 if (neighbours.length == 3 && implicitHydrogen <= 1) {
92 // implicit hydrogen (implicitHydrogen = 1) or lone pair (implicitHydrogen = 0)
93 neighbours = Arrays.copyOf(neighbours, 4);
94 neighbours[3] = i;
95 Arrays.sort(neighbours);//In SMILES the implicit hydrogen/lone pair occurs at the same position as the central atom
96 for (int j = 0; j < 4; j++) {
97 int idx = neighbours[j];
98 atoms[j] = idx == i && implicitHydrogen == 1 ? InchiStereo.STEREO_IMPLICIT_H : input.getAtom(idx);
99 }
100 }
101 else if (neighbours.length == 4 && implicitHydrogen == 0) {
102 for (int j = 0; j < 4; j++) {
103 atoms[j] = input.getAtom(neighbours[j]);
104 }
105 }
106 else {
107 // is this actually tetrahedral???
108 continue;
109 }
110
111 InchiStereoParity parity = stereoConfig == Configuration.TH1 ? InchiStereoParity.ODD : InchiStereoParity.EVEN;
112 input.addStereo(InchiStereo.createTetrahedralStereo(a, atoms[0], atoms[1], atoms[2], atoms[3], parity));
113 }
114 break;
115 case ExtendedTetrahedral:
116 addAllenalStereo(input, g, i);
117 break;
118 case DoubleBond:
119 case Octahedral:
120 case SquarePlanar:
121 case TrigonalBipyramidal:
122 case Implicit:
123 case None:
124 break;
125 default:
126 break;
127 }
128 }
129
130 for (Edge smiBond : g.edges()) {
131 int start = smiBond.either();
132 int end = smiBond.other(start);
133 InchiBondType type;
134 switch (smiBond.bond()) {
135 case AROMATIC:
136 //Shoudn't occur as molecule has been kekulized
137 type = InchiBondType.ALTERN;
138 break;
139 case DOT:
140 type = InchiBondType.NONE;
141 break;
142 case DOUBLE:
143 case DOUBLE_AROMATIC:
144 type = InchiBondType.DOUBLE;
145 break;
146 case DOWN:
147 case UP:
148 case IMPLICIT:
149 case IMPLICIT_AROMATIC:
150 case SINGLE:
151 type = InchiBondType.SINGLE;
152 break;
153 case TRIPLE:
154 case QUADRUPLE:
155 type = InchiBondType.TRIPLE;
156 default:
157 type = InchiBondType.SINGLE;
158 break;
159 }
160 InchiBond b = new InchiBond(input.getAtom(start), input.getAtom(end), type);
161 input.addBond(b);
162
163 if (smiBond.bond().order() == 2) {
164 // find double bond stereochemistry
165 if (!findDoublebondStereo(g, input, start, end)) {
166 findCumuleneStereo(g, input, start, end);
167 }
168 }
169 }
170 return input;
171 }
172
173 private static boolean findDoublebondStereo(Graph g, InchiInput input, int start, int end) {
174 Edge dirEdge1 = findDirectionalEdge(g, start);
175 if (dirEdge1 == null) {
176 return false;
177 }
178
179 Edge dirEdge2 = findDirectionalEdge(g, end);
180 if (dirEdge2 == null) {
181 return false;
182 }
183 InchiStereoParity parity = (dirEdge1.bond(start) == dirEdge2.bond(end)) ? InchiStereoParity.ODD
184 : InchiStereoParity.EVEN;
185
186 InchiAtom atom1 = input.getAtom(dirEdge1.other(start));
187 InchiAtom atom2 = input.getAtom(start);
188 InchiAtom atom3 = input.getAtom(end);
189 InchiAtom atom4 = input.getAtom(dirEdge2.other(end));
190 input.addStereo(InchiStereo.createDoubleBondStereo(atom1, atom2, atom3, atom4, parity));
191 return true;
192 }
193
194 private static boolean findCumuleneStereo(Graph g, InchiInput input, int start, int end) {
195 int nextDb1 = nextDb(g, start, end);
196 if (nextDb1 < 0) {
197 return false;
198 }
199
200 int nextDb2 = nextDb(g, end, start);
201 if (nextDb2 < 0) {
202 return false;
203 }
204 //cumulenes longer than 3 double bonds are not supported by InChI
205 Edge dirEdge1 = findDirectionalEdge(g, nextDb1);
206 if (dirEdge1 == null) {
207 return false;
208 }
209
210 Edge dirEdge2 = findDirectionalEdge(g, nextDb2);
211 if (dirEdge2 == null) {
212 return false;
213 }
214
215 InchiStereoParity parity = (dirEdge1.bond(nextDb1) == dirEdge2.bond(nextDb2)) ? InchiStereoParity.ODD
216 : InchiStereoParity.EVEN;
217
218 InchiAtom atom1 = input.getAtom(dirEdge1.other(nextDb1));
219 InchiAtom atom2 = input.getAtom(start);
220 InchiAtom atom3 = input.getAtom(end);
221 InchiAtom atom4 = input.getAtom(dirEdge2.other(nextDb2));
222 input.addStereo(InchiStereo.createDoubleBondStereo(atom1, atom2, atom3, atom4, parity));
223 return true;
224 }
225
226 private static Edge findDirectionalEdge(Graph g, int atom) {
227 List<Edge> edges = g.edges(atom);
228 if (edges.size() > 1) {
229 for (Edge e : edges) {
230 Bond b = e.bond();
231 if (b == Bond.UP || b == Bond.DOWN) {
232 return e;
233 }
234 }
235 }
236 return null;
237 }
238
239 private static void addAllenalStereo(InchiInput input, Graph g, int allenalCenter) {
240 List<Edge> bonds = g.edges(allenalCenter);
241 if (bonds.size() !=2 || bonds.get(0).bond().order() != 2 || bonds.get(1).bond().order() != 2) {
242 return;
243 }
244 int doubleBondsInCumulene = 2;
245
246 int next1 = bonds.get(0).other(allenalCenter);
247 int next2 = bonds.get(1).other(allenalCenter);
248 int prev1 = allenalCenter;
249 int prev2 = allenalCenter;
250
251 int tmp1;
252 int tmp2;
253 while ((tmp1 = nextDb(g, next1, prev1)) >=0 && (tmp2 = nextDb(g, next2, prev2)) >=0 ) {
254 prev1 = next1;
255 prev2 = next2;
256 next1 = tmp1;
257 next2 = tmp2;
258 doubleBondsInCumulene +=2;
259 }
260 if (doubleBondsInCumulene > 2) {
261 return;//Not currently supported by InChI
262 }
263 int[] atomIdxs = new int[4];
264 int pos = 0;
265 for (Edge e : g.edges(next1)) {
266 if (e.bond().order() == 2) {
267 continue;
268 }
269 atomIdxs[pos++] = e.other(next1);
270 }
271 int implHyd1 = g.implHCount(next1);
272 if (implHyd1 > 0) {
273 if (implHyd1 > 1) {
274 return;
275 }
276 atomIdxs[pos++] = next1;
277 }
278
279 if (pos != 2) {
280 return;//incorrect number of substituents for allenal stereo
281 }
282 for (Edge e : g.edges(next2)) {
283 if (e.bond().order() == 2) {
284 continue;
285 }
286 atomIdxs[pos++] = e.other(next2);
287 }
288 int implHyd2 = g.implHCount(next2);
289 if (implHyd2 > 0) {
290 if (implHyd2 > 1) {
291 return;
292 }
293 atomIdxs[pos++] = next2;
294 }
295
296 if (pos != 4) {
297 return;//incorrect number of substituents for allenal stereo
298 }
299 Arrays.sort(atomIdxs);
300 InchiAtom[] atoms = new InchiAtom[4];
301 for (int i = 0; i < atomIdxs.length; i++) {
302 int idx = atomIdxs[i];
303 atoms[i] = (idx == next1 || idx == next2) ? InchiStereo.STEREO_IMPLICIT_H : input.getAtom(idx);
304 }
305 InchiStereoParity parity = (g.configurationOf(allenalCenter) == Configuration.AL1) ? InchiStereoParity.ODD : InchiStereoParity.EVEN;
306 input.addStereo(InchiStereo.createAllenalStereo(input.getAtom(allenalCenter), atoms[0], atoms[1], atoms[2], atoms[3], parity));
307 }
308
309 private static int nextDb(Graph g, int current, int prev) {
310 List<Edge> bonds = g.edges(current);
311 if (bonds.size() !=2 || bonds.get(0).bond().order() != 2 || bonds.get(1).bond().order() != 2) {
312 return -1;
313 }
314 for (Edge e : bonds) {
315 int next = e.other(current);
316 if (next != prev) {
317 return next;
318 }
319 }
320 return -1;
321 }
322 }