NCBI C++ Toolkit Cross Reference

  C++/src/objmgr/seq_loc_cvt.cpp


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
/* $Id: seq_loc_cvt.cpp 73104 2016-06-20 16:37:02Z vasilche $ * =========================================================================== * * 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. * * =========================================================================== * * Author: Eugene Vasilchenko * * File Description: * Class for mapping Seq-loc between sequences. * */ #include <ncbi_pch.hpp> #include <objmgr/impl/seq_loc_cvt.hpp> #include <objmgr/seq_loc_mapper.hpp> #include <objmgr/impl/seq_align_mapper.hpp> #include <objmgr/seq_map_ci.hpp> #include <objmgr/impl/scope_impl.hpp> #include <objmgr/annot_types_ci.hpp> #include <objmgr/impl/annot_object.hpp> #include <objmgr/impl/seq_annot_info.hpp> #include <objmgr/impl/seq_table_info.hpp> #include <objects/seqloc/Seq_loc.hpp> #include <objects/seqloc/Seq_interval.hpp> #include <objects/seqloc/Seq_point.hpp> #include <objects/seqloc/Seq_loc_equiv.hpp> #include <objects/seqloc/Seq_bond.hpp> #include <objects/seqfeat/Seq_feat.hpp> #include <objects/seqfeat/Cdregion.hpp> #include <objects/seqfeat/Code_break.hpp> #include <objects/seqfeat/RNA_ref.hpp> #include <objects/seqfeat/Trna_ext.hpp> #include <algorithm> BEGIN_NCBI_SCOPE BEGIN_SCOPE(objects) ///////////////////////////////////////////////////////////////////////////// // CSeq_loc_Conversion ///////////////////////////////////////////////////////////////////////////// CSeq_loc_Conversion::CSeq_loc_Conversion(CSeq_loc& master_loc_empty, const CSeq_id_Handle& dst_id, const CSeqMap_CI& seg, const CSeq_id_Handle& src_id, CScope* scope) : m_Src_id_Handle(src_id), m_Src_from(0), m_Src_to(0), m_Shift(0), m_Reverse(false), m_Dst_id_Handle(dst_id), m_Dst_loc_Empty(&master_loc_empty), m_Partial(false), m_PartialHasUnconvertedId(false), m_PartialFlag(0), m_LastType(eMappedObjType_not_set), m_LastStrand(eNa_strand_unknown), m_Scope(scope) { SetConversion(seg); Reset(); } CSeq_loc_Conversion::CSeq_loc_Conversion(CSeq_loc& master_loc_empty, const CSeq_id_Handle& dst_id, const TRange& dst_rg, const CSeq_id_Handle& src_id, TSeqPos src_start, bool reverse, CScope* scope) : m_Src_id_Handle(src_id), m_Src_from(0), m_Src_to(0), m_Shift(0), m_Reverse(reverse), m_Dst_id_Handle(dst_id), m_Dst_loc_Empty(&master_loc_empty), m_Partial(false), m_PartialHasUnconvertedId(false), m_PartialFlag(0), m_LastType(eMappedObjType_not_set), m_LastStrand(eNa_strand_unknown), m_Scope(scope) { m_Src_from = src_start; m_Src_to = m_Src_from + dst_rg.GetLength() - 1; if ( !m_Reverse ) { m_Shift = dst_rg.GetFrom() - m_Src_from; } else { m_Shift = dst_rg.GetFrom() + m_Src_to; } Reset(); } CSeq_loc_Conversion::~CSeq_loc_Conversion(void) { _ASSERT(!IsSpecialLoc()); } void CSeq_loc_Conversion::ResetKeepPartial(void) { _ASSERT(!IsSpecialLoc()); m_TotalRange = TRange::GetEmpty(); m_PartialFlag = 0; m_DstFuzz_from.Reset(); m_DstFuzz_to.Reset(); m_GraphRanges.Reset(); } void CSeq_loc_Conversion::Reset(void) { ResetKeepPartial(); m_Partial = false; m_PartialHasUnconvertedId = false; } void CSeq_loc_Conversion::CombineWith(CSeq_loc_Conversion& cvt) { _ASSERT(cvt.m_Src_id_Handle == m_Dst_id_Handle); TRange dst_rg = GetDstRange(); TRange cvt_src_rg = cvt.GetSrcRange(); TRange overlap = dst_rg & cvt_src_rg; _ASSERT( !overlap.Empty() ); TSeqPos new_dst_from = cvt.ConvertPos(overlap.GetFrom()); _ASSERT(new_dst_from != kInvalidSeqPos); _ASSERT(cvt.ConvertPos(overlap.GetTo()) != kInvalidSeqPos); bool new_reverse = cvt.m_Reverse ? !m_Reverse : m_Reverse; if (overlap.GetFrom() > dst_rg.GetFrom()) { TSeqPos l_trunc = overlap.GetFrom() - dst_rg.GetFrom(); // Truncated range if ( !m_Reverse ) { m_Src_from += l_trunc; } else { m_Src_to -= l_trunc; } } if (overlap.GetTo() < dst_rg.GetTo()) { TSeqPos r_trunc = dst_rg.GetTo() - overlap.GetTo(); // Truncated range if ( !m_Reverse ) { m_Src_to -= r_trunc; } else { m_Src_from += r_trunc; } } m_Reverse = new_reverse; if ( !m_Reverse ) { m_Shift = new_dst_from - m_Src_from; } else { m_Shift = new_dst_from + m_Src_to; } m_Dst_id_Handle = cvt.m_Dst_id_Handle; m_Dst_loc_Empty = cvt.m_Dst_loc_Empty; cvt.Reset(); Reset(); } void CSeq_loc_Conversion::SetConversion(const CSeqMap_CI& seg) { m_Src_from = seg.GetRefPosition(); m_Src_to = m_Src_from + seg.GetLength() - 1; m_Reverse = seg.GetRefMinusStrand(); if ( !m_Reverse ) { m_Shift = seg.GetPosition() - m_Src_from; } else { m_Shift = seg.GetPosition() + m_Src_to; } } CConstRef<CInt_fuzz> CSeq_loc_Conversion::ReverseFuzz(const CInt_fuzz& fuzz) const { if ( fuzz.IsLim() ) { CInt_fuzz::ELim lim = fuzz.GetLim(); switch ( lim ) { case CInt_fuzz::eLim_lt: lim = CInt_fuzz::eLim_gt; break; case CInt_fuzz::eLim_gt: lim = CInt_fuzz::eLim_lt; break; case CInt_fuzz::eLim_tr: lim = CInt_fuzz::eLim_tl; break; case CInt_fuzz::eLim_tl: lim = CInt_fuzz::eLim_tr; break; default: return ConstRef(&fuzz); } CRef<CInt_fuzz> ret(new CInt_fuzz); ret->SetLim(lim); return ret; } return ConstRef(&fuzz); } void CSeq_loc_Conversion::ConvertSimpleLoc(const CSeq_id_Handle& src_id, const CRange<TSeqPos> src_range, const SAnnotObject_Index& src_index) { if ( src_id != m_Src_id_Handle ) { m_Partial = m_PartialHasUnconvertedId = true; return; } ENa_strand strand; switch ( src_index.m_Flags & src_index.fStrand_mask ) { case SAnnotObject_Index::fStrand_plus: strand = eNa_strand_plus; break; case SAnnotObject_Index::fStrand_minus: strand = eNa_strand_minus; break; default: strand = eNa_strand_unknown; break; } if ( src_index.LocationIsPoint() ) { ConvertPoint(src_range.GetFrom(), strand); } else if ( src_index.LocationIsInterval() ) { ConvertInterval(src_range.GetFrom(), src_range.GetTo(), strand); } else { _ASSERT(src_index.LocationIsWhole()); CBioseq_Handle bh = m_Scope->GetBioseqHandle(src_id, CScope::eGetBioseq_All); ConvertInterval(0, bh.GetBioseqLength()-1, eNa_strand_unknown); } } bool CSeq_loc_Conversion::ConvertPoint(const CSeq_point& src) { ENa_strand strand = src.IsSetStrand()? src.GetStrand(): eNa_strand_unknown; bool ret = GoodSrcId(src.GetId()) && ConvertPoint(src.GetPoint(), strand); if ( ret ) { if ( src.IsSetFuzz() ) { if ( m_Reverse ) { m_DstFuzz_from = ReverseFuzz(src.GetFuzz()); } else { m_DstFuzz_from = &src.GetFuzz(); } // normalize left and right fuzz values if ( m_DstFuzz_from && m_DstFuzz_from->IsLim() && m_DstFuzz_from->GetLim() == CInt_fuzz::eLim_lt ) { m_DstFuzz_from.Reset(); m_PartialFlag |= fPartial_from; } } } else if ( m_GraphRanges ) { m_GraphRanges->IncOffset(1); } return ret; } bool CSeq_loc_Conversion::ConvertInterval(const CSeq_interval& src) { ENa_strand strand = src.IsSetStrand()? src.GetStrand(): eNa_strand_unknown; bool ret = GoodSrcId(src.GetId()) && ConvertInterval(src.GetFrom(), src.GetTo(), strand); if ( ret ) { if ( m_Reverse ) { if ( !(m_PartialFlag & fPartial_to) && src.IsSetFuzz_from() ) { m_DstFuzz_to = ReverseFuzz(src.GetFuzz_from()); } if ( !(m_PartialFlag & fPartial_from) && src.IsSetFuzz_to() ) { m_DstFuzz_from = ReverseFuzz(src.GetFuzz_to()); } } else { if ( !(m_PartialFlag & fPartial_from) && src.IsSetFuzz_from() ) { m_DstFuzz_from = &src.GetFuzz_from(); } if ( !(m_PartialFlag & fPartial_to) && src.IsSetFuzz_to() ) { m_DstFuzz_to = &src.GetFuzz_to(); } } // normalize left and right fuzz values if ( m_DstFuzz_from && m_DstFuzz_from->IsLim() && m_DstFuzz_from->GetLim() == CInt_fuzz::eLim_lt ) { m_DstFuzz_from.Reset(); m_PartialFlag |= fPartial_from; } if ( m_DstFuzz_to && m_DstFuzz_to->IsLim() && m_DstFuzz_to->GetLim() == CInt_fuzz::eLim_gt ) { m_DstFuzz_to.Reset(); m_PartialFlag |= fPartial_to; } } else if ( m_GraphRanges ) { m_GraphRanges->IncOffset(src.GetLength()); } return ret; } bool CSeq_loc_Conversion::ConvertPoint(TSeqPos src_pos, ENa_strand src_strand) { _ASSERT(!IsSpecialLoc()); m_PartialFlag = 0; m_DstFuzz_from.Reset(); m_DstFuzz_to.Reset(); if ( src_pos < m_Src_from || src_pos > m_Src_to ) { m_Partial = true; return false; } TSeqPos dst_pos; if ( !m_Reverse ) { m_LastStrand = src_strand; dst_pos = m_Shift + src_pos; } else { m_LastStrand = Reverse(src_strand); dst_pos = m_Shift - src_pos; } m_LastType = eMappedObjType_Seq_point; m_TotalRange += m_LastRange.SetFrom(dst_pos).SetTo(dst_pos); if ( m_GraphRanges ) { m_GraphRanges->AddRange(TRange(src_pos, src_pos)); m_GraphRanges->IncOffset(1); } return true; } bool CSeq_loc_Conversion::ConvertInterval(TSeqPos src_from, TSeqPos src_to, ENa_strand src_strand) { _ASSERT(!IsSpecialLoc()); m_PartialFlag = 0; m_DstFuzz_from.Reset(); m_DstFuzz_to.Reset(); bool partial_from = false, partial_to = false; TSeqPos len = src_to - src_from + 1; TRange graph_rg(0, len - 1); if ( src_from < m_Src_from ) { m_Partial = partial_from = true; graph_rg.SetFrom(m_Src_from - src_from); src_from = m_Src_from; } if ( src_to > m_Src_to ) { m_Partial = partial_to = true; graph_rg.SetLength(m_Src_to - src_from + 1); src_to = m_Src_to; } if ( src_from > src_to ) { m_Partial = true; return false; } TSeqPos dst_from, dst_to; if ( !m_Reverse ) { m_LastStrand = src_strand; dst_from = m_Shift + src_from; dst_to = m_Shift + src_to; } else { m_LastStrand = Reverse(src_strand); dst_from = m_Shift - src_to; dst_to = m_Shift - src_from; swap(partial_from, partial_to); } m_LastType = eMappedObjType_Seq_interval; m_TotalRange += m_LastRange.SetFrom(dst_from).SetTo(dst_to); if ( partial_from ) { m_PartialFlag |= fPartial_from; } if ( partial_to ) { m_PartialFlag |= fPartial_to; } if ( m_GraphRanges ) { m_GraphRanges->AddRange(graph_rg); m_GraphRanges->IncOffset(len); } return true; } inline void CSeq_loc_Conversion::CheckDstInterval(void) { if ( m_LastType != eMappedObjType_Seq_interval ) { NCBI_THROW(CAnnotException, eBadLocation, "Wrong last location type"); } m_LastType = eMappedObjType_not_set; } inline void CSeq_loc_Conversion::CheckDstPoint(void) { if ( m_LastType != eMappedObjType_Seq_point ) { NCBI_THROW(CAnnotException, eBadLocation, "Wrong last location type"); } m_LastType = eMappedObjType_not_set; } inline void CSeq_loc_Conversion::CheckDstMix(void) { if ( m_LastType != eMappedObjType_Seq_loc_mix ) { NCBI_THROW(CAnnotException, eBadLocation, "Wrong last location type"); } m_LastType = eMappedObjType_not_set; } CRef<CSeq_interval> CSeq_loc_Conversion::GetDstInterval(void) { CheckDstInterval(); CRef<CSeq_interval> ret(new CSeq_interval); CSeq_interval& interval = *ret; interval.SetId(GetDstId()); interval.SetFrom(m_LastRange.GetFrom()); interval.SetTo(m_LastRange.GetTo()); if ( m_LastStrand != eNa_strand_unknown ) { interval.SetStrand(m_LastStrand); } if ( m_PartialFlag & fPartial_from ) { interval.SetFuzz_from().SetLim(CInt_fuzz::eLim_lt); } else if ( m_DstFuzz_from ) { interval.SetFuzz_from(const_cast<CInt_fuzz&>(*m_DstFuzz_from)); } if ( m_PartialFlag & fPartial_to ) { interval.SetFuzz_to().SetLim(CInt_fuzz::eLim_gt); } else if ( m_DstFuzz_to ) { interval.SetFuzz_to(const_cast<CInt_fuzz&>(*m_DstFuzz_to)); } return ret; } CRef<CSeq_point> CSeq_loc_Conversion::GetDstPoint(void) { CheckDstPoint(); _ASSERT(m_LastRange.GetLength() == 1); CRef<CSeq_point> ret(new CSeq_point); CSeq_point& point = *ret; point.SetId(GetDstId()); point.SetPoint(m_LastRange.GetFrom()); // Points can not be partial if ( m_LastStrand != eNa_strand_unknown ) { point.SetStrand(m_LastStrand); } if ( m_PartialFlag & fPartial_from ) { point.SetFuzz().SetLim(CInt_fuzz::eLim_lt); } else if ( m_DstFuzz_from ) { point.SetFuzz(const_cast<CInt_fuzz&>(*m_DstFuzz_from)); } return ret; } void CSeq_loc_Conversion::MakeDstMix(CSeq_loc_mix& dst, const CSeq_loc_mix& src) const { CSeq_loc_mix::Tdata& dst_mix = dst.Set(); const CSeq_loc_mix::Tdata& src_mix = src.Get(); ITERATE ( CSeq_loc_mix::Tdata, it, src_mix ) { const CSeq_interval& src_int = (*it)->GetInt(); CRef<CSeq_loc> dst_loc(new CSeq_loc); CSeq_interval& dst_int = dst_loc->SetInt(); dst_int.SetId(m_Dst_loc_Empty.GetNCObject().SetEmpty()); ENa_strand src_strand = src_int.IsSetStrand()? src_int.GetStrand(): eNa_strand_unknown; TSeqPos src_from = src_int.GetFrom(), src_to = src_int.GetTo(); ENa_strand dst_strand; TSeqPos dst_from, dst_to; if ( !m_Reverse ) { dst_strand = src_strand; dst_from = m_Shift + src_from; dst_to = m_Shift + src_to; } else { dst_strand = Reverse(src_strand); dst_from = m_Shift - src_to; dst_to = m_Shift - src_from; } if ( dst_strand != eNa_strand_unknown ) { dst_int.SetStrand(dst_strand); } dst_int.SetFrom(dst_from); dst_int.SetTo(dst_to); dst_mix.push_back(dst_loc); } } CRef<CSeq_loc_mix> CSeq_loc_Conversion::GetDstMix(void) { CRef<CSeq_loc_mix> ret(new CSeq_loc_mix); CheckDstMix(); MakeDstMix(*ret, m_SrcLoc->GetMix()); m_SrcLoc.Reset(); return ret; } void CSeq_loc_Conversion::SetDstLoc(CRef<CSeq_loc>& dst) { CSeq_loc* loc = 0; if ( !dst ) { switch ( m_LastType ) { case eMappedObjType_Seq_interval: dst.Reset(loc = new CSeq_loc); loc->SetInt(*GetDstInterval()); break; case eMappedObjType_Seq_point: dst.Reset(loc = new CSeq_loc); loc->SetPnt(*GetDstPoint()); break; case eMappedObjType_Seq_loc_mix: dst.Reset(loc = new CSeq_loc); loc->SetMix(*GetDstMix()); break; default: _ASSERT(0); break; } } else { _ASSERT(!IsSpecialLoc()); } } void CSeq_loc_Conversion::ConvertPacked_int(const CSeq_loc& src, CRef<CSeq_loc>& dst) { _ASSERT(src.Which() == CSeq_loc::e_Packed_int); const CPacked_seqint::Tdata& src_ints = src.GetPacked_int().Get(); CPacked_seqint::Tdata* dst_ints = 0; // If the result contains NULLs, may need to use mix instead. CSeq_loc_mix::Tdata* dst_mix = 0; bool last_truncated = false; ITERATE ( CPacked_seqint::Tdata, i, src_ints ) { if ( ConvertInterval(**i) ) { if ( !dst_ints && !dst_mix ) { dst.Reset(new CSeq_loc); dst_ints = &dst->SetPacked_int().Set(); } CRef<CSeq_interval> dst_int = GetDstInterval(); if ( last_truncated && !CSeq_loc_Mapper_Base::GetNonMappingAsNull() && !dst_int->IsPartialStart(eExtreme_Biological) ) { dst_int->SetPartialStart(true, eExtreme_Biological); } if ( dst_mix ) { CRef<CSeq_loc> mix_loc(new CSeq_loc); mix_loc->SetInt(*dst_int); dst_mix->push_back(mix_loc); } else { dst_ints->push_back(dst_int); } last_truncated = false; } else { if (last_truncated) continue; if (CSeq_loc_Mapper_Base::GetNonMappingAsNull()) { dst_mix = CSeq_loc_Conversion::s_ConvertToMix(dst); _ASSERT(dst_mix); CRef<CSeq_loc> null_loc(new CSeq_loc); null_loc->SetNull(); dst_mix->push_back(null_loc); } else if (dst && !dst->IsPartialStop(eExtreme_Biological) ) { dst->SetPartialStop(true, eExtreme_Biological); } last_truncated = true; } } } void CSeq_loc_Conversion::ConvertPacked_pnt(const CSeq_loc& src, CRef<CSeq_loc>& dst) { _ASSERT(src.Which() == CSeq_loc::e_Packed_pnt); const CPacked_seqpnt& src_pack_pnts = src.GetPacked_pnt(); if ( !GoodSrcId(src_pack_pnts.GetId()) ) { if ( m_GraphRanges ) { m_GraphRanges->IncOffset(TSeqPos(src_pack_pnts.GetPoints().size())); } return; } const CPacked_seqpnt::TPoints& src_pnts = src_pack_pnts.GetPoints(); CPacked_seqpnt::TPoints* dst_pnts = 0; CSeq_loc_mix::Tdata* dst_mix = 0; ENa_strand dst_strand = eNa_strand_unknown; CRef<CInt_fuzz> dst_fuzz; if ( src_pack_pnts.IsSetStrand() ) { dst_strand = ConvertStrand(src_pack_pnts.GetStrand()); } if ( src_pack_pnts.IsSetFuzz() ) { dst_fuzz.Reset(new CInt_fuzz); if ( m_Reverse ) { dst_fuzz->Assign(*ReverseFuzz(src_pack_pnts.GetFuzz())); } else { dst_fuzz->Assign(src_pack_pnts.GetFuzz()); } } ITERATE ( CPacked_seqpnt::TPoints, i, src_pnts ) { TSeqPos dst_pos = ConvertPos(*i); if ( dst_pos == kInvalidSeqPos ) { if (CSeq_loc_Mapper::GetNonMappingAsNull()) { dst_mix = s_ConvertToMix(dst); CRef<CSeq_loc> null_loc(new CSeq_loc); null_loc->SetNull(); dst_mix->push_back(null_loc); } continue; } if ( dst_mix ) { CRef<CSeq_loc> mix_loc(new CSeq_loc); mix_loc->SetPnt().SetId(GetDstId()); mix_loc->SetPnt().SetPoint(dst_pos); if ( src_pack_pnts.IsSetStrand() ) { mix_loc->SetPnt().SetStrand(dst_strand); } if ( dst_fuzz ) { mix_loc->SetPnt().SetFuzz(*dst_fuzz); } } else { if ( !dst_pnts ) { dst.Reset(new CSeq_loc); CPacked_seqpnt& pnts = dst->SetPacked_pnt(); pnts.SetId(GetDstId()); dst_pnts = &pnts.SetPoints(); if ( src_pack_pnts.IsSetStrand() ) { pnts.SetStrand(dst_strand); } if ( dst_fuzz ) { pnts.SetFuzz(*dst_fuzz); } } dst_pnts->push_back(dst_pos); } m_TotalRange += TRange(dst_pos, dst_pos); } } bool CSeq_loc_Conversion::ConvertSimpleMix(const CSeq_loc& src) { const CSeq_loc_mix::Tdata& src_mix = src.GetMix().Get(); if ( src_mix.empty() ) { return false; } const CSeq_loc& first_loc = **src_mix.begin(); if ( !first_loc.IsInt() ) { return false; } const CSeq_interval& first_int = first_loc.GetInt(); ENa_strand src_strand = first_int.IsSetStrand()? first_int.GetStrand(): eNa_strand_unknown; TSeqPos src_from, src_to; if ( !IsReverse(src_strand) ) { // forward TSeqPos prev_pos = m_Src_from; src_from = first_int.GetFrom(); ITERATE ( CSeq_loc_mix::Tdata, i, src_mix ) { const CSeq_loc& loc = **i; if ( !loc.IsInt() ) { return false; } const CSeq_interval& cur_int = loc.GetInt(); if ( cur_int.IsSetFuzz_from() || cur_int.IsSetFuzz_to() ) { return false; } if ( !GoodSrcId(cur_int.GetId()) ) { return false; } ENa_strand strand = cur_int.IsSetStrand()? cur_int.GetStrand(): eNa_strand_unknown; if ( strand != src_strand ) { return false; } TSeqPos from = cur_int.GetFrom(); TSeqPos to = cur_int.GetTo(); if ( to < from || from < prev_pos || to > m_Src_to ) { return false; } prev_pos = to+1; } src_to = prev_pos-1; } else { TSeqPos prev_pos = m_Src_to; src_to = first_int.GetTo(); ITERATE ( CSeq_loc_mix::Tdata, i, src_mix ) { const CSeq_loc& loc = **i; if ( !loc.IsInt() ) { return false; } const CSeq_interval& cur_int = loc.GetInt(); if ( cur_int.IsSetFuzz_from() || cur_int.IsSetFuzz_to() ) { return false; } if ( !GoodSrcId(cur_int.GetId()) ) { return false; } ENa_strand strand = cur_int.IsSetStrand()? cur_int.GetStrand(): eNa_strand_unknown; if ( strand != src_strand ) { return false; } TSeqPos from = cur_int.GetFrom(); TSeqPos to = cur_int.GetTo(); if ( to < from || to > prev_pos || from < m_Src_from ) { return false; } prev_pos = from-1; } src_from = prev_pos+1; } ENa_strand dst_strand; TSeqPos dst_from, dst_to; if ( !m_Reverse ) { dst_strand = src_strand; dst_from = m_Shift + src_from; dst_to = m_Shift + src_to; } else { dst_strand = Reverse(src_strand); dst_from = m_Shift - src_to; dst_to = m_Shift - src_from; } m_PartialFlag = 0; m_DstFuzz_from.Reset(); m_DstFuzz_to.Reset(); m_LastStrand = dst_strand; m_LastType = eMappedObjType_Seq_loc_mix; m_SrcLoc = &src; m_TotalRange += m_LastRange.SetFrom(dst_from).SetTo(dst_to); return true; } void CSeq_loc_Conversion::ConvertMix(const CSeq_loc& src, CRef<CSeq_loc>& dst, EConvertFlag flag) { _ASSERT(src.Which() == CSeq_loc::e_Mix); if ( flag != eCnvAlways && ConvertSimpleMix(src) ) { return; } const CSeq_loc_mix::Tdata& src_mix = src.GetMix().Get(); CSeq_loc_mix::Tdata* dst_mix = 0; CRef<CSeq_loc> dst_loc; bool last_truncated = false; ITERATE ( CSeq_loc_mix::Tdata, i, src_mix ) { if ( Convert(**i, dst_loc, eCnvAlways) ) { _ASSERT(dst_loc); dst_mix = s_ConvertToMix(dst); if ( last_truncated && !CSeq_loc_Mapper_Base::GetNonMappingAsNull() && !dst_loc->IsPartialStart(eExtreme_Biological) ) { dst_loc->SetPartialStart(true, eExtreme_Biological); } dst_mix->push_back(dst_loc); last_truncated = false; } else { if (last_truncated) continue; if (CSeq_loc_Mapper_Base::GetNonMappingAsNull()) { CRef<CSeq_loc> null_loc(new CSeq_loc); null_loc->SetNull(); dst_mix = s_ConvertToMix(dst); dst_mix->push_back(null_loc); } else if (dst && !dst->IsPartialStop(eExtreme_Biological)) { dst->SetPartialStop(true, eExtreme_Biological); } last_truncated = true; } } } void CSeq_loc_Conversion::ConvertEquiv(const CSeq_loc& src, CRef<CSeq_loc>& dst) { _ASSERT(src.Which() == CSeq_loc::e_Equiv); const CSeq_loc_equiv::Tdata& src_equiv = src.GetEquiv().Get(); CSeq_loc_equiv::Tdata* dst_equiv = 0; CRef<CSeq_loc> dst_loc; ITERATE ( CSeq_loc_equiv::Tdata, i, src_equiv ) { if ( Convert(**i, dst_loc, eCnvAlways) || CSeq_loc_Mapper::GetNonMappingAsNull()) { if ( !dst_equiv ) { dst.Reset(new CSeq_loc); dst_equiv = &dst->SetEquiv().Set(); } if (!dst_loc) { dst_loc.Reset(new CSeq_loc); dst_loc->SetNull(); } dst_equiv->push_back(dst_loc); } } } void CSeq_loc_Conversion::ConvertBond(const CSeq_loc& src, CRef<CSeq_loc>& dst) { _ASSERT(src.Which() == CSeq_loc::e_Bond); const CSeq_bond& src_bond = src.GetBond(); CSeq_bond* dst_bond = 0; if ( ConvertPoint(src_bond.GetA()) ) { dst.Reset(new CSeq_loc); dst_bond = &dst->SetBond(); dst_bond->SetA(*GetDstPoint()); if ( src_bond.IsSetB() ) { dst_bond->SetB().Assign(src_bond.GetB()); } } if ( src_bond.IsSetB() ) { if ( ConvertPoint(src_bond.GetB()) ) { if ( !dst_bond ) { dst.Reset(new CSeq_loc); dst_bond = &dst->SetBond(); dst_bond->SetA().Assign(src_bond.GetA()); } dst_bond->SetB(*GetDstPoint()); } } } bool CSeq_loc_Conversion::Convert(const CSeq_loc& src, CRef<CSeq_loc>& dst, EConvertFlag flag) { dst.Reset(); CSeq_loc* loc = 0; _ASSERT(!IsSpecialLoc()); m_LastType = eMappedObjType_Seq_loc; switch ( src.Which() ) { case CSeq_loc::e_not_set: case CSeq_loc::e_Feat: // Nothing to do, although this should never happen -- // the seq_loc is intersecting with the conv. loc. _ASSERT("this cannot happen" && 0); break; case CSeq_loc::e_Null: { dst.Reset(loc = new CSeq_loc); loc->SetNull(); break; } case CSeq_loc::e_Empty: { if ( GoodSrcId(src.GetEmpty()) ) { dst.Reset(loc = new CSeq_loc); loc->SetEmpty(GetDstId()); } break; } case CSeq_loc::e_Whole: { const CSeq_id& src_id = src.GetWhole(); // Convert to the allowed master seq interval if ( GoodSrcId(src_id) ) { CBioseq_Handle bh = m_Scope->GetBioseqHandle(CSeq_id_Handle::GetHandle(src_id), CScope::eGetBioseq_All); ConvertInterval(0, bh.GetBioseqLength()-1, eNa_strand_unknown); } else if ( m_GraphRanges ) { CBioseq_Handle bh = m_Scope->GetBioseqHandle(CSeq_id_Handle::GetHandle(src_id), CScope::eGetBioseq_All); m_GraphRanges->IncOffset(bh.GetBioseqLength()); } break; } case CSeq_loc::e_Int: { ConvertInterval(src.GetInt()); break; } case CSeq_loc::e_Pnt: { ConvertPoint(src.GetPnt()); break; } case CSeq_loc::e_Packed_int: { ConvertPacked_int(src, dst); break; } case CSeq_loc::e_Packed_pnt: { ConvertPacked_pnt(src, dst); break; } case CSeq_loc::e_Mix: { ConvertMix(src, dst, flag); break; } case CSeq_loc::e_Equiv: { ConvertEquiv(src, dst); break; } case CSeq_loc::e_Bond: { ConvertBond(src, dst); break; } default: NCBI_THROW(CAnnotException, eBadLocation, "Unsupported location type"); } if ( flag == eCnvAlways && IsSpecialLoc() ) { SetDstLoc(dst); } return dst.NotEmpty(); } void CSeq_loc_Conversion::ConvertCdregion(CAnnotObject_Ref& ref, const CSeq_feat& orig_feat, CRef<CSeq_feat>& mapped_feat) { const CAnnotObject_Info& obj = ref.GetAnnotObject_Info(); _ASSERT( obj.IsFeat() ); const CSeqFeatData& src_feat_data = orig_feat.GetData(); _ASSERT( src_feat_data.IsCdregion() ); if (!src_feat_data.GetCdregion().IsSetCode_break()) { return; } const CCdregion& src_cd = src_feat_data.GetCdregion(); // Map code-break locations const CCdregion::TCode_break& src_cb = src_cd.GetCode_break(); mapped_feat.Reset(new CSeq_feat); // Initialize mapped feature ref.GetMappingInfo().InitializeMappedSeq_feat(*obj.GetFeatFast(), *mapped_feat); // Copy Cd-region, do not change the original one CRef<CSeqFeatData> new_data(new CSeqFeatData); mapped_feat->SetData(*new_data); CCdregion& new_cd = new_data->SetCdregion(); if ( src_cd.IsSetOrf() ) { new_cd.SetOrf(src_cd.GetOrf()); } else { new_cd.ResetOrf(); } if ( src_cd.IsSetFrame() ) { new_cd.SetFrame(src_cd.GetFrame()); } else { new_cd.ResetFrame(); } if ( src_cd.IsSetConflict() ) { new_cd.SetConflict(src_cd.GetConflict()); } else { new_cd.ResetConflict(); } if ( src_cd.IsSetGaps() ) { new_cd.SetGaps(src_cd.GetGaps()); } else { new_cd.ResetGaps(); } if ( src_cd.IsSetMismatch() ) { new_cd.SetMismatch(src_cd.GetMismatch()); } else { new_cd.ResetMismatch(); } if ( src_cd.IsSetCode() ) { new_cd.SetCode(const_cast<CGenetic_code&>(src_cd.GetCode())); } else { new_cd.ResetCode(); } if ( src_cd.IsSetStops() ) { new_cd.SetStops(src_cd.GetStops()); } else { new_cd.ResetStops(); } CCdregion::TCode_break& mapped_cbs = new_cd.SetCode_break(); mapped_cbs.clear(); ITERATE(CCdregion::TCode_break, it, src_cb) { CRef<CSeq_loc> cb_loc; Convert((*it)->GetLoc(), cb_loc, eCnvAlways); // Preserve partial flag ResetKeepPartial(); if (cb_loc && cb_loc->Which() != CSeq_loc::e_not_set) { CRef<CCode_break> cb(new CCode_break); cb->SetAa(const_cast<CCode_break::TAa&>((*it)->GetAa())); cb->SetLoc(*cb_loc); mapped_cbs.push_back(cb); } /*else { // Keep the original code-break CRef<CCode_break> cb(&const_cast<CCode_break&>(**it)); mapped_cbs.push_back(cb); }*/ } } void CSeq_loc_Conversion::ConvertRna(CAnnotObject_Ref& ref, const CSeq_feat& orig_feat, CRef<CSeq_feat>& mapped_feat) { const CAnnotObject_Info& obj = ref.GetAnnotObject_Info(); _ASSERT( obj.IsFeat() ); const CSeqFeatData& src_feat_data = orig_feat.GetData(); _ASSERT( src_feat_data.IsRna() ); if (!src_feat_data.GetRna().IsSetExt() || !src_feat_data.GetRna().GetExt().IsTRNA() || !src_feat_data.GetRna().GetExt().GetTRNA().IsSetAnticodon()) { return; } const CRNA_ref::TExt& src_ext = src_feat_data.GetRna().GetExt(); // Map anticodon location const CSeq_loc& src_anticodon = src_ext.GetTRNA().GetAnticodon(); mapped_feat.Reset(new CSeq_feat); // Initialize mapped feature ref.GetMappingInfo().InitializeMappedSeq_feat(*obj.GetFeatFast(), *mapped_feat); // Copy RNA-ext, do not change the original one CRef<CRNA_ref::TExt> new_ext(new CRNA_ref::TExt); // Shallow-copy the feature, replace data.rna.ext.trna.anticodon // with the mapped location mapped_feat->Assign(*obj.GetFeatFast(), eShallow); mapped_feat->SetData(*(new CSeqFeatData)); mapped_feat->SetData().Assign(src_feat_data, eShallow); mapped_feat->SetData().SetRna(*(new CRNA_ref)); mapped_feat->SetData().SetRna().SetType(src_feat_data.GetRna().GetType()); if ( src_feat_data.GetRna().IsSetPseudo() ) { mapped_feat->SetData().SetRna().SetPseudo( src_feat_data.GetRna().GetPseudo()); } else { mapped_feat->SetData().SetRna().ResetPseudo(); } mapped_feat->SetData().SetRna().SetExt().SetTRNA().SetAa( const_cast<CTrna_ext::C_Aa&>(src_ext.GetTRNA().GetAa())); if ( src_ext.GetTRNA().IsSetCodon() ) { mapped_feat->SetData().SetRna().SetExt().SetTRNA().SetCodon() = src_ext.GetTRNA().GetCodon(); } else { mapped_feat->SetData().SetRna().SetExt().SetTRNA().ResetCodon(); } CRef<CSeq_loc> ac_loc; Convert(src_anticodon, ac_loc, eCnvAlways); // Preserve partial flag ResetKeepPartial(); if (ac_loc && ac_loc->Which() != CSeq_loc::e_not_set) { mapped_feat->SetData() .SetRna().SetExt().SetTRNA().SetAnticodon(*ac_loc); } else { mapped_feat->SetData() .SetRna().SetExt().SetTRNA().ResetAnticodon(); } } static inline bool NeedFullFeature(const CAnnotObject_Ref& ref, CSeq_loc_Conversion::ELocationType loctype) { if ( loctype != CSeq_loc_Conversion::eLocation ) { return false; } const CAnnotObject_Info& obj = ref.GetAnnotObject_Info(); _ASSERT( obj.IsFeat() ); CSeqFeatData::E_Choice type = obj.GetFeatType(); if ( type == CSeqFeatData::e_Rna ) { if ( !obj.IsRegular() ) { return true; } const CSeqFeatData& data = obj.GetFeatFast()->GetData(); _ASSERT( data.IsRna() ); return data.GetRna().IsSetExt() && data.GetRna().GetExt().IsTRNA() && data.GetRna().GetExt().GetTRNA().IsSetAnticodon(); } else if ( type == CSeqFeatData::e_Cdregion ) { if ( !obj.IsRegular() ) { return true; } const CSeqFeatData& data = obj.GetFeatFast()->GetData(); _ASSERT( data.IsCdregion() ); return data.GetCdregion().IsSetCode_break(); } return false; } void CSeq_loc_Conversion::ConvertFeature(CAnnotObject_Ref& ref, const CSeq_feat& orig_feat, CRef<CSeq_feat>& mapped_feat) { switch ( orig_feat.GetData().Which() ) { case CSeqFeatData::e_Cdregion: ConvertCdregion(ref, orig_feat, mapped_feat); break; case CSeqFeatData::e_Rna: ConvertRna(ref, orig_feat, mapped_feat); break; default: break; } } void CSeq_loc_Conversion::Convert(CAnnotObject_Ref& ref, ELocationType loctype) { Reset(); CAnnotMapping_Info& map_info = ref.GetMappingInfo(); const CAnnotObject_Info& obj = ref.GetAnnotObject_Info(); switch ( obj.Which() ) { case CSeq_annot::C_Data::e_Ftable: { if ( NeedFullFeature(ref, loctype) ) { CConstRef<CSeq_feat> orig_feat; if ( obj.IsRegular() ) { orig_feat = obj.GetFeatFast(); } else { CRef<CSeq_feat> created_feat; CRef<CSeq_point> created_point; CRef<CSeq_interval> created_interval; const CSeq_annot_Info& annot = obj.GetSeq_annot_Info(); annot.UpdateTableFeat(created_feat, created_point, created_interval, obj); orig_feat = created_feat; } CRef<CSeq_feat> mapped_feat; CRef<CSeq_loc> mapped_loc; if ( loctype == eLocation ) { ConvertFeature(ref, *orig_feat, mapped_feat); Convert(orig_feat->GetLocation(), mapped_loc); } else { Convert(orig_feat->GetProduct(), mapped_loc); } map_info.SetMappedSeq_loc(mapped_loc.GetPointerOrNull()); if ( mapped_feat ) { // SetMappedLocation must be called before SetMappedSeq_feat SetMappedLocation(ref, loctype); // This will also set location and partial of mapped feature map_info.SetMappedSeq_feat(*mapped_feat); return; } } else { CConstRef<CSeq_loc> orig_loc; if ( obj.IsRegular() ) { if ( loctype == eLocation ) { orig_loc = &obj.GetFeatFast()->GetLocation(); } else { orig_loc = &obj.GetFeatFast()->GetProduct(); } } else { CRef<CSeq_loc> created_loc; CRef<CSeq_point> created_point; CRef<CSeq_interval> created_interval; const CSeq_annot_Info& annot = obj.GetSeq_annot_Info(); if ( loctype == eLocation ) { annot.UpdateTableFeatLocation(created_loc, created_point, created_interval, obj); } else { annot.UpdateTableFeatProduct(created_loc, created_point, created_interval, obj); } orig_loc = created_loc; } CRef<CSeq_loc> mapped_loc; Convert(*orig_loc, mapped_loc); map_info.SetMappedSeq_loc(mapped_loc.GetPointerOrNull()); } break; } case CSeq_annot::C_Data::e_Graph: { CRef<CSeq_loc> mapped_loc; m_GraphRanges.Reset(new CGraphRanges); Convert(obj.GetGraphFast()->GetLoc(), mapped_loc); map_info.SetMappedSeq_loc(mapped_loc.GetPointerOrNull()); map_info.SetGraphRanges(m_GraphRanges.GetPointerOrNull()); break; } default: _ASSERT(0); break; } SetMappedLocation(ref, loctype); } void CSeq_loc_Conversion::Convert(CAnnotObject_Ref& ref, ELocationType loctype, const CSeq_id_Handle& id, const CRange<TSeqPos>& range, const SAnnotObject_Index& index) { Reset(); CAnnotMapping_Info& map_info = ref.GetMappingInfo(); const CAnnotObject_Info& obj = ref.GetAnnotObject_Info(); switch ( obj.Which() ) { case CSeq_annot::C_Data::e_Ftable: { if ( NeedFullFeature(ref, loctype) ) { CConstRef<CSeq_feat> orig_feat; if ( obj.IsRegular() ) { orig_feat = obj.GetFeatFast(); } else { CRef<CSeq_feat> created_feat; CRef<CSeq_point> created_point; CRef<CSeq_interval> created_interval; const CSeq_annot_Info& annot = obj.GetSeq_annot_Info(); annot.UpdateTableFeat(created_feat, created_point, created_interval, obj); orig_feat = created_feat; } CRef<CSeq_feat> mapped_feat; CRef<CSeq_loc> mapped_loc; if ( loctype == eLocation ) { ConvertFeature(ref, *orig_feat, mapped_feat); Convert(orig_feat->GetLocation(), mapped_loc, eCnvAlways); } else { Convert(orig_feat->GetProduct(), mapped_loc, eCnvAlways); } map_info.SetMappedSeq_loc(mapped_loc.GetPointerOrNull()); if ( mapped_feat ) { // SetMappedLocation must be called before SetMappedSeq_feat SetMappedLocation(ref, loctype); // This will also set location and partial of mapped feature map_info.SetMappedSeq_feat(*mapped_feat); return; } } else if ( index.LocationIsSimple() ) { // simple conversion of location is possible // no need for ConvertFeature ConvertSimpleLoc(id, range, index); } else { CConstRef<CSeq_loc> orig_loc; if ( obj.IsRegular() ) { if ( loctype == eLocation ) { orig_loc = &obj.GetFeatFast()->GetLocation(); } else { orig_loc = &obj.GetFeatFast()->GetProduct(); } } else { CRef<CSeq_loc> created_loc; CRef<CSeq_point> created_point; CRef<CSeq_interval> created_interval; const CSeq_annot_Info& annot = obj.GetSeq_annot_Info(); if ( loctype == eLocation ) { annot.UpdateTableFeatLocation(created_loc, created_point, created_interval, obj); } else { annot.UpdateTableFeatProduct(created_loc, created_point, created_interval, obj); } orig_loc = created_loc; } CRef<CSeq_loc> mapped_loc; Convert(*orig_loc, mapped_loc); map_info.SetMappedSeq_loc(mapped_loc.GetPointerOrNull()); } break; } case CSeq_annot::C_Data::e_Graph: { CRef<CSeq_loc> mapped_loc; m_GraphRanges.Reset(new CGraphRanges); Convert(obj.GetGraphFast()->GetLoc(), mapped_loc); map_info.SetMappedSeq_loc(mapped_loc.GetPointerOrNull()); map_info.SetGraphRanges(m_GraphRanges.GetPointerOrNull()); break; } case CSeq_annot::C_Data::e_Seq_table: { CRef<CSeq_loc> mapped_loc; const CSeq_annot_Info& annot = obj.GetSeq_annot_Info(); const CSeqTableInfo& table = annot.GetTableInfo(); CConstRef<CSeq_loc> loc = table.GetTableLocation(); if ( loc ) { Convert(*loc, mapped_loc); map_info.SetMappedSeq_loc(mapped_loc.GetPointerOrNull()); } break; } default: _ASSERT(0); break; } SetMappedLocation(ref, loctype); } void CSeq_loc_Conversion::SetMappedLocation(CAnnotObject_Ref& ref, ELocationType loctype) { CAnnotMapping_Info& map_info = ref.GetMappingInfo(); map_info.SetProduct(loctype == eProduct); map_info.SetPartial(m_Partial || map_info.IsPartial()); map_info.SetTotalRange(m_TotalRange); if ( IsSpecialLoc() ) { if ( m_DstFuzz_from || m_DstFuzz_to ) { CRef<CSeq_loc> mapped_loc; SetDstLoc(mapped_loc); map_info.SetMappedSeq_loc(mapped_loc); } else if ( m_LastType != eMappedObjType_Seq_loc_mix ) { // special interval or point map_info.SetMappedSeq_id(GetDstId(), m_LastType == eMappedObjType_Seq_point); map_info.SetMappedStrand(m_LastStrand); if ( m_PartialFlag & fPartial_from ) { map_info.SetMappedPartial_from(); } if ( m_PartialFlag & fPartial_to ) { map_info.SetMappedPartial_to(); } } else { // special mix map_info.SetMappedConverstion(*this); map_info.SetMappedStrand(m_LastStrand); } m_LastType = eMappedObjType_not_set; } else if ( map_info.GetMappedObjectType() == CAnnotMapping_Info::eMappedObjType_not_set ) { if ( m_Partial ) { // set empty location map_info.SetMappedSeq_loc(m_Dst_loc_Empty); } } } CSeq_loc_mix::Tdata* CSeq_loc_Conversion::s_ConvertToMix(CRef<CSeq_loc>& loc) { if ( !loc ) { loc.Reset(new CSeq_loc); return &loc->SetMix().Set(); } if (loc->IsMix()) return &loc->SetMix().Set(); CRef<CSeq_loc> new_dst(new CSeq_loc); CSeq_loc_mix::Tdata* dst_mix = &new_dst->SetMix().Set(); switch (loc->Which()) { case CSeq_loc::e_Packed_int: NON_CONST_ITERATE(CPacked_seqint::Tdata, ii, loc->SetPacked_int().Set()) { CRef<CSeq_loc> mix_loc(new CSeq_loc); mix_loc->SetInt(**ii); dst_mix->push_back(mix_loc); } break; case CSeq_loc::e_Packed_pnt: { CPacked_seqpnt& ppnt = loc->SetPacked_pnt(); NON_CONST_ITERATE(CPacked_seqpnt::TPoints, pi, ppnt.SetPoints()) { CRef<CSeq_loc> mix_loc(new CSeq_loc); mix_loc->SetPnt().SetId(ppnt.SetId()); if (ppnt.IsSetFuzz()) { mix_loc->SetPnt().SetFuzz(ppnt.SetFuzz()); } if (ppnt.IsSetStrand()) { mix_loc->SetPnt().SetStrand(ppnt.SetStrand()); } mix_loc->SetPnt().SetPoint(*pi); dst_mix->push_back(mix_loc); } break; } default: dst_mix->push_back(loc); break; } loc.Reset(new_dst); return dst_mix; } ///////////////////////////////////////////////////////////////////////////// // CSeq_loc_Conversion_Set ///////////////////////////////////////////////////////////////////////////// CSeq_loc_Conversion_Set::CSeq_loc_Conversion_Set(CHeapScope& scope) : m_SingleConv(0), m_SingleIndex(0), m_Partial(false), m_PartialHasUnconvertedId(false), m_TotalRange(TRange::GetEmpty()), m_Scope(scope) { return; } void CSeq_loc_Conversion_Set::Add(CSeq_loc_Conversion& cvt, unsigned int loc_index) { m_Dst_id_Handles.insert(cvt.GetDst_id_Handle()); if (!m_SingleConv) { m_SingleConv.Reset(&cvt); m_SingleIndex = loc_index; return; } else if ( m_CvtByIndex.empty() ) { x_Add(*m_SingleConv, m_SingleIndex); } x_Add(cvt, loc_index); } void CSeq_loc_Conversion_Set::x_Add(CSeq_loc_Conversion& cvt, unsigned int loc_index) { TIdMap& id_map = m_CvtByIndex[loc_index]; TRangeMap& ranges = id_map[cvt.GetSrc_id_Handle()]; ranges.insert(TRangeMap::value_type(TRange(cvt.GetSrc_from(), cvt.GetSrc_to()), Ref(&cvt))); } CSeq_loc_Conversion_Set::TRangeIterator CSeq_loc_Conversion_Set::BeginRanges(CSeq_id_Handle id, TSeqPos from, TSeqPos to, unsigned int loc_index) { TConvByIndex::iterator index_iter = m_CvtByIndex.find(loc_index); if ( index_iter == m_CvtByIndex.end() ) { index_iter = m_CvtByIndex.find(kAllIndexes); } if ( index_iter != m_CvtByIndex.end() ) { TIdMap::iterator ranges = index_iter->second.find(id); if ( ranges != index_iter->second.end() ) { return ranges->second.begin(TRange(from, to)); } } m_PartialHasUnconvertedId = true; return TRangeIterator(); } void CSeq_loc_Conversion_Set::ConvertCdregion(CAnnotObject_Ref& ref, const CSeq_feat& orig_feat, CRef<CSeq_feat>& mapped_feat) { const CAnnotObject_Info& obj = ref.GetAnnotObject_Info(); _ASSERT( obj.IsFeat() ); const CSeqFeatData& src_feat_data = orig_feat.GetData(); _ASSERT( src_feat_data.IsCdregion() ); if (!src_feat_data.GetCdregion().IsSetCode_break()) { return; } const CCdregion& src_cd = src_feat_data.GetCdregion(); // Map code-break locations const CCdregion::TCode_break& src_cb = src_cd.GetCode_break(); mapped_feat.Reset(new CSeq_feat); // Initialize mapped feature ref.GetMappingInfo().InitializeMappedSeq_feat(*obj.GetFeatFast(), *mapped_feat); // Copy Cd-region, do not change the original one CRef<CSeqFeatData> new_data(new CSeqFeatData); mapped_feat->SetData(*new_data); CCdregion& new_cd = new_data->SetCdregion(); if ( src_cd.IsSetOrf() ) { new_cd.SetOrf(src_cd.GetOrf()); } else { new_cd.ResetOrf(); } if ( src_cd.IsSetFrame() ) { new_cd.SetFrame(src_cd.GetFrame()); } else { new_cd.ResetFrame(); } if ( src_cd.IsSetConflict() ) { new_cd.SetConflict(src_cd.GetConflict()); } else { new_cd.ResetConflict(); } if ( src_cd.IsSetGaps() ) { new_cd.SetGaps(src_cd.GetGaps()); } else { new_cd.ResetGaps(); } if ( src_cd.IsSetMismatch() ) { new_cd.SetMismatch(src_cd.GetMismatch()); } else { new_cd.ResetMismatch(); } if ( src_cd.IsSetCode() ) { new_cd.SetCode(const_cast<CGenetic_code&>(src_cd.GetCode())); } else { new_cd.ResetCode(); } if ( src_cd.IsSetStops() ) { new_cd.SetStops(src_cd.GetStops()); } else { new_cd.ResetStops(); } CCdregion::TCode_break& mapped_cbs = new_cd.SetCode_break(); mapped_cbs.clear(); ITERATE(CCdregion::TCode_break, it, src_cb) { CRef<CSeq_loc> cb_loc; Convert((*it)->GetLoc(), cb_loc, 0); m_TotalRange = TRange::GetEmpty(); if (cb_loc && cb_loc->Which() != CSeq_loc::e_not_set) { CRef<CCode_break> cb(new CCode_break); cb->SetAa(const_cast<CCode_break::TAa&>((*it)->GetAa())); cb->SetLoc(*cb_loc); mapped_cbs.push_back(cb); } } } void CSeq_loc_Conversion_Set::ConvertRna(CAnnotObject_Ref& ref, const CSeq_feat& orig_feat, CRef<CSeq_feat>& mapped_feat) { const CAnnotObject_Info& obj = ref.GetAnnotObject_Info(); _ASSERT( obj.IsFeat() ); const CSeqFeatData& src_feat_data = orig_feat.GetData(); _ASSERT( src_feat_data.IsRna() ); if (!src_feat_data.GetRna().IsSetExt() || !src_feat_data.GetRna().GetExt().IsTRNA() || !src_feat_data.GetRna().GetExt().GetTRNA().IsSetAnticodon()) { return; } const CRNA_ref::TExt& src_ext = src_feat_data.GetRna().GetExt(); // Map anticodon location const CSeq_loc& src_anticodon = src_ext.GetTRNA().GetAnticodon(); mapped_feat.Reset(new CSeq_feat); // Initialize mapped feature ref.GetMappingInfo().InitializeMappedSeq_feat(*obj.GetFeatFast(), *mapped_feat); // Copy RNA-ext, do not change the original one CRef<CRNA_ref::TExt> new_ext(new CRNA_ref::TExt); // Shallow-copy the feature, replace data.rna.ext.trna.anticodon // with the mapped location mapped_feat->Assign(*obj.GetFeatFast(), eShallow); mapped_feat->SetData(*(new CSeqFeatData)); mapped_feat->SetData().Assign(src_feat_data, eShallow); mapped_feat->SetData().SetRna(*(new CRNA_ref)); mapped_feat->SetData().SetRna().SetType(src_feat_data.GetRna().GetType()); if ( src_feat_data.GetRna().IsSetPseudo() ) { mapped_feat->SetData().SetRna().SetPseudo( src_feat_data.GetRna().GetPseudo()); } else { mapped_feat->SetData().SetRna().ResetPseudo(); } mapped_feat->SetData().SetRna().SetExt().SetTRNA().SetAa( const_cast<CTrna_ext::C_Aa&>(src_ext.GetTRNA().GetAa())); if ( src_ext.GetTRNA().IsSetCodon() ) { mapped_feat->SetData().SetRna().SetExt().SetTRNA().SetCodon() = src_ext.GetTRNA().GetCodon(); } else { mapped_feat->SetData().SetRna().SetExt().SetTRNA().ResetCodon(); } CRef<CSeq_loc> ac_loc; Convert(src_anticodon, ac_loc, 0); // Preserve partial flag m_TotalRange = TRange::GetEmpty(); if (ac_loc && ac_loc->Which() != CSeq_loc::e_not_set) { mapped_feat->SetData() .SetRna().SetExt().SetTRNA().SetAnticodon(*ac_loc); } else { mapped_feat->SetData() .SetRna().SetExt().SetTRNA().ResetAnticodon(); } } void CSeq_loc_Conversion_Set::ConvertFeature(CAnnotObject_Ref& ref, const CSeq_feat& orig_feat, CRef<CSeq_feat>& mapped_feat) { switch ( orig_feat.GetData().Which() ) { case CSeqFeatData::e_Cdregion: ConvertCdregion(ref, orig_feat, mapped_feat); break; case CSeqFeatData::e_Rna: ConvertRna(ref, orig_feat, mapped_feat); break; default: break; } } void CSeq_loc_Conversion_Set::Reset(void) { m_TotalRange = TRange::GetEmpty(); m_Partial = false; m_PartialHasUnconvertedId = false; m_GraphRanges.Reset(); } void CSeq_loc_Conversion_Set::Convert(CAnnotObject_Ref& ref, CSeq_loc_Conversion::ELocationType loctype) { if ( !m_SingleConv ) { _ASSERT(m_CvtByIndex.empty()); // Special case - empty set for filtering duplicates only, // no mapping required return; } if ( m_CvtByIndex.empty() && !ref.IsAlign() ) { // No multiple mappings m_SingleConv->Convert(ref, loctype); m_Partial = m_SingleConv->IsPartial(); m_PartialHasUnconvertedId = m_SingleConv->HasUnconvertedId(); m_TotalRange = m_SingleConv->GetTotalRange(); m_GraphRanges = m_SingleConv->m_GraphRanges; return; } Reset(); CRef<CSeq_feat> mapped_feat; CAnnotMapping_Info& map_info = ref.GetMappingInfo(); const CAnnotObject_Info& obj = ref.GetAnnotObject_Info(); switch ( obj.Which() ) { case CSeq_annot::C_Data::e_Ftable: { CRef<CSeq_loc> mapped_loc; const CSeq_loc* src_loc; unsigned int loc_index = 0; if ( loctype != CSeq_loc_Conversion::eProduct ) { ConvertFeature(ref, *obj.GetFeatFast(), mapped_feat); src_loc = &obj.GetFeatFast()->GetLocation(); } else { src_loc = &obj.GetFeatFast()->GetProduct(); loc_index = 1; } Convert(*src_loc, mapped_loc, loc_index); map_info.SetMappedSeq_loc(mapped_loc.GetPointerOrNull()); break; } case CSeq_annot::C_Data::e_Graph: { CRef<CSeq_loc> mapped_loc; m_GraphRanges.Reset(new CGraphRanges); Convert(obj.GetGraphFast()->GetLoc(), mapped_loc, 0); map_info.SetMappedSeq_loc(mapped_loc.GetPointerOrNull()); map_info.SetGraphRanges(m_GraphRanges.GetPointerOrNull()); break; } case CSeq_annot::C_Data::e_Align: { map_info.SetMappedSeq_align_Cvts(*this); break; } default: _ASSERT(0); break; } map_info.SetProduct(loctype == CSeq_loc_Conversion::eProduct); map_info.SetPartial(m_Partial || map_info.IsPartial()); map_info.SetTotalRange(m_TotalRange); if ( mapped_feat ) { // This will also set location and partial of the mapped feature map_info.SetMappedSeq_feat(*mapped_feat); } } bool CSeq_loc_Conversion_Set::ConvertPoint(const CSeq_point& src, CRef<CSeq_loc>& dst, unsigned int loc_index) { bool res = false; TRangeIterator mit = BeginRanges(CSeq_id_Handle::GetHandle(src.GetId()), src.GetPoint(), src.GetPoint(), loc_index); for ( ; mit; ++mit) { CSeq_loc_Conversion& cvt = *mit->second; cvt.Reset(); if (cvt.ConvertPoint(src)) { dst->SetPnt(*cvt.GetDstPoint()); m_TotalRange += cvt.GetTotalRange(); res = true; break; } } if ( !res && m_GraphRanges ) { m_GraphRanges->IncOffset(1); } m_Partial |= !res; return res; } namespace { struct FConversions_Less { bool operator()(const CSeq_loc_Conversion& cvt1, const CSeq_loc_Conversion& cvt2) const { if ( cvt1.GetSrc_from() != cvt2.GetSrc_from() ) { return cvt1.GetSrc_from() < cvt2.GetSrc_from(); } if ( cvt1.GetSrc_to() != cvt2.GetSrc_to() ) { return cvt1.GetSrc_to() > cvt2.GetSrc_to(); } //return &cvt1 < &cvt2; return false; } bool operator()(const CRef<CSeq_loc_Conversion>& cvt1, const CRef<CSeq_loc_Conversion>& cvt2) const { return (*this)(*cvt1, *cvt2); } }; struct FConversions_ReverseLess { bool operator()(const CSeq_loc_Conversion& cvt1, const CSeq_loc_Conversion& cvt2) const { if ( cvt1.GetSrc_to() != cvt2.GetSrc_to() ) { return cvt1.GetSrc_to() > cvt2.GetSrc_to(); } if ( cvt1.GetSrc_from() != cvt2.GetSrc_from() ) { return cvt1.GetSrc_from() < cvt2.GetSrc_from(); } //return &cvt1 < &cvt2; return false; } bool operator()(const CRef<CSeq_loc_Conversion>& cvt1, const CRef<CSeq_loc_Conversion>& cvt2) const { return (*this)(*cvt1, *cvt2); } }; struct FConversions_Equal { bool operator()(const CSeq_loc_Conversion& cvt1, const CSeq_loc_Conversion& cvt2) const { return cvt1.GetSrc_from() == cvt2.GetSrc_from() && cvt1.GetSrc_to() == cvt2.GetSrc_to(); } bool operator()(const CRef<CSeq_loc_Conversion>& cvt1, const CRef<CSeq_loc_Conversion>& cvt2) const { return (*this)(*cvt1, *cvt2); } }; } bool CSeq_loc_Conversion_Set::ConvertInterval(const CSeq_interval& src, CRef<CSeq_loc>& dst, unsigned int loc_index) { CRef<CSeq_loc> tmp(new CSeq_loc); CPacked_seqint::Tdata& ints = tmp->SetPacked_int().Set(); TRange total_range(TRange::GetEmpty()); bool revert_order = (src.IsSetStrand() && IsReverse(src.GetStrand())); bool res = false; typedef vector< CRef<CSeq_loc_Conversion> > TConversions; TConversions cvts; TRangeIterator mit = BeginRanges(CSeq_id_Handle::GetHandle(src.GetId()), src.GetFrom(), src.GetTo(), loc_index); for ( ; mit; ++mit) { cvts.push_back(mit->second); } if ( revert_order ) { reverse(cvts.begin(), cvts.end()); stable_sort(cvts.begin(), cvts.end(), FConversions_ReverseLess()); cvts.erase(unique(cvts.begin(), cvts.end(), FConversions_Equal()), cvts.end()); } else { stable_sort(cvts.begin(), cvts.end(), FConversions_Less()); cvts.erase(unique(cvts.begin(), cvts.end(), FConversions_Equal()), cvts.end()); } CRef<CSeq_interval> last_int; TSeqPos last_to = kInvalidSeqPos; TSeqPos graph_offset = m_GraphRanges ? m_GraphRanges->GetOffset() : 0; NON_CONST_ITERATE ( TConversions, it, cvts ) { CRef<CSeq_loc_Conversion> cvt = *it; cvt->Reset(); cvt->m_GraphRanges = m_GraphRanges; if (cvt->ConvertInterval(src)) { CRef<CSeq_interval> mapped = cvt->GetDstInterval(); if ( revert_order ) { if (last_int && cvt->GetSrc_to() == last_to - 1) { last_int->SetPartialStop(false, eExtreme_Biological); mapped->SetPartialStart(false, eExtreme_Biological); //last_int->ResetFuzz_from(); //mapped->ResetFuzz_to(); } last_to = cvt->GetSrc_from(); } else { if (last_int && cvt->GetSrc_from() == last_to + 1) { last_int->SetPartialStop(false, eExtreme_Biological); mapped->SetPartialStart(false, eExtreme_Biological); //last_int->ResetFuzz_to(); //mapped->ResetFuzz_from(); } last_to = cvt->GetSrc_to(); } last_int = mapped; ints.push_back(mapped); total_range += cvt->GetTotalRange(); res = true; } if (m_GraphRanges) { // All conversions start with the same offset m_GraphRanges->SetOffset(graph_offset); } } if ( m_GraphRanges ) { // Now it's time to update the offset m_GraphRanges->IncOffset(src.GetLength()); } if (ints.size() > 1) { dst.Reset(tmp); } else if (ints.size() == 1) { dst->SetInt(**ints.begin()); } m_TotalRange += total_range; // does not guarantee the whole interval is mapped, but should work // in normal situations m_Partial |= (!res || src.GetLength() > total_range.GetLength()); return res; } bool CSeq_loc_Conversion_Set::ConvertPacked_int(const CSeq_loc& src, CRef<CSeq_loc>& dst, unsigned int loc_index) { bool res = false; _ASSERT(src.Which() == CSeq_loc::e_Packed_int); const CPacked_seqint::Tdata& src_ints = src.GetPacked_int().Get(); CPacked_seqint::Tdata* dst_ints = &dst->SetPacked_int().Set(); // If the result contains NULLs, may need to use mix instead. CSeq_loc_mix::Tdata* dst_mix = 0; bool last_truncated = false; ITERATE ( CPacked_seqint::Tdata, i, src_ints ) { CRef<CSeq_loc> dst_int(new CSeq_loc); bool mapped = ConvertInterval(**i, dst_int, loc_index); if (mapped) { if ( last_truncated && !CSeq_loc_Mapper_Base::GetNonMappingAsNull() && !dst_int->IsPartialStart(eExtreme_Biological) ) { dst_int->SetPartialStart(true, eExtreme_Biological); } if (dst_mix) { dst_mix->push_back(dst_int); } else { if ( dst_int->IsInt() ) { dst_ints->push_back(CRef<CSeq_interval>(&dst_int->SetInt())); } else if ( dst_int->IsPacked_int() ) { dst_ints->splice(dst_ints->end(), dst_int->SetPacked_int().Set()); } else { dst_mix = CSeq_loc_Conversion::s_ConvertToMix(dst); _ASSERT(dst_mix); dst_mix->push_back(dst_int); } } } else { if (last_truncated) continue; if (CSeq_loc_Mapper_Base::GetNonMappingAsNull()) { dst_mix = CSeq_loc_Conversion::s_ConvertToMix(dst); _ASSERT(dst_mix); CRef<CSeq_loc> null_loc(new CSeq_loc); null_loc->SetNull(); dst_mix->push_back(null_loc); } else if (!dst->IsPartialStop(eExtreme_Biological)) { dst->SetPartialStop(true, eExtreme_Biological); } } m_Partial |= !mapped; res |= mapped; last_truncated = !mapped; } return res; } bool CSeq_loc_Conversion_Set::ConvertPacked_pnt(const CSeq_loc& src, CRef<CSeq_loc>& dst, unsigned int loc_index) { bool res = false; _ASSERT(src.Which() == CSeq_loc::e_Packed_pnt); const CPacked_seqpnt& src_pack_pnts = src.GetPacked_pnt(); const CPacked_seqpnt::TPoints& src_pnts = src_pack_pnts.GetPoints(); // using mix, not point, since mappings may have // different strand, fuzz etc. if (!dst) { dst.Reset(new CSeq_loc); } CSeq_loc_mix::Tdata& locs = dst->SetMix().Set(); ITERATE ( CPacked_seqpnt::TPoints, i, src_pnts ) { bool mapped = false; TSeqPos graph_offset = m_GraphRanges ? m_GraphRanges->GetOffset() : 0; TRangeIterator mit = BeginRanges( CSeq_id_Handle::GetHandle(src_pack_pnts.GetId()), *i, *i, loc_index); for ( ; mit; ++mit) { CSeq_loc_Conversion& cvt = *mit->second; cvt.Reset(); if ( !cvt.GoodSrcId(src_pack_pnts.GetId()) ) { continue; } TSeqPos dst_pos = cvt.ConvertPoint(*i, src_pack_pnts.IsSetStrand() ? src_pack_pnts.GetStrand() : eNa_strand_unknown); if ( dst_pos != kInvalidSeqPos ) { CRef<CSeq_loc> pnt(new CSeq_loc); pnt->SetPnt(*cvt.GetDstPoint()); _ASSERT(pnt); locs.push_back(pnt); m_TotalRange += cvt.GetTotalRange(); mapped = true; break; } if ( m_GraphRanges ) { m_GraphRanges->SetOffset(graph_offset); } } if ( m_GraphRanges ) { m_GraphRanges->IncOffset(1); } if (!mapped && CSeq_loc_Mapper::GetNonMappingAsNull()) { CRef<CSeq_loc> null_loc(new CSeq_loc); null_loc->SetNull(); locs.push_back(null_loc); } m_Partial |= !mapped; res |= mapped; } return res; } bool CSeq_loc_Conversion_Set::ConvertMix(const CSeq_loc& src, CRef<CSeq_loc>& dst, unsigned int loc_index) { bool res = false; _ASSERT(src.Which() == CSeq_loc::e_Mix); const CSeq_loc_mix::Tdata& src_mix = src.GetMix().Get(); CRef<CSeq_loc> dst_loc; CSeq_loc_mix::Tdata& dst_mix = dst->SetMix().Set(); bool last_truncated = false; ITERATE ( CSeq_loc_mix::Tdata, i, src_mix ) { dst_loc.Reset(new CSeq_loc); if ( Convert(**i, dst_loc, loc_index) ) { _ASSERT(dst_loc); if ( last_truncated && !CSeq_loc_Mapper::GetNonMappingAsNull() && !dst_loc->IsPartialStart(eExtreme_Biological) ) { dst_loc->SetPartialStart(true, eExtreme_Biological); } dst_mix.push_back(dst_loc); res = true; last_truncated = false; } else { if (last_truncated) continue; if (CSeq_loc_Mapper::GetNonMappingAsNull()) { CRef<CSeq_loc> null_loc(new CSeq_loc); null_loc->SetNull(); dst_mix.push_back(null_loc); } else if (!dst->IsPartialStop(eExtreme_Biological)) { dst->SetPartialStop(true, eExtreme_Biological); } last_truncated = true; } } m_Partial |= !res; return res; } bool CSeq_loc_Conversion_Set::ConvertEquiv(const CSeq_loc& src, CRef<CSeq_loc>& dst, unsigned int loc_index) { bool res = false; _ASSERT(src.Which() == CSeq_loc::e_Equiv); const CSeq_loc_equiv::Tdata& src_equiv = src.GetEquiv().Get(); CRef<CSeq_loc> dst_loc; CSeq_loc_equiv::Tdata& dst_equiv = dst->SetEquiv().Set(); ITERATE ( CSeq_loc_equiv::Tdata, i, src_equiv ) { if ( Convert(**i, dst_loc, loc_index) || CSeq_loc_Mapper::GetNonMappingAsNull() ) { if (!dst_loc) { dst_loc.Reset(new CSeq_loc); dst_loc->SetNull(); } dst_equiv.push_back(dst_loc); res = true; } } m_Partial |= !res; return res; } bool CSeq_loc_Conversion_Set::ConvertBond(const CSeq_loc& src, CRef<CSeq_loc>& dst, unsigned int loc_index) { bool res = false; _ASSERT(src.Which() == CSeq_loc::e_Bond); const CSeq_bond& src_bond = src.GetBond(); // using mix, not bond, since mappings may have // different strand, fuzz etc. dst->SetBond(); CRef<CSeq_point> pntA; CRef<CSeq_point> pntB; {{ TRangeIterator mit = BeginRanges( CSeq_id_Handle::GetHandle(src_bond.GetA().GetId()), src_bond.GetA().GetPoint(), src_bond.GetA().GetPoint(), loc_index); for ( ; mit && !pntA; ++mit) { CSeq_loc_Conversion& cvt = *mit->second; cvt.Reset(); if (cvt.ConvertPoint(src_bond.GetA())) { pntA = cvt.GetDstPoint(); m_TotalRange += cvt.GetTotalRange(); res = true; } } }} if ( src_bond.IsSetB() ) { TRangeIterator mit = BeginRanges( CSeq_id_Handle::GetHandle(src_bond.GetB().GetId()), src_bond.GetB().GetPoint(), src_bond.GetB().GetPoint(), loc_index); for ( ; mit && !pntB; ++mit) { CSeq_loc_Conversion& cvt = *mit->second; cvt.Reset(); if (!pntB && cvt.ConvertPoint(src_bond.GetB())) { pntB = cvt.GetDstPoint(); m_TotalRange += cvt.GetTotalRange(); res = true; } } } CSeq_bond& dst_bond = dst->SetBond(); if ( pntA || pntB ) { if ( pntA ) { dst_bond.SetA(*pntA); } else { dst_bond.SetA().Assign(src_bond.GetA()); } if ( pntB ) { dst_bond.SetB(*pntB); } else if ( src_bond.IsSetB() ) { dst_bond.SetB().Assign(src_bond.GetB()); } } m_Partial |= (!pntA || !pntB); return res; } bool CSeq_loc_Conversion_Set::Convert(const CSeq_loc& src, CRef<CSeq_loc>& dst, unsigned int loc_index) { dst.Reset(new CSeq_loc); bool res = false; switch ( src.Which() ) { case CSeq_loc::e_not_set: case CSeq_loc::e_Feat: // Nothing to do, although this should never happen -- // the seq_loc is intersecting with the conv. loc. _ASSERT("this cannot happen" && 0); break; case CSeq_loc::e_Null: { dst->SetNull(); res = true; break; } case CSeq_loc::e_Empty: { TRangeIterator mit = BeginRanges(CSeq_id_Handle::GetHandle(src.GetEmpty()), TRange::GetWhole().GetFrom(), TRange::GetWhole().GetTo(), loc_index); for ( ; mit; ++mit) { CSeq_loc_Conversion& cvt = *mit->second; cvt.Reset(); if ( cvt.GoodSrcId(src.GetEmpty()) ) { dst->SetEmpty(cvt.GetDstId()); res = true; break; } } break; } case CSeq_loc::e_Whole: { const CSeq_id& src_id = src.GetWhole(); // Convert to the allowed master seq interval CSeq_interval whole_int; whole_int.SetId().Assign(src_id); whole_int.SetFrom(0); CBioseq_Handle bh = m_Scope->GetBioseqHandle(CSeq_id_Handle::GetHandle(src_id), CScope::eGetBioseq_All); whole_int.SetTo(bh.GetBioseqLength()); res = ConvertInterval(whole_int, dst, loc_index); break; } case CSeq_loc::e_Int: { res = ConvertInterval(src.GetInt(), dst, loc_index); break; } case CSeq_loc::e_Pnt: { res = ConvertPoint(src.GetPnt(), dst, loc_index); break; } case CSeq_loc::e_Packed_int: { res = ConvertPacked_int(src, dst, loc_index); break; } case CSeq_loc::e_Packed_pnt: { res = ConvertPacked_pnt(src, dst, loc_index); break; } case CSeq_loc::e_Mix: { res = ConvertMix(src, dst, loc_index); break; } case CSeq_loc::e_Equiv: { res = ConvertEquiv(src, dst, loc_index); break; } case CSeq_loc::e_Bond: { res = ConvertBond(src, dst, loc_index); break; } default: NCBI_THROW(CAnnotException, eBadLocation, "Unsupported location type"); } return res; } void CSeq_loc_Conversion_Set::Convert(const CSeq_align& src, CRef<CSeq_align>& dst) { CSeq_loc_Mapper loc_mapper(0, NULL); CSeq_align_Mapper mapper(src, loc_mapper); mapper.Convert(*this); dst = mapper.GetDstAlign(); } END_SCOPE(objects) END_NCBI_SCOPE

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.