/usr/include/shogun/lib/Trie.h is in libshogun-dev 3.2.0-7.5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
* This program 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.
*
* Written (W) 1999-2009 Soeren Sonnenburg
* Written (W) 1999-2009 Gunnar Raetsch
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/
#ifndef _TRIE_H___
#define _TRIE_H___
#include <string.h>
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>
#include <shogun/base/DynArray.h>
#include <shogun/mathematics/Math.h>
#include <shogun/base/SGObject.h>
namespace shogun
{
#ifndef DOXYGEN_SHOULD_SKIP_THIS
// sentinel is 0xFFFFFFFC or float -2
#define NO_CHILD ((int32_t)-1073741824)
#define WEIGHTS_IN_TRIE
//#define TRIE_CHECK_EVERYTHING
#ifdef TRIE_CHECK_EVERYTHING
#define TRIE_ASSERT_EVERYTHING(x) ASSERT(x)
#else
#define TRIE_ASSERT_EVERYTHING(x)
#endif
//#define TRIE_ASSERT(x) ASSERT(x)
#define TRIE_ASSERT(x)
#define TRIE_TERMINAL_CHARACTER 7
/** consensus entry */
struct ConsensusEntry
{
/** string */
uint64_t string;
/** score */
float32_t score;
/** bt */
int32_t bt;
};
/** POIM trie */
struct POIMTrie
{
/** weight */
float64_t weight;
#ifdef TRIE_CHECK_EVERYTHING
/** has sequence */
bool has_seq;
/** has floats */
bool has_floats;
#endif
union
{
/** child weights */
float32_t child_weights[4];
/** children */
int32_t children[4];
/** sequence */
uint8_t seq[16] ;
};
/** super_string_score */
float64_t S;
/** left_partial_overlap_score */
float64_t L;
/** right_partial_overlap_score */
float64_t R;
};
/** DNA trie */
struct DNATrie
{
/** weight */
float64_t weight;
#ifdef TRIE_CHECK_EVERYTHING
/** has sequence */
bool has_seq;
/** has floats */
bool has_floats;
#endif
union
{
/** child weights */
float32_t child_weights[4];
/** children */
int32_t children[4];
/** sequence */
uint8_t seq[16] ;
};
};
/** tree parse info */
struct TreeParseInfo {
/** number of symbols */
int32_t num_sym;
/** number of features */
int32_t num_feat;
/** p */
int32_t p;
/** k */
int32_t k;
/** nofsKmers */
int32_t* nofsKmers;
/** margFactors */
float64_t* margFactors;
/** x */
int32_t* x;
/** substrs */
int32_t* substrs;
/** y0 */
int32_t y0;
/** C k */
float64_t* C_k;
/** L k */
float64_t* L_k;
/** R k */
float64_t* R_k;
};
#endif // DOXYGEN_SHOULD_SKIP_THIS
template <class Trie> class CTrie;
#define IGNORE_IN_CLASSLIST
/** @brief Template class Trie implements a suffix trie, i.e. a tree in which all
* suffixes up to a certain length are stored.
*
* It is excessively used in the CWeightedDegreeStringKernel and
* CWeightedDegreePositionStringKernel to construct the whole features space
* \f$\Phi(x)\f$ and enormously helps here to speed up SVM training and
* evaluation.
*
* Note that depending on the underlying structure used, a single symbol in the
* tree requires 20 bytes (DNATrie). It is also used to do the efficient
* recursion in computing positional oligomer importance matrices (POIMs) where
* the structure requires * 20+3*8 (POIMTrie) bytes.
*
* Finally note that this tree may use compact internal nodes (for strings that
* appear without modifications, thus not requiring further branches), which
* may save a lot of memory on higher degree tries.
*
*/
IGNORE_IN_CLASSLIST template <class Trie> class CTrie : public CSGObject
{
public:
/** default constructor */
CTrie();
/** constructor
*
* @param d degree
* @param p_use_compact_terminal_nodes if compact terminal nodes shall
* be used
*/
CTrie(int32_t d, bool p_use_compact_terminal_nodes=true);
/** copy constructor */
CTrie(const CTrie & to_copy);
virtual ~CTrie();
/** overload operator = */
const CTrie & operator=(const CTrie & to_copy);
/** compare traverse
*
* @param node node
* @param other other trie
* @param other_node other node
* @return if comparison was successful
*/
bool compare_traverse(
int32_t node, const CTrie & other, int32_t other_node);
/** compare
*
* @param other other trie
* @return if comparison was successful
*/
bool compare(const CTrie & other);
/** find node
*
* @param node node to find
* @param trace trace
* @param trace_len length of trace
*/
bool find_node(int32_t node, int32_t * trace, int32_t &trace_len) const;
/** find deepest node
*
* @param start_node start node
* @param deepest_node deepest node will be stored in here
* @return depth of deepest node
*/
int32_t find_deepest_node(
int32_t start_node, int32_t &deepest_node) const;
/** display node
*
* @param node node to display
*/
void display_node(int32_t node) const;
/** destroy */
void destroy();
/** set degree
*
* @param d new degree
*/
void set_degree(int32_t d);
/** create
*
* @param len length of new trie
* @param p_use_compact_terminal_nodes if compact terminal nodes shall
* be used
*/
void create(int32_t len, bool p_use_compact_terminal_nodes=true);
/** delete trees
*
* @param p_use_compact_terminal_nodes if compact terminal nodes shall
* be used
*/
void delete_trees(bool p_use_compact_terminal_nodes=true);
/** add to trie
*
* @param i i
* @param seq_offset sequence offset
* @param vec vector
* @param alpha alpha
* @param weights weights
* @param degree_times_position_weights if degree times position
* weights shall be applied
*/
void add_to_trie(
int32_t i, int32_t seq_offset, int32_t* vec, float32_t alpha,
float64_t *weights, bool degree_times_position_weights);
/** compute absolute weights tree
*
* @param tree tree to compute for
* @param depth depth
* @return computed absolute weights tree
*/
float64_t compute_abs_weights_tree(int32_t tree, int32_t depth);
/** compute absolute weights
*
* @param len length
* @return computed absolute weights
*/
float64_t* compute_abs_weights(int32_t &len);
/** compute by tree helper
*
* @param vec vector
* @param len length
* @param seq_pos sequence position
* @param tree_pos tree position
* @param weight_pos weight position
* @param weights
* @param degree_times_position_weights if degree times position
* weights shall be applied
* @return a computed value
*/
float64_t compute_by_tree_helper(
int32_t* vec, int32_t len, int32_t seq_pos, int32_t tree_pos,
int32_t weight_pos, float64_t * weights,
bool degree_times_position_weights) ;
/** compute by tree helper
*
* @param vec vector
* @param len length
* @param seq_pos sequence position
* @param tree_pos tree position
* @param weight_pos weight position
* @param LevelContrib level contribution
* @param factor factor
* @param mkl_stepsize MKL stepsize
* @param weights
* @param degree_times_position_weights if degree times position
* weights shall be applied
*/
void compute_by_tree_helper(
int32_t* vec, int32_t len, int32_t seq_pos, int32_t tree_pos,
int32_t weight_pos, float64_t* LevelContrib, float64_t factor,
int32_t mkl_stepsize, float64_t * weights,
bool degree_times_position_weights);
/** compute scoring helper
*
* @param tree tree
* @param i i
* @param j j
* @param weight weight
* @param d degree
* @param max_degree maximum degree
* @param num_feat number of features
* @param num_sym number of symbols
* @param sym_offset symbol offset
* @param offs offsets
* @param result result
*/
void compute_scoring_helper(
int32_t tree, int32_t i, int32_t j, float64_t weight, int32_t d,
int32_t max_degree, int32_t num_feat, int32_t num_sym,
int32_t sym_offset, int32_t offs, float64_t* result);
/** add example to tree mismatch recursion
*
* @param tree tree
* @param i i
* @param alpha alpha
* @param vec vector
* @param len_rem length of rem
* @param degree_rec degree rec
* @param mismatch_rec mismatch rec
* @param max_mismatch maximum mismatch
* @param weights weights
*/
void add_example_to_tree_mismatch_recursion(
int32_t tree, int32_t i, float64_t alpha, int32_t *vec,
int32_t len_rem, int32_t degree_rec, int32_t mismatch_rec,
int32_t max_mismatch, float64_t * weights);
/** traverse
*
* @param tree tree
* @param p p
* @param info tree parse info
* @param depth depth
* @param x x
* @param k k
*/
void traverse(
int32_t tree, const int32_t p, struct TreeParseInfo info,
const int32_t depth, int32_t* const x, const int32_t k);
/** count
*
* @param w w
* @param depth depth
* @param info tree parse info
* @param p p
* @param x x
* @param k
*/
void count(
const float64_t w, const int32_t depth,
const struct TreeParseInfo info, const int32_t p, int32_t* x,
const int32_t k);
/** compact nodes
*
* @param start_node start node
* @param depth depth
* @param weights weights
*/
int32_t compact_nodes(int32_t start_node, int32_t depth, float64_t * weights);
/** get cumulative score
*
* @param pos position
* @param seq sequence
* @param deg degree
* @param weights weights
* @return cumulative score
*/
float64_t get_cumulative_score(
int32_t pos, uint64_t seq, int32_t deg, float64_t* weights);
/** fill backtracking table recursion
*
* @param tree tree
* @param depth depth
* @param seq sequence
* @param value value
* @param table table of concensus entries
* @param weights weights
*/
void fill_backtracking_table_recursion(
Trie* tree, int32_t depth, uint64_t seq, float64_t value,
DynArray<ConsensusEntry>* table, float64_t* weights);
/** fill backtracking table
*
* @param pos position
* @param prev previous concencus entry
* @param cur current concensus entry
* @param cumulative if is cumulative
* @param weights weights
*/
void fill_backtracking_table(
int32_t pos, DynArray<ConsensusEntry>* prev,
DynArray<ConsensusEntry>* cur, bool cumulative,
float64_t* weights);
/** POIMs extract W
*
* @param W W
* @param K K
*/
void POIMs_extract_W(float64_t* const* const W, const int32_t K);
/** POIMs precalc SLR
*
* @param distrib distribution
*/
void POIMs_precalc_SLR(const float64_t* const distrib);
/** POIMs get SLR
*
* @param parentIdx parent index
* @param sym symbol
* @param depth depth
* @param S will point to S
* @param L will point to L
* @param R will point to R
*/
void POIMs_get_SLR(
const int32_t parentIdx, const int32_t sym, const int32_t depth,
float64_t* S, float64_t* L, float64_t* R);
/** POIMs add SLR
*
* @param poims POIMs
* @param K K
* @param debug debug level
*/
void POIMs_add_SLR(
float64_t* const* const poims, const int32_t K,
const int32_t debug);
/** get use compact terminal nodes
*
* @return if compact terminal nodes are used
*/
inline bool get_use_compact_terminal_nodes()
{
return use_compact_terminal_nodes ;
}
/** set use compact terminal nodes
*
* @param p_use_compact_terminal_nodes if compact terminal nodes shall
* be used
*/
inline void set_use_compact_terminal_nodes(
bool p_use_compact_terminal_nodes)
{
use_compact_terminal_nodes=p_use_compact_terminal_nodes ;
}
/** get number of used nodes
*
* @return number of used nodes
*/
inline int32_t get_num_used_nodes()
{
return TreeMemPtr;
}
/** set position weights
*
* @param p_position_weights new position weights
*/
inline void set_position_weights(float64_t* p_position_weights)
{
position_weights=p_position_weights;
}
/** get node
*
* @return node
*/
inline int32_t get_node(bool last_node=false)
{
int32_t ret = TreeMemPtr++;
check_treemem() ;
if (last_node)
{
for (int32_t q=0; q<4; q++)
TreeMem[ret].child_weights[q]=0.0;
}
else
{
for (int32_t q=0; q<4; q++)
TreeMem[ret].children[q]=NO_CHILD;
}
#ifdef TRIE_CHECK_EVERYTHING
TreeMem[ret].has_seq=false ;
TreeMem[ret].has_floats=false ;
#endif
TreeMem[ret].weight=0.0;
return ret ;
}
/** check tree memory usage */
inline void check_treemem()
{
if (TreeMemPtr+10 < TreeMemPtrMax)
return;
SG_DEBUG("Extending TreeMem from %i to %i elements\n",
TreeMemPtrMax, (int32_t) ((float64_t)TreeMemPtrMax*1.2));
int32_t old_sz=TreeMemPtrMax;
TreeMemPtrMax = (int32_t) ((float64_t)TreeMemPtrMax*1.2);
TreeMem = SG_REALLOC(Trie, TreeMem, old_sz, TreeMemPtrMax);
}
/** set weights in tree
*
* @param weights_in_tree_ if weights shall be in tree
*/
inline void set_weights_in_tree(bool weights_in_tree_)
{
weights_in_tree = weights_in_tree_;
}
/** get weights in tree
*
* @return if weights are in tree
*/
inline bool get_weights_in_tree()
{
return weights_in_tree;
}
/** POIMs extract W helper
*
* @param nodeIdx node index
* @param depth depth
* @param offset offset
* @param y0 y0
* @param W W
* @param K K
*/
void POIMs_extract_W_helper(
const int32_t nodeIdx, const int32_t depth, const int32_t offset,
const int32_t y0, float64_t* const* const W, const int32_t K);
/** POIMs calc SLR helper
*
* @param distrib distribution
* @param i i
* @param nodeIdx node index
* @param left_tries_idx left tries index
* @param depth depth
* @param lastSym last symbol
* @param S S
* @param L L
* @param R R
*/
void POIMs_calc_SLR_helper1(
const float64_t* const distrib, const int32_t i,
const int32_t nodeIdx, int32_t left_tries_idx[4],
const int32_t depth, int32_t const lastSym, float64_t* S,
float64_t* L, float64_t* R);
/** POIMs calc SLR helper 2
* @param distrib distribution
* @param i i
* @param nodeIdx node index
* @param left_tries_idx left tries index
* @param depth depth
* @param S S
* @param L L
* @param R R
*/
void POIMs_calc_SLR_helper2(
const float64_t* const distrib, const int32_t i,
const int32_t nodeIdx, int32_t left_tries_idx[4],
const int32_t depth, float64_t* S, float64_t* L, float64_t* R);
/** POIMs add SLR helper 1
*
* @param nodeIdx node index
* @param depth depth
* @param i i
* @param y0 y0
* @param poims POIMs
* @param K K
* @param debug debug level
*/
void POIMs_add_SLR_helper1(
const int32_t nodeIdx, const int32_t depth,const int32_t i,
const int32_t y0, float64_t* const* const poims, const int32_t K,
const int32_t debug);
/** POIMs add SLR helper 2
*
* @param poims POIMs
* @param K K
* @param k k
* @param i i
* @param y y
* @param valW value W
* @param valS value S
* @param valL value L
* @param valR value R
* @param debug debug level
*/
void POIMs_add_SLR_helper2(
float64_t* const* const poims, const int32_t K, const int32_t k,
const int32_t i, const int32_t y, const float64_t valW,
const float64_t valS, const float64_t valL, const float64_t valR,
const int32_t debug);
/** @return object name */
virtual const char* get_name() const { return "Trie"; }
public:
/** number of symbols */
int32_t NUM_SYMS;
protected:
/** length */
int32_t length;
/** trees */
int32_t * trees;
/** degree */
int32_t degree;
/** position weights */
float64_t* position_weights;
/** tree memory */
Trie* TreeMem;
/** tree memory pointer */
int32_t TreeMemPtr;
/** tree memory pointer maximum */
int32_t TreeMemPtrMax;
/** if compact terminal nodes are used */
bool use_compact_terminal_nodes;
/** if weights are in tree */
bool weights_in_tree;
/** nofsKmers */
int32_t* nofsKmers;
};
template <class Trie>
CTrie<Trie>::CTrie()
: CSGObject(), degree(0), position_weights(NULL),
use_compact_terminal_nodes(false),
weights_in_tree(true)
{
TreeMemPtrMax=0;
TreeMemPtr=0;
TreeMem=NULL;
length=0;
trees=NULL;
NUM_SYMS=4;
}
template <class Trie>
CTrie<Trie>::CTrie(int32_t d, bool p_use_compact_terminal_nodes)
: CSGObject(), degree(d), position_weights(NULL),
use_compact_terminal_nodes(p_use_compact_terminal_nodes),
weights_in_tree(true)
{
TreeMemPtrMax=1024*1024/sizeof(Trie);
TreeMemPtr=0;
TreeMem=SG_MALLOC(Trie, TreeMemPtrMax);
length=0;
trees=NULL;
NUM_SYMS=4;
}
template <class Trie>
CTrie<Trie>::CTrie(const CTrie & to_copy)
: CSGObject(to_copy), degree(to_copy.degree), position_weights(NULL),
use_compact_terminal_nodes(to_copy.use_compact_terminal_nodes)
{
if (to_copy.position_weights!=NULL)
{
position_weights = to_copy.position_weights;
/*SG_MALLOC(float64_t, to_copy.length);
for (int32_t i=0; i<to_copy.length; i++)
position_weights[i]=to_copy.position_weights[i]; */
}
else
position_weights=NULL;
TreeMemPtrMax=to_copy.TreeMemPtrMax;
TreeMemPtr=to_copy.TreeMemPtr;
TreeMem=SG_MALLOC(Trie, TreeMemPtrMax);
memcpy(TreeMem, to_copy.TreeMem, TreeMemPtrMax*sizeof(Trie));
length=to_copy.length;
trees=SG_MALLOC(int32_t, length);
for (int32_t i=0; i<length; i++)
trees[i]=to_copy.trees[i];
NUM_SYMS=4;
}
template <class Trie>
const CTrie<Trie> &CTrie<Trie>::operator=(const CTrie<Trie> & to_copy)
{
degree=to_copy.degree ;
use_compact_terminal_nodes=to_copy.use_compact_terminal_nodes ;
SG_FREE(position_weights);
position_weights=NULL ;
if (to_copy.position_weights!=NULL)
{
position_weights=to_copy.position_weights ;
/*position_weights = SG_MALLOC(float64_t, to_copy.length);
for (int32_t i=0; i<to_copy.length; i++)
position_weights[i]=to_copy.position_weights[i] ;*/
}
else
position_weights=NULL ;
TreeMemPtrMax=to_copy.TreeMemPtrMax ;
TreeMemPtr=to_copy.TreeMemPtr ;
SG_FREE(TreeMem) ;
TreeMem = SG_MALLOC(Trie, TreeMemPtrMax);
memcpy(TreeMem, to_copy.TreeMem, TreeMemPtrMax*sizeof(Trie)) ;
length = to_copy.length ;
if (trees)
SG_FREE(trees);
trees=SG_MALLOC(int32_t, length);
for (int32_t i=0; i<length; i++)
trees[i]=to_copy.trees[i] ;
return *this ;
}
template <class Trie>
int32_t CTrie<Trie>::find_deepest_node(
int32_t start_node, int32_t& deepest_node) const
{
#ifdef TRIE_CHECK_EVERYTHING
int32_t ret=0 ;
SG_DEBUG("start_node=%i\n", start_node)
if (start_node==NO_CHILD)
{
for (int32_t i=0; i<length; i++)
{
int32_t my_deepest_node ;
int32_t depth=find_deepest_node(i, my_deepest_node) ;
SG_DEBUG("start_node %i depth=%i\n", i, depth)
if (depth>ret)
{
deepest_node=my_deepest_node ;
ret=depth ;
}
}
return ret ;
}
if (TreeMem[start_node].has_seq)
{
for (int32_t q=0; q<16; q++)
if (TreeMem[start_node].seq[q]!=TRIE_TERMINAL_CHARACTER)
ret++ ;
deepest_node=start_node ;
return ret ;
}
if (TreeMem[start_node].has_floats)
{
deepest_node=start_node ;
return 1 ;
}
for (int32_t q=0; q<4; q++)
{
int32_t my_deepest_node ;
if (TreeMem[start_node].children[q]==NO_CHILD)
continue ;
int32_t depth=find_deepest_node(abs(TreeMem[start_node].children[q]), my_deepest_node) ;
if (depth>ret)
{
deepest_node=my_deepest_node ;
ret=depth ;
}
}
return ret ;
#else
SG_ERROR("not implemented\n")
return 0 ;
#endif
}
template <class Trie>
int32_t CTrie<Trie>::compact_nodes(
int32_t start_node, int32_t depth, float64_t * weights)
{
SG_ERROR("code buggy\n")
int32_t ret=0 ;
if (start_node==NO_CHILD)
{
for (int32_t i=0; i<length; i++)
compact_nodes(i,1, weights) ;
return 0 ;
}
if (start_node<0)
return -1 ;
if (depth==degree-1)
{
TRIE_ASSERT_EVERYTHING(TreeMem[start_node].has_floats)
int32_t num_used=0 ;
for (int32_t q=0; q<4; q++)
if (TreeMem[start_node].child_weights[q]!=0.0)
num_used++ ;
if (num_used>1)
return -1 ;
return 1 ;
}
TRIE_ASSERT_EVERYTHING(!TreeMem[start_node].has_floats)
int32_t num_used = 0 ;
int32_t q_used=-1 ;
for (int32_t q=0; q<4; q++)
{
if (TreeMem[start_node].children[q]==NO_CHILD)
continue ;
num_used++ ;
q_used=q ;
}
if (num_used>1)
{
if (depth>=degree-2)
return -1 ;
for (int32_t q=0; q<4; q++)
{
if (TreeMem[start_node].children[q]==NO_CHILD)
continue ;
int32_t num=compact_nodes(abs(TreeMem[start_node].children[q]), depth+1, weights) ;
if (num<=2)
continue ;
int32_t node=get_node() ;
int32_t last_node=TreeMem[start_node].children[q] ;
if (weights_in_tree)
{
ASSERT(weights[depth]!=0.0)
TreeMem[node].weight=TreeMem[last_node].weight/weights[depth] ;
}
else
TreeMem[node].weight=TreeMem[last_node].weight ;
#ifdef TRIE_CHECK_EVERYTHING
TreeMem[node].has_seq=true ;
#endif
memset(TreeMem[node].seq, TRIE_TERMINAL_CHARACTER, 16) ;
for (int32_t n=0; n<num; n++)
{
ASSERT(depth+n+1<=degree-1)
ASSERT(last_node!=NO_CHILD)
if (depth+n+1==degree-1)
{
TRIE_ASSERT_EVERYTHING(TreeMem[last_node].has_floats)
int32_t k ;
for (k=0; k<4; k++)
if (TreeMem[last_node].child_weights[k]!=0.0)
break ;
if (k==4)
break ;
TreeMem[node].seq[n]=k ;
break ;
}
else
{
TRIE_ASSERT_EVERYTHING(!TreeMem[last_node].has_floats)
int32_t k ;
for (k=0; k<4; k++)
if (TreeMem[last_node].children[k]!=NO_CHILD)
break ;
if (k==4)
break ;
TreeMem[node].seq[n]=k ;
last_node=TreeMem[last_node].children[k] ;
}
}
TreeMem[start_node].children[q]=-node ;
}
return -1 ;
}
if (num_used==0)
return 0 ;
ret=compact_nodes(abs(TreeMem[start_node].children[q_used]), depth+1, weights) ;
if (ret<0)
return ret ;
return ret+1 ;
}
template <class Trie>
bool CTrie<Trie>::compare_traverse(
int32_t node, const CTrie<Trie> & other, int32_t other_node)
{
SG_DEBUG("checking nodes %i and %i\n", node, other_node)
if (fabs(TreeMem[node].weight-other.TreeMem[other_node].weight)>=1e-5)
{
SG_DEBUG("CTrie::compare: TreeMem[%i].weight=%f!=other.TreeMem[%i].weight=%f\n", node, TreeMem[node].weight, other_node,other.TreeMem[other_node].weight)
SG_DEBUG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
display_node(node) ;
SG_DEBUG("============================================================\n")
other.display_node(other_node) ;
SG_DEBUG("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n")
return false ;
}
#ifdef TRIE_CHECK_EVERYTHING
if (TreeMem[node].has_seq!=other.TreeMem[other_node].has_seq)
{
SG_DEBUG("CTrie::compare: TreeMem[%i].has_seq=%i!=other.TreeMem[%i].has_seq=%i\n", node, TreeMem[node].has_seq, other_node,other.TreeMem[other_node].has_seq)
SG_DEBUG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
display_node(node) ;
SG_DEBUG("============================================================\n")
other.display_node(other_node) ;
SG_DEBUG("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n")
return false ;
}
if (TreeMem[node].has_floats!=other.TreeMem[other_node].has_floats)
{
SG_DEBUG("CTrie::compare: TreeMem[%i].has_floats=%i!=other.TreeMem[%i].has_floats=%i\n", node, TreeMem[node].has_floats, other_node, other.TreeMem[other_node].has_floats)
return false ;
}
if (other.TreeMem[other_node].has_floats)
{
for (int32_t q=0; q<4; q++)
if (fabs(TreeMem[node].child_weights[q]-other.TreeMem[other_node].child_weights[q])>1e-5)
{
SG_DEBUG("CTrie::compare: TreeMem[%i].child_weights[%i]=%e!=other.TreeMem[%i].child_weights[%i]=%e\n", node, q,TreeMem[node].child_weights[q], other_node,q,other.TreeMem[other_node].child_weights[q])
SG_DEBUG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
display_node(node) ;
SG_DEBUG("============================================================\n")
other.display_node(other_node) ;
SG_DEBUG("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n")
return false ;
}
}
if (other.TreeMem[other_node].has_seq)
{
for (int32_t q=0; q<16; q++)
if ((TreeMem[node].seq[q]!=other.TreeMem[other_node].seq[q]) && ((TreeMem[node].seq[q]<4)||(other.TreeMem[other_node].seq[q]<4)))
{
SG_DEBUG("CTrie::compare: TreeMem[%i].seq[%i]=%i!=other.TreeMem[%i].seq[%i]=%i\n", node,q,TreeMem[node].seq[q], other_node,q,other.TreeMem[other_node].seq[q])
SG_DEBUG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
display_node(node) ;
SG_DEBUG("============================================================\n")
other.display_node(other_node) ;
SG_DEBUG("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n")
return false ;
}
}
if (!other.TreeMem[other_node].has_seq && !other.TreeMem[other_node].has_floats)
{
for (int32_t q=0; q<4; q++)
{
if ((TreeMem[node].children[q]==NO_CHILD) && (other.TreeMem[other_node].children[q]==NO_CHILD))
continue ;
if ((TreeMem[node].children[q]==NO_CHILD)!=(other.TreeMem[other_node].children[q]==NO_CHILD))
{
SG_DEBUG("CTrie::compare: TreeMem[%i].children[%i]=%i!=other.TreeMem[%i].children[%i]=%i\n", node,q,TreeMem[node].children[q], other_node,q,other.TreeMem[other_node].children[q])
SG_DEBUG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
display_node(node) ;
SG_DEBUG("============================================================\n")
other.display_node(other_node) ;
SG_DEBUG("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n")
return false ;
}
if (!compare_traverse(abs(TreeMem[node].children[q]), other, abs(other.TreeMem[other_node].children[q])))
return false ;
}
}
#else
SG_ERROR("not implemented\n")
#endif
return true ;
}
template <class Trie>
bool CTrie<Trie>::compare(const CTrie<Trie> & other)
{
bool ret=true ;
for (int32_t i=0; i<length; i++)
if (!compare_traverse(trees[i], other, other.trees[i]))
return false ;
else
SG_DEBUG("two tries at %i identical\n", i)
return ret ;
}
template <class Trie>
bool CTrie<Trie>::find_node(
int32_t node, int32_t * trace, int32_t& trace_len) const
{
#ifdef TRIE_CHECK_EVERYTHING
ASSERT(trace_len-1>=0)
ASSERT((trace[trace_len-1]>=0) && (trace[trace_len-1]<TreeMemPtrMax))
if (TreeMem[trace[trace_len-1]].has_seq)
return false ;
if (TreeMem[trace[trace_len-1]].has_floats)
return false ;
for (int32_t q=0; q<4; q++)
{
if (TreeMem[trace[trace_len-1]].children[q]==NO_CHILD)
continue ;
int32_t tl=trace_len+1 ;
if (TreeMem[trace[trace_len-1]].children[q]>=0)
trace[trace_len]=TreeMem[trace[trace_len-1]].children[q] ;
else
trace[trace_len]=-TreeMem[trace[trace_len-1]].children[q] ;
if (trace[trace_len]==node)
{
trace_len=tl ;
return true ;
}
if (find_node(node, trace, tl))
{
trace_len=tl ;
return true ;
}
}
trace_len=0 ;
return false ;
#else
SG_ERROR("not implemented\n")
return false ;
#endif
}
template <class Trie>
void CTrie<Trie>::display_node(int32_t node) const
{
#ifdef TRIE_CHECK_EVERYTHING
int32_t * trace=SG_MALLOC(int32_t, 2*degree);
int32_t trace_len=-1 ;
bool found = false ;
int32_t tree=-1 ;
for (tree=0; tree<length; tree++)
{
trace[0]=trees[tree] ;
trace_len=1 ;
found=find_node(node, trace, trace_len) ;
if (found)
break ;
}
ASSERT(found)
SG_PRINT("position %i trace: ", tree)
for (int32_t i=0; i<trace_len-1; i++)
{
int32_t branch=-1 ;
for (int32_t q=0; q<4; q++)
if (abs(TreeMem[trace[i]].children[q])==trace[i+1])
{
branch=q;
break ;
}
ASSERT(branch!=-1)
char acgt[5]="ACGT" ;
SG_PRINT("%c", acgt[branch])
}
SG_PRINT("\nnode=%i\nweight=%f\nhas_seq=%i\nhas_floats=%i\n", node, TreeMem[node].weight, TreeMem[node].has_seq, TreeMem[node].has_floats)
if (TreeMem[node].has_floats)
{
for (int32_t q=0; q<4; q++)
SG_PRINT("child_weighs[%i] = %f\n", q, TreeMem[node].child_weights[q])
}
if (TreeMem[node].has_seq)
{
for (int32_t q=0; q<16; q++)
SG_PRINT("seq[%i] = %i\n", q, TreeMem[node].seq[q])
}
if (!TreeMem[node].has_seq && !TreeMem[node].has_floats)
{
for (int32_t q=0; q<4; q++)
{
if (TreeMem[node].children[q]!=NO_CHILD)
{
SG_PRINT("children[%i] = %i -> \n", q, TreeMem[node].children[q])
display_node(abs(TreeMem[node].children[q])) ;
}
else
SG_PRINT("children[%i] = NO_CHILD -| \n", q, TreeMem[node].children[q])
}
}
SG_FREE(trace);
#else
SG_ERROR("not implemented\n")
#endif
}
template <class Trie> CTrie<Trie>::~CTrie()
{
destroy() ;
SG_FREE(TreeMem) ;
}
template <class Trie> void CTrie<Trie>::destroy()
{
if (trees!=NULL)
{
delete_trees();
for (int32_t i=0; i<length; i++)
trees[i] = NO_CHILD;
SG_FREE(trees);
TreeMemPtr=0;
length=0;
trees=NULL;
}
}
template <class Trie> void CTrie<Trie>::set_degree(int32_t d)
{
delete_trees(get_use_compact_terminal_nodes());
degree=d;
}
template <class Trie> void CTrie<Trie>::create(
int32_t len, bool p_use_compact_terminal_nodes)
{
destroy();
trees=SG_MALLOC(int32_t, len);
TreeMemPtr=0 ;
for (int32_t i=0; i<len; i++)
trees[i]=get_node(degree==1);
length = len ;
use_compact_terminal_nodes=p_use_compact_terminal_nodes ;
}
template <class Trie> void CTrie<Trie>::delete_trees(
bool p_use_compact_terminal_nodes)
{
if (trees==NULL)
return;
TreeMemPtr=0 ;
for (int32_t i=0; i<length; i++)
trees[i]=get_node(degree==1);
use_compact_terminal_nodes=p_use_compact_terminal_nodes ;
}
template <class Trie>
float64_t CTrie<Trie>::compute_abs_weights_tree(int32_t tree, int32_t depth)
{
float64_t ret=0 ;
if (tree==NO_CHILD)
return 0 ;
TRIE_ASSERT(tree>=0)
if (depth==degree-2)
{
ret+=(TreeMem[tree].weight) ;
for (int32_t k=0; k<4; k++)
ret+=(TreeMem[tree].child_weights[k]) ;
return ret ;
}
ret+=(TreeMem[tree].weight) ;
for (int32_t i=0; i<4; i++)
if (TreeMem[tree].children[i]!=NO_CHILD)
ret += compute_abs_weights_tree(TreeMem[tree].children[i], depth+1) ;
return ret ;
}
template <class Trie>
float64_t *CTrie<Trie>::compute_abs_weights(int32_t &len)
{
float64_t * sum=SG_MALLOC(float64_t, length*4);
for (int32_t i=0; i<length*4; i++)
sum[i]=0 ;
len=length ;
for (int32_t i=0; i<length; i++)
{
TRIE_ASSERT(trees[i]!=NO_CHILD)
for (int32_t k=0; k<4; k++)
{
sum[i*4+k]=compute_abs_weights_tree(TreeMem[trees[i]].children[k], 0) ;
}
}
return sum ;
}
template <class Trie>
void CTrie<Trie>::add_example_to_tree_mismatch_recursion(
int32_t tree, int32_t i, float64_t alpha,
int32_t *vec, int32_t len_rem,
int32_t degree_rec, int32_t mismatch_rec,
int32_t max_mismatch, float64_t * weights)
{
if (tree==NO_CHILD)
tree=trees[i] ;
TRIE_ASSERT(tree!=NO_CHILD)
if ((len_rem<=0) || (mismatch_rec>max_mismatch) || (degree_rec>degree))
return ;
const int32_t other[4][3] = { {1,2,3},{0,2,3},{0,1,3},{0,1,2} } ;
int32_t subtree = NO_CHILD ;
if (degree_rec==degree-1)
{
TRIE_ASSERT_EVERYTHING(TreeMem[tree].has_floats)
if (weights_in_tree)
TreeMem[tree].child_weights[vec[0]] += alpha*weights[degree_rec+degree*mismatch_rec];
else
if (weights[degree_rec]!=0.0)
TreeMem[tree].child_weights[vec[0]] += alpha*weights[degree_rec+degree*mismatch_rec]/weights[degree_rec];
if (mismatch_rec+1<=max_mismatch)
for (int32_t o=0; o<3; o++)
{
if (weights_in_tree)
TreeMem[tree].child_weights[other[vec[0]][o]] += alpha*weights[degree_rec+degree*(mismatch_rec+1)];
else
if (weights[degree_rec]!=0.0)
TreeMem[tree].child_weights[other[vec[0]][o]] += alpha*weights[degree_rec+degree*(mismatch_rec+1)]/weights[degree_rec];
}
return ;
}
else
{
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_floats)
if (TreeMem[tree].children[vec[0]]!=NO_CHILD)
{
subtree=TreeMem[tree].children[vec[0]] ;
if (weights_in_tree)
TreeMem[subtree].weight += alpha*weights[degree_rec+degree*mismatch_rec];
else
if (weights[degree_rec]!=0.0)
TreeMem[subtree].weight += alpha*weights[degree_rec+degree*mismatch_rec]/weights[degree_rec];
}
else
{
int32_t tmp = get_node(degree_rec==degree-2);
ASSERT(tmp>=0)
TreeMem[tree].children[vec[0]]=tmp ;
subtree=tmp ;
#ifdef TRIE_CHECK_EVERYTHING
if (degree_rec==degree-2)
TreeMem[subtree].has_floats=true ;
#endif
if (weights_in_tree)
TreeMem[subtree].weight = alpha*weights[degree_rec+degree*mismatch_rec] ;
else
if (weights[degree_rec]!=0.0)
TreeMem[subtree].weight = alpha*weights[degree_rec+degree*mismatch_rec]/weights[degree_rec] ;
else
TreeMem[subtree].weight = 0.0 ;
}
add_example_to_tree_mismatch_recursion(subtree, i, alpha,
&vec[1], len_rem-1,
degree_rec+1, mismatch_rec, max_mismatch, weights) ;
if (mismatch_rec+1<=max_mismatch)
{
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_floats)
for (int32_t o=0; o<3; o++)
{
int32_t ot = other[vec[0]][o] ;
if (TreeMem[tree].children[ot]!=NO_CHILD)
{
subtree=TreeMem[tree].children[ot] ;
if (weights_in_tree)
TreeMem[subtree].weight += alpha*weights[degree_rec+degree*(mismatch_rec+1)];
else
if (weights[degree_rec]!=0.0)
TreeMem[subtree].weight += alpha*weights[degree_rec+degree*(mismatch_rec+1)]/weights[degree_rec];
}
else
{
int32_t tmp = get_node(degree_rec==degree-2);
ASSERT(tmp>=0)
TreeMem[tree].children[ot]=tmp ;
subtree=tmp ;
#ifdef TRIE_CHECK_EVERYTHING
if (degree_rec==degree-2)
TreeMem[subtree].has_floats=true ;
#endif
if (weights_in_tree)
TreeMem[subtree].weight = alpha*weights[degree_rec+degree*(mismatch_rec+1)] ;
else
if (weights[degree_rec]!=0.0)
TreeMem[subtree].weight = alpha*weights[degree_rec+degree*(mismatch_rec+1)]/weights[degree_rec] ;
else
TreeMem[subtree].weight = 0.0 ;
}
add_example_to_tree_mismatch_recursion(subtree, i, alpha,
&vec[1], len_rem-1,
degree_rec+1, mismatch_rec+1, max_mismatch, weights) ;
}
}
}
}
template <class Trie>
void CTrie<Trie>::compute_scoring_helper(
int32_t tree, int32_t i, int32_t j, float64_t weight, int32_t d,
int32_t max_degree, int32_t num_feat, int32_t num_sym, int32_t sym_offset,
int32_t offs, float64_t* result)
{
if (i+j<num_feat)
{
float64_t decay=1.0; //no decay by default
//if (j>d)
// decay=pow(0.5,j); //marginalize out lower order matches
if (j<degree-1)
{
for (int32_t k=0; k<num_sym; k++)
{
if (TreeMem[tree].children[k]!=NO_CHILD)
{
int32_t child=TreeMem[tree].children[k];
//continue recursion if not yet at max_degree, else add to result
if (d<max_degree-1)
compute_scoring_helper(child, i, j+1, weight+decay*TreeMem[child].weight, d+1, max_degree, num_feat, num_sym, sym_offset, num_sym*offs+k, result);
else
result[sym_offset*(i+j-max_degree+1)+num_sym*offs+k] += weight+decay*TreeMem[child].weight;
////do recursion starting from this position
if (d==0)
compute_scoring_helper(child, i, j+1, 0.0, 0, max_degree, num_feat, num_sym, sym_offset, offs, result);
}
}
}
else if (j==degree-1)
{
for (int32_t k=0; k<num_sym; k++)
{
//continue recursion if not yet at max_degree, else add to result
if (d<max_degree-1 && i<num_feat-1)
compute_scoring_helper(trees[i+1], i+1, 0, weight+decay*TreeMem[tree].child_weights[k], d+1, max_degree, num_feat, num_sym, sym_offset, num_sym*offs+k, result);
else
result[sym_offset*(i+j-max_degree+1)+num_sym*offs+k] += weight+decay*TreeMem[tree].child_weights[k];
}
}
}
}
template <class Trie>
void CTrie<Trie>::traverse(
int32_t tree, const int32_t p, struct TreeParseInfo info,
const int32_t depth, int32_t* const x, const int32_t k)
{
const int32_t num_sym = info.num_sym;
const int32_t y0 = info.y0;
const int32_t y1 = (k==0) ? 0 : y0 - ( (depth<k) ? 0 : info.nofsKmers[k-1] * x[depth-k] );
//const int32_t temp = info.substrs[depth]*num_sym - ( (depth<=k) ? 0 : info.nofsKmers[k] * x[depth-k-1] );
//if( !( info.y0 == temp ) ) {
// printf( "\n temp=%d y0=%d k=%d depth=%d \n", temp, info.y0, k, depth );
//}
//ASSERT( info.y0 == temp )
int32_t sym;
ASSERT( depth < degree )
//ASSERT( 0 <= info.substrs[depth] && info.substrs[depth] < info.nofsKmers[k] )
if (depth<degree-1)
{
for( sym=0; sym<num_sym; ++sym ) {
const int32_t childNum = TreeMem[tree].children[ sym ];
if( childNum != NO_CHILD ) {
int32_t child = childNum ;
x[depth] = sym;
info.substrs[depth+1] = y0 + sym;
info.y0 = (k==0) ? 0 : (y1+sym)*num_sym;
//ASSERT( info.y0 == ( info.substrs[depth+1]*num_sym - ( (depth<k) ? 0 : info.nofsKmers[k] * x[depth-k] ) ) )
count( TreeMem[child].weight, depth, info, p, x, k );
traverse( child, p, info, depth+1, x, k );
x[depth] = -1;
}
}
}
else if( depth == degree-1 )
{
for( sym=0; sym<num_sym; ++sym ) {
const float64_t w = TreeMem[tree].child_weights[ sym ];
if( w != 0.0 ) {
x[depth] = sym;
info.substrs[depth+1] = y0 + sym;
info.y0 = (k==0) ? 0 : (y1+sym)*num_sym;
//ASSERT( info.y0 == ( info.substrs[depth+1]*num_sym - ( (depth<k) ? 0 : info.nofsKmers[k] * x[depth-k] ) ) )
count( w, depth, info, p, x, k );
x[depth] = -1;
}
}
}
//info.substrs[depth+1] = -1;
//info.y0 = temp;
}
template <class Trie>
void CTrie<Trie>::count(
const float64_t w, const int32_t depth, const struct TreeParseInfo info,
const int32_t p, int32_t* x, const int32_t k)
{
ASSERT( fabs(w) < 1e10 )
ASSERT( x[depth] >= 0 )
ASSERT( x[depth+1] < 0 )
if ( depth < k ) {
return;
}
//ASSERT( info.margFactors[ depth-k ] == pow( 0.25, depth-k ) )
const int32_t nofKmers = info.nofsKmers[k];
const float64_t margWeight = w * info.margFactors[ depth-k ];
const int32_t m_a = depth - k + 1;
const int32_t m_b = info.num_feat - p;
const int32_t m = ( m_a < m_b ) ? m_a : m_b;
// all proper k-substrings
const int32_t offset0 = nofKmers * p;
register int32_t i;
register int32_t offset;
offset = offset0;
for( i = 0; i < m; ++i ) {
const int32_t y = info.substrs[i+k+1];
info.C_k[ y + offset ] += margWeight;
offset += nofKmers;
}
if( depth > k ) {
// k-prefix
const int32_t offsR = info.substrs[k+1] + offset0;
info.R_k[offsR] += margWeight;
// k-suffix
if( p+depth-k < info.num_feat ) {
const int32_t offsL = info.substrs[depth+1] + nofKmers * (p+depth-k);
info.L_k[offsL] += margWeight;
}
}
// # N.x = substring represented by N
// # N.d = length of N.x
// # N.s = starting position of N.x
// # N.w = weight for feature represented by N
// if( N.d >= k )
// margContrib = w / 4^(N.d-k)
// for i = 1 to (N.d-k+1)
// y = N.x[i:(i+k-1)] # overlapped k-mer
// C_k[ N.s+i-1, y ] += margContrib
// end;
// if( N.d > k )
// L_k[ N.s+N.d-k, N.x[N.d-k+(1:k)] ] += margContrib # j-suffix of N.x
// R_k[ N.s, N.x[1:k] ] += margContrib # j-prefix of N.x
// end;
// end;
}
template <class Trie>
void CTrie<Trie>::add_to_trie(
int32_t i, int32_t seq_offset, int32_t * vec, float32_t alpha,
float64_t *weights, bool degree_times_position_weights)
{
int32_t tree = trees[i] ;
//ASSERT(seq_offset==0)
int32_t max_depth = 0 ;
float64_t* weights_column ;
if (degree_times_position_weights)
weights_column = &weights[(i+seq_offset)*degree] ;
else
weights_column = weights ;
if (weights_in_tree)
{
for (int32_t j=0; (j<degree) && (i+j<length); j++)
if (CMath::abs(weights_column[j]*alpha)>0)
max_depth = j+1 ;
}
else
// don't use the weights
max_depth=degree ;
for (int32_t j=0; (j<max_depth) && (i+j+seq_offset<length); j++)
{
TRIE_ASSERT((vec[i+j+seq_offset]>=0) && (vec[i+j+seq_offset]<4))
if ((j<degree-1) && (TreeMem[tree].children[vec[i+j+seq_offset]]!=NO_CHILD))
{
if (TreeMem[tree].children[vec[i+j+seq_offset]]<0)
{
// special treatment of the next nodes
TRIE_ASSERT(j >= degree-16)
// get the right element
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_floats)
int32_t node = - TreeMem[tree].children[vec[i+j+seq_offset]] ;
TRIE_ASSERT((node>=0) && (node<=TreeMemPtrMax))
TRIE_ASSERT_EVERYTHING(TreeMem[node].has_seq)
TRIE_ASSERT_EVERYTHING(!TreeMem[node].has_floats)
// check whether the same string is stored
int32_t mismatch_pos = -1 ;
{
int32_t k ;
for (k=0; (j+k<max_depth) && (i+j+seq_offset+k<length); k++)
{
TRIE_ASSERT((vec[i+j+seq_offset+k]>=0) && (vec[i+j+seq_offset+k]<4))
// ###
if ((TreeMem[node].seq[k]>=4) && (TreeMem[node].seq[k]!=TRIE_TERMINAL_CHARACTER))
fprintf(stderr, "+++i=%i j=%i seq[%i]=%i\n", i, j, k, TreeMem[node].seq[k]) ;
TRIE_ASSERT((TreeMem[node].seq[k]<4) || (TreeMem[node].seq[k]==TRIE_TERMINAL_CHARACTER))
TRIE_ASSERT(k<16)
if (TreeMem[node].seq[k]!=vec[i+j+seq_offset+k])
{
mismatch_pos=k ;
break ;
}
}
}
// what happens when the .seq sequence is longer than vec? should we branch???
if (mismatch_pos==-1)
// if so, then just increase the weight by alpha and stop
TreeMem[node].weight+=alpha ;
else
// otherwise
// 1. replace current node with new node
// 2. create new nodes until mismatching positon
// 2. add a branch with old string (old node) and the new string (new node)
{
// replace old node
int32_t last_node=tree ;
// create new nodes until mismatch
int32_t k ;
for (k=0; k<mismatch_pos; k++)
{
TRIE_ASSERT((vec[i+j+seq_offset+k]>=0) && (vec[i+j+seq_offset+k]<4))
TRIE_ASSERT(vec[i+j+seq_offset+k]==TreeMem[node].seq[k])
int32_t tmp=get_node();
TreeMem[last_node].children[vec[i+j+seq_offset+k]]=tmp ;
last_node=tmp ;
if (weights_in_tree)
TreeMem[last_node].weight = (TreeMem[node].weight+alpha)*weights_column[j+k] ;
else
TreeMem[last_node].weight = (TreeMem[node].weight+alpha) ;
TRIE_ASSERT(j+k!=degree-1)
}
if ((TreeMem[node].seq[mismatch_pos]>=4) && (TreeMem[node].seq[mismatch_pos]!=TRIE_TERMINAL_CHARACTER))
fprintf(stderr, "**i=%i j=%i seq[%i]=%i\n", i, j, k, TreeMem[node].seq[mismatch_pos]) ;
ASSERT((TreeMem[node].seq[mismatch_pos]<4) || (TreeMem[node].seq[mismatch_pos]==TRIE_TERMINAL_CHARACTER))
TRIE_ASSERT(vec[i+j+seq_offset+mismatch_pos]!=TreeMem[node].seq[mismatch_pos])
if (j+k==degree-1)
{
// init child weights with zero if after dropping out
// of the k<mismatch_pos loop we are one level below degree
// (keep this even after get_node() change!)
for (int32_t q=0; q<4; q++)
TreeMem[last_node].child_weights[q]=0.0 ;
if (weights_in_tree)
{
if (TreeMem[node].seq[mismatch_pos]<4) // i.e. !=TRIE_TERMINAL_CHARACTER
TreeMem[last_node].child_weights[TreeMem[node].seq[mismatch_pos]]+=TreeMem[node].weight*weights_column[degree-1] ;
TreeMem[last_node].child_weights[vec[i+j+seq_offset+k]] += alpha*weights_column[degree-1] ;
}
else
{
if (TreeMem[node].seq[mismatch_pos]<4) // i.e. !=TRIE_TERMINAL_CHARACTER
TreeMem[last_node].child_weights[TreeMem[node].seq[mismatch_pos]]=TreeMem[node].weight ;
TreeMem[last_node].child_weights[vec[i+j+seq_offset+k]] = alpha ;
}
#ifdef TRIE_CHECK_EVERYTHING
TreeMem[last_node].has_floats=true ;
#endif
}
else
{
// the branch for the existing string
if (TreeMem[node].seq[mismatch_pos]<4) // i.e. !=TRIE_TERMINAL_CHARACTER
{
TreeMem[last_node].children[TreeMem[node].seq[mismatch_pos]] = -node ;
// move string by mismatch_pos positions
for (int32_t q=0; q<16; q++)
{
if ((j+q+mismatch_pos<degree) && (i+j+seq_offset+q+mismatch_pos<length))
TreeMem[node].seq[q] = TreeMem[node].seq[q+mismatch_pos] ;
else
TreeMem[node].seq[q] = TRIE_TERMINAL_CHARACTER ;
}
#ifdef TRIE_CHECK_EVERYTHING
TreeMem[node].has_seq=true ;
#endif
}
// the new branch
TRIE_ASSERT((vec[i+j+seq_offset+mismatch_pos]>=0) && (vec[i+j+seq_offset+mismatch_pos]<4))
int32_t tmp = get_node() ;
TreeMem[last_node].children[vec[i+j+seq_offset+mismatch_pos]] = -tmp ;
last_node=tmp ;
TreeMem[last_node].weight = alpha ;
#ifdef TRIE_CHECK_EVERYTHING
TreeMem[last_node].has_seq = true ;
#endif
memset(TreeMem[last_node].seq, TRIE_TERMINAL_CHARACTER, 16) ;
for (int32_t q=0; (j+q+mismatch_pos<degree) && (i+j+seq_offset+q+mismatch_pos<length); q++)
TreeMem[last_node].seq[q] = vec[i+j+seq_offset+mismatch_pos+q] ;
}
}
break ;
}
else
{
tree=TreeMem[tree].children[vec[i+j+seq_offset]] ;
TRIE_ASSERT((tree>=0) && (tree<TreeMemPtrMax))
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
if (weights_in_tree)
TreeMem[tree].weight += alpha*weights_column[j];
else
TreeMem[tree].weight += alpha ;
}
}
else if (j==degree-1)
{
// special treatment of the last node
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
TRIE_ASSERT_EVERYTHING(TreeMem[tree].has_floats)
if (weights_in_tree)
TreeMem[tree].child_weights[vec[i+j+seq_offset]] += alpha*weights_column[j] ;
else
TreeMem[tree].child_weights[vec[i+j+seq_offset]] += alpha;
break;
}
else
{
bool use_seq = use_compact_terminal_nodes && (j>degree-16) ;
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_floats)
int32_t tmp = get_node((j==degree-2) && (!use_seq));
if (use_seq)
TreeMem[tree].children[vec[i+j+seq_offset]] = -tmp ;
else
TreeMem[tree].children[vec[i+j+seq_offset]] = tmp ;
tree=tmp ;
TRIE_ASSERT((tree>=0) && (tree<TreeMemPtrMax))
#ifdef TRIE_CHECK_EVERYTHING
TreeMem[tree].has_seq = use_seq ;
#endif
if (use_seq)
{
TreeMem[tree].weight = alpha ;
// important to have the terminal characters (see ###)
memset(TreeMem[tree].seq, TRIE_TERMINAL_CHARACTER, 16) ;
for (int32_t q=0; (j+q<degree) && (i+j+seq_offset+q<length); q++)
{
TRIE_ASSERT(q<16)
TreeMem[tree].seq[q]=vec[i+j+seq_offset+q] ;
}
break ;
}
else
{
if (weights_in_tree)
TreeMem[tree].weight = alpha*weights_column[j] ;
else
TreeMem[tree].weight = alpha ;
#ifdef TRIE_CHECK_EVERYTHING
if (j==degree-2)
TreeMem[tree].has_floats = true ;
#endif
}
}
}
}
template <class Trie>
float64_t CTrie<Trie>::compute_by_tree_helper(
int32_t* vec, int32_t len, int32_t seq_pos, int32_t tree_pos,
int32_t weight_pos, float64_t* weights,
bool degree_times_position_weights)
{
int32_t tree = trees[tree_pos] ;
if ((position_weights!=NULL) && (position_weights[weight_pos]==0))
return 0.0;
float64_t *weights_column=NULL ;
if (degree_times_position_weights)
weights_column=&weights[weight_pos*degree] ;
else // weights is a vector (1 x degree)
weights_column=weights ;
float64_t sum=0 ;
for (int32_t j=0; seq_pos+j < len; j++)
{
TRIE_ASSERT((vec[seq_pos+j]<4) && (vec[seq_pos+j]>=0))
if ((j<degree-1) && (TreeMem[tree].children[vec[seq_pos+j]]!=NO_CHILD))
{
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_floats)
if (TreeMem[tree].children[vec[seq_pos+j]]<0)
{
tree = - TreeMem[tree].children[vec[seq_pos+j]];
TRIE_ASSERT(tree>=0)
TRIE_ASSERT_EVERYTHING(TreeMem[tree].has_seq)
float64_t this_weight=0.0 ;
for (int32_t k=0; (j+k<degree) && (seq_pos+j+k<length); k++)
{
TRIE_ASSERT((vec[seq_pos+j+k]<4) && (vec[seq_pos+j+k]>=0))
if (TreeMem[tree].seq[k]!=vec[seq_pos+j+k])
break ;
this_weight += weights_column[j+k] ;
}
sum += TreeMem[tree].weight * this_weight ;
break ;
}
else
{
tree=TreeMem[tree].children[vec[seq_pos+j]];
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
if (weights_in_tree)
sum += TreeMem[tree].weight ;
else
sum += TreeMem[tree].weight * weights_column[j] ;
} ;
}
else
{
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
if (j==degree-1)
{
TRIE_ASSERT_EVERYTHING(TreeMem[tree].has_floats)
if (weights_in_tree)
sum += TreeMem[tree].child_weights[vec[seq_pos+j]] ;
else
sum += TreeMem[tree].child_weights[vec[seq_pos+j]] * weights_column[j] ;
}
else
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_floats)
break;
}
}
if (position_weights!=NULL)
return sum*position_weights[weight_pos] ;
else
return sum ;
}
template <class Trie>
void CTrie<Trie>::compute_by_tree_helper(
int32_t* vec, int32_t len, int32_t seq_pos, int32_t tree_pos,
int32_t weight_pos, float64_t* LevelContrib, float64_t factor,
int32_t mkl_stepsize, float64_t * weights,
bool degree_times_position_weights)
{
int32_t tree = trees[tree_pos] ;
if (factor==0)
return ;
if (position_weights!=NULL)
{
factor *= position_weights[weight_pos] ;
if (factor==0)
return ;
if (!degree_times_position_weights) // with position_weigths, weights is a vector (1 x degree)
{
for (int32_t j=0; seq_pos+j<len; j++)
{
if ((j<degree-1) && (TreeMem[tree].children[vec[seq_pos+j]]!=NO_CHILD))
{
if (TreeMem[tree].children[vec[seq_pos+j]]<0)
{
tree = -TreeMem[tree].children[vec[seq_pos+j]];
TRIE_ASSERT_EVERYTHING(TreeMem[tree].has_seq)
for (int32_t k=0; (j+k<degree) && (seq_pos+j+k<length); k++)
{
if (TreeMem[tree].seq[k]!=vec[seq_pos+j+k])
break ;
if (weights_in_tree)
LevelContrib[weight_pos/mkl_stepsize] += factor*TreeMem[tree].weight ;
else
LevelContrib[weight_pos/mkl_stepsize] += factor*TreeMem[tree].weight*weights[j+k] ;
}
break ;
}
else
{
tree=TreeMem[tree].children[vec[seq_pos+j]];
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
if (weights_in_tree)
LevelContrib[weight_pos/mkl_stepsize] += factor*TreeMem[tree].weight ;
else
LevelContrib[weight_pos/mkl_stepsize] += factor*TreeMem[tree].weight*weights[j] ;
}
}
else
{
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
if (j==degree-1)
{
if (weights_in_tree)
LevelContrib[weight_pos/mkl_stepsize] += factor*TreeMem[tree].child_weights[vec[seq_pos+j]] ;
else
LevelContrib[weight_pos/mkl_stepsize] += factor*TreeMem[tree].child_weights[vec[seq_pos+j]]*weights[j] ;
}
}
}
}
else // with position_weigths, weights is a matrix (len x degree)
{
for (int32_t j=0; seq_pos+j<len; j++)
{
if ((j<degree-1) && (TreeMem[tree].children[vec[seq_pos+j]]!=NO_CHILD))
{
if (TreeMem[tree].children[vec[seq_pos+j]]<0)
{
tree = -TreeMem[tree].children[vec[seq_pos+j]];
TRIE_ASSERT_EVERYTHING(TreeMem[tree].has_seq)
for (int32_t k=0; (j+k<degree) && (seq_pos+j+k<length); k++)
{
if (TreeMem[tree].seq[k]!=vec[seq_pos+j+k])
break ;
if (weights_in_tree)
LevelContrib[weight_pos/mkl_stepsize] += factor*TreeMem[tree].weight ;
else
LevelContrib[weight_pos/mkl_stepsize] += factor*TreeMem[tree].weight*weights[j+k+weight_pos*degree] ;
}
break ;
}
else
{
tree=TreeMem[tree].children[vec[seq_pos+j]];
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
if (weights_in_tree)
LevelContrib[weight_pos/mkl_stepsize] += factor*TreeMem[tree].weight ;
else
LevelContrib[weight_pos/mkl_stepsize] += factor*TreeMem[tree].weight*weights[j+weight_pos*degree] ;
}
}
else
{
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
if (j==degree-1)
{
if (weights_in_tree)
LevelContrib[weight_pos/mkl_stepsize] += factor*TreeMem[tree].child_weights[vec[seq_pos+j]] ;
else
LevelContrib[weight_pos/mkl_stepsize] += factor*TreeMem[tree].child_weights[vec[seq_pos+j]]*weights[j+weight_pos*degree] ;
}
break ;
}
}
}
}
else if (!degree_times_position_weights) // no position_weigths, weights is a vector (1 x degree)
{
for (int32_t j=0; seq_pos+j<len; j++)
{
if ((j<degree-1) && (TreeMem[tree].children[vec[seq_pos+j]]!=NO_CHILD))
{
if (TreeMem[tree].children[vec[seq_pos+j]]<0)
{
tree = -TreeMem[tree].children[vec[seq_pos+j]];
TRIE_ASSERT_EVERYTHING(TreeMem[tree].has_seq)
for (int32_t k=0; (j+k<degree) && (seq_pos+j+k<length); k++)
{
if (TreeMem[tree].seq[k]!=vec[seq_pos+j+k])
break ;
if (weights_in_tree)
LevelContrib[(j+k)/mkl_stepsize] += factor*TreeMem[tree].weight ;
else
LevelContrib[(j+k)/mkl_stepsize] += factor*TreeMem[tree].weight*weights[j+k] ;
}
break ;
}
else
{
tree=TreeMem[tree].children[vec[seq_pos+j]];
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
if (weights_in_tree)
LevelContrib[j/mkl_stepsize] += factor*TreeMem[tree].weight ;
else
LevelContrib[j/mkl_stepsize] += factor*TreeMem[tree].weight*weights[j] ;
}
}
else
{
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
if (j==degree-1)
{
if (weights_in_tree)
LevelContrib[j/mkl_stepsize] += factor*TreeMem[tree].child_weights[vec[seq_pos+j]] ;
else
LevelContrib[j/mkl_stepsize] += factor*TreeMem[tree].child_weights[vec[seq_pos+j]]*weights[j] ;
}
break ;
}
}
}
else // no position_weigths, weights is a matrix (len x degree)
{
/*if (!position_mask)
{
position_mask = SG_MALLOC(bool, len);
for (int32_t i=0; i<len; i++)
{
position_mask[i]=false ;
for (int32_t j=0; j<degree; j++)
if (weights[i*degree+j]!=0.0)
{
position_mask[i]=true ;
break ;
}
}
}
if (position_mask[weight_pos]==0)
return ;*/
for (int32_t j=0; seq_pos+j<len; j++)
{
if ((j<degree-1) && (TreeMem[tree].children[vec[seq_pos+j]]!=NO_CHILD))
{
if (TreeMem[tree].children[vec[seq_pos+j]]<0)
{
tree = -TreeMem[tree].children[vec[seq_pos+j]];
TRIE_ASSERT_EVERYTHING(TreeMem[tree].has_seq)
for (int32_t k=0; (j+k<degree) && (seq_pos+j+k<length); k++)
{
if (TreeMem[tree].seq[k]!=vec[seq_pos+j+k])
break ;
if (weights_in_tree)
LevelContrib[(j+k+degree*weight_pos)/mkl_stepsize] += factor*TreeMem[tree].weight ;
else
LevelContrib[(j+k+degree*weight_pos)/mkl_stepsize] += factor*TreeMem[tree].weight*weights[j+k+weight_pos*degree] ;
}
break ;
}
else
{
tree=TreeMem[tree].children[vec[seq_pos+j]];
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
if (weights_in_tree)
LevelContrib[(j+degree*weight_pos)/mkl_stepsize] += factor * TreeMem[tree].weight ;
else
LevelContrib[(j+degree*weight_pos)/mkl_stepsize] += factor * TreeMem[tree].weight * weights[j+weight_pos*degree] ;
}
}
else
{
TRIE_ASSERT_EVERYTHING(!TreeMem[tree].has_seq)
if (j==degree-1)
{
if (weights_in_tree)
LevelContrib[(j+degree*weight_pos)/mkl_stepsize] += factor * TreeMem[tree].child_weights[vec[seq_pos+j]] ;
else
LevelContrib[(j+degree*weight_pos)/mkl_stepsize] += factor * TreeMem[tree].child_weights[vec[seq_pos+j]] * weights[j+weight_pos*degree] ;
}
break ;
}
}
}
}
template <class Trie>
void CTrie<Trie>::fill_backtracking_table_recursion(
Trie* tree, int32_t depth, uint64_t seq, float64_t value,
DynArray<ConsensusEntry>* table, float64_t* weights)
{
float64_t w=1.0;
if (weights_in_tree || depth==0)
value+=tree->weight;
else
{
w=weights[degree-1];
value+=weights[depth-1]*tree->weight;
}
if (degree-1==depth)
{
for (int32_t sym=0; sym<4; sym++)
{
float64_t v=w*tree->child_weights[sym];
if (v!=0.0)
{
ConsensusEntry entry;
entry.bt=-1;
entry.score=value+v;
entry.string=seq | ((uint64_t) sym) << (2*(degree-depth-1));
table->append_element(entry);
}
}
}
else
{
for (int32_t sym=0; sym<4; sym++)
{
uint64_t str=seq | ((uint64_t) sym) << (2*(degree-depth-1));
if (tree->children[sym] != NO_CHILD)
fill_backtracking_table_recursion(&TreeMem[tree->children[sym]], depth+1, str, value, table, weights);
}
}
}
template <class Trie>
float64_t CTrie<Trie>::get_cumulative_score(
int32_t pos, uint64_t seq, int32_t deg, float64_t* weights)
{
float64_t result=0.0;
//SG_PRINT("pos:%i length:%i deg:%i seq:0x%0llx...\n", pos, length, deg, seq)
for (int32_t i=pos; i<pos+deg && i<length; i++)
{
//SG_PRINT("loop %d\n", i)
Trie* tree = &TreeMem[trees[i]];
for (int32_t d=0; d<deg-i+pos; d++)
{
//SG_PRINT("loop degree %d shit: %d\n", d, (2*(deg-1-d-i+pos)))
ASSERT(d-1<degree)
int32_t sym = (int32_t) (seq >> (2*(deg-1-d-i+pos)) & 3);
float64_t w=1.0;
if (!weights_in_tree)
w=weights[d];
ASSERT(tree->children[sym] != NO_CHILD)
tree=&TreeMem[tree->children[sym]];
result+=w*tree->weight;
}
}
//SG_PRINT("cum: %f\n", result)
return result;
}
template <class Trie>
void CTrie<Trie>::fill_backtracking_table(
int32_t pos, DynArray<ConsensusEntry>* prev,
DynArray<ConsensusEntry>* cur, bool cumulative, float64_t* weights)
{
ASSERT(pos>=0 && pos<length)
ASSERT(!use_compact_terminal_nodes)
Trie* t = &TreeMem[trees[pos]];
fill_backtracking_table_recursion(t, 0, (uint64_t) 0, 0.0, cur, weights);
if (cumulative)
{
int32_t num_cur=cur->get_num_elements();
for (int32_t i=0; i<num_cur; i++)
{
ConsensusEntry entry=cur->get_element(i);
entry.score+=get_cumulative_score(pos+1, entry.string, degree-1, weights);
cur->set_element(entry,i);
//SG_PRINT("cum: str:0%0llx sc:%f bt:%d\n",entry.string,entry.score,entry.bt)
}
}
//if previous tree exists find maximum scoring path
//for each element in cur and update bt table
if (prev)
{
int32_t num_cur=cur->get_num_elements();
int32_t num_prev=prev->get_num_elements();
for (int32_t i=0; i<num_cur; i++)
{
//uint64_t str_cur_old= cur->get_element(i).string;
uint64_t str_cur= cur->get_element(i).string >> 2;
//SG_PRINT("...cur:0x%0llx cur_noprfx:0x%0llx...\n", str_cur_old, str_cur)
int32_t bt=-1;
float64_t max_score=0.0;
for (int32_t j=0; j<num_prev; j++)
{
//uint64_t str_prev_old= prev->get_element(j).string;
uint64_t mask=
((((uint64_t)0)-1) ^ (((uint64_t) 3) << (2*(degree-1))));
uint64_t str_prev= mask & prev->get_element(j).string;
//SG_PRINT("...prev:0x%0llx prev_nosfx:0x%0llx mask:%0llx...\n", str_prev_old, str_prev,mask)
if (str_cur == str_prev)
{
float64_t sc=prev->get_element(j).score+cur->get_element(i).score;
if (bt==-1 || sc>max_score)
{
bt=j;
max_score=sc;
//SG_PRINT("new_max[%i,%i] = %f\n", j,i, max_score)
}
}
}
ASSERT(bt!=-1)
ConsensusEntry entry;
entry.bt=bt;
entry.score=max_score;
entry.string=cur->get_element(i).string;
cur->set_element(entry, i);
//SG_PRINT("entry[%d]: str:0%0llx sc:%f bt:%d\n",i, entry.string,entry.score,entry.bt)
}
}
}
}
#endif // _TRIE_H___
|