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 : : #include "mode.h"
10 : : #include "ichicant.h"
11 : : #include "ichitime.h"
12 : : #include "ichirvrs.h"
13 : :
14 : : #include "bcf_s.h"
15 : :
16 : : #if ( READ_INCHI_STRING == 1 )
17 : :
18 : : #define INC_ADD_EDGE 64
19 : :
20 : : /* local types */
21 : :
22 : : /* types for TgDiffHChgFH */
23 : : #define fNumRPosChgH 0 /* number of positive charges on endpoints that have H in at2[] */
24 : : #define fNumRPosChgU 1 /* number of positive charges on endpoints that have no H in at2[] */
25 : : #define fNumRNegChgO 2 /* number of negative charges on O endpoints */
26 : : #define fNumRNegChgN 3 /* number of negative charges on N endpoints */
27 : : #define fNumRNeutrlH 4 /* number of neutral endp that have H in at2[] */
28 : :
29 : : #define fNumNPosChgH 5 /* number of positive charges on endpoints that have H in atf[] */
30 : : #define fNumNPosChgU 6 /* number of positive charges on endpoints that have no H in atf[] */
31 : : #define fNumNNegChgO 7 /* number of negative charges on O endpoints */
32 : : #define fNumNNegChgN 8 /* number of negative charges on N endpoints */
33 : : #define fNumNNeutrlH 9 /* number of neutral endp that have H in atf[] */
34 : :
35 : : #define fNumAllChgT 10 /* total number of fNum... */
36 : :
37 : : typedef struct tagTgDiffHChgFH {
38 : : short itg; /* t-group index; endpoint = itg+1 */
39 : : short nNumHInchi; /* number of H in t-group from orig. InChI */
40 : : short nNumHRevrs; /* number of H in at2[] */
41 : : short nNumHNorml; /* number of H in Normalized atfMobile_H_Revrs[] */
42 : : short nNumMInchi; /* number of (-) in InChI */
43 : : short nNumMRevrs; /* number of (-) in at2[] */
44 : : short nNumMNorml; /* number of (-) in atf[] */
45 : : short nNumPRevrs; /* number of (+) in at2[] */
46 : : short nNumPNorml; /* number of (+) in Normalized atfMobile_H_Revrs[] */
47 : : short n[fNumAllChgT]; /* all numbers */
48 : : short i[fNumAllChgT]; /* all indices */
49 : : } TgDiffHChgFH;
50 : :
51 : : /* local prototypes */
52 : : static int FillTgDiffHChgFH( TgDiffHChgFH tdhc[], int max_tdhc, inp_ATOM at2[], inp_ATOM atf[],
53 : : AT_NUMB *nCanon2AtnoRevrs, VAL_AT *pVA, T_GROUP_INFO *ti, EDGE_LIST *pAtomIndList );
54 : :
55 : :
56 : : /****************************************************************************/
57 : 0 : int bHas_N_V( inp_ATOM *at2, int num_atoms )
58 : : {
59 : 0 : int i, num_found = 0;
60 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
61 : : {
62 [ # # ]: 0 : if (at2[i].el_number == EL_NUMBER_N &&
63 [ # # ]: 0 : !at2[i].charge &&
64 [ # # ]: 0 : !at2[i].num_H &&
65 [ # # ]: 0 : !at2[i].radical &&
66 [ # # ]: 0 : at2[i].chem_bonds_valence == 5 &&
67 [ # # ]: 0 : ( at2[i].valence == 3 ))
68 : : {
69 : 0 : num_found++;
70 : : }
71 : : }
72 : :
73 : 0 : return num_found;
74 : : }
75 : :
76 : :
77 : : /****************************************************************************/
78 : 0 : int FillTgDiffHChgFH( TgDiffHChgFH tdhc[],
79 : : int max_tdhc,
80 : : inp_ATOM at2[],
81 : : inp_ATOM atf[],
82 : : AT_NUMB *nCanon2AtnoRevrs,
83 : : VAL_AT *pVA,
84 : : T_GROUP_INFO *ti,
85 : : EDGE_LIST *pAtomIndList )
86 : : {
87 : :
88 : : int i, j, iat, itg, num, itg_out; /* djb-rwth: removing redundant variables */
89 : : EDGE_LIST IndList; /* type, itg */
90 : : TgDiffHChgFH cur_tdhc;
91 : : AT_NUMB *pEndp0;
92 : : inp_ATOM *at2i, *atfi;
93 : 0 : int typeR, typeN, type, ret = 0, nCurIndListLen;
94 : :
95 : 0 : AllocEdgeList( &IndList, EDGE_LIST_CLEAR );
96 : 0 : pAtomIndList->num_edges = 0;
97 : 0 : itg_out = 0;
98 : : /* djb-rwth: removing redundant code */
99 : 0 : memset( tdhc, 0, max_tdhc ); /* djb-rwth: memset_s C11/Annex K variant? */
100 : :
101 [ # # ]: 0 : for (itg = 0; itg < ti->num_t_groups; itg++)
102 : : {
103 : 0 : memset( &cur_tdhc, 0, sizeof( cur_tdhc ) ); /* djb-rwth: memset_s C11/Annex K variant? */
104 : :
105 : 0 : cur_tdhc.itg = itg;
106 : 0 : cur_tdhc.nNumHInchi = ti->t_group[itg].num[0] - ti->t_group[itg].num[1];
107 : 0 : cur_tdhc.nNumMInchi = ti->t_group[itg].num[1];
108 : :
109 : 0 : pEndp0 = ti->nEndpointAtomNumber + ti->t_group[itg].nFirstEndpointAtNoPos;
110 : 0 : nCurIndListLen = IndList.num_edges;
111 [ # # ]: 0 : for (j = 0; j < ti->t_group[itg].nNumEndpoints; j++)
112 : : {
113 : 0 : i = pEndp0[j];
114 : 0 : iat = nCanon2AtnoRevrs[i];
115 : :
116 : 0 : at2i = at2 + iat;
117 : 0 : atfi = atf + iat;
118 : :
119 : 0 : typeR = typeN = -1;
120 [ # # ]: 0 : if (at2i->charge == 1)
121 : : {
122 [ # # ]: 0 : if (at2i->num_H)
123 : : {
124 : 0 : typeR = fNumRPosChgH;
125 : : }
126 : : else
127 : : {
128 : 0 : typeR = fNumRPosChgU;
129 : : }
130 : 0 : cur_tdhc.nNumPRevrs++;
131 : : }
132 : : else
133 : : {
134 [ # # ]: 0 : if (at2i->charge == -1)
135 : : {
136 [ # # ]: 0 : if (pVA[iat].cNumValenceElectrons == 6)
137 : : {
138 : 0 : typeR = fNumRNegChgO;
139 : : }
140 : : else
141 : : {
142 [ # # ]: 0 : if (pVA[iat].cNumValenceElectrons == 5)
143 : : {
144 : 0 : typeR = fNumRNegChgN;
145 : : }
146 : : }
147 : 0 : cur_tdhc.nNumMRevrs++;
148 : : }
149 : : else
150 : : {
151 [ # # # # ]: 0 : if (at2i->num_H && at2i->valence == at2i->chem_bonds_valence)
152 : : {
153 : 0 : typeR = fNumRNeutrlH;
154 : : }
155 : : }
156 : : }
157 : 0 : cur_tdhc.nNumHRevrs += at2i->num_H;
158 : :
159 [ # # ]: 0 : if (atfi->charge == 1)
160 : : {
161 [ # # ]: 0 : if (atfi->num_H)
162 : : {
163 : 0 : typeN = fNumNPosChgH;
164 : : }
165 : : else
166 : : {
167 : 0 : typeN = fNumNPosChgU;
168 : : }
169 : 0 : cur_tdhc.nNumPNorml++;
170 : : }
171 : : else
172 : : {
173 [ # # ]: 0 : if (atfi->charge == -1)
174 : : {
175 [ # # ]: 0 : if (pVA[iat].cNumValenceElectrons == 6)
176 : : {
177 : 0 : typeN = fNumNNegChgO;
178 : : }
179 : : else
180 [ # # ]: 0 : if (pVA[iat].cNumValenceElectrons == 5)
181 : : {
182 : 0 : typeN = fNumNNegChgN;
183 : : }
184 : 0 : cur_tdhc.nNumMNorml++;
185 : : }
186 : : else
187 : : {
188 [ # # # # ]: 0 : if (atfi->num_H && atfi->valence == atfi->chem_bonds_valence)
189 : : {
190 : 0 : typeN = fNumNNeutrlH;
191 : : }
192 : : }
193 : : }
194 : 0 : cur_tdhc.nNumHNorml += atfi->num_H;
195 [ # # # # ]: 0 : if (at2[iat].charge < 0 || 0 < pVA[iat].nCPlusGroupEdge)
196 : : {
197 [ # # # # ]: 0 : if (typeR >= 0 && (
198 [ # # ]: 0 : ( ret = AddToEdgeList( &IndList, typeR, INC_ADD_EDGE ) ) ||
199 [ # # ]: 0 : ( ret = AddToEdgeList( &IndList, itg, INC_ADD_EDGE ) ) ||
200 : 0 : ( ret = AddToEdgeList( &IndList, iat, INC_ADD_EDGE ) ) ))
201 : : {
202 : 0 : goto exit_function;
203 : : }
204 [ # # # # ]: 0 : if (typeN >= 0 && (
205 [ # # ]: 0 : ( ret = AddToEdgeList( &IndList, typeN, INC_ADD_EDGE ) ) ||
206 [ # # ]: 0 : ( ret = AddToEdgeList( &IndList, itg, INC_ADD_EDGE ) ) ||
207 : 0 : ( ret = AddToEdgeList( &IndList, iat, INC_ADD_EDGE ) ) ))
208 : : {
209 : 0 : goto exit_function;
210 : : }
211 : : }
212 : : }
213 [ # # ]: 0 : if (cur_tdhc.nNumHNorml == cur_tdhc.nNumHInchi &&
214 [ # # ]: 0 : cur_tdhc.nNumMNorml == cur_tdhc.nNumMInchi)
215 : : {
216 : 0 : IndList.num_edges = nCurIndListLen; /* t-group seems to be correct */
217 : 0 : continue;
218 : : }
219 [ # # ]: 0 : if (itg_out < max_tdhc)
220 : : {
221 : 0 : tdhc[itg_out++] = cur_tdhc;
222 : : }
223 : : else
224 : : {
225 : : /* djb-rwth: removing redundant code */
226 : 0 : IndList.num_edges = nCurIndListLen;
227 : 0 : break;
228 : : }
229 : : }
230 : :
231 : : /* fill out atom index list */
232 [ # # ]: 0 : if (itg_out)
233 : : {
234 : 0 : itg_out--; /* djb-rwth: fixing undefined index value / buffer overflow */
235 : : /* djb-rwth: removing redundant code */
236 [ # # ]: 0 : for (type = 0; type < fNumAllChgT; type++)
237 : : {
238 : 0 : j = 0;
239 [ # # ]: 0 : for (i = 0; i < itg_out; i++)
240 : : {
241 : 0 : num = 0;
242 : 0 : itg = tdhc[i].itg;
243 : 0 : tdhc[i].i[type] = -999; /* empty */
244 [ # # ]: 0 : while (IndList.pnEdges[j + 1] == itg)
245 : : {
246 [ # # ]: 0 : if (IndList.pnEdges[j] == type)
247 : : {
248 [ # # ]: 0 : if (!num++)
249 : : {
250 : 0 : tdhc[i].i[type] = pAtomIndList->num_edges;
251 : : }
252 [ # # ]: 0 : if ((ret = AddToEdgeList( pAtomIndList, IndList.pnEdges[j + 2], INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
253 : : {
254 : 0 : goto exit_function;
255 : : }
256 : : }
257 : 0 : j += 3;
258 : : }
259 : 0 : tdhc[i].n[type] = num;
260 : : }
261 : : }
262 : : }
263 : 0 : ret = itg_out;
264 : :
265 : 0 : exit_function:
266 : :
267 : 0 : AllocEdgeList( &IndList, EDGE_LIST_FREE );
268 : :
269 : 0 : return ret;
270 : : /*
271 : : #undef fNumRPosChgH
272 : : #undef fNumRPosChgU
273 : : #undef fNumRNegChgO
274 : : #undef fNumRNegChgN
275 : :
276 : : #undef fNumNPosChgH
277 : : #undef fNumNPosChgU
278 : : #undef fNumNNegChgO
279 : : #undef fNumNNegChgN
280 : :
281 : : #undef fNumAllChgT
282 : : */
283 : : }
284 : :
285 : :
286 : : /****************************************************************************/
287 : : /* djb-rwth: placed as global variables to avoid function buffer issues */
288 : : short iat_DB_O[MAX_DIFF_FIXH], iat_SB_O_Minus[MAX_DIFF_FIXH];
289 : : short iat_DB_O_Plus[MAX_DIFF_FIXH], iat_SB_NH[MAX_DIFF_FIXH];
290 : : short iat_SB_N_Minus[MAX_DIFF_FIXH], iat_DB_N[MAX_DIFF_FIXH];
291 : : short iat_SB_Neutr[MAX_DIFF_FIXH], iat_DB_Charged[MAX_DIFF_FIXH];
292 : : short iat_DB_NHn_Plus[MAX_DIFF_FIXH], iat_SB_NHm_Neutr[MAX_DIFF_FIXH];
293 : : short iat_DB_O_Neutr[MAX_DIFF_FIXH], iat_DB_N_Neutr[MAX_DIFF_FIXH];
294 : : short iat_SB_N_Neutr[MAX_DIFF_FIXH], iat_N_V_Array[MAX_DIFF_FIXH];
295 : : short iat_Central[MAX_DIFF_FIXH], iat_NO2[MAX_DIFF_FIXH];
296 : : TgDiffHChgFH tdhc[MAX_DIFF_FIXH];
297 : :
298 : 0 : int FixFixedHRestoredStructure( CANON_GLOBALS *pCG,
299 : : INCHI_CLOCK *ic,
300 : : ICHICONST INPUT_PARMS *ip,
301 : : STRUCT_DATA *sd,
302 : : BN_STRUCT *pBNS,
303 : : BN_DATA *pBD,
304 : : StrFromINChI *pStruct,
305 : : inp_ATOM *at,
306 : : inp_ATOM *at2,
307 : : inp_ATOM *at3,
308 : : VAL_AT *pVA,
309 : : ALL_TC_GROUPS *pTCGroups,
310 : : T_GROUP_INFO **ppt_group_info,
311 : : inp_ATOM **ppat_norm,
312 : : inp_ATOM **ppat_prep,
313 : : INChI *pInChI[],
314 : : long num_inp,
315 : : int bHasSomeFixedH,
316 : : int *pnNumRunBNS,
317 : : int *pnTotalDelta,
318 : : int forbidden_edge_mask,
319 : : int forbidden_stereo_edge_mask )
320 : : {
321 : : /*--------- process extra or missing Fixed-H on non-tautomeric atoms ------*/
322 : : /* at2 should be the most recently restored atom, Fixed-H */
323 : 0 : int i, j, k, delta, num_try, tot_succes, cur_success, ret = 0, bAllowedNFlowerEdges = 0, num_zero_ret;
324 : : CMP2FHINCHI c2i;
325 : 0 : CMP2FHINCHI *pc2i = &c2i;
326 : :
327 : : EDGE_LIST AllChargeEdges, CurrEdges, SFlowerEdges, NFlowerEdges, OtherNFlowerEdges, FixedLargeRingStereoEdges;
328 : : EDGE_LIST AllBondEdges;
329 : :
330 : : EdgeIndex e;
331 : : BNS_EDGE *pe;
332 : : Vertex v1, v2;
333 : : BNS_VERTEX *pv1, *pv2;
334 : :
335 : : Vertex vPathStart, vPathEnd;
336 : : int nPathLen, nDeltaH, nDeltaCharge, nNumVisitedAtoms;
337 : :
338 : 0 : int nNumRunBNS = 0, forbidden_edge_mask_inv = ~forbidden_edge_mask; /* djb-rwth: ignoring LLVM warning: variable used */
339 : :
340 : : INCHI_HEAPCHK
341 : :
342 : 0 : AllocEdgeList( &AllChargeEdges, EDGE_LIST_CLEAR );
343 : 0 : AllocEdgeList( &CurrEdges, EDGE_LIST_CLEAR );
344 : 0 : AllocEdgeList( &NFlowerEdges, EDGE_LIST_CLEAR );
345 : 0 : AllocEdgeList( &SFlowerEdges, EDGE_LIST_CLEAR );
346 : 0 : AllocEdgeList( &OtherNFlowerEdges, EDGE_LIST_CLEAR );
347 : 0 : AllocEdgeList( &FixedLargeRingStereoEdges, EDGE_LIST_CLEAR );
348 : 0 : AllocEdgeList( &AllBondEdges, EDGE_LIST_CLEAR );
349 : :
350 : 0 : tot_succes = 0;
351 : :
352 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
353 : : {
354 : 0 : goto exit_function; /* no fixed-H found */
355 : : }
356 : :
357 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
358 : : {
359 [ # # # # : 0 : if (( e = pVA[i].nCMinusGroupEdge - 1 ) >= 0 && !pBNS->edge[e].forbidden &&
# # ]
360 : 0 : ( ret = AddToEdgeList( &AllChargeEdges, e, INC_ADD_EDGE ) ))
361 : : {
362 : 0 : goto exit_function;
363 : : }
364 [ # # # # ]: 0 : if (( e = pVA[i].nCPlusGroupEdge - 1 ) >= 0 && !pBNS->edge[e].forbidden)
365 : : {
366 [ # # ]: 0 : if ((ret = AddToEdgeList( &AllChargeEdges, e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
367 : : {
368 : 0 : goto exit_function;
369 : : }
370 : :
371 : : /* in addition, disallow N(V) creation by forbidding charge flower edge that has flow=1 */
372 [ # # # # : 0 : if (pVA[i].cNumValenceElectrons == 5 && !pVA[i].cMetal && /* N, P, As */
# # ]
373 : 0 : NO_VERTEX != ( j = GetChargeFlowerUpperEdge( pBNS, pVA, e ) ))
374 : : {
375 : :
376 [ # # ]: 0 : if (pBNS->edge[j].forbidden)
377 : : {
378 : 0 : continue;
379 : : }
380 : :
381 [ # # ]: 0 : if (pBNS->edge[j].flow)
382 : : {
383 [ # # ]: 0 : if ((ret = AddToEdgeList( &AllChargeEdges, j, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
384 : : {
385 : 0 : goto exit_function;
386 : : }
387 [ # # ]: 0 : if ((ret = AddToEdgeList( &NFlowerEdges, j, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
388 : : {
389 : 0 : goto exit_function;
390 : : }
391 : : }
392 : : else
393 : : {
394 [ # # ]: 0 : if ((ret = AddToEdgeList( &OtherNFlowerEdges, j, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
395 : : {
396 : 0 : goto exit_function;
397 : : }
398 : : }
399 : : }
400 : : else
401 : : {
402 : : /* in addition, disallow N(V) creation by forbidding charge flower edge that has flow=1 */
403 [ # # # # : 0 : if (pVA[i].cNumValenceElectrons == 6 && !pVA[i].cMetal && /* N, P, As */
# # ]
404 : 0 : NO_VERTEX != ( j = GetChargeFlowerUpperEdge( pBNS, pVA, e ) ))
405 : : {
406 : :
407 [ # # ]: 0 : if (pBNS->edge[j].forbidden)
408 : : {
409 : 0 : continue;
410 : : }
411 : :
412 [ # # ]: 0 : if (pBNS->edge[j].flow)
413 : : {
414 [ # # ]: 0 : if ((ret = AddToEdgeList( &SFlowerEdges, j, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
415 : : {
416 : 0 : goto exit_function;
417 : : }
418 : : }
419 : : }
420 : : }
421 : : }
422 [ # # ]: 0 : for (j = 0; j < at2[i].valence; j++)
423 : : {
424 : 0 : k = at2[i].neighbor[j];
425 [ # # # # ]: 0 : if (k < i && !pBNS->edge[e = pBNS->vert[i].iedge[j]].forbidden)
426 : : {
427 [ # # ]: 0 : if ((ret = AddToEdgeList( &AllBondEdges, e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
428 : : {
429 : 0 : goto exit_function;
430 : : }
431 : : }
432 : : }
433 : : }
434 [ # # ]: 0 : if (forbidden_stereo_edge_mask)
435 : : {
436 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
437 : : {
438 [ # # ]: 0 : for (j = 0; j < at2[i].valence; j++)
439 : : {
440 [ # # ]: 0 : if (pBNS->edge[k = pBNS->vert[i].iedge[j]].forbidden == forbidden_stereo_edge_mask)
441 : : {
442 : 0 : int nMinRingSize = is_bond_in_Nmax_memb_ring( at2, i, j, pStruct->pbfsq->q,
443 : 0 : pStruct->pbfsq->nAtomLevel,
444 : 0 : pStruct->pbfsq->cSource, 99 /* max ring size */ );
445 [ # # # # ]: 0 : if (0 < nMinRingSize && ( ret = AddToEdgeList( &FixedLargeRingStereoEdges, k, INC_ADD_EDGE ) ))
446 : : {
447 : 0 : goto exit_function;
448 : : }
449 : : }
450 : : }
451 : : }
452 : : }
453 : :
454 : : INCHI_HEAPCHK
455 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr( pStruct ))) /* djb-rwth: addressing LLVM warning */
456 : : {
457 : 0 : goto exit_function;
458 : : }
459 : : INCHI_HEAPCHK
460 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI( pStruct, at2, pVA, pInChI, pc2i ))) /* djb-rwth: addressing LLVM warning */
461 : : {
462 : 0 : goto exit_function;
463 : : }
464 : :
465 : : INCHI_HEAPCHK
466 : :
467 [ # # ]: 0 : if (!pc2i->bHasDifference ||
468 [ # # # # ]: 0 : (!pc2i->len_c2at && pc2i->nNumTgRevrs == pc2i->nNumTgInChI &&
469 [ # # ]: 0 : pc2i->nNumEndpRevrs == pc2i->nNumRemHInChI &&
470 [ # # ]: 0 : pc2i->nNumEndpRevrs == pc2i->nNumEndpInChI &&
471 [ # # # # ]: 0 : !pc2i->nNumTgDiffMinus && !pc2i->nNumTgDiffH)) /* djb-rwth: addressing LLVM warning */
472 : : {
473 : 0 : goto exit_function; /* nothing to do */
474 : : }
475 : :
476 : : /*goto exit_function;*/ /* debug only*/
477 : :
478 [ # # ]: 0 : if (pc2i->len_c2at >= 2)
479 : : {
480 : : /*----------------------------------------------------*/
481 : : /* case 01: restored: O=AB-O(-) original: (-)O-AB=O */
482 : : /* FixH: 0 -1 -1 0 */
483 : : /* MobH: 0 1 1 0 */
484 : : /* non-taut non-taut */
485 : : /* O = O, S, Se; charged atoms O are not tautomeric */
486 : : /* Solution: move (-) from B-O(-) to O=A */
487 : : /*----------------------------------------------------*/
488 : 0 : int num_DB_O = 0, num_SB_O_Minus = 0, iat;
489 : 0 : cur_success = 0;
490 : :
491 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
492 : : {
493 : 0 : iat = pc2i->c2at[i].atomNumber;
494 [ # # ]: 0 : if (pc2i->c2at[i].nValElectr == 6 /* && !pc2i->c2at[i].endptInChI -- mod#1*/ &&
495 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden)
496 : : {
497 [ # # ]: 0 : if ( /* orig. InChI info: */
498 : 0 : num_SB_O_Minus < MAX_DIFF_FIXH &&
499 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == 0 && pc2i->c2at[i].nMobHInChI == 0 &&
500 : : /* reversed structure info: */
501 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs == -1 && pc2i->c2at[i].nMobHRevrs == 1 &&
502 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == -1 && !at2[iat].num_H && /* at2 is Fixed-H */
503 [ # # # # ]: 0 : at2[iat].valence == 1 && at2[iat].chem_bonds_valence == 1)
504 : : {
505 : 0 : iat_SB_O_Minus[num_SB_O_Minus++] = iat;
506 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
507 : : {
508 : 0 : goto exit_function;
509 : : }
510 : : }
511 : : else
512 : : {
513 [ # # ]: 0 : if ( /* orig. InChI info: */
514 : 0 : num_DB_O < MAX_DIFF_FIXH &&
515 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == -1 && pc2i->c2at[i].nMobHInChI == 1 &&
516 : : /* reversed structure info: */
517 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && pc2i->c2at[i].nMobHRevrs == 0 &&
518 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && !at2[iat].num_H &&
519 [ # # # # ]: 0 : at2[iat].valence == 1 && at2[iat].chem_bonds_valence == 2)
520 : : {
521 : 0 : iat_DB_O[num_DB_O++] = iat;
522 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
523 : : {
524 : 0 : goto exit_function;
525 : : }
526 : : }
527 : : }
528 : : }
529 : : }
530 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_O_Minus, num_DB_O))) /* djb-rwth: addressing LLVM warning */
531 : : {
532 : : /* detected; attempt to fix */
533 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
534 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
535 : 0 : delta = 1;
536 [ # # # # ]: 0 : for (i = 0; i < num_SB_O_Minus && cur_success < num_try; i++)
537 : : {
538 : 0 : iat = iat_SB_O_Minus[i];
539 : 0 : pe = pBNS->edge + pVA[iat].nCMinusGroupEdge - 1;
540 [ # # ]: 0 : if (!pe->flow)
541 : 0 : continue;
542 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
543 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
544 : :
545 : 0 : pe->forbidden |= forbidden_edge_mask;
546 : 0 : pe->flow -= delta; /* remove (-) from AB-O(-) */
547 : 0 : pv1->st_edge.flow -= delta;
548 : 0 : pv2->st_edge.flow -= delta;
549 : 0 : pBNS->tot_st_flow -= 2 * delta;
550 : :
551 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
552 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
553 : :
554 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
555 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 1) /* djb-rwth: addressing LLVM warnings */
# # ]
556 : : {
557 : : /* Added (-)charge to O=AB => nDeltaCharge == -1 */
558 : : /* Flow change on pe (-)charge edge (atom B-O(-)) is not known to RunBnsTestOnce()) */
559 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
560 [ # # ]: 0 : if (ret > 0)
561 : : {
562 : 0 : nNumRunBNS++;
563 : 0 : cur_success++; /* 01 */
564 : : }
565 : : }
566 : : else
567 : : {
568 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
569 : 0 : pe->flow += delta;
570 : 0 : pv1->st_edge.flow += delta;
571 : 0 : pv2->st_edge.flow += delta;
572 : 0 : pBNS->tot_st_flow += 2 * delta;
573 : : }
574 : : INCHI_HEAPCHK
575 : : }
576 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
577 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
578 : : }
579 [ # # ]: 0 : if (cur_success)
580 : : {
581 : 0 : tot_succes += cur_success;
582 : : /* recalculate InChI from the structure */
583 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic,
584 : : ip, sd, pBNS, pStruct,
585 : : at, at2, at3, pVA,
586 : : pTCGroups,
587 : : ppt_group_info, ppat_norm,
588 : : ppat_prep)))
589 : : {
590 : 0 : goto exit_function;
591 : : }
592 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
593 : : {
594 : 0 : goto exit_function;
595 : : }
596 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
597 : : {
598 : 0 : goto exit_function; /* no fixed-H found */
599 : : }
600 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
601 : : {
602 : 0 : goto exit_function;
603 : : }
604 [ # # ]: 0 : if (!pc2i->bHasDifference)
605 : : {
606 : 0 : goto exit_function; /* nothing to do */
607 : : }
608 : : }
609 : : }
610 : :
611 [ # # ]: 0 : if (pc2i->len_c2at >= 1)
612 : : {
613 : : /*--------------------------------------------------------------*/
614 : : /* case 02: restored: -O(+)=AB-NH2 original: -O-AB=NH2(+) */
615 : : /* FixH: 0 0 0 1 */
616 : : /* MobH: 0 2 0 1 */
617 : : /* O = P, As, Sb, O, S, Se, F, Cl, Br, I; not taut. in InChI */
618 : : /* N = N, O, S, Se, Te; has H; tautomeric or not tautomeric */
619 : : /* Solution: move (+) from O(+) to NH2 */
620 : : /*--------------------------------------------------------------*/
621 : 0 : int num_DB_O_Plus = 0, num_SB_NH = 0, iat;
622 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
623 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
624 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : NULL;
625 : 0 : cur_success = 0;
626 : 0 : num_zero_ret = 0;
627 : :
628 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
629 : : {
630 : 0 : iat = pc2i->c2at[i].atomNumber;
631 [ # # ]: 0 : if ( /* orig. InChI info: =NH2(+), =OH(+) */
632 : 0 : num_SB_NH < MAX_DIFF_FIXH &&
633 [ # # # # ]: 0 : ((pc2i->c2at[i].nValElectr == 5 && pc2i->c2at[i].nPeriodNum == 1) ||
634 [ # # ]: 0 : pc2i->c2at[i].nValElectr == 6) /* N, O, S, Se, Te */ &&
635 : : /*!pc2i->c2at[i].endptInChI &&*/ /* <=== relaxation */
636 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
637 [ # # ]: 0 : pc2i->c2at[i].nFixHInChI>0 /*== 1 --modification#2*/ && /*pc2i->c2at[i].nMobHInChI == 1 &&*/
638 : : /* reversed structure info: */
639 [ # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && /* pc2i->c2at[i].nMobHRevrs == 0 &&*/
640 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && at2[iat].num_H &&
641 [ # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence) /* djb-rwth: addressing LLVM warning */
642 : : {
643 : 0 : iat_SB_NH[num_SB_NH++] = iat;
644 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
645 : : {
646 : 0 : goto exit_function;
647 : : }
648 : : }
649 : : }
650 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
651 : : {
652 : : /* i = canonical number - 1 */
653 : 0 : iat = nCanon2AtnoRevrs[i];
654 [ # # ]: 0 : if ( /* in restored atom: charge=+1, no H, has double bond, P, As, O, S, Se, Te, F, Cl, Br, I */
655 : 0 : num_DB_O_Plus < MAX_DIFF_FIXH &&
656 [ # # # # ]: 0 : at2[iat].charge == 1 && !at2[iat].num_H &&
657 [ # # # # ]: 0 : at2[iat].valence < at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
658 [ # # # # ]: 0 : (pVA[iat].cNumValenceElectrons == 6 || pVA[iat].cNumValenceElectrons == 7 ||
659 [ # # # # ]: 0 : (pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber > 1)) &&
660 : : /* in orig.InChI: not an endpoint, has no H */
661 [ # # ]: 0 : !pStruct->endpoint[i] &&
662 [ # # # # : 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
663 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
664 : : /* has (+) edge */
665 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden) /* djb-rwth: addressing LLVM warning */
666 : : {
667 : 0 : iat_DB_O_Plus[num_DB_O_Plus++] = iat;
668 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
669 : : {
670 : 0 : goto exit_function;
671 : : }
672 : : }
673 : : }
674 [ # # ]: 0 : if ((num_try = inchi_min(num_DB_O_Plus, num_SB_NH))) /* djb-rwth: addressing LLVM warning */
675 : : {
676 : : /* detected; attempt to fix */
677 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
678 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
679 : 0 : delta = 1;
680 : 0 : repeat_02_allow_NV:
681 [ # # # # ]: 0 : for (i = 0; i < num_SB_NH && cur_success < num_try; i++)
682 : : {
683 : 0 : iat = iat_SB_NH[i];
684 : 0 : pe = pBNS->edge + pVA[iat].nCPlusGroupEdge - 1;
685 [ # # ]: 0 : if (!pe->flow)
686 : 0 : continue;
687 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
688 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
689 : :
690 : 0 : pe->forbidden |= forbidden_edge_mask;
691 : 0 : pe->flow -= delta;
692 : 0 : pv1->st_edge.flow -= delta;
693 : 0 : pv2->st_edge.flow -= delta;
694 : 0 : pBNS->tot_st_flow -= 2 * delta;
695 : :
696 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
697 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
698 : :
699 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
700 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == -1) /* djb-rwth: addressing LLVM warning */
# # ]
701 : : {
702 : : /* Removed charge from O(+) => nDeltaCharge == -1 */
703 : : /* Flow change on pe (+)charge edge (atom NH2) is not known to RunBnsTestOnce()) */
704 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
705 [ # # ]: 0 : if (ret > 0)
706 : : {
707 : 0 : nNumRunBNS++;
708 : 0 : cur_success++; /* 02 */
709 : : }
710 : : }
711 : : else
712 : : {
713 : 0 : num_zero_ret += !ret;
714 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
715 : 0 : pe->flow += delta;
716 : 0 : pv1->st_edge.flow += delta;
717 : 0 : pv2->st_edge.flow += delta;
718 : 0 : pBNS->tot_st_flow += 2 * delta;
719 : : }
720 : : INCHI_HEAPCHK
721 : : }
722 [ # # # # : 0 : if (num_zero_ret == num_try && !bAllowedNFlowerEdges && NFlowerEdges.num_edges)
# # ]
723 : : {
724 : 0 : RemoveForbiddenEdgeMask(pBNS, &NFlowerEdges, forbidden_edge_mask);
725 : 0 : bAllowedNFlowerEdges = 1;
726 : 0 : goto repeat_02_allow_NV;
727 : : }
728 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
729 : : /* djb-rwth: removing redundant code */
730 : : }
731 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
732 [ # # ]: 0 : if (cur_success)
733 : : {
734 : 0 : tot_succes += cur_success;
735 : : /* recalculate InChI from the structure */
736 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
737 : : ppt_group_info, ppat_norm, ppat_prep)))
738 : : {
739 : 0 : goto exit_function;
740 : : }
741 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
742 : : {
743 : 0 : goto exit_function;
744 : : }
745 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
746 : : {
747 : 0 : goto exit_function; /* no fixed-H found */
748 : : }
749 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
750 : : {
751 : 0 : goto exit_function;
752 : : }
753 [ # # ]: 0 : if (!pc2i->bHasDifference)
754 : : {
755 : 0 : goto exit_function; /* nothing to do */
756 : : }
757 : : }
758 : : }
759 : :
760 [ # # # # ]: 0 : if (pc2i->len_c2at >= 1 && pc2i->nNumTgRevrs == 1 &&
761 [ # # # # ]: 0 : ( pc2i->nNumEndpRevrs > pc2i->nNumEndpInChI || pc2i->nNumTgInChI > 1 ) /* ADP in Revrs */)
762 : : {
763 : : /*--------------------------------------------------------------*/
764 : : /* case 03: restored: -N(-)-AB=O original: -N=AB-O(-) */
765 : : /* FixH: 0 0 0 -1 */
766 : : /* MobH: 0 0 0 1 */
767 : : /* O = O, S, Se; N = N; */
768 : : /* restored atoms are tautomeric; original atoms are not taut. */
769 : : /* restored struct has 1 t-group; original has less endpoints */
770 : : /* and possibly >1 t-groups */
771 : : /* Solution: move (-) from N(-) to =O */
772 : : /* these atoms are tautomeric in restored structure */
773 : : /*--------------------------------------------------------------*/
774 : 0 : int num_SB_N_Minus = 0, num_DB_O = 0, iat;
775 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
776 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
777 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
778 : : /*
779 : : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H? pInChI[1]->nNum_H :
780 : : pInChI[0] && pInChI[0]->nNum_H? pInChI[0]->nNum_H : 0;
781 : : */
782 : 0 : cur_success = 0;
783 : :
784 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
785 : : {
786 : 0 : iat = pc2i->c2at[i].atomNumber;
787 [ # # ]: 0 : if ( /* orig. InChI info: -O(-) */
788 : 0 : num_DB_O < MAX_DIFF_FIXH &&
789 [ # # ]: 0 : pc2i->c2at[i].nValElectr == 6 /* O, S, Se, Te */ &&
790 [ # # ]: 0 : !pc2i->c2at[i].endptInChI &&
791 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
792 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == -1 && pc2i->c2at[i].nMobHInChI == 1 &&
793 : : /* reversed structure info: */
794 [ # # ]: 0 : pc2i->c2at[i].endptRevrs &&
795 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && pc2i->c2at[i].nMobHRevrs == 0 &&
796 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && !at2[iat].num_H &&
797 [ # # # # ]: 0 : at2[iat].valence == 1 && at2[iat].chem_bonds_valence == 2)
798 : : {
799 : 0 : iat_DB_O[num_DB_O++] = iat;
800 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
801 : : {
802 : 0 : goto exit_function;
803 : : }
804 : : }
805 : : }
806 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
807 : : {
808 : : /* i = canonical number - 1 */
809 : 0 : iat = nCanon2AtnoRevrs[i];
810 [ # # ]: 0 : if ( /* in restored atom N: charge=-1, no H, has no double bond, endpoint */
811 : 0 : num_SB_N_Minus < MAX_DIFF_FIXH &&
812 [ # # ]: 0 : at2[iat].charge == -1 && /*!at2[iat].num_H &&*/
813 [ # # # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
814 [ # # # # : 0 : pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1 &&
# # ]
815 [ # # ]: 0 : at_Mobile_H_Revrs && at_Mobile_H_Revrs[iat].endpoint &&
816 : : /* in orig.InChI: not an endpoint, has no H */
817 : : /* !pStruct->endpoint[i] && */
818 : : /*
819 : : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
820 : : !(nMobHInChI && nMobHInChI[i] ) &&
821 : : */
822 : : /* has (-) edge */
823 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden)
824 : : {
825 : 0 : iat_SB_N_Minus[num_SB_N_Minus++] = iat;
826 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
827 : : {
828 : 0 : goto exit_function;
829 : : }
830 : : }
831 : : }
832 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_N_Minus, num_DB_O))) /* djb-rwth: addressing LLVM warning */
833 : : {
834 : : /* detected; attempt to fix */
835 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
836 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
837 : 0 : delta = 1;
838 [ # # # # ]: 0 : for (i = 0; i < num_SB_N_Minus && cur_success < num_try; i++)
839 : : {
840 : 0 : iat = iat_SB_N_Minus[i];
841 : 0 : pe = pBNS->edge + pVA[iat].nCMinusGroupEdge - 1; /* 2006-03-03: changed from CPlusGroupEdge */
842 [ # # ]: 0 : if (!pe->flow)
843 : 0 : continue;
844 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
845 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
846 : :
847 : 0 : pe->forbidden |= forbidden_edge_mask;
848 : 0 : pe->flow -= delta;
849 : 0 : pv1->st_edge.flow -= delta;
850 : 0 : pv2->st_edge.flow -= delta;
851 : 0 : pBNS->tot_st_flow -= 2 * delta;
852 : :
853 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
854 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
855 : :
856 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
857 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 1) /* djb-rwth: addressing LLVM warning */
# # ]
858 : : {
859 : : /* Added (-) charge to =O => nDeltaCharge == 1 */
860 : : /* Flow change on pe (-)charge edge (atom -N(-)-) is not known to RunBnsTestOnce()) */
861 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
862 [ # # ]: 0 : if (ret > 0)
863 : : {
864 : 0 : nNumRunBNS++;
865 : 0 : cur_success++; /* 03 */
866 : : }
867 : : }
868 : : else
869 : : {
870 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
871 : 0 : pe->flow += delta;
872 : 0 : pv1->st_edge.flow += delta;
873 : 0 : pv2->st_edge.flow += delta;
874 : 0 : pBNS->tot_st_flow += 2 * delta;
875 : : }
876 : : INCHI_HEAPCHK
877 : : }
878 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
879 : : }
880 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
881 [ # # ]: 0 : if (cur_success)
882 : : {
883 : 0 : tot_succes += cur_success;
884 : : /* recalculate InChI from the structure */
885 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
886 : : ppt_group_info, ppat_norm, ppat_prep)))
887 : : {
888 : 0 : goto exit_function;
889 : : }
890 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
891 : : {
892 : 0 : goto exit_function;
893 : : }
894 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
895 : : {
896 : 0 : goto exit_function; /* no fixed-H found */
897 : : }
898 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
899 : : {
900 : 0 : goto exit_function;
901 : : }
902 [ # # ]: 0 : if (!pc2i->bHasDifference)
903 : : {
904 : 0 : goto exit_function; /* nothing to do */
905 : : }
906 : : }
907 : : }
908 : :
909 [ # # ]: 0 : if (pc2i->nNumTgRevrs == 1 && /* pc2i->nNumRemHInChI < 0 &&*/
910 [ # # # # ]: 0 : ( pc2i->nNumEndpRevrs > pc2i->nNumEndpInChI || pc2i->nNumTgInChI > 1 ) /* ADP in Revrs */)
911 : : {
912 : : /*--------------------------------------------------------------*/
913 : : /* case 03a:restored: -N(-)-AB=O original: -N=AB-O(-) */
914 : : /* FixH: 0 0 0 0 */
915 : : /* MobH: 0 0 0 0 */
916 : : /* O = O, S, Se; N = N; taut */
917 : : /* restored atoms are tautomeric; original atom is; N may be. */
918 : : /* restored struct has 1 t-group; original has less endpoints */
919 : : /* and possibly >1 t-groups */
920 : : /* Solution: move (-) from N(-) to =O */
921 : : /* these atoms are tautomeric in restored structure */
922 : : /*--------------------------------------------------------------*/
923 : 0 : int num_SB_N_Minus = 0, num_DB_O = 0, iat;
924 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
925 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
926 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
927 [ # # # # ]: 0 : S_CHAR *pnMobHInChI = ( pInChI[1] && pInChI[1]->nNum_H ) ? pInChI[1]->nNum_H :
928 [ # # # # ]: 0 : ( pInChI[0] && pInChI[0]->nNum_H ) ? pInChI[0]->nNum_H : NULL;
929 : 0 : S_CHAR *pnFixHInChI = pStruct->fixed_H;
930 : :
931 : 0 : cur_success = 0;
932 : 0 : CurrEdges.num_edges = 0;
933 : :
934 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
935 : : { /* i = canonical number - 1 */
936 : 0 : iat = nCanon2AtnoRevrs[i];
937 [ # # ]: 0 : if ( /* in restored atom N: charge=-1, no H, has no double bond, endpoint */
938 : 0 : num_SB_N_Minus < MAX_DIFF_FIXH &&
939 [ # # ]: 0 : at2[iat].charge == -1 && /*!at2[iat].num_H &&*/
940 [ # # # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
941 [ # # # # : 0 : pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1 &&
# # ]
942 [ # # ]: 0 : at_Mobile_H_Revrs && at_Mobile_H_Revrs[iat].endpoint &&
943 : : /* in orig.InChI: may be an endpoint, has no H */
944 : : /* has (-) edge */
945 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden)
946 : : {
947 : 0 : iat_SB_N_Minus[num_SB_N_Minus++] = iat;
948 : : }
949 : : else
950 [ # # ]: 0 : if (num_DB_O < MAX_DIFF_FIXH &&
951 [ # # ]: 0 : at2[iat].charge == 0 && /*!at2[iat].num_H &&*/
952 [ # # # # ]: 0 : at2[iat].valence + 1 == at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
953 [ # # # # ]: 0 : pVA[iat].cNumValenceElectrons == 6 &&
954 [ # # ]: 0 : at_Mobile_H_Revrs && at_Mobile_H_Revrs[iat].endpoint && /* endpoint in Reconstructed */
955 [ # # # # ]: 0 : (pStruct->endpoint[i] || /* endpoint or H(+) acceptor in original */
956 [ # # # # : 0 : (pnMobHInChI && pnMobHInChI[i] == 1 && pnFixHInChI && pnFixHInChI[i] == -1)) &&
# # ]
957 : : /* has (-) edge */
958 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden) /* djb-rwth: addressing LLVM warning */
959 : : {
960 : 0 : iat_DB_O[num_DB_O++] = iat;
961 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
962 : : {
963 : 0 : goto exit_function;
964 : : }
965 : : }
966 : : }
967 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_N_Minus, num_DB_O))) /* djb-rwth: addressing LLVM warning */
968 : : {
969 : : /* detected; attempt to fix */
970 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
971 : : /* allow charge transfer to all found =O */
972 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
973 : 0 : delta = 1;
974 [ # # # # ]: 0 : for (i = 0; i < num_SB_N_Minus && cur_success < num_try; i++)
975 : : {
976 : 0 : iat = iat_SB_N_Minus[i];
977 : 0 : pe = pBNS->edge + pVA[iat].nCMinusGroupEdge - 1;
978 [ # # ]: 0 : if (!pe->flow)
979 : 0 : continue;
980 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
981 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
982 : :
983 : 0 : pe->forbidden |= forbidden_edge_mask;
984 : 0 : pe->flow -= delta;
985 : 0 : pv1->st_edge.flow -= delta;
986 : 0 : pv2->st_edge.flow -= delta;
987 : 0 : pBNS->tot_st_flow -= 2 * delta;
988 : :
989 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
990 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
991 : :
992 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
993 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 1) /* djb-rwth: addressing LLVM warnings */
# # ]
994 : : {
995 : : /* Added (-) charge to =O => nDeltaCharge == 1 */
996 : : /* Flow change on pe (-)charge edge (atom -N(-)-) is not known to RunBnsTestOnce()) */
997 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
998 [ # # ]: 0 : if (ret > 0)
999 : : {
1000 : 0 : nNumRunBNS++;
1001 : 0 : cur_success++; /* 03a */
1002 : : }
1003 : : }
1004 : : else
1005 : : {
1006 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
1007 : 0 : pe->flow += delta;
1008 : 0 : pv1->st_edge.flow += delta;
1009 : 0 : pv2->st_edge.flow += delta;
1010 : 0 : pBNS->tot_st_flow += 2 * delta;
1011 : : }
1012 : : INCHI_HEAPCHK
1013 : : }
1014 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
1015 : : }
1016 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
1017 [ # # ]: 0 : if (cur_success)
1018 : : {
1019 : 0 : tot_succes += cur_success;
1020 : : /* recalculate InChI from the structure */
1021 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
1022 : : ppt_group_info, ppat_norm, ppat_prep)))
1023 : : {
1024 : 0 : goto exit_function;
1025 : : }
1026 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
1027 : : {
1028 : 0 : goto exit_function;
1029 : : }
1030 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
1031 : : {
1032 : 0 : goto exit_function; /* no fixed-H found */
1033 : : }
1034 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
1035 : : {
1036 : 0 : goto exit_function;
1037 : : }
1038 [ # # ]: 0 : if (!pc2i->bHasDifference)
1039 : : {
1040 : 0 : goto exit_function; /* nothing to do */
1041 : : }
1042 : : }
1043 : : }
1044 : :
1045 [ # # # # ]: 0 : if (pc2i->len_c2at >= 1 && pc2i->nNumTgInChI == 1 && /* ADP in InChI */
1046 [ # # # # ]: 0 : ( pc2i->nNumEndpRevrs < pc2i->nNumEndpInChI || pc2i->nNumTgRevrs > 1 ))
1047 : : {
1048 : : /*--------------------------------------------------------------*/
1049 : : /* case 04: restored: OH(+)=AB-O- OH- orig. HO-AB=O(+)- OH- */
1050 : : /* FixH: 1 0 0 1 0 1 */
1051 : : /* MobH: 0 0 1 0 0 0 */
1052 : : /* non-taut. taut taut */
1053 : : /* ADP: one t-group or more endpoints */
1054 : : /* O(+) = N, P, As, As, O, S, Se; OH = N, O, S, Se, Te */
1055 : : /* Solution: move (+) from O(+) to NH2 */
1056 : : /*--------------------------------------------------------------*/
1057 : 0 : int num_SB_Neutr = 0, num_DB_Charged = 0, iat;
1058 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
1059 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
1060 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
1061 : 0 : cur_success = 0;
1062 : :
1063 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
1064 : : { /* i = canonical number - 1 */
1065 : 0 : iat = nCanon2AtnoRevrs[i];
1066 [ # # ]: 0 : if ( /* in restored atom: charge=+1, has H, has double bond, N, O, S, Se, Te */
1067 : 0 : num_DB_Charged < MAX_DIFF_FIXH &&
1068 [ # # # # ]: 0 : at2[iat].charge == 1 && at2[iat].num_H &&
1069 [ # # # # ]: 0 : at2[iat].valence < at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
1070 [ # # ]: 0 : (pVA[iat].cNumValenceElectrons == 6 ||
1071 [ # # # # ]: 0 : (pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1)) &&
1072 : : /* in orig.InChI: an endpoint, has fixed-H */
1073 [ # # ]: 0 : pStruct->endpoint[i] &&
1074 [ # # # # ]: 0 : (pStruct->fixed_H && pStruct->fixed_H[i]) &&
1075 : : /*!(nMobHInChI && nMobHInChI[i] ) &&*/
1076 : : /* has (+) edge */
1077 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden) /* djb-rwth: addressing LLVM warning */
1078 : : {
1079 : :
1080 : 0 : iat_DB_Charged[num_DB_Charged++] = iat;
1081 : :
1082 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
1083 : : {
1084 : 0 : goto exit_function;
1085 : : }
1086 : : }
1087 : : else
1088 : : {
1089 [ # # ]: 0 : if ( /* in restored atom: charge=0, has no H, has no double bond, N, P, O, S, Se, Te */
1090 : 0 : num_SB_Neutr < MAX_DIFF_FIXH &&
1091 [ # # # # ]: 0 : at2[iat].charge == 0 && !at2[iat].num_H &&
1092 [ # # # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
1093 [ # # ]: 0 : (pVA[iat].cNumValenceElectrons == 6 ||
1094 [ # # ]: 0 : pVA[iat].cNumValenceElectrons == 5) &&
1095 : : /* in orig.InChI: an endpoint, has fixed-H */
1096 : : /* pStruct->endpoint[i] && */
1097 [ # # # # : 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
1098 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
1099 : : /* has (+) edge */
1100 [ # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 &&
1101 [ # # ]: 0 : 0 == pBNS->edge[e].forbidden)
1102 : : {
1103 : :
1104 : 0 : iat_SB_Neutr[num_SB_Neutr++] = iat;
1105 : :
1106 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
1107 : : {
1108 : 0 : goto exit_function;
1109 : : }
1110 : : }
1111 : : }
1112 : : }
1113 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_Neutr, num_DB_Charged))) /* djb-rwth: addressing LLVM warning */
1114 : : {
1115 : : /* detected; attempt to fix */
1116 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
1117 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
1118 : 0 : delta = 1;
1119 [ # # # # ]: 0 : for (i = 0; i < num_SB_Neutr && cur_success < num_try; i++)
1120 : : {
1121 : 0 : iat = iat_SB_Neutr[i];
1122 : 0 : pe = pBNS->edge + pVA[iat].nCPlusGroupEdge - 1;
1123 [ # # ]: 0 : if (!pe->flow)
1124 : 0 : continue;
1125 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
1126 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
1127 : :
1128 : 0 : pe->forbidden |= forbidden_edge_mask;
1129 : 0 : pe->flow -= delta;
1130 : 0 : pv1->st_edge.flow -= delta;
1131 : 0 : pv2->st_edge.flow -= delta;
1132 : 0 : pBNS->tot_st_flow -= 2 * delta;
1133 : :
1134 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
1135 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
1136 : :
1137 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
1138 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == -1) /* djb-rwth: addressing LLVM warnings */
# # ]
1139 : : {
1140 : : /* Removed charge from O(+) => nDeltaCharge == -1 */
1141 : : /* Flow change on pe (+)charge edge (atom NH2) is not known to RunBnsTestOnce()) */
1142 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
1143 [ # # ]: 0 : if (ret > 0)
1144 : : {
1145 : 0 : nNumRunBNS++;
1146 : 0 : cur_success++; /* 04 */
1147 : : }
1148 : : }
1149 : : else
1150 : : {
1151 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
1152 : 0 : pe->flow += delta;
1153 : 0 : pv1->st_edge.flow += delta;
1154 : 0 : pv2->st_edge.flow += delta;
1155 : 0 : pBNS->tot_st_flow += 2 * delta;
1156 : : }
1157 : : INCHI_HEAPCHK
1158 : : }
1159 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
1160 : : }
1161 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
1162 [ # # ]: 0 : if (cur_success)
1163 : : {
1164 : 0 : tot_succes += cur_success;
1165 : : /* recalculate InChI from the structure */
1166 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
1167 : : ppt_group_info, ppat_norm, ppat_prep)))
1168 : : {
1169 : 0 : goto exit_function;
1170 : : }
1171 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
1172 : : {
1173 : 0 : goto exit_function;
1174 : : }
1175 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
1176 : : {
1177 : 0 : goto exit_function; /* no fixed-H found */
1178 : : }
1179 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
1180 : : {
1181 : 0 : goto exit_function;
1182 : : }
1183 [ # # ]: 0 : if (!pc2i->bHasDifference)
1184 : : {
1185 : 0 : goto exit_function; /* nothing to do */
1186 : : }
1187 : : }
1188 : : }
1189 : :
1190 [ # # ]: 0 : if (pc2i->len_c2at > 1)
1191 : : {
1192 : : /*--------------------------------------------------------------*/
1193 : : /* case 05: restored: O=AB-NH original:(-)O-AB=NH(+) */
1194 : : /* FixH: 0 0 -1 1 */
1195 : : /* MobH: 0 1 1 0 */
1196 : : /* O = O, S, Se; N = N, O, S, Se, Te; all atoms not tautomeric */
1197 : : /* Solution: Separate charges */
1198 : : /*--------------------------------------------------------------*/
1199 : 0 : int num_DB_O = 0, num_SB_NH = 0, iat;
1200 : 0 : cur_success = 0;
1201 : :
1202 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
1203 : : {
1204 : 0 : iat = pc2i->c2at[i].atomNumber;
1205 [ # # ]: 0 : if ( /* orig. InChI info: =NH2(+), =OH(+) */
1206 : 0 : num_SB_NH < MAX_DIFF_FIXH &&
1207 [ # # # # ]: 0 : ((pc2i->c2at[i].nValElectr == 5 && pc2i->c2at[i].nPeriodNum == 1) ||
1208 [ # # ]: 0 : pc2i->c2at[i].nValElectr == 6) /* N, O, S, Se, Te */ &&
1209 [ # # ]: 0 : !pc2i->c2at[i].endptInChI &&
1210 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
1211 [ # # ]: 0 : pc2i->c2at[i].nFixHInChI == 1 && /*pc2i->c2at[i].nMobHInChI == 1 &&*/
1212 : : /* reversed structure info: */
1213 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && pc2i->c2at[i].nMobHRevrs &&
1214 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && at2[iat].num_H &&
1215 [ # # ]: 0 : !pc2i->c2at[i].endptRevrs &&
1216 [ # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence)
1217 : : {
1218 : 0 : iat_SB_NH[num_SB_NH++] = iat;
1219 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
1220 : : {
1221 : 0 : goto exit_function;
1222 : : }
1223 : : }
1224 : : else
1225 : : {
1226 [ # # ]: 0 : if ( /* orig. InChI info: -O(-) */
1227 : 0 : num_DB_O < MAX_DIFF_FIXH &&
1228 [ # # ]: 0 : (pc2i->c2at[i].nValElectr == 6) /* O, S, Se, Te */ &&
1229 [ # # ]: 0 : !pc2i->c2at[i].endptInChI &&
1230 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
1231 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == -1 && pc2i->c2at[i].nMobHInChI == 1 &&
1232 : : /* reversed structure info: */
1233 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && pc2i->c2at[i].nMobHRevrs == 0 &&
1234 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && !at2[iat].num_H &&
1235 [ # # ]: 0 : !pc2i->c2at[i].endptRevrs &&
1236 [ # # ]: 0 : at2[iat].valence + 1 == at2[iat].chem_bonds_valence)
1237 : : {
1238 : 0 : iat_DB_O[num_DB_O++] = iat;
1239 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
1240 : : {
1241 : 0 : goto exit_function;
1242 : : }
1243 : : }
1244 : : }
1245 : : }
1246 [ # # ]: 0 : if ((num_try = inchi_min(num_DB_O, num_SB_NH))) /* djb-rwth: addressing LLVM warning */
1247 : : {
1248 : : /* detected; attempt to fix */
1249 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
1250 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
1251 : 0 : delta = 1;
1252 [ # # # # ]: 0 : for (i = 0; i < num_SB_NH && cur_success < num_try; i++)
1253 : : {
1254 : 0 : iat = iat_SB_NH[i];
1255 : 0 : pe = pBNS->edge + pVA[iat].nCPlusGroupEdge - 1;
1256 [ # # ]: 0 : if (!pe->flow)
1257 : 0 : continue;
1258 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
1259 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
1260 : :
1261 : 0 : pe->forbidden |= forbidden_edge_mask;
1262 : 0 : pe->flow -= delta;
1263 : 0 : pv1->st_edge.flow -= delta;
1264 : 0 : pv2->st_edge.flow -= delta;
1265 : 0 : pBNS->tot_st_flow -= 2 * delta;
1266 : :
1267 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
1268 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
1269 : :
1270 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
1271 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 1) /* djb-rwth: addressing LLVM warnings */
# # ]
1272 : : {
1273 : : /* Added charge to =O => nDeltaCharge == 1 */
1274 : : /* Flow change on pe (+)charge edge (atom NH2) is not known to RunBnsTestOnce()) */
1275 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
1276 [ # # ]: 0 : if (ret > 0)
1277 : : {
1278 : 0 : nNumRunBNS++;
1279 : 0 : cur_success++; /* 05 */
1280 : : }
1281 : : }
1282 : : else
1283 : : {
1284 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
1285 : 0 : pe->flow += delta;
1286 : 0 : pv1->st_edge.flow += delta;
1287 : 0 : pv2->st_edge.flow += delta;
1288 : 0 : pBNS->tot_st_flow += 2 * delta;
1289 : : }
1290 : : INCHI_HEAPCHK
1291 : : }
1292 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
1293 : : }
1294 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
1295 [ # # ]: 0 : if (cur_success)
1296 : : {
1297 : 0 : tot_succes += cur_success;
1298 : : /* recalculate InChI from the structure */
1299 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
1300 : : ppt_group_info, ppat_norm, ppat_prep)))
1301 : : {
1302 : 0 : goto exit_function;
1303 : : }
1304 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
1305 : : {
1306 : 0 : goto exit_function;
1307 : : }
1308 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
1309 : : {
1310 : 0 : goto exit_function; /* no fixed-H found */
1311 : : }
1312 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
1313 : : {
1314 : 0 : goto exit_function;
1315 : : }
1316 [ # # ]: 0 : if (!pc2i->bHasDifference)
1317 : : {
1318 : 0 : goto exit_function; /* nothing to do */
1319 : : }
1320 : : }
1321 : : }
1322 : :
1323 [ # # # # : 0 : if (pStruct->fixed_H && pStruct->endpoint && pc2i->nChargeFixHInChI > 0 && pc2i->nChargeFixHInChI > pc2i->nChargeMobHInChI)
# # # # ]
1324 : : {
1325 : : /*----------------------------------------------------------*/
1326 : : /* case 06c: restored -NH- or -NH(+) orig: -NH- */
1327 : : /* Fixed-H 1 1 0 */
1328 : : /* Mobile-H 0 0 1 */
1329 : : /* not tautomeric not tautomeric */
1330 : : /* has adjacent (+) */
1331 : : /* charges */
1332 : : /* Solution: move (+) charges to the -NH- unless it already*/
1333 : : /* N = N, O, S, Se, Te */
1334 : : /* has (+) charge blocked by adjacent (+) */
1335 : : /*----------------------------------------------------------*/
1336 : : int iat;
1337 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
1338 : : /*
1339 : : inp_ATOM *at_Mobile_H_Revrs = (pStruct->pOne_norm_data[1] &&
1340 : : pStruct->pOne_norm_data[1]->at)? pStruct->pOne_norm_data[1]->at : NULL;
1341 : : inp_ATOM *atfMobile_H_Revrs = pStruct->pOne_norm_data[TAUT_YES] &&
1342 : : pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds?
1343 : : pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds : NULL;
1344 : : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H? pInChI[1]->nNum_H :
1345 : : pInChI[0] && pInChI[0]->nNum_H? pInChI[0]->nNum_H : NULL;
1346 : : */
1347 : : EDGE_LIST CurChargeEdges;
1348 : : EdgeIndex e2;
1349 : 0 : cur_success = 0;
1350 : 0 : AllocEdgeList( &CurChargeEdges, EDGE_LIST_CLEAR );
1351 : 0 : CurrEdges.num_edges = 0;
1352 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
1353 : : {
1354 : : /* atoms -NH- from which H(+) were removed by the Normalization in orig. InChI */
1355 : 0 : iat = pc2i->c2at[i].atomNumber;
1356 [ # # ]: 0 : if (( pc2i->c2at[i].nValElectr == 6 ||
1357 [ # # # # ]: 0 : (pc2i->c2at[i].nValElectr == 5 && pc2i->c2at[i].nPeriodNum == 1) ) &&
1358 [ # # ]: 0 : !pc2i->c2at[i].endptInChI &&
1359 [ # # # # ]: 0 : ( e = pVA[iat].nCPlusGroupEdge - 1 ) >= 0 && !pBNS->edge[e].forbidden) /* djb-rwth: addressing LLVM warning */
1360 : : {
1361 : 0 : if ( /* orig. InChI info: -NH- */
1362 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == 1 && pc2i->c2at[i].nMobHInChI == 0 &&
1363 : : /* reversed structure info: */
1364 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && pc2i->c2at[i].nMobHRevrs == 1 && /* was not removed */
1365 [ # # ]: 0 : /*pc2i->c2at[i].nAtChargeRevrs == 0 &&*/ at2[iat].num_H && /* at2 is Fixed-H */
1366 [ # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence)
1367 : : {
1368 [ # # ]: 0 : if ((ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
1369 : : {
1370 : 0 : goto exit_function;
1371 : : }
1372 : : }
1373 : : }
1374 : : }
1375 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
1376 : : {
1377 : : /* find adjacent charged atoms */
1378 : 0 : iat = nCanon2AtnoRevrs[i];
1379 [ # # # # : 0 : if (pStruct->endpoint[i] || at2[iat].charge != 1 || at2[iat].radical || pVA[iat].cMetal)
# # # # ]
1380 : : {
1381 : 0 : continue;
1382 : : }
1383 [ # # # # : 0 : if (0 <= ( e = pVA[iat].nCPlusGroupEdge - 1 ) && !pBNS->edge[e].forbidden && !pBNS->edge[e].flow && pVA[iat].cNumValenceElectrons >= 5)
# # # # ]
1384 : : {
1385 : : /* positively charged atom */
1386 [ # # ]: 0 : for (j = 0; j < at2[iat].valence; j++)
1387 : : {
1388 [ # # # # ]: 0 : if (at2[k = (int) at2[iat].neighbor[j]].charge == 1 && !pVA[k].cMetal &&
1389 [ # # # # : 0 : 0 <= ( e2 = pVA[k].nCPlusGroupEdge - 1 ) && !pBNS->edge[e2].forbidden && !pBNS->edge[e2].flow)
# # ]
1390 : : {
1391 [ # # # # ]: 0 : if (0 > FindInEdgeList( &CurrEdges, e ) &&
1392 [ # # ]: 0 : 0 > FindInEdgeList( &CurChargeEdges, e ) &&
1393 : 0 : ( ret = AddToEdgeList( &CurChargeEdges, e, INC_ADD_EDGE ) ))
1394 : : {
1395 : 0 : goto exit_case_06c;
1396 : : }
1397 [ # # # # ]: 0 : if (0 > FindInEdgeList( &CurrEdges, e2 ) &&
1398 [ # # ]: 0 : 0 > FindInEdgeList( &CurChargeEdges, e2 ) &&
1399 : 0 : ( ret = AddToEdgeList( &CurChargeEdges, e2, INC_ADD_EDGE ) ))
1400 : : {
1401 : 0 : goto exit_case_06c;
1402 : : }
1403 : : }
1404 : : }
1405 : : }
1406 : : }
1407 [ # # ]: 0 : if ((num_try = inchi_min( CurrEdges.num_edges, CurChargeEdges.num_edges ))) /* djb-rwth: addressing LLVM warning */
1408 : : {
1409 : : /* detected; attempt to fix */
1410 : 0 : SetForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
1411 : 0 : RemoveForbiddenEdgeMask( pBNS, &CurChargeEdges, forbidden_edge_mask );
1412 : 0 : delta = 1;
1413 [ # # # # ]: 0 : for (i = 0; i < CurrEdges.num_edges && cur_success < num_try; i++)
1414 : : {
1415 : 0 : e = CurrEdges.pnEdges[i];
1416 : 0 : pe = pBNS->edge + e; /* (+)charge edge of -NH- or -OH */
1417 [ # # ]: 0 : if (!pe->flow)
1418 : 0 : continue;
1419 : 0 : pv1 = pBNS->vert + ( v1 = pe->neighbor1 );
1420 : 0 : pv2 = pBNS->vert + ( v2 = pe->neighbor12 ^ v1 );
1421 : :
1422 : 0 : pe->flow -= delta; /* add (+) to -NHm */
1423 : 0 : pv1->st_edge.flow -= delta;
1424 : 0 : pv2->st_edge.flow -= delta;
1425 : 0 : pBNS->tot_st_flow -= 2 * delta;
1426 : :
1427 : 0 : ret = RunBnsTestOnce( pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
1428 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms );
1429 : :
1430 [ # # # # : 0 : if (ret == 1 && ( (vPathEnd == v1 && vPathStart == v2) ||
# # ]
1431 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1) ) && nDeltaCharge == -1) /* djb-rwth: addressing LLVM warning */
# # ]
1432 : : {
1433 : : /* Removed (+)charge from -NH- => nDeltaCharge == -1 */
1434 : : /* Flow change on pe (+)charge edge (atom NHm(+)) is not known to RunBnsTestOnce()) */
1435 : 0 : ret = RunBnsRestoreOnce( pBNS, pBD, pVA, pTCGroups );
1436 [ # # ]: 0 : if (ret > 0)
1437 : : {
1438 : 0 : nNumRunBNS++;
1439 : 0 : cur_success++; /* 06c */
1440 : : }
1441 : : }
1442 : : else
1443 : : {
1444 : 0 : pe->flow += delta;
1445 : 0 : pv1->st_edge.flow += delta;
1446 : 0 : pv2->st_edge.flow += delta;
1447 : 0 : pBNS->tot_st_flow += 2 * delta;
1448 : : }
1449 : : INCHI_HEAPCHK
1450 : : }
1451 : 0 : RemoveForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
1452 : : }
1453 : 0 : exit_case_06c:
1454 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
1455 : 0 : AllocEdgeList( &CurChargeEdges, EDGE_LIST_FREE );
1456 [ # # ]: 0 : if (ret < 0)
1457 : : {
1458 : 0 : goto exit_function;
1459 : : }
1460 [ # # ]: 0 : if (cur_success)
1461 : : {
1462 : 0 : tot_succes += cur_success;
1463 : : /* recalculate InChI from the structure */
1464 [ # # ]: 0 : if (0 >( ret = MakeOneInChIOutOfStrFromINChI2( pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
1465 : : ppt_group_info, ppat_norm, ppat_prep ) ))
1466 : : {
1467 : 0 : goto exit_function;
1468 : : }
1469 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr( pStruct ))) /* djb-rwth: addressing LLVM warning */
1470 : : {
1471 : 0 : goto exit_function;
1472 : : }
1473 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
1474 : : {
1475 : 0 : goto exit_function; /* no fixed-H found */
1476 : : }
1477 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI( pStruct, at2, pVA, pInChI, pc2i ))) /* djb-rwth: addressing LLVM warning */
1478 : : {
1479 : 0 : goto exit_function;
1480 : : }
1481 [ # # ]: 0 : if (!pc2i->bHasDifference)
1482 : : {
1483 : 0 : goto exit_function; /* nothing to do */
1484 : : }
1485 : : }
1486 : : }
1487 : :
1488 [ # # ]: 0 : if (pc2i->len_c2at >= 2)
1489 : : {
1490 : : /*------------------------------------------------------------*/
1491 : : /* case 06d: restored: XH(+)=-AB-NH orig.: XH-=AB=NH(+) */
1492 : : /* FixH: 1 1 0 0 1 1 */
1493 : : /* MobH: 0 taut 1 1 taut 0 */
1494 : : /* */
1495 : : /* */
1496 : : /* N = N, O, S, Se; atoms N are not tautomeric in orig InChI */
1497 : : /* X = N, O, S, Se, Te, F, Cl, Br, I; atom X is non-taut */
1498 : : /* Solution: move (+) from X to NH */
1499 : : /*------------------------------------------------------------*/
1500 : : int iat;
1501 : : /*
1502 : : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
1503 : : inp_ATOM *at_Mobile_H_Revrs = (pStruct->pOne_norm_data[1] &&
1504 : : pStruct->pOne_norm_data[1]->at)? pStruct->pOne_norm_data[1]->at : NULL;
1505 : : inp_ATOM *atfMobile_H_Revrs = pStruct->pOne_norm_data[TAUT_YES] &&
1506 : : pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds?
1507 : : pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds :
1508 : : pStruct->pOne_norm_data[TAUT_NON]->at;
1509 : : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H? pInChI[1]->nNum_H :
1510 : : pInChI[0] && pInChI[0]->nNum_H? pInChI[0]->nNum_H : 0;
1511 : : */
1512 : : EDGE_LIST CurChargeEdges;
1513 : 0 : cur_success = 0;
1514 : 0 : AllocEdgeList( &CurChargeEdges, EDGE_LIST_CLEAR );
1515 : 0 : CurrEdges.num_edges = 0;
1516 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
1517 : : {
1518 : 0 : iat = pc2i->c2at[i].atomNumber;
1519 : : /* XH(+) */
1520 : 0 : if ( /* reconstructed: non-taut and (+) */
1521 [ # # ]: 0 : ( pc2i->c2at[i].nMobHRevrs + 1 == pc2i->c2at[i].nFixHRevrs &&
1522 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs > 0 && !pc2i->c2at[i].endptRevrs &&
1523 [ # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 1 &&
1524 : : /* original InChI: non-taut & has H or an endpoint, has Fixed H */
1525 [ # # # # ]: 0 : ( (!pc2i->c2at[i].nFixHInChI && pc2i->c2at[i].nMobHInChI == pc2i->c2at[i].nFixHRevrs) ||
1526 [ # # # # ]: 0 : (pc2i->c2at[i].nFixHInChI == pc2i->c2at[i].nFixHRevrs && pc2i->c2at[i].endptInChI) ) ) &&
1527 [ # # # # : 0 : 0 <= ( e = pVA[iat].nCPlusGroupEdge - 1 ) && !pBNS->edge[e].forbidden && !pBNS->edge[e].flow) /* djb-rwth: addressing LLVM warnings */
# # ]
1528 : : {
1529 : :
1530 [ # # ]: 0 : if ((ret = AddToEdgeList( &CurChargeEdges, e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
1531 : : {
1532 : 0 : goto exit_case_06d;
1533 : : }
1534 : : }
1535 : : else
1536 : : {
1537 : : /* -NH- */
1538 : 0 : if ( /* original InChI: has H and is not an endpoint */
1539 [ # # ]: 0 : ( pc2i->c2at[i].nMobHInChI + 1 == pc2i->c2at[i].nFixHInChI &&
1540 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI > 0 && !pc2i->c2at[i].endptInChI &&
1541 [ # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 &&
1542 : : /* reconstructed InChI: non-taut & has H or an endpoint, has Fixed H */
1543 [ # # # # ]: 0 : ( (!pc2i->c2at[i].nFixHRevrs && pc2i->c2at[i].nMobHRevrs == pc2i->c2at[i].nFixHInChI) ||
1544 [ # # # # ]: 0 : (pc2i->c2at[i].nFixHRevrs == pc2i->c2at[i].nFixHInChI && pc2i->c2at[i].endptRevrs) ) ) &&
1545 [ # # # # ]: 0 : 0 <= ( e = pVA[iat].nCPlusGroupEdge - 1 ) && !pBNS->edge[e].forbidden &&
1546 [ # # ]: 0 : pBNS->edge[e].flow) /* djb-rwth: addressing LLVM warnings */
1547 : : {
1548 : :
1549 [ # # ]: 0 : if ((ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
1550 : : {
1551 : 0 : goto exit_case_06d;
1552 : : }
1553 : : }
1554 : : }
1555 : : }
1556 [ # # ]: 0 : if ((num_try = inchi_min( CurrEdges.num_edges, CurChargeEdges.num_edges ))) /* djb-rwth: addressing LLVM warning */
1557 : : {
1558 : : /* detected; attempt to fix */
1559 : 0 : int bSFlowerEdgesMayBeForbidden = ( SFlowerEdges.num_edges > 0 );
1560 : : int bSFlowerEdgesIsForbidden;
1561 : 0 : for (bSFlowerEdgesIsForbidden = bSFlowerEdgesMayBeForbidden;
1562 [ # # ]: 0 : 0 <= bSFlowerEdgesIsForbidden; bSFlowerEdgesIsForbidden--)
1563 : : {
1564 [ # # ]: 0 : if (bSFlowerEdgesIsForbidden)
1565 : : {
1566 : : /* on the 1st pass disallow -S(+)= => =S=, allow only -S(+)= => -S- */
1567 : 0 : SetForbiddenEdgeMask( pBNS, &SFlowerEdges, forbidden_edge_mask );
1568 : : }
1569 : 0 : SetForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
1570 : 0 : RemoveForbiddenEdgeMask( pBNS, &CurChargeEdges, forbidden_edge_mask );
1571 : 0 : delta = 1;
1572 [ # # # # ]: 0 : for (i = 0; i < CurrEdges.num_edges && cur_success < num_try; i++)
1573 : : {
1574 : 0 : e = CurrEdges.pnEdges[i];
1575 : 0 : pe = pBNS->edge + e; /* (+)charge edge of -NH- or -OH */
1576 [ # # ]: 0 : if (!pe->flow)
1577 : 0 : continue;
1578 : 0 : pv1 = pBNS->vert + ( v1 = pe->neighbor1 );
1579 : 0 : pv2 = pBNS->vert + ( v2 = pe->neighbor12 ^ v1 );
1580 : :
1581 : 0 : pe->flow -= delta; /* add (+) to -NHm */
1582 : 0 : pv1->st_edge.flow -= delta;
1583 : 0 : pv2->st_edge.flow -= delta;
1584 : 0 : pBNS->tot_st_flow -= 2 * delta;
1585 : :
1586 : 0 : ret = RunBnsTestOnce( pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
1587 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms );
1588 : :
1589 [ # # # # : 0 : if (ret == 1 && ( (vPathEnd == v1 && vPathStart == v2) ||
# # ]
1590 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1) ) && nDeltaCharge == -1) /* djb-rwth: addressing LLVM warnings */
# # ]
1591 : : {
1592 : : /* Removed (+)charge from -NH- => nDeltaCharge == -1 */
1593 : : /* Flow change on pe (+)charge edge (atom NHm(+)) is not known to RunBnsTestOnce()) */
1594 : 0 : ret = RunBnsRestoreOnce( pBNS, pBD, pVA, pTCGroups );
1595 [ # # ]: 0 : if (ret > 0)
1596 : : {
1597 : 0 : nNumRunBNS++;
1598 : 0 : cur_success++; /* 06d */
1599 : : }
1600 : : }
1601 : : else
1602 : : {
1603 : 0 : pe->flow += delta;
1604 : 0 : pv1->st_edge.flow += delta;
1605 : 0 : pv2->st_edge.flow += delta;
1606 : 0 : pBNS->tot_st_flow += 2 * delta;
1607 : : }
1608 : : INCHI_HEAPCHK
1609 : : }
1610 : 0 : RemoveForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
1611 : 0 : RemoveForbiddenEdgeMask( pBNS, &SFlowerEdges, forbidden_edge_mask );
1612 : : }
1613 : : }
1614 : 0 : exit_case_06d:
1615 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
1616 : 0 : AllocEdgeList( &CurChargeEdges, EDGE_LIST_FREE );
1617 [ # # ]: 0 : if (cur_success)
1618 : : {
1619 : 0 : tot_succes += cur_success;
1620 : : /* recalculate InChI from the structure */
1621 [ # # ]: 0 : if (0 > ( ret = MakeOneInChIOutOfStrFromINChI2( pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
1622 : : ppt_group_info, ppat_norm, ppat_prep ) ))
1623 : : {
1624 : 0 : goto exit_function;
1625 : : }
1626 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr( pStruct ))) /* djb-rwth: addressing LLVM warning */
1627 : : {
1628 : 0 : goto exit_function;
1629 : : }
1630 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
1631 : : {
1632 : 0 : goto exit_function; /* no fixed-H found */
1633 : : }
1634 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI( pStruct, at2, pVA, pInChI, pc2i ))) /* djb-rwth: addressing LLVM warning */
1635 : : {
1636 : 0 : goto exit_function;
1637 : : }
1638 [ # # ]: 0 : if (!pc2i->bHasDifference)
1639 : : {
1640 : 0 : goto exit_function; /* nothing to do */
1641 : : }
1642 : : }
1643 : : }
1644 : :
1645 [ # # ]: 0 : if (pc2i->len_c2at >= 2)
1646 : : {
1647 : : /*--------------------------------------------------------*/
1648 : : /* case 06: restored: NHn(+)=AB-NHm orig.: NHn-AB=NHm(+) */
1649 : : /* FixH: 1 0 0 1 */
1650 : : /* MobH: n-1 m n m-1 */
1651 : : /* N = N, O, S, Se; atoms N are not tautomeric */
1652 : : /* Solution: move (+) from NHn(+) to NHn */
1653 : : /*--------------------------------------------------------*/
1654 : 0 : int num_DB_NHn_Plus = 0, num_SB_NHm_Neutr = 0, iat;
1655 : 0 : cur_success = 0;
1656 : :
1657 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
1658 : : {
1659 : 0 : iat = pc2i->c2at[i].atomNumber;
1660 [ # # ]: 0 : if ((pc2i->c2at[i].nValElectr == 6 ||
1661 [ # # # # ]: 0 : (pc2i->c2at[i].nValElectr == 5 && pc2i->c2at[i].nPeriodNum == 1)) &&
1662 [ # # ]: 0 : !pc2i->c2at[i].endptInChI &&
1663 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden) /* djb-rwth: addressing LLVM warning */
1664 : : {
1665 [ # # ]: 0 : if ( /* orig. InChI info: NHm */
1666 : 0 : num_SB_NHm_Neutr < MAX_DIFF_FIXH &&
1667 [ # # ]: 0 : pc2i->c2at[i].nFixHInChI == 1 && /*pc2i->c2at[i].nMobHInChI == 0 &&*/
1668 : : /* reversed structure info: */
1669 [ # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && /*pc2i->c2at[i].nMobHRevrs == 1 &&*/
1670 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && at2[iat].num_H && /* at2 is Fixed-H */
1671 [ # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence)
1672 : : {
1673 : 0 : iat_SB_NHm_Neutr[num_SB_NHm_Neutr++] = iat;
1674 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
1675 : : {
1676 : 0 : goto exit_function;
1677 : : }
1678 : : }
1679 : : else
1680 : : {
1681 [ # # ]: 0 : if ( /* orig. InChI info: */
1682 : 0 : num_DB_NHn_Plus < MAX_DIFF_FIXH &&
1683 [ # # ]: 0 : pc2i->c2at[i].nFixHInChI == 0 && /*pc2i->c2at[i].nMobHInChI &&*/
1684 : : /* reversed structure info: */
1685 [ # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 1 && /*pc2i->c2at[i].nMobHRevrs == 0 &&*/
1686 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 1 && at2[iat].num_H &&
1687 [ # # ]: 0 : at2[iat].valence < at2[iat].chem_bonds_valence)
1688 : : {
1689 : 0 : iat_DB_NHn_Plus[num_DB_NHn_Plus++] = iat;
1690 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
1691 : : {
1692 : 0 : goto exit_function;
1693 : : }
1694 : : }
1695 : : }
1696 : : }
1697 : : }
1698 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_NHm_Neutr, num_DB_NHn_Plus))) /* djb-rwth: addressing LLVM warning */
1699 : : {
1700 : : /* detected; attempt to fix */
1701 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
1702 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
1703 : 0 : delta = 1;
1704 [ # # # # ]: 0 : for (i = 0; i < num_SB_NHm_Neutr && cur_success < num_try; i++)
1705 : : {
1706 : 0 : iat = iat_SB_NHm_Neutr[i];
1707 : 0 : pe = pBNS->edge + pVA[iat].nCPlusGroupEdge - 1;
1708 [ # # ]: 0 : if (!pe->flow)
1709 : 0 : continue;
1710 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
1711 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
1712 : :
1713 : 0 : pe->forbidden |= forbidden_edge_mask;
1714 : 0 : pe->flow -= delta; /* add (+) to -NHm */
1715 : 0 : pv1->st_edge.flow -= delta;
1716 : 0 : pv2->st_edge.flow -= delta;
1717 : 0 : pBNS->tot_st_flow -= 2 * delta;
1718 : :
1719 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
1720 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
1721 : :
1722 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
1723 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == -1) /* djb-rwth: addressing LLVM warnings */
# # ]
1724 : : {
1725 : : /* Removed (+)charge from -NHn => nDeltaCharge == -1 */
1726 : : /* Flow change on pe (+)charge edge (atom NHm(+)) is not known to RunBnsTestOnce()) */
1727 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
1728 [ # # ]: 0 : if (ret > 0)
1729 : : {
1730 : 0 : nNumRunBNS++;
1731 : 0 : cur_success++; /* 06 */
1732 : : }
1733 : : }
1734 : : else
1735 : : {
1736 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
1737 : 0 : pe->flow += delta;
1738 : 0 : pv1->st_edge.flow += delta;
1739 : 0 : pv2->st_edge.flow += delta;
1740 : 0 : pBNS->tot_st_flow += 2 * delta;
1741 : : }
1742 : : INCHI_HEAPCHK
1743 : : }
1744 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
1745 : : }
1746 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
1747 [ # # ]: 0 : if (cur_success)
1748 : : {
1749 : 0 : tot_succes += cur_success;
1750 : : /* recalculate InChI from the structure */
1751 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
1752 : : ppt_group_info, ppat_norm, ppat_prep)))
1753 : : {
1754 : 0 : goto exit_function;
1755 : : }
1756 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
1757 : : {
1758 : 0 : goto exit_function;
1759 : : }
1760 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
1761 : : {
1762 : 0 : goto exit_function; /* no fixed-H found */
1763 : : }
1764 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
1765 : : {
1766 : 0 : goto exit_function;
1767 : : }
1768 [ # # ]: 0 : if (!pc2i->bHasDifference)
1769 : : {
1770 : 0 : goto exit_function; /* nothing to do */
1771 : : }
1772 : : }
1773 : : }
1774 : :
1775 [ # # # # ]: 0 : if (( (pc2i->nNumTgInChI > pc2i->nNumTgRevrs && pc2i->nNumTgRevrs == 1) ||
1776 [ # # ]: 0 : pc2i->nNumEndpInChI < pc2i->nNumEndpRevrs ) &&
1777 [ # # ]: 0 : pStruct->nNumRemovedProtonsMobHInChI == pStruct->One_ti.tni.nNumRemovedProtons &&
1778 [ # # # # : 0 : pStruct->fixed_H && pStruct->endpoint && pStruct->pOne_norm_data[TAUT_YES] && pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds) /* djb-rwth: addressing LLVM warning; fixing a NULL pointer dereference */
# # # # ]
1779 : : {
1780 : : /*----------------------------------------------------------*/
1781 : : /* case 06a: restored: N'(+)=-AB-NH orig.: N'-=AB=NH(+) */
1782 : : /* FixH: 0 1 0 1 */
1783 : : /* MobH: 0 0 0 0 */
1784 : : /* single t-group multiple t-groups */
1785 : : /* N = N, O, S, Se; atoms N are not tautomeric */
1786 : : /* N' = N atom N' is not tautomeric */
1787 : : /* Solution: move (+) from N' to NH */
1788 : : /*----------------------------------------------------------*/
1789 : : int iat;
1790 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
1791 : : /*
1792 : : inp_ATOM *at_Mobile_H_Revrs = (pStruct->pOne_norm_data[1] &&
1793 : : pStruct->pOne_norm_data[1]->at)? pStruct->pOne_norm_data[1]->at : NULL;
1794 : : */
1795 : 0 : inp_ATOM *atfMobile_H_Revrs = pStruct->pOne_norm_data[TAUT_YES] &&
1796 [ # # ]: 0 : pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds ?
1797 [ # # ]: 0 : pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds : NULL;
1798 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
1799 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
1800 : : EDGE_LIST CurChargeEdges;
1801 : 0 : cur_success = 0;
1802 : 0 : AllocEdgeList( &CurChargeEdges, EDGE_LIST_CLEAR );
1803 : 0 : CurrEdges.num_edges = 0;
1804 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
1805 : : {
1806 : 0 : iat = nCanon2AtnoRevrs[i];
1807 [ # # ]: 0 : if (pStruct->endpoint[i])
1808 : : {
1809 : 0 : continue;
1810 : : }
1811 : : /* -NH-, -OH */
1812 [ # # # # : 0 : if (pStruct->fixed_H[i] && nMobHInChI && !nMobHInChI[i] &&
# # ]
1813 [ # # # # ]: 0 : at2[iat].charge == 0 && at2[iat].radical == 0 &&
1814 [ # # # # : 0 : 0 <= ( e = pVA[iat].nCPlusGroupEdge - 1 ) && !pBNS->edge[e].forbidden && pBNS->edge[e].flow &&
# # # # ]
1815 : 0 : ( ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ) )) /* djb-rwth: fixing a NULL pointer dereference */
1816 : : {
1817 : 0 : goto exit_case_06a;
1818 : : }
1819 : : else
1820 : : {
1821 : : /* >N(+)= */
1822 [ # # # # ]: 0 : if (at2[iat].charge == 1 && !at2[iat].num_H &&
1823 [ # # # # : 0 : pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1 &&
# # ]
1824 [ # # ]: 0 : atfMobile_H_Revrs && atfMobile_H_Revrs[iat].charge == 0 &&
1825 [ # # # # : 0 : 0 <= ( e = pVA[iat].nCPlusGroupEdge - 1 ) && !pBNS->edge[e].forbidden && !pBNS->edge[e].flow &&
# # # # ]
1826 : 0 : ( ret = AddToEdgeList( &CurChargeEdges, e, INC_ADD_EDGE ) ))
1827 : : {
1828 : 0 : goto exit_case_06a;
1829 : : }
1830 : : }
1831 : : }
1832 [ # # ]: 0 : if ((num_try = inchi_min( CurrEdges.num_edges, CurChargeEdges.num_edges ))) /* djb-rwth: addressing LLVM warning */
1833 : : {
1834 : : /* detected; attempt to fix */
1835 : 0 : SetForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
1836 : 0 : RemoveForbiddenEdgeMask( pBNS, &CurChargeEdges, forbidden_edge_mask );
1837 : 0 : delta = 1;
1838 [ # # # # ]: 0 : for (i = 0; i < CurrEdges.num_edges && cur_success < num_try; i++)
1839 : : {
1840 : 0 : e = CurrEdges.pnEdges[i];
1841 : 0 : pe = pBNS->edge + e; /* (+)charge edge of -NH- or -OH */
1842 [ # # ]: 0 : if (!pe->flow)
1843 : 0 : continue;
1844 : 0 : pv1 = pBNS->vert + ( v1 = pe->neighbor1 );
1845 : 0 : pv2 = pBNS->vert + ( v2 = pe->neighbor12 ^ v1 );
1846 : :
1847 : 0 : pe->flow -= delta; /* add (+) to -NHm */
1848 : 0 : pv1->st_edge.flow -= delta;
1849 : 0 : pv2->st_edge.flow -= delta;
1850 : 0 : pBNS->tot_st_flow -= 2 * delta;
1851 : :
1852 : 0 : ret = RunBnsTestOnce( pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
1853 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms );
1854 : :
1855 [ # # # # : 0 : if (ret == 1 && ( (vPathEnd == v1 && vPathStart == v2) ||
# # ]
1856 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1) ) && nDeltaCharge == -1) /* djb-rwth: addressing LLVM warnings */
# # ]
1857 : : {
1858 : : /* Removed (+)charge from -NH- => nDeltaCharge == -1 */
1859 : : /* Flow change on pe (+)charge edge (atom NHm(+)) is not known to RunBnsTestOnce()) */
1860 : 0 : ret = RunBnsRestoreOnce( pBNS, pBD, pVA, pTCGroups );
1861 [ # # ]: 0 : if (ret > 0)
1862 : : {
1863 : 0 : nNumRunBNS++;
1864 : 0 : cur_success++; /* 06a */
1865 : : }
1866 : : }
1867 : : else
1868 : : {
1869 : 0 : pe->flow += delta;
1870 : 0 : pv1->st_edge.flow += delta;
1871 : 0 : pv2->st_edge.flow += delta;
1872 : 0 : pBNS->tot_st_flow += 2 * delta;
1873 : : }
1874 : : INCHI_HEAPCHK
1875 : : }
1876 : 0 : RemoveForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
1877 : : }
1878 : 0 : exit_case_06a:
1879 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
1880 : 0 : AllocEdgeList( &CurChargeEdges, EDGE_LIST_FREE );
1881 [ # # ]: 0 : if (cur_success)
1882 : : {
1883 : 0 : tot_succes += cur_success;
1884 : : /* recalculate InChI from the structure */
1885 [ # # ]: 0 : if (0 > ( ret = MakeOneInChIOutOfStrFromINChI2( pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
1886 : : ppt_group_info, ppat_norm, ppat_prep ) ))
1887 : : {
1888 : 0 : goto exit_function;
1889 : : }
1890 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr( pStruct ))) /* djb-rwth: addressing LLVM warning */
1891 : : {
1892 : 0 : goto exit_function;
1893 : : }
1894 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
1895 : : {
1896 : 0 : goto exit_function; /* no fixed-H found */
1897 : : }
1898 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI( pStruct, at2, pVA, pInChI, pc2i ))) /* djb-rwth: addressing LLVM warning */
1899 : : {
1900 : 0 : goto exit_function;
1901 : : }
1902 [ # # ]: 0 : if (!pc2i->bHasDifference)
1903 : : {
1904 : 0 : goto exit_function; /* nothing to do */
1905 : : }
1906 : : }
1907 : : }
1908 [ # # # # ]: 0 : if (( (pc2i->nNumTgInChI > pc2i->nNumTgRevrs && pc2i->nNumTgRevrs == 1) ||
1909 [ # # ]: 0 : pc2i->nNumEndpInChI < pc2i->nNumEndpRevrs ) &&
1910 [ # # ]: 0 : ( pStruct->nNumRemovedProtonsMobHInChI == pStruct->One_ti.tni.nNumRemovedProtons ||
1911 [ # # ]: 0 : pStruct->nNumRemovedProtonsMobHInChI > pStruct->One_ti.tni.nNumRemovedProtons ) &&
1912 [ # # # # : 0 : pStruct->fixed_H && pStruct->endpoint && pStruct->pOne_norm_data[TAUT_YES] && pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds) /* djb-rwth: addressing LLVM warning; fixing a NULL pointer dereference */
# # # # ]
1913 : : {
1914 : : /*----------------------------------------------------------*/
1915 : : /* case 06b: restored: X(+)=-AB-NH orig.: X-=AB=NH(+) */
1916 : : /* FixH: 0 1 1 0 1 */
1917 : : /* MobH: 0 0 t 0 0 */
1918 : : /* single t-group multiple t-groups */
1919 : : /* or no t-groupd */
1920 : : /* N = N, O, S, Se; atoms N are not tautomeric */
1921 : : /* X = O, S, Se, Te, F, Cl, Br, I; atom X is not tautomeric*/
1922 : : /* Solution: move (+) from X to NH */
1923 : : /*----------------------------------------------------------*/
1924 : : int iat;
1925 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
1926 : : /*
1927 : : inp_ATOM *at_Mobile_H_Revrs = (pStruct->pOne_norm_data[1] &&
1928 : : pStruct->pOne_norm_data[1]->at)? pStruct->pOne_norm_data[1]->at : NULL;
1929 : : */
1930 : 0 : inp_ATOM *atfMobile_H_Revrs = pStruct->pOne_norm_data[TAUT_YES] &&
1931 [ # # ]: 0 : pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds ?
1932 [ # # ]: 0 : pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds : NULL;
1933 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
1934 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
1935 : : EDGE_LIST CurChargeEdges;
1936 : 0 : cur_success = 0;
1937 : 0 : AllocEdgeList( &CurChargeEdges, EDGE_LIST_CLEAR );
1938 : 0 : CurrEdges.num_edges = 0;
1939 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
1940 : : {
1941 : 0 : iat = nCanon2AtnoRevrs[i];
1942 [ # # ]: 0 : if (pStruct->endpoint[i])
1943 : : {
1944 : 0 : continue;
1945 : : }
1946 : : /* -NH-, -OH */
1947 [ # # # # : 0 : if (pStruct->fixed_H[i] && nMobHInChI && !nMobHInChI[i] &&
# # ]
1948 [ # # # # ]: 0 : at2[iat].charge == 0 && at2[iat].radical == 0 &&
1949 [ # # # # : 0 : 0 <= ( e = pVA[iat].nCPlusGroupEdge - 1 ) && !pBNS->edge[e].forbidden && pBNS->edge[e].flow &&
# # # # ]
1950 : 0 : ( ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ) )) /* djb-rwth: fixing a NULL pointer dereference */
1951 : : {
1952 : 0 : goto exit_case_06b;
1953 : : }
1954 : : else
1955 : : {
1956 : : /* X(+)= */
1957 [ # # # # ]: 0 : if (at2[iat].charge == 1 && !at2[iat].num_H &&
1958 [ # # # # : 0 : ( pVA[iat].cNumValenceElectrons == 6 || pVA[iat].cPeriodicRowNumber == 7 ) &&
# # ]
1959 [ # # ]: 0 : atfMobile_H_Revrs && atfMobile_H_Revrs[iat].charge == 1 &&
1960 [ # # # # : 0 : 0 <= ( e = pVA[iat].nCPlusGroupEdge - 1 ) && !pBNS->edge[e].forbidden && !pBNS->edge[e].flow &&
# # # # ]
1961 : 0 : ( ret = AddToEdgeList( &CurChargeEdges, e, INC_ADD_EDGE ) ))
1962 : : {
1963 : 0 : goto exit_case_06b;
1964 : : }
1965 : : }
1966 : : }
1967 [ # # ]: 0 : if ((num_try = inchi_min( CurrEdges.num_edges, CurChargeEdges.num_edges ))) /* djb-rwth: addressing LLVM warning */
1968 : : {
1969 : : /* detected; attempt to fix */
1970 : 0 : int bSFlowerEdgesMayBeForbidden = ( SFlowerEdges.num_edges > 0 );
1971 : : int bSFlowerEdgesIsForbidden;
1972 : 0 : for (bSFlowerEdgesIsForbidden = bSFlowerEdgesMayBeForbidden;
1973 [ # # ]: 0 : 0 <= bSFlowerEdgesIsForbidden; bSFlowerEdgesIsForbidden--)
1974 : : {
1975 [ # # ]: 0 : if (bSFlowerEdgesIsForbidden)
1976 : : {
1977 : : /* on the 1st pass disallow -S(+)= => =S=, allow only -S(+)= => -S- */
1978 : 0 : SetForbiddenEdgeMask( pBNS, &SFlowerEdges, forbidden_edge_mask );
1979 : : }
1980 : 0 : SetForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
1981 : 0 : RemoveForbiddenEdgeMask( pBNS, &CurChargeEdges, forbidden_edge_mask );
1982 : 0 : delta = 1;
1983 [ # # # # ]: 0 : for (i = 0; i < CurrEdges.num_edges && cur_success < num_try; i++)
1984 : : {
1985 : 0 : e = CurrEdges.pnEdges[i];
1986 : 0 : pe = pBNS->edge + e; /* (+)charge edge of -NH- or -OH */
1987 [ # # ]: 0 : if (!pe->flow)
1988 : 0 : continue;
1989 : 0 : pv1 = pBNS->vert + ( v1 = pe->neighbor1 );
1990 : 0 : pv2 = pBNS->vert + ( v2 = pe->neighbor12 ^ v1 );
1991 : :
1992 : 0 : pe->flow -= delta; /* add (+) to -NHm */
1993 : 0 : pv1->st_edge.flow -= delta;
1994 : 0 : pv2->st_edge.flow -= delta;
1995 : 0 : pBNS->tot_st_flow -= 2 * delta;
1996 : :
1997 : 0 : ret = RunBnsTestOnce( pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
1998 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms );
1999 : :
2000 [ # # # # : 0 : if (ret == 1 && ( (vPathEnd == v1 && vPathStart == v2) ||
# # ]
2001 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1) ) && nDeltaCharge == -1) /* djb-rwth: addressing LLVM warnings */
# # ]
2002 : : {
2003 : : /* Removed (+)charge from -NH- => nDeltaCharge == -1 */
2004 : : /* Flow change on pe (+)charge edge (atom NHm(+)) is not known to RunBnsTestOnce()) */
2005 : 0 : ret = RunBnsRestoreOnce( pBNS, pBD, pVA, pTCGroups );
2006 [ # # ]: 0 : if (ret > 0)
2007 : : {
2008 : 0 : nNumRunBNS++;
2009 : 0 : cur_success++; /* 06b */
2010 : : }
2011 : : }
2012 : : else
2013 : : {
2014 : 0 : pe->flow += delta;
2015 : 0 : pv1->st_edge.flow += delta;
2016 : 0 : pv2->st_edge.flow += delta;
2017 : 0 : pBNS->tot_st_flow += 2 * delta;
2018 : : }
2019 : : INCHI_HEAPCHK
2020 : : }
2021 : 0 : RemoveForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
2022 : 0 : RemoveForbiddenEdgeMask( pBNS, &SFlowerEdges, forbidden_edge_mask );
2023 : : }
2024 : : }
2025 : 0 : exit_case_06b:
2026 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
2027 : 0 : AllocEdgeList( &CurChargeEdges, EDGE_LIST_FREE );
2028 [ # # ]: 0 : if (cur_success)
2029 : : {
2030 : 0 : tot_succes += cur_success;
2031 : : /* recalculate InChI from the structure */
2032 [ # # ]: 0 : if (0 > ( ret = MakeOneInChIOutOfStrFromINChI2( pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
2033 : : ppt_group_info, ppat_norm, ppat_prep ) ))
2034 : : {
2035 : 0 : goto exit_function;
2036 : : }
2037 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr( pStruct ))) /* djb-rwth: addressing LLVM warning */
2038 : : {
2039 : 0 : goto exit_function;
2040 : : }
2041 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
2042 : : {
2043 : 0 : goto exit_function; /* no fixed-H found */
2044 : : }
2045 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI( pStruct, at2, pVA, pInChI, pc2i ))) /* djb-rwth: addressing LLVM warning */
2046 : : {
2047 : 0 : goto exit_function;
2048 : : }
2049 [ # # ]: 0 : if (!pc2i->bHasDifference)
2050 : : {
2051 : 0 : goto exit_function; /* nothing to do */
2052 : : }
2053 : : }
2054 : : }
2055 : :
2056 [ # # ]: 0 : if (pc2i->nNumTgInChI > 1 &&
2057 [ # # # # ]: 0 : ( pStruct->nNumRemovedProtonsMobHInChI > 0 || pStruct->ti.tni.nNumRemovedProtons > 0 ) &&
2058 [ # # # # ]: 0 : pStruct->fixed_H && pStruct->endpoint &&
2059 [ # # # # ]: 0 : pStruct->pOne_norm_data[TAUT_YES] && pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds)
2060 : : {
2061 : : /*----------------------------------------------------------*/
2062 : : /* case 06e:restored: XHn(+)=-AB-YHm orig.: XHn-=AB=YHm(+) */
2063 : : /* FixH: 1 0 1 1 */
2064 : : /* MobH: 0 1 t t */
2065 : : /* non-taut atoms multiple t-groups */
2066 : : /* */
2067 : : /* 1. orig. t-group has more H on its endpoints counted */
2068 : : /* in atf and has no (+) on endpoint that has H */
2069 : : /* 2. orig. t-group has less H on its endpoints counted */
2070 : : /* in atf and has (+) on endpoint that has H */
2071 : : /* in reconstructed struct and less H in atf */
2072 : : /* Solution: move (+) from (2) to atom in (1) that has H */
2073 : : /* */
2074 : : /* tg1 reconstr: XHn and more H than in orig t-group */
2075 : : /* atf: XHn */
2076 : : /* tg2 reconstr: XHm(+) and less H than in */
2077 : : /* atf: XH(m-1) orig in t-group */
2078 : : /* */
2079 : : /* N = N, O, S, Se; atoms N are not tautomeric */
2080 : : /* X = O, S, Se, Te, F, Cl, Br, I; atom X is not tautomeric*/
2081 : : /* Solution: move (+) from X to NH */
2082 : : /*----------------------------------------------------------*/
2083 : :
2084 : : int iat, nNumWrongTg, jjoffs, jj, nNum2RemovePlus, nNum2AddPlus, nNum2MovePlus;
2085 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
2086 : : /*
2087 : : inp_ATOM *at_Mobile_H_Revrs = (pStruct->pOne_norm_data[1] &&
2088 : : pStruct->pOne_norm_data[1]->at)? pStruct->pOne_norm_data[1]->at : NULL;
2089 : : */
2090 : 0 : inp_ATOM *atfMobile_H_Revrs = pStruct->pOne_norm_data[TAUT_YES] &&
2091 [ # # ]: 0 : pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds ?
2092 [ # # ]: 0 : pStruct->pOne_norm_data[TAUT_YES]->at_fixed_bonds :
2093 : 0 : pStruct->pOne_norm_data[TAUT_YES] &&
2094 [ # # ]: 0 : pStruct->pOne_norm_data[TAUT_YES]->at ?
2095 [ # # ]: 0 : pStruct->pOne_norm_data[TAUT_YES]->at : NULL;
2096 : : /*
2097 : : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H? pInChI[1]->nNum_H :
2098 : : pInChI[0] && pInChI[0]->nNum_H? pInChI[0]->nNum_H : 0;
2099 : : */
2100 : : EDGE_LIST CurChargeEdges /* source of (+)*/, EndpList;
2101 : : BNS_VERTEX *pv1n, *pv2n;
2102 : : BNS_EDGE *pe1n, *pe2n;
2103 : : Vertex v1n, v2n;
2104 : :
2105 : 0 : cur_success = 0;
2106 : 0 : AllocEdgeList( &CurChargeEdges, EDGE_LIST_CLEAR );
2107 : 0 : AllocEdgeList( &EndpList, EDGE_LIST_CLEAR );
2108 : 0 : CurrEdges.num_edges = 0; /* receptors of (+) */
2109 : :
2110 [ # # ]: 0 : if (!atfMobile_H_Revrs)
2111 : : {
2112 : 0 : goto exit_case_06e;
2113 : : }
2114 : 0 : nNumWrongTg = FillTgDiffHChgFH(tdhc, MAX_DIFF_FIXH, at2, atfMobile_H_Revrs,
2115 : : nCanon2AtnoRevrs, pVA, &pStruct->ti, &EndpList);
2116 [ # # ]: 0 : if (nNumWrongTg < 1)
2117 : : {
2118 : 0 : goto exit_case_06e; /* for now only transfer (+) from one Mobile-H group to another */
2119 : : }
2120 : 0 : nNum2RemovePlus = nNum2AddPlus = 0; /* djb-rwth: removing redundant code */
2121 [ # # ]: 0 : for (i = 0; i < nNumWrongTg; i++)
2122 : : {
2123 : : /* detect t-group that has extra (+) on H */
2124 [ # # ]: 0 : if (tdhc[i].nNumHInchi > tdhc[i].nNumHNorml &&
2125 [ # # # # ]: 0 : tdhc[i].nNumPRevrs > tdhc[i].nNumPNorml && tdhc[i].n[fNumRPosChgH])
2126 : 0 : {
2127 : : /* count how many (+) to remove */
2128 : : /* store XH(+) atom numbers */
2129 : 0 : int nNumNeeded = inchi_min(tdhc[i].nNumHInchi - tdhc[i].nNumHNorml, tdhc[i].n[fNumRPosChgH]);
2130 : 0 : nNum2RemovePlus += nNumNeeded;
2131 : 0 : jjoffs = tdhc[i].i[fNumRPosChgH];
2132 [ # # ]: 0 : for (jj = 0; jj < tdhc[i].n[fNumRPosChgH]; jj++)
2133 : : {
2134 : 0 : iat = EndpList.pnEdges[jjoffs + jj];
2135 : 0 : e = pVA[iat].nCPlusGroupEdge - 1;
2136 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurChargeEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
2137 : : {
2138 : 0 : goto exit_case_06e;
2139 : : }
2140 : : }
2141 : : }
2142 : : else
2143 : : {
2144 : : /* detect t-group that needs (+) on XH to reduce number of H */
2145 [ # # # # ]: 0 : if (tdhc[i].nNumHInchi < tdhc[i].nNumHNorml && tdhc[i].n[fNumRNeutrlH])
2146 : : {
2147 : : /* store XH atom numbers */
2148 : 0 : int nNumNeeded = inchi_min(tdhc[i].nNumHNorml - tdhc[i].nNumHInchi, tdhc[i].n[fNumRNeutrlH]);
2149 : 0 : nNum2AddPlus += nNumNeeded;
2150 : 0 : jjoffs = tdhc[i].i[fNumRNeutrlH];
2151 [ # # ]: 0 : for (jj = 0; jj < tdhc[i].n[fNumRNeutrlH]; jj++)
2152 : : {
2153 : 0 : iat = EndpList.pnEdges[jjoffs + jj];
2154 : 0 : e = pVA[iat].nCPlusGroupEdge - 1;
2155 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
2156 : : {
2157 : 0 : goto exit_case_06e;
2158 : : }
2159 : : }
2160 : : }
2161 : : }
2162 : : }
2163 : 0 : nNum2MovePlus = inchi_min(nNum2RemovePlus, nNum2AddPlus);
2164 [ # # # # ]: 0 : if (CurrEdges.num_edges > 0 && CurChargeEdges.num_edges > 0)
2165 : : {
2166 [ # # # # ]: 0 : for (i = 0; 0 < nNum2MovePlus && i < nNumWrongTg; i++)
2167 : : {
2168 : : /* detect t-group that has extra (+) on H */
2169 [ # # ]: 0 : if (tdhc[i].nNumHInchi > tdhc[i].nNumHNorml &&
2170 [ # # # # ]: 0 : tdhc[i].nNumPRevrs > tdhc[i].nNumPNorml && tdhc[i].n[fNumRPosChgH])
2171 : : {
2172 : 0 : int nNum2Remove = tdhc[i].nNumHInchi - tdhc[i].nNumHNorml;
2173 [ # # ]: 0 : if (nNum2Remove < tdhc[i].n[fNumRPosChgH])
2174 : : {
2175 : 0 : nNum2Remove = tdhc[i].n[fNumRPosChgH];
2176 : : }
2177 : : /* store XH(+) atom numbers */
2178 : 0 : jjoffs = tdhc[i].i[fNumRPosChgH];
2179 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
2180 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
2181 [ # # # # : 0 : for (jj = 0; 0 < nNum2MovePlus && 0 < nNum2Remove && jj < tdhc[i].n[fNumRPosChgH]; jj++)
# # ]
2182 : : {
2183 : 0 : iat = EndpList.pnEdges[jjoffs + jj];
2184 : 0 : e = pVA[iat].nCPlusGroupEdge - 1;
2185 : 0 : pe = pBNS->edge + pVA[iat].nCPlusGroupEdge - 1;
2186 [ # # ]: 0 : if (pe->flow)
2187 : 0 : continue;
2188 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
2189 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
2190 : :
2191 [ # # ]: 0 : for (j = pv1->num_adj_edges - 1; 0 <= j; j--)
2192 : : {
2193 : 0 : pe1n = pBNS->edge + pv1->iedge[j];
2194 [ # # # # ]: 0 : if (pe1n->flow && !pe1n->forbidden)
2195 : : {
2196 : 0 : pv1n = pBNS->vert + (v1n = pe1n->neighbor12 ^ v1);
2197 : 0 : break;
2198 : : }
2199 : : }
2200 [ # # ]: 0 : if (j < 0)
2201 : : {
2202 : 0 : continue; /* not found */
2203 : : }
2204 [ # # ]: 0 : for (j = pv2->num_adj_edges - 2; 0 <= j; j--)
2205 : : {
2206 : 0 : pe2n = pBNS->edge + pv2->iedge[j];
2207 [ # # # # ]: 0 : if (pe2n->flow && !pe2n->forbidden)
2208 : : {
2209 : 0 : pv2n = pBNS->vert + (v2n = pe2n->neighbor12 ^ v2);
2210 : 0 : break;
2211 : : }
2212 : : }
2213 [ # # ]: 0 : if (j < 0)
2214 : : {
2215 : 0 : continue; /* not found */
2216 : : }
2217 : 0 : delta = 1;
2218 : 0 : pe->flow += delta;
2219 : 0 : pe1n->flow -= delta;
2220 : 0 : pe2n->flow -= delta;
2221 : 0 : pv1n->st_edge.flow -= delta;
2222 : 0 : pv2n->st_edge.flow -= delta;
2223 : 0 : pBNS->tot_st_flow -= 2 * delta;
2224 : :
2225 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
2226 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
2227 : :
2228 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1n && vPathStart == v2n) ||
# # ]
2229 [ # # # # ]: 0 : (vPathEnd == v2n && vPathStart == v1n)) &&
2230 [ # # # # ]: 0 : (nDeltaCharge == 0 || nDeltaCharge == 1)) /* djb-rwth: addressing LLVM warnings */
2231 : : {
2232 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2233 [ # # ]: 0 : if (ret > 0)
2234 : : {
2235 : 0 : nNumRunBNS++;
2236 : 0 : nNum2Remove--;
2237 : 0 : nNum2MovePlus--;
2238 : 0 : cur_success++; /* 06e */
2239 : : }
2240 : : }
2241 : : else
2242 : : {
2243 : 0 : pe->flow -= delta;
2244 : 0 : pe1n->flow += delta;
2245 : 0 : pe2n->flow += delta;
2246 : 0 : pv1n->st_edge.flow += delta;
2247 : 0 : pv2n->st_edge.flow += delta;
2248 : 0 : pBNS->tot_st_flow += 2 * delta;
2249 : : }
2250 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE)))
2251 : : {
2252 : 0 : goto exit_case_06e;
2253 : : }
2254 : : }
2255 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
2256 : : }
2257 : : }
2258 : : }
2259 : 0 : exit_case_06e:
2260 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
2261 : 0 : AllocEdgeList(&CurChargeEdges, EDGE_LIST_FREE);
2262 : 0 : AllocEdgeList(&EndpList, EDGE_LIST_FREE);
2263 [ # # ]: 0 : if (cur_success)
2264 : : {
2265 : 0 : tot_succes += cur_success;
2266 : : /* recalculate InChI from the structure */
2267 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
2268 : : ppt_group_info, ppat_norm, ppat_prep)))
2269 : : {
2270 : 0 : goto exit_function;
2271 : : }
2272 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
2273 : : {
2274 : 0 : goto exit_function;
2275 : : }
2276 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
2277 : : {
2278 : 0 : goto exit_function; /* no fixed-H found */
2279 : : }
2280 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
2281 : : {
2282 : 0 : goto exit_function;
2283 : : }
2284 [ # # ]: 0 : if (!pc2i->bHasDifference)
2285 : : {
2286 : 0 : goto exit_function; /* nothing to do */
2287 : : }
2288 : : }
2289 : : }
2290 : :
2291 [ # # ]: 0 : if (pc2i->len_c2at >= 1)
2292 : : {
2293 : : /*--------------------------------------------------------------*/
2294 : : /* case 07: restored: O(-)-AB=O original: O=AB-O(-) */
2295 : : /* FixH: 0 0 0 -1 */
2296 : : /* MobH: 0 0 0 1 */
2297 : : /* taut (non-taut) (taut) non-taut */
2298 : : /* taut (taut) (non-taut) non-taut */
2299 : : /* O = O, S, Se, Te */
2300 : : /* Solution: move (-) from O(-)-AB to AB=O */
2301 : : /*--------------------------------------------------------------*/
2302 : 0 : int num_SB_O_Minus = 0, num_DB_O_Neutr = 0, iat;
2303 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
2304 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
2305 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
2306 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
2307 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
2308 : 0 : cur_success = 0;
2309 : :
2310 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
2311 : : {
2312 : 0 : iat = pc2i->c2at[i].atomNumber;
2313 [ # # ]: 0 : if ( /* orig. InChI info: -O(-), non-taut */
2314 : 0 : num_DB_O_Neutr < MAX_DIFF_FIXH &&
2315 [ # # ]: 0 : pc2i->c2at[i].nValElectr == 6 /* O, S, Se, Te */ &&
2316 [ # # ]: 0 : !pc2i->c2at[i].endptInChI &&
2317 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
2318 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == -1 && pc2i->c2at[i].nMobHInChI == 1 &&
2319 : : /* reversed structure info: */
2320 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && pc2i->c2at[i].nMobHRevrs == 0 &&
2321 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && !at2[iat].num_H &&
2322 [ # # ]: 0 : at2[iat].valence < at2[iat].chem_bonds_valence)
2323 : : {
2324 : 0 : iat_DB_O_Neutr[num_DB_O_Neutr++] = iat;
2325 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
2326 : : {
2327 : 0 : goto exit_function;
2328 : : }
2329 : : }
2330 : : }
2331 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
2332 : : {
2333 : : /* i = canonical number - 1 */
2334 : 0 : iat = nCanon2AtnoRevrs[i];
2335 [ # # ]: 0 : if ( /* in restored atom: charge=-1, no H, has single bond, O, S, Se, Te */
2336 : 0 : num_SB_O_Minus < MAX_DIFF_FIXH &&
2337 [ # # # # ]: 0 : at2[iat].charge == -1 && !at2[iat].num_H &&
2338 [ # # # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
2339 [ # # # # ]: 0 : pVA[iat].cNumValenceElectrons == 6 &&
2340 [ # # ]: 0 : at_Mobile_H_Revrs && at_Mobile_H_Revrs[iat].endpoint &&
2341 : : /* in orig.InChI: not an endpoint, has no H */
2342 : : /*pStruct->endpoint[i] && -- modificatuion#1 */
2343 [ # # # # : 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
2344 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
2345 : : /* has (-) edge */
2346 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden)
2347 : : {
2348 : 0 : iat_SB_O_Minus[num_SB_O_Minus++] = iat;
2349 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
2350 : : {
2351 : 0 : goto exit_function;
2352 : : }
2353 : : }
2354 : : }
2355 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_O_Minus, num_DB_O_Neutr))) /* djb-rwth: addressing LLVM warning */
2356 : : {
2357 : : /* detected; attempt to fix */
2358 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
2359 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
2360 : 0 : delta = 1;
2361 [ # # # # ]: 0 : for (i = 0; i < num_SB_O_Minus && cur_success < num_try; i++)
2362 : : {
2363 : 0 : iat = iat_SB_O_Minus[i];
2364 : 0 : pe = pBNS->edge + pVA[iat].nCMinusGroupEdge - 1;
2365 [ # # ]: 0 : if (!pe->flow)
2366 : 0 : continue;
2367 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
2368 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
2369 : :
2370 : 0 : pe->forbidden |= forbidden_edge_mask;
2371 : 0 : pe->flow -= delta;
2372 : 0 : pv1->st_edge.flow -= delta;
2373 : 0 : pv2->st_edge.flow -= delta;
2374 : 0 : pBNS->tot_st_flow -= 2 * delta;
2375 : :
2376 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
2377 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
2378 : :
2379 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
2380 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 1) /* djb-rwth: addressing LLVM warning */
# # ]
2381 : : {
2382 : : /* Moved (-) charge to AB=O => nDeltaCharge == 1 */
2383 : : /* Flow change on pe (-)charge edge (O(-)-AB) is not known to RunBnsTestOnce()) */
2384 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2385 [ # # ]: 0 : if (ret > 0)
2386 : : {
2387 : 0 : nNumRunBNS++;
2388 : 0 : cur_success++; /* 07 */
2389 : : }
2390 : : }
2391 : : else
2392 : : {
2393 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
2394 : 0 : pe->flow += delta;
2395 : 0 : pv1->st_edge.flow += delta;
2396 : 0 : pv2->st_edge.flow += delta;
2397 : 0 : pBNS->tot_st_flow += 2 * delta;
2398 : : }
2399 : : INCHI_HEAPCHK
2400 : : }
2401 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
2402 : : }
2403 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
2404 [ # # ]: 0 : if (cur_success)
2405 : : {
2406 : 0 : tot_succes += cur_success;
2407 : : /* recalculate InChI from the structure */
2408 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
2409 : : ppt_group_info, ppat_norm, ppat_prep)))
2410 : : {
2411 : 0 : goto exit_function;
2412 : : }
2413 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
2414 : : {
2415 : 0 : goto exit_function;
2416 : : }
2417 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
2418 : : {
2419 : 0 : goto exit_function; /* no fixed-H found */
2420 : : }
2421 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
2422 : : {
2423 : 0 : goto exit_function;
2424 : : }
2425 [ # # ]: 0 : if (!pc2i->bHasDifference)
2426 : : {
2427 : 0 : goto exit_function; /* nothing to do */
2428 : : }
2429 : : }
2430 : : }
2431 : :
2432 [ # # ]: 0 : if (pc2i->len_c2at >= 1)
2433 : : {
2434 : : /*--------------------------------------------------------------*/
2435 : : /* case 07a: restored: O(-)-N(V)B=O original: O=N(V)B-O(-) */
2436 : : /* FixH: 0 0 0 -1 */
2437 : : /* MobH: 0 0 0 1 */
2438 : : /* non-taut (non-taut) non-taut non-taut */
2439 : : /* non-taut (taut) non-taut non-taut */
2440 : : /* O = O, S, Se, Te */
2441 : : /* Solution: move (-) from O(-)-AB to AB=O */
2442 : : /*--------------------------------------------------------------*/
2443 : 0 : int num_SB_O_Minus = 0, num_DB_O_Neutr = 0, iat, iN;
2444 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
2445 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
2446 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
2447 : 0 : cur_success = 0;
2448 : :
2449 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
2450 : : {
2451 : 0 : iat = pc2i->c2at[i].atomNumber;
2452 [ # # ]: 0 : if ( /* orig. InChI info: -O(-), non-taut */
2453 : 0 : num_DB_O_Neutr < MAX_DIFF_FIXH &&
2454 [ # # ]: 0 : pc2i->c2at[i].nValElectr == 6 /* O, S, Se, Te */ &&
2455 [ # # ]: 0 : !pc2i->c2at[i].endptInChI &&
2456 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
2457 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == -1 && pc2i->c2at[i].nMobHInChI == 1 &&
2458 : : /* reversed structure info: */
2459 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && pc2i->c2at[i].nMobHRevrs == 0 &&
2460 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && !at2[iat].num_H &&
2461 [ # # ]: 0 : at2[iat].valence < at2[iat].chem_bonds_valence)
2462 : : {
2463 : 0 : iat_DB_O_Neutr[num_DB_O_Neutr++] = iat;
2464 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
2465 : : {
2466 : 0 : goto exit_function;
2467 : : }
2468 : : }
2469 : : }
2470 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
2471 : : {
2472 : : /* i = canonical number - 1 */
2473 : 0 : iat = nCanon2AtnoRevrs[i];
2474 [ # # ]: 0 : if ( /* in restored atom: charge=-1, no H, has single bond, O, S, Se, Te */
2475 : 0 : num_SB_O_Minus < MAX_DIFF_FIXH &&
2476 [ # # # # ]: 0 : at2[iat].charge == -1 && !at2[iat].num_H &&
2477 [ # # # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
2478 [ # # ]: 0 : pVA[iat].cNumValenceElectrons == 6 &&
2479 : : /*at_Mobile_H_Revrs && !at_Mobile_H_Revrs[iat].endpoint &&*/
2480 : : /* in orig.InChI: not an endpoint, has no H */
2481 [ # # ]: 0 : !pStruct->endpoint[i] &&
2482 [ # # # # : 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
2483 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
2484 : : /* has N(V) neighbor */
2485 [ # # # # ]: 0 : 1 == at2[iat].valence && at2[iN = at2[iat].neighbor[0]].chem_bonds_valence == 5 &&
2486 [ # # # # ]: 0 : !at2[iN].charge && pVA[iN].cNumValenceElectrons == 5 &&
2487 : : /* has (-) edge */
2488 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden)
2489 : : {
2490 : 0 : iat_SB_O_Minus[num_SB_O_Minus++] = iat;
2491 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
2492 : : {
2493 : 0 : goto exit_function;
2494 : : }
2495 : : }
2496 : : }
2497 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_O_Minus, num_DB_O_Neutr))) /* djb-rwth: addressing LLVM warning */
2498 : : {
2499 : : /* detected; attempt to fix */
2500 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
2501 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
2502 : 0 : delta = 1;
2503 [ # # # # ]: 0 : for (i = 0; i < num_SB_O_Minus && cur_success < num_try; i++)
2504 : : {
2505 : 0 : iat = iat_SB_O_Minus[i];
2506 : 0 : pe = pBNS->edge + pVA[iat].nCMinusGroupEdge - 1;
2507 [ # # ]: 0 : if (!pe->flow)
2508 : 0 : continue;
2509 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
2510 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
2511 : :
2512 : 0 : pe->forbidden |= forbidden_edge_mask;
2513 : 0 : pe->flow -= delta;
2514 : 0 : pv1->st_edge.flow -= delta;
2515 : 0 : pv2->st_edge.flow -= delta;
2516 : 0 : pBNS->tot_st_flow -= 2 * delta;
2517 : :
2518 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
2519 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
2520 : :
2521 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
2522 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 1) /* djb-rwth: addressing LLVM warnings */
# # ]
2523 : : {
2524 : : /* Moved (-) charge to AB=O => nDeltaCharge == 1 */
2525 : : /* Flow change on pe (-)charge edge (O(-)-AB) is not known to RunBnsTestOnce()) */
2526 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2527 [ # # ]: 0 : if (ret > 0)
2528 : : {
2529 : 0 : nNumRunBNS++;
2530 : 0 : cur_success++; /* 07 */
2531 : : }
2532 : : }
2533 : : else
2534 : : {
2535 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
2536 : 0 : pe->flow += delta;
2537 : 0 : pv1->st_edge.flow += delta;
2538 : 0 : pv2->st_edge.flow += delta;
2539 : 0 : pBNS->tot_st_flow += 2 * delta;
2540 : : }
2541 : : INCHI_HEAPCHK
2542 : : }
2543 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
2544 : : }
2545 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
2546 [ # # ]: 0 : if (cur_success)
2547 : : {
2548 : 0 : tot_succes += cur_success;
2549 : : /* recalculate InChI from the structure */
2550 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
2551 : : ppt_group_info, ppat_norm, ppat_prep)))
2552 : : {
2553 : 0 : goto exit_function;
2554 : : }
2555 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
2556 : : {
2557 : 0 : goto exit_function;
2558 : : }
2559 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
2560 : : {
2561 : 0 : goto exit_function; /* no fixed-H found */
2562 : : }
2563 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
2564 : : {
2565 : 0 : goto exit_function;
2566 : : }
2567 [ # # ]: 0 : if (!pc2i->bHasDifference)
2568 : : {
2569 : 0 : goto exit_function; /* nothing to do */
2570 : : }
2571 : : }
2572 : : }
2573 : :
2574 [ # # ]: 0 : if ( /*(pc2i->len_c2at >= 1 || pc2i->nNumRemHRevrs) &&*/ pc2i->nNumTgInChI == 1 && /* ADP in InChI */
2575 [ # # # # ]: 0 : (pc2i->nNumEndpRevrs < pc2i->nNumEndpInChI || pc2i->nNumTgRevrs > 1))
2576 : : {
2577 : : /*----------------------------------------------------------------*/
2578 : : /* case 08: restored: O(-)-AB=N- OH- orig. O=AB-N(-)- OH- */
2579 : : /* FixH: 1 0 0 0 0 1 */
2580 : : /* MobH: 0 0 1 0 0 0 */
2581 : : /* may be taut or not non-taut taut taut taut */
2582 : : /* ADP: one t-group or more endpoints */
2583 : : /* O(-) = S, Se, Te; N = N; */
2584 : : /* Solution: move (-) from O(-) to =N-; avoid stereogenic DB on N */
2585 : : /*----------------------------------------------------------------*/
2586 : 0 : int num_DB_N_Neutr = 0, num_SB_O_Minus = 0, iat;
2587 : :
2588 : 0 : AT_NUMB* nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
2589 [ # # # # ]: 0 : S_CHAR* nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
2590 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
2591 : 0 : cur_success = 0;
2592 : :
2593 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
2594 : : {
2595 : : /* i = canonical number - 1 */
2596 : 0 : iat = nCanon2AtnoRevrs[i];
2597 [ # # ]: 0 : if ( /* in restored atom: charge=-1, has no H, has single bond, O, S, Se, Te */
2598 : 0 : num_SB_O_Minus < MAX_DIFF_FIXH &&
2599 [ # # # # ]: 0 : at2[iat].charge == -1 && !at2[iat].num_H &&
2600 [ # # # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
2601 [ # # ]: 0 : pVA[iat].cNumValenceElectrons == 6 &&
2602 : : /* in orig.InChI: an endpoint, may have fixed-H */
2603 [ # # # # ]: 0 : pStruct->endpoint[i] &&
2604 : : /*!(pStruct->fixed_H && pStruct->fixed_H[i]) &&*/
2605 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
2606 : : /* has (-) edge */
2607 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden)
2608 : : {
2609 : :
2610 : 0 : iat_SB_O_Minus[num_SB_O_Minus++] = iat;
2611 : :
2612 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
2613 : : {
2614 : 0 : goto exit_function;
2615 : : }
2616 : : }
2617 : : else
2618 : : {
2619 [ # # ]: 0 : if ( /* in restored atom: charge=0, has no H, has double non-stereogenic bond, N */
2620 : 0 : num_DB_N_Neutr < MAX_DIFF_FIXH &&
2621 [ # # # # : 0 : at2[iat].charge == 0 && !at2[iat].num_H && !at2[iat].sb_parity[0] &&
# # ]
2622 [ # # # # ]: 0 : at2[iat].valence < at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
2623 [ # # # # ]: 0 : pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1 &&
2624 : : /* in orig.InChI: an endpoint, has no fixed-H */
2625 [ # # ]: 0 : pStruct->endpoint[i] &&
2626 [ # # # # : 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
2627 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
2628 : : /* has (-) edge */
2629 [ # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 &&
2630 [ # # ]: 0 : 0 == pBNS->edge[e].forbidden)
2631 : : {
2632 : :
2633 : 0 : iat_DB_N_Neutr[num_DB_N_Neutr++] = iat;
2634 : :
2635 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
2636 : : {
2637 : 0 : goto exit_function;
2638 : : }
2639 : : }
2640 : : }
2641 : : }
2642 [ # # ]: 0 : if ((num_try = inchi_min(num_DB_N_Neutr, num_SB_O_Minus))) /* djb-rwth: addressing LLVM warning */
2643 : : {
2644 : : /* detected; attempt to fix */
2645 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
2646 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
2647 : : /* allow stereobonds in rings change */
2648 [ # # ]: 0 : if (forbidden_stereo_edge_mask)
2649 : 0 : RemoveForbiddenEdgeMask(pBNS, &FixedLargeRingStereoEdges, forbidden_stereo_edge_mask);
2650 : :
2651 : 0 : delta = 1;
2652 [ # # # # ]: 0 : for (i = 0; i < num_SB_O_Minus && cur_success < num_try; i++)
2653 : : {
2654 : 0 : iat = iat_SB_O_Minus[i];
2655 : 0 : pe = pBNS->edge + pVA[iat].nCMinusGroupEdge - 1;
2656 [ # # ]: 0 : if (!pe->flow)
2657 : 0 : continue;
2658 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
2659 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
2660 : :
2661 : 0 : pe->forbidden |= forbidden_edge_mask;
2662 : 0 : pe->flow -= delta;
2663 : 0 : pv1->st_edge.flow -= delta;
2664 : 0 : pv2->st_edge.flow -= delta;
2665 : 0 : pBNS->tot_st_flow -= 2 * delta;
2666 : :
2667 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
2668 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
2669 : :
2670 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
2671 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 1) /* djb-rwth: addressing LLVM warning */
# # ]
2672 : : {
2673 : : /* Moved (-) charge to =N- => nDeltaCharge == 1 */
2674 : : /* Flow change on pe (-)charge edge (atom (-)O-) is not known to RunBnsTestOnce()) */
2675 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
2676 [ # # ]: 0 : if (ret > 0)
2677 : : {
2678 : 0 : nNumRunBNS++;
2679 : 0 : cur_success++; /* 08 */
2680 : : }
2681 : : }
2682 : : else
2683 : : {
2684 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
2685 : 0 : pe->flow += delta;
2686 : 0 : pv1->st_edge.flow += delta;
2687 : 0 : pv2->st_edge.flow += delta;
2688 : 0 : pBNS->tot_st_flow += 2 * delta;
2689 : : }
2690 : : INCHI_HEAPCHK
2691 : : }
2692 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
2693 [ # # ]: 0 : if (forbidden_stereo_edge_mask)
2694 : 0 : SetForbiddenEdgeMask(pBNS, &FixedLargeRingStereoEdges, forbidden_stereo_edge_mask);
2695 : : }
2696 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
2697 [ # # ]: 0 : if (cur_success)
2698 : : {
2699 : 0 : tot_succes += cur_success;
2700 : : /* recalculate InChI from the structure */
2701 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
2702 : : ppt_group_info, ppat_norm, ppat_prep)))
2703 : : {
2704 : 0 : goto exit_function;
2705 : : }
2706 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
2707 : : {
2708 : 0 : goto exit_function;
2709 : : }
2710 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
2711 : : {
2712 : 0 : goto exit_function; /* no fixed-H found */
2713 : : }
2714 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
2715 : : {
2716 : 0 : goto exit_function;
2717 : : }
2718 [ # # ]: 0 : if (!pc2i->bHasDifference)
2719 : : {
2720 : 0 : goto exit_function; /* nothing to do */
2721 : : }
2722 : : }
2723 : : }
2724 : :
2725 [ # # ]: 0 : if (pc2i->len_c2at >= 2)
2726 : : {
2727 : : /*--------------------------------------------------------*/
2728 : : /* case 09: restored: NH2(+)=C--NH2 orig.: NH2-C(+)-NH2 */
2729 : : /* FixH: 2 | 2 0 | 0 */
2730 : : /* MobH: 0 0 2 2 */
2731 : : /* N = N, taut taut non-taut non-taut*/
2732 : : /* Solution: move (+) from NH2(+) to C */
2733 : : /*--------------------------------------------------------*/
2734 : : int iat;
2735 : 0 : cur_success = 0;
2736 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
2737 : : {
2738 : 0 : iat = pc2i->c2at[i].atomNumber;
2739 [ # # # # ]: 0 : if (( pc2i->c2at[i].nValElectr == 5 && pc2i->c2at[i].nPeriodNum == 1 ) &&
2740 : : /* orig. InChI info: */
2741 [ # # ]: 0 : !pc2i->c2at[i].endptInChI &&
2742 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == 0 && pc2i->c2at[i].nMobHInChI &&
2743 : : /* reversed structure info: */
2744 [ # # ]: 0 : pc2i->c2at[i].endptRevrs &&
2745 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs && !pc2i->c2at[i].nMobHRevrs &&
2746 [ # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 1 &&
2747 [ # # ]: 0 : at2[iat].valence + 1 == at2[iat].chem_bonds_valence &&
2748 [ # # # # ]: 0 : ( e = pVA[iat].nCPlusGroupEdge - 1 ) >= 0 && !pBNS->edge[e].forbidden)
2749 : : {
2750 : 0 : EdgeIndex eNC = NO_VERTEX, eCPlusC;
2751 : : int iNH2, iatC, iatNH2; /* djb-rwth: removing redundant variables */
2752 : : /* found NH2(+)=; locate =C< and find whether it has -NH2 neighbor */
2753 [ # # ]: 0 : for (j = 0; j < at2[iat].valence; j++)
2754 : : {
2755 [ # # ]: 0 : if (at2[iat].bond_type[j] == BOND_TYPE_DOUBLE)
2756 : 0 : break;
2757 : : }
2758 [ # # ]: 0 : if (j == at2[iat].valence)
2759 : : {
2760 : 0 : continue;
2761 : : }
2762 : 0 : eNC = pBNS->vert[iat].iedge[j]; /* edge NH2(+)=C */
2763 : 0 : iatC = at2[iat].neighbor[j];
2764 [ # # # # : 0 : if (pVA[iatC].cNumValenceElectrons != 4 || pVA[iatC].cMetal || at2[iatC].charge ||
# # ]
2765 [ # # # # ]: 0 : at2[iatC].valence != 3 || at2[iatC].valence + 1 != at2[iatC].chem_bonds_valence ||
2766 [ # # # # ]: 0 : ( eCPlusC = pVA[iatC].nCPlusGroupEdge - 1 ) < 0 || pBNS->edge[eCPlusC].forbidden)
2767 : : {
2768 : 0 : continue;
2769 : : }
2770 [ # # ]: 0 : for (j = 0; j < at2[iatC].valence; j++)
2771 : : {
2772 : 0 : iatNH2 = at2[iatC].neighbor[j];
2773 [ # # # # ]: 0 : if (iatNH2 == iat || pVA[iatNH2].cNumValenceElectrons != 5 ||
2774 [ # # # # : 0 : pVA[iatNH2].cPeriodicRowNumber != 1 || !at2[iatNH2].num_H || at2[iatNH2].charge)
# # ]
2775 : 0 : continue;
2776 : : /* djb-rwth: removing redundant code */
2777 [ # # ]: 0 : for (iNH2 = 0; iNH2 < pc2i->len_c2at; iNH2++)
2778 : : {
2779 [ # # ]: 0 : if (iatNH2 == pc2i->c2at[iNH2].atomNumber)
2780 : 0 : break;
2781 : : }
2782 [ # # ]: 0 : if (iNH2 == pc2i->len_c2at)
2783 : : {
2784 : 0 : continue;
2785 : : }
2786 : :
2787 [ # # # # ]: 0 : if (( pc2i->c2at[iNH2].nValElectr == 5 && pc2i->c2at[iNH2].nPeriodNum == 1 ) &&
2788 : : /* orig. InChI info: */
2789 [ # # ]: 0 : !pc2i->c2at[iNH2].endptInChI &&
2790 [ # # # # ]: 0 : pc2i->c2at[iNH2].nFixHInChI == 0 && pc2i->c2at[iNH2].nMobHInChI &&
2791 : : /* reversed structure info: */
2792 [ # # ]: 0 : pc2i->c2at[iNH2].endptRevrs &&
2793 [ # # # # ]: 0 : pc2i->c2at[iNH2].nFixHRevrs && !pc2i->c2at[iNH2].nMobHRevrs &&
2794 [ # # ]: 0 : pc2i->c2at[iNH2].nAtChargeRevrs == 0 &&
2795 [ # # ]: 0 : at2[iatNH2].valence == at2[iatNH2].chem_bonds_valence)
2796 : : {
2797 : : /* we have found NH2(+)=, =C<, and bond between them */
2798 : :
2799 [ # # ]: 0 : if ((ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
2800 : : {
2801 : 0 : goto exit_function;
2802 : : }
2803 [ # # ]: 0 : if ((ret = AddToEdgeList( &CurrEdges, eCPlusC, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
2804 : : {
2805 : 0 : goto exit_function;
2806 : : }
2807 : 0 : SetForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
2808 : 0 : RemoveForbiddenEdgeMask( pBNS, &CurrEdges, forbidden_edge_mask );
2809 : 0 : delta = 1;
2810 : :
2811 : 0 : pe = pBNS->edge + eNC;
2812 [ # # ]: 0 : if (!pe->flow)
2813 : 0 : continue;
2814 : 0 : pv1 = pBNS->vert + ( v1 = pe->neighbor1 );
2815 : 0 : pv2 = pBNS->vert + ( v2 = pe->neighbor12 ^ v1 );
2816 : :
2817 : 0 : pe->forbidden |= forbidden_edge_mask;
2818 : 0 : pe->flow -= delta; /* add (+) to -NHm */
2819 : 0 : pv1->st_edge.flow -= delta;
2820 : 0 : pv2->st_edge.flow -= delta;
2821 : 0 : pBNS->tot_st_flow -= 2 * delta;
2822 : :
2823 : 0 : ret = RunBnsTestOnce( pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
2824 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms );
2825 : :
2826 [ # # # # : 0 : if (ret == 1 && ( (vPathEnd == v1 && vPathStart == v2) ||
# # ]
2827 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1) ) && nDeltaCharge == 0) /* djb-rwth: addressing LLVM warnings */
# # ]
2828 : : {
2829 : : /* Removed (+)charge from -NHn => nDeltaCharge == -1 */
2830 : : /* Flow change on pe (+)charge edge (atom NHm(+)) is not known to RunBnsTestOnce()) */
2831 : 0 : ret = RunBnsRestoreOnce( pBNS, pBD, pVA, pTCGroups );
2832 [ # # ]: 0 : if (ret > 0)
2833 : : {
2834 : 0 : nNumRunBNS++;
2835 : 0 : cur_success++; /* 09 */
2836 : : }
2837 : : }
2838 : : else
2839 : : {
2840 : 0 : pe->flow += delta;
2841 : 0 : pv1->st_edge.flow += delta;
2842 : 0 : pv2->st_edge.flow += delta;
2843 : 0 : pBNS->tot_st_flow += 2 * delta;
2844 : : }
2845 : : INCHI_HEAPCHK
2846 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
2847 : 0 : RemoveForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
2848 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
2849 : 0 : break;
2850 : : }
2851 : : }
2852 : : }
2853 : : }
2854 [ # # ]: 0 : if (cur_success)
2855 : : {
2856 : 0 : tot_succes += cur_success;
2857 : : /* recalculate InChI from the structure */
2858 [ # # ]: 0 : if (0 > ( ret = MakeOneInChIOutOfStrFromINChI2( pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
2859 : : ppt_group_info, ppat_norm, ppat_prep ) ))
2860 : : {
2861 : 0 : goto exit_function;
2862 : : }
2863 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr( pStruct ))) /* djb-rwth: addressing LLVM warning */
2864 : : {
2865 : 0 : goto exit_function;
2866 : : }
2867 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
2868 : : {
2869 : 0 : goto exit_function; /* no fixed-H found */
2870 : : }
2871 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI( pStruct, at2, pVA, pInChI, pc2i ))) /* djb-rwth: addressing LLVM warning */
2872 : : {
2873 : 0 : goto exit_function;
2874 : : }
2875 [ # # ]: 0 : if (!pc2i->bHasDifference)
2876 : : {
2877 : 0 : goto exit_function; /* nothing to do */
2878 : : }
2879 : : }
2880 : : }
2881 : :
2882 [ # # ]: 0 : if (pc2i->len_c2at >= 2)
2883 : : {
2884 : : /*--------------------------------------------------------*/
2885 : : /* case 10: restored: NH2-X(+)-NH- orig.: NH2(+)=X-NH- */
2886 : : /* FixH: 0 0 2 1 */
2887 : : /* MobH: 2 1 0 0 */
2888 : : /* N = N,O,S,Se,Te non-taut non-taut taut taut */
2889 : : /* Solution: move (+) from X(+) to NH2 or NH */
2890 : : /*--------------------------------------------------------*/
2891 : : int iat;
2892 : 0 : cur_success = 0;
2893 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
2894 : : {
2895 [ # # ]: 0 : if (pc2i->c2at[i].nValue)
2896 : 0 : continue;
2897 : 0 : iat = pc2i->c2at[i].atomNumber;
2898 [ # # ]: 0 : if (( pc2i->c2at[i].nValElectr == 6 ||
2899 [ # # # # ]: 0 : (pc2i->c2at[i].nValElectr == 5 && pc2i->c2at[i].nPeriodNum == 1) ) &&
2900 : : /* orig. InChI info: */
2901 [ # # ]: 0 : pc2i->c2at[i].endptInChI &&
2902 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI && !pc2i->c2at[i].nMobHInChI &&
2903 : : /* reversed structure info: */
2904 [ # # ]: 0 : !pc2i->c2at[i].endptRevrs &&
2905 [ # # # # ]: 0 : !pc2i->c2at[i].nFixHRevrs && pc2i->c2at[i].nMobHRevrs &&
2906 [ # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 &&
2907 [ # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence &&
2908 [ # # # # ]: 0 : ( e = pVA[iat].nCPlusGroupEdge - 1 ) >= 0 && !pBNS->edge[e].forbidden) /* djb-rwth: addressing LLVM warning */
2909 : : {
2910 : :
2911 : 0 : EdgeIndex eCPlusC, eCPlusNH2, bContinue = 1;
2912 : : int iNH2, iatC, iatNH2, j1, j2; /* djb-rwth: removing redundant variables */
2913 : : BNS_EDGE *pe_iat, *pe_iNH2;
2914 : : /* found NH2- locate -X(+) and find whether it has another -NH2 neighbor */
2915 [ # # # # ]: 0 : for (j1 = 0; j1 < at2[iat].valence && bContinue; j1++)
2916 : : {
2917 [ # # ]: 0 : if (at2[iat].bond_type[j1] == BOND_TYPE_SINGLE &&
2918 [ # # ]: 0 : at2[iatC = at2[iat].neighbor[j1]].charge == 1 &&
2919 [ # # # # ]: 0 : ( 4 <= pVA[iatC].cNumValenceElectrons && pVA[iatC].cNumValenceElectrons <= 6 ) &&
2920 [ # # ]: 0 : at2[iatC].valence == at2[iatC].chem_bonds_valence &&
2921 [ # # # # ]: 0 : ( eCPlusC = pVA[iatC].nCPlusGroupEdge - 1 ) >= 0 && !pBNS->edge[eCPlusC].forbidden)
2922 : : {
2923 : : /* found a candidate for X; find another NH2 */
2924 [ # # # # ]: 0 : for (j2 = 0; j2 < at2[iatC].valence && bContinue; j2++)
2925 : : {
2926 [ # # ]: 0 : if (at2[iatC].bond_type[j2] == BOND_TYPE_SINGLE &&
2927 [ # # ]: 0 : iat != ( iatNH2 = at2[iatC].neighbor[j2] ) &&
2928 [ # # # # ]: 0 : at2[iatNH2].charge == 0 && at2[iatNH2].num_H &&
2929 [ # # # # ]: 0 : ( pVA[iatNH2].cNumValenceElectrons == 5 || pVA[iatNH2].cNumValenceElectrons == 6 ) &&
2930 [ # # ]: 0 : at2[iatNH2].valence == at2[iatNH2].chem_bonds_valence &&
2931 [ # # # # ]: 0 : ( eCPlusNH2 = pVA[iatNH2].nCPlusGroupEdge - 1 ) >= 0 && !pBNS->edge[eCPlusNH2].forbidden)
2932 : : {
2933 [ # # ]: 0 : for (iNH2 = 0; iNH2 < pc2i->len_c2at; iNH2++)
2934 : : {
2935 [ # # # # ]: 0 : if (iatNH2 != pc2i->c2at[iNH2].atomNumber || pc2i->c2at[iNH2].nValue)
2936 : 0 : continue;
2937 : : /* check the second -NH */
2938 : : /* djb-rwth: removing redundant code */
2939 : 0 : if ( /* orig. InChI info: */
2940 [ # # ]: 0 : pc2i->c2at[iNH2].endptInChI &&
2941 [ # # # # ]: 0 : pc2i->c2at[iNH2].nFixHInChI && !pc2i->c2at[iNH2].nMobHInChI &&
2942 : : /* reversed structure info: */
2943 [ # # ]: 0 : !pc2i->c2at[iNH2].endptRevrs &&
2944 [ # # # # ]: 0 : !pc2i->c2at[iNH2].nFixHRevrs && pc2i->c2at[iNH2].nMobHRevrs &&
2945 [ # # ]: 0 : pc2i->c2at[iNH2].nAtChargeRevrs == 0)
2946 : : {
2947 : : /* we have found NH-X(+)-NH; remove charge from X(+) */
2948 : 0 : pe_iat = pBNS->edge + pBNS->vert[iat].iedge[j1];
2949 : 0 : pe_iNH2 = pBNS->edge + pBNS->vert[iatC].iedge[j2];
2950 : : /* pick up one of -NH to move (+) to it */
2951 [ # # # # ]: 0 : if (!pe_iat->forbidden && pBNS->edge[e].flow)
2952 : : {
2953 : 0 : pe = pBNS->edge + e;
2954 : : }
2955 : : else
2956 [ # # # # ]: 0 : if (!pe_iNH2->forbidden && pBNS->edge[eCPlusNH2].flow)
2957 : : {
2958 : 0 : pe = pBNS->edge + eCPlusNH2;
2959 : : }
2960 : : else
2961 : : {
2962 : 0 : continue; /* none of the two -X(+)- bonds may be changed */
2963 : : }
2964 [ # # ]: 0 : if ((ret = AddToEdgeList( &CurrEdges, eCPlusC, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
2965 : : {
2966 : 0 : goto exit_function;
2967 : : }
2968 : 0 : SetForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
2969 : 0 : RemoveForbiddenEdgeMask( pBNS, &CurrEdges, forbidden_edge_mask );
2970 : 0 : delta = 1;
2971 : :
2972 : 0 : pv1 = pBNS->vert + ( v1 = pe->neighbor1 );
2973 : 0 : pv2 = pBNS->vert + ( v2 = pe->neighbor12 ^ v1 );
2974 : :
2975 : : /*pe->forbidden |= forbidden_edge_mask;*/
2976 : 0 : pe->flow -= delta; /* add (+) to -NHm */
2977 : 0 : pv1->st_edge.flow -= delta;
2978 : 0 : pv2->st_edge.flow -= delta;
2979 : 0 : pBNS->tot_st_flow -= 2 * delta;
2980 : :
2981 : 0 : ret = RunBnsTestOnce( pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
2982 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms );
2983 : :
2984 [ # # # # : 0 : if (ret == 1 && ( (vPathEnd == v1 && vPathStart == v2) ||
# # ]
2985 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1) ) && nDeltaCharge == -1) /* djb-rwth: addressing LLVM warnings */
# # ]
2986 : : {
2987 : : /* Removed (+)charge from -NHn => nDeltaCharge == -1 */
2988 : : /* Flow change on pe (+)charge edge (atom NHm(+)) is not known to RunBnsTestOnce()) */
2989 : 0 : ret = RunBnsRestoreOnce( pBNS, pBD, pVA, pTCGroups );
2990 [ # # ]: 0 : if (ret > 0)
2991 : : {
2992 : 0 : nNumRunBNS++;
2993 : 0 : cur_success++; /* 10 */
2994 : 0 : bContinue = 0;
2995 : 0 : pc2i->c2at[i].nValue = 1; /* mark as used */
2996 : 0 : pc2i->c2at[iNH2].nValue = 1; /* mark as used */
2997 : : }
2998 : : }
2999 : : else
3000 : : {
3001 : 0 : pe->flow += delta;
3002 : 0 : pv1->st_edge.flow += delta;
3003 : 0 : pv2->st_edge.flow += delta;
3004 : 0 : pBNS->tot_st_flow += 2 * delta;
3005 : : }
3006 : : INCHI_HEAPCHK
3007 : :
3008 : : /*pe->forbidden &= forbidden_edge_mask_inv;*/
3009 : 0 : RemoveForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
3010 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
3011 : 0 : break;
3012 : : }
3013 : : } /* iNH2: pc2i->c2at[iNH2] cycle */
3014 : : }
3015 : : } /* j2: iatC neighbors cycle */
3016 : : }
3017 : : } /* j1: iat neighbors cycle */
3018 : : }
3019 : : } /* i: pc2i->c2at[i] cycle */
3020 : :
3021 [ # # ]: 0 : if (cur_success)
3022 : : {
3023 : : /*
3024 : : for ( i = 0; i < pc2i->len_c2at; i ++ ) {
3025 : : pc2i->c2at[i].nValue = 0;
3026 : : }
3027 : : */
3028 : 0 : tot_succes += cur_success;
3029 : : /* recalculate InChI from the structure */
3030 [ # # ]: 0 : if (0 > ( ret = MakeOneInChIOutOfStrFromINChI2( pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
3031 : : ppt_group_info, ppat_norm, ppat_prep ) ))
3032 : : {
3033 : 0 : goto exit_function;
3034 : : }
3035 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr( pStruct ))) /* djb-rwth: addressing LLVM warning */
3036 : : {
3037 : 0 : goto exit_function;
3038 : : }
3039 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
3040 : : {
3041 : 0 : goto exit_function; /* no fixed-H found */
3042 : : }
3043 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI( pStruct, at2, pVA, pInChI, pc2i ))) /* djb-rwth: addressing LLVM warning */
3044 : : {
3045 : 0 : goto exit_function;
3046 : : }
3047 [ # # ]: 0 : if (!pc2i->bHasDifference)
3048 : : {
3049 : 0 : goto exit_function; /* nothing to do */
3050 : : }
3051 : : }
3052 : : }
3053 : :
3054 [ # # ]: 0 : if ( /*pc2i->len_c2at >= 1 &&*/ pc2i->nNumTgInChI == 1 && /* ADP in InChI */
3055 [ # # # # ]: 0 : ( pc2i->nNumEndpRevrs < pc2i->nNumEndpInChI || pc2i->nNumTgRevrs > 1 ))
3056 : : {
3057 : : /*--------------------------------------------------------------*/
3058 : : /* case 11: restored: NH(+)=AB-N< OH- orig. NH-AB=N(+)< OH- */
3059 : : /* FixH: 0 0 0 1 0 1 */
3060 : : /* MobH: 1 0 1 0 0 0 */
3061 : : /* non-taut. taut taut */
3062 : : /* ADP: one t-group or more endpoints */
3063 : : /* NH(+)= => N, O, S, Se; -N< => N */
3064 : : /* Solution: move (+) from NH(+) to -N< */
3065 : : /*--------------------------------------------------------------*/
3066 : 0 : int num_SB_Neutr = 0, num_DB_Charged = 0, iat;
3067 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
3068 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
3069 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
3070 : 0 : cur_success = 0;
3071 : :
3072 : : /* search for NH(+)= */
3073 : : /* search for -N< */
3074 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
3075 : : { /* i = canonical number - 1 */
3076 : 0 : iat = nCanon2AtnoRevrs[i];
3077 [ # # ]: 0 : if ( /* in restored atom: charge=0, has no H, has no double bond, N only */
3078 : 0 : num_DB_Charged < MAX_DIFF_FIXH &&
3079 [ # # # # ]: 0 : at2[iat].charge == 1 && at2[iat].num_H &&
3080 [ # # # # ]: 0 : at2[iat].valence < at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
3081 [ # # # # ]: 0 : ((pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1) ||
3082 [ # # ]: 0 : pVA[iat].cNumValenceElectrons == 6) &&
3083 : : /* in orig.InChI: an endpoint, has fixed-H */
3084 : : /*pStruct->endpoint[i] &&*/
3085 [ # # # # ]: 0 : (pStruct->fixed_H && pStruct->fixed_H[i]) &&
3086 : : /*!(nMobHInChI && nMobHInChI[i] ) &&*/
3087 : : /* has (+) edge */
3088 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && 0 == pBNS->edge[e].forbidden) /* djb-rwth: addressing LLVM warning */
3089 : : {
3090 : :
3091 : 0 : iat_DB_Charged[num_DB_Charged++] = iat;
3092 : : /*
3093 : : if ( ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ) ) {
3094 : : goto exit_function;
3095 : : }
3096 : : */
3097 : : }
3098 : : else
3099 : : {
3100 [ # # ]: 0 : if ( /* in restored atom: charge=0, has no H, has no double bond, N only */
3101 : 0 : num_SB_Neutr < MAX_DIFF_FIXH &&
3102 [ # # # # ]: 0 : at2[iat].charge == 0 && !at2[iat].num_H &&
3103 [ # # # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
3104 [ # # ]: 0 : (pVA[iat].cNumValenceElectrons == 5 &&
3105 [ # # ]: 0 : pVA[iat].cPeriodicRowNumber == 1) &&
3106 : : /* in orig.InChI: an endpoint, has fixed-H */
3107 : : /*pStruct->endpoint[i] &&*/
3108 [ # # # # : 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
3109 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
3110 : : /* has (+) edge */
3111 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && 0 == pBNS->edge[e].forbidden)
3112 : : {
3113 : :
3114 : 0 : iat_SB_Neutr[num_SB_Neutr++] = iat;
3115 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
3116 : : {
3117 : 0 : goto exit_function;
3118 : : }
3119 : : }
3120 : : }
3121 : : }
3122 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_Neutr, num_DB_Charged))) /* djb-rwth: addressing LLVM warning */
3123 : : {
3124 : : /* detected; attempt to fix */
3125 : : BNS_VERTEX* pv1n, * pv2n;
3126 : : BNS_EDGE* pe1n, * pe2n;
3127 : : Vertex v1n, v2n;
3128 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
3129 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
3130 : 0 : delta = 1;
3131 [ # # # # ]: 0 : for (i = 0; i < num_DB_Charged && cur_success < num_try; i++)
3132 : : {
3133 : 0 : iat = iat_DB_Charged[i];
3134 : 0 : pe = pBNS->edge + pVA[iat].nCPlusGroupEdge - 1;
3135 [ # # ]: 0 : if (pe->flow)
3136 : 0 : continue;
3137 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
3138 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
3139 : :
3140 [ # # ]: 0 : for (j = pv1->num_adj_edges - 1; 0 <= j; j--)
3141 : : {
3142 : 0 : pe1n = pBNS->edge + pv1->iedge[j];
3143 [ # # # # ]: 0 : if (pe1n->flow && !pe1n->forbidden)
3144 : : {
3145 : 0 : pv1n = pBNS->vert + (v1n = pe1n->neighbor12 ^ v1);
3146 : 0 : break;
3147 : : }
3148 : : }
3149 [ # # ]: 0 : if (j < 0)
3150 : : {
3151 : 0 : continue; /* not found */
3152 : : }
3153 : :
3154 [ # # ]: 0 : for (j = pv2->num_adj_edges - 2; 0 <= j; j--)
3155 : : {
3156 : 0 : pe2n = pBNS->edge + pv2->iedge[j];
3157 [ # # # # ]: 0 : if (pe2n->flow && !pe2n->forbidden)
3158 : : {
3159 : 0 : pv2n = pBNS->vert + (v2n = pe2n->neighbor12 ^ v2);
3160 : 0 : break;
3161 : : }
3162 : : }
3163 [ # # ]: 0 : if (j < 0)
3164 : : {
3165 : 0 : continue; /* not found */
3166 : : }
3167 : :
3168 : 0 : pe->flow += delta;
3169 : 0 : pe1n->flow -= delta;
3170 : 0 : pe2n->flow -= delta;
3171 : 0 : pv1n->st_edge.flow -= delta;
3172 : 0 : pv2n->st_edge.flow -= delta;
3173 : 0 : pBNS->tot_st_flow -= 2 * delta;
3174 : :
3175 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
3176 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
3177 : :
3178 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1n && vPathStart == v2n) ||
# # ]
3179 [ # # # # ]: 0 : (vPathEnd == v2n && vPathStart == v1n)) &&
3180 [ # # # # ]: 0 : (nDeltaCharge == 0 || nDeltaCharge == 1)) /* djb-rwth: addressing LLVM warnings */
3181 : : {
3182 : : /* before setting flows the structure could be:
3183 : : [NH+ neigh, v1n]=e1n=[NH+,v1]-pe-[+,v2]=e2n=[another at or its chargeStruct]
3184 : : or
3185 : :
3186 : : [NH+ or ChStr, v1n]=pe1n=[NH+ or ChStr, v1]-pe-[+,v2]=pe2n=[at2 or ChStr, v2n]
3187 : : ^ ^ ^
3188 : : NH+(+)edge | N (+) edge: only
3189 : : | these are not forbidden
3190 : : |
3191 : : hetero (+) vertex
3192 : :
3193 : : After setting flows (* mark radicals, =pe= is forbidden):
3194 : :
3195 : : *[NH+ or ChStr, v1n]-pe1n-[NH+ or ChStr, v1]=pe=[+,v2]-pe2n-[at2 or ChStr, v2n]*
3196 : : ^ ^ ^
3197 : : NH+(+)edge | N (+) edge: only
3198 : : | these are not forbidden
3199 : : |
3200 : : hetero (+) vertex
3201 : :
3202 : : Flow in
3203 : : pe1n and pe2n will or will not change, depending on the structure.
3204 : :
3205 : : Consider what happens if pe2n changes. It may only increment.
3206 : : If pe2n flow increments then another (+)edge flow dectrements. If
3207 : : [at2 or ChStr, v2n] is at2 then at2 charge would change from (+) to 0,
3208 : : and another N charge would change from 0 to (+), giving tot. change of
3209 : : number of charges (-1)+(+1)=0. However, if [at2 or ChStr, v2n] is
3210 : : ChargeStruct then at2 will not be on the alt path and only the creation
3211 : : of another (+) will be detected.
3212 : : */
3213 : : /* Removed charge from O(+) => nDeltaCharge == -1 */
3214 : : /* Flow change on pe (+)charge edge (atom NH2) is not known to RunBnsTestOnce()) */
3215 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
3216 [ # # ]: 0 : if (ret > 0)
3217 : : {
3218 : 0 : nNumRunBNS++;
3219 : 0 : cur_success++; /* 11 */
3220 : : }
3221 : : }
3222 : : else
3223 : : {
3224 : 0 : pe->flow -= delta;
3225 : 0 : pe1n->flow += delta;
3226 : 0 : pe2n->flow += delta;
3227 : 0 : pv1n->st_edge.flow += delta;
3228 : 0 : pv2n->st_edge.flow += delta;
3229 : 0 : pBNS->tot_st_flow += 2 * delta;
3230 : : }
3231 : : INCHI_HEAPCHK
3232 : : }
3233 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
3234 : : }
3235 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
3236 [ # # ]: 0 : if (cur_success)
3237 : : {
3238 : 0 : tot_succes += cur_success;
3239 : : /* recalculate InChI from the structure */
3240 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
3241 : : ppt_group_info, ppat_norm, ppat_prep)))
3242 : : {
3243 : 0 : goto exit_function;
3244 : : }
3245 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
3246 : : {
3247 : 0 : goto exit_function;
3248 : : }
3249 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
3250 : : {
3251 : 0 : goto exit_function; /* no fixed-H found */
3252 : : }
3253 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
3254 : : {
3255 : 0 : goto exit_function;
3256 : : }
3257 [ # # ]: 0 : if (!pc2i->bHasDifference)
3258 : : {
3259 : 0 : goto exit_function; /* nothing to do */
3260 : : }
3261 : : }
3262 : : }
3263 : :
3264 [ # # # # ]: 0 : if (pc2i->len_c2at >= 1 && pc2i->nNumTgInChI == 1 &&
3265 [ # # ]: 0 : pc2i->nNumRemHInChI >= -1 && /* 2006-03-03 */
3266 [ # # # # ]: 0 : ( pc2i->nNumEndpInChI > pc2i->nNumEndpRevrs || pc2i->nNumTgRevrs > 1 ) /* ADP in InChI */)
3267 : : {
3268 : : /*--------------------------------------------------------------*/
3269 : : /* case 12: restored: O=AB-N< original: (-)O-AB=N(+)< */
3270 : : /* FixH: 0 0 0 0 */
3271 : : /* MobH: 0 0 0 0 */
3272 : : /* non-taut taut */
3273 : : /* O = O, S, Se, N; N = N; */
3274 : : /* restored atom O is not tautomeric; original atom O is taut. */
3275 : : /* original struct has 1 t-group; restored has less endpoints */
3276 : : /* and/or possibly >1 t-groups */
3277 : : /* Solution: separate charges between O= and -N< */
3278 : : /* allow moving charge to N(V) to make it N(IV)(+) */
3279 : : /*--------------------------------------------------------------*/
3280 : 0 : int bOnly_N_V = 1;
3281 : : /* djb-rwth: removing redundant code */
3282 : : while (1)
3283 : 0 : {
3284 : 0 : int num_SB_N_Neutr = 0, num_DB_O = 0, iat, num_N_V = 0, bN_V;
3285 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
3286 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
3287 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
3288 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
3289 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
3290 : 0 : cur_success = 0;
3291 : :
3292 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
3293 : : {
3294 : 0 : iat = pc2i->c2at[i].atomNumber;
3295 [ # # ]: 0 : if ( /* orig. InChI info: -O(-) */
3296 : 0 : num_DB_O < MAX_DIFF_FIXH &&
3297 [ # # ]: 0 : (pc2i->c2at[i].nValElectr == 6 /* O, S, Se, Te */ ||
3298 [ # # ]: 0 : (pc2i->c2at[i].nValElectr == 5 &&
3299 [ # # ]: 0 : pc2i->c2at[i].nPeriodNum == 1) /* N */) && /* djb-rwth: addressing LLVM warning */
3300 [ # # ]: 0 : pc2i->c2at[i].endptInChI &&
3301 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
3302 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == 0 && pc2i->c2at[i].nMobHInChI == 0 &&
3303 : : /* reversed structure info: */
3304 [ # # ]: 0 : !pc2i->c2at[i].endptRevrs &&
3305 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && pc2i->c2at[i].nMobHRevrs == 0 &&
3306 [ # # # # : 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && !at2[iat].num_H &&
# # ]
3307 [ # # ]: 0 : ((pc2i->c2at[i].nValElectr == 6) ?
3308 [ # # # # ]: 0 : (at2[iat].valence == 1 && at2[iat].chem_bonds_valence == 2) :
3309 [ # # ]: 0 : (pc2i->c2at[i].nValElectr == 5) ?
3310 [ # # # # ]: 0 : (at2[iat].valence == 2 && at2[iat].chem_bonds_valence == 3) :
3311 : : 0))
3312 : : {
3313 : :
3314 : 0 : iat_DB_O[num_DB_O++] = iat;
3315 : : /*
3316 : : if ( ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ) ) {
3317 : : goto exit_function;
3318 : : }
3319 : : */
3320 : : }
3321 : : }
3322 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
3323 : : {
3324 : : /* i = canonical number - 1 */
3325 : 0 : iat = nCanon2AtnoRevrs[i];
3326 : 0 : bN_V = 0;
3327 [ # # ]: 0 : if ( /* in restored atom N: charge=0, no H, has no double bond, not an endpoint */
3328 : 0 : num_SB_N_Neutr < MAX_DIFF_FIXH &&
3329 [ # # # # ]: 0 : at2[iat].charge == 0 && !at2[iat].num_H &&
3330 [ # # ]: 0 : (at2[iat].valence == at2[iat].chem_bonds_valence ||
3331 [ # # ]: 0 : (bN_V = at2[iat].valence + 2 == at2[iat].chem_bonds_valence)) &&
3332 [ # # ]: 0 : !pVA[iat].cMetal &&
3333 [ # # # # : 0 : pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1 &&
# # ]
3334 [ # # ]: 0 : !(at_Mobile_H_Revrs && at_Mobile_H_Revrs[iat].endpoint) &&
3335 : : /* in orig.InChI: not an endpoint, has no H */
3336 [ # # ]: 0 : !pStruct->endpoint[i] &&
3337 [ # # # # : 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
3338 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
3339 : : /* has (+) edge */
3340 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden)
3341 : : {
3342 : :
3343 [ # # # # : 0 : if (bOnly_N_V && bN_V &&
# # ]
3344 : 0 : NO_VERTEX != (j = GetChargeFlowerUpperEdge(pBNS, pVA, e)) &&
3345 [ # # # # ]: 0 : !pBNS->edge[j].forbidden && !pBNS->edge[j].flow)
3346 : : {
3347 [ # # ]: 0 : if (!num_N_V)
3348 : : {
3349 : : /* switch to N(V) only mode */
3350 : 0 : CurrEdges.num_edges = 0;
3351 : 0 : num_SB_N_Neutr = 0;
3352 : : }
3353 : 0 : iat_SB_N_Neutr[num_SB_N_Neutr++] = iat;
3354 : 0 : num_N_V++;
3355 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
3356 : : {
3357 : 0 : goto exit_function;
3358 : : }
3359 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, j, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
3360 : : {
3361 : 0 : goto exit_function;
3362 : : }
3363 : : }
3364 : : else
3365 : : {
3366 [ # # ]: 0 : if (!num_N_V)
3367 : : {
3368 : 0 : iat_SB_N_Neutr[num_SB_N_Neutr++] = iat;
3369 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
3370 : : {
3371 : 0 : goto exit_function;
3372 : : }
3373 : : /* in addition, permit N(V)=>N(IV)(+) change by allowing charge flower edge change flow */
3374 [ # # # # ]: 0 : if (bN_V && NO_VERTEX != (j = GetChargeFlowerUpperEdge(pBNS, pVA, e)) &&
3375 [ # # # # ]: 0 : !pBNS->edge[j].forbidden && !pBNS->edge[j].flow)
3376 : : {
3377 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, j, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
3378 : : {
3379 : 0 : goto exit_function;
3380 : : }
3381 : : }
3382 : : }
3383 : : }
3384 : : }
3385 : : }
3386 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_N_Neutr, num_DB_O))) /* djb-rwth: addressing LLVM warning */
3387 : : {
3388 : : /* detected; attempt to fix */
3389 : : BNS_EDGE* pe_CMinus;
3390 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
3391 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
3392 : 0 : delta = 1;
3393 [ # # # # ]: 0 : for (i = 0; i < num_DB_O && cur_success < num_try; i++)
3394 : : {
3395 : 0 : iat = iat_DB_O[i];
3396 : 0 : pe_CMinus = pBNS->edge + pVA[iat].nCMinusGroupEdge - 1;
3397 : 0 : pe_CMinus->forbidden &= forbidden_edge_mask_inv;
3398 : :
3399 : 0 : pe = pBNS->edge + pBNS->vert[iat].iedge[0]; /* double bond O=...*/
3400 [ # # ]: 0 : if (!pe->flow)
3401 : 0 : continue;
3402 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
3403 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
3404 : :
3405 : 0 : pe->forbidden |= forbidden_edge_mask; /* change bond O=X to O(rad)-X(rad) */
3406 : 0 : pe->flow -= delta;
3407 : 0 : pv1->st_edge.flow -= delta;
3408 : 0 : pv2->st_edge.flow -= delta;
3409 : 0 : pBNS->tot_st_flow -= 2 * delta;
3410 : :
3411 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
3412 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
3413 : :
3414 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
3415 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 2) /* djb-rwth: addressing LLVM warnings */
# # ]
3416 : : {
3417 : : /* Added (-) charge to =O and (+) charge to N => nDeltaCharge == 2 */
3418 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
3419 [ # # ]: 0 : if (ret > 0)
3420 : : {
3421 : 0 : nNumRunBNS++;
3422 : 0 : cur_success++; /* 12 */
3423 : : }
3424 : : }
3425 : : else
3426 : : {
3427 : 0 : pe->flow += delta;
3428 : 0 : pv1->st_edge.flow += delta;
3429 : 0 : pv2->st_edge.flow += delta;
3430 : 0 : pBNS->tot_st_flow += 2 * delta;
3431 : : }
3432 : 0 : pe->forbidden &= forbidden_edge_mask_inv; /* allow changes to O=X bond */
3433 : : INCHI_HEAPCHK
3434 : : }
3435 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
3436 : : }
3437 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
3438 [ # # ]: 0 : if (cur_success)
3439 : : {
3440 : 0 : tot_succes += cur_success;
3441 : : /* recalculate InChI from the structure */
3442 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
3443 : : ppt_group_info, ppat_norm, ppat_prep)))
3444 : : {
3445 : 0 : goto exit_function;
3446 : : }
3447 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
3448 : : {
3449 : 0 : goto exit_function;
3450 : : }
3451 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
3452 : : {
3453 : 0 : goto exit_function; /* no fixed-H found */
3454 : : }
3455 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
3456 : : {
3457 : 0 : goto exit_function;
3458 : : }
3459 [ # # ]: 0 : if (!pc2i->bHasDifference)
3460 : : {
3461 : 0 : goto exit_function; /* nothing to do */
3462 : : }
3463 : 0 : break;
3464 : : }
3465 : : else
3466 : : {
3467 [ # # ]: 0 : if (bOnly_N_V)
3468 : : {
3469 : 0 : bOnly_N_V = 0;
3470 : : }
3471 : : else
3472 : : {
3473 : 0 : break;
3474 : : }
3475 : : }
3476 : : }
3477 : : }
3478 : :
3479 : :
3480 [ # # ]: 0 : if (pc2i->nNumTgDiffMinus /*|| pc2i->nNumTgDiffH */ /* no ADP in InChI needed */)
3481 : : {
3482 : : /*--------------------------------------------------------------*/
3483 : : /* | | */
3484 : : /* case 13: restored: O=AB=N= original: (-)O-AB-N(+)= */
3485 : : /* FixH: 0 0 0 0 */
3486 : : /* MobH: 0 0 0 0 */
3487 : : /* non-taut taut non-taut */
3488 : : /* O = O, S, Se, N; N = N, P, ... */
3489 : : /* t-group in original has same num. endpoints */
3490 : : /* same num_H and less (-) than in the restored structure */
3491 : : /* original atom O is tautomeric, N is not taut in both */
3492 : : /* original struct has 1 t-group; restored has less endpoints */
3493 : : /* and/or possibly >1 t-groups */
3494 : : /* Solution: separate charges between O= and -N< */
3495 : : /* allow moving charge to N(V) to make it N(IV)(+) */
3496 : : /*--------------------------------------------------------------*/
3497 : : int itg;
3498 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
3499 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
3500 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
3501 : :
3502 : 0 : S_CHAR *num_Fixed_H_Revrs = pStruct->pOneINChI[0]->nNum_H_fixed ? pStruct->pOneINChI[0]->nNum_H_fixed : NULL;
3503 [ # # ]: 0 : S_CHAR *pnMobHRevrs = ( pStruct->pOneINChI[1] && pStruct->pOneINChI[1]->nNum_H ) ?
3504 [ # # ]: 0 : pStruct->pOneINChI[1]->nNum_H :
3505 [ # # ]: 0 : ( pStruct->pOneINChI[0] && pStruct->pOneINChI[0]->nNum_H ) ?
3506 [ # # ]: 0 : pStruct->pOneINChI[0]->nNum_H : NULL;
3507 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
3508 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
3509 : : /* djb-rwth: removing redundant code */
3510 : : /* find whether this may help */
3511 [ # # # # ]: 0 : for (itg = 0; itg < pStruct->ti.num_t_groups && itg < pStruct->One_ti.num_t_groups; itg++)
3512 : : {
3513 [ # # ]: 0 : if (pStruct->ti.t_group[itg].nNumEndpoints == pStruct->One_ti.t_group[itg].nNumEndpoints &&
3514 : 0 : pStruct->ti.t_group[itg].num[0] - pStruct->ti.t_group[itg].num[1] ==
3515 [ # # ]: 0 : pStruct->One_ti.t_group[itg].num[0] - pStruct->One_ti.t_group[itg].num[1] &&
3516 [ # # ]: 0 : pStruct->ti.t_group[itg].num[1] > pStruct->One_ti.t_group[itg].num[1])
3517 : : {
3518 : : /* restored InChI t-group has more (-) and same number of H */
3519 : 0 : int num_SB_N_Neutr = 0, num_DB_O = 0, iat;
3520 : 0 : cur_success = 0;
3521 : :
3522 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
3523 : : { /* i = canonical number - 1 */
3524 : 0 : iat = nCanon2AtnoRevrs[i];
3525 [ # # ]: 0 : if ( /* orig. InChI info: -O(-) */
3526 : 0 : num_DB_O < MAX_DIFF_FIXH &&
3527 [ # # ]: 0 : (pVA[i].cNumValenceElectrons == 6 /* O, S, Se, Te */) &&
3528 [ # # ]: 0 : pStruct->endpoint[i] == itg + 1 &&
3529 [ # # # # ]: 0 : (e = pVA[i].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
3530 [ # # # # : 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
3531 [ # # # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
3532 : : /* reversed structure info: */
3533 : : /*!pc2i->c2at[i].endptRevrs &&*/
3534 [ # # # # ]: 0 : !(num_Fixed_H_Revrs && num_Fixed_H_Revrs[iat]) &&
3535 [ # # ]: 0 : !(pnMobHRevrs && pnMobHRevrs[iat]) &&
3536 [ # # # # ]: 0 : at2[iat].charge == 0 && at2[iat].num_H == 0 &&
3537 [ # # # # ]: 0 : at2[iat].valence == 1 && at2[iat].chem_bonds_valence == 2)
3538 : : {
3539 : :
3540 : 0 : iat_DB_O[num_DB_O++] = iat;
3541 : : /*
3542 : : if ( ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ) ) {
3543 : : goto exit_function;
3544 : : }
3545 : : */
3546 : : }
3547 : : else
3548 : : {
3549 [ # # ]: 0 : if ( /* in restored atom N: charge=0, no H, has no double bond, not an endpoint */
3550 : 0 : num_SB_N_Neutr < MAX_DIFF_FIXH &&
3551 [ # # # # ]: 0 : at2[iat].charge == 0 && !at2[iat].num_H &&
3552 : : /*at2[iat].valence == at2[iat].chem_bonds_valence ||*/
3553 [ # # # # ]: 0 : (at2[iat].valence == 4 && at2[iat].chem_bonds_valence == 5) &&
3554 [ # # ]: 0 : !pVA[iat].cMetal &&
3555 [ # # # # : 0 : pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber >= 1 &&
# # ]
3556 [ # # ]: 0 : !(at_Mobile_H_Revrs && at_Mobile_H_Revrs[iat].endpoint) &&
3557 : : /* in orig.InChI: not an endpoint, has no H */
3558 [ # # ]: 0 : !pStruct->endpoint[i] &&
3559 [ # # # # : 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
3560 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
3561 : : /* has (+) edge */
3562 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden)
3563 : : {
3564 : :
3565 : 0 : iat_SB_N_Neutr[num_SB_N_Neutr++] = iat;
3566 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
3567 : : {
3568 : 0 : goto exit_function;
3569 : : }
3570 : : }
3571 : : }
3572 : : }
3573 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_N_Neutr, num_DB_O))) /* djb-rwth: addressing LLVM warning */
3574 : : {
3575 : : /* detected; attempt to fix */
3576 : : BNS_EDGE* pe_CMinus;
3577 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
3578 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
3579 : 0 : delta = 1;
3580 [ # # # # ]: 0 : for (i = 0; i < num_DB_O && cur_success < num_try; i++)
3581 : : {
3582 : 0 : iat = iat_DB_O[i];
3583 : 0 : pe_CMinus = pBNS->edge + pVA[iat].nCMinusGroupEdge - 1;
3584 : 0 : pe_CMinus->forbidden &= forbidden_edge_mask_inv;
3585 : :
3586 : 0 : pe = pBNS->edge + pBNS->vert[iat].iedge[0]; /* double bond O=...*/
3587 [ # # ]: 0 : if (!pe->flow)
3588 : 0 : continue;
3589 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
3590 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
3591 : :
3592 : 0 : pe->forbidden |= forbidden_edge_mask; /* change bond O=X to O(rad)-X(rad) */
3593 : 0 : pe->flow -= delta;
3594 : 0 : pv1->st_edge.flow -= delta;
3595 : 0 : pv2->st_edge.flow -= delta;
3596 : 0 : pBNS->tot_st_flow -= 2 * delta;
3597 : :
3598 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
3599 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
3600 : :
3601 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
3602 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 2) /* djb-rwth: addressing LLVM warning */
# # ]
3603 : : {
3604 : : /* Added (-) charge to =O and (+) charge to N => nDeltaCharge == 2 */
3605 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
3606 [ # # ]: 0 : if (ret > 0)
3607 : : {
3608 : 0 : nNumRunBNS++;
3609 : 0 : cur_success++; /* 13 */
3610 : : }
3611 : : }
3612 : : else
3613 : : {
3614 : 0 : pe->flow += delta;
3615 : 0 : pv1->st_edge.flow += delta;
3616 : 0 : pv2->st_edge.flow += delta;
3617 : 0 : pBNS->tot_st_flow += 2 * delta;
3618 : : }
3619 : 0 : pe->forbidden &= forbidden_edge_mask_inv; /* allow changes to O=X bond */
3620 : : INCHI_HEAPCHK
3621 : : }
3622 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
3623 : : }
3624 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
3625 [ # # ]: 0 : if (cur_success)
3626 : : {
3627 : 0 : tot_succes += cur_success;
3628 : : /* recalculate InChI from the structure */
3629 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
3630 : : ppt_group_info, ppat_norm, ppat_prep)))
3631 : : {
3632 : 0 : goto exit_function;
3633 : : }
3634 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
3635 : : {
3636 : 0 : goto exit_function;
3637 : : }
3638 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
3639 : : {
3640 : 0 : goto exit_function; /* no fixed-H found */
3641 : : }
3642 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
3643 : : {
3644 : 0 : goto exit_function;
3645 : : }
3646 [ # # ]: 0 : if (!pc2i->bHasDifference)
3647 : : {
3648 : 0 : goto exit_function; /* nothing to do */
3649 : : }
3650 : 0 : break;
3651 : : }/* else
3652 : : if ( bOnly_N_V ) {
3653 : : bOnly_N_V = 0;
3654 : : }
3655 : : */
3656 : : }
3657 : : }
3658 : : }
3659 : :
3660 [ # # ]: 0 : if (( (pc2i->nNumTgInChI <= 1 &&
3661 [ # # # # : 0 : pc2i->nNumRemHInChI > pc2i->nNumRemHRevrs) || pc2i->len_c2at ) &&
# # ]
3662 : 0 : bHas_N_V( at2, pStruct->num_atoms )) /* djb-rwth: addressing LLVM warnings */
3663 : : {
3664 : : /*-----------------------------------------------------------------*/
3665 : : /* | | */
3666 : : /* case 14: restored:-N=AB=N=CD-XH original: (-)N-AB-N(+)=CD-XH */
3667 : : /* FixH: 0 0 0/1 0 1 */
3668 : : /* MobH: 0 0 1/0 0 0 */
3669 : : /* non-taut n/t any non any */
3670 : : /* taut */
3671 : : /* X = O, S, Se, N; N = N */
3672 : : /* t-group in original may have more (-) than in restored */
3673 : : /* same num_H and less (-) than in the restored structure */
3674 : : /* atom N(V)/N(IV)(+) is not taut in both */
3675 : : /* The following transformation should be possible: */
3676 : : /* | | */
3677 : : /* N=AB=N=CD-XH -> (-)N-AB-N-CD=XH(+) */
3678 : : /* This allows ADP to remove H(+) from -XH */
3679 : : /* As the result, the original structure has 0 or 1 t-group */
3680 : : /* Solution: separate charges between -N(III)= and N(V) */
3681 : : /*-----------------------------------------------------------------*/
3682 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
3683 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
3684 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
3685 : :
3686 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
3687 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
3688 : 0 : int num_N_V = 0, iat, i1, i2, i3, e1Flower, e1Plus, e2Plus, e2Minus, e3Plus;
3689 : 0 : int max_success = pc2i->nNumRemHInChI - pc2i->nNumRemHRevrs;
3690 : : EDGE_LIST iat_X_List, iat_N_III_List;
3691 : 0 : AllocEdgeList( &iat_X_List, EDGE_LIST_CLEAR );
3692 : 0 : AllocEdgeList( &iat_N_III_List, EDGE_LIST_CLEAR );
3693 : 0 : cur_success = 0;
3694 : 0 : ret = 0;
3695 : :
3696 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
3697 : : {
3698 : 0 : iat = nCanon2AtnoRevrs[i];
3699 : : /* search for N(V), 3 bonds */
3700 [ # # ]: 0 : if ( /* restored structure */
3701 : 0 : num_N_V < MAX_DIFF_FIXH &&
3702 [ # # # # ]: 0 : at2[iat].chem_bonds_valence == 5 && at2[iat].valence == 3 &&
3703 [ # # # # ]: 0 : !at2[iat].charge && !at2[iat].radical &&
3704 [ # # # # : 0 : pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1 &&
# # ]
3705 [ # # ]: 0 : !(at_Mobile_H_Revrs && at_Mobile_H_Revrs[i].endpoint) &&
3706 [ # # ]: 0 : !at2[iat].num_H &&
3707 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
3708 [ # # # # ]: 0 : pBNS->edge[e].flow /* no charge */ &&
3709 [ # # ]: 0 : NO_VERTEX != (j = GetChargeFlowerUpperEdge(pBNS, pVA, e)) && !pBNS->edge[j].forbidden &&
3710 [ # # ]: 0 : !pBNS->edge[j].flow /* neutral, valence=5 */ &&
3711 : : /* orig. InChI */
3712 [ # # # # ]: 0 : !pStruct->endpoint[i] &&
3713 [ # # # # : 0 : !(nMobHInChI && nMobHInChI[i]) && pStruct->fixed_H && !pStruct->fixed_H[i]) /* djb-rwth: fixing a NULL pointer dereference */
# # ]
3714 : : {
3715 : 0 : iat_N_V_Array[num_N_V++] = iat;
3716 : : }
3717 : : else
3718 : : {
3719 : : /* search for -N= */
3720 : 0 : if ( /* restored structure */
3721 [ # # # # ]: 0 : at2[iat].chem_bonds_valence == 3 && at2[iat].valence == 2 &&
3722 [ # # # # ]: 0 : !at2[iat].charge && !at2[iat].radical &&
3723 [ # # # # : 0 : pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1 &&
# # ]
3724 [ # # ]: 0 : !(at_Mobile_H_Revrs && at_Mobile_H_Revrs[i].endpoint) &&
3725 [ # # ]: 0 : !at2[iat].num_H &&
3726 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
3727 [ # # # # ]: 0 : !pBNS->edge[e].flow /* no charge */ &&
3728 : : /* orig. InChI */
3729 : : /*!pStruct->endpoint[i] &&*/
3730 [ # # # # : 0 : !(nMobHInChI && nMobHInChI[i]) && pStruct->fixed_H && !pStruct->fixed_H[i]) /* djb-rwth: fixing a NULL pointer dereference */
# # ]
3731 : : {
3732 : :
3733 [ # # ]: 0 : if ((ret = AddToEdgeList(&iat_N_III_List, iat, 32))) /* djb-rwth: addressing LLVM warning */
3734 : : {
3735 : 0 : goto exit_case_14;
3736 : : }
3737 : : }
3738 : : else
3739 : : {
3740 : : /* search for -OH -NH-, -NH2 */
3741 : 0 : if ( /* restored structure */
3742 [ # # ]: 0 : at2[iat].chem_bonds_valence == at2[iat].valence &&
3743 [ # # # # ]: 0 : !at2[iat].charge && !at2[iat].radical &&
3744 [ # # # # ]: 0 : ((pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1) ||
3745 [ # # ]: 0 : pVA[iat].cNumValenceElectrons == 6) &&
3746 [ # # ]: 0 : at2[iat].num_H &&
3747 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
3748 [ # # # # ]: 0 : pBNS->edge[e].flow /* no charge */ &&
3749 : : /* orig. InChI */
3750 [ # # # # : 0 : !(nMobHInChI && nMobHInChI[i]) && pStruct->fixed_H && pStruct->fixed_H[i]) /* djb-rwth: addressing LLVM warning; fixing a NULL pointer dereference */
# # ]
3751 : : {
3752 : :
3753 [ # # ]: 0 : if ((ret = AddToEdgeList(&iat_X_List, iat, 32))) /* djb-rwth: addressing LLVM warning */
3754 : : {
3755 : 0 : goto exit_case_14;
3756 : : }
3757 : : }
3758 : : }
3759 : : }
3760 : : }
3761 [ # # ]: 0 : if (!max_success)
3762 : : {
3763 : 0 : max_success = inchi_min(num_N_V, iat_N_III_List.num_edges);
3764 : 0 : max_success = inchi_min(max_success, iat_X_List.num_edges);
3765 : : }
3766 [ # # # # : 0 : if (num_N_V && iat_N_III_List.num_edges && iat_X_List.num_edges)
# # ]
3767 : : {
3768 [ # # # # ]: 0 : for (i1 = 0; i1 < num_N_V && cur_success < max_success; i1++)
3769 : : {
3770 : 0 : int iat_N_V = iat_N_V_Array[i1];
3771 [ # # ]: 0 : if (NO_VERTEX == iat_N_V ||
3772 [ # # # # ]: 0 : 0 >= (e1Plus = pVA[iat_N_V].nCPlusGroupEdge - 1) ||
3773 : 0 : NO_VERTEX == (e1Flower = GetChargeFlowerUpperEdge(pBNS, pVA, e1Plus)) ||
3774 [ # # ]: 0 : 1 != pBNS->edge[e1Plus].flow ||
3775 [ # # ]: 0 : 0 != pBNS->edge[e1Flower].flow)
3776 : : {
3777 : 0 : continue;
3778 : : }
3779 [ # # # # ]: 0 : for (i2 = iat_N_III_List.num_edges - 1; 0 <= i2 && cur_success < max_success; i2--)
3780 : : {
3781 : 0 : int iat_N_III = iat_N_III_List.pnEdges[i2];
3782 [ # # ]: 0 : if (NO_VERTEX == iat_N_III ||
3783 [ # # ]: 0 : 0 >= (e2Minus = pVA[iat_N_III].nCMinusGroupEdge - 1) ||
3784 [ # # ]: 0 : 0 >= (e2Plus = pVA[iat_N_III].nCPlusGroupEdge - 1) ||
3785 [ # # ]: 0 : 0 != pBNS->edge[e2Minus].flow ||
3786 [ # # ]: 0 : 1 != pBNS->edge[e2Plus].flow)
3787 : : {
3788 : : /* do not consider this atom anymore */
3789 : 0 : iat_N_III_List.pnEdges[i2] = NO_VERTEX;
3790 : 0 : continue;
3791 : : }
3792 [ # # # # ]: 0 : for (i3 = iat_X_List.num_edges - 1; 0 <= i3 && cur_success < max_success; i3--)
3793 : : {
3794 : 0 : int iat_X = iat_X_List.pnEdges[i3];
3795 : : BNS_VERTEX* pv1n, * pv2n;
3796 : : BNS_EDGE* pe1n, * pe2n, * pe1Plus, * pe2Minus, * pe3Plus;
3797 : : Vertex v1n, v2n;
3798 : 0 : ret = 0;
3799 [ # # ]: 0 : if (NO_VERTEX == iat_X ||
3800 [ # # ]: 0 : 0 >= (e3Plus = pVA[iat_X].nCPlusGroupEdge - 1) ||
3801 [ # # ]: 0 : 1 != pBNS->edge[e3Plus].flow)
3802 : : {
3803 : : /* do not consider this atom anymore */
3804 : 0 : iat_X_List.pnEdges[i3] = NO_VERTEX;
3805 : 0 : continue;
3806 : : }
3807 : : /* all is ready to check whether the following applies:
3808 : : forbid changes of all charges and N,P,... flowers
3809 : : allow to change edges: e2Minus, e3Plus
3810 : : Increment flow in e1Flower
3811 : : The result should be: increase in number of charges by 2
3812 : : */
3813 : 0 : pe1Plus = pBNS->edge + e1Plus; /* N(V) positive charge edge */
3814 : 0 : pe2Minus = pBNS->edge + e2Minus; /* =N- negative charge edge */
3815 : 0 : pe3Plus = pBNS->edge + e3Plus; /* -XH positive charge edge */
3816 : 0 : pe = pBNS->edge + e1Flower; /* N(V) flower edge */
3817 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
3818 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
3819 [ # # ]: 0 : for (j = pv1->num_adj_edges - 1; 0 <= j; j--)
3820 : : {
3821 : 0 : pe1n = pBNS->edge + pv1->iedge[j];
3822 [ # # # # : 0 : if (pe1n->flow && !pe1n->forbidden && pe1n != pe1Plus)
# # ]
3823 : : {
3824 : 0 : pv1n = pBNS->vert + (v1n = pe1n->neighbor12 ^ v1);
3825 : 0 : break;
3826 : : }
3827 : : }
3828 [ # # ]: 0 : if (j < 0)
3829 : : {
3830 : 0 : continue; /* not found -- should not happen */
3831 : : }
3832 [ # # ]: 0 : for (j = pv2->num_adj_edges - 1; 0 <= j; j--)
3833 : : { /* was -2; changed 2006-2-28 12:35pm*/
3834 : 0 : pe2n = pBNS->edge + pv2->iedge[j];
3835 [ # # # # : 0 : if (pe2n->flow && !pe2n->forbidden && pe2n != pe1Plus)
# # ]
3836 : : {
3837 : 0 : pv2n = pBNS->vert + (v2n = pe2n->neighbor12 ^ v2);
3838 : 0 : break;
3839 : : }
3840 : : }
3841 [ # # ]: 0 : if (j < 0)
3842 : : {
3843 : 0 : continue; /* not found -- should not happen */
3844 : : }
3845 : 0 : delta = 1;
3846 : 0 : pe->flow += delta;
3847 : 0 : pe1n->flow -= delta;
3848 : 0 : pe2n->flow -= delta;
3849 : 0 : pv1n->st_edge.flow -= delta;
3850 : 0 : pv2n->st_edge.flow -= delta;
3851 : 0 : pBNS->tot_st_flow -= 2 * delta;
3852 : :
3853 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
3854 : 0 : SetForbiddenEdgeMask(pBNS, &OtherNFlowerEdges, forbidden_edge_mask);
3855 : :
3856 : : /* allow two charges to change */
3857 : 0 : pe2Minus->forbidden &= forbidden_edge_mask_inv;
3858 : 0 : pe3Plus->forbidden &= forbidden_edge_mask_inv;
3859 : : /* test #1 */
3860 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
3861 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
3862 : : INCHI_HEAPCHK
3863 [ # # ]: 0 : if (ret < 0)
3864 : : {
3865 : 0 : goto exit_case_14;
3866 : : }
3867 : : else
3868 : : {
3869 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1n && vPathStart == v2n) ||
# # ]
3870 [ # # # # ]: 0 : (vPathEnd == v2n && vPathStart == v1n)) &&
3871 [ # # ]: 0 : nDeltaCharge == 2) /* djb-rwth: addressing LLVM warning */
3872 : : {
3873 : : ; /* success */
3874 : : }
3875 : : else
3876 : : {
3877 : 0 : ret = 0;
3878 : : }
3879 : : }
3880 : : /* restore BNS */
3881 : 0 : pe2Minus->forbidden |= forbidden_edge_mask;
3882 : 0 : pe3Plus->forbidden |= forbidden_edge_mask;
3883 : 0 : pe->flow -= delta;
3884 : 0 : pe1n->flow += delta;
3885 : 0 : pe2n->flow += delta;
3886 : 0 : pv1n->st_edge.flow += delta;
3887 : 0 : pv2n->st_edge.flow += delta;
3888 : 0 : pBNS->tot_st_flow += 2 * delta;
3889 [ # # ]: 0 : if (ret == 1)
3890 : : {
3891 : : /* test #2: check if charge separation is possible */
3892 : 0 : pe->flow += delta;
3893 : 0 : pe1n->flow -= delta;
3894 : 0 : pe2n->flow -= delta;
3895 : 0 : pv1n->st_edge.flow -= delta;
3896 : 0 : pv2n->st_edge.flow -= delta;
3897 : 0 : pBNS->tot_st_flow -= 2 * delta;
3898 : :
3899 : : /* allow two charges (N(V) and N(III)) to change */
3900 : 0 : pe2Minus->forbidden &= forbidden_edge_mask_inv;
3901 : 0 : pe1Plus->forbidden &= forbidden_edge_mask_inv;
3902 : : /* test #2 */
3903 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
3904 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
3905 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1n && vPathStart == v2n) ||
# # ]
3906 [ # # # # ]: 0 : (vPathEnd == v2n && vPathStart == v1n)) &&
3907 [ # # ]: 0 : nDeltaCharge == 2) /* djb-rwth: addressing LLVM warnings */
3908 : : {
3909 : : /* success; actually change charges */
3910 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
3911 [ # # ]: 0 : if (ret > 0)
3912 : : {
3913 : 0 : nNumRunBNS++;
3914 : 0 : cur_success++; /* 14 */
3915 : : }
3916 : : }
3917 [ # # ]: 0 : if (ret <= 0)
3918 : : {
3919 : : /* failed: restore BNS flow */
3920 : 0 : pe->flow -= delta;
3921 : 0 : pe1n->flow += delta;
3922 : 0 : pe2n->flow += delta;
3923 : 0 : pv1n->st_edge.flow += delta;
3924 : 0 : pv2n->st_edge.flow += delta;
3925 : 0 : pBNS->tot_st_flow += 2 * delta;
3926 : : }
3927 : : INCHI_HEAPCHK
3928 : : }
3929 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
3930 : 0 : RemoveForbiddenEdgeMask(pBNS, &OtherNFlowerEdges, forbidden_edge_mask);
3931 [ # # ]: 0 : if (ret > 0)
3932 : : {
3933 : : /* do not repeat for the same atoms */
3934 : 0 : iat_N_V_Array[i1] = NO_VERTEX;
3935 : 0 : iat_N_III_List.pnEdges[i2] = NO_VERTEX;
3936 : 0 : iat_X_List.pnEdges[i3] = NO_VERTEX;
3937 : : }
3938 [ # # ]: 0 : if (ret < 0)
3939 : : {
3940 : 0 : goto exit_case_14;
3941 : : }
3942 [ # # ]: 0 : if (ret > 0)
3943 : : {
3944 : 0 : break;
3945 : : }
3946 : : } /* i3 cycle */
3947 [ # # ]: 0 : if (ret > 0)
3948 : : {
3949 : 0 : break;
3950 : : }
3951 : : } /* i2 cycle */
3952 : : }
3953 : : }
3954 : 0 : exit_case_14:
3955 : 0 : AllocEdgeList(&iat_X_List, EDGE_LIST_FREE);
3956 : 0 : AllocEdgeList(&iat_N_III_List, EDGE_LIST_FREE);
3957 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
3958 : 0 : RemoveForbiddenEdgeMask(pBNS, &OtherNFlowerEdges, forbidden_edge_mask);
3959 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
3960 [ # # ]: 0 : if (ret < 0)
3961 : : {
3962 : 0 : goto exit_function;
3963 : : }
3964 [ # # ]: 0 : if (cur_success)
3965 : : {
3966 : 0 : tot_succes += cur_success;
3967 : : /* recalculate InChI from the structure */
3968 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
3969 : : ppt_group_info, ppat_norm, ppat_prep)))
3970 : : {
3971 : 0 : goto exit_function;
3972 : : }
3973 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
3974 : : {
3975 : 0 : goto exit_function;
3976 : : }
3977 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
3978 : : {
3979 : 0 : goto exit_function; /* no fixed-H found */
3980 : : }
3981 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
3982 : : {
3983 : 0 : goto exit_function;
3984 : : }
3985 [ # # ]: 0 : if (!pc2i->bHasDifference)
3986 : : {
3987 : 0 : goto exit_function; /* nothing to do */
3988 : : }
3989 : : }
3990 : : }
3991 : :
3992 [ # # ]: 0 : if (pc2i->nNumTgMRevrs > pc2i->nNumTgMInChI ||
3993 [ # # ]: 0 : pc2i->nNumRemHRevrs < pc2i->nNumRemHInChI ||
3994 [ # # ]: 0 : pc2i->nNumEndpRevrs < pc2i->nNumEndpInChI ||
3995 [ # # # # ]: 0 : (pc2i->nNumTgInChI <= 1 && pc2i->nNumTgRevrs > pc2i->nNumTgInChI)) /* djb-rwth: addressing LLVM warning */
3996 : : {
3997 : : /*--------------------------------------------------------------*/
3998 : : /* case 15: restored: -(+)O=AB-N< orig: -O-AB=N(+)< */
3999 : : /* (a) restored t-groups have more (-) than in original InChI */
4000 : : /* (b) Mobile-H charge: restored > original InChI *and* */
4001 : : /* removed H: restored < original InChI */
4002 : : /* (c) restored t-groups have less endpnoits than in orig InChI */
4003 : : /* O = O, S, Se, Te; N = N */
4004 : : /* Solution: move (+) from -O(+)= to -N< */
4005 : : /*--------------------------------------------------------------*/
4006 : 0 : int num_SB_Neutr = 0, num_DB_Charged = 0, iat;
4007 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
4008 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
4009 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
4010 : 0 : cur_success = 0;
4011 : :
4012 : : /* search for -O(+)= */
4013 : : /* search for -N< */
4014 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
4015 : : { /* i = canonical number - 1 */
4016 : 0 : iat = nCanon2AtnoRevrs[i];
4017 [ # # ]: 0 : if ( /* -O(+)= in restored atom: charge=1, has no H, a double bond */
4018 : 0 : num_DB_Charged < MAX_DIFF_FIXH &&
4019 [ # # # # ]: 0 : at2[iat].charge == 1 && !at2[iat].num_H &&
4020 [ # # # # ]: 0 : at2[iat].valence < at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
4021 [ # # ]: 0 : (pVA[iat].cNumValenceElectrons == 6) &&
4022 : : /* in orig.InChI: an endpoint, has fixed-H */
4023 : : /*pStruct->endpoint[i] &&*/
4024 [ # # # # : 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
4025 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
4026 : : /* has (+) edge */
4027 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && 0 == pBNS->edge[e].forbidden)
4028 : : {
4029 : :
4030 : 0 : iat_DB_Charged[num_DB_Charged++] = iat;
4031 : : /*
4032 : : if ( ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ) ) {
4033 : : goto exit_function;
4034 : : }
4035 : : */
4036 : : }
4037 : : else
4038 : : {
4039 [ # # ]: 0 : if ( /* -N< in restored atom: charge=0, has no H, has no double bond, N only */
4040 : 0 : num_SB_Neutr < MAX_DIFF_FIXH &&
4041 [ # # # # ]: 0 : at2[iat].charge == 0 && !at2[iat].num_H &&
4042 [ # # # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
4043 [ # # ]: 0 : (pVA[iat].cNumValenceElectrons == 5 &&
4044 [ # # ]: 0 : pVA[iat].cPeriodicRowNumber == 1) &&
4045 : : /* in orig.InChI: an endpoint, has fixed-H */
4046 : : /*pStruct->endpoint[i] &&*/
4047 [ # # # # : 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
4048 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
4049 : : /* has (+) edge */
4050 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && 0 == pBNS->edge[e].forbidden)
4051 : : {
4052 : :
4053 : 0 : iat_SB_Neutr[num_SB_Neutr++] = iat;
4054 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
4055 : : {
4056 : 0 : goto exit_function;
4057 : : }
4058 : : }
4059 : : }
4060 : : }
4061 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_Neutr, num_DB_Charged))) /* djb-rwth: addressing LLVM warning */
4062 : : {
4063 : : /* detected; attempt to fix */
4064 : : BNS_VERTEX* pv1n, * pv2n;
4065 : : BNS_EDGE* pe1n, * pe2n;
4066 : : Vertex v1n, v2n;
4067 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
4068 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
4069 : 0 : delta = 1;
4070 [ # # # # ]: 0 : for (i = 0; i < num_DB_Charged && cur_success < num_try; i++)
4071 : : {
4072 : 0 : iat = iat_DB_Charged[i];
4073 : 0 : pe = pBNS->edge + pVA[iat].nCPlusGroupEdge - 1;
4074 [ # # ]: 0 : if (pe->flow)
4075 : 0 : continue;
4076 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
4077 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
4078 : :
4079 [ # # ]: 0 : for (j = pv1->num_adj_edges - 1; 0 <= j; j--)
4080 : : {
4081 : 0 : pe1n = pBNS->edge + pv1->iedge[j];
4082 [ # # # # ]: 0 : if (pe1n->flow && !pe1n->forbidden)
4083 : : {
4084 : 0 : pv1n = pBNS->vert + (v1n = pe1n->neighbor12 ^ v1);
4085 : 0 : break;
4086 : : }
4087 : : }
4088 [ # # ]: 0 : if (j < 0)
4089 : : {
4090 : 0 : continue; /* not found */
4091 : : }
4092 : :
4093 [ # # ]: 0 : for (j = pv2->num_adj_edges - 1; 0 <= j; j--)
4094 : : { /* was -2; changed 2006-2-28 12:35pm*/
4095 : 0 : pe2n = pBNS->edge + pv2->iedge[j];
4096 [ # # # # ]: 0 : if (pe2n->flow && !pe2n->forbidden)
4097 : : {
4098 : 0 : pv2n = pBNS->vert + (v2n = pe2n->neighbor12 ^ v2);
4099 : 0 : break;
4100 : : }
4101 : : }
4102 [ # # ]: 0 : if (j < 0)
4103 : : {
4104 : 0 : continue; /* not found */
4105 : : }
4106 : :
4107 : 0 : pe->flow += delta;
4108 : 0 : pe1n->flow -= delta;
4109 : 0 : pe2n->flow -= delta;
4110 : 0 : pv1n->st_edge.flow -= delta;
4111 : 0 : pv2n->st_edge.flow -= delta;
4112 : 0 : pBNS->tot_st_flow -= 2 * delta;
4113 : :
4114 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
4115 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
4116 : :
4117 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1n && vPathStart == v2n) ||
# # ]
4118 [ # # # # ]: 0 : (vPathEnd == v2n && vPathStart == v1n)) &&
4119 [ # # # # ]: 0 : (nDeltaCharge == 0 || nDeltaCharge == 1)) /* djb-rwth: addressing LLVM warnings */
4120 : : {
4121 : : /* Moved charge from O(+) to -N< => nDeltaCharge == 1 or 0 if pe2n = -N< charge edge */
4122 : : /* Flow change on pe (+)charge edge (atom NH2) is not known to RunBnsTestOnce()) */
4123 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
4124 [ # # ]: 0 : if (ret > 0)
4125 : : {
4126 : 0 : nNumRunBNS++;
4127 : 0 : cur_success++; /* 15 */
4128 : : }
4129 : : }
4130 : : else
4131 : : {
4132 : 0 : pe->flow -= delta;
4133 : 0 : pe1n->flow += delta;
4134 : 0 : pe2n->flow += delta;
4135 : 0 : pv1n->st_edge.flow += delta;
4136 : 0 : pv2n->st_edge.flow += delta;
4137 : 0 : pBNS->tot_st_flow += 2 * delta;
4138 : : }
4139 : : INCHI_HEAPCHK
4140 : : }
4141 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
4142 : : }
4143 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
4144 [ # # ]: 0 : if (cur_success)
4145 : : {
4146 : 0 : tot_succes += cur_success;
4147 : : /* recalculate InChI from the structure */
4148 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
4149 : : ppt_group_info, ppat_norm, ppat_prep)))
4150 : : {
4151 : 0 : goto exit_function;
4152 : : }
4153 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
4154 : : {
4155 : 0 : goto exit_function;
4156 : : }
4157 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
4158 : : {
4159 : 0 : goto exit_function; /* no fixed-H found */
4160 : : }
4161 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
4162 : : {
4163 : 0 : goto exit_function;
4164 : : }
4165 [ # # ]: 0 : if (!pc2i->bHasDifference)
4166 : : {
4167 : 0 : goto exit_function; /* nothing to do */
4168 : : }
4169 : : }
4170 : : }
4171 : :
4172 [ # # ]: 0 : if (pc2i->nNumTgDiffMinus)
4173 : : {
4174 : : /*----------------------------------------------------------------*/
4175 : : /* case 16: restored: O=X-NH(-) orig.: O(-)-X=NH */
4176 : : /* t-group: (H,-) (2H) */
4177 : : /* O(-) = S, Se, Te; N = N; */
4178 : : /* Solution: move (-) from O(-) to -NH(-) */
4179 : : /*----------------------------------------------------------------*/
4180 : 0 : int num_SB_N_Minus = 0, num_DB_O_Neutr = 0, iat, itg;
4181 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
4182 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
4183 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
4184 : 0 : cur_success = 0; /* djb-rwth: ignoring LLVM warning: variable used */
4185 : :
4186 [ # # # # ]: 0 : for (itg = 0; itg < pStruct->ti.num_t_groups && itg < pStruct->One_ti.num_t_groups; itg++)
4187 : : {
4188 [ # # ]: 0 : if (pStruct->ti.t_group[itg].nNumEndpoints != pStruct->One_ti.t_group[itg].nNumEndpoints ||
4189 [ # # ]: 0 : pStruct->ti.t_group[itg].num[1] >= pStruct->One_ti.t_group[itg].num[1])
4190 : : {
4191 : 0 : continue;
4192 : : }
4193 : 0 : CurrEdges.num_edges = num_SB_N_Minus = num_DB_O_Neutr = 0;
4194 : 0 : cur_success = 0;
4195 : 0 : for (j = 0, k = pStruct->One_ti.t_group[itg].nFirstEndpointAtNoPos;
4196 [ # # ]: 0 : j < pStruct->One_ti.t_group[itg].nNumEndpoints; j++)
4197 : : {
4198 : 0 : i = pStruct->One_ti.nEndpointAtomNumber[k + j]; /* canonical number in restored struct. */
4199 : 0 : iat = nCanon2AtnoRevrs[i];
4200 [ # # ]: 0 : if ( /* in restored atom: charge=0, has no H, has double bond, O, S, Se, Te */
4201 : 0 : num_DB_O_Neutr < MAX_DIFF_FIXH &&
4202 [ # # # # ]: 0 : at2[iat].charge == 0 && !at2[iat].num_H &&
4203 [ # # # # ]: 0 : at2[iat].valence < at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
4204 [ # # ]: 0 : pVA[iat].cNumValenceElectrons == 6 &&
4205 : : /* in orig.InChI: an endpoint, may have fixed-H */
4206 [ # # # # ]: 0 : pStruct->endpoint[i] &&
4207 : : /*!(pStruct->fixed_H && pStruct->fixed_H[i]) &&*/
4208 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
4209 : : /* has (-) edge */
4210 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden)
4211 : : {
4212 : :
4213 : 0 : iat_DB_O_Neutr[num_DB_O_Neutr++] = iat;
4214 : :
4215 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
4216 : : {
4217 : 0 : goto exit_function;
4218 : : }
4219 : : }
4220 : : else
4221 : : {
4222 [ # # ]: 0 : if ( /* in restored atom: charge=-1, has H, has double bond, N */
4223 : 0 : num_SB_N_Minus < MAX_DIFF_FIXH &&
4224 [ # # # # ]: 0 : at2[iat].charge == -1 && at2[iat].num_H &&
4225 [ # # # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
4226 [ # # # # ]: 0 : pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1 &&
4227 : : /* in orig.InChI: an endpoint, has no fixed-H */
4228 [ # # ]: 0 : pStruct->endpoint[i] &&
4229 [ # # # # : 0 : (pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
4230 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
4231 : : /* has (-) edge */
4232 [ # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 &&
4233 [ # # ]: 0 : 0 == pBNS->edge[e].forbidden)
4234 : : {
4235 : :
4236 : 0 : iat_SB_N_Minus[num_SB_N_Minus++] = iat;
4237 : : /*
4238 : : if ( ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ) ) {
4239 : : goto exit_function;
4240 : : }
4241 : : */
4242 : : }
4243 : : }
4244 : : }
4245 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_N_Minus, num_DB_O_Neutr))) /* djb-rwth: addressing LLVM warning */
4246 : : {
4247 : : /* detected; attempt to fix */
4248 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
4249 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
4250 : : /* allow stereobonds in rings change */
4251 : : /*
4252 : : if ( forbidden_stereo_edge_mask )
4253 : : RemoveForbiddenEdgeMask( pBNS, &FixedLargeRingStereoEdges, forbidden_stereo_edge_mask );
4254 : : */
4255 : 0 : delta = 1;
4256 [ # # # # ]: 0 : for (i = 0; i < num_SB_N_Minus && cur_success < num_try; i++)
4257 : : {
4258 : 0 : iat = iat_SB_N_Minus[i];
4259 : 0 : pe = pBNS->edge + pVA[iat].nCMinusGroupEdge - 1;
4260 [ # # ]: 0 : if (!pe->flow)
4261 : 0 : continue;
4262 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
4263 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
4264 : :
4265 : : /*pe->forbidden |= forbidden_edge_mask;*/
4266 : 0 : pe->flow -= delta;
4267 : 0 : pv1->st_edge.flow -= delta;
4268 : 0 : pv2->st_edge.flow -= delta;
4269 : 0 : pBNS->tot_st_flow -= 2 * delta;
4270 : :
4271 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
4272 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
4273 : :
4274 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
4275 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 1) /* djb-rwth: addressing LLVM warning */
# # ]
4276 : : {
4277 : : /* Moved (-) charge to =O => nDeltaCharge == 1 */
4278 : : /* Flow change on pe (-)charge edge (atom -NH(-)) is not known to RunBnsTestOnce()) */
4279 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
4280 [ # # ]: 0 : if (ret > 0)
4281 : : {
4282 : 0 : nNumRunBNS++;
4283 : 0 : cur_success++; /* 16 */
4284 : : }
4285 : : }
4286 : : else
4287 : : {
4288 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
4289 : 0 : pe->flow += delta;
4290 : 0 : pv1->st_edge.flow += delta;
4291 : 0 : pv2->st_edge.flow += delta;
4292 : 0 : pBNS->tot_st_flow += 2 * delta;
4293 : : }
4294 : : INCHI_HEAPCHK
4295 : : }
4296 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
4297 : : /*
4298 : : if ( forbidden_stereo_edge_mask )
4299 : : SetForbiddenEdgeMask( pBNS, &FixedLargeRingStereoEdges, forbidden_stereo_edge_mask );
4300 : : */
4301 : : }
4302 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
4303 [ # # ]: 0 : if (cur_success)
4304 : : {
4305 : 0 : tot_succes += cur_success;
4306 : : /* recalculate InChI from the structure */
4307 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
4308 : : ppt_group_info, ppat_norm, ppat_prep)))
4309 : : {
4310 : 0 : goto exit_function;
4311 : : }
4312 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
4313 : : {
4314 : 0 : goto exit_function;
4315 : : }
4316 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
4317 : : {
4318 : 0 : goto exit_function; /* no fixed-H found */
4319 : : }
4320 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
4321 : : {
4322 : 0 : goto exit_function;
4323 : : }
4324 [ # # ]: 0 : if (!pc2i->bHasDifference)
4325 : : {
4326 : 0 : goto exit_function; /* nothing to do */
4327 : : }
4328 : : }
4329 : : }
4330 : : }
4331 : :
4332 [ # # ]: 0 : if (pc2i->nNumRemHInChI < pc2i->nNumRemHRevrs)
4333 : : {
4334 : : /*--------------------------------------------------------------*/
4335 : : /* case 17: restored: OH(+)=AB-O- orig. HO-AB=O(+)- */
4336 : : /* number of removed H: n+m n */
4337 : : /* OH(+) = N, O, S, Se; -O- = P,As,O,S,Se,Te,F,Cl,Br,I */
4338 : : /* Solution: move (+) from OH(+) to -O- */
4339 : : /*--------------------------------------------------------------*/
4340 : 0 : int num_SB_Neutr = 0, num_DB_Charged = 0, iat;
4341 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
4342 [ # # # # ]: 0 : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H ? pInChI[1]->nNum_H :
4343 [ # # # # ]: 0 : pInChI[0] && pInChI[0]->nNum_H ? pInChI[0]->nNum_H : 0;
4344 : 0 : cur_success = 0;
4345 : :
4346 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
4347 : : { /* i = canonical number - 1 */
4348 : 0 : iat = nCanon2AtnoRevrs[i];
4349 [ # # ]: 0 : if ( /* in restored atom: charge=+1, has H, has double bond, N, O, S, Se, Te */
4350 : 0 : num_DB_Charged < MAX_DIFF_FIXH &&
4351 [ # # # # ]: 0 : at2[iat].charge == 1 && at2[iat].num_H &&
4352 [ # # # # ]: 0 : at2[iat].valence < at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
4353 [ # # ]: 0 : (pVA[iat].cNumValenceElectrons == 6 ||
4354 [ # # # # ]: 0 : (pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber == 1)) &&
4355 : : /* has (+) edge */
4356 [ # # # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden) /* djb-rwth: addressing LLVM warnings */
4357 : : {
4358 : :
4359 : 0 : iat_DB_Charged[num_DB_Charged++] = iat;
4360 : : /*
4361 : : if ( ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ) ) {
4362 : : goto exit_function;
4363 : : }
4364 : : */
4365 : : }
4366 : : else
4367 : : {
4368 [ # # ]: 0 : if ( /* in restored atom: charge=0, has no H, has no double bond, N, P, O, S, Se, Te */
4369 : 0 : num_SB_Neutr < MAX_DIFF_FIXH &&
4370 [ # # # # ]: 0 : at2[iat].charge == 0 && !at2[iat].num_H &&
4371 [ # # # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
4372 [ # # # # ]: 0 : (pVA[iat].cNumValenceElectrons == 6 || pVA[iat].cNumValenceElectrons == 7 ||
4373 [ # # # # ]: 0 : (pVA[iat].cNumValenceElectrons == 5 && pVA[iat].cPeriodicRowNumber > 1)) &&
4374 : : /* in orig.InChI: not an endpoint */
4375 [ # # ]: 0 : !pStruct->endpoint[i] &&
4376 [ # # # # : 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
# # ]
4377 [ # # ]: 0 : !(nMobHInChI && nMobHInChI[i]) &&
4378 : : /* has (+) edge */
4379 [ # # ]: 0 : (e = pVA[iat].nCPlusGroupEdge - 1) >= 0 &&
4380 [ # # ]: 0 : 0 == pBNS->edge[e].forbidden) /* djb-rwth: addressing LLVM warning */
4381 : : {
4382 : :
4383 : 0 : iat_SB_Neutr[num_SB_Neutr++] = iat;
4384 : :
4385 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
4386 : : {
4387 : 0 : goto exit_function;
4388 : : }
4389 : : }
4390 : : }
4391 : : }
4392 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_Neutr, num_DB_Charged))) /* djb-rwth: addressing LLVM warning */
4393 : : {
4394 : : BNS_VERTEX* pv1n, * pv2n;
4395 : : BNS_EDGE* pe1n, * pe2n;
4396 : : Vertex v1n, v2n;
4397 : :
4398 : 0 : num_try = inchi_min(num_try, pc2i->nNumRemHRevrs - pc2i->nNumRemHInChI);
4399 : : /* detected; attempt to fix */
4400 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
4401 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
4402 : 0 : delta = 1;
4403 [ # # # # ]: 0 : for (i = 0; i < num_DB_Charged && cur_success < num_try; i++)
4404 : : {
4405 : 0 : iat = iat_DB_Charged[i];
4406 : 0 : pe = pBNS->edge + pVA[iat].nCPlusGroupEdge - 1;
4407 [ # # ]: 0 : if (pe->flow)
4408 : : {
4409 : 0 : continue;
4410 : : }
4411 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
4412 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
4413 : :
4414 [ # # ]: 0 : for (j = pv1->num_adj_edges - 1; 0 <= j; j--)
4415 : : {
4416 : 0 : pe1n = pBNS->edge + pv1->iedge[j];
4417 [ # # # # ]: 0 : if (pe1n->flow && !pe1n->forbidden)
4418 : : {
4419 : 0 : pv1n = pBNS->vert + (v1n = pe1n->neighbor12 ^ v1);
4420 : 0 : break;
4421 : : }
4422 : : }
4423 [ # # ]: 0 : if (j < 0)
4424 : : {
4425 : 0 : continue; /* not found */
4426 : : }
4427 : :
4428 [ # # ]: 0 : for (j = pv2->num_adj_edges - 1; 0 <= j; j--)
4429 : : { /* was -2; changed 2006-2-28 12:35pm*/
4430 : 0 : pe2n = pBNS->edge + pv2->iedge[j];
4431 [ # # # # ]: 0 : if (pe2n->flow && !pe2n->forbidden)
4432 : : {
4433 : 0 : pv2n = pBNS->vert + (v2n = pe2n->neighbor12 ^ v2);
4434 : 0 : break;
4435 : : }
4436 : : }
4437 [ # # ]: 0 : if (j < 0)
4438 : 0 : continue; /* not found */
4439 : :
4440 : 0 : pe->flow += delta;
4441 : 0 : pe1n->flow -= delta;
4442 : 0 : pe2n->flow -= delta;
4443 : 0 : pv1n->st_edge.flow -= delta;
4444 : 0 : pv2n->st_edge.flow -= delta;
4445 : 0 : pBNS->tot_st_flow -= 2 * delta;
4446 : :
4447 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
4448 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
4449 : :
4450 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1n && vPathStart == v2n) ||
# # ]
4451 [ # # # # ]: 0 : (vPathEnd == v2n && vPathStart == v1n)) &&
4452 [ # # # # ]: 0 : (nDeltaCharge == 0 || nDeltaCharge == 1)) /* djb-rwth: addressing LLVM warnings */
4453 : : {
4454 : : /* Moved charge from OH(+) to -O- => nDeltaCharge == 1 or 0 if pe2n = -O- charge edge */
4455 : : /* Flow change on pe (+)charge edge (atom OH(+)) is not known to RunBnsTestOnce()) */
4456 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
4457 [ # # ]: 0 : if (ret > 0)
4458 : : {
4459 : 0 : nNumRunBNS++;
4460 : 0 : cur_success++; /* 17 */
4461 : : }
4462 : : }
4463 : : else
4464 : : {
4465 : 0 : pe->flow -= delta;
4466 : 0 : pe1n->flow += delta;
4467 : 0 : pe2n->flow += delta;
4468 : 0 : pv1n->st_edge.flow += delta;
4469 : 0 : pv2n->st_edge.flow += delta;
4470 : 0 : pBNS->tot_st_flow += 2 * delta;
4471 : : }
4472 : : INCHI_HEAPCHK
4473 : : }
4474 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
4475 : : }
4476 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
4477 [ # # ]: 0 : if (cur_success)
4478 : : {
4479 : 0 : tot_succes += cur_success;
4480 : : /* recalculate InChI from the structure */
4481 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
4482 : : ppt_group_info, ppat_norm, ppat_prep)))
4483 : : {
4484 : 0 : goto exit_function;
4485 : : }
4486 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
4487 : : {
4488 : 0 : goto exit_function;
4489 : : }
4490 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
4491 : : {
4492 : 0 : goto exit_function; /* no fixed-H found */
4493 : : }
4494 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
4495 : : {
4496 : 0 : goto exit_function;
4497 : : }
4498 [ # # ]: 0 : if (!pc2i->bHasDifference)
4499 : : {
4500 : 0 : goto exit_function; /* nothing to do */
4501 : : }
4502 : : }
4503 : : }
4504 : :
4505 [ # # # # ]: 0 : if (( pc2i->nNumTgInChI && pStruct->endpoint &&
4506 [ # # # # ]: 0 : pc2i->nNumTgMInChI > pc2i->nNumTgMRevrs && pc2i->nNumEndpInChI > pc2i->nNumEndpRevrs ))
4507 : : {
4508 : : /*-----------------------------------------------------------------*/
4509 : : /* */
4510 : : /* case 18: restored:-N=AB-X -(-)N-AB-X(+) */
4511 : : /* FixH: 0 0 0 0 */
4512 : : /* MobH: 0 0 0 0 */
4513 : : /* non non taut non */
4514 : : /* taut taut taut */
4515 : : /* X = any heteroatom N=N */
4516 : : /* t-group in original has (Hn,-m) in the restored: (Hn,-m+1) */
4517 : : /* same num_H and more (-) than in the restored structure */
4518 : : /* atom X is not taut in both */
4519 : : /* Solution: separate charges between -N(III)= and X */
4520 : : /*-----------------------------------------------------------------*/
4521 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
4522 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
4523 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
4524 : : int iat, e1, itg, max_success;
4525 : 0 : CurrEdges.num_edges = 0;
4526 : 0 : cur_success = 0;
4527 : 0 : ret = 0;
4528 : : /* search for -N= */
4529 [ # # # # ]: 0 : for (itg = 0; itg < pStruct->ti.num_t_groups && itg < pStruct->One_ti.num_t_groups; itg++)
4530 : : {
4531 [ # # ]: 0 : if (pStruct->ti.t_group[itg].nNumEndpoints <= pStruct->One_ti.t_group[itg].nNumEndpoints ||
4532 [ # # ]: 0 : pStruct->ti.t_group[itg].num[1] <= pStruct->One_ti.t_group[itg].num[1])
4533 : : {
4534 : 0 : continue;
4535 : : }
4536 : 0 : CurrEdges.num_edges = 0;
4537 : 0 : cur_success = 0;
4538 : 0 : for (j = 0, k = pStruct->ti.t_group[itg].nFirstEndpointAtNoPos;
4539 [ # # ]: 0 : j < pStruct->ti.t_group[itg].nNumEndpoints; j++)
4540 : : {
4541 : 0 : i = pStruct->ti.nEndpointAtomNumber[k + j]; /* canonical number in restored struct. */
4542 : 0 : iat = nCanon2AtnoRevrs[i];
4543 [ # # # # : 0 : if (!pStruct->endpoint[i] || !at_Mobile_H_Revrs || at_Mobile_H_Revrs[iat].endpoint ||
# # ]
4544 [ # # # # ]: 0 : pVA[i].cNumValenceElectrons != 5 || pVA[i].cPeriodicRowNumber != 1 ||
4545 [ # # # # : 0 : 2 != at2[iat].valence || at2[iat].num_H || at2[iat].radical ||
# # ]
4546 [ # # # # ]: 0 : (0 <= ( e1 = pVA[iat].nCPlusGroupEdge - 1 ) && !pBNS->edge[e1].flow) ||
4547 [ # # # # : 0 : 0 >( e = pVA[iat].nCMinusGroupEdge - 1 ) || pBNS->edge[e].forbidden || pBNS->edge[e].flow) /* djb-rwth: addressing LLVM warning */
# # ]
4548 : : {
4549 : 0 : continue;
4550 : : }
4551 : : /* found -N= */
4552 [ # # ]: 0 : if ((ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
4553 : : {
4554 : 0 : goto exit_function;
4555 : : }
4556 : : }
4557 : : }
4558 [ # # ]: 0 : if (!( max_success = CurrEdges.num_edges ))
4559 : : {
4560 : 0 : goto exit_case_18;
4561 : : }
4562 : : /* search for X */
4563 [ # # # # ]: 0 : for (i = 0; i < pStruct->num_atoms && cur_success < max_success; i++)
4564 : : {
4565 : 0 : iat = nCanon2AtnoRevrs[i];
4566 [ # # # # : 0 : if (pStruct->endpoint[i] || !pVA[i].cNumValenceElectrons || pVA[i].cNumValenceElectrons == 4 ||
# # ]
4567 [ # # # # ]: 0 : at2[iat].num_H || at2[iat].radical ||
4568 [ # # # # ]: 0 : (0 <= ( e1 = pVA[iat].nCMinusGroupEdge - 1 ) && !pBNS->edge[e1].flow) ||
4569 [ # # # # : 0 : 0 >( e = pVA[iat].nCPlusGroupEdge - 1 ) || pBNS->edge[e].forbidden || pBNS->edge[e].flow != 1) /* djb-rwth: addressing LLVM warnings */
# # ]
4570 : : {
4571 : 0 : continue;
4572 : : }
4573 : : /* try to move the charge */
4574 : 0 : SetForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
4575 : 0 : SetForbiddenEdgeMask( pBNS, &OtherNFlowerEdges, forbidden_edge_mask );
4576 : 0 : RemoveForbiddenEdgeMask( pBNS, &CurrEdges, forbidden_edge_mask );
4577 : :
4578 : 0 : pe = pBNS->edge + e;
4579 [ # # ]: 0 : if (!pe->flow)
4580 : 0 : continue;
4581 : 0 : pv1 = pBNS->vert + ( v1 = pe->neighbor1 );
4582 : 0 : pv2 = pBNS->vert + ( v2 = pe->neighbor12 ^ v1 );
4583 : :
4584 : 0 : delta = 1;
4585 : 0 : pe->flow -= delta;
4586 : 0 : pv1->st_edge.flow -= delta;
4587 : 0 : pv2->st_edge.flow -= delta;
4588 : 0 : pBNS->tot_st_flow -= 2 * delta;
4589 : :
4590 : 0 : ret = RunBnsTestOnce( pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
4591 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms );
4592 : :
4593 [ # # # # : 0 : if (ret == 1 && ( (vPathEnd == v1 && vPathStart == v2) ||
# # ]
4594 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1) ) && nDeltaCharge == 1) /* djb-rwth: addressing LLVM warnings */
# # ]
4595 : : {
4596 : : /* Created (-) charge on -N= => nDeltaCharge == 1 */
4597 : : /* Flow change on pe (+)charge edge (atom X) is not known to RunBnsTestOnce()) */
4598 : 0 : ret = RunBnsRestoreOnce( pBNS, pBD, pVA, pTCGroups );
4599 [ # # ]: 0 : if (ret > 0)
4600 : : {
4601 : 0 : nNumRunBNS++;
4602 : 0 : cur_success++; /* 18 */
4603 : : }
4604 : : }
4605 : : else
4606 : : {
4607 : 0 : pe->flow += delta;
4608 : 0 : pv1->st_edge.flow += delta;
4609 : 0 : pv2->st_edge.flow += delta;
4610 : 0 : pBNS->tot_st_flow += 2 * delta;
4611 : : }
4612 : : INCHI_HEAPCHK
4613 : : }
4614 : 0 : exit_case_18:
4615 : 0 : RemoveForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
4616 : 0 : RemoveForbiddenEdgeMask( pBNS, &OtherNFlowerEdges, forbidden_edge_mask );
4617 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
4618 [ # # ]: 0 : if (ret < 0)
4619 : : {
4620 : 0 : goto exit_function;
4621 : : }
4622 [ # # ]: 0 : if (cur_success)
4623 : : {
4624 : 0 : tot_succes += cur_success;
4625 : : /* recalculate InChI from the structure */
4626 [ # # ]: 0 : if (0 >( ret = MakeOneInChIOutOfStrFromINChI2( pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
4627 : : ppt_group_info, ppat_norm, ppat_prep ) ))
4628 : : {
4629 : 0 : goto exit_function;
4630 : : }
4631 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr( pStruct ))) /* djb-rwth: addressing LLVM warning */
4632 : : {
4633 : 0 : goto exit_function;
4634 : : }
4635 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
4636 : : {
4637 : 0 : goto exit_function; /* no fixed-H found */
4638 : : }
4639 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI( pStruct, at2, pVA, pInChI, pc2i ))) /* djb-rwth: addressing LLVM warning */
4640 : : {
4641 : 0 : goto exit_function;
4642 : : }
4643 [ # # ]: 0 : if (!pc2i->bHasDifference)
4644 : : {
4645 : 0 : goto exit_function; /* nothing to do */
4646 : : }
4647 : : }
4648 : : }
4649 : :
4650 [ # # ]: 0 : if (pc2i->len_c2at >= 1)
4651 : : {
4652 : : /*--------------------------------------------------------------*/
4653 : : /* case 19 restored: M--OH original: M(-)==OH(+) */
4654 : : /* FixH: metal 0 1 */
4655 : : /* MobH: 1 0 */
4656 : : /* O = O, S, Se, Te; not taut. in InChI */
4657 : : /* In restored structure has H; tautomeric or not tautomeric */
4658 : : /* Solution: move (+) from -OH to M; charhe on M may vary */
4659 : : /*--------------------------------------------------------------*/
4660 : : int iat;
4661 : : EdgeIndex eMPlus, eMMinus; /* djb-rwth: removing redundant variables/code */
4662 : : BNS_EDGE *peOHPlus, *peMPlus, *peMMinus, *peOMBond;
4663 : : int iatMetal, ChargeOnMetal, DeltaChargeExpected;
4664 : 0 : cur_success = 0;
4665 : : /* djb-rwth: removing redundant code */
4666 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
4667 : : {
4668 : 0 : iat = pc2i->c2at[i].atomNumber;
4669 : 0 : if ( /* orig. InChI info: =NH2(+), =OH(+) */
4670 [ # # ]: 0 : ( pc2i->c2at[i].nValElectr == 6 ) /* N, O, S, Se, Te */ &&
4671 : : /*!pc2i->c2at[i].endptInChI &&*/ /* <=== relaxation */
4672 [ # # # # : 0 : ( e = pVA[iat].nCPlusGroupEdge - 1 ) >= 0 && !pBNS->edge[e].forbidden && pBNS->edge[e].flow &&
# # ]
4673 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == 1 && pc2i->c2at[i].nMobHInChI == 0 &&
4674 : : /* reversed structure info: */
4675 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && pc2i->c2at[i].nMobHRevrs == 1 &&
4676 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && at2[iat].num_H &&
4677 [ # # ]: 0 : at2[iat].valence == 1 &&
4678 [ # # ]: 0 : at2[iat].valence == at2[iat].chem_bonds_valence &&
4679 : : /* metal atom */
4680 [ # # ]: 0 : pVA[iatMetal = at2[iat].neighbor[0]].cMetal &&
4681 [ # # # # ]: 0 : ( eMPlus = pVA[iatMetal].nCPlusGroupEdge - 1 ) >= 0 && !pBNS->edge[eMPlus].forbidden &&
4682 [ # # # # ]: 0 : ( eMMinus = pVA[iatMetal].nCMinusGroupEdge - 1 ) >= 0 && !pBNS->edge[eMMinus].forbidden &&
4683 [ # # ]: 0 : !pBNS->edge[pBNS->vert[iat].iedge[0]].forbidden
4684 : : ) /* djb-rwth: removing redundant code */
4685 : : {
4686 : : /* -OH charge edges */
4687 [ # # ]: 0 : if ((ret = AddToEdgeList( &CurrEdges, iat, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
4688 : : {
4689 : 0 : goto exit_function;
4690 : : }
4691 : : }
4692 : : }
4693 [ # # ]: 0 : if (CurrEdges.num_edges)
4694 : : {
4695 : : /* detected; fix */
4696 : 0 : SetForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
4697 : 0 : SetForbiddenEdgeMask( pBNS, &NFlowerEdges, forbidden_edge_mask );
4698 : 0 : SetForbiddenEdgeMask( pBNS, &AllBondEdges, forbidden_edge_mask );
4699 [ # # ]: 0 : for (i = 0; i < CurrEdges.num_edges; i++)
4700 : : {
4701 : : /* v1 is -OH, v2 is adjacent to it Metal */
4702 : 0 : iat = CurrEdges.pnEdges[i];
4703 : 0 : iatMetal = at2[iat].neighbor[0];
4704 : 0 : peOHPlus = pBNS->edge + ( (long long)pVA[iat].nCPlusGroupEdge - 1 ); /* djb-rwth: removing redundant variables/code */
4705 : 0 : peMPlus = pBNS->edge + ( (long long)pVA[iatMetal].nCPlusGroupEdge - 1 ); /* djb-rwth: removing redundant variables/code */
4706 : 0 : peMMinus = pBNS->edge + ( (long long)pVA[iatMetal].nCMinusGroupEdge - 1 ); /* djb-rwth: removing redundant variables/code */
4707 : 0 : peOMBond = pBNS->edge + ( pBNS->vert[iat].iedge[0] ); /* djb-rwth: removing redundant variables/code */
4708 : : /* remove forbidden edge masks */
4709 : 0 : peMPlus->forbidden &= forbidden_edge_mask_inv;
4710 : 0 : peMMinus->forbidden &= forbidden_edge_mask_inv;
4711 : 0 : peOMBond->forbidden &= forbidden_edge_mask_inv;
4712 : :
4713 : 0 : ChargeOnMetal = ( peMPlus->cap - peMPlus->flow ) - peMMinus->flow;
4714 [ # # ]: 0 : if (1 == ChargeOnMetal)
4715 : : {
4716 : : /* We are going to subtract 1 from the charge on Metal */
4717 : : /* Added (+)charge to -OH is not known to RunBnsTestOnce() */
4718 : 0 : DeltaChargeExpected = -1; /* charge will become = 0 */
4719 : : }
4720 : : else
4721 : : {
4722 [ # # ]: 0 : if (0 == ChargeOnMetal)
4723 : : {
4724 : 0 : DeltaChargeExpected = 1; /* charge on Metal will be created */
4725 : : }
4726 : : else
4727 : : {
4728 : 0 : DeltaChargeExpected = 0;
4729 : : }
4730 : : }
4731 : :
4732 : 0 : delta = 1;
4733 : 0 : pe = peOHPlus;
4734 [ # # ]: 0 : if (!pe->flow)
4735 : 0 : continue;
4736 : 0 : pv1 = pBNS->vert + ( v1 = pe->neighbor1 );
4737 : 0 : pv2 = pBNS->vert + ( v2 = pe->neighbor12 ^ v1 );
4738 : :
4739 : 0 : pe->flow -= delta; /* remove (-) from AB-O(-) */
4740 : 0 : pv1->st_edge.flow -= delta;
4741 : 0 : pv2->st_edge.flow -= delta;
4742 : 0 : pBNS->tot_st_flow -= 2 * delta;
4743 : :
4744 : 0 : ret = RunBnsTestOnce( pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
4745 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms );
4746 : :
4747 [ # # # # : 0 : if (ret == 1 && ( (vPathEnd == v1 && vPathStart == v2) ||
# # ]
4748 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1) ) && nDeltaCharge == DeltaChargeExpected) /* djb-rwth: addressing LLVM warnings */
# # ]
4749 : : {
4750 : 0 : ret = RunBnsRestoreOnce( pBNS, pBD, pVA, pTCGroups );
4751 [ # # ]: 0 : if (ret > 0)
4752 : : {
4753 : 0 : nNumRunBNS++;
4754 : 0 : cur_success++; /* 19 */
4755 : : }
4756 : : }
4757 : : else
4758 : : {
4759 : 0 : pe->flow += delta;
4760 : 0 : pv1->st_edge.flow += delta;
4761 : 0 : pv2->st_edge.flow += delta;
4762 : 0 : pBNS->tot_st_flow += 2 * delta;
4763 : : }
4764 : : INCHI_HEAPCHK
4765 : : /* set forbidden edge masks back */
4766 : 0 : peMPlus->forbidden |= forbidden_edge_mask;
4767 : 0 : peMMinus->forbidden |= forbidden_edge_mask;
4768 : 0 : peOMBond->forbidden |= forbidden_edge_mask;
4769 : : }
4770 : 0 : RemoveForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
4771 : 0 : RemoveForbiddenEdgeMask( pBNS, &NFlowerEdges, forbidden_edge_mask );
4772 : 0 : RemoveForbiddenEdgeMask( pBNS, &AllBondEdges, forbidden_edge_mask );
4773 : :
4774 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
4775 [ # # ]: 0 : if (cur_success)
4776 : : {
4777 : 0 : tot_succes += cur_success;
4778 : : /* recalculate InChI from the structure */
4779 [ # # ]: 0 : if (0 > ( ret = MakeOneInChIOutOfStrFromINChI2( pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
4780 : : ppt_group_info, ppat_norm, ppat_prep ) ))
4781 : : {
4782 : 0 : goto exit_function;
4783 : : }
4784 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr( pStruct ))) /* djb-rwth: addressing LLVM warning */
4785 : : {
4786 : 0 : goto exit_function;
4787 : : }
4788 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
4789 : : {
4790 : 0 : goto exit_function; /* no fixed-H found */
4791 : : }
4792 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI( pStruct, at2, pVA, pInChI, pc2i ))) /* djb-rwth: addressing LLVM warning */
4793 : : {
4794 : 0 : goto exit_function;
4795 : : }
4796 [ # # ]: 0 : if (!pc2i->bHasDifference)
4797 : : {
4798 : 0 : goto exit_function; /* nothing to do */
4799 : : }
4800 : : }
4801 : : }
4802 : : }
4803 : :
4804 [ # # # # : 0 : if (pc2i->len_c2at > 1 && pc2i->nNumTgRevrs && pc2i->nNumTgInChI)
# # ]
4805 : : {
4806 : : /*--------------------------------------------------------------*/
4807 : : /* case 20: restored: O(-)-AB=N- original: O=AB-N(-)- */
4808 : : /* FixH: 0 0 0 -1 */
4809 : : /* MobH: 0 0 0 1 */
4810 : : /* taut non-taut non-taut taut */
4811 : : /* or taut no H */
4812 : : /* no H */
4813 : : /* O = O, S, Se; N = N, O, S, Se, Te; */
4814 : : /* restored atoms are taut/non-taut; original are opposite. */
4815 : : /* Solution: move (-) from O(-) to =N- */
4816 : : /*--------------------------------------------------------------*/
4817 : 0 : int num_SB_O_Minus = 0, num_DB_N = 0, iat;
4818 : :
4819 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
4820 : : /*
4821 : : inp_ATOM *at_Mobile_H_Revrs = (pStruct->pOne_norm_data[1] &&
4822 : : pStruct->pOne_norm_data[1]->at)? pStruct->pOne_norm_data[1]->at : NULL;
4823 : : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H? pInChI[1]->nNum_H :
4824 : : pInChI[0] && pInChI[0]->nNum_H? pInChI[0]->nNum_H : 0;
4825 : : */
4826 : 0 : cur_success = 0;
4827 : :
4828 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
4829 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
4830 : : {
4831 : 0 : iat = pc2i->c2at[i].atomNumber;
4832 [ # # ]: 0 : if ( /* orig. InChI info: =O or -N= */
4833 : 0 : num_DB_N < MAX_DIFF_FIXH &&
4834 [ # # ]: 0 : pc2i->c2at[i].endptInChI &&
4835 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
4836 [ # # ]: 0 : pBNS->edge[e].flow == 0 &&
4837 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == 0 && pc2i->c2at[i].nMobHInChI == 0 &&
4838 : : /* if more than 1 t-group are in orig. InChI then do not move (-) to N */
4839 [ # # # # ]: 0 : (pc2i->nNumTgInChI == 1 || pc2i->c2at[i].nValElectr == 6) &&
4840 : : /* reversed structure info: */
4841 [ # # ]: 0 : !pc2i->c2at[i].endptRevrs &&
4842 [ # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && /*pc2i->c2at[i].nMobHRevrs == 0 &&*/
4843 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && !at2[iat].num_H &&
4844 [ # # ]: 0 : at2[iat].valence + 1 == at2[iat].chem_bonds_valence)
4845 : : {
4846 : 0 : iat_DB_N[num_DB_N++] = iat;
4847 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
4848 : : {
4849 : 0 : goto exit_function;
4850 : : }
4851 : : }
4852 : : else
4853 : : {
4854 [ # # ]: 0 : if ( /* orig. InChI info: -O(-) */
4855 : 0 : num_SB_O_Minus < MAX_DIFF_FIXH &&
4856 [ # # ]: 0 : !pc2i->c2at[i].endptInChI &&
4857 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
4858 [ # # ]: 0 : pBNS->edge[e].flow == 1 &&
4859 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == 0 && pc2i->c2at[i].nMobHInChI == 0 &&
4860 [ # # ]: 0 : pc2i->c2at[i].nValElectr == 6 &&
4861 : : /* reversed structure info: */
4862 [ # # ]: 0 : pc2i->c2at[i].endptRevrs &&
4863 [ # # # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 && pc2i->c2at[i].nMobHRevrs == 0 &&
4864 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == -1 && !at2[iat].num_H &&
4865 [ # # # # ]: 0 : at2[iat].valence == 1 && at2[iat].chem_bonds_valence == 1)
4866 : : {
4867 : 0 : iat_SB_O_Minus[num_SB_O_Minus++] = iat;
4868 : : }
4869 : : }
4870 : : }
4871 [ # # ]: 0 : if (!num_DB_N)
4872 : : {
4873 : : /* search among N that are tautomeric in both cases */
4874 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
4875 : : { /* i = canonical number - 1 */
4876 [ # # ]: 0 : if (!pStruct->endpoint[i])
4877 : : {
4878 : 0 : continue;
4879 : : }
4880 : 0 : iat = nCanon2AtnoRevrs[i];
4881 [ # # ]: 0 : if ( /* in restored atom O: charge=-1, no H, has no double bond, endpoint */
4882 : 0 : num_DB_N < MAX_DIFF_FIXH &&
4883 [ # # # # ]: 0 : at2[iat].charge == 0 && !at2[iat].num_H &&
4884 [ # # # # ]: 0 : at2[iat].valence + 1 == at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
4885 : : /* in orig.InChI: an endpoint, has no H */
4886 [ # # # # ]: 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
4887 : : /*!(nMobHInChI && nMobHInChI[i] ) &&*/
4888 : : /* has (-) edge */
4889 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
4890 [ # # ]: 0 : !pBNS->edge[e].flow)
4891 : : {
4892 : :
4893 : 0 : iat_DB_N[num_DB_N++] = iat;
4894 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
4895 : : {
4896 : 0 : goto exit_function;
4897 : : }
4898 : : }
4899 : : }
4900 : : }
4901 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_O_Minus, num_DB_N))) /* djb-rwth: addressing LLVM warning */
4902 : : {
4903 : : /* detected; attempt to fix */
4904 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
4905 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
4906 : 0 : delta = 1;
4907 [ # # # # ]: 0 : for (i = 0; i < num_SB_O_Minus && cur_success < num_try; i++)
4908 : : {
4909 : 0 : iat = iat_SB_O_Minus[i];
4910 : 0 : pe = pBNS->edge + pVA[iat].nCMinusGroupEdge - 1;
4911 [ # # ]: 0 : if (!pe->flow)
4912 : 0 : continue;
4913 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
4914 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
4915 : :
4916 : 0 : pe->flow -= delta;
4917 : 0 : pv1->st_edge.flow -= delta;
4918 : 0 : pv2->st_edge.flow -= delta;
4919 : 0 : pBNS->tot_st_flow -= 2 * delta;
4920 : :
4921 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
4922 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
4923 : :
4924 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
4925 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 1) /* djb-rwth: addressing LLVM warning */
# # ]
4926 : : {
4927 : : /* Added (-) charge to =N- => nDeltaCharge == 1 */
4928 : : /* Flow change on pe (-)charge edge (atom -O(-)) is not known to RunBnsTestOnce()) */
4929 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
4930 [ # # ]: 0 : if (ret > 0)
4931 : : {
4932 : 0 : nNumRunBNS++;
4933 : 0 : cur_success++; /* 20 */
4934 : : }
4935 : : }
4936 : : else
4937 : : {
4938 : 0 : pe->flow += delta;
4939 : 0 : pv1->st_edge.flow += delta;
4940 : 0 : pv2->st_edge.flow += delta;
4941 : 0 : pBNS->tot_st_flow += 2 * delta;
4942 : : }
4943 : : INCHI_HEAPCHK
4944 : : }
4945 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
4946 : : }
4947 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
4948 [ # # ]: 0 : if (cur_success)
4949 : : {
4950 : 0 : tot_succes += cur_success;
4951 : : /* recalculate InChI from the structure */
4952 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
4953 : : ppt_group_info, ppat_norm, ppat_prep)))
4954 : : {
4955 : 0 : goto exit_function;
4956 : : }
4957 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
4958 : : {
4959 : 0 : goto exit_function;
4960 : : }
4961 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
4962 : : {
4963 : 0 : goto exit_function; /* no fixed-H found */
4964 : : }
4965 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
4966 : : {
4967 : 0 : goto exit_function;
4968 : : }
4969 [ # # ]: 0 : if (!pc2i->bHasDifference)
4970 : : {
4971 : 0 : goto exit_function; /* nothing to do */
4972 : : }
4973 : : }
4974 : : }
4975 : :
4976 [ # # # # : 0 : if (pc2i->len_c2at && pc2i->nNumTgRevrs && pc2i->nNumTgHInChI && pStruct->endpoint)
# # # # ]
4977 : : {
4978 : : /*--------------------------------------------------------------*/
4979 : : /* O(-) O */
4980 : : /* | || */
4981 : : /* case 21: restored: R=S=O original: R-S=O */
4982 : : /* | | */
4983 : : /* O(-) O(-) */
4984 : : /* All O are taut R is not taut */
4985 : : /* */
4986 : : /* In addition, another atom O that should have been tautomeric */
4987 : : /* or has H(+) added in Mobile-H layer is not like that */
4988 : : /* O = O, S, Se; S=S, Se, Te */
4989 : : /* Solution: move (-) from O(-) to =O */
4990 : : /* these atoms are tautomeric in restored structure */
4991 : : /*--------------------------------------------------------------*/
4992 : 0 : int num_SB_O_Minus = 0, num_DB_O = 0, iat, iS;
4993 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
4994 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
4995 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
4996 : : /*
4997 : : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H? pInChI[1]->nNum_H :
4998 : : pInChI[0] && pInChI[0]->nNum_H? pInChI[0]->nNum_H : 0;
4999 : : */
5000 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
5001 : 0 : cur_success = 0;
5002 : :
5003 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
5004 : : {
5005 : 0 : iat = pc2i->c2at[i].atomNumber;
5006 [ # # ]: 0 : if ( /* orig. InChI info: =O */
5007 : 0 : num_DB_O < MAX_DIFF_FIXH &&
5008 [ # # ]: 0 : pc2i->c2at[i].nValElectr == 6 /* O, S, Se, Te */ &&
5009 [ # # # # ]: 0 : (pc2i->c2at[i].endptInChI || pc2i->c2at[i].nMobHInChI) &&
5010 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
5011 [ # # ]: 0 : pc2i->c2at[i].nFixHInChI == 0 && /*pc2i->c2at[i].nMobHInChI == 1 &&*/
5012 : : /* reversed structure info: */
5013 [ # # # # ]: 0 : !(pc2i->c2at[i].endptRevrs || pc2i->c2at[i].nMobHRevrs) &&
5014 [ # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 &&
5015 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && !at2[iat].num_H &&
5016 [ # # # # ]: 0 : at2[iat].valence == 1 && at2[iat].chem_bonds_valence == 2)
5017 : : {
5018 : 0 : iat_DB_O[num_DB_O++] = iat;
5019 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
5020 : : {
5021 : 0 : goto exit_function;
5022 : : }
5023 : : }
5024 : : }
5025 [ # # # # ]: 0 : for (i = 0; num_DB_O && i < pStruct->num_atoms; i++)
5026 : : {
5027 : : /* i = canonical number - 1 */
5028 [ # # ]: 0 : if (!pStruct->endpoint[i])
5029 : : {
5030 : 0 : continue;
5031 : : }
5032 : 0 : iat = nCanon2AtnoRevrs[i];
5033 [ # # ]: 0 : if ( /* in restored atom O: charge=-1, no H, has no double bond, endpoint */
5034 : 0 : num_SB_O_Minus < MAX_DIFF_FIXH &&
5035 [ # # # # ]: 0 : at2[iat].charge == -1 && !at2[iat].num_H &&
5036 [ # # # # : 0 : at2[iat].valence == 1 && at2[iat].chem_bonds_valence && !pVA[iat].cMetal &&
# # ]
5037 [ # # # # ]: 0 : pVA[iat].cNumValenceElectrons == 6 &&
5038 [ # # ]: 0 : (at_Mobile_H_Revrs && at_Mobile_H_Revrs[iat].endpoint) &&
5039 : : /* in orig.InChI: an endpoint, has no H */
5040 [ # # # # ]: 0 : !(pStruct->fixed_H && pStruct->fixed_H[i]) &&
5041 : : /*!(nMobHInChI && nMobHInChI[i] ) &&*/
5042 : : /* has (-) edge */
5043 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
5044 [ # # ]: 0 : pBNS->edge[e].flow)
5045 : : {
5046 : 0 : int nNumTautSB = 0, nNumTautDB = 0, nNumOtherDB = 0, nNumOtherSB = 0, nNumOthers = 0, nNumNegEndp = 0; /* djb-rwth: ignoring LLVM warning: variables used */
5047 : : /* traverse neighbors of the centerpoint iS */
5048 : 0 : iS = at2[i].neighbor[0];
5049 [ # # ]: 0 : for (j = 0; j < num_SB_O_Minus; j++)
5050 : : {
5051 [ # # ]: 0 : if (iat_Central[j] == iS)
5052 : 0 : break;
5053 : : }
5054 [ # # ]: 0 : if (j < num_SB_O_Minus)
5055 : : {
5056 : 0 : continue; /* have already been there */
5057 : : }
5058 [ # # ]: 0 : for (j = 0; j < at[iS].valence; j++)
5059 : : {
5060 : 0 : int bond_type = at2[iS].bond_type[j];
5061 : 0 : k = at2[iS].neighbor[j];
5062 [ # # ]: 0 : if (k == i)
5063 : : {
5064 : 0 : continue;
5065 : : }
5066 [ # # ]: 0 : if (pStruct->endpoint[k] == pStruct->endpoint[i])
5067 : : {
5068 : 0 : nNumTautSB += (bond_type == BOND_TYPE_SINGLE);
5069 : 0 : nNumTautDB += (bond_type == BOND_TYPE_DOUBLE);
5070 : : }
5071 : : else
5072 : : {
5073 [ # # ]: 0 : if (bond_type == BOND_TYPE_DOUBLE)
5074 : : {
5075 : 0 : nNumOtherDB++;
5076 : : }
5077 : : else
5078 : : {
5079 [ # # ]: 0 : if (bond_type == BOND_TYPE_SINGLE)
5080 : : {
5081 : 0 : nNumOtherSB++;
5082 : : }
5083 : : else
5084 : : {
5085 : 0 : nNumOthers++;
5086 : : }
5087 : : }
5088 : : }
5089 [ # # # # ]: 0 : if (at2[k].endpoint == at2[i].endpoint && at2[k].valence == 1 &&
5090 [ # # # # ]: 0 : at2[k].charge == -1 && pVA[k].cNumValenceElectrons == 6)
5091 : : {
5092 : 0 : nNumNegEndp++;
5093 : : }
5094 : : }
5095 [ # # ]: 0 : if (!nNumTautSB)
5096 : : {
5097 : 0 : continue;
5098 : : }
5099 [ # # # # ]: 0 : if (!(nNumOtherDB && nNumTautDB))
5100 : : {
5101 : 0 : continue; /* ignore */
5102 : : }
5103 : :
5104 : 0 : iat_SB_O_Minus[num_SB_O_Minus] = iat;
5105 : 0 : iat_Central[num_SB_O_Minus++] = iS;
5106 : : }
5107 : : }
5108 [ # # ]: 0 : if ((num_try = inchi_min(num_SB_O_Minus, num_DB_O))) /* djb-rwth: addressing LLVM warning */
5109 : : {
5110 : : /* detected; attempt to fix */
5111 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
5112 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
5113 : 0 : delta = 1;
5114 [ # # # # ]: 0 : for (i = 0; i < num_SB_O_Minus && cur_success < num_try; i++)
5115 : : {
5116 : 0 : iat = iat_SB_O_Minus[i];
5117 : 0 : pe = pBNS->edge + pVA[iat].nCMinusGroupEdge - 1;
5118 [ # # ]: 0 : if (!pe->flow)
5119 : 0 : continue;
5120 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
5121 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
5122 : :
5123 : 0 : pe->forbidden |= forbidden_edge_mask;
5124 : 0 : pe->flow -= delta;
5125 : 0 : pv1->st_edge.flow -= delta;
5126 : 0 : pv2->st_edge.flow -= delta;
5127 : 0 : pBNS->tot_st_flow -= 2 * delta;
5128 : :
5129 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
5130 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
5131 : :
5132 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
5133 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 1) /* djb-rwth: addressing LLVM warning */
# # ]
5134 : : {
5135 : : /* Added (-) charge to =O => nDeltaCharge == 1 */
5136 : : /* Flow change on pe (-)charge edge (atom -N(-)-) is not known to RunBnsTestOnce()) */
5137 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
5138 [ # # ]: 0 : if (ret > 0)
5139 : : {
5140 : 0 : nNumRunBNS++;
5141 : 0 : cur_success++; /* 21 */
5142 : : }
5143 : : }
5144 : : else
5145 : : {
5146 : 0 : pe->flow += delta;
5147 : 0 : pv1->st_edge.flow += delta;
5148 : 0 : pv2->st_edge.flow += delta;
5149 : 0 : pBNS->tot_st_flow += 2 * delta;
5150 : : }
5151 : : INCHI_HEAPCHK
5152 : : }
5153 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
5154 : : }
5155 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
5156 [ # # ]: 0 : if (cur_success)
5157 : : {
5158 : 0 : tot_succes += cur_success;
5159 : : /* recalculate InChI from the structure */
5160 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
5161 : : ppt_group_info, ppat_norm, ppat_prep)))
5162 : : {
5163 : 0 : goto exit_function;
5164 : : }
5165 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
5166 : : {
5167 : 0 : goto exit_function;
5168 : : }
5169 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
5170 : : {
5171 : 0 : goto exit_function; /* no fixed-H found */
5172 : : }
5173 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
5174 : : {
5175 : 0 : goto exit_function;
5176 : : }
5177 [ # # ]: 0 : if (!pc2i->bHasDifference)
5178 : : {
5179 : 0 : goto exit_function; /* nothing to do */
5180 : : }
5181 : : }
5182 : : }
5183 : :
5184 [ # # # # : 0 : if (pc2i->len_c2at && pc2i->nNumTgRevrs && pc2i->nNumEndpInChI < pc2i->nNumEndpRevrs)
# # ]
5185 : : {
5186 : : /*--------------------------------------------------------------*/
5187 : : /* O O */
5188 : : /* || || */
5189 : : /* case 21a:restored: R=S-R' =X original: R-S-R' -X(-) */
5190 : : /* | || */
5191 : : /* O(-) O(-) */
5192 : : /* All O and X are taut O and X are not taut */
5193 : : /* it is possible that X is R */
5194 : : /* */
5195 : : /* O = O, S, Se; S=S, Se, Te; X = N, O, S, Se, Te */
5196 : : /* Solution: move (-) from O(-) to =X */
5197 : : /* these atoms are tautomeric in restored structure */
5198 : : /*--------------------------------------------------------------*/
5199 : : int iat, iS;
5200 : : /*
5201 : : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
5202 : : */
5203 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
5204 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
5205 : : /*
5206 : : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H? pInChI[1]->nNum_H :
5207 : : pInChI[0] && pInChI[0]->nNum_H? pInChI[0]->nNum_H : 0;
5208 : : */
5209 : : EDGE_LIST OtherSO, CentralS, SOMinus, MinusAcceptord;
5210 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
5211 : 0 : AllocEdgeList( &OtherSO, EDGE_LIST_CLEAR );
5212 : 0 : AllocEdgeList( &CentralS, EDGE_LIST_CLEAR );
5213 : 0 : AllocEdgeList( &SOMinus, EDGE_LIST_CLEAR );
5214 : 0 : AllocEdgeList( &MinusAcceptord, EDGE_LIST_CLEAR );
5215 : 0 : cur_success = 0;
5216 [ # # ]: 0 : if (!at_Mobile_H_Revrs)
5217 : : {
5218 : 0 : goto exit_case_21a;
5219 : : }
5220 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
5221 : : {
5222 : 0 : iat = pc2i->c2at[i].atomNumber;
5223 : 0 : if ( /* orig. InChI info: -X(-) */
5224 : : /*num_DB_O < MAX_DIFF_FIXH &&*/
5225 : : /*pc2i->c2at[i].nValElectr == 6 */ /* O, S, Se, Te */
5226 [ # # ]: 0 : !pc2i->c2at[i].endptInChI &&
5227 [ # # # # ]: 0 : ( e = pVA[iat].nCMinusGroupEdge - 1 ) >= 0 && !pBNS->edge[e].forbidden &&
5228 [ # # ]: 0 : pc2i->c2at[i].nFixHInChI == 0 && /*pc2i->c2at[i].nMobHInChI == 1 &&*/
5229 : : /* reversed structure info: */
5230 [ # # # # ]: 0 : ( pc2i->c2at[i].endptRevrs || pc2i->c2at[i].nMobHRevrs ) &&
5231 [ # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 &&
5232 [ # # ]: 0 : /*pc2i->c2at[i].nAtChargeRevrs == 0 &&*/ !at2[iat].num_H)
5233 : : {
5234 [ # # # # ]: 0 : if (pVA[iat].cNumValenceElectrons == 6 && at2[iat].charge == -1 &&
5235 [ # # ]: 0 : pBNS->edge[e].flow &&
5236 [ # # # # ]: 0 : at2[iat].valence == 1 && at2[iat].chem_bonds_valence == 1 &&
5237 [ # # # # ]: 0 : pVA[iS = (int) at2[iat].neighbor[0]].cNumValenceElectrons == 6 && pVA[iS].cPeriodicRowNumber > 1 &&
5238 [ # # ]: 0 : at2[iS].valence >= 4)
5239 : 0 : {
5240 : : /* a candidate for S in -SO2- */
5241 : 0 : int nNumTautSB = 0, nNumTautDB = 0, nNumOtherDB = 0, nNumOtherSB = 0; /* djb-rwth: ignoring LLVM warning: variables used */
5242 : 0 : int nNumOthers = 0, nNumNegEndp = 0, nNumEndpO = 0;
5243 : : /* check whether we have already found it */
5244 [ # # ]: 0 : if (0 <= FindInEdgeList( &CentralS, iS ))
5245 : : {
5246 : 0 : continue;
5247 : : }
5248 [ # # ]: 0 : for (j = 0; j < at[iS].valence; j++)
5249 : : {
5250 : 0 : int bond_type = at2[iS].bond_type[j];
5251 : 0 : k = at2[iS].neighbor[j];
5252 [ # # ]: 0 : if (k == iat)
5253 : : {
5254 : 0 : continue;
5255 : : }
5256 [ # # # # ]: 0 : if (pc2i->c2at[i].endptRevrs == at_Mobile_H_Revrs[k].endpoint && !at2[k].endpoint)
5257 : : {
5258 : 0 : nNumTautSB += ( bond_type == BOND_TYPE_SINGLE );
5259 : 0 : nNumTautDB += ( bond_type == BOND_TYPE_DOUBLE );
5260 [ # # # # ]: 0 : nNumEndpO += ( pVA[k].cNumValenceElectrons == 6 && at2[k].valence == 1 );
5261 : : }
5262 : : else
5263 : : {
5264 [ # # ]: 0 : if (bond_type == BOND_TYPE_DOUBLE)
5265 : : {
5266 : 0 : nNumOtherDB++;
5267 : : }
5268 : : else
5269 : : {
5270 [ # # ]: 0 : if (bond_type == BOND_TYPE_SINGLE)
5271 : : {
5272 : 0 : nNumOtherSB++;
5273 : : }
5274 : : else
5275 : : {
5276 : 0 : nNumOthers++;
5277 : : }
5278 : : }
5279 : : }
5280 [ # # # # ]: 0 : if (at2[k].endpoint == at2[i].endpoint && at2[k].valence == 1 &&
5281 [ # # # # ]: 0 : at2[k].charge == -1 && pVA[k].cNumValenceElectrons == 6)
5282 : : {
5283 : 0 : nNumNegEndp++;
5284 : : }
5285 : : }
5286 [ # # ]: 0 : if (!nNumEndpO)
5287 : : {
5288 : 0 : continue;
5289 : : }
5290 [ # # ]: 0 : if (nNumTautSB + nNumTautDB + nNumOtherDB <= nNumEndpO)
5291 : : {
5292 : 0 : continue; /* ignore */
5293 : : }
5294 : : /* collect double bond taut =O */
5295 [ # # ]: 0 : for (j = 0; j < at[iS].valence; j++)
5296 : : {
5297 : 0 : int bond_type = at2[iS].bond_type[j];
5298 : 0 : k = at2[iS].neighbor[j];
5299 [ # # ]: 0 : if (pc2i->c2at[i].endptRevrs == at_Mobile_H_Revrs[k].endpoint &&
5300 [ # # # # : 0 : !at2[k].endpoint && pVA[k].cNumValenceElectrons == 6 && at2[k].valence == 1 &&
# # ]
5301 [ # # # # ]: 0 : 0 <= ( e = pVA[k].nCMinusGroupEdge - 1 ) && !pBNS->edge[e].forbidden)
5302 : : {
5303 [ # # # # : 0 : if (bond_type == BOND_TYPE_DOUBLE && !at2[k].charge && !pBNS->edge[e].flow)
# # ]
5304 : : {
5305 : : /* charges to be unchanged */
5306 [ # # ]: 0 : if ((ret = AddToEdgeList( &OtherSO, e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
5307 : : {
5308 : 0 : goto exit_case_21a;
5309 : : }
5310 : : }
5311 : : else
5312 : : {
5313 [ # # # # : 0 : if (bond_type == BOND_TYPE_SINGLE && at2[k].charge == -1 && pBNS->edge[e].flow)
# # ]
5314 : : {
5315 : : /* charges to be removed */
5316 [ # # ]: 0 : if ((ret = AddToEdgeList( &SOMinus, e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
5317 : : {
5318 : 0 : goto exit_case_21a;
5319 : : }
5320 : : }
5321 : : }
5322 : : }
5323 : : }
5324 [ # # ]: 0 : if ((ret = AddToEdgeList( &CentralS, iS, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
5325 : : {
5326 : 0 : goto exit_case_21a;
5327 : : }
5328 : : }
5329 : : else
5330 [ # # # # ]: 0 : if (at2[iat].charge == 0 && !pBNS->edge[e].flow &&
5331 [ # # ]: 0 : at2[iat].valence + 1 == at2[iat].chem_bonds_valence)
5332 : : {
5333 : : /* changeable charges */
5334 [ # # ]: 0 : if ((ret = AddToEdgeList( &CurrEdges, e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
5335 : : {
5336 : 0 : goto exit_function;
5337 : : }
5338 : : }
5339 : : }
5340 : : }
5341 : : /* remove unchangeable from changeable */
5342 [ # # ]: 0 : for (i = 0; i < OtherSO.num_edges; i++)
5343 : : {
5344 : 0 : RemoveFromEdgeListByValue( &CurrEdges, OtherSO.pnEdges[i] );
5345 : : }
5346 : :
5347 [ # # ]: 0 : if ((num_try = inchi_min( SOMinus.num_edges, CurrEdges.num_edges ))) /* djb-rwth: addressing LLVM warning */
5348 : : {
5349 : : /* detected; attempt to fix */
5350 : 0 : SetForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
5351 : 0 : RemoveForbiddenEdgeMask( pBNS, &CurrEdges, forbidden_edge_mask );
5352 : 0 : delta = 1;
5353 [ # # # # ]: 0 : for (i = 0; i < SOMinus.num_edges && cur_success < num_try; i++)
5354 : : {
5355 : 0 : pe = pBNS->edge + SOMinus.pnEdges[i];
5356 [ # # ]: 0 : if (!pe->flow)
5357 : 0 : continue;
5358 : 0 : pv1 = pBNS->vert + ( v1 = pe->neighbor1 );
5359 : 0 : pv2 = pBNS->vert + ( v2 = pe->neighbor12 ^ v1 );
5360 : :
5361 : : /*pe->forbidden |= forbidden_edge_mask;*/
5362 : 0 : pe->flow -= delta;
5363 : 0 : pv1->st_edge.flow -= delta;
5364 : 0 : pv2->st_edge.flow -= delta;
5365 : 0 : pBNS->tot_st_flow -= 2 * delta;
5366 : :
5367 : 0 : ret = RunBnsTestOnce( pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
5368 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms );
5369 : :
5370 [ # # # # : 0 : if (ret == 1 && ( (vPathEnd == v1 && vPathStart == v2) ||
# # ]
5371 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1) ) && nDeltaCharge == 1) /* djb-rwth: addressing LLVM warnings */
# # ]
5372 : : {
5373 : : /* Added (-) charge to =O => nDeltaCharge == 1 */
5374 : : /* Flow change on pe (-)charge edge (atom -N(-)-) is not known to RunBnsTestOnce()) */
5375 : 0 : ret = RunBnsRestoreOnce( pBNS, pBD, pVA, pTCGroups );
5376 [ # # ]: 0 : if (ret > 0)
5377 : : {
5378 : 0 : nNumRunBNS++;
5379 : 0 : cur_success++; /* 21a */
5380 : : }
5381 : : }
5382 : : else
5383 : : {
5384 : 0 : pe->flow += delta;
5385 : 0 : pv1->st_edge.flow += delta;
5386 : 0 : pv2->st_edge.flow += delta;
5387 : 0 : pBNS->tot_st_flow += 2 * delta;
5388 : : }
5389 : : INCHI_HEAPCHK
5390 : : }
5391 : 0 : RemoveForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
5392 : : }
5393 : 0 : exit_case_21a:
5394 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
5395 : 0 : AllocEdgeList( &OtherSO, EDGE_LIST_FREE );
5396 : 0 : AllocEdgeList( &CentralS, EDGE_LIST_FREE );
5397 : 0 : AllocEdgeList( &SOMinus, EDGE_LIST_FREE );
5398 : 0 : AllocEdgeList( &MinusAcceptord, EDGE_LIST_FREE );
5399 [ # # ]: 0 : if (cur_success)
5400 : : {
5401 : 0 : tot_succes += cur_success;
5402 : : /* recalculate InChI from the structure */
5403 [ # # ]: 0 : if (0 > ( ret = MakeOneInChIOutOfStrFromINChI2( pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
5404 : : ppt_group_info, ppat_norm, ppat_prep ) ))
5405 : : {
5406 : 0 : goto exit_function;
5407 : : }
5408 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr( pStruct ))) /* djb-rwth: addressing LLVM warning */
5409 : : {
5410 : 0 : goto exit_function;
5411 : : }
5412 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
5413 : : {
5414 : 0 : goto exit_function; /* no fixed-H found */
5415 : : }
5416 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI( pStruct, at2, pVA, pInChI, pc2i ))) /* djb-rwth: addressing LLVM warning */
5417 : : {
5418 : 0 : goto exit_function;
5419 : : }
5420 [ # # ]: 0 : if (!pc2i->bHasDifference)
5421 : : {
5422 : 0 : goto exit_function; /* nothing to do */
5423 : : }
5424 : : }
5425 : : }
5426 : :
5427 [ # # ]: 0 : if (pc2i->len_c2at)
5428 : : {
5429 : : /*------------------------------------------------------------------*/
5430 : : /* case 22: restored: N(-)=N(+)=C...=O orig: N#N-N=...-O(-) */
5431 : : /* im InChI -O(-) may have H(+) added by Normalization */
5432 : : /* or may be tautomeric */
5433 : : /* Solution: move (-) from N(-) to =O */
5434 : : /* */
5435 : : /*------------------------------------------------------------------*/
5436 : 0 : int num_DB_O = 0, iat;
5437 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
5438 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
5439 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
5440 : : int iN2, iC;
5441 : : BNS_EDGE *peDB_O_Minus;
5442 : : /*
5443 : : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H? pInChI[1]->nNum_H :
5444 : : pInChI[0] && pInChI[0]->nNum_H? pInChI[0]->nNum_H : 0;
5445 : : */
5446 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
5447 : 0 : cur_success = 0;
5448 : :
5449 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
5450 : : {
5451 : 0 : iat = pc2i->c2at[i].atomNumber;
5452 [ # # ]: 0 : if ( /* orig. InChI info: =O */
5453 : 0 : num_DB_O < MAX_DIFF_FIXH &&
5454 [ # # ]: 0 : pc2i->c2at[i].nValElectr == 6 /* O, S, Se, Te */ &&
5455 [ # # # # ]: 0 : (pc2i->c2at[i].endptInChI || pc2i->c2at[i].nMobHInChI) &&
5456 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
5457 [ # # ]: 0 : pc2i->c2at[i].nFixHInChI == 0 && /*pc2i->c2at[i].nMobHInChI == 1 &&*/
5458 : : /* reversed structure info: */
5459 [ # # # # ]: 0 : !(pc2i->c2at[i].endptRevrs || pc2i->c2at[i].nMobHRevrs) &&
5460 [ # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 &&
5461 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && !at2[iat].num_H &&
5462 [ # # # # ]: 0 : at2[iat].valence == 1 && at2[iat].chem_bonds_valence == 2)
5463 : : {
5464 : 0 : iat_DB_O[num_DB_O++] = iat;
5465 : : }
5466 : : }
5467 [ # # # # ]: 0 : for (i = 0; num_DB_O && i < pStruct->num_atoms; i++)
5468 : : {
5469 : : /* i = canonical number - 1 */
5470 : 0 : iat = nCanon2AtnoRevrs[i];
5471 : 0 : if ( /* in restored atom O: charge=-1, no H, has no double bond, endpoint */
5472 [ # # # # ]: 0 : at2[iat].charge == -1 && !at2[iat].num_H &&
5473 [ # # # # : 0 : at2[iat].valence == 1 && at2[iat].chem_bonds_valence == 2 && !pVA[iat].cMetal &&
# # ]
5474 [ # # ]: 0 : pVA[iat].cNumValenceElectrons == 5 &&
5475 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
5476 [ # # # # ]: 0 : pBNS->edge[e].flow &&
5477 [ # # ]: 0 : !(at_Mobile_H_Revrs && at_Mobile_H_Revrs[iat].endpoint) &&
5478 [ # # ]: 0 : pVA[iN2 = at2[iat].neighbor[0]].cNumValenceElectrons == 5 &&
5479 [ # # ]: 0 : at2[iat].bond_type[0] == BOND_TYPE_DOUBLE &&
5480 [ # # # # : 0 : at2[iN2].charge == 1 && at2[iN2].valence == 2 && at2[iN2].chem_bonds_valence == 4 &&
# # ]
5481 [ # # ]: 0 : pVA[iC = at2[iN2].neighbor[at2[iN2].neighbor[0] == iN2]].cNumValenceElectrons == 4) /* djb-rwth: ignoring LLVM warning: variable used */
5482 : : {
5483 : :
5484 [ # # ]: 0 : if ((ret = AddToEdgeList(&CurrEdges, e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
5485 : : {
5486 : 0 : goto exit_function;
5487 : : }
5488 : : }
5489 : : }
5490 [ # # ]: 0 : if ((num_try = inchi_min(CurrEdges.num_edges, num_DB_O))) /* djb-rwth: addressing LLVM warning */
5491 : : {
5492 : : /* detected; attempt to fix */
5493 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
5494 : 0 : RemoveForbiddenEdgeMask(pBNS, &CurrEdges, forbidden_edge_mask);
5495 : 0 : delta = 1;
5496 [ # # # # ]: 0 : for (i = 0; i < num_DB_O && cur_success < num_try; i++)
5497 : : {
5498 : 0 : iat = iat_DB_O[i];
5499 : :
5500 : 0 : peDB_O_Minus = pBNS->edge + ((long long)pVA[iat].nCMinusGroupEdge - 1); /* djb-rwth: cast operator added */
5501 : 0 : pe = pBNS->edge + pBNS->vert[iat].iedge[0];
5502 [ # # ]: 0 : if (!pe->flow)
5503 : 0 : continue;
5504 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
5505 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
5506 : :
5507 : 0 : pe->forbidden |= forbidden_edge_mask;
5508 : 0 : peDB_O_Minus->forbidden &= forbidden_edge_mask_inv;
5509 : :
5510 : 0 : pe->flow -= delta;
5511 : 0 : pv1->st_edge.flow -= delta;
5512 : 0 : pv2->st_edge.flow -= delta;
5513 : 0 : pBNS->tot_st_flow -= 2 * delta;
5514 : :
5515 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
5516 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
5517 : :
5518 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
5519 [ # # # # : 0 : (vPathEnd == v2 && vPathStart == v1)) && nDeltaCharge == 0) /* djb-rwth: addressing LLVM warnings */
# # ]
5520 : : {
5521 : : /* Added (-) charge to =O and removed from =N(-) => nDeltaCharge == 0 */
5522 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
5523 [ # # ]: 0 : if (ret > 0)
5524 : : {
5525 : 0 : nNumRunBNS++;
5526 : 0 : cur_success++; /* 22 */
5527 : : }
5528 : : }
5529 : : else
5530 : : {
5531 : 0 : pe->flow += delta;
5532 : 0 : pv1->st_edge.flow += delta;
5533 : 0 : pv2->st_edge.flow += delta;
5534 : 0 : pBNS->tot_st_flow += 2 * delta;
5535 : : }
5536 : : INCHI_HEAPCHK
5537 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
5538 : 0 : peDB_O_Minus->forbidden |= forbidden_edge_mask;
5539 : : }
5540 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
5541 : : }
5542 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
5543 [ # # ]: 0 : if (cur_success)
5544 : : {
5545 : 0 : tot_succes += cur_success;
5546 : : /* recalculate InChI from the structure */
5547 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
5548 : : ppt_group_info, ppat_norm, ppat_prep)))
5549 : : {
5550 : 0 : goto exit_function;
5551 : : }
5552 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct))) /* djb-rwth: addressing LLVM warning */
5553 : : {
5554 : 0 : goto exit_function;
5555 : : }
5556 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
5557 : : {
5558 : 0 : goto exit_function; /* no fixed-H found */
5559 : : }
5560 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
5561 : : {
5562 : 0 : goto exit_function;
5563 : : }
5564 [ # # ]: 0 : if (!pc2i->bHasDifference)
5565 : : {
5566 : 0 : goto exit_function; /* nothing to do */
5567 : : }
5568 : : }
5569 : : }
5570 : :
5571 [ # # # # ]: 0 : if (pc2i->len_c2at && pc2i->nNumTgInChI == 1)
5572 : : {
5573 : : /*------------------------------------------------------------------*/
5574 : : /* case 23: -NO2 are to be tautomeric but they are not AND */
5575 : : /* InChI has a SINGLE tautomeric group */
5576 : : /* */
5577 : : /* (-)O (-)O */
5578 : : /* Solution: convert \ \ */
5579 : : /* N-X=...-Z(-) => N(+)=X- ...=Z */
5580 : : /* // / */
5581 : : /* O (-)O */
5582 : : /* */
5583 : : /* O O */
5584 : : /* or \\ \\ */
5585 : : /* N-X=...-Z(-) => N=X- ...=Z */
5586 : : /* // / */
5587 : : /* O (-)O */
5588 : : /* */
5589 : : /* */
5590 : : /* (a) move (-) from other tautomeric atom to O in O=N-X */
5591 : : /* or from other atom that has to be tautomeric */
5592 : : /* but is not */
5593 : : /* (b) create (+) [ion pair creation] on N as in */
5594 : : /* */
5595 : : /* OH OH */
5596 : : /* / / */
5597 : : /* -C=N => =C-N(+) */
5598 : : /* \\ \\ */
5599 : : /* O O */
5600 : : /* */
5601 : : /*------------------------------------------------------------------*/
5602 : 0 : int num_DB_O = 0, iat;
5603 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
5604 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
5605 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
5606 : : /*
5607 : : inp_ATOM *atfMobile_H_Revrs = (pStruct->pOne_norm_data[1] &&
5608 : : pStruct->pOne_norm_data[1]->at_fixed_bonds)?
5609 : : pStruct->pOne_norm_data[1]->at_fixed_bonds : NULL;
5610 : : */
5611 : 0 : S_CHAR *num_Fixed_H_Revrs = pStruct->pOneINChI[0]->nNum_H_fixed ? pStruct->pOneINChI[0]->nNum_H_fixed : NULL;
5612 [ # # ]: 0 : S_CHAR *pnMobHRevrs = ( pStruct->pOneINChI[1] && pStruct->pOneINChI[1]->nNum_H ) ?
5613 [ # # ]: 0 : pStruct->pOneINChI[1]->nNum_H :
5614 [ # # ]: 0 : ( pStruct->pOneINChI[0] && pStruct->pOneINChI[0]->nNum_H ) ?
5615 [ # # ]: 0 : pStruct->pOneINChI[0]->nNum_H : NULL;
5616 : : int iN, one_success;
5617 : : BNS_EDGE *peDB_O_Minus;
5618 : : int neigh, nNumO, nNumOthers;
5619 : : #define CHG_SET_NOOH 0
5620 : : #define CHG_SET_WRONG_TAUT 1
5621 : : #define CHG_SET_TAUT 2
5622 : : #define CHG_LAST_SET 2 /* the last index in trying */
5623 : : #define CHG_SET_O_FIXED 3
5624 : : #define CHG_SET_NUM 4
5625 : : EDGE_LIST ChangeableEdges[CHG_SET_NUM];
5626 : 0 : memset( ChangeableEdges, 0, sizeof( ChangeableEdges ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5627 : : /* equivalent to AllocEdgeList( &EdgeList, EDGE_LIST_CLEAR ); */
5628 : : /*
5629 : : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H? pInChI[1]->nNum_H :
5630 : : pInChI[0] && pInChI[0]->nNum_H? pInChI[0]->nNum_H : 0;
5631 : : */
5632 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
5633 : 0 : cur_success = 0;
5634 : :
5635 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
5636 : : {
5637 : 0 : iat = pc2i->c2at[i].atomNumber;
5638 [ # # ]: 0 : if ( /* orig. InChI info: taut in orig. InChI =O located in -NO2 that is not taut in Reconstructed InChI */
5639 : 0 : num_DB_O < MAX_DIFF_FIXH &&
5640 [ # # ]: 0 : pc2i->c2at[i].nValElectr == 6 /* O, S, Se, Te */ &&
5641 [ # # ]: 0 : (pc2i->c2at[i].endptInChI /*|| pc2i->c2at[i].nMobHInChI*/) &&
5642 [ # # # # ]: 0 : (e = pVA[iat].nCMinusGroupEdge - 1) >= 0 && !pBNS->edge[e].forbidden &&
5643 [ # # ]: 0 : pc2i->c2at[i].nFixHInChI == 0 && /*pc2i->c2at[i].nMobHInChI == 1 &&*/
5644 : : /* reversed structure info: */
5645 [ # # ]: 0 : !(pc2i->c2at[i].endptRevrs /*|| pc2i->c2at[i].nMobHRevrs*/) &&
5646 [ # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 &&
5647 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && !at2[iat].num_H &&
5648 [ # # # # ]: 0 : at2[iat].valence == 1 && at2[iat].chem_bonds_valence == 2 &&
5649 : : /* find whether it belongs to NO2 */
5650 [ # # ]: 0 : pVA[iN = at2[iat].neighbor[0]].cNumValenceElectrons == 5 &&
5651 [ # # # # : 0 : at2[iN].valence == 3 && (at2[iN].charge == 0 || at2[iN].charge == 1) &&
# # ]
5652 [ # # ]: 0 : at2[iN].chem_bonds_valence == 5 - at2[iN].charge)
5653 : : {
5654 : : /* find the second O */
5655 : 0 : nNumO = nNumOthers = 0;
5656 [ # # ]: 0 : for (k = 0; k < at2[iN].valence; k++)
5657 : : {
5658 : 0 : neigh = at2[iN].neighbor[k];
5659 [ # # ]: 0 : if (neigh == iat)
5660 : : {
5661 : 0 : continue;
5662 : : }
5663 [ # # ]: 0 : if (pVA[neigh].cNumValenceElectrons == 6 &&
5664 [ # # # # ]: 0 : pStruct->endpoint[neigh] &&
5665 [ # # ]: 0 : !(at_Mobile_H_Revrs && at_Mobile_H_Revrs[neigh].endpoint) &&
5666 [ # # # # ]: 0 : at2[neigh].valence == 1 && at2[neigh].num_H == 0 &&
5667 [ # # # # : 0 : at2[neigh].radical == 0 && (at2[neigh].charge == 0 || at2[neigh].charge == -1) &&
# # ]
5668 [ # # ]: 0 : at2[neigh].chem_bonds_valence - at2[neigh].charge == 2)
5669 : : {
5670 : 0 : nNumO++;
5671 : : }
5672 : : else
5673 : : {
5674 [ # # ]: 0 : if (at2[iN].bond_type[k] == BOND_TYPE_SINGLE &&
5675 [ # # ]: 0 : at2[neigh].valence > 1 &&
5676 [ # # ]: 0 : at2[neigh].valence < at2[neigh].chem_bonds_valence)
5677 : : {
5678 : 0 : nNumOthers++;
5679 : : }
5680 : : }
5681 : : }
5682 [ # # # # ]: 0 : if (nNumO != 1 || nNumOthers != 1)
5683 : : {
5684 : 0 : continue;
5685 : : }
5686 [ # # ]: 0 : for (k = 0; k < num_DB_O; k++)
5687 : : {
5688 [ # # ]: 0 : if (iat_NO2[k] == iN)
5689 : : {
5690 : 0 : break;
5691 : : }
5692 : : }
5693 [ # # ]: 0 : if (k == num_DB_O)
5694 : : {
5695 : 0 : iat_NO2[num_DB_O] = iN;
5696 : 0 : iat_DB_O[num_DB_O++] = iat;
5697 : : }
5698 : : /* save the edge to avoid interference */
5699 [ # # ]: 0 : if ((ret = AddToEdgeList(&ChangeableEdges[CHG_SET_O_FIXED], e, INC_ADD_EDGE))) /* djb-rwth: addressing LLVM warning */
5700 : : {
5701 : 0 : goto exit_case_23;
5702 : : }
5703 : : }
5704 : : }
5705 [ # # ]: 0 : if (num_DB_O)
5706 : : {
5707 : : /* 1. search for =N(=O)-OH; assume =N(+)(-O(-))(-OH) does not happen */
5708 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
5709 : : {
5710 : : /* i = canonical number - 1 */
5711 : : /* find O=N(V) */
5712 : 0 : iat = nCanon2AtnoRevrs[i];
5713 [ # # # # ]: 0 : if (!pStruct->endpoint[i] || pVA[i].cNumValenceElectrons != 6 ||
5714 [ # # # # ]: 0 : at2[iat].valence != 1 || at2[iat].charge ||
5715 [ # # ]: 0 : 0 > (e = pVA[iat].nCMinusGroupEdge - 1) ||
5716 [ # # ]: 0 : at2[iat].num_H + at2[iat].chem_bonds_valence != 2 ||
5717 [ # # ]: 0 : pVA[iN = at2[iat].neighbor[0]].cNumValenceElectrons != 5 ||
5718 [ # # ]: 0 : 0 > (e = pVA[iN].nCPlusGroupEdge - 1) ||
5719 [ # # # # ]: 0 : pBNS->edge[e].forbidden || !pBNS->edge[e].flow ||
5720 [ # # # # : 0 : at2[iN].charge || at2[iN].valence != 3 || at2[iN].chem_bonds_valence != 5) /* djb-rwth: ignoring LLVM warning: variable used */
# # ]
5721 : : {
5722 : 0 : continue;
5723 : : }
5724 : : /* find the second O, -OH */
5725 : 0 : nNumO = nNumOthers = 0;
5726 [ # # ]: 0 : for (k = 0; k < at2[iN].valence; k++)
5727 : : {
5728 : 0 : neigh = at2[iN].neighbor[k];
5729 [ # # ]: 0 : if (neigh == iat)
5730 : : {
5731 : 0 : continue;
5732 : : }
5733 [ # # ]: 0 : if (pVA[neigh].cNumValenceElectrons == 6 &&
5734 [ # # ]: 0 : pStruct->endpoint[neigh] &&
5735 [ # # # # ]: 0 : at2[neigh].valence == 1 && at2[neigh].num_H == 1 &&
5736 [ # # # # ]: 0 : at2[neigh].radical == 0 && (at2[neigh].charge == 0))
5737 : : {
5738 : 0 : nNumO++;
5739 : : }
5740 : : else
5741 [ # # ]: 0 : if (at2[iN].bond_type[k] == BOND_TYPE_DOUBLE &&
5742 [ # # ]: 0 : at2[neigh].valence >= 2 &&
5743 [ # # ]: 0 : at2[neigh].valence < at2[neigh].chem_bonds_valence)
5744 : : {
5745 : 0 : nNumOthers++;
5746 : : }
5747 : : }
5748 [ # # # # ]: 0 : if (nNumO != 1 || nNumOthers != 1)
5749 : : {
5750 : 0 : continue;
5751 : : }
5752 : : /* save edges to be changed */
5753 [ # # # # ]: 0 : if ((ret = AddToEdgeList(&ChangeableEdges[CHG_SET_NOOH], e, INC_ADD_EDGE)) ||
5754 : 0 : (ret = AddToEdgeList(&ChangeableEdges[CHG_SET_O_FIXED], e, INC_ADD_EDGE)))
5755 : : {
5756 : 0 : goto exit_case_23;
5757 : : }
5758 [ # # # # ]: 0 : if (NO_VERTEX != (j = GetChargeFlowerUpperEdge(pBNS, pVA, e)) &&
5759 [ # # ]: 0 : ((ret = AddToEdgeList(&ChangeableEdges[CHG_SET_NOOH], j, INC_ADD_EDGE)) ||
5760 : 0 : (ret = AddToEdgeList(&ChangeableEdges[CHG_SET_O_FIXED], e, INC_ADD_EDGE))))
5761 : : {
5762 : 0 : goto exit_case_23;
5763 : : }
5764 : : }
5765 : : /* 2. search for (-) atoms that are tautomeric but should not be */
5766 : : /* or that got H from Normalization but they shouldn't */
5767 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
5768 : : { /* i = canonical number - 1 */
5769 : 0 : iat = nCanon2AtnoRevrs[i];
5770 [ # # ]: 0 : if (at2[iat].charge == -1 &&
5771 [ # # # # ]: 0 : !pStruct->endpoint[i] &&
5772 : 0 : (at_Mobile_H_Revrs &&
5773 [ # # # # ]: 0 : (at_Mobile_H_Revrs[i].endpoint || at2[iat].num_H < at_Mobile_H_Revrs[i].num_H)))
5774 : : {
5775 : :
5776 [ # # # # ]: 0 : if (0 <= (e = pVA[iat].nCMinusGroupEdge - 1) &&
5777 : 0 : 0 > FindInEdgeList(&ChangeableEdges[CHG_SET_O_FIXED], e) &&
5778 [ # # # # : 0 : !pBNS->edge[e].forbidden && pBNS->edge[e].flow &&
# # ]
5779 : : (
5780 [ # # ]: 0 : (ret = AddToEdgeList(&ChangeableEdges[CHG_SET_WRONG_TAUT], e, INC_ADD_EDGE)) ||
5781 : 0 : (ret = AddToEdgeList(&ChangeableEdges[CHG_SET_O_FIXED], e, INC_ADD_EDGE))
5782 : : ))
5783 : : {
5784 : 0 : goto exit_case_23;
5785 : : }
5786 : : }
5787 : : else
5788 : : {
5789 : : /* negatively charged atom in Reconstructed structure got H(+) from Normalization */
5790 : : /* and is not tautomeric; in the original structure it is tautomeric */
5791 [ # # ]: 0 : if (at2[iat].charge == -1 &&
5792 [ # # # # ]: 0 : pStruct->endpoint[i] &&
5793 [ # # # # ]: 0 : !(at_Mobile_H_Revrs && at_Mobile_H_Revrs[i].endpoint) &&
5794 [ # # # # ]: 0 : (num_Fixed_H_Revrs && num_Fixed_H_Revrs[i] == -1) &&
5795 [ # # ]: 0 : (pnMobHRevrs && pnMobHRevrs[i] == 1) &&
5796 [ # # ]: 0 : pStruct->fixed_H[i] == 0)
5797 : : {
5798 : :
5799 [ # # # # ]: 0 : if (0 <= (e = pVA[iat].nCMinusGroupEdge - 1) &&
5800 : 0 : 0 > FindInEdgeList(&ChangeableEdges[CHG_SET_O_FIXED], e) &&
5801 [ # # # # : 0 : !pBNS->edge[e].forbidden && pBNS->edge[e].flow &&
# # ]
5802 : : (
5803 [ # # ]: 0 : (ret = AddToEdgeList(&ChangeableEdges[CHG_SET_WRONG_TAUT], e, INC_ADD_EDGE)) ||
5804 : 0 : (ret = AddToEdgeList(&ChangeableEdges[CHG_SET_O_FIXED], e, INC_ADD_EDGE))
5805 : : ))
5806 : : {
5807 : 0 : goto exit_case_23;
5808 : : }
5809 : : }
5810 : : }
5811 : : }
5812 : : /* 3. Search for (-) atoms that are tautomeric */
5813 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
5814 : : {
5815 : : /* i = canonical number - 1 */
5816 : 0 : iat = nCanon2AtnoRevrs[i];
5817 [ # # # # ]: 0 : if (pStruct->endpoint[i] &&
5818 [ # # ]: 0 : (at_Mobile_H_Revrs && at_Mobile_H_Revrs[i].endpoint) &&
5819 [ # # ]: 0 : at2[iat].charge == -1
5820 : : /*&& pVA[i].cNumValenceElectrons == 6*/)
5821 : : {
5822 [ # # ]: 0 : if (0 <= (e = pVA[iat].nCMinusGroupEdge - 1) &&
5823 [ # # # # : 0 : !pBNS->edge[e].forbidden && pBNS->edge[e].flow &&
# # ]
5824 [ # # ]: 0 : 0 > FindInEdgeList(&ChangeableEdges[CHG_SET_O_FIXED], e) &&
5825 : 0 : (ret = AddToEdgeList(&ChangeableEdges[CHG_SET_TAUT], e, INC_ADD_EDGE)))
5826 : : {
5827 : 0 : goto exit_case_23;
5828 : : }
5829 : : }
5830 : : }
5831 : :
5832 : : /* ------- finally, try to move charges from O=N --------------*/
5833 [ # # ]: 0 : for (i = 0; i < num_DB_O; i++)
5834 : : {
5835 : : int nDeltaChargeExpected;
5836 : 0 : one_success = 0;
5837 : 0 : delta = 1;
5838 : 0 : iat = iat_DB_O[i];
5839 : 0 : peDB_O_Minus = pBNS->edge + ((long long)pVA[iat].nCMinusGroupEdge - 1);/* djb-rwth: cast operator added */
5840 : 0 : pe = pBNS->edge + pBNS->vert[iat].iedge[0];
5841 : :
5842 [ # # ]: 0 : if (!pe->flow)
5843 : 0 : continue;
5844 : 0 : pv1 = pBNS->vert + (v1 = pe->neighbor1);
5845 : 0 : pv2 = pBNS->vert + (v2 = pe->neighbor12 ^ v1);
5846 : :
5847 : 0 : pe->forbidden |= forbidden_edge_mask;
5848 : :
5849 : 0 : pe->flow -= delta;
5850 : 0 : pv1->st_edge.flow -= delta;
5851 : 0 : pv2->st_edge.flow -= delta;
5852 : 0 : pBNS->tot_st_flow -= 2 * delta;
5853 : :
5854 [ # # # # ]: 0 : for (k = 0; !one_success && k <= CHG_LAST_SET; k++)
5855 : : {
5856 [ # # ]: 0 : if (!ChangeableEdges[k].num_edges)
5857 : : {
5858 : 0 : continue;
5859 : : }
5860 [ # # ]: 0 : nDeltaChargeExpected = (k == CHG_SET_NOOH) ? 2 : 0;
5861 : :
5862 : 0 : SetForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
5863 : 0 : RemoveForbiddenEdgeMask(pBNS, &ChangeableEdges[k], forbidden_edge_mask);
5864 : : /* allow (-) charge to move to N=O */
5865 : 0 : peDB_O_Minus->forbidden &= forbidden_edge_mask_inv;
5866 : :
5867 : 0 : ret = RunBnsTestOnce(pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
5868 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms);
5869 : :
5870 [ # # # # : 0 : if (ret == 1 && ((vPathEnd == v1 && vPathStart == v2) ||
# # ]
5871 [ # # # # ]: 0 : (vPathEnd == v2 && vPathStart == v1)) &&
5872 [ # # ]: 0 : nDeltaCharge == nDeltaChargeExpected) /* djb-rwth: addressing LLVM warnings */
5873 : : {
5874 : : /* Move (-) charge to =O and remove it an endpoint => nDeltaCharge == 0 */
5875 : 0 : ret = RunBnsRestoreOnce(pBNS, pBD, pVA, pTCGroups);
5876 [ # # ]: 0 : if (ret > 0)
5877 : : {
5878 : 0 : nNumRunBNS++;
5879 : 0 : one_success++; /* 23 */
5880 : : }
5881 : : }
5882 : : INCHI_HEAPCHK
5883 : : }
5884 : 0 : cur_success += one_success;
5885 : :
5886 : 0 : RemoveForbiddenEdgeMask(pBNS, &AllChargeEdges, forbidden_edge_mask);
5887 : 0 : pe->forbidden &= forbidden_edge_mask_inv;
5888 : :
5889 [ # # ]: 0 : if (!one_success)
5890 : : {
5891 : 0 : pe->flow += delta;
5892 : 0 : pv1->st_edge.flow += delta;
5893 : 0 : pv2->st_edge.flow += delta;
5894 : 0 : pBNS->tot_st_flow += 2 * delta;
5895 : : }
5896 : : }
5897 : : }
5898 : 0 : exit_case_23:
5899 [ # # ]: 0 : for (i = 0; i < CHG_SET_NUM; i++)
5900 : : {
5901 : 0 : AllocEdgeList(&ChangeableEdges[i], EDGE_LIST_FREE);
5902 : : }
5903 : :
5904 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
5905 [ # # ]: 0 : if (cur_success)
5906 : : {
5907 : 0 : tot_succes += cur_success;
5908 : : /* recalculate InChI from the structure */
5909 [ # # ]: 0 : if (0 > (ret = MakeOneInChIOutOfStrFromINChI2(pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
5910 : : ppt_group_info, ppat_norm, ppat_prep)))
5911 : : {
5912 : 0 : goto exit_function;
5913 : : }
5914 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr(pStruct)))/* djb-rwth: addressing LLVM warning */
5915 : : {
5916 : 0 : goto exit_function;
5917 : : }
5918 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
5919 : : {
5920 : 0 : goto exit_function; /* no fixed-H found */
5921 : : }
5922 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI(pStruct, at2, pVA, pInChI, pc2i))) /* djb-rwth: addressing LLVM warning */
5923 : : {
5924 : 0 : goto exit_function;
5925 : : }
5926 [ # # ]: 0 : if (!pc2i->bHasDifference)
5927 : : {
5928 : 0 : goto exit_function; /* nothing to do */
5929 : : }
5930 : : }
5931 : :
5932 : : #undef CHG_SET_NOOH
5933 : : #undef CHG_SET_WRONG_TAUT
5934 : : #undef CHG_SET_TAUT
5935 : : #undef CHG_LAST_SET
5936 : : #undef CHG_SET_O_FIXED
5937 : : #undef CHG_SET_NUM
5938 : : }
5939 : :
5940 [ # # # # ]: 0 : if (pc2i->len_c2at && pc2i->nNumTgInChI == 1)
5941 : : {
5942 : : /*------------------------------------------------------------------*/
5943 : : /* case 24: InChI norm. -N(-)-N(+)(IV) => -N=N(V) prevents tauto- */
5944 : : /* merism on -N(-)- in case of ADP */
5945 : : /* */
5946 : : /* Solution: convert N(V)=N- ...=X -> N(IV)(+)-N=...-X(-)*/
5947 : : /* N(IV)(+)-N(-)-...=X */
5948 : : /* */
5949 : : /* Orig InChI taut taut, 1 t-group only(ADP?) */
5950 : : /* Reconstructed struct non-taut possibly not taut */
5951 : : /* */
5952 : : /* Details: 1a. store next to N(V) (+)edge its flower edge */
5953 : : /* 1b. store next to N(-) edge NO_VERTEX */
5954 : : /* 2. Release (-) edges of other missing endpoints or */
5955 : : /* all endpoints if no other is missing */
5956 : : /* 3. Decrement flow on (+) edge */
5957 : : /* if flower edge is stored then expect DeltaCharge=2*/
5958 : : /* otherwise DeltaCharge = 0 */
5959 : : /*------------------------------------------------------------------*/
5960 : : int iat;
5961 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
5962 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
5963 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
5964 [ # # ]: 0 : inp_ATOM *atf = ( pStruct->pOne_norm_data[1] && pStruct->pOne_norm_data[1]->at_fixed_bonds ) ?
5965 [ # # ]: 0 : pStruct->pOne_norm_data[1]->at_fixed_bonds : NULL;
5966 : : int iN, one_success;
5967 : : EdgeIndex ef, e1;
5968 : : BNS_EDGE *pef;
5969 : : #define CHG_SET_MISSED_TAUT 0
5970 : : #define CHG_SET_OTHER_TAUT_O 1
5971 : : #define CHG_SET_OTHER_TAUT_N 2
5972 : : #define CHG_LAST_SET 2 /* the last index in trying */
5973 : : #define CHG_SET_NN 3
5974 : : #define CHG_SET_AVOID 4
5975 : : #define CHG_SET_NUM 5
5976 : : EDGE_LIST ChangeableEdges[CHG_SET_NUM];
5977 : 0 : memset( ChangeableEdges, 0, sizeof( ChangeableEdges ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5978 : : /* equivalent to AllocEdgeList( &EdgeList, EDGE_LIST_CLEAR ); */
5979 : : /*
5980 : : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H? pInChI[1]->nNum_H :
5981 : : pInChI[0] && pInChI[0]->nNum_H? pInChI[0]->nNum_H : 0;
5982 : : */
5983 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
5984 : 0 : cur_success = 0;
5985 [ # # ]: 0 : for (i = 0; i < pc2i->len_c2at; i++)
5986 : : {
5987 : 0 : iat = pc2i->c2at[i].atomNumber;
5988 : 0 : if ( /* orig. InChI info: -N=N(V) */
5989 [ # # ]: 0 : pc2i->c2at[i].nValElectr == 5 /* N or P */ &&
5990 [ # # ]: 0 : ( pc2i->c2at[i].endptInChI /* only N */ ) &&
5991 [ # # # # ]: 0 : ( e1 = pVA[iat].nCMinusGroupEdge - 1 ) >= 0 && !pBNS->edge[e1].forbidden &&
5992 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == 0 && pc2i->c2at[i].nMobHInChI == 0 &&
5993 : : /* reversed structure info: */
5994 [ # # ]: 0 : !pc2i->c2at[i].endptRevrs &&
5995 [ # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 &&
5996 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == 0 && !at2[iat].num_H &&
5997 [ # # # # ]: 0 : at2[iat].valence == 2 && at2[iat].chem_bonds_valence == 3 &&
5998 : : /* find whether -N= has =N(V) neighbor; Note: operator comma: (A,B) returns B */
5999 : 0 : ( iN = at2[iat].neighbor[at2[iat].bond_type[0] != BOND_TYPE_DOUBLE],
6000 [ # # ]: 0 : pVA[iN].cNumValenceElectrons == 5 ) &&
6001 [ # # ]: 0 : at2[iN].chem_bonds_valence == 5 &&
6002 [ # # # # : 0 : at2[iN].charge == 0 && !at2[iN].num_H && !at2[iN].radical &&
# # ]
6003 [ # # # # : 0 : 0 <= ( e = pVA[iN].nCPlusGroupEdge - 1 ) && !pBNS->edge[e].forbidden && pBNS->edge[e].flow &&
# # # # ]
6004 : 0 : 0 > FindInEdgeList( &ChangeableEdges[CHG_SET_AVOID], e ))
6005 : : {
6006 : :
6007 : 0 : ef = GetChargeFlowerUpperEdge( pBNS, pVA, e ); /* == NO_VERTEX if N(V) has 4 bonds */
6008 [ # # # # ]: 0 : if (( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_NN], e, INC_ADD_EDGE ) ) ||
6009 [ # # ]: 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_NN], ef, INC_ADD_EDGE ) ) ||
6010 [ # # ]: 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_NN], 1, INC_ADD_EDGE ) ) || /* expected nDeltaCharge */
6011 [ # # ]: 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_AVOID], e1, INC_ADD_EDGE ) ) ||
6012 [ # # ]: 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_AVOID], e, INC_ADD_EDGE ) ) ||
6013 : 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_AVOID], ef, INC_ADD_EDGE ) ))
6014 : : {
6015 : 0 : goto exit_case_24;
6016 : : }
6017 : : /* mark -N= so that (-) will not be moved to it */
6018 [ # # # # : 0 : if (0 <= ( e = pVA[iat].nCMinusGroupEdge ) && !pBNS->edge[e].forbidden &&
# # ]
6019 [ # # ]: 0 : 0 > FindInEdgeList( &ChangeableEdges[CHG_SET_AVOID], e ) &&
6020 : 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_AVOID], e, INC_ADD_EDGE ) ))
6021 : : {
6022 : 0 : goto exit_case_24;
6023 : : }
6024 : : }
6025 : : else
6026 : : {
6027 [ # # ]: 0 : if ( /* orig. InChI info: -N(-)N(IV)(+) */
6028 : 0 : atf &&
6029 [ # # ]: 0 : pc2i->c2at[i].nValElectr == 5 /* N or P */ &&
6030 [ # # ]: 0 : pc2i->c2at[i].endptInChI /* only N */ &&
6031 [ # # # # ]: 0 : ( e = pVA[iat].nCMinusGroupEdge - 1 ) >= 0 && !pBNS->edge[e].forbidden &&
6032 [ # # # # ]: 0 : pc2i->c2at[i].nFixHInChI == 0 && pc2i->c2at[i].nMobHInChI == 0 &&
6033 : : /* reversed structure info: */
6034 [ # # ]: 0 : !pc2i->c2at[i].endptRevrs &&
6035 [ # # ]: 0 : pc2i->c2at[i].nFixHRevrs == 0 &&
6036 [ # # # # ]: 0 : pc2i->c2at[i].nAtChargeRevrs == -1 && !at2[iat].num_H &&
6037 [ # # # # ]: 0 : at2[iat].valence == 2 && at2[iat].chem_bonds_valence == 2 &&
6038 [ # # # # ]: 0 : atf[iat].valence == 2 && atf[iat].chem_bonds_valence == 3 &&
6039 : : /* find whether -N= has =N(V) neighbor; Note: operator comma: (A,B) returns B */
6040 : 0 : ( iN = atf[iat].neighbor[atf[iat].bond_type[0] != BOND_TYPE_DOUBLE],
6041 [ # # ]: 0 : pVA[iN].cNumValenceElectrons == 5 ) &&
6042 [ # # ]: 0 : at2[iN].charge == 1 && /* double bond neighbor */
6043 [ # # ]: 0 : at2[iN].chem_bonds_valence == 4 &&
6044 [ # # ]: 0 : atf[iN].charge == 0 &&
6045 [ # # ]: 0 : atf[iN].chem_bonds_valence == 5 && /* InChI normalization created N(V)=N- out of N(IV)(+)-N(-)- */
6046 [ # # # # ]: 0 : !at2[iN].num_H && !at2[iN].radical &&
6047 [ # # # # : 0 : 0 <= ( e = pVA[iat].nCMinusGroupEdge - 1 ) && !pBNS->edge[e].forbidden && pBNS->edge[e].flow &&
# # # # ]
6048 : 0 : 0 > FindInEdgeList( &ChangeableEdges[CHG_SET_AVOID], e ))
6049 : : {
6050 : : /* save (-) edge */
6051 [ # # # # ]: 0 : if (( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_NN], e, INC_ADD_EDGE ) ) ||
6052 [ # # ]: 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_NN], NO_VERTEX, INC_ADD_EDGE ) ) ||
6053 [ # # ]: 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_NN], 1, INC_ADD_EDGE ) ) || /* expected nDeltaCharge */
6054 : 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_AVOID], e, INC_ADD_EDGE ) ))
6055 : : {
6056 : 0 : goto exit_case_24;
6057 : : }
6058 : : }
6059 : : }
6060 : : }
6061 [ # # ]: 0 : if (!ChangeableEdges[CHG_SET_NN].num_edges)
6062 : : {
6063 : 0 : goto exit_case_24;
6064 : : }
6065 : : /* Collect all relevant tautomeric atoms */
6066 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
6067 : : {
6068 : : /* i = canonical number - 1 */
6069 [ # # ]: 0 : if (!pStruct->endpoint[i])
6070 : : {
6071 : 0 : continue;
6072 : : }
6073 : 0 : iat = nCanon2AtnoRevrs[i];
6074 [ # # # # : 0 : if (at2[iat].charge || at2[iat].radical || at2[iat].valence == at2[iat].chem_bonds_valence)
# # ]
6075 : : {
6076 : 0 : continue; /* cannot be an acceptor of (-) */
6077 : : }
6078 [ # # # # : 0 : if (0 > ( e = pVA[iat].nCMinusGroupEdge - 1 ) || pBNS->edge[e].forbidden || pBNS->edge[e].flow)
# # ]
6079 : : {
6080 : 0 : continue;
6081 : : }
6082 [ # # ]: 0 : if (0 <= FindInEdgeList( &ChangeableEdges[CHG_SET_AVOID], e ))
6083 : : {
6084 : 0 : continue; /* has already been used */
6085 : : }
6086 : : /* missing endpoint */
6087 [ # # # # ]: 0 : if (!( at_Mobile_H_Revrs && at_Mobile_H_Revrs[iat].endpoint ))
6088 : : {
6089 [ # # # # ]: 0 : if (0 > FindInEdgeList( &ChangeableEdges[CHG_SET_AVOID], e ) && (
6090 [ # # ]: 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_MISSED_TAUT], e, INC_ADD_EDGE ) ) ||
6091 : 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_AVOID], e, INC_ADD_EDGE ) ) ))
6092 : : {
6093 : 0 : goto exit_case_24;
6094 : : }
6095 : : }
6096 : : else
6097 : : {
6098 : : /* endpoint O */
6099 [ # # ]: 0 : if (pVA[iat].cNumValenceElectrons == 6)
6100 : : {
6101 [ # # # # ]: 0 : if (0 > FindInEdgeList( &ChangeableEdges[CHG_SET_AVOID], e ) && (
6102 [ # # ]: 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_OTHER_TAUT_O], e, INC_ADD_EDGE ) ) ||
6103 : 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_AVOID], e, INC_ADD_EDGE ) ) ))
6104 : : {
6105 : 0 : goto exit_case_24;
6106 : : }
6107 : : }
6108 : : else
6109 : : {
6110 : : /* endpoint N */
6111 [ # # ]: 0 : if (pVA[iat].cNumValenceElectrons == 5)
6112 : : {
6113 [ # # # # ]: 0 : if (0 > FindInEdgeList( &ChangeableEdges[CHG_SET_AVOID], e ) && (
6114 [ # # ]: 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_OTHER_TAUT_N], e, INC_ADD_EDGE ) ) ||
6115 : 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_AVOID], e, INC_ADD_EDGE ) ) ))
6116 : : {
6117 : 0 : goto exit_case_24;
6118 : : }
6119 : : }
6120 : : }
6121 : : }
6122 : : }
6123 : : /* ------- finally, try to move charges from -N(-)-N(+) or to N(V) --------------*/
6124 [ # # ]: 0 : for (i = 0; i < ChangeableEdges[CHG_SET_NN].num_edges; i += 3)
6125 : : {
6126 : : int nDeltaChargeExpected;
6127 : 0 : one_success = 0;
6128 : 0 : delta = 1;
6129 : 0 : pe = pBNS->edge + ChangeableEdges[CHG_SET_NN].pnEdges[i];
6130 : 0 : pef = ( NO_VERTEX != ChangeableEdges[CHG_SET_NN].pnEdges[i + 1] ) ?
6131 [ # # ]: 0 : pBNS->edge + ChangeableEdges[CHG_SET_NN].pnEdges[i + 1] : NULL;
6132 : 0 : nDeltaChargeExpected = ChangeableEdges[CHG_SET_NN].pnEdges[i + 2];
6133 : :
6134 [ # # ]: 0 : if (!pe->flow)
6135 : 0 : continue;
6136 : 0 : pv1 = pBNS->vert + ( v1 = pe->neighbor1 );
6137 : 0 : pv2 = pBNS->vert + ( v2 = pe->neighbor12 ^ v1 );
6138 : :
6139 : 0 : pe->flow -= delta;
6140 : 0 : pv1->st_edge.flow -= delta;
6141 : 0 : pv2->st_edge.flow -= delta;
6142 : 0 : pBNS->tot_st_flow -= 2 * delta;
6143 : :
6144 [ # # # # ]: 0 : for (k = 0; !one_success && k <= CHG_LAST_SET; k++)
6145 : : {
6146 [ # # ]: 0 : if (!ChangeableEdges[k].num_edges)
6147 : : {
6148 : 0 : continue;
6149 : : }
6150 : 0 : SetForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
6151 : 0 : RemoveForbiddenEdgeMask( pBNS, &ChangeableEdges[k], forbidden_edge_mask );
6152 : : /* allow change of N(V) flower edge */
6153 [ # # ]: 0 : if (pef)
6154 : : {
6155 : 0 : pef->forbidden &= forbidden_edge_mask_inv;
6156 : : }
6157 : :
6158 : 0 : ret = RunBnsTestOnce( pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
6159 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms );
6160 : :
6161 [ # # # # : 0 : if (ret == 1 && ( (vPathEnd == v1 && vPathStart == v2) ||
# # ]
6162 [ # # # # ]: 0 : (vPathEnd == v2 && vPathStart == v1) ) &&
6163 [ # # ]: 0 : nDeltaCharge == nDeltaChargeExpected) /* djb-rwth: addressing LLVM warnings */
6164 : : {
6165 : : /* Move (-) charge to =O and remove it an endpoint => nDeltaCharge == 0 */
6166 : 0 : ret = RunBnsRestoreOnce( pBNS, pBD, pVA, pTCGroups );
6167 [ # # ]: 0 : if (ret > 0)
6168 : : {
6169 : 0 : nNumRunBNS++;
6170 : 0 : one_success++; /* 24 */
6171 : : }
6172 : : }
6173 : : INCHI_HEAPCHK
6174 : : }
6175 : 0 : cur_success += one_success;
6176 : :
6177 : 0 : RemoveForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
6178 : :
6179 [ # # ]: 0 : if (!one_success)
6180 : : {
6181 : 0 : pe->flow += delta;
6182 : 0 : pv1->st_edge.flow += delta;
6183 : 0 : pv2->st_edge.flow += delta;
6184 : 0 : pBNS->tot_st_flow += 2 * delta;
6185 : : }
6186 : : }
6187 : 0 : exit_case_24:
6188 [ # # ]: 0 : for (i = 0; i < CHG_SET_NUM; i++)
6189 : : {
6190 : 0 : AllocEdgeList( &ChangeableEdges[i], EDGE_LIST_FREE );
6191 : : }
6192 : :
6193 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
6194 [ # # ]: 0 : if (cur_success)
6195 : : {
6196 : 0 : tot_succes += cur_success;
6197 : : /* recalculate InChI from the structure */
6198 [ # # ]: 0 : if (0 >( ret = MakeOneInChIOutOfStrFromINChI2( pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
6199 : : ppt_group_info, ppat_norm, ppat_prep ) ))
6200 : : {
6201 : 0 : goto exit_function;
6202 : : }
6203 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr( pStruct ))) /* djb-rwth: addressing LLVM warning */
6204 : : {
6205 : 0 : goto exit_function;
6206 : : }
6207 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
6208 : : {
6209 : 0 : goto exit_function; /* no fixed-H found */
6210 : : }
6211 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI( pStruct, at2, pVA, pInChI, pc2i ))) /* djb-rwth: addressing LLVM warning */
6212 : : {
6213 : 0 : goto exit_function;
6214 : : }
6215 [ # # ]: 0 : if (!pc2i->bHasDifference)
6216 : : {
6217 : 0 : goto exit_function; /* nothing to do */
6218 : : }
6219 : : }
6220 : : #undef CHG_SET_NN
6221 : : #undef CHG_SET_MISSED_TAUT
6222 : : #undef CHG_SET_OTHER_TAUT_O
6223 : : #undef CHG_SET_OTHER_TAUT_N
6224 : : #undef CHG_LAST_SET
6225 : : #undef CHG_SET_AVOID
6226 : : #undef CHG_SET_NUM
6227 : : }
6228 : :
6229 : : /* pStruct->nNumRemovedProtonsMobHInChI == pc2i->nNumRemHInChI */
6230 : :
6231 [ # # # # ]: 0 : if (pc2i->len_c2at && pc2i->nNumTgInChI == 1 &&
6232 [ # # # # ]: 0 : pc2i->nNumRemHRevrs > pc2i->nNumRemHInChI && 0 > pc2i->nNumRemHInChI &&
6233 [ # # ]: 0 : ( pc2i->nNumEndpRevrs < pc2i->nNumEndpInChI ||
6234 [ # # ]: 0 : pc2i->nNumTgRevrs > pc2i->nNumTgInChI ))
6235 : : {
6236 : : /*------------------------------------------------------------------*/
6237 : : /* case 25: Restored InChI does not have 2 or more added protons */
6238 : : /* possibly taut. endpoints are missing */
6239 : : /* has -N(-O(-))-O(-) group(s) */
6240 : : /* Original InChI has only one t-group */
6241 : : /* */
6242 : : /* Solution: convert -N(-O(-))-O(-) -> -N(+)(=O)-O(-) */
6243 : : /* and direct 2(-) to the missing taut atoms*/
6244 : : /* at first attempt try to move (-) to N only */
6245 : : /* */
6246 : : /*------------------------------------------------------------------*/
6247 : : int iat;
6248 : 0 : AT_NUMB *nCanon2AtnoRevrs = pStruct->nCanon2Atno[0];
6249 : 0 : AT_NUMB *nAtno2CanonRevrs = pStruct->nAtno2Canon[0];
6250 : 0 : inp_ATOM *at_Mobile_H_Revrs = ( pStruct->pOne_norm_data[1] &&
6251 [ # # # # ]: 0 : pStruct->pOne_norm_data[1]->at ) ? pStruct->pOne_norm_data[1]->at : NULL;
6252 : : /*
6253 : : inp_ATOM *atf = (pStruct->pOne_norm_data[1] && pStruct->pOne_norm_data[1]->at_fixed_bonds)?
6254 : : pStruct->pOne_norm_data[1]->at_fixed_bonds : NULL;
6255 : : */
6256 : : int iN, neigh, one_success;
6257 : : EdgeIndex e1, bFirst;
6258 : : BNS_EDGE *pef;
6259 : : #define CHG_SET_MISSED_TAUT_1 0
6260 : : #define CHG_SET_MISSED_TAUT_ALL 1
6261 : : #define CHG_SET_OTHER_TAUT_1 2
6262 : : #define CHG_SET_OTHER_TAUT_ALL 3
6263 : : #define CHG_LAST_SET 3 /* the last index in trying */
6264 : : #define CHG_SET_NO_IN_NO2M2 4
6265 : : #define CHG_SET_AVOID 5
6266 : : #define CHG_SET_NUM 6
6267 : : EDGE_LIST ChangeableEdges[CHG_SET_NUM];
6268 : 0 : memset( ChangeableEdges, 0, sizeof( ChangeableEdges ) ); /* djb-rwth: memset_s C11/Annex K variant? */
6269 : : /* equivalent to AllocEdgeList( &EdgeList, EDGE_LIST_CLEAR ); */
6270 : : /*
6271 : : S_CHAR *nMobHInChI = pInChI[1] && pInChI[1]->nNum_H? pInChI[1]->nNum_H :
6272 : : pInChI[0] && pInChI[0]->nNum_H? pInChI[0]->nNum_H : 0;
6273 : : */
6274 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
6275 : 0 : cur_success = 0;
6276 : : /* find all -N(-O(-))-O(-) */
6277 [ # # ]: 0 : for (i = 0; i < pStruct->num_atoms; i++)
6278 : : {
6279 : 0 : iat = nCanon2AtnoRevrs[i];
6280 [ # # ]: 0 : if (pStruct->endpoint[i])
6281 : : {
6282 [ # # # # : 0 : if (0 >( e = pVA[iat].nCMinusGroupEdge - 1 ) || pBNS->edge[e].forbidden ||
# # ]
6283 : 0 : 0 <= FindInEdgeList( &ChangeableEdges[CHG_SET_AVOID], e ))
6284 : : {
6285 : 0 : continue;
6286 : : }
6287 [ # # # # ]: 0 : bFirst = ( (pVA[iat].cNumValenceElectrons == 5 && pc2i->nNumTgInChI == 1) ||
6288 [ # # # # ]: 0 : (pVA[iat].cNumValenceElectrons == 6 && pc2i->nNumTgInChI != 1) ); /* djb-rwth: addressing LLVM warnings */
6289 : : /* many or no t-groups -> try O only first */
6290 : : /* single t-group -> try only N first */
6291 [ # # # # ]: 0 : if (!( at_Mobile_H_Revrs && at_Mobile_H_Revrs[i].endpoint ))
6292 : : {
6293 : : /* missed tautomeric endpoint */
6294 [ # # # # ]: 0 : if (bFirst &&
6295 : 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_MISSED_TAUT_1], e, INC_ADD_EDGE ) ))
6296 : : {
6297 : 0 : goto exit_case_25;
6298 : : }
6299 [ # # ]: 0 : if ((ret = AddToEdgeList( &ChangeableEdges[CHG_SET_MISSED_TAUT_ALL], e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
6300 : : {
6301 : 0 : goto exit_case_25;
6302 : : }
6303 : : }
6304 [ # # # # ]: 0 : if (bFirst &&
6305 : 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_OTHER_TAUT_1], e, INC_ADD_EDGE ) ))
6306 : : {
6307 : 0 : goto exit_case_25;
6308 : : }
6309 [ # # ]: 0 : if ((ret = AddToEdgeList( &ChangeableEdges[CHG_SET_OTHER_TAUT_ALL], e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
6310 : : {
6311 : 0 : goto exit_case_25;
6312 : : }
6313 [ # # ]: 0 : if ((ret = AddToEdgeList( &ChangeableEdges[CHG_SET_AVOID], e, INC_ADD_EDGE ))) /* djb-rwth: addressing LLVM warning */
6314 : : {
6315 : 0 : goto exit_case_25;
6316 : : }
6317 : : }
6318 : : else
6319 : : {
6320 [ # # # # ]: 0 : if (at2[iat].valence == 1 && at2[iat].charge == -1 &&
6321 [ # # ]: 0 : pVA[iat].cNumValenceElectrons == 6 &&
6322 [ # # ]: 0 : pVA[iN = at2[iat].neighbor[0]].cNumValenceElectrons == 5 && /* -O(-) */
6323 [ # # ]: 0 : !pStruct->endpoint[nAtno2CanonRevrs[iN]] &&
6324 [ # # # # ]: 0 : at2[iN].valence == 3 && at2[iN].chem_bonds_valence == 3 &&
6325 [ # # # # ]: 0 : !at2[iN].charge && !at2[iN].radical &&
6326 [ # # # # ]: 0 : 0 <= ( e = pVA[iN].nCPlusGroupEdge - 1 ) && !pBNS->edge[e].forbidden &&
6327 [ # # ]: 0 : pBNS->edge[e].flow && /* NPlus edge */
6328 [ # # # # ]: 0 : 0 <= ( e1 = pVA[iat].nCMinusGroupEdge - 1 ) && !pBNS->edge[e1].forbidden &&
6329 [ # # # # ]: 0 : pBNS->edge[e1].flow && /* OMinus edge */
6330 [ # # ]: 0 : 0 > FindInEdgeList( &ChangeableEdges[CHG_SET_AVOID], e ) &&
6331 : 0 : 0 > FindInEdgeList( &ChangeableEdges[CHG_SET_AVOID], e1 ))
6332 : : {
6333 : : /* found >N-O(-) */
6334 : 0 : int nNumO = 0, nNumOthers = 0;
6335 [ # # ]: 0 : for (k = 0; k < at2[iN].valence; k++)
6336 : : {
6337 : 0 : neigh = at2[iN].neighbor[k];
6338 [ # # ]: 0 : if (neigh == iat)
6339 : : {
6340 : 0 : continue;
6341 : : }
6342 [ # # ]: 0 : if (pVA[neigh].cNumValenceElectrons == 6 &&
6343 [ # # ]: 0 : !pStruct->endpoint[neigh] &&
6344 [ # # # # ]: 0 : at2[neigh].valence == 1 && at2[neigh].num_H == 0 &&
6345 [ # # # # ]: 0 : at2[neigh].radical == 0 && at2[neigh].charge == -1 &&
6346 [ # # ]: 0 : at2[neigh].chem_bonds_valence == 1)
6347 : : {
6348 : 0 : nNumO++;
6349 : : }
6350 : : else
6351 : : {
6352 [ # # ]: 0 : if (at2[iN].bond_type[k] == BOND_TYPE_SINGLE &&
6353 [ # # ]: 0 : at2[neigh].valence > 1 &&
6354 [ # # ]: 0 : at2[neigh].valence < at2[neigh].chem_bonds_valence)
6355 : : {
6356 : 0 : nNumOthers++;
6357 : : }
6358 : : }
6359 : : }
6360 [ # # # # ]: 0 : if (nNumO != 1 && nNumOthers != 1)
6361 : : {
6362 : 0 : continue;
6363 : : }
6364 : : /* save charge edges: NPlus first, OMinus second */
6365 [ # # # # ]: 0 : if (( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_NO_IN_NO2M2], e, INC_ADD_EDGE ) ) ||
6366 [ # # ]: 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_NO_IN_NO2M2], e1, INC_ADD_EDGE ) ) ||
6367 [ # # ]: 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_AVOID], e, INC_ADD_EDGE ) ) ||
6368 : 0 : ( ret = AddToEdgeList( &ChangeableEdges[CHG_SET_AVOID], e1, INC_ADD_EDGE ) ))
6369 : : {
6370 : 0 : goto exit_case_25;
6371 : : }
6372 : : }
6373 : : }
6374 : : }
6375 [ # # ]: 0 : if (!ChangeableEdges[CHG_SET_NO_IN_NO2M2].num_edges ||
6376 [ # # ]: 0 : !ChangeableEdges[CHG_SET_OTHER_TAUT_ALL].num_edges)
6377 : : {
6378 : 0 : goto exit_case_25;
6379 : : }
6380 : : /* ------- finally, try to move charges from -NO2(2-) or to tautomeric endpoints ----*/
6381 [ # # ]: 0 : for (i = 0; i < ChangeableEdges[CHG_SET_NO_IN_NO2M2].num_edges; i += 2)
6382 : : {
6383 : 0 : int nDeltaChargeExpected = 3;
6384 : : /* change flow on O(-) to make it neutral; 3 new charges will be created:
6385 : : N(+), and two (-) on InChI endpoints
6386 : : alternatively, if we change flow on N to make N(+) then O(-) will
6387 : : be nutralized (-1 charge) and two (-) charges on taut. endpoints will be
6388 : : created (+2); the total change in this case would be (-1)+(+2) = +1
6389 : : */
6390 : 0 : one_success = 0;
6391 : 0 : delta = 1;
6392 : 0 : pe = pBNS->edge + ChangeableEdges[CHG_SET_NO_IN_NO2M2].pnEdges[i + 1]; /* O(-) edge */
6393 : 0 : pef = pBNS->edge + ChangeableEdges[CHG_SET_NO_IN_NO2M2].pnEdges[i]; /* >N- (+) edge */
6394 : :
6395 [ # # ]: 0 : if (!pe->flow)
6396 : 0 : continue;
6397 : 0 : pv1 = pBNS->vert + ( v1 = pe->neighbor1 );
6398 : 0 : pv2 = pBNS->vert + ( v2 = pe->neighbor12 ^ v1 );
6399 : :
6400 : 0 : pe->flow -= delta;
6401 : 0 : pv1->st_edge.flow -= delta;
6402 : 0 : pv2->st_edge.flow -= delta;
6403 : 0 : pBNS->tot_st_flow -= 2 * delta;
6404 : :
6405 [ # # # # ]: 0 : for (k = 0; !one_success && k <= CHG_LAST_SET; k++)
6406 : : {
6407 [ # # ]: 0 : if (!ChangeableEdges[k].num_edges)
6408 : : {
6409 : 0 : continue;
6410 : : }
6411 : 0 : SetForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
6412 : 0 : RemoveForbiddenEdgeMask( pBNS, &ChangeableEdges[k], forbidden_edge_mask );
6413 : : /* allow change of N(V) flower edge */
6414 : 0 : pef->forbidden &= forbidden_edge_mask_inv;
6415 : :
6416 : 0 : ret = RunBnsTestOnce( pBNS, pBD, pVA, &vPathStart, &vPathEnd, &nPathLen,
6417 : : &nDeltaH, &nDeltaCharge, &nNumVisitedAtoms );
6418 : :
6419 [ # # # # : 0 : if (ret == 1 && ( (vPathEnd == v1 && vPathStart == v2) ||
# # ]
6420 [ # # # # ]: 0 : (vPathEnd == v2 && vPathStart == v1) ) &&
6421 [ # # ]: 0 : nDeltaCharge == nDeltaChargeExpected) /* djb-rwth: addressing LLVM warnings */
6422 : : {
6423 : : /* Move (-) charge to =O and remove it an endpoint => nDeltaCharge == 0 */
6424 : 0 : ret = RunBnsRestoreOnce( pBNS, pBD, pVA, pTCGroups );
6425 [ # # ]: 0 : if (ret > 0)
6426 : : {
6427 : 0 : nNumRunBNS++;
6428 : 0 : one_success++; /* 24 */
6429 : : }
6430 : : }
6431 : : INCHI_HEAPCHK
6432 : : }
6433 : 0 : cur_success += one_success;
6434 : :
6435 : 0 : RemoveForbiddenEdgeMask( pBNS, &AllChargeEdges, forbidden_edge_mask );
6436 : :
6437 [ # # ]: 0 : if (!one_success)
6438 : : {
6439 : 0 : pe->flow += delta;
6440 : 0 : pv1->st_edge.flow += delta;
6441 : 0 : pv2->st_edge.flow += delta;
6442 : 0 : pBNS->tot_st_flow += 2 * delta;
6443 : : }
6444 : : }
6445 : 0 : exit_case_25:
6446 [ # # ]: 0 : for (i = 0; i < CHG_SET_NUM; i++)
6447 : : {
6448 : 0 : AllocEdgeList( &ChangeableEdges[i], EDGE_LIST_FREE );
6449 : : }
6450 : :
6451 : 0 : CurrEdges.num_edges = 0; /* clear current edge list */
6452 [ # # ]: 0 : if (cur_success)
6453 : : {
6454 : 0 : tot_succes += cur_success;
6455 : : /* recalculate InChI from the structure */
6456 [ # # ]: 0 : if (0 >( ret = MakeOneInChIOutOfStrFromINChI2( pCG, ic, ip, sd, pBNS, pStruct, at, at2, at3, pVA, pTCGroups,
6457 : : ppt_group_info, ppat_norm, ppat_prep ) ))
6458 : : {
6459 : 0 : goto exit_function;
6460 : : }
6461 [ # # ]: 0 : if ((ret = FillOutExtraFixedHDataRestr( pStruct ))) /* djb-rwth: addressing LLVM warning */
6462 : : {
6463 : 0 : goto exit_function;
6464 : : }
6465 [ # # # # ]: 0 : if (!pInChI[0]->nNum_H_fixed && !pStruct->pOneINChI[0]->nNum_H_fixed)
6466 : : {
6467 : 0 : goto exit_function; /* no fixed-H found */
6468 : : }
6469 [ # # ]: 0 : if ((ret = FillOutCMP2FHINCHI( pStruct, at2, pVA, pInChI, pc2i ))) /* djb-rwth: addressing LLVM warning */
6470 : : {
6471 : 0 : goto exit_function;
6472 : : }
6473 [ # # ]: 0 : if (!pc2i->bHasDifference)
6474 : : {
6475 : 0 : goto exit_function; /* nothing to do */
6476 : : }
6477 : : }
6478 : : #undef CHG_SET_NN
6479 : : #undef CHG_SET_MISSED_TAUT
6480 : : #undef CHG_SET_OTHER_TAUT_O
6481 : : #undef CHG_SET_OTHER_TAUT_N
6482 : : #undef CHG_LAST_SET
6483 : : #undef CHG_SET_AVOID
6484 : : #undef CHG_SET_NUM
6485 : : }
6486 : :
6487 : :
6488 : 0 : exit_function:
6489 : :
6490 : 0 : AllocEdgeList( &AllChargeEdges, EDGE_LIST_FREE );
6491 : 0 : AllocEdgeList( &CurrEdges, EDGE_LIST_FREE );
6492 : 0 : AllocEdgeList( &NFlowerEdges, EDGE_LIST_FREE );
6493 : 0 : AllocEdgeList( &SFlowerEdges, EDGE_LIST_FREE );
6494 : 0 : AllocEdgeList( &OtherNFlowerEdges, EDGE_LIST_FREE );
6495 : 0 : AllocEdgeList( &FixedLargeRingStereoEdges, EDGE_LIST_FREE );
6496 : 0 : AllocEdgeList( &AllBondEdges, EDGE_LIST_FREE );
6497 : :
6498 [ # # # # : 0 : return ret < 0 ? ret : ( tot_succes && pc2i->bHasDifference );
# # ]
6499 : : }
6500 : :
6501 : : #endif
|