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 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237
|
/* ===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information (NCBI)
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government do not place any restriction on its use or reproduction.
* We would, however, appreciate having the NCBI and the author cited in
* any work or product based on this material.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* ===========================================================================
*
* File Name: alignmgr2.c
*
* Author: Sarah Wheelan
*
* Version Creation Date: 10/01
*
* $Revision: 6.66 $
*
* File Description: SeqAlign indexing, access, and manipulation functions
*
* Modifications:
* --------------------------------------------------------------------------
* $Log: alignmgr2.c,v $
* Revision 6.66 2016/09/02 14:57:38 ucko
* Formally clean up calls to printf-family functions that are at least
* nominally unsafe, as already done in Debian/Ubuntu packages.
*
* Revision 6.65 2013/11/26 01:23:42 kans
* JIRA:GP-6623 AlnMgr2ConvertAllToDenseSeg bails specifically for Spliced-seg
*
* Revision 6.64 2013/11/26 00:15:42 kans
* JIRA:GP-5360 AlnMgr2ConvertAllToDenseSeg returns Boolean if not Dense-diag or Dense-seg to avoid crash on Spliced-seg
*
* Revision 6.63 2008/12/01 19:35:39 bollin
* prevent crash when mapping positions and row of alignment is entirely in the gapl.
*
* Revision 6.62 2007/03/09 20:37:06 bollin
* Fixed insidious double-increment bug in AlnMgr2MergeTwoAlignments - if the
* second alignment to be merged had more than one segment, the seg index was
* incremented past the number of segments (and some segments were not initialized).
*
* Revision 6.61 2007/01/09 14:13:52 bollin
* Fixed bug in AlnMgr2ExtendToCoords - prior version was not extending on 5' end.
*
* Revision 6.60 2006/09/06 15:48:33 bollin
* removed compiler warnings
*
* Revision 6.59 2006/09/06 15:14:54 bollin
* fixed bug that was generating segments of length zero at the end of an
* alignment
*
* Revision 6.58 2005/03/01 13:56:03 bollin
* if the alignment we want to index is a DenseSeg and not a list of alignments,
* just give it a simple index - don't decompose to pairwise and reconstruct it.
*
* Revision 6.57 2005/02/23 14:40:55 bollin
* when condensing columns in AlnMgr2CondenseColumns, make sure we do not
* disturb the ascending order of starts for each row
*
* Revision 6.56 2004/09/15 14:59:19 bollin
* make sure we do not read outside the alignment index arrays
*
* Revision 6.55 2004/05/20 19:46:25 bollin
* removed unused variables
*
* Revision 6.54 2004/05/11 13:19:49 bollin
* update the dimension of the shared alignment after adding a sequence.
*
* Revision 6.53 2004/04/13 14:43:07 kskatz
* Final resolution of revisions 6.51 and 6.52: reverted 6.52; then cleaned up readability of AlnMgr2SeqPortRead() and ensured that it will never call SeqPortRead for a length > AM_SEQPORTSIZE
*
* Revision 6.52 2004/04/12 19:52:15 kskatz
* Revision 6.51 was right neighborhood,wrong off-by-one: It was in AlnMgr2ComputeFreqMatrix() call to AlnMgr2SeqPortRead() when using l+AM_SEQPORTSIZE instead of l+AM_SEQPORTSIZE-1
*
* Revision 6.51 2004/04/12 17:00:44 kskatz
* Fixed off-by-one error in AlnMgr2SeqPortRead() length passed to SeqPortRead(); stop-start+1 changed to stop-start
*
* Revision 6.50 2004/03/11 14:15:41 bollin
* added extra check in AlnMgr2GetNthSeqIdPtr to avoid core dump if there are
* fewer than N SeqIDs in the alignment.
*
* Revision 6.49 2003/10/20 17:54:34 kans
* AlnMgr2ComputeFreqMatrix protect against dereferencing NULL bsp
*
* Revision 6.48 2003/10/09 13:46:52 rsmith
* Add AlnMgr2GetFirstNForSipList.
*
* Revision 6.47 2003/05/15 18:53:10 rsmith
* in AlnMgr2GetSeqRangeForSipInStdSeg always return start & stop in coordinate order. Do not assume what minus strand will do or not.
*
* Revision 6.46 2003/04/24 20:28:48 rsmith
* made AlnMgr2GetNthStdSeg use 1 based numbering like the other Nth functions.
*
* Revision 6.45 2003/04/23 20:36:13 rsmith
* Added four functions in Section 11 to get information about Std-Seg alignments.
*
* Revision 6.44 2003/03/31 20:17:11 todorov
* Added AlnMgr2IndexSeqAlignEx
*
* Revision 6.43 2003/02/03 12:36:22 kans
* AlnMgr2ComputeScoreForSeqAlign checks return value of AlnMgr2ComputeFreqMatrix, returns -1 if NULL to avoid dereference crash
*
* Revision 6.42 2002/10/23 16:32:19 todorov
* CondenseColumns fixed: needed to move the lens too.
*
* Revision 6.40 2002/10/16 15:54:28 todorov
* use the default dim value if not set
*
* Revision 6.39 2002/08/07 21:57:33 kans
* added AlignMgr2GetFirstNForStdSeg
*
* Revision 6.38 2002/07/11 14:35:51 kans
* fixed Mac complaints about prototypes
*
* Revision 6.37 2002/07/11 12:55:38 wheelan
* added support for std-seg alignments
*
* Revision 6.36 2002/06/04 17:43:07 todorov
* 1) Substituted AddInNewSA with a new and optimized AddInNewPairwiseSA function.
* 2) Fixed a few bugs in other functions.
*
* Revision 6.35 2002/05/17 15:04:42 wheelan
* bug fix in ExtendToCoords
*
* Revision 6.34 2002/05/17 11:02:36 wheelan
* bug fixes in Merge func
*
* Revision 6.32 2002/03/04 17:19:18 wheelan
* added AlnMgr2FuseSet, changed behavior of RemoveInconsistent, fixed GetNextAlnBitBugs
*
* Revision 6.31 2002/01/31 17:41:47 wheelan
* various bug fixes -- no more 0 len segments, better handling of rows that are one big insert, etc.
*
* Revision 6.30 2002/01/30 19:12:53 wheelan
* added RemoveInconsistentAlnsFromSet, ExtractPairwiseSeqAlign, changed behavior of GetSubAlign, changed structures and behavior of GetNextAlnBit, added GetInterruptInfo, added AlnMgr2IndexAsRows, bug fixes in indexing routines
*
* Revision 6.29 2002/01/02 15:05:07 wheelan
* changes to force more efficient ordering in CompareAsp callbacks, plus more stringent checks in AlnMgr2AddInNewSA
*
* Revision 6.28 2001/12/28 22:53:20 wheelan
* bug fixes; added AlnMgr2DupAlnAndIndexes, changed some New and Free funcs
*
* Revision 6.27 2001/12/27 16:07:22 wheelan
* bug fix in ExtendToEnd
*
* Revision 6.26 2001/12/20 19:43:20 wheelan
* bug fix in GetNextAlnBit -- no more incorrect inserts
*
* Revision 6.25 2001/12/18 16:36:57 wheelan
* scattered fixes to unaligned region code
*
* Revision 6.24 2001/12/17 19:36:39 wheelan
* various fixes in AlnMgr2AddInNewSA
*
* Revision 6.23 2001/12/14 12:38:50 wheelan
* added functions for ddv
*
* Revision 6.22 2001/12/05 12:25:49 wheelan
* bug fix in SortByNthRow
*
* Revision 6.21 2001/12/04 19:28:55 wheelan
* bug fixes in AddInNewSA and in IndexSingleDenseSegSA
*
* Revision 6.20 2001/12/04 14:31:27 wheelan
* fixes to avoid mistakenly processing AM2_LITE as real indexed alignments
*
* Revision 6.19 2001/11/30 16:55:21 wheelan
* added AlnMgr2PadConservatively
*
* Revision 6.18 2001/11/29 18:38:47 wheelan
* cleanup as recommended by Mac compiler
*
* Revision 6.17 2001/11/29 17:37:16 wheelan
* added ExtendToCoords and MergeTwoAlignments
*
* Revision 6.16 2001/11/27 15:47:40 wheelan
* bug fixes in AnchorSeqAlign, DoCondense, and AddInNewSA
*
* Revision 6.15 2001/11/15 18:23:06 wheelan
* small change in AlnMgr2GetNthRowSpan
*
* Revision 6.14 2001/11/15 18:09:38 wheelan
* another bug fix in AddInNewSA
*
* Revision 6.13 2001/11/15 15:30:54 wheelan
* many bugs fixed, leaks plugged, plus reworked AddInNewSA to use new AMSmall field
*
* Revision 6.12 2001/11/13 14:36:13 wheelan
* many bug fixes in AddInNewSA and MapBioseqToSeqAlign
*
* Revision 6.11 2001/11/08 19:56:07 wheelan
* added AlnMgr2GetNthRowSpanInSA, fixed various memory errors
*
* Revision 6.10 2001/11/08 01:39:15 wheelan
* many bug fixes in and around AddInNewSA
*
* Revision 6.9 2001/11/02 14:01:30 wheelan
* bug fixes in AlnMgr2AddInNewSA
*
* Revision 6.8 2001/10/31 12:00:46 wheelan
* commented out the mistakenly uncommented comment
*
* Revision 6.7 2001/10/30 20:14:38 wheelan
* bug fixes for minus strands in AddInNewSA, bug fix in GetSubAlign
*
* Revision 6.6 2001/10/23 12:14:27 wheelan
* changes in AlnMgr2GetNextAlnBit as well as tree-based multiple alignment algorithm
*
* Revision 6.5 2001/10/18 15:10:53 wheelan
* fixed AlnMgr2ComputeScoreForSeqAlign
*
* Revision 6.4 2001/10/16 12:00:17 wheelan
* added GetParent and FreeEitherIndex
*
* Revision 6.3 2001/10/08 18:43:29 wheelan
* added comments
*
* Revision 6.2 2001/10/03 18:13:01 wheelan
* changed some colliding defines
*
* Revision 6.1 2001/10/03 14:20:11 wheelan
* initial checkin
*
* ==========================================================================
*
*/
#include <alignmgr2.h>
/***************************************************************************
*
* static functions
*
***************************************************************************/
/* SECTION 1 */
static SARowDat2Ptr SARowDat2New(void);
static void SARowDat2Free(SARowDat2Ptr srdp);
static SARowDat2Ptr SARowDat2Copy(SARowDat2Ptr srdp);
static SAIndex2Ptr SAIndex2New(void);
static SAIndex2Ptr SAIndex2Copy(VoidPtr index);
static AMAlignIndex2Ptr AMAlignIndex2Copy(VoidPtr index);
static void AMIntervalSetFree(AMIntervalSetPtr amint);
/* SECTION 2 */
static void AlnMgr2ConvertDendiagToDensegChain(SeqAlignPtr sap);
static void AlnMgr2IndexSingleDenseSegSA(SeqAlignPtr sap);
static Boolean AlnMgr2UnpackSeqAlign(SeqAlignPtr sap);
static Boolean AlnMgr2ConvertAllToDenseSeg(SeqAlignPtr sap);
static void AlnMgr2DecomposeToPairwise(SeqAlignPtr sap);
static void AlnMgr2HidePairwiseConflicts(SeqAlignPtr sap);
static void AlnMgr2SortBySeqId(SeqAlignPtr sap);
static int LIBCALLBACK AlnMgr2CompareIds(VoidPtr ptr1, VoidPtr ptr2);
static void AlnMgr2TossWorse(SeqAlignPtr sap, Int4 i, Int4 j);
static AMIntervalSetPtr AlnMgr2MakeIntervals(SeqAlignPtr sap);
static void AlnMgr2SortIntervals(AMIntervalSetPtr amint);
static int LIBCALLBACK AlnMgr2CompareIntervals(VoidPtr ptr1, VoidPtr ptr2);
static AMVertexPtr PNTR AlnMgr2MakeVerticesFromIntervals(SeqAlignPtr sap, AMIntervalSetPtr amint_head, AMVertexPtr PNTR vertexhead, AMEdgePtr PNTR edgehead, Int4Ptr numvertices);
static void AlnMgr2SortVerticesByNumEdges(AMVertexPtr PNTR vertexarray, Int4 numvertices);
static int LIBCALLBACK AlnMgr2CompareVertices(VoidPtr ptr1, VoidPtr ptr2);
static void AlnMgr2SortEdgesByWeight(AMEdgePtr PNTR edge_head);
static int LIBCALLBACK AlnMgr2CompareEdges(VoidPtr ptr1, VoidPtr ptr2);
static Int4 AlnMgr2MatchToVertex(SeqIdPtr sip, Int4 start, Int4 stop, AMVertexPtr PNTR vertexarray, Int4 numvertices);
static void AlnMgr2UsePrimsAlgorithm(AMVertexPtr PNTR vertexarray, Int4 numvertices, AMEdgePtr edge_head);
static void AlnMgr2RecursePrims(AMVertexPtr PNTR vertexarray, AMEdgePtr edge_head);
static AMVertexPtr AlnMgr2GetBetterVertex(AMVertexPtr PNTR vertexarray, AMEdgePtr edge);
static AMEdgePtr AlnMgr2GetEdgeList(Int4 vertexnum, AMEdgePtr edge_head, AMEdgePtr already_used);
static void AlnMgr2CleanUpLeftovers(AMVertexPtr PNTR vertexarray, Int4 numvertices, AMEdgePtr edge_head);
static Boolean AlnMgr2SameSeq(AMVertexPtr vertex1, AMVertexPtr vertex2);
static void AlnMgr2BuildAlignmentFromTree(AMVertexPtr PNTR vertexarray, Int4 numvertices, AMEdgePtr edge_head, SeqAlignPtr sap);
static AMVertexPtr AlnMgr2GetAdjacentVertices(AMVertexPtr vertex, AMVertexPtr PNTR vertexarray, AMEdgePtr edge_head);
static void AlnMgr2AddInNewSA(SeqAlignPtr parent, SeqAlignPtr sap);
static void AlnMgr2AddInNewPairwiseSA(SeqAlignPtr parent, SeqAlignPtr sap);
static Int4 AlnMgr2MapSegStartToSegStart(SeqAlignPtr sap, Int4 pos, Int4 row1, Int4 row2, Int4 len);
static Int4 AlnMgr2GetSegForStartPos(SeqAlignPtr sap, Int4 pos, Int4 row);
static void AlnMgr2CondenseColumns(DenseSegPtr dsp);
static void AlnMgr2CondenseRows(DenseSegPtr dsp, Int4 whichrow);
static Boolean AlnMgr2DoCondense(DenseSegPtr dsp, Int4 rownum1, Int4 rownum2);
static int LIBCALLBACK AlnMgr2CompareCdRows(VoidPtr ptr1, VoidPtr ptr2);
static int LIBCALLBACK AlnMgr2CompareAsps(VoidPtr ptr1, VoidPtr ptr2);
static int LIBCALLBACK AlnMgr2CompareAspsMinus(VoidPtr ptr1, VoidPtr ptr2);
static void AlnMgr2GetFirstSharedRow(SeqAlignPtr sap1, SeqAlignPtr sap2, Int4Ptr n1, Int4Ptr n2);
static SeqIdPtr AlnMgr2SeqIdListsOverlap(SeqIdPtr sip1, SeqIdPtr sip2);
static Int4 AlnMgr2OrderSeqIds(SeqIdPtr sip1, SeqIdPtr sip2);
static void AlnMgr2SetUnaln(SeqAlignPtr sap);
static int LIBCALLBACK AlnMgr2CompareUnalnAMS(VoidPtr ptr1, VoidPtr ptr2);
/* SECTION 4 */
static Int4 binary_search_on_uint4_list(Uint4Ptr list, Uint4 pos, Uint4 listlen);
static Int4 binary_search_on_uint2_list(Uint2Ptr list, Int4 ele, Uint2 listlen);
static void AlnMgr2GetUnalignedInfo(SeqAlignPtr sap, Int4 segment, Int4 row, Int4Ptr from, Int4Ptr to);
static void AlnMgr2GetNthSeqRangeInSASet(SeqAlignPtr sap, Int4 n, Int4Ptr start, Int4Ptr stop);
static Int4 AlnMgr2GetMaxUnalignedLength(SeqAlignPtr sap, Int4 seg);
/* SECTION 5 */
static void AlnMgr2AnchorChild(SeqAlignPtr sap, Int4 which_row);
/* SECTION 8 */
static Int4 AlnMgr2GetScoreForPair(Int4 res1, Int4 res2, Boolean is_prot);
/* SECTION 9 */
static int LIBCALLBACK AMCompareStarts(VoidPtr ptr1, VoidPtr ptr2);
typedef struct am_seqpieceset AMSeqPieceSet, PNTR AMSeqPieceSetPtr;
typedef struct am_seqpiece AMSeqPiece, PNTR AMSeqPiecePtr;
struct am_seqpiece {
Int4 beg;
Int4 end;
Int4 left;
Int4 right;
Int4 orig_left;
Int4 orig_right;
Boolean aligned;
Int4 seg;
Int4 pos;
DenseSegPtr alt_dsp;
Int4 alt_seg;
Int4 alt_pos;
AMSeqPiecePtr next;
AMSeqPiecePtr prev;
AMSeqPieceSetPtr set;
};
struct am_seqpieceset {
AMSeqPiecePtr head;
AMSeqPiecePtr tail;
DenseSegPtr dsp;
DenseSegPtr alt_dsp;
Int4 row;
Int4 row2;
Int4 alt_row;
Int4 alt_row2;
Uint1 strand;
Boolean plus;
Int4 max_pos;
Boolean sign;
AMSeqPieceSetPtr next;
};
/***************************************************************************
*
* SECTION 1: Functions for allocating and freeing data structures used
* by the alignment manager; copying functions are also here.
*
***************************************************************************/
/* SECTION 1 */
static SARowDat2Ptr SARowDat2New(void)
{
return (SARowDat2Ptr)MemNew(sizeof(SARowDat2));
}
/* SECTION 1 */
static void SARowDat2Free(SARowDat2Ptr srdp)
{
if (srdp == NULL)
return;
if (srdp->sect != NULL)
MemFree(srdp->sect);
if (srdp->unsect != NULL)
MemFree(srdp->unsect);
MemFree(srdp->insect);
MemFree(srdp->unaligned);
MemFree(srdp);
}
/* SECTION 1 */
static SARowDat2Ptr SARowDat2Copy(SARowDat2Ptr srdp)
{
Int4 i;
SARowDat2Ptr srdp2;
if (srdp == NULL)
return NULL;
srdp2 = SARowDat2New();
srdp2->numsect = srdp->numsect;
srdp2->sect = (Uint2Ptr)MemNew(srdp2->numsect*sizeof(Uint2));
for (i=0; i<srdp2->numsect; i++)
{
srdp2->sect[i] = srdp->sect[i];
}
srdp2->numunsect = srdp->numunsect;
srdp2->unsect = (Uint2Ptr)MemNew(srdp2->numunsect*sizeof(Uint2));
for (i=0; i<srdp2->numunsect; i++)
{
srdp2->unsect[i] = srdp->unsect[i];
}
srdp2->numinsect = srdp->numinsect;
srdp2->insect = (Uint2Ptr)MemNew(srdp2->numinsect*sizeof(Uint2));
for (i=0; i<srdp2->numinsect; i++)
{
srdp2->insect[i] = srdp->insect[i];
}
srdp2->numunaln = srdp->numunaln;
srdp2->unaligned = (Uint2Ptr)MemNew(srdp2->numunaln*sizeof(Uint2));
for (i=0; i<srdp2->numunaln; i++)
{
srdp2->unaligned[i] = srdp->unaligned[i];
}
return srdp2;
}
/* SECTION 1 */
static SAIndex2Ptr SAIndex2New(void)
{
SAIndex2Ptr saip;
saip = (SAIndex2Ptr)MemNew(sizeof(SAIndex2));
saip->indextype = INDEX_CHILD;
saip->freefunc = (SeqAlignIndexFreeFunc)(SAIndex2Free2);
saip->anchor = -1;
return saip;
}
/* SECTION 1 */
NLM_EXTERN Boolean LIBCALLBACK SAIndex2Free2(VoidPtr index)
{
Int4 i;
SAIndex2Ptr saip;
if (index == NULL)
return TRUE;
saip = (SAIndex2Ptr)(index);
MemFree(saip->aligncoords);
for (i=0; i<saip->numrows; i++)
{
SARowDat2Free(saip->srdp[i]);
}
MemFree(saip->srdp);
MemFree(saip);
return TRUE;
}
/* SECTION 1 */
NLM_EXTERN void AlnMgr2FreeInterruptInfo(AMInterrInfoPtr interr)
{
if (interr == NULL)
return;
MemFree(interr->starts);
MemFree(interr->lens);
MemFree(interr->types);
MemFree(interr);
}
/* SECTION 1*/
static SAIndex2Ptr SAIndex2Copy(VoidPtr index)
{
Int4 i;
SAIndex2Ptr saip;
SAIndex2Ptr saip2;
saip2 = SAIndex2New();
saip = (SAIndex2Ptr)(index);
saip2->numseg = saip->numseg;
saip2->aligncoords = (Uint4Ptr)MemNew(saip2->numseg*sizeof(Uint4));
for (i=0; i<saip2->numseg; i++)
{
saip2->aligncoords[i] = saip->aligncoords[i];
}
saip2->anchor = saip->anchor;
saip2->numrows = saip->numrows;
saip2->numseg = saip->numseg;
saip2->srdp = (SARowDat2Ptr PNTR)MemNew(saip2->numrows*sizeof(SARowDat2));
for (i=0; i<saip2->numrows; i++)
{
saip2->srdp[i] = SARowDat2Copy(saip->srdp[i]);
}
saip2->numunaln = saip->numunaln;
saip2->unaln = (Uint4Ptr)MemNew(saip2->numunaln*sizeof(Uint4));
for (i=0; i<saip2->numunaln; i++)
{
saip2->unaln[i] = saip->unaln[i];
}
saip2->numinchain = saip->numinchain;
saip2->numsplitaln = saip->numsplitaln;
saip2->score = saip->score;
saip2->aligned = saip->aligned;
return saip2;
}
/* SECTION 1 */
static AMAlignIndex2Ptr AMAlignIndex2New(void)
{
AMAlignIndex2Ptr amaip;
amaip = (AMAlignIndex2Ptr)MemNew(sizeof(AMAlignIndex2));
amaip->indextype = INDEX_PARENT;
amaip->freefunc = (SeqAlignIndexFreeFunc)(AMAlignIndex2Free2);
return amaip;
}
/* SECTION 1 */
NLM_EXTERN Boolean LIBCALLBACK AMAlignIndex2Free2(VoidPtr index)
{
AMAlignIndex2Ptr amaip;
Int4 i;
if (index == NULL)
return FALSE;
amaip = (AMAlignIndex2Ptr)(index);
for (i=0; i<amaip->numrows; i++)
{
SeqIdFree(amaip->ids[i]);
}
MemFree(amaip->ids);
MemFree(amaip->saps);
MemFree(amaip->aligned);
SeqAlignFree(amaip->sharedaln);
MemFree(amaip);
return TRUE;
}
/* SECTION 1 */
static AMAlignIndex2Ptr AMAlignIndex2Copy(VoidPtr index)
{
AMAlignIndex2Ptr amaip;
AMAlignIndex2Ptr amaip2;
Int4 i;
if (index == NULL)
return NULL;
amaip = (AMAlignIndex2Ptr)(index);
amaip2 = AMAlignIndex2New();
amaip2->alnstyle = amaip->alnstyle;
amaip2->anchor = amaip->anchor;
amaip2->numrows = amaip->numrows;
amaip2->ids = (SeqIdPtr PNTR)MemNew(amaip2->numrows*sizeof(SeqIdPtr));
for (i=0; i<amaip2->numrows; i++)
{
amaip2->ids[i] = SeqIdDup(amaip->ids[i]);
}
amaip2->numsaps = amaip->numsaps;
amaip2->saps = (SeqAlignPtr PNTR)MemNew(amaip2->numsaps*sizeof(SeqAlignPtr));
amaip2->aligned = (Boolean PNTR)MemNew(amaip2->numsaps*sizeof(Boolean));
for (i=0; i<amaip2->numsaps; i++)
{
amaip2->saps[i] = SeqAlignDup(amaip->saps[i]);
amaip2->aligned[i] = amaip->aligned[i];
if (i>0)
amaip2->saps[i-1]->next = amaip2->saps[i];
}
amaip2->sharedaln = AlnMgr2DupAlnAndIndexes(amaip->sharedaln);
return amaip2;
}
/* SECTION 1 */
NLM_EXTERN void AMAlignIndexFreeEitherIndex(SeqAlignPtr sap)
{
if (sap == NULL || sap->saip == NULL)
return;
if (sap->saip->indextype == INDEX_PARENT)
AMAlignIndex2Free2(sap->saip);
else
SAIndex2Free2(sap->saip);
sap->saip = NULL;
}
/* SECTION 1 */
NLM_EXTERN SeqAlignPtr AlnMgr2DupAlnAndIndexes(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
SAIndex2Ptr saip;
SeqAlignPtr sap_new;
if (sap == NULL)
return NULL;
if (sap->saip == NULL)
return (SeqAlignDup(sap));
sap_new = NULL;
if (sap->saip->indextype == INDEX_CHILD)
{
sap_new = SeqAlignDup(sap);
sap_new->saip = (Pointer)SAIndex2Copy(sap->saip);
saip = (SAIndex2Ptr)(sap_new->saip);
saip->top = AlnMgr2GetParent(sap);
} else if (sap->saip->indextype == INDEX_PARENT)
{
sap_new = SeqAlignNew();
sap_new->type = sap->type;
sap_new->segtype = sap->segtype;
sap_new->saip = (Pointer)(AMAlignIndex2Copy(sap->saip));
amaip = (AMAlignIndex2Ptr)(sap_new->saip);
sap_new->segs = amaip->saps[0];
}
return sap_new;
}
/* SECTION 1 */
NLM_EXTERN AlnMsg2Ptr AlnMsgNew2(void)
{
AlnMsg2Ptr amp;
amp = (AlnMsg2Ptr)MemNew(sizeof(AlnMsg2));
amp->real_from = -2;
return amp;
}
/* SECTION 1 */
NLM_EXTERN AlnMsg2Ptr AlnMsgFree2(AlnMsg2Ptr amp)
{
if (amp->left_interrupt != NULL)
{
MemFree(amp->left_interrupt);
amp->left_interrupt = NULL;
}
if (amp->right_interrupt != NULL)
{
MemFree(amp->right_interrupt);
amp->right_interrupt = NULL;
}
MemFree(amp);
return NULL;
}
/* SECTION 1 */
NLM_EXTERN void AlnMsgReNew2(AlnMsg2Ptr amp)
{
if (amp == NULL)
return;
if (amp->left_interrupt != NULL)
{
MemFree(amp->left_interrupt);
amp->left_interrupt = NULL;
}
if (amp->right_interrupt != NULL)
{
MemFree(amp->right_interrupt);
amp->right_interrupt = NULL;
}
amp->real_from = -2;
amp->len = -2;
return;
}
/* SECTION 1 */
static void AMIntervalSetFree(AMIntervalSetPtr amint)
{
AMIntervalPtr intv;
AMIntervalPtr intv_next;
intv = amint->int_head;
while (intv != NULL)
{
intv_next = intv->next;
MemFree(intv);
intv = intv_next;
}
SeqIdFree(amint->sip);
MemFree(amint);
}
/* SECTION 1 */
NLM_EXTERN void AMFreqFree(AMFreqPtr afp)
{
Int4 i;
if (afp == NULL)
return;
for (i=0; i<afp->size; i++)
{
MemFree(afp->freq[i]);
}
MemFree(afp->freq);
MemFree(afp);
}
/* SECTION 1 */
static void AMSeqPieceSetFree(AMSeqPieceSetPtr s_set)
{
AMSeqPieceSetPtr s_set_next;
AMSeqPiecePtr s, s_next;
while (s_set) {
s = s_set->head;
while (s) {
s_next = s->next;
MemFree(s);
s = s_next;
}
s_set_next = s_set->next;
MemFree(s_set);
s_set = s_set_next;
}
}
/***************************************************************************
*
* SECTION 2: Functions used to create the indexes for parent and child
* seqaligns.
* SECTION 2a: Functions to create indexes for child seqaligns, and
* to convert seqaligns to dense-seg type
* SECTION 2b: Functions to unpack and rearrange complicated seqaligns
* into simple chains of dense-seg and dense-diag types
* SECTION 2c: Functions to create indexes for parent seqaligns
* SECTION 2d: Accessory functions for parent indexing
*
***************************************************************************/
/***************************************************************************
*
* AlnMgr2ConvertDendiagToDensegChain takes a dense-diag style alignment
* and makes each diag into its own denseg seqalign, then links the new
* alignments together.
*
***************************************************************************/
/* SECTION 2a */
static void AlnMgr2ConvertDendiagToDensegChain(SeqAlignPtr sap)
{
DenseDiagPtr ddp;
DenseDiagPtr ddp_next;
DenseSegPtr dsp;
Int4 i;
SeqAlignPtr sap_new;
SeqAlignPtr sap_next;
SeqAlignPtr sap_prev;
if (sap == NULL || sap->segtype != SAS_DENDIAG)
return;
sap_next = sap->next;
ddp = (DenseDiagPtr)(sap->segs);
/* convert the first diag to dense-seg and put it in the original alignment */
dsp = DenseSegNew();
dsp->ids = ddp->id;
ddp->id = NULL;
dsp->dim = ddp->dim;
dsp->numseg = 1;
dsp->starts = (Int4Ptr)MemNew((dsp->dim)*(dsp->numseg)*sizeof(Int4));
dsp->lens = (Int4Ptr)MemNew((dsp->numseg)*sizeof(Int4));
dsp->strands = (Uint1Ptr)MemNew((dsp->dim)*(dsp->numseg)*sizeof(Uint1));
for (i=0; i<dsp->dim; i++)
{
dsp->starts[i] = ddp->starts[i];
if (ddp->strands != NULL)
dsp->strands[i] = ddp->strands[i];
else
dsp->strands[i] = Seq_strand_plus;
}
dsp->lens[0] = ddp->len;
sap->segs = (Pointer)(dsp);
sap->segtype = SAS_DENSEG;
ddp_next = ddp->next;
ddp->next = NULL;
DenseDiagFree(ddp);
ddp = ddp_next;
if (ddp == NULL)
return;
sap_prev = sap;
while (ddp)
{
sap_new = SeqAlignNew();
sap_new->type = SAT_PARTIAL;
sap_new->segtype = SAS_DENSEG;
sap_new->dim = ddp->dim;
dsp = DenseSegNew();
dsp->ids = ddp->id;
ddp->id = NULL;
dsp->dim = ddp->dim;
dsp->numseg = 1;
dsp->starts = (Int4Ptr)MemNew((dsp->dim)*(dsp->numseg)*sizeof(Int4));
dsp->lens = (Int4Ptr)MemNew((dsp->numseg)*sizeof(Int4));
dsp->strands = (Uint1Ptr)MemNew((dsp->dim)*(dsp->numseg)*sizeof(Uint1));
for (i=0; i<dsp->dim; i++)
{
dsp->starts[i] = ddp->starts[i];
if (ddp->strands != NULL)
dsp->strands[i] = ddp->strands[i];
else
dsp->strands[i] = Seq_strand_plus;
}
dsp->lens[0] = ddp->len;
sap_new->segs = (Pointer)(dsp);
ddp_next = ddp->next;
ddp->next = NULL;
DenseDiagFree(ddp);
ddp = ddp_next;
sap_prev->next = sap_new;
sap_prev = sap_new;
}
sap_new->next = sap_next;
}
/* SECTION 2a */
/***************************************************************************
*
* AlnMgr2IndexSingleDenseSegSA creates the SAIndex2 structure for a given
* dense-seg seqalign. This structure has binary-searchable indexes into
* the segs. If the strands are not allocated, this function allocates
* them and sets them to Seq_strand_plus.
*
***************************************************************************/
static void AlnMgr2IndexSingleDenseSegSA(SeqAlignPtr sap)
{
DenseSegPtr dsp;
Int4 i;
Int4 j;
Int4 last;
Int4 next;
Int4 row;
SAIndex2Ptr saip;
Boolean unal;
if (sap->segtype != SAS_DENSEG)
return;
dsp = (DenseSegPtr)(sap->segs);
if (dsp->strands == NULL)
{
dsp->strands = (Uint1Ptr)MemNew(dsp->dim*dsp->numseg*sizeof(Uint1));
for (i=0; i<dsp->dim*dsp->numseg; i++)
{
dsp->strands[i] = Seq_strand_plus;
}
}
saip = SAIndex2New();
saip->aligncoords = (Uint4Ptr)MemNew((dsp->numseg)*sizeof(Uint4));
saip->srdp = (SARowDat2Ptr PNTR)MemNew((dsp->dim)*sizeof(SARowDat2Ptr));
saip->numrows = dsp->dim;
saip->numseg = dsp->numseg;
for (i=0; i<dsp->dim; i++)
{
saip->srdp[i] = SARowDat2New();
}
for (i=0; i<dsp->numseg; i++)
{
if (i != 0)
saip->aligncoords[i] = saip->aligncoords[i-1] + dsp->lens[i-1];
for (row=0; row<dsp->dim; row++)
{
if (dsp->starts[dsp->dim*i + row] != -1)
saip->srdp[row]->numsect++;
}
}
for (row=0; row<dsp->dim; row++)
{
saip->srdp[row]->sect = (Uint2Ptr)MemNew((saip->srdp[row]->numsect)*sizeof(Uint2));
saip->srdp[row]->unsect = (Uint2Ptr)MemNew((dsp->numseg - saip->srdp[row]->numsect)*sizeof(Uint2));
saip->srdp[row]->numsect = 0;
saip->srdp[row]->unaligned = (Uint2Ptr)MemNew(dsp->numseg*sizeof(Uint2));
}
for (i=0; i<dsp->numseg; i++)
{
for (row=0; row<dsp->dim; row++)
{
if (dsp->starts[dsp->dim*i + row] != -1)
{
saip->srdp[row]->sect[saip->srdp[row]->numsect] = i;
saip->srdp[row]->numsect++;
} else
{
saip->srdp[row]->unsect[saip->srdp[row]->numunsect] = i;
saip->srdp[row]->numunsect++;
}
}
}
for (row=0; row<dsp->dim; row++)
{
for (i=0; i<dsp->numseg; i++)
{
unal = FALSE;
last = -1;
j = i; /* only blocks with sequence can have flanking unal. regions */
if (j >= 0 && dsp->starts[dsp->dim*j+row] != -1)
{
if (dsp->strands[row] == Seq_strand_minus)
last = dsp->starts[dsp->dim*j+row];
else
last = dsp->starts[dsp->dim*j+row] + dsp->lens[j];
}
if (last > -1)
{
next = -1;
j++;
/* find next block of aligned sequence in this row */
for (j; j<dsp->numseg && next == -1; j++)
{
if (dsp->starts[dsp->dim*j+row] != -1)
{
if (dsp->strands[row] == Seq_strand_minus)
next = dsp->starts[dsp->dim*j+row] + dsp->lens[j];
else
next = dsp->starts[dsp->dim*j+row];
}
}
if (next > -1) /* look for unaligned seq on right side of this seg */
{
if (next != last)
unal = TRUE;
}
}
if (unal == TRUE)
{
saip->srdp[row]->unaligned[saip->srdp[row]->numunaln] = i;
saip->srdp[row]->numunaln++;
}
}
}
sap->saip = (SeqAlignIndexPtr)(saip);
}
/* SECTION 2a */
/***************************************************************************
*
* AlnMgr2IndexSingleChildSeqAlign takes a simple dense-seg or dense-diag
* seqalign, converts it to dense-seg, and then calls
* AlnMgr2IndexSingleDenseSegSA to create the indexes. If the alignment has
* already been indexed, this erases that index and reindexes the alignment.
* (SINGCHILD)
*
***************************************************************************/
NLM_EXTERN Boolean AlnMgr2IndexSingleChildSeqAlign(SeqAlignPtr sap)
{
SeqAlignPtr salp;
SeqAlignPtr salp_prev;
SeqAlignPtr sap_next;
if (sap == NULL)
return FALSE;
if (sap->saip != NULL)
{
if (sap->saip->indextype != INDEX_CHILD)
return FALSE;
SAIndex2Free2(sap->saip);
sap->saip = NULL;
}
sap_next = sap->next;
sap->next = NULL;
if (sap->segtype == SAS_DISC)
return FALSE;
if (sap->segtype == SAS_DENDIAG)
AlnMgr2ConvertDendiagToDensegChain(sap);
salp = sap;
salp_prev = sap;
while (salp != NULL)
{
AlnMgr2IndexSingleDenseSegSA(salp);
salp_prev = salp;
salp = salp->next;
}
salp_prev->next = sap_next;
return TRUE;
}
/***************************************************************************
*
* AlnMgr2UnpackSeqAlign rearranges any seqalign (except alignments with
* more than two levels of nested discontinuous alignments) to a simple
* discontinuous alignment or a linked list of alignments.
*
***************************************************************************/
/* SECTION 2b */
static Boolean AlnMgr2UnpackSeqAlign(SeqAlignPtr sap)
{
SeqAlignPtr sap_new;
SeqAlignPtr sap_next;
SeqAlignPtr sap_segs;
SeqAlignPtr sap_segs_head;
SeqAlignPtr sap_segs_prev;
if (sap == NULL)
return FALSE;
sap_segs = NULL;
if (sap->segtype == SAS_DISC)
{
sap_segs_head = (SeqAlignPtr)(sap->segs);
if (sap_segs_head->segtype == SAS_DISC)
{
sap_segs_prev = (SeqAlignPtr)(sap_segs_head->segs);
sap_segs_head->segs = NULL;
sap_next = sap_segs_head->next;
sap_segs_head->next = NULL;
SeqAlignFree(sap_segs_head);
sap_segs_head = sap_segs_prev;
sap->segs = (Pointer)(sap_segs_head);
while (sap_segs_prev->next)
{
sap_segs_prev = sap_segs_prev->next;
if (sap_segs_prev->segtype == SAS_DISC)
return FALSE;
}
sap_segs_prev->next = sap_next;
sap_segs = sap_next;
} else
sap_segs = sap_segs_head->next;
while (sap_segs)
{
if (sap_segs->segtype == SAS_DISC)
{
sap_next = sap_segs->next;
sap_segs->next = NULL;
sap_segs_prev->next = (SeqAlignPtr)(sap_segs->segs);
sap_segs->segs = NULL;
SeqAlignFree(sap_segs);
while (sap_segs_prev->next)
{
sap_segs_prev = sap_segs_prev->next;
if (sap_segs_prev->segtype == SAS_DISC)
return FALSE;
}
sap_segs_prev->next = sap_next;
sap_segs = sap_next;
} else
sap_segs = sap_segs->next;
}
} else
{
sap_new = SeqAlignNew();
sap_new->type = SAT_GLOBAL;
sap_new->segtype = sap->segtype;
sap_new->dim = sap->dim;
sap_new->segs = sap->segs;
sap_new->master = sap->master;
sap_new->bounds = sap->bounds;
sap_new->next = sap->next;
sap_new->score = sap->score;
sap->next = NULL;
sap->segtype = SAS_DISC;
sap->type = 0;
sap->dim = 0;
sap->master = NULL;
sap->bounds = NULL;
sap->score = NULL;
sap->segs = (Pointer)sap_new;
sap_segs_prev = sap_new;
sap_segs = sap_new->next;
while (sap_segs)
{
if (sap_segs->segtype == SAS_DISC)
{
sap_next = sap_segs->next;
sap_segs->next = NULL;
sap_segs_prev->next = (SeqAlignPtr)(sap_segs->segs);
sap_segs->segs = NULL;
SeqAlignFree(sap_segs);
while (sap_segs_prev->next)
{
sap_segs_prev = sap_segs_prev->next;
if (sap_segs_prev->segtype == SAS_DISC)
return FALSE;
}
sap_segs_prev->next = sap_next;
sap_segs = sap_next;
} else
sap_segs = sap_segs->next;
}
}
return TRUE;
}
/* SECTION 2b */
static void AlnMgr2UnpackSeqAlignChain(SeqAlignPtr sap)
{
Int4 i;
SeqAlignPtr salp_head;
SeqAlignPtr salp_prev;
SeqAlignPtr sap_next;
SeqAlignPtr sap_orig;
SeqAlignPtr sap_prev;
salp_head = salp_prev = NULL;
i = 0;
while (sap != NULL)
{
sap_next = sap->next;
sap->next = NULL;
AlnMgr2UnpackSeqAlign(sap);
while (sap != NULL)
{
if (salp_prev != NULL)
{
salp_prev->next = (SeqAlignPtr)(sap->segs);
sap->segs = NULL;
while (salp_prev->next != NULL)
{
salp_prev = salp_prev->next;
}
} else
{
salp_head = salp_prev = (SeqAlignPtr)(sap->segs);
sap->segs = NULL;
while (salp_prev->next != NULL)
{
salp_prev = salp_prev->next;
}
}
sap_prev = sap;
sap = sap->next;
sap_prev->next = NULL;
if (i>0)
SeqAlignFree(sap_prev);
else
sap_orig = sap_prev; /* this is the pointer that was passed in */
i++;
}
sap = sap_next;
}
sap_orig->segs = (Pointer)(salp_head);
}
/* SECTION 2b */
/***************************************************************************
*
* AlnMgr2ConvertAllToDenseSeg goes through a chain of simple child
* seqaligns and makes sure that each is a dense-seg seqalign with the
* strands explicitly allocated; dense-diag alignments are converted and
* non-allocated strands are allocated and all set to Seq_strand_plus.
*
***************************************************************************/
static Boolean AlnMgr2ConvertAllToDenseSeg(SeqAlignPtr sap)
{
DenseSegPtr dsp;
Int4 i;
SeqAlignPtr sap_next;
while (sap != NULL)
{
sap_next = sap->next;
if (sap->segtype == SAS_DENDIAG) {
AlnMgr2ConvertDendiagToDensegChain(sap);
}
else if (sap->segtype == SAS_DENSEG)
{
dsp = (DenseSegPtr)(sap->segs);
if (dsp->strands == NULL)
{
dsp->strands = (Uint1Ptr)MemNew((dsp->dim)*(dsp->numseg)*sizeof(Uint1));
for (i=0; i<(dsp->dim)*(dsp->numseg); i++)
{
dsp->strands[i] = Seq_strand_plus;
}
}
}
else if (sap->segtype == SAS_SPLICED)
{
return FALSE;
}
sap = sap_next;
}
return TRUE;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2IndexLite takes a seqalign or a list of seqaligns, converts
* each alignment to a dense-seg structure and indexes it, and then
* allocates an AMAlignIndex2 structure and fills in the saps array.
*
***************************************************************************/
NLM_EXTERN Boolean AlnMgr2IndexLite(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
Int4 i;
SAIndex2Ptr saip;
SeqAlignPtr salp;
if (sap == NULL)
return FALSE;
if (!AlnMgr2UnpackSeqAlign(sap))
return FALSE;
if (!AlnMgr2ConvertAllToDenseSeg((SeqAlignPtr)sap->segs))
return FALSE;
amaip = AMAlignIndex2New();
amaip->alnstyle = AM2_LITE;
salp = (SeqAlignPtr)(sap->segs);
while (salp != NULL)
{
amaip->numsaps++;
AlnMgr2IndexSingleChildSeqAlign(salp);
salp = salp->next;
}
amaip->saps = (SeqAlignPtr PNTR)MemNew((amaip->numsaps)*sizeof(SeqAlignPtr));
salp = (SeqAlignPtr)(sap->segs);
i = 0;
while (salp != NULL)
{
amaip->saps[i] = salp;
i++;
saip = (SAIndex2Ptr)(salp->saip);
saip->numinchain = i;
saip->top = sap;
salp = salp->next;
}
sap->saip = (SeqAlignIndexPtr)amaip;
amaip->aligned = (Boolean PNTR)MemNew((amaip->numsaps)*sizeof(Boolean));
for (i=0; i<amaip->numsaps; i++)
{
amaip->aligned[i] = TRUE;
}
return TRUE;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2IndexSeqAlign takes a seqalign of any type except std-seg and
* creates indexes on it for easy retrieval of useful information by other
* AlnMgr2 functions. If the seqalign is a single alignment, that alignment
* gets a simple index and is left alone otherwise. If the seqalign is
* a set of alignments or a dense-diag set, the subalignments get
* individually indexed and then are combined into a (fake) multiple
* alignment which also gets indexed. The subalignments can now be accessed
* as a multiple alignment by AlnMgr2 functions.
*
***************************************************************************/
NLM_EXTERN void AlnMgr2IndexSeqAlign(SeqAlignPtr sap)
{
AlnMgr2IndexSeqAlignEx(sap, TRUE);
}
NLM_EXTERN void AlnMgr2IndexSeqAlignEx(SeqAlignPtr sap, Boolean replace_gi)
{
AMAlignIndex2Ptr amaip;
AMIntervalSetPtr amint;
AMIntervalSetPtr amint_head;
AMEdgePtr edge;
AMEdgePtr edge_head;
Int4 i;
Int4 numvertices;
AMVertexPtr vertex_head;
AMVertexPtr PNTR vertexarray;
if (sap == NULL || sap->saip != NULL)
return;
if (replace_gi) {
SAM_ReplaceGI(sap);
}
if (sap->next == NULL && sap->segtype == SAS_DENSEG)
{
AlnMgr2IndexSingleChildSeqAlign(sap);
return;
}
AlnMgr2IndexLite(sap);
AlnMgr2DecomposeToPairwise(sap);
amaip = (AMAlignIndex2Ptr)(sap->saip);
amaip->alnstyle = AM2_FULLINDEX;
AlnMgr2HidePairwiseConflicts(sap);
amint_head = AlnMgr2MakeIntervals(sap);
vertex_head = NULL;
edge_head = NULL;
vertexarray = AlnMgr2MakeVerticesFromIntervals(sap, amint_head, &vertex_head, &edge_head, &numvertices);
while (amint_head != NULL)
{
amint = amint_head->next;
AMIntervalSetFree(amint_head);
amint_head = amint;
}
AlnMgr2UsePrimsAlgorithm(vertexarray, numvertices, edge_head);
AlnMgr2BuildAlignmentFromTree(vertexarray, numvertices, edge_head, sap);
for (i=0; i<numvertices; i++)
{
SeqIdFree(vertexarray[i]->sip);
MemFree(vertexarray[i]);
}
MemFree(vertexarray);
while (edge_head != NULL)
{
edge = edge_head->next;
MemFree(edge_head);
edge_head = edge;
}
amaip = (AMAlignIndex2Ptr)(sap->saip);
amaip->alnstyle = AM2_FULLINDEX;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2ReIndexSeqAlign takes an indexed alignment (that has, presumably,
* been changed), makes sure all child seqaligns are indexed (if they are
* already indexed they are not reindexed), and reindexes all the child
* seqaligns as a set.
*
***************************************************************************/
NLM_EXTERN void AlnMgr2ReIndexSeqAlign(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
AMIntervalSetPtr amint;
AMIntervalSetPtr amint_head;
AMEdgePtr edge_head;
Int4 i;
Int4 numvertices;
AMVertexPtr vertex_head;
AMVertexPtr PNTR vertexarray;
if (sap == NULL)
return;
if (sap->saip == NULL)
{
AlnMgr2IndexSeqAlign(sap);
return;
}
if (sap->saip->indextype == INDEX_CHILD)
return;
amaip = (AMAlignIndex2Ptr)(sap->saip);
for (i=0; i<amaip->numsaps; i++)
{
if (amaip->saps[i]->saip == NULL)
AlnMgr2IndexSingleChildSeqAlign(amaip->saps[i]);
}
if (amaip->alnstyle != AM2_LITE)
return;
AlnMgr2DecomposeToPairwise(sap);
AlnMgr2HidePairwiseConflicts(sap);
amint_head = AlnMgr2MakeIntervals(sap);
vertex_head = NULL;
edge_head = NULL;
vertexarray = AlnMgr2MakeVerticesFromIntervals(sap, amint_head, &vertex_head, &edge_head, &numvertices);
while (amint_head != NULL)
{
amint = amint_head->next;
AMIntervalSetFree(amint_head);
amint_head = amint;
}
AlnMgr2UsePrimsAlgorithm(vertexarray, numvertices, edge_head);
AlnMgr2BuildAlignmentFromTree(vertexarray, numvertices, edge_head, sap);
MemFree(vertexarray);
}
static int LIBCALLBACK AlnMgr2CompareByAnchor(VoidPtr ptr1, VoidPtr ptr2)
{
DenseSegPtr dsp;
int ret;
SAIndex2Ptr saip1;
SAIndex2Ptr saip2;
SeqAlignPtr sap1;
SeqAlignPtr sap2;
SeqIdPtr sip1;
SeqIdPtr sip2;
Int4 start1;
Int4 start2;
Int4 stop1;
Int4 stop2;
sap1 = *((SeqAlignPtr PNTR)ptr1);
sap2 = *((SeqAlignPtr PNTR)ptr2);
saip1 = (SAIndex2Ptr)(sap1->saip);
saip2 = (SAIndex2Ptr)(sap2->saip);
dsp = (DenseSegPtr)(sap1->segs);
if (saip1->tmp == 1)
sip1 = dsp->ids->next;
else
sip1 = dsp->ids;
dsp = (DenseSegPtr)(sap2->segs);
if (saip2->tmp == 1)
sip2 = dsp->ids->next;
else
sip2 = dsp->ids;
ret = AlnMgr2OrderSeqIds(sip1, sip2);
if (ret != 0)
return ret;
/* these share both ids -- put best first */
if (saip1->score == 0)
saip1->score = AlnMgr2ComputeScoreForSeqAlign(sap1);
if (saip2->score == 0)
saip2->score = AlnMgr2ComputeScoreForSeqAlign(sap2);
if (saip1->score > saip2->score)
return -1;
else if (saip1->score < saip2->score)
return 1;
AlnMgr2GetNthSeqRangeInSA(sap1, saip1->tmp, &start1, &stop1);
AlnMgr2GetNthSeqRangeInSA(sap2, saip2->tmp, &start2, &stop2);
if (start1 < start2)
return -1;
else if (start1 > start2)
return 1;
else if (stop1 > stop2)
return -1;
else if (stop1 < stop2)
return 1;
return 0;
}
/* SECTION 2c */
NLM_EXTERN Boolean AlnMgr2IndexAsRows(SeqAlignPtr sap, Uint1 strand, Boolean truncate)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
DenseSegPtr dsp_tmp;
Boolean found;
Int4 i;
Boolean impossible;
Int4 numsaps;
SAIndex2Ptr saip;
SeqAlignPtr salp;
SeqAlignPtr sap_head;
SeqAlignPtr sap_prev;
SeqAlignPtr sap_tmp;
SeqAlignPtr PNTR saparray;
SeqAlignPtr set_head;
SeqAlignPtr set_prev;
SeqIdPtr sharedsip;
SeqIdPtr sip;
SeqIdPtr sip_next;
SeqIdPtr sip_tmp;
Int4 tmp;
if (sap == NULL)
return FALSE;
if (sap->saip != NULL)
AMAlignIndexFreeEitherIndex(sap);
AlnMgr2IndexLite(sap);
AlnMgr2DecomposeToPairwise(sap);
/* need to figure out which row is shared by all saps */
sap_tmp = (SeqAlignPtr)(sap->segs);
dsp = (DenseSegPtr)(sap_tmp->segs);
sip = dsp->ids;
found = FALSE;
while (!found && sip != NULL)
{
sap_tmp = (SeqAlignPtr)(sap->segs);
sip_next = sip->next;
sip->next = NULL;
impossible = FALSE;
while (!impossible && sap_tmp != NULL)
{
dsp_tmp = (DenseSegPtr)(sap_tmp->segs);
if (AlnMgr2SeqIdListsOverlap(sip, dsp_tmp->ids) == NULL)
impossible = TRUE;
sap_tmp = sap_tmp->next;
}
sip->next = sip_next;
if (!impossible) /* found one that matched a row in every alignment */
found = TRUE;
else
sip = sip_next;
}
if (!found) /* didn't find a seqid that was contained in all alignments */
return FALSE;
/* mark the shared row to make things easier */
sharedsip = SeqIdDup(sip);
sap_tmp = (SeqAlignPtr)(sap->segs);
i = 0;
while (sap_tmp != NULL)
{
saip = (SAIndex2Ptr)(sap_tmp->saip);
dsp_tmp = (DenseSegPtr)(sap_tmp->segs);
if (SeqIdComp(sharedsip, dsp_tmp->ids) == SIC_YES)
saip->tmp = 1;
else
saip->tmp = 2;
sap_tmp = sap_tmp->next;
i++;
}
saparray = (SeqAlignPtr PNTR)MemNew(i*sizeof(SeqAlignPtr));
sap_tmp = (SeqAlignPtr)(sap->segs);
i = 0;
while (sap_tmp != NULL)
{
saparray[i] = sap_tmp;
i++;
sap_tmp = sap_tmp->next;
}
numsaps = i;
HeapSort(saparray, i, sizeof(SeqAlignPtr), AlnMgr2CompareByAnchor);
/* now each clump of alignments is a row -- need to eliminate overlaps next */
sip = NULL;
i = 0;
sap_head = sap_prev = NULL;
while (i<numsaps)
{
saparray[i]->next = NULL;
set_head = set_prev = saparray[i];
saip = (SAIndex2Ptr)(saparray[i]->saip);
sip = AlnMgr2GetNthSeqIdPtr(saparray[i], 3-saip->tmp); /* get other seqid */
i++;
if (i<numsaps)
sip_tmp = AlnMgr2GetNthSeqIdPtr(saparray[i], 3-saip->tmp);
while (i<numsaps && SeqIdComp(sip, sip_tmp) == SIC_YES)
{
set_prev->next = saparray[i];
set_prev = saparray[i];
saparray[i]->next = NULL;
i++;
SeqIdFree(sip_tmp);
if (i<numsaps)
sip_tmp = AlnMgr2GetNthSeqIdPtr(saparray[i], 3-saip->tmp);
}
AlnMgr2IndexLite(set_head);
if (!truncate)
AlnMgr2RemoveInconsistentAlnsFromSet(set_head, 0);
else
AlnMgr2RemoveInconsistentAlnsFromSet(set_head, -1);
sap_tmp = (SeqAlignPtr)(set_head->segs);
while (sap_tmp != NULL)
{
saip = (SAIndex2Ptr)(sap_tmp->saip);
dsp_tmp = (DenseSegPtr)(sap_tmp->segs);
if (SeqIdComp(sharedsip, dsp_tmp->ids) == SIC_YES)
saip->tmp = 1;
else
saip->tmp = 2;
sap_tmp = sap_tmp->next;
}
if (sap_head != NULL)
sap_prev->next = set_head;
else
sap_head = sap_prev = set_head;
while (sap_prev->next != NULL)
{
sap_prev = sap_prev->next;
}
sap_prev->next = NULL;
}
/* now we have lots of freed pointers sitting in the array */
MemFree(saparray);
saparray = NULL;
/* sap_head is the head of a chain of LITE-indexed alignments, each of which is one row */
/* first make sure that the shared row is on the requested strand */
sap_tmp = sap_head;
if (strand == Seq_strand_both || strand == Seq_strand_unknown || strand == 0)
strand = Seq_strand_plus;
while (sap_tmp != NULL)
{
salp = (SeqAlignPtr)(sap_tmp->segs);
saip = (SAIndex2Ptr)(salp->saip);
/* strand is same for all children */
if (AlnMgr2GetNthStrand(salp, saip->tmp) != strand)
{
SeqAlignListReverseStrand(salp);
while (salp != NULL)
{
saip = (SAIndex2Ptr)salp->saip;
tmp = saip->tmp;
SAIndex2Free2(salp->saip);
salp->saip = NULL;
AlnMgr2IndexSingleChildSeqAlign(salp);
saip = (SAIndex2Ptr)salp->saip;
saip->tmp = tmp;
salp = salp->next;
}
}
sap_tmp = sap_tmp->next;
}
sap_tmp = sap_head;
sap->segs = NULL;
AMAlignIndex2Free2(sap->saip);
sap->saip = (SeqAlignIndexPtr)AMAlignIndex2New();
amaip = (AMAlignIndex2Ptr)(sap->saip);
amaip->alnstyle = AM2_FULLINDEX;
set_head = set_prev = NULL;
while (sap_tmp != NULL)
{
salp = (SeqAlignPtr)(sap_tmp->segs);
while (salp != NULL)
{
AlnMgr2AddInNewPairwiseSA(sap, salp);
if (set_head != NULL)
{
set_prev->next = salp;
set_prev = salp;
} else
set_head = set_prev = salp;
salp = salp->next;
}
sap_tmp->segs = NULL;
sap_tmp = sap_tmp->next;
}
AlnMgr2CondenseColumns((DenseSegPtr)(amaip->sharedaln->segs));
AlnMgr2IndexSingleChildSeqAlign(amaip->sharedaln);
set_prev->next = NULL;
sap->segs = (Pointer)(set_head);
SeqAlignListFree(sap_head);
SeqIdFree(sharedsip);
return TRUE;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2IndexIndexedChain takes a linked list of indexed seqaligns
* and does an in-place transformation to an indexed parent-child
* seqalign set.
*
***************************************************************************/
NLM_EXTERN void AlnMgr2IndexIndexedChain(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
AMIntervalSetPtr amint;
AMIntervalSetPtr amint_head;
AMEdgePtr edge_head;
Int4 numvertices;
AMVertexPtr vertex_head;
AMVertexPtr PNTR vertexarray;
if (sap == NULL || sap->saip == NULL || sap->saip->indextype != INDEX_CHILD)
return;
AlnMgr2IndexLite(sap);
AlnMgr2DecomposeToPairwise(sap);
amaip = (AMAlignIndex2Ptr)(sap->saip);
amaip->alnstyle = AM2_FULLINDEX;
AlnMgr2HidePairwiseConflicts(sap);
amint_head = AlnMgr2MakeIntervals(sap);
vertex_head = NULL;
edge_head = NULL;
vertexarray = AlnMgr2MakeVerticesFromIntervals(sap, amint_head, &vertex_head, &edge_head, &numvertices);
while (amint_head != NULL)
{
amint = amint_head->next;
AMIntervalSetFree(amint_head);
amint_head = amint;
}
AlnMgr2UsePrimsAlgorithm(vertexarray, numvertices, edge_head);
AlnMgr2BuildAlignmentFromTree(vertexarray, numvertices, edge_head, sap);
MemFree(vertexarray);
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2DecomposeToPairwise takes a parent seqalign and goes through all
* its children, checking their dimensions. If a child seqalign is found
* with dimension greater than 2, that alignment is copied into a set of
* two-row alignments, each new alignment containing the first row of the
* original alignment and a different row. This function does NOT take out
* segs with only gaps (is this a problem????). The resulting seqaligns
* are all individually indexed and then the whole set is indexed lite.
*
***************************************************************************/
static void AlnMgr2DecomposeToPairwise(SeqAlignPtr sap)
{
DenseSegPtr dsp;
DenseSegPtr dsp_orig;
Int4 i;
Int4 j;
Int4 n;
SAIndex2Ptr saip;
SAIndex2Ptr saip_orig;
SeqAlignPtr salp;
SeqAlignPtr salp_new;
SeqAlignPtr salp_next;
SeqAlignPtr salp_prev;
if (sap == NULL || sap->saip == NULL || sap->saip->indextype != INDEX_PARENT)
return;
salp = (SeqAlignPtr)(sap->segs);
salp_prev = NULL;
while (salp)
{
n = AlnMgr2GetNumRows(salp);
if (n > 2)
{
salp_next = salp->next;
saip_orig = (SAIndex2Ptr)(salp->saip);
for (i=2; i<=n; i++)
{
salp_new = SeqAlignNew();
dsp_orig = (DenseSegPtr)(salp->segs);
dsp = DenseSegNew();
dsp->dim = 2;
dsp->numseg = dsp_orig->numseg;
dsp->ids = AlnMgr2GetNthSeqIdPtr(salp, 1);
dsp->ids->next = AlnMgr2GetNthSeqIdPtr(salp, i);
dsp->starts = (Int4Ptr)MemNew(dsp->numseg*2*sizeof(Int4));
dsp->lens = (Int4Ptr)MemNew(dsp->numseg*sizeof(Int4));
dsp->strands = (Uint1Ptr)MemNew(dsp->numseg*2*sizeof(Uint1));
for (j=0; j<dsp->numseg; j++)
{
dsp->lens[j] = dsp_orig->lens[j];
dsp->starts[2*j] = dsp_orig->starts[dsp_orig->dim*j];
dsp->starts[2*j+1] = dsp_orig->starts[dsp_orig->dim*j+i-1];
dsp->strands[2*j] = dsp_orig->strands[dsp_orig->dim*j];
dsp->strands[2*j+1] = dsp_orig->strands[dsp_orig->dim*j+i-1];
}
salp_new = SeqAlignNew();
salp_new->dim = 2;
salp_new->segs = (Pointer)dsp;
salp_new->segtype = SAS_DENSEG;
AlnMgr2IndexSingleChildSeqAlign(salp_new);
saip = (SAIndex2Ptr)(salp_new->saip);
saip->numinchain = saip_orig->numinchain;
saip->numsplitaln = i-1;
if (salp_prev == NULL)
{
salp_prev = salp_new;
sap->segs = (Pointer)salp_new;
} else
{
salp_prev->next = salp_new;
salp_prev = salp_new;
}
}
salp_prev->next = salp_next;
salp->next = NULL;
SeqAlignFree(salp);
salp = salp_next;
} else
{
salp_prev = salp;
salp = salp->next;
}
}
AMAlignIndex2Free2(sap->saip);
sap->saip = NULL;
AlnMgr2IndexLite(sap);
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2HidePairwiseConflicts looks through a set of indexed seqaligns
* to find pairs of alignments that share the same seqids and that provide
* conflicting information. These pairs are then sent to AlnMgr2TossWorse,
* which hides the worse alignment by unaligning it. Note that the hidden
* alignments are not destroyed and are not taken out of the set.
*
***************************************************************************/
static void AlnMgr2HidePairwiseConflicts(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
Int4 i;
Boolean inset;
Int4 j;
Boolean match;
SeqIdPtr sip11;
SeqIdPtr sip12;
SeqIdPtr sip21;
SeqIdPtr sip22;
Int4 start11;
Int4 start12;
Int4 start21;
Int4 start22;
Int4 stop11;
Int4 stop12;
Int4 stop21;
Int4 stop22;
Int4Ptr tossed;
if (sap == NULL || sap->saip == NULL || sap->saip->indextype != INDEX_PARENT)
return;
amaip = (AMAlignIndex2Ptr)(sap->saip);
AlnMgr2SortBySeqId(sap);
tossed = (Int4Ptr)MemNew(amaip->numsaps*sizeof(Int4));
for (i=0; i<amaip->numsaps-1; i++)
{
for (j=0; j<amaip->numsaps; j++)
{
tossed[j] = 0;
}
inset = TRUE;
for (j=i+1; amaip->aligned[i] && j<amaip->numsaps && inset == TRUE; j++)
{
if (tossed[j] == 0 && amaip->aligned[i] && amaip->aligned[j])
{
sip11 = AlnMgr2GetNthSeqIdPtr(amaip->saps[i], 1);
sip12 = AlnMgr2GetNthSeqIdPtr(amaip->saps[i], 2);
sip21 = AlnMgr2GetNthSeqIdPtr(amaip->saps[j], 1);
sip22 = AlnMgr2GetNthSeqIdPtr(amaip->saps[j], 2);
match = FALSE;
if (SeqIdComp(sip11, sip21) == SIC_YES && SeqIdComp(sip12, sip22) == SIC_YES)
{
match = TRUE;
AlnMgr2GetNthSeqRangeInSA(amaip->saps[i], 1, &start11, &stop11);
AlnMgr2GetNthSeqRangeInSA(amaip->saps[i], 2, &start12, &stop12);
AlnMgr2GetNthSeqRangeInSA(amaip->saps[j], 1, &start21, &stop21);
AlnMgr2GetNthSeqRangeInSA(amaip->saps[j], 2, &start22, &stop22);
} else if (SeqIdComp(sip11, sip22) == SIC_YES && SeqIdComp(sip12, sip21) == SIC_YES)
{
match = TRUE;
AlnMgr2GetNthSeqRangeInSA(amaip->saps[i], 1, &start11, &stop11);
AlnMgr2GetNthSeqRangeInSA(amaip->saps[i], 2, &start12, &stop12);
AlnMgr2GetNthSeqRangeInSA(amaip->saps[j], 2, &start21, &stop21);
AlnMgr2GetNthSeqRangeInSA(amaip->saps[j], 1, &start22, &stop22);
} else if (SeqIdComp(sip11, sip21) != SIC_YES && SeqIdComp(sip11, sip22) != SIC_YES)
inset = FALSE;
if (match == TRUE)
{
if ((start11 < start21 && stop11 > stop21) || (start11 < stop21 && stop11 > stop21) || (start11 > start21 && stop11 < stop21))
{
AlnMgr2TossWorse(sap, i, j);
if (amaip->aligned[j] == 0) /* j just got tossed -- put it in the list */
tossed[j] = 1;
}
}
SeqIdFree(sip11);
SeqIdFree(sip12);
SeqIdFree(sip21);
SeqIdFree(sip22);
}
}
if (amaip->aligned[i] == 0) /* the query alignment got tossed -- restore */
{ /* all the ones that it tossed out */
for (j=0; j<amaip->numsaps; j++)
{
if (tossed[j] == 1)
amaip->aligned[j] = 1;
}
}
}
MemFree(tossed);
}
/* SECTION 2c */
static void AlnMgr2SortBySeqId(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
Int4 i;
SAIndex2Ptr saip;
amaip = (AMAlignIndex2Ptr)(sap->saip);
for (i=0; i<amaip->numsaps; i++)
{
saip = (SAIndex2Ptr)(amaip->saps[i]->saip);
saip->aligned = amaip->aligned[i];
}
HeapSort(amaip->saps, amaip->numsaps, sizeof(amaip->saps), AlnMgr2CompareIds);
for (i=0; i<amaip->numsaps; i++)
{
saip = (SAIndex2Ptr)(amaip->saps[i]->saip);
amaip->aligned[i] = saip->aligned;
}
}
/* SECTION 2c */
static int LIBCALLBACK AlnMgr2CompareIds(VoidPtr ptr1, VoidPtr ptr2)
{
Int4 ret;
SAIndex2Ptr saip1;
SAIndex2Ptr saip2;
SeqAlignPtr sap1;
SeqAlignPtr sap2;
SeqIdPtr sip1;
SeqIdPtr sip2;
if (ptr1 == NULL || ptr2 == NULL)
return 0;
sap1 = *((SeqAlignPtr PNTR) ptr1);
sap2 = *((SeqAlignPtr PNTR) ptr2);
sip1 = AlnMgr2GetNthSeqIdPtr(sap1, 1);
sip2 = AlnMgr2GetNthSeqIdPtr(sap2, 1);
ret = (AlnMgr2OrderSeqIds(sip1, sip2));
SeqIdFree(sip1);
SeqIdFree(sip2);
if (ret != 0)
return ret;
saip1 = (SAIndex2Ptr)(sap1->saip);
saip2 = (SAIndex2Ptr)(sap2->saip);
if (saip1->score == 0)
saip1->score = AlnMgr2ComputeScoreForSeqAlign(sap1);
if (saip2->score == 0)
saip2->score = AlnMgr2ComputeScoreForSeqAlign(sap2);
if (saip1->score > saip2->score)
return -1;
if (saip1->score < saip2->score)
return 1;
return 0;
}
/* SECTION 2c */
/***************************************************************************
*
* Given an indexed seqalign set, AlnMgr2TossWorse looks at the indicated
* pair of seqaligns, gets their scores, and sets the unaligned bit of the
* seqalign with the worse score.
*
***************************************************************************/
static void AlnMgr2TossWorse(SeqAlignPtr sap, Int4 i, Int4 j)
{
AMAlignIndex2Ptr amaip;
SAIndex2Ptr saip1;
SAIndex2Ptr saip2;
Int4 score1;
Int4 score2;
amaip = (AMAlignIndex2Ptr)(sap->saip);
saip1 = (SAIndex2Ptr)(amaip->saps[i]->saip);
saip2 = (SAIndex2Ptr)(amaip->saps[j]->saip);
if (saip1->score == 0)
saip1->score = score1 = AlnMgr2ComputeScoreForSeqAlign(amaip->saps[i]);
else
score1 = saip1->score;
if (saip1->score == 0)
saip2->score = score2 = AlnMgr2ComputeScoreForSeqAlign(amaip->saps[j]);
else
score2 = saip2->score;
if (score1 >= score2)
amaip->aligned[j] = FALSE;
else if (score2 > score1)
amaip->aligned[i] = FALSE;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2MakeIntervals takes every row from every seqalign and bins it
* with other sequences with the same seqid and the same strand.
*
***************************************************************************/
static AMIntervalSetPtr AlnMgr2MakeIntervals(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
AMIntervalSetPtr amint;
AMIntervalSetPtr amint_head;
AMIntervalSetPtr amint_prev;
Boolean found;
Int4 i;
AMIntervalPtr intv;
AMIntervalPtr int_prev;
Int4 j;
Int4 k;
SeqIdPtr sip;
Uint1 strand;
if (sap == NULL || sap->saip == NULL || sap->saip->indextype != INDEX_PARENT)
return NULL;
amaip = (AMAlignIndex2Ptr)(sap->saip);
amint_head = amint_prev = NULL;
for (i=0; i<amaip->numsaps; i++)
{
if (amaip->aligned[i])
{
j = AlnMgr2GetNumRows(amaip->saps[i]);
for (k=0; k<j; k++)
{
intv = (AMIntervalPtr)MemNew(sizeof(AMInterval));
AlnMgr2GetNthSeqRangeInSA(amaip->saps[i], k+1, &(intv->from), &(intv->to));
sip = AlnMgr2GetNthSeqIdPtr(amaip->saps[i], k+1);
strand = AlnMgr2GetNthStrand(amaip->saps[i], k+1);
if (strand != Seq_strand_minus)
strand = Seq_strand_plus; /* to avoid dealing with Seq_strand_unknown */
intv->strand = strand;
if (amint_head != NULL) /* figure out which interval set this goes in */
{
amint = amint_head;
found = FALSE;
while (amint != NULL && !found)
{
if (SeqIdComp(sip, amint->sip) == SIC_YES && strand == amint->strand)
found = TRUE;
else
amint = amint->next;
}
if (found) /* add this to the interval set matched */
{
int_prev = amint->int_head;
while (int_prev->next != NULL)
{
int_prev = int_prev->next;
}
int_prev->next = intv;
} else /* make a new interval set */
{
amint = (AMIntervalSetPtr)MemNew(sizeof(AMIntervalSet));
amint->sip = SeqIdDup(sip);
amint->strand = strand;
amint->int_head = intv;
amint_prev = amint_head;
while (amint_prev->next != NULL)
{
amint_prev = amint_prev->next;
}
amint_prev->next = amint;
}
} else /* make a new interval set */
{
amint = (AMIntervalSetPtr)MemNew(sizeof(AMIntervalSet));
amint->sip = SeqIdDup(sip);
amint->strand = strand;
amint->int_head = intv;
amint_head = amint;
}
SeqIdFree(sip);
}
}
}
return amint_head;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2MakeVerticesFromIntervals takes the set of intervals created from
* the alignments, and makes nonoverlapping vertices. Each vertex is a
* single seqid plus a start and stop (so one seqid may have more than one
* vertex). Each vertex is also associated with edges, or alignments, which
* link the vertices together. An edge is simply two vertices plus a weight,
* which is the alignment quality score. This function creates the vertices,
* then creates the edges, and sorts the edges and vertices by quality and
* by number of edges per vertex.
*
***************************************************************************/
static AMVertexPtr PNTR AlnMgr2MakeVerticesFromIntervals(SeqAlignPtr sap, AMIntervalSetPtr amint_head, AMVertexPtr PNTR vertexhead, AMEdgePtr PNTR edgehead, Int4Ptr numvertices)
{
AMAlignIndex2Ptr amaip;
AMIntervalSetPtr amint;
AMEdgePtr edge;
AMEdgePtr edge_head;
AMEdgePtr edge_prev;
Int4 i;
AMIntervalPtr intv;
Int4 j;
Int4 k;
Int4 n;
SAIndex2Ptr saip;
SeqIdPtr sip1;
SeqIdPtr sip2;
Int4 start;
Int4 stop;
Int4 v1;
Int4 v2;
AMVertexPtr vertex;
AMVertexPtr vertex_head;
AMVertexPtr vertex_prev;
AMVertexPtr PNTR vertexarray;
if (sap == NULL || sap->saip == NULL || sap->saip->indextype != INDEX_PARENT)
return NULL;
amint = amint_head;
vertex_head = vertex_prev = NULL;
while (amint != NULL)
{
AlnMgr2SortIntervals(amint);
vertex = (AMVertexPtr)MemNew(sizeof(AMVertex));
intv = amint->int_head;
vertex->sip = SeqIdDup(amint->sip);
vertex->strand = amint->strand;
vertex->from = intv->from;
vertex->to = intv->to;
intv = intv->next;
while (intv != NULL)
{
if ((intv->from <= vertex->to && intv->from >= vertex->from) || (intv->to <= vertex->to && intv->to >= vertex->from))
{
if (intv->from < vertex->from)
vertex->from = intv->from;
if (intv->to > vertex->to)
vertex->to = intv->to;
} else
{
if (vertex_head != NULL)
{
vertex_prev->next = vertex;
vertex_prev = vertex;
} else
vertex_head = vertex_prev = vertex;
vertex = (AMVertexPtr)MemNew(sizeof(AMVertex));
vertex->from = intv->from;
vertex->to = intv->to;
vertex->sip = SeqIdDup(amint->sip);
vertex->strand = amint->strand;
}
intv = intv->next;
}
if (vertex_head != NULL)
{
vertex_prev->next = vertex;
vertex_prev = vertex;
} else
vertex_head = vertex_prev = vertex;
amint = amint->next;
}
vertex = vertex_head;
i = 0;
while (vertex != NULL)
{
i++;
vertex = vertex->next;
}
vertexarray = (AMVertexPtr PNTR)MemNew(i*sizeof(AMVertexPtr));
*numvertices = i;
vertex = vertex_head;
i = 0;
while (vertex != NULL)
{
vertexarray[i] = vertex;
vertex = vertex->next;
i++;
}
amaip = (AMAlignIndex2Ptr)(sap->saip);
/* now make the edges from the alignments */
edge_head = NULL;
for (i=0; i<amaip->numsaps; i++)
{
if (amaip->aligned[i])
{
j = AlnMgr2GetNumRows(amaip->saps[i]);
for (k=0; k<j; k++)
{
sip1 = AlnMgr2GetNthSeqIdPtr(amaip->saps[i], k+1);
AlnMgr2GetNthSeqRangeInSA(amaip->saps[i], k+1, &start, &stop);
v1 = AlnMgr2MatchToVertex(sip1, start, stop, vertexarray, *numvertices);
for (n=k+1; n<j; n++)
{
vertexarray[v1]->numedges++;
sip2 = AlnMgr2GetNthSeqIdPtr(amaip->saps[i], n+1);
AlnMgr2GetNthSeqRangeInSA(amaip->saps[i], n+1, &start, &stop);
v2 = AlnMgr2MatchToVertex(sip2, start, stop, vertexarray, *numvertices);
vertexarray[v2]->numedges++;
edge = (AMEdgePtr)MemNew(sizeof(AMEdge));
edge->vertex1 = v1;
edge->vertex2 = v2;
saip = NULL;
if (amaip->saps[i]->saip != NULL)
saip = (SAIndex2Ptr)(amaip->saps[i]->saip);
if (saip != NULL && saip->score != 0)
edge->weight = saip->score;
else
edge->weight = AlnMgr2ComputeScoreForSeqAlign(amaip->saps[i]);
edge->sap = amaip->saps[i];
edge->used = 0;
if (edge_head != NULL)
{
edge_prev->next = edge;
edge_prev = edge;
} else
edge_head = edge_prev = edge;
SeqIdFree(sip2);
}
SeqIdFree(sip1);
}
}
}
AlnMgr2SortEdgesByWeight(&edge_head);
*vertexhead = vertexarray[0];
*edgehead = edge_head;
return vertexarray;
}
/* SECTION 2C */
/***************************************************************************
*
* AlnMgr2SortVerticesByNumEdges -- the name says it all -- each vertex is
* associated with one or more edges and the most populated vertices get
* put first.
*
***************************************************************************/
static void AlnMgr2SortVerticesByNumEdges(AMVertexPtr PNTR vertexarray, Int4 numvertices)
{
Int4 i;
HeapSort(vertexarray, numvertices, sizeof(vertexarray), AlnMgr2CompareVertices);
for (i=0; i<numvertices-1; i++)
{
vertexarray[i]->next = vertexarray[i+1];
}
vertexarray[numvertices-1]->next = NULL;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2CompareVertices is the HeapSort callback for
* AlnMgr2SortVerticesByNumEdges.
*
***************************************************************************/
static int LIBCALLBACK AlnMgr2CompareVertices(VoidPtr ptr1, VoidPtr ptr2)
{
AMVertexPtr vertex1;
AMVertexPtr vertex2;
if (ptr1 != NULL && ptr2 != NULL)
{
vertex1 = *((AMVertexPtr PNTR)ptr1);
vertex2 = *((AMVertexPtr PNTR)ptr2);
if (vertex1->numedges > vertex2->numedges)
return -1;
else if (vertex1->numedges < vertex2->numedges)
return 1;
else
return 0;
}
return 0;
}
/* SECTION 2C */
/***************************************************************************
*
* AlnMgr2SortEdgesByWeight takes a set of edges (alignments) and sorts
* them by their preset weights (alignment scores), using AlnMgr2CompareEdges
* as its HeapSort callback.
*
***************************************************************************/
static void AlnMgr2SortEdgesByWeight(AMEdgePtr PNTR edge_head)
{
AMEdgePtr edge;
AMEdgePtr PNTR edgearray;
Int4 i;
Int4 j;
if (edge_head == NULL || *edge_head == NULL)
return;
edge = *edge_head;
i = 0;
while (edge != NULL)
{
i++;
edge = edge->next;
}
edgearray = (AMEdgePtr PNTR)MemNew(i*sizeof(AMEdgePtr));
edge = *edge_head;
i = 0;
while (edge != NULL)
{
edgearray[i] = edge;
edge = edge->next;
i++;
}
HeapSort(edgearray, i, sizeof(edgearray), AlnMgr2CompareEdges);
for (j=0; j<i-1; j++)
{
edgearray[j]->next = edgearray[j+1];
}
edgearray[i-1]->next = NULL;
*edge_head = edgearray[0];
MemFree(edgearray);
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2CompareEdges is the HeapSort callback for AlnMgr2SortEdgesByWeight.
* It simply compares the preset edge weights.
*
***************************************************************************/
static int LIBCALLBACK AlnMgr2CompareEdges(VoidPtr ptr1, VoidPtr ptr2)
{
AMEdgePtr edge1;
AMEdgePtr edge2;
if (ptr1 != NULL && ptr2 != NULL)
{
edge1 = *((AMEdgePtr PNTR)ptr1);
edge2 = *((AMEdgePtr PNTR)ptr2);
if (edge1->weight > edge2->weight)
return -1;
else if (edge1->weight < edge2->weight)
return 1;
else
return 0;
}
return 0;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2MatchToVertex is called by AlnMgr2MakeVerticesFromIntervals to
* figure out which vertex in the array the seqid, start, and stop match to.
*
***************************************************************************/
static Int4 AlnMgr2MatchToVertex(SeqIdPtr sip, Int4 start, Int4 stop, AMVertexPtr PNTR vertexarray, Int4 numvertices)
{
Int4 i;
if (sip == NULL || vertexarray == NULL)
return -1;
i = 0;
while (i<numvertices)
{
if (SeqIdComp(sip, vertexarray[i]->sip) == SIC_YES)
{
if (start >= vertexarray[i]->from && start <= vertexarray[i]->to && stop >= vertexarray[i]->from && stop <= vertexarray[i]->to)
return i;
}
i++;
}
return -1;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2SortIntervals sorts the AMIntervals by start position within the
* set, calling AlnMgr2CompareIntervals in a HeapSort.
*
***************************************************************************/
static void AlnMgr2SortIntervals(AMIntervalSetPtr amint)
{
Int4 i;
AMIntervalPtr PNTR intarray;
AMIntervalPtr intv;
AMIntervalPtr intv_head;
Int4 j;
i = 0;
intv = amint->int_head;
while (intv != NULL)
{
i++;
intv = intv->next;
}
intarray = (AMIntervalPtr PNTR)MemNew(i*sizeof(AMIntervalPtr));
intv = amint->int_head;
i = 0;
while (intv != NULL)
{
intarray[i] = intv;
intv = intv->next;
i++;
}
HeapSort(intarray, i, sizeof(intarray), AlnMgr2CompareIntervals);
intv_head = intv = intarray[0];
for (j=1; j<i; j++)
{
intv->next = intarray[j];
intarray[j]->next = NULL;
intv = intv->next;
}
amint->int_head = intv_head;
MemFree(intarray);
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2CompareIntervals is the HeapSort callback for
* AlnMgr2SortIntervals, which sorts a set of AMIntervals by start position.
*
***************************************************************************/
static int LIBCALLBACK AlnMgr2CompareIntervals(VoidPtr ptr1, VoidPtr ptr2)
{
AMIntervalPtr intv1;
AMIntervalPtr intv2;
if (ptr1 != NULL && ptr2 != NULL)
{
intv1 = *((AMIntervalPtr PNTR)ptr1);
intv2 = *((AMIntervalPtr PNTR)ptr2);
if (intv1->from > intv2->from)
return 1;
else if (intv1->from < intv2->from)
return -1;
else
{
if (intv1->to > intv2->to)
return 1;
else
return -1;
}
}
return 0;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2UsePrimsAlgorithm takes the set of edges and vertices produced by
* earlier functions and creates a subset of edges that can be made into
* a multiple alignment.
*
***************************************************************************/
static void AlnMgr2UsePrimsAlgorithm(AMVertexPtr PNTR vertexarray, Int4 numvertices, AMEdgePtr edge_head)
{
if (vertexarray == NULL || edge_head == NULL)
return;
edge_head->used = AM_USED;
vertexarray[edge_head->vertex1]->used = TRUE;
vertexarray[edge_head->vertex2]->used = TRUE;
AlnMgr2RecursePrims(vertexarray, edge_head);
AlnMgr2CleanUpLeftovers(vertexarray, numvertices, edge_head);
return;
}
/* SECTION 2C */
static AMEdgePtr AlnMgr2GetEdgeList(Int4 vertexnum, AMEdgePtr edge_head, AMEdgePtr already_used)
{
AMEdgePtr edge;
AMEdgePtr list;
AMEdgePtr list_head;
AMEdgePtr list_prev;
edge = edge_head;
list_head = NULL;
while (edge != NULL)
{
if ((edge->vertex1 == vertexnum || edge->vertex2 == vertexnum) && edge != already_used)
{
list = (AMEdgePtr)MemNew(sizeof(AMEdge));
list->vertex1 = edge->vertex1;
list->vertex2 = edge->vertex2;
list->weight = edge->weight;
list->used = edge->used;
if (list_head != NULL)
{
list_prev->next = list;
list_prev = list;
} else
list_head = list_prev = list;
}
edge = edge->next;
}
return list_head;
}
/* SECTION 2C */
/***************************************************************************
*
* AlnMgr2GetBetterVertex returns the vertex of the edge indicated that
* is shared by the largest number of other edges.
*
***************************************************************************/
static AMVertexPtr AlnMgr2GetBetterVertex(AMVertexPtr PNTR vertexarray, AMEdgePtr edge)
{
if (vertexarray[edge->vertex1]->numedges >= vertexarray[edge->vertex2]->numedges)
return vertexarray[edge->vertex1];
else
return vertexarray[edge->vertex2];
}
/* SECTION 2C */
/***************************************************************************
*
* AlnMgr2RecursePrims is a simple yet powerful algorithm that builds a
* minimal spanning tree of the edges and vertexes by starting with a set
* of edges and vertices, picking the best/shortest edge, then picking
* other edges one by one that join a vertex in the set with a vertex not
* in the set, until all edges are used (or deemed impossible).
*
***************************************************************************/
static void AlnMgr2RecursePrims(AMVertexPtr PNTR vertexarray, AMEdgePtr edge_head)
{
AMEdgePtr edge;
Boolean found;
edge = edge_head;
found = FALSE;
/* find an edge that isn't used, that joins a vertex in the set */
/* with a vertex outside the set, and add it and the new vertex */
while (edge != NULL && !found)
{
if (edge->used == AM_NOTUSED)
{
if (vertexarray[edge->vertex1]->used != vertexarray[edge->vertex2]->used)
{
found = TRUE;
vertexarray[edge->vertex1]->used = TRUE;
vertexarray[edge->vertex2]->used = TRUE;
edge->used = AM_USED;
AlnMgr2RecursePrims(vertexarray, edge_head);
}
}
edge = edge->next;
}
}
/* SECTION 2C */
/***************************************************************************
*
* AlnMgr2CleanUpLeftovers takes the edges that are unused after
* AlnMgr2RecursePrims and looks for edges that duplicate another edge in
* the set or edges that share a seqid (but not a vertex) with another edge
* already in the set. It adds these edges to the set; they don't belong
* there in tree-based terms but as alignments they are related.
*
***************************************************************************/
static void AlnMgr2CleanUpLeftovers(AMVertexPtr PNTR vertexarray, Int4 numvertices, AMEdgePtr edge_head)
{
AMEdgePtr edge;
AMEdgePtr edge_tmp;
Boolean found;
Int4 i;
BoolPtr tmpverts;
tmpverts = (BoolPtr)MemNew(numvertices*sizeof(Boolean));
for (i=0; i<numvertices; i++)
{
tmpverts[i] = vertexarray[i]->used;
}
edge = edge_head;
while (edge != NULL)
{
if (edge->used == AM_NOTUSED)
{
if (tmpverts[edge->vertex1] == TRUE && tmpverts[edge->vertex2] == TRUE)
{
/* see if this edge duplicates another edge; if so, add it */
edge_tmp = edge_head;
found = FALSE;
while (edge_tmp != NULL && !found)
{
if ((edge->vertex1 == edge_tmp->vertex1 && edge->vertex2 == edge_tmp->vertex2) || (edge->vertex1 == edge_tmp->vertex2 && edge->vertex2 == edge_tmp->vertex1))
{
found = TRUE;
edge->used = AM_USED;
}
edge_tmp = edge_tmp->next;
}
if (!found)
edge->used = AM_CONFLICT;
} else if (tmpverts[edge->vertex1] == FALSE && tmpverts[edge->vertex2] == FALSE)
{
/* if one of the vertices shares a seqid with a vertex in the set, put both vertices */
/* and the edge in the set. */
found = FALSE;
for (i=0; i<numvertices && !found; i++)
{
if (tmpverts[i] == TRUE && (SeqIdComp(vertexarray[i]->sip, vertexarray[edge->vertex1]->sip) == SIC_YES || SeqIdComp(vertexarray[i]->sip, vertexarray[edge->vertex2]->sip) == SIC_YES))
{
found = TRUE;
vertexarray[edge->vertex1]->used = TRUE;
vertexarray[edge->vertex2]->used = TRUE;
edge->used = AM_USED;
}
}
if (!found)
edge->used = AM_CONFLICT;
}
}
edge = edge->next;
}
MemFree(tmpverts);
}
/* SECTION 2C */
/***************************************************************************
*
* AlnMgr2SameSeq decides whether two vertices come from the same
* sequence (simple seqid compare).
*
***************************************************************************/
static Boolean AlnMgr2SameSeq(AMVertexPtr vertex1, AMVertexPtr vertex2)
{
if (vertex1 == NULL || vertex2 == NULL)
return FALSE;
if (SeqIdComp(vertex1->sip, vertex2->sip) == SIC_YES)
return TRUE;
else
return FALSE;
}
/* SECTION 2C */
/***************************************************************************
*
* AlnMgr2BuildAlignmentFromTree performs a breadth-first traversal of
* the tree, adding edges to the growing alignment as it goes.
*
***************************************************************************/
static void AlnMgr2BuildAlignmentFromTree(AMVertexPtr PNTR vertexarray, Int4 numvertices, AMEdgePtr edge_head, SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
AMVertexPtr adj;
AMVertexPtr adj_head;
AMEdgePtr edge;
Int4 i;
Int4 j;
AMQueuePtr q;
AMQueuePtr q_head;
AMQueuePtr q_prev;
amaip = (AMAlignIndex2Ptr)(sap->saip);
AlnMgr2AddInNewPairwiseSA(sap, edge_head->sap);
edge_head->aligned = TRUE;
q_head = (AMQueuePtr)MemNew(sizeof(AMQueue));
q_head->vertex = AlnMgr2GetBetterVertex(vertexarray, edge_head);
q_head->vertex->visited = TRUE;
/* unlink the vertices */
for (i=0; i<numvertices; i++)
{
vertexarray[i]->next = NULL;
}
while (q_head != NULL)
{
q_prev = q_head;
while (q_prev->next != NULL)
{
q_prev = q_prev->next;
}
adj_head = AlnMgr2GetAdjacentVertices(q_head->vertex, vertexarray, edge_head);
adj = adj_head;
while (adj != NULL)
{
if (adj->visited == FALSE)
{
edge = edge_head;
while (edge != NULL)
{
/* if the edge is used in the tree but not yet aligned, and it's adjacent, align it */
if (edge->aligned == FALSE && edge->used == AM_USED && ((AlnMgr2SameSeq(vertexarray[edge->vertex1], q_head->vertex) && AlnMgr2SameSeq(vertexarray[edge->vertex2], adj)) || (AlnMgr2SameSeq(vertexarray[edge->vertex1], adj) && AlnMgr2SameSeq(vertexarray[edge->vertex2], q_head->vertex))))
{
AlnMgr2AddInNewPairwiseSA(sap, edge->sap);
edge->aligned = TRUE;
}
edge = edge->next;
}
q = (AMQueuePtr)MemNew(sizeof(AMQueue));
q->vertex = adj;
q_prev->next = q;
q_prev = q;
adj->visited = TRUE;
}
adj = adj->next;
}
q = q_head->next;
MemFree(q_head);
q_head = q;
if (q_head == NULL) /* look for discontinuous sets -- those will be left over */
{
edge = edge_head;
while (edge != NULL && q_head == NULL)
{
if (edge->aligned == FALSE && (vertexarray[edge->vertex1]->visited == FALSE || vertexarray[edge->vertex2]->visited == FALSE))
{
q_head = (AMQueuePtr)MemNew(sizeof(AMQueue));
q_head->vertex = AlnMgr2GetBetterVertex(vertexarray, edge);
vertexarray[edge->vertex1]->visited = vertexarray[edge->vertex2]->visited = TRUE;
}
edge = edge->next;
}
}
}
/* now the vertices are no longer in a linked list -> put them back together */
for (j=0; j<i-1; j++)
{
vertexarray[j]->next = vertexarray[j+1];
vertexarray[j+1]->next = NULL;
}
AlnMgr2CondenseColumns((DenseSegPtr)(amaip->sharedaln->segs));
AlnMgr2IndexSingleChildSeqAlign(amaip->sharedaln);
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2GetAdjacentVertices returns a linked list of all vertices which
* are adjacent to the given edge; that is, it returns a list of all
* vertices which are linked by an edge to either vertex of the given edge.
*
***************************************************************************/
static AMVertexPtr AlnMgr2GetAdjacentVertices(AMVertexPtr vertex, AMVertexPtr PNTR vertexarray, AMEdgePtr edge_head)
{
AMVertexPtr adj_head;
AMVertexPtr adj_prev;
AMEdgePtr edge;
edge = edge_head;
adj_head = adj_prev = NULL;
while (edge != NULL)
{
if (AlnMgr2SameSeq(vertexarray[edge->vertex1], vertex))
{
if (adj_head == NULL)
adj_head = adj_prev = vertexarray[edge->vertex2];
else
{
adj_prev->next = vertexarray[edge->vertex2];
adj_prev = adj_prev->next;
}
} else if (AlnMgr2SameSeq(vertexarray[edge->vertex2], vertex))
{
if (adj_head == NULL)
adj_head = adj_prev = vertexarray[edge->vertex1];
else
{
adj_prev->next = vertexarray[edge->vertex1];
adj_prev = adj_prev->next;
}
}
if (adj_prev != NULL)
adj_prev->next = NULL;
edge = edge->next;
}
return adj_head;
}
/* SECTION 2c */
static Boolean AlnMgr2GetFirstRowForSeqId(
DenseSegPtr dsp,
SeqIdPtr sip,
Uint1 strand,
Int4Ptr row_curr,
SeqIdPtr PNTR sip_curr)
{
Boolean found = FALSE;
while (*sip_curr) {
(*row_curr)++;
if (SeqIdComp(sip, *sip_curr) == SIC_YES &&
strand == dsp->strands[*row_curr]) {
found = TRUE;
}
*sip_curr = (*sip_curr)->next;
if (found) return TRUE;
}
return FALSE;
}
static AMSeqPieceSetPtr AlnMgr2CreateSeqPieceSet(DenseSegPtr dsp, Int4 row)
{
AMSeqPieceSetPtr s_set = (AMSeqPieceSetPtr)MemNew(sizeof(AMSeqPieceSet));
AMSeqPiecePtr s = (AMSeqPiecePtr)MemNew(sizeof(AMSeqPiece));
s->beg = -1;
s->end = -1;
s->seg = -1;
s->pos = row - dsp->dim;
s->set = s_set;
s->prev = NULL;
s->next = NULL;
s->left = -1;
s->right = -1;
s->orig_left = -2;
s->orig_right = -2;
s->aligned = FALSE;
s->alt_dsp = NULL;
s->alt_seg = -1;
s->alt_pos = -1;
s->next = NULL;
s_set->dsp = dsp;
s_set->row = row;
s_set->row2 = -1;
s_set->alt_row = -1;
s_set->alt_row2 = -1;
s_set->head = s;
s_set->tail = s;
s_set->max_pos = dsp->dim * dsp->numseg;
s_set->strand = dsp->strands[row];
s_set->plus = s_set->strand != Seq_strand_minus;
s_set->next = NULL;
return s_set;
}
static AMSeqPiecePtr AlnMgr2GetNextSeqPiece(AMSeqPiecePtr s)
{
DenseSegPtr dsp;
Int4 max_pos;
AMSeqPiecePtr s_new;
dsp = s->set->dsp;
max_pos = s->set->max_pos;
if (s->pos < max_pos) {
s_new = (AMSeqPiecePtr)MemNew(sizeof(AMSeqPiece));
s_new->pos = s->pos + dsp->dim;
s_new->seg = s->seg + 1;
s_new->set = s->set;
s_new->prev = s;
s = s->next = s_new;
s->set->tail = s;
s->next = NULL;
/* initialize the following */
s->left = -1;
s->right = -1;
s->aligned = FALSE;
s->alt_dsp = NULL;
s->alt_seg = -1;
s->alt_pos = -1;
s->orig_left = -2;
s->orig_right = -2;
/* find the beg and end */
while (s->pos < max_pos) {
if (dsp->starts[s->pos] != -1) {
s->beg = s->end = dsp->starts[s->pos];
if (s->set->plus) {
s->end += dsp->lens[s->seg] - 1;
} else {
s->beg += dsp->lens[s->seg] - 1;
}
return s;
} else {
s->seg++;
s->pos += dsp->dim;
}
}
s->beg = -1;
s->end = -1;
return s;
}
return NULL;
}
static AMSeqPiecePtr AlnMgr2GetNextLimitedSeqPiece(
AMSeqPiecePtr s,
AMSeqPiecePtr right)
{
DenseSegPtr dsp;
Int4 new_pos, new_seg, max_pos, max_seg;
AMSeqPiecePtr s_new;
AMSeqPiecePtr left = right->prev;
dsp = s->set->dsp;
max_pos = s->set->max_pos;
max_seg = right->seg;
new_pos = s->pos + dsp->dim;
new_seg = s->seg + 1;
while (new_pos < max_pos && new_seg <= max_seg) {
if (dsp->starts[new_pos] != -1) {
s_new = (AMSeqPiecePtr)MemNew(sizeof(AMSeqPiece));
s_new->pos = new_pos;
s_new->seg = new_seg;
s_new->set = s->set;
s_new->next = NULL;
s_new->prev = s;
s = s->next = s_new;
s->set->tail = s;
s->beg = s->end = dsp->starts[s->pos];
if (s->set->plus) {
s->end += dsp->lens[s->seg] - 1;
} else {
s->beg += dsp->lens[s->seg] - 1;
}
/* aligned to a sequence in anchor or not */
if (s->seg == right->seg) {
s->aligned = TRUE;
s->left = right->beg;
s->right = right->end;
} else {
s->aligned = FALSE;
s->left = left->end;
s->right = right->beg;
}
/* these are not yet used */
s->orig_left = -2;
s->orig_right = -2;
s->alt_dsp = NULL;
s->alt_seg = -1;
s->alt_pos = -1;
return s;
}
new_pos += dsp->dim;
new_seg++;
}
return NULL;
}
static void AlnMgr2AddSeqPiece(
AMSeqPieceSetPtr set,
AMSeqPiecePtr what)
{
AMSeqPiecePtr s;
DenseSegPtr dsp = set->dsp;
DenseSegPtr alt_dsp = what->set->dsp;
s = (AMSeqPiecePtr)MemNew(sizeof(AMSeqPiece));
s->beg = what->beg;
s->end = what->end;
if (alt_dsp == dsp) {
s->seg = what->seg;
s->pos = what->pos;
s->alt_dsp = NULL;
s->alt_seg = -1;
s->alt_pos = -1;
} else {
s->seg = -1;
s->pos = -1;
s->alt_dsp = alt_dsp;
s->alt_seg = what->seg;
s->alt_pos = what->pos;
}
s->left = what->left;
s->right = what->right;
s->orig_left = what->orig_left;
s->orig_right = what->orig_right;
s->aligned = what->aligned;
s->set = set;
s->next = NULL;
if ((s->prev = set->tail) != NULL) {
s->prev->next = s;
}
set->tail = s;
}
static void AlnMgr2InsertSeqPiece(
AMSeqPiecePtr where,
AMSeqPiecePtr what,
Int4 end)
{
AMSeqPiecePtr s;
DenseSegPtr dsp = where->set->dsp;
DenseSegPtr alt_dsp = what->set->dsp;
s = (AMSeqPiecePtr)MemNew(sizeof(AMSeqPiece));
s->beg = what->beg;
s->end = end;
if (where->beg == what->beg) {
s->seg = where->seg;
s->pos = where->pos;
where->beg = end + (where->set->plus? 1 : -1);
if (alt_dsp == dsp) {
s->alt_dsp = NULL;
s->alt_seg = -1;
s->alt_pos = -1;
} else {
s->alt_dsp = alt_dsp;
s->alt_seg = what->seg;
s->alt_pos = what->pos;
}
} else {
if (alt_dsp == dsp) {
s->seg = what->seg;
s->pos = what->pos;
s->alt_dsp = NULL;
s->alt_seg = -1;
s->alt_pos = -1;
} else {
s->seg = -1;
s->pos = -1;
s->alt_dsp = alt_dsp;
s->alt_seg = what->seg;
s->alt_pos = what->pos;
}
}
s->left = what->left;
s->right = what->right;
s->orig_left = what->orig_left;
s->orig_right = what->orig_right;
s->aligned = what->aligned;
s->set = where->set;
s->next = where;
if ((s->prev = where->prev) != NULL) {
if (s->prev) {
s->prev->next = s;
} else {
if (s->set->head == where) {
s->set->head = s;
}
}
where->prev = s;
}
}
static void AlnMgr2CopySeg(
DenseSegPtr DSP,
Int4 PNTR SEG_ptr,
Int4 PNTR POS_ptr,
DenseSegPtr Dsp,
Int4 PNTR Seg_ptr,
Int4 PNTR Pos_ptr,
AMSeqPiecePtr PNTR s_ptr)
{
Int4 i, rdelta, ldelta, POS, Pos, max_Pos, pos2, alt_pos2, SEG, Seg,
beg, end;
AMSeqPiecePtr s;
Boolean plus;
POS = *POS_ptr; Pos = *Pos_ptr;
SEG = *SEG_ptr; Seg = *Seg_ptr;
s = *s_ptr;
if (s->set->row != s->set->row2) { /* if not a B */
if (!(s->next)) {
*s_ptr = NULL;
return; /* skip the last A */
}
}
max_Pos = POS+Dsp->dim;
DSP->lens[SEG] = ABS(s->end - s->beg) + 1;
if (s->set->dsp != Dsp) { /* the extra row for the non-anchor seq */
for (i = 0; POS < max_Pos; POS++, i++) {
DSP->starts[POS] = -1;
DSP->strands[POS] = Dsp->strands[i];
}
DSP->starts[POS] = MIN(s->beg, s->end);
DSP->strands[POS] = s->set->strand;
POS++;
} else { /* not dealing with the extra row itself */
if (s->pos >= 0 && s->set->row != s->set->row2) { /* Dsp involved */
beg = end = s->set->dsp->starts[s->pos];
if (s->set->plus) {
end += s->set->dsp->lens[s->seg]-1;
} else {
beg += s->set->dsp->lens[s->seg]-1;
}
if (ldelta = ABS(s->beg - beg)) {
/* need to "continue" from the orig seg */
Pos = s->pos - s->set->row;
Seg = s->seg;
}
rdelta = ABS(end - s->end);
for (; POS < max_Pos; POS++, Pos++) {
DSP->strands[POS] = Dsp->strands[Pos];
plus = DSP->strands[POS] != Seq_strand_minus;
if (Dsp->starts[Pos] != -1) {
DSP->starts[POS] = Dsp->starts[Pos] + (plus ? ldelta : rdelta);
} else {
DSP->starts[POS] = -1;
}
}
if (ldelta) {
/* restore these */
Pos = *Pos_ptr;
Seg = *Seg_ptr;
} else {
Seg++;
}
if (s->alt_dsp) { /* dsp involved too */
alt_pos2 =
s->alt_pos + s->set->alt_row2 - s->set->alt_row;
beg = end = s->alt_dsp->starts[s->alt_pos];
if (s->alt_dsp->strands[s->alt_pos] == Seq_strand_minus) {
beg += s->alt_dsp->lens[s->alt_seg]-1;
} else {
end += s->alt_dsp->lens[s->alt_seg]-1;
}
ldelta = ABS(s->beg - beg);
rdelta = ABS(end - s->end);
if (s->set->row2 != -1) { /* 2nd row merged*/
pos2 = POS - DSP->dim + s->set->row2;
} else { /* extra row */
pos2 = POS;
POS++;
}
DSP->strands[pos2] = s->alt_dsp->strands[alt_pos2];
plus = DSP->strands[pos2] != Seq_strand_minus;
if (s->alt_dsp->starts[alt_pos2] != -1) {
DSP->starts[pos2] = s->alt_dsp->starts[alt_pos2] +
(plus ? ldelta : rdelta);
} else {
DSP->starts[pos2] = -1;
}
} else { /* dsp not involved */
if (s->set->row2 == -1) { /* 2nd row not merged */
DSP->starts[POS] = -1;
DSP->strands[POS] =
s->set->alt_dsp->strands[s->set->alt_row2];
POS++;
}
}
} else { /* Dsp not involved */
for (i = 0; POS < max_Pos; POS++, i++) {
DSP->starts[POS] = -1;
DSP->strands[POS] = Dsp->strands[i];
}
if (s->set->row == s->set->row2) { /* if a B */
if (!(s->alt_dsp)) {
Pos += s->set->dsp->dim; /* move to next seg */
Seg++;
}
} else { /* not a B */
alt_pos2 =
s->alt_pos + s->set->alt_row2 - s->set->alt_row;
beg = end = s->alt_dsp->starts[s->alt_pos];
if (s->alt_dsp->strands[s->alt_pos] == Seq_strand_minus) {
beg += s->alt_dsp->lens[s->alt_seg]-1;
} else {
end += s->alt_dsp->lens[s->alt_seg]-1;
}
ldelta = ABS(s->beg - beg);
rdelta = ABS(end - s->end);
if (s->set->row2 != -1) { /* merged row2 */
pos2 = POS - DSP->dim + s->set->row2;
} else {
pos2 = POS;
POS++;
}
DSP->strands[pos2] = s->alt_dsp->strands[alt_pos2];
plus = DSP->strands[pos2] != Seq_strand_minus;
if (s->alt_dsp->starts[alt_pos2] != -1) {
DSP->starts[pos2] = s->alt_dsp->starts[alt_pos2] +
(plus ? ldelta : rdelta);
} else {
DSP->starts[pos2] = -1;
}
}
DSP->starts[POS + s->set->row - DSP->dim] = MIN(s->beg, s->end);
}
}
(*SEG_ptr)++;
*Seg_ptr = Seg;
*s_ptr = (*s_ptr)->next;
*POS_ptr = POS;
*Pos_ptr = Pos;
}
NLM_EXTERN void AlnMgr2AddInNewPairwiseSA(SeqAlignPtr parent, SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp, Dsp, DSP;
Int4 Seg, SEG;
Int4 Pos, POS, max_POS;
Int4 A_end, B_beg;
Int4 anchor, Anchor;
Int4 row;
SeqIdPtr sip, extra_sip;
AMSeqPieceSetPtr a_set, A_set, b_set, B_set_head, B_set;
AMSeqPiecePtr a, A, b, B;
Boolean conflict;
Boolean a_plus, b_plus;
Int4 upper_limit;
Int4 extra_segs;
dsp = (DenseSegPtr)(sap->segs);
if (dsp->dim != 2) {
if (dsp->dim == 0) {
dsp->dim = 2; /* set to default */
} else {
ErrPostEx(SEV_ERROR, 0,0,
"AlnMgr2AddInNewPairwiseSA: dsp->dim (=%d) should be 2.",
dsp->dim);
return;
}
}
if (dsp->numseg < 1) {
ErrPostEx(SEV_ERROR, 0,0,
"AlnMgr2AddInNewPairwiseSA: dsp->numseg (=%d) should be > 0.",
dsp->numseg);
return;
}
amaip = (AMAlignIndex2Ptr)(parent->saip);
if (amaip->sharedaln == NULL) {/* first alignment to be added */
SeqAlignPtr salp;
Int4 i;
salp = SeqAlignDup(sap);
AlnMgr2IndexSingleChildSeqAlign(salp);
amaip->sharedaln = salp;
amaip->numrows = dsp->dim;
sip = dsp->ids;
amaip->ids = (SeqIdPtr PNTR)MemNew((dsp->dim)*sizeof(SeqIdPtr));
i = 0;
while (sip != NULL) {
amaip->ids[i] = SeqIdDup(sip);
sip = sip->next;
i++;
}
MemFree(amaip->saps);
amaip->saps = (SeqAlignPtr PNTR)MemNew(sizeof(SeqAlignPtr));
amaip->saps[0] = sap;
amaip->numsaps = 1;
MemFree(amaip->aligned);
amaip->aligned = (Boolean PNTR) MemNew(sizeof(Boolean));
amaip->aligned[0] = TRUE;
return;
}
/* add the new sap */
amaip->numsaps++;
amaip->saps = (SeqAlignPtr PNTR) MemMore
(amaip->saps, amaip->numsaps*sizeof(SeqAlignPtr));
amaip->saps[amaip->numsaps-1] = sap;
amaip->aligned = (Boolean PNTR) MemMore
(amaip->aligned, (amaip->numsaps)*sizeof(Boolean));
amaip->aligned[amaip->numsaps-1] = TRUE;
Dsp = (DenseSegPtr)(amaip->sharedaln->segs);
AlnMgr2GetFirstSharedRow(amaip->sharedaln, sap, &Anchor, &anchor);
{{ /* make sure the shared rows are on the same strand */
Uint1 Strand, strand;
Strand = AlnMgr2GetNthStrand(amaip->sharedaln, Anchor);
if (Strand == Seq_strand_unknown)
Strand = Seq_strand_plus;
strand = AlnMgr2GetNthStrand(sap, anchor);
if (strand == Seq_strand_unknown)
strand = Seq_strand_plus;
if (Strand != strand) {
SeqAlignListReverseStrand(sap);
SAIndex2Free2(sap->saip);
sap->saip = NULL;
AlnMgr2IndexSingleChildSeqAlign(sap);
dsp = (DenseSegPtr)(sap->segs);
strand = AlnMgr2GetNthStrand(sap, anchor);
if (strand == Seq_strand_unknown)
strand = Seq_strand_plus;
}
a_plus = strand != Seq_strand_minus;
}}
anchor--; Anchor--; /* make them 0-based */
/* create new dsp */
DSP = DenseSegNew();
DSP->numseg = Dsp->numseg;
DSP->dim = Dsp->dim;
/* DSP->ids = SeqIdDupList(Dsp->ids); */
/* collect other shared seqids */
b_set = B_set = B_set_head = NULL;
row = -1; sip = Dsp->ids;
extra_sip = dsp->ids;
if (anchor == 0) {
extra_sip = extra_sip->next;
}
while (AlnMgr2GetFirstRowForSeqId
(Dsp, extra_sip, dsp->strands[1-anchor], &row, &sip)) {
if (B_set) {
B_set->next = AlnMgr2CreateSeqPieceSet(Dsp, row);
B_set = B_set->next;
} else {
B_set = B_set_head = AlnMgr2CreateSeqPieceSet(Dsp, row);
}
}
b_plus = dsp->strands[1-anchor] != Seq_strand_minus;
/* ids */
DSP->ids = Dsp->ids;
Dsp->ids = NULL;
/* collect a, b */
a_set = AlnMgr2CreateSeqPieceSet(dsp, anchor);
a = a_set->head;
b_set = AlnMgr2CreateSeqPieceSet(dsp, 1-anchor);
while (a = AlnMgr2GetNextSeqPiece(a)) {
b = b_set->tail;
while (b = AlnMgr2GetNextLimitedSeqPiece(b, a)) {
if (!b->aligned) {
DSP->numseg++;
}
}
}
/* collect A, B */
A_set = AlnMgr2CreateSeqPieceSet(Dsp, Anchor);
A = A_set->head;
while (A = AlnMgr2GetNextSeqPiece(A)) {
B_set = B_set_head;
while (B_set) {
B = B_set->tail;
while (B = AlnMgr2GetNextLimitedSeqPiece(B, A)) {};
B_set=B_set->next;
}
}
/* resolve a, A */
A_set->alt_row = a_set->row;
a = a_set->head->next;
A = A_set->head->next;
while (a && A && a->next && A->next) {
if (a_plus ? a->beg < A->beg : a->beg > A->beg) {
AlnMgr2InsertSeqPiece
(A, a, a_plus ? MIN(a->end, A->beg-1) : MAX(a->end, A->beg+1));
DSP->numseg++;
if (a_plus ? a->end < A->beg : a->end > A->beg) {
a = a->next;
} else {
a->beg = A->beg;
}
} else if (a_plus ? A->beg < a->beg : A->beg > a->beg) {
if (a_plus ? A->end < a->beg : A->end > a->beg) {
A = A->next;
} else {
AlnMgr2InsertSeqPiece(A, A, a_plus ? a->beg - 1 : a->beg + 1);
DSP->numseg++;
}
} else { /* a->beg == A->beg */
if (a_plus ? a->end < A->end : a->end > A->end) {
AlnMgr2InsertSeqPiece(A, a, a->end);
DSP->numseg++;
a = a->next;
} else if (a_plus ? a->end > A->end : a->end < A->end) {
a->beg = A->end + (a_plus ? 1 : -1);
A->alt_dsp = a->set->dsp;
A->alt_seg = a->seg;
A->alt_pos = a->pos;
A = A->next;
} else { /* a->end == A->end */
A->alt_dsp = a->set->dsp;
A->alt_seg = a->seg;
A->alt_pos = a->pos;
a = a->next;
A = A->next;
}
}
}
while (a && a->next) {
AlnMgr2InsertSeqPiece(A, a, a->end);
DSP->numseg++;
a = a->next;
}
/* set the upper limits */
if (B_set_head) {
if (a_plus) {
upper_limit =
A_set->tail->end = A_set->tail->beg = A_set->tail->prev->end + 1;
b = b_set->tail;
while (b && b->right == -1) {
b->right = upper_limit;
b = b->prev;
}
B_set = B_set_head;
while (B_set) {
B = B_set->tail;
while (B && B->right == -1) {
B->right = upper_limit;
B = B->prev;
}
B_set = B_set->next;
}
} else {
upper_limit =
A_set->head->beg = A_set->head->end = A_set->head->next->beg + 1;
b = b_set->head;
while (b && b->left == -1) {
b->left = upper_limit;
b = b->next;
}
B_set = B_set_head;
while (B_set) {
B = B_set->head;
while (B && B->left == -1) {
B->left = upper_limit;
B = B->next;
}
B_set = B_set->next;
}
}
}
/* try to resolve b, B */
if (B_set_head) {
b = b_set->head->next;
B_set = B_set_head;
while (B_set) {
B = B_set->head->next;
conflict = FALSE;
extra_segs = 0;
while (b && B) {
if (b_plus ? b->beg < B->beg : b->beg > B->beg) {
if (b_plus ? b->end < B->beg : b->end > B->beg) {
/* trim the limits */
if (a_plus ? B->left <= b->left : B->left >= b->left) {
if (a_plus ? B->right < b->left : B->right > b->left) {
conflict = TRUE; break;
} else {
if (B->aligned) {
conflict = TRUE; break; /* no trimming allowed */
} else {
B->left = b->left;
}
}
if (a_plus ? b->right > B->right : b->right < B->right) {
if (b->aligned) {
conflict = TRUE; break; /* no trimming allowed */
} else {
b->orig_right = b->right; /* for recovering */
b->right = B->right;
}
}
}
AlnMgr2InsertSeqPiece(B, b, b->end);
if (!(b->aligned)) extra_segs++;
b = b->next;
} else {
conflict = TRUE; break;
}
} else if (b_plus ? B->beg < b->beg : B->beg > b->beg) {
if (b_plus ? B->end < b->beg : B->end > b->beg) {
/* trim the limits */
if (a_plus ? b->left < B->left : b->left > B->left) {
if (a_plus ? b->right < B->left : b->right > B->left) {
conflict = TRUE; break;
} else {
if (b->aligned) {
conflict = TRUE; break; /* no trimming allowed */
} else {
b->orig_left = b->left; /* for recovering */
b->left = B->left;
}
}
if (a_plus ? B->right > b->right : B->right < b->right) {
if (B->aligned) {
conflict = TRUE; break; /* no trimming allowed */
} else {
B->right = b->right;
}
}
}
B = B->next;
} else {
conflict = TRUE; break;
}
} else { /* B->beg == b->beg */
conflict = TRUE; break;
}
}
if (!conflict) {
while (b) {
AlnMgr2AddSeqPiece(B_set, b);
if (!(b->aligned)) extra_segs++;
b = b->next;
}
/* DSP->numseg += extra_segs; */
break;
}
/* conflict, roll back b, recovering limits, try next B */
if (!b) {
b = b_set->tail;
}
while (b) {
if (b->orig_left != -2) {
b->left = b->orig_left;
}
if (b->orig_right != -2) {
b->right = b->orig_right;
}
b = b->prev;
}
b = b_set->head->next;
B_set = B_set->next;
}
}
if (B_set) { /* B_set has no conflict with b_set */
B = B_set->head->next;
B_set->row2 = B_set->row; /* mark the set */
A_set->row2 = B_set->row;
A_set->alt_row2 = b_set->row;
} else { /* this mean extra row */
A_set->row2 = -1;
A_set->alt_row2 = b_set->row;
A_set->alt_dsp = b_set->dsp;
DSP->dim++;
sip = DSP->ids;
while (sip->next) {
sip = sip->next;
}
AddSeqId(&sip, extra_sip);
/* fix the index too */
amaip->numrows = DSP->dim;
amaip->ids = (SeqIdPtr PNTR)MemMore
(amaip->ids,amaip->numrows*sizeof(SeqIdPtr));
amaip->ids[amaip->numrows-1] = SeqIdDup(extra_sip);
b_set->row2 = b_set->row; /* mark the set */
B = b_set->head->next;
B_beg = -1; /* nothing to comp Bs to */
}
/* allocate memory for the new sharedaln matrix */
DSP->starts = (Int4Ptr)MemNew(DSP->numseg * DSP->dim * sizeof(Int4));
DSP->strands = (Uint1Ptr)MemNew(DSP->numseg * DSP->dim * sizeof(Uint1));
DSP->lens = (Int4Ptr)MemNew(DSP->numseg * sizeof(Int4));
/* loop through segments */
POS = 0; Pos = 0; Seg = 0; SEG = 0;
A = A_set->head->next;
while (Seg < Dsp->numseg) {
A_end = Dsp->starts[Pos+A_set->row];
if (a_plus && A_end >= 0) {
A_end += Dsp->lens[Seg] - 1;
}
if (B_set) {
B_beg = Dsp->starts[Pos+B_set->row];
}
if (A_end >= 0) {
while (A && (a_plus ? A->end <= A_end : A->end >= A_end)) {
while (B && (a_plus ? B->left < A->beg : B->left > A->beg)) {
if (B->aligned) {
B = B->next;
break; /* the aligned piece should be last */
} else {
AlnMgr2CopySeg(DSP, &SEG, &POS, Dsp, &Seg, &Pos, &B);
}
}
if (B && B->aligned && B->left == A->beg) {
B = B->next;
}
AlnMgr2CopySeg(DSP, &SEG, &POS, Dsp, &Seg, &Pos, &A);
}
} else if (B && B_beg >= 0) {
while (B && (b_plus ? B->beg <= B_beg : B->beg >= B_beg)) {
while (A && (a_plus ? A->beg <= B->left : A->beg >= B->left)) {
AlnMgr2CopySeg(DSP, &SEG, &POS, Dsp, &Seg, &Pos, &A);
}
if (B->aligned) {
B = B->next;
} else {
AlnMgr2CopySeg(DSP, &SEG, &POS, Dsp, &Seg, &Pos, &B);
}
}
} else {
/* just copy the Dsp segment */
DSP->lens[SEG] = Dsp->lens[Seg];
max_POS = POS + Dsp->dim;
for (; POS < max_POS; POS++, Pos++) {
DSP->starts[POS] = Dsp->starts[Pos];
DSP->strands[POS] = Dsp->strands[Pos];
}
if (DSP->dim > Dsp->dim) {
DSP->starts[POS] = -1;
DSP->strands[POS] = dsp->strands[1-anchor];
POS++;
}
SEG++;
Seg++;
}
}
while (A) {
while (B && (a_plus ? B->right <= A->beg : B->right >= A->beg)) {
if (B->aligned) {
B = B->next;
} else {
AlnMgr2CopySeg(DSP, &SEG, &POS, Dsp, &Seg, &Pos, &B);
}
}
AlnMgr2CopySeg(DSP, &SEG, &POS, Dsp, &Seg, &Pos, &A);
}
while (B) {
if (B->aligned) {
B = B->next;
} else {
AlnMgr2CopySeg(DSP, &SEG, &POS, Dsp, &Seg, &Pos, &B);
}
}
/* Done */
AMSeqPieceSetFree(A_set);
AMSeqPieceSetFree(a_set);
AMSeqPieceSetFree(B_set_head);
AMSeqPieceSetFree(b_set);
amaip->sharedaln->segs = DSP;
/* update the dim for the shared_aln to match the new DensegPtr */
amaip->sharedaln->dim = DSP->dim;
DenseSegFree(Dsp);
}
/***************************************************************************
*
* AlnMgr2AddInNewSA adds a seqalign to an existing seqalign. The new
* seqalign must share at least one row with the existing seqalign. The
* new, combined dense-seg structure is computed, and then it is condensed
* using AlnMgr2CondenseRows to make sure that there are no superfluous rows.
*
***************************************************************************/
static void AlnMgr2AddInNewSA(SeqAlignPtr parent, SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
AM_Small2Ptr asp;
AM_Small2Ptr asp_head;
AM_Small2Ptr asp_prev;
AM_Small2Ptr asp_tmp;
AM_Small2Ptr asp_tmp2;
AM_Small2Ptr PNTR asparray;
Int4 currstop;
DenseSegPtr dsp;
DenseSegPtr dsp_new;
DenseSegPtr dsp_shared;
Boolean found;
Int4 i;
Int4 j;
Int4 k;
Int4 n1;
Int4 n2;
Int4 numrows;
Int4 offset;
SeqAlignPtr salp;
SeqAlignPtr sap_new;
SeqAlignPtr PNTR saptmp;
SeqIdPtr sip;
SeqIdPtr sip_head;
SeqIdPtr sip_tmp;
Int4 state;
Int4 stop1;
Int4 stop2;
Uint1 strand1;
Uint1 strand2;
amaip = (AMAlignIndex2Ptr)(parent->saip);
if (amaip->sharedaln == NULL) /* this is the first alignment to be added */
{
salp = SeqAlignDup(sap);
AlnMgr2IndexSingleChildSeqAlign(salp);
dsp = (DenseSegPtr)(salp->segs);
amaip->sharedaln = salp;
amaip->numrows = dsp->dim;
sip = dsp->ids;
amaip->ids = (SeqIdPtr PNTR)MemNew((dsp->dim)*sizeof(SeqIdPtr));
i = 0;
while (sip != NULL)
{
amaip->ids[i] = SeqIdDup(sip);
sip = sip->next;
i++;
}
MemFree(amaip->saps);
amaip->saps = (SeqAlignPtr PNTR)MemNew(sizeof(SeqAlignPtr));
amaip->saps[0] = sap;
amaip->numsaps = 1;
} else
{
/* free ids */
for (i=0; i<amaip->numrows; i++)
{
SeqIdFree(amaip->ids[i]);
}
MemFree(amaip->ids);
/* add the new sap */
saptmp = amaip->saps;
amaip->saps = (SeqAlignPtr PNTR)MemNew((amaip->numsaps+1)*sizeof(SeqAlignPtr));
for (i=0; i<amaip->numsaps; i++)
{
amaip->saps[i] = saptmp[i];
}
amaip->saps[amaip->numsaps] = sap;
MemFree(saptmp);
amaip->numsaps++;
/* dsp, dsp_shared, n1, n2 */
dsp = (DenseSegPtr)(sap->segs);
dsp_shared = (DenseSegPtr)(amaip->sharedaln->segs);
AlnMgr2GetFirstSharedRow(amaip->sharedaln, sap, &n1, &n2);
if (n1 == n2 && n1 == 0)
return;
/* make sure the shared rows are on the same strand */
strand1 = AlnMgr2GetNthStrand(amaip->sharedaln, n1);
if (strand1 == Seq_strand_unknown)
strand1 = Seq_strand_plus;
strand2 = AlnMgr2GetNthStrand(sap, n2);
if (strand2 == Seq_strand_unknown)
strand2 = Seq_strand_plus;
if (strand1 != strand2)
{
SeqAlignListReverseStrand(sap);
SAIndex2Free2(sap->saip);
sap->saip = NULL;
AlnMgr2IndexSingleChildSeqAlign(sap);
dsp = (DenseSegPtr)(sap->segs);
strand2 = AlnMgr2GetNthStrand(sap, n2);
if (strand2 == Seq_strand_unknown)
strand2 = Seq_strand_plus;
}
/* numrows */
numrows = dsp->dim + dsp_shared->dim - 1; /* for now this works; compress at the end */
asp_head = NULL;
/* currstop */
if (strand1 == Seq_strand_minus)
AlnMgr2GetNthSeqRangeInSA(amaip->sharedaln, n1, NULL, &currstop);
else
currstop = -1;
/* add asp for each dsp_shared seg */
for (i=0; i<dsp_shared->numseg; i++)
{
asp = (AM_Small2Ptr)MemNew(sizeof(AM_Small2));
if (dsp_shared->starts[(dsp_shared->dim)*i + n1 - 1] < 0)
{
asp->n1 = currstop;
asp->n2 = i+1;
asp->n3 = AM_GAP;
asp->n4 = dsp_shared->lens[i];
if (asp_head != NULL)
{
asp_prev->next = asp;
/*if (asp_prev->n1 == asp->n1)
asp->n5 = asp_prev->n5+1;*/
asp_prev = asp;
} else
asp_head = asp_prev = asp;
} else
{
asp->n1 = dsp_shared->starts[(dsp_shared->dim)*i + n1 - 1];
asp->n2 = 1;
asp->n3 = AM_START;
asp->n4 = dsp_shared->lens[i];
if (asp_head != NULL)
{
asp_prev->next = asp;
/*if (asp_prev->n1 == asp->n1)
asp->n5 = asp_prev->n5+1;*/
asp_prev = asp;
} else
asp_head = asp_prev = asp;
asp = (AM_Small2Ptr)MemNew(sizeof(AM_Small2));
asp->n1 = dsp_shared->starts[(dsp_shared->dim)*i + n1 - 1] + dsp_shared->lens[i] - 1;
asp->n2 = 1;
j = i+1;
while (j<dsp_shared->numseg && dsp_shared->starts[(dsp_shared->dim)*j + n1 - 1] == -1)
{
j++;
}
if (j<dsp_shared->numseg)
{
if (dsp_shared->starts[(dsp_shared->dim)*j + n1 - 1] > asp->n1 + 1)
asp->n3 = AM_HARDSTOP;
else
asp->n3 = AM_STOP;
} else
asp->n3 = AM_HARDSTOP;
if (asp->n3 == AM_HARDSTOP)
{
if (strand1 != Seq_strand_minus)
asp->n4 = -(dsp_shared->starts[(dsp_shared->dim)*i+n1-1] + dsp_shared->lens[i]-1);
else
asp->n4 = -dsp_shared->starts[(dsp_shared->dim)*i+n1-1];
} else
asp->n4 = -dsp_shared->lens[i];
if (strand1 != Seq_strand_minus)
currstop = asp->n1;
else
currstop = asp_prev->n1-1;
asp_prev->next = asp;
/*if (asp_prev->n1 == asp->n1)
asp->n5 = asp_prev->n5+1;*/
asp_prev = asp;
}
} /* asp for each dsp_shared seg */
/* currstop = start of sap's n2-th seq */
if (strand1 == Seq_strand_minus)
AlnMgr2GetNthSeqRangeInSA(sap, n2, NULL, &currstop);
else
AlnMgr2GetNthSeqRangeInSA(sap, n2, &currstop, NULL);
/* add asp for each dsp seg */
for (i=0; i<dsp->numseg; i++)
{
asp = (AM_Small2Ptr)MemNew(sizeof(AM_Small2));
if (dsp->starts[(dsp->dim)*i + n2 - 1] < 0)
{
asp->n1 = currstop;
asp->n2 = dsp_shared->numseg+i+1;
asp->n3 = AM_GAP;
asp->n4 = dsp->lens[i];
asp_prev->next = asp;
/*if (asp_prev->n1 == asp->n1)
asp->n5 = asp_prev->n5 + 1;*/
asp_prev = asp;
} else
{
asp->n1 = dsp->starts[(dsp->dim)*i + n2 - 1];
asp->n2 = 1;
asp->n3 = AM_START;
asp->n4 = dsp->lens[i];
asp_prev->next = asp;
/*if (asp_prev->n1 == asp->n1)
asp->n5 = asp_prev->n5+1;*/
asp_prev = asp;
asp = (AM_Small2Ptr)MemNew(sizeof(AM_Small2));
asp->n1 = dsp->starts[(dsp->dim)*i + n2 - 1] + dsp->lens[i] - 1;
asp->n2 = 1;
j = i+1;
while (j<dsp->numseg && dsp->starts[(dsp->dim)* j + n2 - 1] == -1)
{
j++;
}
if (j<dsp->numseg)
{
if (dsp->starts[(dsp->dim)*j + n2 - 1] > asp->n1 + 1)
asp->n3 = AM_HARDSTOP;
else
asp->n3 = AM_STOP;
} else
asp->n3 = AM_HARDSTOP;
if (asp->n3 == AM_HARDSTOP)
{
if (strand1 != Seq_strand_minus)
asp->n4 = -(dsp->starts[(dsp->dim)*i+n1-1] + dsp->lens[i]-1);
else
asp->n4 = -dsp->starts[(dsp->dim)*i+n1-1];
/* so if n4 is negative, this is the highest-numbered residue in the interval */
} else
asp->n4 = dsp->lens[i];
if (strand1 != Seq_strand_minus)
currstop = asp->n1;
else
currstop = asp_prev->n1-1;
asp_prev->next = asp;
/*if (asp_prev->n1 == asp->n1)
asp->n5 = asp_prev->n5 + 1;*/
asp_prev = asp;
}
}
/* create asparray and heapsort it */
asp = asp_head;
i = 0;
while (asp != NULL)
{
i++;
asp = asp->next;
}
asparray = (AM_Small2Ptr PNTR)MemNew(i*sizeof(AM_Small2Ptr));
asp = asp_head;
i = 0;
while (asp != NULL)
{
asparray[i] = asp;
i++;
asp = asp->next;
}
if (strand1 != Seq_strand_minus)
HeapSort(asparray, i, sizeof(asparray), AlnMgr2CompareAsps);
else
HeapSort(asparray, i, sizeof(asparray), AlnMgr2CompareAspsMinus);
/* now need to remove redundant (identical) points */
/* but still need to count those points toward the states */
asp = asparray[0];
asp->next = NULL;
for (j=0; j<i-1; j++)
{
if (asparray[j+1]->n1 != asp->n1 || asparray[j+1]->n3 != asp->n3 || asp->n3 == AM_GAP)
{
asp->next = asparray[j+1];
asp->next->next = NULL;
asp = asp->next;
} else
{
k = j;
while (asparray[k] == NULL && k >= 0)
{
k--;
}
if (k>=0 && asparray[k]->n3 != AM_GAP)
asparray[k]->n2++;
MemFree(asparray[j+1]);
asparray[j+1] = NULL;
}
}
asp_head = asparray[0];
MemFree(asparray);
j=0;
asp = asp_head;
asp_prev = NULL;
/* count up the segments; two consecutive stops make a segment */
state = 0;
if (strand1 != Seq_strand_minus)
{
while (asp != NULL)
{
if (asp->n3 == AM_START)
{
state += asp->n2;
j++;
} else if (asp->n3 == AM_STOP)
{
state -= asp->n2;
asp_tmp = asp->next;
while (asp_tmp != NULL && asp_tmp->n3 == AM_GAP)
{
asp_tmp = asp_tmp->next;
}
if (state != 0 && asp_tmp != NULL && asp_tmp->n1 != asp->n1+1 && (asp_tmp->n3 != AM_HARDSTOP || asp_tmp->n1 != asp->n1))
j++;
else if (state != 0 && asp->next != NULL && asp_tmp != NULL && (asp_tmp->n3 != AM_HARDSTOP || asp_tmp->n1 != asp->n1))
{
asp_tmp2 = asp_tmp;
while (asp_tmp2 != NULL && asp->n1+1 == asp_tmp2->n1 && asp_tmp2->n3 != AM_START)
{
asp_tmp2 = asp_tmp2->next;
}
if (asp_tmp2 != NULL && ((asp_tmp2->n1 == asp->n1+1 && asp_tmp2->n3 != AM_START) || asp_tmp2->n1 != asp->n1+1) && (asp_tmp->n3 != AM_HARDSTOP || asp_tmp->n1 != asp->n1))
j++;
}
} else if (asp->n3 == AM_GAP)
j++;
else if (asp->n3 == AM_HARDSTOP)
{
state -= asp->n2;
asp_tmp = asp->next;
while (asp_tmp != NULL && asp_tmp->n3 == AM_GAP)
{
asp_tmp = asp_tmp->next;
}
if (state != 0 && asp_tmp != NULL && asp_tmp->n1 != asp->n1+1)
j++;
else if (state != 0 && asp->next != NULL && asp_tmp != NULL)
{
asp_tmp2 = asp_tmp;
while (asp_tmp2 != NULL && asp->n1+1 == asp_tmp2->n1 && asp_tmp2->n3 != AM_START)
{
asp_tmp2 = asp_tmp2->next;
}
if (asp_tmp2 != NULL && ((asp_tmp2->n1 == asp->n1+1 && asp_tmp2->n3 != AM_START) || asp_tmp2->n1 != asp->n1+1))
j++;
else if (asp_tmp2 == NULL)
j++;
}
}
asp = asp->next;
}
} else
{
currstop = -1;
while (asp != NULL)
{
if (asp->n3 == AM_STOP || asp->n3 == AM_HARDSTOP)
{
if (currstop != asp->n1 && state > 0)
j++;
currstop = asp->n1;
state += asp->n2;
} else if (asp->n3 == AM_START)
{
state -= asp->n2;
j++;
currstop = asp->n1 - 1;
} else if (asp->n3 == AM_GAP)
j++;
asp = asp->next;
}
}
/* dsp_new */
dsp_new = DenseSegNew();
dsp_new->dim = numrows;
dsp_new->numseg = j;
dsp_new->ids = SeqIdDupList(dsp_shared->ids);
dsp_new->starts = (Int4Ptr)MemNew((dsp_new->numseg)*(dsp_new->dim)*sizeof(Int4));
dsp_new->strands = (Uint1Ptr)MemNew((dsp_new->numseg)*(dsp_new->dim)*sizeof(Uint1));
dsp_new->lens = (Int4Ptr)MemNew((dsp_new->numseg)*sizeof(Int4));
/* get all the ids except for the duplicated one */
sip_head = NULL;
sip_tmp = NULL;
sip = dsp->ids;
i=0;
/* get all the ids except for the duplicated one */
while (sip != NULL)
{
if (i+1 != n2)
{
if (sip_tmp != NULL)
{
sip_tmp->next = SeqIdDup(sip);
sip_tmp = sip;
} else
sip_head = sip_tmp = SeqIdDup(sip);
}
i++;
sip = sip->next;
}
sip = dsp_new->ids;
while (sip->next != NULL)
{
sip = sip->next;
}
sip->next = sip_head;
/* construct starts and lens from asps */
asp = asp_head;
i=0;
state = 0;
currstop = -1;
if (strand1 != Seq_strand_minus)
{
while (asp != NULL)
{
if (asp->n3 == AM_START)
{
state += asp->n2;
dsp_new->starts[dsp_new->dim*i+n1-1] = asp->n1;
dsp_new->lens[i] = asp->n4;
i++;
} else if (asp->n3 == AM_STOP)
{
state -= asp->n2;
asp_tmp = asp->next;
while (asp_tmp != NULL && asp_tmp->n3 == AM_GAP)
{
asp_tmp = asp_tmp->next;
}
if (state != 0 && asp_tmp != NULL && asp_tmp->n1 != asp->n1+1 && (asp_tmp->n3 != AM_HARDSTOP || asp_tmp->n1 != asp->n1))
{
dsp_new->starts[dsp_new->dim*i+n1-1] = asp->n1 + 1;
dsp_new->lens[i] = asp->n4;
i++;
} else if (state != 0 && asp->next != NULL && asp_tmp != NULL && i < dsp_new->numseg && (asp_tmp->n3 != AM_HARDSTOP || asp_tmp->n1 != asp->n1))
{
asp_tmp2 = asp_tmp;
while (asp_tmp2 != NULL && asp->n1+1 == asp_tmp2->n1 && asp_tmp2->n3 != AM_START)
{
asp_tmp2 = asp_tmp2->next;
}
if (asp_tmp2 != NULL && ((asp_tmp2->n1 == asp->n1+1 && asp_tmp2->n3 != AM_START) || asp_tmp2->n1 != asp->n1+1) && (asp_tmp->n3 != AM_HARDSTOP || asp_tmp->n1 != asp->n1))
{
dsp_new->starts[dsp_new->dim*i+n1-1] = asp->n1 + 1;
dsp_new->lens[i] = asp->n4;
i++;
}
}
} else if (asp->n3 == AM_GAP)
{
dsp_new->starts[dsp_new->dim*i+n1-1] = -asp->n2;
if (asp->n2 > dsp_shared->numseg)
dsp_new->lens[i] = dsp->lens[(asp->n2-1)-(dsp_shared->numseg)];
else
dsp_new->lens[i] = dsp_shared->lens[asp->n2-1];
i++;
} else if (asp->n3 == AM_HARDSTOP)
{
state -= asp->n2;
asp_tmp = asp->next;
while (asp_tmp != NULL && asp_tmp->n3 == AM_GAP)
{
asp_tmp = asp_tmp->next;
}
if (state != 0 && asp->next != NULL && asp_tmp != NULL && asp_tmp->n1 != asp->n1+1 && i < dsp_new->numseg)
{
dsp_new->starts[dsp_new->dim*i+n1-1] = asp->n1 + 1;
if (asp->n1 > -asp->n4)
dsp_new->lens[i] = asp->n4;
i++;
} else if (state != 0 && asp->next != NULL && asp_tmp != NULL && i < dsp_new->numseg)
{
asp_tmp2 = asp_tmp;
while (asp_tmp2 != NULL && asp->n1+1 == asp_tmp2->n1 && asp_tmp2->n3 != AM_START)
{
asp_tmp2 = asp_tmp2->next;
}
if (asp_tmp2 != NULL && ((asp_tmp2->n1 == asp->n1+1 && asp_tmp2->n3 != AM_START) || asp_tmp2->n1 != asp->n1+1))
{
dsp_new->starts[dsp_new->dim*i+n1-1] = asp->n1 + 1;
if (asp->n1 > -asp->n4)
dsp_new->lens[i] = asp->n4;
i++;
} else if (asp_tmp2 == NULL)
{
dsp_new->starts[dsp_new->dim*i+n1-1] = asp->n1 + 1;
if (asp->n1 > -asp->n4)
dsp_new->lens[i] = asp->n4;
i++;
}
}
}
asp = asp->next;
}
for (i=0; i<dsp_new->numseg; i++)
{
found = FALSE;
for (j=i+1; j<dsp_new->numseg && !found; j++)
{
if (dsp_new->starts[dsp_new->dim*j+n1-1] > -1)
{
if (dsp_new->lens[i] == 0)
dsp_new->lens[i] = dsp_new->starts[dsp_new->dim*j+n1-1] - dsp_new->starts[dsp_new->dim*i+n1-1];
else if (dsp_new->lens[i] > 0)
dsp_new->lens[i] = MIN(dsp_new->lens[i], dsp_new->starts[dsp_new->dim*j+n1-1] - dsp_new->starts[dsp_new->dim*i+n1-1]);
else if (dsp_new->lens[i] < 0)
dsp_new->lens[i] = -dsp_new->lens[i]-dsp_new->starts[dsp_new->dim*i+n1-1]+1;
found = TRUE;
}
}
if (!found) /* last segment */
{
if (dsp_new->starts[dsp_new->dim*i+n1-1] >= 0)
{
AlnMgr2GetNthSeqRangeInSA(amaip->sharedaln, n1, NULL, &stop1);
AlnMgr2GetNthSeqRangeInSA(sap, n2, NULL, &stop2);
dsp_new->lens[i] = (MAX(stop1, stop2) + 1) - dsp_new->starts[dsp_new->dim*i+n1-1];
}
}
}
} else
{
while (asp != NULL)
{
if (asp->n3 == AM_STOP)
{
if (currstop != asp->n1 && state > 0)
{
dsp_new->starts[dsp_new->dim*i+n1-1] = asp->n1+1;
dsp_new->lens[i] = currstop - asp->n1;
i++;
}
currstop = asp->n1;
state += asp->n2;
} else if (asp->n3 == AM_START)
{
state -= asp->n2;
dsp_new->starts[dsp_new->dim*i+n1-1] = asp->n1;
dsp_new->lens[i] = currstop - asp->n1 + 1;
i++;
currstop = asp->n1 - 1;
} else if (asp->n3 == AM_GAP)
{
dsp_new->starts[dsp_new->dim*i+n1-1] = -asp->n2;
if (asp->n2 > dsp_shared->numseg)
dsp_new->lens[i] = dsp->lens[(asp->n2-1)-(dsp_shared->numseg)];
else
dsp_new->lens[i] = dsp_shared->lens[asp->n2-1];
i++;
} else if (asp->n3 == AM_HARDSTOP)
{
if (currstop != asp->n1 && state > 0 && asp->next != NULL)
{
dsp_new->starts[dsp_new->dim*i+n1-1] = asp->n1+1;
dsp_new->lens[i] = currstop - asp->n1;
i++;
}
currstop = asp->n1;
state += asp->n2;
}
asp = asp->next;
}
}
/* now add in the other rows, starting with rows from the sharedaln */
for (i=0; i<dsp_shared->dim; i++)
{
if (i+1 != n1)
{
for (j=0; j<dsp_new->numseg; j++)
{
if (dsp_new->starts[dsp_new->dim*j+n1-1] >= 0)
dsp_new->starts[dsp_new->dim*j+i] = AlnMgr2MapSegStartToSegStart(amaip->sharedaln, dsp_new->starts[dsp_new->dim*j+n1-1], n2, i+1, dsp_new->lens[j]);
else
{
if (-(dsp_new->starts[dsp_new->dim*j+n1-1]) > dsp_shared->numseg)
/* this gap came from the new sap */
dsp_new->starts[dsp_new->dim*j+i] = -1;
else /* this gap came from the sharedaln */
dsp_new->starts[dsp_new->dim*j+i] = dsp_shared->starts[dsp_shared->dim*(-dsp_new->starts[dsp_new->dim*j+n1-1]-1)+i];
}
dsp_new->strands[dsp_new->dim*j+i] = AlnMgr2GetNthStrand(amaip->sharedaln, i+1);
}
}
}
for (i=0; i<dsp->dim; i++)
{
if (i+1 != n2)
{
if (i+1 > n2)
offset = 1;
else
offset = 0;
for (j=0; j<dsp_new->numseg; j++)
{
if (dsp_new->starts[dsp_new->dim*j+n1-1] >= 0)
dsp_new->starts[dsp_new->dim*j+i+dsp_shared->dim-offset] = AlnMgr2MapSegStartToSegStart(sap, dsp_new->starts[dsp_new->dim*j+n1-1], n1, i+1, dsp_new->lens[j]);
else
{
if (-(dsp_new->starts[dsp_new->dim*j+n1-1]) > dsp_shared->numseg)
/* this gap is from the new sap */
dsp_new->starts[dsp_new->dim*j+i+dsp_shared->dim-offset] = dsp->starts[dsp->dim*((-dsp_new->starts[dsp_new->dim*j+n1-1])-dsp_shared->numseg-1)+i];
else /* this gap is from the shared alignment */
dsp_new->starts[dsp_new->dim*j+i+dsp_shared->dim-offset] = -1;
}
dsp_new->strands[dsp_new->dim*j+i+dsp_shared->dim-offset] = AlnMgr2GetNthStrand(sap, i+1);
}
}
}
/* fill in strand info for shared row, and get rid of segment keys (neg numbers) */
for (j=0; j<dsp_new->numseg; j++)
{
dsp_new->strands[dsp_new->dim*j+n1-1] = AlnMgr2GetNthStrand(amaip->sharedaln, n1);
if (dsp_new->starts[dsp_new->dim*j+n1-1] < 0)
dsp_new->starts[dsp_new->dim*j+n1-1] = -1;
}
if (dsp_new->dim > 10)
dsp_new->dim = dsp_new->dim;
AlnMgr2CondenseRows(dsp_new, dsp_new->dim);
sap_new = SeqAlignNew();
sap_new->segtype = SAS_DENSEG;
sap_new->segs = (Pointer)(dsp_new);
AlnMgr2IndexSingleChildSeqAlign(sap_new);
SeqAlignFree(amaip->sharedaln);
amaip->sharedaln = sap_new;
amaip->numrows = dsp_new->dim;
amaip->ids = (SeqIdPtr PNTR)MemNew(amaip->numrows*sizeof(SeqIdPtr));
sip = dsp_new->ids;
for (i=0; i<amaip->numrows; i++)
{
amaip->ids[i] = SeqIdDup(sip);
sip = sip->next;
}
while (asp_head != NULL)
{
asp = asp_head->next;
MemFree(asp_head);
asp_head = asp;
}
}
}
/* SECTION 2c */
static Int4 AlnMgr2MapSegStartToSegStart(SeqAlignPtr sap, Int4 pos, Int4 row1, Int4 row2, Int4 len)
{
Int4 diff;
DenseSegPtr dsp;
Int4 pos2;
Int4 seg;
Uint1 strand1;
Uint1 strand2;
if (sap == NULL)
return -1;
seg = AlnMgr2GetSegForStartPos(sap, pos, row1);
if (seg < 0)
return -1;
dsp = (DenseSegPtr)(sap->segs);
if (dsp->starts[dsp->dim*seg+row2-1] == -1)
return -1;
strand1 = dsp->strands[dsp->dim*seg+row1-1];
strand2 = dsp->strands[dsp->dim*seg+row2-1];
if (strand1 != strand2)
pos = pos + len - 1;
if (strand1 == Seq_strand_minus)
diff = dsp->lens[seg] - (pos - dsp->starts[dsp->dim*seg+row1-1]) - 1;
else
diff = pos - dsp->starts[dsp->dim*seg+row1-1];
if (diff > dsp->lens[seg]) /* unaligned here */
return -1;
if (strand2 == Seq_strand_minus)
pos2 = dsp->starts[dsp->dim*seg+row2-1] + dsp->lens[seg] - diff -1;
else
pos2 = dsp->starts[dsp->dim*seg+row2-1]+ diff;
return pos2;
}
/* SECTION 2c */
static Int4 AlnMgr2GetSegForStartPos(SeqAlignPtr sap, Int4 pos, Int4 row)
{
Uint2Ptr array;
DenseSegPtr dsp;
Int4 L;
Int4 mid;
Int4 offset;
Int4 R;
SAIndex2Ptr saip;
SARowDat2Ptr srdp;
Int4 start;
Int4 stop;
Uint1 strand;
if (sap == NULL || sap->saip == NULL || row < 1)
return -1;
AlnMgr2GetNthSeqRangeInSA(sap, row, &start, &stop);
if (pos < start || pos > stop)
return -1;
saip = (SAIndex2Ptr)(sap->saip);
if (row > saip->numrows)
return -1;
srdp = saip->srdp[row-1];
strand = AlnMgr2GetNthStrand(sap, row);
dsp = (DenseSegPtr)(sap->segs);
L = 0;
R = srdp->numsect - 1;
if (strand != Seq_strand_minus)
{
while (L < R)
{
mid = MIN((L + R)/2, srdp->numsect-2);
if (dsp->starts[(srdp->sect[mid + 1])*(dsp->dim)+row-1] <= pos)
L = mid+1;
else
R = mid;
}
} else
{
while (L < R)
{
mid = (L + R)/2;
if (dsp->starts[(srdp->sect[mid])*(dsp->dim)+row-1] > pos)
L = mid + 1;
else
R = mid;
}
}
offset = pos - dsp->starts[(srdp->sect[L])*(dsp->dim)+row-1];
if (offset >= dsp->lens[srdp->sect[L]])
return -2; /* this is an insert */
if (saip->anchor > 0)
{
array = saip->srdp[saip->anchor-1]->sect;
R = binary_search_on_uint2_list(array, srdp->sect[L], saip->srdp[saip->anchor-1]->numsect);
L = R;
}
return srdp->sect[L];
}
static Int4 GetNextStart (DenseSegPtr dsp, Int4 row, Int4 col, Int4Ptr pnext_start_col)
{
Int4 next_start_col;
if (dsp == NULL || row < 0 || row >= dsp->dim || col < 0 || col >= dsp->numseg)
{
return -1;
}
for (next_start_col = col + 1;
next_start_col < dsp->numseg
&& dsp->starts[(next_start_col * dsp->dim) + row] == -1;
next_start_col++)
{
}
if (next_start_col < dsp->numseg)
{
if (pnext_start_col != NULL)
{
*pnext_start_col = next_start_col;
}
return dsp->starts[(next_start_col * dsp->dim) + row];
}
else
{
return -1;
}
}
static void AlnMgr2CondenseColumns(DenseSegPtr dsp)
/***************************************************************************
*
* AlnMgr2CondenseColumns finds adjacent columns which appear to align but
* were not put in one column by the mixing mechanism because the input was
* a set of pairwise alignment with a gap on the common sequence in this
* segment. Or graphically:
*
* ----- ----- ----- ----- -----
* AACCG ----- ----- ----- becomes AACCG
* ----- AACCG ----- ----- AACCG
* ----- ----- AACCG ----- AACCG
* ----- ----- ----- AACCG AACCG
*
***************************************************************************/
{
int gap_start_seg = -1;
int gap_end_seg = -1;
int row, seg, base_col, col, next_start, next_start_col;
Boolean can_fit;
for (seg = 0; seg < dsp->numseg; ++seg) {
if (dsp->starts[dsp->dim * seg] == -1) {
if (gap_start_seg == -1) {
gap_start_seg = seg;
}
else {
if (seg == dsp->numseg - 1) {
gap_end_seg = seg + 1;
}
}
}
else {
if (gap_start_seg != -1) {
gap_end_seg = seg;
}
}
if (gap_end_seg != -1) {
for (base_col = gap_start_seg; base_col<gap_end_seg; ++base_col) {
int len = dsp->lens[base_col];
for (col = base_col + 1; col<gap_end_seg; ++col) {
if (dsp->lens[col] != len) {
continue;
}
can_fit = TRUE;
for (row = 0; row < dsp->dim; ++row) {
if (dsp->starts[dsp->dim * col + row] != -1 &&
dsp->starts[dsp->dim * base_col + row] != -1) {
can_fit = FALSE;
break;
}
else if (dsp->starts[dsp->dim * col + row] != -1)
{
/* make sure we aren't going to disturb the order of
* the starts */
next_start = GetNextStart (dsp, row, base_col, &next_start_col);
if (next_start > -1
&& next_start < dsp->starts[dsp->dim * col + row]
&& next_start_col < col)
{
can_fit = FALSE;
}
}
}
if (can_fit) {
for (row = 0; row<dsp->dim; ++row) {
if (dsp->starts[dsp->dim * col + row] != -1) {
dsp->starts[dsp->dim * base_col + row] =
dsp->starts[dsp->dim * col + row];
}
}
/* remove column col */
{{
Int4Ptr starts, lens;
Uint1Ptr strands;
Uint4 pos, new_pos;
starts = (Int4Ptr)MemNew(dsp->dim*(dsp->numseg-1)*sizeof(Int4));
strands = (Uint1Ptr)MemNew(dsp->dim*(dsp->numseg-1)*sizeof(Uint1));
lens = (Int4Ptr)MemNew((dsp->numseg-1)*sizeof(Int4));
for (pos=0; pos<dsp->dim*col; pos++) {
starts[pos] = dsp->starts[pos];
strands[pos] = dsp->strands[pos];
}
for (new_pos=pos, pos+=dsp->dim; pos<dsp->dim*dsp->numseg;
pos++, new_pos++) {
starts[new_pos] = dsp->starts[pos];
strands[new_pos] = dsp->strands[pos];
}
for (pos=0; pos<col; pos++) {
lens[pos] = dsp->lens[pos];
}
for (new_pos=pos, pos++; pos<dsp->numseg; pos++, new_pos++) {
lens[new_pos] = dsp->lens[pos];
}
MemFree(dsp->starts);
MemFree(dsp->strands);
dsp->starts = starts;
dsp->strands = strands;
dsp->lens = lens;
dsp->numseg--;
}}
--gap_end_seg;
--seg;
--col;
}
}
}
gap_start_seg = -1;
gap_end_seg = -1;
}
}
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2CondenseRows finds rows of a dense-seg structure that are related
* and that could be condensed into a single row (or fewer rows). It then
* calls AlnMgr2DoCondense to condense those rows into continuous or
* discontinuous rows. whichrow designates which row to merge, if
* less than 1, the function tries to merge the last row.
*
***************************************************************************/
static void AlnMgr2CondenseRows(DenseSegPtr dsp, Int4 whichrow)
{
Boolean done;
Int4 i;
Int4 j;
Int4 k;
Int4 numrows;
AMCdRowPtr row;
AMCdRowPtr PNTR rowarray;
SeqIdPtr sip;
SeqIdPtr targetsip;
sip = dsp->ids;
rowarray = (AMCdRowPtr PNTR)MemNew((dsp->dim)*sizeof(AMCdRowPtr));
if (whichrow < 1 || whichrow > dsp->dim)
whichrow = dsp->dim;
for (i=0; i<dsp->dim; i++)
{
row = (AMCdRowPtr)MemNew(sizeof(AMCdRow));
row->sip = SeqIdDup(sip);
sip = sip->next;
row->strand = dsp->strands[i];
row->rownum = i+1;
rowarray[i] = row;
if (i+1 == whichrow)
targetsip = row->sip;
}
HeapSort(rowarray, i, sizeof(rowarray), AlnMgr2CompareCdRows);
numrows = dsp->dim;
j = -1; /* j marks the first occurrence of each sip */
for (i=0; j==-1 && i<numrows; i++)
{
if (SeqIdComp(rowarray[i]->sip, targetsip) == SIC_YES)
{
j = i;
if (rowarray[i]->rownum == whichrow) /* no other rows w/sip */
{
for (i=0; i<numrows; i++)
{
SeqIdFree(rowarray[i]->sip);
MemFree(rowarray[i]);
}
MemFree(rowarray);
return;
}
}
}
sip = SeqIdDup(rowarray[j]->sip);
done = FALSE;
for (i=j; !done && rowarray[i]->rownum < whichrow; i++)
{
if (SeqIdComp(rowarray[i]->sip, sip) == SIC_YES)
{
if (rowarray[i]->strand == rowarray[j]->strand)
{
if (AlnMgr2DoCondense(dsp, rowarray[i]->rownum, whichrow))
{
for (k=0; k<numrows; k++)
{
if (rowarray[k]->rownum > rowarray[i]->rownum)
{
rowarray[k]->rownum--;
whichrow--;
}
}
}
}
} else
{
done = TRUE;
SeqIdFree(sip);
sip = SeqIdDup(rowarray[i]->sip);
j = i;
}
}
SeqIdFree(sip);
for (i=0; i<numrows; i++)
{
SeqIdFree(rowarray[i]->sip);
MemFree(rowarray[i]);
}
MemFree(rowarray);
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2DoCondense arithmetically condenses two related rows of a dense-seg
* structure into a single continuous row, a single discontinuous row, or
* two rows with different information than before.
*
***************************************************************************/
static Boolean AlnMgr2DoCondense(DenseSegPtr dsp, Int4 rownum1, Int4 rownum2)
{
Int4 aln;
SeqAlignPtr fake_sap;
Boolean fits;
Boolean found;
Int4 i;
SeqIdPtr id;
SeqIdPtr id_head;
SeqIdPtr id_prev;
Int4 j;
Int4 k;
Int4 max1;
Int4 max2;
Boolean merged;
Int4 min1;
Int4 min2;
SAIndex2Ptr saip;
Boolean someseq1;
Boolean someseq2;
Int4Ptr starts;
Uint1 strand1;
Uint1 strand2;
Uint1Ptr strands;
AM_Small2Ptr window;
AM_Small2Ptr window_head;
AM_Small2Ptr window_prev;
/* always merge up to rownum1 (better rows are first) */
if (rownum1 > rownum2)
{
i = rownum2;
rownum2 = rownum1;
rownum1 = i;
}
strand1 = dsp->strands[rownum1-1];
strand2 = dsp->strands[rownum2-1];
if (strand1 != strand2)
return FALSE;
i = 0;
window_head = window_prev = NULL;
while (i < dsp->numseg)
{
j = i;
someseq1 = someseq2 = FALSE;
if (dsp->starts[dsp->dim*j+rownum1-1] >= 0)
{
someseq1 = TRUE;
while (j<dsp->numseg && dsp->starts[dsp->dim*j+rownum2-1] < 0)
{
j++;
}
} else if (dsp->starts[dsp->dim*j+rownum2-1] >= 0)
{
someseq2 = TRUE;
while (j<dsp->numseg && dsp->starts[dsp->dim*j+rownum1-1] < 0)
{
j++;
}
}
fits = FALSE;
if (j > i)
{
if (strand1 == Seq_strand_minus)
{
if (someseq1 == FALSE)
{
min1 = -1;
for (k=j; min1 == -1 && k<dsp->numseg; k++)
{
if (dsp->starts[dsp->dim*k+rownum1-1] > -1)
min1 = dsp->starts[dsp->dim*k+rownum1-1]+dsp->lens[k]-1;
}
max1 = -1;
for (k=(i-1); max1 == -1 && k>=0; k--)
{
max1 = dsp->starts[dsp->dim*k+rownum1-1];
}
} else
{
min1 = -1;
for (k=j-1; min1 == -1 && k>=i; k--)
{
min1 = dsp->starts[dsp->dim*(k)+rownum1-1];
}
max1 = -1;
for (k=i; min1 == -1 && k<j; k++)
{
if (dsp->starts[dsp->dim*k+rownum1-1] >= 0)
max1 = dsp->starts[dsp->dim*k+rownum1-1] + dsp->lens[k] -1;
}
}
} else
{
if (someseq1 == FALSE)
{
min1 = -1;
for (k=i-1; min1 == -1 && k >= 0; k--)
{
if (dsp->starts[dsp->dim*k+rownum1-1] > -1)
min1 = dsp->starts[dsp->dim*k+rownum1-1]+dsp->lens[k]-1;
}
max1 = -1;
for (k=j; max1 == -1 && k<dsp->numseg; k++)
{
max1 = dsp->starts[dsp->dim*k+rownum1-1];
}
} else
{
min1 = -1;
for (k=i; min1 == -1 && k<j; k++)
{
min1 = dsp->starts[dsp->dim*k+rownum1-1];
}
max1 = -1;
for (k=j-1; max1 == -1 && k>i; k--)
{
if (dsp->starts[dsp->dim*k+rownum1-1] >= 0)
max1 = dsp->starts[dsp->dim*(k)+rownum1-1] + dsp->lens[k] - 1;
}
}
}
if (strand2 == Seq_strand_minus)
{
if (someseq2 == FALSE)
{
min2 = -1;
for (k=j; min2 == -1 && k<dsp->numseg; k++)
{
if (dsp->starts[dsp->dim*k+rownum2-1] > -1)
min2 = dsp->starts[dsp->dim*k+rownum2-1]+dsp->lens[k]-1;
}
max2 = -1;
for (k=(i-1); max2 == -1 && k>=0; k--)
{
max2 = dsp->starts[dsp->dim*k+rownum2-1];
}
} else
{
min2 = -1;
for (k=j-1; min2 == -1 && k>=i; k--)
{
min2 = dsp->starts[dsp->dim*(k)+rownum2-1];
}
max2 = -1;
for (k=i; max2 == -1 && k<j; k++)
{
if (dsp->starts[dsp->dim*k+rownum2-1] >= 0)
max2 = dsp->starts[dsp->dim*k+rownum2-1] + dsp->lens[k]-1;
}
}
} else
{
if (someseq2 == FALSE)
{
min2 = -1;
for (k=i-1; min2 == -1 && k >= 0; k--)
{
if (dsp->starts[dsp->dim*k+rownum2-1] > -1)
min2 = dsp->starts[dsp->dim*k+rownum2-1]+dsp->lens[k]-1;
}
max2 = -1;
for (k=j; max2 == -1 && k<dsp->numseg; k++)
{
max2 = dsp->starts[dsp->dim*k+rownum2-1];
}
} else
{
min2 = -1;
for (k=i; min2 == -1 && k<j; k++)
{
min2 = dsp->starts[dsp->dim*k+rownum2-1];
}
max2 = -1;
for (k=j-1; max2 == -1 && k>=i; k--)
{
if (dsp->starts[dsp->dim*(k)+rownum2-1] >= 0)
max2 = dsp->starts[dsp->dim*(k)+rownum2-1] + dsp->lens[k] - 1;
}
}
}
if (someseq1 == FALSE)
{
if ((min1 < min2 || min2 == -1) && (max1 > max2 || max1 == -1))
fits = TRUE;
} else
{
if ((min2 < min1 || min1 == -1) && (max2 > max1 || max2 == -1))
fits = TRUE;
}
window = (AM_Small2Ptr)MemNew(sizeof(AM_Small2));
window->n1 = i;
window->n2 = j-1;
if (!fits)
window->n4 = -1;
if (window_head != NULL)
{
window_prev->next = window;
window_prev = window;
} else
window_head = window_prev = window;
}
if (i == j)
i++;
else
i = j;
}
if (window_head == NULL)
return FALSE;
fake_sap = SeqAlignNew();
fake_sap->segtype = SAS_DENSEG;
fake_sap->segs = (Pointer)dsp;
AlnMgr2IndexSingleChildSeqAlign(fake_sap);
aln = AlnMgr2GetNumAlnBlocks(fake_sap);
if (aln == 1) /* only merge if there is a single fitted window flanked by gaps */
/*or if there are several contiguous fitted windows flanked by gaps */
{
if (window_head->next != NULL && window_head->n4 == 0)
{
window = window_head->next;
while (window_head->n2+1 < dsp->numseg && dsp->starts[dsp->dim*(window_head->n2+1)+rownum1-1] == -1 && dsp->starts[dsp->dim*(window_head->n2+1)+rownum2-1] == -1)
{
window_head->n2++;
}
while (window != NULL && window->n4 == 0 && window->n1 == window_head->n2+1)
{
window_head->n2 = window->n2;
window = window->next;
while (window_head->n2+1 < dsp->numseg && dsp->starts[dsp->dim*(window_head->n2+1)+rownum1-1] == -1 && dsp->starts[dsp->dim*(window_head->n2+1)+rownum2-1] == -1)
{
window_head->n2++;
}
}
if (window != NULL)
{
while (window_head != NULL)
{
window = window_head->next;
MemFree(window_head);
window_head = window;
}
fake_sap->segs = NULL;
SeqAlignFree(fake_sap);
return FALSE;
}
}
if (window_head->n4 == -1)
{
while (window_head != NULL)
{
window = window_head->next;
MemFree(window_head);
window_head = window;
}
fake_sap->segs = NULL;
SeqAlignFree(fake_sap);
return FALSE;
}
found = FALSE;
for (i=0; !found && i<window_head->n1; i++)
{
if (dsp->starts[dsp->dim*i+rownum1-1] != -1 && dsp->starts[dsp->dim*i+rownum2-1] != -1)
found = TRUE;
}
for (i=window_head->n2+1; !found && i<dsp->numseg; i++)
{
if (dsp->starts[dsp->dim*i+rownum1-1] != -1 && dsp->starts[dsp->dim*i+rownum2-1] != -1)
found = TRUE;
}
if (found)
{
while (window_head != NULL)
{
window = window_head->next;
MemFree(window_head);
window_head = window;
}
fake_sap->segs = NULL;
SeqAlignFree(fake_sap);
return FALSE;
}
/* merge whole row up to rownum1 */
for (i=0; i<dsp->numseg; i++)
{
dsp->starts[dsp->dim*i+rownum1-1] = MAX(dsp->starts[dsp->dim*i+rownum1-1], dsp->starts[dsp->dim*i+rownum2-1]);
}
starts = (Int4Ptr)MemNew((dsp->dim-1)*(dsp->numseg)*sizeof(Int4));
strands = (Uint1Ptr)MemNew((dsp->dim-1)*(dsp->numseg)*sizeof(Uint1));
k = 0;
for (i=0; i<dsp->dim; i++)
{
if (i != rownum2-1)
{
for (j=0; j<dsp->numseg; j++)
{
starts[(dsp->dim-1)*j+k] = dsp->starts[dsp->dim*j+i];
strands[(dsp->dim-1)*j+k] = dsp->strands[dsp->dim*j+i];
}
k++;
}
}
MemFree(dsp->starts);
MemFree(dsp->strands);
dsp->starts = starts;
dsp->strands = strands;
dsp->dim--;
id_head = id_prev = NULL;
id = dsp->ids;
j = 0;
while (id != NULL)
{
if (j+1 != rownum2)
{
if (id_head != NULL)
{
id_prev->next = SeqIdDup(id);
id_prev = id_prev->next;
} else
id_head = id_prev = SeqIdDup(id);
}
j++;
id = id->next;
}
SeqIdSetFree(dsp->ids);
dsp->ids = id_head;
while (window_head != NULL)
{
window = window_head->next;
MemFree(window_head);
window_head = window;
}
fake_sap->segs = NULL;
SeqAlignFree(fake_sap);
return TRUE;
}
/* now go through and find the largest piece of every window that can be merged */
/* (can't split up an aligned region with the merge, though) */
window = window_head;
saip = (SAIndex2Ptr)(fake_sap->saip);
while (window != NULL)
{
j = k = -1;
found = FALSE;
for (i=0; !found && i<window->n1; i++)
{
if (dsp->starts[dsp->dim*i+rownum1-1] != -1 && dsp->starts[dsp->dim*i+rownum2-1] != -1)
found = TRUE;
}
if (!found)
j = window->n1;
found = FALSE;
for (i=window->n2+1; !found && i<dsp->numseg; i++)
{
if (dsp->starts[dsp->dim*i+rownum1-1] != -1 && dsp->starts[dsp->dim*i+rownum2-1] != -1)
found = TRUE;
}
if (!found)
k = window->n2;
if (j == -1)
{
found = FALSE;
for (i = window->n1-1; !found && i<window->n2; i++)
{
j = binary_search_on_uint4_list(saip->unaln, i, saip->numunaln);
if (j == i)
found = TRUE;
else
j = -1;
}
}
if (k == -1)
{
found = FALSE;
for (i = window->n2; !found && i>=window->n1; i++)
{
k = binary_search_on_uint4_list(saip->unaln, i, saip->numunaln);
if (k == i)
found = TRUE;
else
k = -1;
}
}
if (j > -1 && k > -1 && k > j)
{
window->n1 = j+1;
window->n2 = k;
} else
window->n1 = -1;
window = window->next;
}
window = window_head;
while (window != NULL)
{
if (window->n4 == -1 && i >= 0) /* see if it fits now */
{
i = window->n1;
j = window->n2+1;
if (strand1 == Seq_strand_minus)
{
if (dsp->starts[dsp->dim*(j-1)+rownum1-1] == -1)
{
min1 = -1;
for (k=j; min1 == -1 && k<dsp->numseg; k++)
{
min1 = dsp->starts[dsp->dim*k+rownum1-1];
}
max1 = -1;
for (k=(i-1); max1 == -1 && k>=0; k--)
{
max1 = dsp->starts[dsp->dim*k+rownum1-1];
}
} else
{
min1 = dsp->starts[dsp->dim*(j-1)+rownum1-1];
max1 = dsp->starts[dsp->dim*i+rownum1-1] + dsp->lens[i];
}
} else
{
if (dsp->starts[dsp->dim*(j-1)+rownum1-1] == -1)
{
min1 = -1;
for (k=i-1; min1 == -1 && k >= 0; k--)
{
min1 = dsp->starts[dsp->dim*k+rownum1-1];
}
max1 = -1;
for (k=j; max1 == -1 && k<dsp->numseg; k++)
{
max1 = dsp->starts[dsp->dim*k+rownum1-1];
}
} else
{
min1 = dsp->starts[dsp->dim*i+rownum1-1];
max1 = dsp->starts[dsp->dim*(j-1)+rownum1-1] + dsp->lens[j-1];
}
}
if (strand2 == Seq_strand_minus)
{
if (dsp->starts[dsp->dim*(j-1)+rownum2-1] == -1)
{
min2 = -1;
for (k=j; min2 == -1 && k<dsp->numseg; k++)
{
min2 = dsp->starts[dsp->dim*k+rownum2-1];
}
max2 = -1;
for (k=(i-1); max2 == -1 && k>=0; k--)
{
max2 = dsp->starts[dsp->dim*k+rownum2-1];
}
} else
{
min2 = dsp->starts[dsp->dim*(j-1)+rownum2-1];
max2 = dsp->starts[dsp->dim*i+rownum2-1] + dsp->lens[i];
}
} else
{
if (dsp->starts[dsp->dim*(j-1)+rownum2-1] == -1)
{
min2 = -1;
for (k=i-1; min2 == -1 && k >= 0; k--)
{
min2 = dsp->starts[dsp->dim*k+rownum2-1];
}
max2 = -1;
for (k=j; max2 == -1 && k<dsp->numseg; k++)
{
max2 = dsp->starts[dsp->dim*k+rownum2-1];
}
} else
{
min2 = dsp->starts[dsp->dim*i+rownum2-1];
max2 = dsp->starts[dsp->dim*(j-1)+rownum2-1] + dsp->lens[j-1];
}
}
if (dsp->starts[dsp->dim*j+rownum1-1] == -1)
{
if (min1 < min2 && (max1 > max2 || max1 == -1))
window->n4 = 0;
} else
{
if (min2 < min1 && (max2 > max1 || max2 == -1))
window->n4 = 0;
}
}
if (window->n1 >= 0 && window->n4 >= 0)
{
for (i=window->n1; i<=window->n2; i++)
{
dsp->starts[dsp->dim*i+rownum1-1] = MAX(dsp->starts[dsp->dim*i+rownum1-1], dsp->starts[dsp->dim+i+rownum2-1]);
}
}
window = window->next;
}
found = FALSE;
/* check to see if rownum2 is all gaps now */
for (i=0; !found && i<dsp->numseg; i++)
{
if (dsp->starts[dsp->dim*i+rownum2-1] != -1)
found = TRUE;
}
merged = FALSE;
if (!found) /* just gaps */
{
/* merge whole row up to rownum1 */
for (i=0; i<dsp->numseg; i++)
{
dsp->starts[dsp->dim*i+rownum1-1] = MAX(dsp->starts[dsp->dim*i+rownum1-1], dsp->starts[dsp->dim*i+rownum2-1]);
}
starts = (Int4Ptr)MemNew((dsp->dim-1)*(dsp->numseg)*sizeof(Int4));
strands = (Uint1Ptr)MemNew((dsp->dim-1)*(dsp->numseg)*sizeof(Uint1));
k = 0;
for (i=0; i<dsp->dim; i++)
{
if (i != rownum2-1)
{
for (j=0; j<dsp->numseg; j++)
{
starts[dsp->dim*j+k] = dsp->starts[dsp->dim*j+i];
strands[dsp->dim*j+k] = dsp->strands[dsp->dim*j+i];
}
k++;
}
}
MemFree(dsp->starts);
MemFree(dsp->strands);
dsp->starts = starts;
dsp->strands = strands;
dsp->dim--;
id_head = id_prev = NULL;
id = dsp->ids;
j = 0;
while (id != NULL)
{
if (j+1 != rownum2)
{
if (id_head != NULL)
{
id_prev->next = SeqIdDup(id);
id_prev = id_prev->next;
} else
id_head = id_prev = SeqIdDup(id);
}
j++;
id = id->next;
}
SeqIdSetFree(dsp->ids);
dsp->ids = id_head;
merged = TRUE;
}
while (window_head != NULL)
{
window = window_head->next;
MemFree(window_head);
window_head = window;
}
fake_sap->segs = NULL;
SeqAlignFree(fake_sap);
return merged;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2CompareCdRows is the HeapSort callback for AlnMgr2CondenseRows.
* It puts the CDRows in order first by seqid and secondarily by row number.
*
***************************************************************************/
static int LIBCALLBACK AlnMgr2CompareCdRows(VoidPtr ptr1, VoidPtr ptr2)
{
Int4 i;
AMCdRowPtr row1;
AMCdRowPtr row2;
if (ptr1 == NULL || ptr2 == NULL)
return 0;
row1 = *((AMCdRowPtr PNTR)ptr1);
row2 = *((AMCdRowPtr PNTR)ptr2);
i = AlnMgr2OrderSeqIds(row1->sip, row2->sip);
if (i == 0) /* sort from least rownum to greatest within each seqid */
{
if (row1->rownum < row2->rownum)
return -1;
else
return 1;
} else
return i;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2CompareAsps is a HeapSort callback for AlnMgr2AddInNewSA. It
* compares the starts (n1) of the two AM_Small2Ptrs; if those are the same
* it compares the types.
*
***************************************************************************/
static int LIBCALLBACK AlnMgr2CompareAsps(VoidPtr ptr1, VoidPtr ptr2)
{
AM_Small2Ptr asp1;
AM_Small2Ptr asp2;
if (ptr1 != NULL && ptr2 != NULL)
{
asp1 = *((AM_Small2Ptr PNTR)ptr1);
asp2 = *((AM_Small2Ptr PNTR)ptr2);
if (asp1->n1 < asp2->n1)
return -1;
else if (asp1->n1 > asp2->n1)
return 1;
else if (asp1->n5 < asp2->n5)
return -1;
else if (asp1->n5 > asp2->n5)
return 1;
else
{
if (asp1->n3 == AM_GAP && asp2->n3 == AM_GAP)
{
if (asp1->n2 < asp2->n2)
return -1;
if (asp1->n2 > asp2->n2)
return 1;
}
if (asp1->n3 == AM_START)
{
if (asp2->n3 == AM_STOP)
return -1;
else if (asp2->n3 == AM_GAP)
return -1;
else if (asp2->n3 == AM_HARDSTOP)
return -1;
else
return 0;
} else if (asp1->n3 == AM_STOP)
{
if (asp2->n3 == AM_START)
return 1;
else if (asp2->n3 == AM_GAP)
return 1;
else if (asp2->n3 == AM_HARDSTOP)
return -1;
else
return 0;
} else if (asp1->n3 == AM_GAP)
{
if (asp2->n3 == AM_START)
return 1;
else if (asp2->n3 == AM_STOP)
return -1;
else if (asp2->n3 == AM_HARDSTOP)
return -1;
else
return 0;
} else if (asp1->n3 == AM_HARDSTOP)
{
if (asp2->n3 == AM_START)
return 1;
else if (asp2->n3 == AM_STOP)
return 1;
else if (asp2->n3 == AM_GAP)
return 1;
else
return 0;
}
}
}
return 0;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2CompareAspsMinus is a HeapSort callback for AlnMgr2AddInNewSA. It
* compares the starts (n1) of the two AM_Small2Ptrs; if those are the same
* it compares the types. The only difference from AlnMgr2CompareAsps is
* that it sorts the structures in the opposite order.
*
***************************************************************************/
static int LIBCALLBACK AlnMgr2CompareAspsMinus(VoidPtr ptr1, VoidPtr ptr2)
{
AM_Small2Ptr asp1;
AM_Small2Ptr asp2;
if (ptr1 != NULL && ptr2 != NULL)
{
asp1 = *((AM_Small2Ptr PNTR)ptr1);
asp2 = *((AM_Small2Ptr PNTR)ptr2);
if (asp1->n1 > asp2->n1)
return -1;
else if (asp1->n1 < asp2->n1)
return 1;
else if (asp1->n5 < asp2->n5)
return -1;
else if (asp1->n5 > asp2->n5)
return 1;
else
{
if (asp1->n3 == AM_GAP && asp2->n3 == AM_GAP)
{
if (asp1->n2 < asp2->n2)
return -1;
if (asp1->n2 > asp2->n2)
return 1;
}
if (asp1->n3 == AM_START)
{
if (asp2->n3 == AM_STOP)
return 1;
else if (asp2->n3 == AM_GAP)
return -1;
else if (asp2->n3 == AM_HARDSTOP)
return 1;
else
return 0;
} else if (asp1->n3 == AM_STOP)
{
if (asp2->n3 == AM_START)
return -1;
else if (asp2->n3 == AM_GAP)
return -1;
else if (asp2->n3 == AM_HARDSTOP)
return 1;
else
return 0;
} else if (asp1->n3 == AM_GAP)
{
if (asp2->n3 == AM_START)
return 1;
else if (asp2->n3 == AM_STOP)
return 1;
else if (asp2->n3 == AM_HARDSTOP)
return 1;
else
return 0;
} else if (asp1->n3 == AM_HARDSTOP)
{
if (asp2->n3 == AM_START)
return -1;
else if (asp2->n3 == AM_STOP)
return -1;
else if (asp2->n3 == AM_GAP)
return -1;
else
return 0;
}
}
}
return 0;
}
/* SECTION 2c */
/***************************************************************************
*
* AlnMgr2GetFirstSharedRow takes two indexed or unindexed dense-seg
* seqaligns and returns the row numbers of the first sequence that is
* shared between the two alignments. If the alignments do not share any
* sequences, both n1 and n2 are set to 0.
*
***************************************************************************/
static void AlnMgr2GetFirstSharedRow(SeqAlignPtr sap1, SeqAlignPtr sap2, Int4Ptr n1, Int4Ptr n2)
{
DenseSegPtr dsp1;
DenseSegPtr dsp2;
Int4 i;
Int4 j;
SeqIdPtr sip1;
SeqIdPtr sip2;
dsp1 = (DenseSegPtr)(sap1->segs);
dsp2 = (DenseSegPtr)(sap2->segs);
sip1 = dsp1->ids;
i = 1;
while (sip1 != NULL)
{
j = 1;
sip2 = dsp2->ids;
while (sip2 != NULL)
{
if (SeqIdComp(sip1, sip2) == SIC_YES)
{
*n1 = i;
*n2 = j;
return;
}
sip2 = sip2->next;
j++;
}
sip1 = sip1->next;
i++;
}
/* nothing found */
*n1 = 0;
*n2 = 0;
}
/* SECTION 2d */
static SeqIdPtr AlnMgr2SeqIdListsOverlap(SeqIdPtr sip1, SeqIdPtr sip2)
{
SeqIdPtr sip;
SeqIdPtr sip_tmp;
if (sip1 == NULL || sip2 == NULL)
return NULL;
sip = sip1;
while (sip != NULL)
{
sip_tmp = sip2;
while (sip_tmp != NULL)
{
if (SeqIdComp(sip, sip_tmp) == SIC_YES)
return sip;
sip_tmp = sip_tmp->next;
}
sip = sip->next;
}
return NULL;
}
/***************************************************************************
*
* AlnMgr2OrderSeqIds simply alphabetizes printed seqids in order to sort
* them in order to group identical ones in a set.
*
***************************************************************************/
static Int4 AlnMgr2OrderSeqIds(SeqIdPtr sip1, SeqIdPtr sip2)
{
Char txt1[42];
Char txt2[42];
if (sip1 == NULL && sip2 == NULL)
return 0;
if (sip1 == NULL && sip2 != NULL)
return 1;
if (sip1 != NULL && sip2 == NULL)
return -1;
SeqIdWrite(sip1, txt1, PRINTID_TEXTID_ACC_VER, 41);
SeqIdWrite(sip2, txt2, PRINTID_TEXTID_ACC_VER, 41);
txt1[41] = txt2[41] = '\0';
return StringICmp(txt1, txt2);
}
/* SECTION 2d */
/***************************************************************************
*
* AlnMgr2SetUnaln takes an indexed alignment and sets the numunaln and
* unaln array fields. The unaligned regions are numbered the same
* regardless of whether the alignment is anchored, although they will
* most likely be accessed and displayed differently.
*
***************************************************************************/
static void AlnMgr2SetUnaln(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
AM_Small2Ptr ams;
AM_Small2Ptr ams_head;
AM_Small2Ptr ams_prev;
AM_Small2Ptr PNTR amsarray;
DenseSegPtr dsp;
Int4 i;
Int4 j;
SAIndex2Ptr saip;
if (sap == NULL || sap->saip == NULL)
return;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
dsp = (DenseSegPtr)(sap->segs);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
} else
return;
MemFree(saip->unaln);
saip->unaln = NULL;
ams_head = ams_prev = NULL;
for (i=0; i<saip->numrows; i++)
{
for (j=0; j<saip->srdp[i]->numunaln; j++)
{
ams = (AM_Small2Ptr)MemNew(sizeof(AM_Small2));
ams->n1 = saip->srdp[i]->unaligned[j];
if (ams_head != NULL)
{
ams_prev->next = ams;
ams_prev = ams;
} else
ams_head = ams_prev = ams;
}
}
if (ams_head == NULL)
{
saip->numunaln = -1;
return;
}
j = 0;
ams = ams_head;
while (ams != NULL)
{
j++;
ams = ams->next;
}
amsarray = (AM_Small2Ptr PNTR)MemNew(j*sizeof(AM_Small2Ptr));
j = 0;
ams = ams_head;
while (ams != NULL)
{
amsarray[j] = ams;
j++;
ams = ams->next;
}
HeapSort(amsarray, j, sizeof(AM_Small2Ptr), AlnMgr2CompareUnalnAMS);
saip->numunaln = 1;
for (i=1; i<j; i++)
{
if (amsarray[i]->n1 != amsarray[i-1]->n1)
saip->numunaln++;
}
saip->unaln = (Uint4Ptr)MemNew(saip->numunaln*sizeof(Uint4));
saip->unaln[0] = amsarray[0]->n1;
saip->numunaln = 1;
for (i=1; i<j; i++)
{
if (amsarray[i]->n1 != amsarray[i-1]->n1)
{
saip->unaln[saip->numunaln] = amsarray[i]->n1;
saip->numunaln++;
}
}
for (i=0; i<j; i++)
{
MemFree(amsarray[i]);
}
MemFree(amsarray);
}
/* SECTION 2d */
/***************************************************************************
*
* AlnMgr2CompareUnalnAMS is the HeapSort callback for AlnMgr2SetUnaln;
* it simply compares two AM_Small2 structures and orders them by their
* n1 fields.
*
***************************************************************************/
static int LIBCALLBACK AlnMgr2CompareUnalnAMS(VoidPtr ptr1, VoidPtr ptr2)
{
AM_Small2Ptr ams1;
AM_Small2Ptr ams2;
if (ptr1 == NULL || ptr2 == NULL)
return 0;
ams1 = *((AM_Small2Ptr PNTR)ptr1);
ams2 = *((AM_Small2Ptr PNTR)ptr2);
if (ams1->n1 < ams2->n1)
return -1;
else if (ams1->n1 > ams2->n1)
return 1;
else
return 0;
}
/***************************************************************************
*
* SECTION 3: Functions for debugging
*
***************************************************************************/
/* SECTION 3 */
NLM_EXTERN void am_print_sa_index(SeqAlignPtr sap, FILE *ofp)
{
Int4 i;
Int4 j;
SAIndex2Ptr saip;
if (sap == NULL || sap->saip == NULL || sap->saip->indextype != INDEX_CHILD)
return;
saip = (SAIndex2Ptr)(sap->saip);
fprintf(ofp, "Rows: %d\n", saip->numrows);
fprintf(ofp, "Segments: %d\n", saip->numseg);
fprintf(ofp, "Anchor: %d\n", saip->anchor);
fprintf(ofp, "Alignment coordinates: ");
for (i=0; i<saip->numseg; i++)
{
fprintf(ofp, "%d ", saip->aligncoords[i]);
}
fprintf(ofp, "\n\n");
for (i=0; i<saip->numrows; i++)
{
fprintf(ofp, "row %d\n", i+1);
fprintf(ofp, "numsect: %d\n", saip->srdp[i]->numsect);
for (j=0; j<saip->srdp[i]->numsect; j++)
{
fprintf(ofp, "%d ", saip->srdp[i]->sect[j]);
}
fprintf(ofp, "\n");
fprintf(ofp, "numunsect: %d\n", saip->srdp[i]->numunsect);
for (j=0; j<saip->srdp[i]->numunsect; j++)
{
fprintf(ofp, "%d ", saip->srdp[i]->unsect[j]);
}
fprintf(ofp, "\n");
fprintf(ofp, "numinsect: %d\n", saip->srdp[i]->numinsect);
for (j=0; j<saip->srdp[i]->numinsect; j++)
{
fprintf(ofp, "%d ", saip->srdp[i]->insect[j]);
}
fprintf(ofp, "\n");
}
}
/* SECTION 3 */
/***************************************************************************
*
* AlnMgr2PrintSeqAlign prints an interleaved output of the entire
* indexed alignment, with 'linesize' characters on each line (max 200).
*
***************************************************************************/
NLM_EXTERN void AlnMgr2PrintSeqAlign(SeqAlignPtr sap, Int4 linesize, Boolean isnuc, FILE *ofp)
{
AlnMsg2Ptr amp;
BioseqPtr bsp;
Char buf[201];
Int4 ctr;
Boolean done;
Int4 i;
Int4 j;
Int4 len;
Boolean more;
Int4 numrows;
Int4 row;
Uint1 seqcode;
SeqIdPtr sip;
SeqPortPtr spp;
Char text[42];
if (sap == NULL || sap->saip == NULL || linesize > 200)
return;
if (isnuc)
seqcode = Seq_code_iupacna;
else
seqcode = Seq_code_iupacaa;
amp = AlnMsgNew2();
numrows = AlnMgr2GetNumRows(sap);
len = AlnMgr2GetAlnLength(sap, FALSE);
for (i=0; i<len; i+=linesize)
{
fprintf(ofp, "%d - %d\n", i, MIN(i+linesize-1, len-1));
for (row=0; row<numrows; row++)
{
sip = AlnMgr2GetNthSeqIdPtr(sap, row+1);
SeqIdWrite(sip, text, PRINTID_FASTA_SHORT, 41);
done = FALSE;
for (j=0; j<12; j++)
{
if (text[j] == '\0')
done = TRUE;
if (done == TRUE)
fprintf(ofp, " ");
else
fprintf(ofp, "%c", text[j]);
}
bsp = BioseqLockById(sip);
AlnMsgReNew2(amp);
amp->row_num = row+1;
amp->from_aln = i;
amp->to_aln = MIN(i+linesize-1, len-1);
while (more = AlnMgr2GetNextAlnBit(sap, amp))
{
if (amp->type == AM_GAP)
{
for (j=amp->from_row; j<=amp->to_row; j++)
{
fprintf(ofp, "-");
}
} else
{
spp = SeqPortNew(bsp, amp->from_row, amp->to_row, amp->strand, seqcode);
ctr = SeqPortRead(spp, (Uint1Ptr)buf, amp->to_row-amp->from_row+1);
buf[ctr] = '\0';
fwrite(buf, 1, ctr, ofp);
SeqPortFree(spp);
}
}
BioseqUnlock(bsp);
fprintf(ofp, "\n");
}
fprintf(ofp, "\n\n");
}
AlnMsgFree2(amp);
}
/* SECTION 3 */
NLM_EXTERN void AlnMgr2DumpIndexedAlnToFile(SeqAlignPtr sap, CharPtr filename)
{
AsnIoPtr aip;
AMAlignIndex2Ptr amaip;
SeqAlignPtr sap_tmp;
if (sap == NULL || sap->saip == NULL)
return;
if (sap->saip->indextype == INDEX_CHILD)
{
if (sap->dim == 0)
sap->dim = AlnMgr2GetNumRows(sap);
aip = AsnIoOpen(filename, "w");
SeqAlignAsnWrite(sap, aip, NULL);
AsnIoClose(aip);
return;
}
amaip = (AMAlignIndex2Ptr)(sap->saip);
aip = AsnIoOpen(filename, "w");
if (amaip->alnstyle != AM2_LITE)
{
amaip->sharedaln->dim = 0; /* mark it as the sharedaln */
SeqAlignAsnWrite(amaip->sharedaln, aip, NULL);
}
sap_tmp = sap;
if (sap->dim == 0)
sap->dim = AlnMgr2GetNumRows(sap);
while (sap_tmp != NULL)
{
SeqAlignAsnWrite(sap_tmp, aip, NULL);
sap_tmp = sap_tmp->next;
}
AsnIoClose(aip);
}
/***************************************************************************
*
* SECTION 4: API-level functions (and their helper functions) used to
* access an indexed alignment.
* SECTION 4a: AlnMgr2GetNextAlnBit and associated functions
* SECTION 4b: "GetNth" functions
* SECTION 4c: other functions for accessing the alignment
*
***************************************************************************/
/* SECTION 4a */
/***************************************************************************
*
* AlnMgr2GetNextAlnBit takes an indexed seqalign and returns it, piece
* by piece, in the row and across the range specified in the AlnMsg
* structure. amp->from_aln and amp->to_aln must be filled in; these are
* in alignment coordinates. AlnMgr2GetNextAlnBit will return the AlnMsg
* structure with amp->from_row and amp->to_row filled in. If amp->type is
* AM_SEQ, these numbers are sequence coordinates; if amp->type is AM_GAP
* the numbers are alignment coordinates and there is a gap in that row.
* AlnMgr2GetNextAlnBit returns one continuous piece of sequence or gap
* at each call, and keeps returning TRUE until it has returned all the
* information for the piece of the alignment requested.
*
***************************************************************************/
NLM_EXTERN Boolean AlnMgr2GetNextAlnBit(SeqAlignPtr sap, AlnMsg2Ptr amp) /* NEXT */
{
AMAlignIndex2Ptr amaip;
Uint2Ptr array;
Int4 arraylen;
Int4 ctr;
Int4 disc;
Int4 disc1;
DenseSegPtr dsp;
Int4 endoffset;
Boolean found;
Int4 i;
Int4 index;
Int4 intfrom;
Int4 intto;
Int4 j;
Int4 len;
Int4 offset;
SAIndex2Ptr saip;
SARowDat2Ptr srdp;
Int4 start_sect;
Int4 stop_sect;
Uint2Ptr trans;
Int4 translen;
if (sap == NULL || sap->saip == NULL || amp == NULL)
return FALSE;
if (amp->left_interrupt != NULL)
{
MemFree(amp->left_interrupt);
amp->left_interrupt = NULL;
}
if (amp->right_interrupt != NULL)
{
MemFree(amp->right_interrupt);
amp->right_interrupt = NULL;
}
if (sap->saip->indextype == INDEX_CHILD)
{
dsp = (DenseSegPtr)(sap->segs);
saip = (SAIndex2Ptr)(sap->saip);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return FALSE;
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
}
/* reality checks */
if (amp->row_num > saip->numrows)
return FALSE;
if (amp->len <= 0)
amp->len = AlnMgr2GetAlnLength(sap, FALSE);
if (amp->from_aln < 0 || amp->from_aln > amp->len-1 || amp->real_from > amp->to_aln)
return FALSE;
if (amp->to_aln == -1)
amp->to_aln = amp->len - 1;
if (amp->to_aln < amp->from_aln || amp->to_aln > amp->len-1)
return FALSE;
if (amp->real_from == -2)
amp->real_from = amp->from_aln;
amp->strand = AlnMgr2GetNthStrand(sap, amp->row_num);
srdp = saip->srdp[amp->row_num-1];
len = 0;
start_sect = binary_search_on_uint4_list(saip->aligncoords, amp->real_from, saip->numseg);
offset = amp->real_from - saip->aligncoords[start_sect];
endoffset = 0;
stop_sect = binary_search_on_uint4_list(saip->aligncoords, amp->to_aln, saip->numseg);
/* now figure out whether it starts in sequence or a gap, and figure out how */
/* long it continues in the same mode without interruption by inserts or unaligned */
/* regions; the whole contiguous stretch will be reported */
if (saip->anchor > 0)
{
trans = saip->srdp[saip->anchor-1]->sect;
translen = saip->srdp[saip->anchor-1]->numsect;
} else
{
trans = (Uint2Ptr)MemNew(dsp->numseg*sizeof(Uint2));
for (i=0; i<dsp->numseg; i++)
{
trans[i] = i;
}
translen = dsp->numseg;
}
arraylen = -1;
if ((index = binary_search_on_uint2_list(srdp->sect, trans[start_sect], srdp->numsect)) != -1)
{
amp->type = AM_SEQ;
array = srdp->sect;
arraylen = srdp->numsect;
} else if ((index = binary_search_on_uint2_list(srdp->unsect, trans[start_sect], srdp->numunsect)) != -1)
{
amp->type = AM_GAP;
array = srdp->unsect;
arraylen = srdp->numunsect;
}
if (arraylen == -1) /* error */
return FALSE;
if (amp->row_num == saip->anchor)
{
amp->type = AM_SEQ;
/* find limits of aligned region */
i = start_sect;
j = srdp->sect[start_sect];
disc = binary_search_on_uint2_list(srdp->unaligned, j, srdp->numunaln);
while (j<srdp->sect[stop_sect] && disc == -1)
{
j++;
disc = binary_search_on_uint2_list(srdp->unaligned, j, srdp->numunaln);
}
i = binary_search_on_uint2_list(srdp->sect, j, srdp->numsect);
if (i == -1)
{
i = binary_search_on_uint2_list(srdp->unsect, j, srdp->numunsect);
}
endoffset = dsp->lens[trans[i]] - (amp->to_aln - saip->aligncoords[i]) - 1;
if (endoffset < 0)
endoffset = 0;
if (i<stop_sect && endoffset == 0) /* there's an unaligned region here, and we go to the end of the segment */
{
AlnMgr2GetUnalignedInfo(sap, trans[i], amp->row_num, &intfrom, &intto);
amp->right_interrupt = (AMInterruptPtr)MemNew(sizeof(AMInterrupt));
amp->right_interrupt->row = amp->row_num;
amp->right_interrupt->unalnlen = intto - intfrom + 1;
amp->right_interrupt->segnum = trans[i];
amp->right_interrupt->which_side = AM2_RIGHT;
}
stop_sect = i;
if (start_sect > 0 && offset == 0)
{
disc = binary_search_on_uint2_list(srdp->unaligned, trans[start_sect]-1, srdp->numunaln);
if (disc != -1) /* there is a left unaligned region */
{
AlnMgr2GetUnalignedInfo(sap, trans[start_sect]-1, amp->row_num, &intfrom, &intto);
amp->left_interrupt = (AMInterruptPtr)MemNew(sizeof(AMInterrupt));
amp->left_interrupt->row = amp->row_num;
amp->left_interrupt->unalnlen = intto - intfrom + 1;
amp->left_interrupt->segnum = trans[start_sect];
amp->left_interrupt->which_side = AM2_LEFT;
}
}
len = 0;
for (i=start_sect; i<= stop_sect; i++)
{
len += dsp->lens[trans[i]];
}
len = len - offset - endoffset;
if (amp->strand == Seq_strand_minus)
amp->from_row = dsp->starts[trans[stop_sect]*dsp->dim+amp->row_num-1] + endoffset;
else
amp->from_row = dsp->starts[trans[start_sect]*dsp->dim+amp->row_num-1] + offset;
amp->to_row = amp->from_row + len - 1;
amp->real_from += amp->to_row - amp->from_row + 1;
if (saip->anchor <= 0)
MemFree(trans);
return TRUE;
}
/* look for limits of aligned/gapped region */
i = index;
j = start_sect+1;
disc = -1;
found = FALSE;
while (i+1<arraylen && disc == -1 && array[i] <= trans[stop_sect] && array[i+1]-1 == array[i])
{
disc = binary_search_on_uint2_list(srdp->unaligned, array[i], srdp->numunaln);
if (disc == -1)
i++;
}
disc = binary_search_on_uint2_list(srdp->unaligned, array[i], srdp->numunaln);
j = binary_search_on_uint2_list(trans, array[i], translen);
if (amp->type == AM_SEQ && j <= stop_sect) /* there is an interrupting region, either seq/gap, insert, or unaligned, plus just check last piece */
{
i = binary_search_on_uint2_list(srdp->insect, trans[j]+1, srdp->numinsect);
if (i != -1) /* there's an insert */
{
amp->right_interrupt = (AMInterruptPtr)MemNew(sizeof(AMInterrupt));
amp->right_interrupt->row = amp->row_num;
amp->right_interrupt->segnum = trans[j];
amp->right_interrupt->insertlen = dsp->lens[srdp->insect[i]];
amp->right_interrupt->which_side = AM2_RIGHT;
/* look for unaligned regions off insert */
disc1 = -1;
if (j > 0)
disc1 = binary_search_on_uint2_list(srdp->unaligned, trans[j]+1, srdp->numunaln);
if (disc1 != -1)
{
AlnMgr2GetUnalignedInfo(sap, srdp->unaligned[disc1], amp->row_num, &intfrom, &intto);
amp->right_interrupt->unalnlen = intto - intfrom + 1;
}
i++;
ctr = 1;
while (i<srdp->numinsect && srdp->insect[i] == srdp->insect[i-1]+1)
{
amp->right_interrupt->insertlen += dsp->lens[srdp->insect[i]];
/* look for unaligned regions off insert */
disc1 = -1;
if (j > 0) {
disc1 = binary_search_on_uint2_list(srdp->unaligned, trans[j]+1+ctr, srdp->numunaln);
}
if (disc1 != -1)
{
AlnMgr2GetUnalignedInfo(sap, srdp->unaligned[disc1], amp->row_num, &intfrom, &intto);
amp->right_interrupt->unalnlen += intto - intfrom + 1;
}
i++;
ctr++;
}
}
if (disc != -1) /* there's an unaligned region */
{
if (amp->right_interrupt == NULL)
amp->right_interrupt = (AMInterruptPtr)MemNew(sizeof(AMInterrupt));
amp->right_interrupt->row = amp->row_num;
amp->right_interrupt->segnum = trans[j];
amp->right_interrupt->which_side = AM2_RIGHT;
AlnMgr2GetUnalignedInfo(sap, srdp->unaligned[disc], amp->row_num, &intfrom, &intto);
amp->right_interrupt->unalnlen += intto - intfrom + 1;
}
}
stop_sect = j;
/* now look for left-side unaligned or inserted regions if offset == 0 */
if (amp->type == AM_SEQ && offset == 0)
{
disc = -1;
j = 1;
i = -1;
if ((Int2)trans[start_sect]-j > 0)
i = binary_search_on_uint2_list(srdp->sect, trans[start_sect]-j, srdp->numsect);
while (i == -1 && (Int2)(trans[start_sect])-j-1 >= 0)
{
i = binary_search_on_uint2_list(srdp->sect, trans[start_sect]-j-1, srdp->numsect);
j++;
}
disc = binary_search_on_uint2_list(srdp->unaligned, trans[start_sect]-j, srdp->numunaln);;
if (disc > -1)
{
AlnMgr2GetUnalignedInfo(sap, trans[start_sect]-j, amp->row_num, &intfrom, &intto);
amp->left_interrupt = (AMInterruptPtr)MemNew(sizeof(AMInterrupt));
amp->left_interrupt->row = amp->row_num;
amp->left_interrupt->segnum = trans[start_sect];
amp->left_interrupt->which_side = AM2_LEFT;
amp->left_interrupt->unalnlen = intto - intfrom + 1;
}
i = binary_search_on_uint2_list(srdp->insect, trans[start_sect]-j, srdp->numinsect);
if (i != -1) /* there's an insert */
{
if (amp->left_interrupt == NULL)
amp->left_interrupt = (AMInterruptPtr)MemNew(sizeof(AMInterrupt));
amp->left_interrupt->row = amp->row_num;
amp->left_interrupt->segnum = trans[start_sect];
amp->left_interrupt->which_side = AM2_LEFT;
amp->left_interrupt->insertlen = dsp->lens[srdp->insect[i]];
/* look for unaligned regions off insert */
j = trans[start_sect]-j;
disc1 = binary_search_on_uint2_list(srdp->unaligned, j, srdp->numunaln);
if (disc1 != -1)
{
AlnMgr2GetUnalignedInfo(sap, srdp->unaligned[disc1], amp->row_num, &intfrom, &intto);
amp->left_interrupt->unalnlen += intto - intfrom + 1;
}
i--;
j--;
while (i-1>=0 && srdp->insect[i] == srdp->insect[i+1]-1)
{
amp->left_interrupt->insertlen += dsp->lens[srdp->insect[i]];
disc1 = binary_search_on_uint2_list(srdp->unaligned, j, srdp->numunaln);
if (disc1 != -1)
{
AlnMgr2GetUnalignedInfo(sap, srdp->unaligned[disc1], amp->row_num, &intfrom, &intto);
amp->left_interrupt->unalnlen += intto - intfrom + 1;
}
i--;
j--;
}
if (i>=0) /* look one more over for unaligned */
{
disc1 = binary_search_on_uint2_list(srdp->unaligned, j, srdp->numunaln);
if (disc1 != -1)
{
AlnMgr2GetUnalignedInfo(sap, srdp->unaligned[disc1], amp->row_num, &intfrom, &intto);
amp->left_interrupt->unalnlen += intto - intfrom + 1;
}
}
}
}
endoffset = dsp->lens[trans[stop_sect]] - (amp->to_aln - saip->aligncoords[stop_sect]) - 1;
if (endoffset < 0)
endoffset = 0;
if (amp->right_interrupt != NULL && endoffset > 0)
{
MemFree(amp->right_interrupt);
amp->right_interrupt = NULL;
}
len = 0;
for (i=start_sect; i<=stop_sect; i++)
{
len += dsp->lens[trans[i]];
}
len = len - offset - endoffset;
if (amp->type == AM_GAP)
{
amp->from_row = amp->real_from;
amp->to_row = amp->from_row + len - 1;
} else
{
if (amp->strand == Seq_strand_minus)
{
amp->from_row = dsp->starts[trans[stop_sect]*dsp->dim+amp->row_num-1] + endoffset;
amp->to_row = amp->from_row + len - 1;
} else
{
amp->from_row = dsp->starts[trans[start_sect]*dsp->dim+amp->row_num-1] + offset;
amp->to_row = amp->from_row + len - 1;
}
}
if (saip->anchor <= 0)
MemFree(trans);
amp->real_from += amp->to_row - amp->from_row + 1;
return TRUE;
}
/* SECTION 4a */
static Int4 binary_search_on_uint4_list(Uint4Ptr list, Uint4 pos, Uint4 listlen)
{
Uint4 L;
Uint4 mid;
Uint4 R;
if (list == NULL || listlen == 0)
return 0;
L = 0;
R = listlen - 1;
while (L < R)
{
mid = (L+R)/2;
if (list[mid + 1] <= pos)
L = mid + 1;
else
R = mid;
}
return R;
}
/* SECTION 4a */
static Int4 binary_search_on_uint2_list(Uint2Ptr list, Int4 ele, Uint2 listlen)
{
Uint2 L;
Uint2 mid;
Uint2 R;
if (list == NULL || listlen == 0 || ele < 0)
return -1;
L = 0;
R = listlen - 1;
while (L < R)
{
mid = (L+R)/2;
if (ele <= list[mid])
R = mid;
else
L = mid+1;
}
if (ele == list[R])
return R;
else
return -1;
}
/* SECTION 4a */
static void AlnMgr2GetUnalignedInfo(SeqAlignPtr sap, Int4 segment, Int4 row, Int4Ptr from, Int4Ptr to)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
Boolean found;
Int4 i;
SAIndex2Ptr saip;
Uint1 strand;
Int4 tmp;
if (sap == NULL)
return;
strand = AlnMgr2GetNthStrand(sap, row);
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
dsp = (DenseSegPtr)(sap->segs);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
}
found = FALSE;
*from = *to = -1;
for (i=segment; i>=0 && !found; i--)
{
if (dsp->starts[dsp->dim*i+row-1] != -1)
{
found = TRUE;
if (strand == Seq_strand_minus)
*to = dsp->starts[dsp->dim*i+row-1]-1;
else
*from = dsp->starts[dsp->dim*i+row-1]+dsp->lens[i];
}
}
found = FALSE;
for (i=segment+1; i<dsp->numseg && !found; i++)
{
if (dsp->starts[dsp->dim*i+row-1] != -1)
{
found = TRUE;
if (strand == Seq_strand_minus)
*from = dsp->starts[dsp->dim*i+row-1]+dsp->lens[i];
else
*to = dsp->starts[dsp->dim*i+row-1]-1;
}
}
if (*from > *to)
{
tmp = *from;
*from = *to;
*to = tmp;
}
}
/* SECTION 4a */
/***************************************************************************
*
* AlnMgr2GetInterruptInfo returns a structure describing the inserts and
* unaligned regions in an interrupt. The structure is allocated by this
* function and must be freed with AlnMgr2FreeInterruptInfo.
*
***************************************************************************/
NLM_EXTERN AMInterrInfoPtr AlnMgr2GetInterruptInfo(SeqAlignPtr sap, AMInterruptPtr interrupt)
{
AMAlignIndex2Ptr amaip;
Int4 disc;
Boolean done;
DenseSegPtr dsp;
Int4 i;
AMInterrInfoPtr iip;
Int4 inserts;
Int4 intfrom;
Int4 intto;
Int4 j;
Int4 k;
Int4 n;
SAIndex2Ptr saip;
SARowDat2Ptr srdp;
Uint1 strand;
Uint2Ptr trans;
Int4 translen;
Int4 u;
if (interrupt == NULL || sap == NULL || sap->saip == NULL)
return NULL;
if (sap->saip->indextype == INDEX_CHILD)
{
dsp = (DenseSegPtr)(sap->segs);
saip = (SAIndex2Ptr)(sap->saip);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return FALSE;
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
}
if (dsp->numseg < interrupt->segnum)
return NULL;
if (saip->anchor > 0)
{
trans = saip->srdp[saip->anchor-1]->sect;
translen = saip->srdp[saip->anchor-1]->numsect;
} else
{
trans = (Uint2Ptr)MemNew(dsp->numseg*sizeof(Uint2));
for (i=0; i<dsp->numseg; i++)
{
trans[i] = i;
}
translen = dsp->numseg;
}
strand = AlnMgr2GetNthStrand(sap, interrupt->row-1);
srdp = saip->srdp[interrupt->row-1];
/* now look for inserts and unaligned regions on the side indicated */
if (interrupt->which_side == AM2_RIGHT)
{
/* check if this is unaligned */
disc = binary_search_on_uint2_list(srdp->unaligned, interrupt->segnum, srdp->numunaln);
/* then look for inserts */
done = FALSE;
iip = (AMInterrInfoPtr)MemNew(sizeof(AMInterrInfo));
if (disc != -1)
iip->num = 1;
inserts = 0;
for (i=interrupt->segnum+1; !done; i++)
{
n = binary_search_on_uint2_list(srdp->insect, i, srdp->numinsect);
if (n == -1)
n = binary_search_on_uint2_list(srdp->unsect, i, srdp->numunsect);
if (n == -1)
{
done = TRUE;
} else
{
inserts++; /* only increment if region gets interrupted */
disc = binary_search_on_uint2_list(srdp->unaligned, i, srdp->numunaln);
if (disc != -1) /* this insert has an unaligned region */
{
iip->num += inserts;
iip->num++;
inserts = 0;
}
}
}
if (inserts != 0)
iip->num++;
iip->starts = (Int4Ptr)MemNew(iip->num*sizeof(Int4));
iip->lens = (Int4Ptr)MemNew(iip->num*sizeof(Int4));
iip->types = (Int4Ptr)MemNew(iip->num*sizeof(Int4));
k = 0;
disc = binary_search_on_uint2_list(srdp->unaligned, interrupt->segnum, srdp->numunaln);
if (disc != -1) /* starts with unaligned */
{
AlnMgr2GetUnalignedInfo(sap, interrupt->segnum, interrupt->row, &intfrom, &intto);
iip->starts[k] = intfrom;
iip->lens[k] = intto - intfrom + 1;
iip->types[k] = AM_UNALIGNED;
k++;
}
disc = 0;
done = FALSE;
for (i=interrupt->segnum+1; !done; i++)
{
n = binary_search_on_uint2_list(srdp->insect, i, srdp->numinsect);
u = binary_search_on_uint2_list(srdp->unsect, i, srdp->numinsect);
if (n == -1 && u == -1)
{
done = TRUE;
} else
{
if (u == -1)
{
if (disc != -1 || strand == Seq_strand_minus) /* only record new start if region gets interrupted or if on minus strand */
iip->starts[k] = dsp->starts[dsp->dim*i + interrupt->row-1];
iip->lens[k] += dsp->lens[i];
iip->types[k] = AM_INSERT;
disc = binary_search_on_uint2_list(srdp->unaligned, i, srdp->numunaln);
if (disc != -1) /* this insert has an unaligned region */
{
k++;
AlnMgr2GetUnalignedInfo(sap, i, interrupt->row, &intfrom, &intto);
iip->starts[k] = intfrom;
iip->lens[k] = intto - intfrom + 1;
iip->types[k] = AM_UNALIGNED;
k++;
}
}
}
}
} else if (interrupt->which_side == AM2_LEFT)
{
/* check if the next non-gap segment to the left has unaligned */
j = 1;
n = 0;
while (n != -1 && interrupt->segnum-j >= 0)
{
n = binary_search_on_uint2_list(srdp->unsect, interrupt->segnum-j, srdp->numunsect);
if (n == -1)
n = binary_search_on_uint2_list(srdp->insect, interrupt->segnum-j, srdp->numinsect);
if (n != -1)
j++;
}
disc = binary_search_on_uint2_list(srdp->unaligned, interrupt->segnum-j, srdp->numunaln);
/* then look for inserts */
done = FALSE;
iip = (AMInterrInfoPtr)MemNew(sizeof(AMInterrInfo));
if (disc != -1)
iip->num = 1;
inserts = 0;
for (i=interrupt->segnum-1; !done; i--)
{
n = binary_search_on_uint2_list(srdp->insect, i, srdp->numinsect);
if (n == -1)
n = binary_search_on_uint2_list(srdp->unsect, i, srdp->numunsect);
if (n == -1)
{
done = TRUE;
} else
{
inserts++; /* only increment if region gets interrupted */
disc = binary_search_on_uint2_list(srdp->unaligned, i, srdp->numunaln);
if (disc != -1) /* this insert has an unaligned region */
{
iip->num += inserts;
iip->num++;
inserts = 0;
}
}
}
i++;
iip->starts = (Int4Ptr)MemNew(iip->num*sizeof(Int4));
iip->lens = (Int4Ptr)MemNew(iip->num*sizeof(Int4));
iip->types = (Int4Ptr)MemNew(iip->num*sizeof(Int4));
k = 0;
disc = 0;
/* check first non-inserted segment for unaligned */
if (i >= 0)
{
disc = binary_search_on_uint2_list(srdp->unaligned, i, srdp->numunaln);
if (disc != -1) /* there's an unaligned region */
{
AlnMgr2GetUnalignedInfo(sap, i, interrupt->row, &intfrom, &intto);
iip->starts[k] = intfrom;
iip->lens[k] = intto - intfrom + 1;
iip->types[k] = AM_UNALIGNED;
k++;
}
}
i++; /* start from leftmost end of inserts/unaligned */
for (i; i<interrupt->segnum; i++)
{
u = binary_search_on_uint2_list(srdp->unsect, i, srdp->numunsect);
if (u == -1)
{
if (disc != -1 || strand == Seq_strand_minus) /* only record new start if region gets interrupted or if on minus strand */
iip->starts[k] = dsp->starts[dsp->dim*i + interrupt->row-1];
iip->lens[k] += dsp->lens[i];
iip->types[k] = AM_INSERT;
disc = binary_search_on_uint2_list(srdp->unaligned, i, srdp->numunaln);
if (disc != -1) /* this insert has an unaligned region */
{
k++;
AlnMgr2GetUnalignedInfo(sap, binary_search_on_uint2_list(trans, i, translen), interrupt->row, &intfrom, &intto);
iip->starts[k] = intfrom;
iip->lens[k] = intto - intfrom + 1;
iip->types[k] = AM_UNALIGNED;
k++;
}
}
}
}
iip->strand = strand;
return iip;
}
/* SECTION 4b */
/***************************************************************************
*
* AlnMgr2GetNthStrand takes an indexed seqalign and a row number and
* returns the strand of the row indicated. A return of 0 indicates
* an error.
*
***************************************************************************/
NLM_EXTERN Uint1 AlnMgr2GetNthStrand(SeqAlignPtr sap, Int4 n)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
if (sap == NULL || sap->saip == NULL || n < 1)
return 0;
if (sap->saip->indextype == INDEX_CHILD)
{
dsp = (DenseSegPtr)(sap->segs);
if (n > dsp->dim)
return 0;
if (dsp->strands == NULL)
return Seq_strand_plus;
return (dsp->strands[n-1]);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE) /* can't get Nth strand for this */
return 0;
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
if (n > dsp->dim)
return 0;
if (dsp->strands == NULL)
return Seq_strand_plus;
return (dsp->strands[n-1]);
}
return 0;
}
/* SECTION 4b */
/***************************************************************************
*
* AlnMgr2GetNthSeqIdPtr returns the seqid (this is a duplicated,
* allocated seqid that must be freed) of the nth row (1-based) of an
* indexed parent or child seqalign.
*
***************************************************************************/
NLM_EXTERN SeqIdPtr AlnMgr2GetNthSeqIdPtr(SeqAlignPtr sap, Int4 n)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
Int4 i;
SeqIdPtr sip;
if (sap == NULL || sap->saip == NULL)
return NULL;
if (sap->saip->indextype == INDEX_CHILD)
{
dsp = (DenseSegPtr)(sap->segs);
if (n > dsp->dim)
return NULL;
sip = dsp->ids;
for (i=1; i<n && sip != NULL; i++)
{
sip = sip->next;
}
if (sip == NULL) return NULL;
return (SeqIdDup(sip));
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (n > amaip->numrows)
return NULL;
sip = SeqIdDup(amaip->ids[n-1]);
return sip;
} else
return NULL;
}
/* SECTION 4b */
/***************************************************************************
*
* AlnMgr2GetNthSeqRangeInSA returns the smallest and largest sequence
* coordinates contained in the nth row of an indexed seqalign. Either
* start or stop can be NULL to only retrieve one of the coordinates.
* If start and stop are -1, there is an error; if they are both -2, the
* row is just one big insert. RANGE
*
***************************************************************************/
NLM_EXTERN void AlnMgr2GetNthSeqRangeInSA(SeqAlignPtr sap, Int4 n, Int4Ptr start, Int4Ptr stop)
{
AMAlignIndex2Ptr amaip;
Int4 beg;
DenseSegPtr dsp;
Int4 end;
SAIndex2Ptr saip;
SARowDat2Ptr srdp;
Uint1 strand;
if (start != NULL)
*start = -1;
if (stop != NULL)
*stop = -1;
if (sap == NULL || sap->saip == NULL)
return;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
dsp = (DenseSegPtr)(sap->segs);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
{
AlnMgr2GetNthSeqRangeInSASet(sap, n, start, stop);
return;
}
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
}
if (n > saip->numrows || n <= 0)
return;
srdp = saip->srdp[n-1];
beg = -1;
if (srdp->numsect == 0) /* just one big insert */
beg = end = -2;
strand = AlnMgr2GetNthStrand(sap, n);
if (beg != -2 && strand != Seq_strand_minus)
{
beg = dsp->starts[srdp->sect[0]*(dsp->dim) + n-1];
end = dsp->starts[srdp->sect[srdp->numsect-1]*(dsp->dim) + n-1] + dsp->lens[srdp->sect[srdp->numsect-1]] - 1;
} else if (beg != -2)
{
beg = dsp->starts[srdp->sect[srdp->numsect-1]*(dsp->dim) + n-1];
end = dsp->starts[srdp->sect[0]*(dsp->dim) + n-1] + dsp->lens[srdp->sect[0]] - 1;
}
if (start != NULL)
*start = beg;
if (stop != NULL)
*stop = end;
return;
}
/* SECTION 4b */
/***************************************************************************
*
* AlnMgr2GetNthRowSpanInSA returns the least and greatest alignment
* coordinates (inclusive) spanned by the indicated row. Either stop or
* start can be NULL to retrieve just one of the coordinates.
*
***************************************************************************/
NLM_EXTERN void AlnMgr2GetNthRowSpanInSA(SeqAlignPtr sap, Int4 n, Int4Ptr start, Int4Ptr stop)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
Int4 i;
SAIndex2Ptr saip;
SARowDat2Ptr srdp;
if (start != NULL)
*start = -1;
if (stop != NULL)
*stop = -1;
if (sap == NULL || sap->saip == NULL)
return;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
dsp = (DenseSegPtr)(sap->segs);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
}
if (n > saip->numrows || n <= 0)
return;
srdp = saip->srdp[n-1];
if (srdp->numsect == 0)
{
if (start != NULL)
*start = -1;
if (stop != NULL)
*stop = -1;
return;
}
if (start != NULL)
{
if (saip->anchor > 0)
i = binary_search_on_uint2_list(saip->srdp[saip->anchor-1]->sect, srdp->sect[0], saip->srdp[saip->anchor-1]->numsect);
else
i = srdp->sect[0];
*start = saip->aligncoords[i];
}
if (stop != NULL)
{
if (saip->anchor > 0)
i = binary_search_on_uint2_list(saip->srdp[saip->anchor-1]->sect, srdp->sect[srdp->numsect-1], saip->srdp[saip->anchor-1]->numsect);
else
i = srdp->sect[srdp->numsect-1];
*stop = saip->aligncoords[i] + dsp->lens[srdp->sect[srdp->numsect-1]] - 1;
}
return;
}
/* SECTION 4b */
static void AlnMgr2GetNthSeqRangeInSASet(SeqAlignPtr sap, Int4 n, Int4Ptr start, Int4Ptr stop)
{
AMAlignIndex2Ptr amaip;
Int4 from;
Int4 i;
Int4 max;
Int4 min;
Int4 to;
if (start != NULL)
*start = -1;
if (stop != NULL)
*stop = -1;
if (sap == NULL || sap->saip == NULL || n < 0)
return;
if (sap->saip->indextype == INDEX_CHILD)
{
AlnMgr2GetNthSeqRangeInSA(sap, n, start, stop);
return;
}
amaip = (AMAlignIndex2Ptr)(sap->saip);
min = max = -1;
for (i=0; i<amaip->numsaps; i++)
{
AlnMgr2GetNthSeqRangeInSA(amaip->saps[i], n, &from, &to);
if (from != -1 && (from < min || min == -1))
min = from;
if (to > max)
max = to;
}
if (start != NULL)
*start = from;
if (stop != NULL)
*stop = to;
}
/* SECTION 4b */
NLM_EXTERN Int4 AlnMgr2GetMaxTailLength(SeqAlignPtr sap, Uint1 which_tail)
{
Int4 i;
Int4 maxlen;
Int4 n;
Int4 start;
Int4 stop;
Uint1 strand;
if (sap == NULL || sap->saip == NULL)
return 0;
n = AlnMgr2GetNumRows(sap);
maxlen = -1;
for (i=0; i<n; i++)
{
AlnMgr2GetNthRowTail(sap, i+1, which_tail, &start, &stop, &strand);
if (stop - start + 1 > maxlen)
maxlen = stop - start + 1;
}
return maxlen;
}
/* SECTION 4b */
/***************************************************************************
*
* AlnMgr2GetNthRowTail returns the sequence extremities that are not
* contained in the alignment (if the alignment starts at 10 in row 2, the
* tail in that row is 0-9). It takes an indexed seqalign, a 1-based row
* number, and AM2_LEFT_TAIL or AM2_RIGHT_TAIL, and returns the start, stop,
* and strand of the tail indicated in the row desired. AlnMgr2GetNthRowTail
* returns TRUE if the calculations were successfully completed.
*
***************************************************************************/
NLM_EXTERN Boolean AlnMgr2GetNthRowTail(SeqAlignPtr sap, Int4 n, Uint1 which_tail, Int4Ptr start, Int4Ptr stop, Uint1Ptr strand)
{
BioseqPtr bsp;
SeqIdPtr sip;
Int4 tmp_start;
Int4 tmp_stop;
Uint1 tmp_strand;
if (sap == NULL || n < 1 || sap->saip == NULL)
return FALSE;
tmp_start = tmp_stop = -1;
AlnMgr2GetNthSeqRangeInSA(sap, n, &tmp_start, &tmp_stop);
if (tmp_start == -1 || tmp_stop == -1)
return FALSE;
tmp_strand = AlnMgr2GetNthStrand(sap, n);
if (which_tail == AM2_LEFT_TAIL)
{
if (tmp_strand == Seq_strand_minus)
{
sip = AlnMgr2GetNthSeqIdPtr(sap, n);
bsp = BioseqLockById(sip);
SeqIdFree(sip);
if (bsp == NULL)
return FALSE;
if (tmp_stop == bsp->length-1 || stop == NULL)
{
if (start)
*start = -1;
if (stop)
*stop = -1;
} else
{
if (start)
*start = tmp_stop+1;
if (stop)
*stop = bsp->length-1;
}
BioseqUnlock(bsp);
if (strand)
*strand = tmp_strand;
} else
{
if (tmp_start >= 1)
{
if (start)
*start = 0;
if (stop)
*stop = tmp_start - 1;
} else
{
if (start)
*start = -1;
if (stop)
*stop = -1;
}
if (strand)
*strand = tmp_strand;
}
} else if (which_tail == AM2_RIGHT_TAIL)
{
if (tmp_strand == Seq_strand_minus)
{
if (tmp_start >= 1)
{
if (start)
*start = 0;
if (stop)
*stop = tmp_start - 1;
} else
{
if (start)
*start = -1;
if (stop)
*stop = -1;
}
if (strand)
*strand = tmp_strand;
} else
{
sip = AlnMgr2GetNthSeqIdPtr(sap, n);
bsp = BioseqLockById(sip);
SeqIdFree(sip);
if (bsp == NULL)
return FALSE;
if (bsp->length-1 == tmp_stop)
{
if (start)
*start = -1;
if (stop)
*stop = -1;
} else
{
if (start)
*start = tmp_stop + 1;
if (stop)
*stop = bsp->length-1;
}
if (strand)
*strand = tmp_strand;
BioseqUnlock(bsp);
}
}
return TRUE;
}
/* SECTION 4c */
/***************************************************************************
*
* AlnMgr2GetAlnLength returns the total alignment length of an indexed
* alignment. If fill_in is TRUE, the function computes the total length
* of all the internal unaligned regions and adds that to the alignment
* length; otherwise only the aligned portions are considered. (LENGTH)
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2GetAlnLength(SeqAlignPtr sap, Boolean fill_in)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
Int4 i;
Uint2 lastseg;
Int4 len;
SAIndex2Ptr saip;
SeqAlignPtr salp;
if (sap == NULL || sap->saip == NULL)
return -1;
if (sap->saip->indextype == INDEX_CHILD)
{
dsp = (DenseSegPtr)(sap->segs);
saip = (SAIndex2Ptr)(sap->saip);
salp = sap;
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return -1;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
salp = amaip->sharedaln;
}
if (saip->unaln == FALSE || fill_in == FALSE)
{
if (saip->anchor == -1)
return (saip->aligncoords[saip->numseg-1]+dsp->lens[saip->numseg-1]);
else
{
lastseg = saip->srdp[saip->anchor-1]->sect[saip->srdp[saip->anchor-1]->numsect-1];
return (saip->aligncoords[saip->numseg-1]+dsp->lens[lastseg]);
}
} else
{
len = 0;
for (i=0; i<dsp->numseg; i++)
{
len += dsp->lens[i];
len += AlnMgr2GetMaxUnalignedLength(salp, i);
}
}
return len;
}
/* SECTION 4c */ /* FOR DDV */
NLM_EXTERN Boolean AlnMgr2IsSAPDiscAli(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
SAIndex2Ptr saip;
if (sap == NULL || sap->saip == NULL)
return FALSE;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return FALSE;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
}
if (saip->numunaln > 0)
return TRUE;
return FALSE;
}
/* SECTION 4c */ /* FOR DDV */
NLM_EXTERN Int4 AlnMgr2GetNumAlnBlocks(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
SAIndex2Ptr saip;
if (sap == NULL || sap->saip == NULL)
return -1;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return -1;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
}
if (saip->numunaln >= 0)
return (saip->numunaln + 1);
else if (saip->numunaln == -1)
return 1;
else
return -1;
}
/* SECTION 4c */ /* FOR DDV */
NLM_EXTERN Boolean AlnMgr2GetNthBlockRange(SeqAlignPtr sap, Int4 n, Int4Ptr start, Int4Ptr stop)
{
AMAlignIndex2Ptr amaip;
SAIndex2Ptr saip;
if (sap == NULL || sap->saip == NULL)
return FALSE;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return FALSE;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
}
if (!start || !stop)
return FALSE;
*start = -1;
*stop = -1;
if (n >= saip->numunaln)
return FALSE;
if (n < saip->numunaln)
{
*start = saip->aligncoords[saip->unaln[n-1]+1];
*stop = saip->aligncoords[saip->unaln[n]] - 1;
}
return TRUE;
}
/* SECTION 4c */ /* FOR DDV */
/***************************************************************************
*
* AlnMgr2GetNthUnalignedForNthRow returns the bioseq coordinates for the
* requested row, in the requested unaligned region. Any error will result
* in -1 returns for both start and stop.
*
***************************************************************************/
NLM_EXTERN Boolean AlnMgr2GetNthUnalignedForNthRow(SeqAlignPtr sap, Int4 unaligned, Int4 row, Int4Ptr start, Int4Ptr stop)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
Int4 i;
SAIndex2Ptr saip;
Int4 seg;
Uint1 strand;
if (sap == NULL || sap->saip == NULL)
return FALSE;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
dsp = (DenseSegPtr)(sap->segs);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return FALSE;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
}
if (row > saip->numrows)
return FALSE;
if (saip->numunaln == 0) /* not set yet */
AlnMgr2SetUnaln(sap);
if (saip->numunaln == -1 || unaligned > saip->numunaln)
{
if (start)
*start = -1;
if (stop)
*stop = -1;
return FALSE;
}
seg = -1;
if (unaligned <= saip->numunaln && unaligned > 0)
seg = saip->unaln[unaligned-1];
if (start)
*start = -1;
if (stop)
*stop = -1;
i = binary_search_on_uint2_list(saip->srdp[row-1]->unaligned, seg, saip->srdp[row-1]->numunaln);
if (i == -1 || saip->srdp[row-1]->unaligned[i] >= dsp->numseg-1)
return FALSE;
strand = AlnMgr2GetNthStrand(sap, row);
if (strand == Seq_strand_minus)
{
*start = dsp->starts[(saip->srdp[row-1]->unaligned[i]+1)*dsp->dim+row-1] + dsp->lens[(saip->srdp[row-1]->unaligned[i])];
*stop = dsp->starts[(saip->srdp[row-1]->unaligned[i])*dsp->dim+row-1] - 1;
} else
{
*start = dsp->starts[(saip->srdp[row-1]->unaligned[i])*dsp->dim+row-1] + dsp->lens[(saip->srdp[row-1]->unaligned[i])];
*stop = dsp->starts[(saip->srdp[row-1]->unaligned[i]+1)*dsp->dim+row-1] - 1;
}
return TRUE;
}
/* SECTION 4c */ /* FOR DDV */
/***************************************************************************
*
* AlnMgr2GetNextLengthBit is called in a loop on an indexed alignment, with
* seg starting at 0, to return the lengths of the aligned and unaligned
* regions. If the length returned is negative, it's an unaligned region;
* otherwise it's aligned.
*
***************************************************************************/
NLM_EXTERN Boolean AlnMgr2GetNextLengthBit(SeqAlignPtr sap, Int4Ptr len, Int4Ptr seg)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
Int4 i;
Int4 lastseg;
Int4 maxseg;
SAIndex2Ptr saip;
if (sap == NULL || sap->saip == NULL || seg == NULL)
return FALSE;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
dsp = (DenseSegPtr)(sap->segs);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return FALSE;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
}
if (saip->numunaln == -1) /* the whole thing is just one big aligned segment */
{
if (*seg != 0)
return FALSE;
if (saip->anchor == -1)
{
*len = saip->aligncoords[saip->numseg-1]+dsp->lens[saip->numseg-1];
*seg = 1;
} else
{
lastseg = saip->srdp[saip->anchor-1]->sect[saip->srdp[saip->anchor-1]->numsect-1];
*len = saip->aligncoords[saip->numseg-1]+dsp->lens[lastseg];
*seg = 1;
}
return TRUE;
} else
{
if (saip->unaln == 0) /* not set */
{
AlnMgr2SetUnaln(sap);
if (saip->numunaln == -1) /* no unaligned regions */
{
if (*seg != 0)
return FALSE;
if (saip->anchor == -1)
*len = saip->aligncoords[saip->numseg-1]+dsp->lens[saip->numseg-1];
else
{
lastseg = saip->srdp[saip->anchor-1]->sect[saip->srdp[saip->anchor-1]->numsect-1];
*len = saip->aligncoords[saip->numseg-1]+dsp->lens[lastseg];
}
*seg = 1;
return TRUE;
}
}
if (*seg > saip->numunaln || -(*seg) > saip->numunaln)
return FALSE;
if (*seg >= 0)
{
*len = 0;
if (*seg == 0)
i = 0;
else
i = saip->unaln[*seg-1]+1;
if (*seg < saip->numunaln)
maxseg = saip->unaln[*seg];
else
maxseg = dsp->numseg-1;
while (i<=maxseg)
{
(*len) += dsp->lens[i];
i++;
}
*seg = -(*seg+1);
return TRUE;
} else
{
*len = -AlnMgr2GetMaxUnalignedLength(sap, saip->unaln[-(*seg)-1]);
*seg = -(*seg);
return TRUE;
}
}
}
/* SECTION 4c */
static Int4 AlnMgr2GetMaxUnalignedLength(SeqAlignPtr sap, Int4 seg)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
Boolean found;
Int4 from;
Int4 i;
Int4 max;
Int4 row;
SAIndex2Ptr saip;
Int4 to;
if (sap == NULL)
return -1;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
dsp = (DenseSegPtr)(sap->segs);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return -1;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
}
found = FALSE;
for (row=0; row<dsp->dim && !found; row++)
{
for (i=0; i<saip->srdp[row]->numunaln && !found; i++)
{
if (saip->srdp[row]->unaligned[i] == seg)
found = TRUE;
}
}
if (!found)
return 0;
max = 0;
for (i=0; i<dsp->dim; i++)
{
AlnMgr2GetUnalignedInfo(sap, seg, i+1, &from, &to);
if (to - from > max)
max = to - from;
}
return max;
}
/* SECTION 4c */
/***************************************************************************
*
* AlnMgr2GetNumRows returns the number of rows in an indexed seqalign.
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2GetNumRows(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
SAIndex2Ptr saip;
if (sap == NULL || sap->saip == NULL)
return -1;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
return (saip->numrows);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
return (amaip->numrows);
}
return -1;
}
/* SECTION 4c */
/***************************************************************************
*
* AlnMgr2GetNumSegs returns the number of gap- or aligned- contiguous
* segments in the alignment (continuous or not).
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2GetNumSegs(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
if (sap == NULL || sap->saip == NULL)
return -1;
if (sap->saip->indextype == INDEX_CHILD)
{
dsp = (DenseSegPtr)(sap->segs);
return dsp->numseg;
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return -1;
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
return dsp->numseg;
}
return -1;
}
/* SECTION 4c */
/***************************************************************************
*
* AlnMgr2GetNumSegsInRange returns the number of alignment segments
* spanned by the given range (partially or fully). The range is
* given in alignment coordinates.
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2GetNumSegsInRange(SeqAlignPtr sap, Int4 from, Int4 to, Int4Ptr start_seg)
{
Uint4Ptr aligncoords;
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
Int4 len;
SAIndex2Ptr saip;
Int4 start;
Int4 stop;
if (start_seg != NULL)
*start_seg = -1;
if (sap == NULL || sap->saip == NULL)
return -1;
len = AlnMgr2GetAlnLength(sap, FALSE);
if (from < 0 || to > len-1)
return -1;
if (sap->saip->indextype == INDEX_CHILD)
{
dsp = (DenseSegPtr)(sap->segs);
saip = (SAIndex2Ptr)(sap->saip);
aligncoords = saip->aligncoords;
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return -1;
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
saip = (SAIndex2Ptr)(sap->saip);
aligncoords = saip->aligncoords;
}
if (from == 0 && to == len-1) /* whole alignment */
{
if (start_seg)
*start_seg = 0;
return dsp->numseg;
}
start = binary_search_on_uint4_list(aligncoords, from, dsp->numseg);
stop = binary_search_on_uint4_list(aligncoords, to, dsp->numseg);
if (start_seg != NULL)
*start_seg = start;
return (stop-start+1);
}
/* SECTION 4c */
/***************************************************************************
*
* AlnMgr2GetNthSegmentRange returns the alignment coordinate range of the
* Nth segment (count starts at 1) of the seqalign. start and stop are
* optional arguments (in case only one end is desired).
*
***************************************************************************/
NLM_EXTERN void AlnMgr2GetNthSegmentRange(SeqAlignPtr sap, Int4 n, Int4Ptr start, Int4Ptr stop)
{
AMAlignIndex2Ptr amaip;
Int4 i;
SAIndex2Ptr saip;
if (sap == NULL || sap->saip == NULL)
return;
i = AlnMgr2GetNumSegs(sap);
if (n > i || n < 0)
return;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
if (start != NULL)
*start = saip->aligncoords[n-1];
if (stop != NULL)
{
if (i > n) /* not the last segment */
*stop = saip->aligncoords[n] - 1;
else
*stop = AlnMgr2GetAlnLength(sap, FALSE) - 1;
}
return;
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
if (start != NULL)
*start = saip->aligncoords[n-1];
if (stop != NULL)
{
if (i > n) /* not the last segment */
*stop = saip->aligncoords[n] - 1;
else
*stop = AlnMgr2GetAlnLength(sap, FALSE) - 1;
}
return;
}
}
/* SECTION 4c */
/***************************************************************************
*
* AlnMgr2GetFirstNForSip returns the first row that a seqid occurs on,
* or -1 if the seqid is not in the alignment or if there is another
* error.
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2GetFirstNForSip(SeqAlignPtr sap, SeqIdPtr sip)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
Int4 i;
SeqIdPtr sip_tmp;
if (sap == NULL || sip == NULL || sap->saip == NULL)
return -1;
if (sap->saip->indextype == INDEX_CHILD)
{
dsp = (DenseSegPtr)(sap->segs);
sip_tmp = dsp->ids;
i = 1;
while (sip_tmp != NULL)
{
if (SeqIdComp(sip, sip_tmp) == SIC_YES)
return i;
sip_tmp = sip_tmp->next;
i++;
}
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return -1;
for (i=0; i<amaip->numrows; i++)
{
if (SeqIdComp(sip, amaip->ids[i]) == SIC_YES)
return (i+1);
}
}
return -1;
}
/***************************************************************************
*
* AlnMgr2GetFirstNForSipList returns the first row that one of a list of seqids occur on,
* or -1 if none of the seqids are in the alignment or if there is another
* error.
* Handy if sip comes from a BioSeq, where it can point to a linked list
* of SeqIds.
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2GetFirstNForSipList(SeqAlignPtr sap, SeqIdPtr sip)
{
Int4 i;
if (sap == NULL || sap->saip == NULL)
return -1;
for (; sip; sip = sip->next) {
i = AlnMgr2GetFirstNForSip(sap, sip);
if (i != -1)
return i;
}
return -1;
}
/***************************************************************************
*
* AlnMgr2GetParent returns the top-level seqalign associated with a given
* indexed alignment. It returns the actual pointer, not a copy.
*
***************************************************************************/
NLM_EXTERN SeqAlignPtr AlnMgr2GetParent(SeqAlignPtr sap)
{
SAIndex2Ptr saip;
if (sap == NULL || sap->saip == NULL)
return NULL;
if (sap->saip->indextype == INDEX_PARENT)
return sap;
saip = (SAIndex2Ptr)(sap->saip);
return (saip->top);
}
/***************************************************************************
*
* SECTION 5: Functions to change, assign or retrieve an anchor row.
* SECTION 5a: functions for child seqaligns
* SECTION 5b: functions for parent seqaligns
* SECTION 5c: functions to retrieve anchor row information
*
***************************************************************************/
/* SECTION 5a */
static void AlnMgr2AnchorChild(SeqAlignPtr sap, Int4 which_row)
{
AMBitty2Ptr abp;
AMBitty2Ptr abp_head;
AMBitty2Ptr abp_head2;
AMBitty2Ptr abp_prev;
AMBitty2Ptr abp_prev2;
AMBitty2Ptr abp_uhead;
AMBitty2Ptr abp_uprev;
Uint2Ptr anchor_unsect;
Int4 curr;
Int4 curr2;
DenseSegPtr dsp;
Int4 i;
Int4 j;
Uint2 numunsect;
SAIndex2Ptr saip;
SARowDat2Ptr srdp;
if (sap == NULL || sap->saip == NULL || sap->saip->indextype != INDEX_CHILD)
return;
saip = (SAIndex2Ptr)(sap->saip);
if (which_row > saip->numrows)
return;
if (saip->anchor == which_row) /*already anchored to the right row */
return;
if (saip->anchor != -1 || which_row <= 0) /* already anchored -- must reindex as a flat alignment first */
{
SAIndex2Free2(sap->saip);
sap->saip = NULL;
AlnMgr2IndexSingleDenseSegSA(sap);
if (which_row <= 0)
return;
saip = (SAIndex2Ptr)(sap->saip);
}
numunsect = saip->srdp[which_row-1]->numunsect;
if (numunsect > 0)
anchor_unsect = saip->srdp[which_row-1]->unsect;
else
anchor_unsect = NULL;
for (i=0; i<saip->numrows; i++)
{
if (i+1 != which_row)
{
abp_head = NULL;
abp_head2 = NULL;
abp_uhead = NULL;
curr = 0;
curr2 = 0;
srdp = saip->srdp[i];
for (j=0; j<srdp->numsect; j++)
{
if (anchor_unsect != NULL && curr < numunsect && srdp->sect[j] > anchor_unsect[curr])
{
while (curr < numunsect && srdp->sect[j] > anchor_unsect[curr])
{
curr++;
}
}
if (curr < numunsect && anchor_unsect != NULL && srdp->sect[j] == anchor_unsect[curr]) /* this one is an insert */
{
abp = (AMBitty2Ptr)MemNew(sizeof(AMBitty2));
abp->n = srdp->sect[j];
if (abp_head == NULL)
abp_head = abp_prev = abp;
else
{
abp_prev->next = abp;
abp_prev = abp;
}
curr++;
} else /* put it in the keeper pile */
{
abp = (AMBitty2Ptr)MemNew(sizeof(AMBitty2));
abp->n = srdp->sect[j];
if (abp_head2 == NULL)
abp_head2 = abp_prev2 = abp;
else
{
abp_prev2->next = abp;
abp_prev2 = abp;
}
}
}
for (j=0; j<srdp->numunsect; j++)
{
if (anchor_unsect != NULL && curr2 < numunsect && srdp->unsect[j] > anchor_unsect[curr2])
{
while (curr2 < numunsect && srdp->unsect[j] > anchor_unsect[curr2])
{
curr2++;
}
}
if (curr2 >= numunsect || (curr2 < numunsect && (anchor_unsect == NULL || srdp->unsect[j] != anchor_unsect[curr2]))) /* these get kept */
{
abp = (AMBitty2Ptr)MemNew(sizeof(AMBitty2));
abp->n = srdp->unsect[j];
if (abp_uhead == NULL)
abp_uhead = abp_uprev = abp;
else
{
abp_uprev->next = abp;
abp_uprev = abp;
}
}
}
MemFree(srdp->sect);
MemFree(srdp->unsect);
srdp->numsect = srdp->numunsect = srdp->numinsect = 0;
abp = abp_head; /* inserts */
while (abp != NULL)
{
srdp->numinsect++;
abp = abp->next;
}
srdp->insect = (Uint2Ptr)MemNew((srdp->numinsect)*sizeof(Uint2));
abp = abp_head;
j = 0;
while (abp != NULL)
{
srdp->insect[j] = abp->n;
j++;
abp_prev = abp;
abp = abp->next;
MemFree(abp_prev);
}
abp = abp_head2; /* aligned sections */
while (abp != NULL)
{
srdp->numsect++;
abp = abp->next;
}
srdp->sect = (Uint2Ptr)MemNew((srdp->numsect)*sizeof(Uint2));
abp = abp_head2;
j = 0;
while (abp != NULL)
{
srdp->sect[j] = abp->n;
j++;
abp_prev = abp;
abp = abp->next;
MemFree(abp_prev);
}
abp = abp_uhead; /* aligned gaps */
while (abp != NULL)
{
srdp->numunsect++;
abp = abp->next;
}
srdp->unsect = (Uint2Ptr)MemNew((srdp->numunsect)*sizeof(Uint2));
abp = abp_uhead;
j = 0;
while (abp != NULL)
{
srdp->unsect[j] = abp->n;
j++;
abp_prev = abp;
abp = abp->next;
MemFree(abp_prev);
}
} else /* this is the anchor row -- fill in the alignment coords*/
{
srdp = saip->srdp[i];
MemFree(saip->aligncoords);
saip->numseg = srdp->numsect;
saip->aligncoords = (Uint4Ptr)MemNew((saip->numseg)*sizeof(Uint4));
dsp = (DenseSegPtr)(sap->segs);
for (j=1; j<saip->numseg; j++)
{
saip->aligncoords[j] = saip->aligncoords[j-1] + dsp->lens[srdp->sect[j-1]];
}
saip->anchor = i+1;
}
}
}
/* SECTION 5c */
/***************************************************************************
*
* AlnMgr2AnchorSeqAlign takes an indexed seqalign and a row (1-based) and
* reindexes the alignment so that there are no gaps in the row indicated.
* Other rows may contain inserts after this operation. After an alignment
* is anchored, its length often shrinks. If which_row is less than 1, the
* function reindexes the alignment as a flat alignment.
*
***************************************************************************/
NLM_EXTERN void AlnMgr2AnchorSeqAlign(SeqAlignPtr sap, Int4 which_row)
{
AMAlignIndex2Ptr amaip;
if (sap == NULL || sap->saip == NULL)
return;
if (sap->saip->indextype == INDEX_CHILD)
AlnMgr2AnchorChild(sap, which_row);
else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return;
AlnMgr2AnchorChild(amaip->sharedaln, which_row);
amaip->anchor = which_row;
}
}
/* SECTION 5c */
/***************************************************************************
*
* AlnMgr2FindAnchor returns the row number (1-based) of the anchor row
* for an indexed seqalign, or -1 if the alignment is unanchored or if
* there is another type of error.
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2FindAnchor(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
SAIndex2Ptr saip;
if (sap == NULL || sap->saip == NULL)
return -1;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
return (saip->anchor);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return -1;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
return (saip->anchor);
} else
return -1;
}
/***************************************************************************
*
* SECTION 6: Functions for coordinate conversion (bioseq to seqalign
* coordinates and vice versa)
*
***************************************************************************/
/* SECTION 6 */
/***************************************************************************
*
* AlnMgr2MapBioseqToSeqAlign takes an indexed seqalign, a position in a
* row of the alignment, and a 1-based row number, and maps the row position
* to alignment coordinates.
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2MapBioseqToSeqAlign(SeqAlignPtr sap, Int4 pos, Int4 row)
{
AMAlignIndex2Ptr amaip;
Uint2Ptr array;
DenseSegPtr dsp;
Int4 L;
Int4 mid;
Int4 offset;
Int4 R;
Int4 retval;
SAIndex2Ptr saip;
SARowDat2Ptr srdp;
Int4 start;
Int4 stop;
Uint1 strand;
if (sap == NULL || sap->saip == NULL || row < 1)
return -1;
AlnMgr2GetNthSeqRangeInSA(sap, row, &start, &stop);
if (pos < start || pos > stop)
return -1;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
dsp = (DenseSegPtr)(sap->segs);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return -1;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
}
if (row > saip->numrows)
return -1;
srdp = saip->srdp[row-1];
if (srdp->numsect < 1) {
return -1;
}
strand = AlnMgr2GetNthStrand(sap, row);
L = 0;
R = srdp->numsect - 1;
if (strand != Seq_strand_minus)
{
while (L < R)
{
mid = (L + R)/2;
if (dsp->starts[(srdp->sect[mid+1])*(dsp->dim)+row-1] <= pos)
L = mid + 1;
else
R = mid;
}
} else
{
while (L < R)
{
mid = ceil((L + R)/2);
if (dsp->starts[(srdp->sect[mid])*(dsp->dim)+row-1] > pos)
L = mid + 1;
else
R = mid;
}
}
offset = pos - dsp->starts[(srdp->sect[L])*(dsp->dim)+row-1];
if (offset > dsp->lens[srdp->sect[L]])
return -2; /* this is an insert */
if (saip->anchor > 0)
{
array = saip->srdp[saip->anchor-1]->sect;
R = binary_search_on_uint2_list(array, srdp->sect[L], saip->srdp[saip->anchor-1]->numsect);
L = R;
srdp = saip->srdp[saip->anchor-1];
if (strand != Seq_strand_minus)
retval = (saip->aligncoords[L] + offset);
else
retval = (saip->aligncoords[L] + dsp->lens[srdp->sect[L]] - offset - 1);
} else
{
if (strand != Seq_strand_minus)
retval = saip->aligncoords[srdp->sect[L]] + offset;
else
retval = (saip->aligncoords[srdp->sect[L]] + dsp->lens[srdp->sect[L]] - offset - 1);
}
return retval;
}
/* SECTION 6 */
/***************************************************************************
*
* AlnMgr2MapSeqAlignToBioseq takes an indexed seqalign, an alignment
* coordinate (pos), and the 1-based number of a row, and maps the alignment
* coordinate to the corresponding bioseq coordinate of the row desired.
* A return of -1 indicates an error; a return of -2 means that the bioseq
* is gapped at this alignment position.
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2MapSeqAlignToBioseq(SeqAlignPtr sap, Int4 pos, Int4 row)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
Int4 len;
Int4 offset;
SAIndex2Ptr saip;
Int4 sect;
SARowDat2Ptr srdp;
Int4 start;
Uint1 strand;
Uint2Ptr trans;
if (sap == NULL || sap->saip == NULL)
return -1;
len = AlnMgr2GetAlnLength(sap, FALSE);
if (pos < 0 || pos > len - 1)
return -1;
if (sap->saip->indextype == INDEX_CHILD)
{
saip = (SAIndex2Ptr)(sap->saip);
dsp = (DenseSegPtr)(sap->segs);
} else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return -1;
saip = (SAIndex2Ptr)(amaip->sharedaln->saip);
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
}
if (row > saip->numrows)
return -1;
sect = binary_search_on_uint4_list(saip->aligncoords, pos, saip->numseg);
offset = pos - saip->aligncoords[sect];
if (saip->anchor > 0)
{
trans = saip->srdp[saip->anchor-1]->sect;
sect = trans[sect];
}
srdp = saip->srdp[row-1];
start = binary_search_on_uint2_list(srdp->sect, sect, srdp->numsect);
if (start == -1)
return -2; /* this row has a gap or insert at this alignment position */
strand = AlnMgr2GetNthStrand(sap, row);
if (strand != Seq_strand_minus)
return (dsp->starts[sect*(dsp->dim)+row-1] + offset);
else
return (dsp->starts[sect*(dsp->dim)+row-1] + dsp->lens[sect] - 1 - offset);
}
/* SECTION 6 */
/***************************************************************************
*
* AlnMgr2MapRowToRow takes an indexed seqalign, a position in row1, the
* 1-based number of row1, and a target row (row2), and maps the bioseq
* coordinate in row 1 to the corresponding (aligned) bioseq coordinate in
* row2. A return of -1 indicates an error while a return of -2 means that
* the bioseq in row2 is gapped at the desired position.
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2MapRowToRow(SeqAlignPtr sap, Int4 pos, Int4 row1, Int4 row2)
{
Int4 alnpos;
if (sap == NULL)
return -1;
alnpos = AlnMgr2MapBioseqToSeqAlign(sap, pos, row1);
return (AlnMgr2MapSeqAlignToBioseq(sap, alnpos, row2));
}
/***************************************************************************
*
* SECTION 7: Functions to change an alignment and retrieve parts of an
* alignment
*
***************************************************************************/
/***************************************************************************
*
* AlnMgr2TruncateSeqAlign truncates a given seqalign to contain only the
* bioseq coordinates from start to stop on the indicated row. Anything
* before those coordinates is discarded; anything remaining afterwards
* is made into another seqalign and put in sap->next (the original next,
* if any, is now at sap->next->next). Doesn't work on parent seqaligns.
* The function returns TRUE if the orignal alignment extended past stop.
*
***************************************************************************/
/* SECTION 7 */
NLM_EXTERN Boolean AlnMgr2TruncateSeqAlign(SeqAlignPtr sap, Int4 start, Int4 stop, Int4 row)
{
DenseDiagPtr ddp;
DenseDiagPtr ddp2;
DenseSegPtr dsp;
Int4 from;
Int4 i;
Int4 mstart;
Int4 mstop;
SeqAlignPtr sap1;
SeqAlignPtr sap2;
Int4 tmp;
Int4 to;
if (sap == NULL || stop<start || row < 1)
return FALSE;
if (sap->segtype == SAS_DENSEG)
{
if (sap->saip == NULL)
AlnMgr2IndexSingleChildSeqAlign(sap);
AlnMgr2GetNthSeqRangeInSA(sap, row, &mstart, &mstop);
if (mstart > start || mstop < stop)
return FALSE;
if (mstart == start)
{
if (mstop == stop)
return FALSE;
else if (mstop > stop)
{
from = AlnMgr2MapBioseqToSeqAlign(sap, start, row);
to = AlnMgr2MapBioseqToSeqAlign(sap, stop, row);
if (to < from)
{
tmp = to;
to = from;
from = tmp;
}
sap1 = AlnMgr2GetSubAlign(sap, from, to, 0, TRUE);
AlnMgr2IndexSingleChildSeqAlign(sap1);
from = AlnMgr2MapBioseqToSeqAlign(sap, stop+1, row);
if (from < 0)
return FALSE;
to = AlnMgr2MapBioseqToSeqAlign(sap, mstop, row);
if (to < from)
{
tmp = to;
to = from;
from = tmp;
}
sap2 = AlnMgr2GetSubAlign(sap, from, to, 0, TRUE);
sap2->next = sap->next;
sap->next = sap2;
dsp = (DenseSegPtr)(sap->segs);
sap->segs = (Pointer)(sap1->segs);
sap1->segs = NULL;
DenseSegFree(dsp);
SeqAlignFree(sap1);
AlnMgr2IndexSingleChildSeqAlign(sap);
AlnMgr2IndexSingleChildSeqAlign(sap2);
return TRUE;
}
} else if (mstart < start) /* throw away the first part */
{
from = AlnMgr2MapBioseqToSeqAlign(sap, start, row);
to = AlnMgr2MapBioseqToSeqAlign(sap, stop, row);
if (to < from)
{
tmp = to;
to = from;
from = tmp;
}
sap1 = AlnMgr2GetSubAlign(sap, from, to, 0, TRUE);
if (mstop == stop) /* done */
{
dsp = (DenseSegPtr)(sap->segs);
sap->segs = (Pointer)(sap1->segs);
sap1->segs = NULL;
DenseSegFree(dsp);
SeqAlignFree(sap1);
AlnMgr2IndexSingleChildSeqAlign(sap);
return FALSE;
} else if (mstop > stop)
{
from = AlnMgr2MapBioseqToSeqAlign(sap, stop+1, row);
if (from < 0)
return FALSE;
to = AlnMgr2MapBioseqToSeqAlign(sap, mstop, row);
if (to < from)
{
tmp = to;
to = from;
from = tmp;
}
sap2 = AlnMgr2GetSubAlign(sap, from, to, 0, TRUE);
sap2->next = sap->next;
sap->next = sap2;
AlnMgr2IndexSingleChildSeqAlign(sap2);
dsp = (DenseSegPtr)(sap->segs);
sap->segs = (Pointer)(sap1->segs);
sap1->segs = NULL;
DenseSegFree(dsp);
SeqAlignFree(sap1);
AlnMgr2IndexSingleChildSeqAlign(sap);
return TRUE;
}
}
} else if (sap->segtype == SAS_DENDIAG)
{
ddp = (DenseDiagPtr)(sap->segs);
if (ddp->dim < row)
return FALSE;
mstart = ddp->starts[row-1];
mstop = mstart + ddp->len - 1;
if (mstart > start || mstop < stop)
return FALSE;
if (mstart == start)
{
if (mstop == stop)
return FALSE;
else if (mstop > stop)
{
ddp2 = DenseDiagNew();
ddp2->dim = ddp->dim;
ddp2->starts = (Int4Ptr)MemNew((ddp->dim)*sizeof(Int4));
ddp2->id = SeqIdDupList(ddp->id);
ddp2->strands = (Uint1Ptr)MemNew((ddp->dim)*sizeof(Uint1));
ddp2->scores = ScoreDup(ddp->scores);
for (i=0; i<ddp->dim; i++)
{
ddp2->starts[i] = ddp->starts[i] + ddp->len - (mstop - stop);
ddp2->strands[i] = ddp->strands[i];
}
ddp2->len = mstop - stop;
ddp->len = ddp->len - (mstop - stop);
sap2 = SeqAlignNew();
sap2->type = SAT_PARTIAL;
sap2->segtype = SAS_DENSEG;
sap2->segs = (Pointer)ddp2;
sap2->next = sap->next;
sap->next = sap2;
AlnMgr2IndexSingleChildSeqAlign(sap2);
return TRUE;
}
} else if (mstart < start)
{
for (i=0; i<ddp->dim; i++)
{
ddp->starts[i] = ddp->starts[i] + start - mstart;
}
ddp->len = ddp->len - (start - mstart);
AlnMgr2IndexSingleChildSeqAlign(sap);
if (mstop == stop)
return FALSE;
else if (mstop > stop)
{
ddp2 = DenseDiagNew();
ddp2->dim = ddp->dim;
ddp2->starts = (Int4Ptr)MemNew((ddp->dim)*sizeof(Int4));
ddp2->id = SeqIdDupList(ddp->id);
ddp2->strands = (Uint1Ptr)MemNew((ddp->dim)*sizeof(Uint1));
ddp2->scores = ScoreDup(ddp->scores);
for (i=0; i<ddp->dim; i++)
{
ddp2->starts[i] = ddp->starts[i] + ddp->len - (mstop - stop);
ddp2->strands[i] = ddp->strands[i];
}
ddp2->len = mstop - stop;
ddp->len = ddp->len - (mstop - stop);
sap2 = SeqAlignNew();
sap2->type = SAT_PARTIAL;
sap2->segtype = SAS_DENSEG;
sap2->segs = (Pointer)ddp2;
sap2->next = sap->next;
sap->next = sap2;
AlnMgr2IndexSingleChildSeqAlign(sap2);
return TRUE;
}
}
} else
return FALSE;
return FALSE;
}
/* SECTION 7 */
/***************************************************************************
*
* AlnMgr2GetSubAlign retrieves a portion of an indexed alignment, from
* 'from' to 'to' in the row coordinates specified, or if which_row is 0,
* 'from' and 'to' are assumed to be alignment coordinates. If 'to' is -1,
* the subalignment will go to the end of the specified row (or to the end
* of the whole alignment). If the alignment is discontinuous and fill_in
* is FALSE, the alignment will be returned as an SAS_DISC set, each piece
* represented by a single alignment. If the alignment is discontinuous and
* fill_in is TRUE, the unaligned regions will be added in to the alignment,
* with all gaps in all other rows. If the alignment is continuous, it
* doesn't matter whether fill_in is TRUE or FALSE. (SUBALIGN)
*
***************************************************************************/
NLM_EXTERN SeqAlignPtr AlnMgr2GetSubAlign(SeqAlignPtr sap, Int4 from, Int4 to, Int4 which_row, Boolean fill_in)
{
Int4 a;
AMAlignIndex2Ptr amaip;
AlnMsg2Ptr amp;
Boolean anchored;
Int4 currlen;
DenseSegPtr dsp;
DenseSegPtr dsp_new;
Int4 from_aln;
Int4 from_seq;
Int4 i;
SeqIdPtr id;
Int4 j;
Int4 k;
Int4 len;
Int4 lengthbit;
Int4 minlen;
Boolean more;
Int4 n;
Int4 numseg;
Int4 numunaln;
AMRowInfoPtr row;
AMRowInfoPtr row_head;
AMRowInfoPtr row_prev;
AMRowInfoPtr PNTR rowheads;
AMRowInfoPtr PNTR rows;
SeqAlignPtr salp;
SeqAlignPtr salp_head;
SeqAlignPtr salp_prev;
SeqAlignPtr sap_real;
Int4 seg;
Int4 start_seg;
Uint1 strand;
SeqAlignPtr subsalp;
Int4 tmp;
Int4 to_aln;
Int4 to_seq;
Int4 ustart;
Int4 ustop;
if (sap == NULL || sap->saip == NULL)
return NULL;
len = AlnMgr2GetAlnLength(sap, FALSE);
if (which_row == 0 && (to > len-1 || from < 0))
return NULL;
n = AlnMgr2GetNumRows(sap);
if (which_row < 0 || which_row > n)
return NULL;
if (to == -1)
{
if (which_row == 0)
to = len-1;
else
AlnMgr2GetNthSeqRangeInSA(sap, which_row, NULL, &to);
}
if (sap->saip->indextype == INDEX_CHILD)
sap_real = sap;
else if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return NULL;
sap_real = amaip->sharedaln;
if (from == 0 && to == len-1 && !AlnMgr2IsSAPDiscAli(sap_real)) /* need whole aln -- take a shortcut! */
return SeqAlignDup(sap_real);
}
if ((a = AlnMgr2FindAnchor(sap_real)) > 0)
{
anchored = TRUE;
salp = SeqAlignDup(sap_real);
AlnMgr2IndexSingleChildSeqAlign(salp);
if (which_row == 0) /* anchor coordinates */
{
AlnMgr2GetNthSeqRangeInSA(salp, a, &from_seq, &to_seq);
from_aln = AlnMgr2MapBioseqToSeqAlign(salp, from_seq, a);
to_aln = AlnMgr2MapBioseqToSeqAlign(salp, to_seq, a);
if (from_aln > to_aln)
{
tmp = from_aln;
from_aln = to_aln;
to_aln = tmp;
}
} else
{
from_aln = AlnMgr2MapBioseqToSeqAlign(salp, from, which_row);
to_aln = AlnMgr2MapBioseqToSeqAlign(salp, to, which_row);
if (from_aln > to_aln)
{
tmp = from_aln;
from_aln = to_aln;
to_aln = tmp;
}
}
} else
{
anchored = FALSE;
salp = sap_real;
if (which_row == 0) /* alignment coordinates */
{
from_aln = from;
to_aln = to;
} else
{
from_aln = AlnMgr2MapBioseqToSeqAlign(salp, from, which_row);
to_aln = AlnMgr2MapBioseqToSeqAlign(salp, to, which_row);
if (from_aln > to_aln)
{
tmp = from_aln;
from_aln = to_aln;
to_aln = tmp;
}
}
}
rows = (AMRowInfoPtr PNTR)MemNew(n*sizeof(AMRowInfoPtr));
amp = AlnMsgNew2();
seg = lengthbit = 0;
currlen = 0;
numunaln = 0;
salp_head = salp_prev = NULL;
while (AlnMgr2GetNextLengthBit(sap, &lengthbit, &seg))
{
if (currlen <= to_aln && seg >= 0 && currlen+lengthbit-1 >= from_aln)
{
numseg = AlnMgr2GetNumSegsInRange(sap, currlen, currlen+lengthbit-1, &start_seg);
numunaln = 0;
for (i=0; i<n; i++)
{
row_head = NULL;
for (j=start_seg; j<numseg+start_seg; j++)
{
AlnMsgReNew2(amp);
AlnMgr2GetNthSegmentRange(sap, j+1, &->from_aln, &->to_aln);
amp->from_aln = MAX(amp->from_aln, from_aln);
amp->to_aln = MIN(amp->to_aln, to_aln);
amp->row_num = i+1;
while ((more = AlnMgr2GetNextAlnBit(salp, amp)) == TRUE)
{
if (amp->right_interrupt != NULL && amp->right_interrupt->unalnlen > 0)
numunaln++;
row = (AMRowInfoPtr)MemNew(sizeof(AMRowInfo));
if (amp->type == AM_GAP)
row->from = -1;
else
row->from = amp->from_row;
row->len = amp->to_row - amp->from_row + 1;
if (row_head != NULL)
{
row_prev->next = row;
row_prev = row;
} else
row_head = row_prev = row;
}
}
rows[i] = row_head;
}
}
rowheads = (AMRowInfoPtr PNTR)MemNew(n*sizeof(AMRowInfoPtr));
for (i=0; i<n; i++)
{
rowheads[i] = rows[i];
}
while (rows[0] != NULL)
{
minlen = -1;
for (i=0; i<n; i++)
{
if (rows[i]->len < minlen || minlen == -1)
minlen = rows[i]->len;
}
for (i=0; i<n; i++)
{
if (rows[i]->len > minlen)
{
row = (AMRowInfoPtr)MemNew(sizeof(AMRowInfo));
row->next = rows[i]->next;
rows[i]->next = row;
if (rows[i]->from == -1)
row->from = -1;
else if (AlnMgr2GetNthStrand(salp, i) == Seq_strand_minus)
{
row->from = rows[i]->from;
rows[i]->from = rows[i]->from + rows[i]->len - 1 - minlen;
} else
row->from = rows[i]->from + minlen;
row->len = rows[i]->len - minlen;
rows[i]->len = minlen;
}
rows[i] = rows[i]->next;
}
}
for (i=0; i<n; i++)
{
rows[i] = rowheads[i];
}
MemFree(rowheads);
dsp = DenseSegNew();
row = rows[0];
while (row != NULL)
{
dsp->numseg++;
row = row->next;
}
if (fill_in)
dsp->numseg += numunaln;
dsp->dim = n;
dsp->lens = (Int4Ptr)MemNew((dsp->numseg)*sizeof(Int4));
dsp->starts = (Int4Ptr)MemNew((dsp->numseg)*(dsp->dim)*sizeof(Int4));
dsp->strands = (Uint1Ptr)MemNew((dsp->numseg)*(dsp->dim)*sizeof(Int4));
j = 0;
row = rows[0];
while (row != NULL)
{
dsp->lens[j] = row->len;
j++;
row = row->next;
}
id = AlnMgr2GetNthSeqIdPtr(salp, 0);
dsp->ids = id;
for (i=0; i<n; i++)
{
if (i > 0)
{
id->next = AlnMgr2GetNthSeqIdPtr(salp, i+1);
id = id->next;
}
row = rows[i];
j = 0;
strand = AlnMgr2GetNthStrand(salp, i+1);
while (row != NULL)
{
dsp->starts[n*j + i] = row->from;
dsp->strands[n*j + i] = strand;
j++;
row = row->next;
}
}
if (fill_in)
{
for (i=0; i<n; i++)
{
AlnMgr2GetNthUnalignedForNthRow(sap, seg+1, i+1, &ustart, &ustop);
if (ustart >= 0 && ustop >= ustart)
{
for (k=0; k<n; k++)
{
dsp->starts[n*j + k] = -1;
dsp->strands[n*j + k] = dsp->strands[i];
}
dsp->starts[n*j + i] = ustart;
j++;
}
}
}
subsalp = SeqAlignNew();
subsalp->type = SAT_PARTIAL;
subsalp->segtype = SAS_DENSEG;
subsalp->dim = n;
subsalp->segs = (Pointer)(dsp);
for (i=0; i<n; i++)
{
row = rows[i];
while (row != NULL)
{
row_prev = row->next;
MemFree(row);
row = row_prev;
}
}
if (seg < 0)
seg = -seg;
currlen += lengthbit;
seg++;
if (salp_head != NULL)
{
salp_prev->next = subsalp;
salp_prev = subsalp;
} else
salp_head = salp_prev = subsalp;
}
MemFree(rows);
AlnMsgFree2(amp);
if (fill_in && salp_head->next != NULL) /* stick subsalps together into a big aln */
{
j = 0;
subsalp = salp_head;
while (subsalp != NULL)
{
dsp = (DenseSegPtr)(subsalp->segs);
j += dsp->numseg;
subsalp = subsalp->next;
}
dsp_new = DenseSegNew();
dsp_new->dim = n;
dsp_new->numseg = j;
dsp_new->lens = (Int4Ptr)MemNew((dsp->numseg)*sizeof(Int4));
dsp_new->starts = (Int4Ptr)MemNew((dsp->numseg)*(dsp->dim)*sizeof(Int4));
dsp_new->strands = (Uint1Ptr)MemNew((dsp->numseg)*(dsp->dim)*sizeof(Int4));
subsalp = salp_head;
k = 0;
while (subsalp != NULL)
{
dsp = (DenseSegPtr)(subsalp->segs);
for (j=0; j<dsp->numseg; j++)
{
dsp_new->lens[k] = dsp->lens[j];
for (i=0; i<n; i++)
{
dsp_new->starts[k*n+i] = dsp->starts[j*n+i];
dsp_new->strands[k*n+i] = dsp->strands[j*n+i];
}
k++;
}
subsalp = subsalp->next;
}
subsalp = SeqAlignNew();
subsalp->type = SAT_PARTIAL;
subsalp->segtype = SAS_DENSEG;
subsalp->dim = n;
subsalp->segs = (Pointer)(dsp_new);
SeqAlignSetFree(salp_head);
} else if (!fill_in && salp_head->next != NULL)
{
subsalp = SeqAlignNew();
subsalp->segtype = SAS_DISC;
subsalp->type = SAT_PARTIAL;
subsalp->segs = (SeqAlignPtr)(salp_head);
salp_prev = salp_head;
while (salp_prev != NULL)
{
AMAlignIndexFreeEitherIndex(salp_prev);
salp_prev = salp_prev->next;
}
} else /* if !salp_head->next */
{
subsalp = salp_head;
subsalp->dim = AlnMgr2GetNumRows(subsalp);
subsalp->type = SAT_PARTIAL;
AMAlignIndexFreeEitherIndex(subsalp);
}
if (anchored)
SeqAlignFree(salp);
return subsalp;
}
/***************************************************************************
*
* SECTION 8: Miscellaneous functions to compute useful information
* about an alignment
*
***************************************************************************/
/* SECTION 8 */
/***************************************************************************
*
* AlnMgr2ComputeScoreForSeqAlign computes an ad hoc numerical score for
* an indexed alignment by computing a similarity score for the whole
* alignment (residue pair by residue pair score, from a matrix for proteins
* and identity for nucleotides) and then subtracting gap open and gap
* extension penalties.
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2ComputeScoreForSeqAlign(SeqAlignPtr sap)
{
AMFreqPtr afp;
DenseSegPtr dsp;
Int4 gaplen;
Int4 i;
Boolean is_prot;
Int4 j;
Int4 len;
Int4 mismatch;
Int4 numgaps;
Int4 numseqs;
Boolean open;
Int4 res1;
Int4 res2;
Int4 score;
Int4 seqscore;
if (sap->segtype == SAS_DISC)
return -1;
if (sap->saip == NULL)
AlnMgr2IndexSingleChildSeqAlign(sap);
is_prot = AlnMgr2IsItProtein(sap);
len = AlnMgr2GetAlnLength(sap, FALSE);
dsp = (DenseSegPtr)(sap->segs);
numseqs = dsp->dim;
open = FALSE;
gaplen = 0;
numgaps = 0;
for (i=0; i<dsp->dim; i++)
{
for (j=0; j<dsp->numseg; j++)
{
if (dsp->starts[(dsp->dim)*j+i] == -1)
{
if (!open)
{
gaplen += dsp->lens[j];
numgaps++;
open = TRUE;
} else
gaplen += dsp->lens[j];
} else
open = FALSE;
}
}
mismatch = 0;
seqscore = 0;
afp = AlnMgr2ComputeFreqMatrix(sap, 0, -1, 0);
if (afp == NULL)
return -1;
for (i=0; i<afp->len; i++)
{
res1 = -1;
res2 = -1;
for (j=0; j<afp->size; j++)
{
if (afp->freq[j][i] == 1)
{
if (res1 == -1)
res1 = j;
else
res2 = j;
} else if (afp->freq[j][i] == 2)
res1 = res2 = j;
}
if (res1 > 0 && res2 > 0) /* don't penalize gaps */
seqscore += AlnMgr2GetScoreForPair(res1, res2, is_prot);
}
AMFreqFree(afp);
score = seqscore + numgaps*AM_GAPOPEN + gaplen*AM_GAPEXT;
return score;
}
static Int4 AlnMgr2SeqPortRead(SeqPortPtr PNTR spp, Uint1Ptr buf, Int4Ptr bufpos, Int4 start, Int4 stop, Uint1 strand, Uint1 code, BioseqPtr bsp)
{
if (*spp == NULL) /* first call */ {
if (strand == Seq_strand_minus){
*spp = SeqPortNew(bsp, MAX(0, stop-AM_SEQPORTSIZE), stop, strand, code);
*bufpos = MAX(0, stop-AM_SEQPORTSIZE);
}
else {
*spp = SeqPortNew(bsp, start, MIN(start+AM_SEQPORTSIZE, bsp->length-1), strand, code);
*bufpos = start;
}
}
/* see if what we need is in current seqport or a new one is needed */
else if ((start < *bufpos) || (start > *bufpos+AM_SEQPORTSIZE)
|| (stop < *bufpos) || (stop > *bufpos+AM_SEQPORTSIZE)) {
SeqPortFree(*spp);
if (strand == Seq_strand_minus) {
*spp = SeqPortNew(bsp, MAX(0, stop-AM_SEQPORTSIZE), stop, strand, code);
*bufpos = MAX(0, stop-AM_SEQPORTSIZE);
}
else {
*spp = SeqPortNew(bsp, start, MIN(start+AM_SEQPORTSIZE, bsp->length-1), strand, code);
*bufpos = start;
}
}
return (SeqPortRead(*spp, buf, (MIN(start+AM_SEQPORTSIZE-1, stop)) - start+1));
}
/* SECTION 8 */
/***************************************************************************
*
* AlnMgr2ComputeFreqMatrix takes an indexed seqalign and returns a matrix
* indicating nucleotide or amino acid frequency at each position of the
* alignment. The matrix can be made over only a part of the alignment, if
* from and to are nonzero, and if row is nonzero, from and to are taken
* to be bioseq coordinates from that row (if row == 0 from and to are
* assumed to be alignment coordinates).
*
***************************************************************************/
NLM_EXTERN AMFreqPtr AlnMgr2ComputeFreqMatrix(SeqAlignPtr sap, Int4 from, Int4 to, Int4 row)
{
AMFreqPtr afp;
AlnMsg2Ptr amp;
BioseqPtr bsp;
Uint1 buf[AM_SEQPORTSIZE];
Int4 bufpos;
Uint1 code;
Int4 counter;
Int4 ctr;
Int4 from_a;
Int4 i;
Boolean isna;
Int4 j;
Int4 l;
Int4 len;
Boolean more;
Int4 n;
Int4 numrows;
Uint1 res;
SeqIdPtr sip;
SeqPortPtr spp;
Int4 tmp;
Int4 to_a;
if (sap == NULL || sap->saip == NULL || (from > to && to != -1))
return NULL;
numrows = AlnMgr2GetNumRows(sap);
bufpos = -1;
if (row > numrows || row < 0)
return NULL;
len = AlnMgr2GetAlnLength(sap, FALSE);
if (to >= len)
return NULL;
if (to == -1)
to = len-1;
sip = AlnMgr2GetNthSeqIdPtr(sap, 1);
bsp = BioseqLockById(sip);
if (bsp != NULL)
isna = ISA_na(bsp->mol);
else
{
SeqIdFree(sip);
return NULL;
}
BioseqUnlock(bsp);
SeqIdFree(sip);
if (isna)
code = Seq_code_ncbi4na;
else
code = Seq_code_ncbistdaa;
afp = (AMFreqPtr)MemNew(sizeof(AMFreq));
afp->len = len;
if (isna)
afp->size = AM_NUCSIZE;
else
afp->size = AM_PROTSIZE;
afp->freq = (Int4Ptr PNTR)MemNew((afp->size)*sizeof(Int4Ptr));
for (i=0; i<afp->size; i++)
{
afp->freq[i] = (Int4Ptr)MemNew((afp->len)*sizeof(Int4));
}
amp = AlnMsgNew2();
if (row != 0)
{
from_a = AlnMgr2MapBioseqToSeqAlign(sap, from, row);
to_a = AlnMgr2MapBioseqToSeqAlign(sap, to, row);
if (from_a > to_a)
{
tmp = to_a;
to_a = from_a;
from_a = tmp;
}
} else
{
from_a = from;
to_a = to;
}
for (i=0; i<numrows; i++)
{
spp = NULL;
AlnMsgReNew2(amp);
amp->from_aln = from_a;
amp->to_aln = to_a;
amp->row_num = i+1;
j = 0;
while ((more = AlnMgr2GetNextAlnBit(sap, amp)))
{
if (amp->type == AM_GAP)
{
for (n=0; n<(amp->to_row - amp->from_row+1); n++)
{
afp->freq[0][j] = afp->freq[0][j]+1;
j++;
}
} else if (amp->type == AM_SEQ)
{
sip = AlnMgr2GetNthSeqIdPtr(sap, i+1);
bsp = BioseqLockById(sip);
if (bsp != NULL) {
for (l=amp->from_row; l<=amp->to_row; l+=AM_SEQPORTSIZE)
{
counter = AlnMgr2SeqPortRead(&spp, buf, &bufpos, l, MIN(l+AM_SEQPORTSIZE, amp->to_row), amp->strand, code, bsp);
ctr = 0;
while (ctr < counter)
{
res = buf[ctr];
if (isna)
{
if (res == 1 || res == 2)
afp->freq[res][j]++;
else if (res == 4)
afp->freq[3][j]++;
else if (res == 8)
afp->freq[4][j]++;
else
afp->freq[5][j]++;
} else
afp->freq[res][j]++;
j++;
ctr++;
}
}
BioseqUnlock(bsp);
}
SeqIdFree(sip);
}
}
SeqPortFree(spp);
}
AlnMsgFree2(amp);
return afp;
}
/* SECTION 8 */
/***************************************************************************
*
* AlnMgr2GetScoreForPair assigns scores to nucleotide and protein residue
* pairs. Nucleotide pairs are scored according to a standard mismatch
* penalty, and amino acid pairs are scored according to the BLOSUM62
* matrix below. This matrix has been rearranged so that the rows and
* columns appear in alphabetical order, so that it directly correlates
* to the NCBIstdaa alphabet (with a minus-one difference).
*
***************************************************************************/
static Int4 AlnMgr2GetScoreForPair(Int4 res1, Int4 res2, Boolean is_prot)
{
Int4 matrix[24][24] = {
{4, -2, 0, -2, -1, -2, 0, -2, -1, -1, -1, -1, -2, -1, -1, -1, 1, 0, 0, -3, 0, -2, -1, -4},
{-2, 4, -3, 4, 1, -3, -1, 0, -3, 0, -4, -3, 3, -2, 0, -1, 0, -1, -3, -4, -1, -3, 1, -4},
{0, -3, 9, -3, -4, -2, -3, -3, -1, -3, -1, -1, -3, -3, -3, -3, -1, -1, -1, -2, -2, -2, -3, -4},
{-2, 4, -3, 6, 2, -3, -1, -1, -3, -1, -4, -3, 1, -1, 0, -2, 0, -1, -3, -4, -1, -3, 1, -4},
{-1, 1, -4, 2, 5, -3, -2, 0, -3, 1, -3, -2, 0, -1, 2, 0, 0, -1, -2, -3, -1, -2, 4, -4},
{-2, -3, -2, -3, -3, 6, -3, -1, 0, -3, 0, 0, -3, -4, -3, -3, -2, -2, -1, 1, -1, 3, -3, -4},
{0, -1, -3, -1, -2, -3, 6, -2, -4, -2, -4, -3, 0, -2, -2, -2, 0, -2, -3, -2, -1, -3, -2, -4},
{-2, 0, -3, -1, 0, -1, -2, 8, -3, -1, -3, -2, 1, -2, 0, 0, -1, -2, -3, -2, -1, 2, 0, -4},
{-1, -3, -1, -3, -3, 0, -4, -3, 4, -3, 2, 1, -3, -3, -3, -3, -2, -1, 3, -3, -1, -1, -3, -4},
{-1, 0, -3, -1, 1, -3, -2, -1, -3, 5, -2, -1, 0, -1, 1, 2, 0, -1, -2, -3, -1, -2, 1, -4},
{-1, -4, -1, -4, -3, 0, -4, -3, 2, -2, 4, 2, -3, -3, -2, -2, -2, -1, 1, -2, -1, -1, -3, -4},
{-1, -3, -1, -3, -2, 0, -3, -2, 1, -1, 2, 5, -2, -2, 0, -1, -1, -1, 1, -1, -1, -1, -1, -4},
{-2, 3, -3, 1, 0, -3, 0, 1, -3, 0, -3, -2, 6, -2, 0, 0, 1, 0, -3, -4, -1, -2, 0, -4},
{-1, -2, -3, -1, -1, -4, -2, -2, -3, -1, -3, -2, -2, 7, -1, -2, -1, -1, -2, -4, -2, -3, -1, -4},
{-1, 0, -3, 0, 2, -3, -2, 0, -3, 1, -2, 0, 0, -1, 5, 1, 0, -1, -2, -2, -1, -1, 3, -4},
{-1, -1, -3, -2, 0, -3, -2, 0, -3, 2, -2, -1, 0, -2, 1, 5, -1, -1, -3, -3, -1, -2, 0, -4},
{1, 0, -1, 0, 0, -2, 0, -1, -2, 0, -2, -1, 1, -1, 0, -1, 4, 1, -2, -3, 0, -2, 0, -4},
{0, -1, -1, -1, -1, -2, -2, -2, -1, -1, -1, -1, 0, -1, -1, -1, 1, 5, 0, -2, 0, -2, -1, -4},
{0, -3, -1, -3, -2, -1, -3, -3, 3, -2, 1, 1, -3, -2, -2, -3, -2, 0, 4, -3, -1, -1, -2, -4},
{-3, -4, -2, -4, -3, 1, -2, -2, -3, -3, -2, -1, -4, -4, -2, -3, -3, -2, -3, 11, -2, 2, -3, -4},
{0, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -1, -1, 0, 0, -1, -2, -1, -1, -1, -4},
{-2, -3, -2, -3, -2, 3, -3, 2, -1, -2, -1, -1, -2, -3, -1, -2, -2, -2, -1, 2, -1, 7, -2, -4},
{-1, 1, -3, 1, 4, -3, -2, 0, -3, 1, -3, -1, 0, -1, 3, 0, 0, -1, -2, -3, -1, -2, 4, -4},
{-4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, 1}};
if (is_prot) /* protein->use BLOSUM62 matrix */
return matrix[res1-1][res2-1];
else /* nucleotide->use match score/mismatch penalty */
{
if (res1 == 0 || res2 == 0) /* don't count gaps */
return 0;
if (res1 == res2)
return 1;
else
return -3;
}
}
/* SECTION 8 */
/***************************************************************************
*
* AlnMgr2IsItProtein takes an indexed alignment and quickly decides if
* it's a protein or nucleotide alignment, returning TRUE for protein.
*
***************************************************************************/
NLM_EXTERN Boolean AlnMgr2IsItProtein(SeqAlignPtr sap)
{
BioseqPtr bsp;
Boolean is_na;
SeqIdPtr sip;
if (sap == NULL || sap->saip == NULL)
return FALSE;
sip = AlnMgr2GetNthSeqIdPtr(sap, 1);
bsp = BioseqLockById(sip);
if (bsp == NULL)
return FALSE;
is_na = ISA_na(bsp->mol);
SeqIdFree(sip);
BioseqUnlock(bsp);
return (!is_na);
}
/***************************************************************************
*
* SECTION 9: Sorting functions and other algorithms to help order
* alignments for various purposes
*
***************************************************************************/
/* SECTION 9 */
static int LIBCALLBACK AMCompareStarts(VoidPtr ptr1, VoidPtr ptr2)
{
AMBitty2Ptr bit1;
AMBitty2Ptr bit2;
if (ptr1 != NULL && ptr2 != NULL)
{
bit1 = (AMBitty2Ptr)ptr1;
bit2 = (AMBitty2Ptr)ptr2;
if (bit1->num2 < bit2->num2)
return -1;
else if (bit1->num2 > bit2->num2)
return 1;
else if (bit1->num3 > bit2->num3) /* compare aln lengths */
return -1;
else if (bit1->num3 < bit2->num3)
return 1;
else
return 0;
}
return 0;
}
/* SECTION 9 */
/***************************************************************************
*
* AlnMgr2SortAlnSetByNthRowPos takes an indexed parent alignment and sorts
* all the child alignments along the row indicated. If the indicated row
* is aligned on the plus strand, the alignments are sorted from smaller
* to larger coordinates along that row; otherwise they are sorted in
* reverse order.
*
***************************************************************************/
NLM_EXTERN void AlnMgr2SortAlnSetByNthRowPos(SeqAlignPtr sap, Int4 row)
{
AMAlignIndex2Ptr amaip;
AMBitty2Ptr bit;
Int4 i;
SeqAlignPtr PNTR saparray;
Uint1 strand;
if (sap == NULL || sap->saip == NULL || sap->saip->indextype != INDEX_PARENT)
return;
amaip = (AMAlignIndex2Ptr)(sap->saip);
bit = (AMBitty2Ptr)MemNew((amaip->numsaps)*sizeof(AMBitty2));
saparray = (SeqAlignPtr PNTR)MemNew((amaip->numsaps)*sizeof(SeqAlignPtr));
for (i=0; i<amaip->numsaps; i++)
{
bit[i].num1 = i;
AlnMgr2GetNthSeqRangeInSA(amaip->saps[i], row, &bit[i].num2, NULL);
bit[i].num3 = AlnMgr2GetAlnLength(amaip->saps[i], FALSE);
strand = AlnMgr2GetNthStrand(amaip->saps[i], row);
if (strand == Seq_strand_minus)
bit[i].num2 = -bit[i].num2;
saparray[i] = amaip->saps[i];
}
HeapSort(bit, amaip->numsaps, sizeof(AMBitty2), AMCompareStarts);
for (i=0; i<amaip->numsaps; i++)
{
amaip->saps[i] = saparray[bit[i].num1];
}
MemFree(saparray);
MemFree(bit);
if (amaip->alnstyle != AM2_LITE)
AlnMgr2ReIndexSeqAlign(sap);
}
/***************************************************************************
*
* SECTION 10: Basic alignment operations
*
***************************************************************************/
/***************************************************************************
*
* AlnMgr2MergeTwoAlignments takes two alignments, with identical rows in
* the same order (otherwise it rejects the alignments), and merges them
* into a single alignment. If there is unaligned space between the two
* alignments and this space is the same length for every row, the function
* aligns those sequences; it rejects alignments when the unaligned spaces
* are different sizes. The function returns a newly allocated alignment.
*
***************************************************************************/
NLM_EXTERN SeqAlignPtr AlnMgr2MergeTwoAlignments(SeqAlignPtr sap1_orig, SeqAlignPtr sap2_orig)
{
Int4 c;
DenseSegPtr dsp;
DenseSegPtr dsp1;
DenseSegPtr dsp2;
DenseSegPtr dsp_new;
Int4 i;
Int4 j;
Int4 n1;
Int4 n2;
SeqAlignPtr sap1;
SeqAlignPtr sap2;
SeqAlignPtr sap_new;
SeqIdPtr sip1;
SeqIdPtr sip2;
Int4 start1;
Int4 start2;
Int4 stop1;
Int4 stop2;
Uint1 strand1;
Uint1 strand2;
SeqAlignPtr tmp;
if (sap1_orig == NULL || sap2_orig == NULL)
return NULL;
if (sap1_orig->next != NULL)
{
AlnMgr2IndexSeqAlign(sap1_orig);
sap1 = AlnMgr2GetSubAlign(sap1_orig, 0, -1, 0, TRUE);
} else
sap1 = SeqAlignDup(sap1_orig);
if (sap2_orig->next != NULL)
{
AlnMgr2IndexSeqAlign(sap2_orig);
sap2 = AlnMgr2GetSubAlign(sap2_orig, 0, -1, 0, TRUE);
} else
sap2 = SeqAlignDup(sap2_orig);
AlnMgr2IndexSingleChildSeqAlign(sap1);
AlnMgr2IndexSingleChildSeqAlign(sap2);
n1 = AlnMgr2GetNumRows(sap1);
n2 = AlnMgr2GetNumRows(sap2);
if (n1 != n2)
{
SeqAlignFree(sap1);
SeqAlignFree(sap2);
return NULL;
}
/* put the alignments in order by the first row */
AlnMgr2GetNthSeqRangeInSA(sap1, 1, &start1, &stop1);
AlnMgr2GetNthSeqRangeInSA(sap2, 1, &start2, &stop2);
strand1 = AlnMgr2GetNthStrand(sap1, 1);
if (strand1 == Seq_strand_minus)
{
if (stop2 > start1)
{
tmp = sap1;
sap1 = sap2;
sap2 = tmp;
}
} else
{
if (stop1 > start2)
{
tmp = sap1;
sap1 = sap2;
sap2 = tmp;
}
}
dsp1 = (DenseSegPtr)(sap1->segs);
dsp2 = (DenseSegPtr)(sap2->segs);
sip1 = dsp1->ids;
sip2 = dsp2->ids;
while (sip1 != NULL && sip2 != NULL)
{
if (SeqIdComp(sip1, sip2) != SIC_YES)
{
SeqAlignFree(sap1);
SeqAlignFree(sap2);
return NULL;
}
sip1 = sip1->next;
sip2 = sip2->next;
}
dsp = DenseSegNew();
dsp->dim = n1;
dsp->numseg = 1;
dsp->starts = (Int4Ptr)MemNew(n1*sizeof(Int4));
dsp->lens = (Int4Ptr)MemNew(sizeof(Int4));
dsp->strands = (Uint1Ptr)MemNew(n1*sizeof(Int4));
for (i=0; i<n1; i++)
{
strand1 = AlnMgr2GetNthStrand(sap1, i+1);
strand2 = AlnMgr2GetNthStrand(sap2, i+1);
if (strand1 != strand2)
{
DenseSegFree(dsp);
SeqAlignFree(sap1);
SeqAlignFree(sap2);
return NULL;
}
AlnMgr2GetNthSeqRangeInSA(sap1, i+1, &start1, &stop1);
AlnMgr2GetNthSeqRangeInSA(sap2, i+1, &start2, &stop2);
if (strand1 == Seq_strand_minus)
{
dsp->starts[i] = stop2 + 1;
if (i == 0)
dsp->lens[0] = start2 - (stop2 + 1);
else
{
if (start2 - (stop2 + 1) != dsp->lens[0])
{
DenseSegFree(dsp);
SeqAlignFree(sap1);
SeqAlignFree(sap2);
return NULL;
}
}
} else
{
dsp->starts[i] = stop1 + 1;
if (i == 0)
dsp->lens[0] = start2 - (stop1 + 1);
else
{
if (start2 - (stop1 + 1) != dsp->lens[0])
{
DenseSegFree(dsp);
SeqAlignFree(sap1);
SeqAlignFree(sap2);
return NULL;
}
}
}
dsp->strands[i] = strand1;
}
if (dsp->lens[0] == 0)
{
DenseSegFree(dsp);
dsp = NULL;
}
dsp_new = DenseSegNew();
dsp_new->numseg = dsp1->numseg + dsp2->numseg;
if (dsp != NULL)
dsp_new->numseg++;
dsp_new->dim = n1;
dsp_new->starts = (Int4Ptr)MemNew(dsp_new->dim*dsp_new->numseg*sizeof(Int4));
dsp_new->lens = (Int4Ptr)MemNew(dsp_new->numseg*sizeof(Int4));
dsp_new->strands = (Uint1Ptr)MemNew(dsp_new->dim*dsp_new->numseg*sizeof(Uint1));
for (i=0; i<dsp1->numseg; i++)
{
for (j=0; j<n1; j++)
{
dsp_new->starts[i*n1 + j] = dsp1->starts[i*n1 + j];
dsp_new->strands[i*n1 + j] = dsp1->strands[i*n1 + j];
}
dsp_new->lens[i] = dsp1->lens[i];
}
c = dsp1->numseg;
if (dsp != NULL)
{
for (j=0; j<n1; j++)
{
dsp_new->starts[c*n1 + j] = dsp->starts[j];
dsp_new->strands[c*n1 + j] = dsp->strands[j];
}
dsp_new->lens[c] = dsp->lens[0];
c++;
}
for (i=0; i<dsp2->numseg; i++, c++)
{
for (j=0; j<n1; j++)
{
dsp_new->starts[c*n1 + j] = dsp2->starts[i*n1 + j];
dsp_new->strands[c*n1 + j] = dsp2->strands[i*n1 + j];
}
dsp_new->lens[c] = dsp2->lens[i];
}
dsp_new->ids = SeqIdDupList(dsp1->ids);
sap_new = SeqAlignNew();
sap_new->segtype = SAS_DENSEG;
sap_new->dim = n1;
sap_new->segs = (Pointer)dsp_new;
if (dsp != NULL)
DenseSegFree(dsp);
SeqAlignFree(sap1);
SeqAlignFree(sap2);
return sap_new;
}
/* SECTION 10 */
/***************************************************************************
*
* AlnMgr2ExtendToCoords takes an indexed child seqalign and blindly extends
* it to the coordinates specified on the given row. If other rows are too
* short to allow this extension, the alignment is extended as far as
* possible. If to == -1 the extension goes to the end of the sequence
* specified.
*
***************************************************************************/
NLM_EXTERN void AlnMgr2ExtendToCoords(SeqAlignPtr sap, Int4 from, Int4 to, Int4 row)
{
BioseqPtr bsp;
Int4 diff1;
Int4 diff2;
DenseSegPtr dsp;
DenseSegPtr dsp_new;
Int4 i;
Int4 j;
Int4 numrows;
Int4 numseg;
Int4 prediff1;
Int4 prediff2;
Int4 seg;
SeqIdPtr sip;
Int4 start;
Int4 stop;
if (sap == NULL || sap->saip == NULL || sap->saip->indextype != INDEX_CHILD)
return;
numrows = AlnMgr2GetNumRows(sap);
if (row < 1 || row > numrows)
return;
AlnMgr2GetNthSeqRangeInSA(sap, row, &start, &stop);
numseg = 0;
dsp = (DenseSegPtr)(sap->segs);
if (start <= from)
from = start;
else
numseg++;
diff1 = start - from;
sip = AlnMgr2GetNthSeqIdPtr(sap, row);
bsp = BioseqLockById(sip);
if (to == -1)
to = bsp->length - 1;
BioseqUnlock(bsp);
SeqIdFree(sip);
if (stop >= to)
to = stop;
else
numseg++;
diff2 = to - stop;
if (numseg == 0)
return;
sip = dsp->ids;
prediff1 = diff1;
prediff2 = diff2;
for (i=0; i<numrows; i++)
{
bsp = BioseqLockById(sip);
if (dsp->strands[i] == Seq_strand_minus)
{
if (dsp->starts[i]+dsp->lens[0]+diff1 > bsp->length)
diff1 = bsp->length - (dsp->starts[i] + dsp->lens[0]);
if (dsp->starts[(dsp->numseg-1)*dsp->dim+i] > diff2)
diff2 = dsp->starts[(dsp->numseg-1)*dsp->dim+i];
} else
{
if (dsp->starts[i] < diff1)
diff1 = dsp->starts[i];
if (dsp->starts[(dsp->numseg-1)*dsp->dim+i]+dsp->lens[dsp->numseg-1]+diff2 > bsp->length)
diff2 = bsp->length - (dsp->starts[(dsp->numseg-1)*dsp->dim+i] + dsp->lens[dsp->numseg-1]);
}
sip = sip->next;
BioseqUnlock(bsp);
}
if (diff1 == 0 && prediff1 != 0)
numseg--;
else if (diff1 < 0)
numseg--;
if (diff2 == 0 && prediff2 != 0)
numseg--;
else if (diff2 < 0)
numseg--;
if (numseg == 0)
return;
dsp_new = DenseSegNew();
dsp_new->dim = dsp->dim;
dsp_new->numseg = dsp->numseg+numseg;
dsp_new->starts = (Int4Ptr)MemNew(dsp_new->dim*dsp_new->numseg*sizeof(Int4));
dsp_new->lens = (Int4Ptr)MemNew(dsp_new->numseg*sizeof(Int4));
dsp_new->strands = (Uint1Ptr)MemNew(dsp_new->dim*dsp_new->numseg*sizeof(Uint1));
seg = 0;
if (diff1 > 0)
{
for (j=0; j<dsp->dim; j++)
{
AlnMgr2GetNthSeqRangeInSA(sap, j+1, &start, &stop);
if (dsp->strands[j] == Seq_strand_minus)
dsp_new->starts[j] = stop+1;
else
dsp_new->starts[j] = start-diff1;
dsp_new->strands[j] = dsp->strands[j];
}
dsp_new->lens[0] = diff1;
seg++;
}
for (i=0; i<dsp->numseg; i++)
{
for (j=0; j<dsp->dim; j++)
{
dsp_new->starts[(seg)*dsp->dim+j] = dsp->starts[i*dsp->dim+j];
dsp_new->strands[(seg)*dsp->dim+j] = dsp->strands[i*dsp->dim+j];
}
dsp_new->lens[seg] = dsp->lens[i];
seg++;
}
if (diff2 > 0)
{
for (j=0; j<dsp->dim; j++)
{
AlnMgr2GetNthSeqRangeInSA(sap, j+1, &start, &stop);
if (dsp->strands[j] == Seq_strand_minus)
dsp_new->starts[seg*dsp->dim+j] = start-diff2;
else
dsp_new->starts[seg*dsp->dim+j] = stop+1;
dsp_new->strands[seg*dsp->dim+j] = dsp->strands[j];
}
dsp_new->lens[seg] = diff2;
}
dsp_new->ids = dsp->ids;
dsp->ids = NULL;
DenseSegFree(dsp);
sap->segs = (Pointer)dsp_new;
SAIndex2Free2(sap->saip);
sap->saip = NULL;
AlnMgr2IndexSingleChildSeqAlign(sap);
}
/* SECTION 10 */
/***************************************************************************
*
* AlnMgr2PadConservatively extends an alignment so that the whole of
* all sequences is included. If two sequences have tails on the same
* side, they are each aligned with columns of all gaps:
*
* <-new aln region->
* xxxxxxxx----------xxxxxxxxxxxxxxxxxxxx
* --------xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
* This function returns a newly allocated alignment and doesn't change
* the original (except for indexing). If the extension was not done for
* some reason, the function returns NULL;
*
***************************************************************************/
NLM_EXTERN SeqAlignPtr AlnMgr2PadConservatively(SeqAlignPtr sap)
{
AMAlignIndex2Ptr amaip;
BioseqPtr bsp;
Int4 ctr1;
Int4 ctr2;
DenseSegPtr dsp;
DenseSegPtr dsp_new;
Int4 i;
Int4 j;
Int4Ptr lenarray;
Int4 n1;
Int4 n2;
Int4 newseg;
SeqAlignPtr sap_new;
SeqIdPtr sip;
Int4 start;
Int4 stop;
Uint1 strand;
if (sap == NULL || sap->next != NULL)
return NULL;
if (sap->saip == NULL)
AlnMgr2IndexSeqAlign(sap);
if (sap->saip->indextype == INDEX_PARENT)
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
if (amaip->alnstyle == AM2_LITE)
return NULL;
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
} else
dsp = (DenseSegPtr)(sap->segs);
newseg = 0;
lenarray = (Int4Ptr)MemNew(dsp->dim*sizeof(Int4));
n1 = n2 = 0;
for (i=0; i<dsp->dim; i++)
{
sip = AlnMgr2GetNthSeqIdPtr(sap, i+1);
bsp = BioseqLockById(sip);
lenarray[i] = bsp->length;
BioseqUnlock(bsp);
SeqIdFree(sip);
AlnMgr2GetNthSeqRangeInSA(sap, i+1, &start, &stop);
if (start > 0)
{
n1++;
newseg++;
}
if (stop < lenarray[i]-1)
{
newseg++;
}
}
if (newseg == 0)
{
MemFree(lenarray);
return NULL;
}
dsp_new = DenseSegNew();
dsp_new->numseg = dsp->numseg + newseg;
dsp_new->dim = dsp->dim;
dsp_new->ids = SeqIdDupList(dsp->ids);
dsp_new->starts = (Int4Ptr)MemNew(dsp_new->numseg*dsp_new->dim*sizeof(Int4));
dsp_new->lens = (Int4Ptr)MemNew(dsp_new->numseg*sizeof(Int4));
dsp_new->strands = (Uint1Ptr)MemNew(dsp_new->numseg*dsp_new->dim*sizeof(Uint1));
n2 = n1+dsp->numseg;
ctr1 = 0;
ctr2 = 0;
for (i=0; i<dsp->dim; i++)
{
AlnMgr2GetNthSeqRangeInSA(sap, i+1, &start, &stop);
strand = AlnMgr2GetNthStrand(sap, i+1);
if (strand == Seq_strand_minus && lenarray[i]-1-stop > 0)
{
for (j=0; j<ctr1; j++)
{
dsp_new->starts[dsp->dim*j+i] = -1;
dsp_new->strands[dsp->dim*j+i] = strand;
}
dsp_new->starts[dsp->dim*ctr1+i] = stop+1;
dsp_new->lens[ctr1] = lenarray[i]-1-stop;
dsp_new->strands[dsp->dim*ctr1+i] = strand;
for (j=ctr1+1; j<n1; j++)
{
dsp_new->starts[dsp->dim*j+i] = -1;
dsp_new->strands[dsp->dim*j+i] = strand;
}
ctr1++;
} else if (strand == Seq_strand_plus && start > 0)
{
for (j=0; j<ctr1; j++)
{
dsp_new->starts[dsp->dim*j+i] = -1;
dsp_new->strands[dsp->dim*j+i] = strand;
}
dsp_new->starts[dsp->dim*ctr1+i] = 0;
dsp_new->lens[ctr1] = start;
dsp_new->strands[dsp->dim*ctr1+i] = strand;
for (j=ctr1+1; j<n1; j++)
{
dsp_new->starts[dsp->dim*j+i] = -1;
dsp_new->strands[dsp->dim*j+i] = strand;
}
ctr1++;
} else /* nothing to add on this row, just fill in with -1s */
{
for (j=0; j<n1; j++)
{
dsp_new->starts[dsp->dim*j+i] = -1;
dsp_new->strands[dsp->dim*j+i] = strand;
}
}
/* now fill in the non-extended part of the alignment (copy from original) */
for (j=0; j<dsp->numseg; j++)
{
dsp_new->starts[dsp->dim*(j+n1)+i] = dsp->starts[dsp->dim*j+i];
dsp_new->lens[j+n1] = dsp->lens[j];
dsp_new->strands[dsp->dim*(j+n1)+i] = dsp->strands[dsp->dim*j+i];
}
/* now the other ends */
if (strand == Seq_strand_minus && start > 0)
{
for (j=n2; j<n2+ctr2; j++)
{
dsp_new->starts[dsp->dim*j+i] = -1;
dsp_new->strands[dsp->dim*j+i] = strand;
}
dsp_new->starts[dsp->dim*(ctr2+n2)+i] = 0;
dsp_new->lens[ctr2+n2] = start;
dsp_new->strands[dsp->dim*(ctr2+n2)+i] = strand;
for (j=n2+ctr2+1; j<dsp_new->numseg; j++)
{
dsp_new->starts[dsp->dim*j+i] = -1;
dsp_new->strands[dsp->dim*j+i] = strand;
}
ctr2++;
} else if (strand == Seq_strand_plus && lenarray[i]-1-stop > 0)
{
for (j=n2; j<ctr2+n2; j++)
{
dsp_new->starts[dsp->dim*j+i] = -1;
dsp_new->strands[dsp->dim*j+i] = strand;
}
dsp_new->starts[dsp->dim*(ctr2+n2)+i] = stop+1;
dsp_new->lens[ctr2+n2] = lenarray[i]-1-stop;
dsp_new->strands[dsp->dim*(ctr2+n2)+i] = strand;
for (j=ctr2+n2+1; j<dsp_new->numseg; j++)
{
dsp_new->starts[dsp->dim*j+i] = -1;
dsp_new->strands[dsp->dim*j+i] = strand;
}
ctr2++;
} else /* nothing to add on this row, just fill in with -1s */
{
for (j=n2; j<dsp_new->numseg; j++)
{
dsp_new->starts[dsp->dim*j+i] = -1;
dsp_new->strands[dsp->dim*j+i] = strand;
}
}
}
sap_new = SeqAlignNew();
sap_new->dim = dsp->dim;
sap_new->segtype = SAS_DENSEG;
sap_new->segs = (Pointer)(dsp_new);
MemFree(lenarray);
return sap_new;
}
/* SECTION 10 */
/***************************************************************************
*
* AlnMgr2ExtractPairwiseSeqAlign takes an indexed alignment (parent or
* child, but must be fully indexed, not lite) and extracts a pairwise
* subalignment containing the two requested rows. The subalignment is
* unindexed and may have internal unaligned regions.
*
***************************************************************************/
NLM_EXTERN SeqAlignPtr AlnMgr2ExtractPairwiseSeqAlign(SeqAlignPtr sap, Int4 n1, Int4 n2)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp;
DenseSegPtr dsp_new;
Int4 i;
Int4 j;
Int4 n;
SeqAlignPtr sap_new;
if (sap == NULL || sap->saip == NULL || n1 == n2 || n1 <= 0 || n2 <= 0)
return NULL;
if (sap->saip->indextype == INDEX_CHILD)
dsp = (DenseSegPtr)(sap->segs);
else
{
amaip = (AMAlignIndex2Ptr)(sap->saip);
dsp = (DenseSegPtr)(amaip->sharedaln->segs);
}
if (n1 > dsp->dim || n2 > dsp->dim)
return NULL;
n = 0;
for (i=0; i<dsp->numseg; i++)
{
if (dsp->starts[dsp->dim*i+n1-1] == -1 && dsp->starts[dsp->dim*i+n2-1] == -1)
n++;
}
if (n == dsp->numseg) /* no overlap at all */
return NULL;
dsp_new = DenseSegNew();
dsp_new->numseg = dsp->numseg - n;
dsp_new->starts = (Int4Ptr)MemNew(2*dsp_new->numseg*sizeof(Int4));
dsp_new->strands = (Uint1Ptr)MemNew(2*dsp_new->numseg*sizeof(Uint1));
dsp_new->lens = (Int4Ptr)MemNew(dsp_new->numseg*sizeof(Int4));
dsp_new->dim = 2;
dsp_new->ids = AlnMgr2GetNthSeqIdPtr(sap, n1);
dsp_new->ids->next = AlnMgr2GetNthSeqIdPtr(sap, n2);
j = 0;
for (i=0; i<dsp->numseg; i++)
{
if (dsp->starts[dsp->dim*i+n1-1] > -1 || dsp->starts[dsp->dim*i+n2-1] > -1)
{
dsp_new->starts[2*j] = dsp->starts[dsp->dim*i+n1-1];
dsp_new->starts[2*j+1] = dsp->starts[dsp->dim*i+n2-1];
dsp_new->strands[2*j] = dsp->strands[n1-1];
dsp_new->strands[2*j+1] = dsp->strands[n2-1];
dsp_new->lens[j] = dsp->lens[i];
j++;
}
}
sap_new = SeqAlignNew();
sap_new->dim = 2;
sap_new->type = SAT_PARTIAL;
sap_new->segtype = SAS_DENSEG;
sap_new->segs = (Pointer)dsp_new;
return sap_new;
}
/* SECTION 10 */
static void amconssetfree(AMConsSetPtr acp)
{
AMConsSetPtr acp_next;
while (acp != NULL)
{
acp_next = acp->next;
MemFree(acp->starts);
MemFree(acp->stops);
MemFree(acp->strands);
MemFree(acp);
acp = acp_next;
}
}
static int LIBCALLBACK AlnMgr2SortForConsistent(VoidPtr ptr1, VoidPtr ptr2)
{
AMConsSetPtr acp1;
AMConsSetPtr acp2;
SAIndex2Ptr saip1;
SAIndex2Ptr saip2;
acp1 = *((AMConsSetPtr PNTR)ptr1);
acp2 = *((AMConsSetPtr PNTR)ptr2);
saip1 = (SAIndex2Ptr)(acp1->sap->saip);
saip2 = (SAIndex2Ptr)(acp2->sap->saip);
if (saip1->score == 0)
saip1->score = AlnMgr2ComputeScoreForSeqAlign(acp1->sap);
if (saip2->score == 0)
saip2->score = AlnMgr2ComputeScoreForSeqAlign(acp2->sap);
if (saip1->score > saip2->score)
return -1;
else if (saip1->score < saip2->score)
return 1;
else
return 0;
}
/* SECTION 10 */
/***************************************************************************
*
* AlnMgr2RemoveInconsistentAlnsFromSet takes an alignment that is
* indexed at least at the AM2_LITE level, and prunes the child
* alignments so that the remaining alignments form a consistent,
* nonoverlapping set. All alignments must have the same number of rows,
* and they must be the same rows (although not necessarily in the same
* order). The function uses a simple greedy algorithm to construct the
* nonoverlapping set, starting with the highest-scoring alignment.
* If fuzz is negative, the function creates the best nonoverlapping set
* by actually truncating alignments.
*
***************************************************************************/
NLM_EXTERN void AlnMgr2RemoveInconsistentAlnsFromSet(SeqAlignPtr sap_head, Int4 fuzz)
{
AMConsSetPtr acp;
AMConsSetPtr acp_head;
AMConsSetPtr acp_prev;
AMConsSetPtr PNTR acparray;
DenseSegPtr dsp;
Int4 i;
Int4 j;
Int4 k;
Int4 lfuzz;
SeqAlignPtr newsap;
Int4 numrows;
Int4 numsaps;
Int4 orientation;
Int4 row;
SAIndex2Ptr saip;
SeqAlignPtr salp_head;
SeqAlignPtr salp_prev;
SeqAlignPtr sap;
SeqAlignPtr sapnext;
Int4 score;
SeqIdPtr sip;
SeqIdPtr sip_head;
Uint1 strand;
lfuzz = fuzz;
if (fuzz < 0)
fuzz = 1;
sap = (SeqAlignPtr)(sap_head->segs);
if (sap->next == NULL)
return;
dsp = (DenseSegPtr)(sap->segs);
sip_head = dsp->ids;
numrows = AlnMgr2GetNumRows(sap);
acp_head = NULL;
strand = AlnMgr2GetNthStrand(sap, 1);
numsaps = 0;
while (sap != NULL)
{
if (AlnMgr2GetNumRows(sap) != numrows)
{
amconssetfree(acp_head);
return;
}
numsaps++;
acp = (AMConsSetPtr)MemNew(sizeof(AMConsSet));
acp->starts = (Int4Ptr)MemNew(numrows*sizeof(Int4));
acp->stops = (Int4Ptr)MemNew(numrows*sizeof(Int4));
acp->strands = (Uint1Ptr)MemNew(numrows*sizeof(Uint1));
acp->which = (Int4Ptr)MemNew(numrows*sizeof(Int4));
acp->sap = sap;
if (acp_head != NULL)
{
acp_prev->next = acp;
acp_prev = acp;
} else
acp_head = acp_prev = acp;
sip = sip_head;
row = AlnMgr2GetFirstNForSip(sap, sip);
if (row <= 0)
{
amconssetfree(acp_head);
return;
}
if (acp->strands[row] != strand)
{
sapnext = acp->sap->next;
acp->sap->next = NULL;
score = ((SAIndex2Ptr)(acp->sap->saip))->score;
SeqAlignListReverseStrand(acp->sap);
AMAlignIndexFreeEitherIndex(acp->sap);
AlnMgr2IndexSingleChildSeqAlign(acp->sap);
saip = (SAIndex2Ptr)(acp->sap->saip);
saip->score = score;
acp->strands[row] = strand;
acp->sap->next = sapnext;
}
for (i=0; i<numrows; i++)
{
acp->which[i] = row;
AlnMgr2GetNthSeqRangeInSA(sap, i+1, &acp->starts[i], &acp->stops[i]);
acp->strands[i] = AlnMgr2GetNthStrand(sap, i+1);
}
sap = sap->next;
}
acparray = (AMConsSetPtr PNTR)MemNew(numsaps*sizeof(AMConsSetPtr));
acp = acp_head;
i = 0;
while (acp != NULL)
{
acparray[i] = acp;
acp = acp->next;
i++;
}
HeapSort(acparray, numsaps, sizeof(AMConsSetPtr), AlnMgr2SortForConsistent);
/* orientation -1 means that ith is before jth in ALL rows, 1 means ith is after jth in ALL rows */
for (i=0; i<numsaps; i++)
{
if (acparray[i]->used != -1)
{
for (j=i+1; j<numsaps; j++)
{
orientation = 0;
for (k=0; acparray[j]->used != -1 && k<numrows; k++)
{
if (acparray[i]->strands[k] != acparray[j]->strands[k])
acparray[j]->used = -1;
if (acparray[i]->starts[k] - fuzz < acparray[j]->starts[k])
{
if (acparray[i]->stops[k] - fuzz < acparray[j]->starts[k])
{
if ((acparray[i]->strands[k] == Seq_strand_plus && orientation == 1) || (acparray[i]->strands[k] == Seq_strand_minus && orientation == -1))
acparray[j]->used = -1;
else if (orientation == 0)
{
if (acparray[i]->strands[k] == Seq_strand_minus)
orientation = 1;
else
orientation = -1;
}
} else
{
if (lfuzz >= 0) /* just mark it for deletion */
acparray[j]->used = -1;
else /* truncate it */
{
if (acparray[j]->stops[k] > acparray[i]->stops[k])
{
newsap = AlnMgr2GetSubAlign(acparray[j]->sap, acparray[i]->stops[k]+1, acparray[j]->stops[k], k+1, TRUE);
SeqAlignFree(acparray[j]->sap);
acparray[j]->sap = newsap;
acparray[j]->starts[k] = acparray[i]->stops[k]+1;
} else
acparray[j]->used = -1;
}
}
} else if (acparray[i]->starts[k] - fuzz > acparray[j]->starts[k])
{
if (acparray[i]->starts[k] + fuzz > acparray[j]->stops[k])
{
if ((acparray[i]->strands[k] == Seq_strand_plus && orientation == -1) || (acparray[i]->strands[k] == Seq_strand_minus && orientation == 1))
acparray[j]->used = -1;
else if (orientation == 0)
{
if (acparray[i]->strands[k] == Seq_strand_minus)
orientation = -1;
else
orientation = 1;
}
} else
{
if (lfuzz >= 0) /* mark for deletion */
acparray[j]->used = -1;
else /* truncate */
{
if (acparray[j]->starts[k] < acparray[i]->starts[k])
{
newsap = AlnMgr2GetSubAlign(acparray[j]->sap, acparray[j]->starts[k], acparray[i]->starts[k]-1, k+1, TRUE);
SeqAlignFree(acparray[j]->sap);
acparray[j]->sap = newsap;
AlnMgr2IndexSingleChildSeqAlign(newsap);
acparray[j]->starts[k] = acparray[i]->stops[k]+1;
} else
acparray[j]->used = -1;
}
}
} else
acparray[j]->used = -1;
}
}
}
}
/* now free all the unused ones, stick the rest back together, reindex, and return */
salp_head = salp_prev = NULL;
for (i=0; i<numsaps; i++)
{
if (acparray[i]->used == -1)
{
SeqAlignFree(acparray[i]->sap);
acparray[i]->sap = NULL;
} else
{
if (salp_head != NULL)
{
salp_prev->next = acparray[i]->sap;
salp_prev = acparray[i]->sap;
salp_prev->next = NULL;
} else
{
salp_head = salp_prev = acparray[i]->sap;
salp_prev->next = NULL;
}
}
}
amconssetfree(acp_head);
MemFree(acparray);
sap_head->segs = (Pointer)(salp_head);
AMAlignIndex2Free2(sap_head->saip);
AlnMgr2IndexLite(sap_head);
}
static int LIBCALLBACK AlnMgr2CompareByScore(VoidPtr ptr1, VoidPtr ptr2)
{
SAIndex2Ptr saip1;
SAIndex2Ptr saip2;
SeqAlignPtr sap1;
SeqAlignPtr sap2;
if (ptr1 == NULL || ptr2 == NULL)
return 0;
sap1 = *((SeqAlignPtr PNTR) ptr1);
sap2 = *((SeqAlignPtr PNTR) ptr2);
saip1 = (SAIndex2Ptr)(sap1->saip);
saip2 = (SAIndex2Ptr)(sap2->saip);
if (saip1->score == 0)
saip1->score = AlnMgr2ComputeScoreForSeqAlign(sap1);
if (saip2->score == 0)
saip2->score = AlnMgr2ComputeScoreForSeqAlign(sap2);
if (saip1->score > saip2->score)
return -1;
if (saip1->score < saip2->score)
return 1;
return 0;
}
/***************************************************************************
*
* AlnMgr2FuseSet takes a set of alignments sharing all their rows and orders
* the alignments, then fuses together any adjacent alignments. If returnall
* is TRUE, all pieces are returned; if not, then only the largest piece is
* returned. This function will work best when called after
* AlnMgr2RemoveInconsistentAlnsFromSet(sap_head, -1).
*
***************************************************************************/
NLM_EXTERN SeqAlignPtr AlnMgr2FuseSet(SeqAlignPtr sap_head, Boolean returnall)
{
AMAlignIndex2Ptr amaip;
DenseSegPtr dsp_new;
DenseSegPtr dsp1;
DenseSegPtr dsp2;
Boolean found;
Int4 i;
Int4 n;
Int4 numrows;
Int4 r;
SeqAlignPtr sap_keep;
SeqAlignPtr sap_keep_head;
SeqAlignPtr sap_keep_prev;
SAIndex2Ptr saip;
SeqAlignPtr PNTR saparray;
Int4 start1;
Int4 start2;
Int4 stop1;
Int4 stop2;
Uint1 strand;
if (sap_head == NULL || sap_head->saip == NULL)
return NULL;
AlnMgr2SortAlnSetByNthRowPos(sap_head, 1);
amaip = (AMAlignIndex2Ptr)(sap_head->saip);
sap_keep = amaip->saps[0];
sap_keep_head = sap_keep_prev = NULL;
numrows = AlnMgr2GetNumRows(sap_keep);
for (i=1; i<amaip->numsaps; i++)
{
/* check for consistency with sap_keep; fuse if possible */
found = FALSE;
for (n=0; !found && n<numrows; n++)
{
strand = AlnMgr2GetNthStrand(sap_keep, n+1);
AlnMgr2GetNthSeqRangeInSA(sap_keep, n+1, &start1, &stop1);
AlnMgr2GetNthSeqRangeInSA(amaip->saps[i], n+1, &start2, &stop2);
if (strand == Seq_strand_minus)
{
if (stop2+1 != start1)
found = TRUE;
} else
{
if (start2 != stop1+1)
found = TRUE;
}
}
if (!found) /* fuse together */
{
dsp1 = (DenseSegPtr)(sap_keep->segs);
dsp2 = (DenseSegPtr)(amaip->saps[i]->segs);
dsp_new = DenseSegNew();
dsp_new->dim = dsp1->dim;
dsp_new->numseg = dsp1->numseg+dsp2->numseg;
dsp_new->starts = (Int4Ptr)MemNew(dsp_new->numseg*dsp_new->dim*sizeof(Int4));
dsp_new->lens = (Int4Ptr)MemNew(dsp_new->numseg*sizeof(Int4));
dsp_new->strands = (Uint1Ptr)MemNew(dsp_new->numseg*dsp_new->dim*sizeof(Int4));
for (n=0; n<dsp_new->numseg; n++)
{
for (r=0; r<dsp_new->dim; r++)
{
if (n >= dsp1->numseg)
dsp_new->starts[r*n*r] = dsp2->starts[r*(n-dsp1->numseg)+r];
else
dsp_new->starts[r*n+r] = dsp1->starts[r*n+r];
dsp_new->strands[r*n*r] = dsp1->strands[r];
}
if (n >= dsp1->numseg)
dsp_new->lens[n] = dsp2->lens[n-dsp1->numseg];
else
dsp_new->lens[n] = dsp1->lens[n];
}
SeqAlignFree(amaip->saps[i]);
amaip->saps[i] = NULL;
} else /* add next alignment to keepers pile */
{
if (sap_keep_head == NULL)
{
if (sap_keep != NULL)
{
sap_keep_head = sap_keep;
sap_keep->next = amaip->saps[i];
sap_keep_prev = amaip->saps[i];
} else
sap_keep_head = sap_keep_prev = amaip->saps[i];
} else
{
sap_keep_prev->next = amaip->saps[i];
sap_keep_prev = amaip->saps[i];
}
}
}
if (sap_keep_head == NULL || sap_keep_head->next == NULL) /* everything was fused */
sap_keep_head = sap_keep;
if (returnall)
{
sap_head->segs = (Pointer)(sap_keep_head);
return sap_keep_head;
}
i=0;
sap_keep = sap_keep_head;
while (sap_keep != NULL)
{
sap_keep = sap_keep->next;
i++;
}
saparray = (SeqAlignPtr PNTR)MemNew(i*sizeof(SeqAlignPtr));
i = 0;
sap_keep = sap_keep_head;
while (sap_keep != NULL)
{
saip = (SAIndex2Ptr)(sap_keep->saip);
saip->score = 0;
saparray[i] = sap_keep;
i++;
sap_keep = sap_keep->next;
}
HeapSort(saparray, i, sizeof(SeqAlignPtr), AlnMgr2CompareByScore);
sap_keep = saparray[0];
for (n=1; n<i; n++)
{
SeqAlignFree(saparray[n]);
}
MemFree(saparray);
return sap_keep;
}
NLM_EXTERN void AlnMgr2FillInUnaligned(SeqAlignPtr sap)
{
Int4 curr;
DenseSegPtr dsp;
DenseSegPtr dsp_new;
Boolean found;
Int4 i;
Int4 j;
Int4 k;
Int4 last;
Int4 n;
Int4 offset;
Int4 start;
Int4 stop;
Uint1 strand;
if (sap == NULL || (sap->saip != NULL && sap->saip->indextype != INDEX_CHILD))
return;
n = 0;
dsp = (DenseSegPtr)(sap->segs);
for (i=0; i<dsp->dim; i++)
{
j = 0;
AlnMgr2GetNthSeqRangeInSA(sap, i, &start, &stop);
strand = dsp->strands[i];
last = -1;
while (j<dsp->numseg-1)
{
if (strand == Seq_strand_minus)
{
if (last != -1)
{
found = FALSE;
while (j<dsp->numseg && !found)
{
if (dsp->starts[j*dsp->dim+i] != -1)
{
if (dsp->starts[j*dsp->dim+i]+dsp->lens[j] != last)
n++;
found = TRUE;
}
if (!found)
j++;
}
} else
last = dsp->starts[j*dsp->dim+i];
} else
{
if (last != -1)
{
found = FALSE;
while (j<dsp->numseg && !found)
{
if (dsp->starts[j*dsp->dim+i] != -1)
{
if (dsp->starts[j*dsp->dim+i]+dsp->lens[j] != last)
n++;
found = TRUE;
}
if (!found)
j++;
}
} else
{
last = dsp->starts[j*dsp->dim+i];
if (last != -1)
last += dsp->lens[j];
}
}
}
}
if (n == 0) /* no unaligned regions */
return;
dsp_new = DenseSegNew();
dsp_new->numseg = dsp->numseg + n;
dsp_new->dim = dsp->dim;
dsp_new->starts = (Int4Ptr)MemNew(dsp_new->dim*dsp_new->numseg*sizeof(Int4));
dsp_new->strands = (Uint1Ptr)MemNew(dsp_new->dim*dsp_new->numseg*sizeof(Uint1));
for (i=0; i<dsp_new->numseg; i++)
{
for (j=0; j<dsp_new->dim; j++)
{
dsp_new->strands[i*dsp_new->dim+j] = dsp->strands[j];
}
}
dsp_new->ids = SeqIdDupList(dsp->ids);
dsp_new->lens = (Int4Ptr)MemNew(dsp_new->numseg*sizeof(Int4));
curr = 0;
for (j=0; j<dsp->numseg; j++)
{
for (i=0; i<dsp->dim; i++)
{
offset = 0;
strand = dsp->strands[i];
if (dsp->starts[j*dsp->dim+i] == -1)
dsp_new->starts[curr*dsp_new->dim+i] = -1;
else
{
k = j+1;
found = FALSE;
while (k < dsp->numseg)
{
if (dsp->starts[k*dsp->dim+i] != -1)
{
found = TRUE;
if (strand == Seq_strand_minus)
{
if (dsp->starts[k*dsp->dim+i] + dsp->lens[k] != dsp->starts[j*dsp->dim+i])
{
dsp_new->lens[curr+offset] = dsp->starts[j*dsp->dim+i] - dsp->starts[k*dsp->dim+i] - dsp->lens[k];
dsp_new->starts[(curr+offset)*dsp->dim+i] = dsp->starts[k*dsp->dim+i] + dsp->lens[k];
offset++;
}
} else
{
if (dsp->starts[j*dsp->dim+i] + dsp->lens[j] != dsp->starts[k*dsp->dim+i])
{
dsp_new->lens[curr+offset] = dsp->starts[k*dsp->dim+i] - dsp->starts[j*dsp->dim+i] - dsp->lens[j];
dsp_new->starts[(curr+offset)*dsp->dim+i] = dsp->starts[j*dsp->dim+i] + dsp->lens[j];
}
}
}
k++;
}
}
}
curr = curr + 1 + offset;
}
DenseSegFree(dsp);
sap->segs = (Pointer)(dsp_new);
AMAlignIndexFreeEitherIndex(sap);
}
/* SECTION 11 -- functions for std-segs */
NLM_EXTERN SeqIdPtr AlnMgr2GetNthSeqIdPtrStdSeg(SeqAlignPtr sap, Int4 n)
{
SeqLocPtr slp;
StdSegPtr ssp;
if (sap == NULL || sap->segtype != SAS_STD)
return NULL;
ssp = (StdSegPtr)(sap->segs);
slp = ssp->loc;
n--;
while (n > 0)
{
if (slp == NULL)
return NULL;
slp = slp->next;
n--;
}
return (SeqIdDup(SeqLocId(slp)));
}
NLM_EXTERN Int4 AlignMgr2GetFirstNForStdSeg(SeqAlignPtr sap, SeqIdPtr sip)
{
Int4 i;
SeqIdPtr sip_tmp;
StdSegPtr ssp;
if (sap == NULL || sap->segtype != SAS_STD)
return -1;
ssp = (StdSegPtr)(sap->segs);
sip_tmp = ssp->ids;
i = 1;
while (sip_tmp != NULL)
{
if (SeqIdComp(sip, sip_tmp) == SIC_YES)
return i;
sip_tmp = sip_tmp->next;
i++;
}
return -1;
}
NLM_EXTERN void AlnMgr2GetNthSeqRangeInSAStdSeg(SeqAlignPtr sap, Int4 n, Int4Ptr start, Int4Ptr stop)
{
SeqLocPtr slp;
StdSegPtr ssp;
if (start != NULL)
*start = -1;
if (stop != NULL)
*stop = -1;
if (sap == NULL || sap->segtype != SAS_STD)
return;
ssp = (StdSegPtr)(sap->segs);
slp = ssp->loc;
n--;
while (n > 0)
{
if (slp == NULL)
return;
slp = slp->next;
n--;
}
if (slp == NULL)
return;
if (start != NULL)
*start = SeqLocStart(slp);
if (stop != NULL)
*stop = SeqLocStop(slp);
}
/***************************************************************************
*
* AlnMgr2GetSeqRangeForSipInSAStdSeg returns the smallest and largest sequence
* coordinates in in a Std-Seg seqalign for a given Sequence Id. Also return the
* strand type. Either start, stop or strand can be NULL to only retrieve some of them.
* If start and stop are -1, there is an error (not a std-seg), the SeqID does not participate in this
* alignment or the alignment is one big insert on that id. Returns true if the sip was found
* in the alignment with real coordinates, i.e. *start would not be -1. RANGE
*
***************************************************************************/
NLM_EXTERN Boolean AlnMgr2GetSeqRangeForSipInSAStdSeg(SeqAlignPtr sap, SeqIdPtr sip, Int4Ptr start, Int4Ptr stop, Uint1Ptr strand)
{
Int4 c_start, c_stop;
Uint1 c_strand;
StdSegPtr ssp;
Boolean range_found = FALSE;
Boolean strands_inconsistent = FALSE;
if (start) *start = -1;
if (stop) *stop = -1;
if (strand) *strand = Seq_strand_unknown;
if (sap->segtype != SAS_STD)
return FALSE;
ssp = (StdSegPtr)(sap->segs);
while (ssp) {
if (AlnMgr2GetSeqRangeForSipInStdSeg(ssp, sip, &c_start, &c_stop, &c_strand, NULL) &&
c_start != -1) /* skip inserts on our bioseq */
{
range_found = TRUE;
if (start) {
if (*start == -1) {
*start = c_start;
} else {
*start = MIN(*start, c_start);
}
}
if (stop) {
*stop = MAX(*stop, c_stop);
}
if (strand && ! strands_inconsistent) {
/* if strands are different each time, ignore them. */
if (*strand != Seq_strand_unknown && *strand != c_strand) {
*strand = Seq_strand_unknown;
strands_inconsistent = TRUE;
} else {
*strand = c_strand;
}
}
}
ssp = ssp->next;
}
return range_found;
}
/***************************************************************************
*
* AlnMgr2GetSeqRangeForSipInStdSeg returns the start and stop sequence
* coordinates in a Std-Segment for a given Sequence Id. Also return the
* strand type. Either start, stop or strand can be NULL to only retrieve some of them.
* If start and stop are -1, the SeqID was not found in this segment.
* Returns true if the sip was found, even if it is a gap (start, stop = -1). RANGE
*
***************************************************************************/
NLM_EXTERN Boolean AlnMgr2GetSeqRangeForSipInStdSeg(
StdSegPtr ssp,
SeqIdPtr sip,
Int4Ptr start,
Int4Ptr stop,
Uint1Ptr strand,
Uint1Ptr segType) /* AM_SEQ, AM_GAP, AM_INSERT */
{
SeqLocPtr loc;
Uint1 m_strand;
Int4 m_start, m_stop, m_swap;
Boolean s_present = FALSE;
Boolean m_present = FALSE;
Boolean found_id = FALSE;
for ( loc = ssp->loc;
loc != NULL;
loc = loc->next ) {
/* One SeqLoc for each Sequence aligned by this segment. */
/* find the one that matches the sip parameter. */
if (SeqIdForSameBioseq(sip, SeqLocId(loc))) {
m_strand = SeqLocStrand(loc);
m_start = SeqLocStart(loc);
m_stop = SeqLocStop(loc);
/* Might have to reverse the order of start and stop on
minus strands so that start is less than stop. */
if (m_start > m_stop) {
m_swap = m_start;
m_start = m_stop;
m_stop = m_swap;
}
if (start) *start = m_start;
if (stop) *stop = m_stop;
if (strand) *strand = m_strand;
if (m_start != -1)
m_present = TRUE;
/* found our sequence in this segment. */
found_id = TRUE;
} else { /* a different sequence */
if (SeqLocStart(loc) != -1)
s_present = TRUE;
}
}
if (segType) {
if (m_present && s_present)
*segType = AM_SEQ;
else if (!m_present && s_present)
*segType = AM_INSERT;
else if (m_present && !s_present)
*segType = AM_GAP;
else
*segType = AM_GAP; /* start will be -1 */
}
return found_id;
}
/***************************************************************************
*
* AlnMgr2GetNthStdSeg returns the a pointer to the Nth segment of
* a standard segment alignment. Numbering starts with 1.
* returns NULL if not n segments or is not a std-seg aligment.
* Useful to pass its return value to AlnMgr2GetSeqRangeForSipInStdSeg()
*
***************************************************************************/
NLM_EXTERN StdSegPtr AlnMgr2GetNthStdSeg(SeqAlignPtr sap, Int2 n)
{
StdSegPtr ssp;
Int2 i;
if (sap == NULL || sap->segtype != SAS_STD || n < 1)
return NULL;
i = 1;
ssp = (StdSegPtr)(sap->segs);
while(ssp)
{
if (i == n)
return ssp;
++i;
ssp = ssp->next;
}
return NULL;
}
/***************************************************************************
*
* AlnMgr2GetNumStdSegs returns the number of segments in a standar-seg alignment.
* returns -1 if sap is null or not a standard-seg alignment.
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2GetNumStdSegs(SeqAlignPtr sap)
{
Int4 seg_count = 0;
StdSegPtr ssp;
if (sap == NULL || sap->segtype != SAS_STD)
return -1;
ssp = (StdSegPtr)(sap->segs);
while(ssp)
{
++seg_count;
ssp = ssp->next;
}
return seg_count;
}
static SeqLocPtr AlnMgr2GetLongestSeqLoc(SeqAlignPtr sap)
{
Int4 longest;
Int4 n;
SeqLocPtr slp;
SeqLocPtr slp_longest;
StdSegPtr ssp;
if (sap == NULL || sap->segtype != SAS_STD)
return NULL;
longest = -1;
ssp = (StdSegPtr)(sap->segs);
slp = ssp->loc;
while (slp != NULL)
{
n = SeqLocLen(slp);
if (n > longest)
{
slp_longest = slp;
longest = n;
}
slp = slp->next;
}
return slp_longest;
}
/***************************************************************************
*
* The two mapping functions act a little differently for std-segs. The
* alignment coordinates are 1:1 linearly correlated with the longest
* seqloc in the set; the others may be significantly shorter.
* The mapping functions deal with % lengths, and map those instead of
* coordinates (which may not be linear);
*
***************************************************************************/
NLM_EXTERN Int4 AlnMgr2MapBioseqToSeqAlignStdSeg(SeqAlignPtr sap, Int4 n, Int4 pos)
{
SeqLocPtr slp;
SeqLocPtr slp_longest;
StdSegPtr ssp;
Int4 start1;
Int4 start2;
Int4 stop1;
Int4 stop2;
if (sap == NULL || sap->segtype != SAS_STD)
return -1;
slp_longest = AlnMgr2GetLongestSeqLoc(sap);
start1 = SeqLocStart(slp_longest);
stop1 = SeqLocStop(slp_longest);
ssp = (StdSegPtr)(sap->segs);
slp = ssp->loc;
n--;
while (n > 0)
{
if (slp == NULL)
return -1;
n--;
slp = slp->next;
}
if (slp == NULL)
return -1;
start2 = SeqLocStart(slp);
stop2 = SeqLocStop(slp);
if (start2 == -1) /* NULL */
return -1;
return (((stop1-start1)*(pos - start2))/(stop2-start2));
}
NLM_EXTERN Int4 AlnMgr2MapSeqAlignToBioseqStdSeg(SeqAlignPtr sap, Int4 n, Int4 pos)
{
SeqLocPtr slp;
SeqLocPtr slp_longest;
StdSegPtr ssp;
Int4 start1;
Int4 start2;
Int4 stop1;
Int4 stop2;
if (sap == NULL || sap->segtype != SAS_STD)
return -1;
slp_longest = AlnMgr2GetLongestSeqLoc(sap);
start1 = SeqLocStart(slp_longest);
stop1 = SeqLocStop(slp_longest);
ssp = (StdSegPtr)(sap->segs);
slp = ssp->loc;
n--;
while (n > 0)
{
if (slp == NULL)
return -1;
n--;
slp = slp->next;
}
if (slp == NULL)
return -1;
start2 = SeqLocStart(slp);
stop2 = SeqLocStop(slp);
if (start2 == -1) /* NULL */
return -1;
return (start2 + ((stop2-start2)*(pos-start1))/(stop1-start1));
}
NLM_EXTERN Int4 AlnMgr2GetAlnLengthStdSeg(SeqAlignPtr sap)
{
SeqLocPtr slp_longest;
if (sap == NULL || sap->segtype != SAS_STD)
return -1;
slp_longest = AlnMgr2GetLongestSeqLoc(sap);
return (SeqLocLen(slp_longest));
}
|