| 1 | #include <stdio.h> |
|---|
| 2 | #include <math.h> |
|---|
| 3 | #include <stdlib.h> |
|---|
| 4 | #include <string.h> |
|---|
| 5 | #include <ctype.h> |
|---|
| 6 | #include "clustalw.h" |
|---|
| 7 | #include "matrices.h" |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | /* |
|---|
| 11 | * Prototypes |
|---|
| 12 | */ |
|---|
| 13 | static Boolean commentline(char *line); |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | /* |
|---|
| 17 | * Global variables |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | extern char *amino_acid_codes; |
|---|
| 21 | extern sint gap_pos1, gap_pos2; |
|---|
| 22 | extern sint max_aa; |
|---|
| 23 | extern short def_dna_xref[],def_aa_xref[]; |
|---|
| 24 | extern sint mat_avscore; |
|---|
| 25 | extern sint debug; |
|---|
| 26 | extern Boolean dnaflag; |
|---|
| 27 | |
|---|
| 28 | extern Boolean user_series; |
|---|
| 29 | extern UserMatSeries matseries; |
|---|
| 30 | extern short usermatseries[MAXMAT][NUMRES][NUMRES]; |
|---|
| 31 | extern short aa_xrefseries[MAXMAT][NUMRES+1]; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | void init_matrix(void) |
|---|
| 35 | { |
|---|
| 36 | |
|---|
| 37 | char c1,c2; |
|---|
| 38 | short i, j, maxres; |
|---|
| 39 | |
|---|
| 40 | max_aa = strlen(amino_acid_codes)-2; |
|---|
| 41 | gap_pos1 = NUMRES-2; /* code for gaps inserted by clustalw */ |
|---|
| 42 | gap_pos2 = NUMRES-1; /* code for gaps already in alignment */ |
|---|
| 43 | |
|---|
| 44 | /* |
|---|
| 45 | set up cross-reference for default matrices hard-coded in matrices.h |
|---|
| 46 | */ |
|---|
| 47 | for (i=0;i<NUMRES;i++) def_aa_xref[i] = -1; |
|---|
| 48 | for (i=0;i<NUMRES;i++) def_dna_xref[i] = -1; |
|---|
| 49 | |
|---|
| 50 | maxres = 0; |
|---|
| 51 | for (i=0;(c1=amino_acid_order[i]);i++) |
|---|
| 52 | { |
|---|
| 53 | for (j=0;(c2=amino_acid_codes[j]);j++) |
|---|
| 54 | { |
|---|
| 55 | if (c1 == c2) |
|---|
| 56 | { |
|---|
| 57 | def_aa_xref[i] = j; |
|---|
| 58 | maxres++; |
|---|
| 59 | break; |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | if ((def_aa_xref[i] == -1) && (amino_acid_order[i] != '*')) |
|---|
| 63 | { |
|---|
| 64 | error("residue %c in matrices.h is not recognised", |
|---|
| 65 | amino_acid_order[i]); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | maxres = 0; |
|---|
| 70 | for (i=0;(c1=nucleic_acid_order[i]);i++) |
|---|
| 71 | { |
|---|
| 72 | for (j=0;(c2=amino_acid_codes[j]);j++) |
|---|
| 73 | { |
|---|
| 74 | if (c1 == c2) |
|---|
| 75 | { |
|---|
| 76 | def_dna_xref[i] = j; |
|---|
| 77 | maxres++; |
|---|
| 78 | break; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | if ((def_dna_xref[i] == -1) && (nucleic_acid_order[i] != '*')) |
|---|
| 82 | { |
|---|
| 83 | error("nucleic acid %c in matrices.h is not recognised", |
|---|
| 84 | nucleic_acid_order[i]); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | sint get_matrix(short *matptr, short *xref, sint matrix[NUMRES][NUMRES], Boolean neg_flag, sint scale) |
|---|
| 90 | { |
|---|
| 91 | sint gg_score = 0; |
|---|
| 92 | sint gr_score = 0; |
|---|
| 93 | sint i, j, k, ix = 0; |
|---|
| 94 | sint ti, tj; |
|---|
| 95 | sint maxres; |
|---|
| 96 | sint av1,av2,av3,min, max; |
|---|
| 97 | /* |
|---|
| 98 | default - set all scores to 0 |
|---|
| 99 | */ |
|---|
| 100 | for (i=0;i<=max_aa;i++) |
|---|
| 101 | for (j=0;j<=max_aa;j++) |
|---|
| 102 | matrix[i][j] = 0; |
|---|
| 103 | |
|---|
| 104 | ix = 0; |
|---|
| 105 | maxres = 0; |
|---|
| 106 | for (i=0;i<=max_aa;i++) |
|---|
| 107 | { |
|---|
| 108 | ti = xref[i]; |
|---|
| 109 | for (j=0;j<=i;j++) |
|---|
| 110 | { |
|---|
| 111 | tj = xref[j]; |
|---|
| 112 | if ((ti != -1) && (tj != -1)) |
|---|
| 113 | { |
|---|
| 114 | k = matptr[ix]; |
|---|
| 115 | if (ti==tj) |
|---|
| 116 | { |
|---|
| 117 | matrix[ti][ti] = k * scale; |
|---|
| 118 | maxres++; |
|---|
| 119 | } |
|---|
| 120 | else |
|---|
| 121 | { |
|---|
| 122 | matrix[ti][tj] = k * scale; |
|---|
| 123 | matrix[tj][ti] = k * scale; |
|---|
| 124 | } |
|---|
| 125 | ix++; |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | --maxres; |
|---|
| 131 | |
|---|
| 132 | av1 = av2 = av3 = 0; |
|---|
| 133 | for (i=0;i<=max_aa;i++) |
|---|
| 134 | { |
|---|
| 135 | for (j=0;j<=i;j++) |
|---|
| 136 | { |
|---|
| 137 | av1 += matrix[i][j]; |
|---|
| 138 | if (i==j) |
|---|
| 139 | { |
|---|
| 140 | av2 += matrix[i][j]; |
|---|
| 141 | } |
|---|
| 142 | else |
|---|
| 143 | { |
|---|
| 144 | av3 += matrix[i][j]; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | av1 /= (maxres*maxres)/2; |
|---|
| 150 | av2 /= maxres; |
|---|
| 151 | av3 /= ((float)(maxres*maxres-maxres))/2; |
|---|
| 152 | mat_avscore = -av3; |
|---|
| 153 | |
|---|
| 154 | min = max = matrix[0][0]; |
|---|
| 155 | for (i=0;i<=max_aa;i++) |
|---|
| 156 | for (j=1;j<=i;j++) |
|---|
| 157 | { |
|---|
| 158 | if (matrix[i][j] < min) min = matrix[i][j]; |
|---|
| 159 | if (matrix[i][j] > max) max = matrix[i][j]; |
|---|
| 160 | } |
|---|
| 161 | if (debug>1) fprintf(stdout,"maxres %d\n",(pint)max_aa); |
|---|
| 162 | if (debug>1) fprintf(stdout,"average mismatch score %d\n",(pint)av3); |
|---|
| 163 | if (debug>1) fprintf(stdout,"average match score %d\n",(pint)av2); |
|---|
| 164 | if (debug>1) fprintf(stdout,"average score %d\n",(pint)av1); |
|---|
| 165 | |
|---|
| 166 | /* |
|---|
| 167 | if requested, make a positive matrix - add -(lowest score) to every entry |
|---|
| 168 | */ |
|---|
| 169 | if (neg_flag == FALSE) |
|---|
| 170 | { |
|---|
| 171 | |
|---|
| 172 | if (debug>1) fprintf(stdout,"min %d max %d\n",(pint)min,(pint)max); |
|---|
| 173 | if (min < 0) |
|---|
| 174 | { |
|---|
| 175 | for (i=0;i<=max_aa;i++) |
|---|
| 176 | { |
|---|
| 177 | ti = xref[i]; |
|---|
| 178 | if (ti != -1) |
|---|
| 179 | { |
|---|
| 180 | for (j=0;j<=max_aa;j++) |
|---|
| 181 | { |
|---|
| 182 | tj = xref[j]; |
|---|
| 183 | /* |
|---|
| 184 | if (tj != -1) matrix[ti][tj] -= (2*av3); |
|---|
| 185 | */ |
|---|
| 186 | if (tj != -1) matrix[ti][tj] -= min; |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | /* |
|---|
| 192 | gr_score = av3; |
|---|
| 193 | gg_score = -av3; |
|---|
| 194 | */ |
|---|
| 195 | |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | for (i=0;i<gap_pos1;i++) |
|---|
| 201 | { |
|---|
| 202 | matrix[i][gap_pos1] = gr_score; |
|---|
| 203 | matrix[gap_pos1][i] = gr_score; |
|---|
| 204 | matrix[i][gap_pos2] = gr_score; |
|---|
| 205 | matrix[gap_pos2][i] = gr_score; |
|---|
| 206 | } |
|---|
| 207 | matrix[gap_pos1][gap_pos1] = gg_score; |
|---|
| 208 | matrix[gap_pos2][gap_pos2] = gg_score; |
|---|
| 209 | matrix[gap_pos2][gap_pos1] = gg_score; |
|---|
| 210 | matrix[gap_pos1][gap_pos2] = gg_score; |
|---|
| 211 | |
|---|
| 212 | maxres += 2; |
|---|
| 213 | |
|---|
| 214 | return(maxres); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | sint read_matrix_series(char *filename, short *usermat, short *xref) |
|---|
| 219 | { |
|---|
| 220 | FILE *fd = NULL, *matfd = NULL; |
|---|
| 221 | char mat_filename[FILENAMELEN]; |
|---|
| 222 | char inline1[1024]; |
|---|
| 223 | sint maxres = 0; |
|---|
| 224 | sint nmat; |
|---|
| 225 | sint n,llimit,ulimit; |
|---|
| 226 | |
|---|
| 227 | if (filename[0] == '\0') |
|---|
| 228 | { |
|---|
| 229 | error("comparison matrix not specified"); |
|---|
| 230 | return((sint)0); |
|---|
| 231 | } |
|---|
| 232 | if ((fd=fopen(filename,"r"))==NULL) |
|---|
| 233 | { |
|---|
| 234 | error("cannot open %s", filename); |
|---|
| 235 | return((sint)0); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | /* check the first line to see if it's a series or a single matrix */ |
|---|
| 239 | while (fgets(inline1,1024,fd) != NULL) |
|---|
| 240 | { |
|---|
| 241 | if (commentline(inline1)) continue; |
|---|
| 242 | if(linetype(inline1,"CLUSTAL_SERIES")) |
|---|
| 243 | user_series=TRUE; |
|---|
| 244 | else |
|---|
| 245 | user_series=FALSE; |
|---|
| 246 | break; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | /* it's a single matrix */ |
|---|
| 250 | if(user_series == FALSE) |
|---|
| 251 | { |
|---|
| 252 | fclose(fd); |
|---|
| 253 | maxres=read_user_matrix(filename,usermat,xref); |
|---|
| 254 | return(maxres); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | /* it's a series of matrices, find the next MATRIX line */ |
|---|
| 258 | nmat=0; |
|---|
| 259 | matseries.nmat=0; |
|---|
| 260 | while (fgets(inline1,1024,fd) != NULL) |
|---|
| 261 | { |
|---|
| 262 | if (commentline(inline1)) continue; |
|---|
| 263 | if(linetype(inline1,"MATRIX")) |
|---|
| 264 | { |
|---|
| 265 | if(sscanf(inline1+6,"%d %d %s",&llimit,&ulimit,mat_filename)!=3) |
|---|
| 266 | { |
|---|
| 267 | error("Bad format in file %s\n",filename); |
|---|
| 268 | fclose(fd); |
|---|
| 269 | return((sint)0); |
|---|
| 270 | } |
|---|
| 271 | if(llimit<0 || llimit > 100 || ulimit <0 || ulimit>100) |
|---|
| 272 | { |
|---|
| 273 | error("Bad format in file %s\n",filename); |
|---|
| 274 | fclose(fd); |
|---|
| 275 | return((sint)0); |
|---|
| 276 | } |
|---|
| 277 | if(ulimit<=llimit) |
|---|
| 278 | { |
|---|
| 279 | error("in file %s: lower limit is greater than upper (%d-%d)\n",filename,llimit,ulimit); |
|---|
| 280 | fclose(fd); |
|---|
| 281 | return((sint)0); |
|---|
| 282 | } |
|---|
| 283 | n=read_user_matrix(mat_filename,&usermatseries[nmat][0][0],&aa_xrefseries[nmat][0]); |
|---|
| 284 | if(n<=0) |
|---|
| 285 | { |
|---|
| 286 | error("Bad format in matrix file %s\n",mat_filename); |
|---|
| 287 | fclose(fd); |
|---|
| 288 | return((sint)0); |
|---|
| 289 | } |
|---|
| 290 | matseries.mat[nmat].llimit=llimit; |
|---|
| 291 | matseries.mat[nmat].ulimit=ulimit; |
|---|
| 292 | matseries.mat[nmat].matptr=&usermatseries[nmat][0][0]; |
|---|
| 293 | matseries.mat[nmat].aa_xref=&aa_xrefseries[nmat][0]; |
|---|
| 294 | nmat++; |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | fclose(fd); |
|---|
| 298 | matseries.nmat=nmat; |
|---|
| 299 | |
|---|
| 300 | maxres=n; |
|---|
| 301 | return(maxres); |
|---|
| 302 | |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | sint read_user_matrix(char *filename, short *usermat, short *xref) |
|---|
| 306 | { |
|---|
| 307 | double f; |
|---|
| 308 | FILE *fd; |
|---|
| 309 | sint numargs,farg; |
|---|
| 310 | sint i, j, k = 0; |
|---|
| 311 | char codes[NUMRES]; |
|---|
| 312 | char inline1[1024]; |
|---|
| 313 | char *args[NUMRES+4]; |
|---|
| 314 | char c1,c2; |
|---|
| 315 | sint ix1, ix = 0; |
|---|
| 316 | sint maxres = 0; |
|---|
| 317 | float scale; |
|---|
| 318 | |
|---|
| 319 | if (filename[0] == '\0') |
|---|
| 320 | { |
|---|
| 321 | error("comparison matrix not specified"); |
|---|
| 322 | return((sint)0); |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | if ((fd=fopen(filename,"r"))==NULL) |
|---|
| 326 | { |
|---|
| 327 | error("cannot open %s", filename); |
|---|
| 328 | return((sint)0); |
|---|
| 329 | } |
|---|
| 330 | maxres = 0; |
|---|
| 331 | while (fgets(inline1,1024,fd) != NULL) |
|---|
| 332 | { |
|---|
| 333 | if (commentline(inline1)) continue; |
|---|
| 334 | if(linetype(inline1,"CLUSTAL_SERIES")) |
|---|
| 335 | { |
|---|
| 336 | error("in %s - single matrix expected.", filename); |
|---|
| 337 | fclose(fd); |
|---|
| 338 | return((sint)0); |
|---|
| 339 | } |
|---|
| 340 | /* |
|---|
| 341 | read residue characters. |
|---|
| 342 | */ |
|---|
| 343 | k = 0; |
|---|
| 344 | for (j=0;j<strlen(inline1);j++) |
|---|
| 345 | { |
|---|
| 346 | if (isalpha((int)inline1[j])) codes[k++] = inline1[j]; |
|---|
| 347 | if (k>NUMRES) |
|---|
| 348 | { |
|---|
| 349 | error("too many entries in matrix %s",filename); |
|---|
| 350 | fclose(fd); |
|---|
| 351 | return((sint)0); |
|---|
| 352 | } |
|---|
| 353 | } |
|---|
| 354 | codes[k] = '\0'; |
|---|
| 355 | break; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | if (k == 0) |
|---|
| 359 | { |
|---|
| 360 | error("wrong format in matrix %s",filename); |
|---|
| 361 | fclose(fd); |
|---|
| 362 | return((sint)0); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | /* |
|---|
| 366 | cross-reference the residues |
|---|
| 367 | */ |
|---|
| 368 | for (i=0;i<NUMRES;i++) xref[i] = -1; |
|---|
| 369 | |
|---|
| 370 | maxres = 0; |
|---|
| 371 | for (i=0;(c1=codes[i]);i++) |
|---|
| 372 | { |
|---|
| 373 | for (j=0;(c2=amino_acid_codes[j]);j++) |
|---|
| 374 | if (c1 == c2) |
|---|
| 375 | { |
|---|
| 376 | xref[i] = j; |
|---|
| 377 | maxres++; |
|---|
| 378 | break; |
|---|
| 379 | } |
|---|
| 380 | if ((xref[i] == -1) && (codes[i] != '*')) |
|---|
| 381 | { |
|---|
| 382 | warning("residue %c in matrix %s not recognised", |
|---|
| 383 | codes[i],filename); |
|---|
| 384 | } |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | /* |
|---|
| 389 | get the weights |
|---|
| 390 | */ |
|---|
| 391 | |
|---|
| 392 | ix = ix1 = 0; |
|---|
| 393 | while (fgets(inline1,1024,fd) != NULL) |
|---|
| 394 | { |
|---|
| 395 | if (inline1[0] == '\n') continue; |
|---|
| 396 | if (inline1[0] == '#' || |
|---|
| 397 | inline1[0] == '!') break; |
|---|
| 398 | numargs = getargs(inline1, args, (int)(k+1)); |
|---|
| 399 | if (numargs < maxres) |
|---|
| 400 | { |
|---|
| 401 | error("wrong format in matrix %s",filename); |
|---|
| 402 | fclose(fd); |
|---|
| 403 | return((sint)0); |
|---|
| 404 | } |
|---|
| 405 | if (isalpha(args[0][0])) farg=1; |
|---|
| 406 | else farg=0; |
|---|
| 407 | |
|---|
| 408 | /* decide whether the matrix values are float or decimal */ |
|---|
| 409 | scale=1.0; |
|---|
| 410 | for(i=0;i<strlen(args[farg]);i++) |
|---|
| 411 | if(args[farg][i]=='.') |
|---|
| 412 | { |
|---|
| 413 | /* we've found a float value */ |
|---|
| 414 | scale=10.0; |
|---|
| 415 | break; |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | for (i=0;i<=ix;i++) |
|---|
| 419 | { |
|---|
| 420 | if (xref[i] != -1) |
|---|
| 421 | { |
|---|
| 422 | f = atof(args[i+farg]); |
|---|
| 423 | usermat[ix1++] = (short)(f*scale); |
|---|
| 424 | } |
|---|
| 425 | } |
|---|
| 426 | ix++; |
|---|
| 427 | } |
|---|
| 428 | if (ix != k+1) |
|---|
| 429 | { |
|---|
| 430 | error("wrong format in matrix %s",filename); |
|---|
| 431 | fclose(fd); |
|---|
| 432 | return((sint)0); |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | maxres += 2; |
|---|
| 437 | fclose(fd); |
|---|
| 438 | |
|---|
| 439 | return(maxres); |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | int getargs(char *inline1,char *args[],int max) |
|---|
| 443 | { |
|---|
| 444 | |
|---|
| 445 | char *inptr; |
|---|
| 446 | /* |
|---|
| 447 | #ifndef MAC |
|---|
| 448 | char *strtok(char *s1, const char *s2); |
|---|
| 449 | #endif |
|---|
| 450 | */ |
|---|
| 451 | int i; |
|---|
| 452 | |
|---|
| 453 | inptr=inline1; |
|---|
| 454 | for (i=0;i<=max;i++) |
|---|
| 455 | { |
|---|
| 456 | if ((args[i]=strtok(inptr," \t\n"))==NULL) |
|---|
| 457 | break; |
|---|
| 458 | inptr=NULL; |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | return(i); |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | static Boolean commentline(char *line) |
|---|
| 466 | { |
|---|
| 467 | int i; |
|---|
| 468 | |
|---|
| 469 | if(line[0] == '#') return TRUE; |
|---|
| 470 | for(i=0;line[i]!='\n' && line[i]!=EOS;i++) { |
|---|
| 471 | if(!isspace(line[i])) |
|---|
| 472 | return FALSE; |
|---|
| 473 | } |
|---|
| 474 | return TRUE; |
|---|
| 475 | } |
|---|
| 476 | |
|---|