1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
|
/* (C) Copyright 1993-2001, Fred Hutchinson Cancer Research Center */
/* Use, modification or distribution of these programs is subject to */
/* the terms of the non-commercial licensing agreement in license.h. */
/* convert.c: functions for different methods of converting a block into a */
/* matrix */
/* Written by: Bill Alford */
/* Change log information is at the end of the file. */
/* system headers not in global.h */
#include <assert.h>
#include <math.h>
/* blimps library headers */
#include <global.h>
#include <options.h>
#include <blocks.h> /* includes sequences.h, output.h */
#include <matrix.h> /* includes pattern.h */
#include <residues.h>
#include <frequency.h>
#include <protomat.h> /* for MAXLINE */
#include <convert.h>
/* headers in current directory */
#include "blimps.h"
/*
* Exported variables and data structures
*/
double RTot;
struct float_qij *Qij;
/*
* Local variables and data structures
*/
/*
* Function definitions
*/
/*
* --OLD-- Sequence weighting methods. Various methods of giving different
* weights to the sequences.
*/
static double *clustered_weights(/*block*/);
static double *pre_weighted_sequences(/*block*/);
/*
* --OLD-- Matrix construction methods. Build matricies from blocks and
* sequence weights.
*/
static void basic_matrix_construction(/*block, seq_weight, matrix*/);
/*
* Conversion methods
* void default_conversion_method(block, matrix)
* void original_conversion_method_cleaned_up(block, matrix)
* void original_conversion_method(block, matrix)
* void pre_weighted_conversion_method(block, matrix)
* void altschul_data_dependent_conversion_method(block, matrix)
* void gribskov_conversion_method(block, matrix)
*/
/*
* block_to_matrix
* converts a block into a matrix (possition specific matrix?) according
* to the rule specified in BlockToMatrixConversionMethod. The block field
* of the matrix is set in this function.
* Parameters:
* Block *block: the block to convert
* Return codes: Returns the pointer to the new Matrix.
* Error codes:
*/
Matrix *block_to_matrix(block, conversion_method)
Block *block;
int conversion_method;
{
Matrix *matrix;
char *tmp;
/* get new matrix */
matrix = new_matrix(block->width);
/* initialize the pattern */
matrix->patterns = NULL;
/* copy the relevant block information into the matrix */
matrix->block = block;
strncpy(Buffer, block->id, SMALL_BUFF_LENGTH);
/* NOTE: Chance to goof by replacing the wrong "BLOCK" */
tmp = strstr(Buffer, "BLOCK");
if (tmp != NULL) {
strncpy(strstr(Buffer, "BLOCK"), "MATRIX\0", 7);
strncpy(matrix->id, Buffer, SMALL_BUFF_LENGTH);
}
else {
strncpy(matrix->id,
strncat(Buffer, "; MATRIX", SMALL_BUFF_LENGTH - strlen(Buffer)),
SMALL_BUFF_LENGTH);
}
strcpy(matrix->ac, block->ac);
strncpy(matrix->de, block->de, DESC_WIDTH);
strncpy(matrix->ma, block->bl, SMALL_BUFF_LENGTH);
/* Just leave it BLxxxxx */
strcpy(matrix->number, block->number);
strncpy(matrix->motif, block->motif, 20);
/* This 20 is from the size in the struct */
matrix->width = block->width;
matrix->max_length = matrix->width;
matrix->percentile = block->percentile;
matrix->strength = block->strength;
matrix->num_sequences = block->num_sequences;
switch (conversion_method) {
case 0: /* seq wts from clumps, odds ratios */
original_conversion_method_cleaned_up(block, matrix);
break;
case 1: /* seq wts from clumps, odds ratios */
original_conversion_method(block, matrix);
break;
case 2: /* seq wts from block else pb_weights(), odds ratios */
pre_weighted_conversion_method(block, matrix);
break;
/* DEFAULT is case 3 (scale=0) */
/* cases 3-40 use seq wts from block else pb_weights() */
/* cases 3-6, 10, 20 use position-specific pseudo counts */
/* require frequency[] = default.amino.frq, RTot and */
/* Qij = default.qij which must be initialized by calling program */
case 3: /* Altschul's data-dependent method of computing a PSSM */
/* log_e(odds ratios)=nats, positive values */
altschul_data_dependent_conversion_method(block, matrix, 0);
break;
case 4: /* log_2(odds ratios)=bits, signed integers */
altschul_data_dependent_conversion_method(block, matrix, 1);
break;
case 5: /* log_2(odds ratios)/2= half bits, signed integers */
altschul_data_dependent_conversion_method(block, matrix, 2);
break;
case 6: /* log_2(odds ratios)/3= third bits, signed integers */
altschul_data_dependent_conversion_method(block, matrix, 3);
break;
case 9: /* pseudo-counts close to zero */
/* log_e(odds ratios)=nats, positive values */
altschul_data_dependent_conversion_method(block, matrix, 9);
break;
case 10: /* odds ratios */
altschul_data_dependent_conversion_method(block, matrix, 10);
break;
case 20: /* (count[aa]+pseudo-count[aa])/(totcount+totpseudo) */
altschul_data_dependent_conversion_method(block, matrix, 20);
break;
case 21: /* count[aa]/totcount */
altschul_data_dependent_conversion_method(block, matrix, 21);
break;
case 30: /* Gribskov's average score method; loads default.sij */
gribskov_conversion_method(block, matrix);
break;
case 40: /* SIFT method; requires default.rank, default.diri */
/* which are loaded here */
SIFT_conversion_method(block, matrix);
break;
default: /* the default case */
sprintf(ErrorBuffer,
"Invalid block to matrix conversion method specified, %d.",
conversion_method);
ErrorReport(WARNING_ERR_LVL);
sprintf(ErrorBuffer, /* ^^^^----------------vvvvvvvvvvvvvvvvvvv */
"Using the default conversion method of Altschul's data-dependent method.\n");
ErrorReport(WARNING_ERR_LVL);
altschul_data_dependent_conversion_method(block, matrix, 0);
break;
} /* end switch of conversion types */
/* return the matrix */
return matrix;
}
/*
* original_conversion_method_cleaned_up
* The original conversion method. This is done by weighted average of the
* clusters. This follows the method in patmat but has been written in
* a more legible manner and has the following changes:
* no more reliance on flag values for B, Z, and X
* X, '-', '*', & non-code scores are read straight from the frequencies
* the frequencies for B and Z are ignored, when a B or Z is encountered
* it is partitioned between D & N or E & Q.
* the matrix scores for B and Z are computed from the matrix scores of
* D & N and E & Q.
* Parameters:
* Block *block: the block to be converted
* Matrix *matrix: where the resulting matrix will be put
* Error codes: none
*/
void original_conversion_method_cleaned_up(block, matrix)
Block *block;
Matrix *matrix;
{
double *seq_weight; /* the contribution of this sequence to the */
/* block. = 1/(num seq in cluster) */
/* get the weights of the sequences */
seq_weight = clustered_weights(block);
basic_matrix_construction(block, seq_weight, matrix);
free(seq_weight);
}
/*
* pre_weighted_conversion_method
* This conversion method uses the pre-weighted sequence scores and
* uses the basic matrix construction method. This method has the
* following properties:
* X, '-', '*', & non-code scores are read straight from the frequencies
* the frequencies for B and Z are ignored, when a B or Z is encountered
* it is partitioned between D & N or E & Q.
* the matrix scores for B and Z are computed from the matrix scores of
* D & N and E & Q.
* Parameters:
* Block *block: the block to be converted
* Matrix *matrix: where the resulting matrix will be put
* Error codes: none
*/
void pre_weighted_conversion_method(block, matrix)
Block *block;
Matrix *matrix;
{
double *seq_weight; /* the contribution of this sequence to the */
/* block. = the value given in the block */
/* get the weights of the sequences */
seq_weight = pre_weighted_sequences(block);
if (seq_weight != NULL) {
basic_matrix_construction(block, seq_weight, matrix);
free(seq_weight);
}
else {
/* remember that seq_weight has been freed already if NULL is returned */
sprintf(ErrorBuffer,
"All weights in the block were less than or equal to zero.");
ErrorReport(WARNING_ERR_LVL);
sprintf(ErrorBuffer,
"Computing position-based sequence weights.\n");
ErrorReport(WARNING_ERR_LVL);
pb_weights(block);
seq_weight = pre_weighted_sequences(block);
if (seq_weight != NULL) {
basic_matrix_construction(block, seq_weight, matrix);
free(seq_weight);
}
}
} /* end of pre_weighted_convertion_method */
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
/*
* altschul data dependent conversion method aux. structures/defines
*/
/* these are from JGH's makealts, I am not sure where these are from exctly. */
/* I think they are the actual values from the aabet.h structures. */
/* AAS and AASALL are not matched up with any of the aabet.h defines. */
#define AASALL 26
/* 0- 1A 2R 3N 4D 5C 6Q 7E 8G 9H 10I 11L 12K 13M 14F 15P 16S 17T 18W 19Y
20V 21B 22Z 23X 24* 25J,O,U */
#define MAXWIDTH 400 /* Max. block width */
struct working { /* Working information for one column */
double cnt[AASALL]; /* Sequence-weighted counts */
double totcnt;
double raw[AASALL]; /* unweighted counts */
double totraw;
double reg[AASALL]; /* pseudo counts */
double totreg;
double probn[MAXDIRI]; /* for dirichlet */
double probj[MAXDIRI]; /* for dirichlet */
};
struct work_pssm { /* Working PSSM area */
double** value; /* double value[MAXWIDTH][AASALL]*/
double* sum ; /* double sum[MAXWIDTH] */
};
struct rank_cell {
int aa;
double value;
};
/*
* altschul data dependent conversion method aux. functions
*/
/*==================================================================*/
static struct working *make_col()
{
struct working *col;
int aa;
CheckMem(
col = (struct working *) malloc(sizeof(struct working))
);
col->totcnt = col->totreg = 0.0;
for (aa=0; aa < AASALL; aa++) {
col->cnt[aa] = col->reg[aa] = 0.0;
}
return col;
} /* end of make_col */
/*=====================================================================*/
static struct work_pssm *make_work_pssm(length)
int length;
{
struct work_pssm *pssm;
int pos, aa;
double* double_pointer;
CheckMem(
pssm = (struct work_pssm *) malloc(sizeof(struct work_pssm))
);
double_pointer = (double*) calloc (length * AASALL, sizeof (double));
pssm->value = (double **) calloc (length, sizeof (double*));
pssm->sum = (double *) calloc (length, sizeof (double));
for (pos = 0; pos < length; pos++) {
pssm->value[pos] = &(double_pointer[pos * AASALL]);
}
for (pos = 0; pos < length; pos++) {
pssm->sum[pos] = 0.0;
for (aa=0; aa < AASALL; aa++) {
pssm->value[pos][aa] = 0.0;
}
}
return pssm;
} /* end of make_work_pssm */
/*=====================================================================*/
void free_work_pssm(pssm)
struct work_pssm *pssm;
{
free(pssm->value[0]);
free(pssm->value);
free(pssm->sum);
free(pssm);
} /* end of free_work_pssm */
/*======================================================================*/
static void counts(block, col, pos)
Block *block;
struct working *col;
int pos;
{
int seq, aa, aa1;
col->totcnt = col->totraw = col->totreg = 0.0;
for (aa = 0; aa < AASALL; aa++) {
col->cnt[aa] = col->raw[aa] = col->reg[aa] = 0.0;
}
/* Only count the real 20 aas, combine B(21) with D & Z(22) with E */
for (seq = 0; seq < block->num_sequences; seq++) {
aa = block->residues[seq][pos];
if (aa == 21) aa = 4; /* combine B with D */
if (aa == 22) aa = 7; /* combine Z with E */
if (aa >= 1 && aa < AAS) {
col->cnt[aa] += block->sequences[seq].weight;
col->totcnt += block->sequences[seq].weight;
col->raw[aa] += 1.0;
col->totraw += 1.0;
}
else {
/*
sprintf(ErrorBuffer,
"Uncounted \"residue\" character for %s: %c\n",
block->number, aa_btoa[block->residues[seq][pos]]);
ErrorReport(INFO_ERR_LVL);
*/
/* If not one of the basic aas, divide the count among them */
for (aa1 = 1; aa1 < AAS; aa1++)
{
col->cnt[aa1] += (block->sequences[seq].weight / 20.0);
col->raw[aa1] += (1.0 / 20.0) ;
}
col->totcnt += block->sequences[seq].weight;
col->totraw += 1.0;
}
}
} /* end of counts */
/*=====================================================================*/
static int count_residues(col)
struct working *col;
{
int aa, nr;
nr = 0;
for (aa = 1; aa < AAS; aa++) {
if (col->cnt[aa] > 0.0) nr++;
}
return nr;
} /* end of count_residues */
/*=======================================================================*/
static void pseudo_alts(col, qij, epsilon)
struct working *col;
struct float_qij *qij;
double epsilon;
{
int aa, row;
/*---------- get the pseudo counts -------------------------------*/
for (aa=1; aa < AAS; aa++) {
col->reg[aa] = 0.0;
for (row = 1; row < AAS; row++) {
col->reg[aa] += (col->cnt[row] * qij->value[aa][row] / qij->marg[row]);
}
col->reg[aa] *= epsilon;
if (col->totcnt > 0.0) col->reg[aa] /= col->totcnt;
col->totreg += col->reg[aa];
}
} /* end of pseudo_alts */
/*========================================================================
Computes scores for B, Z, X, -(gap) and *(stop) in a column
of a PSSM using other scores in the column
frequency[] is a global array created by load_frequencies()
why is it an argument here?
==========================================================================*/
/* SOME DUPLICATE CODE FROM basic_matrix_construction(). */
static void compute_BZX(frequency, matrix, col)
double *frequency;
Matrix *matrix;
int col;
{
int aa;
double dmean, dmin;
double part_D; /* the partition of D for B. */
/* = freq[D] / ( freq[D] + freq[N] ) */
double part_N; /* the partition of N for B. */
/* = freq[N] / ( freq[D] + freq[N] ) */
double part_E; /* the partition of E for Z. */
/* = freq[E] / ( freq[E] + freq[Q] ) */
double part_Q; /* the partition of Q for Z. */
/* = freq[Q] / ( freq[E] + freq[Q] ) */
/*
* find the partitions of D, N, E, and Q for B and Z
*/
part_D = frequency[aa_atob['D']] /
( frequency[aa_atob['D']] + frequency[aa_atob['N']] );
part_N = frequency[aa_atob['N']] /
( frequency[aa_atob['D']] + frequency[aa_atob['N']] );
part_E = frequency[aa_atob['E']] /
( frequency[aa_atob['E']] + frequency[aa_atob['Q']] );
part_Q = frequency[aa_atob['Q']] /
( frequency[aa_atob['E']] + frequency[aa_atob['Q']] );
/* fill in the matrix for B, Z, X, gap, stop and non */
matrix->weights[aa_atob['B']][col] =
(part_D * matrix->weights[aa_atob['D']][col] +
part_N * matrix->weights[aa_atob['N']][col]);
matrix->weights[aa_atob['Z']][col] =
(part_E * matrix->weights[aa_atob['E']][col] +
part_Q * matrix->weights[aa_atob['Q']][col]);
/* X or unk gets the weighted average score; - and * get the min score */
dmin = 999.99;
dmean = 0.0;
for (aa=1; aa<20; aa++)
{
dmean += frequency[aa] * matrix->weights[aa][col];
if (matrix->weights[aa][col] < dmin) dmin = matrix->weights[aa][col];
}
matrix->weights[aa_atob['X']][col] = dmean;
matrix->weights[aa_atob[25]][col] = dmean; /* unknown res */
matrix->weights[aa_atob['-']][col] = dmin;
if (dmin > 0.0) matrix->weights[aa_atob['*']][col] = 0.0;
else matrix->weights[aa_atob['*']][col] = dmin;
} /* end of compute_BZX */
/*=========================================================================
Adds negative minval to give all positive matrix,
then multiplies by 99/maxval to give scores ranging from 0 to 99
NOTE: Not 0 to 100 because "output_matrix" routine might not leave
enough space.
===========================================================================*/
static void positive_matrix(freqs, pssm, matrix)
double *freqs;
struct work_pssm *pssm;
Matrix *matrix;
{
int pos, aa;
double factor, maxval, minval, dtemp;
minval = 9999.9;
maxval = -9999.9;
for (pos = 0; pos < matrix->width; pos++) {
for (aa=1; aa < AAS; aa++) {
if (pssm->value[pos][aa] < minval) minval = pssm->value[pos][aa];
if (pssm->value[pos][aa] > maxval) maxval = pssm->value[pos][aa];
}
}
if (minval < 0.0) {
factor = 99.0 / (maxval - minval);
}
else {
factor = 99.0 / maxval;
}
if (factor < 1.0) {
factor = 1.0;
}
for (pos = 0; pos < matrix->width; pos++) {
for (aa=1; aa < AAS; aa++) {
if (minval < 0.0) {
dtemp = factor * (pssm->value[pos][aa] - minval);
}
else {
dtemp = factor * pssm->value[pos][aa];
}
matrix->weights[aa][pos] = (MatType) dtemp;
}
compute_BZX(freqs, matrix, pos);
} /* end of for pos */
} /* end of positive_matrix */
/*==========================================================================
Uses Altschul's method of getting pseudo-counts with a qij matrix,
scale=0 => DEFAULT nats, positive integers
scale=1 => bits, signed integers
scale=2 => half bits, signed integers
scale=3 => third bits, signed integers
scale=9 => few pseudo counts, bits, positive integers
scale=10 => odds ratios
scale=20 => counts + pseudo counts
scale=21 => counts
===========================================================================*/
static void make_alts(block, matrix, freqs, qij, RTot, scale)
Block *block;
Matrix *matrix;
double *freqs;
struct float_qij *qij;
double RTot; /* Total R for Altschul */
int scale;
{
double factor, dtemp, epsilon;
int pos, aa;
struct working *col;
struct work_pssm *pssm;
factor = 1.0;
if (scale > 0 && scale < 9) factor = (double) scale / log(2.0);
col = make_col();
pssm = make_work_pssm(block->width);
/*-------------- Do one position at a time -------------------*/
for (pos = 0; pos < block->width; pos++)
{
/*-------- count the number of each aa in this position ------------*/
counts(block, col, pos);
/*-------- determine total number of pseudo-counts in column ------*/
/* 9 => compute minimal number of pseudo-counts; RTot should
be a large number in this case, such as 1000 */
if (scale == 9)
/*
{ epsilon = (double) col->totcnt / RTot ; }
*/
{ epsilon = (double) 1.0 / RTot ; }
else
{ epsilon = (double) RTot * count_residues(col); }
/*---------- get the pseudo counts -------------------------------*/
pseudo_alts(col, qij, epsilon);
/*--------- Fill in the matrix entries --------------------*/
pssm->sum[pos] = 0.0;
for (aa=1; aa < AAS; aa++)
{
/* Count proportions; returned if scale == 21 */
if (scale == 21)
{
pssm->value[pos][aa] = col->cnt[aa];
if ( col->totcnt > 0.0)
pssm->value[pos][aa] /= col->totcnt;
}
/* Count+pseudo proportions; returned if scale == 20 */
if (scale <= 20)
{
pssm->value[pos][aa] = col->cnt[aa] + col->reg[aa];
if ( (col->totcnt + col->totreg) > 0.0)
pssm->value[pos][aa] /= (col->totcnt + col->totreg);
}
/* Odds ratios; returned if scale == 10 */
if (scale < 20 && freqs[aa] > 0.0)
pssm->value[pos][aa] /= freqs[aa];
/* take the log of the odds ratio */
if (scale < 10 && pssm->value[pos][aa] > 0.0)
pssm->value[pos][aa] = log(pssm->value[pos][aa]);
pssm->sum[pos] += pssm->value[pos][aa];
/* scale the matrix */
dtemp = factor * pssm->value[pos][aa];
matrix->weights[aa][pos] = dtemp;
} /* end of aa */
compute_BZX(freqs, matrix, pos);
} /* end of for pos */
/*------ Now make the final scores; make log scores non-neg */
/* by subtracting the min. value for all positions ----- */
if (scale==0 || scale==9) positive_matrix(freqs, pssm, matrix);
free(col);
free_work_pssm(pssm);
} /* end of make_alts */
/*==========================================================================
Uses Gribskov's method
Assumes a non-negative substition matrix (min. value = 0)
Sets 20 aas plus B and Z from the matrix; X, etc to zero = min. value
Index for B is 21, for Z is 22
===========================================================================*/
void make_gribs(block, matrix, subst)
Block *block;
Matrix *matrix;
struct float_qij *subst;
{
double sum, temp[AASALL];
int pos, aa, aa1;
struct working *col;
col = make_col();
for (pos = 0; pos < block->width; pos++)
{
/*-------- count the number of each aa in this position ------------*/
counts(block, col, pos);
sum = 0.0;
/* pauline changed from 22 to AAS . this will skip Z, but
remove compilation warnings*/
for (aa=0; aa <= AAS; aa++)
{
temp[aa] = 0.0;
for (aa1=1; aa1 <= AAS ; aa1++)
{
temp[aa] += col->cnt[aa1] * subst->value[aa][aa1];
}
temp[aa] /= col->totcnt;
sum += temp[aa];
}
for (aa=1; aa <= 22; aa++)
{
/* ends up rounding to zero!
dtemp = temp[aa];
matrix->weights[aa][pos] = round(dtemp);
*/
matrix->weights[aa][pos] = temp[aa];
}
/* Set X, * and - to zero */
matrix->weights[0][pos] = matrix->weights[23][pos] = 0.0;
matrix->weights[24][pos] = matrix->weights[25][pos] = 0.0;
} /* end of for pos */
free(col);
} /* end of make_gribs */
/*======================================================================*/
struct float_qij *load_qij( FILE *fin)
{
char line[LARGE_BUFF_LENGTH], *ptr;
double total;
int alpha[AAS], nrows, ncols, row, col, i;
struct float_qij *new;
CheckMem (
new = (struct float_qij *) malloc(sizeof(struct float_qij))
);
/*----------Read file until first non-blank line --------------*/
/* Skip comments at beginning of file - 1st char = #, > or ; */
line[0] = '\0';
while (((int) strlen(line) < 1 ||
line[0]=='#' || line[0]=='>' || line[0]==';')
&& fgets(line, sizeof(line), fin) != NULL)
;
/*------See if the first line has characters on it ------------*/
for (col=0; col < AAS; col++) alpha[col] = -1;
if (strstr(line, "A") != NULL) { /* This line has characters */
row = 0; /* # of alphabetic characters on the line */
for (i=0; i< (int) strlen(line); i++) {
col = -1;
col = aa_atob[(int)line[i]];
if (col >= 0 && col < AAS) {
alpha[row] = col;
row++;
}
else if (isalpha(line[i])) {
row++;
}
}
}
/*-------Get the data values now ------------*/
for (row=0; row<AAS; row++) {
for (col=0; col<AAS; col++) {
new->value[row][col] = -1.0; /* Null value */
}
}
nrows = 0;
line[0] = '\0';
while (fgets(line, sizeof(line), fin) != NULL) {
if ((int) strlen(line) > 1 && nrows < AAS) {
if (alpha[nrows] >= 0 && alpha[nrows] < AAS) {
row = alpha[nrows]; ncols = 0;
ptr = strtok(line, " ,\n");
while (ptr != NULL) {
if (strspn(ptr, ".+-0123456789") == strlen(ptr)) {
col = alpha[ncols];
if (col >= 0 && col < AAS) {
new->value[row][col] = (double) atof(ptr);
}
ncols++;
}
ptr = strtok(NULL, " ,\n");
}
}
nrows++;
}
}
/*-------If some entries are still missing, assume symmetry ---------*/
for (row=0; row<AAS; row++) {
for (col=0; col<AAS; col++) {
if (new->value[row][col] < 0.0) {
new->value[row][col] = new->value[col][row];
}
}
}
/*-------compute the marginal probabilities ---------*/
total = 0.0;
for (row=1; row<AAS; row++) {
new->marg[row] = 0.0;
for (col=1; col<AAS; col++) {
new->marg[row] += new->value[row][col];
}
total += new->marg[row];
}
return new;
} /* end of load_qij */
/*=========================================================================
Normalize sequence weights to add up to the number of sequences
=========================================================================*/
void normalize(block)
Block *block;
{
double sum, factor;
int seq;
sum = 0.0;
for (seq = 0; seq < block->num_sequences; seq++) {
sum += block->sequences[seq].weight;
}
if (sum <= 0.0) /* All seqs have zero weight! Add pb weights */
{
sprintf(ErrorBuffer,
"All weights in the block were less than or equal to zero.");
ErrorReport(WARNING_ERR_LVL);
sprintf(ErrorBuffer,
"Computing position-based sequence weights.\n");
ErrorReport(WARNING_ERR_LVL);
pb_weights(block);
for (seq = 0; seq < block->num_sequences; seq++) {
sum += block->sequences[seq].weight;
}
}
if (sum > 0.0) factor = block->num_sequences / sum;
else factor = 1.0;
for (seq = 0; seq < block->num_sequences; seq++) {
block->sequences[seq].weight *= factor;
}
} /* end of normalize */
/*
* altschul_data_dependent_conversion_method
* Expects global variables frequency[], Qij[][] and RTot
* Parameters:
* Block *block: the block to be converted
* Matrix *matrix: where the resulting matrix will be put
scale = 0 log odds ratios, positive values
1 bits, log odds ratios
2 half bits, log odds ratios
3 third bits, log odds ratios
9 few pseudo counts
10 odds ratios
20 (counts+pseudo counts)/tot
21 counts
* Error codes: Checks MAXWIDTH, global variables
*/
void altschul_data_dependent_conversion_method(block, matrix, scale)
Block *block;
Matrix *matrix;
int scale;
{
int itemp;
if (block->width > MAXWIDTH)
{
itemp = MAXWIDTH;
sprintf(ErrorBuffer, "convert: Block is too wide, unable to continue (max=%d).\n",
itemp);
ErrorReport(FATAL_ERR_LVL);
}
if (Qij == NULL) {
sprintf(ErrorBuffer, "Qij matrix missing, unable to continue.\n");
ErrorReport(FATAL_ERR_LVL);
}
/* check to see if the block has sequence weights */
normalize(block); /* Make weights sum to number of sequences */
make_alts(block, matrix, frequency, Qij, RTot, scale);
} /* end of altschul_data_dependent_conversion_method */
/*
* gribskov_dependent_conversion_method
* Expects global variables frequency[]
* Loads default.sij
* Parameters:
* Block *block: the block to be converted
* Matrix *matrix: where the resulting matrix will be put
* Error codes: none
*/
void
gribskov_conversion_method(block, matrix)
Block *block;
Matrix *matrix;
{
char sijname[SMALL_BUFF_LENGTH], *blimps_dir;
struct float_qij *sij_matrix;
FILE *fp=NULL;
/* Is this an error? fp is never opened */
/* load the substitution matrix */
blimps_dir = getenv("BLIMPS_DIR");
if (blimps_dir != NULL)
{
sprintf(sijname, "%s/docs/default.sij", blimps_dir);
}
else
{
sprintf(sijname, "default.sij");
}
sij_matrix = load_qij(fp);
fclose(fp);
if (sij_matrix == NULL)
{
sprintf(ErrorBuffer,
"gribskov_conversion_method: default.sij matrix missing, Cannot continue.\n");
ErrorReport(FATAL_ERR_LVL);
}
/* check to see if the block has sequence weights */
normalize(block); /* Make weights sum to number of sequences */
make_gribs(block, matrix, sij_matrix);
}
/* end of gribskov_dependent_conversion_method
* Sequence weighting methods. Various methods of giving different
* weights to the sequences.
*/
/*
* static double *clustered_weights(block)
* static double *pre_weighted_sequences(block)
* void pb_weights(block)
*/
/*
* clustered_weights
* This sequence weighing depends on the clustering of the block.
* The weight of a sequence is one over the number of sequences in
* that cluster.
* Parameters:
* Block* block: the block that the sequences are from.
* Return codes: a pointer to the sequence weights array.
* Error codes:
*/
static double *clustered_weights(block)
Block* block;
{
double *seq_weight; /* the contribution of this sequence to the */
/* block. = 1/(num seq in cluster) */
int clust; /* a cluster counter */
int seq; /* a sequence counter */
int seq_num; /* keep's track of where to start when */
/* filling seq_weight*/
double weight; /* a temporary holder for the weight */
/*
* create and fill the seq_weight array.
*/
/* allocate space */
CheckMem(
seq_weight = (double *) calloc(block->num_sequences, sizeof(double))
);
/* fill the seq_weight array */
seq_num = 0;
for (clust=0; clust<block->num_clusters; clust++) {
weight = 1.0/(double)block->clusters[clust].num_sequences;
/* fill each sequence in the cluster */
for (seq=seq_num; seq<seq_num+block->clusters[clust].num_sequences;
seq++) {
seq_weight[seq] = weight;
}
seq_num += block->clusters[clust].num_sequences;
}
return seq_weight;
} /* end of clustered_weights */
/*
* pre_weighted_sequences
* This sequence weighing was done beforehand. The weight of the
* sequence was read from the block.
* Parameters:
* Block* block: the block that the sequences are from.
* Return codes: a pointer to the sequence weights array.
* Error codes: NULL if all of the weights in the block are <= zero.
*/
static double *pre_weighted_sequences(block)
Block* block;
{
double *seq_weight; /* the contribution of this sequence to the */
/* block. */
int seq; /* a sequence counter */
int num_seqs; /* keep's track of total sequences */
double max_weight;
/*
* create and fill the seq_weight array.
*/
/* allocate space */
CheckMem(
seq_weight = (double *) calloc(block->num_sequences, sizeof(double))
);
num_seqs = block->num_sequences;
/* fill each sequence weight from the block */
max_weight = 0.0;
for (seq=0; seq<num_seqs; seq++) {
seq_weight[seq] = block->sequences[seq].weight;
if (seq_weight[seq] > max_weight) {
max_weight = seq_weight[seq];
}
}
if (max_weight <= 0) {
/* all weights were zero */
free(seq_weight);
return NULL; /* the error is handled in the calling function */
}
return seq_weight;
} /* end of pre_weighted_sequences */
/*=======================================================================
Compute Steve's position-based sequence weights
NOTE: Characters - (0), X (23) and * (24) are ignored
Characters 1-22 are the 20 basic aas plus B & Z
MATRIX_AA_WIDTH = 26, defined in matrix.h
========================================================================*/
void pb_weights(block)
Block *block;
{
struct pb_counts *pb;
//double factor, dtemp;
double dtemp;
int seq, pos, aa, width;
width = block->width;
CheckMem(
pb = (struct pb_counts *) malloc(width*sizeof(struct pb_counts))
);
for (pos = 0; pos < width; pos++)
{
pb[pos].diffaas = 0.0;
for (aa = 0; aa < MATRIX_AA_WIDTH; aa++)
pb[pos].naas[aa] = (double) 0.0;
}
for (pos = 0; pos < width; pos++)
for (seq = 0; seq < block->num_sequences; seq++)
{
if (block->residues[seq][pos] >= 1 &&
block->residues[seq][pos] <= 22)
{
pb[pos].naas[block->residues[seq][pos]] += 1;
}
else
{
sprintf(ErrorBuffer, "pb_weights:%d ignored for %s\n",
block->residues[seq][pos], block->number);
// ErrorReport(INFO_ERR_LVL);
}
}
// factor = 1.0;
for (pos = 0; pos < width; pos++)
{
for (aa = 1; aa <= 22; aa++)
{
if (pb[pos].naas[aa] > 0.0)
{
pb[pos].diffaas += 1; /* # of different types of aas in pos */
}
}
}
for (seq = 0; seq < block->num_sequences; seq++)
{
block->sequences[seq].weight = 0.0;
for (pos = 0; pos < width; pos++)
{
aa = block->residues[seq][pos];
dtemp = pb[pos].diffaas * pb[pos].naas[aa];
if (dtemp > 0.0)
block->sequences[seq].weight += 1.0 / dtemp;
}
}
free(pb);
} /* end of pb_weights */
/*
* Matrix construction methods. Build matricies from blocks and
* sequence weights.
*/
/*
* static void basic_matrix_construction(block, seq_weight, matrix)
*
*/
/*
* basic_matrix_construction
* This is the most general matrix construction method. Each
* occurence of a residue in a sequence contributes the sequence
* weight of the sequence divided by the frequency of the residue to
* the total weight of the residue in the matrix at that column.
* This contribution is scaled by the total of the other residue
* contributions for the column of the matrix. The following are
* exceptions to the method:
* X, '-', '*', & non-code scores are read straight from the frequencies
* the frequencies for B and Z are ignored, when a B or Z is encountered
* it is partitioned between D & N or E & Q.
* the matrix scores for B and Z are computed from the matrix scores of
* D & N and E & Q.
* Parameters:
* Block *block: the block to be converted
* double *seq_weight: the weights of each sequence.
* Matrix *matrix: where the resulting matrix will be put
* Error codes: none
*/
static void basic_matrix_construction(block, seq_weight, matrix)
Block *block;
double *seq_weight;
Matrix *matrix;
{
int seq; /* a sequence counter */
int col; /* a column counter */
int aa; /* an amino acid counter */
int num_sequences; /* the number of sequences in the block */
Residue res; /* a residue. Used for keeping hold of */
/* block->residues[seq][col] */
double total; /* the sum of the seq_weight/freq[] for each */
/* column (every amino acid). This is used */
/* to scale each matrix entry for a column */
/* to a percentage */
double count[MATRIX_AA_WIDTH]; /* the sum of the weights for each amino */
/* acid in the column */
double part_D; /* the partition of D for B. */
/* = freq[D] / ( freq[D] + freq[N] ) */
double part_N; /* the partition of N for B. */
/* = freq[N] / ( freq[D] + freq[N] ) */
double part_E; /* the partition of E for Z. */
/* = freq[E] / ( freq[E] + freq[Q] ) */
double part_Q; /* the partition of Q for Z. */
/* = freq[Q] / ( freq[E] + freq[Q] ) */
/*
* find the partitions of D, N, E, and Q for B and Z
*/
part_D = frequency[aa_atob['D']] /
( frequency[aa_atob['D']] + frequency[aa_atob['N']] );
part_N = frequency[aa_atob['N']] /
( frequency[aa_atob['D']] + frequency[aa_atob['N']] );
part_E = frequency[aa_atob['E']] /
( frequency[aa_atob['E']] + frequency[aa_atob['Q']] );
part_Q = frequency[aa_atob['Q']] /
( frequency[aa_atob['E']] + frequency[aa_atob['Q']] );
/*
* make the matrix
*/
/* for every column in the block, count up the countribution of each aa, */
/* and fill the matrix column */
for (col=0; col<block->width; col++) {
/* reset the counts array and the total */
for (aa=0; aa<MATRIX_AA_WIDTH; aa++) {
count[aa] = 0.0;
}
total = 0.0;
/* for every sequence in the block build up the count and total data */
num_sequences = block->num_sequences;
for (seq=0; seq<num_sequences; seq++) {
res = block->residues[seq][col];
/* if not B, Z, X, gap, stop and non, do the regular */
if ((res != aa_atob['B']) &&
(res != aa_atob['Z']) &&
(res != aa_atob['X']) &&
(res != aa_atob['-']) && /* gap */
(res != aa_atob['*']) && /* stop */
(res != AAID_NAR)) { /* non */
if (frequency[res] != 0.0) { /* try to protect from div by zero */
count[res] += seq_weight[seq] / frequency[res];
total += seq_weight[seq] / frequency[res];
}
}
/* otherwise, if it is B, partition B between D and N */
else if (res == aa_atob['B']) {
if (frequency[aa_atob['D']] != 0.0) { /* try to protect div by zero */
count[aa_atob['D']] +=
(part_D * seq_weight[seq]) / frequency[aa_atob['D']];
total +=
(part_D * seq_weight[seq]) / frequency[aa_atob['D']];
}
if (frequency[aa_atob['N']] != 0.0) { /* try to protect div by zero */
count[aa_atob['N']] +=
(part_N * seq_weight[seq]) / frequency[aa_atob['N']];
total +=
(part_N * seq_weight[seq]) / frequency[aa_atob['N']];
}
}
/* otherwise, if it is Z, partition Z between E and Q */
else if (res == aa_atob['Z']) {
if (frequency[aa_atob['E']] != 0.0) { /* try to protect div by zero */
count[aa_atob['E']] +=
(part_E * seq_weight[seq]) / frequency[aa_atob['E']];
total +=
(part_E * seq_weight[seq]) / frequency[aa_atob['E']];
}
if (frequency[aa_atob['Q']] != 0.0) { /* try to protect div by zero */
count[aa_atob['Q']] +=
(part_Q * seq_weight[seq]) / frequency[aa_atob['Q']];
total +=
(part_Q * seq_weight[seq]) / frequency[aa_atob['Q']];
}
}
/* otherwise, if it is X, gap, stop or non, don't calculate */
}
/* for every amino acid, fill in the matrix for this column, unless it */
/* is B, Z, X, gap, stop or non */
for (aa=0; aa<MATRIX_AA_WIDTH; aa++) {
if ((aa != aa_atob['B']) &&
(aa != aa_atob['Z']) &&
(aa != aa_atob['X']) &&
(aa != aa_atob['-']) && /* gap */
(aa != aa_atob['*']) && /* stop */
(aa != AAID_NAR)) { /* non */
if (total != 0.0) { /* try to protect from div by zero */
matrix->weights[aa][col] = count[aa] * 100.0 / total;
}
else {
matrix->weights[aa][col] = 0.0;
}
}
}
/* fill in the matrix for B, Z, X, gap, stop and non */
matrix->weights[aa_atob['B']][col] =
(part_D * matrix->weights[aa_atob['D']][col] +
part_N * matrix->weights[aa_atob['N']][col]);
matrix->weights[aa_atob['Z']][col] =
(part_E * matrix->weights[aa_atob['E']][col] +
part_Q * matrix->weights[aa_atob['Q']][col]);
matrix->weights[aa_atob['X']][col] = frequency[aa_atob['X']];
matrix->weights[aa_atob['-']][col] = frequency[aa_atob['-']];
matrix->weights[aa_atob['*']][col] = frequency[aa_atob['*']];
matrix->weights[AAID_NAR][col] = frequency[AAID_NAR];
} /* end for each column */
} /* end of basic_matrix_construction */
/*
* The original PATMAT method. Hardly ever done anymore.
*/
/*
* original_conversion_method
* The original conversion method. This is done by weighted average of the
* clusters. This follows the method in patmat.
* Parameters:
* Block *block: the block to be converted
* Matrix *matrix: where the resulting matrix will be put
* Error codes: none
*/
void original_conversion_method(block, matrix)
Block *block;
Matrix *matrix;
{
int aa, pos, clust, seq;
int num_clusters, num_sequences, width;
Residue *sequence;
double *divisor;
double tmp_double;
double *number_occurred[MATRIX_AA_WIDTH]; /* [amino acid][position] */
/* allocate the space for the number occured 2-d array ([amino acid][pos]) */
CheckMem(
number_occurred[0] = (double *) calloc(matrix->width*MATRIX_AA_WIDTH,
sizeof(double))
);
/* setup the array of pointers */
for (aa=0; aa < MATRIX_AA_WIDTH; aa++) {
number_occurred[aa] = number_occurred[0] + aa*matrix->width;
}
/* get the space for the divisor array */
CheckMem(
divisor = (double *) calloc(block->width, sizeof(double))
);
width = block->width;
/*
* get the number of residues at each position. clusters use
* fractional values.
*/
/* for each cluster */
num_clusters = block->num_clusters;
for (clust=0; clust < num_clusters; clust++) {
/* for each sequence in the cluster */
num_sequences = block->clusters[clust].num_sequences;
for (seq=0; seq < num_sequences; seq++) {
/* for each position in the sequence */
sequence = block->clusters[clust].sequences[seq].sequence;
for (pos=0; pos < width; pos++) {
/* increase the number of amino acids seen at this position */
/* in the number_occured matrix */
number_occurred[sequence[pos]][pos] +=
(double) 1.0/(double)num_sequences; /* type casting to be safe */
} /* end for each position */
} /* end for each sequence */
} /* end for each cluster */
/*
* calculate the divisor for later use.
* divisor[i] = summation over all amino acids of
* ( num_occured[aa][i] / frequency[aa] )
*/
/* for each position */
for (pos=0; pos < width; pos++) {
/* for all amino acids */
for (aa=0; aa < MATRIX_AA_WIDTH; aa++) {
if (frequency[aa] > 0.0) {
if (number_occurred[aa][pos] > 0.0) {
divisor[pos] += number_occurred[aa][pos] / frequency[aa];
}
}
} /* end for all amino acids */
} /* end for each position */
/*
* calculate the matrix weights.
* ( num_occur[aa][i] / freq[aa] ) * 100
* in general: weights[aa][i] = -------------------------------------
* divisor[i]
*/
/* for each amino acid */
for (aa=0; aa < MATRIX_AA_WIDTH; aa++) {
/* if there is a valid weight (frequency is not -1.0) */
if ((int)frequency[aa] != -1) { /* typecast to be safe */
/* for each position */
for (pos=0; pos < width; pos++) {
if (frequency[aa] < -1.0) {
tmp_double = frequency[aa];
}
else if (number_occurred[aa][pos] > 0) {
tmp_double = ( (number_occurred[aa][pos] / frequency[aa]) * 100 )
/ divisor[pos];
}
else {
tmp_double = 0.0;
}
matrix->weights[aa][pos] = tmp_double;
} /* end for each position */
} /* end if valid weight */
} /* end for each amino acid */
/* free temporarily allocated space */
free(divisor);
free(number_occurred[0]);
} /* end of original_conversion_method */
/*===================================================================*/
/* Pauline's additions to convert.c for SIFT */
void SIFT_conversion_method(block, pssm)
Block *block;
Matrix *pssm;
{
int diri_option, gap_option, exp_option, subtract_option;
diri_option = gap_option = exp_option = TRUE;
subtract_option = FALSE;
if (Qij == NULL) {
sprintf(ErrorBuffer, "Qij matrix missing, unable to continue.\n");
ErrorReport(FATAL_ERR_LVL);
}
normalize (block);
SIFT_pssm(block, pssm, frequency, Qij, diri_option, gap_option,
exp_option, subtract_option);
} /* end of SIFT_conversion_method */
/*==========================================================================
10/20/00 SIFT pseudocounts
13-dirichlet component
option: diri_pseudocounts
TRUE: use 13-component Dirichlet
FALSE use BLOSUM62 qij's
option: gap: TRUE -allow everything
FALSE - just look at 20 amino acids
option: exp (m)
m=0 # of amino acids at pos (default)
m=1 similiarity scale (more conservative)
option: subtract_threshold TRUE : scores -= SIFT_TOLERANCE
FALSE: leave as original scores
===========================================================================*/
void SIFT_pssm(block, matrix, freqs, qij,
diri_pseudocounts, gap_option,
exp_option, subtract_threshold)
Block *block;
Matrix *matrix;
double *freqs;
struct float_qij *qij;
int diri_pseudocounts, gap_option, exp_option, subtract_threshold;
{
FILE *rfp;
double dtemp, epsilon;
int pos, aa, itemp;
struct working *col;
struct work_pssm *pssm;
int original_aa;
struct diri* diric;
int max_aa;
double min_freq;
int div_by_max;
struct float_qij *rank_matrix;
char *blimps_dir, diriname[SMALL_BUFF_LENGTH], rankname[SMALL_BUFF_LENGTH];
blimps_dir = getenv("BLIMPS_DIR");
if (blimps_dir != NULL)
{
sprintf(diriname, "%s/docs/default.diri", blimps_dir);
sprintf(rankname, "%s/docs/default.rank", blimps_dir);
/*>>>>> Need to check that rankname & diriname actually exists
for WWW servers would be better to check in current
directory first <<<<<<*/
}
else
{
sprintf(diriname, "default.diri");
sprintf(rankname, "default.rank");
}
diric = load_diri (diriname);
rank_matrix = NULL;
if (exp_option == 1)
{
if ( (rfp = fopen(rankname, "r") ) == NULL)
{
sprintf(ErrorBuffer, "SIFT_pssm(): Cannot open %s\n", rankname);
ErrorReport(FATAL_ERR_LVL);
}
else
{
rank_matrix = load_qij(rfp);
fclose(rfp);
}
}
div_by_max = TRUE;
col = make_col();
pssm = make_work_pssm(block->width);
/*-------------- Do one position at a time -------------------*/
for (pos = 0; pos < block->width; pos++)
{
/*-------- count the number of each aa in this position ------------*/
if (gap_option) {
counts (block,col, pos);
} else {
counts_nogaps(block, col, pos);
}
/*-------- determine total number of pseudo-counts in column ------*/
epsilon = 0;
itemp = count_residues (col);
original_aa = block->residues[0][pos];
if (exp_option == 1) {
/*printf ("pos %d : ", pos); */
dtemp = similarity_dependent_scale(col, rank_matrix, original_aa);
if (itemp == 1) {
epsilon = 0;
} else {
epsilon = exp (dtemp);
}
} else {
if (itemp == 1) { epsilon = 0; }
else if (itemp > 7) { epsilon = 1000; }
else { epsilon = exp( (double) itemp) ; }
}
/*---------- get the pseudo counts -------------------------------*/
if (diri_pseudocounts) {
pseudo_diri (col, diric, epsilon );
} else {
pseudo_alts(col, qij, epsilon);
}
/*--------- Fill in the matrix entries --------------------*/
pssm->sum[pos] = 0.0;
for (aa=1; aa < AAS; aa++)
{
pssm->value[pos][aa] = col->cnt[aa] + epsilon * col->reg[aa];
if ( (col->totcnt + col->totreg) > 0.0)
pssm->value[pos][aa] /= (col->totcnt + epsilon);
} /* end of aa, pssm->values filled for a given pos */
if (div_by_max) {
max_aa = find_max_aa_pssm (pssm, pos);
min_freq = pssm->value[pos][max_aa];
for (aa = 1; aa < AAS; aa++) {
pssm->value[pos][aa] /= min_freq;
}
}
if (subtract_threshold) {
for (aa = 1; aa < AAS; aa++) {
pssm->value[pos][aa] -= SIFT_TOLERANCE;
}
}
pssm->sum[pos] += pssm->value[pos][aa];
for (aa = 1; aa < AAS; aa++) {
matrix->weights[aa][pos] = pssm->value[pos][aa];
}
original_aa = block->residues[0][pos];
if ( (matrix->weights[original_aa][pos] < SIFT_TOLERANCE
&& (!subtract_threshold)) ||
(matrix->weights[original_aa][pos] < 0.0 && subtract_threshold) )
{
sprintf (ErrorBuffer, "SIFT_pssm(): Amino acid %c at pos %d in your original sequence was not allowed by the prediction.\n",
aa_btoa[original_aa], pos);
ErrorReport(WARNING_ERR_LVL);
}
} /* end of for pos */
if (diri_pseudocounts) {
free (diric);
}
free(col);
free_work_pssm(pssm);
} /* end of SIFT_pssm */
/*======================================================================
Dirichlet input file order (0-19) is ARNDCQEGHILKMFPSTWYV
Blimps order (0-24) is -ARNDCQEGHILKMFPSTWYVBZX*
Store Dirichlet alphas in positions 1-20 to match blimps
=======================================================================*/
struct diri *load_diri (filename)
char filename[LARGE_BUFF_LENGTH];
{
FILE* fin;
int i, aa, numc, type;
char line[MAXLINE], *ptr;
struct diri *diric;
double denom;
double background_frequency[AAS];
if ((fin = fopen (filename, "r")) == NULL)
{
sprintf (ErrorBuffer, "dirichlet(): Cannot open dirichlet file %s\n",
filename);
ErrorReport(FATAL_ERR_LVL);
}
diric = (struct diri *) malloc(sizeof(struct diri));
if (diric == NULL)
{
sprintf (ErrorBuffer, "dirichlet(): OUT OF MEMORY\n");
ErrorReport(FATAL_ERR_LVL);
}
numc = 0;
while (numc < MAXDIRI && !feof(fin) && fgets(line, MAXLINE, fin) != NULL)
{
type = 0;
if (strstr(line, "Mixture=") != NULL) type = 1;
if (strstr(line, "Alpha=") != NULL) type = 2;
if (type > 0)
{
ptr = strtok(line, "="); ptr = strtok(NULL, "\t\n\r ");
switch (type)
{
case 1:
diric->q[numc] = atof(ptr);
break;
case 2:
diric->altot[numc] = atof(ptr);
aa = 1;
while (aa < AAS && ptr != NULL)
{
ptr = strtok(NULL, "\t\n\r ");
diric->alpha[numc][aa++] = atof(ptr);
}
numc++;
break;
default:
break;
}
}
} /* end of while */
diric->ncomp = numc;
for (i = 0; i < numc; i++) {
for (aa = 1; aa < AAS; aa++) {
diric->alpha_normalized[i][aa] = diric->alpha[i][aa]/diric->altot[i];
}
}
for (aa = 1; aa < AAS; aa++) {
denom = 0.0;
for (i = 0; i < numc; i++) {
denom += (diric->q[i] * diric->alpha[i][aa] /
diric->altot[i]);
}
background_frequency[aa] = denom;
}
for (i =0; i < numc; i++) {
/* printf ("Component %d ratio of aa relative to background \n", i); */
for (aa = 1; aa < AAS; aa++) {
diric->frequency_to_background_ratio[i][aa] =
diric->alpha[i][aa]/(diric->altot[i] * background_frequency[aa]);
}
}
fclose(fin);
return(diric);
} /* end of load_diri */
/*========================================================================
==========================================================================*/
/* only count valid amino acids, no considerationto gaps */
void counts_nogaps (block, col, pos)
Block *block;
struct working *col;
int pos;
{
int seq, aa;
col->totcnt = col->totraw = col->totreg = 0.0;
for (aa = 0; aa < AASALL; aa++) {
col->cnt[aa] = col->raw[aa] = col->reg[aa] = 0.0;
}
/* Only count the real 20 aas, combine B(21) with D & Z(22) with E */
for (seq = 0; seq < block->num_sequences; seq++) {
aa = block->residues[seq][pos];
if (aa >= 1 && aa < AAS) {
col->cnt[aa] += block->sequences[seq].weight;
col->totcnt += block->sequences[seq].weight;
col->raw[aa] += 1.0;
col->totraw += 1.0;
}
} /* end of for */
} /* end of counts_nogaps */
/*========================================================================
==========================================================================*/
double similarity_dependent_scale (col, rank_matrix, original_aa)
struct working *col;
struct float_qij *rank_matrix;
int original_aa;
{
int aa, rank; double sum;
int n;
int max_aa;
max_aa = find_max_aa_col(col);
original_aa = max_aa; /* change to max aa 10/24/00 */
n = 0;
sum = 0.0;
for (aa = 1; aa <= 20 ; aa++) {
if (col->cnt[aa] > 0.0) {
n++;
rank = (int) rank_matrix->value[original_aa][aa];
sum += rank * col->cnt[aa]/col->totcnt;
/* printf ("\taa %c rank %d weight %.3f", aa_btoa[aa], rank, col->cnt[aa]/col->totcnt); */
}
}
/* printf (" sim score %.3f\n", sum); */
return sum;
} /* end of similarity_dependent_scale */
/*=====================================================================*/
/*========================================================================
==========================================================================*/
void pseudo_diri(col, diric, epsilon)
struct working *col;
struct diri *diric;
double epsilon;
{
int j, aa;
double denom, dtemp;
double total;
/*----------- compute equation (3), Prob(n|j) ------------ */
for (j = 0; j < diric->ncomp; j++)
{
col->probn[j] = lgamma(col->totcnt + 1.0) + lgamma(diric->altot[j]);
col->probn[j] -= lgamma(col->totcnt + diric->altot[j]);
/* Note range on aa varies; Diric values only for 1-20 */
for (aa = 1; aa < AAS; aa++)
{
/* ni = cnt[i] */
if (col->cnt[aa] >= 0.0)
{
dtemp = lgamma(col->cnt[aa] + diric->alpha[j][aa]);
/* printf ("aa %c cnt %.3f alpha %.3f dtemp %.3f\n", aa_btoa[aa], col->cnt[aa], diric->alpha[j][aa], dtemp); */
dtemp -= lgamma(col->cnt[aa] + 1.0);
dtemp -= lgamma(diric->alpha[j][aa]);
col->probn[j] += dtemp;
/* printf ("col->probn is now %.3f prob is %.3f \n\n", col->probn[j], exp (col->probn[j])) ; */
} /* end of if > = 0.0 */
} /* end of for amino acids */
} /* end of for all j components */
/*------ compute sum qk * p(n|k) using logs & exponents ----------*/
denom = log(diric->q[0]) + col->probn[0];
for (j = 1; j < diric->ncomp; j++)
{
dtemp = log(diric->q[j]) + col->probn[j];
denom = add_logs(denom, dtemp);
}
/* printf ("%.3f denom number of comp %d\n", denom, diric->ncomp); */
/* compute equation (3), Prob(j|n) */
for (j = 0; j < diric->ncomp; j++)
{
col->probj[j] = log(diric->q[j]) + col->probn[j] - denom;
/* printf("j=%d probn[j]=%f probj[j]=%f\n",
j, exp(col->probn[j]), exp(col->probj[j])); */
}
/* ----- compute equation (4), ni + bi, bi = Prob(j|n) * alpha[j][i] */
for (aa = 1; aa < AAS; aa++)
{
for (j = 0; j < diric->ncomp; j++) {
col->reg[aa] +=
(exp(col->probj[j]) * diric->alpha[j][aa]);
/* * epsilon); */
}
col->totreg += col->reg[aa];
}
/* printf (" total prob. %.2f\n", col->totreg); */
/* scale dirichlet to probabilities */
total = 0.0;
for (aa = 1; aa < AAS; aa ++) {
col->reg[aa] /= col->totreg;
total += col->reg[aa];
}
/* printf ("%.2f total\n", total); */
/* printf ("%c %.3f", aa_btoa[aa], col->reg[aa]);
if (col->cnt[aa] > 0.0) {printf ("*\n"); } else { printf ("\n");} */
} /* end of pseudo_diri */
/*========================================================================
==========================================================================*/
int find_max_aa_col (col)
struct working *col;
{
int aa, max_aa;
double max = 0.0;
max_aa = -1;
for (aa = 1; aa < AAS; aa++) {
if (col->cnt[aa] > max) {
max = col->cnt[aa];
max_aa = aa;
}
}
return max_aa;
} /* end of find_max_aa_col */
/*========================================================================
==========================================================================*/
int find_max_aa_pssm (pssm, pos)
struct work_pssm *pssm;
int pos;
{
int aa, max_aa;
double max;
max_aa = -1;
max = 0.0;
for (aa = 1; aa < AAS; aa++) {
if (pssm->value[pos][aa] > max) {
max_aa = aa;
max = pssm->value[pos][aa];
}
}
return max_aa;
} /* end of find_max_aa_pssm */
/*===============================================================
Returns log(e^x + e^y)
================================================================*/
double add_logs(lx, ly)
double lx, ly;
{
if (lx > ly) return(lx + log(1.0 + exp(ly - lx)));
else return(ly + log(1.0 + exp(lx - ly)));
} /* end of add_logs */
/*=========================================================================*/
/* Change log information follows. */
/*
Changes since version 3.5:
8/27/03 Fix problem in make_alts() with scale=10,20,21,30
Changes since version 3.4:
12/29/00 Added Pauline's routines for SIFT (SIFT_prediction, etc.)
Modified struct working, struct work_pssm and make_work_pssm()
(width is now dynamic) Added free_work_pssm()
6/16/00 Added type 22 (pseudo-counts nearly zero) to block_to_matrix()
Changes since version 3.3.2:
5/24/00 Increased MAXWIDTH from 100 to 400, but need to do away with
this restriction!
Changes since version 3.3:
9/ 4/99 Added type 21 (counts only) to block_to_matrix()
Changes since version 3.2.5:
2/22/99 Removed block from positive_matrix()
Changes since version 3.2.4:
12/12/98 compute_BZX(): Use 0 for * column instead of min value, unless
min value is negative.
Changes since version 3.2.3:
8/11/98 convert.c: MAXWIDTH increased, error check added.
4/ 8/98 Avoid division by zero in pseudo_alts() & block_to_matrix().
Divide counts of aas other than the basic 20 among those 20.
Changes since version 3.1:
1/30/97 Changed no weights error message from SERIOUS to WARNING.
10/10/96 Fixed bug in pb_weights() when X (#23) in sequence.
9/25/96 Changed compute_BZX() to compute average score for X.
*/
|