1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802
|
import itertools
import json
import os
import pathlib
import re
from os import remove
from tempfile import TemporaryDirectory, mktemp
from unittest import TestCase
import numpy
import pytest
from numpy import array, log2, nan
from numpy.testing import assert_allclose, assert_equal
from cogent3 import (
get_app,
get_code,
get_moltype,
load_aligned_seqs,
load_seq,
load_unaligned_seqs,
make_aligned_seqs,
make_seq,
make_unaligned_seqs,
open_,
)
from cogent3._version import __version__
from cogent3.core.alignment import (
Aligned,
Alignment,
ArrayAlignment,
DataError,
SequenceCollection,
_coerce_to_unaligned_seqs,
_construct_unaligned_seq,
_SequenceCollectionBase,
make_gap_filter,
)
from cogent3.core.alphabet import AlphabetError
from cogent3.core.annotation_db import GffAnnotationDb
from cogent3.core.moltype import AB, ASCII, BYTES, DNA, PROTEIN, RNA
from cogent3.core.sequence import ArraySequence, RnaSequence, Sequence, SeqView
from cogent3.maths.util import safe_p_log_p
from cogent3.parse.fasta import MinimalFastaParser
from cogent3.util.misc import get_object_provenance
class SequenceCollectionBaseTests(object):
"""base class for testing the SequenceCollection object.
Unlike Alignments, SequenceCollections can have sequences that are not equal
length. This module contains all the code that _doesn't_ depend on being
able to look at "ragged" SequenceCollections. It is intended that all
classes that inherit from SequenceCollection should have test classes that
inherit from this class, but that the SequenceCollection tests themselves
will additionally contain code to deal with SequenceCollections of unequal
length.
set self.Class in subclasses to generate the right constructor.
"""
Class = SequenceCollection
brca1_data = load_aligned_seqs("data/brca1.fasta").to_dict()
def setUp(self):
"""Define some standard SequenceCollection objects."""
self.one_seq = self.Class({"a": "AAAAA"})
self.ragged_padded = self.Class({"a": "AAAAAA", "b": "AAA---", "c": "AAAA--"})
self.identical = self.Class({"a": "AAAA", "b": "AAAA"})
self.gaps = self.Class({"a": "AAAAAAA", "b": "A--A-AA", "c": "AA-----"})
self.gaps_rna = self.Class(
{
"a": RnaSequence("AAAAAAA"),
"b": RnaSequence("A--A-AA"),
"c": RnaSequence("AA-----"),
}
)
self.unordered = self.Class({"a": "AAAAA", "b": "BBBBB"})
self.ordered1 = self.Class({"a": "AAAAA", "b": "BBBBB"}, names=["a", "b"])
self.ordered2 = self.Class({"a": "AAAAA", "b": "BBBBB"}, names=["b", "a"])
self.mixed = self.Class({"a": "ABCDE", "b": "LMNOP"})
self.end_gaps = self.Class(
{"a": "--A-BC-", "b": "-CB-A--", "c": "--D-EF-"}, names=["a", "b", "c"]
)
self.many = self.Class(
{
"a": RnaSequence("UCAGUCAGUU"),
"b": RnaSequence("UCCGUCAAUU"),
"c": RnaSequence("ACCAUCAGUC"),
"d": RnaSequence("UCAAUCGGUU"),
"e": RnaSequence("UUGGUUGGGU"),
"f": RnaSequence("CCGGGCGGCC"),
"g": RnaSequence("UCAACCGGAA"),
}
)
# Additional SequenceCollections for tests added 6/4/04 by Jeremy
# Widmann
self.sequences = self.Class(list(map(RnaSequence, ["UCAG", "UCAG", "UCAG"])))
# Additional SequenceCollection for tests added 1/30/06 by Cathy
# Lozupone
self.omitSeqsTemplate_aln = self.Class(
{
"s1": RnaSequence("UC-----CU---C"),
"s2": RnaSequence("UC------U---C"),
"s3": RnaSequence("UUCCUUCUU-UUC"),
"s4": RnaSequence("UU-UUUU-UUUUC"),
"s5": RnaSequence("-------------"),
}
)
self.a = ArrayAlignment(["AAA", "AAA"])
self.b = Alignment(["AAA", "AAA"])
self.c = SequenceCollection(["AAA", "AAA"])
def test_init_aln(self):
"""SequenceCollection should init from existing alignments"""
exp = self.Class(["AAA", "AAA"])
x = self.Class(self.a)
y = self.Class(self.b)
z = self.Class(self.c)
assert x == exp
assert z == exp
assert y == exp
test_init_aln.__doc__ = Class.__name__ + test_init_aln.__doc__
def test_names_attribute(self):
"""expected to be a list"""
seqs = self.Class({"a": b"AAAAA", "b": b"BBBBB"}, names=("a", "b"))
self.assertIsInstance(seqs.names, list)
def test_init_name_mapped(self):
"""SequenceCollection init should allow name mapping function"""
d = {"a": "AAAAA", "b": "BBBBB"}
def f(x):
return x.upper()
a = self.Class(d, label_to_name=f)
self.assertNotEqual(a, d)
self.assertNotEqual(sorted(a.named_seqs.items()), sorted(d.items()))
d_upper = {"A": "AAAAA", "B": "BBBBB"}
self.assertEqual(a, d_upper)
self.assertEqual(sorted(a.named_seqs.items()), sorted(d_upper.items()))
def test_init_seq(self):
"""SequenceCollection init from list of sequences should use indices as keys"""
seqs = ["AAAAA", "BBBBB", "CCCCC"]
a = self.Class(seqs)
self.assertEqual(len(a.named_seqs), 3)
self.assertEqual(a.named_seqs["seq_0"], "AAAAA")
self.assertEqual(a.named_seqs["seq_1"], "BBBBB")
self.assertEqual(a.named_seqs["seq_2"], "CCCCC")
self.assertEqual(a.names, ["seq_0", "seq_1", "seq_2"])
self.assertEqual(list(a.seqs), ["AAAAA", "BBBBB", "CCCCC"])
def test_init_pairs(self):
"""SequenceCollection init from list of (key,val) pairs should work correctly"""
seqs = [["x", "XXX"], ["b", "BBB"], ["c", "CCC"]]
a = self.Class(seqs)
self.assertEqual(len(a.named_seqs), 3)
self.assertEqual(a.named_seqs["x"], "XXX")
self.assertEqual(a.named_seqs["b"], "BBB")
self.assertEqual(a.named_seqs["c"], "CCC")
self.assertEqual(a.names, ["x", "b", "c"])
self.assertEqual(list(a.seqs), ["XXX", "BBB", "CCC"])
def test_init_ordered(self):
"""SequenceCollection should iterate over seqs correctly even if ordered"""
first = self.ordered1
sec = self.ordered2
un = self.unordered
self.assertEqual(first.names, ["a", "b"])
self.assertEqual(sec.names, ["b", "a"])
self.assertEqual(set(un.names), set(un.named_seqs.keys()))
first_list = list(first.seqs)
sec_list = list(sec.seqs)
un_list = list(un.seqs)
self.assertEqual(first_list, ["AAAAA", "BBBBB"])
self.assertEqual(sec_list, ["BBBBB", "AAAAA"])
# check that the unordered seq matches one of the lists
self.assertTrue((un_list == first_list) or (un_list == sec_list))
self.assertNotEqual(first_list, sec_list)
def test_init_ambig(self):
"""SequenceCollection should tolerate ambiguous chars"""
aln = self.Class(["AAA", "CCC"], moltype=DNA)
aln = self.Class(["ANS", "CWC"], moltype=DNA)
aln = self.Class(["A-A", "CC-"], moltype=DNA)
aln = self.Class(["A?A", "CC-"], moltype=DNA)
def test_seq_len_get(self):
"""SequenceCollection seq_len should return length of longest seq"""
self.assertEqual(self.one_seq.seq_len, 5)
self.assertEqual(self.identical.seq_len, 4)
self.assertEqual(self.gaps.seq_len, 7)
def test_Seqs(self):
"""SequenceCollection seqs property should return seqs in correct order."""
first = self.ordered1
sec = self.ordered2
un = self.unordered
first_list = list(first.seqs)
sec_list = list(sec.seqs)
un_list = list(un.seqs)
self.assertEqual(first_list, ["AAAAA", "BBBBB"])
self.assertEqual(sec_list, ["BBBBB", "AAAAA"])
# check that the unordered seq matches one of the lists
self.assertTrue((un_list == first_list) or (un_list == sec_list))
self.assertNotEqual(first_list, sec_list)
def test_iter_seqs(self):
"""SequenceCollection iter_seqs() method should support reordering of seqs"""
self.ragged_padded = self.Class(
self.ragged_padded.named_seqs, names=["a", "b", "c"]
)
seqs = list(self.ragged_padded.iter_seqs())
self.assertEqual(seqs, ["AAAAAA", "AAA---", "AAAA--"])
seqs = list(self.ragged_padded.iter_seqs(seq_order=["b", "a", "a"]))
self.assertEqual(seqs, ["AAA---", "AAAAAA", "AAAAAA"])
self.assertIs(seqs[1], seqs[2])
self.assertIs(seqs[0], self.ragged_padded.named_seqs["b"])
def test_Items(self):
"""SequenceCollection iter_selected should iterate over items in specified order."""
# should work if one row
self.assertEqual(list(self.one_seq.iter_selected()), ["A"] * 5)
# should take order into account
self.assertEqual(list(self.ordered1.iter_selected()), ["A"] * 5 + ["B"] * 5)
self.assertEqual(list(self.ordered2.iter_selected()), ["B"] * 5 + ["A"] * 5)
def test_take_seqs(self):
"""SequenceCollection take_seqs should return new SequenceCollection with selected seqs."""
a = self.ragged_padded.take_seqs(list("bc"))
self.assertTrue(isinstance(a, _SequenceCollectionBase))
self.assertEqual(a, {"b": "AAA---", "c": "AAAA--"})
# should be able to negate
a = self.ragged_padded.take_seqs(list("bc"), negate=True)
self.assertEqual(a, {"a": "AAAAAA"})
def test_take_seqs_str(self):
"""string arg to SequenceCollection take_seqs should work."""
a = self.ragged_padded.take_seqs("a", negate=True)
self.assertTrue(isinstance(a, _SequenceCollectionBase))
self.assertEqual(a, {"b": "AAA---", "c": "AAAA--"})
# should be able to negate
a = self.ragged_padded.take_seqs("a")
self.assertEqual(a, {"a": "AAAAAA"})
def test_take_seqs_info(self):
"""take_seqs should preserve info attribute"""
orig = self.Class(
data={"a": "CCCCCC", "b": "AAA---", "c": "AAAA--"}, info={"key": "value"}
)
subset = orig.take_seqs(list("ab"))
self.assertEqual(set(subset.info), set(orig.info))
def test_take_seqs_moltype(self):
"""take_seqs should preserve the MolType"""
orig = self.Class(
data={"a": "CCCCCC", "b": "AAA---", "c": "AAAA--"}, moltype=DNA
)
subset = orig.take_seqs(list("ab"))
self.assertEqual(set(subset.moltype), set(orig.moltype))
def test_get_seq_indices(self):
"""SequenceCollection get_seq_indices should return names of seqs where f(row) is True"""
srp = self.ragged_padded
def is_long(x):
return len(x) > 10
def is_med(x):
return len(str(x).replace("-", "")) > 3 # strips gaps
def is_any(x):
return len(x) > 0
self.assertEqual(srp.get_seq_indices(is_long), [])
srp.names = "cba"
self.assertEqual(srp.get_seq_indices(is_med), ["c", "a"])
srp.names = "bac"
self.assertEqual(srp.get_seq_indices(is_med), ["a", "c"])
self.assertEqual(srp.get_seq_indices(is_any), ["b", "a", "c"])
# should be able to negate
self.assertEqual(srp.get_seq_indices(is_med, negate=True), ["b"])
self.assertEqual(srp.get_seq_indices(is_any, negate=True), [])
def test_take_seqs_if(self):
"""SequenceCollection take_seqs_if should return seqs where f(row) is True"""
def is_long(x):
return len(x) > 10
def is_med(x):
return len(str(x).replace("-", "")) > 3
def is_any(x):
return len(x) > 0
srp = self.ragged_padded
self.assertEqual(srp.take_seqs_if(is_long), {})
srp.names = "cba"
self.assertEqual(srp.take_seqs_if(is_med), {"c": "AAAA--", "a": "AAAAAA"})
srp.names = list(srp.named_seqs.keys())
self.assertEqual(srp.take_seqs_if(is_med), {"c": "AAAA--", "a": "AAAAAA"})
self.assertEqual(srp.take_seqs_if(is_any), srp)
self.assertTrue(isinstance(srp.take_seqs_if(is_med), _SequenceCollectionBase))
# should be able to negate
self.assertEqual(srp.take_seqs_if(is_med, negate=True), {"b": "AAA---"})
def test_get_identical_sets(self):
"""correctly identify sets of identical sequences"""
from warnings import catch_warnings, filterwarnings
# for DNA
data = {
"a": "ACGG",
"b": "ACGG", # strict identical
"c": "ACGN", # non-strict matches above
"d": "ACGT",
"e": "ACGT",
"k": "ACGT", # strict identical
"f": "RAAA",
"g": "YAAA",
} # non-strict identical
seqs = self.Class(data=data, moltype=DNA)
got = seqs.get_identical_sets(mask_degen=False) # a strict comparison
# convert to frozenset, so we can do a comparison robust to set order
got = frozenset(frozenset(s) for s in got)
expect = [{"a", "b"}, {"d", "e", "k"}]
expect = frozenset(frozenset(s) for s in expect)
self.assertEqual(got, expect)
got = seqs.get_identical_sets(mask_degen=True)
got = frozenset(frozenset(s) for s in got)
expect = [{"a", "b", "c"}, {"d", "e", "k"}, {"f", "g"}]
expect = frozenset(frozenset(s) for s in expect)
self.assertEqual(got, expect)
# for PROTEIN
data = {
"a": "ACGT",
"b": "ACGT", # strict identical
"c": "ACGX", # non-strict matches above
"d": "TTTT",
"e": "TTTT",
"k": "TTTT", # strict identical
"f": "BAAA",
"g": "ZAAA",
} # non-strict identical
seqs = self.Class(data=data, moltype=PROTEIN)
got = seqs.get_identical_sets(mask_degen=False) # a strict comparison
# convert to frozenset, so we can do a comparison robust to set order
got = frozenset(frozenset(s) for s in got)
expect = [{"a", "b"}, {"d", "e", "k"}]
expect = frozenset(frozenset(s) for s in expect)
self.assertEqual(got, expect)
got = seqs.get_identical_sets(mask_degen=True)
got = frozenset(frozenset(s) for s in got)
expect = [{"a", "b", "c"}, {"d", "e", "k"}, {"f", "g"}]
expect = frozenset(frozenset(s) for s in expect)
self.assertEqual(got, expect)
# if the moltype has no degen characters, just return for mask_degen
seqs = self.Class(data=data, moltype=ASCII)
got = seqs.get_identical_sets(mask_degen=False)
# convert to frozenset, so we can do a comparison robust to set order
got = frozenset(frozenset(s) for s in got)
expect = [{"a", "b"}, {"d", "e", "k"}]
expect = frozenset(frozenset(s) for s in expect)
self.assertEqual(got, expect)
with catch_warnings():
filterwarnings("ignore", category=UserWarning)
got = seqs.get_identical_sets(mask_degen=True)
got = frozenset(frozenset(s) for s in got)
self.assertEqual(got, expect)
def test_is_ragged(self):
"""SequenceCollection is_ragged should return true if ragged alignment"""
assert not self.identical.is_ragged()
assert not self.gaps.is_ragged()
def test_to_phylip(self):
"""SequenceCollection should return PHYLIP string format correctly"""
align_norm = self.Class(
[
"ACDEFGHIKLMNPQRSTUVWY-",
"ACDEFGHIKLMNPQRSUUVWF-",
"ACDEFGHIKLMNPERSKUVWC-",
"ACNEFGHIKLMNPQRS-UVWP-",
]
)
self.assertEqual(
align_norm.to_phylip(),
"""4 22\nseq_0 ACDEFGHIKLMNPQRSTUVWY-\nseq_1 ACDEFGHIKLMNPQRSUUVWF-\nseq_2 ACDEFGHIKLMNPERSKUVWC-\nseq_3 ACNEFGHIKLMNPQRS-UVWP-\n""",
)
def test_to_fasta(self):
"""SequenceCollection should return correct FASTA string"""
aln1 = self.Class(["AAA", "CCC"])
self.assertEqual(aln1.to_fasta(), ">seq_0\nAAA\n>seq_1\nCCC\n")
self.assertEqual(aln1.to_fasta(block_size=2), ">seq_0\nAA\nA\n>seq_1\nCC\nC\n")
aln2 = self.Class(["GCATGCAT", "TCAGACGT"])
self.assertEqual(aln2.to_fasta(), ">seq_0\nGCATGCAT\n>seq_1\nTCAGACGT\n")
self.assertEqual(
aln2.to_fasta(block_size=4), ">seq_0\nGCAT\nGCAT\n>seq_1\nTCAG\nACGT\n"
)
self.assertEqual(
aln2.to_fasta(block_size=3), ">seq_0\nGCA\nTGC\nAT\n>seq_1\nTCA\nGAC\nGT\n"
)
def test_to_nexus(self):
"""SequenceCollection should return correct Nexus string format"""
align_norm = self.Class(
[
"ACDEFGHIKLMNPQRSTUVWY-",
"ACDEFGHIKLMNPQRSUUVWF-",
"ACDEFGHIKLMNPERSKUVWC-",
"ACNEFGHIKLMNPQRS-UVWP-",
]
)
expect = (
"#NEXUS\n\nbegin data;\n"
" dimensions ntax=4 nchar=22;\n"
" format datatype=protein interleave=yes missing=? gap=-;\n"
" matrix\n"
" seq_0 ACDEFGHIKLMNPQRSTUVWY-\n"
" seq_1 ACDEFGHIKLMNPQRSUUVWF-\n"
" seq_2 ACDEFGHIKLMNPERSKUVWC-\n"
" seq_3 ACNEFGHIKLMNPQRS-UVWP-\n\n ;\nend;"
)
got = align_norm.to_nexus("protein")
self.assertEqual(got, expect)
def test_num_seqs(self):
"""SequenceCollection.num_seqs should count seqs."""
aln = self.Class({"seq1": "ACGU", "seq2": "CGUA", "seq3": "CCGU"})
self.assertEqual(aln.num_seqs, 3)
def test_add_info(self):
"""__add__ should preserve info attribute"""
align1 = self.Class(
{"a": "AAAA", "b": "TTTT", "c": "CCCC"}, info={"key": "foo"}
)
align2 = self.Class(
{"a": "GGGG", "b": "----", "c": "NNNN"}, info={"key": "bar"}
)
align = align1 + align2
self.assertEqual(align.info["key"], "foo")
def test_add_seqs_info(self):
"""add_seqs should preserve info attribute"""
data = [("name1", "AAA"), ("name2", "AAA"), ("name3", "AAA"), ("name4", "AAA")]
data2 = [("name5", "BBB"), ("name6", "CCC")]
aln = self.Class(data, info={"key": "foo"})
aln2 = self.Class(data2, info={"key": "bar"})
out_aln = aln.add_seqs(aln2)
self.assertEqual(out_aln.info["key"], "foo")
def test_write(self):
"""SequenceCollection.write should write in correct format"""
aln = self.Class([("a", "AAAA"), ("b", "TTTT"), ("c", "CCCC")])
fn = mktemp(suffix=".fasta")
aln.write(fn)
with open(fn, newline=None) as infile:
result = infile.read()
self.assertEqual(result, ">a\nAAAA\n>b\nTTTT\n>c\nCCCC\n")
remove(fn)
def test_len(self):
"""len(SequenceCollection) returns length of longest sequence"""
aln = self.Class([("a", "AAAA"), ("b", "TTTT"), ("c", "CCCC")])
self.assertEqual(len(aln), 4)
def test_get_seq(self):
"""SequenceCollection.get_seq should return specified seq"""
aln = self.Class({"seq1": "GATTTT", "seq2": "GATC??"})
self.assertEqual(aln.get_seq("seq1"), "GATTTT")
self.assertRaises(KeyError, aln.get_seq, "seqx")
def test_to_dict(self):
"""SequenceCollection.to_dict should return dict of strings (not obj)"""
aln = self.Class({"seq1": "GATTTT", "seq2": "GATC??"})
self.assertEqual(aln.to_dict(), {"seq1": "GATTTT", "seq2": "GATC??"})
for i in list(aln.to_dict().values()):
assert isinstance(i, str)
def test_get_ambiguous_positions(self):
"""SequenceCollection.get_ambiguous_positions should return pos"""
aln = self.Class({"s1": "ATGRY?", "s2": "T-AG??"}, moltype=DNA)
self.assertEqual(
aln.get_ambiguous_positions(),
{"s2": {4: "?", 5: "?"}, "s1": {3: "R", 4: "Y", 5: "?"}},
)
def test_degap(self):
"""SequenceCollection.degap should strip gaps from each seq"""
aln = self.Class({"s1": "ATGRY?", "s2": "T-AG??"}, moltype=DNA)
self.assertEqual(aln.degap(), {"s1": "ATGRY", "s2": "TAG"})
def test_degap_info(self):
""".degap should preserve info attributes"""
aln = self.Class({"s1": "ATGRY?", "s2": "T-AG??"}, moltype=DNA)
aln.info.path = "blah"
got = aln.degap()
self.assertEqual(got.info.path, "blah")
def test_with_modified_termini(self):
"""SequenceCollection.with_modified_termini should code trailing gaps as ?"""
aln = self.Class({"s1": "AATGR--", "s2": "-T-AG?-"}, moltype=DNA)
self.assertEqual(
aln.with_modified_termini(), {"s1": "AATGR??", "s2": "?T-AG??"}
)
def test_make_gap_filter(self):
"""make_gap_filter returns f(seq) -> True if aligned ok w/ query"""
s1 = RnaSequence("UC-----CU---C")
s3 = RnaSequence("UUCCUUCUU-UUC")
s4 = RnaSequence("UU-UUUU-UUUUC")
# check that the behavior is ok for gap runs
f1 = make_gap_filter(s1, 0.9, 5)
f3 = make_gap_filter(s3, 0.9, 5)
# Should return False since s1 has gap run >= 5 with respect to s3
self.assertEqual(f3(s1), False)
# Should return False since s3 has an insertion run >= 5 to s1
self.assertEqual(f1(s3), False)
# Should retun True since s4 does not have a long enough gap or ins run
self.assertEqual(f3(s4), True)
f3 = make_gap_filter(s3, 0.9, 6)
self.assertEqual(f3(s1), True)
# Check that behavior is ok for gap_fractions
f1 = make_gap_filter(s1, 0.5, 6)
f3 = make_gap_filter(s3, 0.5, 6)
# Should return False since 0.53% of positions are diff for gaps
self.assertEqual(f3(s1), False)
self.assertEqual(f1(s3), False)
self.assertEqual(f3(s4), True)
def test_omit_gap_seqs(self):
"""SequenceCollection omit_gap_seqs should return alignment w/o seqs with gaps"""
# check default params
self.assertEqual(self.gaps.omit_gap_seqs(), self.gaps.omit_gap_seqs(0))
# check for boundary effects
self.assertEqual(self.gaps.omit_gap_seqs(-1), {})
self.assertEqual(self.gaps.omit_gap_seqs(0), {"a": "AAAAAAA"})
self.assertEqual(self.gaps.omit_gap_seqs(0.1), {"a": "AAAAAAA"})
self.assertEqual(self.gaps.omit_gap_seqs(3.0 / 7 - 0.01), {"a": "AAAAAAA"})
self.assertEqual(
self.gaps.omit_gap_seqs(3.0 / 7), {"a": "AAAAAAA", "b": "A--A-AA"}
)
self.assertEqual(
self.gaps.omit_gap_seqs(3.0 / 7 + 0.01), {"a": "AAAAAAA", "b": "A--A-AA"}
)
self.assertEqual(
self.gaps.omit_gap_seqs(5.0 / 7 - 0.01), {"a": "AAAAAAA", "b": "A--A-AA"}
)
self.assertEqual(self.gaps.omit_gap_seqs(5.0 / 7 + 0.01), self.gaps)
self.assertEqual(self.gaps.omit_gap_seqs(0.99), self.gaps)
# check new object creation
self.assertIsNot(self.gaps.omit_gap_seqs(0.99), self.gaps)
self.assertTrue(
isinstance(self.gaps.omit_gap_seqs(3.0 / 7), _SequenceCollectionBase)
)
# repeat tests for object that supplies its own gaps
self.assertEqual(self.gaps_rna.omit_gap_seqs(-1), {})
self.assertEqual(self.gaps_rna.omit_gap_seqs(0), {"a": "AAAAAAA"})
self.assertEqual(self.gaps_rna.omit_gap_seqs(0.1), {"a": "AAAAAAA"})
self.assertEqual(self.gaps_rna.omit_gap_seqs(3.0 / 7 - 0.01), {"a": "AAAAAAA"})
self.assertEqual(
self.gaps_rna.omit_gap_seqs(3.0 / 7), {"a": "AAAAAAA", "b": "A--A-AA"}
)
self.assertEqual(
self.gaps_rna.omit_gap_seqs(3.0 / 7 + 0.01),
{"a": "AAAAAAA", "b": "A--A-AA"},
)
self.assertEqual(
self.gaps_rna.omit_gap_seqs(5.0 / 7 - 0.01),
{"a": "AAAAAAA", "b": "A--A-AA"},
)
self.assertEqual(self.gaps_rna.omit_gap_seqs(5.0 / 7 + 0.01), self.gaps_rna)
self.assertEqual(self.gaps_rna.omit_gap_seqs(0.99), self.gaps_rna)
self.assertIsNot(self.gaps_rna.omit_gap_seqs(0.99), self.gaps_rna)
self.assertTrue(
isinstance(self.gaps_rna.omit_gap_seqs(3.0 / 7), _SequenceCollectionBase)
)
def test_omit_gap_runs(self):
"""SequenceCollection omit_gap_runs should return alignment w/o runs of gaps"""
# negative value will still let through ungapped sequences
self.assertEqual(self.gaps.omit_gap_runs(-5), {"a": "AAAAAAA"})
# test edge effects
self.assertEqual(self.gaps.omit_gap_runs(0), {"a": "AAAAAAA"})
self.assertEqual(self.gaps.omit_gap_runs(1), {"a": "AAAAAAA"})
self.assertEqual(self.gaps.omit_gap_runs(2), {"a": "AAAAAAA", "b": "A--A-AA"})
self.assertEqual(self.gaps.omit_gap_runs(3), {"a": "AAAAAAA", "b": "A--A-AA"})
self.assertEqual(self.gaps.omit_gap_runs(4), {"a": "AAAAAAA", "b": "A--A-AA"})
self.assertEqual(self.gaps.omit_gap_runs(5), self.gaps)
self.assertEqual(self.gaps.omit_gap_runs(6), self.gaps)
self.assertEqual(self.gaps.omit_gap_runs(1000), self.gaps)
# test new object creation
self.assertIsNot(self.gaps.omit_gap_runs(6), self.gaps)
self.assertTrue(isinstance(self.gaps.omit_gap_runs(6), _SequenceCollectionBase))
def test_consistent_gap_degen_handling(self):
"""gap degen character should be treated consistently"""
# the degen character '?' can be a gap, so when we strip gaps it should
# be gone too
raw_seq = "---??-??TC-GGCG-GCA-G-GC-?-C-TAN-GCGC-CCTC-AGGA?-???-??--"
raw_ungapped = re.sub("[-?]", "", raw_seq)
re.sub("[N?]+", "", raw_seq)
dna = DNA.make_seq(raw_seq)
aln = self.Class(data=[("a", dna), ("b", dna)])
expect = self.Class(data=[("a", raw_ungapped), ("b", raw_ungapped)]).to_fasta()
self.assertEqual(aln.degap().to_fasta(), expect)
seqs = self.Class(data=[("a", dna), ("b", dna)])
self.assertEqual(seqs.degap().to_fasta(), expect)
def test_pad_seqs(self):
"""SequenceCollection pad_seqs should work on alignment."""
# pad to max length
padded1 = self.ragged_padded.pad_seqs()
seqs1 = list(padded1.iter_seqs(seq_order=["a", "b", "c"]))
self.assertEqual(list(map(str, seqs1)), ["AAAAAA", "AAA---", "AAAA--"])
# pad to alternate length
padded1 = self.ragged_padded.pad_seqs(pad_length=10)
seqs1 = list(padded1.iter_seqs(seq_order=["a", "b", "c"]))
self.assertEqual(
list(map(str, seqs1)), ["AAAAAA----", "AAA-------", "AAAA------"]
)
# assertRaises error when pad_length is less than max seq length
self.assertRaises(ValueError, self.ragged_padded.pad_seqs, 5)
def test_to_moltype_info(self):
"""correctly convert to specified moltype"""
data = {"seq1": "ACGTACGTA", "seq2": "ACCGAA---", "seq3": "ACGTACGTT"}
seqs = self.Class(data=data, info={"key": "value"})
dna = seqs.to_moltype(DNA)
self.assertEqual(dna.info["key"], "value")
def test_get_lengths(self):
"""get_lengths handles motif length, allow_gaps etc.."""
data = {"a": "AAAA??????", "b": "CCCGGG--NN"}
coll = self.Class(data=data, moltype=DNA)
got = coll.get_lengths()
expect = {"a": 4, "b": 6}
self.assertEqual(got, expect)
got = coll.get_lengths(include_ambiguity=True)
expect = {"a": 4, "b": 8} # note ? is excluded as it could be a gap
self.assertEqual(got, expect)
got = coll.get_lengths(include_ambiguity=True, allow_gap=True)
# note ? is excluded as it could be a gap
expect = {"a": 10, "b": 10}
self.assertEqual(got, expect)
def test_strand_symmetry(self):
"""exercising strand symmetry test"""
data = {"seq1": "ACGTACGTA", "seq2": "ACCGAA---", "seq3": "ACGTACGTT"}
seqs = self.Class(data, moltype=DNA)
result = seqs.strand_symmetry()
assert_allclose(result["seq1"].observed.array, [[3, 2], [2, 2]])
assert_allclose(result["seq2"].observed.array, [[3, 0], [2, 1]])
def test_rename_seqs(self):
"""successfully rename sequences"""
data = {"seq1": "ACGTACGTA", "seq2": "ACCGAA---", "seq3": "ACGTACGTT"}
seqs = self.Class(data, moltype=DNA)
new = seqs.rename_seqs(lambda x: x.upper())
expect = {n.upper() for n in data}
self.assertEqual(set(new.names), expect)
# renamed contains the name map as info attribute
self.assertEqual(new.info.name_map, {k.upper(): k for k in data})
# if Alignment class, make sure data attribute does not have gaps
if self.Class == Alignment:
for seq in new.seqs:
self.assertFalse("-" in str(seq.data))
def test_apply_pssm(self):
"""should successfully produce pssm scores"""
from cogent3.parse import jaspar
_, pwm = jaspar.read("data/sample.jaspar")
data = {
"ENSMUSG00000056468": "GCCAGGGGGGAAAGGGAGAA",
"ENSMUSG00000039616": "GCCCTTCAAATTTGGTTTCT",
"ENSMUSG00000024091": "TTTCCAGGGCAGACAAGACG",
"ENSMUSG00000024056": "ACAATAATGCCGAGAGCCAG",
"ENSMUSG00000054321": "TATGAAAATTTTTGCCAGGC",
"ENSMUSG00000052469": "CCTGTTTGCCTTTAAATATT",
"ENSMUSG00000024261": "CAGACAAGAAACCAGCAACA",
"ENSMUSG00000052031": "AGCGAGTATNCACGCACAGA",
"ENSMUSG00000067872": "ACACAGCTCTGACAACTCAT",
"ENSMUSG00000023892": "GTAACATCAGTACAGCACAG",
}
seqs = self.Class(data=data, moltype=DNA)
scores = seqs.apply_pssm(path="data/sample.jaspar", show_progress=False)
self.assertEqual(scores.shape, (len(data), len(seqs) - pwm.shape[0] + 1))
scores = seqs.apply_pssm(pssm=pwm.to_pssm(), show_progress=False)
self.assertEqual(scores.shape, (len(data), len(seqs) - pwm.shape[0] + 1))
# using the names argument works to return scores in the correct order
seqs = self.Class(
data={"ENSMUSG00000056468": "GCCAGGGGGGAAAGGGAGAA"}, moltype=DNA
)
expect = []
expect.extend(seqs.apply_pssm(pssm=pwm.to_pssm(), show_progress=False))
seqs = self.Class(
data={"ENSMUSG00000039616": "GCCCTTCAAATTTGGTTTCT"}, moltype=DNA
)
expect.extend(seqs.apply_pssm(pssm=pwm.to_pssm(), show_progress=False))
expect = numpy.array(expect)
seqs = self.Class(
data={
"ENSMUSG00000056468": "GCCAGGGGGGAAAGGGAGAA",
"ENSMUSG00000039616": "GCCCTTCAAATTTGGTTTCT",
},
moltype=DNA,
)
got = seqs.apply_pssm(
pssm=pwm.to_pssm(), show_progress=False, names="ENSMUSG00000056468"
)
assert_allclose(got, expect[:1])
got = seqs.apply_pssm(
pssm=pwm.to_pssm(), show_progress=False, names=["ENSMUSG00000039616"]
)
assert_allclose(got, expect[1:])
got = seqs.apply_pssm(
pssm=pwm.to_pssm(),
show_progress=False,
names=["ENSMUSG00000056468", "ENSMUSG00000039616"],
)
assert_allclose(got, expect)
got = seqs.apply_pssm(
pssm=pwm.to_pssm(),
show_progress=False,
names=["ENSMUSG00000039616", "ENSMUSG00000056468"],
)
assert_allclose(got, expect[::-1])
def test_set_repr_policy_no_input(self):
"""repr_policy should remain unchanged"""
seqs = self.Class({"a": "AAAAA"})
seqs.set_repr_policy(num_seqs=None, num_pos=None)
self.assertEqual(
seqs._repr_policy,
dict(num_seqs=10, num_pos=60, ref_name="longest", wrap=60),
)
def test_set_repr_policy_invalid_input(self):
"""repr_policy should remain unchanged"""
seqs = self.Class({"a": "AAAAA"})
invalid_args = (
dict(num_seqs="foo", err=TypeError),
dict(num_pos=4.2, err=TypeError),
dict(ref_name="blah", err=ValueError),
dict(wrap=3.1, err=TypeError),
)
for arg in invalid_args:
err = arg.pop("err")
with self.assertRaises(err):
seqs.set_repr_policy(**arg)
self.assertEqual(
seqs._repr_policy,
dict(num_seqs=10, num_pos=60, ref_name="longest", wrap=60),
)
def test_set_wrap_affects_repr_html(self):
"""the wrap argument affects the number of columns"""
# indirectly tested via counting number of occurrences of 'class="label"'
seqs = self.Class({"a": "AAAAA", "b": "AAA--"})
orig = seqs._repr_html_()
seqs.set_repr_policy(wrap=3) # break alignment into 2
got = seqs._repr_html_()
token = 'class="label"'
self.assertEqual(got.count(token), 2 * orig.count(token))
# using environment variable
env_name = "COGENT3_ALIGNMENT_REPR_POLICY"
os.environ[env_name] = "wrap=2"
seqs = self.Class({"a": "AAAAA", "b": "AAA--"})
got = seqs._repr_html_()
os.environ.pop(env_name, None)
self.assertEqual(got.count(token), 3 * orig.count(token))
def test_get_seq_entropy(self):
"""get_seq_entropy should get entropy of each seq"""
a = self.Class(dict(a="ACCC", b="AGTA"), moltype=DNA)
entropy = a.entropy_per_seq()
e = 0.81127812445913283 # sum(p log_2 p) for p = 0.25, 0.75
assert_allclose(entropy, array([e, 1.5]))
def test_write_to_json(self):
# test writing to json file
aln = self.Class([("a", "AAAA"), ("b", "TTTT"), ("c", "CCCC")])
with TemporaryDirectory(".") as dirname:
path = str(pathlib.Path(dirname) / "sample.json")
aln.write(path)
with open_(path) as fn:
got = json.loads(fn.read())
self.assertEqual(got, aln.to_rich_dict())
class SequenceCollectionTests(SequenceCollectionBaseTests, TestCase):
"""Tests of the SequenceCollection object. Includes ragged collection tests.
Should not test alignment-specific features.
"""
def setUp(self):
"""Adds self.ragged for ragged collection tests."""
self.ragged = SequenceCollection({"a": "AAAAAA", "b": "AAA", "c": "AAAA"})
super(SequenceCollectionTests, self).setUp()
def test_seq_len_get_ragged(self):
"""SequenceCollection seq_len get should work for ragged seqs"""
self.assertEqual(self.ragged.seq_len, 6)
def test_is_ragged_ragged(self):
"""SequenceCollection is_ragged should return True if ragged"""
self.assertTrue(self.ragged.is_ragged())
def test_Seqs_ragged(self):
"""SequenceCollection seqs should work on ragged alignment"""
self.ragged.names = "bac"
self.assertEqual(list(self.ragged.seqs), ["AAA", "AAAAAA", "AAAA"])
def test_iter_seqs_ragged(self):
"""SequenceCollection iter_seqs() method should support reordering of seqs"""
self.ragged.names = ["a", "b", "c"]
seqs = list(self.ragged.iter_seqs())
self.assertEqual(seqs, ["AAAAAA", "AAA", "AAAA"])
seqs = list(self.ragged.iter_seqs(seq_order=["b", "a", "a"]))
self.assertEqual(seqs, ["AAA", "AAAAAA", "AAAAAA"])
self.assertIs(seqs[1], seqs[2])
self.assertIs(seqs[0], self.ragged.named_seqs["b"])
def test_toPHYLIP_ragged(self):
"""SequenceCollection should refuse to convert ragged seqs to phylip"""
align_rag = self.Class(
[
"ACDEFGHIKLMNPQRSTUVWY-",
"ACDEFGHIKLMNPQRSUUVWF-",
"ACDEFGHIKLMNPERSKUVWC-",
"ACNEFGHIKLMNUVWP-",
]
)
# no longer applicable in new implementation
with self.assertRaises(ValueError):
align_rag.to_phylip()
def test_pad_seqs_ragged(self):
"""SequenceCollection pad_seqs should work on ragged alignment."""
# pad to max length
padded1 = self.ragged.pad_seqs()
seqs1 = list(padded1.iter_seqs(seq_order=["a", "b", "c"]))
self.assertEqual(list(map(str, seqs1)), ["AAAAAA", "AAA---", "AAAA--"])
# pad to alternate length
padded1 = self.ragged.pad_seqs(pad_length=10)
seqs1 = list(padded1.iter_seqs(seq_order=["a", "b", "c"]))
self.assertEqual(
list(map(str, seqs1)), ["AAAAAA----", "AAA-------", "AAAA------"]
)
# assertRaises error when pad_length is less than max seq length
self.assertRaises(ValueError, self.ragged.pad_seqs, 5)
def test_info_source(self):
"""info.source exists if load seqs given a filename"""
path = pathlib.Path("data/brca1.fasta")
seqs = load_unaligned_seqs(path)
self.assertEqual(seqs.info.source, str(path))
def test_apply_pssm2(self):
"""apply_pssm fail if ragged sequences"""
data = {
"ENSMUSG00000056468": "GCCAGGGGGGAAAGGGAGAA",
"ENSMUSG00000039616": "GCCCTTCAAATTT",
}
seqs = self.Class(data=data, moltype=DNA)
with self.assertRaises(AssertionError):
_ = seqs.apply_pssm(path="data/sample.jaspar", show_progress=False)
def test_construction(self):
"""correctly construct from list of sequences of length 2"""
seq1 = make_seq("AC", name="seq1")
seq2 = make_seq("AC", name="seq2")
coll = SequenceCollection(data=[seq1, seq2])
def _make_filter_func(aln):
array_align = type(aln) == ArrayAlignment
if array_align:
gap = aln.alphabet.with_gap_motif().to_indices("-")[0]
else:
gap = "-"
def func_str(x):
return gap not in "".join([str(s) for s in x])
def func_arr(x):
return (x != gap).all()
return func_arr if array_align else func_str
class AlignmentBaseTests(SequenceCollectionBaseTests):
"""Tests of basic Alignment functionality. All Alignments should pass these.
Note that this is not a TestCase: need to subclass to test each specific
type of Alignment. Override self.Constructor with your alignment class
as a constructor.
"""
def test_alignment_quality(self):
"""check alignment method correctly invokes underlying app"""
aln = self.Class(["AAAC", "ACGC", "AGCC", "A-TC"], moltype="dna")
got = aln.alignment_quality(equifreq_mprobs=False)
expect = (
2 * log2(1 / 0.4)
+ log2(1 / (4 * 0.4))
+ (1 / 2) * log2(1 / (8 / 15))
+ (1 / 4) * log2(1 / (4 / 15))
)
assert_allclose(got, expect)
def test_filter_drop_remainder(self):
"""filter allows dropping"""
raw = {"a": "ACGACGACG", "b": "CCC---CCC", "c": "AAAA--AAA"}
aln = self.Class(raw)
func = _make_filter_func(aln)
got = aln.filtered(func, motif_length=1, warn=False)
self.assertEqual(len(got), 6)
# raises an assertion if the length is not modulo
with self.assertRaises(ValueError):
# because alignment not modulo 2
got = aln.filtered(func, motif_length=2, drop_remainder=False)
got = aln.filtered(func, motif_length=2, drop_remainder=True, warn=False)
self.assertEqual(len(got), 4)
def test_take_positions_info(self):
aln = self.Class(
{"a": "AAAAAAA", "b": "A--A-AA", "c": "AA-----"}, info={"key": "value"}
)
tps = aln.take_positions([5, 4, 0])
self.assertEqual(tps.info["key"], "value")
def test_take_positions_if(self):
"""SequenceCollection take_positions_if should return cols where f(col) is True"""
def gap_1st(x):
return x[0] == "-"
def gap_2nd(x):
return x[1] == "-"
def gap_3rd(x):
return x[2] == "-"
def is_list(x):
return isinstance(x, list)
self.gaps.names = "abc"
self.assertEqual(
self.gaps.take_positions_if(gap_1st), {"a": "", "b": "", "c": ""}
)
self.assertEqual(
self.gaps.take_positions_if(gap_2nd), {"a": "AAA", "b": "---", "c": "A--"}
)
self.assertEqual(
self.gaps.take_positions_if(gap_3rd),
{"a": "AAAAA", "b": "-A-AA", "c": "-----"},
)
self.assertEqual(self.gaps.take_positions_if(is_list), self.gaps)
self.assertEqual(self.gaps.take_positions_if(is_list), self.gaps)
self.assertTrue(
isinstance(self.gaps.take_positions_if(gap_1st), _SequenceCollectionBase)
)
# should be able to negate
self.assertEqual(self.gaps.take_positions_if(gap_1st, negate=True), self.gaps)
self.assertEqual(
self.gaps.take_positions_if(gap_2nd, negate=True),
{"a": "AAAA", "b": "AAAA", "c": "A---"},
)
self.assertEqual(
self.gaps.take_positions_if(gap_3rd, negate=True),
{"a": "AA", "b": "A-", "c": "AA"},
)
def test_omit_gap_pos2(self):
"""consistency with different motif_length values"""
data = {
"seq1": "CAGGTCGACCTCGGC---------CACGAC",
"seq2": "CAGATCGACCTCGGC---------CACGAC",
"seq3": "CAGATCGACCTCGGT---------CACGAT",
"seq4": "CAGATCGACCTCGGCGAACACGGCCATGAT",
"seq5": "CCGATCGACATGGGC---------CACGAT",
"seq6": "GCC---------------------------",
}
aln = self.Class(data, moltype=DNA)
got1 = aln.omit_gap_pos(motif_length=1)
got3 = aln.omit_gap_pos(motif_length=3)
self.assertEqual(len(got3), len(got1))
self.assertEqual(got3.to_dict(), got1.to_dict())
def test_omit_bad_seqs(self):
"""omit_bad_seqs should return alignment w/o seqs causing most gaps"""
data = {
"s1": "---ACC---TT-",
"s2": "---ACC---TT-",
"s3": "---ACC---TT-",
"s4": "--AACCG-GTT-",
"s5": "--AACCGGGTTT",
"s6": "AGAACCGGGTT-",
}
aln = self.Class(data, moltype=DNA)
# with defaults, excludes s6
expect = data.copy()
del expect["s6"]
result = aln.omit_bad_seqs()
self.assertEqual(result.to_dict(), expect)
# with quantile 0.5, just s1, s2, s3
expect = data.copy()
for key in ("s6", "s5"):
del expect[key]
result = aln.omit_bad_seqs(0.5)
self.assertEqual(result.to_dict(), expect)
def test_matching_ref(self):
"""Alignment.matching_ref returns new aln with well-aln to temp"""
aln = self.omitSeqsTemplate_aln
result = aln.matching_ref("s3", 0.9, 5)
self.assertEqual(result, {"s3": "UUCCUUCUU-UUC", "s4": "UU-UUUU-UUUUC"})
result2 = aln.matching_ref("s4", 0.9, 4)
self.assertEqual(result2, {"s3": "UUCCUUCUU-UUC", "s4": "UU-UUUU-UUUUC"})
result3 = aln.matching_ref("s1", 0.9, 4)
self.assertEqual(
result3,
{"s2": "UC------U---C", "s1": "UC-----CU---C", "s5": "-------------"},
)
result4 = aln.matching_ref("s3", 0.5, 13)
self.assertEqual(result4, {"s3": "UUCCUUCUU-UUC", "s4": "UU-UUUU-UUUUC"})
def test_iupac_consensus_RNA(self):
"""SequenceCollection iupac_consensus should use RNA IUPAC symbols correctly"""
alignmentUpper = self.Class(
[
"UCAGN-UCAGN-UCAGN-UCAGAGCAUN-",
"UUCCAAGGNN--UUCCAAGGNNAGCAG--",
"UUCCAAGGNN--UUCCAAGGNNAGCUA--",
"UUUUCCCCAAAAGGGGNNNN--AGCUA--",
"UUUUCCCCAAAAGGGGNNNN--AGCUA--",
],
moltype=RNA,
)
# following IUPAC consensus calculated by hand
# Test all uppper
self.assertEqual(
alignmentUpper.iupac_consensus(), "UYHBN?BSNN??KBVSN?NN??AGCWD?-"
)
def test_iupac_consensus_DNA(self):
"""SequenceCollection iupac_consensus should use DNA IUPAC symbols correctly"""
alignmentUpper = self.Class(
[
"TCAGN-TCAGN-TCAGN-TCAGAGCATN-",
"TTCCAAGGNN--TTCCAAGGNNAGCAG--",
"TTCCAAGGNN--TTCCAAGGNNAGCTA--",
"TTTTCCCCAAAAGGGGNNNN--AGCTA--",
"TTTTCCCCAAAAGGGGNNNN--AGCTA--",
]
)
# following IUPAC consensus calculated by hand
# Test all uppper
self.assertEqual(
alignmentUpper.iupac_consensus(DNA), "TYHBN?BSNN??KBVSN?NN??AGCWD?-"
)
def test_iupac_consensus_Protein(self):
"""SequenceCollection iupac_consensus should use protein IUPAC symbols correctly"""
alignmentUpper = self.Class(
[
"ACDEFGHIKLMNPQRSTUVWY-",
"ACDEFGHIKLMNPQRSUUVWF-",
"ACDEFGHIKLMNPERSKUVWC-",
"ACNEFGHIKLMNPQRS-UVWP-",
]
)
# following IUPAC consensus calculated by hand
# Test all uppper
self.assertEqual(
alignmentUpper.iupac_consensus(PROTEIN), "ACBEFGHIKLMNPZRS?UVWX-"
)
def test_is_ragged(self):
"""SequenceCollection is_ragged should return true if ragged alignment"""
assert not self.identical.is_ragged()
assert not self.gaps.is_ragged()
def test_probs_per_pos(self):
"""SequenceCollection.probs_per_pos should find Pr(symbol) in each
column"""
# make an alignment with 4 seqs (easy to calculate probabilities)
align = self.Class(["AAA", "ACA", "GGG", "GUC"])
got = align.probs_per_pos()
# check that the column probs match the counts we expect
expect = [
{"A": 0.5, "G": 0.5},
{"A": 0.25, "C": 0.25, "G": 0.25, "U": 0.25},
{"A": 0.5, "G": 0.25, "C": 0.25},
]
for pos, probs in enumerate(expect):
for char, prob in probs.items():
assert_allclose(got[pos, char], prob)
def test_majority_consensus(self):
"""SequenceCollection.majority_consensus should return commonest symbol per column"""
# Check the exact strings expected from string transform
self.assertEqual(self.sequences.majority_consensus(), "UCAG")
def test_uncertainties(self):
"""SequenceCollection.uncertainties should match hand-calculated values"""
aln = self.Class(["ABC", "AXC"])
obs = aln.entropy_per_pos()
assert_allclose(obs, [0, 1, 0])
# check what happens with only one input sequence
aln = self.Class(["ABC"])
obs = aln.entropy_per_pos()
assert_allclose(obs, [0, 0, 0])
def test_sample_with_replacement(self):
# test with replacement -- just verify that it rnus
alignment = self.Class({"seq1": "gatc", "seq2": "gatc"})
sample = alignment.sample(1000, with_replacement=True)
self.assertEqual(len(sample), 1000)
# ensure that sampling with replacement works on single col alignment
alignment1 = self.Class({"seq1": "A", "seq2": "A"})
result = alignment1.sample(with_replacement=True)
self.assertEqual(len(result), 1)
def test_sample_tuples(self):
##### test with motif size != 1 #####
alignment = self.Class(
{
"seq1": "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP",
"seq2": "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP",
}
)
shuffled = alignment.sample(motif_length=2)
# ensure length correct
sample = alignment.sample(10, motif_length=2)
self.assertEqual(len(sample), 20)
# test columns alignment preserved
seqs = list(sample.to_dict().values())
self.assertEqual(seqs[0], seqs[1])
# ensure each char occurs twice as sampling dinucs without replacement
for char in seqs[0]:
self.assertEqual(seqs[0].count(char), 2)
def test_copy(self):
"""correctly copy an alignment"""
aln = self.Class(data=[("a", "AC-GT"), ("b", "ACCGT")])
copied = aln.copy()
self.assertTrue(type(aln), type(copied))
self.assertEqual(aln.to_dict(), copied.to_dict())
self.assertEqual(id(aln.moltype), id(copied.moltype))
aln = self.Class(data=[("a", "AC-GT"), ("b", "ACCGT")], info={"check": True})
copied = aln.copy()
self.assertEqual(aln.info, copied.info)
def test_to_pretty(self):
"""produce correct pretty print formatted text"""
seqs = {"seq1": "ACGAANGA", "seq2": "-CGAACGA", "seq3": "ATGAACGA"}
expect = ["seq1 ACGAANGA", "seq2 -....C..", "seq3 .T...C.."]
aln = self.Class(data=seqs, moltype=DNA)
got = aln.to_pretty(name_order=["seq1", "seq2", "seq3"])
self.assertEqual(got, "\n".join(expect))
got = aln.to_pretty(name_order=["seq1", "seq2", "seq3"], wrap=4)
expect = [
"seq1 ACGA",
"seq2 -...",
"seq3 .T..",
"",
"seq1 ANGA",
"seq2 .C..",
"seq3 .C..",
]
self.assertEqual(got, "\n".join(expect))
def test_to_html(self):
"""produce correct html formatted text"""
seqs = {"seq1": "ACG", "seq2": "-CT"}
aln = self.Class(data=seqs, moltype=DNA)
got = aln.to_html(ref_name="longest") # name_order=['seq1', 'seq2'])
# ensure balanced tags are in the txt
for tag in ["<style>", "</style>", "<div", "</div>", "<table>", "</table>"]:
self.assertTrue(tag in got)
ref_row = (
'<tr><td class="label">seq1</td>'
'<td><span class="A_dna">A</span>'
'<span class="C_dna">C</span>'
'<span class="G_dna">G</span></td></tr>'
)
other_row = (
'<tr><td class="label">seq2</td>'
'<td><span class="ambig_dna">-</span>'
'<span class="C_dna">.</span>'
'<span class="T_dna">T</span></td></tr>'
)
self.assertTrue(ref_row in got)
self.assertTrue(other_row in got)
self.assertTrue(got.find(ref_row) < got.find(other_row))
# using different ref sequence
ref_row = (
'<tr><td class="label">seq2</td>'
'<td><span class="terminal_ambig_dna">-</span>'
'<span class="C_dna">C</span>'
'<span class="T_dna">T</span></td></tr>'
)
other_row = (
'<tr><td class="label">seq1</td>'
'<td><span class="A_dna">A</span>'
'<span class="C_dna">.</span>'
'<span class="G_dna">G</span></td></tr>'
)
got = aln.to_html(ref_name="seq2")
# order now changes
self.assertTrue(got.find(ref_row) < got.find(other_row))
def test_variable_positions(self):
"""correctly identify variable positions"""
new_seqs = {"A": "-CG-C", "B": "ACAA?", "C": "GCGAC"}
aln = self.Class(data=new_seqs, moltype=DNA)
self.assertEqual(aln.variable_positions(include_gap_motif=True), [0, 2, 3, 4])
self.assertEqual(aln.variable_positions(include_gap_motif=False), [0, 2])
new_seqs = {"A": "GCGAC", "B": "GCGAC", "C": "GCGAC"}
aln = self.Class(data=new_seqs, moltype=DNA)
self.assertEqual(aln.variable_positions(include_gap_motif=True), [])
self.assertEqual(aln.variable_positions(include_gap_motif=False), [])
def test_to_type_info(self):
"""interconverting between alignment types preserves info attribute"""
new_seqs = {"seq1": "ACGTACGTA", "seq2": "ACCGAA---", "seq3": "ACGTACGTT"}
array_align = self.Class == ArrayAlignment
# when array_align arg matches instance class, no conversion
# and get back self
aln = self.Class(data=new_seqs, info={"key": "value"})
new = aln.to_type(array_align=array_align)
self.assertEqual(id(aln), id(new))
self.assertEqual(new.info["key"], "value")
def test_to_dna(self):
"""alignment cast to DNA works"""
data = {"seq1": "ACGTACGTA", "seq2": "ACCGAA---", "seq3": "ACGTACGTT"}
aln = self.Class(data=data)
dna = aln.to_dna()
self.assertEqual(set(dna.names), set(aln.names))
self.assertTrue(dna.moltype == DNA)
# should fail if invalid character set
paln = dna.get_translation()
self.assertRaises(AlphabetError, paln.to_dna)
def test_to_dna_info(self):
"""alignment cast to DNA preserves info attribute"""
data = {"seq1": "ACGTACGTA", "seq2": "ACCGAA---", "seq3": "ACGTACGTT"}
aln = self.Class(data=data, info={"key": "value"})
dna = aln.to_dna()
self.assertEqual(dna.info["key"], "value")
def test_to_rna(self):
"""alignment cast to RNA works"""
data = {"seq1": "ACGUACGUA", "seq2": "ACCGAA---", "seq3": "ACGUACGUU"}
aln = self.Class(data=data)
rna = aln.to_rna()
self.assertEqual(set(rna.names), set(aln.names))
self.assertTrue(rna.moltype == RNA)
def test_to_rna_info(self):
"""alignment cast to RNA preserves info attribute"""
data = {"seq1": "ACGTACGTA", "seq2": "ACCGAA---", "seq3": "ACGTACGTT"}
aln = self.Class(data=data, info={"key": "value"})
rna = aln.to_rna()
self.assertEqual(rna.info["key"], "value")
def test_to_protein(self):
"""alignment cast to protein works"""
data = {"seq1": "TYV", "seq3": "TYV", "seq2": "TE-"}
aln = self.Class(data=data)
paln = aln.to_protein()
self.assertEqual(set(paln.names), set(aln.names))
self.assertTrue(paln.moltype == PROTEIN)
# should fail if invalid character set
self.assertRaises(AlphabetError, paln.to_dna)
def test_to_protein_info(self):
"""alignment cast to protein preserves info attribute"""
data = {"seq1": "ACGTACGTA", "seq2": "ACCGAA---", "seq3": "ACGTACGTT"}
aln = self.Class(data=data, info={"key": "value"})
dna = aln.to_dna()
self.assertEqual(dna.info["key"], "value")
def test_replace_seqs_info(self):
"""replace_seqs should preserve info attribute"""
a = self.Class(
{"seq1": "ACGU", "seq2": "C-UA", "seq3": "C---"}, info={"key": "value"}
)
seqs = {"seq1": "AAACCCGGGUUU", "seq2": "CCCUUUAAA", "seq3": "CCC"}
result = a.replace_seqs(seqs) # default behaviour
self.assertEqual(result.info["key"], "value")
def test_counts(self):
"""SequenceCollection.counts handles motif length, allow_gaps etc.."""
data = {"a": "AAAA??????", "b": "CCCGGG--NN"}
coll = self.Class(data=data, moltype=DNA)
got = coll.counts()
expect = dict(A=4, C=3, G=3)
for k, v in expect.items():
self.assertEqual(got[k], v)
got = coll.counts(motif_length=2)
expect = dict(AA=2, CC=1, CG=1, GG=1)
for k, v in expect.items():
self.assertEqual(got[k], v)
got = coll.counts(motif_length=2, allow_gap=True)
expect.update({"--": 1})
for k, v in expect.items():
self.assertEqual(got[k], v)
got = coll.counts(motif_length=2, include_ambiguity=True, allow_gap=True)
expect = dict(AA=2, CC=1, CG=1, GG=1, NN=1)
expect.update({"??": 3, "--": 1})
for k, v in expect.items():
self.assertEqual(got[k], v)
def test_counts_per_pos(self):
"""correctly count motifs"""
exp = array(
[
[1, 1, 1, 0],
[0, 2, 0, 1],
[0, 0, 3, 0],
[1, 1, 0, 1],
[0, 0, 3, 0],
[1, 1, 0, 1],
]
)
exp_gap = array(
[
[1, 1, 0, 1, 0],
[0, 2, 0, 0, 1],
[0, 0, 3, 0, 0],
[0, 2, 0, 1, 0],
[0, 1, 2, 0, 0],
[0, 2, 0, 1, 0],
]
)
s1 = DNA.make_seq("TCAGAG", name="s1")
s2 = DNA.make_seq("CCACAC", name="s2")
s3 = DNA.make_seq("AGATAT", name="s3")
s4 = DNA.make_seq("G-ACCC", name="s4")
aln = self.Class([s1, s2, s3], moltype=DNA)
obs = aln.counts_per_pos()
assert_equal(obs.array, exp)
assert_equal(obs.motifs, tuple(DNA.alphabet))
obs = aln.counts_per_pos(motif_length=2)
assert_equal(obs[0, "TC"], 1)
assert_equal(obs[1, "AC"], 1)
assert_equal(obs[2, "AC"], 1)
aln = self.Class([s1, s2, s4], moltype=DNA)
obs = aln.counts_per_pos(allow_gap=True)
assert_equal(obs.array, exp_gap)
aln = self.Class(["-RAT", "ACCT", "GTGT"], moltype="dna")
c = aln.counts_per_pos(include_ambiguity=False, allow_gap=True)
assert_equal(set(c.motifs), set("ACGT-"))
def test_counts_per_seq_default_moltype(self):
"""produce correct counts per seq with default moltypes"""
data = {"a": "AAAA??????", "b": "CCCGGG--NN", "c": "CCGGTTCCAA"}
coll = self.Class(data=data)
got = coll.counts_per_seq()
try:
self.assertEqual(got.col_sum()["-"], 0)
except KeyError:
pass # text moltype in Alignment excludes '-'
got = coll.counts_per_seq(include_ambiguity=True, allow_gap=True)
self.assertEqual(got.col_sum()["-"], 2)
def test_counts_per_pos_default_moltype(self):
"""produce correct counts per pos with default moltypes"""
data = {"a": "AAAA??????", "b": "CCCGGG--NN", "c": "CCGGTTCCAA"}
coll = self.Class(data=data)
got = coll.counts_per_pos()
# should not include gap character
self.assertNotIn("-", got.motifs)
# allowing gaps
got = coll.counts_per_pos(allow_gap=True)
# should include gap character
self.assertEqual(got[5, "-"], 0)
self.assertEqual(got[6, "-"], 1)
# now with motif-length 2
got = coll.counts_per_pos(motif_length=2)
found_motifs = set()
lengths = set()
for m in got.motifs:
lengths.add(len(m))
found_motifs.update(m)
self.assertTrue("-" not in found_motifs)
self.assertEqual(lengths, {2})
def test_entropy_per_pos_just_gaps(self):
"""pos with just gaps have nan"""
a = self.Class(dict(a="A---", b="C---", c="C---"), moltype=DNA)
entropy = a.entropy_per_pos()
assert_allclose(entropy, [0.91829583, numpy.nan, numpy.nan, numpy.nan])
a = self.Class(dict(a="---", b="---", c="---"), moltype=DNA)
entropy = a.entropy_per_pos()
assert_allclose(entropy, [numpy.nan, numpy.nan, numpy.nan])
def test_entropy_excluding_unobserved(self):
"""omitting unobserved motifs should not affect entropy calculation"""
a = self.Class(dict(a="ACAGGG", b="AGACCC", c="GGCCTA"), moltype=DNA)
entropy_excluded = a.entropy_per_seq(exclude_unobserved=True)
entropy_unexcluded = a.entropy_per_seq(exclude_unobserved=False)
assert_allclose(entropy_excluded, entropy_unexcluded)
def test_distance_matrix(self):
"""Alignment distance_matrix should produce correct scores"""
data = dict([("s1", "ACGTACGTA"), ("s2", "GTGTACGTA")])
aln = self.Class(data=data, moltype="dna")
dists = aln.distance_matrix(calc="hamming", show_progress=False)
self.assertEqual(dists, {("s1", "s2"): 2.0, ("s2", "s1"): 2.0})
# and for protein
aa = aln.get_translation()
dists = aa.distance_matrix(calc="hamming")
self.assertEqual(dists, {("s1", "s2"): 1.0, ("s2", "s1"): 1.0})
# when there are invalid data
data = dict(
seq1="AGGGGGGGGGGCCCCCCCCCCCCCCCCCGGGGGGGGGGGGGGGCGGTTTTTTTTTTTTTTTTTT",
seq2="TAAAAAAAAAAGGGGGGGGGGGGGGGGGGTTTTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCC",
seq3="TACAAAAAAAAGGGGCGGGGGGGGGGGGGTTTTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCC",
)
aln = self.Class(data=data, moltype="dna")
with self.assertRaises(ArithmeticError):
# default settings cause an exception
dists = aln.distance_matrix(calc="paralinear")
# but setting drop_invalid=False allows calc
dists = aln.distance_matrix(calc="paralinear", drop_invalid=True)
def test_get_gapped_seq(self):
"""Alignment.get_gapped_seq should return seq, with gaps"""
aln = self.Class({"seq1": "--TTT?", "seq2": "GATC??"})
self.assertEqual(str(aln.get_gapped_seq("seq1")), "--TTT?")
def test_count_gaps_per_seq(self):
"""correctly compute the number of gaps"""
data = {"a": "AAAA---GGT", "b": "CCC--GG?GT"}
aln = self.Class(data=data, moltype=DNA)
got = aln.count_gaps_per_seq(include_ambiguity=False)
assert_equal(got.array, [3, 2])
assert_equal(got["b"], 2)
got = aln.count_gaps_per_seq(include_ambiguity=True)
assert_equal(got.array, [3, 3])
assert_equal(got["b"], 3)
# per seq, unique
got = aln.count_gaps_per_seq(include_ambiguity=False, unique=True)
assert_equal(got.array, [1, 2])
got = aln.count_gaps_per_seq(include_ambiguity=True, unique=True)
assert_equal(got.array, [2, 2])
data = {"a": "AAAGGG", "b": "------", "c": "------"}
aln = self.Class(data=data, moltype=DNA)
got = aln.count_gaps_per_seq(include_ambiguity=False, unique=True)
assert_equal(got.array, [6, 0, 0])
assert_equal(got["a"], 6)
assert_equal(got["b"], 0)
# per_seq, induced_by
data = {"a": "--ACGT---GTAC", "b": "--ACGTA--GT--", "c": "--ACGTA-AGT--"}
aln = self.Class(data=data, moltype=DNA)
got = aln.count_gaps_per_seq(unique=False, induced_by=True)
assert_equal(got.array, [2, 1, 2])
assert_equal(got["b"], 1)
def test_coevolution(self):
"""correctly produces matrix of coevo measures"""
data = {"s0": "AA", "s1": "AA", "s2": "BB", "s3": "BB", "s4": "BC"}
aln = self.Class(data=data)
coevo = aln.coevolution(method="rmi", show_progress=False)
expect = array([[nan, nan], [0.78333333, nan]])
assert_allclose(coevo.array, expect)
coevo = aln.coevolution(method="nmi", show_progress=False)
self.assertNotEqual(coevo[1, 1], expect[1, 1])
# now check invoking drawable produces a result object with a drawable
# attribute
coevo = aln.coevolution(method="nmi", drawable="box", show_progress=False)
self.assertTrue(hasattr(coevo, "drawable"))
aln = load_aligned_seqs("data/brca1.fasta", moltype="dna")
aln = aln.take_seqs(aln.names[:20])
aln = aln.no_degenerates()[:20]
def test_info_source(self):
"""info.source exists if load_aligned_seqs given a filename"""
array_align = self.Class == ArrayAlignment
seqs = load_aligned_seqs("data/brca1.fasta", array_align=array_align)
self.assertEqual(seqs.info.source, "data/brca1.fasta")
def test_seq_entropy_just_gaps(self):
"""get_seq_entropy should get entropy of each seq"""
a = self.Class(dict(a="A---", b="----"), moltype=DNA)
entropy = a.entropy_per_seq()
assert_allclose(entropy, [0, numpy.nan])
a = self.Class(dict(a="----", b="----"), moltype=DNA)
entropy = a.entropy_per_seq()
self.assertIs(entropy, None)
def test_repr_html(self):
"""exercises method normally invoked in notebooks"""
aln = self.Class({"a": "AAAAA", "b": "AAA--"})
aln.set_repr_policy(num_seqs=5, num_pos=40)
self.assertEqual(aln[:3]._repr_policy, aln._repr_policy)
row_a = '<tr><td class="label">a</td>'
row_b = '<tr><td class="label">b</td>'
# default order is longest sequence at top
got = aln._repr_html_()
self.assertTrue(got.find(row_a) < got.find(row_b))
# change order, a should now be last
aln.set_repr_policy(num_seqs=5, num_pos=40, ref_name="b")
got = aln._repr_html_()
self.assertTrue(got.find(row_a) > got.find(row_b))
# tests repr policy has been successfully applied
aln = load_aligned_seqs("data/brca1.fasta", moltype="dna")
aln.set_repr_policy(num_seqs=2)
got = aln._repr_html_()
self.assertEqual(got.count("</tr>"), 3)
aln.set_repr_policy(num_seqs=3)
got = aln._repr_html_()
self.assertEqual(got.count("</tr>"), 4)
aln.set_repr_policy(num_seqs=len(aln.seqs))
got = aln._repr_html_()
self.assertEqual(got.count("</tr>"), len(aln.seqs) + 1)
# tests _repr_html_ displays correct number of sequences
aln = load_aligned_seqs("data/brca1.fasta", moltype="dna")
got = aln._repr_html_()
self.assertIn("%d x %d" % (aln.num_seqs, aln.seq_len), got.splitlines()[-2])
def test_seqlogo(self):
"""exercise producing a seq logo"""
data = {
"seq1": "CAGGTCGACCTCGGC---------CACGAC",
"seq2": "CAGATCGACCTCGGC---------CACGAC",
"seq3": "CAGATCGACCTCGGT---------CACGAT",
"seq4": "CAGATCGACCTCGGCGAACACGGCCATGAT",
"seq5": "CCGATCGACATGGGC---------CACGAT",
"seq6": "GCC---------------------------",
}
# with a defined moltype
aln = self.Class(data, moltype=DNA)
logo = aln.seqlogo()
# using wrap argument
logo = aln.seqlogo(wrap=20)
# should work for protein too
aa = aln.get_translation()
aa.seqlogo()
# without a defined moltype
aln = self.Class(data)
aln.seqlogo()
class ArrayAlignmentTests(AlignmentBaseTests, TestCase):
Class = ArrayAlignment
def test_slice_align(self):
"""slicing alignment should work correctly"""
data = {"seq1": "ACGACGACG", "seq2": "ACGACGACG", "seq3": "ACGACGACG"}
alignment = self.Class(data=data)
sub_align = alignment[2:5]
self.assertTrue(isinstance(sub_align, self.Class))
expect = {"seq1": "GAC", "seq2": "GAC", "seq3": "GAC"}
self.assertEqual(sub_align.to_dict(), expect)
# slice third positions
sub_align = alignment[2::3]
expect = {"seq1": "GGG", "seq2": "GGG", "seq3": "GGG"}
self.assertEqual(sub_align.to_dict(), expect)
def test_slice_align_info(self):
"""slicing alignment preserves info attribute"""
data = {"seq1": "ACGACGACG", "seq2": "ACGACGACG", "seq3": "ACGACGACG"}
alignment = self.Class(data=data, info={"key": "value"})
sub_align = alignment[2:5]
self.assertTrue(len(sub_align) == 3)
self.assertEqual(sub_align.info["key"], "value")
class AlignmentTests(AlignmentBaseTests, TestCase):
Class = Alignment
def test_sliding_windows(self):
"""sliding_windows should return slices of alignments."""
alignment = self.Class(
{"seq1": "ACGTACGT", "seq2": "ACGTACGT", "seq3": "ACGTACGT"}
)
result = []
for bit in alignment.sliding_windows(5, 2):
result += [bit]
self.assertEqual(
result[0].to_dict(), {"seq3": "ACGTA", "seq2": "ACGTA", "seq1": "ACGTA"}
)
self.assertEqual(
result[1].to_dict(), {"seq3": "GTACG", "seq2": "GTACG", "seq1": "GTACG"}
)
result = []
for bit in alignment.sliding_windows(5, 1):
result += [bit]
self.assertEqual(
result[0].to_dict(), {"seq3": "ACGTA", "seq2": "ACGTA", "seq1": "ACGTA"}
)
self.assertEqual(
result[1].to_dict(), {"seq3": "CGTAC", "seq2": "CGTAC", "seq1": "CGTAC"}
)
self.assertEqual(
result[2].to_dict(), {"seq3": "GTACG", "seq2": "GTACG", "seq1": "GTACG"}
)
self.assertEqual(
result[3].to_dict(), {"seq3": "TACGT", "seq2": "TACGT", "seq1": "TACGT"}
)
def test_with_gaps_from(self):
"""with_gaps_from should overwrite with gaps."""
gapless = self.Class({"seq1": "TCG", "seq2": "TCG"})
pregapped = self.Class({"seq1": "-CG", "seq2": "TCG"})
template = self.Class({"seq1": "A-?", "seq2": "ACG"})
r1 = gapless.with_gaps_from(template).to_dict()
r2 = pregapped.with_gaps_from(template).to_dict()
self.assertEqual(r1, {"seq1": "T-G", "seq2": "TCG"})
self.assertEqual(r2, {"seq1": "--G", "seq2": "TCG"})
def test_get_degapped_relative_to_info(self):
"""should remove all columns with a gap in sequence with given name
while preserving info attribute"""
aln = self.Class(
[
["name1", "-AC-DEFGHI---"],
["name2", "XXXXXX--XXXXX"],
["name3", "YYYY-YYYYYYYY"],
["name4", "-KL---MNPR---"],
],
info={"key": "foo"},
)
out_aln = self.Class(
[
["name1", "ACDEFGHI"],
["name2", "XXXX--XX"],
["name3", "YY-YYYYY"],
["name4", "KL--MNPR"],
],
info={"key": "bar"},
)
gdrt = aln.get_degapped_relative_to("name1")
self.assertEqual(gdrt.info["key"], "foo")
def test_deepcopy2(self):
""" "Aligned.deepcopy correctly handles gapped sequences"""
seqs = self.Class(
data={
"a": "CAGATTTGGCAGTT-",
"b": "-AGATTCAGCAGGTG",
"c": "CAGAT-CAGCAGGTG",
"d": "CAGATTCAGCAGGTG",
},
moltype="dna",
)
lengths = {len(s.deepcopy(sliced=True)) for s in seqs.seqs}
self.assertEqual(lengths, {len(seqs)})
rc = seqs.rc()
lengths = {len(s.deepcopy(sliced=True)) for s in rc.seqs}
self.assertEqual(lengths, {len(seqs)})
def test_to_moltype_annotations(self):
"""correctly convert to specified moltype with proper sequence annotations"""
s1 = Sequence("TTTTTTAAAA", name="test_seq1")
s2 = Sequence("AAAATTTTTT", name="test_seq2")
s3 = Sequence("AATTTTTAAA", name="test_seq3")
s1.add_feature(biotype="exon", name="fred", spans=[(0, 6)])
s2.add_feature(biotype="exon", name="fred", spans=[(4, 10)])
s3.add_feature(biotype="exon", name="fred", spans=[(2, 7)])
data = {"seq1": s1, "seq2": s2, "seq3": s3}
aln = self.Class(data=data)
aln.add_feature(biotype="demo", name="one", spans=[(0, 1), (2, 4)])
rna = aln.to_moltype("rna")
for name in rna.names:
orig_seq = aln.get_seq(name)
new_seq = rna.get_seq(name)
# check the sequence moltypes
self.assertEqual({s.data.moltype.label for s in rna.seqs}, {"rna"})
self.assertEqual(rna.moltype.label, "rna")
def test_construction(self):
"""correctly construct from list of sequences of length 2"""
seq1 = make_seq("AC-", name="seq1")
seq1 = Aligned(*seq1.parse_out_gaps())
seq2 = make_seq("ACG", name="seq2")
seq2 = Aligned(*seq2.parse_out_gaps())
coll = self.Class(data=[seq1, seq2])
class ArrayAlignmentSpecificTests(TestCase):
"""Tests of the ArrayAlignment object and its methods"""
def setUp(self):
"""Define some standard alignments."""
self.a2 = ArrayAlignment(["ABC", "DEF"], names=["x", "y"])
seqs = []
for s in ["abaa", "abbb"]:
seqs.append(AB.make_seq(s, preserve_case=True))
self.a = ArrayAlignment(seqs, moltype=AB)
self.b = Alignment(["ABC", "DEF"])
self.c = SequenceCollection(["ABC", "DEF"])
def test_init(self):
"""ArrayAlignment init should work from a sequence"""
a = ArrayAlignment(array([[0, 1, 2], [3, 4, 5]]).T)
assert_equal(a.seq_data, array([[0, 3], [1, 4], [2, 5]], "B"))
assert_equal(a.array_positions, array([[0, 1, 2], [3, 4, 5]], "B"))
assert_equal(a.names, ["seq_0", "seq_1", "seq_2"])
def test_init_seqs(self):
"""ArrayAlignment init should work from ArraySequence objects."""
s = list(map(ArraySequence, ["abc", "def"]))
a = ArrayAlignment(s)
assert_equal(a.seq_data, array(["abc", "def"], "c").view("B"))
def test_init_generic(self):
"""ArrayAlignment init should work from generic objects."""
s = ["abc", "def"]
a = ArrayAlignment(s)
assert_equal(a.seq_data, array(["abc", "def"], "c").view("B"))
def test_init_aln(self):
"""ArrayAlignment init should work from another alignment."""
s = ["abc", "def"]
a = ArrayAlignment(s)
b = ArrayAlignment(a)
self.assertIsNot(a.seq_data, b.seq_data)
assert_equal(b.seq_data, array(["abc", "def"], "c").view("B"))
def test_init_dict(self):
"""ArrayAlignment init should work from dict."""
s = {"abc": "AAACCC", "xyz": "GCGCGC"}
a = ArrayAlignment(s, names=["abc", "xyz"])
assert_equal(a.seq_data, array(["AAACCC", "GCGCGC"], "c").view("B"))
self.assertEqual(tuple(a.names), ("abc", "xyz"))
def test_iter(self):
"""ArrayAlignment iter should iterate over positions"""
result = list(iter(self.a2))
for i, j in zip(result, ["AD", "BE", "CF"]):
self.assertEqual(i, list(j))
def test_getitem(self):
"""ArrayAlignment getitem act like standard alignment slice"""
a2 = self.a2
expect = {"x": "B", "y": "E"}
got = a2[1]
self.assertEqual(got.to_dict(), expect)
expect = {"x": "BC", "y": "EF"}
got = a2[1:]
self.assertEqual(got.to_dict(), expect)
def test_get_sub_alignment(self):
"""ArrayAlignment get_sub_alignment should get requested part of alignment"""
a = ArrayAlignment({"x": "ABCE", "y": "FGHI", "z": "JKLM"})
# passing in positions should keep all seqs, but just selected
# positions
b = ArrayAlignment({"x": "BC", "y": "GH", "z": "KL"})
a_1 = a.get_sub_alignment(pos=[1, 2])
self.assertEqual(a_1.names, b.names)
self.assertEqual(a_1.seqs, b.seqs)
# ...and with negate_pos, should keep all except the positions passed in
a_2 = a.get_sub_alignment(pos=[0, 3], negate_pos=True)
self.assertEqual(a_2.seqs, b.seqs)
self.assertEqual(a_2.names, b.names)
# passing in seqs should keep all positions, but just selected seqs
c = ArrayAlignment({"x": "ABCE", "z": "JKLM"})
a_3 = a.get_sub_alignment(seqs=[0, 2])
self.assertEqual(a_3.seqs, c.seqs)
# check that labels were updates as well...
self.assertEqual(a_3.names, c.names)
# ...and should work with negate_seqs to exclude just selected seqs
a_4 = a.get_sub_alignment(seqs=[1], negate_seqs=True)
self.assertEqual(a_4.seqs, c.seqs)
self.assertEqual(a_4.names, c.names)
# should be able to do both seqs and positions simultaneously
d = ArrayAlignment({"x": "BC", "z": "KL"})
a_5 = a.get_sub_alignment(seqs=[0, 2], pos=[1, 2])
self.assertEqual(a_5.seqs, d.seqs)
self.assertEqual(a_5.names, d.names)
def test_get_sub_alignment_info(self):
"""ArrayAlignment get_sub_alignment should preserve info attribute"""
a = ArrayAlignment({"x": "ABCE", "y": "FGHI", "z": "JKLM"}, info={"key": "foo"})
# passing in positions should keep all seqs, but just selected
# positions
b = ArrayAlignment({"x": "BC", "y": "GH", "z": "KL"}, info={"key": "bar"})
a_1 = a.get_sub_alignment(pos=[1, 2])
self.assertEqual(a_1.names, b.names)
self.assertEqual(a.info["key"], "foo")
def test_str(self):
"""ArrayAlignment str should return FASTA representation of aln"""
self.assertEqual(str(self.a2), ">x\nABC\n>y\nDEF\n")
# should work if labels diff length
self.a2.names[-1] = "yyy"
self.assertEqual(str(self.a2), ">x\nABC\n>yyy\nDEF\n")
def test_entropy_per_pos(self):
"""entropy_per_pos should get entropy of each pos"""
a = self.a
f = a.entropy_per_pos()
e = array([0, 0, 1, 1])
assert_allclose(f, e)
f = a.entropy_per_pos(motif_length=2)
e = array([0, 1])
assert_allclose(f, e)
seqs = []
for s in ["-GAT", "ACCT", "GAGT"]:
seqs.append(make_seq(s, moltype="dna"))
a = ArrayAlignment(seqs)
f = a.entropy_per_pos(allow_gap=True)
e = array([1.584962500721156, 1.584962500721156, 1.584962500721156, 0])
assert_allclose(f, e)
seqs = []
for s in ["-RAT", "ACCT", "GTGT"]:
seqs.append(make_seq(s, moltype="dna"))
a = ArrayAlignment(seqs)
# "-RAT"
# "ACCT"
# "GTGT"
f = a.entropy_per_pos(allow_gap=False, include_ambiguity=False)
e = [
2 * safe_p_log_p(array([1 / 2])).sum(),
2 * safe_p_log_p(array([1 / 2])).sum(),
3 * safe_p_log_p(array([1 / 3])).sum(),
0,
]
assert_allclose(f, e)
f = a.entropy_per_pos(include_ambiguity=True)
e = [
2 * safe_p_log_p(array([1 / 2])).sum(),
3 * safe_p_log_p(array([1 / 3])).sum(),
3 * safe_p_log_p(array([1 / 3])).sum(),
0,
]
assert_allclose(f, e)
f = a.entropy_per_pos(allow_gap=True)
e = [
3 * safe_p_log_p(array([1 / 3])).sum(),
2 * safe_p_log_p(array([1 / 2])).sum(),
3 * safe_p_log_p(array([1 / 3])).sum(),
0,
]
assert_allclose(f, e)
class IntegrationTests(TestCase):
"""Test for integration between regular and model seqs and alns"""
def setUp(self):
"""Intialize some standard sequences"""
self.r1 = RNA.make_seq("AAA", name="x")
self.r2 = RNA.make_seq("CCC", name="y")
self.m1 = RNA.make_array_seq("AAA", name="xx")
self.m2 = RNA.make_array_seq("CCC", name="yy")
def test_model_to_model(self):
"""Model seq should work with dense alignment"""
a = ArrayAlignment([self.m1, self.m2])
self.assertEqual(a.to_dict(), {"xx": "AAA", "yy": "CCC"})
a = ArrayAlignment([self.m1, self.m2], moltype=DNA)
self.assertEqual(a.to_dict(), {"xx": "AAA", "yy": "CCC"})
self.assertEqual(self.m1.name, "xx")
def test_regular_to_model(self):
"""Regular seq should work with dense alignment"""
a = ArrayAlignment([self.r1, self.r2])
self.assertEqual(str(a), ">x\nAAA\n>y\nCCC\n")
a = ArrayAlignment([self.r1, self.r2], moltype=DNA)
self.assertEqual(str(a), ">x\nAAA\n>y\nCCC\n")
self.assertEqual(self.r1.name, "x")
def test_model_to_regular(self):
"""Model seq should work with regular alignment"""
a = Alignment([self.m1, self.m2])
self.assertEqual(str(a), ">xx\nAAA\n>yy\nCCC\n")
a = Alignment([self.m1, self.m2], moltype=DNA)
self.assertEqual(str(a), ">xx\nAAA\n>yy\nCCC\n")
self.assertEqual(self.m1.name, "xx")
def test_regular_to_regular(self):
"""Regular seq should work with regular alignment"""
a = Alignment([self.r1, self.r2])
self.assertEqual(str(a), ">x\nAAA\n>y\nCCC\n")
a = Alignment([self.r1, self.r2], moltype=DNA)
self.assertEqual(str(a), ">x\nAAA\n>y\nCCC\n")
self.assertEqual(self.r1.name, "x")
def test_model_aln_to_regular_aln(self):
"""Dense aln should convert to regular aln"""
a = ArrayAlignment([self.r1, self.r2])
d = Alignment(a)
self.assertEqual(str(d), ">x\nAAA\n>y\nCCC\n")
d = Alignment(a, moltype=DNA)
self.assertEqual(str(d), ">x\nAAA\n>y\nCCC\n")
self.assertEqual(self.r1.name, "x")
def test_regular_aln_to_model_aln(self):
"""Regular aln should convert to model aln"""
a = Alignment([self.r1, self.r2])
d = ArrayAlignment(a)
self.assertEqual(str(d), ">x\nAAA\n>y\nCCC\n")
d = ArrayAlignment(a, moltype=DNA)
self.assertEqual(str(d), ">x\nAAA\n>y\nCCC\n")
self.assertEqual(self.r1.name, "x")
def test_regular_aln_to_regular_aln(self):
"""Regular aln should convert to regular aln"""
a = Alignment([self.r1, self.r2])
d = Alignment(a)
self.assertEqual(str(d), ">x\nAAA\n>y\nCCC\n")
d = Alignment(a, moltype=DNA)
self.assertEqual(str(d), ">x\nAAA\n>y\nCCC\n")
self.assertEqual(self.r1.name, "x")
def test_model_aln_to_model_aln(self):
"""Model aln should convert to model aln"""
a = Alignment([self.r1, self.r2])
d = Alignment(a)
self.assertEqual(str(d), ">x\nAAA\n>y\nCCC\n")
d = Alignment(a, moltype=DNA)
self.assertEqual(str(d), ">x\nAAA\n>y\nCCC\n")
self.assertEqual(self.r1.name, "x")
@pytest.mark.parametrize(
"raw_seq,coords",
(("ACGGTAAAG", ((2, 4), (5, 8))), ("CCC---CCC", ((0, 3), (6, 9)))),
)
def test_featuremap_slice_aligned(raw_seq, coords):
from cogent3.core.location import FeatureMap, Span
im, seq = DNA.make_seq(raw_seq).parse_out_gaps()
ia = Aligned(im, seq)
length = len(raw_seq)
fmap = FeatureMap(spans=[Span(s, e) for s, e in coords], parent_length=length)
expect = "".join(raw_seq[s:e] for s, e in fmap.get_coordinates())
got = ia[fmap]
assert str(got) == expect
@pytest.mark.parametrize("cls", (ArrayAlignment, Alignment))
def test_to_type(cls):
"""correctly interconvert between alignment types"""
new_seqs = {"seq1": "ACGTACGTA", "seq2": "ACCGAA---", "seq3": "ACGTACGTT"}
array_align = cls == ArrayAlignment
# when array_align arg matches instance class, no conversion
# and get back self
aln = cls(data=new_seqs)
new = aln.to_type(array_align=array_align)
assert id(aln) == id(new)
# when array_align arg does not match, should get back the opposite type
new = aln.to_type(array_align=not array_align)
assert not isinstance(new, cls)
# we should be able to specify moltype and alignment
new = aln.to_type(array_align=not array_align, moltype=DNA)
assert new.to_dict() == new_seqs
# and translate
assert new.get_translation().to_dict() == {
"seq1": "TYV",
"seq3": "TYV",
"seq2": "TE-",
}
# should work on ArrayAlign when just moltype changes
new_seqs = {"seq1": "ACGTACGTA", "seq2": "ACCGAA---"}
aln = cls(data=new_seqs)
new = aln.to_type(array_align=array_align, moltype=DNA)
new = new.no_degenerates() # this should not fail!
assert len(new) == len(aln) - 3
# should correctly apply to existing moltype
aln = cls(data=new_seqs, moltype=DNA)
new = aln.to_type(array_align=not array_align)
assert aln.moltype == new.moltype
@pytest.mark.parametrize(
"moltype,array_align",
tuple(itertools.product(["rna", "dna", "protein"], [True, False])),
)
def test_upac_consensus_allow_gaps(moltype, array_align):
aln = make_aligned_seqs(
data={"s1": "ACGG", "s2": "ACGG", "s3": "-CGG"},
moltype=moltype,
array_align=array_align,
)
# default behaviour
iupac = aln.iupac_consensus()
assert iupac == "?CGG"
# allow_gaps
iupac = aln.iupac_consensus(allow_gap=False)
assert iupac == "ACGG"
def test_rc_iter():
dna = DNA.make_seq("ACG", name="seq1")
rc = dna.rc()
got = [x for x in rc]
expect = ["C", "G", "T"]
assert got == expect
def test_to_dna_raises():
seq = make_seq("ETV", moltype="protein")
with pytest.raises(AlphabetError):
seq.to_moltype("dna")
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment))
def test_annotate_from_gff3(cls):
"""annotate_from_gff should work on data from gff3 files"""
fasta_path = os.path.join("data/c_elegans_WS199_dna_shortened.fasta")
gff3_path = os.path.join("data/c_elegans_WS199_shortened_gff.gff3")
seq = load_seq(fasta_path, moltype="dna")
# using annotate_from_gff will nest annotations
seq_coll = cls({seq.name: seq})
seq_coll.annotate_from_gff(gff3_path)
member_seq = seq_coll.get_seq(seq.name)
matches = list(member_seq.get_features())
# 11 features
assert len(matches) == 11
matches = list(member_seq.get_features(biotype="gene"))
assert len(matches) == 1
matches = list(matches[0].get_children(biotype="mRNA"))
assert len(matches) == 1
matches = list(matches[0].get_children(biotype="exon"))
assert len(matches) == 3
@pytest.fixture(scope="session")
def seqcoll_db():
fasta_path = os.path.join("data/c_elegans_WS199_dna_shortened.fasta")
gff3_path = os.path.join("data/c_elegans_WS199_shortened_gff.gff3")
seq = load_seq(fasta_path, moltype="dna")
seq_coll = SequenceCollection({seq.name: seq})
seq_coll.annotate_from_gff(gff3_path)
return seq_coll
def test_seqcoll_query(seqcoll_db):
"""querying from a SequenceCollection produces features bound to their seqs"""
matches = list(seqcoll_db.get_features(seqid="I"))
# 11 features
assert len(matches) == 11
matches = list(seqcoll_db.get_features(seqid="I", biotype="gene"))
assert len(matches) == 1
matches = list(matches[0].get_children(biotype="mRNA"))
assert len(matches) == 1
matches = list(matches[0].get_children(biotype="exon"))
assert len(matches) == 3
def test_align_get_features():
# 0123456789 the positions
seq1 = make_seq("ACG--ACCGT", moltype="dna", name="seq1")
seq2 = make_seq("ACGGGCCCGT", moltype="dna", name="seq2")
# ***** the CDS feature
seq2.add_feature(biotype="CDS", name="fake01", spans=[(2, 7)], strand="+")
aln = make_aligned_seqs(data=[seq1, seq2], array_align=False)
feat = list(aln.get_features(biotype="CDS"))[0]
sl = aln[feat]
# slice is correct length
assert len(sl) == (7 - 2)
# returns correct value
assert sl.to_dict() == dict(seq1="G--AC", seq2="GGGCC")
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment))
def test_init_annotated_seqs(cls):
"""correctly construct from list with annotated seq"""
seq = make_seq("GCCAGGGGGGAAAG-GGAGAA", name="seq1")
_ = seq.add_feature(biotype="exon", name="name", spans=[(4, 10)])
coll = cls(data=[seq])
features = list(coll.get_features(biotype="exon"))
assert len(features) == 1
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment))
def test_get_annotations_from_any_seq(cls):
"""get_annotations_from_any_seq returns correct annotations"""
data = {"seq1": "ACGTACGTA", "seq2": "ACCGAA---", "seq3": "ACGTACGTT"}
seqs = cls(data, moltype=DNA)
db = GffAnnotationDb()
db.add_feature(seqid="seq1", biotype="exon", name="annotation1", spans=[(3, 8)])
db.add_feature(seqid="seq2", biotype="exon", name="annotation2", spans=[(1, 2)])
db.add_feature(seqid="seq3", biotype="exon", name="annotation3", spans=[(3, 6)])
seqs.annotation_db = db
got = list(seqs.get_features())
assert len(got) == 3
assert "biotype='exon', name='annotation1', map=[3:8]/9" in str(got[0])
assert "biotype='exon', name='annotation2', map=[1:2]/9" in str(got[1])
assert "biotype='exon', name='annotation3', map=[3:6]/9" in str(got[2])
got = list(seqs.get_features(name="annotation1"))
assert len(got) == 1
assert "biotype='exon', name='annotation1', map=[3:8]/9" in str(got[0])
got = list(seqs.get_features(biotype="exon", name="annotation2"))
assert len(got) == 1
assert "biotype='exon', name='annotation2', map=[1:2]/9" in str(got[0])
got = list(seqs.get_features(name="annotation3"))
assert len(got) == 1
assert "biotype='exon', name='annotation3', map=[3:6]/9" in str(got[0])
def test_annotate_matches_to():
"""Aligned.annotate_matches_to correctly delegates to sequence"""
aln = Alignment(dict(x="TTCCACTTCCGCTT"), moltype="dna")
aln.annotation_db = GffAnnotationDb()
seq = aln.named_seqs["x"]
pattern = "CCRC"
annot = seq.annotate_matches_to(
pattern=pattern, biotype="domain", name="fred", allow_multiple=True
)
got = [a.get_slice() for a in annot]
matches = ["CCAC", "CCGC"]
assert got == matches
annot = seq.annotate_matches_to(
pattern=pattern, biotype="domain", name="fred", allow_multiple=False
)
got = [a.get_slice() for a in annot]
assert got == matches[:1]
# handles regex from aa
aln = Alignment(dict(x="TTCCACTTCCGCTT"), moltype="dna")
aln.annotation_db = GffAnnotationDb()
gc = get_code(1)
aa_regex = gc.to_regex("FHF")
s = aln.named_seqs["x"].annotate_matches_to(
aa_regex, "domain", "test", allow_multiple=False
)
a = list(aln.get_features(seqid="x"))[0]
assert str(aln[a].named_seqs["x"]) == "TTCCACTTC"
@pytest.fixture(scope="function")
def gb_db(DATA_DIR):
from cogent3.core.annotation_db import load_annotations
return load_annotations(path=DATA_DIR / "annotated_seq.gb")
@pytest.fixture(scope="function")
def gff_db(DATA_DIR):
from cogent3.core.annotation_db import load_annotations
return load_annotations(path=DATA_DIR / "simple.gff")
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment))
def test_copy_annotations(cls, gff_db):
"""copy_annotations copies records from annotation db"""
seq_coll = cls({"seq1": "ACGU", "seq2": "CGUA", "test_seq": "CCGU"})
seq_coll.add_feature(seqid="seq1", biotype="xyz", name="abc", spans=[(1, 2)])
seq_coll.add_feature(seqid="seq2", biotype="xyzzz", name="abc", spans=[(1, 2)])
expect = seq_coll.annotation_db.num_matches() + gff_db.num_matches()
seq_coll.copy_annotations(gff_db)
assert seq_coll.annotation_db.num_matches() == expect
def _make_seq(name):
raw_seq = "AACCCAAAATTTTTTGGGGGGGGGGCCCC"
cds = (15, 25)
utr = (12, 15)
# name is required for creating annotations
seq = DNA.make_seq(raw_seq, name=name)
seq.add_feature(biotype="CDS", name="CDS", spans=[cds])
seq.add_feature(biotype="5'UTR", name="5' UTR", spans=[utr])
return seq
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment))
def test_init_seqs_have_annotations(cls, gff_db):
"""annotations on input seqs correctly merged and propagated"""
seq_coll = cls({"seq1": _make_seq("seq1"), "seq2": _make_seq("seq2")})
coll_db = seq_coll.annotation_db
assert len(coll_db) == 4
for seq in seq_coll.seqs:
if cls == Alignment:
db = seq.data.annotation_db
else:
db = seq.annotation_db
assert db is coll_db
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment))
def test_add_to_seq_updates_coll(cls, gff_db):
"""annotating a seq updates the db of the propagated"""
seq_coll = cls(
{"x": "AACCCAAAATTTTTTGGGGGGGGGGCCCC", "y": "AACCCAAAATTTTTTGGGGGGGGGGCCCC"}
)
x = seq_coll.get_seq("x")
assert len(seq_coll.annotation_db) == len(x.annotation_db) == 0
x.add_feature(biotype="exon", name="E1", spans=[(3, 8)])
assert len(seq_coll.annotation_db) == len(x.annotation_db) == 1
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment))
def test_assign_none(cls, gff_db):
"""assigning None to annotation_db breaks conection"""
seq_coll = cls({"seq1": "ACGU", "seq2": "CGUA", "test_seq": "CCGU"})
seq_coll.add_feature(seqid="seq1", biotype="xyz", name="abc", spans=[(1, 2)])
seq_coll.add_feature(seqid="seq2", biotype="xyzzz", name="abc", spans=[(1, 2)])
seq_coll.annotation_db = None
assert seq_coll.annotation_db is None
def test_copy_annotations_incompat_fails(seqcoll_db, gb_db):
"""copy_annotations copies records from annotation db"""
db = seqcoll_db.annotation_db
seqcoll = seqcoll_db.rename_seqs(
lambda x: "AE017341" if x == seqcoll_db.names[0] else seqcoll_db.names[0]
)
seqcoll.annotation_db = db
with pytest.raises(TypeError):
seqcoll.copy_annotations(gb_db)
def test_copy_annotations_incompat_type_fails(seqcoll_db, gb_db):
"""copy_annotations copies records from annotation db"""
with pytest.raises(TypeError):
seqcoll_db.copy_annotations({"a": "ACGGT"})
def test_aligned_deepcopy_sliced():
a = Aligned(*DNA.make_seq("GCAAGGCGCCAA").parse_out_gaps())
sliced = a[2:3]
sl_cp = sliced.deepcopy(sliced=True)
assert str(sl_cp) == str(sliced)
def test_deepcopy_with_features(DATA_DIR):
"""correctly deepcopy Aligned objects in an alignment"""
path = DATA_DIR / "brca1_5.paml"
# generates an annotatable Alignment object
aln = load_aligned_seqs(path, array_align=False, moltype="dna")
db = GffAnnotationDb()
# when the annotation is outside(before) boundary of the slice
db.add_feature(seqid="NineBande", biotype="exon", name="annot1", spans=[(0, 10)])
# when the annotation is across boundary of the slice
db.add_feature(seqid="Mouse", biotype="exon", name="annot2", spans=[(10, 21)])
# when the annotation is within boundary of the slice
db.add_feature(seqid="Human", biotype="exon", name="annot3", spans=[(20, 25)])
# when the annotation is across boundary of the slice
db.add_feature(seqid="HowlerMon", biotype="exon", name="annot4", spans=[(25, 32)])
# when the annotation is outside(after) boundary of the slice
db.add_feature(seqid="DogFaced", biotype="exon", name="annot5", spans=[(40, 45)])
aln.annotation_db = db
aln = aln[20:30]
# no slice
copied = aln.deepcopy(sliced=False)
feats = list(copied.get_features(biotype="exon", allow_partial=True))
assert len(feats) == 3 # overlap is Mouse, Human, HowlerMon
# with slice
copied = aln.deepcopy(sliced=True)
feats = list(copied.get_features(biotype="exon", allow_partial=True))
assert len(feats) == 3
# rc drops annotations only when sliced is True
rced = aln.rc()
copied = rced.deepcopy(sliced=False)
feats = list(copied.get_features(biotype="exon", allow_partial=True))
assert len(feats) == 3 # overlap is Mouse, Human, HowlerMon
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment))
def test_deepcopy_aligned(cls):
"""correctly deep copy aligned objects in an alignment"""
data = {"seq1": "ACGACGACG", "seq2": "ACGACGACG"}
seqs = cls(data)
copied = seqs.deepcopy(sliced=True)
orig_rd = seqs.to_rich_dict()
cpy_rd = copied.to_rich_dict()
assert orig_rd == cpy_rd
assert id(copied) != id(seqs)
for name in seqs.names:
assert id(copied.named_seqs[name]) != copied.named_seqs[name]
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment))
def test_seq_rename_preserves_annotations(cls):
"""rename seqs discards all annotations"""
data = {"seq1": "ACGTACGTA", "seq2": "ACCGAA---", "seq3": "ACGTACGTT"}
seqs = cls(data, moltype=DNA)
seqs.add_feature(seqid="seq1", biotype="exon", name="fred", spans=[(3, 8)])
assert seqs.annotation_db is not None
new = seqs.rename_seqs(lambda x: x.upper())
assert len(new.annotation_db) == 1
assert len(list(new.get_features(biotype="exon")))
# using original seq name should also work
assert len(list(new.get_features(seqid="seq1")))
@pytest.mark.parametrize(
"cls",
(SequenceCollection, ArrayAlignment),
)
def test_to_rich_dict_not_alignment(cls):
"""to_rich_dict produces correct dict"""
data = {"seq1": "ACGG", "seq2": "CGCA", "seq3": "CCG-"}
aln = cls(data, moltype="dna")
try:
seq_type = get_object_provenance(aln.seqs[0].data)
except AttributeError:
seq_type = get_object_provenance(aln.seqs[0])
got = aln.to_rich_dict()
data = {k: SeqView(s, seqid=k).to_rich_dict() for k, s in data.items()}
expect = {
"seqs": {
"seq1": {
"name": "seq1",
"seq": data["seq1"],
"info": None,
"type": seq_type,
"moltype": aln.moltype.label,
"version": __version__,
"annotation_offset": 0,
},
"seq2": {
"name": "seq2",
"seq": data["seq2"],
"info": None,
"type": seq_type,
"moltype": aln.moltype.label,
"version": __version__,
"annotation_offset": 0,
},
"seq3": {
"name": "seq3",
"seq": data["seq3"],
"info": None,
"type": seq_type,
"moltype": aln.moltype.label,
"version": __version__,
"annotation_offset": 0,
},
},
"moltype": aln.moltype.label,
"info": None,
"type": get_object_provenance(aln),
"version": __version__,
}
assert got == expect
def test_to_rich_dict_alignment():
"""to_rich_dict produces correct dict"""
data = {"seq1": "ACGG", "seq2": "CGCA", "seq3": "CCG-"}
aln = Alignment(data, moltype="dna")
got = aln.to_rich_dict()
data = {
k: Aligned(*make_seq(s, name=k, moltype="dna").parse_out_gaps()).to_rich_dict()
for k, s in data.items()
}
expect = {
"seqs": {
"seq1": data["seq1"],
"seq2": data["seq2"],
"seq3": data["seq3"],
},
"moltype": aln.moltype.label,
"info": None,
"type": get_object_provenance(aln),
"version": __version__,
}
assert got == expect
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment))
def test_dotplot_annotated(cls):
"""exercising dotplot method with annotated sequences"""
db = GffAnnotationDb()
db.add_feature(seqid="Human", biotype="exon", name="fred", spans=[(10, 15)])
seqs = cls(data={"Human": "CAGATTTGGCAGTT-", "Mouse": "CAGATTCAGCAGGTG"})
seqs.annotation_db = db
seqs = seqs.take_seqs(["Human", "Mouse"])
_ = seqs.dotplot(show_progress=False)
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_get_seq_entropy(cls):
"""ArrayAlignment get_seq_entropy should get entropy of each seq"""
seqs = [AB.make_seq(s, preserve_case=True) for s in ["abab", "bbbb", "abbb"]]
a = cls(seqs, moltype=AB)
entropy = a.entropy_per_seq()
e = 0.81127812445913283 # sum(p log_2 p) for p = 0.25, 0.75
assert_allclose(entropy, array([1, 0, e]))
@pytest.mark.parametrize("moltype", ("dna", "rna"))
def test_distance_matrix_singleton_collection(moltype):
"""SequenceCollection.distance_matrix() should raise error if collection
only contains a single sequence"""
collection = make_unaligned_seqs(data={"s1": "ACGTACGTAGTCGCG"}, moltype=moltype)
with pytest.raises(ValueError):
_ = collection.distance_matrix()
@pytest.mark.parametrize("moltype", ("dna", "rna"))
def test_collection_distance_matrix_same_seq(moltype):
"""Identical seqs should return distance measure of 0.0"""
data = dict(
[("s1", "ACGTACGTAGTCGCG"), ("s2", "GTGTACGTATCGCG"), ("s3", "GTGTACGTATCGCG")]
)
collection = make_unaligned_seqs(data=data, moltype=moltype)
dists = collection.distance_matrix(calc="pdist")
# all comparison of a sequence to itself should be zero
for seq in collection.names:
assert dists[(seq, seq)] == 0.0
# s2 and s3 are identical, so should be zero
assert dists[("s2", "s3")] == 0.0
assert dists[("s3", "s2")] == 0.0
@pytest.mark.parametrize("moltype", ("protein", "text", "bytes"))
def test_distance_matrix_fails_wrong_moltype(moltype):
data = [("s1", "ACGTA"), ("s2", "ACGTA")]
seqs = make_unaligned_seqs(data=data, moltype=moltype)
with pytest.raises(NotImplementedError):
seqs.distance_matrix()
@pytest.mark.parametrize("moltype", ("dna", "rna"))
def test_distance_matrix_passes_correct_moltype(moltype):
data = [("s1", "ACGTA"), ("s2", "ACGTA")]
seqs = make_unaligned_seqs(data=data, moltype=moltype)
seqs.distance_matrix()
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
@pytest.mark.parametrize(
"seqs", ({"seq1": "GATTTT", "seq2": "GATC??"}, {"seq1": "GAT---", "seq2": "?GATCT"})
)
def test_get_translation2(cls, seqs):
"""SequenceCollection.get_translation translates each seq"""
alignment = cls(data=seqs, moltype=DNA)
got = alignment.get_translation()
assert len(got) == 2
assert got.moltype == PROTEIN
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
def test_get_translation_with_stop(cls):
seqs = {"seq1": "GATTAG", "seq2": "?GATCT"}
alignment = cls(data=seqs, moltype=DNA)
got = alignment.get_translation(include_stop=True)
assert got.to_dict() == {"seq1": "D*", "seq2": "XS"}
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment, SequenceCollection))
def test_get_translation_trim_stop(cls):
seqs = {"seq1": "GATTCCTAG", "seq2": "GATTCCTCC"}
alignment = cls(data=seqs, moltype=DNA)
expect = {"seq1": "DS", "seq2": "DSS"}
if cls != SequenceCollection:
expect = {"seq1": "DS-", "seq2": "DSS"}
got = alignment.get_translation(trim_stop=True)
assert got.to_dict() == expect
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
@pytest.mark.parametrize(
"seqs", ({"seq1": "GATTTT", "seq2": "GATC??"}, {"seq1": "GAT---", "seq2": "?GATCT"})
)
def test_get_translation_error(cls, seqs):
"""SequenceCollection.get_translation translates each seq"""
# check for a failure when no moltype specified
alignment = cls(data=seqs)
with pytest.raises(TypeError):
alignment.get_translation()
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
@pytest.mark.parametrize("method", ("ic_score", "cogent3_score", "sp_score"))
def test_alignment_quality_methods(cls, method):
data = {
"DogFaced": "TG----AATATGT------GAAAGAG",
"FreeTaile": "TTGAAGAATATGT------GAAAGAG",
"LittleBro": "CTGAAGAACCTGTGAAAGTGAAAGAG",
}
expected_score = dict(
cogent3_score=-123.0, ic_score=32.93032499, sp_score=-25.25687109
)[method]
aln = cls(
data,
moltype="dna",
info=dict(align_params=dict(lnL=-123.0)),
)
app = get_app(method)
score = app(aln)
assert_allclose(score, expected_score)
@pytest.mark.parametrize("method", ("ic_score", "cogent3_score", "sp_score"))
def test_alignment_quality_methods_oneseq(method):
data = {
"DogFaced": "TG----AATATGT------GAAAGAG",
}
aln = make_aligned_seqs(
data,
moltype="dna",
info=dict(align_params=dict(lnL=-123.0)),
)
app = get_app(method)
score = app(aln)
assert_allclose(score, 0.0)
@pytest.mark.parametrize("method", ("ic_score", "cogent3_score", "sp_score"))
def test_alignment_quality_methods_zero_length(method):
data = {
"a": "",
"b": "",
"c": "",
}
aln = make_aligned_seqs(
data,
moltype="dna",
info=dict(align_params=dict(lnL=-123.0)),
)
app = get_app(method)
score = app(aln)
assert_allclose(score, 0.0)
def test_get_gap_array_equivalence():
# make sure produced gap arrays are identical between the
# two Alignment classes
data = {
"DogFaced": "TG----AATATGT------GAAAGAG",
"FreeTaile": "TTGAAGAATATGT------GAAAGAG",
"LittleBro": "CTGAAGAACCTGTGAAAGTGAAAGAG",
}
array_aln = make_aligned_seqs(data, moltype="dna", array_align=True)
aln = make_aligned_seqs(data, moltype="dna", array_align=False)
assert_allclose(array_aln.get_gap_array(), aln.get_gap_array())
@pytest.mark.parametrize("reverse", (False, True))
def test_aligned_rich_dict(reverse):
map_, s = make_seq(
"TTGAAGAATATGT------GAAAGAG", name="s1", moltype="dna"
).parse_out_gaps()
seq = Aligned(map_, s)
if reverse:
seq = seq.rc()
rd = seq.to_rich_dict()
got = Aligned.from_rich_dict(rd)
assert str(got) == str(seq)
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
@pytest.mark.parametrize(
"seqs",
(
{"seq1": "GATTTT", "seq2": "GATC??"},
{"seq1": "GAT---", "seq2": "?GATCT"},
),
)
def test_get_translation_info(cls, seqs):
"""SequenceCollection.get_translation preserves info attribute"""
alignment = cls(data=seqs, moltype=DNA, info={"key": "value"})
got = alignment.get_translation()
assert got.info["key"] == "value"
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
@pytest.mark.parametrize(
"gc,seqs",
(
(1, ("TCCTGA", "GATTT?")),
(1, ("ACGTAA---", "ACGAC----", "ACGCAATGA")),
(2, ("GATTTT", "TCCAGG")),
),
)
def test_has_terminal_stop_true(cls, gc, seqs):
gc = get_code(gc)
data = {f"s{i}": s for i, s in enumerate(seqs)}
seqs = cls(data=data, moltype="dna")
assert seqs.has_terminal_stop(gc=gc)
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
@pytest.mark.parametrize(
"gc,seqs",
((1, ("TCCTCA", "GATTTT")), (2, ("GATTTT", "TCCCGG")), (1, ("CCTCA", "ATTTT"))),
)
def test_has_terminal_stop_false(cls, gc, seqs):
gc = get_code(gc)
data = {f"s{i}": s for i, s in enumerate(seqs)}
seqs = cls(data=data, moltype="dna")
assert not seqs.has_terminal_stop(gc=gc)
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
def test_has_terminal_stop_strict(cls):
gc = get_code(1)
data = {f"s{i}": s for i, s in enumerate(("CCTCA", "ATTTT"))}
seqs = cls(data=data, moltype="dna")
with pytest.raises(AlphabetError):
seqs.has_terminal_stop(gc=gc, strict=True)
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
@pytest.mark.parametrize(
"gc,seqs",
(
(1, ("--AT-CTGA", "GATAAATT?")),
(1, ("ACGTGA---", "ACGAC----", "ACGCAATGA")),
(1, ("CCTCA-", "ATTTTA")),
(2, ("GATTTT", "TCCAGG")),
),
)
def test_trim_stops_true(cls, gc, seqs):
gc = get_code(gc)
data = {f"s{i}": s for i, s in enumerate(seqs)}
expect = {}
for k, v in data.items():
if cls != SequenceCollection or "-" in v:
v = re.sub("(TGA|AGG)(?=[-]*$)", "---", v)
else:
v = re.sub("(TGA|AGG)", "", v)
expect[k] = v
seqs = cls(data=data, moltype="dna")
got = seqs.trim_stop_codons(gc=gc).to_dict()
assert got == expect
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
@pytest.mark.parametrize(
"gc,seqs",
((1, ("T-CTGC", "GATAA?")), (2, ("GATTTT", "TCCCGG")), (1, ("CCTGC", "GATAA"))),
)
def test_trim_terminal_stops_nostop(cls, gc, seqs):
gc = get_code(gc)
data = {f"s{i}": s for i, s in enumerate(seqs)}
seqs = cls(data=data, moltype="dna")
got = seqs.trim_stop_codons(gc=gc)
assert got is seqs
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
@pytest.mark.parametrize("seqs", (("CCTCA", "ATTTT"), ("CCTCA-", "ATTTTA")))
def test_trim_terminal_stops_strict(cls, seqs):
gc = get_code(1)
data = {f"s{i}": s for i, s in enumerate(seqs)}
seqs = cls(data=data, moltype="dna")
with pytest.raises(AlphabetError):
seqs.trim_stop_codons(gc=gc, strict=True)
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
def test_trim_stop_codons_info(cls):
"""trim_stop_codons should preserve info attribute"""
coll = cls(
data={"seq1": "ACGTAA", "seq2": "ACGACG", "seq3": "ACGCGT"},
moltype=DNA,
info={"key": "value"},
)
coll = coll.trim_stop_codons()
assert coll.info["key"] == "value"
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
def test_get_translation_incomplete(cls):
"""get translation works on incomplete codons"""
alignment = cls(data={"seq1": "GATN--", "seq2": "?GATCT"}, moltype=DNA)
got = alignment.get_translation(incomplete_ok=True)
assert got.to_dict() == {"seq1": "D?", "seq2": "XS"}
with pytest.raises(AlphabetError):
_ = alignment.get_translation(incomplete_ok=False)
@pytest.mark.parametrize("name", ("s1", "s2", "s3"))
def test_get_seq_with_sliced_aln(name):
seqs = {
"s1": "GTTGAAGTAGTAGAAGTTCCAAATAATGAA",
"s2": "GTG------GTAGAAGTTCCAAATAATGAA",
"s3": "GCTGAAGTAGTGGAAGTTGCAAAT---GAA",
}
aln = make_aligned_seqs(data=seqs, moltype="dna", array_align=False)
start, stop = 1, 5
a1 = aln[start:stop]
seq = a1.get_seq(name)
assert isinstance(seq, Sequence), seq
got = str(seq)
expect = seqs[name][start:stop].replace("-", "")
assert got == expect, (got, expect)
@pytest.mark.parametrize("name", ("s1", "s2", "s3"))
def test_get_seq_with_sliced_rced_aln(name):
seqs = {
"s1": "GTTGAAGTAGTAGAAGTTCCAAATAATGAA",
"s2": "GTG------GTAGAAGTTCCAAATAATGAA",
"s3": "GCTGAAGTAGTGGAAGTTGCAAAT---GAA",
}
aln = make_aligned_seqs(data=seqs, moltype="dna", array_align=False)
start, stop = 1, 5
a1 = aln[start:stop]
a1 = a1.rc()
got = str(a1.get_seq(name))
dna = get_moltype("dna")
expect = dna.complement(seqs[name][start:stop].replace("-", ""))[::-1]
assert got == expect, (got, expect)
@pytest.mark.parametrize("name", ("s1", "s2", "s3"))
def test_get_seq_with_sliced_aln_multiple_spans(name):
seqs = { # the sliced seq has:
"s1": "GTTGA--TAGTAGAAGTTCCAAATAATGAA", # span gap span
"s2": "G----TT------AAGTTCCAAATAATGAA", # gap span gap
"s3": "G--GA--TA--GGAAGTTGCAAAT---GAA", # gap span gap span gap
}
aln = make_aligned_seqs(data=seqs, moltype="dna", array_align=False)
start, stop = 1, 10
a1 = aln[start:stop]
seq = a1.get_seq(name)
assert isinstance(seq, Sequence), seq
expect = seqs[name][start:stop].replace("-", "")
got = str(seq)
assert got == expect, (got, expect)
@pytest.mark.parametrize("name", ("s1", "s2", "s3"))
def test_get_seq_with_sliced_rced_aln_multiple_spans(name):
seqs = { # the sliced seq has:
"s1": "GTTGA--TAGTAGAAGTTCCAAATAATGAA", # span gap span
"s2": "G----TT------AAGTTCCAAATAATGAA", # gap span gap
"s3": "G--GA--TA--GGAAGTTGCAAAT---GAA", # gap span gap span gap
}
aln = make_aligned_seqs(data=seqs, moltype="dna", array_align=False)
start, stop = 1, 10
a1 = aln[start:stop]
a1 = a1.rc()
got = str(a1.get_seq(name))
dna = get_moltype("dna")
expect = dna.complement(seqs[name][start:stop].replace("-", ""))[::-1]
assert got == expect, (got, expect)
@pytest.mark.parametrize("name", ("s1", "s2", "s3"))
def test_get_gapped_seq_with_sliced_aln(name):
seqs = {
"s1": "G-TG---TAGTAGAAGTTCCAAATAATGAA",
"s2": "GTG------GTAGAAGTTCCAAATAATGAA",
"s3": "GC--AAGTAGTGGAAGTTGCAAAT---GAA",
}
aln = make_aligned_seqs(data=seqs, moltype="dna", array_align=False)
start, stop = 1, 10
a1 = aln[start:stop]
seq = a1.get_gapped_seq(name)
assert isinstance(seq, Sequence), seq
got = str(seq)
expect = seqs[name][start:stop]
assert got == expect, (got, expect)
@pytest.mark.parametrize("name", ("s1", "s2", "s3"))
@pytest.mark.parametrize("array_align", (True, False))
def test_aln_rev_slice(name, array_align):
seqs = {
"s1": "AAGGTTCC",
"s2": "AAGGTTCC",
"s3": "AAGGTTCC",
}
aln = make_aligned_seqs(data=seqs, moltype="dna", array_align=array_align)
got = aln[5:1]
assert not got
seq = got.get_gapped_seq(name)
assert not str(seq)
seq = got.get_seq(name)
assert not str(seq)
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
def test_init_duplicate_keys_raises(cls):
"""SequenceCollection init from (key, val) pairs should fail on dup. keys"""
data = [["x", "XXX"], ["b", "BBB"], ["x", "CCC"], ["d", "DDD"], ["a", "AAA"]]
with pytest.raises(ValueError):
cls(data)
@pytest.mark.parametrize(
"seq",
(
"ACGG",
DNA.make_seq("ACGG"),
numpy.array([2, 1, 3, 3], dtype=int),
Aligned(*DNA.make_seq("AC-GG").parse_out_gaps()),
),
)
def test_construct_unaligned_seq(seq):
moltype = get_moltype("dna")
got = _construct_unaligned_seq(seq, name="seq1", moltype=moltype)
assert isinstance(got, (Sequence, ArraySequence))
@pytest.mark.parametrize(
"data",
(
[["s1", "ACGG"]],
{"s1": "ACGG"},
{"s1": DNA.make_seq("ACGG")},
(
Aligned(
*DNA.make_seq("AC-GG", name="s1").parse_out_gaps(),
),
),
iter([["s1", "ACGG"]]),
),
)
def test_to_unaligned_seqs(data):
moltype = get_moltype("dna")
got = _coerce_to_unaligned_seqs(data, names=None, moltype=moltype)
assert isinstance(got[0], list)
assert isinstance(got[0][0], (Sequence, ArraySequence))
@pytest.mark.parametrize("moltype", ("dna", "protein"))
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
def test_same_moltype(cls, moltype):
moltype = get_moltype(moltype)
data = dict(s1="ACGTT", s2="ACCTT")
seqs = cls(data, moltype=moltype)
got = seqs.to_moltype(moltype=moltype)
assert got is seqs
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
def test_add(cls):
"""__add__ should concatenate sequence data, by name"""
align1 = cls({"a": "AAAA", "b": "TTTT", "c": "CCCC"})
align2 = cls({"a": "GGGG", "b": "----", "c": "NNNN"})
align = align1 + align2
concatdict = align.to_dict()
assert concatdict == {"a": "AAAAGGGG", "b": "TTTT----", "c": "CCCCNNNN"}
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_count_gaps_per_pos(cls):
"""correctly compute the number of gaps"""
data = {"a": "AAAA---GGT", "b": "CCC--GG?GT"}
aln = cls(data=data, moltype=DNA)
# per position
got = aln.count_gaps_per_pos(include_ambiguity=False)
assert_equal(got.array, [0, 0, 0, 1, 2, 1, 1, 0, 0, 0])
got = aln.count_gaps_per_pos(include_ambiguity=True)
assert_equal(got.array, [0, 0, 0, 1, 2, 1, 1, 1, 0, 0])
def _make_and_filter(cls, raw, expected, motif_length, drop_remainder):
# a simple filter func
aln = cls(raw, info={"key": "value"})
func = _make_filter_func(aln)
result = aln.filtered(
func,
motif_length=motif_length,
warn=False,
drop_remainder=drop_remainder,
)
assert result.to_dict() == expected
assert result.info["key"] == "value"
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_filtered(cls):
"""filtered should return new alignment with positions consistent with
provided callback function"""
# a simple filter option
raw = {"a": "ACGACGACG", "b": "CCC---CCC", "c": "AAAA--AAA"}
_make_and_filter(cls, raw, {"a": "ACGACG", "b": "CCCCCC", "c": "AAAAAA"}, 1, True)
# check with motif_length = 2
_make_and_filter(cls, raw, {"a": "ACAC", "b": "CCCC", "c": "AAAA"}, 2, True)
# check with motif_length = 3
_make_and_filter(cls, raw, {"a": "ACGACG", "b": "CCCCCC", "c": "AAAAAA"}, 3, True)
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
def test_counts_per_seq(cls):
"""SequenceCollection.counts_per_seq handles motif length, allow_gaps etc.."""
data = {"a": "AAAA??????", "b": "CCCGGG--NN", "c": "CCGGTTCCAA"}
coll = cls(data=data, moltype="dna")
mtype = coll.moltype
got = coll.counts_per_seq()
assert got["a", "A"] == 4
assert len(got.motifs) == len(mtype.alphabet)
got = coll.counts_per_seq(include_ambiguity=True, allow_gap=True)
# N, -, ? are the additional states
assert len(got.motifs) == 7
expect = {"-": 2, "?": 0, "A": 0, "C": 3, "G": 3, "N": 2, "T": 0}
b = got["b"].to_dict()
for k in expect:
assert b[k] == expect[k]
got = coll.counts_per_seq(motif_length=2)
assert len(got.motifs), len(mtype.alphabet) ** 2
assert got["a", "AA"] == 2
assert got["b", "GG"] == 1
got = coll.counts_per_seq(exclude_unobserved=True)
expect = {"C": 4, "G": 2, "T": 2, "A": 2}
c = got["c"].to_dict()
for k in expect:
assert c[k] == expect[k]
def test_counts_per_seq_arr_alignment():
"""ArrayAlignment counts_per_seq should return motif counts each seq"""
seqs = []
for s in ["abaa", "abbb"]:
seqs.append(AB.make_seq(s, preserve_case=True))
a = ArrayAlignment(seqs, moltype=AB)
f = a.counts_per_seq()
assert_equal(f.array, array([[3, 1], [1, 3]]))
f = a.counts_per_seq(motif_length=2, exclude_unobserved=True)
assert_equal(f.array, array([[1, 1, 0], [0, 1, 1]]))
def test_coevolution_segments():
"""specifying coordinate segments produces matrix with just those"""
aln = load_aligned_seqs("data/brca1.fasta", moltype="dna")
aln = aln.take_seqs(aln.names[:20])
aln = aln.no_degenerates()[:20]
coevo = aln.coevolution(segments=[(4, 6), (11, 13)], show_progress=False)
assert coevo.template.names[0] == [4, 5, 11, 12]
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_iter_positions(cls):
data = {"a": "AAAAAA", "b": "AAA---", "c": "AAAA--"}
r = cls({k: data[k] for k in "cb"})
assert list(r.iter_positions(pos_order=[5, 1, 3])) == list(
map(list, ["--", "AA", "A-"])
)
# reorder names
r = cls(data)
cols = list(r.iter_positions())
assert cols == list(map(list, ["AAA", "AAA", "AAA", "A-A", "A--", "A--"]))
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_add_from_ref_aln(cls):
"""should add or insert seqs based on align to reference"""
aln1 = cls(
[
["name1", "-AC-DEFGHI---"],
["name2", "XXXXXX--XXXXX"],
["name3", "YYYY-YYYYYYYY"],
]
)
aln2 = cls(
[
["name1", "ACDEFGHI"],
["name4", "KL--MNPR"],
["name5", "KLACMNPR"],
["name6", "KL--MNPR"],
]
)
aligned_to_ref_out_aln_inserted = cls(
[
["name1", "-AC-DEFGHI---"],
["name4", "-KL---MNPR---"],
["name5", "-KL-ACMNPR---"],
["name6", "-KL---MNPR---"],
["name2", "XXXXXX--XXXXX"],
["name3", "YYYY-YYYYYYYY"],
]
)
aln2_wrong_refseq = cls((("name1", "ACDXFGHI"), ("name4", "KL--MNPR")))
aln2_wrong_refseq_name = cls([["nameY", "ACDEFGHI"], ["name4", "KL--MNPR"]])
aln2_different_aln_class = ArrayAlignment(
[["name1", "ACDEFGHI"], ["name4", "KL--MNPR"]]
)
aligned_to_ref_out_aln = cls(
[
["name1", "-AC-DEFGHI---"],
["name2", "XXXXXX--XXXXX"],
["name3", "YYYY-YYYYYYYY"],
["name4", "-KL---MNPR---"],
]
)
out_aln = aln1.add_from_ref_aln(aln2, after_name="name1")
assert str(aligned_to_ref_out_aln_inserted) == str(out_aln) # test insert_after
out_aln = aln1.add_from_ref_aln(aln2, before_name="name2")
assert aligned_to_ref_out_aln_inserted == out_aln # test insert_before
with pytest.raises(ValueError):
aln1.add_from_ref_aln(aln2_wrong_refseq_name)
aln = aln1.add_from_ref_aln(aln2_different_aln_class)
# test_align_to_refseq_different_aln_class
assert aligned_to_ref_out_aln == aln
with pytest.raises(ValueError):
# test wrong_refseq
aln1.add_from_ref_aln(aln2_wrong_refseq)
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_replace_seqs(cls):
"""replace_seqs should replace 1-letter w/ 3-letter seqs"""
a = cls({"seq1": "ACGU", "seq2": "C-UA", "seq3": "C---"})
seqs = {"seq1": "AAACCCGGGUUU", "seq2": "CCCUUUAAA", "seq3": "CCC"}
result = a.replace_seqs(seqs) # default behaviour
assert result.to_dict() == {
"seq1": "AAACCCGGGUUU",
"seq2": "CCC---UUUAAA",
"seq3": "CCC---------",
}
result = a.replace_seqs(seqs, aa_to_codon=True) # default behaviour
assert result.to_dict() == {
"seq1": "AAACCCGGGUUU",
"seq2": "CCC---UUUAAA",
"seq3": "CCC---------",
}
# should correctly gap the same sequences with same length
result = a.replace_seqs(a.degap(), aa_to_codon=False) # default behaviour
assert result.to_dict() == {"seq1": "ACGU", "seq2": "C-UA", "seq3": "C---"}
# should fail when not same length if aa_to_codon is False
new = SequenceCollection(
[(n, s.replace("-", "")) for n, s in list(a[:3].to_dict().items())],
moltype="bytes",
)
with pytest.raises(ValueError):
a.replace_seqs(new, aa_to_codon=False)
# check the gaps are changed
aln1 = cls(data={"a": "AC-CT", "b": "ACGCT"})
aln2 = cls(data={"a": "ACC-T", "b": "ACGCT"})
result = aln1.replace_seqs(aln2, aa_to_codon=False)
assert id(aln1) != id(aln2)
assert aln1.to_dict() == result.to_dict()
def test_get_alphabet_and_moltype():
"""ArrayAlignment should figure out correct alphabet and moltype"""
s1 = "A"
s2 = RNA.make_seq("AA")
d = ArrayAlignment(s1)
assert d.moltype is BYTES
assert d.alphabet is BYTES.alphabet
d = ArrayAlignment(s1, moltype=RNA)
assert d.moltype is RNA
assert d.alphabet is RNA.alphabets.degen_gapped
d = ArrayAlignment([s2])
assert d.moltype is RNA
assert d.alphabet is RNA.alphabets.degen_gapped
d = ArrayAlignment(s2, moltype=DNA)
assert d.moltype is DNA
assert d.alphabet is DNA.alphabets.degen_gapped
# checks for containers
d = ArrayAlignment([s2])
assert d.moltype is RNA
d = ArrayAlignment({"x": s2})
assert d.moltype is RNA
d = ArrayAlignment(set([s2]))
assert d.moltype is RNA
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment, SequenceCollection))
def test_aln_from_fasta_parser(cls):
"""aln_from_fasta_parser should init from iterator"""
s = ">aa\nAC\n>bb\nAA\n>c\nGG\n".splitlines()
p = MinimalFastaParser(s)
aln = cls(p, moltype=DNA)
assert aln.named_seqs["aa"] == "AC"
assert aln.to_dict() == {"aa": "AC", "bb": "AA", "c": "GG"}
s2 = ">aa\nAC\n>bb\nAA\n>c\nGG\n"
d = cls(MinimalFastaParser(s2.splitlines()))
assert d.to_dict() == aln.to_dict()
@pytest.mark.parametrize(
"d", ({"a": "AAAAA", "b": "BBBBB"}, {"a": b"AAAAA", "b": b"BBBBB"})
)
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment, SequenceCollection))
def test_init_dict(cls, d):
"""SequenceCollection init from dict should work as expected"""
# from bytes strings
a = cls(d, names=["a", "b"])
expect = {k: v.decode("utf8") if isinstance(v, bytes) else v for k, v in d.items()}
assert a.to_dict() == expect
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment, SequenceCollection))
def test_init_seq_info(cls):
"""SequenceCollection init from seqs w/ info should preserve data"""
a = Sequence("AAA", name="a", info={"x": 3})
b = Sequence("CCC", name="b", info={"x": 4})
c = Sequence("GGG", name="c", info={"x": 5})
seqs = [c, b, a]
a = cls(seqs)
assert list(a.names) == ["c", "b", "a"]
assert list(map(str, a.seqs)) == ["GGG", "CCC", "AAA"]
if cls is not ArrayAlignment:
# ArrayAlignment is allowed to strip info objects
assert [i.info.x for i in a.seqs] == [5, 4, 3]
# check it still works if constructed from same class
b = cls(a)
assert list(b.names) == ["c", "b", "a"]
assert list(map(str, b.seqs)) == ["GGG", "CCC", "AAA"]
if cls is not ArrayAlignment:
# ArrayAlignment is allowed to strip Info objects
assert [i.info.x for i in b.seqs] == [5, 4, 3]
@pytest.mark.parametrize("val", (None, 3))
def test_init_empty(val):
"""ArrayAlignment init should fail if empty."""
with pytest.raises((ValueError, TypeError)):
ArrayAlignment(val)
ArrayAlignment()
def test_make_case():
data = {"a": "tata", "b": "tgtc", "c": "gcga", "d": "gaac", "e": "gagc"}
got = make_aligned_seqs(data=data, moltype="dna")
assert got == {k: v.upper() for k, v in data.items()}
@pytest.fixture(scope="session")
def brca1_data():
return load_aligned_seqs("data/brca1.fasta").to_dict()
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment, SequenceCollection))
def test_dotplot(cls, brca1_data):
"""exercising dotplot method"""
seqs = cls(data=brca1_data, moltype=DNA)
_ = seqs.dotplot()
with pytest.raises(AssertionError):
seqs.dotplot(window=5, k=11)
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_dotplot_alignment(cls):
"""exercising dotplot method"""
aln = cls([["name1", "TTTTTTAAAA"], ["name2", "AAAATTTTTT"]])
aln = aln[2:8]
draw = aln.dotplot(show_progress=False)
expected = {("name1", "TTTTAA"), ("name2", "AATTTT")}
got = {(s.name, str(s)) for s in (draw.seq1, draw.seq2)}
assert got == expected
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_get_degapped_relative_to(cls):
"""should remove all columns with a gap in sequence with given name"""
aln = cls(
[
["name1", "-AC-DEFGHI---"],
["name2", "XXXXXX--XXXXX"],
["name3", "YYYY-YYYYYYYY"],
["name4", "-KL---MNPR---"],
]
)
expect = dict(
[
["name1", "ACDEFGHI"],
["name2", "XXXX--XX"],
["name3", "YY-YYYYY"],
["name4", "KL--MNPR"],
]
)
result = aln.get_degapped_relative_to("name1")
assert result.to_dict() == expect
with pytest.raises(ValueError):
aln.get_degapped_relative_to("nameX")
@pytest.mark.parametrize(
"data",
(
{"seq1": "ACAACGACG", "seq2": "ACGACGACG"},
{"seq1": "-CAACGACG", "seq2": "ACGACGACG"},
{"seq1": "ACAACGAC-", "seq2": "ACGACGACG"},
{"seq1": "AC-ACGACG", "seq2": "ACGACGACG"},
{"seq1": "ACA-CGAC-", "seq2": "ACGACGACG"},
),
)
@pytest.mark.parametrize("name", ("seq1", "seq2"))
@pytest.mark.parametrize("rev", (False, True))
def test_sliced_deepcopy(data, name, rev):
"""correctly deep copy aligned objects in an alignment"""
orig = Alignment(data, moltype="dna")
slice_start, slice_end = 3, 5
aln = orig[slice_start:slice_end]
if rev:
aln = aln.rc()
notsliced = aln.deepcopy(sliced=False)
# the annotation offsets should match original object
assert {s.data.annotation_offset for s in notsliced.seqs} == {
s.data.annotation_offset for s in aln.seqs
}
sliced = aln.deepcopy(sliced=True)
assert sliced.to_dict() == notsliced.to_dict()
sliced_seq = sliced.named_seqs[name]
notsliced_seq = notsliced.named_seqs[name]
assert str(sliced_seq) == str(notsliced_seq)
# the annotation offsets should match original object
assert {s.data.annotation_offset for s in sliced.seqs} == {
s.data.annotation_offset for s in aln.seqs
}
# if not sliced in copy, underlying seq data is identical to original
assert (
notsliced.named_seqs[name].data._seq.seq is orig.named_seqs[name].data._seq.seq
)
# but not the same for sliced
assert (
sliced.named_seqs[name].data._seq.seq is not orig.named_seqs[name].data._seq.seq
)
assert sliced.named_seqs[name].map.parent_length == len(
str(sliced_seq).replace("-", "")
)
assert notsliced.named_seqs[name].map.parent_length == len(
str(notsliced_seq).replace("-", "")
)
# and map.parent_length and len(data) should match
assert sliced.named_seqs[name].map.parent_length == len(
sliced.named_seqs[name].data
)
# if sliced, seq data should be < orig
assert len(sliced.named_seqs[name].data) < len(orig.named_seqs[name].data)
def test_aligned_deepcopy_sliced_map_matches_data():
m, seq = DNA.make_seq("ACAACGACG", name="seq1").parse_out_gaps()
aligned = Aligned(m, seq)
sliced = aligned[3:5]
sliced_copy = sliced.deepcopy(sliced=True)
assert sliced_copy.map.parent_length == len(sliced_copy.data)
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
def test_iter_selected(cls):
"""SequenceCollection iter_selected() should iterate over items in correct order"""
# should work if one row
one_seq = cls({"a": "AAAAA"})
ragged_padded = cls({"a": "AAAAAA", "b": "AAA---", "c": "AAAA--"})
ordered1 = cls({"a": "AAAAA", "b": "BBBBB"}, names=["a", "b"])
ordered2 = cls({"a": "AAAAA", "b": "BBBBB"}, names=["b", "a"])
assert list(one_seq.iter_selected()) == (["A"] * 5)
# should take order into account
assert list(ordered1.iter_selected()) == (["A"] * 5 + ["B"] * 5)
assert list(ordered2.iter_selected()) == (["B"] * 5 + ["A"] * 5)
# should allow row and/or col specification
r = ragged_padded
assert list(r.iter_selected(seq_order=["c", "b"], pos_order=[5, 1, 3])) == list(
"-AA-A-"
)
# should not interfere with superclass iteritems()
i = list(r.named_seqs.items())
i.sort()
assert i == ([("a", "AAAAAA"), ("b", "AAA---"), ("c", "AAAA--")])
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_take_positions(cls):
"""SequenceCollection take_positions should return new alignment w/ specified pos"""
gaps = cls({"a": "AAAAAAA", "b": "A--A-AA", "c": "AA-----"})
assert gaps.take_positions([5, 4, 0]) == {"a": "AAA", "b": "A-A", "c": "--A"}
assert isinstance(gaps.take_positions([0]), _SequenceCollectionBase)
# should be able to negate
assert gaps.take_positions([5, 4, 0], negate=True) == {
"a": "AAAA",
"b": "--AA",
"c": "A---",
}
@pytest.mark.parametrize("array_align", (True, False))
@pytest.mark.parametrize(
"seq_moltype", (("ACCT--GT", "dna"), ("ACCT--GU", "rna"), ("MTST--T", "protein"))
)
def test_alignment_propogates_seqid_to_seqview(array_align, seq_moltype):
data = {"seq1": seq_moltype[0], "seq2": seq_moltype[0], "seq3": seq_moltype[0]}
aln = make_aligned_seqs(data, moltype=seq_moltype[1], array_align=array_align)
if array_align:
assert aln.seqs[0]._seq.seqid == "seq1"
else:
assert aln.seqs[0].data._seq.seqid == "seq1"
@pytest.mark.parametrize("cls", (str, bytes))
def test_construct_unaligned_seq_propogates_seqid(cls):
data = "ACGT"
if cls is bytes:
seq = cls(data, "utf8")
else:
seq = cls(data)
got = _construct_unaligned_seq(seq, name="seq1", moltype=DNA)
assert got._seq.seqid == "seq1"
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_sample_info(cls):
"""Alignment.sample should preserver info attribute"""
alignment = cls(
{"seq1": "ABCDEFGHIJKLMNOP", "seq2": "ABCDEFGHIJKLMNOP"},
info={"key": "value"},
)
# effectively permute columns, preserving length
shuffled = alignment.sample()
assert shuffled.info["key"] == "value"
# ensure length correct
sample = alignment.sample(10)
assert sample.info["key"] == "value"
@pytest.mark.parametrize("cls", (SequenceCollection, Alignment, ArrayAlignment))
def test_to_json(cls):
"""roundtrip of to_json produces correct dict"""
aln = cls({"seq1": "ACGG", "seq2": "CGCA", "seq3": "CCG-"})
txt = aln.to_json()
got = json.loads(txt)
expect = aln.to_rich_dict()
assert got == expect
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_get_position_indices(cls):
"""get_position_indices should return names of cols where f(col)"""
def gap_1st(x):
return x[0] == "-"
def gap_2nd(x):
return x[1] == "-"
def gap_3rd(x):
return x[2] == "-"
def is_list(x):
return isinstance(x, list)
gaps = cls({"a": "AAAAAAA", "b": "A--A-AA", "c": "AA-----"}, names=["a", "b", "c"])
assert gaps.get_position_indices(gap_1st) == []
assert gaps.get_position_indices(gap_2nd) == [1, 2, 4]
assert gaps.get_position_indices(gap_3rd) == [2, 3, 4, 5, 6]
assert gaps.get_position_indices(is_list) == [0, 1, 2, 3, 4, 5, 6]
# should be able to negate
assert gaps.get_position_indices(gap_2nd, negate=True) == [0, 3, 5, 6]
assert gaps.get_position_indices(gap_1st, negate=True) == [0, 1, 2, 3, 4, 5, 6]
assert gaps.get_position_indices(is_list, negate=True) == []
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_positions(cls):
"""positions property should iterate over positions, using self.names"""
r = cls({"a": "AAAAAA", "b": "AAA---", "c": "AAAA--"})
r.names = ["a", "b", "c"]
expect = [list(v) for v in ("AAA", "AAA", "AAA", "A-A", "A--", "A--")]
assert list(r.positions) == expect
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment, SequenceCollection))
def test_set_repr_policy_valid_input(cls):
"""repr_policy should be set to new values"""
seqs = cls({"a": "AAAAA", "b": "AAA--"})
seqs.set_repr_policy(num_seqs=5, num_pos=40, ref_name="a", wrap=10)
assert seqs._repr_policy == dict(num_seqs=5, num_pos=40, ref_name="a", wrap=10)
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_set_repr_policy_valid_input_slices(cls):
"""repr_policy should be set to new values"""
seqs = cls({"a": "AAAAA", "b": "AAA--"})
seqs.set_repr_policy(num_seqs=5, num_pos=40, ref_name="a", wrap=10)
# should persist in slicing
sliced = seqs[:2]
assert sliced._repr_policy == dict(num_seqs=5, num_pos=40, ref_name="a", wrap=10)
def test_array_align_error_with_mixed_length():
data = dict(s1="ACGG", s2="A-G")
with pytest.raises(ValueError, match=".* not all the same length.*"):
make_aligned_seqs(data=data)
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment, SequenceCollection))
def test_add_seqs(cls):
"""add_seqs should return an alignment with the new sequences appended or inserted"""
data = [("name1", "AAA"), ("name2", "AAA"), ("name3", "AAA"), ("name4", "AAA")]
data1 = [("name1", "AAA"), ("name2", "AAA")]
data2 = [("name3", "AAA"), ("name4", "AAA")]
data3 = [("name5", "BBB"), ("name6", "CCC")]
aln = cls(data)
aln3 = cls(data3)
out_aln = aln.add_seqs(aln3)
# test append at the end
assert str(out_aln) == str(cls(data + data3))
out_aln = aln.add_seqs(aln3, before_name="name3")
assert str(out_aln) == str(cls(data1 + data3 + data2))
# test insert before
out_aln = aln.add_seqs(aln3, after_name="name2")
assert str(out_aln) == str(cls(data1 + data3 + data2)) # test insert after
out_aln = aln.add_seqs(aln3, before_name="name1")
# test if insert before first seq works
assert str(out_aln) == str(cls(data3 + data))
out_aln = aln.add_seqs(aln3, after_name="name4")
# test if insert after last seq works
assert str(out_aln) == str(cls(data + data3))
with pytest.raises(ValueError):
# wrong after/before name
aln.add_seqs(aln3, before_name="name5")
with pytest.raises(ValueError):
# wrong after/before name
aln.add_seqs(aln3, after_name="name5")
if isinstance(aln, Alignment) or isinstance(aln, ArrayAlignment):
with pytest.raises((DataError, ValueError)):
aln.add_seqs(aln3 + aln3)
else:
exp = set([seq for name, seq in data])
exp.update([seq + seq for name, seq in data3])
got = set()
for seq in aln.add_seqs(aln3 + aln3).seqs:
got.update([str(seq).strip()])
assert got == exp
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_omit_gap_pos(cls):
"""Alignment omit_gap_pos should return alignment w/o positions of gaps"""
aln = cls({"a": "--A-BC-", "b": "-CB-A--", "c": "--D-EF-"}, names=["a", "b", "c"])
# first, check behavior when we're just acting on the cols (and not
# trying to delete the naughty seqs).
# default should strip out cols that are 100% gaps
result = aln.omit_gap_pos()
assert result.to_dict() == {"a": "-ABC", "b": "CBA-", "c": "-DEF"}
# if allowed_gap_frac is 1, shouldn't delete anything
assert aln.omit_gap_pos(1).to_dict() == {
"a": "--A-BC-",
"b": "-CB-A--",
"c": "--D-EF-",
}
# if allowed_gap_frac is 0, should strip out any cols containing gaps
assert aln.omit_gap_pos(0).to_dict() == {"a": "AB", "b": "BA", "c": "DE"}
# intermediate numbers should work as expected
assert aln.omit_gap_pos(0.4).to_dict() == {"a": "ABC", "b": "BA-", "c": "DEF"}
assert aln.omit_gap_pos(0.7).to_dict() == {"a": "-ABC", "b": "CBA-", "c": "-DEF"}
# when we increase the number of sequences to 6, more differences
# start to appear.
new_aln_data = aln.named_seqs.copy()
new_aln_data["d"] = "-------"
new_aln_data["e"] = "XYZXYZX"
new_aln_data["f"] = "AB-CDEF"
aln = cls(new_aln_data)
# if no gaps are allowed, we get None
assert aln.omit_gap_pos(0) is None
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
def test_no_degenerates(cls):
"""no_degenerates correctly excludes columns containing IUPAC ambiguity codes"""
data = {
"s1": "AAA CCC GGG TTT".replace(" ", ""),
"s2": "CCC GGG T-T AAA".replace(" ", ""),
"s3": "GGR YTT AAA CCC".replace(" ", ""),
}
aln = cls(data=data, moltype=DNA)
# motif length of 1, defaults - no gaps allowed
result = aln.no_degenerates().to_dict()
expect = {
"s1": "AA CC GG TTT".replace(" ", ""),
"s2": "CC GG TT AAA".replace(" ", ""),
"s3": "GG TT AA CCC".replace(" ", ""),
}
assert result == expect
# allow gaps
result = aln.no_degenerates(allow_gap=True).to_dict()
expect = {
"s1": "AA CC GGG TTT".replace(" ", ""),
"s2": "CC GG T-T AAA".replace(" ", ""),
"s3": "GG TT AAA CCC".replace(" ", ""),
}
assert result == expect
# motif length of 3, defaults - no gaps allowed
result = aln.no_degenerates(motif_length=3, allow_gap=False).to_dict()
expect = {
"s1": "TTT".replace(" ", ""),
"s2": "AAA".replace(" ", ""),
"s3": "CCC".replace(" ", ""),
}
assert result == expect
# allow gaps
result = aln.no_degenerates(motif_length=3, allow_gap=True).to_dict()
expect = {
"s1": "GGG TTT".replace(" ", ""),
"s2": "T-T AAA".replace(" ", ""),
"s3": "AAA CCC".replace(" ", ""),
}
assert result == expect
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
@pytest.mark.parametrize("moltype", ("bytes", "text"))
def test_no_degenerates_invalid_moltype(cls, moltype):
# raises ValueError if a default moltype -- with no
# degen characters -- is used
data = {
"s1": "AAA CCC GGG TTT".replace(" ", ""),
"s2": "CCC GGG T-T AAA".replace(" ", ""),
"s3": "GGR YTT AAA CCC".replace(" ", ""),
}
aln = cls(data=data, moltype=moltype)
with pytest.raises(ValueError):
aln.no_degenerates()
@pytest.mark.parametrize("cls", (Alignment, ArrayAlignment))
@pytest.mark.parametrize("calc", ("hamming", None))
def test_quick_tree(cls, calc, brca1_data):
"""quick tree method returns tree"""
aln = cls(brca1_data, moltype=DNA)[:100]
aln = aln.take_seqs(["Human", "Rhesus", "HowlerMon", "Galago", "Mouse"])
kwargs = dict(show_progress=False)
if calc:
kwargs["calc"] = calc
# bootstrap
tree = aln.quick_tree(bootstrap=2, **kwargs)
assert set(tree.get_tip_names()) == set(aln.names)
types = {
type(float(edge.params["support"]))
for edge in tree.preorder()
if not edge.is_root()
}
assert types == {float}
@pytest.mark.parametrize("raw", ("-AAAGGGGGAACCCT", "AAAGGGGGAACCCT"))
def test_slice_aligned(raw):
imap, seq = DNA.make_seq(raw, name="x").parse_out_gaps()
al = Aligned(imap, seq)
sliced = al[:-3]
assert str(sliced) == raw[:-3]
def test_slice_aligned_featuremap_allgap():
from cogent3.core.location import FeatureMap, LostSpan
imap, seq = DNA.make_seq("AAAGGGGGAACCCT", name="x").parse_out_gaps()
al = Aligned(imap, seq)
fmap = FeatureMap(spans=[LostSpan(4)], parent_length=0)
sliced = al[fmap]
assert not sliced
def test_slice_aligned_featuremap_multi_spans():
from cogent3.core.location import FeatureMap
# 1111111
# 01234567890123456
# *** ** ***
raw_seq = "AAAGG--GGG-AACCCT"
# 01234 567 890123
# 1111
imap, seq = DNA.make_seq(raw_seq, name="x").parse_out_gaps()
al = Aligned(imap, seq)
fmap = FeatureMap.from_locations(
locations=[(1, 4), (7, 9), (13, 16)], parent_length=len(raw_seq)
)
sliced = al[fmap]
assert str(sliced) == "AAGGGCCC"
def test_sequence_collection_repr():
data = {
"ENSMUSG00000056468": "GCCAGGGGGAAAAGGGAGAA",
"ENSMUSG00000039616": "GCCCTTCAAATTT",
}
seqs = SequenceCollection(data=data, moltype=DNA)
assert (
repr(seqs)
== "2x (ENSMUSG00000039616[GCCCTTCAAA...], ENSMUSG00000056468[GCCAGGGGGA...]) dna seqcollection"
)
data = {
"ENSMUSG00000039616": "GCCCTTCAAATTT",
"ENSMUSG00000056468": "GCCAGGGGGAAAAGGGAGAA",
}
seqs = SequenceCollection(data=data, moltype=DNA)
assert (
repr(seqs)
== "2x (ENSMUSG00000039616[GCCCTTCAAA...], ENSMUSG00000056468[GCCAGGGGGA...]) dna seqcollection"
)
data = {
"a": "TCGAT",
}
seqs = SequenceCollection(data=data, moltype=DNA)
assert repr(seqs) == "1x (a[TCGAT]) dna seqcollection"
data = {
"a": "TCGAT" * 2,
}
seqs = SequenceCollection(data=data, moltype=DNA)
assert repr(seqs) == "1x (a[TCGATTCGAT]) dna seqcollection"
data = {
"a": "A" * 11,
"b": "B" * 3,
"c": "C" * 3,
"d": "D" * 11,
"e": "E" * 8,
}
seqs = SequenceCollection(data=data, moltype=ASCII)
assert repr(seqs) == "5x (b[BBB], ..., d[DDDDDDDDDD...]) text seqcollection"
@pytest.mark.parametrize("cls", (ArrayAlignment, Alignment))
def test_alignment_repr(cls):
data = {
"ENSMUSG00000056468": "GCCAGGGGGAAAA",
"ENSMUSG00000039616": "GCCCTTCAAATTT",
}
seqs = cls(data=data, moltype=DNA)
assert (
repr(seqs)
== "2 x 13 dna alignment: ENSMUSG00000056468[GCCAGGGGGA...], ENSMUSG00000039616[GCCCTTCAAA...]"
)
data = {
"ENSMUSG00000039616": "GCCCTTCAAATTT",
"ENSMUSG00000056468": "GCCAGGGGGAAAA",
}
seqs = cls(data=data, moltype=DNA)
assert (
repr(seqs)
== "2 x 13 dna alignment: ENSMUSG00000039616[GCCCTTCAAA...], ENSMUSG00000056468[GCCAGGGGGA...]"
)
data = {
"a": "TCGAT",
}
seqs = cls(data=data, moltype=DNA)
assert repr(seqs) == "1 x 5 dna alignment: a[TCGAT]"
data = {
"a": "TCGAT" * 2,
}
seqs = cls(data=data, moltype=DNA)
assert repr(seqs) == "1 x 10 dna alignment: a[TCGATTCGAT]"
data = {
"a": "A" * 11,
"b": "B" * 11,
"c": "C" * 11,
"d": "D" * 11,
"e": "E" * 11,
}
seqs = cls(data=data, moltype=ASCII)
assert (
repr(seqs)
== "5 x 11 text alignment: a[AAAAAAAAAA...], b[BBBBBBBBBB...], c[CCCCCCCCCC...], ..."
)
@pytest.mark.parametrize("cls", (ArrayAlignment, Alignment, SequenceCollection))
def test_empty_data(cls):
with pytest.raises(ValueError) as e:
_ = cls(())
assert str(e.value) == f"{cls.__name__} must take at least one sequence."
|