ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/freemol/trunk/src/mengine/src/read_cml.c
Revision: 144
Committed: Wed Sep 30 14:34:50 2009 UTC (10 years ago) by gilbertke
File size: 11496 byte(s)
Log Message:
added cml support
Line File contents
1 #define EXTERN extern
2
3 #include "pcwin.h"
4 #include "pcmod.h"
5
6 EXTERN struct t_files {
7 int nfiles, append, batch, icurrent, ibatno;
8 } files;
9 EXTERN struct ElementType { char symbol[3];
10 int atomnum;
11 float weight, extweight, covradius, vdwradius;
12 int s,p,d,f,type ;
13 } Elements[];
14
15 EXTERN char *Struct_Title;
16
17 void write_cml(void);
18 int make_atom(int,float,float,float,char *);
19 void initialize(void);
20 void make_bond(int,int,int);
21 void message_alert(char *, char *);
22 FILE * fopen_path ( char * , char * , char * ) ;
23 int FetchRecord(FILE *, char *);
24 int read_cml(char *);
25 void get_molecule_memory(int);
26 char *get_structure_title(void);
27
28 /* ------------------------------- */
29 int tokenizer(int n,char *string,char *attr, char *value)
30 {
31 int i,j;
32 int icount, jcount;
33
34 icount = jcount = 0;
35 strcpy(attr,"");
36 strcpy(value,"");
37
38 for (i = n; i < strlen(string); i++)
39 {
40 if (string[i] == '>')
41 {
42 attr[icount] = '\0';
43 value[jcount] = '\0';
44 return (i+1);
45 }else if (string[i] == '<')
46 continue;
47 else if (string[i] == ' ')
48 {
49 attr[icount] = '\0';
50 value[jcount] = '\0';
51 return (i+1);
52 }else if (string[i] == '=')
53 {
54 attr[icount] = '\0';
55 i += 2;
56 for (j=i; j < strlen(string); j++)
57 {
58 if (string[j] == '"')
59 {
60 value[jcount] = '\0';
61 return (j+2);
62 } else
63 {
64 value[jcount] = string[j];
65 jcount++;
66 }
67 }
68 } else
69 {
70 attr[icount] = string[i];
71 icount++;
72 }
73 }
74 return(1000);
75 }
76 // ====================================
77 int read_cml(char *infilename)
78 {
79 int i,n, pos;
80 int newatom,niatm,at1,at2,ibo;
81 int Makeatom,Makebond;
82 char atomID[1000][5], as1[5],as2[5];
83 char line[255];
84 char dummy[30],dummy1[90],atomchar[3];
85 float x1,y1,z1;
86 FILE *infile;
87
88 infile = fopen(infilename,"r");
89 if (infile == NULL)
90 {
91 message_alert("Error opening cml file","Error");
92 return -1;
93 }
94 // check for valid XML file should be <?xml
95 FetchRecord(infile,line);
96 *(line+ strlen(line) ) = '\0';
97 sscanf(line,"%s %s",dummy,dummy1);
98 if (strcmp(dummy,"<?xml") != 0)
99 {
100 message_alert("This does not appear to be an XML file","Error");
101 fclose(infile);
102 return -1;
103 }
104 // count number of atoms - then close and reopen
105 niatm = 0;
106 L_20:
107 FetchRecord(infile,line);
108 *(line + strlen(line) ) = '\0';
109 if (feof(infile))
110 goto L_DONE;
111 n = 0;
112 pos = 0;
113
114 while (TRUE)
115 {
116 n = pos;
117 pos = tokenizer(n,line,dummy,dummy1);
118 if (pos == 1000) goto L_20;
119 if (strcmp(dummy,"atomArray") == 0) // start of atoms
120 {
121 L_05:
122 n = pos = 0;
123 FetchRecord(infile,line);
124 L_10:
125 n = pos;
126 pos = tokenizer(n,line,dummy,dummy1);
127 if (pos == 1000) goto L_05;
128 if (strcmp(dummy,"/atomArray") == 0)
129 {
130 fclose(infile);
131 get_molecule_memory(niatm);
132 infile = fopen(infilename,"r");
133 goto L_30;
134 } else if (strcmp(dummy,"atom") == 0)
135 {
136 niatm++;
137 goto L_05;
138 } else
139 goto L_10;
140 }
141 }
142
143 L_30:
144 if (niatm == 0)
145 {
146 printf("no atoms found\n");
147 return -1;
148 }
149 FetchRecord(infile,line);
150 *(line + strlen(line) ) = '\0';
151 if (feof(infile))
152 goto L_DONE;
153 n = 0;
154 pos = 0;
155 // start of token read
156
157 L_40:
158 n = pos;
159 pos = tokenizer(n,line,dummy,dummy1);
160 if (pos == 1000) goto L_30;
161 if (strcmp(dummy,"molecule") == 0)
162 {
163 n = pos;
164 pos = tokenizer(n,line,dummy,dummy1);
165 if (strcmp(dummy,"title") == 0)
166 {
167 strcpy(Struct_Title,dummy1);
168 }
169 goto L_40;
170 } else if (strcmp(dummy,"/molecule") == 0)
171 {
172 goto L_DONE;
173 } else if (strcmp(dummy,"atomArray") == 0)
174 {
175 niatm = 0;
176 L_50:
177 n = 0;
178 pos = 0;
179 FetchRecord(infile,line);
180 *(line + strlen(line) ) = '\0';
181 if (feof(infile))
182 goto L_DONE;
183 L_51:
184 n = pos;
185 pos = tokenizer(n,line,dummy,dummy1);
186 if (pos == 1000) goto L_50;
187 if (strcmp(dummy,"/atomArray") == 0)
188 goto L_30;
189 else if (strcmp(dummy,"atom") == 0)
190 {
191 x1 = y1 = z1 = 0.0;
192 Makeatom = FALSE;
193 L_60:
194 n = pos;
195 pos = tokenizer(n,line,dummy,dummy1);
196 if (pos == 1000) // end of line
197 {
198 if (Makeatom == FALSE)
199 {
200 newatom = make_atom(0,x1,y1,z1,atomchar);
201 Makeatom = TRUE;
202 }
203 goto L_50;
204 }
205 if (strcmp(dummy,"id") == 0)
206 {
207 strcpy(atomID[niatm],dummy1);
208 niatm++;
209 goto L_60;
210 } else if (strcmp(dummy,"elementType") == 0)
211 {
212 strcpy(atomchar,dummy1);
213 goto L_60;
214 } else if (strcmp(dummy,"formalCharge") == 0)
215 {
216 goto L_60;
217 } else if (strcmp(dummy,"x2") == 0)
218 {
219 sscanf(dummy1,"%f",&x1);
220 goto L_60;
221 } else if (strcmp(dummy,"x3") == 0)
222 {
223 sscanf(dummy1,"%f",&x1);
224 goto L_60;
225 } else if (strcmp(dummy,"y2") == 0)
226 {
227 sscanf(dummy1,"%f",&y1);
228 goto L_60;
229 } else if (strcmp(dummy,"y3") == 0)
230 {
231 sscanf(dummy1,"%f",&y1);
232 goto L_60;
233 } else if (strcmp(dummy,"z3") == 0)
234 {
235 sscanf(dummy1,"%f",&z1);
236 goto L_60;
237 } else if (strcmp(dummy,"/atom") == 0)
238 {
239 newatom = make_atom(0,x1,y1,z1,atomchar);
240 goto L_50;
241 } else if (strcmp(dummy,"/") == 0)
242 {
243 newatom = make_atom(0,x1,y1,z1,atomchar);
244 Makeatom = TRUE;
245 goto L_50;
246 }
247 else
248 goto L_60;
249 }
250 goto L_51; // unrecognized token
251 } else if (strcmp(dummy,"bondArray") == 0)
252 {
253 L_70:
254 n = 0;
255 pos = 0;
256 FetchRecord(infile,line);
257 *(line + strlen(line) ) = '\0';
258 if (feof(infile))
259 goto L_DONE;
260 L_71:
261 n = pos;
262 pos = tokenizer(n,line,dummy,dummy1);
263 if (strcmp(dummy,"/bondArray") == 0)
264 goto L_30;
265 else if (strcmp(dummy,"bond") == 0)
266 {
267 at1 = at2 = 0;
268 Makebond = FALSE;
269 L_80:
270 n = pos;
271 pos = tokenizer(n,line,dummy,dummy1);
272 if (pos == 1000 && Makebond == FALSE)
273 {
274 Makebond = TRUE;
275 if (at1 > 0 && at2 > 0)
276 make_bond(at1,at2,ibo);
277 goto L_70;
278 }
279 if (strcmp(dummy,"atomRefs2") == 0)
280 {
281 sscanf(dummy1,"%s %s",as1,as2);
282 for (i=0; i < niatm; i++)
283 {
284 if (strcmp(as1,atomID[i]) == 0)
285 {
286 at1 = i+1;
287 break;
288 }
289 }
290 for (i=0; i < niatm; i++)
291 {
292 if (strcmp(as2,atomID[i]) == 0)
293 {
294 at2 = i+1;
295 break;
296 }
297 }
298 goto L_80;
299 } else if (strcmp(dummy,"atomRef") == 0)
300 {
301 sscanf(dummy1,"%s",as1);
302 for (i=0; i < niatm; i++)
303 {
304 if (strcmp(as1,atomID[i]) == 0)
305 {
306 if (at1 == 0)
307 at1 = i+1;
308 else
309 at2 = i+1;
310 break;
311 }
312 }
313 goto L_80;
314 } else if (strcmp(dummy,"atomRef1") == 0)
315 {
316 sscanf(dummy1,"%s",as1);
317 for (i=0; i < niatm; i++)
318 {
319 if (strcmp(as1,atomID[i]) == 0)
320 {
321 at1 = i+1;
322 break;
323 }
324 }
325 goto L_80;
326 } else if (strcmp(dummy,"atomRef2") == 0)
327 {
328 sscanf(dummy1,"%s",as2);
329 for (i=0; i < niatm; i++)
330 {
331 if (strcmp(as2,atomID[i]) == 0)
332 {
333 at2 = i+1;
334 break;
335 }
336 }
337 goto L_80;
338 } else if (strcmp(dummy,"order") == 0)
339 {
340 sscanf(dummy1,"%s",as1);
341 if (strcmp(as1,"S") == 0)
342 ibo = 1;
343 else if (strcmp(as1,"D") == 0)
344 ibo = 2;
345 else if (strcmp(as1,"A") == 0) // aromatic bond
346 ibo = 1;
347 else
348 ibo = atoi(as1);
349 goto L_80;
350 } else if (strcmp(dummy,"/bond") == 0)
351 {
352 Makebond = TRUE;
353 if (at1 > 0 && at2 > 0)
354 make_bond(at1,at2,ibo);
355 goto L_70;
356 } else
357 goto L_80;
358 }
359 goto L_71;
360 }
361 goto L_40;
362 L_DONE:
363 fclose(infile);
364 return TRUE;
365 }
366 // ==============================================
367 void write_cml()
368 {
369 int i,j,nbond;
370 int at1,at2,ibo;
371 FILE *wfile = pcmoutfile;
372
373 //
374 /* ** calculate the number of bonds in the molecule ** */
375 nbond = 0;
376 for( j = 1; j <= natom; j++ )
377 {
378 for( i = 0; i < atom.nconnect[j]; i++ )
379 {
380 if( atom.iat[j][i] != 0 )
381 {
382 if( atom.iat[j][i] < j )
383 nbond = nbond + 1;
384 }
385 }
386 }
387 //
388 fprintf(wfile,"<molecule title=\"%s\">\n",Struct_Title);
389 fprintf(wfile,"<atomArray>\n");
390 for (i=1; i<= natom; i++)
391 {
392 fprintf(wfile,"<atom id=\"%d\" elementType=\"%s\" x3=\"%f\" y3=\"%f\" z3=\"%f\"></atom>\n",
393 i,Elements[atom.atomnum[i]-1].symbol,atom.x[i],atom.y[i],atom.z[i]);
394 }
395 fprintf(wfile,"</atomArray>\n");
396 //
397 fprintf(wfile,"<bondArray>\n");
398 for( j = 1; j <= natom; j++ )
399 {
400 for( i = 0; i < atom.nconnect[j]; i++ )
401 {
402 if( atom.iat[j][i] != 0 )
403 {
404 if (atom.iat[j][i] > j)
405 {
406 at1 = j;
407 at2 = atom.iat[j][i];
408 ibo = atom.bo[j][i];
409 fprintf(wfile,"<bond atomRef1=\"%d\" atomRef2=\"%d\" order=\"%d\"></bond>\n",at1,at2,ibo);
410 }
411 }
412 }
413 }
414 fprintf(wfile,"</bondArray>\n");
415 fprintf(wfile,"</molecule>\n");
416
417 fclose(wfile);
418 }
419