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