Branch data Line data Source code
1 : : /*
2 : : * International Chemical Identifier (InChI)
3 : : * Version 1
4 : : * Software version 1.07
5 : : * April 30, 2024
6 : : *
7 : : * MIT License
8 : : *
9 : : * Copyright (c) 2024 IUPAC and InChI Trust
10 : : *
11 : : * Permission is hereby granted, free of charge, to any person obtaining a copy
12 : : * of this software and associated documentation files (the "Software"), to deal
13 : : * in the Software without restriction, including without limitation the rights
14 : : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 : : * copies of the Software, and to permit persons to whom the Software is
16 : : * furnished to do so, subject to the following conditions:
17 : : *
18 : : * The above copyright notice and this permission notice shall be included in all
19 : : * copies or substantial portions of the Software.
20 : : *
21 : : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 : : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 : : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 : : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 : : * SOFTWARE.
28 : : *
29 : : * The InChI library and programs are free software developed under the
30 : : * auspices of the International Union of Pure and Applied Chemistry (IUPAC).
31 : : * Originally developed at NIST.
32 : : * Modifications and additions by IUPAC and the InChI Trust.
33 : : * Some portions of code were developed/changed by external contributors
34 : : * (either contractor or volunteer) which are listed in the file
35 : : * 'External-contributors' included in this distribution.
36 : : *
37 : : * info@inchi-trust.org
38 : : *
39 : : */
40 : :
41 : : #include "mode.h"
42 : : #include "ichicomn.h"
43 : :
44 : : #include "bcf_s.h"
45 : :
46 : : /**********************************************************************************/
47 : 619 : AT_ISO_SORT_KEY make_iso_sort_key( int iso_atw_diff, int num_1H, int num_2H, int num_3H )
48 : : {
49 : 619 : AT_ISO_SORT_KEY iso_sort_key = 0, mult = 1;
50 : :
51 : 619 : iso_sort_key += mult * num_1H;
52 : 619 : mult *= AT_ISO_SORT_KEY_MULT;
53 : 619 : iso_sort_key += mult * num_2H;
54 : 619 : mult *= AT_ISO_SORT_KEY_MULT;
55 : 619 : iso_sort_key += mult * num_3H;
56 : 619 : mult *= AT_ISO_SORT_KEY_MULT;
57 : 619 : iso_sort_key += mult * iso_atw_diff;
58 : :
59 : 619 : return iso_sort_key;
60 : : }
61 : :
62 : :
63 : :
64 : : /****************************************************************************
65 : : Set sp_ATOM isotopic sort keys
66 : : ****************************************************************************/
67 : 69 : int set_atom_iso_sort_keys( int num_at,
68 : : sp_ATOM *at,
69 : : T_GROUP_INFO* t_group_info,
70 : : int *bHasIsotopicInTautomerGroups )
71 : : {
72 : 69 : int i, num_isotopic = 0, bMergedTgroup;
73 : : AT_ISO_SORT_KEY iso_sort_key;
74 : 69 : T_GROUP *t_group =
75 : 69 : ( t_group_info &&
76 [ + - ]: 69 : t_group_info->t_group &&
77 [ + - - + ]: 138 : t_group_info->num_t_groups > 0 ) ? t_group_info->t_group : NULL;
78 : :
79 [ + - ]: 69 : if (bHasIsotopicInTautomerGroups)
80 : : {
81 : 69 : *bHasIsotopicInTautomerGroups = 0;
82 : : }
83 [ + + ]: 688 : for (i = 0; i < num_at; i++)
84 : : {
85 [ + - - + : 619 : bMergedTgroup = ( t_group_info && t_group_info->nIsotopicEndpointAtomNumber && ( at[i].cFlags & AT_FLAG_ISO_H_POINT ) );
- - ]
86 [ - + - - : 619 : if (( !at[i].endpoint || !t_group ) && !bMergedTgroup)
+ - ]
87 : : {
88 : 619 : iso_sort_key = make_iso_sort_key( at[i].iso_atw_diff, at[i].num_iso_H[0], at[i].num_iso_H[1], at[i].num_iso_H[2] );
89 : : }
90 : : else
91 : : {
92 : : /* H isotopes go to the tautomer part of the CT (name) */
93 : : /* if (at[i].endpoint && t_group) ... */
94 : 0 : iso_sort_key = make_iso_sort_key( at[i].iso_atw_diff, 0, 0, 0 );
95 [ # # ]: 0 : if (bHasIsotopicInTautomerGroups)
96 : : {
97 [ # # # # : 0 : *bHasIsotopicInTautomerGroups += ( at[i].num_iso_H[0] || at[i].num_iso_H[1] || at[i].num_iso_H[2] || bMergedTgroup );
# # # # ]
98 : : }
99 : : }
100 : 619 : at[i].iso_sort_key = iso_sort_key;
101 : 619 : num_isotopic += ( iso_sort_key != 0 );
102 : : }
103 : :
104 : 69 : return num_isotopic;
105 : : }
106 : :
107 : :
108 : : #ifdef NEVER
109 : :
110 : :
111 : : /****************************************************************************/
112 : : int unpack_iso_sort_key( AT_ISO_SORT_KEY iso_sort_key,
113 : : S_CHAR *num_1H,
114 : : S_CHAR *num_2H,
115 : : S_CHAR *num_3H,
116 : : S_CHAR *iso_atw_diff )
117 : : {
118 : : int is_negative;
119 : : AT_ISO_SORT_KEY HOnlyAtwPart;
120 : : static const AT_ISO_SORT_KEY MultAtwDiff = AT_ISO_SORT_KEY_MULT*AT_ISO_SORT_KEY_MULT*AT_ISO_SORT_KEY_MULT;
121 : : if (!iso_sort_key)
122 : : {
123 : : *num_1H = *num_2H = *num_3H = *iso_atw_diff = 0;
124 : : return 0;
125 : : }
126 : : else
127 : : {
128 : : if (iso_sort_key < 0)
129 : : {
130 : : is_negative = 1;
131 : : iso_sort_key = -iso_sort_key;
132 : : HOnlyAtwPart = MultAtwDiff - iso_sort_key % MultAtwDiff;
133 : : iso_sort_key += HOnlyAtwPart;
134 : : }
135 : : else
136 : : {
137 : : is_negative = 0;
138 : : HOnlyAtwPart = iso_sort_key % MultAtwDiff;
139 : : iso_sort_key -= HOnlyAtwPart;
140 : : }
141 : : }
142 : :
143 : : iso_sort_key /= MultAtwDiff;
144 : :
145 : : *num_1H = (S_CHAR) ( HOnlyAtwPart % AT_ISO_SORT_KEY_MULT );
146 : : HOnlyAtwPart /= AT_ISO_SORT_KEY_MULT;
147 : : *num_2H = (S_CHAR) ( HOnlyAtwPart % AT_ISO_SORT_KEY_MULT );
148 : : HOnlyAtwPart /= AT_ISO_SORT_KEY_MULT;
149 : : *num_3H = (S_CHAR) ( HOnlyAtwPart % AT_ISO_SORT_KEY_MULT );
150 : :
151 : : *iso_atw_diff = (S_CHAR) ( is_negative ? -iso_sort_key : iso_sort_key );
152 : :
153 : : return 1;
154 : : }
155 : : #endif
|