1 | #include <stdlib.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <string.h> |
---|
4 | #include <errno.h> |
---|
5 | #include <stdarg.h> |
---|
6 | #include <ctype.h> |
---|
7 | #include "clustalw.h" |
---|
8 | |
---|
9 | #include <gets_noOverflow.h> |
---|
10 | |
---|
11 | extern char **seq_array; |
---|
12 | extern sint *seqlen_array; |
---|
13 | extern char **names,**titles; |
---|
14 | extern sint *output_index; |
---|
15 | extern sint *seq_weight; |
---|
16 | extern double **tmat; |
---|
17 | |
---|
18 | |
---|
19 | /* |
---|
20 | * ckalloc() |
---|
21 | * |
---|
22 | * Tries to allocate "bytes" bytes of memory. Exits program if failed. |
---|
23 | * Return value: |
---|
24 | * Generic pointer to the newly allocated memory. |
---|
25 | */ |
---|
26 | |
---|
27 | void *ckalloc(size_t bytes) |
---|
28 | { |
---|
29 | register void *ret; |
---|
30 | |
---|
31 | if( (ret = calloc(bytes, sizeof(char))) == NULL) |
---|
32 | /* |
---|
33 | if( (ret = malloc(bytes)) == NULL) |
---|
34 | */ |
---|
35 | fatal("Out of memory\n"); |
---|
36 | else |
---|
37 | return ret; |
---|
38 | |
---|
39 | return ret; |
---|
40 | } |
---|
41 | |
---|
42 | /* |
---|
43 | * ckrealloc() |
---|
44 | * |
---|
45 | * Tries to reallocate "bytes" bytes of memory. Exits program if failed. |
---|
46 | * Return value: |
---|
47 | * Generic pointer to the re-allocated memory. |
---|
48 | */ |
---|
49 | |
---|
50 | void *ckrealloc(void *ptr, size_t bytes) |
---|
51 | { |
---|
52 | register void *ret=NULL; |
---|
53 | |
---|
54 | if (ptr == NULL) |
---|
55 | fatal("Bad call to ckrealloc\n"); |
---|
56 | else if( (ret = realloc(ptr, bytes)) == NULL) |
---|
57 | fatal("Out of memory\n"); |
---|
58 | else |
---|
59 | return ret; |
---|
60 | |
---|
61 | return ret; |
---|
62 | } |
---|
63 | |
---|
64 | /* |
---|
65 | * ckfree() |
---|
66 | * |
---|
67 | * Tries to free memory allocated by ckalloc. |
---|
68 | * Return value: |
---|
69 | * None. |
---|
70 | */ |
---|
71 | |
---|
72 | void *ckfree(void *ptr) |
---|
73 | { |
---|
74 | if (ptr == NULL) |
---|
75 | warning("Bad call to ckfree\n"); |
---|
76 | else { |
---|
77 | free(ptr); |
---|
78 | ptr = NULL; |
---|
79 | } |
---|
80 | return ptr; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | /* |
---|
85 | * rtrim() |
---|
86 | * |
---|
87 | * Removes trailing blanks from a string |
---|
88 | * |
---|
89 | * Return values: |
---|
90 | * Pointer to the processed string |
---|
91 | */ |
---|
92 | |
---|
93 | char * rtrim(char *str) |
---|
94 | { |
---|
95 | register int p; |
---|
96 | |
---|
97 | p = strlen(str) - 1; |
---|
98 | |
---|
99 | while ( isspace(str[p]) ) |
---|
100 | p--; |
---|
101 | |
---|
102 | str[p + 1] = EOS; |
---|
103 | |
---|
104 | return str; |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /* |
---|
109 | * blank_to_() |
---|
110 | * |
---|
111 | * Replace blanks in a string with underscores |
---|
112 | * |
---|
113 | * Also replaces , ; : ( or ) with _ |
---|
114 | * |
---|
115 | * Return value: |
---|
116 | * Pointer to the processed string |
---|
117 | */ |
---|
118 | |
---|
119 | char * blank_to_(char *str) |
---|
120 | { |
---|
121 | int i,p; |
---|
122 | |
---|
123 | p = strlen(str) - 1; |
---|
124 | |
---|
125 | for(i=0;i<=p;i++) |
---|
126 | if( |
---|
127 | (str[i]==' ') || |
---|
128 | (str[i]==';') || |
---|
129 | (str[i]==',') || |
---|
130 | (str[i]=='(') || |
---|
131 | (str[i]==')') || |
---|
132 | (str[i]==':') |
---|
133 | ) |
---|
134 | str[i] = '_'; |
---|
135 | |
---|
136 | return str; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | /* |
---|
141 | * upstr() |
---|
142 | * |
---|
143 | * Converts string str to uppercase. |
---|
144 | * Return values: |
---|
145 | * Pointer to the converted string. |
---|
146 | */ |
---|
147 | |
---|
148 | char * upstr(char *str) |
---|
149 | { |
---|
150 | register char *s = str; |
---|
151 | |
---|
152 | while( (*s = toupper(*s)) ) |
---|
153 | s++; |
---|
154 | |
---|
155 | return str; |
---|
156 | } |
---|
157 | |
---|
158 | /* |
---|
159 | * lowstr() |
---|
160 | * |
---|
161 | * Converts string str to lower case. |
---|
162 | * Return values: |
---|
163 | * Pointer to the converted string. |
---|
164 | */ |
---|
165 | |
---|
166 | char * lowstr(char *str) |
---|
167 | { |
---|
168 | register char *s = str; |
---|
169 | |
---|
170 | while( (*s = tolower(*s)) ) |
---|
171 | s++; |
---|
172 | |
---|
173 | return str; |
---|
174 | } |
---|
175 | |
---|
176 | void getstr(const char *instr,char *outstr, int outstrSize) |
---|
177 | { |
---|
178 | fprintf(stdout,"%s: ",instr); |
---|
179 | gets_noOverflow(outstr, outstrSize); |
---|
180 | } |
---|
181 | |
---|
182 | double getreal(const char *instr,double minx,double maxx,double def) |
---|
183 | { |
---|
184 | int status; |
---|
185 | float ret; |
---|
186 | char line[MAXLINE]; |
---|
187 | |
---|
188 | while(TRUE) { |
---|
189 | fprintf(stdout,"%s (%.1f-%.1f) [%.1f]: ",instr,minx,maxx,def); |
---|
190 | gets_noOverflow(line, MAXLINE); |
---|
191 | status=sscanf(line,"%f",&ret); |
---|
192 | if(status == EOF) return def; |
---|
193 | if(ret>maxx) { |
---|
194 | fprintf(stdout,"ERROR: Max. value=%.1f\n\n",maxx); |
---|
195 | continue; |
---|
196 | } |
---|
197 | if(ret<minx) { |
---|
198 | fprintf(stdout,"ERROR: Min. value=%.1f\n\n",minx); |
---|
199 | continue; |
---|
200 | } |
---|
201 | break; |
---|
202 | } |
---|
203 | return (double)ret; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | int getint(const char *instr,int minx,int maxx, int def) |
---|
208 | { |
---|
209 | int ret,status; |
---|
210 | char line[MAXLINE]; |
---|
211 | |
---|
212 | while(TRUE) { |
---|
213 | fprintf(stdout,"%s (%d..%d) [%d]: ", |
---|
214 | instr,(pint)minx,(pint)maxx,(pint)def); |
---|
215 | gets_noOverflow(line, MAXLINE); |
---|
216 | status=sscanf(line,"%d",&ret); |
---|
217 | if(status == EOF) return def; |
---|
218 | if(ret>maxx) { |
---|
219 | fprintf(stdout,"ERROR: Max. value=%d\n\n",(pint)maxx); |
---|
220 | continue; |
---|
221 | } |
---|
222 | if(ret<minx) { |
---|
223 | fprintf(stdout,"ERROR: Min. value=%d\n\n",(pint)minx); |
---|
224 | continue; |
---|
225 | } |
---|
226 | break; |
---|
227 | } |
---|
228 | return ret; |
---|
229 | } |
---|
230 | |
---|
231 | void do_system(void) |
---|
232 | { |
---|
233 | char line[MAXLINE]; |
---|
234 | |
---|
235 | getstr("\n\nEnter system command",line, MAXLINE); |
---|
236 | if(*line != EOS) |
---|
237 | system(line); |
---|
238 | fprintf(stdout,"\n\n"); |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | Boolean linetype(const char *line,const char *code) |
---|
243 | { |
---|
244 | return( strncmp(line,code,strlen(code)) == 0 ); |
---|
245 | } |
---|
246 | |
---|
247 | Boolean keyword(const char *line,const char *code) |
---|
248 | { |
---|
249 | int i; |
---|
250 | char key[MAXLINE]; |
---|
251 | |
---|
252 | for(i=0;!isspace(line[i]) && line[i]!=EOS;i++) |
---|
253 | key[i]=line[i]; |
---|
254 | key[i]=EOS; |
---|
255 | return( strcmp(key,code) == 0 ); |
---|
256 | } |
---|
257 | |
---|
258 | Boolean blankline(char *line) |
---|
259 | { |
---|
260 | int i; |
---|
261 | |
---|
262 | for(i=0;line[i]!='\n' && line[i]!=EOS;i++) { |
---|
263 | if( isdigit(line[i]) || |
---|
264 | isspace(line[i]) || |
---|
265 | (line[i] == '*') || |
---|
266 | (line[i] == ':') || |
---|
267 | (line[i] == '.')) |
---|
268 | ; |
---|
269 | else |
---|
270 | return FALSE; |
---|
271 | } |
---|
272 | return TRUE; |
---|
273 | } |
---|
274 | |
---|
275 | |
---|
276 | void get_path(char *str,char *path) |
---|
277 | { |
---|
278 | register int i; |
---|
279 | |
---|
280 | strcpy(path,str); |
---|
281 | for(i=strlen(path)-1;i>-1;--i) { |
---|
282 | if(str[i]==DIRDELIM) { |
---|
283 | i = -1; |
---|
284 | break; |
---|
285 | } |
---|
286 | if(str[i]=='.') break; |
---|
287 | } |
---|
288 | if(i<0) |
---|
289 | strcat(path,"."); |
---|
290 | else |
---|
291 | path[i+1]=EOS; |
---|
292 | } |
---|
293 | |
---|
294 | void alloc_aln(sint nseqs) |
---|
295 | { |
---|
296 | sint i,j; |
---|
297 | |
---|
298 | seqlen_array = (sint *)ckalloc( (nseqs+1) * sizeof (sint)); |
---|
299 | |
---|
300 | seq_array = (char **)ckalloc( (nseqs + 1) * sizeof (char *) ); |
---|
301 | for(i=0;i<nseqs+1;i++) |
---|
302 | seq_array[i]=NULL; |
---|
303 | |
---|
304 | names = (char **)ckalloc( (nseqs+1) * sizeof (char *) ); |
---|
305 | for(i=1;i<=nseqs;i++) |
---|
306 | names[i] = (char *)ckalloc((MAXNAMES+1) * sizeof (char)); |
---|
307 | |
---|
308 | titles = (char **)ckalloc( (nseqs+1) * sizeof (char *) ); |
---|
309 | for(i=1;i<=nseqs;i++) |
---|
310 | titles[i] = (char *)ckalloc((MAXTITLES+1) * sizeof (char)); |
---|
311 | |
---|
312 | output_index = (sint *)ckalloc( (nseqs+1) * sizeof (sint)); |
---|
313 | |
---|
314 | tmat = (double **) ckalloc( (nseqs+1) * sizeof (double *) ); |
---|
315 | for(i=1;i<=nseqs;i++) |
---|
316 | tmat[i] = (double *)ckalloc( (nseqs+1) * sizeof (double) ); |
---|
317 | for(i=1;i<=nseqs;i++) |
---|
318 | for(j=1;j<=nseqs;j++) |
---|
319 | tmat[i][j]=0.0; |
---|
320 | |
---|
321 | seq_weight = (sint *)ckalloc( (nseqs+1) * sizeof (sint)); |
---|
322 | for(i=1;i<=nseqs;i++) |
---|
323 | seq_weight[i]=100; |
---|
324 | } |
---|
325 | |
---|
326 | void realloc_aln(sint first_seq,sint nseqs) |
---|
327 | { |
---|
328 | sint i,j; |
---|
329 | |
---|
330 | seqlen_array = (sint *)ckrealloc(seqlen_array, (first_seq+nseqs+1) * sizeof (sint)); |
---|
331 | |
---|
332 | seq_array = (char **)ckrealloc(seq_array, (first_seq+nseqs+1) * sizeof (char *) ); |
---|
333 | for(i=first_seq;i<first_seq+nseqs+1;i++) |
---|
334 | seq_array[i]=NULL; |
---|
335 | |
---|
336 | names = (char **)ckrealloc(names, (first_seq+nseqs+1) * sizeof (char *) ); |
---|
337 | for(i=first_seq;i<first_seq+nseqs;i++) |
---|
338 | names[i] = (char *)ckalloc((MAXNAMES+1) * sizeof (char)); |
---|
339 | |
---|
340 | titles = (char **)ckrealloc(titles, (first_seq+nseqs+1) * sizeof (char *) ); |
---|
341 | for(i=first_seq;i<first_seq+nseqs;i++) |
---|
342 | titles[i] = (char *)ckalloc((MAXTITLES+1) * sizeof (char)); |
---|
343 | |
---|
344 | output_index = (sint *)ckrealloc(output_index, (first_seq+nseqs+1) * sizeof (sint)); |
---|
345 | |
---|
346 | seq_weight = (sint *)ckrealloc(seq_weight, (first_seq+nseqs+1) * sizeof (sint)); |
---|
347 | for(i=first_seq;i<first_seq+nseqs;i++) |
---|
348 | seq_weight[i]=100; |
---|
349 | |
---|
350 | tmat = (double **) ckrealloc(tmat, (first_seq+nseqs+1) * sizeof (double *) ); |
---|
351 | for(i=1;i<first_seq;i++) |
---|
352 | tmat[i] = (double *)ckrealloc(tmat[i], (first_seq+nseqs+1) * sizeof (double) ); |
---|
353 | for(i=first_seq;i<first_seq+nseqs;i++) |
---|
354 | tmat[i] = (double *)ckalloc( (first_seq+nseqs+1) * sizeof (double) ); |
---|
355 | for(i=1;i<first_seq;i++) |
---|
356 | for(j=first_seq;j<first_seq+nseqs;j++) |
---|
357 | { |
---|
358 | tmat[i][j]=0.0; |
---|
359 | tmat[j][i]=0.0; |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | void free_aln(sint nseqs) |
---|
364 | { |
---|
365 | sint i; |
---|
366 | |
---|
367 | if(nseqs<=0) return; |
---|
368 | |
---|
369 | seqlen_array = ckfree(seqlen_array); |
---|
370 | |
---|
371 | for(i=1;i<=nseqs;i++) |
---|
372 | seq_array[i] = ckfree(seq_array[i]); |
---|
373 | seq_array = ckfree(seq_array); |
---|
374 | |
---|
375 | for(i=1;i<=nseqs;i++) |
---|
376 | names[i] = ckfree(names[i]); |
---|
377 | names = ckfree(names); |
---|
378 | |
---|
379 | for(i=1;i<=nseqs;i++) |
---|
380 | titles[i] = ckfree(titles[i]); |
---|
381 | titles = ckfree(titles); |
---|
382 | |
---|
383 | output_index = ckfree(output_index); |
---|
384 | |
---|
385 | seq_weight = ckfree(seq_weight); |
---|
386 | |
---|
387 | for(i=1;i<=nseqs;i++) |
---|
388 | tmat[i] = ckfree(tmat[i]); |
---|
389 | tmat = ckfree(tmat); |
---|
390 | } |
---|
391 | |
---|
392 | void alloc_seq(sint seq_no,sint length) |
---|
393 | { |
---|
394 | seq_array[seq_no] = (char *)ckalloc((length+2) * sizeof (char)); |
---|
395 | } |
---|
396 | |
---|
397 | void realloc_seq(sint seq_no,sint length) |
---|
398 | { |
---|
399 | seq_array[seq_no] = (char *)realloc(seq_array[seq_no], (length+2) * sizeof (char)); |
---|
400 | |
---|
401 | } |
---|
402 | |
---|
403 | void free_seq(sint seq_no) |
---|
404 | { |
---|
405 | seq_array[seq_no]=ckfree(seq_array[seq_no]); |
---|
406 | } |
---|
407 | |
---|