| 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.InputStream; |
| 21 |
import java.nio.charset.StandardCharsets; |
| 22 |
import java.util.HashMap; |
| 23 |
import java.util.List; |
| 24 |
import java.util.Map; |
| 25 |
import java.util.Properties; |
| 26 |
|
| 27 |
import com.sun.jna.NativeLong; |
| 28 |
import com.sun.jna.Platform; |
| 29 |
|
| 30 |
import io.github.dan2097.jnainchi.inchi.InchiLibrary; |
| 31 |
import io.github.dan2097.jnainchi.inchi.InchiLibrary.IXA_BOND_WEDGE; |
| 32 |
import io.github.dan2097.jnainchi.inchi.InchiLibrary.IXA_DBLBOND_CONFIG; |
| 33 |
import io.github.dan2097.jnainchi.inchi.InchiLibrary.IXA_INCHIBUILDER_OPTION; |
| 34 |
import io.github.dan2097.jnainchi.inchi.InchiLibrary.IXA_INCHIBUILDER_STEREOOPTION; |
| 35 |
import io.github.dan2097.jnainchi.inchi.InchiLibrary.tagRetValGetINCHI; |
| 36 |
import io.github.dan2097.jnainchi.inchi.InchiLibrary.tagRetValMOL2INCHI; |
| 37 |
import io.github.dan2097.jnainchi.inchi.IxaFunctions; |
| 38 |
import io.github.dan2097.jnainchi.inchi.IxaFunctions.IXA_ATOMID; |
| 39 |
import io.github.dan2097.jnainchi.inchi.IxaFunctions.IXA_BONDID; |
| 40 |
import io.github.dan2097.jnainchi.inchi.IxaFunctions.IXA_INCHIBUILDER_HANDLE; |
| 41 |
import io.github.dan2097.jnainchi.inchi.IxaFunctions.IXA_MOL_HANDLE; |
| 42 |
import io.github.dan2097.jnainchi.inchi.IxaFunctions.IXA_STATUS_HANDLE; |
| 43 |
import io.github.dan2097.jnainchi.inchi.IxaFunctions.IXA_STEREOID; |
| 44 |
import io.github.dan2097.jnainchi.inchi.tagINCHIStereo0D; |
| 45 |
import io.github.dan2097.jnainchi.inchi.tagINCHI_Input; |
| 46 |
import io.github.dan2097.jnainchi.inchi.tagINCHI_InputINCHI; |
| 47 |
import io.github.dan2097.jnainchi.inchi.tagINCHI_Output; |
| 48 |
import io.github.dan2097.jnainchi.inchi.tagINCHI_OutputStruct; |
| 49 |
import io.github.dan2097.jnainchi.inchi.tagInchiAtom; |
| 50 |
import io.github.dan2097.jnainchi.inchi.tagInchiInpData; |
| 51 |
|
| 52 |
public class JnaInchi { |
| 53 |
|
| 54 |
private static final String platform; |
| 55 |
private static final Throwable libraryLoadingError; |
| 56 |
private static final int ISOTOPIC_SHIFT_RANGE_MIN = InchiLibrary.ISOTOPIC_SHIFT_FLAG - InchiLibrary.ISOTOPIC_SHIFT_MAX; |
| 57 |
private static final int ISOTOPIC_SHIFT_RANGE_MAX = InchiLibrary.ISOTOPIC_SHIFT_FLAG + InchiLibrary.ISOTOPIC_SHIFT_MAX; |
| 58 |
private static final Map<String, Integer> inchiBaseAtomicMasses = new HashMap<>(); |
| 59 |
|
| 60 |
static { |
| 61 |
Throwable t = null; |
| 62 |
String p = null; |
| 63 |
try { |
| 64 |
p = Platform.RESOURCE_PREFIX; |
| 65 |
InchiLibrary.JNA_NATIVE_LIB.getName(); |
| 66 |
} |
| 67 |
catch (Throwable e) { |
| 68 |
t = e; |
| 69 |
} |
| 70 |
platform = p; |
| 71 |
libraryLoadingError = t; |
| 72 |
|
| 73 |
//avg mw from util.c |
| 74 |
inchiBaseAtomicMasses.put("H", 1); |
| 75 |
inchiBaseAtomicMasses.put("D", 2); |
| 76 |
inchiBaseAtomicMasses.put("T", 3); |
| 77 |
inchiBaseAtomicMasses.put("He", 4); |
| 78 |
inchiBaseAtomicMasses.put("Li", 7); |
| 79 |
inchiBaseAtomicMasses.put("Be", 9); |
| 80 |
inchiBaseAtomicMasses.put("B", 11); |
| 81 |
inchiBaseAtomicMasses.put("C", 12); |
| 82 |
inchiBaseAtomicMasses.put("N", 14); |
| 83 |
inchiBaseAtomicMasses.put("O", 16); |
| 84 |
inchiBaseAtomicMasses.put("F", 19); |
| 85 |
inchiBaseAtomicMasses.put("Ne", 20); |
| 86 |
inchiBaseAtomicMasses.put("Na", 23); |
| 87 |
inchiBaseAtomicMasses.put("Mg", 24); |
| 88 |
inchiBaseAtomicMasses.put("Al", 27); |
| 89 |
inchiBaseAtomicMasses.put("Si", 28); |
| 90 |
inchiBaseAtomicMasses.put("P", 31); |
| 91 |
inchiBaseAtomicMasses.put("S", 32); |
| 92 |
inchiBaseAtomicMasses.put("Cl", 35); |
| 93 |
inchiBaseAtomicMasses.put("Ar", 40); |
| 94 |
inchiBaseAtomicMasses.put("K", 39); |
| 95 |
inchiBaseAtomicMasses.put("Ca", 40); |
| 96 |
inchiBaseAtomicMasses.put("Sc", 45); |
| 97 |
inchiBaseAtomicMasses.put("Ti", 48); |
| 98 |
inchiBaseAtomicMasses.put("V", 51); |
| 99 |
inchiBaseAtomicMasses.put("Cr", 52); |
| 100 |
inchiBaseAtomicMasses.put("Mn", 55); |
| 101 |
inchiBaseAtomicMasses.put("Fe", 56); |
| 102 |
inchiBaseAtomicMasses.put("Co", 59); |
| 103 |
inchiBaseAtomicMasses.put("Ni", 59); |
| 104 |
inchiBaseAtomicMasses.put("Cu", 64); |
| 105 |
inchiBaseAtomicMasses.put("Zn", 65); |
| 106 |
inchiBaseAtomicMasses.put("Ga", 70); |
| 107 |
inchiBaseAtomicMasses.put("Ge", 73); |
| 108 |
inchiBaseAtomicMasses.put("As", 75); |
| 109 |
inchiBaseAtomicMasses.put("Se", 79); |
| 110 |
inchiBaseAtomicMasses.put("Br", 80); |
| 111 |
inchiBaseAtomicMasses.put("Kr", 84); |
| 112 |
inchiBaseAtomicMasses.put("Rb", 85); |
| 113 |
inchiBaseAtomicMasses.put("Sr", 88); |
| 114 |
inchiBaseAtomicMasses.put("Y", 89); |
| 115 |
inchiBaseAtomicMasses.put("Zr", 91); |
| 116 |
inchiBaseAtomicMasses.put("Nb", 93); |
| 117 |
inchiBaseAtomicMasses.put("Mo", 96); |
| 118 |
inchiBaseAtomicMasses.put("Tc", 98); |
| 119 |
inchiBaseAtomicMasses.put("Ru", 101); |
| 120 |
inchiBaseAtomicMasses.put("Rh", 103); |
| 121 |
inchiBaseAtomicMasses.put("Pd", 106); |
| 122 |
inchiBaseAtomicMasses.put("Ag", 108); |
| 123 |
inchiBaseAtomicMasses.put("Cd", 112); |
| 124 |
inchiBaseAtomicMasses.put("In", 115); |
| 125 |
inchiBaseAtomicMasses.put("Sn", 119); |
| 126 |
inchiBaseAtomicMasses.put("Sb", 122); |
| 127 |
inchiBaseAtomicMasses.put("Te", 128); |
| 128 |
inchiBaseAtomicMasses.put("I", 127); |
| 129 |
inchiBaseAtomicMasses.put("Xe", 131); |
| 130 |
inchiBaseAtomicMasses.put("Cs", 133); |
| 131 |
inchiBaseAtomicMasses.put("Ba", 137); |
| 132 |
inchiBaseAtomicMasses.put("La", 139); |
| 133 |
inchiBaseAtomicMasses.put("Ce", 140); |
| 134 |
inchiBaseAtomicMasses.put("Pr", 141); |
| 135 |
inchiBaseAtomicMasses.put("Nd", 144); |
| 136 |
inchiBaseAtomicMasses.put("Pm", 145); |
| 137 |
inchiBaseAtomicMasses.put("Sm", 150); |
| 138 |
inchiBaseAtomicMasses.put("Eu", 152); |
| 139 |
inchiBaseAtomicMasses.put("Gd", 157); |
| 140 |
inchiBaseAtomicMasses.put("Tb", 159); |
| 141 |
inchiBaseAtomicMasses.put("Dy", 163); |
| 142 |
inchiBaseAtomicMasses.put("Ho", 165); |
| 143 |
inchiBaseAtomicMasses.put("Er", 167); |
| 144 |
inchiBaseAtomicMasses.put("Tm", 169); |
| 145 |
inchiBaseAtomicMasses.put("Yb", 173); |
| 146 |
inchiBaseAtomicMasses.put("Lu", 175); |
| 147 |
inchiBaseAtomicMasses.put("Hf", 178); |
| 148 |
inchiBaseAtomicMasses.put("Ta", 181); |
| 149 |
inchiBaseAtomicMasses.put("W", 184); |
| 150 |
inchiBaseAtomicMasses.put("Re", 186); |
| 151 |
inchiBaseAtomicMasses.put("Os", 190); |
| 152 |
inchiBaseAtomicMasses.put("Ir", 192); |
| 153 |
inchiBaseAtomicMasses.put("Pt", 195); |
| 154 |
inchiBaseAtomicMasses.put("Au", 197); |
| 155 |
inchiBaseAtomicMasses.put("Hg", 201); |
| 156 |
inchiBaseAtomicMasses.put("Tl", 204); |
| 157 |
inchiBaseAtomicMasses.put("Pb", 207); |
| 158 |
inchiBaseAtomicMasses.put("Bi", 209); |
| 159 |
inchiBaseAtomicMasses.put("Po", 209); |
| 160 |
inchiBaseAtomicMasses.put("At", 210); |
| 161 |
inchiBaseAtomicMasses.put("Rn", 222); |
| 162 |
inchiBaseAtomicMasses.put("Fr", 223); |
| 163 |
inchiBaseAtomicMasses.put("Ra", 226); |
| 164 |
inchiBaseAtomicMasses.put("Ac", 227); |
| 165 |
inchiBaseAtomicMasses.put("Th", 232); |
| 166 |
inchiBaseAtomicMasses.put("Pa", 231); |
| 167 |
inchiBaseAtomicMasses.put("U", 238); |
| 168 |
inchiBaseAtomicMasses.put("Np", 237); |
| 169 |
inchiBaseAtomicMasses.put("Pu", 244); |
| 170 |
inchiBaseAtomicMasses.put("Am", 243); |
| 171 |
inchiBaseAtomicMasses.put("Cm", 247); |
| 172 |
inchiBaseAtomicMasses.put("Bk", 247); |
| 173 |
inchiBaseAtomicMasses.put("Cf", 251); |
| 174 |
inchiBaseAtomicMasses.put("Es", 252); |
| 175 |
inchiBaseAtomicMasses.put("Fm", 257); |
| 176 |
inchiBaseAtomicMasses.put("Md", 258); |
| 177 |
inchiBaseAtomicMasses.put("No", 259); |
| 178 |
inchiBaseAtomicMasses.put("Lr", 260); |
| 179 |
inchiBaseAtomicMasses.put("Rf", 261); |
| 180 |
inchiBaseAtomicMasses.put("Db", 270); |
| 181 |
inchiBaseAtomicMasses.put("Sg", 269); |
| 182 |
inchiBaseAtomicMasses.put("Bh", 270); |
| 183 |
inchiBaseAtomicMasses.put("Hs", 270); |
| 184 |
inchiBaseAtomicMasses.put("Mt", 278); |
| 185 |
inchiBaseAtomicMasses.put("Ds", 281); |
| 186 |
inchiBaseAtomicMasses.put("Rg", 281); |
| 187 |
inchiBaseAtomicMasses.put("Cn", 285); |
| 188 |
inchiBaseAtomicMasses.put("Nh", 278); |
| 189 |
inchiBaseAtomicMasses.put("Fl", 289); |
| 190 |
inchiBaseAtomicMasses.put("Mc", 289); |
| 191 |
inchiBaseAtomicMasses.put("Lv", 293); |
| 192 |
inchiBaseAtomicMasses.put("Ts", 297); |
| 193 |
inchiBaseAtomicMasses.put("Og", 294); |
| 194 |
} |
| 195 |
|
| 196 |
public static InchiOutput toInchi(InchiInput inchiInput) { |
| 197 |
return toInchi(inchiInput, InchiOptions.DEFAULT_OPTIONS); |
| 198 |
} |
| 199 |
|
| 200 |
public static InchiOutput toInchi(InchiInput inchiInput, InchiOptions options) { |
| 201 |
checkLibrary(); |
| 202 |
List<InchiAtom> atoms = inchiInput.getAtoms(); |
| 203 |
int atomCount = atoms.size(); |
| 204 |
if (atomCount > Short.MAX_VALUE) { |
| 205 |
throw new IllegalStateException("InChI is limited to 32767 atoms, input contained " + atomCount + " atoms"); |
| 206 |
} |
| 207 |
List<InchiBond> bonds = inchiInput.getBonds(); |
| 208 |
List<InchiStereo> stereos = inchiInput.getStereos(); |
| 209 |
if (stereos.size() > Short.MAX_VALUE) { |
| 210 |
throw new IllegalStateException("Too many stereochemistry elements in input"); |
| 211 |
} |
| 212 |
|
| 213 |
IXA_STATUS_HANDLE logger = IxaFunctions.IXA_STATUS_Create(); |
| 214 |
IXA_MOL_HANDLE nativeMol = IxaFunctions.IXA_MOL_Create(logger); |
| 215 |
IxaFunctions.IXA_MOL_ReserveSpace(logger, nativeMol, atomCount, bonds.size(), stereos.size()); |
| 216 |
try { |
| 217 |
Map<InchiAtom, IXA_ATOMID> atomToNativeAtom = addAtoms(nativeMol, logger, atoms); |
| 218 |
addBonds(nativeMol, logger, bonds, atomToNativeAtom); |
| 219 |
addStereos(nativeMol, logger, stereos, atomToNativeAtom); |
| 220 |
return buildInchi(logger, nativeMol, options); |
| 221 |
} |
| 222 |
finally { |
| 223 |
IxaFunctions.IXA_MOL_Destroy(logger, nativeMol); |
| 224 |
IxaFunctions.IXA_STATUS_Destroy(logger); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
private static Map<InchiAtom, IXA_ATOMID> addAtoms(IXA_MOL_HANDLE mol, IXA_STATUS_HANDLE logger, List<InchiAtom> atoms) { |
| 229 |
Map<InchiAtom, IXA_ATOMID> atomToNativeAtom = new HashMap<>(); |
| 230 |
for (InchiAtom atom : atoms) { |
| 231 |
//For performance only call IxaFunctions when values differ from the defaults |
| 232 |
IXA_ATOMID nativeAtom = IxaFunctions.IXA_MOL_CreateAtom(logger, mol); |
| 233 |
atomToNativeAtom.put(atom, nativeAtom); |
| 234 |
|
| 235 |
if (atom.getX() != 0) { |
| 236 |
IxaFunctions.IXA_MOL_SetAtomX(logger, mol, nativeAtom, atom.getX()); |
| 237 |
} |
| 238 |
if (atom.getY() != 0) { |
| 239 |
IxaFunctions.IXA_MOL_SetAtomY(logger, mol, nativeAtom, atom.getY()); |
| 240 |
} |
| 241 |
if (atom.getZ() != 0) { |
| 242 |
IxaFunctions.IXA_MOL_SetAtomZ(logger, mol, nativeAtom, atom.getZ()); |
| 243 |
} |
| 244 |
String elName = atom.getElName(); |
| 245 |
if (!elName.equals("C")) { |
| 246 |
if (elName.length() > 5) { |
| 247 |
throw new IllegalArgumentException("Element name was too long: " + elName); |
| 248 |
} |
| 249 |
IxaFunctions.IXA_MOL_SetAtomElement(logger, mol, nativeAtom, elName); |
| 250 |
} |
| 251 |
if (atom.getIsotopicMass() != 0) { |
| 252 |
IxaFunctions.IXA_MOL_SetAtomMass(logger, mol, nativeAtom, atom.getIsotopicMass()); |
| 253 |
} |
| 254 |
if (atom.getCharge() != 0) { |
| 255 |
IxaFunctions.IXA_MOL_SetAtomCharge(logger, mol, nativeAtom, atom.getCharge()); |
| 256 |
} |
| 257 |
if (atom.getRadical() != InchiRadical.NONE) { |
| 258 |
IxaFunctions.IXA_MOL_SetAtomRadical(logger, mol, nativeAtom, atom.getRadical().getCode()); |
| 259 |
} |
| 260 |
if (atom.getImplicitHydrogen() != 0) { |
| 261 |
IxaFunctions.IXA_MOL_SetAtomHydrogens(logger, mol, nativeAtom, 0, atom.getImplicitHydrogen()); |
| 262 |
} |
| 263 |
if (atom.getImplicitProtium() != 0) { |
| 264 |
IxaFunctions.IXA_MOL_SetAtomHydrogens(logger, mol, nativeAtom, 1, atom.getImplicitProtium()); |
| 265 |
} |
| 266 |
if (atom.getImplicitDeuterium() != 0) { |
| 267 |
IxaFunctions.IXA_MOL_SetAtomHydrogens(logger, mol, nativeAtom, 2, atom.getImplicitDeuterium()); |
| 268 |
} |
| 269 |
if (atom.getImplicitTritium() != 0) { |
| 270 |
IxaFunctions.IXA_MOL_SetAtomHydrogens(logger, mol, nativeAtom, 3, atom.getImplicitTritium()); |
| 271 |
} |
| 272 |
} |
| 273 |
return atomToNativeAtom; |
| 274 |
} |
| 275 |
|
| 276 |
private static void addBonds(IXA_MOL_HANDLE mol, IXA_STATUS_HANDLE logger, List<InchiBond> bonds, Map<InchiAtom, IXA_ATOMID> atomToNativeAtom) { |
| 277 |
for (InchiBond bond : bonds) { |
| 278 |
IXA_ATOMID nativeAtom1 = atomToNativeAtom.get(bond.getStart()); |
| 279 |
IXA_ATOMID nativeAtom2 = atomToNativeAtom.get(bond.getEnd()); |
| 280 |
if (nativeAtom1 == null || nativeAtom2 == null) { |
| 281 |
throw new IllegalStateException("Bond referenced an atom that was not part of the InchiInput"); |
| 282 |
} |
| 283 |
IXA_BONDID nativeBond = IxaFunctions.IXA_MOL_CreateBond(logger, mol, nativeAtom1, nativeAtom2); |
| 284 |
InchiBondType bondType = bond.getType(); |
| 285 |
if (bondType != InchiBondType.SINGLE) { |
| 286 |
IxaFunctions.IXA_MOL_SetBondType(logger, mol, nativeBond, bondType.getCode()); |
| 287 |
} |
| 288 |
switch (bond.getStereo()) { |
| 289 |
case DOUBLE_EITHER: |
| 290 |
//Default is to perceive configuration from 2D coordinates |
| 291 |
IxaFunctions.IXA_MOL_SetDblBondConfig(logger, mol, nativeBond, IXA_DBLBOND_CONFIG.IXA_DBLBOND_CONFIG_EITHER); |
| 292 |
break; |
| 293 |
case SINGLE_1DOWN: |
| 294 |
IxaFunctions.IXA_MOL_SetBondWedge(logger, mol, nativeBond, nativeAtom1, IXA_BOND_WEDGE.IXA_BOND_WEDGE_DOWN); |
| 295 |
break; |
| 296 |
case SINGLE_1EITHER: |
| 297 |
IxaFunctions.IXA_MOL_SetBondWedge(logger, mol, nativeBond, nativeAtom1, IXA_BOND_WEDGE.IXA_BOND_WEDGE_EITHER); |
| 298 |
break; |
| 299 |
case SINGLE_1UP: |
| 300 |
IxaFunctions.IXA_MOL_SetBondWedge(logger, mol, nativeBond, nativeAtom1, IXA_BOND_WEDGE.IXA_BOND_WEDGE_UP); |
| 301 |
break; |
| 302 |
case SINGLE_2DOWN: |
| 303 |
IxaFunctions.IXA_MOL_SetBondWedge(logger, mol, nativeBond, nativeAtom2, IXA_BOND_WEDGE.IXA_BOND_WEDGE_DOWN); |
| 304 |
break; |
| 305 |
case SINGLE_2EITHER: |
| 306 |
IxaFunctions.IXA_MOL_SetBondWedge(logger, mol, nativeBond, nativeAtom2, IXA_BOND_WEDGE.IXA_BOND_WEDGE_EITHER); |
| 307 |
break; |
| 308 |
case SINGLE_2UP: |
| 309 |
IxaFunctions.IXA_MOL_SetBondWedge(logger, mol, nativeBond, nativeAtom2, IXA_BOND_WEDGE.IXA_BOND_WEDGE_UP); |
| 310 |
break; |
| 311 |
case NONE: |
| 312 |
break; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
private static void addStereos(IXA_MOL_HANDLE nativeMol, IXA_STATUS_HANDLE logger, List<InchiStereo> stereos, Map<InchiAtom, IXA_ATOMID> atomToNativeAtom) { |
| 317 |
for (InchiStereo stereo : stereos) { |
| 318 |
InchiStereoType type = stereo.getType(); |
| 319 |
if (type == InchiStereoType.None) { |
| 320 |
continue; |
| 321 |
} |
| 322 |
InchiAtom[] atomsInCenter = stereo.getAtoms(); |
| 323 |
IXA_ATOMID vertex1 = getStereoVertex(atomToNativeAtom, atomsInCenter[0]); |
| 324 |
IXA_ATOMID vertex2 = getStereoVertex(atomToNativeAtom, atomsInCenter[1]); |
| 325 |
IXA_ATOMID vertex3 = getStereoVertex(atomToNativeAtom, atomsInCenter[2]); |
| 326 |
IXA_ATOMID vertex4 = getStereoVertex(atomToNativeAtom, atomsInCenter[3]); |
| 327 |
|
| 328 |
IXA_STEREOID center; |
| 329 |
switch (type) { |
| 330 |
case Tetrahedral: |
| 331 |
{ |
| 332 |
IXA_ATOMID centralAtom = atomToNativeAtom.get(stereo.getCentralAtom()); |
| 333 |
if (centralAtom == null) { |
| 334 |
throw new IllegalStateException("Stereo configuration central atom referenced an atom that does not exist"); |
| 335 |
} |
| 336 |
center = IxaFunctions.IXA_MOL_CreateStereoTetrahedron(logger, nativeMol, centralAtom, vertex1, vertex2, vertex3, vertex4); |
| 337 |
break; |
| 338 |
} |
| 339 |
case Allene: |
| 340 |
{ |
| 341 |
IXA_ATOMID centralAtom = atomToNativeAtom.get(stereo.getCentralAtom()); |
| 342 |
if (centralAtom == null) { |
| 343 |
throw new IllegalStateException("Stereo configuration central atom referenced an atom that does not exist"); |
| 344 |
} |
| 345 |
center = IxaFunctions.IXA_MOL_CreateStereoAntiRectangle(logger, nativeMol, centralAtom, vertex1, vertex2, vertex3, vertex4); |
| 346 |
break; |
| 347 |
} |
| 348 |
case DoubleBond: |
| 349 |
{ |
| 350 |
IXA_BONDID centralBond = IxaFunctions.IXA_MOL_GetCommonBond(logger, nativeMol, vertex2, vertex3); |
| 351 |
if (centralBond == null) { |
| 352 |
throw new IllegalStateException("Could not find olefin/cumulene central bond"); |
| 353 |
} |
| 354 |
//We intentionally pass dummy values for vertex2/vertex3, as the IXA API doesn't actually need these as long as vertex1 and vertex4 aren't implicit hydrogen |
| 355 |
center = IxaFunctions.IXA_MOL_CreateStereoRectangle(logger, nativeMol, centralBond, vertex1, IxaFunctions.IXA_ATOMID_IMPLICIT_H, IxaFunctions.IXA_ATOMID_IMPLICIT_H, vertex4); |
| 356 |
break; |
| 357 |
} |
| 358 |
default: |
| 359 |
throw new IllegalStateException("Unexpected InChI stereo type:" + type); |
| 360 |
} |
| 361 |
byte parity = stereo.getParity().getCode(); |
| 362 |
IxaFunctions.IXA_MOL_SetStereoParity(logger, nativeMol, center, parity); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
private static IXA_ATOMID getStereoVertex(Map<InchiAtom, IXA_ATOMID> atomToNativeAtom, InchiAtom inchiAtom) { |
| 367 |
if (InchiStereo.STEREO_IMPLICIT_H == inchiAtom) { |
| 368 |
return IxaFunctions.IXA_ATOMID_IMPLICIT_H; |
| 369 |
} |
| 370 |
IXA_ATOMID vertex = atomToNativeAtom.get(inchiAtom); |
| 371 |
if (vertex == null) { |
| 372 |
throw new IllegalStateException("Stereo configuration referenced an atom that does not exist"); |
| 373 |
} |
| 374 |
return vertex; |
| 375 |
} |
| 376 |
|
| 377 |
private static InchiOutput buildInchi(IXA_STATUS_HANDLE logger, IXA_MOL_HANDLE nativeMol, InchiOptions options) { |
| 378 |
IXA_INCHIBUILDER_HANDLE builder = IxaFunctions.IXA_INCHIBUILDER_Create(logger); |
| 379 |
try { |
| 380 |
IxaFunctions.IXA_INCHIBUILDER_SetMolecule(logger, builder, nativeMol); |
| 381 |
|
| 382 |
long timeoutMilliSecs = options.getTimeoutMilliSeconds(); |
| 383 |
if (timeoutMilliSecs != 0) { |
| 384 |
IxaFunctions.IXA_INCHIBUILDER_SetOption_Timeout_MilliSeconds(logger, builder, timeoutMilliSecs); |
| 385 |
} |
| 386 |
for (InchiFlag flag : options.getFlags()) { |
| 387 |
switch (flag) { |
| 388 |
case AuxNone: |
| 389 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_AuxNone, true); |
| 390 |
break; |
| 391 |
case ChiralFlagOFF: |
| 392 |
IxaFunctions.IXA_MOL_SetChiral(logger, nativeMol, false); |
| 393 |
break; |
| 394 |
case ChiralFlagON: |
| 395 |
IxaFunctions.IXA_MOL_SetChiral(logger, nativeMol, true); |
| 396 |
break; |
| 397 |
case DoNotAddH: |
| 398 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_DoNotAddH, true); |
| 399 |
break; |
| 400 |
case FixedH: |
| 401 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_FixedH, true); |
| 402 |
break; |
| 403 |
case KET: |
| 404 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_KET, true); |
| 405 |
break; |
| 406 |
case LargeMolecules: |
| 407 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_LargeMolecules, true); |
| 408 |
break; |
| 409 |
case NEWPSOFF: |
| 410 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_NewPsOff, true); |
| 411 |
break; |
| 412 |
case OneFiveT: |
| 413 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_15T, true); |
| 414 |
break; |
| 415 |
case RecMet: |
| 416 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_RecMet, true); |
| 417 |
break; |
| 418 |
case SLUUD: |
| 419 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_SLUUD, true); |
| 420 |
break; |
| 421 |
case SNon: |
| 422 |
IxaFunctions.IXA_INCHIBUILDER_SetOption_Stereo(logger, builder, IXA_INCHIBUILDER_STEREOOPTION.IXA_INCHIBUILDER_STEREOOPTION_SNon); |
| 423 |
break; |
| 424 |
case SRac: |
| 425 |
IxaFunctions.IXA_INCHIBUILDER_SetOption_Stereo(logger, builder, IXA_INCHIBUILDER_STEREOOPTION.IXA_INCHIBUILDER_STEREOOPTION_SRac); |
| 426 |
break; |
| 427 |
case SRel: |
| 428 |
IxaFunctions.IXA_INCHIBUILDER_SetOption_Stereo(logger, builder, IXA_INCHIBUILDER_STEREOOPTION.IXA_INCHIBUILDER_STEREOOPTION_SRel); |
| 429 |
break; |
| 430 |
case SUCF: |
| 431 |
IxaFunctions.IXA_INCHIBUILDER_SetOption_Stereo(logger, builder, IXA_INCHIBUILDER_STEREOOPTION.IXA_INCHIBUILDER_STEREOOPTION_SUCF); |
| 432 |
break; |
| 433 |
case SAbs: |
| 434 |
IxaFunctions.IXA_INCHIBUILDER_SetOption_Stereo(logger, builder, IXA_INCHIBUILDER_STEREOOPTION.IXA_INCHIBUILDER_STEREOOPTION_SAbs); |
| 435 |
break; |
| 436 |
case SUU: |
| 437 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_SUU, true); |
| 438 |
break; |
| 439 |
case SaveOpt: |
| 440 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_SaveOpt, true); |
| 441 |
break; |
| 442 |
case WarnOnEmptyStructure: |
| 443 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_WarnOnEmptyStructure, true); |
| 444 |
break; |
| 445 |
case NoWarnings: |
| 446 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_NoWarnings, true); |
| 447 |
break; |
| 448 |
case LooseTSACheck: |
| 449 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_LooseTSACheck, true); |
| 450 |
break; |
| 451 |
case Polymers: |
| 452 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_Polymers, true); |
| 453 |
break; |
| 454 |
case Polymers105: |
| 455 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_Polymers105, true); |
| 456 |
break; |
| 457 |
case FoldCRU: |
| 458 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_FoldCRU, true); |
| 459 |
break; |
| 460 |
case NoFrameShift: |
| 461 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_NoFrameShift, true); |
| 462 |
break; |
| 463 |
case NoEdits: |
| 464 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_NoEdits, true); |
| 465 |
break; |
| 466 |
case NPZz: |
| 467 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_NPZZ, true); |
| 468 |
break; |
| 469 |
case SAtZz: |
| 470 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_SATZZ, true); |
| 471 |
break; |
| 472 |
case OutErrInChI: |
| 473 |
IxaFunctions.IXA_INCHIBUILDER_SetOption(logger, builder, IXA_INCHIBUILDER_OPTION.IXA_INCHIBUILDER_OPTION_OutErrInChI, true); |
| 474 |
break; |
| 475 |
default: |
| 476 |
throw new IllegalStateException("Unexpected InChI option flag: " + flag); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
String inchi = IxaFunctions.IXA_INCHIBUILDER_GetInChI(logger, builder); |
| 481 |
String auxInfo = IxaFunctions.IXA_INCHIBUILDER_GetAuxInfo(logger, builder); |
| 482 |
String log = IxaFunctions.IXA_INCHIBUILDER_GetLog(logger, builder); |
| 483 |
|
| 484 |
InchiStatus status = InchiStatus.SUCCESS; |
| 485 |
if (IxaFunctions.IXA_STATUS_HasError(logger)) { |
| 486 |
status = InchiStatus.ERROR; |
| 487 |
} |
| 488 |
else if (IxaFunctions.IXA_STATUS_HasWarning(logger)) { |
| 489 |
status = InchiStatus.WARNING; |
| 490 |
} |
| 491 |
|
| 492 |
StringBuilder sb = new StringBuilder(); |
| 493 |
int messageCount = IxaFunctions.IXA_STATUS_GetCount(logger); |
| 494 |
for (int i = 0; i < messageCount; i++) { |
| 495 |
if (i > 0) { |
| 496 |
sb.append("; "); |
| 497 |
} |
| 498 |
sb.append(IxaFunctions.IXA_STATUS_GetMessage(logger, i)); |
| 499 |
} |
| 500 |
return new InchiOutput(inchi, auxInfo, sb.toString(), log, status); |
| 501 |
} |
| 502 |
finally { |
| 503 |
IxaFunctions.IXA_INCHIBUILDER_Destroy(logger, builder); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
public static InchiOutput molToInchi(String molText) { |
| 508 |
return molToInchi(molText, InchiOptions.DEFAULT_OPTIONS); |
| 509 |
} |
| 510 |
|
| 511 |
public static InchiOutput molToInchi(String molText, InchiOptions options) { |
| 512 |
checkLibrary(); |
| 513 |
tagINCHI_Output nativeOutput = new tagINCHI_Output(); |
| 514 |
try { |
| 515 |
int ret = InchiLibrary.MakeINCHIFromMolfileText(molText, options.toString(), nativeOutput); |
| 516 |
InchiStatus status; |
| 517 |
switch (ret) { |
| 518 |
case tagRetValMOL2INCHI.mol2inchi_Ret_OKAY: |
| 519 |
status = InchiStatus.SUCCESS; |
| 520 |
break; |
| 521 |
case tagRetValMOL2INCHI.mol2inchi_Ret_WARNING: |
| 522 |
status = InchiStatus.WARNING; |
| 523 |
break; |
| 524 |
case tagRetValMOL2INCHI.mol2inchi_Ret_EOF: |
| 525 |
case tagRetValMOL2INCHI.mol2inchi_Ret_ERROR: |
| 526 |
case tagRetValMOL2INCHI.mol2inchi_Ret_ERROR_get: |
| 527 |
case tagRetValMOL2INCHI.mol2inchi_Ret_ERROR_comp: |
| 528 |
status = InchiStatus.ERROR; |
| 529 |
break; |
| 530 |
default: |
| 531 |
status = InchiStatus.ERROR; |
| 532 |
break; |
| 533 |
} |
| 534 |
// The way nativeOutput.szLog is truncated can be a bit odd, but this seems pseudo-intentional, see copy_corrected_log_tail in inchi_dll.c |
| 535 |
return new InchiOutput(nativeOutput.szInChI, nativeOutput.szAuxInfo, nativeOutput.szMessage, nativeOutput.szLog, status); |
| 536 |
} |
| 537 |
finally { |
| 538 |
InchiLibrary.FreeINCHI(nativeOutput); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* Converts InChI into InChI for validation purposes. |
| 544 |
* It may also be used to filter out specific layers. |
| 545 |
* For instance, SNon would remove the stereochemical layer. |
| 546 |
* Omitting FixedH and/or RecMet would remove Fixed-H or Reconnected layers. |
| 547 |
* @param inchi |
| 548 |
* @param options |
| 549 |
* @return |
| 550 |
*/ |
| 551 |
public static InchiOutput inchiToInchi(String inchi, InchiOptions options) { |
| 552 |
checkLibrary(); |
| 553 |
IXA_STATUS_HANDLE logger = IxaFunctions.IXA_STATUS_Create(); |
| 554 |
IXA_MOL_HANDLE nativeMol = IxaFunctions.IXA_MOL_Create(logger); |
| 555 |
try { |
| 556 |
IxaFunctions.IXA_MOL_ReadInChI(logger, nativeMol, inchi); |
| 557 |
return buildInchi(logger, nativeMol, options); |
| 558 |
} |
| 559 |
finally { |
| 560 |
IxaFunctions.IXA_MOL_Destroy(logger, nativeMol); |
| 561 |
IxaFunctions.IXA_STATUS_Destroy(logger); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
public static InchiKeyOutput inchiToInchiKey(String inchi) { |
| 566 |
checkLibrary(); |
| 567 |
byte[] inchiKeyBytes = new byte[28]; |
| 568 |
byte[] szXtra1Bytes = new byte[65]; |
| 569 |
byte[] szXtra2Bytes = new byte[65]; |
| 570 |
InchiKeyStatus ret = InchiKeyStatus.of(InchiLibrary.GetINCHIKeyFromINCHI(inchi, 1, 1, inchiKeyBytes, szXtra1Bytes, szXtra2Bytes)); |
| 571 |
String inchiKeyStr = new String(inchiKeyBytes, StandardCharsets.UTF_8).trim(); |
| 572 |
String szXtra1 = new String(szXtra1Bytes, StandardCharsets.UTF_8).trim(); |
| 573 |
String szXtra2 = new String(szXtra2Bytes, StandardCharsets.UTF_8).trim(); |
| 574 |
return new InchiKeyOutput(inchiKeyStr, ret, szXtra1, szXtra2); |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* Check if the string represents a valid InChI/StdInChI |
| 579 |
* If strict is true, try to perform InChI2InChI conversion; returns success if a resulting InChI string exactly matches source. |
| 580 |
* Be cautious: the result may be too strict, i.e. a 'false alarm', due to imperfection of conversion. |
| 581 |
* @param inchi |
| 582 |
* @param strict if false, just briefly check for proper layout (prefix, version, etc.) |
| 583 |
* @return InchiCheckStatus |
| 584 |
*/ |
| 585 |
public static InchiCheckStatus checkInchi(String inchi, boolean strict) { |
| 586 |
checkLibrary(); |
| 587 |
return InchiCheckStatus.of(InchiLibrary.CheckINCHI(inchi, strict)); |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Check if the string represents valid InChIKey |
| 592 |
* @param inchiKey |
| 593 |
* @return InchiKeyCheckStatus |
| 594 |
*/ |
| 595 |
public static InchiKeyCheckStatus checkInchiKey(String inchiKey) { |
| 596 |
checkLibrary(); |
| 597 |
return InchiKeyCheckStatus.of(InchiLibrary.CheckINCHIKey(inchiKey)); |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* Creates the input data structure for InChI generation out of the auxiliary information (AuxInfo) |
| 602 |
* string produced by previous InChI generator calls |
| 603 |
* @param auxInfo contains ASCIIZ string of InChI output for a single structure or only the AuxInfo line |
| 604 |
* @param doNotAddH if true then InChI will not be allowed to add implicit H |
| 605 |
* @param diffUnkUndfStereo if true, use different labels for unknown and undefined stereo |
| 606 |
* @return |
| 607 |
*/ |
| 608 |
public static InchiInputFromAuxinfoOutput getInchiInputFromAuxInfo(String auxInfo, boolean doNotAddH, boolean diffUnkUndfStereo) { |
| 609 |
checkLibrary(); |
| 610 |
tagINCHI_Input pInp = new tagINCHI_Input(); |
| 611 |
tagInchiInpData input = new tagInchiInpData(pInp); |
| 612 |
try { |
| 613 |
InchiStatus status = getInchiStatus(InchiLibrary.Get_inchi_Input_FromAuxInfo(auxInfo, doNotAddH, diffUnkUndfStereo, input)); |
| 614 |
|
| 615 |
InchiInput inchiInput = new InchiInput(); |
| 616 |
|
| 617 |
tagINCHI_Input populatedInput = input.pInp; |
| 618 |
if (populatedInput.num_atoms > 0) { |
| 619 |
tagInchiAtom[] nativeAtoms = new tagInchiAtom[populatedInput.num_atoms]; |
| 620 |
populatedInput.atom.toArray(nativeAtoms); |
| 621 |
nativeToJavaAtoms(inchiInput, nativeAtoms); |
| 622 |
nativeToJavaBonds(inchiInput, nativeAtoms); |
| 623 |
} |
| 624 |
if (populatedInput.num_stereo0D > 0) { |
| 625 |
tagINCHIStereo0D[] nativeStereos = new tagINCHIStereo0D[populatedInput.num_stereo0D]; |
| 626 |
populatedInput.stereo0D.toArray(nativeStereos); |
| 627 |
nativeToJavaStereos(inchiInput, nativeStereos); |
| 628 |
} |
| 629 |
String message = toString(input.szErrMsg); |
| 630 |
Boolean chiralFlag = null; |
| 631 |
if (input.bChiral == 1) { |
| 632 |
chiralFlag = true; |
| 633 |
} |
| 634 |
else if (input.bChiral == 2) { |
| 635 |
chiralFlag = false; |
| 636 |
} |
| 637 |
return new InchiInputFromAuxinfoOutput(inchiInput, chiralFlag, message, status); |
| 638 |
} |
| 639 |
finally { |
| 640 |
InchiLibrary.Free_inchi_Input(pInp); |
| 641 |
input.clear(); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
public static InchiInputFromInchiOutput getInchiInputFromInchi(String inchi) { |
| 646 |
return getInchiInputFromInchi(inchi, InchiOptions.DEFAULT_OPTIONS); |
| 647 |
} |
| 648 |
|
| 649 |
public static InchiInputFromInchiOutput getInchiInputFromInchi(String inchi, InchiOptions options) { |
| 650 |
checkLibrary(); |
| 651 |
tagINCHI_InputINCHI input = new tagINCHI_InputINCHI(inchi, options.toString()); |
| 652 |
tagINCHI_OutputStruct output = new tagINCHI_OutputStruct(); |
| 653 |
try { |
| 654 |
InchiStatus status = getInchiStatus(InchiLibrary.GetStructFromINCHI(input, output)); |
| 655 |
InchiInput inchiInput = new InchiInput(); |
| 656 |
|
| 657 |
if (output.num_atoms > 0) { |
| 658 |
tagInchiAtom[] nativeAtoms = new tagInchiAtom[output.num_atoms]; |
| 659 |
output.atom.toArray(nativeAtoms); |
| 660 |
nativeToJavaAtoms(inchiInput, nativeAtoms); |
| 661 |
nativeToJavaBonds(inchiInput, nativeAtoms); |
| 662 |
} |
| 663 |
if (output.num_stereo0D > 0) { |
| 664 |
tagINCHIStereo0D[] nativeStereos = new tagINCHIStereo0D[output.num_stereo0D]; |
| 665 |
output.stereo0D.toArray(nativeStereos); |
| 666 |
nativeToJavaStereos(inchiInput, nativeStereos); |
| 667 |
} |
| 668 |
String message = output.szMessage; |
| 669 |
String log = output.szLog; |
| 670 |
NativeLong[] nativeFlags = output.WarningFlags;//This is a flattened multi-dimensional array, unflatten as we convert |
| 671 |
long[][] warningFlags = new long[2][2]; |
| 672 |
for (int i = 0; i < nativeFlags.length; i++) { |
| 673 |
long val = nativeFlags[i].longValue(); |
| 674 |
switch (i) { |
| 675 |
case 0: |
| 676 |
warningFlags[0][0] = val; |
| 677 |
break; |
| 678 |
case 1: |
| 679 |
warningFlags[0][1] = val; |
| 680 |
break; |
| 681 |
case 2: |
| 682 |
warningFlags[1][0] = val; |
| 683 |
break; |
| 684 |
case 3: |
| 685 |
warningFlags[1][1] = val; |
| 686 |
break; |
| 687 |
default: |
| 688 |
break; |
| 689 |
} |
| 690 |
} |
| 691 |
return new InchiInputFromInchiOutput(inchiInput, message, log, status, warningFlags); |
| 692 |
} |
| 693 |
finally { |
| 694 |
InchiLibrary.FreeStructFromINCHI(output); |
| 695 |
input.clear(); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
private static void nativeToJavaAtoms(InchiInput inchiInput, tagInchiAtom[] nativeAtoms) { |
| 700 |
for (int i = 0, numAtoms = nativeAtoms.length; i < numAtoms; i++) { |
| 701 |
tagInchiAtom nativeAtom = nativeAtoms[i]; |
| 702 |
String elSymbol = toString(nativeAtom.elname); |
| 703 |
InchiAtom atom = new InchiAtom(elSymbol); |
| 704 |
atom.setX(nativeAtom.x); |
| 705 |
atom.setY(nativeAtom.y); |
| 706 |
atom.setZ(nativeAtom.z); |
| 707 |
atom.setImplicitHydrogen(nativeAtom.num_iso_H[0]); |
| 708 |
atom.setImplicitProtium(nativeAtom.num_iso_H[1]); |
| 709 |
atom.setImplicitDeuterium(nativeAtom.num_iso_H[2]); |
| 710 |
atom.setImplicitTritium(nativeAtom.num_iso_H[3]); |
| 711 |
int isotopicMass = nativeAtom.isotopic_mass; |
| 712 |
if (isotopicMass >= ISOTOPIC_SHIFT_RANGE_MIN && isotopicMass <= ISOTOPIC_SHIFT_RANGE_MAX) { |
| 713 |
//isotopic mass contains a delta from a hardcoded base mass |
| 714 |
int baseMass = inchiBaseAtomicMasses.getOrDefault(elSymbol, 0); |
| 715 |
int delta = isotopicMass - InchiLibrary.ISOTOPIC_SHIFT_FLAG; |
| 716 |
isotopicMass = baseMass + delta; |
| 717 |
} |
| 718 |
atom.setIsotopicMass(isotopicMass); |
| 719 |
atom.setRadical(InchiRadical.of(nativeAtom.radical)); |
| 720 |
atom.setCharge(nativeAtom.charge); |
| 721 |
inchiInput.addAtom(atom); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
private static void nativeToJavaBonds(InchiInput inchiInput, tagInchiAtom[] nativeAtoms) { |
| 726 |
int numAtoms = nativeAtoms.length; |
| 727 |
boolean[] seenAtoms = new boolean[numAtoms]; |
| 728 |
for (int i = 0; i < numAtoms; i++) { |
| 729 |
tagInchiAtom nativeAtom = nativeAtoms[i]; |
| 730 |
int numBonds = nativeAtom.num_bonds; |
| 731 |
if (numBonds > 0) { |
| 732 |
InchiAtom atom = inchiInput.getAtom(i); |
| 733 |
for (int j = 0; j < numBonds; j++) { |
| 734 |
int neighborIdx = nativeAtom.neighbor[j]; |
| 735 |
if (seenAtoms[neighborIdx]) { |
| 736 |
//Only add each bond once |
| 737 |
continue; |
| 738 |
} |
| 739 |
InchiAtom neighbor = inchiInput.getAtom(neighborIdx); |
| 740 |
InchiBondType bondType = InchiBondType.of(nativeAtom.bond_type[j]); |
| 741 |
InchiBondStereo bondStereo = InchiBondStereo.of(nativeAtom.bond_stereo[j]); |
| 742 |
inchiInput.addBond(new InchiBond(atom, neighbor, bondType, bondStereo)); |
| 743 |
} |
| 744 |
} |
| 745 |
seenAtoms[i] = true; |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
private static void nativeToJavaStereos(InchiInput inchiInput, tagINCHIStereo0D[] nativeStereos) { |
| 750 |
for (tagINCHIStereo0D nativeStereo : nativeStereos) { |
| 751 |
InchiAtom[] atoms = new InchiAtom[4]; |
| 752 |
//idxToAtom will give null for -1 input (implicit hydrogen) |
| 753 |
for (int i = 0; i < 4; i++) { |
| 754 |
int idx = nativeStereo.neighbor[i]; |
| 755 |
atoms[i] = idx >=0 ? inchiInput.getAtom(idx) : null; |
| 756 |
} |
| 757 |
|
| 758 |
InchiAtom centralAtom = nativeStereo.central_atom >=0 ? inchiInput.getAtom(nativeStereo.central_atom) : null; |
| 759 |
InchiStereoType stereoType = InchiStereoType.of(nativeStereo.type); |
| 760 |
InchiStereoParity parity = InchiStereoParity.of(nativeStereo.parity); |
| 761 |
|
| 762 |
inchiInput.addStereo(new InchiStereo(atoms, centralAtom, stereoType, parity)); |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
private static InchiStatus getInchiStatus(int ret) { |
| 767 |
switch (ret) { |
| 768 |
case tagRetValGetINCHI.inchi_Ret_OKAY:/* Success; no errors or warnings*/ |
| 769 |
return InchiStatus.SUCCESS; |
| 770 |
case tagRetValGetINCHI.inchi_Ret_EOF:/* no structural data has been provided */ |
| 771 |
case tagRetValGetINCHI.inchi_Ret_WARNING:/* Success; warning(s) issued*/ |
| 772 |
return InchiStatus.WARNING; |
| 773 |
case tagRetValGetINCHI.inchi_Ret_ERROR:/* Error: no InChI has been created */ |
| 774 |
case tagRetValGetINCHI.inchi_Ret_FATAL:/* Severe error: no InChI has been created (typically, memory allocation failure) */ |
| 775 |
case tagRetValGetINCHI.inchi_Ret_UNKNOWN:/* Unknown program error */ |
| 776 |
case tagRetValGetINCHI.inchi_Ret_BUSY:/* Previous call to InChI has not returned yet*/ |
| 777 |
return InchiStatus.ERROR; |
| 778 |
default: |
| 779 |
return InchiStatus.ERROR; |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
private static String toString(byte[] cstr) { |
| 784 |
StringBuilder sb = new StringBuilder(cstr.length); |
| 785 |
for (int i = 0; i < cstr.length; i++) { |
| 786 |
char ch = (char) cstr[i]; |
| 787 |
if (ch == '\0') { |
| 788 |
break; |
| 789 |
} |
| 790 |
sb.append(ch); |
| 791 |
} |
| 792 |
return sb.toString(); |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
/** |
| 797 |
* Returns the version of the wrapped InChI C library |
| 798 |
* @return Version number String |
| 799 |
*/ |
| 800 |
public static String getInchiLibraryVersion() { |
| 801 |
try(InputStream is = JnaInchi.class.getResourceAsStream("jnainchi_build.props")) { |
| 802 |
Properties props = new Properties(); |
| 803 |
props.load(is); |
| 804 |
return props.getProperty("inchi_version"); |
| 805 |
} |
| 806 |
catch (Exception e) { |
| 807 |
return null; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
/** |
| 812 |
* Returns the version of the JNA-InChI Java library |
| 813 |
* @return Version number String |
| 814 |
*/ |
| 815 |
public static String getJnaInchiVersion() { |
| 816 |
try(InputStream is = JnaInchi.class.getResourceAsStream("jnainchi_build.props")) { |
| 817 |
Properties props = new Properties(); |
| 818 |
props.load(is); |
| 819 |
return props.getProperty("jnainchi_version"); |
| 820 |
} |
| 821 |
catch (Exception e) { |
| 822 |
return null; |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
private static void checkLibrary() { |
| 827 |
if (libraryLoadingError != null) { |
| 828 |
throw new RuntimeException("Error loading InChI native code. Please check that the binaries for your platform (" + platform + ") have been included on the classpath.", libraryLoadingError); |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
} |