Branch data Line data Source code
1 : : /*
2 : : * SPDX-License-Identifier: MIT
3 : : * Copyright (c) 2024 IUPAC and InChI Trust
4 : : */
5 : :
6 : : #include <string.h>
7 : :
8 : : /*#define CHECK_WIN32_VC_HEAP*/
9 : :
10 : : #include "mode.h"
11 : :
12 : : #if ( READ_INCHI_STRING == 1 )
13 : : #include "ichitime.h"
14 : : #include "ichirvrs.h"
15 : : #include "ichicant.h"
16 : : #include "ichi_io.h"
17 : : #include "ichimake.h"
18 : :
19 : : #include "bcf_s.h"
20 : :
21 : : /****************************************************************************/
22 : 0 : int ForbidCarbonChargeEdges(BN_STRUCT* pBNS,
23 : : ALL_TC_GROUPS* pTCGroups,
24 : : EDGE_LIST* pCarbonChargeEdges,
25 : : int forbidden_edge_mask)
26 : : {
27 : : #define MAX_NUM_CARBON_CHARGE_EDGES 2
28 : : int nType, i, k, ret;
29 : : BNS_EDGE* pEdge;
30 [ # # ]: 0 : if ((ret = AllocEdgeList(pCarbonChargeEdges, MAX_NUM_CARBON_CHARGE_EDGES))) /* djb-rwth: addressing LLVM warning */
31 : : {
32 : 0 : goto exit_function;
33 : : }
34 : 0 : pCarbonChargeEdges->num_edges = 0;
35 [ # # ]: 0 : for (i = 0; i < MAX_NUM_CARBON_CHARGE_EDGES; i++)
36 : : {
37 [ # # # ]: 0 : switch (i)
38 : : {
39 : 0 : case 0:
40 : 0 : nType = TCG_Plus_C0;
41 : 0 : break;
42 : 0 : case 1:
43 : 0 : nType = TCG_Minus_C0;
44 : 0 : break;
45 : 0 : default:
46 : 0 : ret = RI_ERR_PROGR;
47 : 0 : goto exit_function;
48 : : }
49 [ # # ]: 0 : if ((k = pTCGroups->nGroup[nType]) >= 0)
50 : : {
51 : 0 : k = pTCGroups->pTCG[k].nForwardEdge;
52 [ # # ]: 0 : if (k > 0)
53 : : {
54 : 0 : pEdge = pBNS->edge + k;
55 [ # # ]: 0 : if (!(pEdge->forbidden & forbidden_edge_mask))
56 : : {
57 : 0 : pEdge->forbidden |= forbidden_edge_mask;
58 [ # # ]: 0 : if ((ret = AddToEdgeList(pCarbonChargeEdges, k, 0))) /* djb-rwth: addressing LLVM warning */
59 : : {
60 : 0 : goto exit_function;
61 : : }
62 : : }
63 : : }
64 : : else
65 : : {
66 : 0 : ret = RI_ERR_PROGR;
67 : 0 : goto exit_function;
68 : : }
69 : : }
70 : : }
71 : 0 : ret = pCarbonChargeEdges->num_edges;
72 : :
73 : 0 : exit_function:
74 : :
75 : 0 : return ret;
76 : : #undef MAX_NUM_CARBON_CHARGE_EDGES
77 : : }
78 : :
79 : :
80 : : /****************************************************************************/
81 : 0 : int ForbidNintrogenPlus2BondsInSmallRings(BN_STRUCT* pBNS,
82 : : inp_ATOM* at,
83 : : int num_at,
84 : : VAL_AT* pVA,
85 : : int min_ring_size,
86 : : ALL_TC_GROUPS* pTCGroups,
87 : : EDGE_LIST* pNplus2BondsEdges,
88 : : int forbidden_edge_mask)
89 : : {
90 : : int i, j, ret;
91 : : BNS_EDGE* e;
92 : :
93 : : /* djb-rwth: removing redundant code */
94 : : /* --- forbid edges that allow to make =N(+)= or #N(+)- in small ring */
95 [ # # ]: 0 : for (i = 0; i < num_at; i++)
96 : : {
97 [ # # ]: 0 : if (at[i].valence == 2 &&
98 [ # # # # ]: 0 : !at[i].num_H && !at[i].endpoint &&
99 [ # # ]: 0 : pVA[i].cNumValenceElectrons == 5 &&
100 [ # # ]: 0 : pVA[i].cPeriodicRowNumber == 1 &&
101 [ # # # # ]: 0 : !pVA[i].cMaxFlowToMetal && pVA[i].nCPlusGroupEdge > 0 &&
102 [ # # # # ]: 0 : pVA[i].cnListIndex > 0 && cnList[pVA[i].cnListIndex - 1].bits == cn_bits_MNP &&
103 [ # # # # ]: 0 : pVA[i].cMinRingSize && pVA[i].cMinRingSize <= min_ring_size)
104 : : {
105 : :
106 : 0 : e = pBNS->edge + (j = pVA[i].nCPlusGroupEdge - 1);
107 [ # # ]: 0 : if (!(e->forbidden & forbidden_edge_mask))
108 : : {
109 : 0 : e->forbidden |= forbidden_edge_mask;
110 [ # # ]: 0 : if ((ret = AddToEdgeList(pNplus2BondsEdges, j, 128))) /* djb-rwth: addressing LLVM warning */
111 : : {
112 : 0 : goto exit_function;
113 : : }
114 : : }
115 : : }
116 : : }
117 : 0 : ret = 0;
118 : :
119 : 0 : exit_function:
120 : :
121 : 0 : return ret;
122 : : }
123 : :
124 : :
125 : : /****************************************************************************
126 : : Problem: Formula in InChI from the reversed structure has
127 : : less H than in the input InChI
128 : : Solutions:
129 : :
130 : : (a) | |
131 : : -B(-)-NH-=..-=N(+)< => -B(-)-NH(+)=-..=-N<
132 : : | |
133 : :
134 : : (H is not removed from the ion pair)
135 : :
136 : : | |
137 : : (b) >N(+)=-=...-=N-NH => >N-=-...=-N(+)-NH
138 : : | |
139 : :
140 : : (charge from onium cannot be moved to remove H+)
141 : : ****************************************************************************/
142 : 0 : int FixLessHydrogenInFormula(BN_STRUCT* pBNS,
143 : : BN_DATA* pBD,
144 : : StrFromINChI* pStruct,
145 : : inp_ATOM* at,
146 : : inp_ATOM* at2,
147 : : inp_ATOM* atf,
148 : : VAL_AT* pVA,
149 : : ALL_TC_GROUPS* pTCGroups,
150 : : int* pnNumRunBNS,
151 : : int* pnTotalDelta, int forbidden_edge_mask)
152 : : {
153 : 0 : int iBPlus = NO_VERTEX, iNV = NO_VERTEX, iNH = NO_VERTEX, neigh;
154 : : EDGE_LIST NewlyFixedEdges;
155 : : int ret, i, j;
156 : 0 : int num_at = pStruct->num_atoms;
157 : 0 : int inv_forbidden_edge_mask = ~forbidden_edge_mask;
158 : : /* for RunBnsTestOnce */
159 : : Vertex vPathStart, vPathEnd;
160 : : int nPathLen, nDeltaH, nDeltaCharge, nNumVisitedAtoms;
161 : :
162 : 0 : AllocEdgeList(&NewlyFixedEdges, EDGE_LIST_CLEAR);
163 [ # # ]: 0 : if ((ret = AllocEdgeList(&NewlyFixedEdges, 2 * num_at))) /* djb-rwth: addressing LLVM warning */
164 : : {
165 : 0 : goto exit_function;
166 : : }
167 [ # # ]: 0 : for (i = 0; i < num_at; i++)
168 : : {
169 [ # # ]: 0 : if ((j = pVA[i].nCMinusGroupEdge - 1) >= 0)
170 : : {
171 [ # # ]: 0 : if ((ret = AddToEdgeList(&NewlyFixedEdges, j, 0))) /* djb-rwth: addressing LLVM warning */
172 : : {
173 : 0 : goto exit_function;
174 : : }
175 : 0 : pBNS->edge[j].forbidden |= forbidden_edge_mask;
176 : : }
177 [ # # ]: 0 : if ((j = pVA[i].nCPlusGroupEdge - 1) >= 0)
178 : : {
179 [ # # ]: 0 : if ((ret = AddToEdgeList(&NewlyFixedEdges, j, 0))) /* djb-rwth: addressing LLVM warning */
180 : : {
181 : 0 : goto exit_function;
182 : : }
183 : 0 : pBNS->edge[j].forbidden |= forbidden_edge_mask;
184 : : }
185 : : }
186 : : /* extra H has been removed; check non-tautomeric atoms */
187 [ # # ]: 0 : for (i = 0; i < num_at; i++)
188 : : {
189 [ # # # # ]: 0 : if (!at2[i].endpoint && !pVA[i].cMetal &&
190 [ # # # # ]: 0 : pVA[i].cNumValenceElectrons == 5 && pVA[i].cPeriodicRowNumber == 1 &&
191 [ # # ]: 0 : at2[i].num_H == atf[i].num_H + 1)
192 : : {
193 : : /* H was removed from N */
194 : 0 : iNH = i;
195 : 0 : break;
196 : : }
197 : : }
198 [ # # # # ]: 0 : if (0 <= iNH && iNH < num_at)
199 : : {
200 : : /* check neighbors for | |
201 : : (a) -B(+)- or (b) =N-
202 : : | |
203 : : */
204 [ # # ]: 0 : for (j = 0; j < at2[i].valence; j++)
205 : : {
206 : 0 : neigh = at2[iNH].neighbor[j];
207 [ # # ]: 0 : if (at2[neigh].valence == 4)
208 : : {
209 [ # # # # ]: 0 : if (at2[neigh].charge == -1 && at2[neigh].chem_bonds_valence == 4 &&
210 [ # # # # ]: 0 : !at2[neigh].radical && !at[neigh].num_H)
211 : : {
212 : 0 : iBPlus = neigh;
213 : : }
214 : : }
215 : : }
216 : : }
217 [ # # # # ]: 0 : if (0 <= iNH && iNH < num_at)
218 : : {
219 : : int bond_type_at2;
220 : : int bond_type_atf;
221 : : /* djb-rwth: removing redundant variables */
222 : 0 : int delta = -1, nxt = iNH, prv = NO_VERTEX, nxt_is_NPlus;
223 : : /* the changed bond to the dehydrogenated atom H should have greater order */
224 : : /* delta = (new bond order in atf[]) - (restored bond order in at2[]) */
225 : 0 : nxt_is_NPlus = 0;
226 : : do
227 : : {
228 : 0 : i = nxt;
229 : 0 : nxt = NO_VERTEX;
230 : 0 : delta = -delta;
231 [ # # ]: 0 : for (j = 0; j < at2[i].valence; j++)
232 : : {
233 : 0 : bond_type_at2 = at2[i].bond_type[j] & BOND_TYPE_MASK; /* restored bond */
234 : 0 : bond_type_atf = atf[i].bond_type[j] & BOND_TYPE_MASK; /* normalized bond */
235 : 0 : nxt_is_NPlus = 0;
236 [ # # # # ]: 0 : if ((bond_type_atf - bond_type_at2 == delta || bond_type_atf == BOND_ALT12NS) &&
237 [ # # # # ]: 0 : BOND_TYPE_SINGLE <= bond_type_at2 + delta && bond_type_at2 + delta <= BOND_TYPE_TRIPLE &&
238 [ # # ]: 0 : !at2[(int)at2[i].neighbor[j]].cFlags)
239 : : {
240 : 0 : prv = i;
241 : 0 : nxt = at2[i].neighbor[j];
242 [ # # ]: 0 : nxt_is_NPlus = at2[nxt].charge == 1 && atf[nxt].charge == 0 &&
243 [ # # # # : 0 : pVA[nxt].cNumValenceElectrons == 5 && pVA[nxt].cPeriodicRowNumber == 1;
# # ]
244 : 0 : at2[i].cFlags |= 1; /* avoid cycling */
245 : : /* djb-rwth: removing redundant code */
246 [ # # # # : 0 : if (delta == -1 && at2[prv].valence == 4 && at2[prv].chem_bonds_valence == 5 &&
# # ]
247 [ # # # # : 0 : !at2[prv].charge && !at2[prv].radical && pVA[prv].cNumValenceElectrons == 5 &&
# # ]
248 [ # # ]: 0 : pVA[prv].nCPlusGroupEdge > 0)
249 : : {
250 : 0 : iNV = prv;
251 : : }
252 [ # # ]: 0 : if (at2[nxt].charge != atf[nxt].charge)
253 : : {
254 [ # # # # ]: 0 : if ((at2[nxt].charge == 1 || atf[nxt].charge == 1) &&
255 [ # # ]: 0 : pVA[nxt].nCPlusGroupEdge > 0)
256 : : {
257 : 0 : pBNS->edge[pVA[nxt].nCPlusGroupEdge - 1].forbidden &= inv_forbidden_edge_mask;
258 : : }
259 [ # # # # ]: 0 : if ((at2[nxt].charge == -1 || atf[nxt].charge == -1) &&
260 [ # # ]: 0 : pVA[nxt].nCMinusGroupEdge > 0)
261 : : {
262 : 0 : pBNS->edge[pVA[nxt].nCMinusGroupEdge - 1].forbidden &= inv_forbidden_edge_mask;
263 : : }
264 : : }
265 : 0 : break; /* found */
266 : : }
267 : : }
268 [ # # # # : 0 : } while (nxt >= 0 && !(nxt_is_NPlus && delta == -1));
# # ]
269 [ # # ]: 0 : for (i = 0; i < num_at; i++)
270 : : {
271 : 0 : at2[i].cFlags = 0;
272 : : }
273 [ # # # # : 0 : if (nxt >= 0 && nxt_is_NPlus && delta == -1)
# # ]
274 : : {
275 : : /* a simple alt path from NH-= to =N(+) has been found */
276 [ # # # # ]: 0 : if (iBPlus || iNV)
277 : : {
278 : : /* move (+) charge from N(+) to iNV or, if iBPlus, then to iNH */
279 [ # # # # : 0 : if ((iNV >= 0 && (j = pVA[iNV].nCPlusGroupEdge - 1) > 0 && pBNS->edge[j].flow > 0) ||
# # # # ]
280 [ # # # # ]: 0 : (iNH >= 0 && (j = pVA[iNH].nCPlusGroupEdge - 1) > 0 && pBNS->edge[j].flow > 0)) /* djb-rwth: addressing LLVM warnings */
281 : : {
282 : : int ieFlower;
283 : 0 : BNS_EDGE* pe = pBNS->edge + j, * peFlower = NULL;
284 : 0 : Vertex v1 = pe->neighbor1;
285 : 0 : Vertex v2 = v1 ^ pe->neighbor12;
286 : 0 : BNS_VERTEX* pv1 = pBNS->vert + v1;
287 : 0 : BNS_VERTEX* pv2 = pBNS->vert + v2;
288 : :
289 : 0 : delta = 1;
290 : : /* prevent conversion of >N(+)= into N(V) neutral */
291 : 0 : ieFlower = GetChargeFlowerUpperEdge(pBNS, pVA, pVA[nxt].nCPlusGroupEdge - 1);
292 [ # # ]: 0 : if (ieFlower >= 0)
293 : : {
294 : 0 : peFlower = pBNS->edge + ieFlower;
295 [ # # ]: 0 : if (peFlower->flow == delta)
296 : : {
297 : 0 : peFlower->forbidden |= forbidden_edge_mask;
298 [ # # ]: 0 : if ((ret = AddToEdgeList(&NewlyFixedEdges, ieFlower, 0))) /* djb-rwth: addressing LLVM warning */
299 : : {
300 : 0 : goto exit_function;
301 : : }
302 : : }
303 : : }
304 : 0 : pe->forbidden |= forbidden_edge_mask;
305 : 0 : pe->flow -= delta;
306 : 0 : pv1->st_edge.flow -= delta;
307 : 0 : pv2->st_edge.flow -= delta;
308 : 0 : pBNS->tot_st_flow -= 2 * delta;
309 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
310 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
311 [ # # ]: 0 : if (ret < 0)
312 : : {
313 : 0 : goto exit_function;
314 : : }
315 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
316 [ # # # # ]: 0 : (vPathEnd == v2 && vPathStart == v1)) &&
317 [ # # ]: 0 : nDeltaCharge <= 0 /* charge moving to this atom disappers*/) /* djb-rwth: addressing LLVM warnings */
318 : : {
319 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
320 : 0 : (*pnNumRunBNS)++;
321 [ # # ]: 0 : if (ret < 0)
322 : : {
323 : 0 : goto exit_function;
324 : : }
325 : : else
326 [ # # ]: 0 : if (ret == 1)
327 : : {
328 : 0 : *pnTotalDelta += ret;
329 : : }
330 : : else
331 : : {
332 : 0 : ret = RI_ERR_PROGR;
333 : 0 : goto exit_function;
334 : : }
335 : : }
336 : : else
337 : : {
338 : 0 : ret = 0;
339 : 0 : pe->flow += delta;
340 : 0 : pv1->st_edge.flow += delta;
341 : 0 : pv2->st_edge.flow += delta;
342 : 0 : pBNS->tot_st_flow += 2 * delta;
343 : : }
344 : : }
345 : : }
346 : : }
347 : : }
348 : :
349 : 0 : exit_function:
350 : : /* remove bond fixation */
351 : 0 : RemoveForbiddenEdgeMask(pBNS, &NewlyFixedEdges, forbidden_edge_mask);
352 : 0 : AllocEdgeList(&NewlyFixedEdges, EDGE_LIST_FREE);
353 : :
354 : 0 : return ret;
355 : : }
356 : :
357 : :
358 : : /****************************************************************************
359 : :
360 : : X=Y-O(-) => X(-)-Y=O
361 : :
362 : : ****************************************************************************/
363 : 0 : int FixMoreHydrogenInFormula(BN_STRUCT* pBNS,
364 : : BN_DATA* pBD,
365 : : StrFromINChI* pStruct,
366 : : inp_ATOM* at,
367 : : inp_ATOM* at2,
368 : : inp_ATOM* atf,
369 : : VAL_AT* pVA,
370 : : ALL_TC_GROUPS* pTCGroups,
371 : : int* pnNumRunBNS,
372 : : int* pnTotalDelta,
373 : : int forbidden_edge_mask)
374 : : {
375 : 0 : int iNH = NO_VERTEX, neigh, neigh2;
376 : : EDGE_LIST NewlyFixedEdges;
377 : 0 : int ret, i, j, k, k2 = 0, delta;
378 : 0 : int num_at = pStruct->num_atoms;
379 : 0 : int inv_forbidden_edge_mask = ~forbidden_edge_mask;
380 : : Vertex v1, v2;
381 : : /* for RunBnsTestOnce */
382 : : Vertex vPathStart, vPathEnd;
383 : : int nPathLen, nDeltaH, nDeltaCharge, nNumVisitedAtoms;
384 : : BNS_EDGE* pe, * pe2;
385 : :
386 : 0 : AllocEdgeList(&NewlyFixedEdges, EDGE_LIST_CLEAR);
387 [ # # ]: 0 : if ((ret = AllocEdgeList(&NewlyFixedEdges, 2 * num_at))) /* djb-rwth: addressing LLVM warning */
388 : : {
389 : 0 : goto exit_function;
390 : : }
391 : : /* fix all charges */
392 [ # # ]: 0 : for (i = 0; i < num_at; i++)
393 : : {
394 [ # # ]: 0 : if ((j = pVA[i].nCMinusGroupEdge - 1) >= 0)
395 : : {
396 [ # # ]: 0 : if ((ret = AddToEdgeList(&NewlyFixedEdges, j, 0))) /* djb-rwth: addressing LLVM warning */
397 : : {
398 : 0 : goto exit_function;
399 : : }
400 : 0 : pBNS->edge[j].forbidden |= forbidden_edge_mask;
401 : : }
402 [ # # ]: 0 : if ((j = pVA[i].nCPlusGroupEdge - 1) >= 0)
403 : : {
404 [ # # ]: 0 : if ((ret = AddToEdgeList(&NewlyFixedEdges, j, 0))) /* djb-rwth: addressing LLVM warning */
405 : : {
406 : 0 : goto exit_function;
407 : : }
408 : 0 : pBNS->edge[j].forbidden |= forbidden_edge_mask;
409 : : }
410 : : }
411 : :
412 : : /* H(+) has been added to -O(-); check non-tautomeric atoms */
413 [ # # ]: 0 : for (i = 0; i < num_at; i++)
414 : : {
415 : 0 : neigh = at2[i].neighbor[0]; /* djb-rwth: avoiding unsequenced modification and access to neigh */
416 [ # # # # : 0 : if (!(pStruct->bMobileH ? at2[i].endpoint : pStruct->endpoint[i]) && !pVA[i].cMetal &&
# # ]
417 [ # # ]: 0 : at2[i].num_H + 1 == atf[i].num_H && /* normalization added H ??? What would happen in Fixed-H case?*/
418 [ # # ]: 0 : (k = pVA[i].nCMinusGroupEdge - 1) >= 0 &&
419 [ # # ]: 0 : pBNS->edge[k].flow == 1 && /* atom had (-) charge before preprocessing */
420 [ # # # # ]: 0 : at2[i].charge == -1 && atf[i].charge == 0 && /* and has no charge after preprocessing */
421 [ # # # # ]: 0 : at2[i].valence == 1 && at2[i].chem_bonds_valence == 1 && /* connected by a single bond */
422 [ # # ]: 0 : pVA[i].cNumValenceElectrons == 6 && /* atom is O, S, Se, Te */
423 [ # # ]: 0 : at2[neigh].chem_bonds_valence > at2[neigh].valence
424 : : /* atom's single neighbor has multiple bond(s)*/
425 : : )
426 : : {
427 : : /* H(+) was added to O in Y=X-O(-), where X is the only neighbor of O, X=neigh, Y=neigh2 */
428 : 0 : iNH = i;
429 [ # # ]: 0 : for (j = 0; j < at2[neigh].valence; j++)
430 : : {
431 : 0 : neigh2 = at2[neigh].neighbor[j];
432 [ # # # # ]: 0 : if (neigh2 != iNH && !at2[neigh2].endpoint &&
433 [ # # ]: 0 : !pBNS->edge[(int)pBNS->vert[neigh].iedge[j]].forbidden &&
434 [ # # ]: 0 : 4 <= pVA[neigh2].cNumValenceElectrons &&
435 [ # # ]: 0 : pVA[neigh2].cNumValenceElectrons <= 5 && /* neig2 is C or N */
436 [ # # ]: 0 : (k2 = pVA[neigh2].nCMinusGroupEdge - 1) >= 0 &&
437 [ # # ]: 0 : !pBNS->edge[k2].flow /* negative charge may be moved to neigh2 */)
438 : : {
439 : 0 : break;
440 : : }
441 : : }
442 [ # # ]: 0 : if (j < at2[neigh].valence)
443 : : {
444 : 0 : delta = 1;
445 : 0 : pe = pBNS->edge + k; /* -O(-) negative charge edge; flow = 1 */
446 : 0 : pe2 = pBNS->edge + k2; /* X charge edge; flow = 0 */
447 : 0 : v1 = pe->neighbor1;
448 : 0 : v2 = pe->neighbor12 ^ v1;
449 : 0 : pe->flow -= delta;
450 : 0 : pBNS->vert[v1].st_edge.flow -= delta;
451 : 0 : pBNS->vert[v2].st_edge.flow -= delta;
452 : 0 : pBNS->tot_st_flow -= 2 * delta;
453 : 0 : pe2->forbidden &= inv_forbidden_edge_mask; /* allow the charge to move */
454 : :
455 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
456 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
457 [ # # ]: 0 : if (ret < 0)
458 : : {
459 : 0 : goto exit_function;
460 : : }
461 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
462 [ # # # # ]: 0 : (vPathEnd == v2 && vPathStart == v1)) &&
463 [ # # ]: 0 : nDeltaCharge <= 1) /* djb-rwth: addressing LLVM warnings */
464 : : {
465 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
466 : 0 : (*pnNumRunBNS)++;
467 [ # # ]: 0 : if (ret < 0)
468 : : {
469 : 0 : goto exit_function;
470 : : }
471 : : else
472 [ # # ]: 0 : if (ret)
473 : : {
474 : 0 : *pnTotalDelta += ret;
475 : : }
476 : : else
477 : : {
478 : 0 : ret = RI_ERR_PROGR;
479 : : }
480 : 0 : break;
481 : : }
482 : : else
483 : : {
484 : : /* the attempt has failed; restore the flow */
485 : 0 : ret = 0;
486 : 0 : pe->flow += delta;
487 : 0 : pBNS->vert[v1].st_edge.flow += delta;
488 : 0 : pBNS->vert[v2].st_edge.flow += delta;
489 : 0 : pBNS->tot_st_flow += 2 * delta;
490 : : }
491 : : }
492 : : }
493 : : }
494 : :
495 : 0 : exit_function:
496 : : /* remove bond fixation */
497 : 0 : RemoveForbiddenEdgeMask(pBNS, &NewlyFixedEdges, forbidden_edge_mask);
498 : 0 : AllocEdgeList(&NewlyFixedEdges, EDGE_LIST_FREE);
499 : :
500 : 0 : return ret;
501 : : }
502 : :
503 : :
504 : : #if ( FIX_ADD_PROTON_FOR_ADP == 1 )
505 : : /****************************************************************************/
506 : : int FixAddProtonForADP(BN_STRUCT* pBNS,
507 : : BN_DATA* pBD,
508 : : StrFromINChI* pStruct,
509 : : inp_ATOM* at,
510 : : inp_ATOM* at2,
511 : : inp_ATOM* atf,
512 : : VAL_AT* pVA,
513 : : ALL_TC_GROUPS* pTCGroups,
514 : : ICR* picr,
515 : : int* pnNumRunBNS,
516 : : int* pnTotalDelta,
517 : : int forbidden_edge_mask)
518 : : {
519 : : int iBPlus = NO_VERTEX, iNV = NO_VERTEX, iNH = NO_VERTEX, neigh, neigh2;
520 : : EDGE_LIST NewlyFixedEdges;
521 : : int ret, i, j, k, k2, delta;
522 : : int num_at = pStruct->num_atoms;
523 : : int inv_forbidden_edge_mask = ~forbidden_edge_mask;
524 : : Vertex v1, v2;
525 : : /* for RunBnsTestOnce */
526 : : Vertex vPathStart, vPathEnd;
527 : : int nPathLen, nDeltaH, nDeltaCharge, nNumVisitedAtoms;
528 : : BNS_EDGE* pe, * pe2;
529 : :
530 : : ret = 0;
531 : : /*
532 : : AllocEdgeList( &NewlyFixedEdges, EDGE_LIST_CLEAR );
533 : :
534 : : for ( i = 0; i < num_at; i ++ ) {
535 : : if ( at2[i].radical == RADICAL_DOUBLET && at2[i].endpoint ) {
536 : : pStruct->bExtract |= EXTRACT_STRUCT_NUMBER;
537 : : ret = 1;
538 : : break;
539 : : }
540 : : }
541 : : */
542 : : return ret;
543 : : }
544 : : #endif
545 : :
546 : :
547 : : /****************************************************************************
548 : : OH OH
549 : : / /
550 : : -NH => -NH(+) to eliminate false tautomerism.
551 : : \\ \ S(IV) or N(V) or P(V) may be a centerpoint
552 : : O O(-)
553 : : ****************************************************************************/
554 : 0 : int FixRemoveExtraTautEndpoints(BN_STRUCT* pBNS,
555 : : BN_DATA* pBD,
556 : : StrFromINChI* pStruct,
557 : : inp_ATOM* at,
558 : : inp_ATOM* at2,
559 : : inp_ATOM* atf,
560 : : inp_ATOM* atn,
561 : : VAL_AT* pVA,
562 : : ALL_TC_GROUPS* pTCGroups, ICR* picr,
563 : : int* pnNumRunBNS,
564 : : int* pnTotalDelta,
565 : : int forbidden_edge_mask)
566 : : {
567 : : EDGE_LIST NewlyFixedEdges;
568 : : int ret, i, j, k, delta, centerpoint, endpoint1, endpoint2;
569 : 0 : int num_at = pStruct->num_atoms;
570 : 0 : int inv_forbidden_edge_mask = ~forbidden_edge_mask;
571 : : Vertex v1, v2;
572 : : /* for RunBnsTestOnce */
573 : : Vertex vPathStart, vPathEnd;
574 : : int nPathLen, nDeltaH, nDeltaCharge, nNumVisitedAtoms;
575 : : BNS_EDGE* pe, * pe2;
576 : :
577 : 0 : ret = 0; /* djb-rwth: ignoring LLVM warning: value might be returned */
578 : :
579 : 0 : AllocEdgeList(&NewlyFixedEdges, EDGE_LIST_CLEAR);
580 [ # # ]: 0 : if ((ret = AllocEdgeList(&NewlyFixedEdges, 2 * num_at))) /* djb-rwth: addressing LLVM warning */
581 : : {
582 : 0 : goto exit_function;
583 : : }
584 : : /* fix all charges */
585 [ # # ]: 0 : for (i = 0; i < num_at; i++)
586 : : {
587 [ # # ]: 0 : if ((j = pVA[i].nCMinusGroupEdge - 1) >= 0)
588 : : {
589 [ # # ]: 0 : if ((ret = AddToEdgeList(&NewlyFixedEdges, j, 0))) /* djb-rwth: addressing LLVM warning */
590 : : {
591 : 0 : goto exit_function;
592 : : }
593 : 0 : pBNS->edge[j].forbidden |= forbidden_edge_mask;
594 : : }
595 [ # # ]: 0 : if ((j = pVA[i].nCPlusGroupEdge - 1) >= 0)
596 : : {
597 [ # # ]: 0 : if ((ret = AddToEdgeList(&NewlyFixedEdges, j, 0))) /* djb-rwth: addressing LLVM warning */
598 : : {
599 : 0 : goto exit_function;
600 : : }
601 : 0 : pBNS->edge[j].forbidden |= forbidden_edge_mask;
602 : : }
603 : : }
604 : :
605 [ # # ]: 0 : for (i = 0; i < picr->num_endp_in1_only; i++)
606 : : {
607 : 0 : endpoint1 = picr->endp_in1_only[i] - 1;
608 [ # # ]: 0 : if (at2[endpoint1].valence == at2[endpoint1].chem_bonds_valence ||
609 [ # # ]: 0 : pVA[endpoint1].nCMinusGroupEdge <= 0)
610 : : {
611 : 0 : continue;
612 : : }
613 : : /* find centerpoint */
614 [ # # ]: 0 : for (j = 0; j < at2[endpoint1].valence; j++)
615 : : {
616 [ # # ]: 0 : if (BOND_TYPE_DOUBLE == (BOND_TYPE_MASK & at2[endpoint1].bond_type[j]))
617 : : {
618 : 0 : centerpoint = at2[endpoint1].neighbor[j];
619 [ # # # # : 0 : if (at2[centerpoint].charge || pVA[centerpoint].nCPlusGroupEdge <= 0 ||
# # ]
620 : 0 : !is_centerpoint_elem(at2[centerpoint].el_number))
621 : : {
622 : 0 : continue;
623 : : }
624 : : /* -- the centerpoint as depicted has no ChargeStruct flower ---
625 : : m = GetChargeFlowerUpperEdge( pBNS, pVA, pVA[centerpoint].nCPlusGroupEdge-1 );
626 : : if ( m < 0 || pBNS->edge[m].flow ) {
627 : : continue;
628 : : }
629 : : */
630 : : /* find 2nd endpoint */
631 [ # # ]: 0 : for (k = 0; k < at2[centerpoint].valence; k++)
632 : : {
633 [ # # ]: 0 : if (BOND_TYPE_SINGLE != (BOND_TYPE_MASK & at2[centerpoint].bond_type[k]))
634 : : {
635 : 0 : continue;
636 : : }
637 : 0 : endpoint2 = at2[centerpoint].neighbor[k];
638 [ # # # # ]: 0 : if (!at2[endpoint2].endpoint && atn[endpoint2].endpoint)
639 : : {
640 : 0 : break;
641 : : }
642 : : }
643 [ # # ]: 0 : if (k == at2[centerpoint].valence)
644 : : {
645 : 0 : continue;
646 : : }
647 : : /* the centerpoint and two extra endpoints have been found */
648 : 0 : pe = pBNS->edge + pVA[centerpoint].nCPlusGroupEdge - 1;
649 [ # # ]: 0 : if (!pe->flow)
650 : : {
651 : 0 : continue;
652 : : }
653 : 0 : pe2 = pBNS->edge + pVA[endpoint1].nCMinusGroupEdge - 1;
654 [ # # ]: 0 : if (pe2->flow)
655 : : {
656 : 0 : continue;
657 : : }
658 : 0 : delta = 1;
659 : 0 : v1 = pe->neighbor1;
660 : 0 : v2 = pe->neighbor12 ^ v1;
661 : 0 : pe->flow -= delta;
662 : 0 : pBNS->vert[v1].st_edge.flow -= delta;
663 : 0 : pBNS->vert[v2].st_edge.flow -= delta;
664 : 0 : pBNS->tot_st_flow -= 2 * delta;
665 : 0 : pe2->forbidden &= inv_forbidden_edge_mask; /* allow the charge to move */
666 : :
667 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
668 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
669 [ # # ]: 0 : if (ret < 0)
670 : : {
671 : 0 : goto exit_function;
672 : : }
673 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
674 [ # # # # ]: 0 : (vPathEnd == v2 && vPathStart == v1)) &&
675 [ # # ]: 0 : nDeltaCharge <= 1) /* djb-rwth: addressing LLVM warnings */
676 : : {
677 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
678 : 0 : (*pnNumRunBNS)++;
679 [ # # ]: 0 : if (ret < 0)
680 : : {
681 : 0 : goto exit_function;
682 : : }
683 : : else
684 : : {
685 [ # # ]: 0 : if (ret)
686 : : {
687 : 0 : *pnTotalDelta += ret;
688 : : }
689 : : else
690 : : {
691 : 0 : ret = RI_ERR_PROGR;
692 : : }
693 : : }
694 : 0 : goto exit_function;
695 : : }
696 : : else
697 : : {
698 : 0 : ret = 0;
699 : 0 : pe->flow += delta;
700 : 0 : pBNS->vert[v1].st_edge.flow += delta;
701 : 0 : pBNS->vert[v2].st_edge.flow += delta;
702 : 0 : pBNS->tot_st_flow += 2 * delta;
703 : 0 : pe2->forbidden |= forbidden_edge_mask;
704 : : }
705 : : }
706 : : }
707 : : }
708 : :
709 : 0 : exit_function:
710 : : /* remove bond fixation */
711 : 0 : RemoveForbiddenEdgeMask(pBNS, &NewlyFixedEdges, forbidden_edge_mask);
712 : 0 : AllocEdgeList(&NewlyFixedEdges, EDGE_LIST_FREE);
713 : :
714 : 0 : return ret;
715 : : }
716 : :
717 : :
718 : : /****************************************************************************/
719 : 0 : int FillOutExtraFixedHDataRestr(StrFromINChI* pStruct)
720 : : {
721 : 0 : int i, j, k, len, ret = 0;
722 : : AT_NUMB* pNum;
723 [ # # ]: 0 : for (i = 0; i < TAUT_NUM; i++)
724 : : {
725 [ # # ]: 0 : if (pStruct->pOneINChI_Aux[i])
726 : : {
727 : 0 : pNum = (pStruct->pOneINChI_Aux[i]->nIsotopicOrigAtNosInCanonOrd &&
728 [ # # ]: 0 : pStruct->pOneINChI_Aux[i]->nIsotopicOrigAtNosInCanonOrd[0]) ?
729 [ # # ]: 0 : pStruct->pOneINChI_Aux[i]->nIsotopicOrigAtNosInCanonOrd :
730 : 0 : (pStruct->pOneINChI_Aux[i]->nOrigAtNosInCanonOrd &&
731 [ # # ]: 0 : pStruct->pOneINChI_Aux[i]->nOrigAtNosInCanonOrd[0]) ?
732 [ # # ]: 0 : pStruct->pOneINChI_Aux[i]->nOrigAtNosInCanonOrd : NULL;
733 : : }
734 : : else
735 : : {
736 : 0 : pNum = NULL;
737 : : }
738 [ # # ]: 0 : if (pNum)
739 : : {
740 : 0 : len = pStruct->num_atoms * sizeof(pStruct->nCanon2Atno[0][0]);
741 [ # # ]: 0 : if ((!pStruct->nCanon2Atno[i] &&
742 [ # # ]: 0 : !(pStruct->nCanon2Atno[i] = (AT_NUMB*)inchi_malloc(len))) ||
743 [ # # ]: 0 : (!pStruct->nAtno2Canon[i] &&
744 [ # # ]: 0 : !(pStruct->nAtno2Canon[i] = (AT_NUMB*)inchi_malloc(len)))) /* djb-rwth: addressing LLVM warnings */
745 : : {
746 : 0 : ret = RI_ERR_ALLOC;
747 : 0 : goto exit_function;
748 : : }
749 : :
750 : : INCHI_HEAPCHK
751 : :
752 : 0 : memcpy(pStruct->nCanon2Atno[i], pNum, len); /* ??? the next for(...) fills it out */
753 : :
754 : : INCHI_HEAPCHK
755 : :
756 [ # # ]: 0 : for (j = 0; j < pStruct->num_atoms; j++)
757 : : {
758 : 0 : k = pNum[j] - 1; /* atom number */
759 : 0 : pStruct->nCanon2Atno[i][j] = (AT_NUMB)k;
760 : 0 : pStruct->nAtno2Canon[i][k] = (AT_NUMB)j;
761 : : INCHI_HEAPCHK
762 : : }
763 : : }
764 : : else
765 : : {
766 [ # # ]: 0 : if (!i)
767 : : {
768 : 0 : ret = RI_ERR_PROGR;
769 : 0 : goto exit_function;
770 : : }
771 : : else
772 : : {
773 [ # # ]: 0 : if (pStruct->nCanon2Atno[i])
774 : : {
775 [ # # ]: 0 : inchi_free(pStruct->nCanon2Atno[i]);
776 : 0 : pStruct->nCanon2Atno[i] = NULL;
777 : : }
778 : : INCHI_HEAPCHK
779 [ # # ]: 0 : if (pStruct->nAtno2Canon[i])
780 : : {
781 [ # # ]: 0 : inchi_free(pStruct->nAtno2Canon[i]);
782 : 0 : pStruct->nAtno2Canon[i] = NULL;
783 : : }
784 : : INCHI_HEAPCHK
785 : : }
786 : : }
787 : : }
788 : :
789 : 0 : exit_function:
790 : :
791 : 0 : return ret;
792 : : }
793 : :
794 : :
795 : : /****************************************************************************/
796 : 0 : int FillOutExtraFixedHDataInChI(StrFromINChI* pStruct, INChI* pInChI[])
797 : : {
798 : 0 : int ret = 0;
799 : : /*--- allocate memory for Mobile/Fixed-H data from the input InChI ---*/
800 [ # # ]: 0 : if (NULL == pStruct->endpoint)
801 : : {
802 : 0 : pStruct->endpoint = (AT_NUMB*)inchi_calloc(pStruct->num_atoms, sizeof(pStruct->endpoint[0]));
803 : : }
804 : : else
805 : : {
806 : 0 : memset(pStruct->endpoint, 0, pStruct->num_atoms * sizeof(pStruct->endpoint[0])); /* djb-rwth: memset_s C11/Annex K variant? */
807 : : }
808 [ # # ]: 0 : if (NULL == pStruct->fixed_H)
809 : : {
810 : 0 : pStruct->fixed_H = (S_CHAR*)inchi_malloc(pStruct->num_atoms * sizeof(pStruct->fixed_H[0]));
811 : : }
812 [ # # # # ]: 0 : if (!pStruct->endpoint || !pStruct->fixed_H)
813 : : {
814 : 0 : ret = RI_ERR_ALLOC;
815 : 0 : goto exit_function;
816 : : }
817 : : /*--- fill out Mobile/Fixed-H data from the input InChI ---*/
818 : 0 : GetTgroupInfoFromInChI(&pStruct->ti, NULL, pStruct->endpoint, pInChI[1]);
819 [ # # ]: 0 : if (pInChI[0]->nNum_H_fixed)
820 : : {
821 : 0 : memcpy(pStruct->fixed_H, pInChI[0]->nNum_H_fixed, pStruct->num_atoms * sizeof(pStruct->fixed_H[0]));
822 : : }
823 : : else
824 : : {
825 : 0 : memset(pStruct->fixed_H, 0, pStruct->num_atoms * sizeof(pStruct->fixed_H[0])); /* djb-rwth: memset_s C11/Annex K variant? */
826 : : }
827 : :
828 : 0 : exit_function:
829 : :
830 : 0 : return ret;
831 : : }
832 : :
833 : :
834 : : /****************************************************************************/
835 : 0 : int FillOutCMP2FHINCHI(StrFromINChI* pStruct,
836 : : inp_ATOM* at2,
837 : : VAL_AT* pVA,
838 : : INChI* pInChI[],
839 : : CMP2FHINCHI* pc2i)
840 : : {
841 : 0 : int ret = 0, i, j;
842 [ # # # # : 0 : int bFixHRevrsExists = pInChI[1] && pInChI[1]->nNumberOfAtoms > 0 && !pInChI[1]->bDeleted;
# # ]
843 : 0 : inp_ATOM* at_Mobile_H_Revrs = (pStruct->pOne_norm_data[1] &&
844 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at) ? pStruct->pOne_norm_data[1]->at : NULL;
845 : 0 : S_CHAR* num_Fixed_H_Revrs = pStruct->pOneINChI[0]->nNum_H_fixed ? pStruct->pOneINChI[0]->nNum_H_fixed : NULL;
846 : : /* atom number in structure that produced original InChI is atom number in all inp_ATOM *atoms */
847 : : /* atom number in structure that produced restored InChI is in nAtomRevrs[]: */
848 : 0 : AT_NUMB* nAtno2CanonRevrs = pStruct->nAtno2Canon[0];
849 [ # # # # ]: 0 : S_CHAR* pnMobHInChI = (pInChI[1] && pInChI[1]->nNum_H) ? pInChI[1]->nNum_H :
850 [ # # # # ]: 0 : (pInChI[0] && pInChI[0]->nNum_H) ? pInChI[0]->nNum_H : NULL;
851 [ # # ]: 0 : S_CHAR* pnMobHRevrs = (pStruct->pOneINChI[1] && pStruct->pOneINChI[1]->nNum_H) ?
852 [ # # ]: 0 : pStruct->pOneINChI[1]->nNum_H :
853 [ # # ]: 0 : (pStruct->pOneINChI[0] && pStruct->pOneINChI[0]->nNum_H) ?
854 [ # # ]: 0 : pStruct->pOneINChI[0]->nNum_H : NULL;
855 : : int nNumTgHInChI, nNumTgMInChI, nNumTgHRevrs, nNumTgMRevrs;
856 : 0 : memset(pc2i, 0, sizeof(*pc2i)); /* djb-rwth: memset_s C11/Annex K variant? */
857 : 0 : pc2i->nNumTgInChI = pStruct->ti.num_t_groups;
858 : 0 : pc2i->nNumTgRevrs = pStruct->One_ti.num_t_groups;
859 : 0 : pc2i->bHasDifference |= pc2i->nNumTgInChI != pc2i->nNumTgRevrs;
860 : :
861 : 0 : pc2i->nNumRemHInChI = pStruct->nNumRemovedProtonsMobHInChI;
862 : 0 : pc2i->nNumRemHRevrs = pStruct->One_ti.tni.nNumRemovedProtons;
863 : 0 : pc2i->bHasDifference |= pc2i->nNumRemHInChI != pc2i->nNumRemHRevrs;
864 : :
865 : 0 : pc2i->bFixedHLayerExistsRevrs = bFixHRevrsExists;
866 : 0 : pc2i->bHasDifference |= !bFixHRevrsExists;
867 : :
868 [ # # # # ]: 0 : for (i = 0; i < pStruct->ti.num_t_groups && i < pStruct->One_ti.num_t_groups; i++)
869 : : {
870 : 0 : nNumTgHInChI = pStruct->ti.t_group[i].num[0] - pStruct->ti.t_group[i].num[1];
871 : 0 : nNumTgMInChI = pStruct->ti.t_group[i].num[1];
872 : 0 : nNumTgHRevrs = pStruct->One_ti.t_group[i].num[0] - pStruct->One_ti.t_group[i].num[1];
873 : 0 : nNumTgMRevrs = pStruct->One_ti.t_group[i].num[1];
874 : :
875 : 0 : pc2i->bHasDifference |= nNumTgHInChI != nNumTgHRevrs;
876 : 0 : pc2i->bHasDifference |= nNumTgMInChI != nNumTgMRevrs;
877 : :
878 : 0 : if (pStruct->ti.t_group[i].nNumEndpoints ==
879 [ # # ]: 0 : pStruct->One_ti.t_group[i].nNumEndpoints)
880 : : {
881 : :
882 [ # # ]: 0 : if (nNumTgHInChI != nNumTgHRevrs)
883 : : {
884 : 0 : pc2i->nNumTgDiffH++;
885 : : }
886 [ # # ]: 0 : if (nNumTgMInChI != nNumTgMRevrs)
887 : : {
888 : 0 : pc2i->nNumTgDiffMinus++;
889 : : }
890 : : }
891 : 0 : pc2i->bHasDifference |= pStruct->ti.t_group[i].nNumEndpoints !=
892 : 0 : pStruct->One_ti.t_group[i].nNumEndpoints;
893 : :
894 : 0 : pc2i->nNumTgHInChI += nNumTgHInChI;
895 : 0 : pc2i->nNumTgMInChI += nNumTgMInChI;
896 : 0 : pc2i->nNumTgHRevrs += nNumTgHRevrs;
897 : 0 : pc2i->nNumTgMRevrs += nNumTgMRevrs;
898 : : }
899 [ # # ]: 0 : for (; i < pStruct->ti.num_t_groups; i++)
900 : : {
901 : 0 : nNumTgHInChI = pStruct->ti.t_group[i].num[0] - pStruct->ti.t_group[i].num[1];
902 : 0 : nNumTgMInChI = pStruct->ti.t_group[i].num[1];
903 : 0 : pc2i->nNumTgHInChI += nNumTgHInChI;
904 : 0 : pc2i->nNumTgMInChI += nNumTgMInChI;
905 : 0 : pc2i->bHasDifference |= 1;
906 : : }
907 [ # # ]: 0 : for (; i < pStruct->One_ti.num_t_groups; i++)
908 : : {
909 : 0 : nNumTgHRevrs = pStruct->One_ti.t_group[i].num[0] - pStruct->One_ti.t_group[i].num[1];
910 : 0 : nNumTgMRevrs = pStruct->One_ti.t_group[i].num[1];
911 : 0 : pc2i->nNumTgHRevrs += nNumTgHRevrs;
912 : 0 : pc2i->nNumTgMRevrs += nNumTgMRevrs;
913 : 0 : pc2i->bHasDifference |= 1;
914 : : }
915 [ # # ]: 0 : for (i = j = 0; i < pStruct->num_atoms; i++)
916 : : {
917 : : /* i = original InChI canonical number - 1 */
918 : : /* k = atom number from InChI created out of restored Fixed-H structure */
919 : 0 : int iCanonRevrs = nAtno2CanonRevrs[i];
920 : 0 : int endptInChI = pStruct->endpoint[i]; /* endpoint in InChI */
921 [ # # ]: 0 : int endptRevrs = at_Mobile_H_Revrs ? at_Mobile_H_Revrs[i].endpoint : 0;
922 : 0 : int nFixHInChI = pStruct->fixed_H[i];
923 [ # # ]: 0 : int nFixHRevrs = num_Fixed_H_Revrs ? num_Fixed_H_Revrs[iCanonRevrs] : 0;
924 [ # # ]: 0 : int nMobHInChI = pnMobHInChI ? pnMobHInChI[i] : 0;
925 [ # # ]: 0 : int nMobHRevrs = pnMobHRevrs ? pnMobHRevrs[iCanonRevrs] : 0;
926 [ # # ]: 0 : if ( /*(!endptInChI || !endptRevrs) &&*/ (nFixHInChI != nFixHRevrs) ||
927 [ # # # # ]: 0 : (!endptInChI != !endptRevrs) || nMobHInChI != nMobHRevrs)
928 : : {
929 : : /* in InChI or reversed InChI atom[i] is not tautomeric */
930 : : /* and number of fixed-H on the atom[i] differs */
931 [ # # ]: 0 : if (j >= MAX_DIFF_FIXH)
932 : : {
933 : 0 : ret = RI_ERR_PROGR;
934 : 0 : goto exit_function;
935 : : }
936 : 0 : pc2i->c2at[j].endptInChI = endptInChI;
937 : 0 : pc2i->c2at[j].endptRevrs = endptRevrs;
938 : 0 : pc2i->bHasDifference |= !endptInChI != !endptRevrs;
939 : 0 : pc2i->c2at[j].atomNumber = i;
940 : 0 : pc2i->c2at[j].nValElectr = pVA[i].cNumValenceElectrons;
941 : 0 : pc2i->c2at[j].nPeriodNum = pVA[i].cPeriodicRowNumber;
942 : 0 : pc2i->c2at[j].nFixHInChI = nFixHInChI;
943 : 0 : pc2i->c2at[j].nFixHRevrs = nFixHRevrs;
944 : 0 : pc2i->bHasDifference |= nFixHInChI != nFixHRevrs;
945 [ # # # # ]: 0 : pc2i->c2at[j].nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H[i] :
946 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H[i] : 0;
947 [ # # # # ]: 0 : pc2i->c2at[j].nMobHRevrs = (pStruct->pOneINChI[1] && pStruct->pOneINChI[1]->nNum_H) ?
948 : 0 : pStruct->pOneINChI[1]->nNum_H[iCanonRevrs] :
949 [ # # # # ]: 0 : (pStruct->pOneINChI[0] && pStruct->pOneINChI[0]->nNum_H) ?
950 : 0 : pStruct->pOneINChI[0]->nNum_H[iCanonRevrs] : 0;
951 [ # # # # : 0 : pc2i->nNumDiffMobH += (nMobHInChI != nMobHRevrs && !endptRevrs && !endptInChI);
# # ]
952 : 0 : pc2i->bHasDifference |= nMobHInChI != nMobHRevrs;
953 : 0 : pc2i->c2at[j].nNumHRevrs = at2[i].num_H;
954 : 0 : pc2i->c2at[j].nAtChargeRevrs = at2[i].charge;
955 : 0 : j++;
956 : : }
957 : 0 : pc2i->nNumEndpInChI += (endptInChI != 0);
958 : 0 : pc2i->nNumEndpRevrs += (endptRevrs != 0);
959 : :
960 [ # # ]: 0 : if (!pVA[i].cMetal)
961 : : {
962 : 0 : pc2i->nChargeFixHRevrsNonMetal += at2[i].charge;
963 [ # # ]: 0 : pc2i->nChargeMobHRevrsNonMetal += at_Mobile_H_Revrs ? at_Mobile_H_Revrs[i].charge : 0;
964 : : }
965 : :
966 : : /*pStruct->bExtract |= EXTRACT_STRUCT_NUMBER;*/
967 : : }
968 [ # # ]: 0 : pc2i->nChargeFixHInChI = pInChI[0] ? pInChI[0]->nTotalCharge : 0;
969 [ # # ]: 0 : pc2i->nChargeMobHInChI = pInChI[1] ? pInChI[1]->nTotalCharge : 0;
970 : :
971 [ # # ]: 0 : pc2i->nChargeMobHRevrs = pStruct->pOneINChI[1] ? pStruct->pOneINChI[1]->nTotalCharge :
972 [ # # ]: 0 : pStruct->pOneINChI[0] ? pStruct->pOneINChI[0]->nTotalCharge : 0;
973 [ # # ]: 0 : pc2i->nChargeFixHRevrs = pStruct->pOneINChI[0] ? pStruct->pOneINChI[0]->nTotalCharge : 0;
974 : :
975 : 0 : pc2i->bHasDifference |= pc2i->nChargeFixHInChI != pc2i->nChargeFixHRevrs;
976 : 0 : pc2i->bHasDifference |= pc2i->nChargeMobHInChI != pc2i->nChargeMobHRevrs;
977 : :
978 : 0 : exit_function:
979 : 0 : pc2i->len_c2at = j;
980 : :
981 : 0 : return ret;
982 : : }
983 : :
984 : :
985 : : /****************************************************************************/
986 : 0 : int FillOutCMP2MHINCHI(StrFromINChI* pStruct,
987 : : ALL_TC_GROUPS* pTCGroups,
988 : : inp_ATOM* at2,
989 : : VAL_AT* pVA,
990 : : INChI* pInChI[],
991 : : CMP2MHINCHI* pc2i)
992 : : {
993 : 0 : int ret = 0, i, j, iat;
994 [ # # # # : 0 : int bFixHRevrsExists = pInChI[1] && pInChI[1]->nNumberOfAtoms > 0 && !pInChI[1]->bDeleted;
# # ]
995 : 0 : inp_ATOM* at_Mobile_H_Revrs = (pStruct->pOne_norm_data[0] &&
996 [ # # # # ]: 0 : pStruct->pOne_norm_data[0]->at) ? pStruct->pOne_norm_data[0]->at : NULL;
997 : : /* atom number in structure that produced original InChI is atom number in all inp_ATOM *atoms */
998 : : /* atom number in structure that produced restored InChI is in nAtomRevrs[]: */
999 : 0 : AT_NUMB* nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
1000 : 0 : AT_NUMB* nAtno2CanonRevrs = pStruct->nAtno2Canon[0];
1001 [ # # # # ]: 0 : S_CHAR* pnMobHInChI = (pInChI[0] && pInChI[0]->nNum_H) ? pInChI[0]->nNum_H : NULL;
1002 [ # # ]: 0 : S_CHAR* pnMobHRevrs = (pStruct->pOneINChI[0] && pStruct->pOneINChI[0]->nNum_H) ?
1003 [ # # ]: 0 : pStruct->pOneINChI[0]->nNum_H : NULL;
1004 : : int nNumTgHInChI, nNumTgMInChI, nNumTgHRevrs, nNumTgMRevrs;
1005 : 0 : memset(pc2i, 0, sizeof(*pc2i)); /* djb-rwth: memset_s C11/Annex K variant? */
1006 : 0 : pc2i->nNumTgInChI = pStruct->ti.num_t_groups;
1007 : 0 : pc2i->nNumTgRevrs = pStruct->One_ti.num_t_groups;
1008 : 0 : pc2i->bHasDifference |= pc2i->nNumTgInChI != pc2i->nNumTgRevrs;
1009 : :
1010 : 0 : pc2i->nNumRemHInChI = pStruct->nNumRemovedProtonsMobHInChI;
1011 : 0 : pc2i->nNumRemHRevrs = pStruct->One_ti.tni.nNumRemovedProtons;
1012 : : /*pc2i->bHasDifference |= pc2i->nNumRemHInChI != pc2i->nNumRemHRevrs;*/
1013 : :
1014 : 0 : pc2i->bFixedHLayerExistsRevrs = bFixHRevrsExists;
1015 : : /*pc2i->bHasDifference |= !bFixHRevrsExists;*/
1016 : :
1017 [ # # ]: 0 : for (i = 0; i < pStruct->ti.num_t_groups; i++)
1018 : : {
1019 : 0 : int jFst = pStruct->ti.t_group[i].nFirstEndpointAtNoPos;
1020 : 0 : int jNum = pStruct->ti.t_group[i].nNumEndpoints;
1021 : : int is_N, is_O;
1022 [ # # ]: 0 : for (j = 0; j < jNum; j++)
1023 : : {
1024 : 0 : iat = pStruct->ti.nEndpointAtomNumber[jFst + j];
1025 [ # # # # ]: 0 : is_N = pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1;
1026 : 0 : is_O = pVA[iat].cNumValenceElectrons == 6;
1027 [ # # ]: 0 : if (is_N + is_O != 1)
1028 : : {
1029 : 0 : return RI_ERR_SYNTAX;
1030 : : }
1031 : 0 : pc2i->nNumTgNInChI += is_N;
1032 : 0 : pc2i->nNumTgOInChI += is_O;
1033 [ # # ]: 0 : if (at2[iat].chem_bonds_valence == at2[iat].valence)
1034 : : {
1035 : : /* donor */
1036 [ # # ]: 0 : if (is_N)
1037 : : {
1038 : : /* N */
1039 [ # # # # ]: 0 : pc2i->nNumTgNHInChI += at2[iat].charge == 0 && at2[iat].num_H == 1;
1040 [ # # # # ]: 0 : pc2i->nNumTgNH2InChI += at2[iat].charge == 0 && at2[iat].num_H == 2;
1041 [ # # # # ]: 0 : pc2i->nNumTgNMinusInChI += at2[iat].charge == -1 && at2[iat].num_H == 0;
1042 [ # # # # ]: 0 : pc2i->nNumTgNHMinusInChI += at2[iat].charge == -1 && at2[iat].num_H == 1;
1043 : : }
1044 : : else
1045 : : {
1046 : : /* O, S, Se, Te */
1047 [ # # # # ]: 0 : pc2i->nNumTgOHInChI += at2[iat].charge == 0 && at2[iat].num_H == 1;
1048 [ # # # # ]: 0 : pc2i->nNumTgOMinusInChI += at2[iat].charge == -1 && at2[iat].num_H == 0;
1049 : : }
1050 : : }
1051 : : else
1052 : : {
1053 [ # # ]: 0 : if (at2[iat].chem_bonds_valence == at2[iat].valence + 1)
1054 : : {
1055 : : /* donor */
1056 [ # # ]: 0 : if (is_N)
1057 : : {
1058 : : /* N */
1059 [ # # # # ]: 0 : pc2i->nNumTgDBNHInChI += at2[iat].charge == 0 && at2[iat].num_H == 1;
1060 [ # # # # ]: 0 : pc2i->nNumTgDBNMinusInChI += at2[iat].charge == -1 && at2[iat].num_H == 0;
1061 [ # # # # ]: 0 : pc2i->nNumTgDBNInChI += at2[iat].charge == 0 && at2[iat].num_H == 0;
1062 : : }
1063 : : else
1064 : : {
1065 : : /* O, S, Se, Te */
1066 [ # # # # ]: 0 : pc2i->nNumTgDBOInChI += at2[iat].charge == 0 && at2[iat].num_H == 0;
1067 : : }
1068 : : }
1069 : : }
1070 : : }
1071 : : }
1072 [ # # ]: 0 : for (i = 0; i < pStruct->One_ti.num_t_groups; i++)
1073 : : {
1074 : 0 : int jFst = pStruct->One_ti.t_group[i].nFirstEndpointAtNoPos;
1075 : 0 : int jNum = pStruct->One_ti.t_group[i].nNumEndpoints;
1076 : : int is_N, is_O;
1077 [ # # ]: 0 : for (j = 0; j < jNum; j++)
1078 : : {
1079 : 0 : iat = nCanon2AtnoRevrs[(int)pStruct->One_ti.nEndpointAtomNumber[jFst + j]];
1080 [ # # # # ]: 0 : is_N = pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1;
1081 : 0 : is_O = pVA[iat].cNumValenceElectrons == 6;
1082 [ # # ]: 0 : if (is_N + is_O != 1)
1083 : : {
1084 : 0 : return RI_ERR_PROGR;
1085 : : }
1086 : 0 : pc2i->nNumTgNRevrs += is_N;
1087 : 0 : pc2i->nNumTgORevrs += is_O;
1088 [ # # ]: 0 : if (at2[iat].chem_bonds_valence == at2[iat].valence)
1089 : : {
1090 : : /* donor */
1091 [ # # ]: 0 : if (is_N)
1092 : : {
1093 : : /* N */
1094 [ # # # # ]: 0 : pc2i->nNumTgNHRevrs += at2[iat].charge == 0 && at2[iat].num_H == 1;
1095 [ # # # # ]: 0 : pc2i->nNumTgNH2Revrs += at2[iat].charge == 0 && at2[iat].num_H == 2;
1096 [ # # # # ]: 0 : pc2i->nNumTgNMinusRevrs += at2[iat].charge == -1 && at2[iat].num_H == 0;
1097 [ # # # # ]: 0 : pc2i->nNumTgNHMinusRevrs += at2[iat].charge == -1 && at2[iat].num_H == 1;
1098 : : }
1099 : : else
1100 : : {
1101 : : /* O, S, Se, Te */
1102 [ # # # # ]: 0 : pc2i->nNumTgOHRevrs += at2[iat].charge == 0 && at2[iat].num_H == 1;
1103 [ # # # # ]: 0 : pc2i->nNumTgOMinusRevrs += at2[iat].charge == -1 && at2[iat].num_H == 0;
1104 : : }
1105 : : }
1106 : : else
1107 : : {
1108 [ # # ]: 0 : if (at2[iat].chem_bonds_valence == at2[iat].valence + 1)
1109 : : {
1110 : : /* donor */
1111 [ # # ]: 0 : if (is_N)
1112 : : {
1113 : : /* N */
1114 [ # # # # ]: 0 : pc2i->nNumTgDBNHRevrs += at2[iat].charge == 0 && at2[iat].num_H == 1;
1115 [ # # # # ]: 0 : pc2i->nNumTgDBNMinusRevrs += at2[iat].charge == -1 && at2[iat].num_H == 0;
1116 [ # # # # ]: 0 : pc2i->nNumTgDBNRevrs += at2[iat].charge == 0 && at2[iat].num_H == 0;
1117 : : }
1118 : : else
1119 : : {
1120 : : /* O, S, Se, Te */
1121 [ # # # # ]: 0 : pc2i->nNumTgDBORevrs += at2[iat].charge == 0 && at2[iat].num_H == 0;
1122 : : }
1123 : : }
1124 : : }
1125 : : }
1126 : : }
1127 : :
1128 [ # # # # ]: 0 : for (i = 0; i < pStruct->ti.num_t_groups && i < pStruct->One_ti.num_t_groups; i++)
1129 : : {
1130 : 0 : nNumTgHInChI = pStruct->ti.t_group[i].num[0] - pStruct->ti.t_group[i].num[1];
1131 : 0 : nNumTgMInChI = pStruct->ti.t_group[i].num[1];
1132 : 0 : nNumTgHRevrs = pStruct->One_ti.t_group[i].num[0] - pStruct->One_ti.t_group[i].num[1];
1133 : 0 : nNumTgMRevrs = pStruct->One_ti.t_group[i].num[1];
1134 : :
1135 : 0 : pc2i->bHasDifference |= nNumTgHInChI != nNumTgHRevrs;
1136 : 0 : pc2i->bHasDifference |= nNumTgMInChI != nNumTgMRevrs;
1137 : :
1138 : 0 : if (pStruct->ti.t_group[i].nNumEndpoints ==
1139 [ # # ]: 0 : pStruct->One_ti.t_group[i].nNumEndpoints)
1140 : : {
1141 : :
1142 [ # # ]: 0 : if (nNumTgHInChI != nNumTgHRevrs)
1143 : : {
1144 : 0 : pc2i->nNumTgDiffH++;
1145 : : }
1146 [ # # ]: 0 : if (nNumTgMInChI != nNumTgMRevrs)
1147 : : {
1148 : 0 : pc2i->nNumTgDiffMinus++;
1149 : : }
1150 : : }
1151 : 0 : pc2i->bHasDifference |= pStruct->ti.t_group[i].nNumEndpoints !=
1152 : 0 : pStruct->One_ti.t_group[i].nNumEndpoints;
1153 : :
1154 : 0 : pc2i->nNumTgHInChI += nNumTgHInChI;
1155 : 0 : pc2i->nNumTgMInChI += nNumTgMInChI;
1156 : 0 : pc2i->nNumTgHRevrs += nNumTgHRevrs;
1157 : 0 : pc2i->nNumTgMRevrs += nNumTgMRevrs;
1158 : : }
1159 [ # # ]: 0 : for (; i < pStruct->ti.num_t_groups; i++)
1160 : : {
1161 : 0 : nNumTgHInChI = pStruct->ti.t_group[i].num[0] - pStruct->ti.t_group[i].num[1];
1162 : 0 : nNumTgMInChI = pStruct->ti.t_group[i].num[1];
1163 : 0 : pc2i->nNumTgHInChI += nNumTgHInChI;
1164 : 0 : pc2i->nNumTgMInChI += nNumTgMInChI;
1165 : 0 : pc2i->bHasDifference |= 1;
1166 : : }
1167 [ # # ]: 0 : for (; i < pStruct->One_ti.num_t_groups; i++)
1168 : : {
1169 : 0 : nNumTgHRevrs = pStruct->One_ti.t_group[i].num[0] - pStruct->One_ti.t_group[i].num[1];
1170 : 0 : nNumTgMRevrs = pStruct->One_ti.t_group[i].num[1];
1171 : 0 : pc2i->nNumTgHRevrs += nNumTgHRevrs;
1172 : 0 : pc2i->nNumTgMRevrs += nNumTgMRevrs;
1173 : 0 : pc2i->bHasDifference |= 1;
1174 : : }
1175 [ # # ]: 0 : for (i = j = 0; i < pStruct->num_atoms; i++)
1176 : : {
1177 : : /* i = original InChI canonical number - 1 */
1178 : : /* k = atom number from InChI created out of restored Fixed-H structure */
1179 : 0 : int iCanonRevrs = nAtno2CanonRevrs[i];
1180 : 0 : int endptInChI = at2[i].endpoint; /* endpoint in InChI */
1181 [ # # ]: 0 : int endptRevrs = at_Mobile_H_Revrs ? at_Mobile_H_Revrs[i].endpoint : 0;
1182 [ # # ]: 0 : int nMobHInChI = pnMobHInChI ? pnMobHInChI[i] : 0;
1183 [ # # ]: 0 : int nMobHRevrs = pnMobHRevrs ? pnMobHRevrs[iCanonRevrs] : 0;
1184 [ # # # # ]: 0 : if ((!endptInChI != !endptRevrs) || nMobHInChI != nMobHRevrs)
1185 : : {
1186 : : /* in InChI or reversed InChI atom[i] is not tautomeric */
1187 : : /* and number of fixed-H on the atom[i] differs */
1188 [ # # ]: 0 : if (j >= MAX_DIFF_FIXH)
1189 : : {
1190 : 0 : ret = RI_ERR_PROGR;
1191 : 0 : goto exit_function;
1192 : : }
1193 : 0 : pc2i->c2at[j].endptInChI = endptInChI;
1194 : 0 : pc2i->c2at[j].endptRevrs = endptRevrs;
1195 : 0 : pc2i->bHasDifference |= !endptInChI != !endptRevrs;
1196 : 0 : pc2i->c2at[j].atomNumber = i;
1197 : 0 : pc2i->c2at[j].nValElectr = pVA[i].cNumValenceElectrons;
1198 : 0 : pc2i->c2at[j].nPeriodNum = pVA[i].cPeriodicRowNumber;
1199 [ # # # # ]: 0 : pc2i->c2at[j].nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H[i] :
1200 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H[i] : 0;
1201 [ # # # # ]: 0 : pc2i->c2at[j].nMobHRevrs = (pStruct->pOneINChI[1] && pStruct->pOneINChI[1]->nNum_H) ?
1202 : 0 : pStruct->pOneINChI[1]->nNum_H[iCanonRevrs] :
1203 [ # # # # ]: 0 : (pStruct->pOneINChI[0] && pStruct->pOneINChI[0]->nNum_H) ?
1204 : 0 : pStruct->pOneINChI[0]->nNum_H[iCanonRevrs] : 0;
1205 : :
1206 [ # # # # : 0 : pc2i->nNumDiffMobH += (nMobHInChI != nMobHRevrs && !endptRevrs && !endptInChI);
# # ]
1207 : 0 : pc2i->bHasDifference |= (nMobHInChI != nMobHRevrs);
1208 : 0 : pc2i->c2at[j].nNumHRevrs = at2[i].num_H;
1209 : 0 : pc2i->c2at[j].nAtChargeRevrs = at2[i].charge;
1210 : 0 : j++;
1211 : : }
1212 : 0 : pc2i->nNumEndpInChI += (endptInChI != 0);
1213 : 0 : pc2i->nNumEndpRevrs += (endptRevrs != 0);
1214 : :
1215 [ # # ]: 0 : if (!pVA[i].cMetal)
1216 : : {
1217 [ # # # # ]: 0 : pc2i->nChargeMobHRevrsNonMetal += (at_Mobile_H_Revrs && !at_Mobile_H_Revrs[i].endpoint) ? at_Mobile_H_Revrs[i].charge : 0;
1218 : : }
1219 : :
1220 : :
1221 : : /*pStruct->bExtract |= EXTRACT_STRUCT_NUMBER;*/
1222 : : }
1223 : 0 : pc2i->nChargeMobHRevrsNonMetal += pTCGroups->tgroup_charge;
1224 : :
1225 [ # # ]: 0 : pc2i->nChargeMobHInChI = pInChI[0] ? pInChI[0]->nTotalCharge : 0;
1226 : :
1227 [ # # ]: 0 : pc2i->nChargeMobHRevrs = pStruct->pOneINChI[0] ? pStruct->pOneINChI[0]->nTotalCharge : 0;
1228 : :
1229 : 0 : pc2i->bHasDifference |= pc2i->nChargeMobHInChI != pc2i->nChargeMobHRevrs;
1230 : :
1231 : 0 : exit_function:
1232 : 0 : pc2i->len_c2at = j;
1233 : :
1234 : 0 : return ret;
1235 : : }
1236 : :
1237 : :
1238 : : /****************************************************************************/
1239 : 0 : int NormalizeAndCompare(CANON_GLOBALS* pCG,
1240 : : INCHI_CLOCK* ic,
1241 : : ICHICONST INPUT_PARMS* ip,
1242 : : STRUCT_DATA* sd,
1243 : : BN_STRUCT* pBNS,
1244 : : BN_DATA* pBD,
1245 : : StrFromINChI* pStruct,
1246 : : inp_ATOM* at,
1247 : : inp_ATOM* at2,
1248 : : inp_ATOM* at3,
1249 : : VAL_AT* pVA,
1250 : : ALL_TC_GROUPS* pTCGroups,
1251 : : INChI* pInChI[],
1252 : : long num_inp,
1253 : : int bHasSomeFixedH,
1254 : : int* pnNumRunBNS,
1255 : : int* pnTotalDelta,
1256 : : int forbidden_edge_mask,
1257 : : int forbidden_stereo_edge_mask)
1258 : : {
1259 : : int i;
1260 : : int err;
1261 : : ICR icr, icr2;
1262 : 0 : int num_norm_endpoints, num_endpoints, num_norm_t_groups, ret = 0; /* djb-rwth: ignoring LLVM warning: variables used; removing redundant variables */
1263 : : #if ( bRELEASE_VERSION == 0 )
1264 : : #ifndef TARGET_API_LIB
1265 : : const char* szCurHdr = (ip->pSdfValue && ip->pSdfValue[0]) ? ip->pSdfValue : "???";
1266 : : int iComponent = pTCGroups->iComponent;
1267 : : #endif
1268 : : #endif
1269 : 0 : T_GROUP_INFO* t_group_info = NULL;
1270 : 0 : inp_ATOM* at_norm = NULL; /* normalized */
1271 : 0 : inp_ATOM* at_prep = NULL; /* preprocessed */
1272 : : INCHI_MODE cmpInChI, cmpInChI2;
1273 : : int nDeltaPrev, nDeltaCur;
1274 : : int iOrigInChI, iRevrInChI;
1275 : :
1276 : :
1277 : : /***********************************************************/
1278 : : /* normalize and create one component InChI */
1279 : : /***********************************************************/
1280 : 0 : ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
1281 : : &t_group_info, &at_norm, &at_prep);
1282 [ # # ]: 0 : if (ret < 0)
1283 : : {
1284 : : #if ( bRELEASE_VERSION == 0 )
1285 : : #ifndef TARGET_API_LIB
1286 : : fprintf(stdout, "\nERROR in MakeOneInchi-1: %ld %s Comp:%d %c%c Err:%d\n", num_inp,
1287 : : szCurHdr ? szCurHdr : "???", iComponent, pStruct->iInchiRec ? 'R' : 'D', pStruct->iMobileH ? 'M' : 'F', ret);
1288 : : #endif
1289 : : #endif
1290 : 0 : goto exit_function;
1291 : : }
1292 [ # # ]: 0 : if (pStruct->bMobileH == TAUT_NON)
1293 : : {
1294 : : /* these indexes are used to compare Mobile-H InChI */
1295 [ # # # # : 0 : iOrigInChI = (pInChI[1] && pInChI[1]->nNumberOfAtoms && !pInChI[1]->bDeleted) ? 1 : 0;
# # ]
1296 [ # # # # : 0 : iRevrInChI = (pStruct->pOneINChI[1] && pStruct->pOneINChI[1]->nNumberOfAtoms && !pStruct->pOneINChI[1]->bDeleted) ? 1 : 0;
# # ]
1297 : : }
1298 : : else
1299 : : {
1300 : 0 : iOrigInChI = 0;
1301 : 0 : iRevrInChI = 0;
1302 : : }
1303 : :
1304 : : /* Intercept and correct non-polymer Zz to Zy if applicable */
1305 [ # # # # ]: 0 : if (pStruct->n_zy && pStruct->n_pzz)
1306 : : {
1307 [ # # ]: 0 : if (pStruct->pOneINChI[iRevrInChI]->szHillFormula)
1308 : : {
1309 : : INCHI_IOS_STRING temp_string_container;
1310 : 0 : INCHI_IOS_STRING* strbuf = &temp_string_container;
1311 : 0 : int len0 = strlen(pStruct->pOneINChI[iRevrInChI]->szHillFormula);
1312 [ # # ]: 0 : if (inchi_strbuf_init(strbuf, len0 + 1, len0 + 1) > 0)
1313 : : {
1314 : 0 : inchi_strbuf_printf(strbuf, "%-s", pStruct->pOneINChI[iRevrInChI]->szHillFormula);
1315 : : }
1316 : 0 : MergeZzInHillFormula(strbuf);
1317 [ # # ]: 0 : if (strbuf->nUsedLength > len0 + 1)
1318 : : {
1319 : : char* ctmp; /* djb-rwth: supplementary variable */
1320 : 0 : ctmp = (char*)inchi_realloc(pStruct->pOneINChI[iRevrInChI]->szHillFormula, (long long)strbuf->nUsedLength + 1); /* djb-rwth: cast operator added */
1321 [ # # ]: 0 : if (ctmp != NULL) /* djb-rwth: NULL pointer must not be assigned to pStruct->pOneINChI[iRevrInChI]->szHillFormula */
1322 : 0 : pStruct->pOneINChI[iRevrInChI]->szHillFormula = ctmp;
1323 : : }
1324 : 0 : strcpy(pStruct->pOneINChI[iRevrInChI]->szHillFormula, strbuf->pStr);
1325 : 0 : inchi_strbuf_close(strbuf);
1326 : : }
1327 : : }
1328 : :
1329 : :
1330 : : /************************************************************/
1331 : : /* compare */
1332 : : /************************************************************/
1333 [ # # # # ]: 0 : if (pStruct->iMobileH == TAUT_NON && (ret = FillOutExtraFixedHDataRestr(pStruct)))
1334 : : {
1335 : 0 : goto exit_function;
1336 : : }
1337 : 0 : cmpInChI = CompareReversedINChI2(pStruct->pOneINChI[iRevrInChI], pInChI[iOrigInChI], pStruct->pOneINChI_Aux[iRevrInChI], NULL /*INChI_Aux *a2*/, &icr, &err);
1338 [ # # ]: 0 : if (cmpInChI & IDIF_PROBLEM)
1339 : : {
1340 : 0 : ret = RI_ERR_PROGR; /* severe restore problem */
1341 : 0 : goto exit_function;
1342 : : }
1343 [ # # ]: 0 : if (err)
1344 : : {
1345 : 0 : ret = RI_ERR_ALLOC;
1346 : 0 : goto exit_function;
1347 : : }
1348 : : /********** InChI from restored structure has LESS hydrogen atoms ******************************/
1349 [ # # # # : 0 : if ((cmpInChI & IDIF_LESS_H) && at_prep && 0 < (nDeltaCur = icr.tot_num_H2 - icr.tot_num_H1))
# # ]
1350 : : {
1351 : : do
1352 : : {
1353 : 0 : ret = FixLessHydrogenInFormula(pBNS, pBD, pStruct, at, at2, at_prep, pVA, pTCGroups,
1354 : : pnNumRunBNS, pnTotalDelta, forbidden_edge_mask);
1355 [ # # ]: 0 : if (ret < 0)
1356 : : {
1357 : 0 : goto exit_function;
1358 : : }
1359 [ # # ]: 0 : if (ret)
1360 : : {
1361 : : /* Probably success. The changes are in pBNS. Create new InChI out of the new restored structure */
1362 : 0 : ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
1363 : : &t_group_info, &at_norm, &at_prep);
1364 [ # # ]: 0 : if (ret < 0)
1365 : : {
1366 : : #if ( bRELEASE_VERSION == 0 )
1367 : : #ifndef TARGET_API_LIB
1368 : : fprintf(stdout, "\nERROR in MakeOneInchi-2: %ld %s Comp:%d %c%c Err:%d\n", num_inp,
1369 : : szCurHdr ? szCurHdr : "???", iComponent, pStruct->iInchiRec ? 'R' : 'D', pStruct->iMobileH ? 'M' : 'F', ret);
1370 : : #endif
1371 : : #endif
1372 : 0 : goto exit_function;
1373 : : }
1374 : : /* compare new InChI to the original InChI */
1375 [ # # ]: 0 : if (pStruct->bMobileH == TAUT_NON)
1376 : : {
1377 [ # # # # : 0 : iRevrInChI = (pStruct->pOneINChI[1] && pStruct->pOneINChI[1]->nNumberOfAtoms && !pStruct->pOneINChI[1]->bDeleted) ? 1 : 0;
# # ]
1378 : : }
1379 : : else
1380 : : {
1381 : 0 : iRevrInChI = 0;
1382 : : }
1383 [ # # # # ]: 0 : if (pStruct->iMobileH == TAUT_NON && (ret = FillOutExtraFixedHDataRestr(pStruct)))
1384 : : {
1385 : 0 : goto exit_function;
1386 : : }
1387 : 0 : cmpInChI = CompareReversedINChI2(pStruct->pOneINChI[iRevrInChI], pInChI[iOrigInChI], pStruct->pOneINChI_Aux[iRevrInChI], NULL, &icr, &err);
1388 : 0 : nDeltaPrev = nDeltaCur;
1389 : 0 : nDeltaCur = icr.tot_num_H2 - icr.tot_num_H1;
1390 : : }
1391 : : else
1392 : : {
1393 : 0 : break;
1394 : : }
1395 [ # # # # : 0 : } while ((cmpInChI & IDIF_LESS_H) && at_prep && nDeltaCur && nDeltaCur < nDeltaPrev);
# # # # ]
1396 : : }
1397 : : /********** InChI from restored structure has MORE hydrogen atoms ******************************/
1398 [ # # # # : 0 : if ((cmpInChI & IDIF_MORE_H) && at_prep && 0 < (nDeltaCur = icr.tot_num_H1 - icr.tot_num_H2))
# # ]
1399 : : {
1400 : : do
1401 : : {
1402 : 0 : ret = FixMoreHydrogenInFormula(pBNS, pBD, pStruct, at, at2, at_prep, pVA, pTCGroups,
1403 : : pnNumRunBNS, pnTotalDelta, forbidden_edge_mask);
1404 [ # # ]: 0 : if (ret < 0)
1405 : : {
1406 : 0 : goto exit_function;
1407 : : }
1408 [ # # ]: 0 : if (ret)
1409 : : {
1410 : : /* Probably success. The changes are in pBNS. Create new InChI out of the new restored structure */
1411 : 0 : ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
1412 : : &t_group_info, &at_norm, &at_prep);
1413 [ # # ]: 0 : if (ret < 0)
1414 : : {
1415 : : #if ( bRELEASE_VERSION == 0 )
1416 : : #ifndef TARGET_API_LIB
1417 : : fprintf(stdout, "\nERROR in MakeOneInchi-3: %ld %s Comp:%d %c%c Err:%d\n", num_inp,
1418 : : szCurHdr ? szCurHdr : "???", iComponent, pStruct->iInchiRec ? 'R' : 'D', pStruct->iMobileH ? 'M' : 'F', ret);
1419 : : #endif
1420 : : #endif
1421 : 0 : goto exit_function;
1422 : : }
1423 : : /* compare new InChI to the original InChI */
1424 [ # # ]: 0 : if (pStruct->bMobileH == TAUT_NON)
1425 : : {
1426 [ # # # # : 0 : iRevrInChI = (pStruct->pOneINChI[1] && pStruct->pOneINChI[1]->nNumberOfAtoms && !pStruct->pOneINChI[1]->bDeleted) ? 1 : 0;
# # ]
1427 : : }
1428 : : else
1429 : : {
1430 : 0 : iRevrInChI = 0;
1431 : : }
1432 [ # # # # ]: 0 : if (pStruct->iMobileH == TAUT_NON && (ret = FillOutExtraFixedHDataRestr(pStruct)))
1433 : : {
1434 : 0 : goto exit_function;
1435 : : }
1436 : 0 : cmpInChI = CompareReversedINChI2(pStruct->pOneINChI[iRevrInChI], pInChI[iOrigInChI], pStruct->pOneINChI_Aux[iRevrInChI], NULL, &icr, &err);
1437 : 0 : nDeltaPrev = nDeltaCur;
1438 : 0 : nDeltaCur = icr.tot_num_H1 - icr.tot_num_H2;
1439 : : }
1440 : : else
1441 : : {
1442 : 0 : break;
1443 : : }
1444 [ # # # # : 0 : } while ((cmpInChI & IDIF_MORE_H) && at_prep && nDeltaCur && nDeltaCur < nDeltaPrev);
# # # # ]
1445 : : }
1446 : : /***************** Fix non-taut atoms normalized to tautomeric endpoints ***********************/
1447 [ # # # # : 0 : if ((cmpInChI & IDIF_EXTRA_TG_ENDP) && at_norm && 0 < (nDeltaCur = icr.num_endp_in1_only))
# # ]
1448 : : {
1449 : : do
1450 : : {
1451 : 0 : ret = FixRemoveExtraTautEndpoints(pBNS, pBD, pStruct, at, at2, at_prep, at_norm, pVA, pTCGroups, &icr,
1452 : : pnNumRunBNS, pnTotalDelta, forbidden_edge_mask);
1453 [ # # ]: 0 : if (ret < 0)
1454 : : {
1455 : 0 : goto exit_function;
1456 : : }
1457 [ # # ]: 0 : if (ret)
1458 : : {
1459 : : /* Probably success. The changes are in pBNS. Create new InChI out of the new restored structure */
1460 : 0 : ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
1461 : : &t_group_info, &at_norm, &at_prep);
1462 [ # # ]: 0 : if (ret < 0)
1463 : : {
1464 : : #if ( bRELEASE_VERSION == 0 )
1465 : : #ifndef TARGET_API_LIB
1466 : : fprintf(stdout, "\nERROR in MakeOneInchi-4: %ld %s Comp:%d %c%c Err:%d\n", num_inp,
1467 : : szCurHdr ? szCurHdr : "???", iComponent, pStruct->iInchiRec ? 'R' : 'D', pStruct->iMobileH ? 'M' : 'F', ret);
1468 : : #endif
1469 : : #endif
1470 : 0 : goto exit_function;
1471 : : }
1472 : : /* compare new InChI to the original InChI */
1473 [ # # ]: 0 : if (pStruct->bMobileH == TAUT_NON)
1474 : : {
1475 [ # # # # : 0 : iRevrInChI = (pStruct->pOneINChI[1] && pStruct->pOneINChI[1]->nNumberOfAtoms && !pStruct->pOneINChI[1]->bDeleted) ? 1 : 0;
# # ]
1476 : : }
1477 : : else
1478 : : {
1479 : 0 : iRevrInChI = 0;
1480 : : }
1481 [ # # # # ]: 0 : if (pStruct->iMobileH == TAUT_NON && (ret = FillOutExtraFixedHDataRestr(pStruct)))
1482 : : {
1483 : 0 : goto exit_function;
1484 : : }
1485 : 0 : cmpInChI = CompareReversedINChI2(pStruct->pOneINChI[iRevrInChI], pInChI[iOrigInChI], pStruct->pOneINChI_Aux[iRevrInChI], NULL, &icr, &err);
1486 : 0 : nDeltaPrev = nDeltaCur;
1487 : 0 : nDeltaCur = icr.num_endp_in1_only;
1488 : : }
1489 : : else
1490 : : {
1491 : 0 : break;
1492 : : }
1493 [ # # # # : 0 : } while ((cmpInChI & IDIF_EXTRA_TG_ENDP) && at_norm && nDeltaCur && nDeltaCur < nDeltaPrev);
# # # # ]
1494 : : }
1495 : : /************************ case of Fixed-H ******************************************************/
1496 : :
1497 [ # # ]: 0 : if (pStruct->bMobileH == TAUT_NON)
1498 : : {
1499 : 0 : int num_tries = 0;
1500 : : do
1501 : : {
1502 [ # # ]: 0 : if (0 > (ret = FixFixedHRestoredStructure(pCG, ic, ip, sd, pBNS, pBD, pStruct, at, at2, at3, pVA, pTCGroups,
1503 : : &t_group_info, &at_norm, &at_prep, pInChI,
1504 : : num_inp, bHasSomeFixedH, pnNumRunBNS, pnTotalDelta, forbidden_edge_mask,
1505 : : forbidden_stereo_edge_mask)))
1506 : : {
1507 : 0 : goto exit_function;
1508 : : }
1509 [ # # # # ]: 0 : } while (num_tries++ < 2 && ret > 0);
1510 : : }
1511 : : /************************ case of Fixed-H ******************************************************/
1512 [ # # ]: 0 : if (pStruct->bMobileH == TAUT_YES)
1513 : : {
1514 [ # # ]: 0 : if (0 > (ret = FixMobileHRestoredStructure(pCG, ic, ip, sd, pBNS, pBD, pStruct, at, at2, at3, pVA, pTCGroups,
1515 : : &t_group_info, &at_norm, &at_prep, pInChI,
1516 : : num_inp, bHasSomeFixedH, pnNumRunBNS, pnTotalDelta, forbidden_edge_mask,
1517 : : forbidden_stereo_edge_mask)))
1518 : : {
1519 : 0 : goto exit_function;
1520 : : }
1521 : : }
1522 : : /**********************************************************************************************/
1523 : : /* stereo */
1524 : 0 : cmpInChI = CompareReversedINChI2(pStruct->pOneINChI[0], pInChI[0], pStruct->pOneINChI_Aux[0], NULL /*INChI_Aux *a2*/, &icr, &err);
1525 [ # # ]: 0 : if (cmpInChI & IDIF_PROBLEM)
1526 : : {
1527 : 0 : ret = RI_ERR_PROGR; /* severe restore problem */
1528 : 0 : goto exit_function;
1529 : : }
1530 [ # # ]: 0 : if (err)
1531 : : {
1532 : 0 : ret = RI_ERR_ALLOC;
1533 : 0 : goto exit_function;
1534 : : }
1535 : 0 : cmpInChI2 = 0;
1536 : 0 : memset(&icr2, 0, sizeof(icr2)); /* djb-rwth: memset_s C11/Annex K variant? */
1537 [ # # # # ]: 0 : if (iRevrInChI || iOrigInChI)
1538 : : {
1539 : : /* additional mobile-H compare in case of Fixed-H */
1540 : 0 : cmpInChI2 = CompareReversedINChI2(pStruct->pOneINChI[iRevrInChI], pInChI[iOrigInChI], pStruct->pOneINChI_Aux[iRevrInChI], NULL /*INChI_Aux *a2*/, &icr2, &err);
1541 [ # # ]: 0 : if (cmpInChI & IDIF_PROBLEM)
1542 : : {
1543 : 0 : ret = RI_ERR_PROGR; /* severe restore problem */
1544 : 0 : goto exit_function;
1545 : : }
1546 [ # # ]: 0 : if (err)
1547 : : {
1548 : 0 : ret = RI_ERR_ALLOC;
1549 : 0 : goto exit_function;
1550 : : }
1551 : : }
1552 : 0 : ret = FixRestoredStructureStereo(pCG, ic,
1553 : : cmpInChI, &icr, cmpInChI2, &icr2,
1554 : : ip, sd, pBNS, pBD, pStruct, at, at2, at3, pVA, pTCGroups,
1555 : : &t_group_info, &at_norm, &at_prep, pInChI,
1556 : : num_inp, pnNumRunBNS, pnTotalDelta, forbidden_edge_mask,
1557 : : forbidden_stereo_edge_mask);
1558 : :
1559 [ # # ]: 0 : if (ret < 0)
1560 : : {
1561 : 0 : goto exit_function;
1562 : : }
1563 : : #if ( FIX_ADD_PROTON_FOR_ADP == 1 )
1564 : : /************************ check and fix ADP by adding a proton (dummy) *************************/
1565 : : if (cmpInChI && pTCGroups->num_tgroups && pBNS->tot_st_cap > pBNS->tot_st_flow)
1566 : : {
1567 : : ret = FixAddProtonForADP(pBNS, pBD, pStruct, at, at2, at_prep, pVA, pTCGroups, &icr,
1568 : : pnNumRunBNS, pnTotalDelta, forbidden_edge_mask);
1569 : : if (ret < 0)
1570 : : {
1571 : : goto exit_function;
1572 : : }
1573 : : }
1574 : : #endif
1575 : : /* moved to MakeOneInChIOutOfStrFromINChI():
1576 : : pStruct->nNumRemovedProtons = (pStruct->iMobileH == TAUT_YES)? pStruct->One_ti.tni.nNumRemovedProtons : 0;
1577 : : */
1578 : :
1579 : : /* count endpoints */
1580 : 0 : num_endpoints = 0;
1581 : 0 : num_norm_endpoints = 0;
1582 : 0 : num_norm_t_groups = 0;
1583 : : /* djb-rwth: removing redundant code */
1584 : 0 : at_norm = pStruct->pOne_norm_data[0]->at;
1585 [ # # ]: 0 : for (i = 0; i < pTCGroups->num_tgroups; i++)
1586 : : {
1587 : 0 : num_endpoints += pTCGroups->pTCG[i].num_edges;
1588 : : /* djb-rwth: removing redundant code */
1589 : : }
1590 : :
1591 [ # # ]: 0 : if (t_group_info)
1592 : : {
1593 : : /* after canonicalization, t_group_info->t_group[i].num[0] = number of H */
1594 : : /* t_group_info->t_group[i].num[1] = number of (-) */
1595 [ # # ]: 0 : for (i = 0; i < t_group_info->num_t_groups; i++)
1596 : : {
1597 [ # # ]: 0 : if (t_group_info->t_group[i].num[0])
1598 : : {
1599 : 0 : num_norm_t_groups++;
1600 : 0 : num_norm_endpoints += t_group_info->t_group[i].nNumEndpoints;
1601 : : /* djb-rwth: removing redundant code */
1602 : : }
1603 : : }
1604 : : }
1605 : : #if ( bRELEASE_VERSION == 0 )
1606 : : #ifndef TARGET_API_LIB
1607 : : if (num_norm_t_groups != pTCGroups->num_tgroups || num_norm_endpoints != num_endpoints)
1608 : : {
1609 : : /* need aggressive (de)protonation */
1610 : : /* pStruct->bExtract |= EXTRACT_STRUCT_NUMBER; */
1611 : : fprintf(stdout, "NORMCOMP: %s comp=%d %c%c: InChI/NormRvrs NumTg=%d/%d NumEndp=%d/%d\n",
1612 : : (*ip).pSdfValue, (*pTCGroups).iComponent,
1613 : : pStruct->iInchiRec ? 'R' : 'D', pStruct->iMobileH ? 'M' : 'F',
1614 : : pTCGroups->num_tgroups, num_norm_t_groups,
1615 : : num_endpoints, num_norm_endpoints);
1616 : : }
1617 : : #endif
1618 : : #endif
1619 : :
1620 : 0 : exit_function:
1621 : :
1622 [ # # ]: 0 : for (i = 0; i < TAUT_NUM; i++)
1623 : : {
1624 : 0 : Free_INChI(&pStruct->pOneINChI[i]);
1625 : 0 : Free_INChI_Aux(&pStruct->pOneINChI_Aux[i]);
1626 : 0 : FreeInpAtomData(pStruct->pOne_norm_data[i]);
1627 [ # # ]: 0 : if (pStruct->pOne_norm_data[i])
1628 : : {
1629 [ # # ]: 0 : inchi_free(pStruct->pOne_norm_data[i]);
1630 : 0 : pStruct->pOne_norm_data[i] = NULL;
1631 : : }
1632 : : }
1633 : :
1634 : 0 : free_t_group_info(&pStruct->One_ti);
1635 : :
1636 : 0 : return ret;
1637 : : }
1638 : :
1639 : :
1640 : : /****************************************************************************
1641 : : Find A=X< where all bonds to X except A=X are marked as stereogenic;
1642 : : temporary allow stereobonds change and make A=X bonds single */
1643 : 0 : int CheckAndRefixStereobonds(BN_STRUCT* pBNS, BN_DATA* pBD, StrFromINChI* pStruct,
1644 : : inp_ATOM* at, inp_ATOM* at2, VAL_AT* pVA, ALL_TC_GROUPS* pTCGroups,
1645 : : int* pnNumRunBNS, int* pnTotalDelta, int forbidden_edge_mask)
1646 : : {
1647 : 0 : int forbidden_edge_stereo = BNS_EDGE_FORBIDDEN_MASK;
1648 : 0 : int inv_forbidden_edge_stereo = ~forbidden_edge_stereo;
1649 : :
1650 : : int i, k, ne, j1, j2, num_wrong, num_fixed;
1651 : : int ret2, retBNS, ret;
1652 : 0 : int num_at = pStruct->num_atoms;
1653 : 0 : int num_deleted_H = pStruct->num_deleted_H;
1654 : 0 : int len_at = num_at + num_deleted_H;
1655 : : EDGE_LIST FixedEdges, WrongEdges, CarbonChargeEdges;
1656 : :
1657 : : BNS_EDGE* pEdge;
1658 : : Vertex v1, v2;
1659 : : BNS_VERTEX* pv1, * pv2;
1660 : :
1661 : 0 : ret = 0;
1662 : :
1663 : : /* to simplify, prepare new at[] from pBNS */
1664 : 0 : memcpy(at2, at, len_at * sizeof(at2[0]));
1665 : 0 : pStruct->at = at2;
1666 : 0 : ret2 = CopyBnsToAtom(pStruct, pBNS, pVA, pTCGroups, 1);
1667 : 0 : pStruct->at = at;
1668 [ # # ]: 0 : if (ret2 < 0)
1669 : : {
1670 : 0 : return ret;
1671 : : }
1672 : :
1673 : 0 : num_wrong = 0;
1674 : : /* find wrong double bonds */
1675 [ # # ]: 0 : for (i = 0; i < num_at; i++)
1676 : : {
1677 [ # # ]: 0 : if (at2[i].valence == 3 &&
1678 [ # # ]: 0 : at2[i].chem_bonds_valence - at2[i].valence == 1 &&
1679 [ # # # # : 0 : at2[i].sb_parity[0] && at2[i].sb_parity[1] && !at2[i].sb_parity[2] &&
# # ]
1680 [ # # ]: 0 : (at2[i].bond_type[j1 = (int)at2[i].sb_ord[0]] & BOND_TYPE_MASK) == BOND_TYPE_SINGLE &&
1681 [ # # # # ]: 0 : (at2[i].bond_type[j2 = (int)at2[i].sb_ord[1]] & BOND_TYPE_MASK) == BOND_TYPE_SINGLE &&
1682 : : j1 != j2)
1683 : : {
1684 : :
1685 : 0 : num_wrong++;
1686 : : }
1687 : : }
1688 [ # # ]: 0 : if (!num_wrong)
1689 : : {
1690 : 0 : return 0;
1691 : : }
1692 : 0 : num_fixed = 0;
1693 [ # # ]: 0 : for (i = 0; i < pBNS->num_bonds; i++)
1694 : : {
1695 : 0 : pEdge = pBNS->edge + i;
1696 [ # # ]: 0 : if (pEdge->forbidden & forbidden_edge_stereo)
1697 : : {
1698 : 0 : num_fixed++;
1699 : : }
1700 : : }
1701 : :
1702 : : /* there may be no fixed stereo bonds at all, see #87607 */
1703 : 0 : AllocEdgeList(&CarbonChargeEdges, EDGE_LIST_CLEAR);
1704 : 0 : AllocEdgeList(&FixedEdges, EDGE_LIST_CLEAR);
1705 : 0 : AllocEdgeList(&WrongEdges, EDGE_LIST_CLEAR);
1706 : :
1707 : : /* do not goto exit_function before reaching this point: EdgeLists have not been initiated */
1708 : :
1709 [ # # ]: 0 : if (0 > (ret = ForbidCarbonChargeEdges(pBNS, pTCGroups, &CarbonChargeEdges, forbidden_edge_mask)))
1710 : : {
1711 : 0 : goto exit_function;
1712 : : }
1713 [ # # # # ]: 0 : if ((ret = AllocEdgeList(&FixedEdges, num_fixed)) ||
1714 : 0 : (ret = AllocEdgeList(&WrongEdges, num_wrong)))
1715 : : {
1716 : 0 : goto exit_function;
1717 : : }
1718 : : /* collect wrong double bonds and set flow=0 */
1719 [ # # # # ]: 0 : for (i = 0; i < num_at && WrongEdges.num_edges < num_wrong; i++)
1720 : : {
1721 [ # # ]: 0 : if (at2[i].valence == 3 &&
1722 [ # # ]: 0 : at2[i].chem_bonds_valence - at2[i].valence == 1 &&
1723 [ # # # # : 0 : at2[i].sb_parity[0] && at2[i].sb_parity[1] && !at2[i].sb_parity[2] &&
# # ]
1724 [ # # ]: 0 : (at2[i].bond_type[j1 = (int)at2[i].sb_ord[0]] & BOND_TYPE_MASK) == BOND_TYPE_SINGLE &&
1725 [ # # # # ]: 0 : (at2[i].bond_type[j2 = (int)at2[i].sb_ord[1]] & BOND_TYPE_MASK) == BOND_TYPE_SINGLE &&
1726 : : j1 != j2)
1727 : : {
1728 [ # # # # ]: 0 : switch (j1 + j2)
1729 : : {
1730 : 0 : case 1: /* 0, 1 */
1731 : 0 : k = 2;
1732 : 0 : break;
1733 : 0 : case 2: /* 0, 2 */
1734 : 0 : k = 1;
1735 : 0 : break;
1736 : 0 : case 3: /* 1, 2 */
1737 : 0 : k = 0;
1738 : 0 : break;
1739 : 0 : default:
1740 : 0 : ret = RI_ERR_PROGR;
1741 : 0 : goto exit_function;
1742 : : }
1743 : 0 : ne = pBNS->vert[i].iedge[k];
1744 : 0 : pEdge = pBNS->edge + ne;
1745 : 0 : v1 = pEdge->neighbor1;
1746 : 0 : v2 = pEdge->neighbor12 ^ v1;
1747 : 0 : pv1 = pBNS->vert + v1;
1748 : 0 : pv2 = pBNS->vert + v2;
1749 : :
1750 [ # # ]: 0 : if (!pEdge->flow)
1751 : : {
1752 : 0 : ret = RI_ERR_PROGR;
1753 : 0 : goto exit_function;
1754 : : }
1755 : 0 : pEdge->flow--;
1756 : 0 : pEdge->forbidden |= forbidden_edge_mask;
1757 : 0 : pv1->st_edge.flow--;
1758 : 0 : pv2->st_edge.flow--;
1759 : 0 : pBNS->tot_st_flow -= 2;
1760 [ # # ]: 0 : if ((ret = AddToEdgeList(&WrongEdges, ne, 0))) /* djb-rwth: addressing LLVM warning */
1761 : : {
1762 : 0 : goto exit_function;
1763 : : }
1764 : : }
1765 : : }
1766 : : /* remove forbidden mark from stereo bonds (unfix stereo bonds) */
1767 [ # # # # ]: 0 : for (i = 0; i < pBNS->num_bonds && FixedEdges.num_edges < num_fixed; i++)
1768 : : {
1769 : 0 : pEdge = pBNS->edge + i;
1770 [ # # ]: 0 : if (pEdge->forbidden & forbidden_edge_stereo)
1771 : : {
1772 : 0 : pEdge->forbidden &= inv_forbidden_edge_stereo;
1773 : 0 : FixedEdges.pnEdges[FixedEdges.num_edges++] = i;
1774 : : }
1775 : : }
1776 : : /* Run BNS to move charges and rearrange bond orders */
1777 : 0 : retBNS = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
1778 : 0 : (*pnNumRunBNS)++;
1779 [ # # ]: 0 : if (retBNS < 0)
1780 : : {
1781 : 0 : goto exit_function;
1782 : : }
1783 : : else
1784 : : {
1785 [ # # ]: 0 : if (retBNS > 0)
1786 : : {
1787 : 0 : *pnTotalDelta += retBNS;
1788 : : }
1789 : : }
1790 : : /* remove forbidden_edge_mask and set forbidden_edge_stereo */
1791 : 0 : RemoveForbiddenEdgeMask(pBNS, &WrongEdges, forbidden_edge_mask);
1792 : : /* allow carbon charges to change */
1793 : 0 : RemoveForbiddenEdgeMask(pBNS, &CarbonChargeEdges, forbidden_edge_mask);
1794 : : /* fix previously unfixed stereo bonds */
1795 : 0 : SetForbiddenEdgeMask(pBNS, &FixedEdges, forbidden_edge_stereo);
1796 : : /* Run BNS again in case not all edge flows are maximal */
1797 : 0 : ret2 = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
1798 : 0 : (*pnNumRunBNS)++;
1799 [ # # ]: 0 : if (ret2 < 0)
1800 : : {
1801 : 0 : goto exit_function;
1802 : : }
1803 : : else
1804 : : {
1805 [ # # ]: 0 : if (ret2 > 0)
1806 : : {
1807 : 0 : *pnTotalDelta += retBNS;
1808 : : }
1809 : : }
1810 : 0 : ret = retBNS;
1811 : :
1812 : 0 : exit_function:
1813 : :
1814 : 0 : AllocEdgeList(&CarbonChargeEdges, EDGE_LIST_FREE);
1815 : 0 : AllocEdgeList(&FixedEdges, EDGE_LIST_FREE);
1816 : 0 : AllocEdgeList(&WrongEdges, EDGE_LIST_FREE);
1817 : :
1818 : 0 : return ret;
1819 : : }
1820 : :
1821 : :
1822 : : /****************************************************************************
1823 : : Find and eliminate false Mobile-H groups: Cl(=O)3(-O(-)) => Cl(-)(=O)4
1824 : : ****************************************************************************/
1825 : 0 : int MoveChargeToRemoveCenerpoints(BN_STRUCT* pBNS,
1826 : : BN_DATA* pBD,
1827 : : StrFromINChI* pStruct,
1828 : : inp_ATOM* at,
1829 : : inp_ATOM* at2,
1830 : : VAL_AT* pVA,
1831 : : ALL_TC_GROUPS* pTCGroups,
1832 : : int* pnNumRunBNS,
1833 : : int* pnTotalDelta,
1834 : : int forbidden_edge_mask)
1835 : : {
1836 : : int i, j, neigh, num_success; /* djb-rwth: removing redundant variables */
1837 : : int num_donors, num_acceptors, bond_type, num_donors_O, num_acceptors_O, is_centerpoint_N, num_known_endpoints, num_wrong_neigh;
1838 : : int ret2, ret_forbid_edges, ret, delta;
1839 : 0 : int num_at = pStruct->num_atoms;
1840 : 0 : int num_deleted_H = pStruct->num_deleted_H;
1841 : 0 : int len_at = num_at + num_deleted_H;
1842 : 0 : int forbidden_edge_test = BNS_EDGE_FORBIDDEN_TEST;
1843 [ # # # # : 0 : int bPossiblyIgnore = pStruct->charge >= 0 && (!pTCGroups->num_tgroups || (pStruct->iMobileH == TAUT_NON && pStruct->ti.num_t_groups)); /* djb-rwth: addressing LLVM warning */
# # # # ]
1844 : : S_CHAR MobileChargeNeigh[MAXVAL], DoubleBondAcceptors[MAXVAL], DoubleBondNotONeigh[MAXVAL];
1845 : : int numMobileChargeNeigh, numDoubleBondAcceptors, numDoubleBondNotONeigh; /* djb-rwth: removing redundant variables */
1846 : : EDGE_LIST ChargeListAllExcept_DB_O;
1847 : :
1848 : :
1849 : : BNS_EDGE* pEdgeMinus, * pe;
1850 : : Vertex v1m, v2m;
1851 : : BNS_VERTEX* pv1m, * pv2m;
1852 : : /* djb-rwth: removing redundant code */
1853 : 0 : num_success = 0;
1854 : :
1855 : : /* count O(+)H, N(+)H */
1856 : :
1857 : : /*
1858 : : if ( pStruct->charge >= 0 && (!pTCGroups->num_tgroups || pStruct->iMobileH == TAUT_NON && pStruct->ti.num_t_groups) ) {
1859 : : goto exit_function;
1860 : : }
1861 : : */
1862 [ # # ]: 0 : if ((ret = AllocEdgeList(&ChargeListAllExcept_DB_O, EDGE_LIST_CLEAR))) /* djb-rwth: addressing LLVM warning */
1863 : : {
1864 : 0 : goto exit_function;
1865 : : }
1866 : :
1867 : :
1868 : : /* to simplify, prepare new at[] from pBNS */
1869 : 0 : memcpy(at2, at, len_at * sizeof(at2[0]));
1870 : 0 : pStruct->at = at2;
1871 : 0 : ret2 = CopyBnsToAtom(pStruct, pBNS, pVA, pTCGroups, 1);
1872 : 0 : pStruct->at = at;
1873 [ # # ]: 0 : if (ret2 < 0)
1874 : : {
1875 : 0 : ret = ret2;
1876 : 0 : goto exit_function;
1877 : : }
1878 : : #if ( FIND_RING_SYSTEMS == 1 )
1879 : 0 : ret2 = MarkRingSystemsInp(at2, num_at, 0);
1880 [ # # ]: 0 : if (ret2 < 0)
1881 : : {
1882 : 0 : ret = ret2;
1883 : 0 : goto exit_function;
1884 : : }
1885 : : #endif
1886 : : /* mark bonds that cannot be tautomeric; do not forget to remove the marks later */
1887 : 0 : ret_forbid_edges = SetForbiddenEdges(pBNS, at2, num_at, forbidden_edge_test, 0, NULL);
1888 [ # # ]: 0 : if (ret_forbid_edges < 0)
1889 : : {
1890 : 0 : ret = ret_forbid_edges;
1891 : 0 : goto exit_function;
1892 : : }
1893 : :
1894 [ # # ]: 0 : for (i = 0; i < num_at; i++)
1895 : : {
1896 [ # # ]: 0 : if (pVA[i].cNumValenceElectrons != 4 && /* not C, Si, Ge */
1897 [ # # # # : 0 : !(pVA[i].nTautGroupEdge || (pStruct->iMobileH == TAUT_NON && pStruct->endpoint && pStruct->endpoint[i])) &&
# # # # ]
1898 [ # # # # : 0 : !at2[i].num_H && !at2[i].charge && at2[i].valence >= 2 &&
# # ]
1899 [ # # # # ]: 0 : at2[i].valence < at2[i].chem_bonds_valence &&
1900 : 0 : is_centerpoint_elem(at2[i].el_number)) /* djb-rwth: addressing LLVM warning */
1901 : : {
1902 : :
1903 [ # # # # : 0 : is_centerpoint_N = (pVA[i].cNumValenceElectrons == 5 && (pVA[i].cPeriodicRowNumber == 1 || pVA[i].cMetal));
# # ]
1904 : : /* look at the neighbors */
1905 : 0 : numMobileChargeNeigh = numDoubleBondAcceptors = numDoubleBondNotONeigh = num_donors = num_acceptors = 0;
1906 : 0 : num_donors_O = num_acceptors_O = 0;
1907 : 0 : num_known_endpoints = num_wrong_neigh = 0;
1908 [ # # ]: 0 : for (j = 0; j < at2[i].valence; j++) /* djb-rwth: removing redundant code */
1909 : : {
1910 : 0 : neigh = at2[i].neighbor[j];
1911 [ # # # # : 0 : if ((at2[neigh].endpoint || (pStruct->iMobileH == TAUT_NON && pStruct->endpoint && pStruct->endpoint[neigh])) || at2[neigh].charge > 0) /* djb-rwth: addressing LLVM warning */
# # # # #
# ]
1912 : : {
1913 : 0 : num_known_endpoints++;
1914 : 0 : continue;
1915 : : }
1916 [ # # ]: 0 : if (pBNS->edge[pBNS->vert[i].iedge[j]].forbidden & forbidden_edge_test)
1917 : : {
1918 : 0 : continue;
1919 : : }
1920 : 0 : bond_type = at2[i].bond_type[j] & BOND_TYPE_MASK;
1921 [ # # ]: 0 : if (bond_type > BOND_TYPE_DOUBLE)
1922 : : {
1923 : 0 : num_wrong_neigh++;
1924 : 0 : continue;
1925 : : }
1926 [ # # # # ]: 0 : if (at2[neigh].num_H && bond_type == BOND_TYPE_SINGLE)
1927 : : {
1928 : 0 : break; /* not this case */
1929 : : }
1930 [ # # ]: 0 : if (at2[neigh].chem_bonds_valence - at2[neigh].charge
1931 : 0 : != get_endpoint_valence(at2[neigh].el_number))
1932 : : {
1933 [ # # # # ]: 0 : if (bond_type == BOND_TYPE_DOUBLE && pVA[neigh].cNumValenceElectrons != 6)
1934 : : {
1935 : 0 : DoubleBondNotONeigh[numDoubleBondNotONeigh++] = j;
1936 : : }
1937 : 0 : continue;
1938 : : }
1939 [ # # # # ]: 0 : if (at2[neigh].charge == -1 && bond_type == BOND_TYPE_SINGLE &&
1940 [ # # # # ]: 0 : (pVA[neigh].nCMinusGroupEdge < 1 || pBNS->edge[pVA[neigh].nCMinusGroupEdge - 1].flow != 1))
1941 : : {
1942 : : break;
1943 : : }
1944 [ # # # ]: 0 : switch (bond_type)
1945 : : {
1946 : 0 : case BOND_TYPE_SINGLE:
1947 [ # # # # ]: 0 : if (at2[neigh].charge != -1 || pVA[neigh].nCMinusGroupEdge <= 0)
1948 : : {
1949 : 0 : num_wrong_neigh++;
1950 : 0 : continue;
1951 : : }
1952 : 0 : num_donors++;
1953 [ # # # # ]: 0 : num_donors_O += (pVA[neigh].cNumValenceElectrons == 6 && pVA[neigh].cPeriodicRowNumber <= 4);
1954 : 0 : MobileChargeNeigh[numMobileChargeNeigh++] = j;
1955 : 0 : break;
1956 : 0 : case BOND_TYPE_DOUBLE:
1957 [ # # ]: 0 : if (at2[neigh].charge)
1958 : : {
1959 : 0 : num_wrong_neigh++;
1960 : 0 : continue;
1961 : : }
1962 : 0 : DoubleBondAcceptors[numDoubleBondAcceptors++] = j;
1963 : 0 : num_acceptors++;
1964 [ # # # # ]: 0 : num_acceptors_O += (pVA[neigh].cNumValenceElectrons == 6 && pVA[neigh].cPeriodicRowNumber <= 4);
1965 : : }
1966 : : }
1967 [ # # # # : 0 : if (j != at2[i].valence || !num_donors || !num_acceptors)
# # ]
1968 : : {
1969 : 0 : continue;
1970 : : }
1971 : : /* special case NOn(-) */
1972 [ # # # # : 0 : if (is_centerpoint_N && (num_donors == num_donors_O) && (num_acceptors == num_acceptors_O))
# # ]
1973 : : {
1974 : 0 : continue;
1975 : : }
1976 [ # # # # ]: 0 : if (pStruct->iMobileH == TAUT_NON && num_donors == numDoubleBondNotONeigh)
1977 : 0 : {
1978 : : /* fix all charges except on =O */
1979 : : Vertex vPathStart, vPathEnd;
1980 : : int nPathLen, nDeltaH, nDeltaCharge, nNumVisitedAtoms;
1981 : 0 : int k, e, num_MovedCharges = 0;
1982 : :
1983 [ # # ]: 0 : if (!ChargeListAllExcept_DB_O.num_edges)
1984 : : {
1985 : : /* djb-rwth: removing redundant code */
1986 [ # # ]: 0 : for (k = 0; k < num_at; k++)
1987 : : {
1988 [ # # # # ]: 0 : if (!((1 == at2[k].valence && pBNS->edge[pBNS->vert[k].iedge[0]].flow &&
1989 [ # # ]: 0 : !pBNS->edge[pBNS->vert[k].iedge[0]].forbidden &&
1990 [ # # # # ]: 0 : !((e = pVA[k].nCMinusGroupEdge - 1) >= 0 && pBNS->edge[e].flow) &&
1991 [ # # # # ]: 0 : !((e = pVA[k].nCPlusGroupEdge - 1) >= 0 && !pBNS->edge[e].flow) &&
1992 : : /* 0 == at2[k].charge && */
1993 [ # # # # ]: 0 : pVA[k].cNumValenceElectrons == 6 && !pVA[k].cMetal &&
1994 [ # # # # ]: 0 : (pStruct->endpoint && pStruct->endpoint[k])) ||
1995 [ # # # # ]: 0 : (pStruct->fixed_H && pStruct->fixed_H[k]))) /* djb-rwth: addressing LLVM warnings */
1996 : : /* djb-rwth: removing redundant code */
1997 [ # # # # ]: 0 : if ((e = pVA[k].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].flow &&
1998 [ # # # # ]: 0 : !pBNS->edge[e].forbidden &&
1999 : 0 : (ret = AddToEdgeList(&ChargeListAllExcept_DB_O, e, 64)))
2000 : : {
2001 : 0 : goto exit_function;
2002 : : }
2003 [ # # ]: 0 : if ((e = pVA[k].nCPlusGroupEdge - 1) >= 0 &&
2004 [ # # # # ]: 0 : !pBNS->edge[e].forbidden &&
2005 : 0 : (ret = AddToEdgeList(&ChargeListAllExcept_DB_O, e, 64)))
2006 : : {
2007 : 0 : goto exit_function;
2008 : : }
2009 : : }
2010 : : }
2011 : : /* fix double bonds to non-O neighbors connected by double bonds;
2012 : : we will try to make these bons single */
2013 [ # # ]: 0 : for (k = 0; k < numDoubleBondNotONeigh; k++)
2014 : : {
2015 : 0 : e = pBNS->vert[i].iedge[(int)DoubleBondNotONeigh[k]];
2016 [ # # # # ]: 0 : if (!pBNS->edge[e].forbidden &&
2017 : 0 : (ret = AddToEdgeList(&ChargeListAllExcept_DB_O, e, 64)))
2018 : : {
2019 : 0 : goto exit_function;
2020 : : }
2021 : : }
2022 : : /* attempt to make DoubleBondNotONeigh[] single */
2023 : 0 : SetForbiddenEdgeMask(pBNS, &ChargeListAllExcept_DB_O, forbidden_edge_mask);
2024 [ # # # # ]: 0 : for (k = 0; k < numDoubleBondNotONeigh && num_MovedCharges < numMobileChargeNeigh; k++)
2025 : : {
2026 : 0 : pe = pBNS->edge + pBNS->vert[i].iedge[(int)DoubleBondNotONeigh[k]];
2027 : 0 : delta = 1;
2028 [ # # ]: 0 : if (pe->flow != delta)
2029 : 0 : continue;
2030 : 0 : pv1m = pBNS->vert + (v1m = pe->neighbor1);
2031 : 0 : pv2m = pBNS->vert + (v2m = pe->neighbor12 ^ v1m);
2032 : 0 : pv1m->st_edge.flow -= delta;
2033 : 0 : pv2m->st_edge.flow -= delta;
2034 : 0 : pe->flow -= delta;
2035 : 0 : pBNS->tot_st_flow -= 2 * delta;
2036 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
2037 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
2038 [ # # ]: 0 : if (ret < 0)
2039 : : {
2040 : 0 : goto exit_function;
2041 : : }
2042 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1m && vPathStart == v2m) ||
# # ]
2043 [ # # # # ]: 0 : (vPathEnd == v2m && vPathStart == v1m)) &&
2044 [ # # ]: 0 : nDeltaCharge == 0 /* (-) moving from one to another atom*/) /* djb-rwth: addressing LLVM warnings */
2045 : : {
2046 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2047 : 0 : (*pnNumRunBNS)++;
2048 [ # # ]: 0 : if (ret < 0)
2049 : : {
2050 : 0 : goto exit_function;
2051 : : }
2052 : : else
2053 : : {
2054 [ # # ]: 0 : if (ret == 1)
2055 : : {
2056 : 0 : *pnTotalDelta += ret;
2057 : 0 : num_MovedCharges++;
2058 : : }
2059 : : else
2060 : : {
2061 : 0 : ret = RI_ERR_PROGR;
2062 : 0 : goto exit_function;
2063 : : }
2064 : : }
2065 : : }
2066 : : else
2067 : : {
2068 : : /* djb-rwth: removing redundant code */
2069 : 0 : pv1m->st_edge.flow += delta;
2070 : 0 : pv2m->st_edge.flow += delta;
2071 : 0 : pe->flow += delta;
2072 : 0 : pBNS->tot_st_flow += 2 * delta;
2073 : : }
2074 : : }
2075 : 0 : RemoveForbiddenEdgeMask(pBNS, &ChargeListAllExcept_DB_O, forbidden_edge_mask);
2076 : : }
2077 : : else
2078 : : {
2079 [ # # # # : 0 : if (!bPossiblyIgnore || (!num_known_endpoints && !num_wrong_neigh && (num_acceptors_O + num_donors_O >= 3))) /* djb-rwth: addressing LLVM warning */
# # # # ]
2080 : : {
2081 : : /* remove negative charges from the neighbors */
2082 : 0 : pBNS->vert[i].st_edge.cap += num_donors; /* enough to make all bonds to donors double */
2083 : 0 : pBNS->tot_st_cap += num_donors;
2084 : 0 : pVA[i].cInitCharge -= num_donors; /* work no matter what are known charge/valence */
2085 [ # # ]: 0 : for (j = 0; j < numMobileChargeNeigh; j++)
2086 : : {
2087 : 0 : neigh = at2[i].neighbor[(int)MobileChargeNeigh[j]];
2088 : 0 : pEdgeMinus = pBNS->edge + ((long long)pVA[neigh].nCMinusGroupEdge - 1); /* djb-rwth: cast operator added */
2089 : 0 : v1m = pEdgeMinus->neighbor1;
2090 : 0 : v2m = pEdgeMinus->neighbor12 ^ v1m;
2091 : 0 : pv1m = pBNS->vert + v1m;
2092 : 0 : pv2m = pBNS->vert + v2m;
2093 : 0 : delta = pEdgeMinus->flow;
2094 : 0 : pv1m->st_edge.flow -= delta;
2095 : 0 : pv2m->st_edge.flow -= delta;
2096 [ # # ]: 0 : if (IS_BNS_VT_C_GR(pv1m->type))
2097 : : {
2098 : : /* irreversible change to ChargeStruct */
2099 : 0 : pv1m->st_edge.cap -= delta;
2100 : : }
2101 : : else
2102 : : {
2103 [ # # ]: 0 : if (IS_BNS_VT_C_GR(pv2m->type))
2104 : : {
2105 : : /* irreversible change to ChargeStruct */
2106 : 0 : pv2m->st_edge.cap -= delta;
2107 : : }
2108 : : else
2109 : : {
2110 : 0 : ret = RI_ERR_PROGR;
2111 : 0 : goto exit_function;
2112 : : }
2113 : : }
2114 : 0 : pBNS->tot_st_cap -= delta;
2115 : 0 : pBNS->tot_st_flow -= 2 * delta;
2116 : 0 : pEdgeMinus->flow -= delta;
2117 : : }
2118 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2119 : 0 : (*pnNumRunBNS)++;
2120 [ # # ]: 0 : if (ret < 0)
2121 : : {
2122 : 0 : goto exit_function;
2123 : : }
2124 : : else
2125 [ # # ]: 0 : if (ret == num_donors)
2126 : : {
2127 : 0 : *pnTotalDelta += ret;
2128 : 0 : num_success++;
2129 : : /*pStruct->bExtract |= EXTRACT_STRUCT_NUMBER;*/
2130 : : }
2131 : : else
2132 : : {
2133 : 0 : ret = RI_ERR_PROGR;
2134 : 0 : goto exit_function;
2135 : : }
2136 : : }
2137 : : }
2138 : : }
2139 : : }
2140 [ # # ]: 0 : if (ret_forbid_edges)
2141 : : {
2142 : : /* remove the marks */
2143 : 0 : RemoveForbiddenBondFlowBits(pBNS, forbidden_edge_test);
2144 : : }
2145 : 0 : ret = num_success;
2146 : :
2147 : 0 : exit_function:
2148 : :
2149 : 0 : AllocEdgeList(&ChargeListAllExcept_DB_O, EDGE_LIST_FREE);
2150 : :
2151 : 0 : return ret;
2152 : : }
2153 : :
2154 : :
2155 : : /****************************************************************************
2156 : : Find and eliminate cases when Mobile H endpoint has radical on it
2157 : : (typical for wrong P(VI)(=O)3OH
2158 : : ****************************************************************************/
2159 : 0 : int MakeSingleBondsMetal2ChargedHeteroat(BN_STRUCT* pBNS,
2160 : : BN_DATA* pBD,
2161 : : StrFromINChI* pStruct,
2162 : : inp_ATOM* at,
2163 : : inp_ATOM* at2,
2164 : : VAL_AT* pVA,
2165 : : ALL_TC_GROUPS* pTCGroups,
2166 : : int* pnNumRunBNS,
2167 : : int* pnTotalDelta,
2168 : : int forbidden_edge_mask)
2169 : : {
2170 : : int i;
2171 : :
2172 : : int ret2, ret, pass;
2173 : 0 : int num_at = pStruct->num_atoms;
2174 : 0 : int num_deleted_H = pStruct->num_deleted_H;
2175 : 0 : int len_at = num_at + num_deleted_H;
2176 : 0 : int inv_forbidden_edge_mask = ~forbidden_edge_mask;
2177 : :
2178 : : int j, k;
2179 : : int cur_num_edges;
2180 : : BNS_EDGE* e;
2181 : : Vertex v1, v2;
2182 : :
2183 : : EdgeIndex* pFixedEdges;
2184 : : int nNumEdgesToFix;
2185 : :
2186 : 0 : ret = 0;
2187 : :
2188 : : /* to simplify, prepare new at[] from pBNS */
2189 : 0 : memcpy(at2, at, len_at * sizeof(at2[0]));
2190 : 0 : pStruct->at = at2;
2191 : 0 : ret2 = CopyBnsToAtom(pStruct, pBNS, pVA, pTCGroups, 1);
2192 : 0 : pStruct->at = at;
2193 [ # # ]: 0 : if (ret2 < 0)
2194 : : {
2195 : 0 : ret = ret2;
2196 : 0 : goto exit_function;
2197 : : }
2198 : :
2199 : 0 : pFixedEdges = NULL;
2200 : :
2201 : 0 : nNumEdgesToFix = 0; /* cpunt nNumEdgesToFix only when pass==0 */
2202 : 0 : cur_num_edges = 0; /* count cur_num_edges only when pass==1; at the end they must be equal */
2203 [ # # ]: 0 : for (pass = 0; pass < 2; pass++)
2204 : : {
2205 [ # # ]: 0 : if (pass)
2206 : : {
2207 : : /* 2nd pass: allocate edge storage */
2208 [ # # ]: 0 : if (!nNumEdgesToFix)
2209 : : {
2210 : 0 : break; /* nothing to do */
2211 : : }
2212 : 0 : pFixedEdges = (EdgeIndex*)inchi_malloc(nNumEdgesToFix * sizeof(pFixedEdges[0]));
2213 [ # # ]: 0 : if (!pFixedEdges)
2214 : : {
2215 : 0 : ret = RI_ERR_ALLOC;
2216 : 0 : goto exit_function;
2217 : : }
2218 : : }
2219 [ # # ]: 0 : for (i = 0; i < num_at; i++)
2220 : : {
2221 : : int neigh;
2222 [ # # ]: 0 : if (pVA[i].cMetal)
2223 : : {
2224 [ # # ]: 0 : for (j = 0; j < at2[i].valence; j++)
2225 : : {
2226 : 0 : neigh = at2[i].neighbor[j];
2227 [ # # ]: 0 : if (pVA[neigh].cNumValenceElectrons == 4 &&
2228 [ # # ]: 0 : pVA[neigh].cPeriodicRowNumber == 1)
2229 : : {
2230 : 0 : continue; /* ignore carbon */
2231 : : }
2232 [ # # # # ]: 0 : if (at2[i].bond_type[j] > BOND_TYPE_SINGLE && at2[neigh].charge &&
2233 [ # # # # ]: 0 : !pVA[neigh].cMetal && pVA[neigh].cnListIndex > 0)
2234 : : {
2235 [ # # ]: 0 : int cnBits = at2[neigh].charge > 0 ? MAKE_CN_BITS(cn_bits_N, cn_bits_P, 0, 0) :
2236 : : MAKE_CN_BITS(cn_bits_N, cn_bits_M, 0, 0);
2237 : 0 : int atBits = cnList[pVA[neigh].cnListIndex - 1].bits;
2238 [ # # ]: 0 : for (k = 0; k < MAX_NUM_CN_BITS - 1; k++, atBits >>= cn_bits_shift)
2239 : : {
2240 : : /* ??? */
2241 [ # # ]: 0 : if ((atBits & cnBits) == cnBits)
2242 : : {
2243 : 0 : break;
2244 : : }
2245 : : }
2246 [ # # ]: 0 : if (k == MAX_NUM_CN_BITS - 1)
2247 : : {
2248 : 0 : continue;
2249 : : }
2250 [ # # ]: 0 : if (pass == 0)
2251 : : {
2252 : 0 : nNumEdgesToFix++;
2253 : : }
2254 : : else
2255 : : {
2256 : 0 : pFixedEdges[cur_num_edges++] = pBNS->vert[i].iedge[j];
2257 : : }
2258 : : }
2259 : : }
2260 : : }
2261 : : }
2262 : : }
2263 : :
2264 : : /* restore the initial structures */
2265 : 0 : memcpy(at2, at, ((long long)num_at + (long long)num_deleted_H) * sizeof(at2[0])); /* djb-rwth: cast operators added */
2266 : :
2267 [ # # # # ]: 0 : if (nNumEdgesToFix && pFixedEdges)
2268 : : {
2269 [ # # ]: 0 : if (nNumEdgesToFix != cur_num_edges)
2270 : : {
2271 : 0 : ret = RI_ERR_PROGR;
2272 : 0 : goto pre_exit_function; /* djb-rwth: fixing coverity ID #499637 */
2273 : : }
2274 : : /* change edge flow, fix the edges, and run BNS */
2275 [ # # ]: 0 : for (i = 0; i < nNumEdgesToFix; i++)
2276 : : {
2277 : 0 : e = pBNS->edge + pFixedEdges[i];
2278 : 0 : v1 = e->neighbor1;
2279 : 0 : v2 = e->neighbor12 ^ v1;
2280 : 0 : e->flow--;
2281 : 0 : e->forbidden |= forbidden_edge_mask;
2282 : 0 : pBNS->vert[v1].st_edge.flow--;
2283 : 0 : pBNS->vert[v2].st_edge.flow--;
2284 : 0 : pBNS->tot_st_flow -= 2;
2285 : 0 : (*pnTotalDelta) -= 2;
2286 : : }
2287 : : /* Run BNS allowing to change any charges */
2288 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2289 : 0 : (*pnNumRunBNS)++;
2290 [ # # ]: 0 : if (ret < 0)
2291 : : {
2292 : 0 : goto pre_exit_function; /* djb-rwth: fixing coverity ID #499637 */
2293 : : }
2294 : : else
2295 : : {
2296 : 0 : (*pnTotalDelta) += ret;
2297 : : }
2298 : : /* unfix the edges */
2299 [ # # ]: 0 : for (i = 0; i < nNumEdgesToFix; i++)
2300 : : {
2301 : 0 : e = pBNS->edge + pFixedEdges[i];
2302 : 0 : e->forbidden &= inv_forbidden_edge_mask;
2303 : : }
2304 [ # # ]: 0 : if (ret < 2 * nNumEdgesToFix)
2305 : : {
2306 : : /* not all fixes succeeded */
2307 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2308 : 0 : (*pnNumRunBNS)++;
2309 [ # # ]: 0 : if (ret < 0)
2310 : : {
2311 : 0 : goto pre_exit_function; /* djb-rwth: fixing coverity ID #499637 */
2312 : : }
2313 : : else
2314 : : {
2315 : 0 : (*pnTotalDelta) += ret;
2316 : : }
2317 : : }
2318 : : }
2319 : :
2320 : 0 : pre_exit_function:
2321 [ # # ]: 0 : if (pFixedEdges)
2322 : : {
2323 [ # # ]: 0 : inchi_free(pFixedEdges);
2324 : 0 : pFixedEdges = NULL;
2325 : : }
2326 : :
2327 : 0 : exit_function:
2328 : 0 : return ret;
2329 : : }
2330 : :
2331 : :
2332 : :
2333 : : /**************************************************************************/
2334 : : /* In Reconnected structure change 'salt bonds' to 'coordination bonds */
2335 : : /* for example, M-O-C= -> M(+)-O(-)-C= */
2336 : : /* Defect: instead of NH2-C=O(+)-M it will restore NH2(+)=C-O(-)-M(+) */
2337 : : /* However, in this release metal-organic compounds do not get much care */
2338 : 0 : int SaltBondsToCoordBonds(BN_STRUCT* pBNS,
2339 : : BN_DATA* pBD,
2340 : : StrFromINChI* pStruct,
2341 : : inp_ATOM* at,
2342 : : inp_ATOM* at2,
2343 : : VAL_AT* pVA,
2344 : : ALL_TC_GROUPS* pTCGroups,
2345 : : int* pnNumRunBNS,
2346 : : int* pnTotalDelta,
2347 : : int forbidden_edge_mask)
2348 : : {
2349 : : int i;
2350 : :
2351 : : int ret2, ret, cur_success;
2352 : 0 : int num_at = pStruct->num_atoms;
2353 : 0 : int num_edges = pBNS->num_bonds + 2 * pBNS->num_atoms;
2354 : 0 : int num_deleted_H = pStruct->num_deleted_H;
2355 : 0 : int len_at = num_at + num_deleted_H;
2356 : 0 : int inv_forbidden_edge_mask = ~forbidden_edge_mask;
2357 : : EDGE_LIST AllChargeEdges;
2358 : :
2359 : : int j, k, n;
2360 : : BNS_EDGE* pe, * pePlusMetal, * peMinusO;
2361 : : BNS_VERTEX* pv1, * pv2, * pvO, * pvM;
2362 : : Vertex v1, v2, vPlusMinus;
2363 : :
2364 : : EdgeIndex ie, iePlusMetal, ieMinusO;
2365 : :
2366 : : Vertex vPathStart, vPathEnd;
2367 : : int delta, nPathLen, nDeltaH, nDeltaCharge, nNumVisitedAtoms;
2368 : :
2369 : 0 : ret = 0;
2370 : 0 : cur_success = 0;
2371 : 0 : AllocEdgeList(&AllChargeEdges, EDGE_LIST_CLEAR);
2372 : :
2373 [ # # # # : 0 : if (pStruct->iInchiRec == INCHI_BAS || !pStruct->pSrm->bMetalAddFlower || pStruct->pSrm->nMetalMinBondOrder)
# # ]
2374 : : {
2375 : 0 : goto exit_function;
2376 : : }
2377 : :
2378 : : /* to simplify, prepare new at[] from pBNS */
2379 : 0 : memcpy(at2, at, len_at * sizeof(at2[0]));
2380 : 0 : pStruct->at = at2;
2381 : 0 : ret2 = CopyBnsToAtom(pStruct, pBNS, pVA, pTCGroups, 1);
2382 : 0 : pStruct->at = at;
2383 [ # # ]: 0 : if (ret2 < 0)
2384 : : {
2385 : 0 : ret = ret2;
2386 : 0 : goto exit_function;
2387 : : }
2388 [ # # ]: 0 : for (i = 0; i < num_at; i++)
2389 : : {
2390 [ # # ]: 0 : if (bIsMetalSalt(at2, i))
2391 : : {
2392 [ # # ]: 0 : if (!AllChargeEdges.num_edges)
2393 : : {
2394 : : /*--------- one-time action: fix all bonds, charges, taut. group edges ------------*/
2395 [ # # ]: 0 : for (j = 0; j < num_at; j++)
2396 : : {
2397 : : /* all bonds */
2398 [ # # ]: 0 : for (k = 0; k < at2[j].valence; k++)
2399 : : {
2400 : 0 : n = at2[j].neighbor[k];
2401 [ # # # # : 0 : if (n < j && !pBNS->edge[ie = pBNS->vert[j].iedge[k]].forbidden &&
# # ]
2402 : 0 : (ret = AddToEdgeList(&AllChargeEdges, ie, num_edges)))
2403 : : {
2404 : 0 : goto exit_function;
2405 : : }
2406 : : }
2407 : : /* charge edges */
2408 [ # # # # : 0 : if ((ie = pVA[j].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[ie].forbidden &&
# # ]
2409 : 0 : (ret = AddToEdgeList(&AllChargeEdges, ie, num_edges)))
2410 : : {
2411 : 0 : goto exit_function;
2412 : : }
2413 [ # # # # : 0 : if ((ie = pVA[j].nCPlusGroupEdge - 1) >= 0 && !pBNS->edge[ie].forbidden &&
# # ]
2414 : 0 : (ret = AddToEdgeList(&AllChargeEdges, ie, num_edges)))
2415 : : {
2416 : 0 : goto exit_function;
2417 : : }
2418 : : }
2419 : : /* taut group edges */
2420 [ # # ]: 0 : for (j = 0; j < pTCGroups->num_tgroups; j++)
2421 : : {
2422 : 0 : pv1 = pBNS->vert + (v1 = pTCGroups->pTCG[j].nVertexNumber); /* t-group vertex */ /* djb-rwth: ignoring LLVM warning: see comment below */
2423 [ # # ]: 0 : for (k = 0; k < pv1->num_adj_edges; k++)
2424 : : {
2425 : : /* ie, pe - tautomeric atom edge; pv2 - endpoint vertex */
2426 : : /* Note: pe, pv2, v1 are not used here; they are to show how to traverse t-group */
2427 : 0 : pv2 = pBNS->vert + (pe = pBNS->edge + (ie = pv1->iedge[k]))->neighbor1; /* djb-rwth: ignoring LLVM warning: see comment above */
2428 [ # # ]: 0 : if ((ret = AddToEdgeList(&AllChargeEdges, ie, num_edges))) /* djb-rwth: addressing LLVM warning */
2429 : : {
2430 : 0 : goto exit_function;
2431 : : }
2432 : : }
2433 : : }
2434 : : /*---------------------------------------------------------------*/
2435 : : }
2436 : : /* replace all single bonds to neutral neighbors with zero-order bonds
2437 : : allow neighbor charge change to (-1) and metal atom charge increment +1 */
2438 [ # # ]: 0 : for (k = 0; k < at2[i].valence; k++)
2439 : : {
2440 : 0 : n = at2[i].neighbor[k];
2441 : 0 : pe = pBNS->edge + pBNS->vert[i].iedge[k];
2442 [ # # # # ]: 0 : if (at2[n].charge || at2[i].bond_type[k] != BOND_TYPE_SINGLE)
2443 : : {
2444 : 0 : continue;
2445 : : }
2446 : 0 : iePlusMetal = pVA[i].nCPlusGroupEdge - 1;
2447 : 0 : ieMinusO = pVA[n].nCMinusGroupEdge - 1;
2448 : :
2449 [ # # # # : 0 : if (pe->flow != 1 || pe->forbidden || iePlusMetal < 0)
# # ]
2450 : : {
2451 : 0 : continue;
2452 : : }
2453 : 0 : pePlusMetal = pBNS->edge + iePlusMetal;
2454 [ # # ]: 0 : if (pePlusMetal->flow <= 0)
2455 : : {
2456 : 0 : continue; /* to add (+) to metal this flow must be decremented */
2457 : : }
2458 [ # # ]: 0 : if (ieMinusO >= 0)
2459 : : {
2460 : : /* usually does not happen */
2461 : 0 : peMinusO = pBNS->edge + ieMinusO;
2462 : :
2463 [ # # # # : 0 : if (peMinusO->flow || pePlusMetal->forbidden || peMinusO->forbidden)
# # ]
2464 : : {
2465 : 0 : continue;
2466 : : }
2467 : :
2468 : : /* decrement bond order to 0 */
2469 : 0 : delta = 1;
2470 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
2471 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
2472 : :
2473 : 0 : pe->flow -= delta;
2474 : 0 : pv1->st_edge.flow -= delta;
2475 : 0 : pv2->st_edge.flow -= delta;
2476 : 0 : pBNS->tot_st_flow -= 2 * delta;
2477 : :
2478 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
2479 : 0 : pePlusMetal->forbidden &= inv_forbidden_edge_mask;
2480 : 0 : peMinusO->forbidden &= inv_forbidden_edge_mask;
2481 : :
2482 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
2483 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
2484 : :
2485 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
2486 [ # # # # ]: 0 : (vPathEnd == v2 && vPathStart == v1)) /*&& nDeltaCharge > 0*/) /* djb-rwth: addressing LLVM warnings */
2487 : : {
2488 : : /* (+)charge was just moved, no change in number of charges */
2489 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2490 [ # # ]: 0 : if (ret > 0)
2491 : : {
2492 : 0 : (*pnNumRunBNS)++;
2493 : 0 : cur_success++; /* 01 */
2494 : : }
2495 : : }
2496 : : else
2497 : : {
2498 : 0 : pe->flow += delta; /* roll back */
2499 : 0 : pv1->st_edge.flow += delta;
2500 : 0 : pv2->st_edge.flow += delta;
2501 : 0 : pBNS->tot_st_flow += 2 * delta;
2502 : : }
2503 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
2504 : : }
2505 : : else
2506 : : {
2507 [ # # ]: 0 : if (NO_VERTEX != (vPlusMinus = GetPlusMinusVertex(pBNS, pTCGroups, 1, 1)))
2508 : : {
2509 : : /* manually add (-) charge to O and (+) charge to metal */
2510 : : /* decrement bond order to 0 */
2511 : : /*---------------------------------------------------------------------------*/
2512 : : /* */
2513 : : /* (+/-)* (+/-) Result: */
2514 : : /* | || */
2515 : : /* | || - Added (+) to M */
2516 : : /* (+)super (+)super - Incremented bond M-O */
2517 : : /* || | */
2518 : : /* || => | To make this attachment H, */
2519 : : /* (Y) (Y) increment */
2520 : : /* | || pTCGroups->pTCG[itg].tg_num_H */
2521 : : /* | || */
2522 : : /* (+)metal (+)hetero Technical details: */
2523 : : /* \\ \ increase capacities of */
2524 : : /* M M(+) edges to (+/-) otherwise */
2525 : : /* | || flow may not be able to */
2526 : : /* -O* -O-O increase */
2527 : : /* */
2528 : : /* After that change M=O bond order from 2 to 0 */
2529 : : /*---------------------------------------------------------------------------*/
2530 : : int i1, j1, k1;
2531 : 0 : delta = 1;
2532 : 0 : pvO = pBNS->vert + n;
2533 : 0 : pvM = pBNS->vert + i;
2534 : : /* Increment st_edge.cap on (+/-) vertex */
2535 : 0 : pBNS->vert[vPlusMinus].st_edge.cap += delta;
2536 : : /* Increment st_edge.cap on O */
2537 : 0 : pvO->st_edge.cap += delta;
2538 : : /* increment cap on M-O edge */
2539 : 0 : pe->cap += delta;
2540 : : /* total cap count */
2541 : 0 : pBNS->tot_st_cap += 2 * delta;
2542 : :
2543 : 0 : v1 = vPlusMinus;
2544 : 0 : v2 = n; /* atom O */
2545 : :
2546 : : /* increase capacities of edges to Y */
2547 [ # # ]: 0 : for (i1 = 0; i1 < pBNS->vert[vPlusMinus].num_adj_edges; i1++)
2548 : : {
2549 : 0 : j1 = pBNS->edge[pBNS->vert[vPlusMinus].iedge[i1]].neighbor12 ^ vPlusMinus;
2550 [ # # ]: 0 : for (k1 = 0; k1 < pBNS->vert[j1].num_adj_edges; k1++)
2551 : : {
2552 : 0 : pBNS->edge[pBNS->vert[j1].iedge[k1]].cap += delta;
2553 : : }
2554 : : }
2555 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
2556 : 0 : pePlusMetal->forbidden &= inv_forbidden_edge_mask;
2557 : 0 : pe->forbidden &= inv_forbidden_edge_mask;
2558 : :
2559 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
2560 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
2561 : 0 : cur_success = 0;
2562 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
2563 [ # # # # ]: 0 : (vPathEnd == v2 && vPathStart == v1)) /*&& nDeltaCharge == 1*/) /* djb-rwth: addressing LLVM warnings */
2564 : : {
2565 : : /* Added (+)charge to -N< => nDeltaCharge == 1 */
2566 : : /* Flow change on pe (-)charge edge (atom B-O(-)) is not known to RunBnsTestOnce()) */
2567 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2568 [ # # ]: 0 : if (ret > 0)
2569 : : {
2570 : 0 : (*pnNumRunBNS)++;
2571 : 0 : cur_success++; /* 01 */
2572 : : }
2573 : : }
2574 [ # # ]: 0 : if (cur_success)
2575 : : {
2576 : : /* set bond M=O order = 0 */
2577 [ # # ]: 0 : if (pe->flow != 2 * delta)
2578 : : {
2579 : 0 : ret = RI_ERR_PROGR;
2580 : 0 : goto exit_function;
2581 : : }
2582 : : /* reduce pe bond order by 2*delta */
2583 : 0 : pe->flow -= 2 * delta;
2584 : 0 : pvO->st_edge.cap -= 2 * delta;
2585 : 0 : pvO->st_edge.flow -= 2 * delta;
2586 : 0 : pvM->st_edge.flow -= 2 * delta;
2587 : 0 : pvM->st_edge.cap -= 2 * delta;
2588 : 0 : pBNS->tot_st_cap -= 3 * delta;
2589 : 0 : pBNS->tot_st_flow -= 4 * delta;
2590 : : /* fix M-O bond order to zero */
2591 : 0 : pe->cap -= 2 * delta;
2592 : : /* add fixed (-) charge to O */
2593 : 0 : pVA[n].cInitCharge -= delta;
2594 : : }
2595 : : else
2596 : : {
2597 : : /* failed */
2598 : 0 : pBNS->vert[vPlusMinus].st_edge.cap -= delta;
2599 : 0 : pvO->st_edge.cap -= delta;
2600 : : /*pTCGroups->pTCG[itg].edges_cap -= delta;*/ /* ???bug??? - commented out 2006-03-22 */
2601 : 0 : pBNS->tot_st_cap -= 2 * delta;
2602 : : /* decrease capacities of edges to Y */
2603 [ # # ]: 0 : for (i1 = 0; i1 < pBNS->vert[vPlusMinus].num_adj_edges; i1++)
2604 : : {
2605 : 0 : j1 = pBNS->edge[pBNS->vert[vPlusMinus].iedge[i1]].neighbor12 ^ vPlusMinus;
2606 [ # # ]: 0 : for (k1 = 0; k1 < pBNS->vert[j1].num_adj_edges; k1++)
2607 : : {
2608 : 0 : pBNS->edge[pBNS->vert[j1].iedge[k1]].cap -= delta;
2609 : : }
2610 : : }
2611 : : }
2612 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
2613 : : }
2614 : : }
2615 : : }
2616 : : }
2617 : : }
2618 : :
2619 : 0 : exit_function:
2620 : :
2621 : 0 : AllocEdgeList(&AllChargeEdges, EDGE_LIST_FREE);
2622 : :
2623 : 0 : return ret;
2624 : : }
2625 : :
2626 : :
2627 : : #if ( KEEP_METAL_EDGE_FLOW == 1 )
2628 : :
2629 : :
2630 : : /****************************************************************************/
2631 : : int ForbidMetalCarbonEdges(BN_STRUCT* pBNS,
2632 : : inp_ATOM* at,
2633 : : int num_at,
2634 : : VAL_AT* pVA,
2635 : : ALL_TC_GROUPS* pTCGroups,
2636 : : EDGE_LIST* pMetalCarbonEdges,
2637 : : int forbidden_edge_mask)
2638 : : {
2639 : :
2640 : : int i, j, neigh, nNumEdgeMetalCarbon = 0, pass = 0, ret = 0;
2641 : : BNS_VERTEX* pVert, * pNeigh;
2642 : : BNS_EDGE* pEdge;
2643 : :
2644 : : /* count carbon-metal edges */
2645 : :
2646 : : if (pTCGroups->num_metal_atoms)
2647 : : {
2648 : : fill_ForbiddenEdgesMetalCarbon:
2649 : : for (i = 0; i < num_at; i++)
2650 : : {
2651 : : if (pVA[i].cMetal && pVA[i].cNumBondsToMetal)
2652 : : {
2653 : : pVert = pBNS->vert + i;
2654 : : for (j = 0; j < pVert->num_adj_edges; j++)
2655 : : {
2656 : : pEdge = pBNS->edge + pVert->iedge[j];
2657 : : neigh = pEdge->neighbor12 ^ i;
2658 : : pNeigh = pBNS->vert + neigh;
2659 : : if (!IS_BNS_VT_ATOM(pNeigh->type))
2660 : : continue;
2661 : : if (at[neigh].endpoint)
2662 : : continue;
2663 : : if (pVA[neigh].cNumValenceElectrons == 4 && pVA[neigh].cPeriodicRowNumber == 1 &&
2664 : : pNeigh->st_edge.cap >= at[neigh].valence + 1)
2665 : : {
2666 : : if (pass)
2667 : : {
2668 : : if (ret = AddToEdgeList(pMetalCarbonEdges, pVert->iedge[j], 0))
2669 : : {
2670 : : goto exit_function;
2671 : : }
2672 : : pEdge->forbidden |= forbidden_edge_mask;
2673 : : }
2674 : : else
2675 : : {
2676 : : nNumEdgeMetalCarbon++;
2677 : : }
2678 : : }
2679 : : }
2680 : : }
2681 : : }
2682 : : if (!pass && nNumEdgeMetalCarbon)
2683 : : {
2684 : : if (ret = AllocEdgeList(pMetalCarbonEdges, nNumEdgeMetalCarbon))
2685 : : {
2686 : : goto exit_function;
2687 : : }
2688 : : pass++;
2689 : : goto fill_ForbiddenEdgesMetalCarbon;
2690 : : }
2691 : : }
2692 : :
2693 : : exit_function:
2694 : :
2695 : : return ret;
2696 : : }
2697 : :
2698 : :
2699 : : #endif
2700 : :
2701 : :
2702 : : /****************************************************************************
2703 : : Restore bonds & charges
2704 : : *****************************************************************************/
2705 : 0 : int RunBnsRestore1(CANON_GLOBALS* pCG,
2706 : : INCHI_CLOCK* ic,
2707 : : ICHICONST INPUT_PARMS* ip,
2708 : : STRUCT_DATA* sd,
2709 : : BN_STRUCT* pBNS,
2710 : : BN_DATA* pBD,
2711 : : StrFromINChI* pStruct,
2712 : : VAL_AT* pVA,
2713 : : ALL_TC_GROUPS* pTCGroups,
2714 : : INChI* pInChI[],
2715 : : long num_inp,
2716 : : int bHasSomeFixedH)
2717 : : {
2718 : 0 : int nNumRunBNS = 0;
2719 : :
2720 : : EDGE_LIST CarbonChargeEdges, MetalCarbonEdges, Nplus2BondsEdges;
2721 : :
2722 : 0 : int nTotalDelta = 0, ret = 0; /* djb-rwth: removing redundant variables */
2723 : 0 : inp_ATOM* at = pStruct->at;
2724 : 0 : inp_ATOM* at2 = NULL; /* restored structure */
2725 : 0 : inp_ATOM* at3 = NULL; /* structure for calculating one InChI */
2726 : 0 : int num_at = pStruct->num_atoms;
2727 : 0 : int num_deleted_H = pStruct->num_deleted_H;
2728 : : #ifdef _DEBUG
2729 : : int ret2;
2730 : : #endif
2731 : :
2732 : : #if ( KEEP_METAL_EDGE_FLOW == 1 )
2733 : : BNS_VERTEX* pVert, * pNeigh;
2734 : : int j, neigh;
2735 : : #endif
2736 : :
2737 : : /* Edge lists initialization */
2738 : 0 : AllocEdgeList(&CarbonChargeEdges, EDGE_LIST_CLEAR);
2739 : 0 : AllocEdgeList(&MetalCarbonEdges, EDGE_LIST_CLEAR);
2740 : 0 : AllocEdgeList(&Nplus2BondsEdges, EDGE_LIST_CLEAR);
2741 : :
2742 [ # # # # ]: 0 : if (pStruct->iMobileH == TAUT_NON &&
2743 : 0 : (ret = FillOutExtraFixedHDataInChI(pStruct, pInChI)))
2744 : : {
2745 : 0 : goto exit_function;
2746 : : }
2747 : :
2748 [ # # # # : 0 : if ((!at2 && !(at2 = (inp_ATOM*)inchi_malloc(((long long)num_at + (long long)num_deleted_H) * sizeof(at2[0])))) ||
# # ]
2749 [ # # ]: 0 : (!at3 && !(at3 = (inp_ATOM*)inchi_malloc(((long long)num_at + (long long)num_deleted_H) * sizeof(at3[0]))))) /* djb-rwth: cast operators added; addressing LLVM warning */
2750 : : {
2751 [ # # ]: 0 : inchi_free(at2);
2752 [ # # ]: 0 : inchi_free(at3);
2753 : 0 : return RI_ERR_ALLOC;
2754 : : }
2755 : :
2756 [ # # ]: 0 : if (0 > (ret = ForbidCarbonChargeEdges(pBNS, pTCGroups, &CarbonChargeEdges, BNS_EDGE_FORBIDDEN_TEMP)))
2757 : : {
2758 : 0 : goto exit_function;
2759 : : }
2760 : :
2761 : : #if ( KEEP_METAL_EDGE_FLOW == 1 )
2762 : : /* count edges of -C(IV)< carbons connected to metals */
2763 : : if (0 > (ret = ForbidMetalCarbonEdges(pBNS, at, num_at, pVA, pTCGroups, &MetalCarbonEdges, BNS_EDGE_FORBIDDEN_TEMP)))
2764 : : {
2765 : : goto exit_function;
2766 : : }
2767 : : #endif
2768 [ # # ]: 0 : if (0 > (ret = ForbidNintrogenPlus2BondsInSmallRings(pBNS, at, num_at, pVA, 6,
2769 : : pTCGroups, &Nplus2BondsEdges, BNS_EDGE_FORBIDDEN_TEMP)))
2770 : : {
2771 : 0 : goto exit_function;
2772 : : }
2773 : :
2774 : : /*********** Run BNS #1: no charge on carbons and =N= ***************/
2775 [ # # ]: 0 : if (Nplus2BondsEdges.num_edges)
2776 : : {
2777 : : /* Run BNS leaving carbon charges unchanged */
2778 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2779 : 0 : nNumRunBNS++;
2780 [ # # ]: 0 : if (ret < 0)
2781 : : {
2782 : 0 : goto exit_function;
2783 : : }
2784 : : else
2785 : : {
2786 : 0 : nTotalDelta += ret;
2787 : : }
2788 : 0 : RemoveForbiddenEdgeMask(pBNS, &Nplus2BondsEdges, BNS_EDGE_FORBIDDEN_TEMP);
2789 : 0 : AllocEdgeList(&Nplus2BondsEdges, EDGE_LIST_FREE);
2790 : : }
2791 : : #ifdef _DEBUG
2792 : : /* debug only */
2793 : : memcpy(at2, at, ((long long)pStruct->num_atoms + (long long)pStruct->num_deleted_H) * sizeof(at2[0])); /* djb-rwth: cast operators added */
2794 : : pStruct->at = at2;
2795 : : ret2 = CopyBnsToAtom(pStruct, pBNS, pVA, pTCGroups, 1);
2796 : : pStruct->at = at;
2797 : : #endif
2798 : : /*************************** extend min ring size to 8 ****************************/
2799 [ # # ]: 0 : if (0 > (ret = ForbidNintrogenPlus2BondsInSmallRings(pBNS, at, num_at, pVA, 8,
2800 : : pTCGroups, &Nplus2BondsEdges, BNS_EDGE_FORBIDDEN_TEMP)))
2801 : : {
2802 : 0 : goto exit_function;
2803 : : }
2804 [ # # ]: 0 : if (Nplus2BondsEdges.num_edges)
2805 : : {
2806 : : /* Run BNS leaving carbon charges unchanged */
2807 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2808 : 0 : nNumRunBNS++;
2809 [ # # ]: 0 : if (ret < 0)
2810 : : {
2811 : 0 : goto exit_function;
2812 : : }
2813 : : else
2814 : : {
2815 : 0 : nTotalDelta += ret;
2816 : : }
2817 : 0 : RemoveForbiddenEdgeMask(pBNS, &Nplus2BondsEdges, BNS_EDGE_FORBIDDEN_TEMP);
2818 : 0 : AllocEdgeList(&Nplus2BondsEdges, EDGE_LIST_FREE);
2819 : : }
2820 : : #ifdef _DEBUG
2821 : : /* debug only */
2822 : : memcpy(at2, at, ((long long)pStruct->num_atoms + (long long)pStruct->num_deleted_H) * sizeof(at2[0])); /* djb-rwth: cast operators added */
2823 : : pStruct->at = at2;
2824 : : ret2 = CopyBnsToAtom(pStruct, pBNS, pVA, pTCGroups, 1);
2825 : : pStruct->at = at;
2826 : : #endif
2827 : : /*******************************************************************/
2828 [ # # ]: 0 : if (CarbonChargeEdges.num_edges > 0)
2829 : : {
2830 : : /* Run BNS leaving carbon charges unchanged */
2831 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2832 : 0 : nNumRunBNS++;
2833 [ # # ]: 0 : if (ret < 0)
2834 : : {
2835 : 0 : goto exit_function;
2836 : : }
2837 : : else
2838 : : {
2839 : 0 : nTotalDelta += ret;
2840 : : }
2841 : 0 : RemoveForbiddenEdgeMask(pBNS, &CarbonChargeEdges, BNS_EDGE_FORBIDDEN_TEMP);
2842 : 0 : AllocEdgeList(&CarbonChargeEdges, EDGE_LIST_FREE);
2843 : : }
2844 : : #ifdef _DEBUG
2845 : : /* debug only */
2846 : : memcpy(at2, at, ((long long)pStruct->num_atoms + (long long)pStruct->num_deleted_H) * sizeof(at2[0])); /* djb-rwth: cast operators added */
2847 : : pStruct->at = at2;
2848 : : ret2 = CopyBnsToAtom(pStruct, pBNS, pVA, pTCGroups, 1);
2849 : : pStruct->at = at;
2850 : : #endif
2851 : : /*******************************************************************/
2852 [ # # ]: 0 : if (MetalCarbonEdges.num_edges > 0)
2853 : : {
2854 : : /* Run BNS leaving carbon charges unchanged */
2855 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2856 : 0 : nNumRunBNS++;
2857 [ # # ]: 0 : if (ret < 0)
2858 : : {
2859 : 0 : goto exit_function;
2860 : : }
2861 : : else
2862 : : {
2863 : 0 : nTotalDelta += ret;
2864 : : }
2865 : 0 : RemoveForbiddenEdgeMask(pBNS, &MetalCarbonEdges, BNS_EDGE_FORBIDDEN_TEMP);
2866 : 0 : AllocEdgeList(&MetalCarbonEdges, EDGE_LIST_FREE);
2867 : : }
2868 : : /*******************************************************************/
2869 : : /* Run BNS allowing to change any charges */
2870 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2871 : 0 : nNumRunBNS++;
2872 [ # # ]: 0 : if (ret < 0)
2873 : : {
2874 : 0 : goto exit_function;
2875 : : }
2876 : : else
2877 : : {
2878 : 0 : nTotalDelta += ret;
2879 : : }
2880 : : #ifdef _DEBUG
2881 : : /* debug only */
2882 : : memcpy(at2, at, ((long long)pStruct->num_atoms + (long long)pStruct->num_deleted_H) * sizeof(at2[0])); /* djb-rwth: cast operators added */
2883 : : pStruct->at = at2;
2884 : : ret2 = CopyBnsToAtom(pStruct, pBNS, pVA, pTCGroups, 1);
2885 : : pStruct->at = at;
2886 : : #endif
2887 : :
2888 : : #if ( BNS_RAD_SEARCH == 1 )
2889 : : /****************************************************************************/
2890 : : /* move unfulfilled 'radicals' from ChargeStruct to atoms */
2891 : : /* and set change charges of affected atoms to fit total charge */
2892 : 0 : ret = MoveRadToAtomsAddCharges(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups, BNS_EDGE_FORBIDDEN_TEMP);
2893 [ # # ]: 0 : if (ret < 0)
2894 : : {
2895 : 0 : goto exit_function;
2896 : : }
2897 : : #endif
2898 : : /**************************************************************/
2899 : : /**************************************************************/
2900 : : /***** fix restore inconsistencies *****/
2901 : : /**************************************************************/
2902 : : /**************************************************************/
2903 : : #ifdef _DEBUG
2904 : : /* debug only */
2905 : : memcpy(at2, at, ((long long)pStruct->num_atoms + (long long)pStruct->num_deleted_H) * sizeof(at2[0])); /* djb-rwth: cast operators added */
2906 : : pStruct->at = at2;
2907 : : ret2 = CopyBnsToAtom(pStruct, pBNS, pVA, pTCGroups, 1);
2908 : : pStruct->at = at;
2909 : : #endif
2910 : :
2911 : : /* rearrange (+) and (-) edges flow so that there is no (+)flow=0 and (-)flow=1 */
2912 : 0 : ret = RearrangePlusMinusEdgesFlow(pBNS, pBD, pVA, pTCGroups, BNS_EDGE_FORBIDDEN_TEMP);
2913 [ # # ]: 0 : if (ret < 0)
2914 : : {
2915 : 0 : goto exit_function;
2916 : : }
2917 : :
2918 : : /*****************************************************************/
2919 : : /* Increment zero order metal bonds to heteroatoms */
2920 : : /*****************************************************************/
2921 : 0 : ret = IncrementZeroOrderBondsToHeteroat(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
2922 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
2923 [ # # ]: 0 : if (ret < 0)
2924 : : {
2925 : 0 : goto exit_function;
2926 : : }
2927 : :
2928 : : #ifdef _DEBUG
2929 : : /* debug only */
2930 : : memcpy(at2, at, ((long long)pStruct->num_atoms + (long long)pStruct->num_deleted_H) * sizeof(at2[0])); /* djb-rwth: cast operators added */
2931 : : pStruct->at = at2;
2932 : : ret2 = CopyBnsToAtom(pStruct, pBNS, pVA, pTCGroups, 1);
2933 : : pStruct->at = at;
2934 : : #endif
2935 : :
2936 : : #if (MOVE_CHARGES_FROM_HETEREO_TO_METAL == 1 )
2937 : : /*****************************************************************/
2938 : : /* move charges from heteroatoms to metal atoms */
2939 : : /*****************************************************************/
2940 : : ret = MoveChargeFromHeteroatomsToMetals(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
2941 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
2942 : : if (ret < 0)
2943 : : {
2944 : : goto exit_function;
2945 : : }
2946 : : #endif
2947 : : /***********************************************************************
2948 : : NH2 NH2
2949 : : \ \
2950 : : C==S(+)- => C(+)-S- where NH2 are not tautomeric
2951 : : / /
2952 : : NH2 NH2
2953 : : ************************************************************************/
2954 : 0 : ret = MovePlusFromS2DiaminoCarbon(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
2955 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
2956 [ # # ]: 0 : if (ret < 0)
2957 : : {
2958 : 0 : goto exit_function;
2959 : : }
2960 : : /*****************************************************************/
2961 : : /* Avoid charge separation on heteroatoms */
2962 : : /*****************************************************************/
2963 : 0 : ret = EliminateChargeSeparationOnHeteroatoms(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
2964 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP, 0);
2965 [ # # ]: 0 : if (ret < 0)
2966 : : {
2967 : 0 : goto exit_function;
2968 : : }
2969 [ # # ]: 0 : if (ret)
2970 : : {
2971 : : /*charge separation remains; allow changes of stereobonds in a ring and try again */
2972 : 0 : ret = EliminateChargeSeparationOnHeteroatoms(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
2973 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP,
2974 : : BNS_EDGE_FORBIDDEN_MASK);
2975 [ # # ]: 0 : if (ret < 0)
2976 : : {
2977 : 0 : goto exit_function;
2978 : : }
2979 : : }
2980 : : /*****************************************************************/
2981 : : /* convert N#N(+)-N= into N(-)=N(+)=N- */
2982 : : /*****************************************************************/
2983 : 0 : ret = RestoreNNNgroup(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
2984 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
2985 [ # # ]: 0 : if (ret < 0)
2986 : : {
2987 : 0 : goto exit_function;
2988 : : }
2989 : : /*****************************************************************/
2990 : : /* convert Metal(q)-N(-)-O(-) Metal(q-2)-N=O (local change) */
2991 : : /*****************************************************************/
2992 : 0 : ret = FixMetal_Nminus_Ominus(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
2993 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
2994 [ # # ]: 0 : if (ret < 0)
2995 : : {
2996 : 0 : goto exit_function;
2997 : : }
2998 : : /*****************************************************************/
2999 : : /* convert N(-)=C= into N#C- - */
3000 : : /*****************************************************************/
3001 : 0 : ret = RestoreCyanoGroup(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3002 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3003 [ # # ]: 0 : if (ret < 0)
3004 : : {
3005 : 0 : goto exit_function;
3006 : : }
3007 : : /*****************************************************************/
3008 : : /* convert C(+)#N(+)- into C(-)#N(+)- */
3009 : : /*****************************************************************/
3010 : 0 : ret = RestoreIsoCyanoGroup(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3011 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3012 [ # # ]: 0 : if (ret < 0)
3013 : : {
3014 : 0 : goto exit_function;
3015 : : }
3016 : : /*****************************************************************/
3017 : : /* eliminate =N(V)= if possible */
3018 : : /* | */
3019 : : /*****************************************************************/
3020 : 0 : ret = EliminateNitrogen5Val3Bonds(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3021 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3022 [ # # ]: 0 : if (ret < 0)
3023 : : {
3024 : 0 : goto exit_function;
3025 : : }
3026 : :
3027 : : /*****************************************************************/
3028 : : /* | | */
3029 : : /* convert -S- to =S= if possible */
3030 : : /* | | */
3031 : : /*****************************************************************/
3032 : 0 : ret = Convert_SIV_to_SVI(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3033 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3034 [ # # ]: 0 : if (ret < 0)
3035 : : {
3036 : 0 : goto exit_function;
3037 : : }
3038 : :
3039 : : /*****************************************************************/
3040 : : /* =N(+)=O =N-O(-) */
3041 : : /* convert => if possible */
3042 : : /* Metal(q) Metal(q+2) */
3043 : : /*****************************************************************/
3044 : 0 : ret = PlusFromDB_N_DB_O_to_Metal(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3045 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3046 [ # # ]: 0 : if (ret < 0)
3047 : : {
3048 : 0 : goto exit_function;
3049 : : }
3050 : :
3051 : : /*****************************************************************/
3052 : : /* forbidden edges prevents required in InChI tautomerism */
3053 : : /* incorrectly restored mobile H mix separate tautomeric groups */
3054 : : /* because an edge may not become forbidden */
3055 : : /* note: removes this 'forbidden_edge' bit from ALL edges */
3056 : : /*****************************************************************/
3057 : 0 : ret = MoveMobileHToAvoidFixedBonds(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3058 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3059 : :
3060 [ # # ]: 0 : if (ret < 0)
3061 : : {
3062 : 0 : goto exit_function;
3063 : : }
3064 : : /**************************************************************************/
3065 : : /* 2. Mobile H endpoint has radical on it (typical for wrong P(VI)(=O)3OH */
3066 : : /* djb-rwth: removing redundant code */
3067 [ # # ]: 0 : if (pStruct->iMobileH == TAUT_NON)
3068 : : {
3069 : 0 : ret = RemoveRadFromMobileHEndpointFixH(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3070 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3071 : : }
3072 : : else
3073 : : {
3074 : 0 : ret = RemoveRadFromMobileHEndpoint(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3075 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3076 : : }
3077 [ # # ]: 0 : if (ret < 0)
3078 : : {
3079 : 0 : goto exit_function;
3080 : : }
3081 : : /* djb-rwth: removing redundant code */
3082 : : /**************************************************************/
3083 : : /* make bonds between a charged heteroatom and a metal single */
3084 : 0 : ret = MakeSingleBondsMetal2ChargedHeteroat(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3085 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3086 [ # # ]: 0 : if (ret < 0)
3087 : : {
3088 : 0 : goto exit_function;
3089 : : }
3090 : : /**************************************************************/
3091 : : /* move (+) charges to >N- and other centerpoints */
3092 : 0 : ret = MoveChargeToMakeCenerpoints(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3093 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3094 [ # # ]: 0 : if (ret < 0)
3095 : : {
3096 : 0 : goto exit_function;
3097 : : }
3098 : :
3099 : : /**************************************************************************/
3100 : : /* Find and eliminate false Mobile-H groups: Cl(=O)3(-O(-)) => Cl(-)(=O)4 */
3101 : 0 : ret = MoveChargeToRemoveCenerpoints(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3102 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3103 [ # # ]: 0 : if (ret < 0)
3104 : : {
3105 : 0 : goto exit_function;
3106 : : }
3107 : : /**************************************************************************/
3108 : : /* Find A=X< where all bonds to X except A=X are marked as stereogenic */
3109 : : /* make bonds A=X single */
3110 : 0 : ret = CheckAndRefixStereobonds(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3111 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3112 [ # # ]: 0 : if (ret < 0)
3113 : : {
3114 : 0 : goto exit_function;
3115 : : }
3116 : : /**************************************************************************/
3117 : : /* In Reconnected structure change 'salt bonds' to 'coordination bonds */
3118 : : /* for example, M-O-C= -> M(+)-O(-)-C= */
3119 : : /* Defect: instead of NH2-C=O(+)-M it will restore NH2(+)=C-O(-)-M(+) */
3120 : : /* However, in this release metal-organic compounds do not get much care */
3121 : 0 : ret = SaltBondsToCoordBonds(pBNS, pBD, pStruct, at, at2, pVA, pTCGroups,
3122 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP);
3123 [ # # ]: 0 : if (ret < 0)
3124 : : {
3125 : 0 : goto exit_function;
3126 : : }
3127 : : /**************************************************************************/
3128 : : /* Normalize the structure and compare t-groups and stereobonds */
3129 : 0 : ret = NormalizeAndCompare(pCG, ic, ip, sd, pBNS, pBD, pStruct, at, at2, at3, pVA, pTCGroups, pInChI, num_inp, bHasSomeFixedH,
3130 : : &nNumRunBNS, &nTotalDelta, BNS_EDGE_FORBIDDEN_TEMP, BNS_EDGE_FORBIDDEN_MASK);
3131 [ # # ]: 0 : if (ret < 0)
3132 : : {
3133 : 0 : goto exit_function;
3134 : : }
3135 : : /**************************************************************************/
3136 : : /* Create InChI out of the restored structure */
3137 : :
3138 : :
3139 : : /*ret = nTotalDelta;*/
3140 : :
3141 : 0 : exit_function:
3142 : :
3143 : 0 : pStruct->at = at;
3144 : 0 : pStruct->at2 = at2;
3145 : 0 : at2 = NULL;
3146 : 0 : AllocEdgeList(&CarbonChargeEdges, EDGE_LIST_FREE);
3147 : 0 : AllocEdgeList(&MetalCarbonEdges, EDGE_LIST_FREE);
3148 : 0 : AllocEdgeList(&Nplus2BondsEdges, EDGE_LIST_FREE);
3149 [ # # ]: 0 : if (at2)
3150 : : {
3151 [ # # ]: 0 : inchi_free(at2);
3152 : : }
3153 [ # # ]: 0 : if (at3)
3154 : : {
3155 [ # # ]: 0 : inchi_free(at3);
3156 : : }
3157 : :
3158 : 0 : return ret;
3159 : : }
3160 : :
3161 : :
3162 : : /****************************************************************************/
3163 : 0 : int RestoreAtomMakeBNS(INCHI_CLOCK* ic, CANON_GLOBALS* pCG,
3164 : : ICHICONST INPUT_PARMS* ip,
3165 : : STRUCT_DATA* sd,
3166 : : StrFromINChI* pStruct,
3167 : : int iComponent,
3168 : : int iAtNoOffset,
3169 : : INChI* pInChI[],
3170 : : const char* szCurHdr,
3171 : : long num_inp,
3172 : : int bHasSomeFixedH)
3173 : : {
3174 : 0 : int i, j, ret = 0, ret2;
3175 : : /*int nDelta, nTotalDelta;*/
3176 : 0 : VAL_AT* pVA = NULL;
3177 : : VAL_AT va1;
3178 : 0 : int num_at = pStruct->num_atoms;
3179 : 0 : inp_ATOM* at = pStruct->at;
3180 : : ALL_TC_GROUPS TCGroups;
3181 : 0 : ALL_TC_GROUPS* pTCGroups = &TCGroups;
3182 : 0 : int nAddEdges2eachAtom = 2, nAddVertices = 0;
3183 : :
3184 : : BFS_Q bfsq;
3185 : :
3186 : : /* BNS creation */
3187 : 0 : BN_STRUCT* pBNS = NULL;
3188 : 0 : BN_DATA* pBD = NULL;
3189 : 0 : int nNum_changed_bonds = 0;
3190 : 0 : int bTreatMoreAtomsAsMetals = 0, bSecondPassNewMetals = 0;
3191 : 0 : int nMaxAddAtoms = 2, nMaxAddEdges = 2, max_altp = BN_MAX_ALTP;
3192 : :
3193 : 0 : memset(pTCGroups, 0, sizeof(pTCGroups[0])); /* djb-rwth: memset_s C11/Annex K variant? */
3194 [ # # ]: 0 : for (i = 0; i < NUM_TCGROUP_TYPES; i++)
3195 : : {
3196 : 0 : pTCGroups->nGroup[i] = TCG_None; /* unassigned */
3197 : : }
3198 : 0 : pTCGroups->iComponent = iComponent;
3199 : 0 : pTCGroups->iAtNoOffset = iAtNoOffset;
3200 : :
3201 [ # # ]: 0 : if (num_at == 1)
3202 : : {
3203 : : /* single atom -- no bonds to restore */
3204 : 0 : inp_ATOM* at2 = (inp_ATOM*)inchi_malloc(sizeof(at2[0]) * ((long long)pStruct->num_atoms + (long long)pStruct->num_deleted_H)); /* djb-rwth: cast operators added */
3205 : 0 : inp_ATOM* at3 = (inp_ATOM*)inchi_malloc(sizeof(at3[0]) * ((long long)pStruct->num_atoms + (long long)pStruct->num_deleted_H)); /* djb-rwth: cast operators added */
3206 : 0 : pStruct->at2 = at2;
3207 : 0 : at[0].charge = pInChI[0]->nTotalCharge;
3208 [ # # ]: 0 : if (at2)
3209 : : {
3210 : 0 : memcpy(at2, at, sizeof(at2[0]) * ((long long)pStruct->num_atoms + (long long)pStruct->num_deleted_H)); /* djb-rwth: cast operators added */
3211 : : }
3212 [ # # # # ]: 0 : if (!at2 || !at3)
3213 : : {
3214 [ # # # # ]: 0 : if (at3) inchi_free(at3);
3215 : 0 : return RI_ERR_ALLOC;
3216 : : }
3217 : 0 : ret = MakeOneInChIOutOfStrFromINChI(pCG, ic, ip, sd, pStruct, pStruct->at2, at3, pTCGroups);
3218 : : /* clean up */
3219 [ # # ]: 0 : for (i = 0; i < TAUT_NUM; i++)
3220 : : {
3221 : 0 : Free_INChI(&pStruct->pOneINChI[i]);
3222 : 0 : Free_INChI_Aux(&pStruct->pOneINChI_Aux[i]);
3223 : 0 : FreeInpAtomData(pStruct->pOne_norm_data[i]);
3224 [ # # ]: 0 : if (pStruct->pOne_norm_data[i])
3225 : : {
3226 [ # # ]: 0 : inchi_free(pStruct->pOne_norm_data[i]);
3227 : 0 : pStruct->pOne_norm_data[i] = NULL;
3228 : : }
3229 : : }
3230 : : /* djb-rwth: fixing oss-fuzz issue #69602 */
3231 : : /* free_t_group_info(&pStruct->One_ti); */
3232 [ # # ]: 0 : inchi_free(at3);
3233 : :
3234 : 0 : return ret;
3235 : : }
3236 : :
3237 : 0 : AllocBfsQueue(&bfsq, BFS_Q_CLEAR, 0);
3238 [ # # ]: 0 : if (!(pVA = (VAL_AT*)inchi_calloc(num_at, sizeof(pVA[0]))))
3239 : : {
3240 : 0 : ret = RI_ERR_ALLOC;
3241 : 0 : goto exit_function;
3242 : : }
3243 : 0 : pStruct->pVA = pVA;
3244 : 0 : memset(&va1, 0, sizeof(va1)); /* djb-rwth: memset_s C11/Annex K variant? */
3245 : 0 : pTCGroups->total_charge = pInChI[0]->nTotalCharge;
3246 [ # # ]: 0 : if (0 > (ret = AllocBfsQueue(&bfsq, num_at, 0 /* min ring size undefined */)))
3247 : : {
3248 : 0 : goto exit_function;
3249 : : }
3250 : 0 : pStruct->pbfsq = &bfsq;
3251 : :
3252 [ # # # # : 0 : if (pStruct->iMobileH == TAUT_NON && pInChI[1] && pInChI[1]->nNumberOfAtoms > 1 &&
# # # # ]
3253 : 0 : (ret = FillOutpStructEndpointFromInChI(pInChI[1], &pStruct->endpoint)))
3254 : : {
3255 : 0 : goto exit_function;
3256 : : }
3257 : :
3258 : : /* mark metal atoms; find min ring sizes for atoms that have 2 bonds */
3259 [ # # ]: 0 : for (i = 0; i < num_at; i++)
3260 : : {
3261 : 0 : pVA[i].cNumValenceElectrons = get_sp_element_type(at[i].el_number, &j);
3262 : 0 : pVA[i].cPeriodicRowNumber = j;
3263 : 0 : pVA[i].cPeriodicNumber = at[i].el_number;
3264 : 0 : pVA[i].cNumValenceElectrons--; /* = -1 d- and f- metals, 0 for H, 1 for Na, 2 for Mg,.. = (ATYPE_Xx-1) */
3265 : :
3266 [ # # ]: 0 : if (is_el_a_metal(at[i].el_number))
3267 : : {
3268 [ # # ]: 0 : if (pStruct->pSrm->bStereoRemovesMetalFlag)
3269 : : {
3270 : : /* treat metal as non-metal if it is stereogenic or has a stereobond */
3271 [ # # # # ]: 0 : pVA[i].cMetal = !(at[i].p_parity || at[i].sb_parity[0]);
3272 : : }
3273 : : else
3274 : : {
3275 : 0 : pVA[i].cMetal = 1;
3276 : : }
3277 : : }
3278 [ # # # # ]: 0 : if (at[i].valence == 2 && !at[i].num_H)
3279 : : {
3280 : 0 : pVA[i].cMinRingSize = is_bond_in_Nmax_memb_ring(at, i, 0, bfsq.q, bfsq.nAtomLevel,
3281 : : bfsq.cSource, 99 /* max ring size */);
3282 : : }
3283 : : else
3284 : : {
3285 : 0 : pVA[i].cMinRingSize = 0;
3286 : : }
3287 : : }
3288 : : /* AllocBfsQueue( &bfsq, BFS_Q_FREE, 0 ); */
3289 : :
3290 : 0 : repeat_for_new_metals:
3291 : : /* set valences for the first time; find ChargeValence structures for each atom */
3292 [ # # ]: 0 : for (i = 0; i < num_at; i++)
3293 : : {
3294 : : /* get additional fictitious atoms information */
3295 : 0 : pVA[i].cInitFreeValences = 0;
3296 : :
3297 : 0 : ret = GetAtomRestoreInfo(pCG, at, i, pVA, pStruct->pSrm, pStruct->bMobileH, pStruct->endpoint);
3298 : :
3299 [ # # ]: 0 : if (ret < 0)
3300 : : {
3301 : 0 : goto exit_function;
3302 : : }
3303 [ # # # # : 0 : if (ret == TREAT_ATOM_AS_METAL && !bSecondPassNewMetals && !pVA[i].cMetal)
# # ]
3304 : : {
3305 [ # # ]: 0 : if (pStruct->pSrm->bStereoRemovesMetalFlag)
3306 : : {
3307 : : /* treat metal as non-metal if it is stereogenic or has a stereobond */
3308 [ # # # # ]: 0 : pVA[i].cMetal = !(at[i].p_parity || at[i].sb_parity[0]);
3309 : : }
3310 : : else
3311 : : {
3312 : 0 : pVA[i].cMetal = 1;
3313 : : }
3314 [ # # ]: 0 : if (pVA[i].cMetal)
3315 : : {
3316 : 0 : bTreatMoreAtomsAsMetals++;
3317 : : }
3318 : : }
3319 : 0 : pTCGroups->charge_on_atoms += pVA[i].cInitCharge;
3320 : : }
3321 [ # # # # ]: 0 : if (bTreatMoreAtomsAsMetals && !bSecondPassNewMetals)
3322 : : {
3323 [ # # ]: 0 : for (i = 0; i < num_at; i++)
3324 : : {
3325 : : /* clear all members of pVA[i] except two */
3326 : 0 : pTCGroups->charge_on_atoms -= pVA[i].cInitCharge;
3327 : 0 : va1.cMetal = pVA[i].cMetal;
3328 : 0 : va1.cMinRingSize = pVA[i].cMinRingSize;
3329 : 0 : va1.cNumValenceElectrons = pVA[i].cNumValenceElectrons;
3330 : 0 : va1.cPeriodicRowNumber = pVA[i].cPeriodicRowNumber;
3331 : 0 : va1.cPeriodicNumber = pVA[i].cPeriodicNumber;
3332 : 0 : pVA[i] = va1;
3333 : : }
3334 : 0 : bSecondPassNewMetals = 1;
3335 : 0 : goto repeat_for_new_metals;
3336 : : }
3337 : :
3338 : : /* count atoms, bonds, additional edges and vertices in ChargeValence structures and t-groups */
3339 : 0 : ret = nCountBnsSizes(at, num_at, nAddEdges2eachAtom, nAddVertices, &pStruct->ti,
3340 : : pVA, pStruct->pSrm, pTCGroups);
3341 [ # # ]: 0 : if (ret < 0)
3342 : : {
3343 : 0 : goto exit_function;
3344 : : }
3345 : :
3346 : : /* find and count groups; add counts of all other vertices to be created */
3347 : 0 : ret = nAddSuperCGroups(pTCGroups);
3348 [ # # ]: 0 : if (ret < 0)
3349 : : {
3350 : 0 : goto exit_function;
3351 : : }
3352 : :
3353 : : /* create the BNS and fill it with all real atoms */
3354 : 0 : pBNS = AllocateAndInitTCGBnStruct(pStruct, pVA, pTCGroups,
3355 : : nMaxAddAtoms, nMaxAddEdges, max_altp, &nNum_changed_bonds);
3356 [ # # ]: 0 : if (!pBNS)
3357 : : {
3358 : 0 : ret = BNS_OUT_OF_RAM;
3359 : 0 : goto exit_function;
3360 : : }
3361 : : /* add t-groups to the BNS */
3362 : 0 : ret = AddTGroups2TCGBnStruct(pBNS, pStruct, pVA, pTCGroups, nMaxAddEdges);
3363 [ # # ]: 0 : if (ret < 0)
3364 : : {
3365 : 0 : goto exit_function;
3366 : : }
3367 : :
3368 : : /* add c-groups to the BNS; adjust charges */
3369 : 0 : ret = AddCGroups2TCGBnStruct(pBNS, pStruct, pVA, pTCGroups, nMaxAddEdges);
3370 [ # # ]: 0 : if (ret < 0)
3371 : : {
3372 : 0 : goto exit_function;
3373 : : }
3374 : :
3375 : 0 : pBNS->ulTimeOutTime = NULL; /* v. 1.05 */
3376 : 0 : pBNS->ic = ic; /* v. 1.05 */
3377 : :
3378 : : /* allocate BNData */
3379 : 0 : pBD = AllocateAndInitBnData(pBNS->max_vertices + pBNS->max_vertices / 2);
3380 [ # # ]: 0 : if (!pBD)
3381 : : {
3382 : 0 : ret = BNS_OUT_OF_RAM;
3383 : 0 : goto exit_function;
3384 : : }
3385 : 0 : CheckBnsConsistency(pStruct, pBNS, pVA, pTCGroups, 0);
3386 : :
3387 : : /* restore bonds & charges */
3388 : 0 : ret = RunBnsRestore1(pCG, ic, ip, sd, pBNS, pBD, pStruct, pVA, pTCGroups, pInChI, num_inp, bHasSomeFixedH);
3389 [ # # ]: 0 : if (ret < 0)
3390 : : {
3391 : 0 : goto exit_function;
3392 : : }
3393 : :
3394 : 0 : ret = CheckBnsConsistency(pStruct, pBNS, pVA, pTCGroups, 1);
3395 : : #if ( bRELEASE_VERSION == 0 )
3396 : : #ifndef TARGET_API_LIB
3397 : : if (ret)
3398 : : {
3399 : : fprintf(stdout, "Msg for: %ld %s comp=%d %c%c\n", num_inp, (szCurHdr && szCurHdr[0]) ? szCurHdr : "", iComponent, pStruct->iInchiRec ? 'R' : 'D', pStruct->iMobileH ? 'M' : 'F');
3400 : : }
3401 : : if (pStruct->iMobileH == TAUT_YES && pStruct->nNumRemovedProtons)
3402 : : {
3403 : : fprintf(stdout, "REMOVED_PROTONS%+d %ld %s\n", pStruct->nNumRemovedProtons, num_inp, (szCurHdr && szCurHdr[0]) ? szCurHdr : "");
3404 : : /*pStruct->bExtract |= EXTRACT_STRUCT_NUMBER;*/
3405 : : }
3406 : : if (pStruct->bExtract & EXTRACT_STRUCT_NUMBER)
3407 : : {
3408 : : fprintf(stdout, "EXTRACT: %ld: %s\n", num_inp, (szCurHdr && szCurHdr[0]) ? szCurHdr : "");
3409 : : }
3410 : : #endif
3411 : : #endif
3412 : : { /* create the final structure in pStruct->at2 */
3413 : 0 : inp_ATOM* at_tmp = pStruct->at;
3414 : 0 : pStruct->at = pStruct->at2;
3415 : 0 : memcpy(pStruct->at, at_tmp, sizeof(pStruct->at[0]) * ((long long)pStruct->num_atoms + (long long)pStruct->num_deleted_H)); /* djb-rwth: cast operators added */
3416 : 0 : ret2 = CopyBnsToAtom(pStruct, pBNS, pVA, pTCGroups, 1);
3417 : 0 : pStruct->at2 = pStruct->at;
3418 : 0 : pStruct->at = at_tmp;
3419 [ # # ]: 0 : if (ret2 < 0)
3420 : : {
3421 : 0 : ret = ret2;
3422 : : }
3423 : : }
3424 : :
3425 : 0 : exit_function:
3426 : :
3427 : 0 : pStruct->pbfsq = NULL;
3428 : 0 : AllocBfsQueue(&bfsq, BFS_Q_FREE, 0);
3429 : :
3430 : 0 : pBD = DeAllocateBnData(pBD); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
3431 : 0 : pBNS = DeAllocateBnStruct(pBNS); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
3432 : : /*
3433 : : if ( pVA ) inchi_free( pVA );
3434 : : */
3435 [ # # # # ]: 0 : if (pTCGroups->pTCG) inchi_free(pTCGroups->pTCG);
3436 : :
3437 : 0 : return ret;
3438 : : }
3439 : :
3440 : :
3441 : : /****************************************************************************/
3442 : 0 : int OneInChI2Atom(INCHI_CLOCK* ic,
3443 : : CANON_GLOBALS* pCG,
3444 : : ICHICONST INPUT_PARMS* ip_inp,
3445 : : STRUCT_DATA* sd,
3446 : : const char* szCurHdr,
3447 : : long num_inp,
3448 : : StrFromINChI* pStruct,
3449 : : int iComponent,
3450 : : int iAtNoOffset,
3451 : : int bHasSomeFixedH,
3452 : : INChI* pInChI[])
3453 : : {
3454 : : int ret;
3455 : : INPUT_PARMS* ip, ip_loc;
3456 : :
3457 : 0 : ip_loc = *ip_inp;
3458 : 0 : ip = &ip_loc;
3459 : :
3460 : 0 : sd->pStrErrStruct[0] = '\0';
3461 : 0 : ret = RestoreAtomConnectionsSetStereo(pStruct, iComponent, iAtNoOffset, pInChI[0], pInChI[1]);
3462 [ # # ]: 0 : if (ret < 0)
3463 : : {
3464 : 0 : goto exit_function;
3465 : : }
3466 : 0 : ret = SetStereoBondTypesFrom0DStereo(pStruct, pInChI[0]);
3467 [ # # ]: 0 : if (ret < 0)
3468 : : {
3469 : 0 : goto exit_function;
3470 : : }
3471 : 0 : ret = ReconcileAllCmlBondParities(pStruct->at, pStruct->num_atoms, 0);
3472 [ # # ]: 0 : if (ret < 0)
3473 : : {
3474 : 0 : goto exit_function;
3475 : : }
3476 : :
3477 : : /* main InChI restore function */
3478 : 0 : ret = RestoreAtomMakeBNS(ic, pCG, ip, sd, pStruct, iComponent, iAtNoOffset, pInChI, szCurHdr, num_inp, bHasSomeFixedH);
3479 : :
3480 : : #ifndef COMPILE_ANSI_ONLY
3481 : : if ((pStruct->num_inp_actual > 0 ? pStruct->num_inp_actual : num_inp) >= ip->first_struct_number &&
3482 : : ((/*ret > 0 &&*/ ip->bDisplayIfRestoreWarnings) && pStruct->pXYZ))
3483 : : {
3484 : : inchiTime ulTStart;
3485 : : InchiTimeGet(&ulTStart);
3486 : : DisplayRestoredComponent(pCG, pStruct, iComponent, iAtNoOffset, pInChI[0], szCurHdr);
3487 : : sd->ulStructTime -= InchiTimeElapsed(ic, &ulTStart); /* subtract display time */
3488 : : }
3489 : : #endif
3490 : :
3491 [ # # ]: 0 : if (ret < 0)
3492 : : {
3493 : 0 : goto exit_function;
3494 : : }
3495 [ # # # # : 0 : if ((pStruct->num_inp_actual ? pStruct->num_inp_actual : num_inp) >= ip->first_struct_number && ret >= 0)
# # ]
3496 : : {
3497 : : /* remove t-group markings and increment zero-order bonds,
3498 : : otherwise MakeInChIOutOfStrFromINChI2() woild fail */
3499 : : /* --- moved to MakeInChIOutOfStrFromINChI2 ---
3500 : : IncrZeroBondsAndClearEndpts(pStruct->at2, pStruct->num_atoms, iComponent+1);
3501 : : CopySt2At( pStruct->at2, pStruct->st, pStruct->num_atoms );
3502 : : */
3503 : : /* include all restored structure features in pStruct->at2 */
3504 : : /* make full InChI out of pStruct->at2, pStruct->num_atoms */
3505 : : /***************************************************************************************/
3506 : : /* !!! pStruct->One_InChI etc. were removed at the exit from NormalizeAndCompare() !!! */
3507 : : /***************************************************************************************/
3508 [ # # # # : 0 : if (bHasSomeFixedH && pStruct->iInchiRec == INCHI_REC && pStruct->iMobileH == TAUT_YES &&
# # ]
3509 [ # # # # ]: 0 : !pStruct->bFixedHExists && !(ip->nMode & REQ_MODE_BASIC))
3510 : : {
3511 : : /* reconnected components without Fixed-H layer may produce 'tautomeric' fragments like Cl(-) */
3512 : 0 : ip->nMode |= REQ_MODE_BASIC;
3513 : : }
3514 : :
3515 : 0 : ret = MakeInChIOutOfStrFromINChI2(ic, pCG, ip, sd, pStruct, iComponent, iAtNoOffset, num_inp);
3516 : :
3517 : : if (ret >= 0)
3518 : : {
3519 : : ;
3520 : : }
3521 : : #if ( bRELEASE_VERSION == 0 )
3522 : : #ifndef TARGET_API_LIB
3523 : : else
3524 : : {
3525 : : fprintf(stdout, "\nERROR in MakeInChI-1: %ld %s Comp:%d %c%c Err:%d\n", num_inp,
3526 : : szCurHdr ? szCurHdr : "???", iComponent, pStruct->iInchiRec ? 'R' : 'D', pStruct->iMobileH ? 'M' : 'F', ret);
3527 : : }
3528 : : #endif
3529 : : #endif
3530 : : }
3531 : :
3532 : 0 : exit_function:
3533 : :
3534 : 0 : return ret;
3535 : : }
3536 : :
3537 : :
3538 : : /****************************************************************************/
3539 : 0 : int MakeProtonComponent(StrFromINChI* pStruct, int iComponent, int num_prot)
3540 : : {
3541 : 0 : inp_ATOM* at = NULL;
3542 : : int i;
3543 : :
3544 [ # # ]: 0 : if (num_prot <= 0)
3545 : : {
3546 : 0 : return 0;
3547 : : }
3548 : : /* allocate */
3549 : 0 : pStruct->at = (inp_ATOM*)inchi_calloc(num_prot, sizeof(pStruct->at[0]));
3550 : 0 : pStruct->at2 = (inp_ATOM*)inchi_calloc(num_prot, sizeof(pStruct->at2[0]));
3551 [ # # # # ]: 0 : if (!pStruct->at || !pStruct->at2)
3552 : : {
3553 : 0 : return 0;
3554 : : }
3555 : : /* create protons */
3556 : 0 : at = pStruct->at;
3557 : : /* fill out proton atom info */
3558 [ # # ]: 0 : for (i = 0; i < num_prot; i++)
3559 : : {
3560 : 0 : strcpy(at[i].elname, "H");
3561 : 0 : at[i].el_number = EL_NUMBER_H;
3562 : 0 : at[i].orig_at_number = i + 1;
3563 : : /*
3564 : : at[i].orig_compt_at_numb = i + 1;
3565 : : at[i].component = i + 1;
3566 : : */
3567 : 0 : at[i].charge = 1;
3568 : : }
3569 : 0 : memcpy(pStruct->at2, at, num_prot * sizeof(pStruct->at2[0]));
3570 : 0 : pStruct->bDeleted = 0;
3571 : 0 : pStruct->num_atoms = num_prot;
3572 : 0 : pStruct->bMobileH = TAUT_YES;
3573 : 0 : pStruct->iMobileH = TAUT_YES;
3574 : :
3575 : 0 : return num_prot;
3576 : : }
3577 : :
3578 : :
3579 : : /****************************************************************************/
3580 : 0 : int AddRemProtonsInRestrStruct(INCHI_CLOCK* ic,
3581 : : CANON_GLOBALS* pCG,
3582 : : ICHICONST INPUT_PARMS* ip_inp,
3583 : : STRUCT_DATA* sd, long num_inp,
3584 : : int bHasSomeFixedH,
3585 : : StrFromINChI* pStruct,
3586 : : int num_components,
3587 : : StrFromINChI* pStructR,
3588 : : int num_componentsR,
3589 : : NUM_H* nProtonsToBeRemovedByNormFromRevrs,
3590 : : int* recmet_change_balance)
3591 : : {
3592 : : /* on entry and exit, all at[i].num_H do not include isotopic H and explicit terminal H are connected */
3593 : 0 : int iComp, q, ret = 0;
3594 : : int num_atoms, num_deleted_H, num_tg, num_changed, num_deleted_components; /* djb-rwth: removing redundant variables */
3595 : : inp_ATOM* at;
3596 : : INPUT_PARMS* ip, ip_loc;
3597 : 0 : int num_prot = *nProtonsToBeRemovedByNormFromRevrs;
3598 : 0 : int delta_recmet_prot, num_prot_prev, bAccumulateChanges = 0, nNumProtAddedByRevrs;
3599 : : INChI_Aux* pINChI_Aux;
3600 : : INCHI_MODE bNormalizationFlags;
3601 : : int nChargeRevrs, nChargeInChI;
3602 : :
3603 [ # # ]: 0 : if (!num_prot)
3604 : : {
3605 : 0 : return 0;
3606 : : }
3607 : 0 : delta_recmet_prot = 0;
3608 : 0 : num_changed = 0;
3609 : 0 : num_deleted_components = 0;
3610 : 0 : ip_loc = *ip_inp;
3611 : 0 : ip = &ip_loc;
3612 : : /*----------------------------------------------------------------------------------
3613 : : nLink < 0 && num_componentsR > 0 => This is a Disconnected structure component; it is
3614 : : same as already processed reconnected one
3615 : : Do no preicess it
3616 : :
3617 : : nLink > 0 && num_componentsR > 0 => This is a Disconnected structure component;
3618 : : (should not happen) It it is a result of (nLink-1)th Reconeected
3619 : : component disconnection (NOT IMPLEMENTED YET)
3620 : :
3621 : : nLink = 0 => Process this component. It is either a reconnected
3622 : : component, or a result of a disconnection (for now)
3623 : :
3624 : : nLink > 0 && num_componentsR = 0 => This is a Reconnected component that is same as
3625 : : a disconnected one that will not be processed.
3626 : : Process and save charge delta.
3627 : : -----------------------------------------------------------------------------------*/
3628 : :
3629 [ # # # # ]: 0 : for (iComp = 0; iComp < num_components && num_prot; iComp++)
3630 : : {
3631 : 0 : bAccumulateChanges = 0;
3632 [ # # # # ]: 0 : if (pStruct[iComp].nLink < 0 && num_componentsR > 0)
3633 : : {
3634 : : /* check */
3635 : 0 : q = -(pStruct[iComp].nLink + 1);
3636 [ # # # # : 0 : if (!pStructR || !num_componentsR || q >= num_componentsR || pStructR[q].nLink != (iComp + 1))
# # # # ]
3637 : : {
3638 : 0 : ret = RI_ERR_PROGR;
3639 : 0 : goto exit_function;
3640 : : }
3641 : 0 : continue; /* Disconnected structure component has already been processed as a Reconnected one */
3642 : : }
3643 : :
3644 : 0 : at = pStruct[iComp].at2;
3645 : 0 : num_atoms = pStruct[iComp].num_atoms;
3646 : 0 : num_deleted_H = pStruct[iComp].num_deleted_H; /* djb-rwth: removing redundant code */
3647 [ # # # # ]: 0 : bAccumulateChanges = (pStruct[iComp].nLink > 0 && !num_componentsR);
3648 : 0 : nChargeRevrs = pStruct[iComp].nChargeRevrs;
3649 : 0 : nChargeInChI = pStruct[iComp].nChargeInChI;
3650 : 0 : num_deleted_components += (0 != pStruct[iComp].bDeleted);
3651 [ # # # # ]: 0 : if (!at || !num_atoms)
3652 : : {
3653 : 0 : continue;
3654 : : }
3655 : : /* find whether it is a reconnected structure */
3656 : 0 : q = bRevInchiComponentExists(pStruct + iComp, INCHI_REC, TAUT_YES, 0) ? INCHI_REC : INCHI_BAS;
3657 : : /*
3658 : : q = pStruct[iComp].RevInChI.pINChI_Aux[INCHI_REC] &&
3659 : : pStruct[iComp].RevInChI.pINChI_Aux[INCHI_REC][0][TAUT_YES] &&
3660 : : pStruct[iComp].RevInChI.pINChI_Aux[INCHI_REC][0][TAUT_YES]->nNumberOfAtoms? INCHI_REC : INCHI_BAS;
3661 : : */
3662 : 0 : pINChI_Aux = pStruct[iComp].RevInChI.pINChI_Aux[q][0][TAUT_YES]; /* 0 = 1st component in RevInChI */
3663 : : /*nNumProtAddedByRevrs = pINChI_Aux->nNumRemovedProtons;*/
3664 : 0 : nNumProtAddedByRevrs = -pStruct[iComp].nNumRemovedProtonsByRevrs;
3665 : 0 : bNormalizationFlags = pINChI_Aux->bNormalizationFlags;
3666 : 0 : num_tg = pINChI_Aux->nNumberOfTGroups;
3667 : :
3668 : :
3669 : : /* disconnect all explicit H and add the number of implicit iso H and all explicit terminal H to the number of implicit H */
3670 [ # # ]: 0 : if (0 > (ret = DisconnectedConnectedH(at, num_atoms, num_deleted_H)))
3671 : : {
3672 : 0 : goto exit_function;
3673 : : }
3674 : 0 : num_prot_prev = num_prot;
3675 : 0 : ret = AddRemoveProtonsRestr(at, num_atoms, &num_prot, nNumProtAddedByRevrs,
3676 : : bNormalizationFlags, num_tg, nChargeRevrs, nChargeInChI);
3677 : :
3678 : 0 : pStruct[iComp].bPostProcessed = ret;
3679 : 0 : num_changed += (ret > 0);
3680 [ # # ]: 0 : if (ret < 0)
3681 : : {
3682 : 0 : goto exit_function;
3683 : : }
3684 [ # # ]: 0 : if (ret > 0)
3685 : : {
3686 : : /* recalculate InChI; it will reconnect at */
3687 : 0 : StrFromINChI* pStruct1 = pStruct + iComp;
3688 : 0 : INCHI_MODE nMode = ip->nMode;
3689 : 0 : FreeAllINChIArrays(pStruct1->RevInChI.pINChI,
3690 : 0 : pStruct1->RevInChI.pINChI_Aux,
3691 : 0 : pStruct1->RevInChI.num_components);
3692 : :
3693 [ # # # # : 0 : if (bHasSomeFixedH && pStruct1->iInchiRec == INCHI_REC && pStruct1->iMobileH == TAUT_YES &&
# # ]
3694 [ # # # # ]: 0 : !pStruct1->bFixedHExists && !(ip->nMode & REQ_MODE_BASIC))
3695 : : {
3696 : : /* reconnected components without Fixed-H layer may produce 'tautomeric' fragments like Cl(-) */
3697 : 0 : ip->nMode |= REQ_MODE_BASIC;
3698 : : }
3699 : : /* calls ConnectDisconnectedH(...): subtracts number of implicit iso H from implicit H */
3700 : :
3701 : 0 : ret = MakeInChIOutOfStrFromINChI2(ic, pCG, ip, sd, pStruct1, 0, 0, num_inp);
3702 : :
3703 : 0 : ip->nMode = nMode;
3704 [ # # ]: 0 : if (ret < 0)
3705 : : {
3706 : 0 : goto exit_function;
3707 : : }
3708 : : }
3709 : : else
3710 : : {
3711 : : /* reconnect disconnected terminal H and subtracts number of implicit iso H from implicit H */
3712 [ # # ]: 0 : if (0 > (ret = ConnectDisconnectedH(at, num_atoms, num_deleted_H)))
3713 : : {
3714 : 0 : goto exit_function;
3715 : : }
3716 : : }
3717 [ # # # # ]: 0 : if (bAccumulateChanges && recmet_change_balance)
3718 : : {
3719 : : /* processed Reconnected layer component that is also present in Disconnected layer */
3720 : 0 : delta_recmet_prot += num_prot - num_prot_prev;
3721 : : }
3722 : : }
3723 : :
3724 : 0 : iComp = num_components - 1;
3725 [ # # # # : 0 : if (!bHasSomeFixedH && num_prot > 0 && 1 == num_deleted_components && iComp >= 0 && pStruct[iComp].bDeleted)
# # # # #
# ]
3726 : : {
3727 : : /* add bare protons to the deleted Mobile-H component; undelete the component */
3728 : 0 : num_prot_prev = num_prot;
3729 [ # # ]: 0 : if (!MakeProtonComponent(pStruct + iComp, iComp, num_prot))
3730 : : {
3731 : 0 : goto exit_function;
3732 : : }
3733 : : else
3734 : : {
3735 : : /* recalculate InChI; it will reconnect at */
3736 : 0 : StrFromINChI* pStruct1 = pStruct + iComp;
3737 : 0 : INCHI_MODE nMode = ip->nMode;
3738 : 0 : num_changed++;
3739 : 0 : num_prot = 0;
3740 : 0 : FreeAllINChIArrays(pStruct1->RevInChI.pINChI,
3741 : 0 : pStruct1->RevInChI.pINChI_Aux,
3742 : 0 : pStruct1->RevInChI.num_components);
3743 : :
3744 [ # # # # : 0 : if (bHasSomeFixedH && pStruct1->iInchiRec == INCHI_REC && pStruct1->iMobileH == TAUT_YES &&
# # ]
3745 [ # # # # ]: 0 : !pStruct1->bFixedHExists && !(ip->nMode & REQ_MODE_BASIC))
3746 : : {
3747 : : /* reconnected components without Fixed-H layer may produce 'tautomeric' fragments like Cl(-) */
3748 : 0 : ip->nMode |= REQ_MODE_BASIC;
3749 : : }
3750 : : /* Although MakeInChIOutOfStrFromINChI2() calls ConnectDisconnectedH(...) */
3751 : : /* to subtracts number of implicit iso H from implicit H */
3752 : : /* this CANNOT have any effect on the deleted H component */
3753 : :
3754 : 0 : ret = MakeInChIOutOfStrFromINChI2(ic, pCG, ip, sd, pStruct1, 0, 0, num_inp);
3755 : :
3756 : 0 : ip->nMode = nMode;
3757 [ # # ]: 0 : if (ret < 0)
3758 : : {
3759 : 0 : goto exit_function;
3760 : : }
3761 [ # # # # ]: 0 : if (bAccumulateChanges && recmet_change_balance)
3762 : : {
3763 : : /* processed Reconnected layer component that is also present in Disconnected layer */
3764 : 0 : delta_recmet_prot += num_prot - num_prot_prev;
3765 : : }
3766 : : }
3767 : : }
3768 : 0 : *nProtonsToBeRemovedByNormFromRevrs = num_prot;
3769 [ # # ]: 0 : if (recmet_change_balance)
3770 : : {
3771 : 0 : *recmet_change_balance = delta_recmet_prot;
3772 : : }
3773 : :
3774 : 0 : exit_function:
3775 : :
3776 [ # # ]: 0 : return ret < 0 ? ret : num_changed;
3777 : : }
3778 : :
3779 : :
3780 : : /****************************************************************************/
3781 : 0 : int AddRemIsoProtonsInRestrStruct(INCHI_CLOCK* ic,
3782 : : CANON_GLOBALS* pCG,
3783 : : ICHICONST INPUT_PARMS* ip_inp,
3784 : : STRUCT_DATA* sd,
3785 : : long num_inp,
3786 : : int bHasSomeFixedH,
3787 : : StrFromINChI* pStruct,
3788 : : int num_components,
3789 : : StrFromINChI* pStructR,
3790 : : int num_componentsR,
3791 : : NUM_H pProtonBalance[],
3792 : : NUM_H recmet_change_balance[])
3793 : : {
3794 : : /* on entry and exit, all at[i].num_H do not include isotopic H and explicit terminal H are connected */
3795 : 0 : int iComp, q, k, ret = 0, bNotEmpty;
3796 : : int num_atoms, num_deleted_H, num_tg, num_changed; /* djb-rwth: removing redundant variables */
3797 : : inp_ATOM* at;
3798 : : NUM_H num_prot[NUM_H_ISOTOPES], delta_recmet_prot[NUM_H_ISOTOPES], num_prot_prev[NUM_H_ISOTOPES];
3799 : : int bAccumulateChanges;
3800 : : INChI_Aux* pINChI_Aux;
3801 : : /* djb-rwth: removing redundant variables */
3802 : : INPUT_PARMS* ip, ip_loc;
3803 : :
3804 : 0 : ip_loc = *ip_inp;
3805 : 0 : ip = &ip_loc;
3806 : :
3807 : 0 : memcpy(num_prot, pProtonBalance, sizeof(num_prot));
3808 [ # # ]: 0 : for (bNotEmpty = 0, k = 0; k < NUM_H_ISOTOPES; k++)
3809 : : {
3810 : 0 : bNotEmpty |= num_prot[k];
3811 : : }
3812 [ # # ]: 0 : if (!bNotEmpty)
3813 : : {
3814 : 0 : return 0;
3815 : : }
3816 : 0 : memset(delta_recmet_prot, 0, sizeof(delta_recmet_prot)); /* djb-rwth: memset_s C11/Annex K variant? */
3817 : 0 : num_changed = 0;
3818 : : /*----------------------------------------------------------------------------------
3819 : : nLink < 0 && num_componentsR > 0 => This is a Disconnected structure component; it is
3820 : : same as already processed reconnected one
3821 : : Do no process it
3822 : :
3823 : : nLink > 0 && num_componentsR > 0 => This is a Disconnected structure component;
3824 : : (should not happen) It it is a result of (nLink-1)th Reconeected
3825 : : component disconnection (NOT IMPLEMENTED YET)
3826 : :
3827 : : nLink = 0 => Process this component. It is either a reconnected
3828 : : component, or a result of a disconnection (for now)
3829 : :
3830 : : nLink > 0 && num_componentsR = 0 => This is a Reconnected component that is same as
3831 : : a disconnected one that will not be processed.
3832 : : Process and save charge delta.
3833 : : -----------------------------------------------------------------------------------*/
3834 : :
3835 [ # # ]: 0 : for (iComp = 0; iComp < num_components && num_prot; iComp++) /* djb-rwth: the condition will always evaluate to true only if pProtonBalance is not NULL */
3836 : : {
3837 : : /* djb-rwth: removing redundant code */
3838 [ # # # # ]: 0 : if (pStruct[iComp].nLink < 0 && num_componentsR > 0)
3839 : : {
3840 : : /* check */
3841 : 0 : q = -(pStruct[iComp].nLink + 1);
3842 [ # # # # : 0 : if (!pStructR || !num_componentsR || q >= num_componentsR || pStructR[q].nLink != (iComp + 1))
# # # # ]
3843 : : {
3844 : 0 : ret = RI_ERR_PROGR;
3845 : 0 : goto exit_function;
3846 : : }
3847 : 0 : continue; /* Disconnected structure component has already been processed as a Reconnected one */
3848 : : }
3849 : :
3850 : 0 : at = pStruct[iComp].at2;
3851 : 0 : num_atoms = pStruct[iComp].num_atoms;
3852 : 0 : num_deleted_H = pStruct[iComp].num_deleted_H; /* djb-rwth: removing redundant code */
3853 [ # # # # ]: 0 : bAccumulateChanges = (pStruct[iComp].nLink > 0 && !num_componentsR);
3854 : :
3855 [ # # # # ]: 0 : if (!at || !num_atoms)
3856 : : {
3857 : 0 : continue;
3858 : : }
3859 : : /* find whether it is a reconnected structure */
3860 : 0 : q = pStruct[iComp].RevInChI.pINChI_Aux[INCHI_REC] &&
3861 [ # # ]: 0 : pStruct[iComp].RevInChI.pINChI_Aux[INCHI_REC][0][TAUT_YES] &&
3862 [ # # # # ]: 0 : pStruct[iComp].RevInChI.pINChI_Aux[INCHI_REC][0][TAUT_YES]->nNumberOfAtoms ? INCHI_REC : INCHI_BAS;
3863 : :
3864 : 0 : pINChI_Aux = pStruct[iComp].RevInChI.pINChI_Aux[q][0][TAUT_YES]; /* 0 = 1st component in RevInChI */
3865 : : /* djb-rwth: removing redundant code */
3866 : 0 : num_tg = pINChI_Aux->nNumberOfTGroups;
3867 : 0 : memcpy(num_prot_prev, num_prot, sizeof(num_prot_prev));
3868 : :
3869 : : /* pass CONNECTED explicit H to AddRemoveIsoProtonsRestr() for isotopic H addition */
3870 : 0 : ret = AddRemoveIsoProtonsRestr(at, num_atoms, num_prot, num_tg);
3871 : :
3872 : 0 : pStruct[iComp].bPostProcessed |= ret;
3873 : 0 : num_changed += (ret > 0);
3874 [ # # ]: 0 : if (ret < 0)
3875 : : {
3876 : 0 : goto exit_function;
3877 : : }
3878 [ # # ]: 0 : if (ret > 0)
3879 : : {
3880 : 0 : StrFromINChI* pStruct1 = pStruct + iComp;
3881 : 0 : INCHI_MODE nMode = ip->nMode;
3882 : : /* recalculate InChI; MakeInChIOutOfStrFromINChI2() will reconnect explicit H */
3883 : : /* disconnect all explicit H and add the number of implicit iso H and all explicit terminal H to the number of implicit H */
3884 [ # # ]: 0 : if (0 > (ret = DisconnectedConnectedH(at, num_atoms, num_deleted_H)))
3885 : : {
3886 : 0 : goto exit_function;
3887 : : }
3888 : 0 : FreeAllINChIArrays(pStruct1->RevInChI.pINChI,
3889 : 0 : pStruct1->RevInChI.pINChI_Aux,
3890 : 0 : pStruct1->RevInChI.num_components);
3891 [ # # # # : 0 : if (bHasSomeFixedH && pStruct1->iInchiRec == INCHI_REC && pStruct1->iMobileH == TAUT_YES &&
# # ]
3892 [ # # # # ]: 0 : !pStruct1->bFixedHExists && !(ip->nMode & REQ_MODE_BASIC))
3893 : : {
3894 : : /* reconnected components without Fixed-H layer may produce 'tautomeric' fragments like Cl(-) */
3895 : 0 : ip->nMode |= REQ_MODE_BASIC;
3896 : : }
3897 : : /* input: disconnected explicit H, output: connected explicit H */
3898 : 0 : ret = MakeInChIOutOfStrFromINChI2(ic, pCG, ip, sd, pStruct1, 0, 0, num_inp);
3899 : 0 : ip->nMode = nMode;
3900 [ # # ]: 0 : if (ret < 0)
3901 : : {
3902 : 0 : goto exit_function;
3903 : : }
3904 : : }
3905 : : /* the following was commented out 2007-08-28 by DT. Reason: it's a bug since H must be already connected */
3906 : : /* else {
3907 : : if ( 0 > ( ret = ConnectDisconnectedH( at, num_atoms, num_deleted_H ) ) ) {
3908 : : goto exit_function;
3909 : : }
3910 : : } */
3911 [ # # ]: 0 : if (bAccumulateChanges)
3912 : : {
3913 : : /* processed Reconnected layer component that is also present in Disconnected layer */
3914 [ # # ]: 0 : for (k = 0; k < NUM_H_ISOTOPES; k++)
3915 : : {
3916 : 0 : delta_recmet_prot[k] += num_prot[k] - num_prot_prev[k];
3917 : : }
3918 : : }
3919 : : }
3920 : :
3921 : 0 : memcpy(pProtonBalance, num_prot, sizeof(num_prot));
3922 [ # # ]: 0 : if (recmet_change_balance)
3923 : : {
3924 : 0 : memcpy(recmet_change_balance, delta_recmet_prot, sizeof(delta_recmet_prot));
3925 : : }
3926 : :
3927 : 0 : exit_function:
3928 : :
3929 [ # # ]: 0 : return ret < 0 ? ret : num_changed;
3930 : : }
3931 : :
3932 : : #endif
|