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 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930
|
/*
* abundances.cpp
* cufflinks
*
* Created by Cole Trapnell on 4/27/09.
* Copyright 2009 Cole Trapnell. All rights reserved.
*
* NOTE: some of the code in this file was derived from (Eriksson et al, 2008)
*/
#include "abundances.h"
#include <numeric>
#include <limits>
#include <algorithm>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/triangular.hpp>
//#define BOOST_UBLAS_TYPE_CHECK 0
#include <boost/numeric/ublas/lu.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/math/tools/roots.hpp>
#include <complex>
#include "filters.h"
#include "replicates.h"
#include "sampling.h"
#include "jensen_shannon.h"
//#define USE_LOG_CACHE
void compute_compatibilities(vector<shared_ptr<Abundance> >& transcripts,
const vector<MateHit>& alignments,
vector<vector<char> >& compatibilities)
{
int M = alignments.size();
int N = transcripts.size();
vector<Scaffold> alignment_scaffs;
for (size_t i = 0; i < alignments.size(); ++i)
{
const MateHit& hit = alignments[i];
alignment_scaffs.push_back(Scaffold(hit));
}
for (int j = 0; j < N; ++j)
{
shared_ptr<Scaffold> transfrag_j = transcripts[j]->transfrag();
for (int i = 0; i < M; ++i)
{
if (transfrag_j->contains(alignment_scaffs[i])
&& Scaffold::compatible(*transfrag_j, alignment_scaffs[i]))
{
compatibilities[j][i] = 1;
}
}
}
}
AbundanceGroup::AbundanceGroup(const vector<shared_ptr<Abundance> >& abundances,
const ublas::matrix<double>& gamma_covariance,
const ublas::matrix<double>& gamma_bootstrap_covariance,
const ublas::matrix<double>& iterated_exp_count_covariance,
const ublas::matrix<double>& count_covariance,
const ublas::matrix<double>& fpkm_covariance,
const long double max_mass_variance,
const set<shared_ptr<ReadGroupProperties const> >& rg_props) :
_abundances(abundances),
_iterated_exp_count_covariance(iterated_exp_count_covariance),
_count_covariance(count_covariance),
_fpkm_covariance(fpkm_covariance),
_gamma_covariance(gamma_covariance),
_gamma_bootstrap_covariance(gamma_bootstrap_covariance),
_max_mass_variance(max_mass_variance),
_salient_frags(0.0),
_total_frags(0.0),
_read_group_props(rg_props)
{
// Calling calculate_FPKM_covariance() also estimates cross-replicate
// count variances
// calculate_FPKM_covariance();
double fpkm_var = 0.0;
for (size_t i = 0; i < _fpkm_covariance.size1(); ++i)
{
for (size_t j = 0; j < _fpkm_covariance.size2(); ++j)
{
fpkm_var += _fpkm_covariance(i,j);
}
}
ublas::matrix<double> test = _count_covariance;
double ret = cholesky_factorize(test);
if (ret != 0)
{
//fprintf(stderr, "Warning: total count covariance is not positive definite!\n");
for (size_t j = 0; j < _abundances.size(); ++j)
{
_abundances[j]->status(NUMERIC_FAIL);
}
}
_FPKM_variance = fpkm_var;
if (final_est_run && library_type != "transfrags")
{
test = _fpkm_covariance;
ret = cholesky_factorize(test);
if (ret != 0 || (_FPKM_variance < 0 && status() == NUMERIC_OK))
{
//fprintf(stderr, "Warning: total count covariance is not positive definite!\n");
for (size_t j = 0; j < _abundances.size(); ++j)
{
_abundances[j]->status(NUMERIC_FAIL);
}
}
assert (FPKM() == 0 || fpkm_var > 0 || status() != NUMERIC_OK);
}
calculate_conf_intervals();
calculate_kappas();
}
AbundanceStatus AbundanceGroup::status() const
{
bool has_lowdata_member = false;
bool has_ok_member = false;
foreach(shared_ptr<Abundance> ab, _abundances)
{
if (ab->status() == NUMERIC_FAIL)
{
return NUMERIC_FAIL;
}
else if (ab->status() == NUMERIC_LOW_DATA)
{
has_lowdata_member = true;
//return NUMERIC_LOW_DATA;
}
else if (ab->status() == NUMERIC_HI_DATA)
{
return NUMERIC_HI_DATA;
}
else if (ab->status() == NUMERIC_OK)
{
has_ok_member = true;
}
}
if (has_ok_member == false)
return NUMERIC_LOW_DATA;
// check that the variance of the group is stable (w.r.t to bootstrap)
double total_cov = 0.0;
double total_gamma = 0.0;
for (size_t i = 0; i < _gamma_covariance.size1(); ++i)
{
for (size_t j = 0; j < _gamma_covariance.size2(); ++j)
{
total_cov += _gamma_covariance(i,j);
//total_bootstrap_cov += _gamma_bootstrap_covariance(i,j);
}
total_gamma = _abundances[i]->gamma();
//total_cov += _gamma_covariance(i,i);
//total_gamma += _gamma_bootstrap_covariance(i,i);
}
// if (total_cov > 0 && total_gamma > 0)
// {
// double bootstrap_gamma_delta = total_cov/total_gamma;
// //double gap = bootstrap_delta_gap * total_cov;
// if (bootstrap_gamma_delta > bootstrap_delta_gap)
// {
// return NUMERIC_LOW_DATA;
// }
// }
return NUMERIC_OK;
}
void TranscriptAbundance::FPKM_variance(double v)
{
assert (v >= 0);
assert(!isnan(v));
_FPKM_variance = v;
}
bool AbundanceGroup::has_member_with_status(AbundanceStatus member_status)
{
foreach(shared_ptr<Abundance> ab, _abundances)
{
if (ab->status() == member_status)
{
return true;
}
}
return false;
}
double AbundanceGroup::num_fragments() const
{
double num_f = 0;
foreach(shared_ptr<Abundance> ab, _abundances)
{
num_f += ab->num_fragments();
}
assert (!isnan(num_f));
return num_f;
}
double AbundanceGroup::mass_fraction() const
{
double mass = 0;
foreach(shared_ptr<Abundance> ab, _abundances)
{
mass += ab->mass_fraction();
}
return mass;
}
double AbundanceGroup::mass_variance() const
{
double mass_var = 0;
foreach(shared_ptr<Abundance> ab, _abundances)
{
mass_var += ab->mass_variance();
}
return mass_var;
}
double AbundanceGroup::FPKM() const
{
double fpkm = 0;
foreach(shared_ptr<Abundance> ab, _abundances)
{
fpkm += ab->FPKM();
}
return fpkm;
}
double AbundanceGroup::gamma() const
{
double gamma = 0;
foreach(shared_ptr<Abundance> ab, _abundances)
{
gamma += ab->gamma();
}
return gamma;
}
void AbundanceGroup::filter_group(const vector<bool>& to_keep,
AbundanceGroup& filtered_group) const
{
//filtered_group = AbundanceGroup();
assert (to_keep.size() == _abundances.size());
size_t num_kept = 0;
foreach(bool keeper, to_keep)
{
num_kept += keeper;
}
ublas::matrix<double> new_cov = ublas::zero_matrix<double>(num_kept,num_kept);
ublas::matrix<double> new_iterated_em_count_cov = ublas::zero_matrix<double>(num_kept,num_kept);
ublas::matrix<double> new_count_cov = ublas::zero_matrix<double>(num_kept,num_kept);
ublas::matrix<double> new_fpkm_cov = ublas::zero_matrix<double>(num_kept,num_kept);
ublas::matrix<double> new_boot_cov = ublas::zero_matrix<double>(num_kept,num_kept);
vector<shared_ptr<Abundance> > new_ab;
// rebuild covariance matrix and abundance vector after filtration
size_t next_cov_row = 0;
for (size_t i = 0; i < _abundances.size(); ++i)
{
if (to_keep[i])
{
new_ab.push_back(_abundances[i]);
size_t next_cov_col = 0;
for (size_t j = 0; j < _abundances.size(); ++j)
{
if (to_keep[j])
{
new_cov(next_cov_row,next_cov_col) = _gamma_covariance(i, j);
new_iterated_em_count_cov(next_cov_row,next_cov_col) = _iterated_exp_count_covariance(i, j);
new_count_cov(next_cov_row,next_cov_col) = _count_covariance(i, j);
new_fpkm_cov(next_cov_row,next_cov_col) = _fpkm_covariance(i, j);
new_boot_cov(next_cov_row,next_cov_col) = _gamma_bootstrap_covariance(i, j);
next_cov_col++;
}
}
next_cov_row++;
}
}
filtered_group = AbundanceGroup(new_ab,
new_cov,
new_boot_cov,
new_iterated_em_count_cov,
new_count_cov,
new_fpkm_cov,
_max_mass_variance,
_read_group_props);
}
void AbundanceGroup::get_transfrags(vector<shared_ptr<Abundance> >& transfrags) const
{
transfrags.clear();
foreach(shared_ptr<Abundance> pA, _abundances)
{
shared_ptr<Scaffold> pS = pA->transfrag();
if (pS)
{
transfrags.push_back(pA);
}
}
}
set<string> AbundanceGroup::gene_id() const
{
set<string> s;
foreach (shared_ptr<Abundance> pA, _abundances)
{
set<string> sub = pA->gene_id();
s.insert(sub.begin(), sub.end());
}
return s;
}
set<string> AbundanceGroup::gene_name() const
{
set<string> s;
foreach (shared_ptr<Abundance> pA, _abundances)
{
set<string> sub = pA->gene_name();
s.insert(sub.begin(), sub.end());
}
return s;
}
set<string> AbundanceGroup::tss_id() const
{
set<string> s;
foreach (shared_ptr<Abundance> pA, _abundances)
{
set<string> sub = pA->tss_id();
s.insert(sub.begin(), sub.end());
}
return s;
}
set<string> AbundanceGroup::protein_id() const
{
set<string> s;
foreach (shared_ptr<Abundance> pA, _abundances)
{
set<string> sub = pA->protein_id();
s.insert(sub.begin(), sub.end());
}
return s;
}
const string& AbundanceGroup::locus_tag() const
{
static string default_locus_tag = "-";
const string* pLast = NULL;
foreach (shared_ptr<Abundance> pA, _abundances)
{
if (pLast)
{
if (pA->locus_tag() != *pLast)
{
assert (false);
return default_locus_tag;
}
}
pLast = &(pA->locus_tag());
}
if (pLast)
{
return *pLast;
}
assert (false);
return default_locus_tag;
}
const string& AbundanceGroup::reference_tag() const
{
static string default_reference_tag = "-";
const string* pLast = NULL;
foreach (shared_ptr<Abundance> pA, _abundances)
{
if (pLast)
{
if (pA->reference_tag() != *pLast)
{
assert (false);
return default_reference_tag;
}
}
pLast = &(pA->reference_tag());
}
if (pLast)
{
return *pLast;
}
assert (false);
return default_reference_tag;
}
double AbundanceGroup::effective_length() const
{
double eff_len = 0.0;
double group_fpkm = FPKM();
if (group_fpkm == 0)
return 0;
foreach (shared_ptr<Abundance> ab, _abundances)
{
eff_len += (ab->effective_length() * (ab->FPKM() / group_fpkm));
}
return eff_len;
}
//void AbundanceGroup::collect_read_group_props()
//{
// size_t M = alignments.size();
//
// for (size_t i = 0; i < M; ++i)
// {
// if (!alignments[i].left_alignment())
// continue;
// shared_ptr<ReadGroupProperties const> rg_props = alignments[i].read_group_props();
//
// _read_group_props.insert(rg_props;
// }
//}
void AbundanceGroup::calculate_locus_scaled_mass_and_variance(const vector<MateHit>& alignments,
const vector<shared_ptr<Abundance> >& transcripts)
{
size_t M = alignments.size();
size_t N = transcripts.size();
if (transcripts.empty())
return;
map<shared_ptr<ReadGroupProperties const>, double> count_per_replicate;
for (size_t i = 0; i < M; ++i)
{
if (!alignments[i].left_alignment())
continue;
bool mapped = false;
for (size_t j = 0; j < N; ++j)
{
if (_abundances[j]->cond_probs()->at(i) > 0)
{
mapped = true;
break;
}
}
if (mapped)
{
shared_ptr<ReadGroupProperties const> rg_props = alignments[i].read_group_props();
//assert (parent != NULL);
pair<map<shared_ptr<ReadGroupProperties const>, double>::iterator, bool> inserted;
inserted = count_per_replicate.insert(make_pair(rg_props, 0.0));
_read_group_props.insert(rg_props);
double more_mass = alignments[i].collapse_mass();
inserted.first->second += more_mass;
}
}
double avg_X_g = 0.0;
double avg_mass_fraction = 0.0;
// as long as all the read groups share the same dispersion model (currently true)
// then all the variances from each read group will be the same, so this
// averaging step isn't strictly necessary. Computing it this way is simply
// convenient.
vector<double> avg_mass_variances(N, 0.0);
double max_mass_var = 0.0;
for (map<shared_ptr<ReadGroupProperties const>, double>::iterator itr = count_per_replicate.begin();
itr != count_per_replicate.end();
++itr)
{
shared_ptr<ReadGroupProperties const> rg_props = itr->first;
double scaled_mass = itr->second; //rg_props->scale_mass(itr->second);
double scaled_total_mass = rg_props->scale_mass(rg_props->normalized_map_mass());
avg_X_g += scaled_mass;
shared_ptr<MassDispersionModel const> disperser = rg_props->mass_dispersion_model();
for (size_t j = 0; j < N; ++j)
{
double scaled_variance;
scaled_variance = disperser->scale_mass_variance(scaled_mass * _abundances[j]->gamma());
avg_mass_variances[j] += scaled_variance;
}
assert (disperser->scale_mass_variance(scaled_mass) != 0 || scaled_mass == 0);
max_mass_var += disperser->scale_mass_variance(scaled_mass);
assert (scaled_total_mass != 0.0);
avg_mass_fraction += (scaled_mass / scaled_total_mass);
}
// Set the maximum mass variance in case we get an identifiability failure
// and need to bound the group expression.
if (!count_per_replicate.empty())
max_mass_var /= count_per_replicate.size();
double num_replicates = count_per_replicate.size();
if (num_replicates)
{
avg_X_g /= num_replicates;
avg_mass_fraction /= num_replicates;
for (size_t j = 0; j < N; ++j)
{
avg_mass_variances[j] /= num_replicates;
}
}
assert (max_mass_var != 0 || avg_X_g == 0);
max_mass_variance(max_mass_var);
for (size_t j = 0; j < _abundances.size(); ++j)
{
_abundances[j]->num_fragments(_abundances[j]->gamma() * avg_X_g);
double j_avg_mass_fraction = _abundances[j]->gamma() * avg_mass_fraction;
_abundances[j]->mass_fraction(j_avg_mass_fraction);
_abundances[j]->mass_variance(avg_mass_variances[j]);
if (j_avg_mass_fraction > 0)
{
double FPKM = j_avg_mass_fraction * 1000000000/ _abundances[j]->effective_length();
_abundances[j]->FPKM(FPKM);
}
else
{
_abundances[j]->FPKM(0);
_abundances[j]->mass_variance(0);
_abundances[j]->mass_fraction(0);
}
}
}
int total_cond_prob_calls = 0;
void collapse_equivalent_hits(const vector<MateHit>& alignments,
vector<shared_ptr<Abundance> >& transcripts,
vector<shared_ptr<Abundance> >& mapped_transcripts,
vector<MateHit>& nr_alignments,
vector<double>& log_conv_factors,
bool require_overlap = true)
{
int N = transcripts.size();
int M = alignments.size();
nr_alignments.clear();
vector<vector<char> > compatibilities(N, vector<char>(M,0));
compute_compatibilities(transcripts, alignments, compatibilities);
vector<vector<double> > cached_cond_probs (M, vector<double>());
vector<bool> replaced(M, false);
int num_replaced = 0;
vector<BiasCorrectionHelper> bchs;
for (size_t j = 0; j < N; ++j)
{
bchs.push_back(BiasCorrectionHelper(transcripts[j]->transfrag()));
}
for(int i = 0 ; i < M; ++i)
{
vector<double> cond_probs_i(N,0);
if (replaced[i] == true)
continue;
if (cached_cond_probs[i].empty())
{
for (int j = 0; j < N; ++j)
{
shared_ptr<Scaffold> transfrag = transcripts[j]->transfrag();
if (compatibilities[j][i]==1)
{
total_cond_prob_calls++;
cond_probs_i[j] = bchs[j].get_cond_prob(alignments[i]);
}
}
cached_cond_probs[i] = cond_probs_i;
}
else
{
cond_probs_i = cached_cond_probs[i];
}
MateHit* curr_align = NULL;
nr_alignments.push_back(alignments[i]);
curr_align = &nr_alignments.back();
log_conv_factors.push_back(0);
if (alignments[i].is_multi()) // don't reduce other hits into multihits
continue;
bool seen_olap = false;
for(int k = i + 1 ; k < M; ++k)
{
if (replaced[k] || alignments[k].is_multi() || alignments[i].read_group_props() != alignments[k].read_group_props())
continue;
if (require_overlap && !::overlap_in_genome(curr_align->left(), curr_align->right(),
alignments[k].left(), alignments[k].right()))
{
if (seen_olap)
break;
else
continue;
}
else
{
seen_olap = true;
}
vector<double>* cond_probs_k;
double last_cond_prob = -1;
bool equiv = true;
if (cached_cond_probs[k].empty())
{
cached_cond_probs[k] = vector<double>(N, 0.0);
cond_probs_k = &cached_cond_probs[k];
for (int j = 0; j < N; ++j)
{
shared_ptr<Scaffold> transfrag = transcripts[j]->transfrag();
if (compatibilities[j][k]==1)
{
total_cond_prob_calls++;
(*cond_probs_k)[j] = bchs[j].get_cond_prob(alignments[k]);
}
}
//cached_cond_probs[k] = cond_probs_k;
}
else
{
cond_probs_k = &cached_cond_probs[k];
}
for (int j = 0; j < N; ++j)
{
if ((*cond_probs_k)[j] != 0 && cond_probs_i[j] != 0)
{
double ratio = (*cond_probs_k)[j] / cond_probs_i[j];
if (last_cond_prob == -1)
{
//assert(ratio < 5);
last_cond_prob = ratio;
}
else
{
if (last_cond_prob != ratio)
{
equiv = false;
break;
}
}
}
else if ((*cond_probs_k)[j] == 0 && cond_probs_i[j] == 0)
{
// just do nothing in this iter.
// last_cond_prob = 0.0;
}
else
{
equiv = false;
break;
}
}
// cond_prob_i vector is a scalar multiple of cond_prob_k, so we
// can collapse k into i via the mass.
if (equiv && last_cond_prob > 0.0)
{
assert(curr_align->read_group_props() == alignments[k].read_group_props());
assert (last_cond_prob > 0);
//double mass_muliplier = sqrt(last_cond_prob);
double mass_multiplier = log(last_cond_prob);
//assert(last_cond_prob < 5);
assert (!isinf(mass_multiplier) && !isnan(mass_multiplier));
log_conv_factors[log_conv_factors.size() - 1] += mass_multiplier;
replaced[k] = true;
cached_cond_probs[k].clear();
vector<double>(cached_cond_probs[k]).swap(cached_cond_probs[k]);
num_replaced++;
//double scale_factor = alignments[k].common_scale_mass();
//double curr_align_mass = curr_align->collapse_mass();
//double more_mass = alignments[k].common_scale_mass() * alignments[k].collapse_mass() ;
double more_mass = alignments[k].collapse_mass();
curr_align->incr_collapse_mass(more_mass);
}
}
}
N = transcripts.size();
//M = nr_alignments.size();
for (int j = 0; j < N; ++j)
{
shared_ptr<Scaffold> transfrag = transcripts[j]->transfrag();
vector<double>& cond_probs = *(new vector<double>(nr_alignments.size(),0));
BiasCorrectionHelper& bch = bchs[j];
size_t last_cond_prob_idx = 0;
for(int i = 0 ; i < M; ++i)
{
if (!cached_cond_probs[i].empty())
{
if (compatibilities[j][i]==1)
{
assert (cached_cond_probs[i].size() > j);
cond_probs[last_cond_prob_idx] = cached_cond_probs[i][j];
}
last_cond_prob_idx++;
}
}
assert (last_cond_prob_idx == nr_alignments.size());
transcripts[j]->effective_length(bch.get_effective_length());
transcripts[j]->cond_probs(&cond_probs);
if (bch.is_mapped())
mapped_transcripts.push_back(transcripts[j]);
}
if (nr_alignments.size())
{
verbose_msg("\nReduced %lu frags to %lu (%lf percent)\n", alignments.size(), nr_alignments.size(), 100.0 * nr_alignments.size()/(double)alignments.size());
}
}
void collapse_equivalent_hits_helper(const vector<MateHit>& alignments,
vector<shared_ptr<Abundance> >& transcripts,
vector<shared_ptr<Abundance> >& mapped_transcripts,
vector<MateHit>& nr_alignments,
vector<double>& log_conv_factors)
{
int N = transcripts.size();
int M = alignments.size();
// If there's a lot of transcripts, just use the old, overlap constrained
// version of the equivalence collapse.
if (N > 24)
{
collapse_equivalent_hits(alignments,
transcripts,
mapped_transcripts,
nr_alignments,
log_conv_factors,
true);
return;
}
vector<vector<const MateHit*> > compat_table(1 << N);
vector<vector<char> > compatibilities(N, vector<char>(M,0));
compute_compatibilities(transcripts, alignments, compatibilities);
for(int i = 0; i < M; ++i)
{
size_t compat_mask = 0;
for (int j = 0; j < N; ++j)
{
compat_mask |= ((compatibilities[j][i] !=0) << j);
}
assert (compat_mask < compat_table.size());
compat_table[compat_mask].push_back(&(alignments[i]));
}
for (size_t i = 0; i < compat_table.size(); ++i)
{
vector<MateHit> tmp_hits;
vector<MateHit> tmp_nr_hits;
vector<double> tmp_log_conv_factors;
vector<shared_ptr<Abundance> > tmp_mapped_transcripts;
for (size_t j = 0; j < compat_table[i].size(); ++j)
{
tmp_hits.push_back(*(compat_table[i][j]));
}
if (tmp_hits.empty())
continue;
collapse_equivalent_hits(tmp_hits,
transcripts,
tmp_mapped_transcripts,
tmp_nr_hits,
tmp_log_conv_factors,
false);
copy(tmp_nr_hits.begin(), tmp_nr_hits.end(), back_inserter(nr_alignments));
copy(tmp_log_conv_factors.begin(), tmp_log_conv_factors.end(), back_inserter(log_conv_factors));
}
}
#define PERFORM_EQUIV_COLLAPSE 1
void AbundanceGroup::calculate_abundance(const vector<MateHit>& alignments)
{
vector<shared_ptr<Abundance> > transcripts;
get_transfrags(transcripts);
vector<shared_ptr<Abundance> > mapped_transcripts; // This collects the transcripts that have alignments mapping to them
vector<MateHit> nr_alignments;
if (cond_prob_collapse)
{
collapse_hits(alignments, nr_alignments);
}
else
{
nr_alignments = alignments;
}
vector<MateHit> non_equiv_alignments;
vector<double> log_conv_factors;
if (cond_prob_collapse)
{
collapse_equivalent_hits_helper(nr_alignments, transcripts, mapped_transcripts, non_equiv_alignments, log_conv_factors);
assert (non_equiv_alignments.size() == log_conv_factors.size());
log_conv_factors = vector<double>(nr_alignments.size(), 0);
nr_alignments.clear();
mapped_transcripts.clear();
compute_cond_probs_and_effective_lengths(non_equiv_alignments, transcripts, mapped_transcripts);
}
else
{
non_equiv_alignments = nr_alignments;
compute_cond_probs_and_effective_lengths(non_equiv_alignments, transcripts, mapped_transcripts);
}
calculate_gammas(non_equiv_alignments, log_conv_factors, transcripts, mapped_transcripts);
//non_equiv_alignments.clear();
//collapse_hits(alignments, nr_alignments);
//This will also compute the transcript level FPKMs
calculate_locus_scaled_mass_and_variance(non_equiv_alignments, transcripts);
calculate_iterated_exp_count_covariance(non_equiv_alignments, transcripts);
// Refresh the variances to match the new gammas computed during iterated
// expectation
calculate_locus_scaled_mass_and_variance(non_equiv_alignments, transcripts);
if(corr_multi && !final_est_run)
{
update_multi_reads(non_equiv_alignments, mapped_transcripts);
}
if (final_est_run) // Only on last estimation run
{
// Calling calculate_FPKM_covariance() also estimates cross-replicate
// count variances
calculate_FPKM_covariance();
// Derive confidence intervals from the FPKM variance/covariance matrix
calculate_conf_intervals();
// Calculate the inter-group relative abundances and variances
calculate_kappas();
}
for (size_t i = 0; i < _abundances.size(); ++i)
{
for (size_t j = 0; j < _abundances.size(); ++j)
{
if (i != j)
{
if (_abundances[i]->transfrag()->contains(*_abundances[j]->transfrag()) &&
Scaffold::compatible(*_abundances[i]->transfrag(),*_abundances[j]->transfrag()))
{
_abundances[j]->status(NUMERIC_LOW_DATA);
}
}
}
}
//fprintf(stderr, "Total calls to get_cond_prob = %d\n", total_cond_prob_calls);
}
void AbundanceGroup::update_multi_reads(const vector<MateHit>& alignments, vector<shared_ptr<Abundance> > transcripts)
{
size_t M = alignments.size();
size_t N = transcripts.size();
if (transcripts.empty())
return;
for (size_t i = 0; i < M; ++i)
{
if (alignments[i].is_multi())
{
double expr = 0.0;
for (size_t j = 0; j < N; ++j)
{
expr += _abundances[j]->cond_probs()->at(i) * _abundances[j]->FPKM() * _abundances[j]->effective_length();
}
alignments[i].read_group_props()->multi_read_table()->add_expr(alignments[i], expr);
}
}
}
long double solve_beta(long double A, long double B, long double C)
{
long double a = -C/B;
long double b = (A + 4*A*C/(B*B) - (4*C/B));
long double c = -A + B - 5*A*A*C/(B*B*B) + 10*A*C/(B*B) - 5*C/B;
long double d = 2*A*A*A*C/(B*B*B*B) - 6*A*A*C/(B*B*B) + 6*A*C/(B*B) - 2*C/B;
complex<long double> q((3*a*c - b*b)/(a*a*9.0));
complex<long double> r((9.0*a*c*b - 27.0*a*a*d - 2.0*b*b*b)/(a*a*a*54.0));
complex<long double> s1 = std::pow((r + std::sqrt(q*q*q + r*r)),complex<long double>(1/3.0));
complex<long double> s2 = std::pow((r - std::sqrt(q*q*q + r*r)),complex<long double>(1/3.0));
complex<long double> R1 = s1 + s2 - complex<long double>(b/(a*3.0));
complex<long double> R2 = -(s1+s2)/complex<long double>(2.0) - complex<long double>(b/(a*3.0)) + (s1-s2) * complex<long double>(0, sqrtl(3.0)/2.0);
complex<long double> R3 = -(s1+s2)/complex<long double>(2.0) - complex<long double>(b/(a*3.0)) - (s1-s2) * complex<long double>(0, sqrtl(3.0)/2.0);
vector<long double> roots;
if (R1.imag() == 0)
roots.push_back(R1.real());
if (R2.imag() == 0)
roots.push_back(R2.real());
if (R3.imag() == 0)
roots.push_back(R3.real());
sort(roots.begin(), roots.end());
if (roots.empty())
return 0;
long double root = roots.back();
return root;
}
// This function takes the point estimate of the number of fragments from
// a transcript, the iterated expection count matrix, and the locus level
// cross replicate variance, and calculates the transcript-level cross-replicate
// count variance
bool estimate_count_variance(long double& variance,
double gamma_t,
double psi_t_count_var,
double X_g,
double V_X_g_t,
double l_t,
double M)
{
if (l_t == 0)
{
return 0;
}
long double A = X_g * gamma_t;
long double B = V_X_g_t;
long double C = psi_t_count_var;
variance = 0.0;
bool numeric_ok = true;
long double dispersion = V_X_g_t - (X_g * gamma_t);
if (psi_t_count_var < 0)
{
//fprintf (stderr, "Warning: psi_t is negative! (psi_t = %lf)\n", psi_t);
psi_t_count_var = 0;
}
assert (psi_t_count_var >= 0);
// we multiply A with the constants here to make things work out
// at the end of the routine when we multiply by the square of those
// constants
long double poisson_variance = A + psi_t_count_var;
long double alpha = 0.0;
long double beta = 0.0;
long double bnb_mean = 0.0;
long double r = 0.0;
if (dispersion < -1 || abs(dispersion) < 1)
{
// default to poisson dispersion
variance = poisson_variance;
}
else // there's some detectable overdispersion here, use mixture of negative binomials
{
if (psi_t_count_var < 1)
{
// default to regular negative binomial case.
variance = V_X_g_t;
}
else
{
r = ceil((A * A) / (B - A));
if (r < 0)
{
numeric_ok = false;
}
// exact cubic
beta = solve_beta(A,B,C);
alpha = 1.0 - (A/(A-B)) * beta;
if (beta <= 2 || alpha <= 1)
{
//printf ("Warning: beta for is %Lg\n", beta);
numeric_ok = false;
variance = V_X_g_t;
}
else
{
bnb_mean = r * beta / (alpha - 1.0);
variance = r * (alpha + r - 1.0) * beta * (alpha + beta - 1);
variance /= (alpha - 2.0) * (alpha - 1.0) * (alpha - 1.0);
}
if (variance < 0)
{
numeric_ok = false;
variance = V_X_g_t;
}
if (variance == 0 && A != 0)
{
variance = poisson_variance;
}
assert (!numeric_ok || variance >= poisson_variance);
assert (!numeric_ok || variance >= V_X_g_t);
if (variance < poisson_variance)
variance = poisson_variance;
if (variance < V_X_g_t)
variance = V_X_g_t;
//assert (abs(FPKM - mean) < 1e-3);
}
}
if (variance < 0)
variance = 0;
variance = ceil(variance);
assert (!numeric_ok || (!isinf(variance) && !isnan(variance)));
assert (!numeric_ok || variance != 0 || A == 0);
return numeric_ok;
}
//bool estimate_group_count_variance(long double& variance,
// const vector<double>& gammas,
// const ublas::matrix<double>& psis,
// double X_g,
// const vector<double>& V_X_gs,
// const vector<double>& ls,
// double M)
//{
// size_t len = gammas.size();
// if (len == 1)
// return estimate_count_variance(variance, gammas.front(), 0.0, X_g, V_X_gs.front(), ls.front(), M);
//
// double total_var = 0.0;
// bool numeric_ok = true;
// for (size_t i = 0; i < len; ++i)
// {
// bool ok = true;
// long double var = 0.0;
// ok = _count_covariance;
// total_var += var;
// }
//
// double cov = 0.0;
//
// for (size_t i = 0; i < len; ++i)
// {
// for (size_t j = 0; j < len; ++j)
// {
// if (ls[i] && ls[j])
// {
// assert(!isnan(psis(i,j)));
// double L = ls[i] * ls[j];
// assert(!isnan(L));
// if (L != 0.0)
// {
// double g = psis(i,j) / L;
// cov += g;
// }
// }
// }
// }
//
// double C = (1000000000.0 / M);
// C *= C;
// cov *= C;
//
// if (cov < 0)
// {
// //fprintf (stderr, "Warning: cov is negative! (cov = %lf)\n", cov);
// cov = 0;
// }
//
// assert (!numeric_ok || cov >= 0.0);
//
// variance = total_var + cov;
// assert (!isinf(variance) && !isnan(variance));
//
// return numeric_ok;
//}
void AbundanceGroup::estimate_count_covariance()
{
vector<double> gammas;
vector<double> ls;
vector<double> V_X_gs;
for (size_t j = 0; j < _abundances.size(); ++j)
{
gammas.push_back(_abundances[j]->gamma());
ls.push_back(_abundances[j]->effective_length());
V_X_gs.push_back(_abundances[j]->mass_variance());
}
_count_covariance = ublas::zero_matrix<double>(_abundances.size(), _abundances.size());
AbundanceStatus group_status = status();
if (group_status == NUMERIC_OK || group_status == NUMERIC_LOW_DATA)
{
// This will compute the transcript level cross-replicate counts
for (size_t j = 0; j < _abundances.size(); ++j)
{
if (_abundances[j]->effective_length() > 0.0 && mass_fraction() > 0)
{
assert (!isnan(_gamma_covariance(j,j)));
long double count_var = 0.0;
bool numerics_ok = estimate_count_variance(count_var,
_abundances[j]->gamma(),
_iterated_exp_count_covariance(j,j),
num_fragments(),
_abundances[j]->mass_variance(),
_abundances[j]->effective_length(),
num_fragments()/mass_fraction());
if (numerics_ok == false)
{
_abundances[j]->status(NUMERIC_LOW_DATA);
}
else
{
assert (!isinf(count_var) && !isnan(count_var));
_count_covariance(j,j) = count_var;
}
}
else
{
// nothing to do here, variances and covariances should be zero.
//assert(false);
}
}
if (group_status == NUMERIC_LOW_DATA)
{
// if the entire group is unstable, then set LOWDATA on all members of
// it to reduce false positives in differential expression analysis.
foreach(shared_ptr<Abundance> ab, _abundances)
{
ab->status(NUMERIC_LOW_DATA);
}
}
if (_abundances.size() > 1)
{
for (size_t j = 0; j < _abundances.size(); ++j)
{
double scale_j = 0.0;
double poisson_variance_j = _abundances[j]->num_fragments();
if (poisson_variance_j == 0)
{
scale_j = 0.0;
}
else
{
scale_j = _abundances[j]->mass_variance() / poisson_variance_j;
// if (-scale_j * _iterated_exp_count_covariance(i,j) > _abundances[j]->mass_variance())
// scale_j = -_abundances[j]->mass_variance() / _iterated_exp_count_covariance(i,j);
}
for (size_t i = 0; i < _abundances.size(); ++i)
{
if (i != j)
{
double scale_i = 0.0;
double poisson_variance_i = _abundances[i]->num_fragments();
if (poisson_variance_i == 0)
{
scale_i = 0.0;
}
else
{
scale_i = _abundances[i]->mass_variance() / poisson_variance_i;
}
if (scale_i != 0 && scale_j != 0)
{
double poisson_scale = sqrt(scale_j) * sqrt(scale_i);
double before = _iterated_exp_count_covariance(i,j);
long double scale = poisson_scale;
assert (!isinf(scale) && !isnan(scale));
if (scale < 1.0)
scale = 1.0;
double after = scale * before;
//assert (after <= _abundances[i]->mass_variance() + _abundances[j]->mass_variance());
assert (_iterated_exp_count_covariance(i,j) <= 0);
assert (before >= after);
_count_covariance(i,j) = after;
}
else
{
_count_covariance(i,j) = 0;
}
assert (!isinf(_count_covariance(i,j)) && !isnan(_count_covariance(i,j)));
// TODO: attach per-transcript cross-replicate count variance here?
}
}
}
}
}
else
{
// if we get here, there was an EM or IS failure, and the covariances can't be reliably calculated.
// assert(false);
}
ublas::matrix<double> test = _count_covariance;
double ret = cholesky_factorize(test);
if (ret != 0)
{
//fprintf(stderr, "Warning: total count covariance is not positive definite!\n");
for (size_t j = 0; j < _abundances.size(); ++j)
{
_abundances[j]->status(NUMERIC_FAIL);
}
}
// cerr << "full count: " << endl;
// for (unsigned i = 0; i < _count_covariance.size1 (); ++ i)
// {
// ublas::matrix_row<ublas::matrix<double> > mr (_count_covariance, i);
// cerr << i << " : " << _abundances[i]->num_fragments() << " : ";
// std::cerr << i << " : " << mr << std::endl;
// }
// cerr << "======" << endl;
// cerr << "ITERATED:" << endl;
// cerr <<_iterated_exp_count_covariance << endl;
//
// cerr << "ITERATED:" << endl;
// cerr <<_iterated_exp_count_covariance << endl;
}
void AbundanceGroup::calculate_FPKM_covariance()
{
if (mass_fraction() == 0 || effective_length() == 0)
{
_fpkm_covariance = ublas::zero_matrix<double>(_abundances.size(), _abundances.size());
return;
}
long double M = num_fragments()/mass_fraction();
estimate_count_covariance();
long double total_var = 0.0;
long double total_count_var = 0.0;
long double total_iterated = 0.0;
double dummy_var = 0.0;
double abundance_weighted_length = 0.0;
double total_abundance = 0.0;
for (size_t j = 0; j < _abundances.size(); ++j)
{
abundance_weighted_length += _abundances[j]->effective_length() * _abundances[j]->FPKM();
total_abundance += _abundances[j]->FPKM();
for (size_t i = 0; i < _abundances.size(); ++i)
{
_fpkm_covariance(i,j) = _count_covariance(i,j);
assert (!isinf(_count_covariance(i,j)) && !isnan(_fpkm_covariance(i,j)));
long double length_i = _abundances[i]->effective_length();
long double length_j = _abundances[j]->effective_length();
assert (!isinf(length_i) && !isnan(length_i));
assert (!isinf(length_j) && !isnan(length_j));
if (length_i > 0 && length_j > 0)
{
_fpkm_covariance(i,j) *=
((1000000000.0 / (length_j *M)))*((1000000000.0 / (length_i *M)));
assert (!isinf(_fpkm_covariance(i,j)) && !isnan(_fpkm_covariance(i,j)));
assert (_fpkm_covariance(i,j) <= _fpkm_covariance(i,i)+_fpkm_covariance(j,j));
}
else
{
_fpkm_covariance(i,j) = 0.0;
}
if (i == j)
{
assert (_abundances[i]->FPKM() == 0 || _fpkm_covariance(i,j) > 0 || _abundances[i]->status() != NUMERIC_OK);
_abundances[i]->FPKM_variance(_fpkm_covariance(i,j));
dummy_var += _fpkm_covariance(i,i);
}
else
{
dummy_var += _iterated_exp_count_covariance(i,j) * ((1000000000.0 / (length_j *M)))*((1000000000.0 / (length_i *M)));;
}
total_count_var += _count_covariance(i,j);
total_var += _fpkm_covariance(i,j);
total_iterated += _iterated_exp_count_covariance(i,j);
}
}
_FPKM_variance = total_var;
if (final_est_run && library_type != "transfrags")
{
ublas::matrix<double> test = _fpkm_covariance;
double ret = cholesky_factorize(test);
if (ret != 0 || (_FPKM_variance < 0 && status() == NUMERIC_OK))
{
//fprintf(stderr, "Warning: total count covariance is not positive definite!\n");
for (size_t j = 0; j < _abundances.size(); ++j)
{
_abundances[j]->status(NUMERIC_FAIL);
}
}
assert (FPKM() == 0 || _FPKM_variance > 0 || status() != NUMERIC_OK);
}
assert (!isinf(_FPKM_variance) && !isnan(_FPKM_variance));
}
void AbundanceGroup::calculate_conf_intervals()
{
// We only really ever call this function for primary abundance groups
// (i.e. the transcript groups and read bundles with which we calculate
// transcript MLE expression levels. Genes, TSS groups, etc get broken
// off of primary bundles, so we should not call this function on those
// secondary groups. The group splitting code needs to manage the task
// of splitting up all the variout covariance matrices we're calculating
// here.
if (status() == NUMERIC_OK)
{
// This will compute the transcript level FPKM confidence intervals
for (size_t j = 0; j < _abundances.size(); ++j)
{
if (_abundances[j]->effective_length() > 0.0 && mass_fraction() > 0)
{
assert (!isnan(_gamma_covariance(j,j)));
long double fpkm_var = _abundances[j]->FPKM_variance();
double FPKM_hi = 0.0;
double FPKM_lo = 0.0;
if (_abundances[j]->status() != NUMERIC_FAIL)
{
FPKM_hi = _abundances[j]->FPKM() + 2 * sqrt(fpkm_var);
FPKM_lo = max(0.0, (double)(_abundances[j]->FPKM() - 2 * sqrt(fpkm_var)));
if (!(FPKM_lo <= _abundances[j]->FPKM() && _abundances[j]->FPKM() <= FPKM_hi))
{
//fprintf(stderr, "Error: confidence intervals are illegal! var = %Lg, fpkm = %lg, lo = %lg, hi %lg, status = %d\n", fpkm_var, _abundances[j]->FPKM(), FPKM_lo, FPKM_hi, _abundances[j]->status());
}
assert (FPKM_lo <= _abundances[j]->FPKM() && _abundances[j]->FPKM() <= FPKM_hi);
ConfidenceInterval conf(FPKM_lo, FPKM_hi);
_abundances[j]->FPKM_conf(conf);
//_abundances[j]->FPKM_variance(fpkm_var);
}
else
{
// we shouldn't be able to get here
assert(false);
// TODO: nothing to do here?
}
}
else
{
_abundances[j]->FPKM_conf(ConfidenceInterval(0.0, 0.0));
//_abundances[j]->FPKM_variance(0.0);
}
}
// Now build a confidence interval for the whole abundance group
double group_fpkm = FPKM();
if (group_fpkm > 0.0)
{
double FPKM_hi = FPKM() + 2 * sqrt(FPKM_variance());
double FPKM_lo = max(0.0, FPKM() - 2 * sqrt(FPKM_variance()));
ConfidenceInterval conf(FPKM_lo, FPKM_hi);
FPKM_conf(conf);
}
else
{
_FPKM_variance = 0.0;
ConfidenceInterval conf(0.0, 0.0);
FPKM_conf(conf);
}
}
else
{
double sum_transfrag_FPKM_hi = 0;
double max_fpkm = 0.0;
//double min_fpkm = 1e100;
foreach(shared_ptr<Abundance> pA, _abundances)
{
double FPKM_hi;
double FPKM_lo;
if (pA->effective_length() > 0)
{
double norm_frag_density = 1000000000;
norm_frag_density /= pA->effective_length();
norm_frag_density *= mass_fraction();
double fpkm_high = norm_frag_density;
double var_fpkm = fpkm_high;
FPKM_hi = fpkm_high + 2 * sqrt(var_fpkm);
FPKM_lo = 0.0;
ConfidenceInterval conf(FPKM_lo, FPKM_hi);
assert (FPKM_lo <= pA->FPKM() && pA->FPKM() <= FPKM_hi);
pA->FPKM_conf(conf);
//pA->FPKM_variance(var_fpkm);
max_fpkm = max(sum_transfrag_FPKM_hi, FPKM_hi);
}
else
{
FPKM_hi = 0.0;
FPKM_lo = 0.0;
ConfidenceInterval conf(0.0, 0.0);
pA->FPKM_conf(conf);
//pA->FPKM_variance(0.0);
}
}
// In the case of a numeric failure, the groups error bars need to be
// set such that
FPKM_conf(ConfidenceInterval(0.0, max_fpkm + 2 * sqrt(FPKM_variance())));
}
}
//void AbundanceGroup::calculate_conf_intervals()
//{
// if (status() == NUMERIC_OK)
// {
// // This will compute the transcript level FPKM confidence intervals
// for (size_t j = 0; j < _abundances.size(); ++j)
// {
// //fprintf(stderr, "%s\n", _abundances[j]->description().c_str());
// if (_abundances[j]->effective_length() > 0.0 && mass_fraction() > 0)
// {
// assert (!isnan(_gamma_covariance(j,j)));
//
// long double fpkm_var = 0.0;
// double FPKM_hi = 0.0;
// double FPKM_lo = 0.0;
//
// bool numerics_ok = calculate_fpkm_variance(fpkm_var,
// _abundances[j]->gamma(),
// _iterated_exp_count_covariance(j,j),
// num_fragments(),
// _abundances[j]->mass_variance(),
// _abundances[j]->effective_length(),
// num_fragments()/mass_fraction());
// if (numerics_ok == false)
// {
// _abundances[j]->status(NUMERIC_LOW_DATA);
// }
// else
// {
// double gamma_cov_j = _gamma_covariance(j,j);
// double bootstrap_j = _gamma_bootstrap_covariance(j,j);
// double bootstrap_gamma_delta = abs(bootstrap_j - gamma_cov_j);
// if (bootstrap_gamma_delta > bootstrap_delta_gap * gamma_cov_j && _abundances.size() > 1)
// {
// _abundances[j]->status(NUMERIC_LOW_DATA);
// }
// }
//
//
// if (fpkm_var < 0)
// {
// //fprintf(stderr, "Warning: FPKM variance < 0 (FPKM = %lf, FPKM variance = %Lf\n", _abundances[j]->FPKM(), fpkm_var);
// }
//
// FPKM_hi = _abundances[j]->FPKM() + 2 * sqrt(fpkm_var);
// FPKM_lo = max(0.0, (double)(_abundances[j]->FPKM() - 2 * sqrt(fpkm_var)));
// assert (!numerics_ok || FPKM_lo <= _abundances[j]->FPKM() && _abundances[j]->FPKM() <= FPKM_hi);
// ConfidenceInterval conf(FPKM_lo, FPKM_hi);
// _abundances[j]->FPKM_conf(conf);
// _abundances[j]->FPKM_variance(fpkm_var);
// }
// else
// {
// _abundances[j]->FPKM_conf(ConfidenceInterval(0.0, 0.0));
// _abundances[j]->FPKM_variance(0.0);
// }
// }
//
// double group_fpkm = FPKM();
// if (group_fpkm > 0.0)
// {
// calculate_FPKM_variance();
// double FPKM_hi = FPKM() + 2 * sqrt(FPKM_variance());
// double FPKM_lo = max(0.0, FPKM() - 2 * sqrt(FPKM_variance()));
// ConfidenceInterval conf(FPKM_lo, FPKM_hi);
// FPKM_conf(conf);
// }
// else
// {
// _FPKM_variance = 0.0;
// ConfidenceInterval conf(0.0, 0.0);
// FPKM_conf(conf);
// }
// }
// else
// {
// double sum_transfrag_FPKM_hi = 0;
// double max_fpkm = 0.0;
// //double min_fpkm = 1e100;
// foreach(shared_ptr<Abundance> pA, _abundances)
// {
// double FPKM_hi;
// double FPKM_lo;
// if (pA->effective_length() > 0)
// {
// double norm_frag_density = 1000000000;
// norm_frag_density /= pA->effective_length();
//
// norm_frag_density *= mass_fraction();
// double fpkm_high = norm_frag_density;
//
// double var_fpkm = fpkm_high;
//
// FPKM_hi = fpkm_high + 2 * sqrt(var_fpkm);
// FPKM_lo = 0.0;
// ConfidenceInterval conf(FPKM_lo, FPKM_hi);
// assert (FPKM_lo <= pA->FPKM() && pA->FPKM() <= FPKM_hi);
// pA->FPKM_conf(conf);
// pA->FPKM_variance(var_fpkm);
// max_fpkm = max(sum_transfrag_FPKM_hi, FPKM_hi);
// }
// else
// {
// FPKM_hi = 0.0;
// FPKM_lo = 0.0;
// ConfidenceInterval conf(0.0, 0.0);
// pA->FPKM_conf(conf);
// pA->FPKM_variance(0.0);
// }
//
// }
// calculate_FPKM_variance();
// // In the case of a numeric failure, the groups error bars need to be
// // set such that
// FPKM_conf(ConfidenceInterval(0.0, max_fpkm + 2 * sqrt(FPKM_variance())));
//
// }
//}
//
//void AbundanceGroup::calculate_FPKM_variance()
//{
// if (mass_fraction() == 0 || effective_length() == 0)
// {
// _FPKM_variance = 0.0;
// return;
// }
//
// vector<double> gammas;
// vector<double> ls;
// vector<double> V_X_gs;
//
// for (size_t j = 0; j < _abundances.size(); ++j)
// {
// gammas.push_back(_abundances[j]->gamma());
// ls.push_back(_abundances[j]->effective_length());
// V_X_gs.push_back(_abundances[j]->mass_variance());
// }
//
// if (status() == NUMERIC_OK)
// {
// long double var = 0.0;
// compute_fpkm_group_variance(var,
// gammas,
// _iterated_exp_count_covariance,
// num_fragments(),
// V_X_gs,
// ls,
// num_fragments()/mass_fraction());
// _FPKM_variance = var;
// }
// else
// {
// long double max_var = 0.0;
// for (size_t i = 0; i < _abundances.size(); ++i)
// {
// bool ok = true;
// long double var = 0.0;
// ok = compute_fpkm_variance(var, 1.0, 0.0, num_fragments(), max_mass_variance(), ls[i], num_fragments()/mass_fraction());
// max_var = max(max_var,var);
// }
// _FPKM_variance = max_var;
// assert (_FPKM_variance != 0 || FPKM() == 0);
// }
//
// assert (!isinf(_FPKM_variance) && !isnan(_FPKM_variance));
//}
void AbundanceGroup::compute_cond_probs_and_effective_lengths(const vector<MateHit>& alignments,
vector<shared_ptr<Abundance> >& transcripts,
vector<shared_ptr<Abundance> >& mapped_transcripts)
{
int N = transcripts.size();
int M = alignments.size();
vector<vector<char> > compatibilities(N, vector<char>(M,0));
compute_compatibilities(transcripts, alignments, compatibilities);
for (int j = 0; j < N; ++j)
{
shared_ptr<Scaffold> transfrag = transcripts[j]->transfrag();
vector<double>& cond_probs = *(new vector<double>(M,0));
BiasCorrectionHelper bch(transfrag);
for(int i = 0 ; i < M; ++i)
{
if (compatibilities[j][i]==1)
{
total_cond_prob_calls++;
cond_probs[i] = bch.get_cond_prob(alignments[i]);
}
}
transcripts[j]->effective_length(bch.get_effective_length());
transcripts[j]->cond_probs(&cond_probs);
if (bch.is_mapped())
mapped_transcripts.push_back(transcripts[j]);
}
}
double trace(const ublas::matrix<double>& m)
{
double t = 0.0;
for (size_t i = 0.0; i < m.size1(); ++i)
{
t += m(i,i);
}
return t;
}
// FIXME: This function doesn't really need to copy the transcripts out of
// the cluster. Needs refactoring
bool AbundanceGroup::calculate_gammas(const vector<MateHit>& nr_alignments,
const vector<double>& log_conv_factors,
const vector<shared_ptr<Abundance> >& transcripts,
const vector<shared_ptr<Abundance> >& mapped_transcripts)
{
if (mapped_transcripts.empty())
{
//gammas = vector<double>(transfrags.size(), 0.0);
foreach (shared_ptr<Abundance> ab, _abundances)
{
ab->gamma(0);
}
_gamma_covariance = ublas::zero_matrix<double>(transcripts.size(),
transcripts.size());
_count_covariance = ublas::zero_matrix<double>(transcripts.size(),
transcripts.size());
_iterated_exp_count_covariance = ublas::zero_matrix<double>(transcripts.size(),
transcripts.size());
_fpkm_covariance = ublas::zero_matrix<double>(transcripts.size(),
transcripts.size());
_gamma_bootstrap_covariance = ublas::zero_matrix<double>(transcripts.size(),
transcripts.size());
return true;
}
vector<double> gammas;
verbose_msg( "Calculating initial MLE\n");
AbundanceStatus mle_success = gamma_mle(mapped_transcripts,
nr_alignments,
log_conv_factors,
gammas);
verbose_msg( "Tossing likely garbage isoforms\n");
for (size_t i = 0; i < gammas.size(); ++i)
{
if (isnan(gammas[i]))
{
verbose_msg("Warning: isoform abundance is NaN!\n");
}
}
double locus_mass = 0.0;
for (size_t i = 0; i < nr_alignments.size(); ++i)
{
const MateHit& alignment = nr_alignments[i];
locus_mass += alignment.collapse_mass();
}
vector<shared_ptr<Abundance> > filtered_transcripts = mapped_transcripts;
vector<double> filtered_gammas = gammas;
filter_junk_isoforms(filtered_transcripts, filtered_gammas, mapped_transcripts, locus_mass);
if (filtered_transcripts.empty())
{
//gammas = vector<double>(transfrags.size(), 0.0);
foreach (shared_ptr<Abundance> ab, _abundances)
{
ab->gamma(0);
}
_gamma_covariance = ublas::zero_matrix<double>(transcripts.size(),
transcripts.size());
_count_covariance = ublas::zero_matrix<double>(transcripts.size(),
transcripts.size());
_iterated_exp_count_covariance = ublas::zero_matrix<double>(transcripts.size(),
transcripts.size());
_fpkm_covariance = ublas::zero_matrix<double>(transcripts.size(),
transcripts.size());
_gamma_bootstrap_covariance = ublas::zero_matrix<double>(transcripts.size(),
transcripts.size());
return true;
}
if (filtered_transcripts.size() != mapped_transcripts.size())
{
filtered_gammas.clear();
verbose_msg( "Revising MLE\n");
mle_success = gamma_mle(filtered_transcripts,
nr_alignments,
log_conv_factors,
filtered_gammas);
}
for (size_t i = 0; i < filtered_gammas.size(); ++i)
{
if (isnan(filtered_gammas[i]))
{
verbose_msg("Warning: isoform abundance is NaN!\n");
}
}
size_t N = transcripts.size();
set<shared_ptr<ReadGroupProperties const> > rg_props;
for (size_t i = 0; i < nr_alignments.size(); ++i)
{
rg_props.insert(nr_alignments[i].read_group_props());
}
AbundanceStatus map_success = NUMERIC_OK;
if (final_est_run) // Only on last estimation run.
{
ublas::vector<double> gamma_mle(filtered_gammas.size());
std::copy(filtered_gammas.begin(), filtered_gammas.end(), gamma_mle.begin());
ublas::vector<double> gamma_map_estimate = ublas::zero_vector<double>(filtered_gammas.size());
ublas::matrix<double> gamma_map_covariance = ublas::zero_matrix<double>(N,N);
double cross_replicate_js = 0.0;
ublas::matrix<double> empir_covariance = ublas::zero_matrix<double>(N,N);
}
for (size_t i = 0; i < filtered_gammas.size(); ++i)
{
if (isnan(gammas[i]))
{
verbose_msg( "Warning: isoform abundance is NaN!\n");
map_success = NUMERIC_FAIL;
}
}
// Now we need to fill in zeros for the isoforms we filtered out of the
// MLE/MAP calculation
vector<double> updated_gammas = vector<double>(N, 0.0);
ublas::matrix<double> updated_gamma_cov;
updated_gamma_cov = ublas::zero_matrix<double>(N, N);
ublas::matrix<double> updated_gamma_bootstrap_cov;
updated_gamma_bootstrap_cov = ublas::zero_matrix<double>(N, N);
ublas::matrix<double> updated_count_cov;
updated_count_cov = ublas::zero_matrix<double>(N, N);
ublas::matrix<double> updated_iterated_exp_count_cov;
updated_iterated_exp_count_cov = ublas::zero_matrix<double>(N, N);
ublas::matrix<double> updated_fpkm_cov;
updated_fpkm_cov = ublas::zero_matrix<double>(N, N);
size_t cfs = 0;
shared_ptr<Scaffold> curr_filtered_scaff = filtered_transcripts[cfs]->transfrag();
StructurallyEqualScaffolds se;
vector<size_t> scaff_present(N, N);
for (size_t i = 0; i < N; ++i)
{
shared_ptr<Scaffold> scaff_i = transcripts[i]->transfrag();
if (cfs < filtered_transcripts.size())
{
curr_filtered_scaff = filtered_transcripts[cfs]->transfrag();
if (se(scaff_i, curr_filtered_scaff))
{
scaff_present[i] = cfs;
cfs++;
}
}
}
for (size_t i = 0; i < N; ++i)
{
if (scaff_present[i] != N)
{
// then scaffolds[i] has a non-zero abundance, we need to fill
// that in along with relevant cells from the covariance matrix
updated_gammas[i] = filtered_gammas[scaff_present[i]];
//cerr << updated_gammas[i] << ",";
for (size_t j = 0; j < N; ++j)
{
if (scaff_present[j] != N)
{
updated_gamma_cov(i,j) = _gamma_covariance(scaff_present[i],
scaff_present[j]);
updated_gamma_bootstrap_cov(i,j) = _gamma_bootstrap_covariance(scaff_present[i],
scaff_present[j]);
updated_iterated_exp_count_cov(i,j) = _iterated_exp_count_covariance(scaff_present[i],
scaff_present[j]);
// Should still be empty but let's do these for consistency:
updated_count_cov(i,j) = _count_covariance(scaff_present[i],
scaff_present[j]);
updated_fpkm_cov(i,j) = _fpkm_covariance(scaff_present[i],
scaff_present[j]);
assert (!isinf(updated_gamma_cov(i,j)));
assert (!isnan(updated_gamma_cov(i,j)));
}
}
}
}
//cerr << endl;
AbundanceStatus numeric_status = NUMERIC_OK;
if (mle_success == NUMERIC_LOW_DATA)
{
numeric_status = NUMERIC_LOW_DATA;
}
else if (mle_success == NUMERIC_FAIL)
{
numeric_status = NUMERIC_FAIL;
}
else
{
assert (mle_success == NUMERIC_OK);
if (map_success == NUMERIC_FAIL)
{
numeric_status = NUMERIC_FAIL;
}
else if (map_success == NUMERIC_LOW_DATA)
{
numeric_status = NUMERIC_LOW_DATA;
}
// otherwise, we're cool.
}
// All scaffolds that go in get abundances, but those that get "filtered"
// from the calculation get zeros.
//gammas = updated_gammas;
for (size_t i = 0; i < _abundances.size(); ++i)
{
_abundances[i]->gamma(updated_gammas[i]);
_abundances[i]->status(numeric_status);
}
_gamma_covariance = updated_gamma_cov;
_count_covariance = updated_count_cov;
_iterated_exp_count_covariance = updated_iterated_exp_count_cov;
_gamma_bootstrap_covariance = updated_gamma_bootstrap_cov;
_fpkm_covariance = updated_fpkm_cov;
return (status() == NUMERIC_OK);
}
void AbundanceGroup::calculate_iterated_exp_count_covariance(const vector<MateHit>& nr_alignments,
const vector<shared_ptr<Abundance> >& transcripts)
{
// Now calculate the _iterated_exp_count_covariance matrix via iterated expectation
vector<vector<double> > cond_probs(transcripts.size(), vector<double>());
for(size_t j = 0; j < transcripts.size(); ++j)
{
cond_probs[j]= *(transcripts[j]->cond_probs());
}
vector<double> u(nr_alignments.size());
for (size_t i = 0; i < nr_alignments.size(); ++i)
{
u[i] = nr_alignments[i].collapse_mass();
}
ublas::matrix<double> count_covariance = ublas::zero_matrix<double>(transcripts.size(), transcripts.size());
ublas::vector<double> total_cond_prob = ublas::zero_vector<double>(nr_alignments.size());
for (size_t i = 0; i < nr_alignments.size(); ++i)
{
// the replicate gamma mles might not be available, if one of the
// replicates returned an error, we'll consider all to be unreliable
for (size_t j = 0; j < cond_probs.size(); ++j)
{
if (cond_probs[j][i] > 0)
{
total_cond_prob(i) += transcripts[j]->gamma() * cond_probs[j][i];
assert (!isnan(total_cond_prob(i) && ! isinf(total_cond_prob(i))));
}
}
}
// Compute the marginal conditional probability for each fragment against each isoform
ublas::matrix<double> marg_cond_prob = ublas::zero_matrix<double>(transcripts.size(), nr_alignments.size());
for (size_t i = 0; i < nr_alignments.size(); ++i)
{
// the replicate gamma mles might not be available, if one of the
// replicates returned an error, we'll consider all to be unreliable
for (size_t j = 0; j < cond_probs.size(); ++j)
{
if (total_cond_prob(i))
{
if (cond_probs[j][i] > 0)
{
marg_cond_prob(j,i) = (transcripts[j]->gamma() * cond_probs[j][i])/total_cond_prob(i);
}
}
}
}
double total_var = 0.0;
double num_salient_frags = 0.0;
//double num_unsalient_frags = 0.0;
double num_frags = 0.0;
//iterate over fragments
for (size_t i = 0; i < marg_cond_prob.size2(); ++i)
{
num_frags += u[i];
//cerr << u[i] << endl;
}
ublas::vector<double> expected_counts = ublas::zero_vector<double>(cond_probs.size());
//iterate over fragments
for (size_t i = 0; i < marg_cond_prob.size2(); ++i)
{
// iterate over transcripts
for (size_t j = 0; j < marg_cond_prob.size1(); ++j)
{
double c_j_i = marg_cond_prob(j,i);
expected_counts(j) += u[i] * marg_cond_prob(j,i);
if (c_j_i == 0 || c_j_i == 1.0)
continue;
for (size_t k = 0; k < marg_cond_prob.size1(); ++k)
{
double c_k_i = marg_cond_prob(k,i);
if (c_k_i == 0 || c_k_i == 1.0)
continue;
if (j == k)
{
double var = u[i] * c_k_i * (1.0 - c_k_i);
count_covariance(k,k) += var;
assert (var >= 0);
assert (!isnan(var) && !isinf(var));
total_var += var;
}
else
{
double covar = -u[i] * c_k_i * c_j_i;
assert (covar <= 0);
assert (!isnan(covar) && !isinf(covar));
count_covariance(k,j) += covar;
}
}
}
}
double total_counts = accumulate(expected_counts.begin(), expected_counts.end(), 0);
if (total_counts > 0)
{
for (size_t i = 0; i < transcripts.size(); ++i)
{
//_abundances[i]->num_fragments(expected_counts(i));
_abundances[i]->gamma(expected_counts(i) / total_counts);
}
}
_iterated_exp_count_covariance = count_covariance;
// take care of little rounding errors
for (size_t i = 0; i < _iterated_exp_count_covariance.size1(); ++i)
{
for (size_t j = 0; j < _iterated_exp_count_covariance.size2(); ++j)
{
if (i == j)
{
double c = _iterated_exp_count_covariance(i,j);
if (c < 0)
_iterated_exp_count_covariance(i,j) = 0;
//assert(c >= 0);
}
else
{
double c = _iterated_exp_count_covariance(i,j);
if (c > 0)
_iterated_exp_count_covariance(i,j) = 0;
//assert(c <= 0);
}
}
}
}
void AbundanceGroup::calculate_kappas()
{
size_t num_members = _abundances.size();
_kappa_covariance = ublas::matrix<double>(num_members,
num_members);
//cerr << gamma_cov <<endl;
assert (_gamma_covariance.size1() == num_members);
assert (_gamma_covariance.size2() == num_members);
//tss_group.sub_quants = vector<QuantGroup>(isos_in_tss);
double S_FPKM = 0.0;
double Z_kappa = 0.0;
double X_S = 0.0;
foreach (shared_ptr<Abundance> pA, _abundances)
{
if (pA->effective_length() > 0)
{
S_FPKM += pA->FPKM();
Z_kappa += pA->num_fragments() / pA->effective_length();
X_S += pA->num_fragments();
}
}
//fprintf (stderr, "*********\n");
foreach (shared_ptr<Abundance> pA, _abundances)
{
if (S_FPKM > 0)
{
pA->kappa(pA->FPKM() / S_FPKM);
double kappa = pA->kappa();
//fprintf (stderr, "kappa = %lg\n", kappa);
//if (kappa < 0.05)
// pA->status(NUMERIC_LOW_DATA);
}
else
{
pA->kappa(0);
}
}
for (size_t k = 0; k < num_members; ++k)
{
for (size_t m = 0; m < num_members; ++m)
{
double L = _abundances[k]->effective_length() *
_abundances[m]->effective_length();
if (L == 0.0)
{
_kappa_covariance(k,m) = 0.0;
}
else if (m == k)
{
// Use the modeled count variance here instead
double l_t = _abundances[k]->effective_length();
double M = num_fragments()/mass_fraction();
double den = (1000000000.0 / (l_t * M));
double counts = num_fragments();
//double count_var2 = _abundances[k]->FPKM_variance() / (den*den);
double count_var = _count_covariance(k, m);
double kappa = _abundances[k]->kappa();
//
// double kappa_var = count_var / (L * Z_kappa * Z_kappa);
double kappa_var;
if (S_FPKM)
{
kappa_var = _abundances[k]->FPKM_variance() / (S_FPKM * S_FPKM);
}
else
{
kappa_var = 0.0;
}
assert (!isnan(kappa_var) && !isinf(kappa_var));
_kappa_covariance(k,m) = kappa_var;
}
else
{
double kappa_covar;
if (S_FPKM)
{
kappa_covar = _fpkm_covariance(k,m) / (S_FPKM * S_FPKM);
}
else
{
kappa_covar = 0.0;
}
_kappa_covariance(k,m) = kappa_covar;
}
}
}
}
void get_alignments_from_scaffolds(const vector<shared_ptr<Abundance> >& abundances,
vector<MateHit>& alignments)
{
set<const MateHit*> hits_in_gene_set;
foreach(shared_ptr<Abundance> pA, abundances)
{
shared_ptr<Scaffold> pS = pA->transfrag();
assert (pS);
hits_in_gene_set.insert(pS->mate_hits().begin(),
pS->mate_hits().end());
}
for(set<const MateHit*>::iterator itr = hits_in_gene_set.begin();
itr != hits_in_gene_set.end();
++itr)
{
alignments.push_back(**itr);
}
sort(alignments.begin(), alignments.end(), mate_hit_lt);
}
void round(vector<double> & p) {
double KILLP = 0; // kill all probabilities below this
for (vector<double>::iterator i = p.begin(); i != p.end(); ++i) {
if ((*i) < KILLP)
*i = 0;
}
}
void Estep (int N,
int M,
vector<double> const & p,
vector<vector<double> >& U,
const vector<vector<double> >& cond_probs,
const vector<double>& u) {
// given p, fills U with expected frequencies
int i,j;
vector<double> frag_prob_sums(M, 0.0);
for (j = 0; j < N; ++j)
{
for (i = 0; i < M; ++i)
{
frag_prob_sums [i] += cond_probs[j][i] * p[j];
}
}
for (i = 0; i < M; ++i)
{
frag_prob_sums[i] = frag_prob_sums[i] ? (1.0 / frag_prob_sums[i]) : 0.0;
}
for (j = 0; j < N; ++j)
{
for (i = 0; i < M; ++i)
{
double ProbY = frag_prob_sums[i];
double exp_i_j = u[i] * cond_probs[j][i] * p[j] * ProbY;
U[j][i] = exp_i_j;
}
}
}
void Mstep (int N, int M, vector<double> & p, vector<vector<double> > const & U) {
vector<double> v(N,0);
double m = 0;
int i,j;
//#pragma omp parallel for
for (j = 0; j < N; ++j) {
//cout << "." << v[j] << ".\n";
for (i = 0; i < M; ++i) {
// cout << U[i][j] << " \n";
v[j] += U[j][i];
}
m += v[j];
}
if (m)
{
for (j = 0; j < N; ++j) {
p[j] = v[j] / m;
}
}
else
{
for (j = 0; j < N; ++j)
{
p[j] = 0.0;
}
}
}
double logLike (int N,
int M,
vector<double> & p,
const vector<vector<double> >& cond_prob,
const vector<double>& u,
const vector<double>& log_conv_factors) {
int i,j;
double ell = accumulate(log_conv_factors.begin(), log_conv_factors.end(), 0.0);
double Prob_Y;
for (i= 0; i < M; i++) {
Prob_Y = 0;
for (j= 0; j < N; j++) {
Prob_Y += cond_prob[j][i] * p[j];
}
if (Prob_Y > 0) {
ell += (u[i] * log(Prob_Y));
}
}
return ell;
}
void grad_ascent_step (int N,
int M,
vector<double> const & p,
vector<vector<double> >& U,
const vector<vector<double> >& cond_probs,
const vector<double>& u,
vector<double>& newP,
double& epsilon)
{
// given p, fills U with expected frequencies
//int i,j;
vector<double> dLL_dj(N, 0.0);
for (size_t i = 0; i < M; ++i)
{
double denom = 0.0;
for (size_t j = 0; j < N; ++j)
{
denom += p[j] * cond_probs[j][i];
}
for (size_t j = 0; j < N; ++j)
{
if (denom > 0)
{
dLL_dj[j] += u[i] * cond_probs[j][i] / denom;
}
}
}
for (size_t j = 0; j < N; ++j)
{
newP[j] = p[j] + epsilon * dLL_dj[j];
}
double m = accumulate(newP.begin(), newP.end(), 0.0);
if (m > 0)
{
for (int j = 0; j < N; ++j) {
newP[j] = newP[j] / m;
}
}
else
{
return;
}
}
double grad_ascent (int N, int M, vector<double> & newP,
const vector<vector<double> >& cond_prob,
vector<double> const & u,
vector<double> const & log_conv_factors,
bool& converged)
{
converged = true;
double sum = 0;
double newEll = 0;
vector<double> p(N,0);
vector<vector<double> > U(N, vector<double>(M,0));
double ell = 0;
int iter = 0;
int j;
for (j = 0; j < N; ++j) {
p[j] = drand48();
sum += p[j];
}
for (j = 0; j < N; ++j) {
p[j] = p[j] / sum;
}
ell = logLike(N, M, p, cond_prob, u, log_conv_factors);
double epsilon = 1e-5;
static const double ACCURACY = 1e-6; // convergence criteria
while (iter <= 2 || iter < max_mle_iterations)
{
grad_ascent_step(N, M, p, U, cond_prob, u, newP, epsilon);
newEll = logLike(N, M, newP, cond_prob,u, log_conv_factors);
double delta = newEll - ell;
//fprintf (stderr, "%g\n", delta);
if (delta > 0)
{
//round(newP);
p = newP;
ell = newEll;
if (abs(delta) < ACCURACY)
{
break;
}
}
else
{
//verbose_msg("Reducing EPSILON \n");
epsilon /= 10;
}
iter++;
}
if (iter == max_mle_iterations)
{
verbose_msg("Warning: ITERMAX reached in abundance estimation, estimation hasn't fully converged\n");
converged = false;
}
verbose_msg("Convergence reached in %d iterations \n", iter);
return newEll;
}
double EM (int N, int M, vector<double> & newP,
const vector<vector<double> >& cond_prob,
vector<double> const & u,
vector<double> const & log_conv_factors,
bool& converged,
vector<double>* p_hint)
{
converged = true;
//double sum = 0;
double newEll = 0;
vector<double> p(N,0);
vector<vector<double> > U(N, vector<double>(M,0));
double ell = 0;
int iter = 0;
int j;
if (p_hint == NULL)
{
for (j = 0; j < N; ++j) {
//p[j] = drand48();
//sum += p[j];
p[j] = 1.0/(double)N;
}
}
else
{
assert (p_hint->size() == N);
p = *p_hint;
}
// for (j = 0; j < N; ++j) {
// p[j] = p[j] / sum;
// }
//#ifdef DEBUG
// for (j = 0; j < N; ++j) {
// cout << p[j] << " ";
// }
// cout << endl;
//#endif
// static const double ACCURACY = 1e-6; // convergence for EM
static const double ACCURACY = mle_accuracy; // convergence for EM
while (((iter <= 2) || (abs(ell - newEll) > ACCURACY)) && (iter < max_mle_iterations)) {
if (iter > 0) {
round(newP);
p = newP;
ell = newEll;
}
Estep(N, M, p, U, cond_prob, u); // fills U
Mstep(N, M, newP,U); // fills p
newEll = logLike(N, M, newP, cond_prob,u, log_conv_factors);
//fprintf(stderr, "%d\t%lf\n", iter, newEll);
//printf("%.3f %.3f %.3f ", newP[0], newP[1], newP[2]);
//printf("%.3f %.3f %.3f ", newP[3], newP[4], newP[5]);
//printf("%.3f %.3f %.3f\n", newP[6], newP[7], newP[8]);
iter++;
}
if (iter >= max_mle_iterations)
{
verbose_msg("Warning: ITERMAX reached in abundance estimation, estimation hasn't fully converged\n");
converged = false;
}
verbose_msg("Convergence reached in %d iterations \n", iter);
return newEll;
}
void compute_fisher(const vector<shared_ptr<Abundance> >& transcripts,
const ublas::vector<double>& abundances,
const vector<MateHit>& alignments,
const vector<double>& u,
boost::numeric::ublas::matrix<double>& fisher)
{
int M = alignments.size();
int N = transcripts.size();
vector<long double> denoms(M, 0.0);
vector<vector<long double> > P(M,vector<long double>(N,0));
for (int j = 0; j < N; ++j)
{
const vector<double>& cond_probs_j = *(transcripts[j]->cond_probs());
for (int x = 0; x < M; ++x)
{
if (cond_probs_j[x]==0)
continue;
long double alpha = 0.0;
alpha = cond_probs_j[x];
alpha *= abundances(j);
denoms[x] += alpha;
}
}
for (int x = 0; x < M; ++x)
denoms[x] *= denoms[x];
for (int j = 0; j < N; ++j)
{
const vector<double>& cond_probs_j = *(transcripts[j]->cond_probs());
for (int k = 0; k < N; ++k)
{
const vector<double>& cond_probs_k = *(transcripts[k]->cond_probs());
for (int x = 0; x < M; ++x)
{
if (cond_probs_j[x]==0 && cond_probs_k[x]==0)
continue;
assert(denoms[x] != 0.0);
double fisher_x_j_k = cond_probs_j[x] * cond_probs_k[x] / denoms[x];
fisher(j,k) += u[x] * fisher_x_j_k;
}
}
}
}
void compute_sample_weights(const ublas::matrix<double>& proposed_cov,
const vector<vector<double> >& cond_probs,
const vector<ublas::vector<double> >& samples,
const vector<double>& u,
const vector<double>& log_conv_factors,
double scale,
const ublas::vector<double>& MLE,
vector<ublas::vector<double> >& weighted_samples,
vector<pair<size_t, double> >& sample_weights)
{
if (cond_probs.empty())
return;
int M = cond_probs.front().size();
int N = cond_probs.size();
//cerr << "Cov^-1"<<inv_cov << endl;
for (size_t i = 0; i < samples.size(); ++i)
{
vector<double> sample(samples[i].begin(), samples[i].end());
//cerr << "s: "<<samples[i] << endl;
double ell = logLike(N,
M,
sample,
cond_probs,
u,
log_conv_factors);
ublas::vector<double> diff = (samples[i] - MLE);
//cerr << "diff: "<<diff << endl;
ublas::vector<double> diff_transpose = ublas::trans(diff);
//cerr << "diff^T" << diff_transpose << endl;
ublas::vector<double> P = prod(proposed_cov, diff);
//cerr << "Prod: "<< P << endl;
double X = inner_prod(diff_transpose,P);
//cerr << diff_transpose << " "<< P << " " << X << endl;
double sample_prob = exp(-0.5 * X) / scale;
if (sample_prob == 0.0)
{
// fprintf(stderr, "Error: sample_prob == 0, %lf after rounding. \n", X);
// cerr << "diff: "<<diff << endl;//cerr << covariance << endl;
// cerr << "Prod: "<< P << endl;
// cerr << "s: "<<samples[i] << endl;
// return false;
continue; // prob is zero after rounding, skip this sample
}
assert (sample_prob);
assert (!isinf(sample_prob));
assert (!isnan(sample_prob));
//cerr << "Prob(sample) = " << sample_prob << endl;
double log_weight;
if (sample_prob == 0)
{
continue;
}
else
{
//assert (sample_prob > 0.0 && sample_prob <= 1.0);
//sample_prob *= scale;
double e_p = ell - log(sample_prob);
log_weight = e_p;
}
ublas::vector<double> scaled_sample(N);
for (size_t v = 0; v < scaled_sample.size(); ++v)
{
assert (samples[i][v]);
scaled_sample(v) = log_weight + log(samples[i][v]);
assert (scaled_sample(v));
assert (!isinf(scaled_sample(v)));
assert (!isnan(scaled_sample(v)));
}
//cerr << scaled_sample << endl;
weighted_samples.push_back(scaled_sample);
sample_weights.push_back(make_pair(i, log_weight));
}
}
AbundanceStatus compute_posterior_expectation(const vector<ublas::vector<double> >& weighted_samples,
const vector<pair<size_t, double> >& sample_weights,
ublas::vector<double>& expectation,
long double& log_total_weight)
{
ublas::vector<double> log_expectation(expectation.size());
log_total_weight = 0.0;
// compute the weighted sum of the samples from the proposal distribution,
// but keep the result in log space to avoid underflow.
for (size_t i = 0; i < weighted_samples.size(); ++i)
{
const ublas::vector<double>& scaled_sample = weighted_samples[i];
double log_weight = sample_weights[i].second;
if (log_total_weight == 0.0)
{
log_expectation = weighted_samples[i];
log_total_weight = log_weight;
}
else
{
for (size_t e = 0; e < log_expectation.size(); ++e)
{
log_expectation(e) = log_space_add<long double>(log_expectation[e], scaled_sample[e]);
}
log_total_weight = log_space_add<long double>(log_total_weight, log_weight);
}
}
if (log_total_weight == 0 || sample_weights.size() < 100)
{
verbose_msg("Warning: restimation failed, importance samples have zero weight.\n\tResorting to MLE and observed Fisher\n");
return NUMERIC_FAIL;
}
// compute the weighted mean, and transform back out of log space
for (size_t e = 0; e < expectation.size(); ++e)
{
expectation(e) = (long double)log_expectation(e) - log_total_weight;
expectation(e) = exp(expectation(e));
}
for (size_t e = 0; e < expectation.size(); ++e)
{
if (isinf(expectation(e)) || isnan(expectation(e)))
{
verbose_msg("Warning: isoform abundance is NaN, restimation failed.\n\tResorting to MLE and observed Fisher.");
return NUMERIC_FAIL;
}
}
for (size_t j = 0; j < expectation.size(); ++j)
{
if (expectation(j) < 0)
expectation(j) = 0;
}
long double m = sum(expectation);
if (m == 0 || isinf(m) || isnan(m))
{
verbose_msg("Warning: restimation failed, could not renormalize MAP estimate\n\tResorting to MLE and observed Fisher.");
return NUMERIC_FAIL;
}
for (size_t j = 0; j < expectation.size(); ++j) {
expectation(j) = expectation(j) / m;
}
return NUMERIC_OK;
}
AbundanceStatus empirical_mean_replicate_gamma_mle(const vector<shared_ptr<Abundance> >& transcripts,
const vector<MateHit>& nr_alignments,
const vector<double>& log_conv_factors,
ublas::vector<double>& gamma_map_estimate,
ublas::matrix<double>& gamma_covariance,
std::map<shared_ptr<ReadGroupProperties const >, ublas::vector<double> >& mles_for_read_groups)
{
size_t N = transcripts.size();
size_t M = nr_alignments.size();
set<shared_ptr<ReadGroupProperties const> > rg_props;
std::vector<ublas::vector<double> > mle_gammas;
for (size_t i = 0; i < M; ++i)
{
rg_props.insert(nr_alignments[i].read_group_props());
}
vector<double> rep_hit_counts;
for(set<shared_ptr<ReadGroupProperties const> >::iterator itr = rg_props.begin();
itr != rg_props.end();
++itr)
{
vector<MateHit> rep_hits;
vector<double> rep_log_conv_factors;
rep_hit_counts.push_back(0);
for (size_t i = 0; i < M; ++i)
{
rep_hits.push_back(nr_alignments[i]);
rep_log_conv_factors.push_back(log_conv_factors[i]);
if (nr_alignments[i].read_group_props() != *itr)
{
rep_hits.back().collapse_mass(0);
rep_log_conv_factors[rep_log_conv_factors.size() - 1] = 0;
}
rep_hit_counts[rep_hit_counts.size() - 1] += rep_hits.back().collapse_mass();
}
//fprintf(stderr,"Replicate # %lu has %lu fragments \n", mle_gammas.size(), rep_hits.size());
vector<double> rep_gammas(0.0, transcripts.size());
AbundanceStatus mle_success = gamma_mle(transcripts,
rep_hits,
rep_log_conv_factors,
rep_gammas);
if (mle_success == NUMERIC_OK)
{
ublas::vector<double> mle = ublas::zero_vector<double>(N);
for(size_t i = 0; i < N; ++i)
{
mle(i) = rep_gammas[i];
}
cerr << mle << endl;
mle_gammas.push_back(mle);
mles_for_read_groups[*itr] = mle;
}
else
{
// if one replicate fails, let's just not trust any of them
mles_for_read_groups.clear();
return mle_success;
}
}
// cerr << "***" << endl;
gamma_covariance = ublas::zero_matrix<double>(N,N);
ublas::vector<double> expected_mle_gamma = ublas::zero_vector<double>(N);
//
foreach(ublas::vector<double>& mle, mle_gammas)
{
expected_mle_gamma += mle;
}
expected_mle_gamma /= mle_gammas.size();
//
// ublas::vector<double> expected_counts = ublas::zero_vector<double>(N);
//
// for (size_t i = 0; i < mle_gammas.size(); ++i)
// {
// ublas::vector<double>& mle = mle_gammas[i];
// expected_counts += mle * rep_hit_counts[i];
// }
// expected_counts /= mle_gammas.size();
//
for (size_t i = 0; i < N; ++i)
{
for (size_t j = 0; j < N; ++j)
{
for (size_t k = 0 ; k < mle_gammas.size(); ++k)
{
double c = (mle_gammas[k](i) - expected_mle_gamma(i)) * (mle_gammas[k](j) - expected_mle_gamma(j));
gamma_covariance(i,j) += c;
}
}
}
gamma_covariance /= mle_gammas.size();
//
// ublas::matrix<double> count_covariance = ublas::zero_matrix<double>(N,N);
// for (size_t k = 0 ; k < mle_gammas.size(); ++k)
// {
// ublas::vector<double>& mle = mle_gammas[k];
// ublas::vector<double> counts = mle * rep_hit_counts[k];
//
// for (size_t i = 0; i < N; ++i)
// {
// for (size_t j = 0; j < N; ++j)
// {
// double c = (counts(i) - expected_counts(i)) * (counts(j) - expected_counts(j));
// count_covariance(i,j) += c;
// }
// }
// }
//
// count_covariance /= mle_gammas.size();
// cerr << "count mean: " << endl;
// cerr << expected_counts << endl;
// cerr << "count covariance: " << endl;
// for (unsigned i = 0; i < count_covariance.size1 (); ++ i)
// {
// ublas::matrix_row<ublas::matrix<double> > mr (count_covariance, i);
// std::cerr << i << " : " << mr << std::endl;
// }
// cerr << "======" << endl;
gamma_map_estimate = expected_mle_gamma;
// cerr << "MLE: " << expected_mle_gamma << endl;
// cerr << "COV:" << endl;
// cerr << gamma_covariance << endl;
//cerr << "*************" << endl;
return NUMERIC_OK;
}
AbundanceStatus calculate_inverse_fisher(const vector<shared_ptr<Abundance> >& transcripts,
const vector<MateHit>& alignments,
const ublas::vector<double>& gamma_mean,
ublas::matrix<double>& inverse_fisher)
{
// size_t N = gamma_covariance.size1();
// gamma_map_covariance = ublas::zero_matrix<double>(N);
typedef ublas::matrix<double> matrix_type;
matrix_type fisher = ublas::zero_matrix<double>(gamma_mean.size(),gamma_mean.size());
vector<double> u(alignments.size());
for (size_t i = 0; i < alignments.size(); ++i)
{
u[i] = alignments[i].collapse_mass();
}
compute_fisher(transcripts,
gamma_mean,
alignments,
u,
fisher);
ublas::matrix<double> epsilon = ublas::zero_matrix<double>(gamma_mean.size(),gamma_mean.size());
for (size_t i = 0; i < gamma_mean.size(); ++i)
{
epsilon(i,i) = 1e-6;
}
fisher += epsilon; // modify matrix to avoid problems during inverse
ublas::matrix<double> fisher_chol = fisher;
double ch = cholesky_factorize(fisher_chol);
if (ch != 0.0)
{
verbose_msg("Warning: Fisher matrix is not positive definite (bad element: %lg)\n", ch);
return NUMERIC_FAIL;
}
inverse_fisher = ublas::zero_matrix<double>(gamma_mean.size(),gamma_mean.size());
bool invertible = chol_invert_matrix(fisher_chol, inverse_fisher);
ublas::matrix<double> test_fisher = inverse_fisher;
ch = cholesky_factorize(test_fisher);
if (ch != 0.0 || !invertible)
{
verbose_msg("Warning: Fisher matrix is not inverible\n", ch);
return NUMERIC_FAIL;
}
return NUMERIC_OK;
}
AbundanceStatus bayesian_gammas(const vector<shared_ptr<Abundance> >& transcripts,
const vector<MateHit>& alignments,
const vector<double>& log_conv_factors,
const ublas::vector<double>& gamma_mle,
ublas::vector<double>& gamma_map_estimate,
ublas::matrix<double>& gamma_map_covariance)
{
ublas::matrix<double> inverse_fisher;
// Calculate the mean gamma MLE and covariance matrix across replicates, so
// we can use it as the proposal distribution for importance sampling. This will
// make the Bayesian prior more conservative than using the inverse of the
// Fisher Information matrix on the mixed likelihood function.
AbundanceStatus fisher_status = calculate_inverse_fisher(transcripts,
alignments,
gamma_mle,
inverse_fisher);
double trace = 0.0;
for (size_t i = 0; i < gamma_mle.size(); ++i)
{
trace += inverse_fisher(i,i);
}
ublas::matrix<double> proposal = inverse_fisher;
#if 1
proposal += ublas::identity_matrix<double>(gamma_mle.size()) * (trace / 10.0);
proposal *= 10.0;
#endif
if (fisher_status != NUMERIC_OK)
return fisher_status;
AbundanceStatus map_status = map_estimation(transcripts,
alignments,
log_conv_factors,
gamma_mle,
proposal,
gamma_map_estimate,
gamma_map_covariance);
return map_status;
}
AbundanceStatus bayesian_gammas_exact(const vector<shared_ptr<Abundance> >& transcripts,
const vector<MateHit>& nr_alignments,
const vector<double>& log_conv_factors,
const ublas::vector<double>& gamma_mle,
ublas::vector<double>& gamma_map_estimate,
ublas::matrix<double>& gamma_map_covariance)
{
ublas::matrix<double> inverse_fisher;
// Calculate the mean gamma MLE and covariance matrix across replicates, so
// we can use it as the proposal distribution for importance sampling. This will
// make the Bayesian prior more conservative than using the inverse of the
// Fisher Information matrix on the mixed likelihood function.
AbundanceStatus fisher_status = calculate_inverse_fisher(transcripts,
nr_alignments,
gamma_mle,
inverse_fisher);
double trace = 0.0;
for (size_t i = 0; i < gamma_mle.size(); ++i)
{
trace += inverse_fisher(i,i);
}
ublas::matrix<double> proposal = inverse_fisher;
#if 1
proposal += ublas::identity_matrix<double>(gamma_mle.size()) * (trace / 10.0);
proposal *= 4.0;
#endif
if (fisher_status != NUMERIC_OK)
return fisher_status;
AbundanceStatus map_status = map_estimation(transcripts,
nr_alignments,
log_conv_factors,
gamma_mle,
proposal,
gamma_map_estimate,
gamma_map_covariance);
return map_status;
}
AbundanceStatus bootstrap_gamma_mle(const vector<shared_ptr<Abundance> >& transcripts,
const vector<MateHit>& nr_alignments,
const vector<double>& log_conv_factors,
ublas::vector<double>& gamma_map_estimate,
ublas::matrix<double>& gamma_covariance,
double& cross_replicate_js)
{
size_t N = transcripts.size();
size_t M = nr_alignments.size();
if (N == 1)
{
gamma_map_estimate = ublas::vector<double>(1);
gamma_map_estimate(0) = 1.0;
gamma_covariance = ublas::matrix<double>(1,1);
gamma_covariance(0,0) = 0.0;
return NUMERIC_OK;
}
vector<MateHit> alignments = nr_alignments;
vector<double> scaled_masses;
vector<double> unscaled_masses;
double num_uncollapsed_frags = 0.0;
for (size_t i = 0; i < M; ++i)
{
double uncollapsed_mass = alignments[i].collapse_mass() / alignments[i].common_scale_mass();
num_uncollapsed_frags += (uncollapsed_mass);
scaled_masses.push_back(alignments[i].collapse_mass());
unscaled_masses.push_back(uncollapsed_mass);
alignments[i].collapse_mass(uncollapsed_mass);
}
// FIXME: this has already been computed above, so just pass it in.
vector<double> orig_gammas(0.0, transcripts.size());
gamma_mle(transcripts,
nr_alignments,
log_conv_factors,
orig_gammas,
false);
std::vector<ublas::vector<double> > mle_gammas;
boost::uniform_int<> uniform_dist(0,num_uncollapsed_frags-1);
boost::mt19937 rng;
boost::variate_generator<boost::mt19937&, boost::uniform_int<> > uniform_gen(rng, uniform_dist);
int num_sample_frags = floor(num_uncollapsed_frags * bootstrap_fraction);
if (num_sample_frags <= 0)
{
return NUMERIC_FAIL;
}
for (size_t i = 0; i < num_bootstrap_samples; ++i)
{
vector<int> sample_idxs;
for (size_t j = 0; j < num_sample_frags; ++j)
{
sample_idxs.push_back(uniform_gen());
}
sort (sample_idxs.begin(), sample_idxs.end());
assert (sample_idxs.empty() == false);
size_t curr_sample = 0;
size_t processed_hits = 0;
vector<double> adjusted_masses(alignments.size(), 0);
for (size_t j = 0; j < alignments.size(); ++j)
{
int adjusted_mass = 0.0;
while (curr_sample < sample_idxs.size() &&
sample_idxs[curr_sample] >= processed_hits &&
sample_idxs[curr_sample] < processed_hits + alignments[j].collapse_mass())
{
adjusted_mass++;
curr_sample++;
}
processed_hits += alignments[j].collapse_mass();
alignments[j].collapse_mass(adjusted_mass);
adjusted_masses[j] = adjusted_mass;
}
for (size_t j = 0; j < alignments.size(); ++j)
{
alignments[j].collapse_mass(alignments[j].collapse_mass() * alignments[j].common_scale_mass());
}
vector<double> bs_gammas(0.0, transcripts.size());
AbundanceStatus mle_success = gamma_mle(transcripts,
alignments,
log_conv_factors,
bs_gammas,
false,
&orig_gammas);
if (mle_success == NUMERIC_OK)
{
ublas::vector<double> mle = ublas::zero_vector<double>(N);
for(size_t j = 0; j < N; ++j)
{
mle(j) = bs_gammas[j];
}
mle_gammas.push_back(mle);
}
for (size_t j = 0; j < alignments.size(); ++j)
{
alignments[j].collapse_mass(unscaled_masses[j]);
}
}
//fprintf(stderr, "Ran %lu bootstrap samples succesfully\n", mle_gammas.size());
if (mle_gammas.empty())
return NUMERIC_FAIL;
gamma_covariance = ublas::zero_matrix<double>(N,N);
ublas::vector<double> expected_mle_gamma = ublas::zero_vector<double>(N);
foreach(ublas::vector<double>& mle, mle_gammas)
{
//cerr << "MLE # "<< MLENUM++ << endl;
//cerr << mle << endl;
expected_mle_gamma += mle;
}
expected_mle_gamma /= mle_gammas.size();
for (size_t i = 0; i < N; ++i)
{
for (size_t j = 0; j < N; ++j)
{
for (size_t k = 0 ; k < mle_gammas.size(); ++k)
{
double c = (mle_gammas[k](i) - expected_mle_gamma(i)) * (mle_gammas[k](j) - expected_mle_gamma(j));
gamma_covariance(i,j) += c;
}
}
}
gamma_covariance /= mle_gammas.size();
gamma_map_estimate = expected_mle_gamma;
//cerr << "MLE: " << expected_mle_gamma << endl;
//cerr << "COV:" << endl;
//cerr << gamma_covariance << endl;
//cerr << "*************" << endl;
return NUMERIC_OK;
}
AbundanceStatus bootstrap_gammas(const vector<shared_ptr<Abundance> >& transcripts,
const vector<MateHit>& alignments,
const vector<double>& log_conv_factors,
ublas::vector<double>& gamma_estimate,
ublas::matrix<double>& gamma_covariance,
double& cross_replicate_js)
{
ublas::vector<double> empirical_gamma_mle = gamma_estimate;
ublas::matrix<double> empirical_gamma_covariance = gamma_covariance;
// Calculate the mean gamma MLE and covariance matrix across replicates, so
// we can use it as the proposal distribution for importance sampling. This will
// make the Bayesian prior more conservative than using the inverse of the
// Fisher Information matrix on the mixed likelihood function.
AbundanceStatus empirical_mle_status = bootstrap_gamma_mle(transcripts,
alignments,
log_conv_factors,
empirical_gamma_mle,
empirical_gamma_covariance,
cross_replicate_js);
if (empirical_mle_status != NUMERIC_OK)
return empirical_mle_status;
gamma_estimate = empirical_gamma_mle;
gamma_covariance = empirical_gamma_covariance;
return NUMERIC_OK;
}
AbundanceStatus empirical_replicate_gammas(const vector<shared_ptr<Abundance> >& transcripts,
const vector<MateHit>& nr_alignments,
const vector<double>& log_conv_factors,
ublas::vector<double>& gamma_estimate,
ublas::matrix<double>& gamma_covariance,
std::map<shared_ptr<ReadGroupProperties const >, ublas::vector<double> >& mles_for_read_groups)
{
ublas::vector<double> empirical_gamma_mle = gamma_estimate;
ublas::matrix<double> empirical_gamma_covariance = gamma_covariance;
// Calculate the mean gamma MLE and covariance matrix across replicates, so
// we can use it as the proposal distribution for importance sampling. This will
// make the Bayesian prior more conservative than using the inverse of the
// Fisher Information matrix on the mixed likelihood function.
AbundanceStatus empirical_mle_status = empirical_mean_replicate_gamma_mle(transcripts,
nr_alignments,
log_conv_factors,
empirical_gamma_mle,
empirical_gamma_covariance,
mles_for_read_groups);
if (empirical_mle_status != NUMERIC_OK)
return empirical_mle_status;
gamma_estimate = empirical_gamma_mle;
gamma_covariance = empirical_gamma_covariance;
#if 0
// // Perform a bayesian estimation to improve the gamma estimate and their covariances
// ublas::matrix<double> epsilon = ublas::zero_matrix<double>(empirical_gamma_mle.size(),empirical_gamma_mle.size());
// for (size_t i = 0; i < empirical_gamma_mle.size(); ++i)
// {
// epsilon(i,i) = 1e-6;
// }
//
// empirical_gamma_covariance += epsilon;
AbundanceStatus map_status = map_estimation(transcripts,
nr_alignments,
log_conv_factors,
empirical_gamma_mle,
empirical_gamma_covariance,
gamma_estimate,
gamma_covariance);
if (map_status != NUMERIC_OK)
return map_status;
#endif
return NUMERIC_OK;
}
AbundanceStatus revise_map_mean_and_cov_estimate(double log_total_weight,
const ublas::vector<double>& expectation,
const vector<pair<size_t, double> >& sample_weights,
const vector<ublas::vector<double> >& weighted_samples,
ublas::vector<double>& gamma_map_estimate,
ublas::matrix<double>& gamma_map_covariance)
{
int N = expectation.size();
// revise gamma by setting it to the posterior expectation computed via the
// importance sampling
gamma_map_estimate = expectation;
// calculate the sample - mean vectors, store them in log space
vector<ublas::vector<double> > sample_expectation_diffs;
ublas::vector<double> check_expectation = ublas::zero_vector<double>(expectation.size());
for (size_t j = 0; j < weighted_samples.size(); ++j)
{
ublas::vector<double> sample = weighted_samples[j];
double log_sample_weight = sample_weights[j].second;
for (size_t e = 0; e < expectation.size(); ++e)
{
// sample is already log transformed after it was weighted, so we
// need to divide by the sample weight to recover the original sample
// value, then undo the log transform, then subtract the mean from it
sample(e) = (exp(((long double)sample(e) - log_sample_weight)) - expectation(e));
//sample(e) *= exp((log_sample_weight - log_total_weight));
}
//cerr << sample << endl;
sample_expectation_diffs.push_back(sample);
}
// We want to revise the covariance matrix from the samples, since we'll
// need it later for the CIs.
ublas::matrix<double> revised_cov = ublas::zero_matrix<double>(N,N);
// accumulate the contributions from the other samples (doing one cell of
// covariance matrix per outer (i x j) loop iteration.
for (size_t j = 0; j < sample_expectation_diffs.size(); ++j)
{
double log_sample_weight = sample_weights[j].second;
double w = exp((log_sample_weight - log_total_weight));
ublas::vector<double> sample = weighted_samples[j];
for (size_t e = 0; e < expectation.size(); ++e)
{
// sample is already log transformed after it was weighted, so we
// need to divide by the sample weight to recover the original sample
// value, then undo the log transform, then subtract the mean from it
sample(e) = exp(sample(e) - log_sample_weight);
//sample(e) *= exp((log_sample_weight - log_total_weight));
}
revised_cov += w * (outer_prod(sample,sample));
}
revised_cov -= outer_prod(expectation,expectation);
//cerr << "Revised COV" << endl;
//cerr << revised_cov << endl;
gamma_map_covariance = revised_cov;
//cerr << "Revised MAP estimate: " << expectation << endl;
//cerr << "Revised Covariance matrix:" << endl;
//cerr << gamma_map_covariance << endl;
//cerr << "*************" << endl;
return NUMERIC_OK;
}
AbundanceStatus calc_is_scale_factor(const ublas::matrix<double>& covariance_chol,
double& is_scale_factor)
{
double det = determinant(covariance_chol);
is_scale_factor = pow(2.0*boost::math::constants::pi<double>(), covariance_chol.size1()/2.0);
double s = sqrt(det);
is_scale_factor *= s;
//assert (det);
if (s == 0.0)
{
verbose_msg("Error: sqrt(det(cov)) == 0, %lf after rounding. \n", det);
//cerr << covariance << endl;
return NUMERIC_FAIL;
}
assert (s);
assert (is_scale_factor);
return NUMERIC_OK;
}
AbundanceStatus map_estimation(const vector<shared_ptr<Abundance> >& transcripts,
const vector<MateHit>& alignments,
const vector<double>& log_conv_factors,
const ublas::vector<double>& proposal_gamma_mean,
const ublas::matrix<double>& proposal_gamma_covariance,
ublas::vector<double>& gamma_map_estimate,
ublas::matrix<double>& gamma_map_covariance)
{
ublas::matrix<double> covariance_chol = proposal_gamma_covariance;
ublas::matrix<double> inv_cov = covariance_chol;
double ch = cholesky_factorize(covariance_chol);
if (ch != 0.0)
{
verbose_msg("Warning: Covariance matrix is not positive definite (bad element: %lg)\n", ch);
return NUMERIC_FAIL;
}
bool invertible = chol_invert_matrix(covariance_chol, inv_cov);
if (!invertible)
{
verbose_msg("Warning: Covariance matrix is not invertible\n");
return NUMERIC_FAIL;
}
//cerr << "Cholesky decomposed proposal covariance" << endl;
//cerr << covariance_chol << endl;
multinormal_generator<double> generator(proposal_gamma_mean, covariance_chol);
vector<ublas::vector<double> > samples;
generate_importance_samples(generator, samples, num_importance_samples, false);
if (samples.size() < 100)
{
verbose_msg("Warning: not-enough samples for MAP re-estimation\n");
return NUMERIC_FAIL;
}
double is_scale_factor = 0.0;
// Calculate the scaling factor for correcting the proposal distribution bias
// during importance sampling
AbundanceStatus scale_status = calc_is_scale_factor(covariance_chol, is_scale_factor);
if (scale_status == NUMERIC_FAIL)
{
return NUMERIC_FAIL;
}
vector<pair<size_t, double> > sample_weights;
ublas::vector<double> expectation(transcripts.size());
vector<ublas::vector<double> > weighted_samples;
vector<vector<double> > cond_probs(transcripts.size(), vector<double>());
for(size_t j = 0; j < transcripts.size(); ++j)
{
cond_probs[j]= *(transcripts[j]->cond_probs());
}
vector<double> u(alignments.size());
for (size_t i = 0; i < alignments.size(); ++i)
{
u[i] = alignments[i].collapse_mass();
}
compute_sample_weights(proposal_gamma_covariance,
cond_probs,
samples,
u,
log_conv_factors,
is_scale_factor,
proposal_gamma_mean,
weighted_samples,
sample_weights);
long double log_total_weight = 0.0;
AbundanceStatus expectation_ok = compute_posterior_expectation(weighted_samples,
sample_weights,
expectation,
log_total_weight);
if (expectation_ok != NUMERIC_OK)
{
return expectation_ok;
}
revise_map_mean_and_cov_estimate(log_total_weight,
expectation,
sample_weights,
weighted_samples,
gamma_map_estimate,
gamma_map_covariance);
return NUMERIC_OK;
}
template<class M, class PM>
bool is_identifiable(M &m, PM &pm)
{
using namespace ublas;
typedef M matrix_type;
typedef typename M::size_type size_type;
typedef typename M::value_type value_type;
int singular = 0;
size_type size1 = m.size1 ();
size_type size2 = m.size2 ();
size_type size = (std::min) (size1, size2);
for (size_type i = 0; i < size; ++ i) {
matrix_column<M> mci (column (m, i));
matrix_row<M> mri (row (m, i));
size_type i_norm_inf = i + index_norm_inf (project (mci, range (i, size1)));
if (m (i_norm_inf, i) != value_type/*zero*/()) {
if (i_norm_inf != i) {
pm (i) = i_norm_inf;
row (m, i_norm_inf).swap (mri);
} else {
//BOOST_UBLAS_CHECK (pm (i) == i_norm_inf, external_logic ());
}
project (mci, range (i + 1, size1)) *= value_type (1) / m (i, i);
} else if (singular == 0) {
singular = i + 1;
}
project (m, range (i + 1, size1), range (i + 1, size2)).minus_assign (outer_prod (project (mci, range (i + 1, size1)),
project (mri, range (i + 1, size2))));
}
return singular == 0;
}
AbundanceStatus gamma_mle(const vector<shared_ptr<Abundance> >& transcripts,
const vector<MateHit>& nr_alignments,
const vector<double>& log_conv_factors,
vector<double>& gammas,
bool check_identifiability,
vector<double>* p_hint)
{
gammas.clear();
if (transcripts.empty())
return NUMERIC_OK;
//long double bundle_mass_fraction = bundle_mass / (long double) map_mass;
if (transcripts.size() == 1)
{
gammas.push_back(1.0);
return NUMERIC_OK;
}
size_t M = nr_alignments.size();
size_t N = transcripts.size();
bool converged = true;
bool identifiable = true;
if (M > 0)
{
//vector<vector<double> > saliencies (M,vector<double>(N,0));
//compute_saliencies(cond_probs, saliencies, saliency_weight);
vector<double> prob(N,0);
double logL;
vector<vector<double> > cond_probs(N, vector<double>());
for (size_t j = 0; j < N; ++j)
{
cond_probs[j] = *(transcripts[j]->cond_probs());
}
if (check_identifiability)
{
ublas::matrix<double> compat = ublas::zero_matrix<double>(M,N);
for (size_t j = 0; j < N; ++j)
{
for (size_t i = 0; i < M; ++i)
{
if (cond_probs[j][i])
{
//compat(i,j) = cond_probs[j][i];
compat(i,j) = 1;
}
}
}
vector<size_t> transcripts_with_frags;
for (size_t j = 0; j < N; ++j)
{
bool has_fragment = false;
for (size_t i = 0; i < M; ++i)
{
if (compat(i,j))
{
has_fragment = true;
break;
}
}
if (has_fragment)
transcripts_with_frags.push_back(j);
}
ublas::matrix<double> reduced_compat = ublas::zero_matrix<double>(M,transcripts_with_frags.size());
for (size_t j = 0; j < transcripts_with_frags.size(); ++j)
{
column(reduced_compat, j) = column(compat, transcripts_with_frags[j]);
}
typedef ublas::permutation_matrix<std::size_t> pmatrix;
// create a permutation matrix for the LU-factorization
pmatrix pm(reduced_compat.size1());
// cerr << compat.size2() <<endl;
// perform LU-factorization
identifiable = is_identifiable<ublas::matrix<double>,pmatrix>(reduced_compat,pm);
}
vector<double> u(M);
for (size_t i = 0; i < M; ++i)
{
u[i] = nr_alignments[i].collapse_mass();
}
if (use_em)
{
logL = EM(N, M, prob, cond_probs, u, log_conv_factors, converged, p_hint);
}
else
{
logL = grad_ascent(N, M, prob, cond_probs, u, log_conv_factors, converged);
}
gammas = prob;
for (size_t i = 0; i < gammas.size(); ++i)
{
if (isnan(gammas[i]) || isinf(gammas[i]))
{
return NUMERIC_FAIL;
}
}
}
else
{
gammas = vector<double>(N, 0.0);
}
double round_err = 0.0;
double num_good = 0;
foreach (double& g, gammas)
{
if (g < min_isoform_fraction)
{
round_err += g;
g = 0.0;
}
else
{
num_good += 1;
}
}
foreach (double& g, gammas)
{
if (g != 0)
{
g += (round_err/num_good);
}
}
if (converged && identifiable)
return NUMERIC_OK;
else
{
if (!identifiable)
//return NUMERIC_LOW_DATA;
return NUMERIC_OK;
else
return NUMERIC_FAIL;
}
return NUMERIC_OK;
}
void calc_isoform_fpkm_conf_intervals(double FPKM,
double variance,
ConfidenceInterval& FPKM_conf)
{
double FPKM_lo = 0.0;
double FPKM_hi = 0.0;
FPKM_hi = FPKM + 2 * sqrt(variance);
FPKM_lo = max(0.0, FPKM - 2 * sqrt(variance));
FPKM_conf = ConfidenceInterval(FPKM_lo, FPKM_hi);
}
bool not_intronic(int p, vector<float>& depth_of_coverage, vector<float>& intronic_cov, float min_intra_intron_fraction,
int& intronic_status) {
bool not_an_intron = (intronic_cov[p]==0 ||
depth_of_coverage[p]/intronic_cov[p] >= min_intra_intron_fraction);
if (not_an_intron) intronic_status--;
else intronic_status++;
return not_an_intron;
}
double compute_doc(int bundle_origin,
const vector<Scaffold>& scaffolds,
vector<float>& depth_of_coverage,
map<pair<int, int>, float>& intron_depth_of_coverage,
bool exclude_intra_intron,
vector<float>* intronic_cov,
vector<int>* scaff_intronic_status)
{
vector<int> i_status;
if (scaff_intronic_status==NULL)
scaff_intronic_status=&i_status;
*scaff_intronic_status = vector<int>(scaffolds.size(), 0);
vector<float> intronic;
if (intronic_cov==NULL)
intronic_cov=&intronic;
*intronic_cov = vector<float>(depth_of_coverage.size(), 0);
//vector<bool> intronic(depth_of_coverage.size(), false);
depth_of_coverage = vector<float>(depth_of_coverage.size(), 0);
set<const MateHit*> hits_in_gene_set;
for (size_t i = 0; i < scaffolds.size(); ++i)
{
hits_in_gene_set.insert(scaffolds[i].mate_hits().begin(),
scaffolds[i].mate_hits().end());
}
vector<Scaffold> hits;
for(set<const MateHit*>::iterator itr = hits_in_gene_set.begin();
itr != hits_in_gene_set.end();
++itr)
{
hits.push_back(Scaffold(**itr));
hits.back().fpkm((**itr).mass());
}
/*
//no need for this here, we do it below with depth_of_coverage
for (size_t i = 0; i < hits.size(); ++i)
{
const vector<AugmentedCuffOp>& aug_ops = hits[i].augmented_ops();
for (size_t j = 0; j < aug_ops.size(); ++j)
{
const AugmentedCuffOp& op = aug_ops[j];
if (op.opcode == CUFF_INTRON)
{
for (int K = op.g_left(); K < op.g_right(); ++K)
{
intronic[K - bundle_origin] = true;
}
}
}
}
*/
for (size_t i = 0; i < hits.size(); ++i)
{
const vector<AugmentedCuffOp>& aug_ops = hits[i].augmented_ops();
for (size_t j = 0; j < aug_ops.size(); ++j)
{
const AugmentedCuffOp& op = aug_ops[j];
if (op.opcode == CUFF_MATCH)
{
for (int K = op.g_left(); K < op.g_right(); ++K)
{
depth_of_coverage[K - bundle_origin] += hits[i].fpkm();
}
}
else if (op.opcode == CUFF_INTRON)
{
for (int K = op.g_left(); K < op.g_right(); ++K)
{
(*intronic_cov)[K - bundle_origin] += hits[i].fpkm();
//intronic[K - bundle_origin] = true;
}
pair<map<pair<int,int>,float>::iterator, bool> is = intron_depth_of_coverage.insert(make_pair(make_pair(op.g_left(), op.g_right()), 0));
is.first->second += hits[i].fpkm();
}
}
}
vector<float> knockout(depth_of_coverage);
double total_doc = 0;
int total_len = 0;
float min_intra_intron_fraction = min(pre_mrna_fraction, min_isoform_fraction);
//for (size_t i = 0; i < hits.size(); ++i)
for (size_t i = 0; i < scaffolds.size(); ++i)
{
//const vector<AugmentedCuffOp>& aug_ops = hits[i].augmented_ops();
const vector<AugmentedCuffOp>& aug_ops = scaffolds[i].augmented_ops();
for (size_t j = 0; j < aug_ops.size(); ++j)
{
const AugmentedCuffOp& op = aug_ops[j];
if (op.opcode == CUFF_MATCH)
{
for (int K = op.g_left(); K < op.g_right(); ++K)
{
//if (!exclude_intra_intron || !intronic[K - bundle_origin])
if (!exclude_intra_intron ||
not_intronic(K-bundle_origin, depth_of_coverage, *intronic_cov, min_intra_intron_fraction,
(*scaff_intronic_status)[i]) )
{
total_doc += knockout[K - bundle_origin];
total_len += (knockout[K - bundle_origin] != 0);
knockout[K - bundle_origin] = 0;
}
}
}
}
}
return total_doc/(double)total_len;
}
double major_isoform_intron_doc(map<pair<int, int>, float>& intron_doc)
{
double major_isoform_intron_doc = 0;
int num_major_introns = 0;
for(map<pair<int, int>, float>::const_iterator itr = intron_doc.begin();
itr != intron_doc.end();
++itr)
{
bool heaviest = true;
for (map<pair<int,int>, float>::const_iterator itr2 = intron_doc.begin();
itr2 != intron_doc.end();
++itr2)
{
if (itr != itr2 &&
itr->second < itr2->second &&
overlap_in_genome(itr->first.first,
itr->first.second,
itr2->first.first,
itr2->first.second))
{
heaviest = false;
break;
}
}
if (heaviest)
{
major_isoform_intron_doc += itr->second;
num_major_introns++;
}
}
if (num_major_introns)
{
return major_isoform_intron_doc / num_major_introns;
}
else
{
return 0.0;
}
}
void record_min_doc_for_scaffolds(int bundle_origin,
const vector<Scaffold>& hits,
const vector<float>& depth_of_coverage,
const map<pair<int, int>, float>& intron_depth_of_coverage,
vector<double>& scaff_doc)
{
for (size_t h = 0; h < hits.size(); ++h)
{
double doc = 99999999.0;
if (hits[h].has_intron())
doc = get_intron_doc(hits[h], intron_depth_of_coverage);
doc = min(doc, get_scaffold_min_doc(bundle_origin,
hits[h],
depth_of_coverage));
scaff_doc.push_back(doc);
}
}
void record_doc_for_scaffolds(int bundle_origin,
const vector<Scaffold>& hits,
const vector<float>& depth_of_coverage,
vector<double>& scaff_doc)
{
for (size_t h = 0; h < hits.size(); ++h)
{
double doc;
doc = get_scaffold_doc(bundle_origin,
hits[h],
depth_of_coverage);
scaff_doc.push_back(doc);
}
}
void record_doc_for_scaffolds(int bundle_origin,
const vector<Scaffold>& hits,
const vector<float>& depth_of_coverage,
const map<pair<int, int>, float>& intron_depth_of_coverage,
vector<double>& scaff_doc)
{
for (size_t h = 0; h < hits.size(); ++h)
{
double doc;
if (hits[h].has_intron())
doc = get_intron_doc(hits[h], intron_depth_of_coverage);
else
doc = get_scaffold_doc(bundle_origin,
hits[h],
depth_of_coverage);
scaff_doc.push_back(doc);
}
}
double get_intron_doc(const Scaffold& s,
const map<pair<int, int>, float >& intron_depth_of_coverage)
{
const vector<AugmentedCuffOp>& aug_ops = s.augmented_ops();
int num_introns = 0;
double doc = 0;
for (size_t j = 0; j < aug_ops.size(); ++j)
{
const AugmentedCuffOp& op = aug_ops[j];
if (op.opcode == CUFF_INTRON)
{
num_introns++;
pair<int,int> op_intron(op.g_left(), op.g_right());
map<pair<int, int>, float >::const_iterator itr = intron_depth_of_coverage.find(op_intron);
// assert (itr != intron_depth_of_coverage.end());
if (itr == intron_depth_of_coverage.end())
{
map<pair<int, int>, float >::const_iterator zi;
for (zi = intron_depth_of_coverage.begin();
zi != intron_depth_of_coverage.end();
++zi)
{
verbose_msg( "Warning: intron not within scaffold ([%d-%d], %d)\n", zi->first.first, zi->first.second, zi->second);
}
}
doc += itr->second;
}
}
return doc / (double)num_introns;
}
double get_scaffold_doc(int bundle_origin,
const Scaffold& s,
const vector<float>& depth_of_coverage)
{
const vector<AugmentedCuffOp>& aug_ops = s.augmented_ops();
int m_len = 0;
double doc = 0;
for (size_t j = 0; j < aug_ops.size(); ++j)
{
const AugmentedCuffOp& op = aug_ops[j];
if (op.opcode == CUFF_MATCH)
{
for (int K = op.g_left(); K < op.g_right(); ++K)
{
m_len++;
doc += depth_of_coverage[K - bundle_origin];
}
}
}
return doc/(double)m_len;
}
double get_scaffold_min_doc(int bundle_origin,
const Scaffold& s,
const vector<float>& depth_of_coverage)
{
const vector<AugmentedCuffOp>& aug_ops = s.augmented_ops();
float min_doc = 99999999;
for (size_t j = 0; j < aug_ops.size(); ++j)
{
const AugmentedCuffOp& op = aug_ops[j];
if (op.opcode == CUFF_MATCH)
{
for (int K = op.g_left(); K < op.g_right(); ++K)
{
if (min_doc > depth_of_coverage[K - bundle_origin])
min_doc = depth_of_coverage[K - bundle_origin];
}
}
}
return min_doc;
}
|