ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/freemol/trunk/src/mengine/src/sring.c
Revision: 58
Committed: Mon Dec 1 06:44:59 2008 UTC (12 years, 6 months ago) by wdelano
File size: 9914 byte(s)
Log Message:
code merge 20081130
Line File contents
1 #define EXTERN extern
2 #include "pcwin.h"
3 #include "pcmod.h"
4 #include "utility.h"
5
6 int is_cyclo3(int, int *);
7 int is_cyclo4(int, int *);
8 int is_cyclo5(int, int *);
9 int is_cyclo6(int, int *);
10 int icompare(int, int *,int *);
11 int find_rsize(int,int);
12 int aromatic_5(int,int *);
13 int aromatic_6(int *);
14 void get_rsize(int,int,int,int *);
15 void search_rings(int);
16 void gmmxsort(int, int *);
17
18 struct t_ringdata {
19 int nring3, nring4, nring5, nring6;
20 int tot3, tot4, tot5, tot6;
21 int **iring3,**iring4,**iring5,**iring6;
22 } ringdata;
23
24 void search_rings(int mode)
25 {
26 int i,j,ij,k,unique,ifive,isix;
27 int array[7],array1[6],array2[6];
28 long int mask3, mask4, mask5, mask6, aromatic_mask;
29
30 if (mode == -1)
31 {
32 if (ringdata.nring3 > 0)
33 free_imatrix(ringdata.iring3, 0,ringdata.nring3, 0,4);
34 if (ringdata.nring4 > 0)
35 free_imatrix(ringdata.iring4, 0,ringdata.nring4, 0,5);
36 if (ringdata.nring5 > 0)
37 free_imatrix(ringdata.iring5, 0,ringdata.nring5, 0,6);
38 if (ringdata.nring6 > 0)
39 free_imatrix(ringdata.iring6, 0,ringdata.nring6, 0,7);
40 return;
41 }
42
43 mask3 = (1L << RING3);
44 mask4 = (1L << RING4);
45 mask5 = (1L << RING5);
46 mask6 = (1L << RING6);
47 aromatic_mask = (1L << AROMATIC_MASK);
48
49 for (i=1; i <= natom; i++)
50 {
51 atom[i].flags &= ~aromatic_mask;
52 }
53
54 for (i=1; i <= natom; i++)
55 {
56 if (atom[i].atomnum > 1)
57 {
58 if ( !(atom[i].flags & mask3)) // cyclopropane
59 {
60 if (is_cyclo3(i, array))
61 atom[i].flags |= mask3;
62 }
63 if ( !(atom[i].flags & mask4)) // cyclobutane
64 {
65 if (is_cyclo4(i, array))
66 atom[i].flags |= mask4;
67 }
68 if ( !(atom[i].flags & mask5)) // cyclopentane
69 {
70 if (is_cyclo5(i,array))
71 {
72 for (j=0; j < 5; j++)
73 atom[array[j]].flags |= mask5;
74 }
75 }
76 if ( !(atom[i].flags & mask6)) // cyclohexane
77 {
78 if (is_cyclo6(i,array))
79 {
80 for (j=0; j < 6; j++)
81 atom[array[j]].flags |= mask6;
82 }
83 }
84 }
85 }
86 // count rings and allocate memory
87 ringdata.nring3 = 0;
88 ringdata.nring4 = 0;
89 ringdata.nring5 = 0;
90 ringdata.nring6 = 0;
91 for (i=1; i <= natom; i++)
92 {
93 if (atom[i].flags & mask3)
94 ringdata.nring3++;
95 if (atom[i].flags & mask4)
96 ringdata.nring4++;
97 if (atom[i].flags & mask5)
98 ringdata.nring5++;
99 if (atom[i].flags & mask6)
100 ringdata.nring6++;
101 }
102 // ringdata.nring6 /= 2;
103
104 if (ringdata.nring3 > 0)
105 ringdata.iring3 = imatrix(0,ringdata.nring3, 0,4);
106 if (ringdata.nring4 > 0)
107 ringdata.iring4 = imatrix(0,ringdata.nring4, 0,5);
108 if (ringdata.nring5 > 0)
109 ringdata.iring5 = imatrix(0,ringdata.nring5, 0,6);
110 if (ringdata.nring6 > 0)
111 ringdata.iring6 = imatrix(0,ringdata.nring6, 0,7);
112
113 for (i=0; i < ringdata.nring5; i++)
114 {
115 for (j=0; j < 5; j++)
116 ringdata.iring5[i][j] = 0;
117 ringdata.iring5[i][5] = FALSE;
118 }
119 for (i=0; i < ringdata.nring6; i++)
120 {
121 for (j=0; j < 6; j++)
122 ringdata.iring6[i][j] = 0;
123 ringdata.iring6[i][6] = FALSE;
124 }
125
126 // find rings
127 ringdata.tot3 = 0;
128 ringdata.tot4 = 0;
129 ringdata.tot5 = 0;
130 ringdata.tot6 = 0;
131
132 for (i=1; i <= natom; i++)
133 {
134 if (atom[i].flags & mask3)
135 {
136 is_cyclo3(i,array);
137 unique = TRUE;
138 for (j=0; j < 3 ;j++)
139 array1[j] = array[j];
140 for(j=0; j < ringdata.tot3; j++)
141 {
142 for (k=0; k < 3; k++)
143 array2[k] = ringdata.iring3[j][k];
144 if ( icompare(3,array1,array2))
145 unique = FALSE;
146 }
147 if (unique == TRUE)
148 {
149 ringdata.iring3[ringdata.tot3][0] = array[0];
150 ringdata.iring3[ringdata.tot3][1] = array[1];
151 ringdata.iring3[ringdata.tot3][2] = array[2];
152 ringdata.tot3++;
153 }
154 }
155 if (atom[i].flags & mask4)
156 {
157 is_cyclo4(i,array);
158 unique = TRUE;
159 for (j=0; j < 4 ;j++)
160 array1[j] = array[j];
161 for(j=0; j < ringdata.tot4; j++)
162 {
163 for (k=0; k < 4; k++)
164 array2[k] = ringdata.iring4[j][k];
165 if ( icompare(4,array1,array2))
166 unique = FALSE;
167 }
168 if (unique == TRUE)
169 {
170 ringdata.iring4[ringdata.tot4][0] = array[0];
171 ringdata.iring4[ringdata.tot4][1] = array[1];
172 ringdata.iring4[ringdata.tot4][2] = array[2];
173 ringdata.iring4[ringdata.tot4][3] = array[3];
174 ringdata.tot4++;
175 }
176 }
177 if (atom[i].flags & mask5)
178 {
179 is_cyclo5(i,array);
180 unique = TRUE;
181 for (j=0; j < 5 ;j++)
182 array1[j] = array[j];
183 for(j=0; j < ringdata.tot5; j++)
184 {
185 for (k=0; k < 5; k++)
186 array2[k] = ringdata.iring5[j][k];
187 if ( icompare(5,array1,array2))
188 unique = FALSE;
189 }
190 if (unique == TRUE)
191 {
192 ringdata.iring5[ringdata.tot5][0] = array[0];
193 ringdata.iring5[ringdata.tot5][1] = array[1];
194 ringdata.iring5[ringdata.tot5][2] = array[2];
195 ringdata.iring5[ringdata.tot5][3] = array[3];
196 ringdata.iring5[ringdata.tot5][4] = array[4];
197 ringdata.tot5++;
198 }
199 }
200 if (atom[i].flags & mask6)
201 {
202 is_cyclo6(i,array);
203 unique = TRUE;
204 for (j=0; j < 6 ;j++)
205 array1[j] = array[j];
206 for(j=0; j < ringdata.tot6; j++)
207 {
208 for (k=0; k < 6; k++)
209 array2[k] = ringdata.iring6[j][k];
210 if ( icompare(6,array1,array2))
211 unique = FALSE;
212 }
213 if (unique == TRUE)
214 {
215 ringdata.iring6[ringdata.tot6][0] = array[0];
216 ringdata.iring6[ringdata.tot6][1] = array[1];
217 ringdata.iring6[ringdata.tot6][2] = array[2];
218 ringdata.iring6[ringdata.tot6][3] = array[3];
219 ringdata.iring6[ringdata.tot6][4] = array[4];
220 ringdata.iring6[ringdata.tot6][5] = array[5];
221 ringdata.tot6++;
222 }
223 }
224 }
225 // now do ring map for fused rings
226 for (i=1; i <= natom; i++)
227 {
228 if ( !(atom[i].flags & aromatic_mask) )
229 {
230 ifive = find_rsize(5,i);
231 isix = find_rsize(6,i);
232 if ( isix > 1) // 6-6 fused ring
233 {
234 continue;
235 } else if (isix == 1 && ifive == 1) // 6-5 fused ring system
236 {
237 for (ij=0; ij < ringdata.tot6; ij++)
238 {
239 get_rsize(i,6,ij,array);
240 isix = aromatic_6(array);
241 if (isix == TRUE)
242 {
243 for(j=0; j < 6; j++)
244 {
245 atom[array[j]].flags |= aromatic_mask;
246 }
247 ringdata.iring6[ij][6] = TRUE;
248 }else
249 ringdata.iring6[ij][6] = FALSE;
250 }
251 for (ij=0; ij < ringdata.tot5; ij++)
252 {
253 get_rsize(i,5,ij,array);
254 isix = aromatic_5(ij,array);
255 if (isix == TRUE)
256 {
257 for(j=0; j < 5; j++)
258 {
259 atom[array[j]].flags |= aromatic_mask;
260 }
261 ringdata.iring5[ij][5] = TRUE;
262 }else
263 ringdata.iring5[ij][5] = FALSE;
264 }
265 } else if (ifive > 1) // 5-5 fused ring
266 {
267 continue;
268 }
269 }
270 }
271 // now do rest of rings - six rings first
272 for (i=0; i < ringdata.tot6; i++)
273 {
274 for(j=0; j < 6; j++)
275 array[j] = ringdata.iring6[i][j];
276 isix = aromatic_6(array);
277 if (isix == TRUE)
278 {
279 for(j=0; j < 6; j++)
280 atom[ringdata.iring6[i][j]].flags |= aromatic_mask;
281 ringdata.iring6[i][6] = TRUE;
282 }else
283 ringdata.iring6[i][6] = FALSE;
284 }
285 for (i=0; i < ringdata.tot5; i++)
286 {
287 for(j=0; j < 5; j++)
288 array[j] = ringdata.iring5[i][j];
289 ifive = aromatic_5(i,array);
290 if (ifive == TRUE)
291 {
292 for(j=0; j < 5; j++)
293 atom[ringdata.iring5[i][j]].flags |= aromatic_mask;
294 ringdata.iring5[i][5] = TRUE;
295 }else
296 ringdata.iring5[i][5] = FALSE;
297 }
298 }
299 /* ======================================= */
300 int icompare(int iatom, int *iarray1, int *iarray2)
301 {
302 int i;
303
304 gmmxsort(iatom, iarray1);
305 gmmxsort(iatom, iarray2);
306 for (i=0; i < iatom; i++)
307 {
308 if (iarray1[i] != iarray2[i])
309 return FALSE;
310 }
311 return TRUE;
312 }
313 /* ======================================= */
314 void gmmxsort(int iatom, int *iarray)
315 {
316 int i, j, itemp;
317
318 for (i=0; i < iatom-1; i++)
319 {
320 for(j=i+1; j < iatom; j++)
321 {
322 if (iarray[i] > iarray[j])
323 {
324 itemp = iarray[i];
325 iarray[i] = iarray[j];
326 iarray[j] = itemp;
327 }
328 }
329 }
330 }