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
|
/* seqmgr.c
* ===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* 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 have not placed any restriction on its use or reproduction.
*
* 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.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* File Name: seqmgr.c
*
* Author: James Ostell
*
* Version Creation Date: 9/94
*
* $Revision: 6.69 $
*
* File Description: Manager for Bioseqs and BioseqSets
*
* Modifications:
* --------------------------------------------------------------------------
* Date Name Description of modification
* ------- ---------- -----------------------------------------------------
*
* $Log: seqmgr.c,v $
* Revision 6.69 1999/01/14 21:50:04 kans
* added packaging level to descriptor context structure
*
* Revision 6.68 1998/12/09 22:08:49 kans
* fixed a typo that prevented cleanup of index on top bioseq set
*
* Revision 6.67 1998/12/07 16:55:08 kans
* fixed memory leak of extra block on top bioseqset when automatically cleaned up from objmgr
*
* Revision 6.66 1998/11/28 20:08:23 kans
* was not initializing or incrementing nummRNAs and numCDSs variables
*
* Revision 6.65 1998/11/24 22:21:24 kans
* index mRNA and CDS by position, allow arbitrary sorted feature array index
*
* Revision 6.64 1998/10/29 01:07:10 kans
* indexing makes new sep if not on top bsp or bssp
*
* Revision 6.63 1998/10/29 00:50:39 kans
* calls BasicSeqEntryCleanup at beginning of indexing
*
* Revision 6.62 1998/10/27 14:53:30 kans
* get overlapping gene/pub/source takes SeqLocPtr - pass in sfp->location
*
* Revision 6.61 1998/10/22 23:41:49 kans
* feat context has bsp, partial flags, far location flag, GetDesired functions can work on entity entity if using itemID
*
* Revision 6.60 1998/10/22 16:05:55 kans
* removed labeltype parameter from SeqMgrIndexFeatures, changed index parameter/field to Uint2
*
* Revision 6.59 1998/10/20 17:45:58 kans
* distinctive messages for whether far accession was handled or not
*
* Revision 6.58 1998/10/16 21:42:02 kans
* fixed comment on last sort criteria to parent seq-annot
*
* Revision 6.57 1998/10/16 21:36:37 kans
* feature sorting continues with internal intervals, subtype, label, and finally seqannot parent
*
* Revision 6.56 1998/10/13 20:59:48 kans
* SeqMgrGetOmdpForBioseq uses new field in Bioseq to avoid binary search lookup
*
* Revision 6.55 1998/10/09 15:45:13 kans
* more informative errors in SeqMgr indexing
*
* Revision 6.54 1998/09/30 14:49:37 kans
* set scope for FindAppropriateBioseq, FindFirstLocalBioseq, over and above gather scope, which does not apply to the above calls even from within a gather callback
*
* Revision 6.53 1998/09/29 20:06:07 kans
* FindFirstLocalBioseq and GetOffsetInFirstLocalBioseq to deal with far segments more gracefully than just not indexing the feature
*
* Revision 6.52 1998/09/29 15:07:17 kans
* corrected logic for seqDescFilter, seqFeatFilter, and featDefFilter in explore functions
*
* Revision 6.51 1998/09/23 16:41:07 kans
* added SeqMgrGetDesiredDescriptor
*
* Revision 6.50 1998/09/22 18:17:01 kans
* descriptor index flag now tracked properly, separate from itemID
*
* Revision 6.49 1998/09/22 18:01:25 kans
* had been skipping bssp for lastDescrItemID
*
* Revision 6.48 1998/09/22 16:55:51 kans
* added SeqMgrGetDesiredFeature and position index field
*
* Revision 6.47 1998/09/22 13:11:59 kans
* locationFilter parameter to explore features function
*
* Revision 6.46 1998/09/01 19:25:25 kans
* context parameter in get best protein, get cds/rna given product
*
* Revision 6.45 1998/08/24 18:27:09 kans
* removed solaris -v -fd warnings
*
* Revision 6.44 1998/08/21 21:32:36 kans
* populate ivals array (start/stop pairs) for indexed features
*
* Revision 6.43 1998/08/21 20:18:59 kans
* added SeqMgrExploreSegments, indexing features on segmented bioseq
*
* Revision 6.42 1998/08/19 16:26:48 kans
* MakeReversedSeqIdString called from original location of code, also finished support for biosource feature indexing
*
* Revision 6.41 1998/08/18 21:43:54 kans
* SeqIdWithinBioseq finds appropriate SeqID in bsp->id chain for use with SeqLocAinB, allowing multiple IDs on a protein bioseq
*
* Revision 6.40 1998/08/16 22:36:24 kans
* fixed direct map up from part to segmented bioseq
*
* Revision 6.39 1998/08/14 15:40:37 kans
* SeqMgrMapPartToSegmentedBioseq neede LIBCALL, speeded up function by adding map up on part if fetched
*
* Revision 6.38 1998/08/13 22:31:45 kans
* SeqMgrMapPartToSegmentedBioseq to speed up GetOffsetInBioseq, start of indexing segments, also index biosource by location for binary search (Wheelan)
*
* Revision 6.37 1998/08/12 22:16:29 kans
* sort seg-parts array by SeqId, handle seqloc_int and seqloc_whole
*
* Revision 6.36 1998/08/12 21:25:04 kans
* forgot to free allocated partslist array
*
* Revision 6.35 1998/08/12 21:10:37 kans
* added parts index to speed segmented bioseq mapping
*
* Revision 6.34 1998/07/23 17:30:41 kans
* get overlapping gene was nulling out sfp, then dereferencing
*
* Revision 6.33 1998/07/23 13:08:56 kans
* SeqMgrGetOverlappingGene and SeqMgrGetOverlappingPub take optional context pointer
*
* Revision 6.32 1998/07/23 01:15:19 kans
* minor fix to not return ignored feature
*
* Revision 6.31 1998/07/23 01:11:33 kans
* added SeqMgrGetOverlappingPub, gene overlap works even when gene spans circular origin
*
* Revision 6.30 1998/07/16 22:30:55 kans
* improved gene overlap function
*
* Revision 6.29 1998/07/16 16:56:38 kans
* added parent BioseqSetPtr field to SeqMgrBioseqContext, check most recent bioseq first when indexing features
*
* Revision 6.28 1998/07/06 16:15:17 kans
* fixed typo in explore bioseqs callback
*
* Revision 6.27 1998/07/06 15:57:29 kans
* SeqMgrExploreBioseqs takes entityID or ptr
*
* Revision 6.26 1998/07/06 15:30:15 kans
* scope on index explore, added SeqMgrExploreBioseqs
*
* Revision 6.25 1998/07/02 22:30:44 kans
* process product before indexing by location, ErrPostItem if cannot find bioseq for location
*
* Revision 6.24 1998/07/02 17:52:33 kans
* CreateBioseqExtraBlock was not being called for protein bioseq to link back to CDS, which was seen first
*
* Revision 6.23 1998/07/01 19:13:16 kans
* SMFeatBlock.data is allocated array of reasonable size
*
* Revision 6.22 1998/06/30 22:08:02 kans
* SeqMgrFeaturesAreIndexed takes entityID, returns time_t time stamp of latest indexing
*
* Revision 6.21 1998/06/30 14:28:00 kans
* changed GetSeqFeat, which collided with asn2ff4 for some linkers
*
* Revision 6.20 1998/06/30 14:20:16 kans
* changes to heap sort order to put genes first, then rnas, if ranges are equal
*
* Revision 6.19 1998/06/30 12:56:45 kans
* code fixes, public functions moved to explore.h
*
* Revision 6.18 1998/06/29 23:37:37 kans
* added context structure for all explores, index every bioseq in an entity
*
* Revision 6.17 1998/06/29 03:06:41 kans
* fixed two conditionals in ProcessFeatureProducts
*
* Revision 6.16 1998/06/29 02:29:44 kans
* new context get descriptor and feature functions now working, get gene not always working
*
* Revision 6.15 1998/06/29 01:33:27 kans
* added SeqMgrGetNextDescriptor and SeqMgrGetNextFeature
*
* Revision 6.14 1998/06/29 00:24:00 kans
* several changes to new indexing functions
*
* Revision 6.13 1998/06/28 03:44:18 kans
* omdp->parentptr is bssp, not omdp, so use ObjMgrFindByData to get higher descriptors
*
* Revision 6.12 1998/06/28 03:15:09 kans
* missing break statement caused features to be ignored
*
* Revision 6.11 1998/06/28 02:38:15 kans
* simplified filters, finished best gene, explore functions
*
* Revision 6.10 1998/06/27 22:23:49 kans
* improvements and further implementation of new indexing, exploration functions
*
* Revision 6.9 1998/06/27 00:03:45 kans
* fix feature heap sort, post increment feature insertion index, look for best prot
* when setting cds back pointer, and merge descriptor count and feature collect callbacks
*
* Revision 6.8 1998/06/26 22:36:24 kans
* initial work on tracking sorted features, and cds and prot links, for rapid collection
*
* Revision 6.7 1998/05/01 16:13:13 kans
* caching of gi with NULL seqID allowed with protection against calling SeqIdDup
*
* Revision 6.6 1998/04/20 22:38:08 kans
* should prevent caching of gi with NULL seqID
*
* Revision 6.5 1998/04/08 16:52:08 kans
* casts to ValNodeLen calls
*
* Revision 6.4 1998/03/30 21:02:25 ostell
* removed check for parenttype != 0 on call to ObjMgrConnect in SeqMgrLinkSeqEntry
* so that disconnects from sets would work as well as connects
*
* Revision 6.3 1997/11/19 22:14:42 ostell
* added support for multithreaded programs
*
* Revision 6.2 1997/09/25 18:20:14 tatiana
* fixing -1 bug for gaps in CountGapsInDeltaSeq
*
* Revision 6.1 1997/09/11 15:55:40 ostell
* Added support for SetColor messages
*
* Revision 6.0 1997/08/25 18:07:06 madden
* Revision changed to 6.0
*
* Revision 5.20 1997/07/31 16:06:49 kans
* BioseqLockById clears scope if first call to BioseqFindFunc fails, tries again
*
* Revision 5.19 1997/07/30 19:44:46 kans
* bug fix by Serge Bazhin
*
* Revision 5.18 1997/07/28 13:29:41 ostell
* Moved GetUniGeneIDForSeqId() to seqmgr.c
*
* Revision 5.17 1997/07/15 17:37:43 ostell
* fixed problems with duplicate Bioseqs in BioseqFindFunc
*
* Revision 5.16 1997/07/09 21:11:53 ostell
* added support for indexed seqid lookups of bioseqs
*
* Revision 5.15 1997/06/19 18:38:43 vakatov
* [WIN32,MSVC++] Adopted for the "NCBIOBJ.LIB" DLL'ization
*
* Revision 5.14 1997/06/17 17:59:09 kans
* GetSeqIdForGI now uses binary search in cache
*
* Revision 5.13 1997/06/17 16:33:57 kans
* first pass at cache in SeqIdForGi
*
* Revision 5.12 1997/03/26 14:01:37 ostell
* removed OMUserData from new copy of Bioseq when uncaching to stop
* memory leak
*
* Revision 5.11 1997/02/24 21:46:17 ostell
* in BioseqFindFunc, when checking with scope a failure occurs, now it
* checks again without scope.
*
* Revision 5.10 1997/01/23 22:38:21 ostell
* added missing newline at end of file (sigh)
*
* Revision 5.9 1997/01/23 22:37:14 ostell
* minor change to seqmgr.h for new indexing
*
* Revision 5.7 1997/01/08 22:48:50 tatiana
* buf and buflen arguments added to CountGapsInDeltaSeq()
*
* Revision 5.6 1996/08/22 14:50:05 ostell
* initialized static arrays in BioseqFindFunc
*
* Revision 5.5 1996/08/21 13:33:33 ostell
* added cachig to BioseqFindFunc
*
* Revision 5.4 1996/08/06 19:56:03 kans
* for SEQLOC_WHOLE, must call SeqIdFindBest on bsp->id
*
* Revision 5.3 1996/08/05 15:57:26 chappey
* in BioseqReloadFunc, the OMUserDataPtr is passed to the new
* ObjMgrDataPtr, and is not deleted anymore.
*
* Revision 5.2 1996/07/25 02:32:26 ostell
* added CountGapsInDeltaSeq()
*
* Revision 5.1 1996/07/19 22:13:13 ostell
* added SpreadGapsInDeltaSeq()
*
* Revision 5.0 1996/05/28 13:23:23 ostell
* Set to revision 5.0
*
* Revision 4.7 1996/03/19 19:05:17 kans
* SeqEntrySetScope now returns old scope, not new scope
*
* Revision 4.6 1996/01/23 14:44:38 kans
* added Pointer casts to MemSet
*
* Revision 4.5 1995/12/22 14:43:59 ostell
* added reload code to BioseqLockById
* break out relad from cache code to be used as part of gather locking
* with BioseqReload
*
* Revision 4.4 1995/12/09 23:12:41 kans
* SeqEntryFind now can deal with a Seq-Submit ultimate parent
*
* Revision 4.3 1995/12/04 21:40:05 ostell
* added GetSeqIdForGI() and GetGIForSeqId()
*
* Revision 4.2 1995/10/03 15:50:37 ostell
* added support for selection by region.. now fully implemented
*
* Revision 4.1 1995/09/30 03:38:31 ostell
* Changed ObjMgrMessage functions to pass a structure
* Added support for selecting regions
* Added ability to remove entity when no more views on it
*
* Revision 4.0 1995/07/26 13:49:01 ostell
* force revision to 4.0
*
* Revision 1.16 1995/05/15 21:46:05 ostell
* added Log line
*
*
*
*
* ==========================================================================
*/
/** for ErrPostEx() ****/
static char *this_module = "ncbiapi";
#define THIS_MODULE this_module
static char *this_file = __FILE__;
#define THIS_FILE this_file
/**********************/
#include <explore.h> /* new public functions prototyped here */
#include <seqmgr.h> /* the interface */
#include <sequtil.h> /* CLEAN THIS UP LATER? */
#include <gather.h>
#include <subutil.h>
#include <ncbithr.h>
#include <objfdef.h>
#include <sqnutils.h>
/*****************************************************************************
*
* Bioseq Management
*
*****************************************************************************/
static BioseqPtr LIBCALLBACK BSFetchFunc PROTO((SeqIdPtr sid, Uint1 ld_type));
static BioseqPtr NEAR BioseqFindFunc PROTO((SeqIdPtr sid, Boolean reload_from_cache));
static Boolean NEAR SeqMgrGenericSelect PROTO((SeqLocPtr region, Int2 type,
Uint1Ptr rgb));
static BioseqPtr NEAR BioseqReloadFunc PROTO((SeqIdPtr sid, ObjMgrDataPtr omdp));
static Boolean NEAR SeqMgrProcessNonIndexedBioseq PROTO((void));
static Boolean NEAR SeqMgrAddIndexElement PROTO((SeqMgrPtr smp, BioseqPtr bsp, CharPtr buf));
static void NEAR RevStringUpper PROTO((CharPtr str));
static BSFetchTop NEAR SeqMgrGetFetchTop (void);
/*****************************************************************************
*
* Return the current SeqMgr
* SeqMgrGet is obsolete
* SeqMgrReadLock, ReadUnlock, WriteLock, WriteUnlock are thread safe
*
*****************************************************************************/
static TNlmMutex smp_mutex = NULL;
static SeqMgrPtr global_smp = NULL;
static TNlmRWlock smp_RWlock = NULL;
static TNlmRWlock sgi_RWlock = NULL;
/*****************************************************************************
*
* Return the current SeqMgr
* Initialize if not done already
* This function will become obsolete
*
*****************************************************************************/
NLM_EXTERN SeqMgrPtr LIBCALL SeqMgrGet (void)
{
Int4 ret;
SeqMgrPtr smp;
if (global_smp != NULL)
return global_smp;
ret = NlmMutexLockEx(&smp_mutex); /* protect this section */
if (ret) /* error */
{
ErrPostEx(SEV_FATAL,0,0,"SeqMgrGet failed [%ld]", (long)ret);
return NULL;
}
if (global_smp == NULL) /* check again after mutex */
{
/*** have to initialize it **/
smp = (SeqMgrPtr) MemNew (sizeof(SeqMgr));
smp->bsfetch = BSFetchFunc; /* BioseqFetch default */
smp->fetch_on_lock = TRUE; /* fetch when locking */
smp_RWlock = NlmRWinit(); /* initialize RW lock */
sgi_RWlock = NlmRWinit(); /* initialize RW lock */
global_smp = smp; /* do this last for mutex safety */
}
NlmMutexUnlock(smp_mutex);
return global_smp;
}
/*****************************************************************************
*
* SeqMgrReadLock()
* Initialize if not done already
* A thread can have only one read or write lock at a time
* Many threads can have read locks
* Only one thread can have a write lock
* No other threads may have read locks if a write lock is granted
* If another thread holds a write lock, this call blocks until write
* is unlocked.
*
*****************************************************************************/
NLM_EXTERN SeqMgrPtr LIBCALL SeqMgrReadLock (void)
{
SeqMgrPtr smp;
Int4 ret;
smp = SeqMgrGet(); /* ensure initialization */
ret = NlmRWrdlock(smp_RWlock);
if (ret != 0)
{
ErrPostEx(SEV_ERROR,0,0,"SeqMgrReadLock: RWrdlock error [%ld]",
(long)ret);
return NULL;
}
return smp;
}
/*****************************************************************************
*
* SeqMgrWriteLock
* Initialize if not done already
* A thread can have only one read or write lock at a time
* Many threads can have read locks
* Only one thread can have a write lock
* No other threads may have read locks if a write lock is granted
* If another thread holds a read or write lock, this call blocks until write
* is unlocked.
*
*****************************************************************************/
NLM_EXTERN SeqMgrPtr LIBCALL SeqMgrWriteLock (void)
{
SeqMgrPtr smp;
Int4 ret;
smp = SeqMgrGet(); /* ensure initialization */
ret = NlmRWwrlock(smp_RWlock);
if (ret != 0)
{
ErrPostEx(SEV_ERROR,0,0,"SeqMgrWriteLock: RWwrlock error [%ld]",
(long)ret);
return NULL;
}
smp->is_write_locked = TRUE;
return smp;
}
/*****************************************************************************
*
* SeqMgrUnlock()
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrUnlock (void)
{
SeqMgrPtr smp;
Int4 ret;
smp = SeqMgrGet(); /* ensure initialization */
ret = NlmRWunlock(smp_RWlock);
if (ret != 0)
{
ErrPostEx(SEV_ERROR,0,0,"SeqMgrUnlock: RWunlock error [%ld]",
(long)ret);
return FALSE;
}
smp->is_write_locked = FALSE; /* can't be write locked */
return TRUE;
}
/****************************************************************************
*
* RevStringUpper(str)
* Up cases and reverses string
* to get different parts early for SeqId StringCmps
*
*****************************************************************************/
static void NEAR RevStringUpper (CharPtr str)
{
CharPtr nd;
Char tmp;
if (str == NULL)
return;
nd = str;
while (*nd != '\0')
nd++;
nd--;
while (nd > str)
{
tmp = TO_UPPER(*nd);
*nd = TO_UPPER(*str);
*str = tmp;
nd--; str++;
}
if (nd == str)
*nd = TO_UPPER(*nd);
return;
}
static Boolean MakeReversedSeqIdString (SeqIdPtr sid, CharPtr buf, size_t len)
{
Uint1 oldchoice;
CharPtr tmp;
TextSeqIdPtr tsip;
if (sid == NULL || buf == NULL || len < 1) return FALSE;
oldchoice = 0;
switch (sid->choice) {
case SEQID_GI:
sprintf (buf, "%ld", (long)(sid->data.ptrvalue));
break;
case SEQID_EMBL:
case SEQID_DDBJ:
oldchoice = sid->choice;
sid->choice = SEQID_GENBANK;
case SEQID_GENBANK:
case SEQID_PIR:
case SEQID_OTHER:
case SEQID_SWISSPROT:
case SEQID_PRF:
tsip = (TextSeqIdPtr) (sid->data.ptrvalue);
if (tsip->accession != NULL) {
tmp = tsip->name;
tsip->name = NULL;
SeqIdWrite (sid, buf, PRINTID_FASTA_SHORT, len);
tsip->name = tmp;
} else {
SeqIdWrite (sid, buf, PRINTID_FASTA_SHORT, len);
}
if (oldchoice)
sid->choice = oldchoice;
break;
default:
SeqIdWrite (sid, buf, PRINTID_FASTA_SHORT, len);
break;
}
RevStringUpper (buf);
return TRUE;
}
/*****************************************************************************
*
* SeqEntrySetScope(sep)
* scopes global seqentry searches to sep
* setting sep=NULL, opens scope to all seqentries in memory
* returns the current scope
*
*****************************************************************************/
NLM_EXTERN SeqEntryPtr LIBCALL SeqEntrySetScope(SeqEntryPtr sep)
{
SeqEntryPtr curr = NULL;
SeqMgrPtr smp;
Int2 i, j;
SMScopePtr smsp;
TNlmThread thr;
Boolean found;
smp = SeqMgrWriteLock();
if (smp == NULL) goto ret;
thr = NlmThreadSelf();
found = FALSE;
for (i = 0, smsp = smp->scope; i < smp->num_scope; i++, smsp++)
{
if (NlmThreadCompare(thr, smsp->thr))
{
curr = smsp->SEscope;
smsp->SEscope = sep;
if (sep == NULL) /* removing one? */
{
smp->num_scope--;
j = smp->num_scope - i; /* number to move */
if (j) /* not last one */
MemCopy(smsp, (smsp+1), (size_t)(j * sizeof(SMScope)));
}
goto ret; /* all done */
}
}
/* thread not on list */
if (sep == NULL)
goto ret; /* nothing to do */
i = smp->num_scope;
j = smp->total_scope;
if (j == i) /* need more room */
{
j += 20; /* new size */
smsp = smp->scope;
smp->scope = MemNew((size_t)(j * sizeof(SMScope)));
MemCopy(smp->scope, smsp, (size_t)(i * sizeof(SMScope)));
smp->total_scope = j;
MemFree(smsp);
}
smp->scope[i].thr = thr;
smp->scope[i].SEscope = sep;
smp->num_scope++;
ret: SeqMgrUnlock();
return curr;
}
/*****************************************************************************
*
* SeqEntryGetScope(sep)
* returns the current scope or NULL if none set
*
*****************************************************************************/
NLM_EXTERN SeqEntryPtr LIBCALL SeqEntryGetScope(void)
{
SeqMgrPtr smp;
SeqEntryPtr scope = NULL;
Int2 i;
SMScopePtr smsp;
TNlmThread thr;
smp = SeqMgrReadLock();
if (smp == NULL) return FALSE;
thr = NlmThreadSelf();
for (i = 0, smsp = smp->scope; i < smp->num_scope; i++, smsp++)
{
if (NlmThreadCompare(thr, smsp->thr))
{
scope = smsp->SEscope;
break;
}
}
SeqMgrUnlock();
return scope;
}
/*****************************************************************************
*
* BioseqFind(SeqIdPtr)
* Just checks in object loaded memory
* Will also restore a Bioseq that has been cached out
*
*****************************************************************************/
NLM_EXTERN BioseqPtr LIBCALL BioseqFind (SeqIdPtr sid)
{
return BioseqFindFunc(sid, TRUE);
}
/*****************************************************************************
*
* BioseqFindCore(sid)
* Finds a Bioseq in memory based on SeqId when only "core" elements needed
* Will NOT restore a Bioseq that has been cached out by SeqMgr
* This function is for use ONLY by functions that only need the parts
* of the Bioseq left when cached out. This includes the SeqId chain,
* and non-pointer components of the Bioseq.
*
*****************************************************************************/
NLM_EXTERN BioseqPtr LIBCALL BioseqFindCore (SeqIdPtr sip)
{
return BioseqFindFunc(sip, FALSE);
}
/*****************************************************************************
*
* BioseqFindEntity(sid, itemIDptr)
* Finds a Bioseq in memory based on SeqId
* Will NOT restore a Bioseq that has been cached out by SeqMgr
* returns EntityID if found, otherwise 0
* itemIDptr is set to the value for itemID in ObjMgr functions
* itemtype is OBJ_BIOSEQ of course
*
*****************************************************************************/
NLM_EXTERN Uint2 LIBCALL BioseqFindEntity (SeqIdPtr sip, Uint2Ptr itemIDptr)
{
BioseqPtr bsp;
Uint2 entityID = 0;
*itemIDptr = 0;
bsp = BioseqFindCore(sip);
if (bsp == NULL) return entityID; /* not found */
entityID = ObjMgrGetEntityIDForPointer((Pointer)bsp);
if (! entityID)
return entityID;
*itemIDptr = GatherItemIDByData(entityID, OBJ_BIOSEQ, (Pointer)bsp);
return entityID;
}
/********************************************************************************
*
* BioseqReload (omdp, lockit)
* reloads the cached SeqEntry at top of omdp
* if (lockit) locks the record
*
*********************************************************************************/
NLM_EXTERN ObjMgrDataPtr LIBCALL BioseqReload(ObjMgrDataPtr omdp, Boolean lockit)
{
BioseqPtr bsp = NULL;
ObjMgrDataPtr retval = NULL;
Int2 j;
ObjMgrPtr omp;
if (omdp == NULL) return retval;
if (! ((omdp->datatype == OBJ_BIOSEQ) || (omdp->datatype == OBJ_BIOSEQSET)))
return retval;
if (omdp->parentptr != NULL)
{
omp = ObjMgrReadLock();
omdp = ObjMgrFindTop(omp, omdp);
ObjMgrUnlock();
if (omdp == NULL)
return retval;
}
if (omdp->tempload == TL_CACHED) /* only need to reload if cached */
{
bsp = BioseqReloadFunc (NULL, omdp);
if (bsp == NULL)
return retval;
omp = ObjMgrReadLock();
j = ObjMgrLookup(omp, (Pointer)bsp);
omdp = ObjMgrFindTop(omp, omp->datalist[j]);
ObjMgrUnlock();
}
if (lockit)
{
ObjMgrLock(omdp->datatype, omdp->dataptr, TRUE);
}
return omdp;
}
static BSFetchTop NEAR SeqMgrGetFetchTop (void)
{
SeqMgrPtr smp;
BSFetchTop bsftp=NULL;
smp = SeqMgrReadLock();
if (smp == NULL) return bsftp;
bsftp = smp->bsfetch;
SeqMgrUnlock();
return bsftp;
}
static BioseqPtr NEAR BioseqReloadFunc (SeqIdPtr sid, ObjMgrDataPtr omdp)
{
Int2 j;
ObjMgrDataPtr oldomdp;
OMUserDataPtr omudp, next;
ObjMgrProcPtr ompp;
OMProcControl ompc;
BioseqPtr bsp= NULL;
Int2 ret;
ObjMgrPtr omp;
BSFetchTop bsftp=NULL;
ompp = NULL;
omp = ObjMgrReadLock();
for (omudp = omdp->userdata; omudp != NULL; omudp = omudp->next)
{
if (omudp->proctype == OMPROC_FETCH) /* caching function */
{
ompp = ObjMgrProcFind(omp, omudp->procid, NULL, 0);
if (ompp != NULL)
break;
}
}
ObjMgrUnlock();
if (ompp == NULL)
return bsp;
if (ompp->outputtype != OBJ_BIOSEQ)
return bsp;
oldomdp = omdp;
omdp = NULL;
bsftp = SeqMgrGetFetchTop();
if (bsftp != NULL)
{
if (ompp != NULL) /* fetch proc left a signal */
{ /* rerun fetch */
MemSet((Pointer)(&ompc), 0, sizeof(OMProcControl));
ompc.input_data = sid;
ompc.input_entityID = oldomdp->EntityID;
ompc.proc = ompp;
ret = (* (ompp->func))((Pointer)&ompc);
switch (ret)
{
case OM_MSG_RET_ERROR:
ErrShow();
break;
case OM_MSG_RET_DEL:
break;
case OM_MSG_RET_OK:
break;
case OM_MSG_RET_DONE:
omp = ObjMgrWriteLock();
ObjMgrSetTempLoad (omp, ompc.output_data);
ObjMgrUnlock();
bsp = (BioseqPtr)(ompc.output_data);
break;
default:
break;
}
}
if (bsp == NULL) /* nope, try regular fetch */
{
bsp = (*(bsftp))(sid, BSFETCH_TEMP);
}
if (bsp != NULL)
{
omp = ObjMgrReadLock();
j = ObjMgrLookup(omp, (Pointer)bsp);
omdp = ObjMgrFindTop(omp, omp->datalist[j]);
ObjMgrUnlock();
omdp->EntityID = oldomdp->EntityID;
oldomdp->EntityID = 0;
omudp = omdp->userdata;
while (omudp != NULL)
{
next = omudp->next;
if (omudp->freefunc != NULL)
(*(omudp->freefunc))(omudp->userdata.ptrvalue);
MemFree(omudp);
omudp = next;
}
omdp->userdata = oldomdp->userdata;
oldomdp->userdata = NULL;
if (oldomdp->choice != NULL)
SeqEntryFree(oldomdp->choice);
else
{
switch(oldomdp->datatype)
{
case OBJ_BIOSEQ:
BioseqFree((BioseqPtr)(oldomdp->dataptr));
break;
case OBJ_BIOSEQSET:
BioseqSetFree((BioseqSetPtr)(oldomdp->dataptr));
break;
default:
ErrPostEx(SEV_ERROR,0,0,"BioseqFindFunc: delete unknown type [%d]",
(int)(oldomdp->datatype));
break;
}
}
}
}
return bsp;
}
/** static func used internally **/
/*******************************************
*
* WARNING: if you change BIOSEQ_CACHE_NUM, you have to change the
* number of NULL in the initialization of the 2 static pointer arrays
* below
*
*******************************************/
/* nb: this cache is cleared in SeqMgrDeleteFromBioseqIndex() */
#define BIOSEQ_CACHE_NUM 3
static SeqEntryPtr se_cache[BIOSEQ_CACHE_NUM] = {
NULL, NULL, NULL}; /* for a few platforms */
static ObjMgrDataPtr omdp_cache[BIOSEQ_CACHE_NUM] = {
NULL, NULL, NULL}; /* for a few platforms */
static TNlmMutex smp_cache_mutex = NULL;
static BioseqPtr NEAR BioseqFindFunc (SeqIdPtr sid, Boolean reload_from_cache)
{
Int4 i, j, num, imin, imax, ret;
SeqIdIndexElementPtr PNTR sipp;
CharPtr tmp;
Char buf[80];
Boolean do_return;
SeqMgrPtr smp;
ObjMgrPtr omp;
ObjMgrDataPtr omdp;
BioseqPtr bsp = NULL, tbsp;
SeqEntryPtr scope;
if (sid == NULL)
return NULL;
ret = NlmMutexLockEx(&smp_cache_mutex); /* protect this section */
if (ret) /* error */
{
ErrPostEx(SEV_FATAL,0,0,"BioseqFindFunc cache mutex failed [%ld]", (long)ret);
return NULL;
}
do_return = FALSE;
scope = SeqEntryGetScope(); /* first check the cache */
for (i = 0; i < BIOSEQ_CACHE_NUM; i++)
{
if (omdp_cache[i] == NULL)
break;
omdp = omdp_cache[i];
if (omdp->datatype == OBJ_BIOSEQ)
{
if ((scope == NULL) || (scope == se_cache[i]))
{
bsp = (BioseqPtr)(omdp->dataptr);
if (BioseqMatch(bsp, sid))
{
for (j = i; j > 0; j--) /* shift to top of cache */
{
omdp_cache[j] = omdp_cache[j-1];
se_cache[j] = se_cache[j-1];
}
omdp_cache[0] = omdp;
se_cache[0] = scope;
if (! reload_from_cache)
{
do_return = TRUE;
goto done_cache;
}
omp = ObjMgrReadLock();
omdp = ObjMgrFindTop(omp, omdp);
ObjMgrUnlock();
if (omdp->tempload != TL_CACHED)
{
do_return = TRUE;
goto done_cache;
}
bsp = BioseqReloadFunc(sid, omdp);
if (bsp == NULL)
{
ErrPostEx(SEV_ERROR,0,0,"BioseqFindFunc: couldn't uncache");
}
do_return = TRUE;
goto done_cache;
}
}
}
}
done_cache:
NlmMutexUnlock(smp_cache_mutex);
if (do_return) /* all done */
{
return bsp;
}
bsp = NULL; /* resetting it */
SeqMgrProcessNonIndexedBioseq(); /* make sure all are indexed */
/* stringify as in SeqMgrAdd */
MakeReversedSeqIdString (sid, buf, 79); /* common function to make id, call RevStringUpper */
/*
oldchoice = 0;
switch (sid->choice)
{
case SEQID_GI:
sprintf(buf, "%ld", (long)(sid->data.ptrvalue));
break;
case SEQID_EMBL:
case SEQID_DDBJ:
oldchoice = sid->choice;
sid->choice = SEQID_GENBANK;
case SEQID_GENBANK:
case SEQID_PIR:
case SEQID_OTHER:
case SEQID_SWISSPROT:
case SEQID_PRF:
tsip = (TextSeqIdPtr)(sid->data.ptrvalue);
if (tsip->accession != NULL)
{
tmp = tsip->name;
tsip->name = NULL;
SeqIdWrite(sid, buf, PRINTID_FASTA_SHORT, 79);
tsip->name = tmp;
}
else
SeqIdWrite(sid, buf, PRINTID_FASTA_SHORT, 79);
if (oldchoice)
sid->choice = oldchoice;
break;
default:
SeqIdWrite(sid, buf, PRINTID_FASTA_SHORT, 79);
break;
}
RevStringUpper(buf);
*/
imin = 0;
smp = SeqMgrReadLock();
imax = smp->BioseqIndexCnt - 1;
sipp = smp->BioseqIndex;
num = -1;
while (imax >= imin)
{
i = (imax + imin)/2;
tmp = sipp[i]->str;
if ((j = StringCmp(tmp, buf)) > 0)
imax = i - 1;
else if (j < 0)
imin = i + 1;
else
{
num = i;
break;
}
}
if (num < 0) /* couldn't find it */
{
/*
Message(MSG_ERROR, "[1] Couldn't find [%s]", buf);
*/
SeqMgrUnlock();
return NULL;
}
if (scope != NULL) /* check in scope */
{
tbsp = (BioseqPtr)(sipp[num]->omdp->dataptr);
if (ObjMgrIsChild(scope->data.ptrvalue, tbsp))
{
bsp = tbsp;
omdp = sipp[num]->omdp;
}
else
{ /* not in scope, could be duplicate SeqId */
i = num-1;
while ((i >= 0) && (bsp == NULL) && (! StringCmp(sipp[i]->str, buf))) /* back up */
{
tbsp = (BioseqPtr)(sipp[i]->omdp->dataptr);
if (ObjMgrIsChild(scope->data.ptrvalue, tbsp))
{
bsp = tbsp;
omdp = sipp[i]->omdp;
}
i--;
}
i = num + 1;
imax = smp->BioseqIndexCnt - 1;
while ((bsp == NULL) && (i <= imax) && (! StringCmp(sipp[i]->str, buf)))
{
tbsp = (BioseqPtr)(sipp[i]->omdp->dataptr);
if (ObjMgrIsChild(scope->data.ptrvalue, tbsp))
{
bsp = tbsp;
omdp = sipp[i]->omdp;
}
i++;
}
}
}
else /* no scope set */
{
omdp = sipp[num]->omdp;
bsp = (BioseqPtr)(omdp->dataptr);
}
SeqMgrUnlock();
if (bsp == NULL) /* not found */
{
/*
Message(MSG_ERROR, "[2] Couldn't find [%s]", buf);
*/
return bsp;
}
ret = NlmMutexLockEx(&smp_cache_mutex); /* protect this section */
if (ret) /* error */
{
ErrPostEx(SEV_FATAL,0,0,"BioseqFindFunc2 cache mutex failed [%ld]", (long)ret);
return NULL;
}
for (j = (BIOSEQ_CACHE_NUM - 1); j > 0; j--) /* shift to top of cache */
{
omdp_cache[j] = omdp_cache[j-1];
se_cache[j] = se_cache[j-1];
}
omdp_cache[0] = omdp;
se_cache[0] = scope;
NlmMutexUnlock(smp_cache_mutex);
if (! reload_from_cache)
return bsp;
omp = ObjMgrReadLock();
omdp = ObjMgrFindTop(omp, omdp);
ObjMgrUnlock();
if (omdp == NULL)
{
bsp = NULL;
goto ret;
}
if (omdp->tempload == TL_CACHED)
bsp = BioseqReloadFunc(sid, omdp);
ret:
return bsp;
}
/*****************************************************************************
*
* SeqMgrFreeCache()
* frees all cached SeqEntrys
* returns FALSE if any errors occurred
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrFreeCache(void)
{
return ObjMgrFreeCache(OBJ_SEQENTRY);
}
/*****************************************************************************
*
* BioseqLockById(SeqIdPtr)
* Finds the Bioseq and locks it
* Makes sure appropriate BioseqContent is present
*
*****************************************************************************/
NLM_EXTERN BioseqPtr LIBCALL BioseqLockById (SeqIdPtr sid)
{
BioseqPtr bsp = NULL;
SeqMgrPtr smp;
SeqEntryPtr oldscope = NULL;
BSFetchTop bsftp;
Boolean fetch_on_lock;
if (sid == NULL) return bsp;
bsp = BioseqFindFunc(sid, TRUE);
if (bsp == NULL)
{
smp = SeqMgrReadLock();
if (smp == NULL) return bsp;
fetch_on_lock = smp->fetch_on_lock;
bsftp = smp->bsfetch;
SeqMgrUnlock();
if (fetch_on_lock)
{
oldscope = SeqEntrySetScope (NULL);
if (oldscope != NULL) {
bsp = BioseqFindFunc(sid, TRUE);
SeqEntrySetScope (oldscope);
}
if (bsp == NULL && bsftp != NULL)
bsp = (*(bsftp))(sid, BSFETCH_TEMP);
}
}
if (bsp == NULL) return NULL;
ObjMgrLock(OBJ_BIOSEQ, (Pointer)bsp, TRUE);
return bsp;
}
/*****************************************************************************
*
* BioseqUnlockById(SeqIdPtr sip)
* Frees a Bioseq to be dumped from memory if necessary
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL BioseqUnlockById (SeqIdPtr sip)
{
BioseqPtr bsp;
if (sip == NULL) return FALSE;
bsp = BioseqFindFunc(sip, FALSE);
if (bsp == NULL)
return FALSE;
ObjMgrLock(OBJ_BIOSEQ, (Pointer)bsp, FALSE);
return TRUE;
}
/*****************************************************************************
*
* BioseqLock(BioseqPtr)
* Locks a Bioseq
* Any cached data is returned to memory
*
*****************************************************************************/
NLM_EXTERN BioseqPtr LIBCALL BioseqLock (BioseqPtr bsp)
{
if (bsp == NULL) return NULL;
ObjMgrLock(OBJ_BIOSEQ, (Pointer)bsp, TRUE);
return bsp;
}
/*****************************************************************************
*
* BioseqUnlock(BioseqPtr)
* Frees a Bioseq to be dumped from memory if necessary
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL BioseqUnlock (BioseqPtr bsp)
{
if (bsp == NULL) return FALSE;
if (ObjMgrLock(OBJ_BIOSEQ, (Pointer)bsp, FALSE) >= 0)
return TRUE;
else
return FALSE;
}
/*****************************************************************************
*
* BioseqFetch(SeqIdPtr, flag)
* loads bioseq into memory if possible
* first trys LocalLoad
* they trys EntrezLoad
*
*****************************************************************************/
static BioseqPtr LIBCALLBACK BSFetchFunc (SeqIdPtr sid, Uint1 ld_type)
{
BioseqPtr bsp = NULL;
ObjMgrProcPtr ompp;
OMProcControl ompc;
Int2 ret;
ObjMgrPtr omp;
ompp = NULL;
while ((ompp = ObjMgrProcFindNext(NULL, OMPROC_FETCH, OBJ_SEQID, OBJ_BIOSEQ, ompp)) != NULL)
{
MemSet((Pointer)(&ompc), 0, sizeof(OMProcControl));
ompc.input_data = sid;
ompc.proc = ompp;
ret = (* (ompp->func))((Pointer)&ompc);
switch (ret)
{
case OM_MSG_RET_ERROR:
ErrShow();
break;
case OM_MSG_RET_DEL:
break;
case OM_MSG_RET_OK:
break;
case OM_MSG_RET_DONE:
if (ld_type == BSFETCH_TEMP)
{
omp = ObjMgrWriteLock();
ObjMgrSetTempLoad (omp, ompc.output_data);
ObjMgrUnlock();
}
bsp = (BioseqPtr)(ompc.output_data);
break;
default:
break;
}
if (bsp != NULL) /* got one */
break;
}
return bsp;
}
NLM_EXTERN BioseqPtr LIBCALL BioseqFetch (SeqIdPtr sid, Uint1 ld_type)
{
BSFetchTop bsftp;
BioseqPtr bsp;
bsp = BioseqFindFunc(sid, TRUE);
if (bsp != NULL) return bsp;
bsftp = SeqMgrGetFetchTop();
if (bsftp == NULL) return NULL;
return (*(bsftp))(sid, ld_type);
}
/*****************************************************************************
*
* GetGIForSeqId(SeqIdPtr)
* returns the GI for a SeqId
* returns 0 if can't find it
*
*****************************************************************************/
NLM_EXTERN Int4 LIBCALL GetGIForSeqId (SeqIdPtr sid)
{
BioseqPtr bsp = NULL;
ObjMgrProcPtr ompp;
OMProcControl ompc;
Int2 ret;
SeqIdPtr sip;
Int4 gi=0;
if (sid == NULL)
return gi;
if (sid->choice == SEQID_GI)
return sid->data.intvalue;
bsp = BioseqFindCore(sid);
if (bsp != NULL)
{
for (sip = bsp->id; sip != NULL; sip = sip->next)
{
if (sip->choice == SEQID_GI)
return sip->data.intvalue;
}
}
ompp = NULL;
while ((ompp = ObjMgrProcFindNext(NULL, OMPROC_FETCH, OBJ_SEQID, OBJ_SEQID, ompp)) != NULL)
{
if ((ompp->subinputtype == 0) && (ompp->suboutputtype == SEQID_GI))
{
MemSet((Pointer)(&ompc), 0, sizeof(OMProcControl));
ompc.input_data = sid;
ompc.proc = ompp;
ret = (* (ompp->func))((Pointer)&ompc);
switch (ret)
{
case OM_MSG_RET_ERROR:
ErrShow();
break;
case OM_MSG_RET_DEL:
break;
case OM_MSG_RET_OK:
break;
case OM_MSG_RET_DONE:
sip = (SeqIdPtr)(ompc.output_data);
if (sip != NULL)
{
if (sip->choice == SEQID_GI)
{
gi = sip->data.intvalue;
SeqIdFree(sip);
return gi;
}
SeqIdFree(sip);
}
break;
default:
break;
}
}
}
return gi;
}
/*****************************************************************************
*
* GetSeqIdForGI(Int4)
* returns the SeqId for a GI
* returns NULL if can't find it
* The returned SeqId is allocated. Caller must free it.
*
*****************************************************************************/
typedef struct seqidblock {
Int4 uid;
SeqIdPtr sip;
} SeqIdBlock, PNTR SeqIdBlockPtr;
static ValNodePtr seqidgicache = NULL;
static ValNodePtr PNTR seqidgiarray = NULL;
static Int2 seqidcount = 0;
static TNlmRWlock sid_RWlock = NULL;
static void RecordInSeqIdGiCache (Int4 gi, SeqIdPtr sip)
{
ValNodePtr vnp, tmp;
ValNodePtr PNTR prev;
SeqIdBlockPtr sibp;
Int4 retval;
/* if (sip == NULL) return; okay to cache NULL because we protect against SeqIdDup */
retval = NlmRWwrlock(sgi_RWlock);
if (retval != 0)
{
ErrPostEx(SEV_ERROR,0,0,"RecSeqIdGi: RWwrlock error [%ld]",
(long)retval);
return;
}
vnp = ValNodeNew (NULL);
if (vnp == NULL) goto ret;
sibp = (SeqIdBlockPtr) MemNew (sizeof (SeqIdBlock));
if (sibp == NULL) {
MemFree (vnp);
goto ret;
}
sibp->uid = gi;
if (sip != NULL) {
sibp->sip = SeqIdDup (sip);
}
vnp->data.ptrvalue = (Pointer) sibp;
if (seqidgicache == NULL) {
seqidgicache = vnp;
goto ret;
}
seqidgiarray = MemFree (seqidgiarray);
prev = (ValNodePtr PNTR) (&seqidgicache);
tmp = seqidgicache;
while (tmp != NULL) {
sibp = (SeqIdBlockPtr) tmp->data.ptrvalue;
if (sibp != NULL) {
if (sibp->uid > gi) {
if (prev != NULL) {
vnp->next = *prev;
*prev = vnp;
}
goto ret;
} else if (sibp->uid == gi) {
goto ret;
} else {
prev = (ValNodePtr PNTR) (& (tmp->next));
}
} else {
prev = (ValNodePtr PNTR) (& (tmp->next));
}
tmp = tmp->next;
}
if (prev != NULL) {
vnp->next = *prev;
*prev = vnp;
}
ret:
retval = NlmRWunlock(sgi_RWlock);
if (retval != 0)
{
ErrPostEx(SEV_ERROR,0,0,"RecSeqIdGiUnlock: RWunlock error [%ld]",
(long)retval);
}
return;
}
static Boolean FetchFromSeqIdGiCache (Int4 gi, SeqIdPtr PNTR sipp)
{
ValNodePtr vnp;
SeqIdBlockPtr sibp = NULL;
Int2 i;
Int2 left, right, mid;
Int4 compare, ret;
Boolean done = FALSE;
if (sipp == NULL) return done;
*sipp = NULL;
if (seqidgicache == NULL) return done;
if (seqidgiarray == NULL) {
ret = NlmRWwrlock(sgi_RWlock);
if (ret != 0)
{
ErrPostEx(SEV_ERROR,0,0,"SeqIdGi: RWwrlock error [%ld]",
(long)ret);
return done;
}
if (seqidgiarray == NULL)
{
seqidcount = (Int2) ValNodeLen (seqidgicache);
seqidgiarray = MemNew (sizeof (ValNodePtr) * (size_t) (seqidcount + 1));
if (seqidgiarray != NULL) {
i = 0;
for (vnp = seqidgicache; vnp != NULL; vnp = vnp->next) {
seqidgiarray [i] = vnp;
i++;
}
}
}
ret = NlmRWunlock(sgi_RWlock);
if (ret != 0)
{
ErrPostEx(SEV_ERROR,0,0,"SeqIdGiUnlock: RWunlock error [%ld]",
(long)ret);
return done;
}
}
ret = NlmRWrdlock(sgi_RWlock);
if (ret != 0)
{
ErrPostEx(SEV_ERROR,0,0,"SeqIdGi: RWrdlock error [%ld]",
(long)ret);
return done;
}
if (seqidgiarray != NULL) {
left = 1;
right = seqidcount;
while (left <= right) {
mid = (left + right) / 2;
compare = 0;
vnp = seqidgiarray [mid - 1];
if (vnp != NULL) {
sibp = (SeqIdBlockPtr) vnp->data.ptrvalue;
if (sibp != NULL) {
compare = gi - sibp->uid;
}
}
if (compare <= 0) {
right = mid - 1;
}
if (compare >= 0) {
left = mid + 1;
}
}
if (left > right + 1 && sibp != NULL) {
if (sibp->sip != NULL) {
*sipp = SeqIdDup (sibp->sip);
}
done = TRUE;
}
}
ret = NlmRWunlock(sgi_RWlock);
if (ret != 0)
{
ErrPostEx(SEV_ERROR,0,0,"SeqIdGiUnlock: RWunlock error [%ld]",
(long)ret);
}
return done;
}
NLM_EXTERN SeqIdPtr LIBCALL GetSeqIdForGI (Int4 gi)
{
BioseqPtr bsp = NULL;
ObjMgrProcPtr ompp;
OMProcControl ompc;
Int2 ret;
SeqIdPtr sip, sip2=NULL, other=NULL, gb=NULL;
ValNode vn;
if (gi <= 0)
return sip2;
vn.choice = SEQID_GI;
vn.data.intvalue = gi;
vn.next = NULL;
bsp = BioseqFindCore(&vn);
if (bsp != NULL)
{
for (sip = bsp->id; sip != NULL; sip = sip->next)
{
switch (sip->choice)
{
case SEQID_LOCAL: /* object id */
case SEQID_GIBBSQ:
case SEQID_GIBBMT:
case SEQID_PATENT:
case SEQID_GENERAL:
other = sip;
break;
case SEQID_GI:
break;
case SEQID_GENBANK:
case SEQID_EMBL:
case SEQID_PIR:
case SEQID_SWISSPROT:
case SEQID_DDBJ:
case SEQID_PRF:
case SEQID_PDB:
gb = sip;
break;
default:
if (other == NULL)
other = sip;
break;
}
}
}
if (gb != NULL)
sip2 = gb;
else if (other != NULL)
sip2 = other;
if (sip2 != NULL)
return SeqIdDup(sip2);
if (FetchFromSeqIdGiCache (gi, &sip2)) {
return sip2;
}
ompp = NULL;
while ((ompp = ObjMgrProcFindNext(NULL, OMPROC_FETCH, OBJ_SEQID, OBJ_SEQID, ompp)) != NULL)
{
if ((ompp->subinputtype == SEQID_GI) && (ompp->suboutputtype == 0))
{
MemSet((Pointer)(&ompc), 0, sizeof(OMProcControl));
ompc.input_data = &vn;
ompc.proc = ompp;
ret = (* (ompp->func))((Pointer)&ompc);
switch (ret)
{
case OM_MSG_RET_ERROR:
ErrShow();
break;
case OM_MSG_RET_DEL:
break;
case OM_MSG_RET_OK:
break;
case OM_MSG_RET_DONE:
sip2 = (SeqIdPtr)(ompc.output_data);
if (sip2 != NULL)
{
RecordInSeqIdGiCache (gi, sip2);
return sip2;
}
break;
default:
break;
}
}
}
RecordInSeqIdGiCache (gi, sip2);
return sip2;
}
/*****************************************************************************
*
* SeqEntryFind(sip)
* returns top level seqentry for sip
*
*****************************************************************************/
NLM_EXTERN SeqEntryPtr LIBCALL SeqEntryFind (SeqIdPtr sid)
{
BioseqPtr bsp;
ObjMgrDataPtr omdp;
ObjMgrDataPtr PNTR omdpp;
SeqEntryPtr result=NULL;
SeqSubmitPtr ssp;
Int2 i;
ObjMgrPtr omp;
bsp = BioseqFind(sid);
if (bsp == NULL) return result;
omp = ObjMgrReadLock();
omdpp = omp->datalist;
i = ObjMgrLookup(omp, (Pointer)bsp);
omdp = omdpp[i];
while (omdp->parentptr != NULL)
{
i = ObjMgrLookup(omp, (omdp->parentptr));
omdp = omdpp[i];
}
if (omdp->datatype == OBJ_SEQSUB) {
ssp = (SeqSubmitPtr) omdp->dataptr;
if (ssp != NULL && ssp->datatype == 1) {
result = (SeqEntryPtr) ssp->data;
}
} else {
result = omdp->choice;
}
ObjMgrUnlock();
return result;
}
/*****************************************************************************
*
* BioseqContextPtr BioseqContextNew (bsp)
*
*****************************************************************************/
NLM_EXTERN BioseqContextPtr LIBCALL BioseqContextNew (BioseqPtr bsp)
{
ObjMgrDataPtr omdp;
ObjMgrDataPtr PNTR omdpp;
Int2 i, ctr=0;
SeqEntryPtr seps[BIOSEQCONTEXTMAX];
BioseqContextPtr bcp;
ObjMgrPtr omp;
if (bsp == NULL)
return NULL;
bcp = MemNew(sizeof(BioseqContext));
bcp->bsp = bsp;
bcp->se.choice = 1; /* bioseq */
bcp->se.data.ptrvalue = bsp;
omp = ObjMgrReadLock();
if (omp == NULL) return BioseqContextFree(bcp);
omdpp = omp->datalist;
i = ObjMgrLookup(omp, (Pointer)bsp);
omdp = omdpp[i];
if (omdp->choice != NULL)
{
seps[ctr] = omdp->choice;
ctr++;
while (omdp->parentptr != NULL)
{
i = ObjMgrLookup(omp, (omdp->parentptr));
omdp = omdpp[i];
if (omdp->choice != NULL)
{
if (ctr == BIOSEQCONTEXTMAX)
ErrPostEx(SEV_ERROR, 0,0, "BioseqContextNew: more than %d levels",(int)ctr);
else
{
seps[ctr] = omdp->choice;
ctr++;
}
}
}
bcp->count = ctr;
for (i = 0; i < bcp->count; i++)
{
ctr--;
bcp->context[i] = seps[ctr];
}
}
if (omdp->tempload == TL_CACHED)
{
ErrPostEx(SEV_ERROR,0,0,"BioseqContextNew: bsp is TL_CACHED");
bcp = BioseqContextFree(bcp);
}
ObjMgrUnlock();
return bcp;
}
/*****************************************************************************
*
* BioseqContextFree(bcp)
*
*****************************************************************************/
NLM_EXTERN BioseqContextPtr LIBCALL BioseqContextFree(BioseqContextPtr bcp)
{
return MemFree(bcp);
}
/*****************************************************************************
*
* BioseqContextGetSeqDescr(bcp, type, curr, SeqEntryPtr PNTR sep)
* returns pointer to the next SeqDescr of this type
* type gives type of Seq-descr
* if (type == 0)
* get them all
* curr is NULL or previous node of this type found
* moves up from bsp
* if (sep != NULL) sep set to SeqEntryPtr containing the SeqDescr.
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL BioseqContextGetSeqDescr (BioseqContextPtr bcp, Int2 type, ValNodePtr curr, SeqEntryPtr PNTR the_sep) /* the last one you used */
{
Int2 i;
ValNodePtr tmp;
Boolean found = FALSE;
BioseqPtr bsp;
BioseqSetPtr bssp;
if (bcp == NULL) return NULL;
if (the_sep != NULL)
*the_sep = NULL;
if (bcp->count == 0) /* just a Bioseq */
{
tmp = BioseqGetSeqDescr(bcp->bsp, type, curr);
if (the_sep != NULL) *the_sep = bcp->context[1];
return tmp;
}
i = bcp->count - 1;
if (curr != NULL) /* find where we are */
{
while ((i >= 0) && (! found))
{
if (IS_Bioseq(bcp->context[i]))
{
bsp = (BioseqPtr)((bcp->context[i])->data.ptrvalue);
tmp = bsp->descr;
}
else
{
bssp = (BioseqSetPtr)((bcp->context[i])->data.ptrvalue);
tmp = bssp->descr;
}
while ((tmp != curr) && (tmp != NULL))
tmp = tmp->next;
if (tmp == curr)
{
found = TRUE;
tmp = tmp->next;
}
else
i--;
}
if (! found) /* can't find it! */
return NULL;
}
else /* get first one */
{
tmp = bcp->bsp->descr;
}
while (i >= 0)
{
while (tmp != NULL)
{
if ((! type) || ((Int2)(tmp->choice) == type))
{
if (the_sep != NULL) *the_sep = bcp->context[i];
return tmp;
}
tmp = tmp->next;
}
i--;
if (i >= 0)
{
if (IS_Bioseq(bcp->context[i]))
{
bsp = (BioseqPtr)((bcp->context[i])->data.ptrvalue);
tmp = bsp->descr;
}
else
{
bssp = (BioseqSetPtr)((bcp->context[i])->data.ptrvalue);
tmp = bssp->descr;
}
}
}
return NULL;
}
/*****************************************************************************
*
* BioseqContextGetSeqFeat(bcp, type, curr, sapp)
* returns pointer to the next Seq-feat of this type
* type gives type of Seq-descr
* if (type == 0)
* get them all
* curr is NULL or previous node of this type found
* moves up from bsp
* if (sapp != NULL) is filled with SeqAnnotPtr containing the SeqFeat
* in:
* 0 = sfp->location only
* 1 = sfp->product only
* 2 = either of above
*
*****************************************************************************/
NLM_EXTERN SeqFeatPtr LIBCALL BioseqContextGetSeqFeat (BioseqContextPtr bcp, Int2 type,
SeqFeatPtr curr, SeqAnnotPtr PNTR sapp, Int2 in) /* the last one you used */
{
SeqEntryPtr sep;
if (bcp == NULL) return NULL;
if (sapp != NULL)
*sapp = NULL;
if (bcp->count == 0) /* just a BioseqSeq */
sep = &(bcp->se);
else
sep = bcp->context[0];
return SeqEntryGetSeqFeat (sep, type, curr, sapp, in, bcp->bsp);
}
typedef struct smgetseqfeat {
Boolean hit;
SeqFeatPtr last,
this;
SeqAnnotPtr sap;
SeqLocPtr slp1, slp2;
Int2 in, type;
} SMGetSeqFeat, PNTR GetSeqFeatPtr;
NLM_EXTERN void GetSeqFeatCallback (SeqEntryPtr sep, Pointer data, Int4 index, Int2 indent);
/*****************************************************************************
*
* SeqEntryGetSeqFeat(sep, type, curr, sapp)
* returns pointer to the next Seq-feat of this type
* type gives type of SeqFeat
* if (type == 0)
* get them all
* curr is NULL or previous node of this type found
* moves up from bsp
* if (sapp != NULL) is filled with SeqAnnotPtr containing the SeqFeat
* if (bsp != NULL) then for that Bioseq match on location by "in"
* in:
* 0 = sfp->location only
* 1 = sfp->product only
* 2 = either of above
*
*****************************************************************************/
NLM_EXTERN SeqFeatPtr LIBCALL SeqEntryGetSeqFeat (SeqEntryPtr sep, Int2 type,
SeqFeatPtr curr, SeqAnnotPtr PNTR sapp, Int2 in, BioseqPtr bsp) /* the last one you used */
{
SMGetSeqFeat gsf;
ValNode vn1, vn2;
if (sep == NULL) return NULL;
if (sapp != NULL)
*sapp = NULL;
if (curr == NULL)
gsf.hit = TRUE;
else
gsf.hit = FALSE;
gsf.last = curr;
gsf.this = NULL;
gsf.sap = NULL;
gsf.type = type;
gsf.in = in;
if (bsp != NULL) /* matching by Bioseq */
{
if ((bsp->repr == Seq_repr_seg) || (bsp->repr == Seq_repr_ref))
{
vn2.choice = SEQLOC_MIX;
vn2.data.ptrvalue = bsp->seq_ext;
gsf.slp2 = (SeqLocPtr)(&vn2);
}
else
gsf.slp2 = NULL;
vn1.choice = SEQLOC_WHOLE;
vn1.data.ptrvalue = (Pointer) SeqIdFindBest (bsp->id, 0);
gsf.slp1 = (SeqLocPtr)(&vn1);
}
else
gsf.slp1 = NULL;
SeqEntryExplore (sep, (Pointer)(&gsf), GetSeqFeatCallback);
if (sapp != NULL)
*sapp = gsf.sap;
return gsf.this;
}
NLM_EXTERN void GetSeqFeatCallback (SeqEntryPtr sep, Pointer data, Int4 index, Int2 indent)
{
GetSeqFeatPtr gsfp;
BioseqPtr bsp;
BioseqSetPtr bssp;
SeqAnnotPtr sap;
SeqFeatPtr sfp, last;
Boolean hit, gotit = FALSE;
Uint1 type;
SeqLocPtr slp1, slp2, slp;
Int2 i, in, retval;
gsfp = (GetSeqFeatPtr)data;
if (gsfp->this != NULL) /* got it */
return;
last = gsfp->last;
hit = gsfp->hit;
type = (Uint1)(gsfp->type);
if (gsfp->slp1 != NULL) /* matching by Bioseq */
{
slp1 = gsfp->slp1;
slp2 = gsfp->slp2;
in = gsfp->in;
}
else
slp1 = NULL;
if (IS_Bioseq(sep))
{
bsp = (BioseqPtr)(sep->data.ptrvalue);
sap = bsp->annot;
}
else
{
bssp = (BioseqSetPtr)(sep->data.ptrvalue);
sap = bssp->annot;
}
while (sap != NULL)
{
if (sap->type == 1) /* feature table */
{
for (sfp = (SeqFeatPtr)(sap->data); sfp != NULL; sfp = sfp->next)
{
if (! hit) /* still looking */
{
if (sfp == last)
{
hit = TRUE;
gsfp->hit = TRUE;
}
}
else
{
if ((! type) || (type == sfp->data.choice))
{
if (slp1 != NULL) /* look for feats on a bioseq */
{
for (i = 0; i < 2; i++)
{
if ((i == 0) && (in != 1))
slp = sfp->location;
else if ((i==1) && (in != 0))
slp = sfp->product;
else
slp = NULL;
if (slp != NULL)
{
retval = SeqLocCompare(slp, slp1);
if (retval)
{
gotit = TRUE;
break;
}
if (slp2 != NULL)
{
retval = SeqLocCompare(slp, slp2);
if (retval)
{
gotit = TRUE;
break;
}
}
}
}
}
else
gotit = TRUE;
if (gotit)
{
gsfp->this = sfp;
gsfp->sap = sap;
return;
}
}
}
}
}
sap = sap->next;
}
return;
}
/*****************************************************************************
*
* BioseqContextGetTitle(bcp)
* returns first title for Bioseq in this context
*
*****************************************************************************/
NLM_EXTERN CharPtr LIBCALL BioseqContextGetTitle(BioseqContextPtr bcp)
{
CharPtr title = NULL;
ValNodePtr vnp;
vnp = BioseqContextGetSeqDescr(bcp, Seq_descr_title, NULL, NULL);
if (vnp != NULL)
title = (CharPtr)vnp->data.ptrvalue;
return title;
}
/*****************************************************************************
*
* SeqMgr Functions
*
*****************************************************************************/
/*****************************************************************************
*
* SeqMgrSeqEntry(type, data, sep)
* Adds the SeqEntryPtr pointing directly to this Bioseq or BioseqSet
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrSeqEntry (Uint1 type, Pointer data, SeqEntryPtr sep)
{
return ObjMgrSetChoice (OBJ_SEQENTRY, sep, data);
}
/*****************************************************************************
*
* SeqMgrGetSeqEntryForData(data)
* returns SeqEntryPtr for a BioseqPtr or BioseqSetPtr
* sep must have been put in SeqMgr using SeqMgrSeqEntry
* the Bioseq/BioseqSets it is a part of must also be in SeqMgr
* returns NULL on failure.
*
*****************************************************************************/
NLM_EXTERN SeqEntryPtr LIBCALL SeqMgrGetSeqEntryForData (Pointer data)
{
return ObjMgrGetChoiceForData(data);
}
/*****************************************************************************
*
* SeqMgrGetEntityIDForSeqEntry(sep)
* returns the EntityID for a SeqEntryPtr
* sep must have been put in SeqMgr using SeqMgrSeqEntry
* the Bioseq/BioseqSets it is a part of must also be in SeqMgr
* This function will move up to the top of the SeqEntry tree it may be
* in. If top level EntityID is 0, one is assigned at this point.
* If an element is moved under a different hierarchy, its EntityID will
* change.
* returns 0 on failure.
*
*****************************************************************************/
NLM_EXTERN Int2 LIBCALL SeqMgrGetEntityIDForSeqEntry (SeqEntryPtr sep)
{
return ObjMgrGetEntityIDForChoice (sep);
}
/*****************************************************************************
*
* SeqMgrGetSeqEntryForEntityID (id)
*
*****************************************************************************/
NLM_EXTERN SeqEntryPtr LIBCALL SeqMgrGetSeqEntryForEntityID (Int2 id)
{
return ObjMgrGetChoiceForEntityID (id);
}
/*****************************************************************************
*
* SeqMgrSetBSFetchTop (fetch, data)
* sets the BSFetchTop routine to "fetch"
* returns previous value
* set to NULL to turn off all fetching for that type
*
* Current value can be called directly as BioseqFetch();
* Default is
* 1) looks in memory
* 2) looks locally if LocalBSFetch is set
* 3) looks remotely if RemoteBSFetch is set
*
*****************************************************************************/
NLM_EXTERN BSFetchTop LIBCALL SeqMgrSetBSFetchTop (BSFetchTop fetch, Pointer data)
{
SeqMgrPtr smp;
BSFetchTop tmp = NULL;
smp = SeqMgrWriteLock();
if (smp == NULL) return tmp;
tmp = smp->bsfetch;
smp->bsfetch = fetch;
smp->TopData = data;
SeqMgrUnlock();
return tmp;
}
/*****************************************************************************
*
* SeqMgrSetFetchOnLock(value)
* if value = TRUE, manager will try to fetch the bioseq if not in
* memory, when BioseqLock is called
* if FALSE, BioseqLock will only look in memory
* returns previous value of fetch_on_lock
* default is to fetch_on_lock
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrSetFetchOnLock (Boolean value)
{
Boolean tmp=FALSE;
SeqMgrPtr smp;
smp = SeqMgrWriteLock();
if (smp == NULL) return tmp;
tmp = smp->fetch_on_lock;
smp->fetch_on_lock = value;
SeqMgrUnlock();
return tmp;
}
/*****************************************************************************
*
* SeqMgrLinkSeqEntry(sep, parenttype, parentptr)
* connects all component seq-entries by traversing the linked list
* all calling SeqMgrConnect and SeqMgrSeqEntry appropriately
* if parenttype != 0, then assumes seqentry is contained in parentptr
* and should be connected to it
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrLinkSeqEntry (SeqEntryPtr sep, Uint2 parenttype, Pointer parentptr)
{
SeqEntryPtr sep2;
BioseqSetPtr bssp;
Uint2 the_type;
if (sep == NULL)
return FALSE;
if (IS_Bioseq(sep))
the_type = OBJ_BIOSEQ;
else
the_type = OBJ_BIOSEQSET;
SeqMgrSeqEntry((Uint1)the_type, sep->data.ptrvalue, sep);
/**** if (parenttype != 0) ****/
ObjMgrConnect(the_type, sep->data.ptrvalue, parenttype, parentptr);
if (! IS_Bioseq(sep))
{
bssp = (BioseqSetPtr)(sep->data.ptrvalue);
for (sep2 = bssp->seq_set; sep2 != NULL; sep2 = sep2->next)
{
SeqMgrLinkSeqEntry (sep2, the_type, sep->data.ptrvalue);
}
}
return TRUE;
}
/*****************************************************************************
*
* Selection Functions for data objects based on SeqLoc
* See also general selection in objmgr.h
*
*****************************************************************************/
/*****************************************************************************
*
* SeqMgrSelect(region)
* region is a SeqLocPtr
* It can only apply to one Bioseq
* selected area will be extreme left and right ends
* fuzziness is ignored
* if something else selected, deselects it first, then selects requested
* item
* to select without deselecting something else, use SeqMgrAlsoSelect()
* returns TRUE if item is now currently selected, FALSE if not
* "region" is always copied. Caller is responsible for managment of
* SeqLoc that is passed in.
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrSelect (SeqLocPtr region)
{
return SeqMgrGenericSelect(region, 1, NULL);
}
NLM_EXTERN Boolean LIBCALL SeqMgrAlsoSelect (SeqLocPtr region)
{
return SeqMgrGenericSelect(region, 2, NULL);
}
/*****************************************************************************
*
* SeqMgrDeSelect(region)
* if this item was selected, then deselects and returns TRUE
* else returns FALSE
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrDeSelect (SeqLocPtr region)
{
return SeqMgrGenericSelect(region, 3, NULL);
}
/*****************************************************************************
*
* SeqMgrSetColor(region, rgb)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrSetColor (SeqLocPtr region, Uint1Ptr rgb)
{
if (rgb == NULL) return FALSE;
return SeqMgrGenericSelect(region, 4, rgb);
}
static Boolean NEAR SeqMgrGenericSelect (SeqLocPtr region, Int2 type,
Uint1Ptr rgb)
{
SeqInt si;
ValNode vn;
SeqIdPtr sip;
Uint2 entityID, itemID;
if (region == NULL) return FALSE;
sip = SeqLocId(region);
if (sip == NULL) return FALSE;
entityID = BioseqFindEntity(sip, &itemID);
if (entityID == 0) return FALSE;
MemSet((Pointer)(&si), 0, sizeof(SeqInt));
MemSet((Pointer)(&vn), 0, sizeof(ValNode));
si.id = sip;
si.from = SeqLocStart(region);
si.to = SeqLocStop(region);
si.strand = SeqLocStrand(region);
if ((si.from < 0) || (si.to < 0) || (si.from > si.to)) return FALSE;
vn.choice = SEQLOC_INT;
vn.data.ptrvalue = &si;
switch (type)
{
case 1:
return ObjMgrSelect(entityID, itemID, OBJ_BIOSEQ, OM_REGION_SEQLOC, &vn);
case 2:
return ObjMgrAlsoSelect(entityID, itemID, OBJ_BIOSEQ, OM_REGION_SEQLOC, &vn);
case 3:
return ObjMgrDeSelect(entityID, itemID, OBJ_BIOSEQ, OM_REGION_SEQLOC, &vn);
case 4:
return ObjMgrSetColor(entityID, itemID, OBJ_BIOSEQ,
OM_REGION_SEQLOC, &vn, rgb);
default:
break;
}
return FALSE;
}
/*****************************************************************************
*
* SpreadGapsInDeltaSeq(BioseqPtr bsp)
* bsp must be a delta seq
* function counts deltas with known lengths ( = known_len)
* counts deltas which are gaps of unknown length ( = unk_count)
* these can delta of length 0, delta with fuzz = lim (unk),
* or SEQLOC_NULL
* converts all unknown gaps to delta with fuzz = lim(unk)
* sets length of all unknown gaps to
* (bsp->length - known_len)/unk_count
* any reminder spread over first few gaps
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SpreadGapsInDeltaSeq (BioseqPtr bsp)
{
Boolean retval = FALSE;
Int4 known_len = 0,
total_gap, gap_len,
unk_count = 0,
remainder;
DeltaSeqPtr dsp;
SeqLocPtr slocp;
SeqLitPtr slp;
IntFuzzPtr ifp;
if (bsp == NULL) return retval;
if ((bsp->repr != Seq_repr_delta) || (bsp->seq_ext == NULL))
return retval;
retval = TRUE; /* can function */
for (dsp = (DeltaSeqPtr)(bsp->seq_ext); dsp != NULL; dsp = dsp->next)
{
switch (dsp->choice)
{
case 1: /* SeqLocPtr */
slocp = (SeqLocPtr)(dsp->data.ptrvalue);
if (slocp == NULL) break;
if (slocp->choice == SEQLOC_NULL) /* convert it */
{
SeqLocFree(slocp);
slp = SeqLitNew();
dsp->choice = 2;
dsp->data.ptrvalue = slp;
ifp = IntFuzzNew();
slp->fuzz = ifp;
ifp->choice = 4; /* lim - type unk */
unk_count++;
}
else /* count length */
known_len += SeqLocLen(slocp);
break;
case 2: /* SeqLitPtr */
slp = (SeqLitPtr)(dsp->data.ptrvalue);
if (slp == NULL) break;
if (slp->seq_data != NULL) /* not a gap */
{
known_len += slp->length;
break;
}
ifp = slp->fuzz;
if (slp->length == 0) /* unknown length */
{
unk_count++;
if (ifp != NULL)
{
if (ifp->choice != 4) /* not lim */
ifp = IntFuzzFree(ifp);
else if (ifp->a != 0) /* not unk */
ifp = IntFuzzFree(ifp);
}
if (ifp == NULL)
{
ifp = IntFuzzNew();
ifp->choice = 4; /* lim - unk */
slp->fuzz = ifp;
}
}
else /* gap length was set */
{
if (ifp == NULL) /* no fuzz - count length */
known_len += slp->length;
else /* might be a guess */
{
if ((ifp->choice == 4) && (ifp->a == 0)) /* lim - unk */
unk_count++;
else
known_len += slp->length;
}
}
break;
default:
break;
}
}
if (unk_count == 0) /* no unknown gaps */
return retval;
total_gap = bsp->length - known_len;
if (total_gap < 0)
total_gap = 0;
gap_len = total_gap / unk_count;
remainder = total_gap - (gap_len * unk_count);
for (dsp = (DeltaSeqPtr)(bsp->seq_ext); dsp != NULL; dsp = dsp->next)
{
switch (dsp->choice)
{
case 1: /* SeqLocPtr */
break;
case 2: /* SeqLitPtr */
slp = (SeqLitPtr)(dsp->data.ptrvalue);
if (slp == NULL) break;
if (slp->seq_data != NULL) break;
ifp = slp->fuzz;
if (ifp == NULL) break;
if ((ifp->choice != 4) || (ifp->a != 0))
break;
slp->length = gap_len;
if (remainder)
{
slp->length++;
remainder--;
}
break;
default:
break;
}
}
return retval;
}
/*****************************************************************************
*
* CountGapsInDeltaSeq(BioseqPtr bsp, &num_segs, &num_gaps, &known_residues, &num_gaps_faked)
* bsp must be a delta seq
* function counts deltas and returns a profile
* num_segs = total number of segments
* num_gaps = total number of segments representing gaps
* known_residues = number of real residues in the sequence (not gaps)
* num_gaps_faked = number of gaps where real length is not known, but where
* a length was guessed by spreading the total gap length
* out over all gaps evenly.
*
* NOTE: any of these pointers except bsp can be NULL
*
* returns TRUE if values in argument were set.
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL CountGapsInDeltaSeq (BioseqPtr bsp, Int4Ptr num_segs, Int4Ptr num_gaps, Int4Ptr known_residues, Int4Ptr num_gaps_faked, CharPtr buf, Int2 buflen)
{
Boolean retval = FALSE;
Int4 residues = 0,
segs = 0,
gaps = 0,
len = 0,
fake_gaps = 0,
from = 0,
tlen = 0;
DeltaSeqPtr dsp;
SeqLocPtr slocp;
SeqLitPtr slp;
IntFuzzPtr ifp;
Boolean unk;
static Char tmp[128];
Int2 diff;
if (bsp == NULL) return retval;
if ((bsp->repr != Seq_repr_delta) || (bsp->seq_ext == NULL))
return retval;
retval = TRUE; /* can function */
for (dsp = (DeltaSeqPtr)(bsp->seq_ext); dsp != NULL; dsp = dsp->next)
{
segs++;
from = len + 1;
switch (dsp->choice)
{
case 1: /* SeqLocPtr */
slocp = (SeqLocPtr)(dsp->data.ptrvalue);
if (slocp == NULL) break;
if (slocp->choice == SEQLOC_NULL) /* gap */
{
gaps++;
sprintf(tmp, "* %ld %ld gap of unknown length~", from, len);
diff = LabelCopy(buf, tmp, buflen);
buflen -= diff;
buf += diff;
}
else { /* count length */
residues += SeqLocLen(slocp);
if (buf != NULL) {
tlen = SeqLocLen(slocp);
len += tlen;
sprintf(tmp, "* %8ld %8ld: contig of %ld bp in length~", from, len, tlen);
diff = LabelCopy(buf, tmp, buflen);
buflen -= diff;
buf += diff;
}
}
break;
case 2: /* SeqLitPtr */
slp = (SeqLitPtr)(dsp->data.ptrvalue);
if (slp == NULL) break;
tlen = slp->length;
len += tlen;
if (slp->seq_data != NULL)
{
residues += slp->length;
if (buf) {
sprintf(tmp, "* %8ld %8ld: contig of %ld bp in length~", from, len, tlen);
diff = LabelCopy(buf, tmp, buflen);
buflen -= diff;
buf += diff;
}
}
else
{
unk = FALSE;
gaps++;
ifp = slp->fuzz;
if (ifp != NULL)
{
if ((ifp->choice == 4) && (ifp->a == 0)) {
unk = TRUE;
fake_gaps++;
if (buf) {
if (from > len) {
sprintf(tmp, "* gap of unknown length~");
} else {
sprintf(tmp, "* %8ld %8ld: gap of unknown length~", from, len);
}
diff = LabelCopy(buf, tmp, buflen);
buflen -= diff;
buf += diff;
}
}
}
if (!unk && buf) {
sprintf(tmp, "* %8ld %ld: gap of %8ld bp~", from, len, tlen);
diff = LabelCopy(buf, tmp, buflen);
buflen -= diff;
buf += diff;
}
}
break;
default:
break;
}
}
if (num_segs != NULL)
*num_segs = segs;
if (num_gaps != NULL)
*num_gaps = gaps;
if (known_residues != NULL)
*known_residues = residues;
if (num_gaps_faked != NULL)
*num_gaps_faked = fake_gaps;
return retval;
}
/*****************************************************************************
*
* SeqMgrAdd(type, data)
* adds a Bioseq or BioseqSet to the sequence manager
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrAdd (Uint2 type, Pointer data)
{
Boolean retval;
retval = ObjMgrAdd(type, data);
if (type != OBJ_BIOSEQ)
return retval;
SeqMgrAddToBioseqIndex((BioseqPtr)data);
return retval;
}
/*****************************************************************************
*
* SeqMgrDelete(type, data)
* deletes a Bioseq or BioseqSet from the sequence manager
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrDelete (Uint2 type, Pointer data)
{
if (type == OBJ_BIOSEQ) /* remove id indexes */
SeqMgrDeleteFromBioseqIndex((BioseqPtr)data);
return ObjMgrDelete(type, data);
}
static Boolean NEAR SeqMgrAddIndexElement(SeqMgrPtr smp, BioseqPtr bsp, CharPtr buf)
{
SeqIdIndexElementPtr sip, PNTR sipp;
SeqIdIndexBlockPtr sibp, prev;
Int4 imin, imax, i, j;
CharPtr tmp, newstr;
ObjMgrDataPtr omdp;
ObjMgrPtr omp;
omp = ObjMgrReadLock();
omdp = ObjMgrFindByData(omp, (Pointer)bsp); /* caching protection */
ObjMgrUnlock();
if (omdp == NULL)
{
return FALSE;
}
sipp = smp->BioseqIndex;
if (smp->BioseqIndexCnt >= smp->BioseqIndexNum) /* expand space */
{
prev = NULL;
for (sibp = smp->BioseqIndexData; sibp != NULL; sibp = sibp->next)
prev = sibp;
sibp = MemNew(sizeof(SeqIdIndexBlock));
if (prev != NULL)
prev->next = sibp;
else
smp->BioseqIndexData = sibp;
smp->BioseqIndex = MemNew((smp->BioseqIndexNum + 100) *
sizeof(SeqIdIndexElementPtr));
MemCopy(smp->BioseqIndex, sipp, (smp->BioseqIndexNum *
sizeof(SeqIdIndexElementPtr)));
MemFree(sipp);
smp->BioseqIndexNum += 100;
sipp = smp->BioseqIndex;
for (i = 0, j = smp->BioseqIndexCnt; i < 100; i++, j++)
sipp[j] = &(sibp->sid[i]);
}
i = smp->BioseqIndexCnt; /* empties are at the end */
sip = sipp[i];
sip->omdp = omdp; /* fill in the values */
sip->str = StringSave(buf);
newstr = sip->str;
RevStringUpper(newstr); /* try to avoid case check */
imin = 0; /* find where it goes */
imax = i-1;
if (imax >= 0)
tmp = sipp[imax]->str;
if ((i) && (StringCmp(newstr, sipp[imax]->str) < 0))
{
i = (imax + imin) / 2;
while (imax > imin)
{
tmp = sipp[i]->str;
if ((j = StringCmp(newstr, tmp)) < 0)
imax = i - 1;
else if (j > 0)
imin = i + 1;
else
break;
i = (imax + imin)/2;
}
if (StringCmp(newstr, sipp[i]->str) > 0) /* check for off by 1 */
{
i++;
}
imax = smp->BioseqIndexCnt - 1; /* open the array */
while (imax >= i)
{
sipp[imax+1] = sipp[imax];
imax--;
}
}
sipp[i] = sip; /* put in the pointer in order */
smp->BioseqIndexCnt++; /* got one more */
return TRUE;
}
/*****************************************************************************
*
* SeqMgrProcessNonIndexedBioseq()
* Indexes a BioseqPtr by SeqId(s)
*
*****************************************************************************/
static Boolean NEAR SeqMgrProcessNonIndexedBioseq(void)
{
BioseqPtr PNTR bspp, bsp;
Int4 i, total, k;
SeqIdPtr sip;
Char buf[80];
CharPtr tmp;
Uint1 oldchoice;
Boolean indexed;
TextSeqIdPtr tsip;
SeqMgrPtr smp;
smp = SeqMgrReadLock();
if (! smp->NonIndexedBioseqCnt)
{
SeqMgrUnlock();
return TRUE;
}
SeqMgrUnlock();
smp = SeqMgrWriteLock();
if (! smp->NonIndexedBioseqCnt)
{
SeqMgrUnlock();
return TRUE;
}
total = smp->NonIndexedBioseqCnt;
bspp = smp->NonIndexedBioseq;
for (i = 0; i < total; i++)
{
indexed = FALSE;
bsp = bspp[i];
if (bsp != NULL)
{
if (bsp->id != NULL)
{
indexed = TRUE;
for (sip = bsp->id; sip != NULL; sip = sip->next)
{
oldchoice = 0;
switch (sip->choice)
{
case SEQID_GI:
sprintf(buf, "%ld", (long)(sip->data.ptrvalue));
SeqMgrAddIndexElement(smp, bsp, buf);
break;
case SEQID_EMBL:
case SEQID_DDBJ:
oldchoice = sip->choice;
sip->choice = SEQID_GENBANK;
case SEQID_GENBANK:
case SEQID_PIR:
case SEQID_OTHER:
case SEQID_SWISSPROT:
case SEQID_PRF:
tsip = (TextSeqIdPtr)(sip->data.ptrvalue);
if (tsip->name != NULL)
{
tmp = tsip->accession;
tsip->accession = NULL;
SeqIdWrite(sip, buf, PRINTID_FASTA_SHORT, 79);
SeqMgrAddIndexElement(smp, bsp, buf);
tsip->accession = tmp;
}
tmp = tsip->name;
tsip->name = NULL;
SeqIdWrite(sip, buf, PRINTID_FASTA_SHORT, 79);
SeqMgrAddIndexElement(smp, bsp, buf);
tsip->name = tmp;
if (oldchoice)
sip->choice = oldchoice;
break;
default:
SeqIdWrite(sip, buf, PRINTID_FASTA_SHORT, 79);
SeqMgrAddIndexElement(smp, bsp, buf);
break;
}
}
}
}
if (indexed)
bspp[i] = NULL;
}
for (i = 0; i < total; i++)
{
if (bspp[i] == NULL)
{
total--;
for (k = i; k < total; k++)
bspp[k] = bspp[k+1];
i--;
}
}
smp->NonIndexedBioseqCnt = total;
SeqMgrUnlock();
return TRUE;
}
/*****************************************************************************
*
* SeqMgrAddToBioseqIndex(bsp)
* Indexes a BioseqPtr by SeqId(s)
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrAddToBioseqIndex (BioseqPtr bsp)
{
SeqMgrPtr smp;
BioseqPtr PNTR bspp;
if (bsp == NULL)
return FALSE;
smp = SeqMgrWriteLock();
/* increase array as
needed */
if (smp->NonIndexedBioseqCnt >= smp->NonIndexedBioseqNum)
{
bspp = smp->NonIndexedBioseq;
smp->NonIndexedBioseq = MemNew((smp->NonIndexedBioseqNum + 10) *
sizeof (BioseqPtr));
MemCopy(smp->NonIndexedBioseq, bspp, (smp->NonIndexedBioseqNum *
sizeof(BioseqPtr)));
MemFree(bspp);
smp->NonIndexedBioseqNum += 10;
}
smp->NonIndexedBioseq[smp->NonIndexedBioseqCnt] = bsp;
smp->NonIndexedBioseqCnt++;
SeqMgrUnlock();
SeqMgrProcessNonIndexedBioseq();
return TRUE;
}
/*****************************************************************************
*
* SeqMgrDeleteDeleteFromBioseqIndex(bsp)
* Removes index on BioseqPtr SeqIds
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrDeleteFromBioseqIndex (BioseqPtr bsp)
{
SeqMgrPtr smp;
SeqIdIndexElementPtr PNTR sipp, sip;
Int4 i, j, num;
BioseqPtr PNTR bspp;
ObjMgrDataPtr omdp;
ObjMgrPtr omp;
smp = SeqMgrWriteLock();
/* check if not
indexed yet */
if (smp->NonIndexedBioseqCnt > 0)
{
num = smp->NonIndexedBioseqCnt;
bspp = smp->NonIndexedBioseq;
for (i = 0; i < num; i++)
{
if (bspp[i] == bsp)
{
num--;
for (j = i; j < num; j++)
bspp[j] = bspp[j+1];
i--;
}
}
smp->NonIndexedBioseqCnt = num;
}
num = smp->BioseqIndexCnt;
sipp = smp->BioseqIndex;
omp = ObjMgrReadLock();
omdp = ObjMgrFindByData(omp, (Pointer)bsp);
ObjMgrUnlock();
for (i = 0; i < BIOSEQ_CACHE_NUM; i++) /* remove from BioseqFind cache */
{
if (omdp_cache[i] == omdp)
{
omdp_cache[i] = NULL;
se_cache[i] = NULL;
}
}
for (i = 0; i < num; i++)
{
if (sipp[i]->omdp == omdp)
{
sipp[i]->omdp = NULL;
sipp[i]->str = MemFree(sipp[i]->str);
sip = sipp[i];
for (j = i; j < (num-1); j++)
sipp[j] = sipp[j+1];
sipp[j] = sip;
num--; i--;
}
}
smp->BioseqIndexCnt = num;
SeqMgrUnlock();
return TRUE;
}
/*****************************************************************************
*
* SeqMgrReplaceInBioseqIndex(bsp)
* Replaces index on BioseqPtr SeqIds
*
*****************************************************************************/
NLM_EXTERN Boolean LIBCALL SeqMgrReplaceInBioseqIndex (BioseqPtr bsp)
{
SeqMgrDeleteFromBioseqIndex(bsp);
return SeqMgrAddToBioseqIndex(bsp);
}
/*****************************************************************************
*
* GetUniGeneIDForSeqId(SeqIdPtr)
* returns the UniGene ID for a SeqId
* returns 0 if can't find it, or not a legal unigene id
* This only applies to genomes division of entrez
*
*****************************************************************************/
/*****************************************************************
*
* IT IS a KLUDGE!! Add 1,000,000 to the unigene id
*
*****************************************************************/
#define KLUDGE_UNIGENE 1000000 /*the kludge offset val add to unigene sequence*/
#define KLUDGE_FlyBase 2000000 /*the kludge offset for FlyBase*/
#define KLUDGE_JACKSON 3000000 /*the kludge offset for the Mouse data*/
#define KLUDGE_JRGP 4000000 /*the kludge offset for the rice data*/
#define KLUDGE_CESC 5000000 /*the kludge offset for the C. elegans data*/
#define KLUDGE_BSNR 6000000 /*the kludge offset for the B. subtilis data*/
#define KLUDGE_HUMGEN 7000000 /*the kludge offset for the Human genomic data*/
#define KLUDGE_YGG 8000000 /*the kludge offset for the yeast data*/
#define KLUDGE_NCBICG 9000000 /*the kludge offset for small genomes*/
#define KLUDGE_MAIZE 10000000 /*the kludge offset for corn*/
NLM_EXTERN Int4 LIBCALL GetUniGeneIDForSeqId (SeqIdPtr sip)
{
DbtagPtr db_tag;
ObjectIdPtr oip;
if (sip == NULL)
return 0;
if(sip->choice != SEQID_GENERAL)
return 0;
db_tag = sip->data.ptrvalue;
if(db_tag == NULL || db_tag->db == NULL)
return 0;
oip = db_tag->tag;
if(oip == NULL || oip->id == 0)
return 0;
if(StringCmp(db_tag->db, "UNIGENE") == 0)
return (KLUDGE_UNIGENE+ oip->id);
if(StringCmp(db_tag->db, "UniGene") == 0)
return (KLUDGE_UNIGENE+ oip->id);
if(StringCmp(db_tag->db, "FlyBase") == 0)
return (KLUDGE_FlyBase+ oip->id);
if(StringCmp(db_tag->db, "JACKSON") == 0)
return (KLUDGE_JACKSON+ oip->id);
if(StringCmp(db_tag->db, "JRGP") == 0)
return (KLUDGE_JRGP + oip->id);
if(StringCmp(db_tag->db, "CESC") == 0)
return (KLUDGE_CESC + oip->id);
if(StringCmp(db_tag->db, "BSNR") == 0)
return (KLUDGE_BSNR + oip->id);
if(StringCmp(db_tag->db, "HUMGEN") == 0)
return (KLUDGE_HUMGEN + oip->id);
if(StringCmp(db_tag->db, "YGG") == 0)
return (KLUDGE_YGG + oip->id);
if(StringCmp(db_tag->db, "NCBICG") == 0)
return (KLUDGE_NCBICG + oip->id);
if(StringCmp(db_tag->db, "MAIZE") == 0)
return (KLUDGE_MAIZE + oip->id);
return 0;
}
/*****************************************************************************
*
* BioseqExtra extensions to preindex for rapid retrieval
*
*****************************************************************************/
/*
* remaining to be done are mapping tables for rapid coordinate conversion
* between genome record and parts, genomic DNA and mRNA, and mRNA and protein
*/
static ObjMgrDataPtr SeqMgrGetOmdpForPointer (Pointer ptr)
{
ObjMgrDataPtr omdp;
ObjMgrPtr omp;
if (ptr == NULL) return NULL;
omp = ObjMgrWriteLock ();
omdp = ObjMgrFindByData (omp, ptr);
ObjMgrUnlock ();
return omdp;
}
static ObjMgrDataPtr SeqMgrGetOmdpForBioseq (BioseqPtr bsp)
{
ObjMgrDataPtr omdp;
ObjMgrPtr omp;
if (bsp == NULL) return NULL;
omdp = (ObjMgrDataPtr) bsp->omdp;
if (omdp != NULL) return omdp;
omp = ObjMgrWriteLock ();
omdp = ObjMgrFindByData (omp, bsp);
ObjMgrUnlock ();
bsp->omdp = (Pointer) omdp;
return omdp;
}
static SeqEntryPtr SeqMgrGetTopSeqEntryForEntity (Uint2 entityID)
{
ObjMgrDataPtr omdp;
SeqSubmitPtr ssp;
omdp = ObjMgrGetData (entityID);
if (omdp == NULL) return FALSE;
switch (omdp->datatype) {
case OBJ_SEQSUB :
ssp = (SeqSubmitPtr) omdp->dataptr;
if (ssp != NULL && ssp->datatype == 1) {
return (SeqEntryPtr) ssp->data;
}
break;
case OBJ_BIOSEQ :
case OBJ_BIOSEQSET :
return (SeqEntryPtr) omdp->choice;
default :
break;
}
return NULL;
}
static Boolean SeqMgrClearBioseqExtraData (ObjMgrDataPtr omdp)
{
BioseqExtraPtr bspextra;
SMFeatBlockPtr currf;
SMSeqIdxPtr currp;
Int2 i;
SMFeatItemPtr itemf;
SMFeatBlockPtr nextf;
SMSeqIdxPtr nextp;
if (omdp == NULL) return FALSE;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return FALSE;
/* free sorted arrays of pointers into data blocks */
bspextra->featsByID = MemFree (bspextra->featsByID);
bspextra->featsBySfp = MemFree (bspextra->featsBySfp);
bspextra->featsByPos = MemFree (bspextra->featsByPos);
bspextra->genesByPos = MemFree (bspextra->genesByPos);
bspextra->mRNAsByPos = MemFree (bspextra->mRNAsByPos);
bspextra->CDSsByPos = MemFree (bspextra->CDSsByPos);
bspextra->pubsByPos = MemFree (bspextra->pubsByPos);
bspextra->orgsByPos = MemFree (bspextra->orgsByPos);
/* free arrays to speed mapping from parts to segmented bioseq */
bspextra->partsByLoc = MemFree (bspextra->partsByLoc);
bspextra->partsBySeqId = MemFree (bspextra->partsBySeqId);
/* free data blocks of feature information */
currf = bspextra->featlisthead;
while (currf != NULL) {
nextf = currf->next;
if (currf->data != NULL) {
/* free allocated label strings within block items */
for (i = 0; i < currf->index; i++) {
itemf = &(currf->data [i]);
MemFree (itemf->label);
MemFree (itemf->ivals);
}
/* free array of SMFeatItems */
MemFree (currf->data);
}
MemFree (currf);
currf = nextf;
}
/* free data blocks of parts to segment mapping information */
currp = bspextra->segparthead;
while (currp != NULL) {
nextp = currp->next;
SeqLocFree (currp->slp);
MemFree (currp->seqIdOfPart);
MemFree (currp);
currp = nextp;
}
/* clean interval list once implemented */
bspextra->featlisthead = NULL;
bspextra->featlisttail = NULL;
bspextra->segparthead = NULL;
bspextra->numfeats = 0;
bspextra->numgenes = 0;
bspextra->nummRNAs = 0;
bspextra->numCDSs = 0;
bspextra->numpubs = 0;
bspextra->numorgs = 0;
bspextra->numsegs = 0;
bspextra->min = INT4_MAX;
bspextra->blocksize = 50;
bspextra->protFeat = NULL;
bspextra->cdsOrRnaFeat = NULL;
/* free genome - parts mapping arrays when they are added */
return TRUE;
}
static Boolean DoSeqMgrFreeBioseqExtraData (ObjMgrDataPtr omdp)
{
if (omdp == NULL) return FALSE;
if (omdp->datatype != OBJ_BIOSEQ && omdp->datatype != OBJ_BIOSEQSET) return FALSE;
if (omdp->extradata != NULL) {
SeqMgrClearBioseqExtraData (omdp);
omdp->extradata = MemFree (omdp->extradata);
omdp->reapextra = NULL;
omdp->reloadextra = NULL;
omdp->freeextra = NULL;
}
return TRUE;
}
/* object manager callbacks to reap, reload, and free extra bioseq data */
NLM_EXTERN Pointer LIBCALLBACK SeqMgrReapBioseqExtraFunc (Pointer data)
{
BioseqExtraPtr bspextra;
SMFeatBlockPtr curr;
Int2 i;
SMFeatItemPtr item;
ObjMgrDataPtr omdp;
omdp = (ObjMgrDataPtr) data;
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return NULL;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return NULL;
/* loop through data blocks of feature information */
curr = bspextra->featlisthead;
while (curr != NULL) {
/* NULL out pointers to cached out feature and annot */
if (curr->data != NULL) {
for (i = 0; i < curr->index; i++) {
item = &(curr->data [i]);
item->sfp = NULL;
item->sap = NULL;
}
}
curr = curr->next;
}
return NULL;
}
NLM_EXTERN Pointer LIBCALLBACK SeqMgrReloadBioseqExtraFunc (Pointer data)
{
return NULL;
}
NLM_EXTERN Pointer LIBCALLBACK SeqMgrFreeBioseqExtraFunc (Pointer data)
{
DoSeqMgrFreeBioseqExtraData ((ObjMgrDataPtr) data);
return NULL;
}
/*****************************************************************************
*
* SeqMgrClearFeatureIndexes clears every bioseq in an entity
*
*****************************************************************************/
static void SeqMgrClearIndexesProc (SeqEntryPtr sep, Pointer mydata, Int4 index, Int2 indent)
{
BioseqPtr bsp;
BioseqSetPtr bssp;
ObjMgrDataPtr omdp = NULL;
BoolPtr rsult;
if (sep == NULL || (! IS_Bioseq (sep))) return;
if (IS_Bioseq (sep)) {
bsp = (BioseqPtr) sep->data.ptrvalue;
if (bsp == NULL) return;
omdp = SeqMgrGetOmdpForBioseq (bsp);
} else if (IS_Bioseq_set (sep)) {
bssp = (BioseqSetPtr) sep->data.ptrvalue;
if (bssp == NULL) return;
omdp = SeqMgrGetOmdpForPointer (bssp);
}
if (omdp != NULL && DoSeqMgrFreeBioseqExtraData (omdp)) {
rsult = (BoolPtr) mydata;
*rsult = TRUE;
}
}
NLM_EXTERN Boolean LIBCALL SeqMgrClearFeatureIndexes (Uint2 entityID, Pointer ptr)
{
ObjMgrDataPtr omdp;
Boolean rsult = FALSE;
SeqEntryPtr sep;
if (entityID == 0) {
entityID = ObjMgrGetEntityIDForPointer (ptr);
}
if (entityID == 0) return FALSE;
sep = SeqMgrGetTopSeqEntryForEntity (entityID);
if (sep == NULL) return FALSE;
SeqEntryExplore (sep, (Pointer) (&rsult), SeqMgrClearIndexesProc);
/* clear out object manager time of indexing flag and master feature itemID list */
omdp = ObjMgrGetData (entityID);
if (omdp != NULL) {
omdp->indexed = 0;
SeqMgrClearBioseqExtraData (omdp);
omdp->extradata = MemFree (omdp->extradata);
omdp->reapextra = NULL;
omdp->reloadextra = NULL;
omdp->freeextra = NULL;
}
return rsult;
}
/*****************************************************************************
*
* FindAppropriateBioseq finds the segmented bioseq if location is join on parts
*
*****************************************************************************/
static BioseqPtr FindAppropriateBioseq (SeqLocPtr loc, BioseqPtr tryfirst)
{
BioseqPtr bsp = NULL;
BioseqExtraPtr bspextra;
BioseqSetPtr bssp;
ObjMgrDataPtr omdp;
BioseqPtr part;
SeqEntryPtr sep;
SeqIdPtr sip;
SeqLocPtr slp;
if (loc == NULL) return NULL;
sip = SeqLocId (loc);
if (sip != NULL) {
if (tryfirst != NULL && SeqIdIn (sip, tryfirst->id)) {
bsp = tryfirst;
} else {
bsp = BioseqFind (sip);
}
/* first see if this is raw local part of segmented bioseq */
if (bsp != NULL && bsp->repr == Seq_repr_raw) {
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp != NULL && omdp->datatype == OBJ_BIOSEQ) {
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra != NULL) {
if (bspextra->parentBioseq != NULL) {
bsp = bspextra->parentBioseq;
}
}
}
}
return bsp;
}
/* otherwise assume location is on multiple parts of a segmented set */
slp = SeqLocFindNext (loc, NULL);
if (slp == NULL) return NULL;
sip = SeqLocId (slp);
if (sip == NULL) return NULL;
part = BioseqFind (sip);
if (part == NULL) return NULL;
omdp = SeqMgrGetOmdpForBioseq (part);
while (omdp != NULL) {
if (omdp->datatype == OBJ_BIOSEQSET) {
bssp = (BioseqSetPtr) omdp->dataptr;
if (bssp != NULL) {
if (bssp->_class == BioseqseqSet_class_segset) {
for (sep = bssp->seq_set; sep != NULL; sep = sep->next) {
if (IS_Bioseq (sep)) {
bsp = (BioseqPtr) sep->data.ptrvalue;
if (bsp != NULL) {
return bsp;
}
}
}
}
}
}
omdp = SeqMgrGetOmdpForPointer (omdp->parentptr);
}
return NULL;
}
/*****************************************************************************
*
* FindFirstLocalBioseq is called as a last resort if FindAppropriateBioseq
* fails, and it scans the feature location to find the first local bioseq
* referenced by a feature interval
*
*****************************************************************************/
static BioseqPtr FindFirstLocalBioseq (SeqLocPtr loc)
{
BioseqPtr bsp;
SeqIdPtr sip;
SeqLocPtr slp = NULL;
if (loc == NULL) return NULL;
while ((slp = SeqLocFindNext (loc, slp)) != NULL) {
sip = SeqLocId (slp);
if (sip != NULL) {
bsp = BioseqFind (sip);
if (bsp != NULL) return bsp;
}
}
return NULL;
}
/*****************************************************************************
*
* GetOffsetInFirstLocalBioseq is called to get the intervals on last resort bioseqs
*
*****************************************************************************/
static Int4 GetOffsetInFirstLocalBioseq (SeqLocPtr loc, BioseqPtr in, Uint1 which_end)
{
SeqLocPtr slp = NULL;
Int4 val;
if (loc == NULL) return -1;
while ((slp = SeqLocFindNext (loc, slp)) != NULL) {
val = GetOffsetInBioseq (slp, in, which_end);
if (val != -1) return val;
}
return -1;
}
/*****************************************************************************
*
* SeqMgrFindSMFeatItemPtr and SeqMgrFindSMFeatItemByID return SMFeatItemPtr
* to access internal fields
* SeqMgrGetDesiredDescriptor and SeqMgrGetDesiredFeature take an itemID,
* position index, or SeqDescPtr or SeqFeatPtr, return the SeqDescPtr or
* SeqFeatPtr, and fill in the context structure
*
*****************************************************************************/
NLM_EXTERN SMFeatItemPtr LIBCALL SeqMgrFindSMFeatItemPtr (SeqFeatPtr sfp)
{
SMFeatItemPtr PNTR array;
BioseqPtr bsp;
BioseqExtraPtr bspextra;
SMFeatBlockPtr curr;
Int2 i;
SMFeatItemPtr item;
Int4 L;
Int4 mid;
ObjMgrDataPtr omdp;
Int4 R;
if (sfp == NULL) return NULL;
bsp = FindAppropriateBioseq (sfp->location, NULL);
if (bsp == NULL) {
bsp = FindFirstLocalBioseq (sfp->location);
}
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return NULL;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return NULL;
/* first try array sorted by SeqFeatPtr value */
array = bspextra->featsBySfp;
if (array != NULL && bspextra->numfeats > 0) {
L = 0;
R = bspextra->numfeats - 1;
while (L < R) {
mid = (L + R) / 2;
item = array [mid];
if (item != NULL && item->sfp < sfp) {
L = mid + 1;
} else {
R = mid;
}
}
item = array [R];
if (item->sfp == sfp) return item;
}
/* now look in feature indices for cached feature information */
curr = bspextra->featlisthead;
while (curr != NULL) {
if (curr->data != NULL) {
for (i = 0; i < curr->index; i++) {
item = &(curr->data [i]);
if (item->sfp == sfp && (! item->ignore)) return item;
}
}
curr = curr->next;
}
return NULL;
}
NLM_EXTERN SMFeatItemPtr LIBCALL SeqMgrFindSMFeatItemByID (Uint2 entityID, BioseqPtr bsp, Uint2 itemID)
{
SMFeatItemPtr PNTR array;
BioseqExtraPtr bspextra;
SMFeatBlockPtr curr;
Int2 i;
SMFeatItemPtr item;
Int4 L;
Int4 mid;
ObjMgrDataPtr omdp;
Int4 R;
if (entityID > 0) {
omdp = ObjMgrGetData (entityID);
if (omdp == NULL) return NULL;
} else {
if (bsp == NULL) return NULL;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return NULL;
}
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return NULL;
/* first try array sorted by itemID value */
array = bspextra->featsByID;
if (array != NULL && bspextra->numfeats > 0) {
L = 0;
R = bspextra->numfeats - 1;
while (L < R) {
mid = (L + R) / 2;
item = array [mid];
if (item != NULL && item->itemID < itemID) {
L = mid + 1;
} else {
R = mid;
}
}
item = array [R];
if (item->itemID == itemID) return item;
}
/* now look in feature indices for cached feature information */
curr = bspextra->featlisthead;
while (curr != NULL) {
if (curr->data != NULL) {
for (i = 0; i < curr->index; i++) {
item = &(curr->data [i]);
if (item->itemID == itemID && (! item->ignore)) return item;
}
}
curr = curr->next;
}
return NULL;
}
NLM_EXTERN SeqFeatPtr LIBCALL SeqMgrGetDesiredFeature (Uint2 entityID, BioseqPtr bsp,
Uint2 itemID, Uint2 index, SeqFeatPtr sfp,
SeqMgrFeatContext PNTR context)
{
SMFeatItemPtr PNTR array;
BioseqExtraPtr bspextra;
SeqFeatPtr curr;
SMFeatItemPtr item = NULL;
ObjMgrDataPtr omdp;
if (entityID > 0) {
omdp = ObjMgrGetData (entityID);
if (omdp == NULL) return NULL;
} else {
if (bsp == NULL) return NULL;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return NULL;
}
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return NULL;
if (itemID > 0) {
item = SeqMgrFindSMFeatItemByID (entityID, bsp, itemID);
} else if (index > 0) {
array = bspextra->featsByPos;
if (array != NULL && bspextra->numfeats > 0 && index <= bspextra->numfeats) {
item = array [index - 1];
}
} else if (sfp != NULL) {
item = SeqMgrFindSMFeatItemPtr (sfp);
}
if (item == NULL) return NULL;
entityID = ObjMgrGetEntityIDForPointer (omdp->dataptr);
curr = item->sfp;
if (curr != NULL && context != NULL && (! item->ignore)) {
context->entityID = entityID;
context->itemID = item->itemID;
context->sfp = curr;
context->sap = item->sap;
context->bsp = item->bsp;
context->label = item->label;
context->left = item->left;
context->right = item->right;
context->partialL = item->partialL;
context->partialR = item->partialR;
context->farloc = item->farloc;
context->strand = item->strand;
context->seqfeattype = FindFeatFromFeatDefType (item->subtype);
context->featdeftype = item->subtype;
context->numivals = item->numivals;
context->ivals = item->ivals;
context->userdata = NULL;
context->omdp = (Pointer) omdp;
context->index = item->index + 1;
}
return curr;
}
static ValNodePtr DesiredDescriptorPerBioseq (SeqEntryPtr sep, BioseqPtr bsp,
Uint2 itemID, Uint2 index, ValNodePtr sdp,
SeqMgrDescContext PNTR context)
{
BioseqSetPtr bssp;
ValNodePtr curr = NULL;
SeqEntryPtr tmp;
if (sep != NULL) {
if (IS_Bioseq (sep)) {
bsp = (BioseqPtr) sep->data.ptrvalue;
if (bsp == NULL) return NULL;
} else if (IS_Bioseq_set (sep)) {
bssp = (BioseqSetPtr) sep->data.ptrvalue;
if (bssp == NULL) return NULL;
for (tmp = bssp->seq_set; tmp != NULL; tmp = tmp->next) {
curr = DesiredDescriptorPerBioseq (tmp, NULL, itemID, index, sdp, context);
if (curr != NULL) return curr;
}
return NULL;
}
}
if (bsp == NULL) return NULL;
while ((curr = SeqMgrGetNextDescriptor (bsp, curr, 0, context)) != NULL) {
if (itemID > 0 && itemID == context->itemID) return curr;
if (index > 0 && index == context->index) return curr;
if (sdp != NULL && sdp == curr) return curr;
}
return NULL;
}
NLM_EXTERN ValNodePtr LIBCALL SeqMgrGetDesiredDescriptor (Uint2 entityID, BioseqPtr bsp,
Uint2 itemID, Uint2 index, ValNodePtr sdp,
SeqMgrDescContext PNTR context)
{
SeqMgrDescContext dfaultcontext;
SeqEntryPtr sep;
if (context == NULL) {
context = &dfaultcontext;
}
if (entityID > 0) {
sep = SeqMgrGetTopSeqEntryForEntity (entityID);
if (sep == NULL) return NULL;
return DesiredDescriptorPerBioseq (sep, NULL, itemID, index, sdp, context);
} else if (bsp != NULL) {
return DesiredDescriptorPerBioseq (NULL, bsp, itemID, index, sdp, context);
}
return NULL;
}
/*****************************************************************************
*
* RecordFeaturesInBioseqs callback explores bioseqs, bioseq sets, and features,
* keeping a running total of the descriptor item counts, and records specific
* information about features on each bioseq
*
*****************************************************************************/
typedef struct extraindex {
BioseqPtr lastbsp;
SeqAnnotPtr lastsap;
BioseqSetPtr lastbssp;
SMSeqIdxPtr segpartail;
Int4 cumulative;
Uint2 descrcount;
Uint2 featcount;
} ExtraIndex, PNTR ExtraIndexPtr;
static void SetDescriptorCounts (ValNodePtr sdp, ExtraIndexPtr exindx, Pointer thisitem, Uint2 thistype)
{
Uint2 count = 0;
ObjMgrDataPtr omdp;
/* count bioseq or bioseq set descriptors, to calculate omdp.lastDescrItemID */
if (sdp == NULL || exindx == NULL) return;
if (thistype == OBJ_BIOSEQ) {
omdp = SeqMgrGetOmdpForBioseq ((BioseqPtr) thisitem);
} else {
omdp = SeqMgrGetOmdpForPointer (thisitem);
}
if (omdp == NULL) return;
omdp->lastDescrItemID = exindx->descrcount;
while (sdp != NULL) {
count++;
sdp = sdp->next;
}
exindx->descrcount += count;
}
static void CreateBioseqExtraBlock (ObjMgrDataPtr omdp, BioseqPtr bsp)
{
BioseqExtraPtr bspextra;
if (omdp == NULL || omdp->extradata != NULL) return;
bspextra = (BioseqExtraPtr) MemNew (sizeof (BioseqExtra));
omdp->extradata = (Pointer) bspextra;
if (bspextra == NULL) return;
omdp->reapextra = SeqMgrReapBioseqExtraFunc;
omdp->reloadextra = SeqMgrReloadBioseqExtraFunc;
omdp->freeextra = SeqMgrFreeBioseqExtraFunc;
bspextra->bsp = bsp;
bspextra->omdp = omdp;
bspextra->min = INT4_MAX;
}
static SeqIdPtr SeqIdWithinBioseq (BioseqPtr bsp, SeqLocPtr slp)
{
SeqIdPtr a;
SeqIdPtr b;
if (bsp == NULL || slp == NULL) return NULL;
a = SeqLocId (slp);
if (a == NULL) return NULL;
for (b = bsp->id; b != NULL; b = b->next) {
if (SeqIdComp (a, b) == SIC_YES) return b;
}
return NULL;
}
static void ProcessFeatureProducts (SeqFeatPtr sfp, Uint2 itemID, GatherContextPtr gcp)
{
BioseqPtr bsp;
BioseqExtraPtr bspextra;
Char buf [81];
CharPtr ctmp;
Int4 diff;
CharPtr loclbl;
Int4 min;
ObjMgrDataPtr omdp;
CharPtr prodlbl;
SeqFeatPtr prt;
CharPtr ptmp;
SeqAnnotPtr sap;
SeqIdPtr sip;
SeqLocPtr slp;
ValNode vn;
if (sfp == NULL || sfp->product == NULL) return;
if (sfp->data.choice != SEQFEAT_CDREGION && sfp->data.choice != SEQFEAT_RNA) return;
sip = SeqLocId (sfp->product);
if (sip == NULL) return;
bsp = BioseqFind (sip);
if (bsp == NULL) return;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) {
CreateBioseqExtraBlock (omdp, bsp);
bspextra = (BioseqExtraPtr) omdp->extradata;
}
if (bspextra == NULL) return;
/* cds or rna reference stored in product bioseq's omdp.cdsOrRnaFeat */
if (bspextra->cdsOrRnaFeat != NULL && bspextra->cdsOrRnaFeat != sfp) {
FeatDefLabel (sfp, buf, sizeof (buf) - 1, OM_LABEL_CONTENT);
ctmp = SeqLocPrint (sfp->location);
loclbl = ctmp;
if (loclbl == NULL) {
loclbl = "?";
}
ptmp = SeqLocPrint (sfp->product);
prodlbl = ptmp;
if (prodlbl == NULL) {
prodlbl = "?";
}
ErrPostItem (SEV_WARNING, 0, 0,
"SeqMgr indexing cds or rna progenitor already set - Feature: %s - Location [%s] - Product [%s]",
buf, loclbl, prodlbl);
MemFree (ctmp);
MemFree (ptmp);
}
if (omdp->tempload == TL_NOT_TEMP) {
bspextra->cdsOrRnaFeat = sfp;
}
if (sfp->data.choice == SEQFEAT_RNA) return;
/* if protFeat exists it was set by exhaustive gather on protein bioseq */
if (bspextra->protFeat != NULL) return;
/* calculate largest protein feature on cds's product bioseq */
min = INT4_MAX;
vn.choice = SEQLOC_WHOLE;
vn.data.ptrvalue = (Pointer) bsp->id;
vn.next = NULL;
slp = (Pointer) (&vn);
sap = bsp->annot;
while (sap != NULL) {
if (sap->type == 1) {
prt = (SeqFeatPtr) sap->data;
while (prt != NULL) {
if (prt->data.choice == SEQFEAT_PROT) {
/* get SeqId in bioseq that matches SeqId used for location */
vn.data.ptrvalue = SeqIdWithinBioseq (bsp, prt->location);
diff = SeqLocAinB (prt->location, slp);
if (diff >= 0) {
if (diff < min) {
min = diff;
if (omdp->tempload == TL_NOT_TEMP) {
bspextra->protFeat = prt;
}
}
}
}
prt = prt->next;
}
}
sap = sap->next;
}
}
static void RecordOneFeature (BioseqExtraPtr bspextra, ObjMgrDataPtr omdp,
BioseqPtr bsp, ExtraIndexPtr exindx, SeqFeatPtr sfp,
Int4 left, Int4 right, Uint2 itemID, Boolean farloc,
Boolean ignore)
{
Char buf [129];
SMFeatBlockPtr curr;
Int2 i;
SMFeatItemPtr item;
Int4Ptr ivals;
SeqLocPtr loc;
SMFeatBlockPtr next;
Int2 numivals = 0;
Boolean single_interval;
SeqLocPtr slp = NULL;
Uint1 subtype = 0;
if (bspextra == NULL || omdp == NULL || bsp == NULL || exindx == NULL || sfp == NULL) return;
if (bspextra->featlisttail != NULL) {
/* just in case blocksize should was not set for some reason */
if (bspextra->blocksize < 1) {
bspextra->blocksize = 5;
}
curr = bspextra->featlisttail;
if (curr->index >= bspextra->blocksize) {
/* allocate next chunk in linked list of blocks */
next = (SMFeatBlockPtr) MemNew (sizeof (SMFeatBlock));
curr->next = next;
if (next != NULL) {
bspextra->featlisttail = next;
curr = next;
}
}
if (curr->index < bspextra->blocksize) {
/* allocate data block if not yet done for this chunk */
if (curr->data == NULL) {
curr->data = (SMFeatItemPtr) MemNew (sizeof (SMFeatItem) * (size_t) (bspextra->blocksize));
}
/* now record desired information about current feature */
if (curr->data != NULL) {
item = &(curr->data [curr->index]);
if (omdp->tempload == TL_NOT_TEMP) {
item->sfp = sfp;
item->sap = exindx->lastsap;
item->bsp = bsp;
}
FeatDefLabel (sfp, buf, sizeof (buf) - 1, OM_LABEL_CONTENT);
item->label = StringSaveNoNull (buf);
item->left = left;
item->right = right;
CheckSeqLocForPartial (sfp->location, &(item->partialL), &(item->partialR));
item->farloc = farloc;
item->strand = SeqLocStrand (sfp->location);
subtype = FindFeatDefType (sfp);
item->subtype = subtype;
item->itemID = itemID;
item->ignore = ignore;
/* record start/stop pairs of intervals on target bioseq */
single_interval = (Boolean) (item->subtype == FEATDEF_GENE ||
item->subtype == FEATDEF_PUB);
loc = SeqLocMerge (bsp, sfp->location, NULL, single_interval, TRUE, FALSE);
slp = NULL;
while ((slp = SeqLocFindNext (loc, slp)) != NULL) {
numivals++;
}
if (numivals > 0) {
ivals = MemNew (sizeof (Int4) * (numivals * 2));
item->ivals = ivals;
item->numivals = numivals;
slp = NULL;
i = 0;
while ((slp = SeqLocFindNext (loc, slp)) != NULL) {
ivals [i] = SeqLocStart (slp);
i++;
ivals [i] = SeqLocStop (slp);
i++;
}
}
SeqLocFree (loc);
}
/* increment count on current block */
(curr->index)++;
/* count all features, per bioseq and per entity */
(bspextra->numfeats)++;
(exindx->featcount)++;
/* count all gene, publication, and biosource features separately */
if (subtype == FEATDEF_GENE) {
(bspextra->numgenes)++;
}
if (subtype == FEATDEF_mRNA) {
(bspextra->nummRNAs)++;
}
if (subtype == FEATDEF_CDS) {
(bspextra->numCDSs)++;
}
if (subtype == FEATDEF_PUB) {
(bspextra->numpubs)++;
}
if (subtype == FEATDEF_BIOSRC) {
(bspextra->numorgs)++;
}
}
}
}
/* callback for recording features and descriptor, prot, and cdsOrRna information */
static Boolean RecordFeaturesInBioseqs (GatherContextPtr gcp)
{
BioseqPtr bsp = NULL;
BioseqExtraPtr bspextra;
BioseqSetPtr bssp = NULL;
Char buf [81];
Int2 count;
CharPtr ctmp;
Int4 diff;
ExtraIndexPtr exindx;
Int4 left;
CharPtr loclbl;
ObjMgrDataPtr omdp;
Int4 right;
SeqAnnotPtr sap = NULL;
ValNodePtr sdp = NULL;
SeqFeatPtr sfp = NULL;
SeqLocPtr slp;
SeqFeatPtr tmp;
Boolean usingLocalBsp = FALSE;
ValNode vn;
switch (gcp->thistype) {
case OBJ_BIOSEQ :
bsp = (BioseqPtr) gcp->thisitem;
if (bsp == NULL) return TRUE;
sdp = bsp->descr;
break;
case OBJ_BIOSEQSET :
bssp = (BioseqSetPtr) gcp->thisitem;
if (bssp == NULL) return TRUE;
sdp = bssp->descr;
break;
case OBJ_SEQANNOT :
sap = (SeqAnnotPtr) gcp->thisitem;
break;
case OBJ_SEQFEAT :
sfp = (SeqFeatPtr) gcp->thisitem;
break;
default :
return TRUE;
}
exindx = (ExtraIndexPtr) gcp->userdata;
if (exindx == NULL) return FALSE;
/* save bspItemID to support bioseq explore functions */
if (bsp != NULL) {
/* save last BioseqPtr to check first for appropriate bioseq */
exindx->lastbsp = bsp;
/* blocksize for new block based only on features packaged on bioseq */
exindx->lastbssp = NULL;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp != NULL) {
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) {
CreateBioseqExtraBlock (omdp, bsp);
bspextra = (BioseqExtraPtr) omdp->extradata;
}
if (bspextra != NULL) {
bspextra->bspItemID = gcp->itemID;
}
}
}
/* save last BioseqSetPtr to calculate blocksize from bioseq set and bioseq features,
features on bioseq set presumably being CDS or mRNA and applying only to nucleotides */
if (bssp != NULL) {
exindx->lastbssp = bssp;
}
/* count bioseq or bioseq set descriptors, to calculate lastDescrItemID */
if (sdp != NULL) {
SetDescriptorCounts (sdp, exindx, gcp->thisitem, gcp->thistype);
return TRUE;
}
/* save SeqAnnotPtr containing next features to be gathered */
if (sap != NULL) {
exindx->lastsap = sap;
return TRUE;
}
/* otherwise index features on every bioseq in entity */
if (sfp == NULL) return TRUE;
/* cds or rna reference stored in product bioseq's omdp.cdsOrRnaFeat,
best protein feature in omdp.protFeat (do before adding CDS) */
if (sfp->product != NULL) {
ProcessFeatureProducts (sfp, gcp->itemID, gcp);
}
bsp = FindAppropriateBioseq (sfp->location, exindx->lastbsp);
/* failure here can be due to SeqLoc that references far accession */
if (bsp == NULL) {
/* if far accession, find first local bioseq on any location interval */
bsp = FindFirstLocalBioseq (sfp->location);
/* report whether far accession was able to be handled */
FeatDefLabel (sfp, buf, sizeof (buf) - 1, OM_LABEL_CONTENT);
ctmp = SeqLocPrint (sfp->location);
loclbl = ctmp;
if (loclbl == NULL) {
loclbl = "?";
}
if (bsp == NULL) {
ErrPostItem (SEV_WARNING, 0, 0,
"SeqMgr indexing feature location problem - Feature: %s - Location [%s]",
buf, loclbl);
} else {
/*
ErrPostItem (SEV_INFO, 0, 0,
"SeqMgr indexing detected and handled far accession - Feature: %s - Location [%s]",
buf, loclbl);
*/
}
MemFree (ctmp);
if (bsp == NULL) return TRUE;
usingLocalBsp = TRUE;
}
/* assume subsequent features will be on this bioseq */
exindx->lastbsp = bsp;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL) return TRUE;
/* now prepare for adding feature to index */
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) {
CreateBioseqExtraBlock (omdp, bsp);
bspextra = (BioseqExtraPtr) omdp->extradata;
}
if (bspextra == NULL) return TRUE;
/* get extreme left and right extents of feature location */
if (usingLocalBsp) {
left = GetOffsetInFirstLocalBioseq (sfp->location, bsp, SEQLOC_LEFT_END);
if (left == -1) return TRUE;
right = GetOffsetInFirstLocalBioseq (sfp->location, bsp, SEQLOC_RIGHT_END);
if (right == -1) return TRUE;
} else {
left = GetOffsetInBioseq (sfp->location, bsp, SEQLOC_LEFT_END);
if (left == -1) return TRUE;
right = GetOffsetInBioseq (sfp->location, bsp, SEQLOC_RIGHT_END);
if (right == -1) return TRUE;
}
/* if indexing protein bioseq, store largest protein feature */
if (sfp->data.choice == SEQFEAT_PROT) {
vn.choice = SEQLOC_WHOLE;
vn.data.ptrvalue = (Pointer) bsp->id;
vn.next = NULL;
slp = (Pointer) &vn;
/* get SeqId in bioseq that matches SeqId used for location */
vn.data.ptrvalue = (Pointer) SeqIdWithinBioseq (bsp, sfp->location);
diff = SeqLocAinB (sfp->location, slp);
if (diff >= 0) {
if (diff < bspextra->min) {
bspextra->min = diff;
if (omdp->tempload == TL_NOT_TEMP) {
bspextra->protFeat = sfp;
}
}
}
}
/* add feature item to linked list of blocks */
if (bspextra->featlisthead == NULL) {
bspextra->featlisthead = (SMFeatBlockPtr) MemNew (sizeof (SMFeatBlock));
/* for first feature indexed on this bioseq, quickly see if few or many
additional features, since most features on a bioseq are packaged in
the same list, and most proteins only have one bioseq */
for (tmp = sfp, count = 0;
tmp != NULL && count < 50;
tmp = tmp->next, count++) continue;
/* extend count if above features were packaged on a bioseq set (presumably CDS or mRNA) */
if (exindx->lastbssp != NULL) {
for (sap = bsp->annot; sap != NULL; sap = sap->next) {
if (sap->type == 1) {
for (tmp = (SeqFeatPtr) sap->data;
tmp != NULL && count < 50;
tmp = tmp->next, count++) continue;
}
}
}
bspextra->blocksize = count;
}
if (bspextra->featlisttail == NULL) {
bspextra->featlisttail = bspextra->featlisthead;
}
if (bspextra->featlisttail != NULL) {
/* if feature spans origin, record with left < 0 */
if (left > right && bsp->topology == TOPOLOGY_CIRCULAR) {
left -= bsp->length;
}
RecordOneFeature (bspextra, omdp, bsp, exindx, sfp, left,
right, gcp->itemID, usingLocalBsp, FALSE);
/* record gene, publication, and biosource features twice if spanning the origin */
if (left < 0 && bsp->topology == TOPOLOGY_CIRCULAR) {
if (sfp->data.choice == SEQFEAT_GENE ||
sfp->data.choice == SEQFEAT_PUB ||
sfp->data.choice == SEQFEAT_BIOSRC) {
RecordOneFeature (bspextra, omdp, bsp, exindx, sfp, left + bsp->length,
right + bsp->length, gcp->itemID, usingLocalBsp, TRUE);
}
}
}
return TRUE;
}
/*****************************************************************************
*
* RecordSegmentsInBioseqs callback explores bioseq segments
*
*****************************************************************************/
static Boolean RecordSegmentsInBioseqs (GatherContextPtr gcp)
{
BioseqPtr bsp = NULL;
BioseqExtraPtr bspextra;
Char buf [80];
ExtraIndexPtr exindx;
Int4 from;
ObjMgrDataPtr omdp;
SMSeqIdxPtr segpartptr;
SeqIdPtr sid;
SeqIntPtr sipp;
SeqLocPtr slp = NULL;
Uint1 strand;
Int4 to;
switch (gcp->thistype) {
case OBJ_BIOSEQ :
bsp = (BioseqPtr) gcp->thisitem;
if (bsp == NULL) return TRUE;
break;
case OBJ_BIOSEQ_SEG :
slp = (SeqLocPtr) gcp->thisitem;
if (slp == NULL) return TRUE;
break;
default :
return TRUE;
}
exindx = (ExtraIndexPtr) gcp->userdata;
if (exindx == NULL) return FALSE;
if (bsp != NULL) {
if (bsp->repr == Seq_repr_seg) {
exindx->lastbsp = bsp;
} else {
exindx->lastbsp = NULL;
}
exindx->cumulative = 0;
return TRUE;
}
if (slp == NULL) return TRUE;
bsp = exindx->lastbsp;
if (bsp == NULL) return TRUE;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL) return TRUE;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) {
CreateBioseqExtraBlock (omdp, bsp);
bspextra = (BioseqExtraPtr) omdp->extradata;
}
if (bspextra == NULL) return TRUE;
if (slp->choice == SEQLOC_INT && slp->data.ptrvalue != NULL) {
sipp = (SeqIntPtr) (slp->data.ptrvalue);
from = sipp->from;
to = sipp->to;
strand = sipp->strand;
} else {
from = 0;
to = SeqLocLen (slp) - 1;
strand = SeqLocStrand (slp);
}
if (to - from + 1 < 1) return TRUE;
/* create and fill in SMSeqIdx element */
segpartptr = MemNew (sizeof (SMSeqIdx));
if (segpartptr != NULL) {
sid = SeqLocId (slp);
if (MakeReversedSeqIdString (sid, buf, sizeof (buf) - 1)) {
segpartptr->slp = AsnIoMemCopy (slp,
(AsnReadFunc) SeqLocAsnRead,
(AsnWriteFunc) SeqLocAsnWrite);
segpartptr->seqIdOfPart = StringSave (buf);
segpartptr->parentBioseq = bsp;
segpartptr->cumOffset = exindx->cumulative;
segpartptr->from = from;
segpartptr->to = to;
segpartptr->strand = strand;
segpartptr->itemID = gcp->itemID;
}
}
exindx->cumulative += (to - from + 1);
/* link into segparthead list of parts IDs */
if (bspextra->segparthead == NULL) {
bspextra->segparthead = segpartptr;
exindx->segpartail = segpartptr;
} else if (exindx->segpartail != NULL) {
exindx->segpartail->next = segpartptr;
exindx->segpartail = segpartptr;
}
return TRUE;
}
/*****************************************************************************
*
* SortFeatItemListByID callback sorts array into feature item table by itemID
* SortFeatItemListBySfp callback sorts array into feature item table by feature pointer
* SortFeatItemListByPos callback sorts array into feature item table by feature position
*
*****************************************************************************/
static int LIBCALLBACK SortFeatItemListByID (VoidPtr vp1, VoidPtr vp2)
{
SMFeatItemPtr PNTR spp1 = vp1;
SMFeatItemPtr PNTR spp2 = vp2;
SMFeatItemPtr sp1;
SMFeatItemPtr sp2;
if (spp1 == NULL || spp2 == NULL) return 0;
sp1 = *((SMFeatItemPtr PNTR) spp1);
sp2 = *((SMFeatItemPtr PNTR) spp2);
if (sp1 == NULL || sp2 == NULL) return 0;
if (sp1->itemID > sp2->itemID) {
return 1;
} else if (sp1->itemID < sp2->itemID) {
return -1;
}
return 0;
}
static int LIBCALLBACK SortFeatItemListBySfp (VoidPtr vp1, VoidPtr vp2)
{
SMFeatItemPtr PNTR spp1 = vp1;
SMFeatItemPtr PNTR spp2 = vp2;
SMFeatItemPtr sp1;
SMFeatItemPtr sp2;
if (spp1 == NULL || spp2 == NULL) return 0;
sp1 = *((SMFeatItemPtr PNTR) spp1);
sp2 = *((SMFeatItemPtr PNTR) spp2);
if (sp1 == NULL || sp2 == NULL) return 0;
if (sp1->sfp > sp2->sfp) {
return 1;
} else if (sp1->sfp < sp2->sfp) {
return -1;
}
return 0;
}
static int LIBCALLBACK SortFeatItemListByPos (VoidPtr vp1, VoidPtr vp2)
{
Int2 compare;
Int2 i;
Int2 j;
Int2 numivals;
SMFeatItemPtr PNTR spp1 = vp1;
SMFeatItemPtr PNTR spp2 = vp2;
SMFeatItemPtr sp1;
SMFeatItemPtr sp2;
if (spp1 == NULL || spp2 == NULL) return 0;
sp1 = *((SMFeatItemPtr PNTR) spp1);
sp2 = *((SMFeatItemPtr PNTR) spp2);
if (sp1 == NULL || sp2 == NULL) return 0;
/* feature with smallest left extreme is first */
if (sp1->left > sp2->left) {
return 1;
} else if (sp1->left < sp2->left) {
return -1;
/* reversing order so that longest feature is first */
} else if (sp1->right > sp2->right) {
return -1; /* was 1 */
} else if (sp1->right < sp2->right) {
return 1; /* was -1 */
/* given identical extremes, put gene features first */
} else if (sp1->subtype == FEATDEF_GENE) {
return -1;
} else if (sp2->subtype == FEATDEF_GENE) {
return 1;
/* then rna features */
} else if (FindFeatFromFeatDefType (sp1->subtype) == SEQFEAT_RNA) {
return -1;
} else if (FindFeatFromFeatDefType (sp2->subtype) == SEQFEAT_RNA) {
return 1;
/* then cds features */
} else if (sp1->subtype == FEATDEF_CDS) {
return -1;
} else if (sp2->subtype == FEATDEF_CDS) {
return 1;
}
/* next compare internal intervals */
numivals = MIN (sp1->numivals, sp2->numivals);
if (numivals > 1 && sp1->ivals != NULL && sp2->ivals != NULL) {
for (i = 0, j = 0; i < numivals; i++) {
/* check left interval */
if (sp1->ivals [i] > sp2->ivals [i]) {
return 1;
} else if (sp1->ivals [i] < sp2->ivals [i]) {
return -1;
}
j++;
/* check right interval */
if (sp1->ivals [i] > sp2->ivals [i]) {
return -1; /* was 1 */
} else if (sp1->ivals [i] < sp2->ivals [i]) {
return 1; /* was -1 */
}
j++;
}
}
/* next compare other feature subtypes */
if (sp1->subtype < sp2->subtype) {
return -1;
} else if (sp1->subtype > sp2->subtype) {
return 1;
}
/* then compare feature label */
compare = StringCmp (sp1->label, sp2->label);
if (compare > 0) {
return 1;
} else if (compare < 0) {
return -1;
}
/* last compare parent seq-annot */
if (sp1->sap > sp2->sap) {
return 1;
} else if (sp1->sap < sp2->sap) {
return -1;
}
return 0;
}
/*****************************************************************************
*
* IndexSegmentedParts callback builds index to speed up mapping
* of parts to segmented bioseqs
*
*****************************************************************************/
static int LIBCALLBACK SortSeqIdxArray (VoidPtr ptr1, VoidPtr ptr2)
{
Int2 compare;
SMSeqIdxPtr PNTR partp1 = ptr1;
SMSeqIdxPtr PNTR partp2 = ptr2;
SMSeqIdxPtr part1, part2;
if (partp1 == NULL || partp2 == NULL) return 0;
part1 = *((SMSeqIdxPtr PNTR) partp1);
part2 = *((SMSeqIdxPtr PNTR) partp2);
if (part1 == NULL || part2 == NULL) return 0;
compare = StringCmp (part1->seqIdOfPart, part2->seqIdOfPart);
if (compare > 0) {
return 1;
} else if (compare < 0) {
return -1;
}
return 0;
}
static void IndexSegmentedParts (SeqEntryPtr sep, BioseqPtr PNTR lastsegbsp)
{
BioseqPtr bsp;
BioseqExtraPtr bspextra;
BioseqSetPtr bssp;
Int2 i;
Int2 numsegs = 0;
Int4 cumulative = 0;
ObjMgrDataPtr omdp;
SMSeqIdxPtr PNTR partsByLoc;
SMSeqIdxPtr PNTR partsBySeqId;
SMSeqIdxPtr segpartptr;
if (sep == NULL) return;
if (IS_Bioseq_set (sep)) {
bssp = (BioseqSetPtr) sep->data.ptrvalue;
if (bssp == NULL) return;
for (sep = bssp->seq_set; sep != NULL; sep = sep->next) {
IndexSegmentedParts (sep, lastsegbsp);
}
if (bssp->_class == BioseqseqSet_class_segset && lastsegbsp != NULL) {
*lastsegbsp = NULL;
}
return;
}
if (! IS_Bioseq (sep)) return;
bsp = (BioseqPtr) sep->data.ptrvalue;
if (bsp == NULL) return;
/* check for raw part packaged with segmented bioseq */
if (bsp->repr == Seq_repr_raw && lastsegbsp != NULL && *lastsegbsp != NULL) {
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL) return;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) {
CreateBioseqExtraBlock (omdp, bsp);
bspextra = (BioseqExtraPtr) omdp->extradata;
}
if (bspextra == NULL) return;
/* now record segmented parent of raw part if all are packaged together */
bspextra->parentBioseq = *lastsegbsp;
return;
}
if (bsp->repr != Seq_repr_seg) return;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL) return;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) {
CreateBioseqExtraBlock (omdp, bsp);
bspextra = (BioseqExtraPtr) omdp->extradata;
}
if (bspextra == NULL) return;
if (lastsegbsp != NULL) {
*lastsegbsp = bsp;
}
for (segpartptr = bspextra->segparthead;
segpartptr != NULL;
segpartptr = segpartptr->next) {
numsegs++;
}
bspextra->numsegs = numsegs;
segpartptr = bspextra->segparthead;
if (numsegs < 1 || segpartptr == NULL) return;
partsByLoc = (SMSeqIdxPtr PNTR) MemNew (sizeof (SMSeqIdxPtr) * (numsegs + 1));
bspextra->partsByLoc = partsByLoc;
if (partsByLoc != NULL) {
i = 0;
while (i < numsegs && segpartptr != NULL) {
partsByLoc [i] = segpartptr;
segpartptr = segpartptr->next;
i++;
}
partsBySeqId = (SMSeqIdxPtr PNTR) MemNew (sizeof (SMSeqIdxPtr) * (numsegs + 1));
bspextra->partsBySeqId = partsBySeqId;
if (partsBySeqId != NULL) {
for (i = 0; i < numsegs; i++) {
partsBySeqId [i] = partsByLoc [i];
}
/* sort array by SeqId for binary search */
HeapSort ((Pointer) partsBySeqId, numsegs, sizeof (SMSeqIdxPtr), SortSeqIdxArray);
}
}
}
/*****************************************************************************
*
* IndexRecordedFeatures callback builds sorted arrays of features and genes
*
*****************************************************************************/
static void IndexRecordedFeatures (SeqEntryPtr sep)
{
BioseqPtr bsp;
BioseqExtraPtr bspextra;
BioseqSetPtr bssp;
SMFeatBlockPtr curr;
SMFeatItemPtr PNTR featsByID;
SMFeatItemPtr PNTR featsBySfp;
SMFeatItemPtr PNTR featsByPos;
SMFeatItemPtr PNTR genesByPos;
SMFeatItemPtr PNTR mRNAsByPos;
SMFeatItemPtr PNTR CDSsByPos;
SMFeatItemPtr PNTR pubsByPos;
SMFeatItemPtr PNTR orgsByPos;
Int4 i;
Int4 j;
SMFeatItemPtr item;
Int4 numfeats;
Int4 numgenes;
Int4 nummRNAs;
Int4 numCDSs;
Int4 numpubs;
Int4 numorgs;
ObjMgrDataPtr omdp;
if (sep == NULL) return;
if (IS_Bioseq_set (sep)) {
bssp = (BioseqSetPtr) sep->data.ptrvalue;
if (bssp == NULL) return;
for (sep = bssp->seq_set; sep != NULL; sep = sep->next) {
IndexRecordedFeatures (sep);
}
return;
}
if (! IS_Bioseq (sep)) return;
bsp = (BioseqPtr) sep->data.ptrvalue;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL) return;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return;
numfeats = bspextra->numfeats;
numgenes = bspextra->numgenes;
nummRNAs = bspextra->nummRNAs;
numCDSs = bspextra->numCDSs;
numpubs = bspextra->numpubs;
numorgs = bspextra->numorgs;
curr = bspextra->featlisthead;
if (bspextra->numfeats > 0 && curr != NULL) {
/* build array of pointers into feature items */
featsByID = (SMFeatItemPtr PNTR) MemNew (sizeof (SMFeatItemPtr) * (numfeats + 1));
bspextra->featsByID = featsByID;
if (featsByID != NULL) {
i = 0;
j = 0;
while (i < numfeats && curr != NULL) {
if (j >= curr->index || j >= bspextra->blocksize) {
j = 0;
curr = curr->next;
}
if (curr != NULL && j < curr->index && curr->data != NULL) {
featsByID [i] = &(curr->data [j]);
i++;
j++;
}
}
if (i < numfeats) {
ErrPostEx (SEV_WARNING, 0, 0, "SeqMgr indexing feature table build problem");
}
featsBySfp = (SMFeatItemPtr PNTR) MemNew (sizeof (SMFeatItemPtr) * (numfeats + 1));
bspextra->featsBySfp = featsBySfp;
if (featsBySfp != NULL) {
for (i = 0; i < numfeats; i++) {
featsBySfp [i] = featsByID [i];
}
/* sort all features by SeqFeatPtr value */
HeapSort ((VoidPtr) featsBySfp, (size_t) numfeats, sizeof (SMFeatItemPtr), SortFeatItemListBySfp);
}
featsByPos = (SMFeatItemPtr PNTR) MemNew (sizeof (SMFeatItemPtr) * (numfeats + 1));
bspextra->featsByPos = featsByPos;
if (featsByPos != NULL) {
for (i = 0; i < numfeats; i++) {
featsByPos [i] = featsByID [i];
}
/* sort all features by feature location on bioseq */
HeapSort ((VoidPtr) featsByPos, (size_t) numfeats, sizeof (SMFeatItemPtr), SortFeatItemListByPos);
for (i = 0; i < numfeats; i++) {
item = featsByPos [i];
if (item != NULL) {
item->index = i;
}
}
/* build subarray of sorted gene features for lookup by overlap */
if (numgenes > 0) {
genesByPos = (SMFeatItemPtr PNTR) MemNew (sizeof (SMFeatItemPtr) * (numgenes + 1));
bspextra->genesByPos = genesByPos;
if (genesByPos != NULL) {
i = 0;
j = 0;
while (i < numfeats && j < numgenes) {
item = featsByPos [i];
if (item->subtype == FEATDEF_GENE) {
genesByPos [j] = item;
j++;
}
i++;
}
}
}
/* build subarray of sorted mRNA features for lookup by overlap */
if (nummRNAs > 0) {
mRNAsByPos = (SMFeatItemPtr PNTR) MemNew (sizeof (SMFeatItemPtr) * (nummRNAs + 1));
bspextra->mRNAsByPos = mRNAsByPos;
if (mRNAsByPos != NULL) {
i = 0;
j = 0;
while (i < numfeats && j < nummRNAs) {
item = featsByPos [i];
if (item->subtype == FEATDEF_mRNA) {
mRNAsByPos [j] = item;
j++;
}
i++;
}
}
}
/* build subarray of sorted CDS features for lookup by overlap */
if (numCDSs > 0) {
CDSsByPos = (SMFeatItemPtr PNTR) MemNew (sizeof (SMFeatItemPtr) * (numCDSs + 1));
bspextra->CDSsByPos = CDSsByPos;
if (CDSsByPos != NULL) {
i = 0;
j = 0;
while (i < numfeats && j < numCDSs) {
item = featsByPos [i];
if (item->subtype == FEATDEF_CDS) {
CDSsByPos [j] = item;
j++;
}
i++;
}
}
}
/* build subarray of sorted publication features for lookup by overlap */
if (numpubs > 0) {
pubsByPos = (SMFeatItemPtr PNTR) MemNew (sizeof (SMFeatItemPtr) * (numpubs + 1));
bspextra->pubsByPos = pubsByPos;
if (pubsByPos != NULL) {
i = 0;
j = 0;
while (i < numfeats && j < numpubs) {
item = featsByPos [i];
if (item->subtype == FEATDEF_PUB) {
pubsByPos [j] = item;
j++;
}
i++;
}
}
}
/* build subarray of sorted biosource features for lookup by overlap */
if (numorgs > 0) {
orgsByPos = (SMFeatItemPtr PNTR) MemNew (sizeof (SMFeatItemPtr) * (numorgs + 1));
bspextra->orgsByPos = orgsByPos;
if (orgsByPos != NULL) {
i = 0;
j = 0;
while (i < numfeats && j < numorgs) {
item = featsByPos [i];
if (item->subtype == FEATDEF_BIOSRC) {
orgsByPos [j] = item;
j++;
}
i++;
}
}
}
}
}
}
}
/*****************************************************************************
*
* IndexFeaturesOnEntity makes feature pointers across all Bioseqs in entity
*
*****************************************************************************/
static void IndexFeaturesOnEntity (SeqEntryPtr sep, SMFeatItemPtr PNTR featsByID, Int4Ptr countP)
{
BioseqPtr bsp;
BioseqExtraPtr bspextra;
BioseqSetPtr bssp;
Int4 count;
Int4 i;
Int4 numfeats;
ObjMgrDataPtr omdp;
if (sep == NULL || featsByID == NULL || countP == NULL) return;
if (IS_Bioseq_set (sep)) {
bssp = (BioseqSetPtr) sep->data.ptrvalue;
if (bssp == NULL) return;
for (sep = bssp->seq_set; sep != NULL; sep = sep->next) {
IndexFeaturesOnEntity (sep, featsByID, countP);
}
return;
}
if (! IS_Bioseq (sep)) return;
bsp = (BioseqPtr) sep->data.ptrvalue;
if (bsp == NULL) return;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL) return;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return;
numfeats = bspextra->numfeats;
if (bspextra->featsByID != NULL || numfeats > 0) {
count = *countP;
for (i = 0; i < numfeats; i++, count++) {
featsByID [count] = bspextra->featsByID [i];
}
*countP = count;
}
}
/*****************************************************************************
*
* SeqMgrReindexBioseqExtraData refreshes internal indices for rapid retrieval
*
*****************************************************************************/
NLM_EXTERN Uint2 LIBCALL SeqMgrIndexFeatures (Uint2 entityID, Pointer ptr)
{
BioseqExtraPtr bspextra;
Int4 count;
ExtraIndex exind;
SMFeatItemPtr PNTR featsByID;
GatherScope gs;
BioseqPtr lastsegbsp = NULL;
SeqEntryPtr oldscope;
ObjMgrDataPtr omdp;
SeqEntryPtr sep;
if (entityID == 0) {
entityID = ObjMgrGetEntityIDForPointer (ptr);
}
if (entityID == 0) return 0;
/* reset any existing index data on all bioseqs in entity */
SeqMgrClearFeatureIndexes (entityID, NULL);
/* want to scope to bioseqs within the entity, to allow for colliding IDs */
sep = SeqMgrGetTopSeqEntryForEntity (entityID);
/* make top SeqEntry if only Bioseq or BioseqSet was read */
if (sep == NULL) {
omdp = ObjMgrGetData (entityID);
if (omdp != NULL) {
if (omdp->datatype == OBJ_BIOSEQ || omdp->datatype == OBJ_BIOSEQSET) {
sep = SeqEntryNew ();
if (sep != NULL) {
if (omdp->datatype == OBJ_BIOSEQ) {
sep->choice = 1;
sep->data.ptrvalue = omdp->dataptr;
SeqMgrSeqEntry (SM_BIOSEQ, omdp->dataptr, sep);
} else {
sep->choice = 2;
sep->data.ptrvalue = omdp->dataptr;
SeqMgrSeqEntry (SM_BIOSEQSET, omdp->dataptr, sep);
}
}
sep = GetTopSeqEntryForEntityID (entityID);
}
}
}
if (sep == NULL) return 0;
/* clean up many old-style ASN.1 problems without changing structure */
BasicSeqEntryCleanup (sep);
/* set scope for FindAppropriateBioseq, FindFirstLocalBioseq */
oldscope = SeqEntrySetScope (sep);
/* gather all segmented locations */
exind.lastbsp = NULL;
exind.lastsap = NULL;
exind.lastbssp = NULL;
exind.segpartail = NULL;
exind.descrcount = 0;
exind.featcount = 0;
MemSet ((Pointer) (&gs), 0, sizeof (GatherScope));
MemSet ((Pointer) (gs.ignore), (int) (TRUE), (size_t) (OBJ_MAX * sizeof (Boolean)));
gs.ignore [OBJ_BIOSEQ] = FALSE;
gs.ignore [OBJ_BIOSEQSET] = FALSE;
gs.ignore [OBJ_BIOSEQ_SEG] = FALSE;
gs.scope = sep;
GatherEntity (entityID, (Pointer) (&exind), RecordSegmentsInBioseqs, &gs);
/* build indexes to speed mapping of parts to segmented bioseq */
lastsegbsp = NULL;
IndexSegmentedParts (sep, &lastsegbsp);
/* now gather to get descriptor itemID counts on each bioseq or bioseq set,
and record features on the bioseq indicated by the feature location */
exind.lastbsp = NULL;
exind.lastsap = NULL;
exind.lastbssp = NULL;
exind.segpartail = NULL;
exind.descrcount = 0;
exind.featcount = 0;
MemSet ((Pointer) (&gs), 0, sizeof (GatherScope));
MemSet ((Pointer) (gs.ignore), (int) (TRUE), (size_t) (OBJ_MAX * sizeof (Boolean)));
gs.ignore [OBJ_BIOSEQ] = FALSE;
gs.ignore [OBJ_BIOSEQSET] = FALSE;
gs.ignore [OBJ_SEQANNOT] = FALSE;
gs.ignore [OBJ_SEQFEAT] = FALSE;
gs.scope = sep;
GatherEntity (entityID, (Pointer) (&exind), RecordFeaturesInBioseqs, &gs);
/* finish building array of sorted features on each indexed bioseq */
IndexRecordedFeatures (sep);
/* resetset scope used to limit FindAppropriateBioseq, FindFirstLocalBioseq */
SeqEntrySetScope (oldscope);
/* stamp top of entity with time of indexing */
omdp = ObjMgrGetData (entityID);
if (omdp != NULL) {
omdp->indexed = GetSecs ();
/* master index of feature if top of entity is not a Bioseq */
if (omdp->extradata == NULL && omdp->datatype != OBJ_BIOSEQ) {
CreateBioseqExtraBlock (omdp, NULL);
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra != NULL) {
/* make master index of features by itemID at top of entity */
if (exind.featcount > 0) {
featsByID = (SMFeatItemPtr PNTR) MemNew (sizeof (SMFeatItemPtr) * (exind.featcount + 1));
if (featsByID != NULL) {
count = 0;
IndexFeaturesOnEntity (sep, featsByID, &count);
/* sort all features on entity-wide list by itemID */
HeapSort ((VoidPtr) featsByID, (size_t) count, sizeof (SMFeatItemPtr), SortFeatItemListByID);
bspextra->featsByID = featsByID;
bspextra->numfeats = count;
}
}
}
}
}
return entityID;
}
/*****************************************************************************
*
* SeqMgrIsBioseqIndexed checks for presence of time of indexing stamp
*
*****************************************************************************/
NLM_EXTERN time_t LIBCALL SeqMgrFeaturesAreIndexed (Uint2 entityID)
{
ObjMgrDataPtr omdp;
if (entityID == 0) return 0;
omdp = ObjMgrGetData (entityID);
if (omdp == NULL) return 0;
return omdp->indexed;
}
/*****************************************************************************
*
* SeqMgrGetBestProteinFeature and SeqMgrGetCDSgivenProduct take a protein
* bioseq to get the best protein feature or encoding CDS
* SeqMgrGetRNAgivenProduct takes an mRNA (cDNA) bioseq and gets encoding mRNA
* feature on the genomic bioseq
*
*****************************************************************************/
static void SetContextForFeature (SeqFeatPtr sfp, SeqMgrFeatContext PNTR context, ObjMgrDataPtr omdp)
{
SMFeatItemPtr best;
if (sfp == NULL || context == NULL || omdp == NULL) return;
best = SeqMgrFindSMFeatItemPtr (sfp);
if (best == NULL) return;
context->entityID = ObjMgrGetEntityIDForPointer (omdp->dataptr);
context->itemID = best->itemID;
context->sfp = best->sfp;
context->sap = best->sap;
context->bsp = best->bsp;
context->label = best->label;
context->left = best->left;
context->right = best->right;
context->partialL = best->partialL;
context->partialR = best->partialR;
context->farloc = best->farloc;
context->strand = best->strand;
context->seqfeattype = FindFeatFromFeatDefType (best->subtype);
context->featdeftype = best->subtype;
context->numivals = best->numivals;
context->ivals = best->ivals;
context->userdata = NULL;
context->omdp = (Pointer) omdp;
context->index = best->index + 1;
}
NLM_EXTERN SeqFeatPtr LIBCALL SeqMgrGetBestProteinFeature (BioseqPtr bsp,
SeqMgrFeatContext PNTR context)
{
BioseqExtraPtr bspextra;
ObjMgrDataPtr omdp;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return NULL;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return NULL;
SetContextForFeature (bspextra->protFeat, context, omdp);
return bspextra->protFeat;
}
NLM_EXTERN SeqFeatPtr LIBCALL SeqMgrGetCDSgivenProduct (BioseqPtr bsp,
SeqMgrFeatContext PNTR context)
{
BioseqExtraPtr bspextra;
ObjMgrDataPtr omdp;
SeqFeatPtr sfp;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return NULL;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return NULL;
sfp = bspextra->cdsOrRnaFeat;
if (sfp == NULL || sfp->data.choice != SEQFEAT_CDREGION) return NULL;
SetContextForFeature (sfp, context, omdp);
return sfp;
}
NLM_EXTERN SeqFeatPtr LIBCALL SeqMgrGetRNAgivenProduct (BioseqPtr bsp,
SeqMgrFeatContext PNTR context)
{
BioseqExtraPtr bspextra;
ObjMgrDataPtr omdp;
SeqFeatPtr sfp;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return NULL;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return NULL;
sfp = bspextra->cdsOrRnaFeat;
if (sfp == NULL || sfp->data.choice != SEQFEAT_RNA) return NULL;
SetContextForFeature (sfp, context, omdp);
return sfp;
}
/*****************************************************************************
*
* SeqMgrGetGeneXref, SeqMgrGeneIsSuppressed, SeqMgrGetOverlappingGene,
* and SeqMgrGetOverlappingPub
*
*****************************************************************************/
static Boolean HasNoText (CharPtr str)
{
Char ch;
if (str != NULL) {
ch = *str;
while (ch != '\0') {
if (ch > ' ') {
return FALSE;
}
str++;
ch = *str;
}
}
return TRUE;
}
NLM_EXTERN GeneRefPtr LIBCALL SeqMgrGetGeneXref (SeqFeatPtr sfp)
{
GeneRefPtr grp = NULL;
SeqFeatXrefPtr xref;
if (sfp == NULL) return NULL;
xref = sfp->xref;
while (xref != NULL && xref->data.choice != SEQFEAT_GENE) {
xref = xref->next;
}
if (xref != NULL) {
grp = (GeneRefPtr) xref->data.value.ptrvalue;
}
return grp;
}
NLM_EXTERN Boolean LIBCALL SeqMgrGeneIsSuppressed (GeneRefPtr grp)
{
if (grp == NULL) return FALSE;
if (grp != NULL && HasNoText (grp->locus) && HasNoText (grp->allele) &&
HasNoText (grp->desc) && HasNoText (grp->maploc) &&
grp->db == NULL && grp->syn == NULL) return TRUE;
return FALSE;
}
static SeqFeatPtr SeqMgrGetBestOverlappingFeat (SeqLocPtr slp, Uint2 subtype,
SMFeatItemPtr PNTR array, Int4 num, Int4Ptr pos,
SeqMgrFeatContext PNTR context)
{
SMFeatItemPtr best = NULL;
BioseqPtr bsp;
BioseqExtraPtr bspextra;
Int4 diff;
SMFeatItemPtr feat;
Uint2 index = 0;
Int4 L;
Int4 left;
Int4 max;
Int4 mid;
ObjMgrDataPtr omdp;
Int4 R;
Int4 right;
Uint1 strand;
if (pos != NULL) {
*pos = 0;
}
if (slp == NULL) return NULL;
bsp = FindAppropriateBioseq (slp, NULL);
if (bsp == NULL) {
bsp = FindFirstLocalBioseq (slp);
}
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return NULL;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return NULL;
switch (subtype) {
case FEATDEF_GENE :
array = bspextra->genesByPos;
num = bspextra->numgenes;
break;
case FEATDEF_CDS :
array = bspextra->CDSsByPos;
num = bspextra->numCDSs;
break;
case FEATDEF_mRNA :
array = bspextra->mRNAsByPos;
num = bspextra->nummRNAs;
break;
case FEATDEF_PUB :
array = bspextra->pubsByPos;
num = bspextra->numpubs;
break;
case FEATDEF_BIOSRC :
array = bspextra->orgsByPos;
num = bspextra->numorgs;
break;
default :
break;
}
if (array == NULL || num < 1) return NULL;
left = GetOffsetInBioseq (slp, bsp, SEQLOC_LEFT_END);
if (left == -1) return NULL;
right = GetOffsetInBioseq (slp, bsp, SEQLOC_RIGHT_END);
if (right == -1) return NULL;
/* if feature spans origin, normalize with left < 0 */
if (left > right && bsp->topology == TOPOLOGY_CIRCULAR) {
left -= bsp->length;
}
/* binary search to leftmost candidate within the xxxByPos array */
L = 0;
R = num - 1;
while (L < R) {
mid = (L + R) / 2;
feat = array [mid];
if (feat != NULL && feat->right < left) {
L = mid + 1;
} else {
R = mid;
}
}
/* linear scan to smallest covering gene, publication, or biosource */
best = NULL;
index = 0;
feat = array [R];
max = INT4_MAX;
strand = SeqLocStrand (slp);
while (R < num && feat != NULL && feat->left <= right) {
if (feat->left <= left && feat->right >= right) {
if (feat->strand == strand ||
strand == Seq_strand_unknown ||
feat->strand == Seq_strand_unknown) {
diff = (left - feat->left) + (feat->right - right);
if (diff < max) {
best = feat;
index = R;
max = diff;
}
}
}
R++;
feat = array [R];
}
if (best != NULL) {
if (pos != NULL) {
*pos = index + 1;
}
if (context != NULL) {
context->entityID = ObjMgrGetEntityIDForPointer (omdp->dataptr);
context->itemID = best->itemID;
context->sfp = best->sfp;
context->sap = best->sap;
context->bsp = best->bsp;
context->label = best->label;
context->left = best->left;
context->right = best->right;
context->partialL = best->partialL;
context->partialR = best->partialR;
context->farloc = best->farloc;
context->strand = best->strand;
context->seqfeattype = FindFeatFromFeatDefType (best->subtype);
context->featdeftype = best->subtype;
context->numivals = best->numivals;
context->ivals = best->ivals;
context->userdata = NULL;
context->omdp = (Pointer) omdp;
context->index = best->index + 1;
}
return best->sfp;
}
return NULL;
}
NLM_EXTERN SeqFeatPtr LIBCALL SeqMgrGetOverlappingGene (SeqLocPtr slp, SeqMgrFeatContext PNTR context)
{
return SeqMgrGetBestOverlappingFeat (slp, FEATDEF_GENE, NULL, 0, NULL, context);
}
NLM_EXTERN SeqFeatPtr LIBCALL SeqMgrGetOverlappingmRNA (SeqLocPtr slp, SeqMgrFeatContext PNTR context)
{
return SeqMgrGetBestOverlappingFeat (slp, FEATDEF_mRNA, NULL, 0, NULL, context);
}
NLM_EXTERN SeqFeatPtr LIBCALL SeqMgrGetOverlappingCDS (SeqLocPtr slp, SeqMgrFeatContext PNTR context)
{
return SeqMgrGetBestOverlappingFeat (slp, FEATDEF_CDS, NULL, 0, NULL, context);
}
NLM_EXTERN SeqFeatPtr LIBCALL SeqMgrGetOverlappingPub (SeqLocPtr slp, SeqMgrFeatContext PNTR context)
{
return SeqMgrGetBestOverlappingFeat (slp, FEATDEF_PUB, NULL, 0, NULL, context);
}
NLM_EXTERN SeqFeatPtr LIBCALL SeqMgrGetOverlappingSource (SeqLocPtr slp, SeqMgrFeatContext PNTR context)
{
return SeqMgrGetBestOverlappingFeat (slp, FEATDEF_BIOSRC, NULL, 0, NULL, context);
}
/*****************************************************************************
*
* SeqMgrBuildFeatureIndex builds a sorted array index for any feature type
* (gene, mRNA, CDS, publication, and biosource have built-in arrays)
* SeqMgrGetOverlappingFeature uses the array to find feature overlap,
* returning the position in the index
* SeqMgrGetFeatureInIndex gets an arbitrary feature indexed by the array
*
*****************************************************************************/
NLM_EXTERN VoidPtr LIBCALL SeqMgrBuildFeatureIndex (BioseqPtr bsp, Int4Ptr num,
Uint1 seqFeatChoice, Uint1 featDefChoice)
{
SMFeatItemPtr PNTR array;
BioseqExtraPtr bspextra;
SMFeatItemPtr PNTR featsByPos;
Int4 i;
Int4 j;
SMFeatItemPtr item;
Int4 numfeats;
Int4 numitems;
ObjMgrDataPtr omdp;
Uint1 seqfeattype;
if (num != NULL) {
*num = 0;
}
if (bsp == NULL) return NULL;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return NULL;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return NULL;
featsByPos = bspextra->featsByPos;
numfeats = bspextra->numfeats;
if (featsByPos == NULL || numfeats < 1) return NULL;
for (i = 0, numitems = 0; i < numfeats; i++) {
item = featsByPos [i];
if (item->subtype == seqFeatChoice) {
numitems++;
}
}
if (numitems < 1) return NULL;
array = (SMFeatItemPtr PNTR) MemNew (sizeof (SMFeatItemPtr) * (numitems + 1));
if (array == NULL) return NULL;
i = 0;
j = 0;
while (i < numfeats && j < numitems) {
item = featsByPos [i];
seqfeattype = FindFeatFromFeatDefType (item->subtype);
if ((seqFeatChoice == 0 || seqfeattype == seqFeatChoice) &&
(featDefChoice == 0 || item->subtype == featDefChoice)) {
array [j] = item;
j++;
}
i++;
}
if (num != NULL) {
*num = numitems;
}
return (VoidPtr) array;
}
NLM_EXTERN SeqFeatPtr LIBCALL SeqMgrGetOverlappingFeature (SeqLocPtr slp, VoidPtr featarray,
Int4 numfeats, Int4Ptr position,
SeqMgrFeatContext PNTR context)
{
return SeqMgrGetBestOverlappingFeat (slp, 0, (SMFeatItemPtr PNTR) featarray,
numfeats, position, context);
}
NLM_EXTERN SeqFeatPtr LIBCALL SeqMgrGetFeatureInIndex (BioseqPtr bsp, VoidPtr featarray,
Int4 numfeats, Uint2 index,
SeqMgrFeatContext PNTR context)
{
SMFeatItemPtr PNTR array;
SeqFeatPtr curr;
Uint2 entityID;
SMFeatItemPtr item = NULL;
ObjMgrDataPtr omdp;
if (bsp == NULL || featarray == NULL || numfeats < 1) return NULL;
if (index < 1 || index > numfeats) return NULL;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return NULL;
array = (SMFeatItemPtr PNTR) featarray;
item = array [index - 1];
if (item == NULL) return NULL;
entityID = ObjMgrGetEntityIDForPointer (omdp->dataptr);
curr = item->sfp;
if (curr != NULL && context != NULL && (! item->ignore)) {
context->entityID = entityID;
context->itemID = item->itemID;
context->sfp = curr;
context->sap = item->sap;
context->bsp = item->bsp;
context->label = item->label;
context->left = item->left;
context->right = item->right;
context->partialL = item->partialL;
context->partialR = item->partialR;
context->farloc = item->farloc;
context->strand = item->strand;
context->seqfeattype = FindFeatFromFeatDefType (item->subtype);
context->featdeftype = item->subtype;
context->numivals = item->numivals;
context->ivals = item->ivals;
context->userdata = NULL;
context->omdp = (Pointer) omdp;
context->index = item->index + 1;
}
return curr;
}
/*****************************************************************************
*
* SeqMgrGetNextDescriptor and SeqMgrGetNextFeature
*
*****************************************************************************/
NLM_EXTERN ValNodePtr LIBCALL SeqMgrGetNextDescriptor (BioseqPtr bsp, ValNodePtr curr,
Uint1 seqDescChoice,
SeqMgrDescContext PNTR context)
{
BioseqSetPtr bssp;
Uint2 entityID;
ObjMgrDataPtr omdp;
SeqEntryPtr sep;
ValNode vn;
if (context == NULL) return NULL;
/* if curr is NULL, initialize context fields (in user's stack) */
if (curr == NULL) {
if (bsp == NULL) return NULL;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return NULL;
context->omdp = (Pointer) omdp;
context->itemID = omdp->lastDescrItemID;
context->index = 0;
context->level = 0;
/* start curr just before beginning of bioseq descriptor list */
curr = &vn;
vn.choice = 0;
vn.data.ptrvalue = 0;
vn.next = bsp->descr;
}
omdp = (ObjMgrDataPtr) context->omdp;
if (omdp == NULL) return NULL;
entityID = ObjMgrGetEntityIDForPointer (omdp->dataptr);
sep = ObjMgrGetChoiceForData (omdp->dataptr);
/* now look for next appropriate descriptor after curr in current chain */
while (curr != NULL) {
curr = curr->next;
if (curr != NULL) {
(context->itemID)++;
(context->index)++;
if (seqDescChoice == 0 || curr->choice == seqDescChoice) {
context->entityID = entityID;
context->sdp = curr;
context->sep = sep;
context->seqdesctype = curr->choice;
context->userdata = NULL;
context->omdp = (Pointer) omdp;
return curr;
}
}
}
/* now go up omdp chain looking for next descriptor */
while (curr == NULL) {
omdp = SeqMgrGetOmdpForPointer (omdp->parentptr);
if (omdp == NULL) return NULL;
/* update current omdp in context */
context->omdp = (Pointer) omdp;
context->itemID = omdp->lastDescrItemID;
switch (omdp->datatype) {
case OBJ_BIOSEQ :
bsp = (BioseqPtr) omdp->dataptr;
curr = bsp->descr;
break;
case OBJ_BIOSEQSET :
bssp = (BioseqSetPtr) omdp->dataptr;
curr = bssp->descr;
break;
default :
break;
}
sep = ObjMgrGetChoiceForData (omdp->dataptr);
(context->level)++;
/* now look for first appropriate descriptor in current chain */
while (curr != NULL) {
(context->itemID)++;
(context->index)++;
if (seqDescChoice == 0 || curr->choice == seqDescChoice) {
context->entityID = entityID;
context->sdp = curr;
context->sep = sep;
context->seqdesctype = curr->choice;
context->userdata = NULL;
context->omdp = (Pointer) omdp;
return curr;
}
curr = curr->next;
}
}
return curr;
}
NLM_EXTERN SeqFeatPtr LIBCALL SeqMgrGetNextFeature (BioseqPtr bsp, SeqFeatPtr curr,
Uint1 seqFeatChoice, Uint1 featDefChoice,
SeqMgrFeatContext PNTR context)
{
BioseqExtraPtr bspextra;
Uint2 entityID;
SMFeatItemPtr PNTR featsByPos;
Uint2 i;
SMFeatItemPtr item;
ObjMgrDataPtr omdp;
Uint1 seqfeattype;
if (context == NULL) return NULL;
/* if curr is NULL, initialize context fields (in user's stack) */
if (curr == NULL) {
if (bsp == NULL) return NULL;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return NULL;
context->omdp = (Pointer) omdp;
context->index = 0;
}
omdp = (ObjMgrDataPtr) context->omdp;
if (omdp == NULL) return NULL;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return NULL;
featsByPos = bspextra->featsByPos;
if (featsByPos == NULL || bspextra->numfeats < 1) return NULL;
entityID = ObjMgrGetEntityIDForPointer (omdp->dataptr);
i = context->index;
/* now look for next appropriate feature */
while (i < bspextra->numfeats) {
item = featsByPos [i];
if (item != NULL) {
curr = item->sfp;
i++;
if (curr != NULL) {
seqfeattype = FindFeatFromFeatDefType (item->subtype);
if ((seqFeatChoice == 0 || seqfeattype == seqFeatChoice) &&
(featDefChoice == 0 || item->subtype == featDefChoice) &&
(! item->ignore)) {
context->entityID = entityID;
context->itemID = item->itemID;
context->sfp = curr;
context->sap = item->sap;
context->bsp = item->bsp;
context->label = item->label;
context->left = item->left;
context->right = item->right;
context->partialL = item->partialL;
context->partialR = item->partialR;
context->farloc = item->farloc;
context->strand = item->strand;
context->seqfeattype = seqfeattype;
context->featdeftype = item->subtype;
context->numivals = item->numivals;
context->ivals = item->ivals;
context->userdata = NULL;
context->omdp = (Pointer) omdp;
context->index = item->index + 1;
return curr;
}
}
}
}
return NULL;
}
/*****************************************************************************
*
* SeqMgrExploreBioseqs, SeqMgrExploreDescriptors, and SeqMgrExploreFeatures
*
*****************************************************************************/
static Boolean JustExamineBioseqs (SeqEntryPtr sep, BioseqSetPtr bssp,
SeqMgrBioseqContextPtr context,
SeqMgrBioseqExploreProc userfunc,
Boolean nucs, Boolean prots, Boolean parts)
{
BioseqPtr bsp;
BioseqExtraPtr bspextra;
ObjMgrDataPtr omdp;
if (sep == NULL || context == NULL || userfunc == NULL) return FALSE;
if (IS_Bioseq (sep)) {
bsp = (BioseqPtr) sep->data.ptrvalue;
if (bsp == NULL) return TRUE;
/* check for desired molecule type */
if (ISA_na (bsp->mol) && (! nucs)) return TRUE;
if (ISA_aa (bsp->mol) && (! prots)) return TRUE;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return TRUE;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return TRUE;
context->itemID = bspextra->bspItemID;
context->bsp = bsp;
context->sep = sep;
context->bssp = bssp;
context->omdp = omdp;
(context->index)++;
/* continue until user function returns FALSE, then exit all recursions */
if (! userfunc (bsp, context)) return FALSE;
return TRUE;
}
if (IS_Bioseq_set (sep)) {
bssp = (BioseqSetPtr) sep->data.ptrvalue;
if (bssp == NULL) return TRUE;
/* check to see if parts should be explored */
if (bssp->_class == BioseqseqSet_class_parts && (! parts)) return TRUE;
/* recursively explore bioseq set until user function returns FALSE */
for (sep = bssp->seq_set; sep != NULL; sep = sep->next) {
if (! JustExamineBioseqs (sep, bssp, context, userfunc, nucs, prots, parts)) return FALSE;
}
}
return TRUE;
}
NLM_EXTERN Boolean LIBCALL SeqMgrExploreBioseqs (Uint2 entityID, Pointer ptr, Pointer userdata,
SeqMgrBioseqExploreProc userfunc,
Boolean nucs, Boolean prots, Boolean parts)
{
SeqMgrBioseqContext context;
SeqEntryPtr sep;
if (entityID == 0) {
entityID = ObjMgrGetEntityIDForPointer (ptr);
}
if (entityID == 0) return 0;
sep = SeqMgrGetTopSeqEntryForEntity (entityID);
if (sep == NULL) return FALSE;
if (userfunc == NULL) return FALSE;
context.entityID = entityID;
context.index = 0;
context.userdata = userdata;
/* recursive call to explore SeqEntry and pass appropriate bioseqs to user */
JustExamineBioseqs (sep, NULL, &context, userfunc, nucs, prots, parts);
return TRUE;
}
NLM_EXTERN Boolean LIBCALL SeqMgrExploreSegments (BioseqPtr bsp, Pointer userdata,
SeqMgrSegmentExploreProc userfunc)
{
BioseqExtraPtr bspextra;
SeqMgrSegmentContext context;
Uint2 entityID;
Uint2 i;
ObjMgrDataPtr omdp;
SMSeqIdxPtr PNTR partsByLoc;
SMSeqIdxPtr segpartptr;
SeqLocPtr slp;
if (bsp == NULL || bsp->repr != Seq_repr_seg) return FALSE;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return FALSE;
if (userfunc == NULL) return FALSE;
entityID = ObjMgrGetEntityIDForPointer (omdp->dataptr);
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return FALSE;
partsByLoc = bspextra->partsByLoc;
if (partsByLoc == NULL || bspextra->numsegs < 1) return FALSE;
for (i = 0; i < bspextra->numsegs; i++) {
segpartptr = partsByLoc [i];
if (segpartptr != NULL) {
slp = segpartptr->slp;
context.entityID = entityID;
context.itemID = segpartptr->itemID;
context.slp = slp;
context.parent = segpartptr->parentBioseq;
context.cumOffset = segpartptr->cumOffset;
context.from = segpartptr->from;
context.to = segpartptr->to;
context.strand = segpartptr->strand;
context.userdata = userdata;
context.omdp = (Pointer) omdp;
context.index = i + 1;
if (! userfunc (slp, &context)) return TRUE;
}
}
return TRUE;
}
NLM_EXTERN Boolean LIBCALL SeqMgrExploreDescriptors (BioseqPtr bsp, Pointer userdata,
SeqMgrDescExploreProc userfunc,
BoolPtr seqDescFilter)
{
BioseqSetPtr bssp;
SeqMgrDescContext context;
Uint2 entityID;
Uint2 itemID;
ObjMgrDataPtr omdp;
ValNodePtr sdp;
SeqEntryPtr sep;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return FALSE;
if (userfunc == NULL) return FALSE;
entityID = ObjMgrGetEntityIDForPointer (omdp->dataptr);
context.index = 0;
context.level = 0;
while (omdp != NULL) {
itemID = omdp->lastDescrItemID;
sdp = NULL;
switch (omdp->datatype) {
case OBJ_BIOSEQ :
bsp = (BioseqPtr) omdp->dataptr;
sdp = bsp->descr;
break;
case OBJ_BIOSEQSET :
bssp = (BioseqSetPtr) omdp->dataptr;
sdp = bssp->descr;
break;
default :
break;
}
sep = ObjMgrGetChoiceForData (omdp->dataptr);
/* call for every appropriate descriptor in current chain */
while (sdp != NULL) {
itemID++;
if (seqDescFilter == NULL || seqDescFilter [sdp->choice]) {
context.entityID = entityID;
context.itemID = itemID;
context.sdp = sdp;
context.sep = sep;
context.seqdesctype = sdp->choice;
context.userdata = userdata;
context.omdp = (Pointer) omdp;
(context.index)++;
if (! userfunc (sdp, &context)) return TRUE;
}
sdp = sdp->next;
}
/* now go up omdp chain looking for next descriptor */
omdp = SeqMgrGetOmdpForPointer (omdp->parentptr);
(context.level)++;
}
return TRUE;
}
NLM_EXTERN Boolean LIBCALL SeqMgrExploreFeatures (BioseqPtr bsp, Pointer userdata,
SeqMgrFeatExploreProc userfunc,
SeqLocPtr locationFilter,
BoolPtr seqFeatFilter, BoolPtr featDefFilter)
{
BioseqExtraPtr bspextra;
SeqMgrFeatContext context;
Uint2 entityID;
SMFeatItemPtr PNTR featsByPos;
Uint2 i;
SMFeatItemPtr item;
Int4 left = INT4_MIN;
ObjMgrDataPtr omdp;
Int4 right = INT4_MAX;
Uint1 seqfeattype;
SeqFeatPtr sfp;
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp == NULL || omdp->datatype != OBJ_BIOSEQ) return FALSE;
if (userfunc == NULL) return FALSE;
entityID = ObjMgrGetEntityIDForPointer (omdp->dataptr);
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return FALSE;
featsByPos = bspextra->featsByPos;
if (featsByPos == NULL || bspextra->numfeats < 1) return FALSE;
if (locationFilter != NULL) {
left = GetOffsetInBioseq (locationFilter, bsp, SEQLOC_LEFT_END);
if (left == -1) left = INT4_MIN;
right = GetOffsetInBioseq (locationFilter, bsp, SEQLOC_RIGHT_END);
if (right == -1) right = INT4_MAX;
}
/* call for every appropriate feature in sorted list */
for (i = 0; i < bspextra->numfeats; i++) {
item = featsByPos [i];
if (item != NULL) {
sfp = item->sfp;
seqfeattype = FindFeatFromFeatDefType (item->subtype);
if ((seqFeatFilter == NULL || seqFeatFilter [seqfeattype]) &&
(featDefFilter == NULL || featDefFilter [item->subtype]) &&
(locationFilter == NULL || (item->right >= left && item->left <= right)) &&
(! item->ignore)) {
context.entityID = entityID;
context.itemID = item->itemID;
context.sfp = sfp;
context.sap = item->sap;
context.bsp = item->bsp;
context.label = item->label;
context.left = item->left;
context.right = item->right;
context.partialL = item->partialL;
context.partialR = item->partialR;
context.farloc = item->farloc;
context.strand = item->strand;
context.seqfeattype = seqfeattype;
context.featdeftype = item->subtype;
context.numivals = item->numivals;
context.ivals = item->ivals;
context.userdata = userdata;
context.omdp = (Pointer) omdp;
context.index = item->index + 1;
if (! userfunc (sfp, &context)) return TRUE;
}
}
}
return TRUE;
}
/*****************************************************************************
*
* SeqMgrMapPartToSegmentedBioseq can speed up sequtil's CheckPointInBioseq
* for indexed part bioseq to segmented bioseq mapping
*
*****************************************************************************/
static SMSeqIdxPtr BinarySearchPartToSegmentMap (BioseqPtr in, Int4 pos, BioseqPtr bsp, SeqIdPtr sip)
{
BioseqExtraPtr bspextra;
Char buf [80];
Int2 compare;
ObjMgrDataPtr omdp;
SMSeqIdxPtr PNTR partsBySeqId;
SMSeqIdxPtr segpartptr;
Int2 L, R, mid;
if (in == NULL) return NULL;
omdp = SeqMgrGetOmdpForBioseq (in);
if (omdp == NULL) return NULL;
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) return NULL;
partsBySeqId = bspextra->partsBySeqId;
if (partsBySeqId == NULL || bspextra->numsegs < 1) return NULL;
if (bsp != NULL) {
sip = bsp->id;
}
if (sip == NULL) return NULL;
/* binary search into array on segmented bioseq sorted by part seqID (reversed) string */
while (sip != NULL) {
if (MakeReversedSeqIdString (sip, buf, sizeof (buf) - 1)) {
L = 0;
R = bspextra->numsegs - 1;
while (L < R) {
mid = (L + R) / 2;
segpartptr = partsBySeqId [mid];
compare = StringCmp (segpartptr->seqIdOfPart, buf);
if (compare < 0) {
L = mid + 1;
} else {
R = mid;
}
}
segpartptr = partsBySeqId [R];
if (StringCmp (segpartptr->seqIdOfPart, buf) == 0) {
if (pos >= segpartptr->from && pos <= segpartptr->to) {
return segpartptr;
}
}
}
sip = sip->next;
}
return NULL;
}
NLM_EXTERN Int4 LIBCALL SeqMgrMapPartToSegmentedBioseq (BioseqPtr in, Int4 pos, BioseqPtr bsp, SeqIdPtr sip)
{
BioseqExtraPtr bspextra;
SMSeqIdxPtr currp;
SMSeqIdxPtr nextp;
ObjMgrDataPtr omdp;
SMSeqIdxPtr segpartptr;
if (in == NULL) return -1;
/* first check to see if part has been loaded and single map up block installed */
if (bsp != NULL) {
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp != NULL) {
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra != NULL) {
/* no need for partsByLoc or partsBySeqId arrays, just use segparthead linked list */
for (segpartptr = bspextra->segparthead; segpartptr != NULL; segpartptr = segpartptr->next) {
if (segpartptr->parentBioseq == in) {
if (pos >= segpartptr->from && pos <= segpartptr->to) {
/* success, immediate return with mapped up value */
if (segpartptr->strand == Seq_strand_minus) {
return segpartptr->cumOffset + (segpartptr->to - pos);
} else {
return segpartptr->cumOffset + (pos - segpartptr->from);
}
}
}
}
}
}
}
/* otherwise do binary search on segmented bioseq mapping data */
segpartptr = BinarySearchPartToSegmentMap (in, pos, bsp, sip);
if (segpartptr == NULL) return -1;
if (pos >= segpartptr->from && pos <= segpartptr->to) {
/* install map up block on part, if it has been loaded, to speed up next search */
if (bsp != NULL) {
omdp = SeqMgrGetOmdpForBioseq (bsp);
if (omdp != NULL) {
bspextra = (BioseqExtraPtr) omdp->extradata;
if (bspextra == NULL) {
CreateBioseqExtraBlock (omdp, bsp);
bspextra = (BioseqExtraPtr) omdp->extradata;
}
if (bspextra != NULL) {
/* clean up any old map up info on part */
for (currp = bspextra->segparthead; currp != NULL; currp = nextp) {
nextp = currp->next;
SeqLocFree (currp->slp);
MemFree (currp->seqIdOfPart);
MemFree (currp);
}
bspextra->segparthead = NULL;
bspextra->numsegs = 0;
bspextra->partsByLoc = MemFree (bspextra->partsByLoc);
bspextra->partsBySeqId = MemFree (bspextra->partsBySeqId);
/* allocate single map up block */
currp = MemNew (sizeof (SMSeqIdx));
if (currp != NULL) {
currp->slp = AsnIoMemCopy (segpartptr->slp,
(AsnReadFunc) SeqLocAsnRead,
(AsnWriteFunc) SeqLocAsnWrite);
currp->seqIdOfPart = StringSave (segpartptr->seqIdOfPart);
currp->parentBioseq = segpartptr->parentBioseq;
currp->cumOffset = segpartptr->cumOffset;
currp->from = segpartptr->from;
currp->to = segpartptr->to;
currp->strand = segpartptr->strand;
}
/* add new map up block to part */
bspextra->segparthead = currp;
}
}
}
/* now return offset result */
if (segpartptr->strand == Seq_strand_minus) {
return segpartptr->cumOffset + (segpartptr->to - pos);
} else {
return segpartptr->cumOffset + (pos - segpartptr->from);
}
}
return -1;
}
|