Debian Codegraph @

jna-rinchi-core/src/main/java/io/github/dan2097/jnarinchi/JnaRinchi.java

raw
1 /**
2 * JNA-RInChI - Library for calling RInChI from Java
3 * Copyright © 2022 Nikolay Kochev
4 * <p>
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 * <p>
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 * <p>
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.jnarinchi;
19
20 import java.io.InputStream;
21 import java.util.Properties;
22
23 import com.sun.jna.Platform;
24 import com.sun.jna.Pointer;
25 import com.sun.jna.ptr.PointerByReference;
26
27 import io.github.dan2097.jnarinchi.cheminfo.MdlReactionReader;
28 import io.github.dan2097.jnarinchi.cheminfo.MdlReactionReaderException;
29 import io.github.dan2097.jnarinchi.cheminfo.MdlReactionWriter;
30
31 /**
32 * Provides access to the native RInChI library functionality via Java code.
33 * This class wraps around the native RInChI library using the JNA native interface
34 * implemented in {@link RinchiLibrary}.
35 * <p>
36 * As the native RInChI library is not thread-safe this wrapper class
37 * makes sure that there is only ever one call placed to the native
38 * RInChI library at a time.
39 * </p>
40 * @author Nikolay Kochev
41 * @author Uli Fechner
42 * @see RinchiLibrary
43 */
44 public class JnaRinchi {
45 // used as a lock object to synchronize on
46 private static final Object lock = new Object();
47 private static final String PROPERTY_KEY_RINCHI_VERSION = "rinchi_version";
48 private static final String PROPERTY_KEY_JNARINCHI_VERSION = "jnarinchi_version";
49 private static final String PROPERTY_FILE_NAME = "jnarinchi_build.props";
50
51 private static final String platform;
52 private static final Throwable libraryLoadingError;
53
54 static {
55 Throwable t = null;
56 String p = null;
57 try {
58 p = Platform.RESOURCE_PREFIX;
59 RinchiLibrary.JNA_NATIVE_LIB.getName();
60 } catch (Throwable e) {
61 t = e;
62 }
63 platform = p;
64 libraryLoadingError = t;
65 }
66
67 /** The RInChI C++ library does not consider platform-dependent line endings, but only uses '\n'. */
68 private static final String RINCHI_DECOMPOSE_LINE_SEPARATOR = "\n";
69 private static final String RINCHI_DECOMPOSE_DIRECTION_SHORT_DESIGNATION = "D";
70 private static final int ERROR_CODE_DECOMPOSE_FROM_LINES = -1;
71
72 /**
73 * Converts a reaction represented as a RinchiInput object into RInChI and RAuxInfo.
74 * The output object of type RinchiOutput contains the generation status, error messages if any,
75 * RInChI and RAuxInfo.
76 * <br>
77 * Default RInChI generation options are applied.
78 *
79 * @param rinchiInput input reaction as RinchiInput object
80 * @return result RinchiOutput object
81 * @see #toRinchi(RinchiInput, RinchiOptions)
82 */
83 public static RinchiOutput toRinchi(RinchiInput rinchiInput) {
84 return toRinchi(rinchiInput, RinchiOptions.DEFAULT_OPTIONS);
85 }
86
87 /**
88 * Converts a reaction represented as a RinchiInput object into RInChI and RAuxInfo.
89 * The output object of type RinchiOutput contains the generation status, error messages if any,
90 * RInChI and RAuxInfo.
91 * <br>
92 * RInChI generation is customized via {@link RinchiOptions}.
93 *
94 * @param rinchiInput input reaction as RinchiInput object
95 * @param options RInChI generation options
96 * @return result RinchiOutput object
97 * @see #toRinchi(RinchiInput)
98 */
99 public static RinchiOutput toRinchi(RinchiInput rinchiInput, RinchiOptions options) {
100 //Converting RinchiInput to RXN/RDFile
101 MdlReactionWriter mdlWriter = new MdlReactionWriter(ReactionFileFormat.RD);
102 String fileText = mdlWriter.rinchiInputToFileText(rinchiInput);
103 return fileTextToRinchi(fileText, options);
104 }
105
106 /**
107 * Converts a reaction represented by RInChI and RAuxInfo into a {@link RinchiInputFromRinchiOutput} object.
108 * The output is an object of type RinchiInputFromRinchiOutput and
109 * contains the generation status, error messages if any and RinchiInput
110 * RInChI and RAuxInfo must not be <code>null</code>.
111 * <br>
112 * If RAuxInfo is not present an empty string (i.e., "") should be given as a value for <code>auxInfo</code>.
113 * <p>
114 * The native RInChI library stores chirality information only via 2D or 3D coordinates (if present in RAuxInfo).
115 * Stereo parity of the chiral atoms is not set within the output MDL RXN/RDFile text.
116 * That is way the resulting RinchiInput objects do not contain stereo elements for tetrahedral chirality.
117 * This information needs to be extracted from the atom coordinates via a third party software.
118 * </p>
119 * @param rinchi input RInChi string
120 * @param auxInfo input RAuxInfo string
121 * @return result RinchiInputFromRinchiOutput object
122 * @see #getRinchiInputFromRinchi(String, String, boolean)
123 */
124 public static RinchiInputFromRinchiOutput getRinchiInputFromRinchi(String rinchi, String auxInfo) {
125 return getRinchiInputFromRinchi(rinchi, auxInfo, false);
126 }
127
128 /**
129 * Converts a reaction represented by RInChI and RAuxInfo into a {@link RinchiInputFromRinchiOutput} object.
130 * The output is an object of type RinchiInputFromRinchiOutput and
131 * contains the generation status, error messages if any and RinchiInput
132 * RInChI and RAuxInfo must not be <code>null</code>.
133 * <br>
134 * If RAuxInfo is not present an empty string (i.e., "") should be given as a value for <code>auxInfo</code>.
135 * <p>
136 * The native RInChI library stores chirality information only via 2D or 3D coordinates (if present in RAuxInfo).
137 * Stereo parity of the chiral atoms is not set within the output MDL RXN/RDFile text.
138 * That is way the resulting RinchiInput objects do not contain stereo elements for tetrahedral chirality.
139 * This information needs to be extracted from the atom coordinates via a third party software.
140 * </p>
141 * <p>
142 * If the value of <code>guessTetrahedralChiralityFromBondsInfo</code> is <code>true</code> stereo elements with
143 * UNDEFINED parity could be guessed from the MDL bond line attributes. This functionality can only be used to
144 * indicate the presence of chiral atoms but the parity (exact chirality) must still be calculated from the coordinates.
145 * </p>
146 * @param rinchi input RInChi string
147 * @param auxInfo input RAuxInfo string
148 * @param guessTetrahedralChiralityFromBondsInfo flag for guessing chiral stereo elements from bond attributes
149 * @return result RinchiInputFromRinchiOutput object
150 * @see #getRinchiInputFromRinchi(String, String)
151 */
152 public static RinchiInputFromRinchiOutput getRinchiInputFromRinchi(String rinchi, String auxInfo,
153 boolean guessTetrahedralChiralityFromBondsInfo) {
154 FileTextOutput ftOut = rinchiToFileText(rinchi, auxInfo, ReactionFileFormat.RD);
155 if (ftOut.getStatus() != Status.SUCCESS)
156 return new RinchiInputFromRinchiOutput(null, Status.ERROR, -1, ftOut.getErrorMessage());
157
158 //Converting RXN/RDFile to RinchiInput
159 MdlReactionReader mdlReader = new MdlReactionReader(ReactionFileFormat.RD, guessTetrahedralChiralityFromBondsInfo);
160
161 try {
162 RinchiInput rInp = mdlReader.fileTextToRinchiInput(ftOut.getReactionFileText());
163 return new RinchiInputFromRinchiOutput(rInp, Status.SUCCESS, 0, "");
164 } catch (MdlReactionReaderException exception) {
165 return new RinchiInputFromRinchiOutput(null, Status.ERROR, -1, exception.getAllErrors());
166 }
167 }
168
169 /**
170 * Converts a reaction represented in MDL RXN or RDFile format into RInChI and RAuxInfo.
171 * The output object of type RinchiOutput contains the generation status, error messages if any,
172 * RInChI and RAuxInfo.
173 * <br>
174 * The file format is automatically recognized.
175 * Default generation options are applied.
176 *
177 * @param reactionTextFile reaction represented in RXN or RDFile format
178 * @return result RinchiOutput object
179 * @see #fileTextToRinchi(String, RinchiOptions)
180 * @see #fileTextToRinchi(String, RinchiOptions, ReactionFileFormat)
181 */
182 public static RinchiOutput fileTextToRinchi(String reactionTextFile) {
183 return fileTextToRinchi(reactionTextFile, RinchiOptions.DEFAULT_OPTIONS, ReactionFileFormat.AUTO);
184 }
185
186 /**
187 * Converts a reaction represented in MDL RXN or RDFile format into RInChI and RAuxInfo.
188 * The output object of type RinchiOutput contains the generation status, error messages if any,
189 * RInChI and RAuxInfo.
190 * <br>
191 * The file format is automatically recognized.
192 * RInChI generation is customized via RinchiOptions.
193 *
194 * @param reactionTextFile reaction represented in RXN or RDFile format
195 * @param options RInChI generation options
196 * @return result RinchiOutput object
197 * @see #fileTextToRinchi(String)
198 * @see #fileTextToRinchi(String, RinchiOptions, ReactionFileFormat)
199 */
200 public static RinchiOutput fileTextToRinchi(String reactionTextFile, RinchiOptions options) {
201 return fileTextToRinchi(reactionTextFile, options, ReactionFileFormat.AUTO);
202 }
203
204 /**
205 * Converts a reaction represented in MDL RXN or RDFile format into RInChI and RAuxInfo.
206 * The output object of type RinchiOutput contains the generation status, error messages if any,
207 * RInChI and RAuxInfo.
208 * <br>
209 * The file format is configured with the argument <code>fileFormat</code>.
210 * RInChI generation is customized via RinchiOptions.
211 *
212 * @param reactionFileText reaction represented in RXN or RDFile format
213 * @param options RInChI generation options
214 * @param fileFormat the MDL file format for reaction representation: {@link ReactionFileFormat#RXN}, {@link ReactionFileFormat#RD} or {@link ReactionFileFormat#AUTO}
215 * @return result RinchiOutput object
216 * @see #fileTextToRinchi(String)
217 * @see #fileTextToRinchi(String, RinchiOptions)
218 */
219 public static RinchiOutput fileTextToRinchi(String reactionFileText, RinchiOptions options, ReactionFileFormat fileFormat) {
220 synchronized (lock) {
221 checkLibrary();
222 requireNonNull(reactionFileText, "reactionFileText");
223 requireNonNull(options, "options");
224 requireNonNull(fileFormat, "fileFormat");
225
226 PointerByReference out_rinchi_string_p = new PointerByReference();
227 PointerByReference out_rinchi_auxinfo_p = new PointerByReference();
228
229 boolean forceEq = options.getFlags().contains(RinchiFlag.ForceEquilibrium);
230 int errCode = RinchiLibrary.rinchilib_rinchi_from_file_text(fileFormat.toString(), reactionFileText,
231 forceEq, out_rinchi_string_p, out_rinchi_auxinfo_p);
232
233 if (errCode != 0) {
234 String errMsg = RinchiLibrary.rinchilib_latest_err_msg();
235 return new RinchiOutput("", "", Status.ERROR, errCode, errMsg);
236 }
237
238 Pointer p = out_rinchi_string_p.getValue();
239 String rinchi = p.getString(0);
240 p = out_rinchi_auxinfo_p.getValue();
241 String auxInfo = p.getString(0);
242
243 return new RinchiOutput(rinchi, auxInfo, Status.SUCCESS, 0, "");
244 }
245 }
246
247 /**
248 * Converts a reaction represented in MDL RXN or RDFile format into RInChIKey.
249 * The output object of type RinchiKeyOutput contains the generation status, error messages if any,
250 * and RInChIKey.
251 * <br>
252 * RInChIKey could be of type: {@link RinchiKeyType#LONG}, {@link RinchiKeyType#SHORT} or {@link RinchiKeyType#WEB}.
253 * The file format is automatically recognized.
254 * Default generation options are applied.
255 *
256 * @param reactionTextFile reaction represented in RXN or RDFile format
257 * @param keyType RInChI-Key type
258 * @return result RinchiKeyOutput object
259 * @see #fileTextToRinchiKey(String, RinchiKeyType, RinchiOptions)
260 * @see #fileTextToRinchiKey(String, RinchiKeyType, RinchiOptions, ReactionFileFormat)
261 */
262 public static RinchiKeyOutput fileTextToRinchiKey(String reactionTextFile, RinchiKeyType keyType) {
263 return fileTextToRinchiKey(reactionTextFile, keyType, RinchiOptions.DEFAULT_OPTIONS, ReactionFileFormat.AUTO);
264 }
265
266 /**
267 * Converts a reaction represented in MDL RXN or RDFile format into RInChIKey.
268 * The output object of type RinchiKeyOutput contains the generation status, error messages if any,
269 * and RInChIKey.
270 * <br>
271 * RInChIKey could be of type: {@link RinchiKeyType#LONG}, {@link RinchiKeyType#SHORT} or {@link RinchiKeyType#WEB}.
272 * The file format is automatically recognized.
273 * RInChIKey generation is customized via RinchiOptions.
274 *
275 * @param reactionFileText reaction represented in RXN or RDFile format
276 * @param keyType RInChI-Key type
277 * @param options RInChI/RInChIKey generation options
278 * @return result RinchiKeyOutput object
279 * @see #fileTextToRinchiKey(String, RinchiKeyType)
280 * @see #fileTextToRinchiKey(String, RinchiKeyType, RinchiOptions, ReactionFileFormat)
281 */
282 public static RinchiKeyOutput fileTextToRinchiKey(String reactionFileText, RinchiKeyType keyType, RinchiOptions options) {
283 return fileTextToRinchiKey(reactionFileText, keyType, options, ReactionFileFormat.AUTO);
284 }
285
286 /**
287 * Converts a reaction represented in MDL RXN or RDFile format into RInChIKey.
288 * The output object of type RinchiKeyOutput contains the generation status, error messages if any,
289 * and RInChIKey.
290 * <br>
291 * RInChIKey could be of type: {@link RinchiKeyType#LONG}, {@link RinchiKeyType#SHORT} or {@link RinchiKeyType#WEB}.
292 * The file format is configured with the argument <code>fileFormat</code>.
293 * RInChIKey generation is customized via RinchiOptions.
294 *
295 * @param reactionFileText reaction represented in RXN or RDFile format
296 * @param keyType RInChI-Key type
297 * @param options RInChI/RInChIKey generation options
298 * @param fileFormat the MDL file format for reaction representation: {@link ReactionFileFormat#RXN}, {@link ReactionFileFormat#RD} or {@link ReactionFileFormat#AUTO}
299 * @return result RinchiKeyOutput object
300 * @see #fileTextToRinchiKey(String, RinchiKeyType)
301 * @see #fileTextToRinchiKey(String, RinchiKeyType, RinchiOptions)
302 */
303 public static RinchiKeyOutput fileTextToRinchiKey(String reactionFileText, RinchiKeyType keyType, RinchiOptions options, ReactionFileFormat fileFormat) {
304 synchronized (lock) {
305 checkLibrary();
306 requireNonNull(reactionFileText, "reactionFileText");
307 requireNonNull(keyType, "keyType");
308 requireNonNull(options, "options");
309 requireNonNull(fileFormat, "fileFormat");
310
311 PointerByReference out_rinchi_key = new PointerByReference();
312 boolean forceEq = options.getFlags().contains(RinchiFlag.ForceEquilibrium);
313 int errCode = RinchiLibrary.rinchilib_rinchikey_from_file_text(fileFormat.toString(), reactionFileText,
314 keyType.getShortDesignation(), forceEq, out_rinchi_key);
315
316 if (errCode != 0) {
317 String err = RinchiLibrary.rinchilib_latest_err_msg();
318 return new RinchiKeyOutput("", keyType, Status.ERROR, errCode, err);
319 }
320
321 Pointer p = out_rinchi_key.getValue();
322 String rinchi_key = p.getString(0);
323
324 return new RinchiKeyOutput(rinchi_key, keyType, Status.SUCCESS, 0, "");
325 }
326 }
327
328 /**
329 * Converts RInChI and RAuxInfo into a reaction, represented in MDL RXN or RDFile format.
330 * The output object of type FileTextOutput contains the conversion status, error messages if any,
331 * and reaction text file.
332 * <br>
333 * The reaction file format is specified by the user. Possible values are {@link ReactionFileFormat#RXN},
334 * {@link ReactionFileFormat#RD} and {@link ReactionFileFormat#AUTO}.
335 *
336 * @param rinchi input RInChi string
337 * @param auxInfo input RAuxInfo string
338 * @param fileFormat the MDL file format for reaction representation: RXN or RDFile (AUTO acts as RDFile)
339 * @return resultant FileTextOutput object
340 */
341 public static FileTextOutput rinchiToFileText(String rinchi, String auxInfo, ReactionFileFormat fileFormat) {
342 synchronized (lock) {
343 checkLibrary();
344 requireNonNull(rinchi, "rinchi");
345 requireNonNull(auxInfo, "auxInfo");
346 requireNonNull(fileFormat, "fileFormat");
347
348 PointerByReference out_file_text_p = new PointerByReference();
349 int errCode = RinchiLibrary.rinchilib_file_text_from_rinchi(rinchi, auxInfo, fileFormat.toString(), out_file_text_p);
350
351 if (errCode != 0) {
352 String err = RinchiLibrary.rinchilib_latest_err_msg();
353 return new FileTextOutput("", fileFormat, Status.ERROR, errCode, err);
354 }
355
356 Pointer p = out_file_text_p.getValue();
357 String reactFileText = p.getString(0);
358
359 return new FileTextOutput(reactFileText, fileFormat, Status.SUCCESS, 0, "");
360 }
361 }
362
363 /**
364 * Generates a RInChIKey from a RInChI string.
365 * The output object of type RinchiKeyOutput contains the generation status, error messages if any,
366 * and RInChIKey.
367 * <br>
368 * RInChIKey could be of type: {@link RinchiKeyType#LONG}, {@link RinchiKeyType#SHORT} or {@link RinchiKeyType#WEB}.
369 *
370 * @param keyType RInChI-Key type
371 * @param rinchi input RInChi string
372 * @return result RinchiKeyOutput object
373 */
374 public static RinchiKeyOutput rinchiToRinchiKey(RinchiKeyType keyType, String rinchi) {
375 synchronized (lock) {
376 checkLibrary();
377 requireNonNull(keyType, "keyType");
378 requireNonNull(rinchi, "rinchi");
379
380 PointerByReference out_rinchi_key = new PointerByReference();
381 int errCode = RinchiLibrary.rinchilib_rinchikey_from_rinchi(rinchi, keyType.getShortDesignation(), out_rinchi_key);
382
383 if (errCode != 0) {
384 String err = RinchiLibrary.rinchilib_latest_err_msg();
385 return new RinchiKeyOutput("", keyType, Status.ERROR, errCode, err);
386 }
387
388 Pointer p = out_rinchi_key.getValue();
389 String rinchi_key = p.getString(0);
390
391 return new RinchiKeyOutput(rinchi_key, keyType, Status.SUCCESS, 0, "");
392 }
393 }
394
395 /**
396 * Splits a RInChI into individual components, that is a list of InChIs.
397 * The output object of type RinchiDecompositionOutput contains the decomposition status, error messages if any,
398 * an array of InChIs together with their roles as components in the reaction, and the RInChI reaction direction.
399 *
400 * @param rinchi input RInChi string
401 * @return result RinchiDecompositionOutput object
402 * @see #decomposeRinchi(String, String)
403 */
404 public static RinchiDecompositionOutput decomposeRinchi(String rinchi) {
405 return decomposeRinchi(rinchi, "");
406 }
407
408 /**
409 * Splits a RInChI into individual components, that is a list of InChIs.
410 * The output object of type RinchiDecompositionOutput contains the decomposition status, error messages if any,
411 * an array of InChIs together with their roles as components in the reaction, an array of AuxInfos, and the
412 * RInChI reaction direction.
413 *
414 * @param rinchi input RInChi string
415 * @param auxInfo input RAuxInfo string
416 * @return result RinchiDecompositionOutput object
417 * @see #decomposeRinchi(String)
418 */
419 public static RinchiDecompositionOutput decomposeRinchi(String rinchi, String auxInfo) {
420 synchronized (lock) {
421 checkLibrary();
422 requireNonNull(rinchi, "rinchi");
423 requireNonNull(auxInfo, "auxInfo");
424
425 PointerByReference out_inchis_text_p = new PointerByReference();
426 int errCode = RinchiLibrary.rinchilib_inchis_from_rinchi(rinchi, auxInfo, out_inchis_text_p);
427
428 if (errCode != 0) {
429 String err = RinchiLibrary.rinchilib_latest_err_msg();
430 return new RinchiDecompositionOutput(ReactionDirection.FORWARD, null, null, null,
431 Status.ERROR, errCode, err);
432 }
433
434 Pointer p = out_inchis_text_p.getValue();
435 String s = p.getString(0);
436
437 return parseNativeOutInchisText(s);
438 }
439 }
440
441 /**
442 * Returns the version of the wrapped RInChI C library.
443 * @return version number string or <code>null</code> if there is an error when retrieving the version number
444 */
445 public static String getRinchiLibraryVersion() {
446 return getVersion(PROPERTY_KEY_RINCHI_VERSION);
447 }
448
449 /**
450 * Returns the version of the JNA-RInChI Java library.
451 * @return version number string or <code>null</code> if there is an error when retrieving the version number
452 */
453 public static String getJnaRinchiVersion() {
454 return getVersion(PROPERTY_KEY_JNARINCHI_VERSION);
455 }
456
457 private static String getVersion(String propertyKey) {
458 try (InputStream is = JnaRinchi.class.getResourceAsStream(PROPERTY_FILE_NAME)) {
459 Properties props = new Properties();
460 props.load(is);
461 return props.getProperty(propertyKey);
462 } catch (Exception e) {
463 return null;
464 }
465 }
466
467 private static RinchiDecompositionOutput parseNativeOutInchisText(String outText) {
468 /*
469 * Output from RInChI native library, function rinchilib_inchis_from_rinchi(),
470 * is a text string with multiple lines in the following format:
471 *
472 * D:<direction>
473 * N:<n1>,<n2>,<n3>
474 * R:<inchi>
475 * R:<auxInfo>
476 * ...
477 * P:<inchi>
478 * p:<auxInfo>
479 * ...
480 * A:<inchi>
481 * A:<auxInfo>
482 * ...
483 */
484
485 String[] lines = outText.split(RINCHI_DECOMPOSE_LINE_SEPARATOR);
486 StringBuilder errorBuffer = new StringBuilder();
487
488 //Check number of lines and determine the number of components
489 int nLines = lines.length;
490 int nComponents = -1;
491 if ((nLines < 2) || (nLines % 2) != 0) {
492 errorBuffer.append("Incorrect number of lines. Expected even number\n");
493 } else
494 nComponents = (nLines - 2) / 2;
495
496 String[] inchis = new String[nComponents];
497 String[] auxInfos = new String[nComponents];
498 ReactionComponentRole[] roles = new ReactionComponentRole[nComponents];
499
500 //Handle reaction direction from first line
501 ReactionDirection direction = null;
502 if (nLines > 0) {
503 if (lines[0].startsWith(RINCHI_DECOMPOSE_DIRECTION_SHORT_DESIGNATION + ":") || lines[0].length() < 3) {
504 String dirStr = lines[0].substring(2);
505 direction = ReactionDirection.getDirectionFromShortDesignation(dirStr);
506 if (direction == null)
507 errorBuffer.append("Incorrect direction definition in the first line: ").append(lines[0]).append("\n");
508 } else
509 errorBuffer.append("Incorrect first line: has to start with ").append(RINCHI_DECOMPOSE_DIRECTION_SHORT_DESIGNATION)
510 .append(":").append("\n");
511 } else
512 errorBuffer.append("No lines available!\n");
513
514 //Iterate all reaction components
515 if (nComponents > 0)
516 for (int i = 0; i < nComponents; i++) {
517 String rinchiLine = lines[2 + 2 * i];
518 String auxInfoLine = lines[2 + 2 * i + 1];
519
520 boolean flagRinchiLineOK = false;
521 ReactionComponentRole role = null;
522 if (rinchiLine.length() < 2)
523 errorBuffer.append("Incorrect RInChI component line: ").append(rinchiLine).append("\n");
524 else {
525 role = ReactionComponentRole.getRoleFromShortDesignation(rinchiLine.substring(0, 1));
526 if (role == null)
527 errorBuffer.append("Incorrect RInChI component line: incorrect role: ").append(rinchiLine).append("\n");
528 else {
529 inchis[i] = rinchiLine.substring(2);
530 flagRinchiLineOK = true;
531 }
532 }
533
534 if (flagRinchiLineOK) {
535 if (auxInfoLine.length() < 2)
536 errorBuffer.append("Incorrect AuxInfo component line: ").append(auxInfoLine).append("\n");
537 else {
538 ReactionComponentRole role2 = ReactionComponentRole.getRoleFromShortDesignation(auxInfoLine.substring(0, 1));
539 if (role2 == null || (role != role2))
540 errorBuffer.append("Incorrect AuxInfo component line: incorrect role: ").append(auxInfoLine).append("\n");
541 else {
542 auxInfos[i] = auxInfoLine.substring(2);
543 roles[i] = role;
544 }
545 }
546 }
547 }
548
549 String err = errorBuffer.toString();
550 if (err.isEmpty())
551 try {
552 return new RinchiDecompositionOutput(direction, inchis, auxInfos, roles,
553 Status.SUCCESS, 0, "");
554 } catch (IllegalArgumentException exception) {
555 // we end up here if the number of InChIs, auxiliary information and reaction component role is not equal
556 return new RinchiDecompositionOutput(direction, null, null, null,
557 Status.ERROR, ERROR_CODE_DECOMPOSE_FROM_LINES, exception.getMessage());
558 }
559 else {
560 //Generally this should never happen. Otherwise, it is a bug in RInChI native C++ code
561 return new RinchiDecompositionOutput(direction, null, null, null,
562 Status.ERROR, ERROR_CODE_DECOMPOSE_FROM_LINES, err);
563 }
564 }
565
566 private static void requireNonNull(Object object, String argumentName) {
567 if (object == null) {
568 throw new IllegalArgumentException("The argument '" + argumentName + "' must not be null.");
569 }
570 }
571
572 static void checkLibrary() {
573 if (libraryLoadingError != null) {
574 throw new RuntimeException("Error loading RInChI native code. Please check that the binaries for your platform (" + platform + ") have been included on the classpath.", libraryLoadingError);
575 }
576 }
577 }