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
|
# cython: embedsignature=True
# cython: profile=True
###############################################################################
###############################################################################
# Cython wrapper for SAM/BAM/CRAM files based on htslib
###############################################################################
# The principal classes defined in this module are:
#
# class AlignedSegment an aligned segment (read)
#
# class PileupColumn a collection of segments (PileupRead) aligned to
# a particular genomic position.
#
# class PileupRead an AlignedSegment aligned to a particular genomic
# position. Contains additional attributes with respect
# to this.
#
# Additionally this module defines numerous additional classes that are part
# of the internal API. These are:
#
# Various iterator classes to iterate over alignments in sequential (IteratorRow)
# or in a stacked fashion (IteratorColumn):
#
# class IteratorRow
# class IteratorRowRegion
# class IteratorRowHead
# class IteratorRowAll
# class IteratorRowAllRefs
# class IteratorRowSelection
#
###############################################################################
#
# The MIT License
#
# Copyright (c) 2015 Andreas Heger
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
###############################################################################
import re
import array
import ctypes
import struct
cimport cython
from cpython cimport array as c_array
from cpython.version cimport PY_MAJOR_VERSION
from cpython cimport PyErr_SetString, PyBytes_FromStringAndSize
from libc.string cimport strchr
from cpython cimport array as c_array
from pysam.libcutils cimport force_bytes, force_str, \
charptr_to_str, charptr_to_bytes
from pysam.libcutils cimport qualities_to_qualitystring, qualitystring_to_array, \
array_to_qualitystring
# Constants for binary tag conversion
cdef char * htslib_types = 'cCsSiIf'
cdef char * parray_types = 'bBhHiIf'
# translation tables
# cigar code to character and vice versa
cdef char* CODE2CIGAR= "MIDNSHP=XB"
cdef int NCIGAR_CODES = 10
if PY_MAJOR_VERSION >= 3:
CIGAR2CODE = dict([y, x] for x, y in enumerate(CODE2CIGAR))
else:
CIGAR2CODE = dict([ord(y), x] for x, y in enumerate(CODE2CIGAR))
CIGAR_REGEX = re.compile("(\d+)([MIDNSHP=XB])")
#####################################################################
# C multiplication with wrapping around
cdef inline uint32_t c_mul(uint32_t a, uint32_t b):
return (a * b) & 0xffffffff
#####################################################################
# typecode guessing
cdef inline char map_typecode_htslib_to_python(uint8_t s):
"""map an htslib typecode to the corresponding python typecode
to be used in the struct or array modules."""
# map type from htslib to python array
cdef char * f = strchr(htslib_types, s)
if f == NULL:
return 0
return parray_types[f - htslib_types]
cdef inline uint8_t map_typecode_python_to_htslib(char s):
"""determine value type from type code of array"""
cdef char * f = strchr(parray_types, s)
if f == NULL:
return 0
return htslib_types[f - parray_types]
# optional tag data manipulation
cdef convert_binary_tag(uint8_t * tag):
"""return bytesize, number of values and array of values
in aux_data memory location pointed to by tag."""
cdef uint8_t auxtype
cdef uint8_t byte_size
cdef int32_t nvalues
# get byte size
auxtype = tag[0]
byte_size = aux_type2size(auxtype)
tag += 1
# get number of values in array
nvalues = (<int32_t*>tag)[0]
tag += 4
# define python array
cdef c_array.array c_values = array.array(
chr(map_typecode_htslib_to_python(auxtype)))
c_array.resize(c_values, nvalues)
# copy data
memcpy(c_values.data.as_voidptr, <uint8_t*>tag, nvalues * byte_size)
# no need to check for endian-ness as bam1_core_t fields
# and aux_data are in host endian-ness. See sam.c and calls
# to swap_data
return byte_size, nvalues, c_values
cdef inline uint8_t get_value_code(value, value_type=None):
'''guess type code for a *value*. If *value_type* is None,
the type code will be inferred based on the Python type of
*value*'''
cdef uint8_t typecode
cdef char * _char_type
if value_type is None:
if isinstance(value, int):
typecode = 'i'
elif isinstance(value, float):
typecode = 'd'
elif isinstance(value, str):
typecode = 'Z'
elif isinstance(value, bytes):
typecode = 'Z'
elif isinstance(value, array.array) or \
isinstance(value, list) or \
isinstance(value, tuple):
typecode = 'B'
else:
return 0
else:
if value_type not in 'Zidf':
return 0
value_type = force_bytes(value_type)
_char_type = value_type
typecode = (<uint8_t*>_char_type)[0]
return typecode
cdef inline bytes getTypecode(value, maximum_value=None):
'''returns the value typecode of a value.
If max is specified, the approprite type is
returned for a range where value is the minimum.
'''
if maximum_value is None:
maximum_value = value
cdef bytes valuetype
t = type(value)
if t is float:
valuetype = b'f'
elif t is int:
# signed ints
if value < 0:
if value >= -128 and maximum_value < 128:
valuetype = b'c'
elif value >= -32768 and maximum_value < 32768:
valuetype = b's'
elif value < -2147483648 or maximum_value >= 2147483648:
raise ValueError(
"at least one signed integer out of range of "
"BAM/SAM specification")
else:
valuetype = b'i'
# unsigned ints
else:
if maximum_value < 256:
valuetype = b'C'
elif maximum_value < 65536:
valuetype = b'S'
elif maximum_value >= 4294967296:
raise ValueError(
"at least one integer out of range of BAM/SAM specification")
else:
valuetype = b'I'
else:
# Note: hex strings (H) are not supported yet
if t is not bytes:
value = value.encode('ascii')
if len(value) == 1:
valuetype = b'A'
else:
valuetype = b'Z'
return valuetype
cdef inline packTags(tags):
"""pack a list of tags. Each tag is a tuple of (tag, tuple).
Values are packed into the most space efficient data structure
possible unless the tag contains a third field with the typecode.
Returns a format string and the associated list of arguments
to be used in a call to struct.pack_into.
"""
fmts, args = ["<"], []
cdef char array_typecode
datatype2format = {
b'c': ('b', 1),
b'C': ('B', 1),
b's': ('h', 2),
b'S': ('H', 2),
b'i': ('i', 4),
b'I': ('I', 4),
b'f': ('f', 4),
b'A': ('c', 1)}
for tag in tags:
if len(tag) == 2:
pytag, value = tag
valuetype = None
elif len(tag) == 3:
pytag, value, valuetype = tag
else:
raise ValueError("malformatted tag: %s" % str(tag))
pytag = force_bytes(pytag)
valuetype = force_bytes(valuetype)
t = type(value)
if t is tuple or t is list:
# binary tags from tuples or lists
if valuetype is None:
# automatically determine value type - first value
# determines type. If there is a mix of types, the
# result is undefined.
valuetype = getTypecode(min(value), max(value))
if valuetype not in datatype2format:
raise ValueError("invalid value type '%s'" % valuetype)
datafmt = "2sccI%i%s" % (len(value), datatype2format[valuetype][0])
args.extend([pytag[:2],
b"B",
valuetype,
len(value)] + list(value))
elif isinstance(value, array.array):
# binary tags from arrays
if valuetype is None:
array_typecode = map_typecode_python_to_htslib(ord(value.typecode))
if array_typecode == 0:
raise ValueError("unsupported type code '{}'"
.format(value.typecode))
valuetype = force_bytes(chr(array_typecode))
if valuetype not in datatype2format:
raise ValueError("invalid value type '%s' (%s)" %
(valuetype, type(valuetype)))
# use array.tostring() to retrieve byte representation and
# save as bytes
datafmt = "2sccI%is" % (len(value) * datatype2format[valuetype][1])
args.extend([pytag[:2],
b"B",
valuetype,
len(value),
force_bytes(value.tostring())])
else:
if valuetype is None:
valuetype = getTypecode(value)
if valuetype in b"AZ":
value = force_bytes(value)
if valuetype == b"Z":
datafmt = "2sc%is" % (len(value)+1)
else:
datafmt = "2sc%s" % datatype2format[valuetype][0]
args.extend([pytag[:2],
valuetype,
value])
fmts.append(datafmt)
return "".join(fmts), args
cdef inline int32_t calculateQueryLength(bam1_t * src):
"""return query length computed from CIGAR alignment.
Return 0 if there is no CIGAR alignment.
"""
cdef uint32_t * cigar_p = pysam_bam_get_cigar(src)
if cigar_p == NULL:
return 0
cdef uint32_t k, qpos
cdef int op
qpos = 0
for k from 0 <= k < pysam_get_n_cigar(src):
op = cigar_p[k] & BAM_CIGAR_MASK
if op == BAM_CMATCH or \
op == BAM_CINS or \
op == BAM_CSOFT_CLIP or \
op == BAM_CHARD_CLIP or \
op == BAM_CEQUAL or \
op == BAM_CDIFF:
qpos += cigar_p[k] >> BAM_CIGAR_SHIFT
return qpos
cdef inline int32_t getQueryStart(bam1_t *src) except -1:
cdef uint32_t * cigar_p
cdef uint32_t k, op
cdef uint32_t start_offset = 0
if pysam_get_n_cigar(src):
cigar_p = pysam_bam_get_cigar(src);
for k from 0 <= k < pysam_get_n_cigar(src):
op = cigar_p[k] & BAM_CIGAR_MASK
if op == BAM_CHARD_CLIP:
if start_offset != 0 and start_offset != src.core.l_qseq:
PyErr_SetString(ValueError, 'Invalid clipping in CIGAR string')
return -1
elif op == BAM_CSOFT_CLIP:
start_offset += cigar_p[k] >> BAM_CIGAR_SHIFT
else:
break
return start_offset
cdef inline int32_t getQueryEnd(bam1_t *src) except -1:
cdef uint32_t * cigar_p
cdef uint32_t k, op
cdef uint32_t end_offset = src.core.l_qseq
# if there is no sequence, compute length from cigar string
if end_offset == 0:
end_offset = calculateQueryLength(src)
# walk backwards in cigar string
if pysam_get_n_cigar(src) > 1:
cigar_p = pysam_bam_get_cigar(src);
for k from pysam_get_n_cigar(src) > k >= 1:
op = cigar_p[k] & BAM_CIGAR_MASK
if op == BAM_CHARD_CLIP:
if end_offset != 0 and end_offset != src.core.l_qseq:
PyErr_SetString(ValueError,
'Invalid clipping in CIGAR string')
return -1
elif op == BAM_CSOFT_CLIP:
end_offset -= cigar_p[k] >> BAM_CIGAR_SHIFT
else:
break
return end_offset
cdef inline bytes getSequenceInRange(bam1_t *src,
uint32_t start,
uint32_t end):
"""return python string of the sequence in a bam1_t object.
"""
cdef uint8_t * p
cdef uint32_t k
cdef char * s
if not src.core.l_qseq:
return None
seq = PyBytes_FromStringAndSize(NULL, end - start)
s = <char*>seq
p = pysam_bam_get_seq(src)
for k from start <= k < end:
# equivalent to seq_nt16_str[bam1_seqi(s, i)] (see bam.c)
# note: do not use string literal as it will be a python string
s[k-start] = seq_nt16_str[p[k/2] >> 4 * (1 - k%2) & 0xf]
return charptr_to_bytes(seq)
cdef inline object getQualitiesInRange(bam1_t *src,
uint32_t start,
uint32_t end):
"""return python array of quality values from a bam1_t object"""
cdef uint8_t * p
cdef uint32_t k
p = pysam_bam_get_qual(src)
if p[0] == 0xff:
return None
# 'B': unsigned char
cdef c_array.array result = array.array('B', [0])
c_array.resize(result, end - start)
# copy data
memcpy(result.data.as_voidptr, <void*>&p[start], end - start)
return result
#####################################################################
## private factory methods
cdef class AlignedSegment
cdef makeAlignedSegment(bam1_t * src, AlignmentFile alignment_file):
'''return an AlignedSegment object constructed from `src`'''
# note that the following does not call __init__
cdef AlignedSegment dest = AlignedSegment.__new__(AlignedSegment)
dest._delegate = bam_dup1(src)
dest._alignment_file = alignment_file
return dest
cdef class PileupColumn
cdef makePileupColumn(bam_pileup1_t ** plp, int tid, int pos,
int n_pu, AlignmentFile alignment_file):
'''return a PileupColumn object constructed from pileup in `plp` and
setting additional attributes.
'''
# note that the following does not call __init__
cdef PileupColumn dest = PileupColumn.__new__(PileupColumn)
dest._alignment_file = alignment_file
dest.plp = plp
dest.tid = tid
dest.pos = pos
dest.n_pu = n_pu
return dest
cdef class PileupRead
cdef inline makePileupRead(bam_pileup1_t * src, AlignmentFile alignment_file):
'''return a PileupRead object construted from a bam_pileup1_t * object.'''
cdef PileupRead dest = PileupRead.__new__(PileupRead)
dest._alignment = makeAlignedSegment(src.b, alignment_file)
dest._qpos = src.qpos
dest._indel = src.indel
dest._level = src.level
dest._is_del = src.is_del
dest._is_head = src.is_head
dest._is_tail = src.is_tail
dest._is_refskip = src.is_refskip
return dest
cdef inline uint32_t get_alignment_length(bam1_t * src):
cdef int k = 0
cdef uint32_t l = 0
if src == NULL:
return 0
cdef uint32_t * cigar_p = bam_get_cigar(src)
if cigar_p == NULL:
return 0
cdef int op
cdef int n = pysam_get_n_cigar(src)
for k from 0 <= k < n:
op = cigar_p[k] & BAM_CIGAR_MASK
if op == BAM_CSOFT_CLIP or op == BAM_CHARD_CLIP:
continue
l += cigar_p[k] >> BAM_CIGAR_SHIFT
return l
# TODO: avoid string copying for getSequenceInRange, reconstituneSequenceFromMD, ...
cdef inline bytes build_alignment_sequence(bam1_t * src):
"""return expanded sequence from MD tag.
The sequence includes substitutions and both insertions in the
reference as well as deletions to the reference sequence. Combine
with the cigar string to reconstitute the query or the reference
sequence.
Positions corresponding to `N` (skipped region from the reference)
in the CIGAR string will not appear in the returned sequence. The
MD should correspondingly not contain these. Thus proper tags are::
Deletion from the reference: cigar=5M1D5M MD=5^C5
Skipped region from reference: cigar=5M1N5M MD=10
Returns
-------
None, if no MD tag is present.
"""
if src == NULL:
return None
cdef uint32_t start = getQueryStart(src)
cdef uint32_t end = getQueryEnd(src)
# get read sequence, taking into account soft-clipping
r = getSequenceInRange(src, start, end)
cdef char * read_sequence = r
cdef uint32_t * cigar_p = pysam_bam_get_cigar(src)
if cigar_p == NULL:
return None
cdef uint32_t r_idx = 0
cdef int op
cdef uint32_t k, i, l, x
cdef int nmatches = 0
cdef int s_idx = 0
cdef uint32_t max_len = get_alignment_length(src)
if max_len == 0:
raise ValueError("could not determine alignment length")
cdef char * s = <char*>calloc(max_len + 1, sizeof(char))
if s == NULL:
raise ValueError(
"could not allocated sequence of length %i" % max_len)
for k from 0 <= k < pysam_get_n_cigar(src):
op = cigar_p[k] & BAM_CIGAR_MASK
l = cigar_p[k] >> BAM_CIGAR_SHIFT
if op == BAM_CMATCH or op == BAM_CEQUAL or op == BAM_CDIFF:
for i from 0 <= i < l:
s[s_idx] = read_sequence[r_idx]
r_idx += 1
s_idx += 1
elif op == BAM_CDEL:
for i from 0 <= i < l:
s[s_idx] = '-'
s_idx += 1
elif op == BAM_CREF_SKIP:
pass
elif op == BAM_CINS:
for i from 0 <= i < l:
# encode insertions into reference as lowercase
s[s_idx] = read_sequence[r_idx] + 32
r_idx += 1
s_idx += 1
elif op == BAM_CSOFT_CLIP:
pass
elif op == BAM_CHARD_CLIP:
pass # advances neither
elif op == BAM_CPAD:
raise NotImplementedError(
"Padding (BAM_CPAD, 6) is currently not supported. "
"Please implement. Sorry about that.")
cdef uint8_t * md_tag_ptr = bam_aux_get(src, "MD")
if md_tag_ptr == NULL:
seq = PyBytes_FromStringAndSize(s, s_idx)
free(s)
return seq
cdef char * md_tag = <char*>bam_aux2Z(md_tag_ptr)
cdef int md_idx = 0
s_idx = 0
while md_tag[md_idx] != 0:
# c is numerical
if md_tag[md_idx] >= 48 and md_tag[md_idx] <= 57:
nmatches *= 10
nmatches += md_tag[md_idx] - 48
md_idx += 1
continue
else:
# save matches up to this point, skipping insertions
for x from 0 <= x < nmatches:
while s[s_idx] >= 'a':
s_idx += 1
s_idx += 1
while s[s_idx] >= 'a':
s_idx += 1
r_idx += nmatches
nmatches = 0
if md_tag[md_idx] == '^':
md_idx += 1
while md_tag[md_idx] >= 65 and md_tag[md_idx] <= 90:
assert s[s_idx] == '-'
s[s_idx] = md_tag[md_idx]
s_idx += 1
md_idx += 1
else:
# save mismatch and change to lower case
s[s_idx] = md_tag[md_idx] + 32
s_idx += 1
r_idx += 1
md_idx += 1
# save matches up to this point, skipping insertions
for x from 0 <= x < nmatches:
while s[s_idx] >= 'a':
s_idx += 1
s_idx += 1
while s[s_idx] >= 'a':
s_idx += 1
seq = PyBytes_FromStringAndSize(s, s_idx)
free(s)
return seq
cdef class AlignedSegment:
'''Class representing an aligned segment.
This class stores a handle to the samtools C-structure representing
an aligned read. Member read access is forwarded to the C-structure
and converted into python objects. This implementation should be fast,
as only the data needed is converted.
For write access, the C-structure is updated in-place. This is
not the most efficient way to build BAM entries, as the variable
length data is concatenated and thus needs to be resized if
a field is updated. Furthermore, the BAM entry might be
in an inconsistent state.
One issue to look out for is that the sequence should always
be set *before* the quality scores. Setting the sequence will
also erase any quality scores that were set previously.
'''
# Now only called when instances are created from Python
def __init__(self):
# see bam_init1
self._delegate = <bam1_t*>calloc(1, sizeof(bam1_t))
# allocate some memory. If size is 0, calloc does not return a
# pointer that can be passed to free() so allocate 40 bytes
# for a new read
self._delegate.m_data = 40
self._delegate.data = <uint8_t *>calloc(
self._delegate.m_data, 1)
self._delegate.l_data = 0
# set some data to make read approximately legit.
# Note, SAM writing fails with q_name of length 0
self._delegate.core.l_qname = 0
self._delegate.core.tid = -1
self._delegate.core.pos = -1
self._delegate.core.mtid = -1
self._delegate.core.mpos = -1
# caching for selected fields
self.cache_query_qualities = None
self.cache_query_alignment_qualities = None
self.cache_query_sequence = None
self.cache_query_alignment_sequence = None
def __dealloc__(self):
bam_destroy1(self._delegate)
def __str__(self):
"""return string representation of alignment.
The representation is an approximate :term:`SAM` format, because
an aligned read might not be associated with a :term:`AlignmentFile`.
As a result :term:`tid` is shown instead of the reference name.
Similarly, the tags field is returned in its parsed state.
To get a valid SAM record, use :meth:`tostring`.
"""
# sam-parsing is done in sam.c/bam_format1_core which
# requires a valid header.
return "\t".join(map(str, (self.query_name,
self.flag,
self.reference_id,
self.reference_start,
self.mapping_quality,
self.cigarstring,
self.next_reference_id,
self.next_reference_start,
self.query_alignment_length,
self.query_sequence,
self.query_qualities,
self.tags)))
def __copy__(self):
return makeAlignedSegment(self._delegate, self._alignment_file)
def __deepcopy__(self, memo):
return makeAlignedSegment(self._delegate, self._alignment_file)
def compare(self, AlignedSegment other):
'''return -1,0,1, if contents in this are binary
<,=,> to *other*
'''
cdef int retval, x
cdef bam1_t *t
cdef bam1_t *o
t = self._delegate
o = other._delegate
# uncomment for debugging purposes
# cdef unsigned char * oo, * tt
# tt = <unsigned char*>(&t.core)
# oo = <unsigned char*>(&o.core)
# for x from 0 <= x < sizeof( bam1_core_t): print x, tt[x], oo[x]
# tt = <unsigned char*>(t.data)
# oo = <unsigned char*>(o.data)
# for x from 0 <= x < max(t.l_data, o.l_data): print x, tt[x], oo[x], chr(tt[x]), chr(oo[x])
# Fast-path test for object identity
if t == o:
return 0
retval = memcmp(&t.core, &o.core, sizeof(bam1_core_t))
if retval:
return retval
# cmp(t.l_data, o.l_data)
retval = (t.l_data > o.l_data) - (t.l_data < o.l_data)
if retval:
return retval
return memcmp(t.data, o.data, t.l_data)
def __richcmp__(self, AlignedSegment other, int op):
if op == 2: # == operator
return self.compare(other) == 0
elif op == 3: # != operator
return self.compare(other) != 0
else:
return NotImplemented
def __hash__(self):
cdef bam1_t * src = self._delegate
cdef int x
# see http://effbot.org/zone/python-hash.htm
cdef uint8_t * c = <uint8_t *>&src.core
cdef uint32_t hash_value = c[0]
for x from 1 <= x < sizeof(bam1_core_t):
hash_value = c_mul(hash_value, 1000003) ^ c[x]
c = <uint8_t *>src.data
for x from 0 <= x < src.l_data:
hash_value = c_mul(hash_value, 1000003) ^ c[x]
return hash_value
cpdef tostring(self, AlignmentFile_t htsfile):
"""returns a string representation of the aligned segment.
The output format is valid SAM format.
Parameters
----------
htsfile -- AlignmentFile object to map numerical
identifiers to chromosome names.
"""
cdef int n_targets = htsfile.header.n_targets
if self._delegate.core.tid >= n_targets \
or self._delegate.core.mtid >= n_targets:
raise ValueError('htsfile does not match aligned segment')
cdef kstring_t line
line.l = line.m = 0
line.s = NULL
if sam_format1(htsfile.header, self._delegate, &line) < 0:
if line.m:
free(line.s)
raise ValueError('sam_format failed')
ret = force_str(line.s[:line.l])
if line.m:
free(line.s)
return ret
########################################################
## Basic attributes in order of appearance in SAM format
property query_name:
"""the query template name (None if not present)"""
def __get__(self):
cdef bam1_t * src
src = self._delegate
if pysam_get_l_qname(src) == 0:
return None
return charptr_to_str(<char *>pysam_bam_get_qname(src))
def __set__(self, qname):
if qname is None or len(qname) == 0:
return
if len(qname) >= 255:
raise ValueError("query length out of range {} > 254".format(
len(qname)))
qname = force_bytes(qname)
cdef bam1_t * src
cdef int l
cdef char * p
src = self._delegate
p = pysam_bam_get_qname(src)
# the qname is \0 terminated
l = len(qname) + 1
pysam_bam_update(src,
pysam_get_l_qname(src),
l,
<uint8_t*>p)
pysam_set_l_qname(src, l)
# re-acquire pointer to location in memory
# as it might have moved
p = pysam_bam_get_qname(src)
strncpy(p, qname, l)
property flag:
"""properties flag"""
def __get__(self):
return pysam_get_flag(self._delegate)
def __set__(self, flag):
pysam_set_flag(self._delegate, flag)
property reference_name:
""":term:`reference` name (None if no AlignmentFile is associated)"""
def __get__(self):
if self._alignment_file is not None:
return self._alignment_file.getrname(self._delegate.core.tid)
return None
property reference_id:
""":term:`reference` ID
.. note::
This field contains the index of the reference sequence in
the sequence dictionary. To obtain the name of the
reference sequence, use
:meth:`pysam.AlignmentFile.getrname()`
"""
def __get__(self): return self._delegate.core.tid
def __set__(self, tid): self._delegate.core.tid = tid
property reference_start:
"""0-based leftmost coordinate"""
def __get__(self): return self._delegate.core.pos
def __set__(self, pos):
## setting the position requires updating the "bin" attribute
cdef bam1_t * src
src = self._delegate
src.core.pos = pos
if pysam_get_n_cigar(src):
pysam_set_bin(src,
hts_reg2bin(
src.core.pos,
bam_endpos(src),
14,
5))
else:
pysam_set_bin(src,
hts_reg2bin(
src.core.pos,
src.core.pos + 1,
14,
5))
property mapping_quality:
"""mapping quality"""
def __get__(self):
return pysam_get_qual(self._delegate)
def __set__(self, qual):
pysam_set_qual(self._delegate, qual)
property cigarstring:
'''the :term:`cigar` alignment as a string.
The cigar string is a string of alternating integers
and characters denoting the length and the type of
an operation.
.. note::
The order length,operation is specified in the
SAM format. It is different from the order of
the :attr:`cigar` property.
Returns None if not present.
To unset the cigarstring, assign None or the
empty string.
'''
def __get__(self):
c = self.cigartuples
if c is None:
return None
# reverse order
else:
return "".join([ "%i%c" % (y,CODE2CIGAR[x]) for x,y in c])
def __set__(self, cigar):
if cigar is None or len(cigar) == 0:
self.cigartuples = []
else:
parts = CIGAR_REGEX.findall(cigar)
# reverse order
self.cigartuples = [(CIGAR2CODE[ord(y)], int(x)) for x,y in parts]
# TODO
# property cigar:
# """the cigar alignment"""
property next_reference_id:
"""the :term:`reference` id of the mate/next read."""
def __get__(self): return self._delegate.core.mtid
def __set__(self, mtid):
self._delegate.core.mtid = mtid
property next_reference_name:
""":term:`reference` name of the mate/next read (None if no
AlignmentFile is associated)"""
def __get__(self):
if self._alignment_file is not None:
return self._alignment_file.getrname(self._delegate.core.mtid)
return None
property next_reference_start:
"""the position of the mate/next read."""
def __get__(self):
return self._delegate.core.mpos
def __set__(self, mpos):
self._delegate.core.mpos = mpos
property query_length:
"""the length of the query/read.
This value corresponds to the length of the sequence supplied
in the BAM/SAM file. The length of a query is 0 if there is no
sequence in the BAM/SAM file. In those cases, the read length
can be inferred from the CIGAR alignment, see
:meth:`pysam.AlignedSegment.infer_query_length`.
The length includes soft-clipped bases and is equal to
``len(query_sequence)``.
This property is read-only but can be set by providing a
sequence.
Returns 0 if not available.
"""
def __get__(self):
return self._delegate.core.l_qseq
property template_length:
"""the observed query template length"""
def __get__(self):
return self._delegate.core.isize
def __set__(self, isize):
self._delegate.core.isize = isize
property query_sequence:
"""read sequence bases, including :term:`soft clipped` bases
(None if not present).
Note that assigning to seq will invalidate any quality scores.
Thus, to in-place edit the sequence and quality scores, copies of
the quality scores need to be taken. Consider trimming for example::
q = read.query_qualities
read.query_squence = read.query_sequence[5:10]
read.query_qualities = q[5:10]
The sequence is returned as it is stored in the BAM file. Some mappers
might have stored a reverse complement of the original read
sequence.
"""
def __get__(self):
if self.cache_query_sequence:
return self.cache_query_sequence
cdef bam1_t * src
cdef char * s
src = self._delegate
if src.core.l_qseq == 0:
return None
self.cache_query_sequence = force_str(getSequenceInRange(
src, 0, src.core.l_qseq))
return self.cache_query_sequence
def __set__(self, seq):
# samtools manages sequence and quality length memory together
# if no quality information is present, the first byte says 0xff.
cdef bam1_t * src
cdef uint8_t * p
cdef char * s
cdef int l, k
cdef Py_ssize_t nbytes_new, nbytes_old
if seq == None:
l = 0
else:
l = len(seq)
seq = force_bytes(seq)
src = self._delegate
# as the sequence is stored in half-bytes, the total length (sequence
# plus quality scores) is (l+1)/2 + l
nbytes_new = (l + 1) / 2 + l
nbytes_old = (src.core.l_qseq + 1) / 2 + src.core.l_qseq
# acquire pointer to location in memory
p = pysam_bam_get_seq(src)
src.core.l_qseq = l
# change length of data field
pysam_bam_update(src,
nbytes_old,
nbytes_new,
p)
if l > 0:
# re-acquire pointer to location in memory
# as it might have moved
p = pysam_bam_get_seq(src)
for k from 0 <= k < nbytes_new:
p[k] = 0
# convert to C string
s = seq
for k from 0 <= k < l:
p[k/2] |= seq_nt16_table[<unsigned char>s[k]] << 4 * (1 - k % 2)
# erase qualities
p = pysam_bam_get_qual(src)
p[0] = 0xff
self.cache_query_sequence = force_str(seq)
# clear cached values for quality values
self.cache_query_qualities = None
self.cache_query_alignment_qualities = None
property query_qualities:
"""read sequence base qualities, including :term:`soft
clipped` bases (None if not present).
Quality scores are returned as a python array of unsigned
chars. Note that this is not the ASCII-encoded value typically
seen in FASTQ or SAM formatted files. Thus, no offset of 33
needs to be subtracted.
Note that to set quality scores the sequence has to be set
beforehand as this will determine the expected length of the
quality score array.
This method raises a ValueError if the length of the
quality scores and the sequence are not the same.
"""
def __get__(self):
if self.cache_query_qualities:
return self.cache_query_qualities
cdef bam1_t * src
cdef char * q
src = self._delegate
if src.core.l_qseq == 0:
return None
self.cache_query_qualities = getQualitiesInRange(src, 0, src.core.l_qseq)
return self.cache_query_qualities
def __set__(self, qual):
# note that memory is already allocated via setting the sequence
# hence length match of sequence and quality needs is checked.
cdef bam1_t * src
cdef uint8_t * p
cdef int l
src = self._delegate
p = pysam_bam_get_qual(src)
if qual is None or len(qual) == 0:
# if absent and there is a sequence: set to 0xff
if src.core.l_qseq != 0:
p[0] = 0xff
return
# check for length match
l = len(qual)
if src.core.l_qseq != l:
raise ValueError(
"quality and sequence mismatch: %i != %i" %
(l, src.core.l_qseq))
# create a python array object filling it
# with the quality scores
# NB: should avoid this copying if qual is
# already of the correct type.
cdef c_array.array result = c_array.array('B', qual)
# copy data
memcpy(p, result.data.as_voidptr, l)
# save in cache
self.cache_query_qualities = qual
property bin:
"""properties bin"""
def __get__(self):
return pysam_get_bin(self._delegate)
def __set__(self, bin):
pysam_set_bin(self._delegate, bin)
##########################################################
# Derived simple attributes. These are simple attributes of
# AlignedSegment getting and setting values.
##########################################################
# 1. Flags
##########################################################
property is_paired:
"""true if read is paired in sequencing"""
def __get__(self):
return (self.flag & BAM_FPAIRED) != 0
def __set__(self,val):
pysam_update_flag(self._delegate, val, BAM_FPAIRED)
property is_proper_pair:
"""true if read is mapped in a proper pair"""
def __get__(self):
return (self.flag & BAM_FPROPER_PAIR) != 0
def __set__(self,val):
pysam_update_flag(self._delegate, val, BAM_FPROPER_PAIR)
property is_unmapped:
"""true if read itself is unmapped"""
def __get__(self):
return (self.flag & BAM_FUNMAP) != 0
def __set__(self, val):
pysam_update_flag(self._delegate, val, BAM_FUNMAP)
property mate_is_unmapped:
"""true if the mate is unmapped"""
def __get__(self):
return (self.flag & BAM_FMUNMAP) != 0
def __set__(self,val):
pysam_update_flag(self._delegate, val, BAM_FMUNMAP)
property is_reverse:
"""true if read is mapped to reverse strand"""
def __get__(self):
return (self.flag & BAM_FREVERSE) != 0
def __set__(self,val):
pysam_update_flag(self._delegate, val, BAM_FREVERSE)
property mate_is_reverse:
"""true is read is mapped to reverse strand"""
def __get__(self):
return (self.flag & BAM_FMREVERSE) != 0
def __set__(self,val):
pysam_update_flag(self._delegate, val, BAM_FMREVERSE)
property is_read1:
"""true if this is read1"""
def __get__(self):
return (self.flag & BAM_FREAD1) != 0
def __set__(self,val):
pysam_update_flag(self._delegate, val, BAM_FREAD1)
property is_read2:
"""true if this is read2"""
def __get__(self):
return (self.flag & BAM_FREAD2) != 0
def __set__(self, val):
pysam_update_flag(self._delegate, val, BAM_FREAD2)
property is_secondary:
"""true if not primary alignment"""
def __get__(self):
return (self.flag & BAM_FSECONDARY) != 0
def __set__(self, val):
pysam_update_flag(self._delegate, val, BAM_FSECONDARY)
property is_qcfail:
"""true if QC failure"""
def __get__(self):
return (self.flag & BAM_FQCFAIL) != 0
def __set__(self, val):
pysam_update_flag(self._delegate, val, BAM_FQCFAIL)
property is_duplicate:
"""true if optical or PCR duplicate"""
def __get__(self):
return (self.flag & BAM_FDUP) != 0
def __set__(self, val):
pysam_update_flag(self._delegate, val, BAM_FDUP)
property is_supplementary:
"""true if this is a supplementary alignment"""
def __get__(self):
return (self.flag & BAM_FSUPPLEMENTARY) != 0
def __set__(self, val):
pysam_update_flag(self._delegate, val, BAM_FSUPPLEMENTARY)
# 2. Coordinates and lengths
property reference_end:
'''aligned reference position of the read on the reference genome.
reference_end points to one past the last aligned residue.
Returns None if not available (read is unmapped or no cigar
alignment present).
'''
def __get__(self):
cdef bam1_t * src
src = self._delegate
if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
return None
return bam_endpos(src)
property reference_length:
'''aligned length of the read on the reference genome.
This is equal to `aend - pos`. Returns None if not available.'''
def __get__(self):
cdef bam1_t * src
src = self._delegate
if (self.flag & BAM_FUNMAP) or pysam_get_n_cigar(src) == 0:
return None
return bam_endpos(src) - \
self._delegate.core.pos
property query_alignment_sequence:
"""aligned portion of the read.
This is a substring of :attr:`seq` that excludes flanking
bases that were :term:`soft clipped` (None if not present). It
is equal to ``seq[qstart:qend]``.
SAM/BAM files may include extra flanking bases that are not
part of the alignment. These bases may be the result of the
Smith-Waterman or other algorithms, which may not require
alignments that begin at the first residue or end at the last.
In addition, extra sequencing adapters, multiplex identifiers,
and low-quality bases that were not considered for alignment
may have been retained.
"""
def __get__(self):
if self.cache_query_alignment_sequence:
return self.cache_query_alignment_sequence
cdef bam1_t * src
cdef uint32_t start, end
src = self._delegate
if src.core.l_qseq == 0:
return None
start = getQueryStart(src)
end = getQueryEnd(src)
self.cache_query_alignment_sequence = force_str(
getSequenceInRange(src, start, end))
return self.cache_query_alignment_sequence
property query_alignment_qualities:
"""aligned query sequence quality values (None if not present). These
are the quality values that correspond to :attr:`query`, that
is, they exclude qualities of :term:`soft clipped` bases. This
is equal to ``qual[qstart:qend]``.
Quality scores are returned as a python array of unsigned
chars. Note that this is not the ASCII-encoded value typically
seen in FASTQ or SAM formatted files. Thus, no offset of 33
needs to be subtracted.
This property is read-only.
"""
def __get__(self):
if self.cache_query_alignment_qualities:
return self.cache_query_alignment_qualities
cdef bam1_t * src
cdef uint32_t start, end
src = self._delegate
if src.core.l_qseq == 0:
return None
start = getQueryStart(src)
end = getQueryEnd(src)
self.cache_query_alignment_qualities = \
getQualitiesInRange(src, start, end)
return self.cache_query_alignment_qualities
property query_alignment_start:
"""start index of the aligned query portion of the sequence (0-based,
inclusive).
This the index of the first base in :attr:`seq` that is not
soft-clipped.
"""
def __get__(self):
return getQueryStart(self._delegate)
property query_alignment_end:
"""end index of the aligned query portion of the sequence (0-based,
exclusive)"""
def __get__(self):
return getQueryEnd(self._delegate)
property query_alignment_length:
"""length of the aligned query sequence.
This is equal to :attr:`qend` - :attr:`qstart`"""
def __get__(self):
cdef bam1_t * src
src = self._delegate
return getQueryEnd(src) - getQueryStart(src)
#####################################################
# Computed properties
def get_reference_positions(self, full_length=False):
"""a list of reference positions that this read aligns to.
By default, this method only returns positions in the
reference that are within the alignment. If *full_length* is
set, None values will be included for any soft-clipped or
unaligned positions within the read. The returned list will
thus be of the same length as the read.
"""
cdef uint32_t k, i, pos
cdef int op
cdef uint32_t * cigar_p
cdef bam1_t * src
cdef bint _full = full_length
src = self._delegate
if pysam_get_n_cigar(src) == 0:
return []
result = []
pos = src.core.pos
cigar_p = pysam_bam_get_cigar(src)
for k from 0 <= k < pysam_get_n_cigar(src):
op = cigar_p[k] & BAM_CIGAR_MASK
l = cigar_p[k] >> BAM_CIGAR_SHIFT
if op == BAM_CSOFT_CLIP or op == BAM_CINS:
if _full:
for i from 0 <= i < l:
result.append(None)
elif op == BAM_CMATCH:
for i from pos <= i < pos + l:
result.append(i)
pos += l
elif op == BAM_CDEL or op == BAM_CREF_SKIP:
pos += l
return result
def infer_query_length(self, always=True):
"""inferred read length from CIGAR string.
If *always* is set to True, the read length
will be always inferred. If set to False, the length
of the read sequence will be returned if it is
available.
Returns None if CIGAR string is not present.
"""
cdef uint32_t * cigar_p
cdef bam1_t * src
src = self._delegate
if not always and src.core.l_qseq:
return src.core.l_qseq
return calculateQueryLength(src)
def get_reference_sequence(self):
"""return the reference sequence.
This method requires the MD tag to be set.
"""
cdef uint32_t k, i
cdef int op
cdef bam1_t * src = self._delegate
ref_seq = force_str(build_alignment_sequence(src))
if ref_seq is None:
raise ValueError("MD tag not present")
cdef uint32_t * cigar_p = pysam_bam_get_cigar(src)
cdef uint32_t r_idx = 0
result = []
for k from 0 <= k < pysam_get_n_cigar(src):
op = cigar_p[k] & BAM_CIGAR_MASK
l = cigar_p[k] >> BAM_CIGAR_SHIFT
if op == BAM_CMATCH or op == BAM_CEQUAL or op == BAM_CDIFF:
for i from 0 <= i < l:
result.append(ref_seq[r_idx])
r_idx += 1
elif op == BAM_CDEL:
for i from 0 <= i < l:
result.append(ref_seq[r_idx])
r_idx += 1
elif op == BAM_CREF_SKIP:
pass
elif op == BAM_CINS:
r_idx += l
elif op == BAM_CSOFT_CLIP:
pass
elif op == BAM_CHARD_CLIP:
pass # advances neither
elif op == BAM_CPAD:
raise NotImplementedError(
"Padding (BAM_CPAD, 6) is currently not supported. "
"Please implement. Sorry about that.")
return "".join(result)
def get_aligned_pairs(self, matches_only=False, with_seq=False):
"""a list of aligned read (query) and reference positions.
For inserts, deletions, skipping either query or reference
position may be None.
Padding is currently not supported and leads to an exception.
Parameters
----------
matches_only : bool
If True, only matched bases are returned - no None on either
side.
with_seq : bool
If True, return a third element in the tuple containing the
reference sequence. Substitutions are lower-case. This option
requires an MD tag to be present.
Returns
-------
aligned_pairs : list of tuples
"""
cdef uint32_t k, i, pos, qpos, r_idx, l
cdef int op
cdef uint32_t * cigar_p
cdef bam1_t * src = self._delegate
cdef bint _matches_only = bool(matches_only)
cdef bint _with_seq = bool(with_seq)
# TODO: this method performs no checking and assumes that
# read sequence, cigar and MD tag are consistent.
if _with_seq:
ref_seq = force_str(self.get_reference_sequence())
if ref_seq is None:
raise ValueError("MD tag not present")
r_idx = 0
if pysam_get_n_cigar(src) == 0:
return []
result = []
pos = src.core.pos
qpos = 0
cigar_p = pysam_bam_get_cigar(src)
for k from 0 <= k < pysam_get_n_cigar(src):
op = cigar_p[k] & BAM_CIGAR_MASK
l = cigar_p[k] >> BAM_CIGAR_SHIFT
if op == BAM_CMATCH or op == BAM_CEQUAL or op == BAM_CDIFF:
if _with_seq:
for i from pos <= i < pos + l:
result.append((qpos, i, ref_seq[r_idx]))
r_idx += 1
qpos += 1
else:
for i from pos <= i < pos + l:
result.append((qpos, i))
qpos += 1
pos += l
elif op == BAM_CINS or op == BAM_CSOFT_CLIP:
if not _matches_only:
if _with_seq:
for i from pos <= i < pos + l:
result.append((qpos, None, None))
qpos += 1
else:
for i from pos <= i < pos + l:
result.append((qpos, None))
qpos += 1
else:
qpos += l
elif op == BAM_CDEL:
if not _matches_only:
if _with_seq:
for i from pos <= i < pos + l:
result.append((None, i, ref_seq[r_idx]))
r_idx += 1
else:
for i from pos <= i < pos + l:
result.append((None, i))
pos += l
elif op == BAM_CHARD_CLIP:
pass # advances neither
elif op == BAM_CREF_SKIP:
if not _matches_only:
if _with_seq:
for i from pos <= i < pos + l:
result.append((None, i, None))
else:
for i from pos <= i < pos + l:
result.append((None, i))
pos += l
elif op == BAM_CPAD:
raise NotImplementedError(
"Padding (BAM_CPAD, 6) is currently not supported. "
"Please implement. Sorry about that.")
return result
def get_blocks(self):
""" a list of start and end positions of
aligned gapless blocks.
The start and end positions are in genomic
coordinates.
Blocks are not normalized, i.e. two blocks
might be directly adjacent. This happens if
the two blocks are separated by an insertion
in the read.
"""
cdef uint32_t k, pos, l
cdef int op
cdef uint32_t * cigar_p
cdef bam1_t * src
src = self._delegate
if pysam_get_n_cigar(src) == 0:
return []
result = []
pos = src.core.pos
cigar_p = pysam_bam_get_cigar(src)
l = 0
for k from 0 <= k < pysam_get_n_cigar(src):
op = cigar_p[k] & BAM_CIGAR_MASK
l = cigar_p[k] >> BAM_CIGAR_SHIFT
if op == BAM_CMATCH:
result.append((pos, pos + l))
pos += l
elif op == BAM_CDEL or op == BAM_CREF_SKIP:
pos += l
return result
def get_overlap(self, uint32_t start, uint32_t end):
"""return number of aligned bases of read overlapping the interval
*start* and *end* on the reference sequence.
Return None if cigar alignment is not available.
"""
cdef uint32_t k, i, pos, overlap
cdef int op, o
cdef uint32_t * cigar_p
cdef bam1_t * src
overlap = 0
src = self._delegate
if pysam_get_n_cigar(src) == 0:
return None
pos = src.core.pos
o = 0
cigar_p = pysam_bam_get_cigar(src)
for k from 0 <= k < pysam_get_n_cigar(src):
op = cigar_p[k] & BAM_CIGAR_MASK
l = cigar_p[k] >> BAM_CIGAR_SHIFT
if op == BAM_CMATCH:
o = min( pos + l, end) - max( pos, start )
if o > 0: overlap += o
if op == BAM_CMATCH or op == BAM_CDEL or op == BAM_CREF_SKIP:
pos += l
return overlap
def get_cigar_stats(self):
"""summary of operations in cigar string.
The output order in the array is "MIDNSHP=X" followed by a
field for the NM tag. If the NM tag is not present, this
field will always be 0.
+-----+--------------+-----+
|M |BAM_CMATCH |0 |
+-----+--------------+-----+
|I |BAM_CINS |1 |
+-----+--------------+-----+
|D |BAM_CDEL |2 |
+-----+--------------+-----+
|N |BAM_CREF_SKIP |3 |
+-----+--------------+-----+
|S |BAM_CSOFT_CLIP|4 |
+-----+--------------+-----+
|H |BAM_CHARD_CLIP|5 |
+-----+--------------+-----+
|P |BAM_CPAD |6 |
+-----+--------------+-----+
|= |BAM_CEQUAL |7 |
+-----+--------------+-----+
|X |BAM_CDIFF |8 |
+-----+--------------+-----+
|NM |NM tag |9 |
+-----+--------------+-----+
If no cigar string is present, empty arrays will be returned.
Parameters
----------
Returns
-------
arrays : two arrays. The first contains the nucleotide counts within
each cigar operation, the second contains the number of blocks for
each cigar operation.
"""
cdef int nfields = NCIGAR_CODES + 1
cdef c_array.array base_counts = array.array(
"I",
[0] * nfields)
cdef uint32_t [:] base_view = base_counts
cdef c_array.array block_counts = array.array(
"I",
[0] * nfields)
cdef uint32_t [:] block_view = block_counts
cdef bam1_t * src = self._delegate
cdef int op
cdef uint32_t l
cdef int32_t k
cdef uint32_t * cigar_p = pysam_bam_get_cigar(src)
if cigar_p == NULL:
return None
for k from 0 <= k < pysam_get_n_cigar(src):
op = cigar_p[k] & BAM_CIGAR_MASK
l = cigar_p[k] >> BAM_CIGAR_SHIFT
base_view[op] += l
block_view[op] += 1
cdef uint8_t * v = bam_aux_get(src, 'NM')
if v != NULL:
base_view[nfields - 1] = <int32_t>bam_aux2i(v)
return base_counts, block_counts
#####################################################
## Unsorted as yet
# TODO: capture in CIGAR object
property cigartuples:
"""the :term:`cigar` alignment. The alignment
is returned as a list of tuples of (operation, length).
If the alignment is not present, None is returned.
The operations are:
+-----+--------------+-----+
|M |BAM_CMATCH |0 |
+-----+--------------+-----+
|I |BAM_CINS |1 |
+-----+--------------+-----+
|D |BAM_CDEL |2 |
+-----+--------------+-----+
|N |BAM_CREF_SKIP |3 |
+-----+--------------+-----+
|S |BAM_CSOFT_CLIP|4 |
+-----+--------------+-----+
|H |BAM_CHARD_CLIP|5 |
+-----+--------------+-----+
|P |BAM_CPAD |6 |
+-----+--------------+-----+
|= |BAM_CEQUAL |7 |
+-----+--------------+-----+
|X |BAM_CDIFF |8 |
+-----+--------------+-----+
.. note::
The output is a list of (operation, length) tuples, such as
``[(0, 30)]``.
This is different from the SAM specification and
the :attr:`cigarstring` property, which uses a
(length, operation) order, for example: ``30M``.
To unset the cigar property, assign an empty list
or None.
"""
def __get__(self):
cdef uint32_t * cigar_p
cdef bam1_t * src
cdef uint32_t op, l
cdef int k
src = self._delegate
if pysam_get_n_cigar(src) == 0:
return None
cigar = []
cigar_p = pysam_bam_get_cigar(src);
for k from 0 <= k < pysam_get_n_cigar(src):
op = cigar_p[k] & BAM_CIGAR_MASK
l = cigar_p[k] >> BAM_CIGAR_SHIFT
cigar.append((op, l))
return cigar
def __set__(self, values):
cdef uint32_t * p
cdef bam1_t * src
cdef op, l
cdef int k, ncigar
k = 0
src = self._delegate
# get location of cigar string
p = pysam_bam_get_cigar(src)
# empty values for cigar string
if values is None:
values = []
ncigar = len(values)
# create space for cigar data within src.data
pysam_bam_update(src,
pysam_get_n_cigar(src) * 4,
ncigar * 4,
<uint8_t*>p)
# length is number of cigar operations, not bytes
pysam_set_n_cigar(src, ncigar)
# re-acquire pointer to location in memory
# as it might have moved
p = pysam_bam_get_cigar(src)
# insert cigar operations
for op, l in values:
p[k] = l << BAM_CIGAR_SHIFT | op
k += 1
## setting the cigar string requires updating the bin
pysam_set_bin(src,
hts_reg2bin(
src.core.pos,
bam_endpos(src),
14,
5))
cpdef set_tag(self,
tag,
value,
value_type=None,
replace=True):
"""sets a particular field *tag* to *value* in the optional alignment
section.
*value_type* describes the type of *value* that is to entered
into the alignment record.. It can be set explicitly to one
of the valid one-letter type codes. If unset, an appropriate
type will be chosen automatically.
An existing value of the same *tag* will be overwritten unless
replace is set to False. This is usually not recommened as a
tag may only appear once in the optional alignment section.
If *value* is None, the tag will be deleted.
"""
cdef int value_size
cdef uint8_t * value_ptr
cdef uint8_t *existing_ptr
cdef uint8_t typecode
cdef float float_value
cdef double double_value
cdef int32_t int_value
cdef bam1_t * src = self._delegate
cdef char * _value_type
cdef c_array.array array_value
cdef object buffer
if len(tag) != 2:
raise ValueError('Invalid tag: %s' % tag)
tag = force_bytes(tag)
if replace:
existing_ptr = bam_aux_get(src, tag)
if existing_ptr:
bam_aux_del(src, existing_ptr)
# setting value to None deletes a tag
if value is None:
return
typecode = get_value_code(value, value_type)
if typecode == 0:
raise ValueError("can't guess type or invalid type code specified")
# Not Endian-safe, but then again neither is samtools!
if typecode == 'Z':
value = force_bytes(value)
value_ptr = <uint8_t*><char*>value
value_size = len(value)+1
elif typecode == 'i':
int_value = value
value_ptr = <uint8_t*>&int_value
value_size = sizeof(int32_t)
elif typecode == 'd':
double_value = value
value_ptr = <uint8_t*>&double_value
value_size = sizeof(double)
elif typecode == 'f':
float_value = value
value_ptr = <uint8_t*>&float_value
value_size = sizeof(float)
elif typecode == 'B':
# the following goes through python, needs to be cleaned up
# pack array using struct
if value_type is None:
fmt, args = packTags([(tag, value)])
else:
fmt, args = packTags([(tag, value, value_type)])
# remove tag and type code as set by bam_aux_append
# first four chars of format (<2sc)
fmt = '<' + fmt[4:]
# first two values to pack
args = args[2:]
value_size = struct.calcsize(fmt)
# buffer will be freed when object goes out of scope
buffer = ctypes.create_string_buffer(value_size)
struct.pack_into(fmt, buffer, 0, *args)
# bam_aux_append copies data from value_ptr
bam_aux_append(src,
tag,
typecode,
value_size,
<uint8_t*>buffer.raw)
return
else:
raise ValueError('unsupported value_type in set_option')
bam_aux_append(src,
tag,
typecode,
value_size,
value_ptr)
cpdef has_tag(self, tag):
"""returns true if the optional alignment section
contains a given *tag*."""
cdef uint8_t * v
cdef int nvalues
btag = force_bytes(tag)
v = bam_aux_get(self._delegate, btag)
return v != NULL
cpdef get_tag(self, tag, with_value_type=False):
"""
retrieves data from the optional alignment section
given a two-letter *tag* denoting the field.
The returned value is cast into an appropriate python type.
This method is the fastest way to access the optional
alignment section if only few tags need to be retrieved.
Parameters
----------
tag :
data tag.
with_value_type : Optional[bool]
if set to True, the return value is a tuple of (tag value, type code).
(default False)
Returns
-------
A python object with the value of the `tag`. The type of the
object depends on the data type in the data record.
Raises
------
KeyError
If `tag` is not present, a KeyError is raised.
"""
cdef uint8_t * v
cdef int nvalues
btag = force_bytes(tag)
v = bam_aux_get(self._delegate, btag)
if v == NULL:
raise KeyError("tag '%s' not present" % tag)
if chr(v[0]) == "B":
auxtype = chr(v[0]) + chr(v[1])
else:
auxtype = chr(v[0])
if auxtype == 'c' or auxtype == 'C' or auxtype == 's' or auxtype == 'S':
value = <int>bam_aux2i(v)
elif auxtype == 'i' or auxtype == 'I':
value = <int32_t>bam_aux2i(v)
elif auxtype == 'f' or auxtype == 'F':
value = <float>bam_aux2f(v)
elif auxtype == 'd' or auxtype == 'D':
value = <double>bam_aux2f(v)
elif auxtype == 'A':
# there might a more efficient way
# to convert a char into a string
value = '%c' % <char>bam_aux2A(v)
elif auxtype == 'Z':
value = charptr_to_str(<char*>bam_aux2Z(v))
elif auxtype[0] == 'B':
bytesize, nvalues, values = convert_binary_tag(v + 1)
value = values
else:
raise ValueError("unknown auxiliary type '%s'" % auxtype)
if with_value_type:
return (value, auxtype)
else:
return value
def get_tags(self, with_value_type=False):
"""the fields in the optional aligment section.
Returns a list of all fields in the optional
alignment section. Values are converted to appropriate python
values. For example:
[(NM, 2), (RG, "GJP00TM04")]
If *with_value_type* is set, the value type as encode in
the AlignedSegment record will be returned as well:
[(NM, 2, "i"), (RG, "GJP00TM04", "Z")]
This method will convert all values in the optional alignment
section. When getting only one or few tags, please see
:meth:`get_tag` for a quicker way to achieve this.
"""
cdef char * ctag
cdef bam1_t * src
cdef uint8_t * s
cdef char auxtag[3]
cdef char auxtype
cdef uint8_t byte_size
cdef int32_t nvalues
src = self._delegate
if src.l_data == 0:
return []
s = pysam_bam_get_aux(src)
result = []
auxtag[2] = 0
while s < (src.data + src.l_data):
# get tag
auxtag[0] = s[0]
auxtag[1] = s[1]
s += 2
auxtype = s[0]
if auxtype in ('c', 'C'):
value = <int>bam_aux2i(s)
s += 1
elif auxtype in ('s', 'S'):
value = <int>bam_aux2i(s)
s += 2
elif auxtype in ('i', 'I'):
value = <int32_t>bam_aux2i(s)
s += 4
elif auxtype == 'f':
value = <float>bam_aux2f(s)
s += 4
elif auxtype == 'd':
value = <double>bam_aux2f(s)
s += 8
elif auxtype == 'A':
value = "%c" % <char>bam_aux2A(s)
s += 1
elif auxtype in ('Z', 'H'):
value = charptr_to_str(<char*>bam_aux2Z(s))
# +1 for NULL terminated string
s += len(value) + 1
elif auxtype == 'B':
s += 1
byte_size, nvalues, value = convert_binary_tag(s)
# 5 for 1 char and 1 int
s += 5 + (nvalues * byte_size) - 1
else:
raise KeyError("unknown type '%s'" % auxtype)
s += 1
if with_value_type:
result.append((charptr_to_str(auxtag), value, chr(auxtype)))
else:
result.append((charptr_to_str(auxtag), value))
return result
def set_tags(self, tags):
"""sets the fields in the optional alignmest section with
a list of (tag, value) tuples.
The :term:`value type` of the values is determined from the
python type. Optionally, a type may be given explicitly as
a third value in the tuple, For example:
x.set_tags([(NM, 2, "i"), (RG, "GJP00TM04", "Z")]
This method will not enforce the rule that the same tag may appear
only once in the optional alignment section.
"""
cdef bam1_t * src
cdef uint8_t * s
cdef char * temp
cdef int new_size = 0
cdef int old_size
src = self._delegate
# convert and pack the data
if tags is not None and len(tags) > 0:
fmt, args = packTags(tags)
new_size = struct.calcsize(fmt)
buffer = ctypes.create_string_buffer(new_size)
struct.pack_into(fmt,
buffer,
0,
*args)
# delete the old data and allocate new space.
# If total_size == 0, the aux field will be
# empty
old_size = pysam_bam_get_l_aux(src)
pysam_bam_update(src,
old_size,
new_size,
pysam_bam_get_aux(src))
# copy data only if there is any
if new_size > 0:
# get location of new data
s = pysam_bam_get_aux(src)
# check if there is direct path from buffer.raw to tmp
p = buffer.raw
# create handle to make sure buffer stays alive long
# enough for memcpy, see issue 129
temp = p
memcpy(s, temp, new_size)
########################################################
# Compatibility Accessors
# Functions, properties for compatibility with pysam < 0.8
#
# Several options
# change the factory functions according to API
# * requires code changes throughout, incl passing
# handles to factory functions
# subclass functions and add attributes at runtime
# e.g.: AlignedSegments.qname = AlignedSegments.query_name
# * will slow down the default interface
# explicit declaration of getters/setters
########################################################
property qname:
"""deprecated, use query_name instead"""
def __get__(self): return self.query_name
def __set__(self, v): self.query_name = v
property tid:
"""deprecated, use reference_id instead"""
def __get__(self): return self.reference_id
def __set__(self, v): self.reference_id = v
property pos:
"""deprecated, use reference_start instead"""
def __get__(self): return self.reference_start
def __set__(self, v): self.reference_start = v
property mapq:
"""deprecated, use mapping_quality instead"""
def __get__(self): return self.mapping_quality
def __set__(self, v): self.mapping_quality = v
property rnext:
"""deprecated, use next_reference_id instead"""
def __get__(self): return self.next_reference_id
def __set__(self, v): self.next_reference_id = v
property pnext:
"""deprecated, use next_reference_start instead"""
def __get__(self):
return self.next_reference_start
def __set__(self, v):
self.next_reference_start = v
property cigar:
"""deprecated, use cigartuples instead"""
def __get__(self):
r = self.cigartuples
if r is None:
r = []
return r
def __set__(self, v): self.cigartuples = v
property tlen:
"""deprecated, use template_length instead"""
def __get__(self):
return self.template_length
def __set__(self, v):
self.template_length = v
property seq:
"""deprecated, use query_sequence instead"""
def __get__(self):
return self.query_sequence
def __set__(self, v):
self.query_sequence = v
property qual:
"""deprecated, query_qualities instead"""
def __get__(self):
return array_to_qualitystring(self.query_qualities)
def __set__(self, v):
self.query_qualities = qualitystring_to_array(v)
property alen:
"""deprecated, reference_length instead"""
def __get__(self):
return self.reference_length
def __set__(self, v):
self.reference_length = v
property aend:
"""deprecated, reference_end instead"""
def __get__(self):
return self.reference_end
def __set__(self, v):
self.reference_end = v
property rlen:
"""deprecated, query_length instead"""
def __get__(self):
return self.query_length
def __set__(self, v):
self.query_length = v
property query:
"""deprecated, query_alignment_sequence instead"""
def __get__(self):
return self.query_alignment_sequence
def __set__(self, v):
self.query_alignment_sequence = v
property qqual:
"""deprecated, query_alignment_qualities instead"""
def __get__(self):
return array_to_qualitystring(self.query_alignment_qualities)
def __set__(self, v):
self.query_alignment_qualities = qualitystring_to_array(v)
property qstart:
"""deprecated, use query_alignment_start instead"""
def __get__(self):
return self.query_alignment_start
def __set__(self, v):
self.query_alignment_start = v
property qend:
"""deprecated, use query_alignment_end instead"""
def __get__(self):
return self.query_alignment_end
def __set__(self, v):
self.query_alignment_end = v
property qlen:
"""deprecated, use query_alignment_length instead"""
def __get__(self):
return self.query_alignment_length
def __set__(self, v):
self.query_alignment_length = v
property mrnm:
"""deprecated, use next_reference_id instead"""
def __get__(self):
return self.next_reference_id
def __set__(self, v):
self.next_reference_id = v
property mpos:
"""deprecated, use next_reference_start instead"""
def __get__(self):
return self.next_reference_start
def __set__(self, v):
self.next_reference_start = v
property rname:
"""deprecated, use reference_id instead"""
def __get__(self):
return self.reference_id
def __set__(self, v):
self.reference_id = v
property isize:
"""deprecated, use template_length instead"""
def __get__(self):
return self.template_length
def __set__(self, v):
self.template_length = v
property blocks:
"""deprecated, use get_blocks() instead"""
def __get__(self):
return self.get_blocks()
property aligned_pairs:
"""deprecated, use get_aligned_pairs() instead"""
def __get__(self):
return self.get_aligned_pairs()
property inferred_length:
"""deprecated, use infer_query_length() instead"""
def __get__(self):
return self.infer_query_length()
property positions:
"""deprecated, use get_reference_positions() instead"""
def __get__(self):
return self.get_reference_positions()
property tags:
"""deprecated, use get_tags() instead"""
def __get__(self):
return self.get_tags()
def __set__(self, tags):
self.set_tags(tags)
def overlap(self):
"""deprecated, use get_overlap() instead"""
return self.get_overlap()
def opt(self, tag):
"""deprecated, use get_tag() instead"""
return self.get_tag(tag)
def setTag(self, tag, value, value_type=None, replace=True):
"""deprecated, use set_tag() instead"""
return self.set_tag(tag, value, value_type, replace)
cdef class PileupColumn:
'''A pileup of reads at a particular reference sequence position
(:term:`column`). A pileup column contains all the reads that map
to a certain target base.
This class is a proxy for results returned by the samtools pileup
engine. If the underlying engine iterator advances, the results
of this column will change.
'''
def __init__(self):
raise TypeError("this class cannot be instantiated from Python")
def __str__(self):
return "\t".join(map(str,
(self.reference_id,
self.reference_pos,
self.nsegments))) +\
"\n" +\
"\n".join(map(str, self.pileups))
property reference_id:
'''the reference sequence number as defined in the header'''
def __get__(self):
return self.tid
property reference_name:
""":term:`reference` name (None if no AlignmentFile is associated)"""
def __get__(self):
if self._alignment_file is not None:
return self._alignment_file.getrname(self.tid)
return None
property nsegments:
'''number of reads mapping to this column.'''
def __get__(self):
return self.n_pu
def __set__(self, n):
self.n_pu = n
property reference_pos:
'''the position in the reference sequence (0-based).'''
def __get__(self):
return self.pos
property pileups:
'''list of reads (:class:`pysam.PileupRead`) aligned to this column'''
def __get__(self):
cdef int x
pileups = []
if self.plp == NULL or self.plp[0] == NULL:
raise ValueError("PileupColumn accessed after iterator finished")
# warning: there could be problems if self.n and self.buf are
# out of sync.
for x from 0 <= x < self.n_pu:
pileups.append(makePileupRead(&(self.plp[0][x]),
self._alignment_file))
return pileups
########################################################
# Compatibility Accessors
# Functions, properties for compatibility with pysam < 0.8
########################################################
property pos:
def __get__(self):
return self.reference_pos
def __set__(self, v):
self.reference_pos = v
property tid:
def __get__(self):
return self.reference_id
def __set__(self, v):
self.reference_id = v
property n:
def __get__(self):
return self.nsegments
def __set__(self, v):
self.nsegments = v
cdef class PileupRead:
'''Representation of a read aligned to a particular position in the
reference sequence.
'''
def __init__(self):
raise TypeError(
"this class cannot be instantiated from Python")
def __str__(self):
return "\t".join(
map(str,
(self.alignment, self.query_position,
self.indel, self.level,
self.is_del, self.is_head,
self.is_tail, self.is_refskip)))
property alignment:
"""a :class:`pysam.AlignedSegment` object of the aligned read"""
def __get__(self):
return self._alignment
property query_position:
"""position of the read base at the pileup site, 0-based.
None if is_del or is_refskip is set.
"""
def __get__(self):
if self.is_del or self.is_refskip:
return None
else:
return self._qpos
property query_position_or_next:
"""position of the read base at the pileup site, 0-based.
If the current position is a deletion, returns the next
aligned base.
"""
def __get__(self):
return self._qpos
property indel:
"""indel length for the position following the current pileup site.
This quantity peeks ahead to the next cigar operation in this
alignment. If the next operation is an insertion, indel will
be positive. If the next operation is a deletion, it will be
negation. 0 if the next operation is not an indel.
"""
def __get__(self):
return self._indel
property level:
"""the level of the read in the "viewer" mode. Note that this value
is currently not computed."""
def __get__(self):
return self._level
property is_del:
"""1 iff the base on the padded read is a deletion"""
def __get__(self):
return self._is_del
property is_head:
"""1 iff the base on the padded read is the left-most base."""
def __get__(self):
return self._is_head
property is_tail:
"""1 iff the base on the padded read is the right-most base."""
def __get__(self):
return self._is_tail
property is_refskip:
"""1 iff the base on the padded read is part of CIGAR N op."""
def __get__(self):
return self._is_refskip
__all__ = [
"AlignedSegment",
"PileupColumn",
"PileupRead"]
|