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
|
/*
* Copyright 2011, Ben Langmead <langmea@cs.jhu.edu>
*
* This file is part of Bowtie 2.
*
* Bowtie 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bowtie 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bowtie 2. If not, see <http://www.gnu.org/licenses/>.
*/
#define TIMER_START() \
struct timeval tv_i, tv_f; \
struct timezone tz_i, tz_f; \
size_t total_usecs; \
gettimeofday(&tv_i, &tz_i)
#define IF_TIMER_END() \
gettimeofday(&tv_f, &tz_f); \
total_usecs = \
(tv_f.tv_sec - tv_i.tv_sec) * 1000000 + (tv_f.tv_usec - tv_i.tv_usec); \
if(total_usecs > 300000)
/*
* aligner_sw_driver.cpp
*
* Routines that drive the alignment process given a collection of seed hits.
* This is generally done in a few stages: extendSeeds visits the set of
* seed-hit BW elements in some order; for each element visited it resolves its
* reference offset; once the reference offset is known, bounds for a dynamic
* programming subproblem are established; if these bounds are distinct from
* the bounds we've already tried, we solve the dynamic programming subproblem
* and report the hit; if the AlnSinkWrap indicates that we can stop, we
* return, otherwise we continue on to the next BW element.
*/
#include <iostream>
#include "aligner_cache.h"
#include "aligner_sw_driver.h"
#include "pe.h"
#include "dp_framer.h"
// -- BTL remove --
#include <stdlib.h>
#include <sys/time.h>
// -- --
using namespace std;
/**
* Given end-to-end alignment results stored in the SeedResults structure, set
* up all of our state for resolving and keeping track of reference offsets for
* hits. Order the list of ranges to examine such that all exact end-to-end
* alignments are examined before any 1mm end-to-end alignments.
*
* Note: there might be a lot of hits and a lot of wide ranges to look for
* here. We use 'maxelt'.
*/
bool SwDriver::eeSaTups(
const Read& rd, // read
SeedResults& sh, // seed hits to extend into full alignments
const Ebwt& ebwt, // BWT
const BitPairReference& ref, // Reference strings
RandomSource& rnd, // pseudo-random generator
WalkMetrics& wlm, // group walk left metrics
SwMetrics& swmSeed, // metrics for seed extensions
size_t& nelt_out, // out: # elements total
size_t maxelt, // max elts we'll consider
bool all) // report all hits?
{
assert_eq(0, nelt_out);
gws_.clear();
rands_.clear();
satpos_.clear();
eehits_.clear();
// First, count up the total number of satpos_, rands_, eehits_, and gws_
// we're going to tuse
size_t nobj = 0;
if(!sh.exactFwEEHit().empty()) nobj++;
if(!sh.exactRcEEHit().empty()) nobj++;
nobj += sh.mm1EEHits().size();
nobj = min(nobj, maxelt);
gws_.ensure(nobj);
rands_.ensure(nobj);
satpos_.ensure(nobj);
eehits_.ensure(nobj);
size_t tot = sh.exactFwEEHit().size() + sh.exactRcEEHit().size();
bool succ = false;
bool firstEe = true;
bool done = false;
if(tot > 0) {
bool fwFirst = true;
// Pick fw / rc to go first in a weighted random fashion
#ifdef BOWTIE_64BIT_INDEX
TIndexOffU rn64 = rnd.nextU64();
TIndexOffU rn = rn64 % (uint64_t)tot;
#else
TIndexOffU rn32 = rnd.nextU32();
TIndexOffU rn = rn32 % (uint32_t)tot;
#endif
if(rn >= sh.exactFwEEHit().size()) {
fwFirst = false;
}
for(int fwi = 0; fwi < 2 && !done; fwi++) {
bool fw = ((fwi == 0) == fwFirst);
EEHit hit = fw ? sh.exactFwEEHit() : sh.exactRcEEHit();
if(hit.empty()) {
continue;
}
assert(hit.fw == fw);
if(hit.bot > hit.top) {
// Possibly adjust bot and width if we would have exceeded maxelt
TIndexOffU tops[2] = { hit.top, 0 };
TIndexOffU bots[2] = { hit.bot, 0 };
TIndexOffU width = hit.bot - hit.top;
if(nelt_out + width > maxelt) {
TIndexOffU trim = (TIndexOffU)((nelt_out + width) - maxelt);
#ifdef BOWTIE_64BIT_INDEX
TIndexOffU rn = rnd.nextU64() % width;
#else
TIndexOffU rn = rnd.nextU32() % width;
#endif
TIndexOffU newwidth = width - trim;
if(hit.top + rn + newwidth > hit.bot) {
// Two pieces
tops[0] = hit.top + rn;
bots[0] = hit.bot;
tops[1] = hit.top;
bots[1] = hit.top + newwidth - (bots[0] - tops[0]);
} else {
// One piece
tops[0] = hit.top + rn;
bots[0] = tops[0] + newwidth;
}
assert_leq(bots[0], hit.bot);
assert_leq(bots[1], hit.bot);
assert_geq(bots[0], tops[0]);
assert_geq(bots[1], tops[1]);
assert_eq(newwidth, (bots[0] - tops[0]) + (bots[1] - tops[1]));
}
for(int i = 0; i < 2 && !done; i++) {
if(bots[i] <= tops[i]) break;
TIndexOffU width = bots[i] - tops[i];
TIndexOffU top = tops[i];
// Clear list where resolved offsets are stored
swmSeed.exranges++;
swmSeed.exrows += width;
if(!succ) {
swmSeed.exsucc++;
succ = true;
}
if(firstEe) {
salistEe_.clear();
pool_.clear();
firstEe = false;
}
// We have to be careful not to allocate excessive amounts of memory here
TSlice o(salistEe_, (TIndexOffU)salistEe_.size(), width);
for(TIndexOffU i = 0; i < width; i++) {
if(!salistEe_.add(pool_, OFF_MASK)) {
swmSeed.exooms++;
return false;
}
}
assert(!done);
eehits_.push_back(hit);
satpos_.expand();
satpos_.back().sat.init(SAKey(), top, OFF_MASK, o);
satpos_.back().sat.key.seq = MAX_U64;
satpos_.back().sat.key.len = (uint32_t)rd.length();
satpos_.back().pos.init(fw, 0, 0, (uint32_t)rd.length());
satpos_.back().origSz = width;
rands_.expand();
rands_.back().init(width, all);
gws_.expand();
SARangeWithOffs<TSlice> sa;
sa.topf = satpos_.back().sat.topf;
sa.len = satpos_.back().sat.key.len;
sa.offs = satpos_.back().sat.offs;
gws_.back().init(
ebwt, // forward Bowtie index
ref, // reference sequences
sa, // SATuple
rnd, // pseudo-random generator
wlm); // metrics
assert(gws_.back().repOk(sa));
nelt_out += width;
if(nelt_out >= maxelt) {
done = true;
}
}
}
}
}
succ = false;
if(!done && !sh.mm1EEHits().empty()) {
sh.sort1mmEe(rnd);
size_t sz = sh.mm1EEHits().size();
for(size_t i = 0; i < sz && !done; i++) {
EEHit hit = sh.mm1EEHits()[i];
assert(hit.repOk(rd));
assert(!hit.empty());
// Possibly adjust bot and width if we would have exceeded maxelt
TIndexOffU tops[2] = { hit.top, 0 };
TIndexOffU bots[2] = { hit.bot, 0 };
TIndexOffU width = hit.bot - hit.top;
if(nelt_out + width > maxelt) {
TIndexOffU trim = (TIndexOffU)((nelt_out + width) - maxelt);
#ifdef BOWTIE_64BIT_INDEX
TIndexOffU rn = rnd.nextU64() % width;
#else
TIndexOffU rn = rnd.nextU32() % width;
#endif
TIndexOffU newwidth = width - trim;
if(hit.top + rn + newwidth > hit.bot) {
// Two pieces
tops[0] = hit.top + rn;
bots[0] = hit.bot;
tops[1] = hit.top;
bots[1] = hit.top + newwidth - (bots[0] - tops[0]);
} else {
// One piece
tops[0] = hit.top + rn;
bots[0] = tops[0] + newwidth;
}
assert_leq(bots[0], hit.bot);
assert_leq(bots[1], hit.bot);
assert_geq(bots[0], tops[0]);
assert_geq(bots[1], tops[1]);
assert_eq(newwidth, (bots[0] - tops[0]) + (bots[1] - tops[1]));
}
for(int i = 0; i < 2 && !done; i++) {
if(bots[i] <= tops[i]) break;
TIndexOffU width = bots[i] - tops[i];
TIndexOffU top = tops[i];
// Clear list where resolved offsets are stored
swmSeed.mm1ranges++;
swmSeed.mm1rows += width;
if(!succ) {
swmSeed.mm1succ++;
succ = true;
}
if(firstEe) {
salistEe_.clear();
pool_.clear();
firstEe = false;
}
TSlice o(salistEe_, (TIndexOffU)salistEe_.size(), width);
for(size_t i = 0; i < width; i++) {
if(!salistEe_.add(pool_, OFF_MASK)) {
swmSeed.mm1ooms++;
return false;
}
}
eehits_.push_back(hit);
satpos_.expand();
satpos_.back().sat.init(SAKey(), top, OFF_MASK, o);
satpos_.back().sat.key.seq = MAX_U64;
satpos_.back().sat.key.len = (uint32_t)rd.length();
satpos_.back().pos.init(hit.fw, 0, 0, (uint32_t)rd.length());
satpos_.back().origSz = width;
rands_.expand();
rands_.back().init(width, all);
gws_.expand();
SARangeWithOffs<TSlice> sa;
sa.topf = satpos_.back().sat.topf;
sa.len = satpos_.back().sat.key.len;
sa.offs = satpos_.back().sat.offs;
gws_.back().init(
ebwt, // forward Bowtie index
ref, // reference sequences
sa, // SATuple
rnd, // pseudo-random generator
wlm); // metrics
assert(gws_.back().repOk(sa));
nelt_out += width;
if(nelt_out >= maxelt) {
done = true;
}
}
}
}
return true;
}
/**
* Extend a seed hit out on either side. Requires that we know the seed hit's
* offset into the read and orientation. Also requires that we know top/bot
* for the seed hit in both the forward and (if we want to extend to the right)
* reverse index.
*/
void SwDriver::extend(
const Read& rd, // read
const Ebwt& ebwtFw, // Forward Bowtie index
const Ebwt* ebwtBw, // Backward Bowtie index
TIndexOffU topf, // top in fw index
TIndexOffU botf, // bot in fw index
TIndexOffU topb, // top in bw index
TIndexOffU botb, // bot in bw index
bool fw, // seed orientation
size_t off, // seed offset from 5' end
size_t len, // seed length
PerReadMetrics& prm, // per-read metrics
size_t& nlex, // # positions we can extend to left w/o edit
size_t& nrex) // # positions we can extend to right w/o edit
{
TIndexOffU t[4], b[4];
TIndexOffU tp[4], bp[4];
SideLocus tloc, bloc;
size_t rdlen = rd.length();
size_t lim = fw ? off : rdlen - len - off;
// We're about to add onto the beginning, so reverse it
#ifndef NDEBUG
if(false) {
// TODO: This will sometimes fail even when the extension is legitimate
// This is because contains() comes in from one extreme or the other,
// whereas we started from the inside and worked outwards. This
// affects which Ns are OK and which are not OK.
// Have to do both because whether we can get through an N depends on
// which direction we're coming in
bool fwContains = ebwtFw.contains(tmp_rdseq_);
tmp_rdseq_.reverse();
bool bwContains = ebwtBw != NULL && ebwtBw->contains(tmp_rdseq_);
tmp_rdseq_.reverse();
assert(fwContains || bwContains);
}
#endif
ASSERT_ONLY(tmp_rdseq_.reverse());
if(lim > 0) {
const Ebwt *ebwt = &ebwtFw;
assert(ebwt != NULL);
// Extend left using forward index
const BTDnaString& seq = fw ? rd.patFw : rd.patRc;
// See what we get by extending
TIndexOffU top = topf, bot = botf;
t[0] = t[1] = t[2] = t[3] = 0;
b[0] = b[1] = b[2] = b[3] = 0;
tp[0] = tp[1] = tp[2] = tp[3] = topb;
bp[0] = bp[1] = bp[2] = bp[3] = botb;
SideLocus tloc, bloc;
INIT_LOCS(top, bot, tloc, bloc, *ebwt);
for(size_t ii = 0; ii < lim; ii++) {
// Starting to left of seed (<off) and moving left
size_t i = 0;
if(fw) {
i = off - ii - 1;
} else {
i = rdlen - off - len - 1 - ii;
}
// Get char from read
int rdc = seq.get(i);
// See what we get by extending
if(bloc.valid()) {
prm.nSdFmops++;
t[0] = t[1] = t[2] = t[3] =
b[0] = b[1] = b[2] = b[3] = 0;
ebwt->mapBiLFEx(tloc, bloc, t, b, tp, bp);
SANITY_CHECK_4TUP(t, b, tp, bp);
int nonz = -1;
bool abort = false;
size_t origSz = bot - top;
for(int j = 0; j < 4; j++) {
if(b[j] > t[j]) {
if(nonz >= 0) {
abort = true;
break;
}
nonz = j;
top = t[j]; bot = b[j];
}
}
assert_leq(bot - top, origSz);
if(abort || (nonz != rdc && rdc <= 3) || bot - top < origSz) {
break;
}
} else {
assert_eq(bot, top+1);
prm.nSdFmops++;
int c = ebwt->mapLF1(top, tloc);
if(c != rdc && rdc <= 3) {
break;
}
bot = top + 1;
}
ASSERT_ONLY(tmp_rdseq_.append(rdc));
if(++nlex == 255) {
break;
}
INIT_LOCS(top, bot, tloc, bloc, *ebwt);
}
}
// We're about to add onto the end, so re-reverse
ASSERT_ONLY(tmp_rdseq_.reverse());
lim = fw ? rdlen - len - off : off;
if(lim > 0 && ebwtBw != NULL) {
const Ebwt *ebwt = ebwtBw;
assert(ebwt != NULL);
// Extend right using backward index
const BTDnaString& seq = fw ? rd.patFw : rd.patRc;
// See what we get by extending
TIndexOffU top = topb, bot = botb;
t[0] = t[1] = t[2] = t[3] = 0;
b[0] = b[1] = b[2] = b[3] = 0;
tp[0] = tp[1] = tp[2] = tp[3] = topf;
bp[0] = bp[1] = bp[2] = bp[3] = botf;
INIT_LOCS(top, bot, tloc, bloc, *ebwt);
for(size_t ii = 0; ii < lim; ii++) {
// Starting to right of seed (<off) and moving right
size_t i;
if(fw) {
i = ii + len + off;
} else {
i = rdlen - off + ii;
}
// Get char from read
int rdc = seq.get(i);
// See what we get by extending
if(bloc.valid()) {
prm.nSdFmops++;
t[0] = t[1] = t[2] = t[3] =
b[0] = b[1] = b[2] = b[3] = 0;
ebwt->mapBiLFEx(tloc, bloc, t, b, tp, bp);
SANITY_CHECK_4TUP(t, b, tp, bp);
int nonz = -1;
bool abort = false;
size_t origSz = bot - top;
for(int j = 0; j < 4; j++) {
if(b[j] > t[j]) {
if(nonz >= 0) {
abort = true;
break;
}
nonz = j;
top = t[j]; bot = b[j];
}
}
assert_leq(bot - top, origSz);
if(abort || (nonz != rdc && rdc <= 3) || bot - top < origSz) {
break;
}
} else {
assert_eq(bot, top+1);
prm.nSdFmops++;
int c = ebwt->mapLF1(top, tloc);
if(c != rdc && rdc <= 3) {
break;
}
bot = top + 1;
}
ASSERT_ONLY(tmp_rdseq_.append(rdc));
if(++nrex == 255) {
break;
}
INIT_LOCS(top, bot, tloc, bloc, *ebwt);
}
}
#ifndef NDEBUG
if(false) {
// TODO: This will sometimes fail even when the extension is legitimate
// This is because contains() comes in from one extreme or the other,
// whereas we started from the inside and worked outwards. This
// affects which Ns are OK and which are not OK.
// Have to do both because whether we can get through an N depends on
// which direction we're coming in
bool fwContains = ebwtFw.contains(tmp_rdseq_);
tmp_rdseq_.reverse();
bool bwContains = ebwtBw != NULL && ebwtBw->contains(tmp_rdseq_);
tmp_rdseq_.reverse();
assert(fwContains || bwContains);
}
#endif
assert_lt(nlex, rdlen);
assert_lt(nrex, rdlen);
return;
}
/**
* Given seed results, set up all of our state for resolving and keeping
* track of reference offsets for hits.
*/
void SwDriver::prioritizeSATups(
const Read& read, // read
SeedResults& sh, // seed hits to extend into full alignments
const Ebwt& ebwtFw, // BWT
const Ebwt* ebwtBw, // BWT
const BitPairReference& ref, // Reference strings
int seedmms, // # mismatches allowed in seed
size_t maxelt, // max elts we'll consider
bool doExtend, // do extension of seed hits?
bool lensq, // square length in weight calculation
bool szsq, // square range size in weight calculation
size_t nsm, // if range as <= nsm elts, it's "small"
AlignmentCacheIface& ca, // alignment cache for seed hits
RandomSource& rnd, // pseudo-random generator
WalkMetrics& wlm, // group walk left metrics
PerReadMetrics& prm, // per-read metrics
size_t& nelt_out, // out: # elements total
bool all) // report all hits?
{
const size_t nonz = sh.nonzeroOffsets(); // non-zero positions
const int matei = (read.mate <= 1 ? 0 : 1);
satups_.clear();
gws_.clear();
rands_.clear();
rands2_.clear();
satpos_.clear();
satpos2_.clear();
size_t nrange = 0, nelt = 0, nsmall = 0, nsmall_elts = 0;
bool keepWhole = false;
EList<SATupleAndPos, 16>& satpos = keepWhole ? satpos_ : satpos2_;
for(size_t i = 0; i < nonz; i++) {
bool fw = true;
uint32_t offidx = 0, rdoff = 0, seedlen = 0;
QVal qv = sh.hitsByRank(i, offidx, rdoff, fw, seedlen);
assert(qv.valid());
assert(!qv.empty());
assert(qv.repOk(ca.current()));
ca.queryQval(qv, satups_, nrange, nelt);
for(size_t j = 0; j < satups_.size(); j++) {
const size_t sz = satups_[j].size();
// Check whether this hit occurs inside the extended boundaries of
// another hit we already processed for this read.
if(seedmms == 0) {
// See if we're covered by a previous extended seed hit
EList<ExtendRange>& range =
fw ? seedExRangeFw_[matei] : seedExRangeRc_[matei];
bool skip = false;
for(size_t k = 0; k < range.size(); k++) {
size_t p5 = range[k].off;
size_t len = range[k].len;
if(p5 <= rdoff && p5 + len >= (rdoff + seedlen)) {
if(sz <= range[k].sz) {
skip = true;
break;
}
}
}
if(skip) {
assert_gt(nrange, 0);
nrange--;
assert_geq(nelt, sz);
nelt -= sz;
continue; // Skip this seed
}
}
satpos.expand();
satpos.back().sat = satups_[j];
satpos.back().origSz = sz;
satpos.back().pos.init(fw, offidx, rdoff, seedlen);
if(sz <= nsm) {
nsmall++;
nsmall_elts += sz;
}
satpos.back().nlex = satpos.back().nrex = 0;
#ifndef NDEBUG
tmp_rdseq_.clear();
uint64_t key = satpos.back().sat.key.seq;
for(size_t k = 0; k < seedlen; k++) {
int c = (int)(key & 3);
tmp_rdseq_.append(c);
key >>= 2;
}
tmp_rdseq_.reverse();
#endif
size_t nlex = 0, nrex = 0;
if(doExtend) {
extend(
read,
ebwtFw,
ebwtBw,
satpos.back().sat.topf,
(TIndexOffU)(satpos.back().sat.topf + sz),
satpos.back().sat.topb,
(TIndexOffU)(satpos.back().sat.topb + sz),
fw,
rdoff,
seedlen,
prm,
nlex,
nrex);
}
satpos.back().nlex = nlex;
satpos.back().nrex = nrex;
if(seedmms == 0 && (nlex > 0 || nrex > 0)) {
assert_geq(rdoff, (fw ? nlex : nrex));
size_t p5 = rdoff - (fw ? nlex : nrex);
EList<ExtendRange>& range =
fw ? seedExRangeFw_[matei] : seedExRangeRc_[matei];
range.expand();
range.back().off = p5;
range.back().len = seedlen + nlex + nrex;
range.back().sz = sz;
}
}
satups_.clear();
}
assert_leq(nsmall, nrange);
nelt_out = nelt; // return the total number of elements
assert_eq(nrange, satpos.size());
satpos.sort();
if(keepWhole) {
gws_.ensure(nrange);
rands_.ensure(nrange);
for(size_t i = 0; i < nrange; i++) {
gws_.expand();
SARangeWithOffs<TSlice> sa;
sa.topf = satpos_.back().sat.topf;
sa.len = satpos_.back().sat.key.len;
sa.offs = satpos_.back().sat.offs;
gws_.back().init(
ebwtFw, // forward Bowtie index
ref, // reference sequences
sa, // SA tuples: ref hit, salist range
rnd, // pseudo-random generator
wlm); // metrics
assert(gws_.back().initialized());
rands_.expand();
rands_.back().init(satpos_[i].sat.size(), all);
}
return;
}
// Resize satups_ list so that ranges having elements that we might
// possibly explore are present
satpos_.ensure(min(maxelt, nelt));
gws_.ensure(min(maxelt, nelt));
rands_.ensure(min(maxelt, nelt));
rands2_.ensure(min(maxelt, nelt));
size_t nlarge_elts = nelt - nsmall_elts;
if(maxelt < nelt) {
size_t diff = nelt - maxelt;
if(diff >= nlarge_elts) {
nlarge_elts = 0;
} else {
nlarge_elts -= diff;
}
}
size_t nelt_added = 0;
// Now we have a collection of ranges in satpos2_. Now we want to decide
// how we explore elements from them. The basic idea is that: for very
// small guys, where "very small" means that the size of the range is less
// than or equal to the parameter 'nsz', we explore them in their entirety
// right away. For the rest, we want to select in a way that is (a)
// random, and (b) weighted toward examining elements from the smaller
// ranges more frequently (and first).
//
// 1. do the smalls
for(size_t j = 0; j < nsmall && nelt_added < maxelt; j++) {
satpos_.expand();
satpos_.back() = satpos2_[j];
gws_.expand();
SARangeWithOffs<TSlice> sa;
sa.topf = satpos_.back().sat.topf;
sa.len = satpos_.back().sat.key.len;
sa.offs = satpos_.back().sat.offs;
gws_.back().init(
ebwtFw, // forward Bowtie index
ref, // reference sequences
sa, // SA tuples: ref hit, salist range
rnd, // pseudo-random generator
wlm); // metrics
assert(gws_.back().initialized());
rands_.expand();
rands_.back().init(satpos_.back().sat.size(), all);
nelt_added += satpos_.back().sat.size();
#ifndef NDEBUG
for(size_t k = 0; k < satpos_.size()-1; k++) {
assert(!(satpos_[k] == satpos_.back()));
}
#endif
}
if(nelt_added >= maxelt || nsmall == satpos2_.size()) {
nelt_out = nelt_added;
return;
}
// 2. do the non-smalls
// Initialize the row sampler
rowsamp_.init(satpos2_, nsmall, satpos2_.size(), lensq, szsq);
// Initialize the random choosers
rands2_.resize(satpos2_.size());
for(size_t j = 0; j < satpos2_.size(); j++) {
rands2_[j].reset();
}
while(nelt_added < maxelt && nelt_added < nelt) {
// Pick a non-small range to sample from
size_t ri = rowsamp_.next(rnd) + nsmall;
assert_geq(ri, nsmall);
assert_lt(ri, satpos2_.size());
// Initialize random element chooser for that range
if(!rands2_[ri].inited()) {
rands2_[ri].init(satpos2_[ri].sat.size(), all);
assert(!rands2_[ri].done());
}
assert(!rands2_[ri].done());
// Choose an element from the range
size_t r = rands2_[ri].next(rnd);
if(rands2_[ri].done()) {
// Tell the row sampler this range is done
rowsamp_.finishedRange(ri - nsmall);
}
// Add the element to the satpos_ list
SATuple sat;
TSlice o;
o.init(satpos2_[ri].sat.offs, r, r+1);
sat.init(satpos2_[ri].sat.key, (TIndexOffU)(satpos2_[ri].sat.topf + r), OFF_MASK, o);
satpos_.expand();
satpos_.back().sat = sat;
satpos_.back().origSz = satpos2_[ri].origSz;
satpos_.back().pos = satpos2_[ri].pos;
// Initialize GroupWalk object
gws_.expand();
SARangeWithOffs<TSlice> sa;
sa.topf = sat.topf;
sa.len = sat.key.len;
sa.offs = sat.offs;
gws_.back().init(
ebwtFw, // forward Bowtie index
ref, // reference sequences
sa, // SA tuples: ref hit, salist range
rnd, // pseudo-random generator
wlm); // metrics
assert(gws_.back().initialized());
// Initialize random selector
rands_.expand();
rands_.back().init(1, all);
nelt_added++;
}
nelt_out = nelt_added;
return;
}
enum {
FOUND_NONE = 0,
FOUND_EE,
FOUND_UNGAPPED,
};
/**
* Given a collection of SeedHits for a single read, extend seed alignments
* into full alignments. Where possible, try to avoid redundant offset lookups
* and dynamic programming wherever possible. Optionally report alignments to
* a AlnSinkWrap object as they are discovered.
*
* If 'reportImmediately' is true, returns true iff a call to msink->report()
* returned true (indicating that the reporting policy is satisfied and we can
* stop). Otherwise, returns false.
*/
int SwDriver::extendSeeds(
Read& rd, // read to align
bool mate1, // true iff rd is mate #1
SeedResults& sh, // seed hits to extend into full alignments
const Ebwt& ebwtFw, // BWT
const Ebwt* ebwtBw, // BWT'
const BitPairReference& ref, // Reference strings
SwAligner& swa, // dynamic programming aligner
const Scoring& sc, // scoring scheme
int seedmms, // # mismatches allowed in seed
int seedlen, // length of seed
int seedival, // interval between seeds
TAlScore& minsc, // minimum score for anchor
int nceil, // maximum # Ns permitted in reference portion
size_t maxhalf, // max width in either direction for DP tables
bool doUngapped, // do ungapped alignment
size_t maxIters, // stop after this many seed-extend loop iters
size_t maxUg, // stop after this many ungaps
size_t maxDp, // stop after this many dps
size_t maxUgStreak, // stop after streak of this many ungap fails
size_t maxDpStreak, // stop after streak of this many dp fails
bool doExtend, // do seed extension
bool enable8, // use 8-bit SSE where possible
size_t cminlen, // use checkpointer if read longer than this
size_t cpow2, // interval between diagonals to checkpoint
bool doTri, // triangular mini-fills?
int tighten, // -M score tightening mode
AlignmentCacheIface& ca, // alignment cache for seed hits
RandomSource& rnd, // pseudo-random source
WalkMetrics& wlm, // group walk left metrics
SwMetrics& swmSeed, // DP metrics for seed-extend
PerReadMetrics& prm, // per-read metrics
AlnSinkWrap* msink, // AlnSink wrapper for multiseed-style aligner
bool reportImmediately, // whether to report hits immediately to msink
bool& exhaustive) // set to true iff we searched all seeds exhaustively
{
bool all = msink->allHits();
assert(!reportImmediately || msink != NULL);
assert(!reportImmediately || !msink->maxed());
assert_geq(nceil, 0);
assert_leq((size_t)nceil, rd.length());
// Calculate the largest possible number of read and reference gaps
const size_t rdlen = rd.length();
TAlScore perfectScore = sc.perfectScore(rdlen);
DynProgFramer dpframe(!gReportOverhangs);
swa.reset();
// Initialize a set of GroupWalks, one for each seed. Also, intialize the
// accompanying lists of reference seed hits (satups*)
const size_t nsm = 5;
const size_t nonz = sh.nonzeroOffsets(); // non-zero positions
size_t eeHits = sh.numE2eHits();
bool eeMode = eeHits > 0;
bool firstEe = true;
bool firstExtend = true;
// Reset all the counters related to streaks
prm.nEeFail = 0;
prm.nUgFail = 0;
prm.nDpFail = 0;
size_t nelt = 0, neltLeft = 0;
size_t rows = rdlen;
size_t eltsDone = 0;
// cerr << "===" << endl;
while(true) {
if(eeMode) {
if(firstEe) {
firstEe = false;
eeMode = eeSaTups(
rd, // read
sh, // seed hits to extend into full alignments
ebwtFw, // BWT
ref, // Reference strings
rnd, // pseudo-random generator
wlm, // group walk left metrics
swmSeed, // seed-extend metrics
nelt, // out: # elements total
maxIters, // max # to report
all); // report all hits?
assert_eq(gws_.size(), rands_.size());
assert_eq(gws_.size(), satpos_.size());
} else {
eeMode = false;
}
}
if(!eeMode) {
if(nonz == 0) {
return EXTEND_EXHAUSTED_CANDIDATES; // No seed hits! Bail.
}
if(minsc == perfectScore) {
return EXTEND_PERFECT_SCORE; // Already found all perfect hits!
}
if(firstExtend) {
nelt = 0;
prioritizeSATups(
rd, // read
sh, // seed hits to extend into full alignments
ebwtFw, // BWT
ebwtBw, // BWT'
ref, // Reference strings
seedmms, // # seed mismatches allowed
maxIters, // max rows to consider per position
doExtend, // extend out seeds
true, // square extended length
true, // square SA range size
nsm, // smallness threshold
ca, // alignment cache for seed hits
rnd, // pseudo-random generator
wlm, // group walk left metrics
prm, // per-read metrics
nelt, // out: # elements total
all); // report all hits?
assert_eq(gws_.size(), rands_.size());
assert_eq(gws_.size(), satpos_.size());
neltLeft = nelt;
firstExtend = false;
}
if(neltLeft == 0) {
// Finished examining gapped candidates
break;
}
}
for(size_t i = 0; i < gws_.size(); i++) {
if(eeMode && eehits_[i].score < minsc) {
return EXTEND_PERFECT_SCORE;
}
bool is_small = satpos_[i].sat.size() < nsm;
bool fw = satpos_[i].pos.fw;
uint32_t rdoff = satpos_[i].pos.rdoff;
uint32_t seedhitlen = satpos_[i].pos.seedlen;
if(!fw) {
// 'rdoff' and 'offidx' are with respect to the 5' end of
// the read. Here we convert rdoff to be with respect to
// the upstream (3') end of ther read.
rdoff = (uint32_t)(rdlen - rdoff - seedhitlen);
}
bool first = true;
// If the range is small, investigate all elements now. If the
// range is large, just investigate one and move on - we might come
// back to this range later.
size_t riter = 0;
while(!rands_[i].done() && (first || is_small || eeMode)) {
assert(!gws_[i].done());
riter++;
if(minsc == perfectScore) {
if(!eeMode || eehits_[i].score < perfectScore) {
return EXTEND_PERFECT_SCORE;
}
} else if(eeMode && eehits_[i].score < minsc) {
break;
}
if(prm.nExDps >= maxDp || prm.nMateDps >= maxDp) {
return EXTEND_EXCEEDED_HARD_LIMIT;
}
if(prm.nExUgs >= maxUg || prm.nMateUgs >= maxUg) {
return EXTEND_EXCEEDED_HARD_LIMIT;
}
if(prm.nExIters >= maxIters) {
return EXTEND_EXCEEDED_HARD_LIMIT;
}
prm.nExIters++;
first = false;
// Resolve next element offset
WalkResult wr;
size_t elt = rands_[i].next(rnd);
//cerr << "elt=" << elt << endl;
SARangeWithOffs<TSlice> sa;
sa.topf = satpos_[i].sat.topf;
sa.len = satpos_[i].sat.key.len;
sa.offs = satpos_[i].sat.offs;
gws_[i].advanceElement((TIndexOffU)elt, ebwtFw, ref, sa, gwstate_, wr, wlm, prm);
eltsDone++;
if(!eeMode) {
assert_gt(neltLeft, 0);
neltLeft--;
}
assert_neq(OFF_MASK, wr.toff);
TIndexOffU tidx = 0, toff = 0, tlen = 0;
bool straddled = false;
ebwtFw.joinedToTextOff(
wr.elt.len,
wr.toff,
tidx,
toff,
tlen,
eeMode, // reject straddlers?
straddled); // did it straddle?
if(tidx == OFF_MASK) {
// The seed hit straddled a reference boundary so the seed hit
// isn't valid
continue;
}
#ifndef NDEBUG
if(!eeMode && !straddled) { // Check that seed hit matches reference
uint64_t key = satpos_[i].sat.key.seq;
for(size_t k = 0; k < wr.elt.len; k++) {
int c = ref.getBase(tidx, toff + wr.elt.len - k - 1);
assert_leq(c, 3);
int ck = (int)(key & 3);
key >>= 2;
assert_eq(c, ck);
}
}
#endif
// Find offset of alignment's upstream base assuming net gaps=0
// between beginning of read and beginning of seed hit
int64_t refoff = (int64_t)toff - rdoff;
// Coordinate of the seed hit w/r/t the pasted reference string
Coord refcoord(tidx, refoff, fw);
if(seenDiags1_.locusPresent(refcoord)) {
// Already handled alignments seeded on this diagonal
prm.nRedundants++;
swmSeed.rshit++;
continue;
}
// Now that we have a seed hit, there are many issues to solve
// before we have a completely framed dynamic programming problem.
// They include:
//
// 1. Setting reference offsets on either side of the seed hit,
// accounting for where the seed occurs in the read
// 2. Adjusting the width of the banded dynamic programming problem
// and adjusting reference bounds to allow for gaps in the
// alignment
// 3. Accounting for the edges of the reference, which can impact
// the width of the DP problem and reference bounds.
// 4. Perhaps filtering the problem down to a smaller problem based
// on what DPs we've already solved for this read
//
// We do #1 here, since it is simple and we have all the seed-hit
// information here. #2 and #3 are handled in the DynProgFramer.
int readGaps = 0, refGaps = 0;
bool ungapped = false;
if(!eeMode) {
readGaps = sc.maxReadGaps(minsc, rdlen);
refGaps = sc.maxRefGaps(minsc, rdlen);
ungapped = (readGaps == 0 && refGaps == 0);
}
int state = FOUND_NONE;
bool found = false;
if(eeMode) {
resEe_.reset();
resEe_.alres.reset();
const EEHit& h = eehits_[i];
assert_leq(h.score, perfectScore);
resEe_.alres.setScore(AlnScore(h.score, h.ns(), 0));
resEe_.alres.setShape(
refcoord.ref(), // ref id
refcoord.off(), // 0-based ref offset
tlen, // length of reference
fw, // aligned to Watson?
rdlen, // read length
true, // pretrim soft?
0, // pretrim 5' end
0, // pretrim 3' end
true, // alignment trim soft?
0, // alignment trim 5' end
0); // alignment trim 3' end
resEe_.alres.setRefNs(h.refns());
if(h.mms() > 0) {
assert_eq(1, h.mms());
assert_lt(h.e1.pos, rd.length());
resEe_.alres.ned().push_back(h.e1);
}
assert(resEe_.repOk(rd));
state = FOUND_EE;
found = true;
Interval refival(refcoord, 1);
seenDiags1_.add(refival);
} else if(doUngapped && ungapped) {
resUngap_.reset();
int al = swa.ungappedAlign(
fw ? rd.patFw : rd.patRc,
fw ? rd.qual : rd.qualRev,
refcoord,
ref,
tlen,
sc,
gReportOverhangs,
minsc,
resUngap_);
Interval refival(refcoord, 1);
seenDiags1_.add(refival);
prm.nExUgs++;
if(al == 0) {
prm.nExUgFails++;
prm.nUgFail++;
if(prm.nUgFail >= maxUgStreak) {
return EXTEND_EXCEEDED_SOFT_LIMIT;
}
swmSeed.ungapfail++;
continue;
} else if(al == -1) {
prm.nExUgFails++;
prm.nUgFail++; // count this as failure
if(prm.nUgFail >= maxUgStreak) {
return EXTEND_EXCEEDED_SOFT_LIMIT;
}
swmSeed.ungapnodec++;
} else {
prm.nExUgSuccs++;
prm.nUgLastSucc = prm.nExUgs-1;
if(prm.nUgFail > prm.nUgFailStreak) {
prm.nUgFailStreak = prm.nUgFail;
}
prm.nUgFail = 0;
found = true;
state = FOUND_UNGAPPED;
swmSeed.ungapsucc++;
}
}
int64_t pastedRefoff = (int64_t)wr.toff - rdoff;
DPRect rect;
if(state == FOUND_NONE) {
found = dpframe.frameSeedExtensionRect(
refoff, // ref offset implied by seed hit assuming no gaps
rows, // length of read sequence used in DP table
tlen, // length of reference
readGaps, // max # of read gaps permitted in opp mate alignment
refGaps, // max # of ref gaps permitted in opp mate alignment
(size_t)nceil, // # Ns permitted
maxhalf, // max width in either direction
rect); // DP rectangle
assert(rect.repOk());
// Add the seed diagonal at least
seenDiags1_.add(Interval(refcoord, 1));
if(!found) {
continue;
}
}
int64_t leftShift = refoff - rect.refl;
size_t nwindow = 0;
if((int64_t)toff >= rect.refl) {
nwindow = (size_t)(toff - rect.refl);
}
// NOTE: We might be taking off more than we should because the
// pasted string omits non-A/C/G/T characters, but we included them
// when calculating leftShift. We'll account for this later.
pastedRefoff -= leftShift;
size_t nsInLeftShift = 0;
if(state == FOUND_NONE) {
if(!swa.initedRead()) {
// Initialize the aligner with a new read
swa.initRead(
rd.patFw, // fw version of query
rd.patRc, // rc version of query
rd.qual, // fw version of qualities
rd.qualRev,// rc version of qualities
0, // off of first char in 'rd' to consider
rdlen, // off of last char (excl) in 'rd' to consider
sc); // scoring scheme
}
swa.initRef(
fw, // whether to align forward or revcomp read
tidx, // reference aligned against
rect, // DP rectangle
ref, // Reference strings
tlen, // length of reference sequence
sc, // scoring scheme
minsc, // minimum score permitted
enable8, // use 8-bit SSE if possible?
cminlen, // minimum length for using checkpointing scheme
cpow2, // interval b/t checkpointed diags; 1 << this
doTri, // triangular mini-fills?
true, // this is a seed extension - not finding a mate
nwindow,
nsInLeftShift);
// Because of how we framed the problem, we can say that we've
// exhaustively scored the seed diagonal as well as maxgaps
// diagonals on either side
Interval refival(tidx, 0, fw, 0);
rect.initIval(refival);
seenDiags1_.add(refival);
// Now fill the dynamic programming matrix and return true iff
// there is at least one valid alignment
TAlScore bestCell = std::numeric_limits<TAlScore>::min();
found = swa.align(bestCell);
swmSeed.tallyGappedDp(readGaps, refGaps);
prm.nExDps++;
if(!found) {
prm.nExDpFails++;
prm.nDpFail++;
if(prm.nDpFail >= maxDpStreak) {
return EXTEND_EXCEEDED_SOFT_LIMIT;
}
if(bestCell > std::numeric_limits<TAlScore>::min() && bestCell > prm.bestLtMinscMate1) {
prm.bestLtMinscMate1 = bestCell;
}
continue; // Look for more anchor alignments
} else {
prm.nExDpSuccs++;
prm.nDpLastSucc = prm.nExDps-1;
if(prm.nDpFail > prm.nDpFailStreak) {
prm.nDpFailStreak = prm.nDpFail;
}
prm.nDpFail = 0;
}
}
bool firstInner = true;
while(true) {
assert(found);
SwResult *res = NULL;
if(state == FOUND_EE) {
if(!firstInner) {
break;
}
res = &resEe_;
} else if(state == FOUND_UNGAPPED) {
if(!firstInner) {
break;
}
res = &resUngap_;
} else {
resGap_.reset();
assert(resGap_.empty());
if(swa.done()) {
break;
}
swa.nextAlignment(resGap_, minsc, rnd);
found = !resGap_.empty();
if(!found) {
break;
}
res = &resGap_;
}
assert(res != NULL);
firstInner = false;
assert(res->alres.matchesRef(
rd,
ref,
tmp_rf_,
tmp_rdseq_,
tmp_qseq_,
raw_refbuf_,
raw_destU32_,
raw_matches_));
Interval refival(tidx, 0, fw, tlen);
assert_gt(res->alres.refExtent(), 0);
if(gReportOverhangs &&
!refival.containsIgnoreOrient(res->alres.refival()))
{
res->alres.clipOutside(true, 0, tlen);
if(res->alres.refExtent() == 0) {
continue;
}
}
assert(gReportOverhangs ||
refival.containsIgnoreOrient(res->alres.refival()));
// Did the alignment fall entirely outside the reference?
if(!refival.overlapsIgnoreOrient(res->alres.refival())) {
continue;
}
// Is this alignment redundant with one we've seen previously?
if(redAnchor_.overlap(res->alres)) {
// Redundant with an alignment we found already
continue;
}
redAnchor_.add(res->alres);
// Annotate the AlnRes object with some key parameters
// that were used to obtain the alignment.
res->alres.setParams(
seedmms, // # mismatches allowed in seed
seedlen, // length of seed
seedival, // interval between seeds
minsc); // minimum score for valid alignment
if(reportImmediately) {
assert(msink != NULL);
assert(res->repOk());
// Check that alignment accurately reflects the
// reference characters aligned to
assert(res->alres.matchesRef(
rd,
ref,
tmp_rf_,
tmp_rdseq_,
tmp_qseq_,
raw_refbuf_,
raw_destU32_,
raw_matches_));
// Report an unpaired alignment
assert(!msink->maxed());
if(msink->report(
0,
mate1 ? &res->alres : NULL,
mate1 ? NULL : &res->alres))
{
// Short-circuited because a limit, e.g. -k, -m or
// -M, was exceeded
return EXTEND_POLICY_FULFILLED;
}
if(tighten > 0 &&
msink->Mmode() &&
msink->hasSecondBestUnp1())
{
if(tighten == 1) {
if(msink->bestUnp1() >= minsc) {
minsc = msink->bestUnp1();
if(minsc < perfectScore &&
msink->bestUnp1() == msink->secondBestUnp1())
{
minsc++;
}
}
} else if(tighten == 2) {
if(msink->secondBestUnp1() >= minsc) {
minsc = msink->secondBestUnp1();
if(minsc < perfectScore) {
minsc++;
}
}
} else {
TAlScore diff = msink->bestUnp1() - msink->secondBestUnp1();
TAlScore bot = msink->secondBestUnp1() + ((diff*3)/4);
if(bot >= minsc) {
minsc = bot;
if(minsc < perfectScore) {
minsc++;
}
}
}
assert_leq(minsc, perfectScore);
}
}
}
// At this point we know that we aren't bailing, and will
// continue to resolve seed hits.
} // while(!gws_[i].done())
}
}
// Short-circuited because a limit, e.g. -k, -m or -M, was exceeded
return EXTEND_EXHAUSTED_CANDIDATES;
}
/**
* Given a collection of SeedHits for both mates in a read pair, extend seed
* alignments into full alignments and then look for the opposite mate using
* dynamic programming. Where possible, try to avoid redundant offset lookups.
* Optionally report alignments to a AlnSinkWrap object as they are discovered.
*
* If 'reportImmediately' is true, returns true iff a call to
* msink->report() returned true (indicating that the reporting
* policy is satisfied and we can stop). Otherwise, returns false.
*
* REDUNDANT SEED HITS
*
* See notes at top of aligner_sw_driver.h.
*
* REDUNDANT ALIGNMENTS
*
* See notes at top of aligner_sw_driver.h.
*
* MIXING PAIRED AND UNPAIRED ALIGNMENTS
*
* There are distinct paired-end alignment modes for the cases where (a) the
* user does or does not want to see unpaired alignments for individual mates
* when there are no reportable paired-end alignments involving both mates, and
* (b) the user does or does not want to see discordant paired-end alignments.
* The modes have implications for this function and for the AlnSinkWrap, since
* it affects when we're "done." Also, whether the user has asked us to report
* discordant alignments affects whether and how much searching for unpaired
* alignments we must do (i.e. if there are no paired-end alignments, we must
* at least do -m 1 for both mates).
*
* Mode 1: Just concordant paired-end. Print only concordant paired-end
* alignments. As soon as any limits (-k/-m/-M) are reached, stop.
*
* Mode 2: Concordant and discordant paired-end. If -k/-m/-M limits are
* reached for paired-end alignments, stop. Otherwise, if no paired-end
* alignments are found, align both mates in an unpaired -m 1 fashion. If
* there is exactly one unpaired alignment for each mate, report the
* combination as a discordant alignment.
*
* Mode 3: Concordant paired-end if possible, otherwise unpaired. If -k/-M
* limit is reached for paired-end alignmnts, stop. If -m limit is reached for
* paired-end alignments or no paired-end alignments are found, align both
* mates in an unpaired fashion. All the same settings governing validity and
* reportability in paired-end mode apply here too (-k/-m/-M/etc).
*
* Mode 4: Concordant or discordant paired-end if possible, otherwise unpaired.
* If -k/-M limit is reached for paired-end alignmnts, stop. If -m limit is
* reached for paired-end alignments or no paired-end alignments are found,
* align both mates in an unpaired fashion. If the -m limit was reached, there
* is no need to search for a discordant alignment, and unapired alignment can
* proceed as in Mode 3. If no paired-end alignments were found, then unpaired
* alignment proceeds as in Mode 3 but with this caveat: alignment must be at
* least as thorough as dictated by -m 1 up until the point where
*
* Print paired-end alignments when there are reportable paired-end
* alignments, otherwise report reportable unpaired alignments. If -k limit is
* reached for paired-end alignments, stop. If -m/-M limit is reached for
* paired-end alignments, stop searching for paired-end alignments and look
* only for unpaired alignments. If searching only for unpaired alignments,
* respect -k/-m/-M limits separately for both mates.
*
* The return value from the AlnSinkWrap's report member function must be
* specific enough to distinguish between:
*
* 1. Stop searching for paired-end alignments
* 2. Stop searching for alignments for unpaired alignments for mate #1
* 3. Stop searching for alignments for unpaired alignments for mate #2
* 4. Stop searching for any alignments
*
* Note that in Mode 2, options affecting validity and reportability of
* alignments apply . E.g. if -m 1 is specified
*
* WORKFLOW
*
* Our general approach to finding paired and unpaired alignments here
* is as follows:
*
* - For mate in mate1, mate2:
* - For each seed hit in mate:
* - Try to extend it into a full alignment; if we can't, continue
* to the next seed hit
* - Look for alignment for opposite mate; if we can't find one,
* -
* -
*
*/
int SwDriver::extendSeedsPaired(
Read& rd, // mate to align as anchor
Read& ord, // mate to align as opposite
bool anchor1, // true iff anchor mate is mate1
bool oppFilt, // true iff opposite mate was filtered out
SeedResults& sh, // seed hits for anchor
const Ebwt& ebwtFw, // BWT
const Ebwt* ebwtBw, // BWT'
const BitPairReference& ref, // Reference strings
SwAligner& swa, // dynamic programming aligner for anchor
SwAligner& oswa, // dynamic programming aligner for opposite
const Scoring& sc, // scoring scheme
const PairedEndPolicy& pepol,// paired-end policy
int seedmms, // # mismatches allowed in seed
int seedlen, // length of seed
int seedival, // interval between seeds
TAlScore& minsc, // minimum score for valid anchor aln
TAlScore& ominsc, // minimum score for valid opposite aln
int nceil, // max # Ns permitted in ref for anchor
int onceil, // max # Ns permitted in ref for opposite
bool nofw, // don't align forward read
bool norc, // don't align revcomp read
size_t maxhalf, // max width in either direction for DP tables
bool doUngapped, // do ungapped alignment
size_t maxIters, // stop after this many seed-extend loop iters
size_t maxUg, // stop after this many ungaps
size_t maxDp, // stop after this many dps
size_t maxEeStreak, // stop after streak of this many end-to-end fails
size_t maxUgStreak, // stop after streak of this many ungap fails
size_t maxDpStreak, // stop after streak of this many dp fails
size_t maxMateStreak, // stop seed range after N mate-find fails
bool doExtend, // do seed extension
bool enable8, // use 8-bit SSE where possible
size_t cminlen, // use checkpointer if read longer than this
size_t cpow2, // interval between diagonals to checkpoint
bool doTri, // triangular mini-fills?
int tighten, // -M score tightening mode
AlignmentCacheIface& ca, // alignment cache for seed hits
RandomSource& rnd, // pseudo-random source
WalkMetrics& wlm, // group walk left metrics
SwMetrics& swmSeed, // DP metrics for seed-extend
SwMetrics& swmMate, // DP metrics for mate finidng
PerReadMetrics& prm, // per-read metrics
AlnSinkWrap* msink, // AlnSink wrapper for multiseed-style aligner
bool swMateImmediately, // whether to look for mate immediately
bool reportImmediately, // whether to report hits immediately to msink
bool discord, // look for discordant alignments?
bool mixed, // look for unpaired as well as paired alns?
bool& exhaustive)
{
bool all = msink->allHits();
assert(!reportImmediately || msink != NULL);
assert(!reportImmediately || !msink->maxed());
assert(!msink->state().doneWithMate(anchor1));
assert_geq(nceil, 0);
assert_geq(onceil, 0);
assert_leq((size_t)nceil, rd.length());
assert_leq((size_t)onceil, ord.length());
const size_t rdlen = rd.length();
const size_t ordlen = ord.length();
const TAlScore perfectScore = sc.perfectScore(rdlen);
const TAlScore operfectScore = sc.perfectScore(ordlen);
assert_leq(minsc, perfectScore);
assert(oppFilt || ominsc <= operfectScore);
TAlScore bestPairScore = perfectScore + operfectScore;
if(tighten > 0 && msink->Mmode() && msink->hasSecondBestPair()) {
// Paired-end alignments should have at least this score from now
TAlScore ps;
if(tighten == 1) {
ps = msink->bestPair();
} else if(tighten == 2) {
ps = msink->secondBestPair();
} else {
TAlScore diff = msink->bestPair() - msink->secondBestPair();
ps = msink->secondBestPair() + (diff * 3)/4;
}
if(tighten == 1 && ps < bestPairScore &&
msink->bestPair() == msink->secondBestPair())
{
ps++;
}
if(tighten >= 2 && ps < bestPairScore) {
ps++;
}
// Anchor mate must have score at least 'ps' minus the best possible
// score for the opposite mate.
TAlScore nc = ps - operfectScore;
if(nc > minsc) {
minsc = nc;
}
assert_leq(minsc, perfectScore);
}
DynProgFramer dpframe(!gReportOverhangs);
swa.reset();
oswa.reset();
// Initialize a set of GroupWalks, one for each seed. Also, intialize the
// accompanying lists of reference seed hits (satups*)
const size_t nsm = 5;
const size_t nonz = sh.nonzeroOffsets(); // non-zero positions
size_t eeHits = sh.numE2eHits();
bool eeMode = eeHits > 0;
bool firstEe = true;
bool firstExtend = true;
// Reset all the counters related to streaks
prm.nEeFail = 0;
prm.nUgFail = 0;
prm.nDpFail = 0;
size_t nelt = 0, neltLeft = 0;
const size_t rows = rdlen;
const size_t orows = ordlen;
size_t eltsDone = 0;
while(true) {
if(eeMode) {
if(firstEe) {
firstEe = false;
eeMode = eeSaTups(
rd, // read
sh, // seed hits to extend into full alignments
ebwtFw, // BWT
ref, // Reference strings
rnd, // pseudo-random generator
wlm, // group walk left metrics
swmSeed, // seed-extend metrics
nelt, // out: # elements total
maxIters, // max elts to report
all); // report all hits
assert_eq(gws_.size(), rands_.size());
assert_eq(gws_.size(), satpos_.size());
neltLeft = nelt;
// Initialize list that contains the mate-finding failure
// streak for each range
mateStreaks_.resize(gws_.size());
mateStreaks_.fill(0);
} else {
eeMode = false;
}
}
if(!eeMode) {
if(nonz == 0) {
// No seed hits! Bail.
return EXTEND_EXHAUSTED_CANDIDATES;
}
if(msink->Mmode() && minsc == perfectScore) {
// Already found all perfect hits!
return EXTEND_PERFECT_SCORE;
}
if(firstExtend) {
nelt = 0;
prioritizeSATups(
rd, // read
sh, // seed hits to extend into full alignments
ebwtFw, // BWT
ebwtBw, // BWT'
ref, // Reference strings
seedmms, // # seed mismatches allowed
maxIters, // max rows to consider per position
doExtend, // extend out seeds
true, // square extended length
true, // square SA range size
nsm, // smallness threshold
ca, // alignment cache for seed hits
rnd, // pseudo-random generator
wlm, // group walk left metrics
prm, // per-read metrics
nelt, // out: # elements total
all); // report all hits?
assert_eq(gws_.size(), rands_.size());
assert_eq(gws_.size(), satpos_.size());
neltLeft = nelt;
firstExtend = false;
mateStreaks_.resize(gws_.size());
mateStreaks_.fill(0);
}
if(neltLeft == 0) {
// Finished examining gapped candidates
break;
}
}
for(size_t i = 0; i < gws_.size(); i++) {
if(eeMode && eehits_[i].score < minsc) {
return EXTEND_PERFECT_SCORE;
}
bool is_small = satpos_[i].sat.size() < nsm;
bool fw = satpos_[i].pos.fw;
uint32_t rdoff = satpos_[i].pos.rdoff;
uint32_t seedhitlen = satpos_[i].pos.seedlen;
if(!fw) {
// 'rdoff' and 'offidx' are with respect to the 5' end of
// the read. Here we convert rdoff to be with respect to
// the upstream (3') end of ther read.
rdoff = (uint32_t)(rdlen - rdoff - seedhitlen);
}
bool first = true;
// If the range is small, investigate all elements now. If the
// range is large, just investigate one and move on - we might come
// back to this range later.
while(!rands_[i].done() && (first || is_small || eeMode)) {
if(minsc == perfectScore) {
if(!eeMode || eehits_[i].score < perfectScore) {
return EXTEND_PERFECT_SCORE;
}
} else if(eeMode && eehits_[i].score < minsc) {
break;
}
if(prm.nExDps >= maxDp || prm.nMateDps >= maxDp) {
return EXTEND_EXCEEDED_HARD_LIMIT;
}
if(prm.nExUgs >= maxUg || prm.nMateUgs >= maxUg) {
return EXTEND_EXCEEDED_HARD_LIMIT;
}
if(prm.nExIters >= maxIters) {
return EXTEND_EXCEEDED_HARD_LIMIT;
}
if(eeMode && prm.nEeFail >= maxEeStreak) {
return EXTEND_EXCEEDED_SOFT_LIMIT;
}
if(!eeMode && prm.nDpFail >= maxDpStreak) {
return EXTEND_EXCEEDED_SOFT_LIMIT;
}
if(!eeMode && prm.nUgFail >= maxUgStreak) {
return EXTEND_EXCEEDED_SOFT_LIMIT;
}
if(mateStreaks_[i] >= maxMateStreak) {
// Don't try this seed range anymore
rands_[i].setDone();
assert(rands_[i].done());
break;
}
prm.nExIters++;
first = false;
assert(!gws_[i].done());
// Resolve next element offset
WalkResult wr;
size_t elt = rands_[i].next(rnd);
SARangeWithOffs<TSlice> sa;
sa.topf = satpos_[i].sat.topf;
sa.len = satpos_[i].sat.key.len;
sa.offs = satpos_[i].sat.offs;
gws_[i].advanceElement((TIndexOffU)elt, ebwtFw, ref, sa, gwstate_, wr, wlm, prm);
eltsDone++;
assert_gt(neltLeft, 0);
neltLeft--;
assert_neq(OFF_MASK, wr.toff);
TIndexOffU tidx = 0, toff = 0, tlen = 0;
bool straddled = false;
ebwtFw.joinedToTextOff(
wr.elt.len,
wr.toff,
tidx,
toff,
tlen,
eeMode, // reject straddlers?
straddled); // straddled?
if(tidx == OFF_MASK) {
// The seed hit straddled a reference boundary so the seed hit
// isn't valid
continue;
}
#ifndef NDEBUG
if(!eeMode && !straddled) { // Check that seed hit matches reference
uint64_t key = satpos_[i].sat.key.seq;
for(size_t k = 0; k < wr.elt.len; k++) {
int c = ref.getBase(tidx, toff + wr.elt.len - k - 1);
assert_leq(c, 3);
int ck = (int)(key & 3);
key >>= 2;
assert_eq(c, ck);
}
}
#endif
// Find offset of alignment's upstream base assuming net gaps=0
// between beginning of read and beginning of seed hit
int64_t refoff = (int64_t)toff - rdoff;
EIvalMergeListBinned& seenDiags = anchor1 ? seenDiags1_ : seenDiags2_;
// Coordinate of the seed hit w/r/t the pasted reference string
Coord refcoord(tidx, refoff, fw);
if(seenDiags.locusPresent(refcoord)) {
// Already handled alignments seeded on this diagonal
prm.nRedundants++;
swmSeed.rshit++;
continue;
}
// Now that we have a seed hit, there are many issues to solve
// before we have a completely framed dynamic programming problem.
// They include:
//
// 1. Setting reference offsets on either side of the seed hit,
// accounting for where the seed occurs in the read
// 2. Adjusting the width of the banded dynamic programming problem
// and adjusting reference bounds to allow for gaps in the
// alignment
// 3. Accounting for the edges of the reference, which can impact
// the width of the DP problem and reference bounds.
// 4. Perhaps filtering the problem down to a smaller problem based
// on what DPs we've already solved for this read
//
// We do #1 here, since it is simple and we have all the seed-hit
// information here. #2 and #3 are handled in the DynProgFramer.
int readGaps = 0, refGaps = 0;
bool ungapped = false;
if(!eeMode) {
readGaps = sc.maxReadGaps(minsc, rdlen);
refGaps = sc.maxRefGaps(minsc, rdlen);
ungapped = (readGaps == 0 && refGaps == 0);
}
int state = FOUND_NONE;
bool found = false;
// In unpaired mode, a seed extension is successful if it
// results in a full alignment that meets the minimum score
// threshold. In paired-end mode, a seed extension is
// successful if it results in a *full paired-end* alignment
// that meets the minimum score threshold.
if(eeMode) {
resEe_.reset();
resEe_.alres.reset();
const EEHit& h = eehits_[i];
assert_leq(h.score, perfectScore);
resEe_.alres.setScore(AlnScore(h.score, h.ns(), 0));
resEe_.alres.setShape(
refcoord.ref(), // ref id
refcoord.off(), // 0-based ref offset
tlen, // reference length
fw, // aligned to Watson?
rdlen, // read length
true, // pretrim soft?
0, // pretrim 5' end
0, // pretrim 3' end
true, // alignment trim soft?
0, // alignment trim 5' end
0); // alignment trim 3' end
resEe_.alres.setRefNs(h.refns());
if(h.mms() > 0) {
assert_eq(1, h.mms());
assert_lt(h.e1.pos, rd.length());
resEe_.alres.ned().push_back(h.e1);
}
assert(resEe_.repOk(rd));
state = FOUND_EE;
found = true;
Interval refival(refcoord, 1);
seenDiags.add(refival);
prm.nExEes++;
prm.nEeFail++; // say it's failed until proven successful
prm.nExEeFails++;
} else if(doUngapped && ungapped) {
resUngap_.reset();
int al = swa.ungappedAlign(
fw ? rd.patFw : rd.patRc,
fw ? rd.qual : rd.qualRev,
refcoord,
ref,
tlen,
sc,
gReportOverhangs,
minsc, // minimum
resUngap_);
Interval refival(refcoord, 1);
seenDiags.add(refival);
prm.nExUgs++;
prm.nUgFail++; // say it's failed until proven successful
prm.nExUgFails++;
if(al == 0) {
swmSeed.ungapfail++;
continue;
} else if(al == -1) {
swmSeed.ungapnodec++;
} else {
found = true;
state = FOUND_UNGAPPED;
swmSeed.ungapsucc++;
}
}
int64_t pastedRefoff = (int64_t)wr.toff - rdoff;
DPRect rect;
if(state == FOUND_NONE) {
found = dpframe.frameSeedExtensionRect(
refoff, // ref offset implied by seed hit assuming no gaps
rows, // length of read sequence used in DP table
tlen, // length of reference
readGaps, // max # of read gaps permitted in opp mate alignment
refGaps, // max # of ref gaps permitted in opp mate alignment
(size_t)nceil, // # Ns permitted
maxhalf, // max width in either direction
rect); // DP rectangle
assert(rect.repOk());
// Add the seed diagonal at least
seenDiags.add(Interval(refcoord, 1));
if(!found) {
continue;
}
}
int64_t leftShift = refoff - rect.refl;
size_t nwindow = 0;
if((int64_t)toff >= rect.refl) {
nwindow = (size_t)(toff - rect.refl);
}
// NOTE: We might be taking off more than we should because the
// pasted string omits non-A/C/G/T characters, but we included them
// when calculating leftShift. We'll account for this later.
pastedRefoff -= leftShift;
size_t nsInLeftShift = 0;
if(state == FOUND_NONE) {
if(!swa.initedRead()) {
// Initialize the aligner with a new read
swa.initRead(
rd.patFw, // fw version of query
rd.patRc, // rc version of query
rd.qual, // fw version of qualities
rd.qualRev,// rc version of qualities
0, // off of first char in 'rd' to consider
rdlen, // off of last char (excl) in 'rd' to consider
sc); // scoring scheme
}
swa.initRef(
fw, // whether to align forward or revcomp read
tidx, // reference aligned against
rect, // DP rectangle
ref, // Reference strings
tlen, // length of reference sequence
sc, // scoring scheme
minsc, // minimum score permitted
enable8, // use 8-bit SSE if possible?
cminlen, // minimum length for using checkpointing scheme
cpow2, // interval b/t checkpointed diags; 1 << this
doTri, // triangular mini-fills?
true, // this is a seed extension - not finding a mate
nwindow,
nsInLeftShift);
// Because of how we framed the problem, we can say that we've
// exhaustively scored the seed diagonal as well as maxgaps
// diagonals on either side
Interval refival(tidx, 0, fw, 0);
rect.initIval(refival);
seenDiags.add(refival);
// Now fill the dynamic programming matrix and return true iff
// there is at least one valid alignment
TAlScore bestCell = std::numeric_limits<TAlScore>::min();
found = swa.align(bestCell);
swmSeed.tallyGappedDp(readGaps, refGaps);
prm.nExDps++;
prm.nDpFail++; // failed until proven successful
prm.nExDpFails++; // failed until proven successful
if(!found) {
TAlScore bestLast = anchor1 ? prm.bestLtMinscMate1 : prm.bestLtMinscMate2;
if(bestCell > std::numeric_limits<TAlScore>::min() && bestCell > bestLast) {
if(anchor1) {
prm.bestLtMinscMate1 = bestCell;
} else {
prm.bestLtMinscMate2 = bestCell;
}
}
continue; // Look for more anchor alignments
}
}
bool firstInner = true;
bool foundConcordant = false;
while(true) {
assert(found);
SwResult *res = NULL;
if(state == FOUND_EE) {
if(!firstInner) {
break;
}
res = &resEe_;
assert(res->repOk(rd));
} else if(state == FOUND_UNGAPPED) {
if(!firstInner) {
break;
}
res = &resUngap_;
assert(res->repOk(rd));
} else {
resGap_.reset();
assert(resGap_.empty());
if(swa.done()) {
break;
}
swa.nextAlignment(resGap_, minsc, rnd);
found = !resGap_.empty();
if(!found) {
break;
}
res = &resGap_;
assert(res->repOk(rd));
}
// TODO: If we're just taking anchor alignments out of the
// same rectangle, aren't we getting very similar
// rectangles for the opposite mate each time? Seems like
// we could save some work by detecting this.
assert(res != NULL);
firstInner = false;
assert(res->alres.matchesRef(
rd,
ref,
tmp_rf_,
tmp_rdseq_,
tmp_qseq_,
raw_refbuf_,
raw_destU32_,
raw_matches_));
Interval refival(tidx, 0, fw, tlen);
assert_gt(res->alres.refExtent(), 0);
if(gReportOverhangs &&
!refival.containsIgnoreOrient(res->alres.refival()))
{
res->alres.clipOutside(true, 0, tlen);
if(res->alres.refExtent() == 0) {
continue;
}
}
assert(gReportOverhangs ||
refival.containsIgnoreOrient(res->alres.refival()));
// Did the alignment fall entirely outside the reference?
if(!refival.overlapsIgnoreOrient(res->alres.refival())) {
continue;
}
// Is this alignment redundant with one we've seen previously?
if(redAnchor_.overlap(res->alres)) {
continue;
}
redAnchor_.add(res->alres);
// Annotate the AlnRes object with some key parameters
// that were used to obtain the alignment.
res->alres.setParams(
seedmms, // # mismatches allowed in seed
seedlen, // length of seed
seedival, // interval between seeds
minsc); // minimum score for valid alignment
bool foundMate = false;
TRefOff off = res->alres.refoff();
if( msink->state().doneWithMate(!anchor1) &&
!msink->state().doneWithMate( anchor1))
{
// We're done with the opposite mate but not with the
// anchor mate; don't try to mate up the anchor.
swMateImmediately = false;
}
if(found && swMateImmediately) {
assert(!msink->state().doneWithMate(!anchor1));
bool oleft = false, ofw = false;
int64_t oll = 0, olr = 0, orl = 0, orr = 0;
assert(!msink->state().done());
foundMate = !oppFilt;
TAlScore ominsc_cur = ominsc;
//bool oungapped = false;
int oreadGaps = 0, orefGaps = 0;
//int oungappedAlign = -1; // defer
if(foundMate) {
// Adjust ominsc given the alignment score of the
// anchor mate
ominsc_cur = ominsc;
if(tighten > 0 && msink->Mmode() && msink->hasSecondBestPair()) {
// Paired-end alignments should have at least this score from now
TAlScore ps;
if(tighten == 1) {
ps = msink->bestPair();
} else if(tighten == 2) {
ps = msink->secondBestPair();
} else {
TAlScore diff = msink->bestPair() - msink->secondBestPair();
ps = msink->secondBestPair() + (diff * 3)/4;
}
if(tighten == 1 && ps < bestPairScore &&
msink->bestPair() == msink->secondBestPair())
{
ps++;
}
if(tighten >= 2 && ps < bestPairScore) {
ps++;
}
// Anchor mate must have score at least 'ps' minus the best possible
// score for the opposite mate.
TAlScore nc = ps - res->alres.score().score();
if(nc > ominsc_cur) {
ominsc_cur = nc;
assert_leq(ominsc_cur, operfectScore);
}
}
oreadGaps = sc.maxReadGaps(ominsc_cur, ordlen);
orefGaps = sc.maxRefGaps (ominsc_cur, ordlen);
//oungapped = (oreadGaps == 0 && orefGaps == 0);
// TODO: Something lighter-weight than DP to scan
// for other mate??
//if(oungapped) {
// oresUngap_.reset();
// oungappedAlign = oswa.ungappedAlign(
// ofw ? ord.patFw : ord.patRc,
// ofw ? ord.qual : ord.qualRev,
// orefcoord,
// ref,
// otlen,
// sc,
// gReportOverhangs,
// ominsc_cur,
// 0,
// oresUngap_);
//}
foundMate = pepol.otherMate(
anchor1, // anchor mate is mate #1?
fw, // anchor aligned to Watson?
off, // offset of anchor mate
orows + oreadGaps, // max # columns spanned by alignment
tlen, // reference length
anchor1 ? rd.length() : ord.length(), // mate 1 len
anchor1 ? ord.length() : rd.length(), // mate 2 len
oleft, // out: look left for opposite mate?
oll,
olr,
orl,
orr,
ofw);
}
DPRect orect;
if(foundMate) {
foundMate = dpframe.frameFindMateRect(
!oleft, // true iff anchor alignment is to the left
oll, // leftmost Watson off for LHS of opp aln
olr, // rightmost Watson off for LHS of opp aln
orl, // leftmost Watson off for RHS of opp aln
orr, // rightmost Watson off for RHS of opp aln
orows, // length of opposite mate
tlen, // length of reference sequence aligned to
oreadGaps, // max # of read gaps in opp mate aln
orefGaps, // max # of ref gaps in opp mate aln
(size_t)onceil, // max # Ns on opp mate
maxhalf, // max width in either direction
orect); // DP rectangle
assert(!foundMate || orect.refr >= orect.refl);
}
if(foundMate) {
oresGap_.reset();
assert(oresGap_.empty());
if(!oswa.initedRead()) {
oswa.initRead(
ord.patFw, // read to align
ord.patRc, // qualities
ord.qual, // read to align
ord.qualRev,// qualities
0, // off of first char to consider
ordlen, // off of last char (ex) to consider
sc); // scoring scheme
}
// Given the boundaries defined by refi and reff, initilize
// the SwAligner with the dynamic programming problem that
// aligns the read to this reference stretch.
size_t onsInLeftShift = 0;
assert_geq(orect.refr, orect.refl);
oswa.initRef(
ofw, // align forward or revcomp read?
tidx, // reference aligned against
orect, // DP rectangle
ref, // Reference strings
tlen, // length of reference sequence
sc, // scoring scheme
ominsc_cur,// min score for valid alignments
enable8, // use 8-bit SSE if possible?
cminlen, // minimum length for using checkpointing scheme
cpow2, // interval b/t checkpointed diags; 1 << this
doTri, // triangular mini-fills?
false, // this is finding a mate - not seed ext
0, // nwindow?
onsInLeftShift);
// TODO: Can't we add some diagonals to the
// opposite mate's seenDiags when we fill in the
// opposite mate's DP? Or can we? We might want
// to use this again as an anchor - will that still
// happen? Also, isn't there a problem with
// consistency of the minimum score? Minimum score
// here depends in part on the score of the anchor
// alignment here, but it won't when the current
// opposite becomes the anchor.
// Because of how we framed the problem, we can say
// that we've exhaustively explored the "core"
// diagonals
//Interval orefival(tidx, 0, ofw, 0);
//orect.initIval(orefival);
//oseenDiags.add(orefival);
// Now fill the dynamic programming matrix, return true
// iff there is at least one valid alignment
TAlScore bestCell = std::numeric_limits<TAlScore>::min();
foundMate = oswa.align(bestCell);
prm.nMateDps++;
swmMate.tallyGappedDp(oreadGaps, orefGaps);
if(!foundMate) {
TAlScore bestLast = anchor1 ? prm.bestLtMinscMate2 : prm.bestLtMinscMate1;
if(bestCell > std::numeric_limits<TAlScore>::min() && bestCell > bestLast) {
if(anchor1) {
prm.bestLtMinscMate2 = bestCell;
} else {
prm.bestLtMinscMate1 = bestCell;
}
}
}
}
bool didAnchor = false;
do {
oresGap_.reset();
assert(oresGap_.empty());
if(foundMate && oswa.done()) {
foundMate = false;
} else if(foundMate) {
oswa.nextAlignment(oresGap_, ominsc_cur, rnd);
foundMate = !oresGap_.empty();
assert(!foundMate || oresGap_.alres.matchesRef(
ord,
ref,
tmp_rf_,
tmp_rdseq_,
tmp_qseq_,
raw_refbuf_,
raw_destU32_,
raw_matches_));
}
if(foundMate) {
// Redundant with one we've seen previously?
if(!redAnchor_.overlap(oresGap_.alres)) {
redAnchor_.add(oresGap_.alres);
}
assert_eq(ofw, oresGap_.alres.fw());
// Annotate the AlnRes object with some key parameters
// that were used to obtain the alignment.
oresGap_.alres.setParams(
seedmms, // # mismatches allowed in seed
seedlen, // length of seed
seedival, // interval between seeds
ominsc); // minimum score for valid alignment
assert_gt(oresGap_.alres.refExtent(), 0);
if(gReportOverhangs &&
!refival.containsIgnoreOrient(oresGap_.alres.refival()))
{
oresGap_.alres.clipOutside(true, 0, tlen);
foundMate = oresGap_.alres.refExtent() > 0;
}
if(foundMate &&
((!gReportOverhangs &&
!refival.containsIgnoreOrient(oresGap_.alres.refival())) ||
!refival.overlapsIgnoreOrient(oresGap_.alres.refival())))
{
foundMate = false;
}
}
ASSERT_ONLY(TRefId refid);
TRefOff off1, off2;
size_t len1, len2;
bool fw1, fw2;
int pairCl = PE_ALS_DISCORD;
if(foundMate) {
ASSERT_ONLY(refid =) res->alres.refid();
assert_eq(refid, oresGap_.alres.refid());
off1 = anchor1 ? off : oresGap_.alres.refoff();
off2 = anchor1 ? oresGap_.alres.refoff() : off;
len1 = anchor1 ?
res->alres.refExtent() : oresGap_.alres.refExtent();
len2 = anchor1 ?
oresGap_.alres.refExtent() : res->alres.refExtent();
fw1 = anchor1 ? res->alres.fw() : oresGap_.alres.fw();
fw2 = anchor1 ? oresGap_.alres.fw() : res->alres.fw();
// Check that final mate alignments are consistent with
// paired-end fragment constraints
pairCl = pepol.peClassifyPair(
off1,
len1,
fw1,
off2,
len2,
fw2);
// Instead of trying
//foundMate = pairCl != PE_ALS_DISCORD;
}
if(msink->state().doneConcordant()) {
foundMate = false;
}
if(reportImmediately) {
if(foundMate) {
// Report pair to the AlnSinkWrap
assert(!msink->state().doneConcordant());
assert(msink != NULL);
assert(res->repOk());
assert(oresGap_.repOk());
// Report an unpaired alignment
assert(!msink->maxed());
assert(!msink->state().done());
bool doneUnpaired = false;
//if(mixed || discord) {
// Report alignment for mate #1 as an
// unpaired alignment.
if(!anchor1 || !didAnchor) {
if(anchor1) {
didAnchor = true;
}
const AlnRes& r1 = anchor1 ?
res->alres : oresGap_.alres;
if(!redMate1_.overlap(r1)) {
redMate1_.add(r1);
if(msink->report(0, &r1, NULL)) {
doneUnpaired = true; // Short-circuited
}
}
}
// Report alignment for mate #2 as an
// unpaired alignment.
if(anchor1 || !didAnchor) {
if(!anchor1) {
didAnchor = true;
}
const AlnRes& r2 = anchor1 ?
oresGap_.alres : res->alres;
if(!redMate2_.overlap(r2)) {
redMate2_.add(r2);
if(msink->report(0, NULL, &r2)) {
doneUnpaired = true; // Short-circuited
}
}
}
//} // if(mixed || discord)
bool donePaired = false;
if(pairCl != PE_ALS_DISCORD) {
foundConcordant = true;
if(msink->report(
0,
anchor1 ? &res->alres : &oresGap_.alres,
anchor1 ? &oresGap_.alres : &res->alres))
{
// Short-circuited because a limit, e.g.
// -k, -m or -M, was exceeded
donePaired = true;
} else {
if(tighten > 0 && msink->Mmode() && msink->hasSecondBestPair()) {
// Paired-end alignments should have at least this score from now
TAlScore ps;
if(tighten == 1) {
ps = msink->bestPair();
} else if(tighten == 2) {
ps = msink->secondBestPair();
} else {
TAlScore diff = msink->bestPair() - msink->secondBestPair();
ps = msink->secondBestPair() + (diff * 3)/4;
}
if(tighten == 1 && ps < bestPairScore &&
msink->bestPair() == msink->secondBestPair())
{
ps++;
}
if(tighten >= 2 && ps < bestPairScore) {
ps++;
}
// Anchor mate must have score at least 'ps' minus the best possible
// score for the opposite mate.
TAlScore nc = ps - operfectScore;
if(nc > minsc) {
minsc = nc;
assert_leq(minsc, perfectScore);
if(minsc > res->alres.score().score()) {
// We're done with this anchor
break;
}
}
assert_leq(minsc, perfectScore);
}
}
} // if(pairCl != PE_ALS_DISCORD)
if(donePaired || doneUnpaired) {
return EXTEND_POLICY_FULFILLED;
}
if(msink->state().doneWithMate(anchor1)) {
// We're now done with the mate that we're
// currently using as our anchor. We're not
// with the read overall.
return EXTEND_POLICY_FULFILLED;
}
} else if((mixed || discord) && !didAnchor) {
didAnchor = true;
// Report unpaired hit for anchor
assert(msink != NULL);
assert(res->repOk());
// Check that alignment accurately reflects the
// reference characters aligned to
assert(res->alres.matchesRef(
rd,
ref,
tmp_rf_,
tmp_rdseq_,
tmp_qseq_,
raw_refbuf_,
raw_destU32_,
raw_matches_));
// Report an unpaired alignment
assert(!msink->maxed());
assert(!msink->state().done());
// Report alignment for mate #1 as an
// unpaired alignment.
if(!msink->state().doneUnpaired(anchor1)) {
const AlnRes& r = res->alres;
RedundantAlns& red = anchor1 ? redMate1_ : redMate2_;
const AlnRes* r1 = anchor1 ? &res->alres : NULL;
const AlnRes* r2 = anchor1 ? NULL : &res->alres;
if(!red.overlap(r)) {
red.add(r);
if(msink->report(0, r1, r2)) {
return EXTEND_POLICY_FULFILLED; // Short-circuited
}
}
}
if(msink->state().doneWithMate(anchor1)) {
// Done with mate, but not read overall
return EXTEND_POLICY_FULFILLED;
}
}
}
} while(!oresGap_.empty());
} // if(found && swMateImmediately)
else if(found) {
assert(!msink->state().doneWithMate(anchor1));
// We found an anchor alignment but did not attempt to find
// an alignment for the opposite mate (probably because
// we're done with it)
if(reportImmediately && (mixed || discord)) {
// Report unpaired hit for anchor
assert(msink != NULL);
assert(res->repOk());
// Check that alignment accurately reflects the
// reference characters aligned to
assert(res->alres.matchesRef(
rd,
ref,
tmp_rf_,
tmp_rdseq_,
tmp_qseq_,
raw_refbuf_,
raw_destU32_,
raw_matches_));
// Report an unpaired alignment
assert(!msink->maxed());
assert(!msink->state().done());
// Report alignment for mate #1 as an
// unpaired alignment.
if(!msink->state().doneUnpaired(anchor1)) {
const AlnRes& r = res->alres;
RedundantAlns& red = anchor1 ? redMate1_ : redMate2_;
const AlnRes* r1 = anchor1 ? &res->alres : NULL;
const AlnRes* r2 = anchor1 ? NULL : &res->alres;
if(!red.overlap(r)) {
red.add(r);
if(msink->report(0, r1, r2)) {
return EXTEND_POLICY_FULFILLED; // Short-circuited
}
}
}
if(msink->state().doneWithMate(anchor1)) {
// Done with mate, but not read overall
return EXTEND_POLICY_FULFILLED;
}
}
}
} // while(true)
if(foundConcordant) {
prm.nMateDpSuccs++;
mateStreaks_[i] = 0;
// Register this as a success. Now we need to
// make the streak variables reflect the
// success.
if(state == FOUND_UNGAPPED) {
assert_gt(prm.nUgFail, 0);
assert_gt(prm.nExUgFails, 0);
prm.nExUgFails--;
prm.nExUgSuccs++;
prm.nUgLastSucc = prm.nExUgs-1;
if(prm.nUgFail > prm.nUgFailStreak) {
prm.nUgFailStreak = prm.nUgFail;
}
prm.nUgFail = 0;
} else if(state == FOUND_EE) {
assert_gt(prm.nEeFail, 0);
assert_gt(prm.nExEeFails, 0);
prm.nExEeFails--;
prm.nExEeSuccs++;
prm.nEeLastSucc = prm.nExEes-1;
if(prm.nEeFail > prm.nEeFailStreak) {
prm.nEeFailStreak = prm.nEeFail;
}
prm.nEeFail = 0;
} else {
assert_gt(prm.nDpFail, 0);
assert_gt(prm.nExDpFails, 0);
prm.nExDpFails--;
prm.nExDpSuccs++;
prm.nDpLastSucc = prm.nExDps-1;
if(prm.nDpFail > prm.nDpFailStreak) {
prm.nDpFailStreak = prm.nDpFail;
}
prm.nDpFail = 0;
}
} else {
prm.nMateDpFails++;
mateStreaks_[i]++;
}
// At this point we know that we aren't bailing, and will continue to resolve seed hits.
} // while(!gw.done())
} // for(size_t i = 0; i < gws_.size(); i++)
}
return EXTEND_EXHAUSTED_CANDIDATES;
}
|