1 /*
2  * International Chemical Identifier (InChI)
3  * Version 1
4  * Software version 1.04
5  * September 9, 2011
6  *
7  * The InChI library and programs are free software developed under the
8  * auspices of the International Union of Pure and Applied Chemistry (IUPAC).
9  * Originally developed at NIST. Modifications and additions by IUPAC
10  * and the InChI Trust.
11  *
12  * IUPAC/InChI-Trust Licence for the International Chemical Identifier (InChI)
13  * Software version 1.0.
14  * Copyright (C) IUPAC and InChI Trust Limited
15  *
16  * This library is free software; you can redistribute it and/or modify it under the
17  * terms of the IUPAC/InChI Trust Licence for the International Chemical Identifier
18  * (InChI) Software version 1.0; either version 1.0 of the License, or
19  * (at your option) any later version.
20  *
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24  * See the IUPAC/InChI Trust Licence for the International Chemical Identifier (InChI)
25  * Software version 1.0 for more details.
26  *
27  * You should have received a copy of the IUPAC/InChI Trust Licence for the
28  * International Chemical Identifier (InChI) Software version 1.0 along with
29  * this library; if not, write to:
30  *
31  * The InChI Trust
32  * c/o FIZ CHEMIE Berlin
33  * Franklinstrasse 11
34  * 10587 Berlin
35  * GERMANY
36  *
37  */
38 
39 
40 #ifndef __INCHICOMN_H__
41 #define __INCHICOMN_H__
42 
43 #include "ichierr.h"
44 
45 
46 /********************************
47  *
48  * Globals for sorting
49  */
50 
51 extern const NEIGH_LIST      *pNeighList_RankForSort;
52 extern const ATOM_INVARIANT2 *pAtomInvariant2ForSort;
53 extern const AT_NUMB         *pNeighborsForSort;
54 extern const AT_RANK         *pn_RankForSort;
55 
56 extern AT_RANK         nMaxAtNeighRankForSort;
57 
58 extern int             nNumCompNeighborsRanksCountEql;
59 
60 
61 #define tsort insertions_sort
62 
63 typedef struct tagEquNeigh {
64     int     num_to;   /* number of neighbors with equal mapping ranks; one of them has min. canon. number */
65     AT_RANK to_at[4]; /* to_atom neighbors #s with equal mapping ranks */
66     AT_RANK from_at;  /* from_at neighbor # which has min. canon. number and can be mapped on any of the above to_at[] */
67     AT_RANK rank;     /* equal mapping rank value */
68     AT_RANK canon_rank;  /* min. canon. number */
69 } EQ_NEIGH;
70 
71 #ifndef COMPILE_ALL_CPP
72 #ifdef  __cplusplus
73 extern "C" {
74 #endif
75 #endif
76 
77 /*******************************************************************/
78 /* ichiisot.c */
79 int unpack_iso_sort_key( AT_ISO_SORT_KEY iso_sort_key, S_CHAR *num_1H, S_CHAR *num_2H, S_CHAR *num_3H, S_CHAR *iso_atw_diff );
80 AT_ISO_SORT_KEY make_iso_sort_key( int iso_atw_diff, int num_1H, int num_2H, int num_3H);
81 int set_atom_iso_sort_keys(  int num_at, sp_ATOM *at, T_GROUP_INFO* t_group_info, int *bHasIsotopicInTautomerGroups );
82 /***********************************************************************/
83 /* ichisort.c */
84 
85 void insertions_sort_NeighList_AT_NUMBERS( NEIGH_LIST base, AT_RANK *nRank );
86 int insertions_sort_NeighList_AT_NUMBERS3( NEIGH_LIST base, AT_RANK *nRank );
87 int insertions_sort_AT_RANK( AT_RANK *base, int num );
88 void insertions_sort_NeighListBySymmAndCanonRank( NEIGH_LIST base, const AT_RANK *nSymmRank, const AT_RANK *nCanonRank );
89 int CompareNeighListLex( NEIGH_LIST pp1, NEIGH_LIST pp2, const AT_RANK *nRank);
90 int CompareNeighListLexUpToMaxRank( NEIGH_LIST pp1, NEIGH_LIST pp2, const AT_RANK *nRank, AT_RANK nMaxAtNeighRank );
91 int compare_NeighLists( const NEIGH_LIST *op1,  const NEIGH_LIST *op2 );
92 int CompNeighborsAT_NUMBER( const void* a1, const void* a2);
93 int comp_AT_RANK( const void* a1, const void* a2);
94 int CompRank(const void* a1, const void* a2 );
95 int CompRanksOrd( const void* a1, const void* a2 );
96 
97 int CompAtomInvariants2Only( const void* a1, const void* a2 );
98 int CompAtomInvariants2( const void* a1, const void* a2 );
99 
100 int CompNeighListRanks( const void* a1, const void* a2 );
101 int CompNeighListRanksOrd( const void* a1, const void* a2 );
102 int CompNeighLists( const void* a1, const void* a2 );
103 int CompNeighListsUpToMaxRank( const void* a1, const void* a2 );
104 int CompNeighborsRanksCountEql( const void* a1, const void* a2 );
105 int CompRanksInvOrd( const void* a1, const void* a2 );
106 int CompChemElemLex( const void *a1, const void *a2 );
107 
108 
109 
110 NEIGH_LIST *CreateNeighList( int num_atoms, int num_at_tg, sp_ATOM* at, int bDoubleBondSquare, T_GROUP_INFO *t_group_info );
111 NEIGH_LIST *CreateNeighListFromLinearCT( AT_NUMB *LinearCT, int nLenCT, int num_atoms );
112 
113 void FreeNeighList( NEIGH_LIST *pp );
114 int BreakAllTies( int num_atoms, int num_max, AT_RANK **pRankStack,
115                      NEIGH_LIST *NeighList, AT_RANK *nTempRank, CANON_STAT *pCS);
116 
117 /******************************************************************************/
118 /* ichimap.c */
119 void switch_ptrs( AT_RANK **p1, AT_RANK **p2 );
120 
121 int SortedEquInfoToRanks( const AT_RANK* nSymmRank, AT_RANK* nRank, const AT_RANK* nAtomNumber, int num_atoms, int *bChanged );
122 int SortedRanksToEquInfo( AT_RANK* nSymmRank, const AT_RANK* nRank, const AT_RANK* nAtomNumber, int num_atoms );
123 
124 int SetNewRanksFromNeighLists( int num_atoms, NEIGH_LIST *NeighList, AT_RANK *nRank, AT_RANK *nNewRank,
125                              AT_RANK *nAtomNumber, int bUseAltSort, int ( *comp )(const void *, const void *) );
126 int SetNewRanksFromNeighLists3( int num_atoms, NEIGH_LIST *NeighList, AT_RANK *nRank, AT_RANK *nNewRank,
127                              AT_RANK *nAtomNumber );
128 int SetNewRanksFromNeighLists4( int num_atoms, NEIGH_LIST *NeighList, AT_RANK *nRank, AT_RANK *nNewRank,
129                              AT_RANK *nAtomNumber, AT_RANK nMaxAtRank );
130 void SortNeighListsBySymmAndCanonRank( int num_atoms, NEIGH_LIST *NeighList, const AT_RANK *nSymmRank, const AT_RANK *nCanonRank );
131 int SortNeighLists2( int num_atoms, AT_RANK *nRank, NEIGH_LIST *NeighList, AT_RANK *nAtomNumber );
132 int  DifferentiateRanks2( int num_atoms, NEIGH_LIST *NeighList,
133                                  int nNumCurrRanks, AT_RANK *pnCurrRank, AT_RANK *pnPrevRank,
134                                  AT_RANK *nAtomNumber, long *lNumIter, int bUseAltSort );
135 
136 int  DifferentiateRanks3( int num_atoms, NEIGH_LIST *NeighList,
137                                  int nNumCurrRanks, AT_RANK *pnCurrRank, AT_RANK *pnPrevRank,
138                                  AT_RANK *nAtomNumber, long *lNumIter );
139 int  DifferentiateRanks4( int num_atoms, NEIGH_LIST *NeighList,
140                           int nNumCurrRanks, AT_RANK *pnCurrRank, AT_RANK *pnPrevRank,
141                           AT_RANK *nAtomNumber, AT_RANK nMaxAtRank, long *lNumIter );
142 int  DifferentiateRanksBasic( int num_atoms, NEIGH_LIST *NeighList,
143                                  int nNumCurrRanks, AT_RANK *pnCurrRank, AT_RANK *pnPrevRank,
144                                  AT_RANK *nAtomNumber, long *lNumIter, int bUseAltSort );
145 
146 int parity_of_mapped_atom2( int from_at, int to_at, const sp_ATOM *at, EQ_NEIGH *pEN,
147                            const AT_RANK *nCanonRankFrom, const AT_RANK *nRankFrom, const AT_RANK *nRankTo );
148 
149 int parity_of_mapped_half_bond( int from_at, int to_at, int from_neigh, int to_neigh,
150                            sp_ATOM *at, EQ_NEIGH *pEN,
151                            const AT_RANK *nCanonRankFrom, const AT_RANK *nRankFrom, const AT_RANK *nRankTo );
152 
153 int HalfStereoBondParity( sp_ATOM *at, int at_no1, int i_sb_neigh, const AT_RANK *nRank );
154 
155 int NumberOfTies( AT_RANK **pRankStack1, AT_RANK **pRankStack2, int length, int at_no1, int at_no2, AT_RANK *nNewRank, int *bAddStack, int *bMapped1 );
156 
157 int map_an_atom2( int num_atoms, int num_max, int at_no1/*from*/, int at_no2/*to*/,
158                 AT_RANK *nTempRank,
159                 int nNumMappedRanks, int *pnNewNumMappedRanks,
160                 CANON_STAT *pCS,
161                 NEIGH_LIST    *NeighList,
162                 AT_RANK  **pRankStack1, AT_RANK  **pRankStack2, int *bAddStack );
163 int ClearPreviousMappings( AT_RANK **pRankStack1 );
164 
165 int SetOneStereoBondIllDefParity( sp_ATOM *at, int jc, /* atom number*/ int k /* stereo bond ord. number*/, int new_parity );
166 int RemoveOneStereoBond( sp_ATOM *at, int jc, /* atom number*/ int k /* stereo bond number*/ );
167 int RemoveOneStereoCenter( sp_ATOM *at, int jc /* atom number*/ );
168 int RemoveCalculatedNonStereo( sp_ATOM *at, int num_atoms, int num_at_tg,
169                               AT_RANK **pRankStack1, AT_RANK **pRankStack2, AT_RANK *nTempRank, NEIGH_LIST *NeighList,
170                               const AT_RANK *nSymmRank, AT_RANK *nCanonRank,
171                               AT_RANK *nAtomNumberCanon, CANON_STAT *pCS,
172                               int vABParityUnknown);
173 
174 
175 int might_change_other_atom_parity( sp_ATOM *at, int num_atoms, int at_no, AT_RANK *nRank2, AT_RANK *nRank1 );
176 
177 int map_stereo_bonds4 (
178                 sp_ATOM *at, int num_atoms, int num_at_tg, int num_max, int bAllene,
179                 const AT_RANK *nCanonRankFrom, const AT_RANK *nAtomNumberCanonFrom, /*  non-stereo canon ranking */
180                 AT_RANK *nCanonRankTo, /* output canonical numbering*/
181                 const AT_RANK *nSymmRank,      AT_RANK **pRankStack1/*from*/,  AT_RANK **pRankStack2/*to*/,
182                 AT_RANK *nTempRank,      int nNumMappedRanksInput,
183                 AT_RANK *nSymmStereo,    NEIGH_LIST *NeighList,
184                 CANON_STAT *pCS,         CUR_TREE *cur_tree,  int nNumMappedBonds,
185                 int vABParityUnknown);
186 
187 int map_stereo_atoms4 (
188                 sp_ATOM *at, int num_atoms, int num_at_tg, int num_max,
189                 const AT_RANK *nCanonRankFrom, const AT_RANK *nAtomNumberCanonFrom, AT_RANK *nCanonRankTo, /*  canonical numbering to be mapped */
190                 const AT_RANK *nSymmRank,      AT_RANK **pRankStack1/*from*/, AT_RANK **pRankStack2/*to*/,
191                 AT_RANK *nTempRank,      int nNumMappedRanksInput,
192                 AT_RANK *nSymmStereo,    NEIGH_LIST *NeighList,
193                 CANON_STAT *pCS,         CUR_TREE *cur_tree, int nNumMappedAtoms,
194                 int vABParityUnknown);
195 
196 
197 int CurTreeAlloc( CUR_TREE *cur_tree, int num_atoms );
198 int CurTreeReAlloc( CUR_TREE *cur_tree );
199 void CurTreeFree( CUR_TREE *cur_tree );
200 int CurTreeAddRank( CUR_TREE *cur_tree, AT_NUMB rank );
201 int CurTreeRemoveLastRank( CUR_TREE *cur_tree );
202 int CurTreeReplaceLastRank( CUR_TREE *cur_tree, AT_NUMB rank );
203 int CurTreeFindTheRankPos( CUR_TREE *cur_tree, AT_NUMB rank );
204 int CurTreeGetPos( CUR_TREE *cur_tree );
205 int CurTreeSetPos( CUR_TREE *cur_tree, int len );
206 int CurTreeAddAtom( CUR_TREE *cur_tree, int at_no );
207 int CurTreeRemoveLastAtom( CUR_TREE *cur_tree );
208 int CurTreeIsLastRank( CUR_TREE *cur_tree, AT_NUMB rank );
209 int CurTreeIsLastAtomEqu( CUR_TREE *cur_tree, int at_no, AT_NUMB *nSymmStereo );
210 int CurTreeRemoveIfLastAtom( CUR_TREE *cur_tree, int at_no );
211 int CurTreeRemoveLastRankIfNoAtoms( CUR_TREE *cur_tree );
212 void CurTreeKeepLastAtomsOnly( CUR_TREE *cur_tree, int tpos, int shift );
213 
214 
215 void SetUseAtomForStereo( S_CHAR *bAtomUsedForStereo, sp_ATOM *at, int num_atoms );
216 
217 int     nJoin2Mcrs( AT_RANK *nEqArray, AT_RANK n1, AT_RANK n2 );
218 AT_RANK nGetMcr( AT_RANK *nEqArray, AT_RANK n );
219 int     bUniqueAtNbrFromMappingRank( AT_RANK **pRankStack, AT_RANK nAtRank, AT_NUMB *nAtNumber );
220 
221 int Next_SB_At_CanonRanks2( AT_RANK *canon_rank1, AT_RANK *canon_rank2, /*  canonical numbers */
222                           AT_RANK *canon_rank1_min, AT_RANK *canon_rank2_min,
223                           int *bFirstTime, S_CHAR *bAtomUsedForStereo,
224                           const ppAT_RANK pRankStack1, const ppAT_RANK pRankStack2,
225                           const AT_RANK *nCanonRankFrom, const AT_RANK *nAtomNumberCanonFrom,
226                           const sp_ATOM *at, int num_atoms, int bAllene );
227 
228 int Next_SC_At_CanonRank2( AT_RANK *canon_rank1,    /*  1st call input: largest canon number mapped so far or 0 */
229                                                     /*  output: suggested canon. rank > than input if success */
230                           AT_RANK *canon_rank1_min, /*  1st call:0 next calls: first tried canon. number */
231                           int *bFirstTime,          /*  1 at the time of the 1st call  */
232                           S_CHAR *bAtomUsedForStereo, /*  STEREO_AT_MARK if the atom has not been mapped yet */
233                     const ppAT_RANK pRankStack1,    /*  mapping ranks/sort order of atoms with canon. numbers (from) */
234                     const ppAT_RANK pRankStack2,    /*  mapping ranks/sort order of atoms with stereo (to) */
235                     const AT_RANK *nAtomNumberCanonFrom, /*  sorted order of the canon. numbers */
236                     int num_atoms );
237 
238 int NextStereoParity2Test( int *stereo_bond_parity, int *sb_parity_calc,
239                          int nNumBest, int nNumWorse, int nNumUnkn, int nNumUndf, int nNumCalc,
240                          int vABParityUnknown);
241 
242 int All_SB_Same(  AT_RANK canon_rank1, AT_RANK canon_rank2, /*  canonical numbers */
243                   const ppAT_RANK pRankStack1, const ppAT_RANK pRankStack2,
244                   const AT_RANK *nAtomNumberCanonFrom,
245                   sp_ATOM *at );
246 
247 int All_SC_Same(  AT_RANK canon_rank1, /*  canonical number */
248                   const ppAT_RANK pRankStack1, const ppAT_RANK pRankStack2,
249                   const AT_RANK *nAtomNumberCanonFrom,
250                   const sp_ATOM *at );
251 
252 
253 int CompareLinCtStereoDoubleToValues( AT_STEREO_DBLE *LinearCTStereoDble,
254                               AT_RANK at_rank_canon1, AT_RANK at_rank_canon2, U_CHAR bond_parity );
255 
256 int CompareLinCtStereoAtomToValues( AT_STEREO_CARB *LinearCTStereoCarb,
257                               AT_RANK at_rank_canon1, U_CHAR parity );
258 int CompareLinCtStereoDble ( AT_STEREO_DBLE *LinearCTStereoDble1, int nLenLinearCTStereoDble1,
259                              AT_STEREO_DBLE *LinearCTStereoDble2, int nLenLinearCTStereoDble2 );
260 int CompareLinCtStereoCarb ( AT_STEREO_CARB *LinearCTStereoCarb1, int nLenLinearCTStereoCarb1,
261                              AT_STEREO_CARB *LinearCTStereoCarb2, int nLenLinearCTStereoCarb2 );
262 int CompareLinCtStereo ( AT_STEREO_DBLE *LinearCTStereoDble1, int nLenLinearCTStereoDble1,
263                          AT_STEREO_CARB *LinearCTStereoCarb1, int nLenLinearCTStereoCarb1,
264                          AT_STEREO_DBLE *LinearCTStereoDble2, int nLenLinearCTStereoDble2,
265                          AT_STEREO_CARB *LinearCTStereoCarb2, int nLenLinearCTStereoCarb2 );
266 /***************************************************************************/
267 /* ichicans.c */
268 int UnmarkNonStereo( sp_ATOM *at, int num_atoms, const AT_RANK *nRank, const AT_RANK *nAtomNumber, int bIsotopic );
269 int FillSingleStereoDescriptors(sp_ATOM *at, int i, int num_trans, const AT_RANK *nRank
270                           , AT_STEREO_CARB *LinearCTStereoCarb, int *nStereoCarbLen, int nMaxStereoCarbLen
271                           , AT_STEREO_DBLE *LinearCTStereoDble, int *nStereoDbleLen, int nMaxStereoDbleLen
272                           , int bAllene );
273 void SwitchAtomStereoAndIsotopicStereo( sp_ATOM *at, int num_atoms, int *bSwitched );
274 void SetCtToIsotopicStereo( CANON_STAT *pCS, CANON_STAT *pCS2 );
275 void SetCtToNonIsotopicStereo( CANON_STAT *pCS, CANON_STAT *pCS2 );
276 int FillAllStereoDescriptors( sp_ATOM *at, int num_atoms, const AT_RANK *nCanonRank, const AT_RANK *nAtomNumberCanon, CANON_STAT *pCS );
277 int FillOutStereoParities( sp_ATOM *at, int num_atoms, const AT_RANK *nCanonRank, const AT_RANK *nAtomNumberCanon,
278                            const AT_RANK *nRank, const AT_RANK *nAtomNumber, CANON_STAT *pCS, int bIsotopic );
279 int InvertStereo( sp_ATOM *at, int num_at_tg,
280                   AT_RANK *nCanonRank, AT_RANK *nAtomNumberCanon,
281                   CANON_STAT *pCS, int bInvertLinearCTStereo );
282 int find_atoms_with_parity( sp_ATOM *at, S_CHAR *visited, int from_atom, int cur_atom );
283 int GetStereoNeighborPos( sp_ATOM *at, int iAt1, int iAt2 );
284 int GetStereoBondParity(sp_ATOM *at, int i, int n, AT_RANK *nRank );
285 int GetStereoCenterParity(sp_ATOM *at, int i, AT_RANK *nRank );
286 int GetPermutationParity( sp_ATOM *at, AT_RANK nAvoidNeighbor, AT_RANK *nCanonRank );
287 
288 #ifndef COMPILE_ALL_CPP
289 #ifdef __cplusplus
290 }
291 #endif
292 #endif
293 
294 
295 #endif /* __INCHICOMN_H__ */
296