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
|
#include "Genotype.h"
#include <vcflib/multichoose.h>
#include "multipermute.h"
vector<Allele*> Genotype::uniqueAlleles(void) {
vector<Allele*> uniques;
for (Genotype::iterator g = this->begin(); g != this->end(); ++g) {
uniques.push_back(&g->allele);
}
return uniques;
}
int Genotype::getPloidy(void) {
int result = 0;
for (Genotype::const_iterator i = this->begin(); i != this->end(); ++i) {
result += i->count;
}
return result;
}
vector<int> Genotype::counts(void) {
vector<int> counts;
for (Genotype::iterator i = this->begin(); i != this->end(); ++i) {
counts.push_back(i->count);
}
return counts;
}
vector<Allele> Genotype::alternateAlleles(string& base) {
vector<Allele> alleles;
for (Genotype::iterator i = this->begin(); i != this->end(); ++i) {
Allele& b = i->allele;
if (base != b.currentBase)
alleles.push_back(b);
}
return alleles;
}
vector<string> Genotype::alternateBases(string& base) {
vector<string> alleles;
for (Genotype::iterator i = this->begin(); i != this->end(); ++i) {
Allele& b = i->allele;
if (base != b.currentBase)
alleles.push_back(b.currentBase);
}
return alleles;
}
int Genotype::alleleCount(const string& base) {
map<string, int>::iterator ge = alleleCounts.find(base);
if (ge == alleleCounts.end()) {
return 0;
} else {
return ge->second;
}
}
int Genotype::alleleCount(Allele& allele) {
map<string, int>::iterator ge = alleleCounts.find(allele.currentBase);
if (ge == alleleCounts.end()) {
return 0;
} else {
return ge->second;
}
}
// returns true when the genotype is composed of a subset of the alleles
bool Genotype::matchesAlleles(vector<Allele>& alleles) {
int p = 0;
for (vector<Allele>::iterator a = alleles.begin(); a != alleles.end(); ++a) {
p += alleleCount(*a);
}
return ploidy == p;
}
double Genotype::alleleSamplingProb(const string& base) {
map<string, int>::iterator ge = alleleCounts.find(base);
if (ge == alleleCounts.end()) {
return 0;
} else {
return (double) ge->second / (double) ploidy;
}
}
double Genotype::alleleSamplingProb(Allele& allele) {
map<string, int>::iterator ge = alleleCounts.find(allele.currentBase);
if (ge == alleleCounts.end()) {
return 0;
} else {
return (double) ge->second / (double) ploidy;
}
}
string Genotype::relativeGenotype(string& refbase, vector<Allele>& alts) {
vector<string> rg;
for (Genotype::iterator i = this->begin(); i != this->end(); ++i) {
Allele& b = i->allele;
string& base = b.currentBase;
if (base == refbase) {
for (int j = 0; j < i->count; ++j)
rg.push_back("0");
} else {
int n = 1;
bool matchingalt = false;
for (vector<Allele>::iterator a = alts.begin(); a != alts.end(); ++a, ++n) {
if (base == a->currentBase) {
matchingalt = true;
for (int j = 0; j < i->count; ++j)
rg.push_back(convert(n));
break;
}
}
if (!matchingalt) {
for (int j = 0; j < i->count; ++j)
rg.push_back(".");
}
}
}
sort(rg.begin(), rg.end()); // enforces the same ordering for all genotypes
//reverse(rg.begin(), rg.end()); // 1/0 ordering, or 1/1/0 etc.
string result = join(rg, "/");
return result; // chop trailing '/'
}
void Genotype::relativeGenotype(vector<int>& rg, string& refbase, vector<Allele>& alts) {
for (Genotype::iterator i = this->begin(); i != this->end(); ++i) {
Allele& b = i->allele;
string& base = b.currentBase;
if (base == refbase) {
for (int j = 0; j < i->count; ++j)
rg.push_back(0);
} else {
int n = 1;
bool matchingalt = false;
for (vector<Allele>::iterator a = alts.begin(); a != alts.end(); ++a, ++n) {
if (base == a->currentBase) {
matchingalt = true;
for (int j = 0; j < i->count; ++j)
rg.push_back(n);
break;
}
}
if (!matchingalt) {
for (int j = 0; j < i->count; ++j)
rg.push_back(-1);
}
}
}
sort(rg.begin(), rg.end()); // enforces the same ordering for all genotypes
//reverse(rg.begin(), rg.end()); // 1/0 ordering, or 1/1/0 etc.
}
void Genotype::relativeGenotype(vector<int>& rg, vector<Allele>& alleles) {
for (Genotype::iterator i = this->begin(); i != this->end(); ++i) {
Allele& b = i->allele;
string& base = b.currentBase;
int n = 0;
bool matchingalt = false;
for (vector<Allele>::iterator a = alleles.begin(); a != alleles.end(); ++a, ++n) {
if (base == a->base()) {
matchingalt = true;
for (int j = 0; j < i->count; ++j)
rg.push_back(n);
break;
}
}
if (!matchingalt) {
for (int j = 0; j < i->count; ++j)
rg.push_back(-1);
}
}
sort(rg.begin(), rg.end()); // enforces the same ordering for all genotypes
//reverse(rg.begin(), rg.end()); // 1/0 ordering, or 1/1/0 etc.
}
string Genotype::relativeGenotype(string& refbase, string& altbase) {
vector<string> rg;
for (Genotype::iterator i = this->begin(); i != this->end(); ++i) {
Allele& b = i->allele;
if (b.currentBase == altbase && refbase != b.currentBase) {
for (int j = 0; j < i->count; ++j)
rg.push_back("1/");
} else if (b.currentBase != altbase && refbase != b.currentBase) {
for (int j = 0; j < i->count; ++j)
rg.push_back("./");
} else {
for (int j = 0; j < i->count; ++j)
rg.push_back("0/");
}
}
sort(rg.begin(), rg.end()); // enforces the same ordering for all genotypes
//reverse(rg.begin(), rg.end()); // 1/0 ordering, or 1/1/0 etc.
string result = accumulate(rg.begin(), rg.end(), string(""));
return result.substr(0, result.size() - 1); // chop trailing '/'
}
bool Genotype::containsAllele(const string& base) {
map<string, int>::iterator ge = alleleCounts.find(base);
if (ge == alleleCounts.end()) {
return false;
} else {
return true;
}
}
bool Genotype::containsAllele(Allele& allele) {
map<string, int>::iterator ge = alleleCounts.find(allele.currentBase);
if (ge == alleleCounts.end()) {
return false;
} else {
return true;
}
}
bool Genotype::isHomozygous(void) {
return size() == 1;
}
// if heterozgyous
bool Genotype::isHeterozygous(void) {
return size() > 1;
}
// if homozygous alternate
bool Genotype::isHomozygousAlternate(void) {
return isHomozygous() && !front().allele.isReference();
}
// if homozygous reference
bool Genotype::isHomozygousReference(void) {
return isHomozygous() && front().allele.isReference();
}
// the probability of drawing each allele out of the genotype, ordered by allele
vector<long double> Genotype::alleleProbabilities(void) {
vector<long double> probs;
for (vector<GenotypeElement>::const_iterator a = this->begin(); a != this->end(); ++a) {
probs.push_back((long double) a->count / (long double) ploidy);
}
return probs;
}
// the probability of drawing each allele out of the genotype, ordered by allele, adjusted for reference bias
vector<long double> Genotype::alleleProbabilities(Bias& observationBias) {
vector<long double> probs;
for (vector<GenotypeElement>::const_iterator a = this->begin(); a != this->end(); ++a) {
long double bias = 1;
if (!a->allele.isReference()) {
int alleleLengthDifference = a->allele.alternateSequence.size() - a->allele.referenceLength;
bias = observationBias.bias(alleleLengthDifference);
}
probs.push_back(((long double) a->count / (long double) ploidy) * bias);
}
normalizeSumToOne(probs);
return probs;
}
string Genotype::str(void) const {
string s;
for (Genotype::const_iterator ge = this->begin(); ge != this->end(); ++ge) {
for (int i = 0; i < ge->count; ++i)
s += ((ge == this->begin() && i == 0) ? "" : "/") + ge->allele.currentBase;
}
return s;
}
string IUPAC(Genotype& genotype) {
const string g = genotype.str();
if (g == "AA") return "A";
if (g == "AC") return "M";
if (g == "AG") return "R";
if (g == "AT") return "W";
if (g == "CA") return "M";
if (g == "CC") return "C";
if (g == "CG") return "S";
if (g == "CT") return "Y";
if (g == "GA") return "R";
if (g == "GC") return "S";
if (g == "GG") return "G";
if (g == "GT") return "K";
if (g == "TA") return "W";
if (g == "TC") return "Y";
if (g == "TG") return "K";
if (g == "TT") return "T";
return g;
}
string IUPAC2GenotypeStr(string iupac, int ploidy) {
if (iupac == "A") return "AA";
if (iupac == "M") return "AC";
if (iupac == "R") return "AG";
if (iupac == "W") return "AT";
if (iupac == "C") return "CC";
if (iupac == "S") return "CG";
if (iupac == "Y") return "CT";
if (iupac == "G") return "GG";
if (iupac == "K") return "GT";
if (iupac == "T") return "TT";
return iupac;
}
ostream& operator<<(ostream& out, const GenotypeElement& rhs) {
for (int i = 0; i < rhs.count; ++i)
out << rhs.allele.base() << "/";
//for (int i = 0; i < rhs.second; ++i)
// out << rhs.first.currentBase;
return out;
}
ostream& operator<<(ostream& out, const Genotype& g) {
out << g.str();
return out;
}
ostream& operator<<(ostream& out, list<GenotypeCombo>& g) {
for (list<GenotypeCombo>::iterator i = g.begin(); i != g.end(); ++i) {
out << *i << endl;
}
return out;
}
ostream& operator<<(ostream& out, GenotypeCombo& g) {
GenotypeCombo::iterator i = g.begin(); ++i;
out << "combo posterior prob: " << g.posteriorProb << endl;
out << "{\"" << g.front()->name << "\":[\"" << *(g.front()->genotype) << "\"," << exp(g.front()->prob) << "]";
for (;i != g.end(); ++i) {
out << ", \"" << (*i)->name << "\":[\"" << *((*i)->genotype) << "\"," << exp((*i)->prob) << "]";
}
out << "}";
return out;
}
bool operator<(Genotype& a, Genotype& b) {
// genotypes of different ploidy are evaluated according to their relative ploidy
if (a.ploidy != b.ploidy)
return a.ploidy < b.ploidy;
// because our constructor sorts each Genotype.alleles, we assume that we
// have two equivalently sorted vectors to work with
Genotype::iterator ai = a.begin();
Genotype::iterator bi = b.begin();
// step through each genotype, and if we find a difference between either
// their allele or count return a<b
for (; ai != a.end() && bi != b.end(); ++ai, ++bi) {
if (ai->allele != bi->allele)
return ai->allele < bi->allele;
else if (ai->count != bi->count)
return ai->count < bi->count;
}
return false; // if the two are equal, then we return false per C++ convention
}
vector<Genotype> allPossibleGenotypes(int ploidy, vector<Allele>& potentialAlleles) {
vector<Genotype> genotypes;
vector<vector<Allele> > alleleCombinations = multichoose(ploidy, potentialAlleles);
for (vector<vector<Allele> >::iterator combo = alleleCombinations.begin(); combo != alleleCombinations.end(); ++combo) {
genotypes.push_back(Genotype(*combo));
}
return genotypes;
}
int GenotypeCombo::numberOfAlleles(void) {
int count = 0;
for (map<string, AlleleCounter>::iterator f = alleleCounters.begin(); f != alleleCounters.end(); ++f) {
const AlleleCounter& allele = f->second;
count += allele.frequency;
}
return count;
}
// initializes cached counts associated with each GenotypeCombo
void GenotypeCombo::init(bool useObsExpectations) {
for (GenotypeCombo::iterator s = begin(); s != end(); ++s) {
const SampleDataLikelihood& sdl = **s;
const Sample& sample = *sdl.sample;
++genotypeCounts[sdl.genotype];
permutationsln += sdl.genotype->permutationsln;
for (Genotype::iterator a = sdl.genotype->begin(); a != sdl.genotype->end(); ++a) {
const string& alleleBase = a->allele.currentBase;
// allele frequencies in selected genotypes in combo
AlleleCounter& alleleCounter = alleleCounters[alleleBase];
alleleCounter.frequency += a->count;
if (useObsExpectations) {
// observational frequencies for binomial priors
Sample::const_iterator as = sample.find(alleleBase);
if (as != sample.end()) {
vector<Allele*> alleles = as->second;
alleleCounter.observations += alleles.size();
for (vector<Allele*>::iterator o = alleles.begin(); o != alleles.end(); ++o) {
const Allele& allele = **o;
if (allele.basesLeft >= allele.basesRight) {
++alleleCounter.placedLeft;
if (allele.strand == STRAND_FORWARD) {
++alleleCounter.placedStart;
} else {
++alleleCounter.placedEnd;
}
} else {
++alleleCounter.placedRight;
if (allele.strand == STRAND_FORWARD) {
++alleleCounter.placedEnd;
} else {
++alleleCounter.placedStart;
}
}
if (allele.strand == STRAND_FORWARD) {
++alleleCounter.forwardStrand;
} else {
++alleleCounter.reverseStrand;
}
}
}
}
}
}
}
void GenotypeCombo::addPriorAlleleCounts(map<string, int>& priorACs) {
for (map<string, int>::iterator p = priorACs.begin(); p != priorACs.end(); ++p) {
const string& alleleBase = p->first;
int count = p->second;
AlleleCounter& alleleCounter = alleleCounters[alleleBase];
//cerr <<"init "<< alleleCounter.frequency;
alleleCounter.frequency += count;
}
}
// frequency... should this just be "allele count"?
int GenotypeCombo::alleleCount(Allele& allele) {
map<string, AlleleCounter>::iterator f = alleleCounters.find(allele.currentBase);
if (f == alleleCounters.end()) {
return 0;
} else {
return f->second.frequency;
}
}
int GenotypeCombo::alleleCount(const string& allele) {
map<string, AlleleCounter>::iterator f = alleleCounters.find(allele);
if (f == alleleCounters.end()) {
return 0;
} else {
return f->second.frequency;
}
}
long double GenotypeCombo::alleleFrequency(Allele& allele) {
return alleleCount(allele) / (long double) numberOfAlleles();
}
long double GenotypeCombo::alleleFrequency(const string& allele) {
return alleleCount(allele) / (long double) numberOfAlleles();
}
long double GenotypeCombo::genotypeFrequency(Genotype* genotype) {
map<Genotype*, int>::iterator g = genotypeCounts.find(genotype);
if (g == genotypeCounts.end()) {
return 0;
} else {
return g->second / size();
}
}
void GenotypeCombo::updateCachedCounts(
Sample* sample,
Genotype* oldGenotype,
Genotype* newGenotype,
bool useObsExpectations) {
// update genotype counts
--genotypeCounts[oldGenotype];
++genotypeCounts[newGenotype];
// update permutations
permutationsln -= oldGenotype->permutationsln;
permutationsln += newGenotype->permutationsln;
// remove allele frequencies which are now 0 or below
map<Genotype*, int>::iterator gc = genotypeCounts.begin();
while (gc != genotypeCounts.end()) {
assert(gc->second >= 0);
if (gc->second == 0) {
genotypeCounts.erase(gc++);
} else {
++gc;
}
}
// TODO can we improve efficiency by only adjusting for bases which are actually changed
// remove allele frequency information for old genotype
for (Genotype::iterator g = oldGenotype->begin(); g != oldGenotype->end(); ++g) {
GenotypeElement& ge = *g;
const string& base = ge.allele.currentBase;
AlleleCounter& alleleCounter = alleleCounters[base];
alleleCounter.frequency -= ge.count;
if (useObsExpectations) {
Sample::iterator s = sample->find(base);
if (s != sample->end()) {
const vector<Allele*>& alleles = s->second;
alleleCounter.observations -= alleles.size();
int forward_strand = 0;
int reverse_strand = 0;
int placed_left = 0;
int placed_right = 0;
int placed_start = 0;
int placed_end = 0;
for (vector<Allele*>::const_iterator a = alleles.begin(); a != alleles.end(); ++a) {
const Allele& allele = **a;
if (allele.strand == STRAND_FORWARD) {
++forward_strand;
} else {
++reverse_strand;
}
if (allele.basesLeft >= allele.basesRight) {
++placed_left;
if (allele.strand == STRAND_FORWARD) {
++placed_start;
} else {
++placed_end;
}
} else {
++placed_right;
if (allele.strand == STRAND_FORWARD) {
++placed_end;
} else {
++placed_start;
}
}
}
alleleCounter.forwardStrand -= forward_strand;
alleleCounter.reverseStrand -= reverse_strand;
alleleCounter.placedLeft -= placed_left;
alleleCounter.placedRight -= placed_right;
alleleCounter.placedStart -= placed_start;
alleleCounter.placedEnd -= placed_end;
}
}
}
// add allele frequency information for new genotype
for (Genotype::iterator g = newGenotype->begin(); g != newGenotype->end(); ++g) {
GenotypeElement& ge = *g;
const string& base = ge.allele.currentBase;
AlleleCounter& alleleCounter = alleleCounters[base];
alleleCounter.frequency += ge.count;
if (useObsExpectations) {
Sample::iterator s = sample->find(base);
if (s != sample->end()) {
const vector<Allele*>& alleles = s->second;
alleleCounter.observations += alleles.size();
int forward_strand = 0;
int reverse_strand = 0;
int placed_left = 0;
int placed_right = 0;
int placed_start = 0;
int placed_end = 0;
for (vector<Allele*>::const_iterator a = alleles.begin(); a != alleles.end(); ++a) {
const Allele& allele = **a;
if (allele.strand == STRAND_FORWARD) {
++forward_strand;
} else {
++reverse_strand;
}
if (allele.basesLeft >= allele.basesRight) {
++placed_left;
if (allele.strand == STRAND_FORWARD) {
++placed_start;
} else {
++placed_end;
}
} else {
++placed_right;
if (allele.strand == STRAND_FORWARD) {
++placed_end;
} else {
++placed_start;
}
}
}
alleleCounter.forwardStrand += forward_strand;
alleleCounter.reverseStrand += reverse_strand;
alleleCounter.placedLeft += placed_left;
alleleCounter.placedRight += placed_right;
alleleCounter.placedStart += placed_start;
alleleCounter.placedEnd += placed_end;
}
}
}
// remove allele frequencies which are now 0 or below
map<string, AlleleCounter>::iterator af = alleleCounters.begin();
while (af != alleleCounters.end()) {
assert(af->second.frequency >= 0);
if (af->second.frequency == 0) {
assert(af->second.observations == 0);
alleleCounters.erase(af++);
} else {
++af;
}
}
}
map<int, int> GenotypeCombo::countFrequencies(void) {
map<int, int> frequencyCounts;
for (map<string, AlleleCounter>::iterator a = alleleCounters.begin(); a != alleleCounters.end(); ++a) {
const AlleleCounter& allele = a->second;
map<int, int>::iterator c = frequencyCounts.find(allele.frequency);
if (c != frequencyCounts.end()) {
c->second += 1;
} else {
frequencyCounts[allele.frequency] = 1;
}
}
return frequencyCounts;
}
vector<int> GenotypeCombo::counts(void) {
//map<string, int> alleleCounters = countAlleles();
vector<int> counts;
for (map<string, AlleleCounter>::iterator a = alleleCounters.begin(); a != alleleCounters.end(); ++a) {
const AlleleCounter& allele = a->second;
counts.push_back(allele.frequency);
}
return counts;
}
int GenotypeCombo::hetCount(void) {
int hc = 0;
for (GenotypeCombo::iterator s = begin(); s != end(); ++s) {
if (!(*s)->genotype->homozygous) {
++hc;
}
}
return hc;
}
vector<int> GenotypeCombo::observationCounts(void) {
vector<int> counts;
for (map<string, AlleleCounter>::iterator a = alleleCounters.begin(); a != alleleCounters.end(); ++a) {
const AlleleCounter& allele = a->second;
counts.push_back(allele.observations);
}
return counts;
}
int GenotypeCombo::observationTotal(void) {
int total = 0;
for (map<string, AlleleCounter>::iterator a = alleleCounters.begin(); a != alleleCounters.end(); ++a) {
const AlleleCounter& allele = a->second;
total += allele.observations;
}
return total;
}
// how many copies of the locus are in the whole genotype combination?
int GenotypeCombo::ploidy(void) {
int copies = 0;
for (map<string, AlleleCounter>::iterator a = alleleCounters.begin(); a != alleleCounters.end(); ++a) {
const AlleleCounter& allele = a->second;
copies += allele.frequency;
}
return copies;
}
vector<long double> GenotypeCombo::alleleProbs(void) {
vector<long double> probs;
long double copies = ploidy();
for (map<string, AlleleCounter>::iterator a = alleleCounters.begin(); a != alleleCounters.end(); ++a) {
const AlleleCounter& allele = a->second;
probs.push_back(allele.frequency / copies);
}
return probs;
}
vector<string> GenotypeCombo::alleles(void) {
vector<string> bases;
for (map<string, AlleleCounter>::iterator a = alleleCounters.begin(); a != alleleCounters.end(); ++a) {
bases.push_back(a->first);
}
return bases;
}
// returns true if the combination is 100% homozygous
bool GenotypeCombo::isHomozygous(void) {
return alleleCounters.size() == 1;
}
void sortSampleDataLikelihoods(vector<SampleDataLikelihood>& likelihoods) {
SampleDataLikelihoodCompare datalikelihoodCompare;
sort(likelihoods.begin(), likelihoods.end(), datalikelihoodCompare);
int i = 0;
for (vector<SampleDataLikelihood>::iterator sdl = likelihoods.begin(); sdl != likelihoods.end(); ++sdl) {
sdl->rank = i++;
}
}
bool sortSampleDataLikelihoodsByMarginals(vector<SampleDataLikelihood>& likelihoods) {
SampleMarginalCompare marginalLikelihoodCompare;
sort(likelihoods.begin(), likelihoods.end(), marginalLikelihoodCompare);
bool reordered = false;
int i = 0;
for (vector<SampleDataLikelihood>::iterator sdl = likelihoods.begin(); sdl != likelihoods.end(); ++sdl) {
int newrank = i++;
if (sdl->rank != newrank) {
reordered = true;
sdl->rank = newrank;
}
}
return reordered;
}
bool sortSampleDataLikelihoodsByMarginals(SampleDataLikelihoods& samplesLikelihoods) {
bool reordered = false;
for (SampleDataLikelihoods::iterator s = samplesLikelihoods.begin(); s != samplesLikelihoods.end(); ++s) {
reordered |= sortSampleDataLikelihoodsByMarginals(*s);
}
return reordered;
}
bool sortSampleDataLikelihoodsByMarginalsAndObs(vector<SampleDataLikelihood>& likelihoods) {
SampleMarginalAndObsCompare marginalLikelihoodAndObsCompare;
sort(likelihoods.begin(), likelihoods.end(), marginalLikelihoodAndObsCompare);
bool reordered = false;
int i = 0;
for (vector<SampleDataLikelihood>::iterator sdl = likelihoods.begin(); sdl != likelihoods.end(); ++sdl) {
int newrank = i++;
if (sdl->rank != newrank) {
reordered = true;
sdl->rank = newrank;
}
}
return reordered;
}
bool sortSampleDataLikelihoodsByMarginalsAndObs(SampleDataLikelihoods& samplesLikelihoods) {
bool reordered = false;
for (SampleDataLikelihoods::iterator s = samplesLikelihoods.begin(); s != samplesLikelihoods.end(); ++s) {
reordered |= sortSampleDataLikelihoodsByMarginalsAndObs(*s);
}
return reordered;
}
bool sortSampleDataLikelihoodsScaledByMarginals(vector<SampleDataLikelihood>& likelihoods) {
SampleLikelihoodCompare likelihoodCompare;
sort(likelihoods.begin(), likelihoods.end(), likelihoodCompare);
bool reordered = false;
int i = 0;
for (vector<SampleDataLikelihood>::iterator sdl = likelihoods.begin(); sdl != likelihoods.end(); ++sdl) {
int newrank = i++;
if (sdl->rank != newrank) {
reordered = true;
sdl->rank = newrank;
}
}
return reordered;
}
bool sortSampleDataLikelihoodsScaledByMarginals(SampleDataLikelihoods& samplesLikelihoods) {
bool reordered = false;
for (SampleDataLikelihoods::iterator s = samplesLikelihoods.begin(); s != samplesLikelihoods.end(); ++s) {
reordered |= sortSampleDataLikelihoodsScaledByMarginals(*s);
}
return reordered;
}
// assumes that the data likelihoods are sorted
void
dataLikelihoodMaxGenotypeCombo(
GenotypeCombo& combo,
SampleDataLikelihoods& sampleDataLikelihoods,
long double theta,
bool pooled,
bool ewensPriors,
bool permute,
bool hwePriors,
bool binomialObsPriors,
bool alleleBalancePriors,
long double diffusionPriorScalar) {
for (SampleDataLikelihoods::iterator s = sampleDataLikelihoods.begin();
s != sampleDataLikelihoods.end(); ++s) {
SampleDataLikelihood* sdl = &s->at(0);
combo.push_back(sdl);
combo.probObsGivenGenotypes += sdl->prob;
}
combo.init(binomialObsPriors);
combo.calculatePosteriorProbability(theta,
pooled,
ewensPriors,
permute,
hwePriors,
binomialObsPriors,
alleleBalancePriors,
diffusionPriorScalar);
}
void
makeComboByDatalLikelihoodRank(
GenotypeCombo& combo,
vector<int>& initialPosition, // starting combo in terms of offsets from data likelihood maximum
SampleDataLikelihoods& variantSampleDataLikelihoods,
SampleDataLikelihoods& invariantSampleDataLikelihoods,
map<string, int>& priorACs,
long double theta,
bool pooled,
bool ewensPriors,
bool permute,
bool hwePriors,
bool binomialObsPriors,
bool alleleBalancePriors,
long double diffusionPriorScalar) {
// generate the best genotype combination according to data
// likelihoods
vector<int>::iterator offset = initialPosition.begin();
for (SampleDataLikelihoods::iterator s = variantSampleDataLikelihoods.begin();
s != variantSampleDataLikelihoods.end(); ++s) {
// use the offsets to generate the starting combination
SampleDataLikelihood* sdl = &s->at(*offset++);
combo.push_back(sdl);
combo.probObsGivenGenotypes += sdl->prob;
}
// these samples have well-differentiated data likelihoods, and
// aren't changed during posterior integration
for (SampleDataLikelihoods::iterator s = invariantSampleDataLikelihoods.begin();
s != invariantSampleDataLikelihoods.end(); ++s) {
SampleDataLikelihood* sdl = &s->at(*offset++);
combo.push_back(sdl);
combo.probObsGivenGenotypes += sdl->prob;
}
combo.init(binomialObsPriors);
// add the prior ACs into the comob allele counters
combo.addPriorAlleleCounts(priorACs);
combo.calculatePosteriorProbability(theta,
pooled,
ewensPriors,
permute,
hwePriors,
binomialObsPriors,
alleleBalancePriors,
diffusionPriorScalar);
}
// 'local' genotype combinations which step only in one sample away from the
// data likelihood maxiumum. deal with all genotypes.
void
allLocalGenotypeCombinations(
list<GenotypeCombo>& combos,
GenotypeCombo& comboKing,
SampleDataLikelihoods& sampleDataLikelihoods,
Samples& samples,
map<string, int>& priorACs,
long double theta,
bool pooled,
bool ewensPriors,
bool permute,
bool hwePriors,
bool binomialObsPriors,
bool alleleBalancePriors,
long double diffusionPriorScalar,
bool keepCombos) {
// make the data likelihood maximum if needed
if (comboKing.empty()) {
vector<int> initialPosition;
initialPosition.assign(sampleDataLikelihoods.size(), 0);
SampleDataLikelihoods nullDataLikelihoods; // dummy variable
makeComboByDatalLikelihoodRank(comboKing,
initialPosition,
sampleDataLikelihoods,
nullDataLikelihoods,
priorACs,
theta,
pooled,
ewensPriors,
permute,
hwePriors,
binomialObsPriors,
alleleBalancePriors,
diffusionPriorScalar);
}
// ensure the comboKing is added
if (combos.empty()) {
combos.push_back(comboKing);
}
// for each sampledatalikelihood
// add a combo for each genotype where the combo is one step from the comboKing
size_t sampleOffset = 0;
//GenotypeCombo::iterator sampleGenotypeItr = comboKing.begin();
for (SampleDataLikelihoods::iterator s = sampleDataLikelihoods.begin();
s != sampleDataLikelihoods.end(); ++s, ++sampleOffset) {
SampleDataLikelihood& oldsdl = *comboKing.at(sampleOffset);
vector<SampleDataLikelihood>& sdls = *s;
for (vector<SampleDataLikelihood>::iterator dl = sdls.begin(); dl != sdls.end(); ++dl) {
SampleDataLikelihood& newsdl = *dl;
if (newsdl.genotype == oldsdl.genotype) { // don't duplicate the comboKing
continue;
}
combos.push_back(comboKing);
GenotypeCombo& combo = combos.back();
// get the old and new genotypes, which we compare
// to change the cached counts and probability of
// the combo
combo.updateCachedCounts(oldsdl.sample,
oldsdl.genotype, newsdl.genotype,
binomialObsPriors);
// replace genotype with new genotype
combo.at(sampleOffset) = &*dl;
// find data likelihood difference from ComboKing
long double diff = oldsdl.prob - newsdl.prob;
// adjust combination total data likelihood
combo.probObsGivenGenotypes -= diff;
combo.calculatePosteriorProbability(theta,
pooled,
ewensPriors,
permute,
hwePriors,
binomialObsPriors,
alleleBalancePriors,
diffusionPriorScalar);
// TODO
// memory-saving intervention, improve this
// difficult if we want to calculate marginals...
if (!keepCombos) {
// we should only have two combos in the list now...
if (combos.front().posteriorProb < combos.back().posteriorProb) {
combos.pop_front();
} else {
combos.pop_back();
}
}
}
}
GenotypeComboResultSorter gcrSorter;
combos.sort(gcrSorter);
combos.unique();
}
bool
bandedGenotypeCombinations(
list<GenotypeCombo>& combos,
GenotypeCombo& comboKing,
SampleDataLikelihoods& variantSampleDataLikelihoods,
SampleDataLikelihoods& invariantSampleDataLikelihoods,
Samples& samples,
map<string, int>& priorACs,
int bandwidth, int banddepth,
long double theta,
bool pooled,
bool ewensPriors,
bool permute,
bool hwePriors,
bool binomialObsPriors,
bool alleleBalancePriors,
long double diffusionPriorScalar,
bool keepCombos) {
// get the number of samples that vary
int nsamples = variantSampleDataLikelihoods.size();
// cap bandwidth at the number of variant samples
bandwidth = (bandwidth > nsamples) ? nsamples : bandwidth;
// no variant samples
if (nsamples == 0) {
combos.push_back(comboKing);
return true;
}
// overview:
//
// For each order of indexes in the bandwidth and banddepth, Obtain
// all multiset permutations of a set of indexes. Then use these
// indexes to get the nth-best genotype from each individual's set
// of genotypes for which we have data likelihoods
// (sampleDataLikelihoods), and turn this set into a genotype
// combination. Update the combination probability inline here so
// we don't incur O(N^2) penalty calculating the probability within
// our genotypeCombinationPriors calculation loop, where we
// estimate the posterior probability of each genotype combination
// given its data likelihood and the prior probability of the
// distribution of alleles it represents.
//
// example (bandwidth = 2, banddepth = 2)
// indexes: 0 0 0 0 1, 0 0 0 1 1
//
// permutations: 0 0 0 0 1
// 0 0 0 1 0
// 0 0 1 0 0
// 0 1 0 0 0
// 1 0 0 0 0
// 1 1 0 0 0
// 0 1 1 0 0
// 1 0 1 0 0
// 0 1 0 1 0
// 0 0 1 1 0
// 1 0 0 1 0
// 0 1 0 0 1
// 0 0 1 0 1
// 0 0 0 1 1
// 1 0 0 0 1
//
// We then convert these permutation to genotype combinations by
// using the index to pick the nth-best genotype according to
// sorted individual genotype data likelihoods.
//
// In addition to this simple case, We can flexibly extend this to
// larger search spaces by changing the depth and width of the
// deviations from the data likelihood maximizer (aka 'king').
//
vector<int> depths;
depths.reserve(banddepth);
for (int i = 0; i < banddepth; ++i) {
depths.push_back(i);
}
vector<vector<int> > deviations = multichoose(bandwidth, depths);
// skip the first vector, which will always be the same as the
// combo king, and has been pushed into our combinations already
for (vector<vector<int> >::iterator d = deviations.begin(); d != deviations.end(); ++d) {
vector<int>& indexes = *d;
indexes.reserve(nsamples);
for (int h = 0; h < (nsamples - bandwidth); ++h) {
indexes.push_back(0);
}
vector<vector<int> > indexPermutations = multipermute(indexes);
for (vector<vector<int> >::const_iterator p = indexPermutations.begin(); p != indexPermutations.end(); ++p) {
combos.push_back(comboKing); // copy the king, and then we'll modify it according to the indicies
GenotypeCombo& combo = combos.back();
GenotypeCombo::iterator sampleGenotypeItr = combo.begin();
vector<int>::const_iterator n = p->begin();
for (SampleDataLikelihoods::iterator s = variantSampleDataLikelihoods.begin();
s != variantSampleDataLikelihoods.end(); ++s, ++n, ++sampleGenotypeItr) {
SampleDataLikelihood& oldsdl = **sampleGenotypeItr;
SampleDataLikelihood*& oldsdl_ptr = *sampleGenotypeItr;
vector<SampleDataLikelihood>& sdls = *s;
int offset = *n + oldsdl.rank;
if (offset > 0) {
// shift-back if this combo is beyond the bounds of the individual's set of genotypes
offset %= s->size();
SampleDataLikelihood* newsdl = &sdls.at(offset);
// get the old and new genotypes, which we compare
// to change the cached counts and probability of
// the combo
combo.updateCachedCounts(oldsdl.sample,
oldsdl.genotype, newsdl->genotype,
binomialObsPriors);
// replace genotype with new genotype
oldsdl_ptr = newsdl;
// find data likelihood difference from ComboKing
long double diff = oldsdl.prob - newsdl->prob;
// adjust combination total data likelihood
combo.probObsGivenGenotypes -= diff;
}
}
combo.calculatePosteriorProbability(theta,
pooled,
ewensPriors,
permute,
hwePriors,
binomialObsPriors,
alleleBalancePriors,
diffusionPriorScalar);
if (!keepCombos && combos.size() > 1) {
// we should only have two combos in the list now...
if (combos.front().posteriorProb < combos.back().posteriorProb) {
combos.pop_front();
} else {
combos.pop_back();
}
}
}
}
GenotypeComboResultSorter gcrSorter;
combos.sort(gcrSorter);
combos.unique();
return true;
}
void
convergentGenotypeComboSearch(
list<GenotypeCombo>& combos,
GenotypeCombo& comboKing,
SampleDataLikelihoods& sampleDataLikelihoods,
SampleDataLikelihoods& variantSampleDataLikelihoods,
SampleDataLikelihoods& invariantSampleDataLikelihoods,
Samples& samples,
vector<Allele>& genotypeAlleles,
map<string, int>& priorACs,
int bandwidth, int banddepth,
long double theta,
bool pooled,
bool ewensPriors,
bool permute,
bool hwePriors,
bool binomialObsPriors,
bool alleleBalancePriors,
long double diffusionPriorScalar,
int maxiterations,
int& totaliterations,
bool addHomozygousCombos) {
if (comboKing.empty()) {
// seed EM with the data likelihood maximum
vector<int> initialPosition;
initialPosition.assign(sampleDataLikelihoods.size(), 0);
makeComboByDatalLikelihoodRank(comboKing,
initialPosition,
variantSampleDataLikelihoods,
invariantSampleDataLikelihoods,
priorACs,
theta,
pooled,
ewensPriors,
permute,
hwePriors,
binomialObsPriors,
alleleBalancePriors,
diffusionPriorScalar);
}
// set best position, which is updated during the EM step
GenotypeCombo bestCombo = comboKing;
int i = 0;
for (; i < maxiterations; ++i) {
combos.clear();
if (bandwidth == 0 && banddepth == 0) {
allLocalGenotypeCombinations(
combos,
bestCombo,
sampleDataLikelihoods,
samples,
priorACs,
theta,
pooled,
ewensPriors,
permute,
hwePriors,
binomialObsPriors,
alleleBalancePriors,
diffusionPriorScalar,
false); // throw away combos, so as to reduce memory usage
} else {
bandedGenotypeCombinations(
combos,
bestCombo,
variantSampleDataLikelihoods,
invariantSampleDataLikelihoods,
samples,
priorACs,
bandwidth,
banddepth,
theta,
pooled,
ewensPriors,
permute,
hwePriors,
binomialObsPriors,
alleleBalancePriors,
diffusionPriorScalar,
false); // throw away combos, so as to reduce memory usage
}
//cerr << "combos size = " << combos.size() << endl;
//cerr << "best combo: " << combos.front() << endl;
// check for convergence
//
// either we've converged on the best homozygous combo, which suggests
// weak support for variation, or we've got the same combo twice in a
// row as our best
if (combos.front().isHomozygous() || bestCombo == combos.front()) {
// we've converged
if (bandwidth == 0 && banddepth == 0) {
// XXX temporary hack
// get the rest of the combos in memory so we can do computation with them...
allLocalGenotypeCombinations(
combos,
combos.front(),
sampleDataLikelihoods,
samples,
priorACs,
theta,
pooled,
ewensPriors,
permute,
hwePriors,
binomialObsPriors,
alleleBalancePriors,
diffusionPriorScalar,
true); // keep combos
} else {
bandedGenotypeCombinations(
combos,
bestCombo,
variantSampleDataLikelihoods,
invariantSampleDataLikelihoods,
samples,
priorACs,
bandwidth,
banddepth,
theta,
pooled,
ewensPriors,
permute,
hwePriors,
binomialObsPriors,
alleleBalancePriors,
diffusionPriorScalar,
true); // keep combos
}
break;
} else {
bestCombo = combos.front();
}
}
//cout << i << " iterations" << "\t" << variantSampleDataLikelihoods.size() << " varying samples"
// << " and " << invariantSampleDataLikelihoods.size() << " invariant samples" << endl;
totaliterations = i;
// add the homozygous cases
if (addHomozygousCombos) {
addAllHomozygousCombos(combos,
sampleDataLikelihoods,
variantSampleDataLikelihoods,
invariantSampleDataLikelihoods,
samples,
genotypeAlleles,
theta,
pooled,
ewensPriors,
permute,
hwePriors,
binomialObsPriors,
alleleBalancePriors,
diffusionPriorScalar);
}
}
void addAllHomozygousCombos(
list<GenotypeCombo>& combos,
SampleDataLikelihoods& sampleDataLikelihoods,
SampleDataLikelihoods& variantSampleDataLikelihoods,
SampleDataLikelihoods& invariantSampleDataLikelihoods,
Samples& samples,
vector<Allele>& genotypeAlleles,
long double theta,
bool pooled,
bool ewensPriors,
bool permute,
bool hwePriors,
bool binomialObsPriors,
bool alleleBalancePriors,
long double diffusionPriorScalar) {
// determine which homozygous combos we already have
map<Allele, bool> allelesWithHomozygousCombos;
for (list<GenotypeCombo>::iterator c = combos.begin(); c != combos.end(); ++c) {
bool allSameAndHomozygous = true;
GenotypeCombo::iterator gc = c->begin();
Genotype* genotype;
if ((*gc)->genotype->homozygous) {
genotype = (*gc)->genotype;
} else {
continue;
}
for (; gc != c->end(); ++gc) {
if (! ((*gc)->genotype == genotype) ) {
allSameAndHomozygous = false;
break;
}
}
if (allSameAndHomozygous) {
allelesWithHomozygousCombos[genotype->front().allele] = true;
}
}
// accumulate the needed homozygous combos
map<Allele, GenotypeCombo> homozygousCombos;
for (vector<Allele>::iterator a = genotypeAlleles.begin(); a != genotypeAlleles.end(); ++a) {
Allele& allele = *a;
map<Allele, bool>::iterator g = allelesWithHomozygousCombos.find(allele);
if (g == allelesWithHomozygousCombos.end()) {
// we need to make a new combo
// iterate through the sample genotype vector
GenotypeCombo& combo = homozygousCombos[allele];
// match the way we make combos in bandedCombos*()
SampleDataLikelihoods::iterator s = variantSampleDataLikelihoods.begin();
while (s != invariantSampleDataLikelihoods.end()) {
// for each sample genotype, if the genotype is the same as our currently needed genotype, push it back onto a new combo
for (vector<SampleDataLikelihood>::iterator d = s->begin(); d != s->end(); ++d) {
SampleDataLikelihood& sdl = *d;
// this check is ploidy-independent
if (sdl.genotype->homozygous && sdl.genotype->front().allele == allele) {
combo.push_back(&sdl);
break;
}
}
++s;
if (s == variantSampleDataLikelihoods.end()) {
s = invariantSampleDataLikelihoods.begin();
}
}
}
}
// accumulate homozygous combos and set their combo data probabilities
for (map<Allele, GenotypeCombo>::iterator c = homozygousCombos.begin(); c != homozygousCombos.end(); ++c) {
GenotypeCombo& gc = c->second;
if (gc.empty()) {
continue;
}
gc.probObsGivenGenotypes = 0;
for (GenotypeCombo::iterator sdl = gc.begin(); sdl != gc.end(); ++sdl) {
gc.probObsGivenGenotypes += (*sdl)->prob; // set up data likelihood for combo
}
gc.init(binomialObsPriors); // cache allele frequency information
gc.calculatePosteriorProbability(theta,
pooled,
ewensPriors,
permute,
hwePriors,
binomialObsPriors,
alleleBalancePriors,
diffusionPriorScalar);
combos.push_back(gc);
}
GenotypeComboResultSorter gcrSorter;
combos.sort(gcrSorter);
combos.unique();
/*
for (list<GenotypeCombo>::iterator g = combos.begin(); g != combos.end(); ++g) {
GenotypeCombo& gc = *g;
cerr << gc << endl
<< "," << gc.probObsGivenGenotypes
<< "," << gc.posteriorProb
<< "," << gc.priorProbG_Af
<< "," << gc.priorProbAf
<< "," << gc.priorProbObservations
<< endl;
map<int, int> acs = gc.countFrequencies();
for (map<int, int>::iterator a = acs.begin(); a != acs.end(); ++a) {
cerr << a->first << " " << a->second << endl;
}
cerr << "***************************" << endl;
}
*/
}
// conditional probability of the genotype combination given the represented allele frequencies
long double GenotypeCombo::probabilityGivenAlleleFrequencyln(bool permute) {
//return -multinomialCoefficientLn(numberOfAlleles(), counts());
int n = numberOfAlleles();
long double lnhetscalar = 0;
if (permute) {
// scale by the product of permutations of heterozygotes
lnhetscalar = permutationsln; // cached permutations of this combo
}
return lnhetscalar - multinomialCoefficientLn(n, counts());
}
long double GenotypeCombo::hweComboProb(void) {
long double comboHweProb = 0;
for (map<Genotype*, int>::iterator gc = genotypeCounts.begin(); gc != genotypeCounts.end(); ++gc) {
Genotype* genotype = gc->first;
comboHweProb += hweProbGenotypeFrequencyln(genotype);
}
return comboHweProb;
}
// probability of the combo under HWE
long double GenotypeCombo::hweExpectedFrequencyln(Genotype* genotype) {
int ploidy = genotype->ploidy;
vector<int> genotypeAlleleCounts;
vector<long double> alleleFrequencies;
for (map<string, AlleleCounter>::iterator a = alleleCounters.begin(); a != alleleCounters.end(); ++a) {
genotypeAlleleCounts.push_back(genotype->alleleCount(a->first));
alleleFrequencies.push_back((long double) a->second.frequency / (long double) numberOfAlleles());
}
long double HWECoefficientln = multinomialCoefficientLn(ploidy, genotypeAlleleCounts);
vector<int>::iterator c = genotypeAlleleCounts.begin();
vector<long double>::iterator f = alleleFrequencies.begin();
for (; c != genotypeAlleleCounts.end(); ++c, ++f) {
HWECoefficientln += powln(log(*f), *c);
}
return HWECoefficientln;
}
// probability that the genotype count in the combo is what it is given the
// counts of the other alleles
long double GenotypeCombo::hweProbGenotypeFrequencyln(Genotype* genotype) {
//cout << endl << *genotype << endl;
int popTotalAlleles = numberOfAlleles();
//cout << "popTotalAlleles = " << popTotalAlleles << endl;
vector<int> popAlleleCounts;
vector<int> thisGenotypeAlleleCounts;
for (map<string, AlleleCounter>::iterator a = alleleCounters.begin(); a != alleleCounters.end(); ++a) {
//cout << a->first << "\t" << a->second.frequency << "\t" << genotype->alleleCount(a->first) << endl;
popAlleleCounts.push_back(a->second.frequency);
thisGenotypeAlleleCounts.push_back(genotype->alleleCount(a->first));
}
int popTotalGenotypes = 0;
vector<int> popGenotypeCounts;
// for haploid, estimate as if we have all ploidy 1
if (genotype->ploidy == 1) {
for (map<string, AlleleCounter>::iterator a = alleleCounters.begin(); a != alleleCounters.end(); ++a) {
popGenotypeCounts.push_back(a->second.frequency);
popTotalGenotypes += a->second.frequency;
}
} else {
for (map<Genotype*, int>::iterator g = genotypeCounts.begin(); g != genotypeCounts.end(); ++g) {
if (g->first->ploidy == genotype->ploidy) {
//cout << *g->first << "\t" << g->second << endl;
popGenotypeCounts.push_back(g->second);
popTotalGenotypes += g->second;
}
}
}
long double arrangementsOfAllelesInSample = multinomialCoefficientLn(popTotalAlleles, popAlleleCounts);
//cout << "arrangementsOfAllelesInSample = " << exp(arrangementsOfAllelesInSample) << endl;
long double arrangementsWithExactlyCountGenotypesGivenAF =
multinomialCoefficientLn(genotype->ploidy, thisGenotypeAlleleCounts)
+ multinomialCoefficientLn(popTotalGenotypes, popGenotypeCounts);
/*
cout << "multinomialCoefficientLn(genotype->ploidy, thisGenotypeAlleleCounts) = "
<< exp(multinomialCoefficientLn(genotype->ploidy, thisGenotypeAlleleCounts)) << endl;
cout << "multinomialCoefficientLn(popTotalGenotypes, popGenotypeCounts) = "
<< exp(multinomialCoefficientLn(popTotalGenotypes, popGenotypeCounts)) << endl;
cout << "arrangementsWithExactlyCountGenotypesGivenAF = " << exp(arrangementsWithExactlyCountGenotypesGivenAF) << endl;
cout << "hwe prob = " << exp(arrangementsWithExactlyCountGenotypesGivenAF - arrangementsOfAllelesInSample) << endl;
*/
return arrangementsWithExactlyCountGenotypesGivenAF - arrangementsOfAllelesInSample;
}
// core calculation of genotype combination likelihoods
//
void
GenotypeCombo::calculatePosteriorProbability(
long double theta,
bool pooled,
bool ewensPriors,
bool permute,
bool hwePriors,
bool binomialObsPriors,
bool alleleBalancePriors,
long double diffusionPriorScalar) {
posteriorProb = 0;
priorProb = 0;
priorProbG_Af = 0;
priorProbAf = 0;
priorProbObservations = 0;
priorProbGenotypesGivenHWE = 0;
// when we are operating on pooled samples, we will not be able to
// ascertain the number of heterozygotes in the pool,
// rendering P(Genotype combo | Allele frequency) meaningless
if (!pooled) {
priorProbG_Af = probabilityGivenAlleleFrequencyln(permute);
}
// XXX XXX hwe
if (hwePriors) {
for (map<Genotype*, int>::iterator gc = genotypeCounts.begin(); gc != genotypeCounts.end(); ++gc) {
Genotype* genotype = gc->first;
priorProbGenotypesGivenHWE += hweProbGenotypeFrequencyln(genotype);
}
}
if (binomialObsPriors) {
// for each alternate and the reference allele
// calculate the binomial probability that we see the given strand balance and read placement prob
//cerr << *this << endl;
for (map<string, AlleleCounter>::iterator ac = alleleCounters.begin(); ac != alleleCounters.end(); ++ac) {
//const string& allele = ac->first;
const AlleleCounter& alleleCounter = ac->second;
int obs = alleleCounter.observations;
/*
cerr << endl
<< "--------------------------------------------" << endl;
cerr << " counts: " << alleleCounter.frequency
<< " observations " << alleleCounter.observations
<< " " << alleleCounter.forwardStrand
<< "," << alleleCounter.reverseStrand
<< " " << alleleCounter.placedLeft
<< "," << alleleCounter.placedRight
<< " " << alleleCounter.placedStart
<< "," << alleleCounter.placedEnd
<< endl;
cerr << "priorProbObservations = " << priorProbObservations << endl;
cerr << "binprobln strand = " << binomialProbln(alleleCounter.forwardStrand, obs, 0.5) << endl;
cerr << "binprobln position = " << binomialProbln(alleleCounter.placedLeft, obs, 0.5) << endl;
cerr << "binprobln start = " << binomialProbln(alleleCounter.placedStart, obs, 0.5) << endl;
*/
priorProbObservations
+= binomialProbln(alleleCounter.forwardStrand, obs, 0.5)
+ binomialProbln(alleleCounter.placedLeft, obs, 0.5)
+ binomialProbln(alleleCounter.placedStart, obs, 0.5);
}
}
// ok... now do the same move for the observation counts
// --- this should capture "Allele Balance"
if (alleleBalancePriors) {
priorProbObservations += multinomialSamplingProbLn(alleleProbs(), observationCounts());
}
// with larger population samples, the effect of
// P(Genotype combo | Allele frequency) may bias us against reporting
// true variants which are under selection despite overwhelming evidence
// for variation. this allows us to scale the effect of this prior
if (diffusionPriorScalar != 1) {
priorProbG_Af /= diffusionPriorScalar;
}
// Ewens' Sampling Formula
if (ewensPriors) {
priorProbAf = alleleFrequencyProbabilityln(countFrequencies(), theta);
}
// posterior probability
/*
cerr << "priorProbG_Af " << priorProbG_Af << endl
<< "priorProbAf " << priorProbAf << endl
<< "priorProbObservations " << priorProbObservations << endl
<< "priorProbGenotypesGivenHWE " << priorProbGenotypesGivenHWE << endl
<< "probObsGivenGenotypes " << probObsGivenGenotypes << endl;
*/
priorProb = priorProbG_Af + priorProbAf + priorProbObservations + priorProbGenotypesGivenHWE;
posteriorProb = priorProb + probObsGivenGenotypes;
/*
cerr << "priorProb " << priorProb << endl;
cerr << "posteriorProb " << posteriorProb << endl;
cerr << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << endl << endl;
*/
}
pair<int, int> alternateAndReferenceCount(vector<Allele*>& observations, string& refbase, string altbase) {
int altcount = 0;
int refcount = 0;
for (vector<Allele*>::iterator allele = observations.begin(); allele != observations.end(); ++allele) {
if ((*allele)->currentBase == refbase)
++refcount;
else if ((*allele)->currentBase == altbase)
++altcount;
}
return make_pair(altcount, refcount);
}
void genotypeCombo2Map(GenotypeCombo& gc, GenotypeComboMap& gcm) {
for (GenotypeCombo::iterator g = gc.begin(); g != gc.end(); ++g) {
gcm[(*g)->name] = *g;;
}
}
void orderedGenotypeCombo(
GenotypeCombo& combo,
GenotypeCombo& orderedCombo,
SampleDataLikelihoods& sampleDataLikelihoods,
long double theta,
bool pooled,
bool ewensPriors,
bool permute,
bool hwePriors,
bool binomialObsPriors,
bool alleleBalancePriors,
long double diffusionPriorScalar) {
GenotypeComboMap bestComboMap;
genotypeCombo2Map(combo, bestComboMap);
for (SampleDataLikelihoods::iterator sdl = sampleDataLikelihoods.begin(); sdl != sampleDataLikelihoods.end(); ++sdl) {
orderedCombo.push_back(bestComboMap[sdl->front().name]);
}
orderedCombo.init(binomialObsPriors);
orderedCombo.calculatePosteriorProbability(theta, pooled, ewensPriors, permute,
hwePriors, binomialObsPriors, alleleBalancePriors,
diffusionPriorScalar);
}
// returns a list of the alternate alleles represented by the given genotype
// combo sorted by frequency
vector<pair<Allele, int> > alternateAlleles(GenotypeCombo& combo, string referenceBase) {
map<Allele, int> alternates;
for (GenotypeCombo::iterator g = combo.begin(); g != combo.end(); ++g) {
vector<Allele> alts = (*g)->genotype->alternateAlleles(referenceBase);
for (vector<Allele>::iterator a = alts.begin(); a != alts.end(); ++a) {
if (alternates.find(*a) == alternates.end()) {
alternates[*a] = 1;
} else {
alternates[*a] += 1;
}
}
}
vector<pair<Allele, int> > sortedAlternates;
for (map<Allele, int>::iterator a = alternates.begin(); a != alternates.end(); ++a) {
sortedAlternates.push_back(make_pair(a->first, a->second));
}
AllelePairIntCompare alleleCountCompare;
sort(sortedAlternates.begin(), sortedAlternates.end(), alleleCountCompare);
return sortedAlternates;
}
int Genotype::containedAlleleTypes(void) {
int t = 0;
for (Genotype::iterator g = begin(); g != end(); ++g) {
t |= g->allele.type;
}
return t;
}
vector<int> Genotype::alleleObservationCounts(Sample& sample) {
vector<int> counts;
for (Genotype::iterator i = begin(); i != end(); ++i) {
Allele& b = i->allele;
counts.push_back(sample.observationCount(b));
}
return counts;
}
int Genotype::alleleObservationCount(Sample& sample) {
int count = 0;
for (Genotype::iterator i = begin(); i != end(); ++i) {
Allele& b = i->allele;
count += sample.observationCount(b);
}
return count;
}
bool Genotype::sampleHasSupportingObservations(Sample& sample) {
for (Genotype::iterator i = begin(); i != end(); ++i) {
Allele& b = i->allele;
if (sample.observationCount(b) != 0) {
return true;
}
}
return false;
}
bool Genotype::sampleHasSupportingObservationsForAllAlleles(Sample& sample) {
vector<int> counts = alleleObservationCounts(sample);
for (vector<int>::iterator c = counts.begin(); c != counts.end(); ++c) {
if (*c == 0) {
return false;
}
}
return true;
}
map<int, vector<Genotype> > getGenotypesByPloidy(vector<int>& ploidies, vector<Allele>& genotypeAlleles) {
map<int, vector<Genotype> > genotypesByPloidy;
for (vector<int>::iterator p = ploidies.begin(); p != ploidies.end(); ++p) {
int ploidy = *p;
if (genotypesByPloidy.find(ploidy) == genotypesByPloidy.end()) {
genotypesByPloidy[ploidy] = allPossibleGenotypes(ploidy, genotypeAlleles);
}
}
return genotypesByPloidy;
}
vector<Genotype*> Genotype::nullMatchingGenotypes(vector<Genotype>& gts) {
vector<Genotype*> results;
// assert that this genotype has null alleles
for (vector<Genotype>::iterator g = gts.begin(); g != gts.end(); ++g) {
Genotype& genotype = *g;
if (genotype.ploidy == ploidy) {
bool match = true;
// if the non-null alleles and counts are the same between genotypes, add the genotype to the results
// null matching genotypes have the same number of alleles and alts as this genotype,
for (Genotype::iterator gt = begin(); gt != end(); ++gt) {
if (genotype.alleleCount(gt->allele) != gt->count) {
match = false;
}
}
if (match) {
results.push_back(&*g);
}
}
}
return results;
}
bool Genotype::hasNullAllele(void) {
return alleleCount("N") != 0;
}
void GenotypeCombo::appendIndependentCombo(GenotypeCombo& other) {
for (map<string, AlleleCounter>::iterator c = other.alleleCounters.begin(); c != other.alleleCounters.end(); ++c) {
const string& allele = c->first;
AlleleCounter& otherCounter = c->second;
AlleleCounter& thisCounter = alleleCounters[allele];
thisCounter.frequency += otherCounter.frequency;
thisCounter.observations += otherCounter.observations;
thisCounter.forwardStrand += otherCounter.forwardStrand;
thisCounter.reverseStrand += otherCounter.reverseStrand;
thisCounter.placedLeft += otherCounter.placedLeft;
thisCounter.placedRight += otherCounter.placedRight;
thisCounter.placedStart += otherCounter.placedStart;
thisCounter.placedEnd += otherCounter.placedEnd;
}
for (GenotypeCombo::iterator s = begin(); s != end(); ++s) {
const SampleDataLikelihood& sdl = **s;
const Sample& sample = *sdl.sample;
++genotypeCounts[sdl.genotype];
}
// permutations
permutationsln += other.permutationsln;
// combine probabilities assuming conditional independence between these two combinations
// data likelihood
probObsGivenGenotypes += other.probObsGivenGenotypes;
// posterior
posteriorProb += other.posteriorProb;
// priors
priorProb += other.priorProb;
priorProbG_Af += other.priorProbG_Af;
priorProbAf += other.priorProbAf;
priorProbObservations += other.priorProbObservations;
priorProbGenotypesGivenHWE += other.priorProbGenotypesGivenHWE;
// add the other sample data likelihoods to this combo
reserve(size() + distance(other.begin(), other.end()));
insert(end(), other.begin(), other.end());
}
// all combos of each population are combined with the best combos of the other pops
// combines all like homozygous combos
void combinePopulationCombos(list<GenotypeCombo>& genotypeCombos, map<string, list<GenotypeCombo> >& genotypeCombosByPopulation) {
if (genotypeCombosByPopulation.size() == 1) {
// one pop, default case is to just pass forward the current set of combos
genotypeCombos = genotypeCombosByPopulation.begin()->second;
} else {
// for each sub-pop
for (map<string, list<GenotypeCombo> >::iterator p = genotypeCombosByPopulation.begin(); p != genotypeCombosByPopulation.end(); ++p) {
const string& population = p->first;
list<GenotypeCombo>& populationGenotypeCombos = p->second;
GenotypeCombo otherPopulationsBestCombo;
// run through all the other combos to generate a best combo for the
// other populations, and accumulate homozygous combos, keyed by allele
for (map<string, list<GenotypeCombo> >::iterator o = genotypeCombosByPopulation.begin(); o != genotypeCombosByPopulation.end(); ++o) {
if (o->first != p->first) { // if the genotype list is for a different population
GenotypeCombo& bestCombo = o->second.front(); // this is the "best" combo from the other population
// add the best combo from this population to the best combos from the other populations
if (otherPopulationsBestCombo.empty()) {
otherPopulationsBestCombo = bestCombo;
} else {
otherPopulationsBestCombo.appendIndependentCombo(bestCombo);
}
}
}
// append the best "other population" combo to all the combos in this set
for (list<GenotypeCombo>::iterator g = populationGenotypeCombos.begin(); g != populationGenotypeCombos.end(); ++g) {
genotypeCombos.push_back(*g);
genotypeCombos.back().appendIndependentCombo(otherPopulationsBestCombo);
}
}
map<Allele, GenotypeCombo> otherPopulationsHomozygousCombos;
// generate the homozygous combos for all the populations
for (map<string, list<GenotypeCombo> >::iterator o = genotypeCombosByPopulation.begin(); o != genotypeCombosByPopulation.end(); ++o) {
// accumulate all the homozygous combos into the otherPopulationsHomozygousCombos
for (list<GenotypeCombo>::iterator c = o->second.begin(); c != o->second.end(); ++c) {
GenotypeCombo& combo = *c;
if (combo.isHomozygous()) {
Allele& allele = combo.front()->genotype->alleles.front();
map<Allele, GenotypeCombo>::iterator g = otherPopulationsHomozygousCombos.find(allele);
if (g == otherPopulationsHomozygousCombos.end()) {
otherPopulationsHomozygousCombos[allele] = combo;
} else {
GenotypeCombo& homozygousCombo = g->second;
homozygousCombo.appendIndependentCombo(combo);
}
}
}
}
// and add them to the result set
for (map<Allele, GenotypeCombo>::iterator h = otherPopulationsHomozygousCombos.begin(); h!= otherPopulationsHomozygousCombos.end(); ++h) {
GenotypeCombo& combo = h->second;
//assert(genotypeCombos.back().size() == combo.size());
genotypeCombos.push_back(combo);
}
// sort the combined combos
GenotypeComboResultSorter gcrSorter;
genotypeCombos.sort(gcrSorter);
genotypeCombos.unique();
}
}
|