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
|
from __future__ import print_function
import pybedtools
import gzip
import os, difflib, sys
from textwrap import dedent
from pybedtools.helpers import BEDToolsError
from pybedtools import featurefuncs
import six
import pysam
from six.moves import socketserver
from six.moves import BaseHTTPServer
import pytest
import threading
import warnings
testdir = os.path.dirname(__file__)
tempdir = os.path.join(os.path.abspath(testdir), 'tmp')
unwriteable = 'unwriteable'
def setup_module():
if not os.path.exists(tempdir):
os.system('mkdir -p %s' % tempdir)
pybedtools.set_tempdir(tempdir)
def teardown_module():
if os.path.exists(tempdir):
os.system('rm -r %s' % tempdir)
pybedtools.cleanup()
try:
FileNotFoundError
except NameError:
# python2
FileNotFoundError = OSError
def fix(x):
"""
Replaces spaces with tabs, removes spurious newlines, and lstrip()s each
line. Makes it really easy to create BED files on the fly for testing and
checking.
"""
s = ""
for i in x.splitlines():
i = i.lstrip()
if i.endswith('\t'):
add_tab = '\t'
else:
add_tab = ''
if len(i) == 0:
continue
i = i.split()
i = '\t'.join(i) + add_tab + '\n'
s += i
return s
# ----------------------------------------------------------------------------
# Tabix support tests
# ----------------------------------------------------------------------------
def make_unwriteable():
"""
Make a directory that cannot be written to and set the pybedtools tempdir
to it. This is used to isolate "streaming" tests to ensure they do not
write to disk.
"""
if os.path.exists(unwriteable):
os.system('rm -rf %s' % unwriteable)
os.system('mkdir -p %s' % unwriteable)
os.system('chmod -w %s' % unwriteable)
pybedtools.set_tempdir(unwriteable)
def cleanup_unwriteable():
"""
Reset to normal tempdir operation....
"""
if os.path.exists(unwriteable):
os.system('rm -rf %s' % unwriteable)
pybedtools.set_tempdir(tempdir)
def test_interval_index():
"""
supplement to the more general test in test_cbedtools.IntervalTest.testGetItemNegative
"""
iv = pybedtools.create_interval_from_list('chr21 9719768 9721892 ALR/Alpha 1004 +'.split())
assert iv[-1] == '+'
assert iv[2:-1] == ['9721892', 'ALR/Alpha', '1004']
iv = pybedtools.create_interval_from_list(
['chr1', 'ucb', 'gene', '465', '805', '.', '+', '.',
'ID=thaliana_1_465_805;match=scaffold_801404.1;rname=thaliana_1_465_805'])
print(iv[4:-3])
assert iv[4:-3] == ['805', '.']
def test_tuple_creation():
# everything as a string
t = [
("chr1", "1", "100", "feature1", "0", "+"),
("chr1", "100", "200", "feature2", "0", "+"),
("chr1", "150", "500", "feature3", "0", "-"),
("chr1", "900", "950", "feature4", "0", "+")
]
x = pybedtools.BedTool(t).saveas()
assert pybedtools.example_bedtool('a.bed') == x
t = [
("chr1", 1, 100, "feature1", 0, "+"),
("chr1", 100, 200, "feature2", 0, "+"),
("chr1", 150, 500, "feature3", 0, "-"),
("chr1", 900, 950, "feature4", 0, "+")
]
x = pybedtools.BedTool(t).saveas()
assert pybedtools.example_bedtool('a.bed') == x
t = [
("chr1", "fake", "gene", "50", "300", ".", "+", ".", "ID=gene1"),
("chr1", "fake", "mRNA", "50", "300", ".", "+", ".", "ID=mRNA1;Parent=gene1;"),
("chr1", "fake", "CDS", "75", "150", ".", "+", ".", "ID=CDS1;Parent=mRNA1;"),
("chr1", "fake", "CDS", "200", "275", ".", "+", ".", "ID=CDS2;Parent=mRNA1;"),
("chr1", "fake", "rRNA", "1200", "1275", ".", "+", ".", "ID=rRNA1;"),]
x = pybedtools.BedTool(t).saveas()
# Make sure that x has actual Intervals and not plain tuples or something
assert isinstance(x[0], pybedtools.Interval)
assert repr(x[0]) == "Interval(chr1:49-300)"
assert x[0]['ID'] == 'gene1'
def test_tabix():
try:
a = pybedtools.example_bedtool('a.bed')
t = a.tabix()
assert t._tabixed()
results = (t.tabix_intervals('chr1:99-200'))
results = str(results)
print(results)
assert results == fix("""
chr1 1 100 feature1 0 +
chr1 100 200 feature2 0 +
chr1 150 500 feature3 0 -""")
assert str(t.tabix_intervals(a[2])) == fix("""
chr1 100 200 feature2 0 +
chr1 150 500 feature3 0 -""")
finally:
# clean up
fns = [
pybedtools.example_filename('a.bed.gz'),
pybedtools.example_filename('a.bed.gz.tbi'),
]
for fn in fns:
if os.path.exists(fn):
os.unlink(fn)
def test_tabix_intervals():
a = pybedtools.BedTool('chr1 25 30', from_string=True).tabix()
assert len(a.tabix_intervals('chr1:30-35')) == 0
assert len(a.tabix_intervals('chr1:29-30')) == 1
# make sure it works OK even if strand was provided
assert len(a.tabix_intervals('chr1:30-35[-]')) == 0
assert len(a.tabix_intervals('chr1:29-30[-]')) == 1
# permit fetching of a contig without a specified region
assert len(a.tabix_intervals('chr1')) == 1
# ----------------------------------------------------------------------------
# Streaming and non-file BedTool tests
# ----------------------------------------------------------------------------
def test_stream():
"""
Stream and file-based equality, both whole-file and Interval by
Interval
"""
cleanup_unwriteable()
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
c = a.intersect(b)
# this should really not be written anywhere
d = a.intersect(b, stream=True)
with pytest.raises(NotImplementedError):
c.__eq__(d)
d = d.saveas()
d_contents = open(d.fn).read()
c_contents = open(c.fn).read()
assert d_contents == c_contents
# reconstruct d and check Interval-by-Interval equality
make_unwriteable()
d = a.intersect(b, stream=True)
for i,j in zip(c, d):
assert str(i) == str(j)
# Now do something similar with GFF files.
a = pybedtools.example_bedtool('a.bed')
f = pybedtools.example_bedtool('d.gff')
# file-based
cleanup_unwriteable()
g1 = f.intersect(a)
# streaming
make_unwriteable()
g2 = f.intersect(a, stream=True)
for i,j in zip(g1, g2):
assert str(i) == str(j)
# this was segfaulting at one point, just run to make sure
g3 = f.intersect(a, stream=True)
for i in iter(g3):
print(i)
for row in a.cut([0, 1, 2, 5], stream=True):
row[0], row[1], row[2]
with pytest.raises(IndexError):
row.__getitem__(4)
cleanup_unwriteable()
def test_stream_of_stream():
"""
Second-level streaming using self-intersections
"""
a = pybedtools.example_bedtool('a.bed')
# Ensure non-stream and stream equality of self-intersection
nonstream1 = a.intersect(a, u=True)
stream1 = a.intersect(a, u=True, stream=True)
nonstream1_str = str(nonstream1)
stream1_str = str(stream1)
a_str = str(a)
assert nonstream1_str == stream1_str == a_str
# Have to reconstruct stream1 cause it was consumed in the str() call
nonstream1 = a.intersect(a, u=True)
stream1 = a.intersect(a, u=True, stream=True)
nonstream2 = a.intersect(nonstream1, u=True)
stream2 = a.intersect(stream1, u=True, stream=True)
nonstream2_str = str(nonstream2)
stream2_str = str(stream2)
assert nonstream2_str == stream2_str == nonstream1_str == stream1_str == a_str
def test_generator():
"""
Equality of BedTools created from file, iter(), and generator
"""
# Test creation from file vs
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.BedTool(iter(a))
assert str(a) == str(b)
# Ensure that streams work well too
b1 = a.intersect(a, stream=True)
b2 = pybedtools.BedTool((i for i in a)).intersect(a)
assert str(b1) == str(b2)
def test_stream_of_generator():
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
b1 = a.intersect(a, stream=True)
b2 = pybedtools.BedTool((i for i in a)).intersect(a, stream=True)
sb1 = str(b1)
sb2 = str(b2)
print(sb1)
print(sb2)
assert sb1 == sb2
def test_many_files():
"""regression test to make sure many files can be created
"""
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
# Previously, IntervalFile would leak open files and would cause OSError
# (too many open files) at iteration 1010 or so.
for i in range(1100):
c = a.intersect(b)
def test_malformed():
"""
Malformed BED lines should raise MalformedBedLineError
"""
a = pybedtools.BedTool("""
chr1 100 200
chr1 100 90
chr1 100 200
chr1 100 200
chr1 100 200
chr1 100 200
""", from_string=True)
a_i = iter(a)
# first feature is OK
print(six.advance_iterator(a_i))
# but next one is not and should raise exception
with pytest.raises(pybedtools.MalformedBedLineError):
a_i.__next__()
def test_remove_invalid():
"""
Remove_invalid() removes invalid lines, track lines, and comments
"""
a = pybedtools.BedTool("""
chr1 100 200
chr1 100 90
track name='try to break parser'
chr1 100 200
chr1 100 200
chr1 100 200
#
chr1 100 200
""", from_string=True)
b = a.remove_invalid()
cleaned = pybedtools.BedTool("""
chr1 100 200
chr1 100 200
chr1 100 200
chr1 100 200
chr1 100 200""", from_string=True)
with pytest.raises(NotImplementedError):
b.__eq__(cleaned)
assert str(b) == str(cleaned)
def test_create_from_list_long_features():
"""
Iterator handles extra fields from long features (BED+GFF -wao intersection)
"""
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('c.gff')
c = a.intersect(b, wao=True, stream=False)
d = a.intersect(b, wao=True, stream=True)
# as of BEDTools v2.22.1, closest assumes sorted input by default.
print(b.sort().closest(a))
for i in d:
print(i)
def test_iterator():
"""
Iterator should ignore non-BED lines
"""
s = """
track name="test"
browser position chrX:1-100
# comment line
chrX 1 10
# more comments
track name="another"
"""
a = pybedtools.BedTool(s, from_string=True)
results = list(a)
print(results[0])
assert str(results[0]) == 'chrX\t1\t10\n', results
def test_indexing():
"""
Indexing into BedTools
"""
a = pybedtools.example_bedtool('a.bed')
# This is the first line
interval = pybedtools.Interval('chr1', 1, 100, 'feature1', '0', '+')
# just to make sure
assert interval == next(iter(a))
# test slice behavior
results = list(a[0:2])
assert len(results) == 2
assert results[0] == interval
# test single-integer indexing
assert a[0] == interval
# only slices and integers allowed....
with pytest.raises(ValueError):
a.__getitem__('key')
def test_repr_and_printing():
"""
Missing files and streams should say so in repr()
"""
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
c = a+b
d = a.intersect(b, stream=True)
os.unlink(c.fn)
assert 'a.bed' in repr(a)
assert 'b.bed' in repr(b)
assert 'MISSING FILE' in repr(c)
assert 'stream' in repr(d)
def test_file_type():
"""
Regression test on file_type checks
Previously file_type was creating a new IntervalFile every time it was
called; now it's cached so an IntervalFile is only created once per
BedTool.
"""
a = pybedtools.example_bedtool('a.bed')
for i in range(5000):
a.file_type
# ----------------------------------------------------------------------------
# BEDTools wrapper tests --
# See test_iter.py, which uses YAML test case definitions, for more complete
# tests of BEDTools wrapper methods.
#
# Here, we assert exception raises and more complicated things that can't be
# easily described in YAML
# ----------------------------------------------------------------------------
def test_introns():
a = pybedtools.example_bedtool('mm9.bed12')
b = pybedtools.BedTool((f for f in a if f.name == "Tcea1,uc007afj.1")).saveas()
bfeat = next(iter(b))
bi = b.introns()
# b[9] is the exonCount from teh bed12 file. there should be
# b[9] -1 introns assuming no utrs.
assert len(bi) == int(bfeat[9]) - 1, (len(bi), len(b))
def test_slop():
"""
Calling slop with no genome should raise ValueError
"""
a = pybedtools.example_bedtool('a.bed')
# Make sure it complains if no genome is set
with pytest.raises(ValueError):
a.slop(**dict(l=100, r=1))
def test_closest():
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
r = a.closest(b)
assert len(r) == len(a)
# TODO: there's enough stuff in here that it's probably worth it to eventually
# make a TestSequenceStuff class
def test_sequence():
"""
From UCSC:
chromStart - The starting position of the feature in the chromosome or
scaffold. The first base in a chromosome is numbered 0.
chromEnd - The ending position of the feature in the chromosome or
scaffold. The chromEnd base is not included in the display of the feature.
For example, the first 100 bases of a chromosome are defined as
chromStart=0, chromEnd=100, and span the bases numbered 0-99. """
fi = os.path.join(testdir, 'test.fasta')
s = """
chrX 9 16 . . +
chrX 9 16 . . -
chrY 1 4 . . +
chrZ 28 31 . . +
"""
fasta = """
>chrX
AAAAAAAAATGCACTGAAAAAAAAAAAAAAA
>chrY
GCTACCCCCCCCCCCCCCCCCCCCCCCCCCC
>chrZ
AAAAAAAAAAAAAAAAAAAAAAAAAAAATCT
"""
a = pybedtools.BedTool(s, from_string=True)
with pytest.raises(ValueError):
a.save_seqs(('none',))
fout = open(fi,'w')
for line in fasta.splitlines(True):
fout.write(line.lstrip())
fout.close()
# redirect stderr for the call to .sequence(), which reports the creation
# of an index file
tmp = open(a._tmp(),'w')
orig_stderr = sys.stderr
sys.stderr = tmp
f = a.sequence(fi=fi)
sys.stderr = orig_stderr
assert f.fn == f.fn
seqs = open(f.seqfn).read()
print(seqs)
expected = """>chrX:9-16
TGCACTG
>chrX:9-16
TGCACTG
>chrY:1-4
CTA
>chrZ:28-31
TCT
"""
print(''.join(difflib.ndiff(seqs,expected)))
print(expected)
assert seqs == expected
f = a.sequence(fi=fi,s=True)
seqs = open(f.seqfn).read()
expected = """>chrX:9-16(+)
TGCACTG
>chrX:9-16(-)
CAGTGCA
>chrY:1-4(+)
CTA
>chrZ:28-31(+)
TCT
"""
print(seqs)
print(expected)
print(''.join(difflib.ndiff(seqs,expected)))
assert seqs == expected
f = f.save_seqs('deleteme.fa')
assert open('deleteme.fa').read() == expected
assert f.print_sequence() == expected
os.unlink('deleteme.fa')
fresh_a = pybedtools.BedTool(s, from_string=True)
assert fresh_a == f
os.unlink(fi)
if os.path.exists(fi+'.fai'):
os.unlink(fi+'.fai')
# ----------------------------------------------------------------------------
# Operator tests
# ----------------------------------------------------------------------------
def test_add_subtract():
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
assert a.intersect(b,u=True) == (a+b)
assert a.intersect(b,v=True) == (a-b)
def test_subset():
a = pybedtools.example_bedtool('a.bed')
import random
random.seed(1)
s = list(a.random_subset(1).features())
assert len(s) == 1
assert isinstance(s[0], pybedtools.Interval)
s2 = list(a.random_subset(len(a)).features())
print(len(s2))
assert len(s2) == len(a)
def test_eq():
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('a.bed')
# BedTool to BedTool
assert a == b
# BedTool to string
s= """chr1 1 100 feature1 0 +
chr1 100 200 feature2 0 +
chr1 150 500 feature3 0 -
chr1 900 950 feature4 0 +
"""
assert a == s
# Test not equa on bedtool
b = pybedtools.example_bedtool('b.bed')
assert b != a
# and string
assert a != "blah"
# Don't allow testing equality on streams
c = a.intersect(b, stream=True)
d = a.intersect(b)
with pytest.raises(NotImplementedError):
c == d
with pytest.raises(NotImplementedError):
d == c
# Test it on iterator, too....
e = pybedtools.BedTool((i for i in a))
with pytest.raises(NotImplementedError):
a == e
with pytest.raises(NotImplementedError):
e == a
# Make sure that if we force the iterator to be consumed, it is in fact
# equal
s = str(e)
print(str(a).splitlines(True))
print(s.splitlines(True))
assert a == s
def test_hash():
a = pybedtools.example_bedtool('a.bed')
d = {}
for i in a:
d[i] = 1
# ----------------------------------------------------------------------------
# Other BedTool method tests
# ----------------------------------------------------------------------------
def test_count_bed():
a = pybedtools.example_bedtool('a.bed')
assert a.count() == 4
assert len(a) == 4
def test_feature_centers():
from pybedtools import featurefuncs
a = pybedtools.BedTool("""
chr1 1 100
chr5 3000 4000
""", from_string=True)
b = a.each(featurefuncs.center, 1)
results = list(b.features())
print(results)
assert results[0].start == 50
assert results[0].stop == 51
assert results[0].chrom == 'chr1'
assert results[1].start == 3500
assert results[1].stop == 3501
assert results[1].chrom == 'chr5'
def test_bedtool_creation():
# make sure we can make a bedtool from a bedtool and that it points to the
# same file
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.BedTool(a)
assert b.fn == a.fn
e = FileNotFoundError if six.PY3 else ValueError
with pytest.raises(e):
pybedtools.BedTool('nonexistend.bed')
# note that *s* has both tabs and spaces....
s = """
chr1 1 100 feature1 0 +
chr1 100 200 feature2 0 +
chr1 150 500 feature3 0 -
chr1 900 950 feature4 0 +
"""
from_string = pybedtools.BedTool(s, from_string=True)
# difflib used here to show a bug where a newline was included when using
# from_string
print(''.join(difflib.ndiff(str(from_string), str(a))))
assert str(from_string) == str(a)
def test_special_methods():
# note that *s* has both tabs and spaces....
s = """
chr1 1 100 feature1 0 +
chr1 100 200 feature2 0 +
chr1 150 500 feature3 0 -
chr1 900 950 feature4 0 +
"""
from_string = pybedtools.BedTool(s, from_string=True)
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
assert from_string == a
assert from_string != b
assert not from_string == b
assert not from_string != a
def test_field_count():
a = pybedtools.example_bedtool('a.bed')
assert a.field_count() == 6
tmp = pybedtools.BedTool._tmp()
open(tmp, 'w').close()
b = pybedtools.BedTool(tmp)
assert b.field_count() == 0
def test_repr_and_printing():
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
c = a+b
os.unlink(c.fn)
assert 'a.bed' in repr(a)
assert 'b.bed' in repr(b)
assert 'MISSING FILE' in repr(c)
print(a.head(1))
def test_cut():
a = pybedtools.example_bedtool('a.bed')
c = a.cut([0, 1, 2, 4])
assert c.field_count() == 4, c
def test_filter():
a = pybedtools.example_bedtool('a.bed')
b = a.filter(lambda f: f.length < 100 and f.length > 0)
assert len(b) == 2
def test_random_intersection():
# TODO:
return
N = 4
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
li = list(a.randomintersection(b, N))
assert len(li) == N, li
def test_cat():
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
b_fn = pybedtools.example_filename('b.bed')
assert a.cat(b) == a.cat(b_fn)
expected = fix("""
chr1 1 500
chr1 800 950
""")
assert a.cat(b) == expected
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
c = a.cat(b, postmerge=False)
assert len(a) + len(b) == len(c), (len(a), len(b), len(c))
print(c)
assert c == fix("""
chr1 1 100 feature1 0 +
chr1 100 200 feature2 0 +
chr1 150 500 feature3 0 -
chr1 900 950 feature4 0 +
chr1 155 200 feature5 0 -
chr1 800 901 feature6 0 +
""")
b_expected = fix("""
chr1 155 200 feature5 0 -
chr1 800 901 feature6 0 +
""")
b_merge_expected = fix("""
chr1 155 200
chr1 800 901
""")
empty = pybedtools.BedTool([])
assert b.cat(empty) == b_merge_expected
assert empty.cat(b) == b_merge_expected
assert b.cat(empty, postmerge=False)== b_expected
assert empty.cat(b, postmerge=False)== b_expected
def test_randomstats():
chromsizes = {'chr1':(1,1000)}
a = pybedtools.example_bedtool('a.bed').set_chromsizes(chromsizes)
b = pybedtools.example_bedtool('b.bed')
try:
results = a.randomstats(b, 100, debug=True)
assert results['actual'] == 3
assert results['median randomized'] == 2.0
assert results['percentile'] == 91.0
except ImportError:
# allow doctests to pass if SciPy not installed
sys.stderr.write('SciPy not installed, so not testing '
'BedTool.randomstats().')
# ----------------------------------------------------------------------------
# Interval tests
# ----------------------------------------------------------------------------
def test_gff_stuff():
s = """
chr1 fake gene 1 100 . + . ID=gene1
chr1 fake mRNA 1 100 . + . Name=mRNA1
chr1 fake CDS 50 90 . + . other=nothing
"""
d = pybedtools.BedTool(s, from_string=True)
f1, f2, f3 = d.features()
assert f1.name == 'gene1', f1.name
assert f2.name == 'mRNA1', f2.name
assert f3.name is None, f3.name
def test_name():
c = next(iter(pybedtools.example_bedtool('c.gff')))
assert c.name == "thaliana_1_465_805" , c.name
# ----------------------------------------------------------------------------
# Other non-BedTool tests
# ----------------------------------------------------------------------------
def test_flatten():
from pybedtools.helpers import _flatten_list
result = _flatten_list([[1,2,3,0,[0,5],9],[100]])
print(result)
assert result == [1, 2, 3, 0, 0, 5, 9, 100]
def test_history_step():
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
c = a.intersect(b)
d = c.subtract(a)
tag = c.history[0].result_tag
assert pybedtools.find_tagged(tag) == c
with pytest.raises(ValueError):
pybedtools.find_tagged('nonexistent')
print(d.history)
d.delete_temporary_history(ask=True, raw_input_func=lambda x: 'n')
assert os.path.exists(a.fn)
assert os.path.exists(b.fn)
assert os.path.exists(c.fn)
assert os.path.exists(d.fn)
d.delete_temporary_history(ask=True, raw_input_func=lambda x: 'Yes')
assert os.path.exists(a.fn)
assert os.path.exists(b.fn)
assert not os.path.exists(c.fn) # this is the only thing that should change
assert os.path.exists(d.fn)
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
c = a.intersect(b)
d = c.subtract(a)
d.delete_temporary_history(ask=False)
assert os.path.exists(a.fn)
assert os.path.exists(b.fn)
assert not os.path.exists(c.fn) # this is the only thing that should change
assert os.path.exists(d.fn)
def test_kwargs():
a = pybedtools.example_bedtool('a.bed')
b = a.intersect(a, s=False)
c = a.intersect(a)
assert str(b) == str(c)
# ----------------------------------------------------------------------------
# gzip support tests
# ----------------------------------------------------------------------------
def test_is_gzip():
gzfn = pybedtools.example_filename('snps.bed.gz')
fn = pybedtools.example_filename('a.bed')
assert pybedtools.helpers.isGZIP(gzfn)
assert not pybedtools.helpers.isGZIP(fn)
def test_gzip():
# make new gzipped files on the fly
agz = pybedtools.BedTool._tmp()
bgz = pybedtools.BedTool._tmp()
os.system('gzip -c %s > %s' % (pybedtools.example_filename('a.bed'), agz))
os.system('gzip -c %s > %s' % (pybedtools.example_filename('b.bed'), bgz))
agz = pybedtools.BedTool(agz)
bgz = pybedtools.BedTool(bgz)
assert agz.file_type == bgz.file_type == 'bed'
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
assert a.intersect(b) == agz.intersect(bgz) == a.intersect(bgz) == agz.intersect(b)
# ----------------------------------------------------------------------------
# BAM support tests
# ----------------------------------------------------------------------------
def test_bam_bedtool_creation():
x = pybedtools.example_bedtool('x.bam')
y = pybedtools.example_bedtool('y.bam')
a = pybedtools.example_bedtool('a.bed')
assert x._isbam
assert y._isbam
assert not a._isbam
def test_print_abam():
x = pybedtools.example_bedtool('gdc.bam')
expected = fix("""
None 0 chr2L 11 255 5M * 0 0 CGACA IIIII NM:i:0 NH:i:1
None 16 chr2L 71 255 5M * 0 0 TTCTC IIIII NM:i:0 NH:i:1
None 16 chr2L 141 255 5M * 0 0 CACCA IIIII NM:i:0 NH:i:1
None 16 chr2L 151 255 5M * 0 0 GTTCA IIIII NM:i:0 NH:i:1
None 0 chr2L 211 255 5M * 0 0 AAATA IIIII NM:i:0 NH:i:1
None 0 chr2L 71 255 5M * 0 0 GAGAA IIIII NM:i:0 NH:i:1
None 0 chr2L 141 255 5M * 0 0 TGGTG IIIII NM:i:0 NH:i:1
None 0 chr2L 161 255 5M * 0 0 GATAA IIIII NM:i:0 NH:i:1""")
print('x:')
print(x)
print('expected:')
print(expected)
assert x == expected
def test_bam_iter():
x = pybedtools.example_bedtool('gdc.bam')
s = 'None 0 chr2L 11 255 5M * 0 0 CGACA IIIII NM:i:0 NH:i:1\n'
assert str(x[0]) == str(next(iter(x))) == s
#TODO: py3 branch fails here
def bam_stream_bed():
x = pybedtools.example_bedtool('gdc.bam')
b = pybedtools.example_bedtool('gdc.gff')
c = x.intersect(b, u=True, bed=True, stream=True)
str_c = str(c)
expected = fix("""
chr2L 70 75 None 255 - 70 75 0,0,0 1 5, 0,
chr2L 140 145 None 255 - 140 145 0,0,0 1 5, 0,
chr2L 150 155 None 255 - 150 155 0,0,0 1 5, 0,
chr2L 210 215 None 255 + 210 215 0,0,0 1 5, 0,
chr2L 70 75 None 255 + 70 75 0,0,0 1 5, 0,
chr2L 140 145 None 255 + 140 145 0,0,0 1 5, 0,
chr2L 160 165 None 255 + 160 165 0,0,0 1 5, 0,
""")
assert str_c == expected
# TODO: py3 branch fails here
def bam_stream_bam():
x = pybedtools.example_bedtool('gdc.bam')
b = pybedtools.example_bedtool('gdc.gff')
c = x.intersect(b, u=True, stream=True)
expected = fix("""
None 16 chr2L 71 255 5M * 0 0 TTCTC IIIII NM:i:0 NH:i:1
None 16 chr2L 141 255 5M * 0 0 CACCA IIIII NM:i:0 NH:i:1
None 16 chr2L 151 255 5M * 0 0 GTTCA IIIII NM:i:0 NH:i:1
None 0 chr2L 211 255 5M * 0 0 AAATA IIIII NM:i:0 NH:i:1
None 0 chr2L 71 255 5M * 0 0 GAGAA IIIII NM:i:0 NH:i:1
None 0 chr2L 141 255 5M * 0 0 TGGTG IIIII NM:i:0 NH:i:1
None 0 chr2L 161 255 5M * 0 0 GATAA IIIII NM:i:0 NH:i:1""")
assert str(c) == expected
# TODO: py3 branch fails here
def bam_stream_bam_stream():
x = pybedtools.example_bedtool('gdc.bam')
b = pybedtools.example_bedtool('gdc.gff')
c = x.intersect(b, u=True, stream=True)
expected = fix("""
None 16 chr2L 71 255 5M * 0 0 TTCTC IIIII NM:i:0 NH:i:1
None 16 chr2L 141 255 5M * 0 0 CACCA IIIII NM:i:0 NH:i:1
None 16 chr2L 151 255 5M * 0 0 GTTCA IIIII NM:i:0 NH:i:1
None 0 chr2L 211 255 5M * 0 0 AAATA IIIII NM:i:0 NH:i:1
None 0 chr2L 71 255 5M * 0 0 GAGAA IIIII NM:i:0 NH:i:1
None 0 chr2L 141 255 5M * 0 0 TGGTG IIIII NM:i:0 NH:i:1
None 0 chr2L 161 255 5M * 0 0 GATAA IIIII NM:i:0 NH:i:1""")
d = c.intersect(b)
print(d)
assert str(d) == expected
def test_bam_interval():
x = pybedtools.example_bedtool('x.bam')
assert x[0].chrom == 'chr2L'
assert x[0].start == 9329
assert x[0][3] == '9330'
assert x[0].stop == 9365
assert len(x[0][9]) == len(x[0]) == 36
def test_bam_regression():
# Regression test: with extra fields, the first item in x.bam was being
# parsed as gff (cause not ==13 fields). This does a check to prevent that
# from happening again.
x = pybedtools.example_bedtool('x.bam')
assert x[0].file_type == 'sam'
assert x[0].chrom == 'chr2L'
def test_sam_filetype():
# file_type was segfaulting cause IntervalFile couldn't parse SAM
a = pybedtools.example_bedtool('gdc.bam')
b = pybedtools.BedTool(i for i in a).saveas()
assert b.file_type == 'sam'
def test_bam_to_sam_to_bam2():
"test directly from #135"
a = pybedtools.example_bedtool('gdc.bam')
orig = str(a)
assert a.file_type == 'bam'
# saveas should maintain BAM format
b = a.saveas()
assert b.file_type == 'bam'
# Converting to string gets SAM format
assert str(b) == orig
# b is a bam; to_bam should return a bam
c = b.to_bam(genome='dm3')
assert c.file_type == 'bam'
# in fact, it should be the same file:
assert c.fn == b.fn
# In order to get SAM format, need to print to file.
d = open(pybedtools.BedTool._tmp(), 'w')
d.write(str(c))
d.close()
d = pybedtools.BedTool(d.name)
assert d.file_type == 'sam'
e = d.to_bam(genome='dm3')
assert e.file_type == 'bam'
# everybody should be the same
assert a == b
assert a == c
assert a == d
assert a == e
def test_bam_to_sam_to_bam():
a = pybedtools.example_bedtool('gdc.bam')
orig = str(a)
assert a.file_type == 'bam'
# saveas should maintain BAM format
b = a.saveas()
assert b.file_type == 'bam'
# Converting to string gets SAM format
assert str(b) == orig
# b is a bam; to_bam should return a bam
c = b.to_bam(genome='dm3')
assert c.file_type == 'bam'
# in fact, it should be the same file:
assert c.fn == b.fn
# In order to get SAM format, need to print to file.
d = open(pybedtools.BedTool._tmp(), 'w')
d.write(str(c))
d.close()
d = pybedtools.BedTool(d.name)
assert d.file_type == 'sam'
e = d.to_bam(genome='dm3')
assert e.file_type == 'bam'
# everybody should be the same
assert a == b == c == d == e
def test_bam_filetype():
# regression test -- this was segfaulting before because IntervalFile
# couldn't parse SAM
a = pybedtools.example_bedtool('gdc.bam')
b = pybedtools.example_bedtool('gdc.gff')
c = a.intersect(b)
assert c.file_type == 'bam'
def test_bam_header():
a = pybedtools.example_bedtool('gdc.bam')
b = pybedtools.example_bedtool('gdc.gff')
c = a.intersect(b)
print(c._bam_header)
assert c._bam_header == "@SQ SN:chr2L LN:23011544\n"
def test_output_kwarg():
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
c = a.intersect(b)
d = a.intersect(b, output='deleteme.bed')
assert c == d
os.unlink('deleteme.bed')
def test_copy():
a = pybedtools.example_bedtool('a.bed')
x = a[0]
# Before adding the __copy__ method to Interval class, making a copy would
# hang and then segfault
import copy
y = copy.copy(x)
assert y.start == x.start
assert y.stop == x.stop
assert y.chrom == x.chrom
assert y.name == x.name
assert y.fields == x.fields
assert y.file_type == x.file_type == 'bed'
# Make sure it's a real copy (changing something in y doesn't change
# something in x)
y.start += 1
assert y.start == x.start + 1
def test_pickleable():
interval = pybedtools.create_interval_from_list(
['chr1', '1', '100', 'asdf'])
fn = pybedtools.BedTool._tmp()
import pickle
out = open(fn, 'wb')
print([type(i) for i in interval.fields])
print(type(str(interval)))
pickle.dump(interval, out)
out.close()
new_interval = pickle.load(open(fn, 'rb'))
assert str(interval) == str(new_interval)
interval = pybedtools.create_interval_from_list(
['chr1', '1', '100'])
fn = pybedtools.BedTool._tmp()
import pickle
out = open(fn, 'wb')
pickle.dump(interval, out)
out.close()
new_interval = pickle.load(open(fn, 'rb'))
assert str(interval) == str(new_interval)
interval = pybedtools.create_interval_from_list(
"chr2L . UTR 41 70 0 + . ID=mRNA:xs2:UTR:41-70;Parent=mRNA:xs2;".split('\t'))
fn = pybedtools.BedTool._tmp()
import pickle
out = open(fn, 'wb')
pickle.dump(interval, out)
out.close()
new_interval = pickle.load(open(fn, 'rb'))
assert str(interval) == str(new_interval)
def test_split():
a = pybedtools.example_bedtool('a.bed')
def func(x, dist1, dist2):
"shift the features around"
newstart = x.start + dist1
newstop = x.stop + dist1
x.start = newstart
x.stop = newstop
yield x
x.start -= dist2
x.stop -= dist2
yield x
result = str(a.split(func, 1000, 100))
assert result == fix("""
chr1 1001 1100 feature1 0 +
chr1 901 1000 feature1 0 +
chr1 1100 1200 feature2 0 +
chr1 1000 1100 feature2 0 +
chr1 1150 1500 feature3 0 -
chr1 1050 1400 feature3 0 -
chr1 1900 1950 feature4 0 +
chr1 1800 1850 feature4 0 +
""")
def test_additional_args():
a = pybedtools.example_bedtool('a.bed')
expected = fix("""
chr1 1 2 1
chr1 100 101 1
chr1 900 901 1""")
assert a.genome_coverage(bg=True, strand='+', g=dict(chr1=(1, 1000)), additional_args='-5') == expected
def test_tss():
a = pybedtools.example_bedtool('a.bed')
results = str(a.each(featurefuncs.TSS, upstream=3, downstream=5, add_to_name='_TSS'))
print(results)
assert results == fix("""
chr1 0 6 feature1_TSS 0 +
chr1 97 105 feature2_TSS 0 +
chr1 495 503 feature3_TSS 0 -
chr1 897 905 feature4_TSS 0 +
""")
def test_extend_fields():
a = pybedtools.example_bedtool('a.bed')
results = str(a.each(featurefuncs.extend_fields, 8))
print(results)
assert results == fix("""
chr1 1 100 feature1 0 + 1 100
chr1 100 200 feature2 0 + 100 200
chr1 150 500 feature3 0 - 150 500
chr1 900 950 feature4 0 + 900 950
""")
def test_gff2bed():
a = pybedtools.example_bedtool('d.gff')
results = str(a.each(featurefuncs.gff2bed, name_field='Parent'))
assert results == fix("""
chr1 49 300 . . +
chr1 49 300 gene1 . +
chr1 74 150 mRNA1 . +
chr1 199 275 mRNA1 . +
chr1 1199 1275 . . +""")
results = str(a.each(featurefuncs.gff2bed))
assert results == fix("""
chr1 49 300 gene1 . +
chr1 49 300 mRNA1 . +
chr1 74 150 CDS1 . +
chr1 199 275 CDS2 . +
chr1 1199 1275 rRNA1 . +
""")
results = str(a.each(featurefuncs.gff2bed, name_field="nonexistent"))
assert results == fix("""
chr1 49 300 . . +
chr1 49 300 . . +
chr1 74 150 . . +
chr1 199 275 . . +
chr1 1199 1275 . . +
""")
results = str(a.each(featurefuncs.gff2bed, name_field=1))
print(results)
assert results == fix("""
chr1 49 300 fake . +
chr1 49 300 fake . +
chr1 74 150 fake . +
chr1 199 275 fake . +
chr1 1199 1275 fake . +""")
def test_add_color():
try:
from matplotlib import cm
except ImportError:
print("matplotlib not installed; skipping test_add_color")
return
def modify_scores(f):
fields = f.fields
fields[4] = str(f[2])
return pybedtools.create_interval_from_list(fields)
a = pybedtools.example_bedtool('a.bed')
a = a.each(modify_scores).saveas()
cmap = cm.jet
norm = a.colormap_normalize()
results = str(a.each(featurefuncs.add_color, cmap=cmap, norm=norm))
print(results)
assert results == fix("""
chr1 1 100 feature1 100 + 1 100 0,0,127
chr1 100 200 feature2 200 + 100 200 0,0,255
chr1 150 500 feature3 500 - 150 500 99,255,147
chr1 900 950 feature4 950 + 900 950 127,0,0""")
#------------------------------------------------------------------------------
# Tests for IntervalFile, as accessed by BedTool objects
#------------------------------------------------------------------------------
def test_any_hits():
a = pybedtools.example_bedtool('a.bed')
assert 1 == a.any_hits(pybedtools.create_interval_from_list(
['chr1', '900', '905', '.', '.', '-']))
assert 0 == a.any_hits(pybedtools.create_interval_from_list(
['chr1', '900', '905', '.', '.', '-']), same_strand=True)
assert 0 == a.any_hits(pybedtools.create_interval_from_list(
['chr1', '8000', '9000', '.', '.', '-']))
def test_all_hits():
a = pybedtools.example_bedtool('a.bed')
assert [a[2], a[3]] == a.all_hits(pybedtools.create_interval_from_list(
['chr1', '450', '905', '.', '.', '-']))
assert [a[2]] == a.all_hits(pybedtools.create_interval_from_list(
['chr1', '450', '905', '.', '.', '-']), same_strand=True)
def test_count_hits():
a = pybedtools.example_bedtool('a.bed')
assert len(a.all_hits(pybedtools.create_interval_from_list(
['chr1', '450', '905', '.', '.', '-']))) == 2
assert len(a.all_hits(pybedtools.create_interval_from_list(
['chr1', '450', '905', '.', '.', '-']), same_strand=True)) == 1
def test_multi_intersect():
# Need to test here because "-i" is not a single other-bedtool like other
# "-i" BEDTools programs, and this throws off the iter testing.
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
x = pybedtools.BedTool()
assert x.multi_intersect(i=[a.fn, b.fn]) == fix("""
chr1 1 155 1 1 1 0
chr1 155 200 2 1,2 1 1
chr1 200 500 1 1 1 0
chr1 800 900 1 2 0 1
chr1 900 901 2 1,2 1 1
chr1 901 950 1 1 1 0""")
assert x.multi_intersect(i=[a.fn, b.fn], cluster=True) == fix("""
chr1 155 200 2 1,2 1 1
chr1 900 901 2 1,2 1 1""")
def test_union_bedgraphs():
# from unionBedGraphs -examples...
a = pybedtools.BedTool("""
chr1 1000 1500 10
chr1 2000 2100 20
""", from_string=True)
b = pybedtools.BedTool("""
chr1 900 1600 60
chr1 1700 2050 50
""", from_string=True)
c = pybedtools.BedTool("""
chr1 1980 2070 80
chr1 2090 2100 20
""", from_string=True)
x = pybedtools.BedTool()
result = x.union_bedgraphs(i=[a.fn, b.fn, c.fn])
assert result == fix("""
chr1 900 1000 0 60 0
chr1 1000 1500 10 60 0
chr1 1500 1600 0 60 0
chr1 1700 1980 0 50 0
chr1 1980 2000 0 50 80
chr1 2000 2050 20 50 80
chr1 2050 2070 20 0 80
chr1 2070 2090 20 0 0
chr1 2090 2100 20 0 20
""")
def test_window_maker():
x = pybedtools.BedTool()
a = pybedtools.example_bedtool('a.bed')
result = x.window_maker(b=a.fn, w=50)
print(result)
assert result == fix("""
chr1 1 51
chr1 51 100
chr1 100 150
chr1 150 200
chr1 150 200
chr1 200 250
chr1 250 300
chr1 300 350
chr1 350 400
chr1 400 450
chr1 450 500
chr1 900 950
""")
x = pybedtools.BedTool()
z = x.window_maker(genome='hg19', w=100000)
assert str(z[0]) == "chr1\t0\t100000\n"
assert str(z[10000]) == 'chr16\t20800000\t20900000\n'
def test_random():
a = pybedtools.BedTool()
result = a.random(l=10, n=10, genome='hg19', seed=1)
assert result == fix("""
chr3 11945098 11945108 1 10 +
chr15 84985693 84985703 2 10 -
chr2 62691196 62691206 3 10 -
chr18 18871346 18871356 4 10 +
chr9 133374407 133374417 5 10 +
chr9 48958184 48958194 6 10 +
chrY 41568406 41568416 7 10 -
chr4 16579517 16579527 8 10 +
chr1 76589882 76589892 9 10 -
chr3 55995799 55995809 10 10 -
""")
def test_links():
# have to be careful about the path, since it is embedded in the HTML
# output -- so make a copy of the example file, and delete when done.
os.system('cp %s a.links.bed' % pybedtools.example_filename('a.bed'))
a = pybedtools.BedTool('a.links.bed')
a = a.links()
exp = open(pybedtools.example_filename('a.links.html')).read()
obs = open(a.links_html).read()
print(exp)
print(obs)
assert exp == obs
os.unlink('a.links.bed')
def test_igv():
a = pybedtools.example_bedtool('a.bed')
a = a.igv()
obs = open(a.igv_script).read()
exp = open(pybedtools.example_filename('a.igv_script')).read()
assert obs == exp
def test_bam_to_fastq():
x = pybedtools.example_bedtool('small.bam')
tmpfn = pybedtools.BedTool._tmp()
y = x.bam_to_fastq(fq=tmpfn)
assert open(y.fastq).read() == open(pybedtools.example_filename('small.fastq')).read()
def test_gtf_gff_attrs():
# smoke test.
#
# this has always worked:
gff = ["chr1","fake","mRNA","51", "300",".", "+",".","ID=mRNA1;Parent=gene1;"]
gff = pybedtools.create_interval_from_list(gff)
gff.attrs
# this previously failed because of the "=" in the attr string.
gff = ['scaffold_52', 'Cufflinks', 'exon', '5478', '5568', '.', '+', '.', 'gene_id "XLOC_017766"; transcript_id "TCONS_00033979"; exon_number "6"; gene_name "g18412"; oId "PAC:26897502"; nearest_ref "PAC:26897502"; class_code "="; tss_id "TSS21210"; p_id "P18851";']
gff = pybedtools.create_interval_from_list(gff)
gff.attrs
# TODO: is it necessary to support GFF vs GTF detection in this case:
#
# GFF:
# class_code=" "
#
# GTF:
# class_code "="
def test_jaccard():
x = pybedtools.example_bedtool('a.bed')
results = x.jaccard(pybedtools.example_bedtool('b.bed'))
assert results == {'intersection': 46, 'union-intersection': 649, 'jaccard': 0.0708783, 'n_intersections': 2}, results
results2 = x.jaccard(pybedtools.example_bedtool('b.bed'), stream=True)
assert results == results2, results2
@pytest.mark.xfail
def test_reldist():
x = pybedtools.example_bedtool('a.bed')
results = x.reldist(pybedtools.example_bedtool('b.bed'))
assert results == {'reldist': [0.15, 0.21, 0.28], 'count': [1, 1, 1], 'total': [3, 3, 3], 'fraction': [0.333, 0.333, 0.333]}, results
results2 = x.reldist(pybedtools.example_bedtool('b.bed'), detail=True)
print(results2)
assert results2 == fix("""
chr1 1 100 feature1 0 + 0.282
chr1 100 200 feature2 0 + 0.153
chr1 150 500 feature3 0 - 0.220""")
def test_remote_bam_raises_exception_when_file_doesnt_exist():
"from #134"
with pytest.raises(ValueError):
pybedtools.BedTool('ftp://ftp-trace.ncbi.nih.gov/this/url/clearly/does/not/exist.bam', remote=True)
@pytest.mark.xfail(reason="Known failure: no support in BEDTools for remote BAM")
def test_remote_bam():
url = 'http://genome.ucsc.edu/goldenPath/help/examples/bamExample.bam'
x = pybedtools.BedTool(url, remote=True)
#for i in x:
# print(i)
# raise ValueError
def gen():
for i, f in enumerate(x.bam_to_bed()):
yield f
if i == 9:
break
results = pybedtools.BedTool(gen()).saveas()
assert results == fix("""
11 60636 60736 SRR081241.13799221/1 0 +
11 60674 60774 SRR077487.5548889/1 0 +
11 60684 60784 SRR077487.12853301/1 0 +
11 60789 60889 SRR077487.5548889/2 0 -
11 60950 61050 SRR077487.13826494/1 0 +
11 60959 61059 SRR081241.13799221/2 0 -
11 61052 61152 SRR077487.12853301/2 0 -
11 61548 61648 SRR081241.16743804/2 0 +
11 61665 61765 SRR081241.16743804/1 0 -
11 61989 62089 SRR077487.167173/2 0 +"""), results
return
# Borrow ideas from gffutils remote testing.
#
# Also, see issue with remote BAM hanging in pysam:
# https://github.com/pysam-developers/pysam/issues/107
print("Testing remote BAM by serving locally")
class ThreadingHTTPServer(socketserver.ThreadingMixIn, BaseHTTPServer.HTTPServer):
pass
#handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = socketserver.ThreadingTCPServer(("", 0), ThreadingHTTPServer)
port = str(httpd.socket.getsockname()[1])
print("Serving at port", port)
served_folder = pybedtools.example_filename("")
os.chdir(served_folder)
print(served_folder)
print("Starting SimpleHTTPServer in thread")
server_thread = threading.Thread(target=httpd.serve_forever)
server_thread.daemon = True
server_thread.start()
try:
url = ''.join(['http://localhost:', port, '/x.bam'])
print(url)
x = pybedtools.BedTool(url, remote=True)
if 0:
def gen():
for i, f in enumerate(x.bam_to_bed(stream=True)):
yield f
if i == 9:
break
results = pybedtools.BedTool(gen()).saveas()
assert results == fix("""
11 60636 60736 SRR081241.13799221/1 0 +
11 60674 60774 SRR077487.5548889/1 0 +
11 60684 60784 SRR077487.12853301/1 0 +
11 60789 60889 SRR077487.5548889/2 0 -
11 60950 61050 SRR077487.13826494/1 0 +
11 60959 61059 SRR081241.13799221/2 0 -
11 61052 61152 SRR077487.12853301/2 0 -
11 61548 61648 SRR081241.16743804/2 0 +
11 61665 61765 SRR081241.16743804/1 0 -
11 61989 62089 SRR077487.167173/2 0 +"""), results
finally:
print("Server shutdown.")
httpd.shutdown()
server_thread.join()
#raise ValueError
def test_empty_overloaded_ops():
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.BedTool("", from_string=True)
assert b.file_type == 'empty'
# NOTE: change in semantics. Previously, intersecting a BED file with an
# empty file would return the original BED file.
assert (a + b) == b
assert (b + a) == b
assert (a - b) == a
assert (b - a) == b
assert (b - b) == b
def test_to_dataframe():
def fix_dataframe(df):
return ''.join(df.splitlines(True)[1:])
try:
import pandas
except ImportError:
pytest.xfail("pandas not installed; skipping test")
a = pybedtools.example_bedtool('a.bed')
results = a.to_dataframe()
assert results.loc[0, 'name'] == 'feature1'
assert list(results.columns) == ['chrom', 'start', 'end', 'name', 'score', 'strand']
assert results.loc[3, 'strand'] == '+'
# reverse should work, too:
df = a.to_dataframe()
a2 = pybedtools.BedTool.from_dataframe(df)
assert a2 == a
# try converting only part of the dataframe to a BedTool
a3 = pybedtools.BedTool.from_dataframe(
df.loc[df.start < 100, ['chrom', 'start', 'end', 'name']]
)
assert a3 == fix(
"""
chr1 1 100 feature1
"""), str(a3)
d = pybedtools.example_bedtool('d.gff')
results = d.to_dataframe()
assert list(results.columns) == [
'seqname', 'source', 'feature', 'start', 'end', 'score', 'strand',
'frame', 'attributes']
assert results.loc[0, 'seqname'] == 'chr1'
assert results.loc[4, 'attributes'] == 'ID=rRNA1;'
# get a gff file with too many fields...
x = pybedtools.example_bedtool('c.gff')
x = x.intersect(x, c=True)
with warnings.catch_warnings(record=True) as w:
#trigger the warning
x.to_dataframe()
#assert a few things
assert len(w) == 1
assert issubclass(w[-1].category, UserWarning)
assert str(w[-1].message).startswith('Default names for filetype')
names = ['seqname', 'source', 'feature', 'start', 'end', 'score', 'strand',
'frame', 'attributes', 'count']
results = x.to_dataframe(names=names)
assert list(results.columns) == ['seqname', 'source', 'feature', 'start',
'end', 'score', 'strand', 'frame',
'attributes', 'count']
assert results.loc[0, 'seqname'] == 'chr1'
assert results.loc[13, 'count'] == 3
def test_tail():
a = pybedtools.example_bedtool('rmsk.hg18.chr21.small.bed')
observed = a.tail(as_string=True)
expected = fix(
"""
chr21 13355834 13356047 MER58A 892 -
chr21 13356250 13356290 AT_rich 26 +
chr21 13356358 13356381 AT_rich 23 +
chr21 13356571 13356910 L2 333 -
chr21 13357179 13357987 L1MEc 1264 -
chr21 13358003 13358300 L1MEc 379 -
chr21 13358304 13358952 L1MEc 1271 -
chr21 13358960 13359288 L2 336 +
chr21 13359444 13359751 AluY 2337 +
chr21 13360044 13360225 L1M5 284 -""")
assert observed == expected
# only ask for 3 lines
observed = a.tail(3, as_string=True)
expected = fix(
"""
chr21 13358960 13359288 L2 336 +
chr21 13359444 13359751 AluY 2337 +
chr21 13360044 13360225 L1M5 284 -""")
assert observed == expected
# For short files, whole thing should be returned
a = pybedtools.example_bedtool('a.bed')
expected = str(a)
obs = a.tail(as_string=True)
assert obs == expected
def test_fisher():
a = pybedtools.example_bedtool('a.bed')
b = pybedtools.example_bedtool('b.bed')
c = a.fisher(b, genome='hg19')
assert str(c) == \
"""# Number of query intervals: 4
# Number of db intervals: 2
# Number of overlaps: 3
# Number of possible intervals (estimated): 13958448
# phyper(3 - 1, 4, 13958448 - 4, 2, lower.tail=F)
# Contingency Table Of Counts
#_________________________________________
# | in -b | not in -b |
# in -a | 3 | 1 |
# not in -a | 0 | 13958444 |
#_________________________________________
# p-values for fisher's exact test
left right two-tail ratio
1 8.8247e-21 8.8247e-21 inf
""", c
def test_zero_len_boolean():
# regression test: with no __nonzero__ method, __len__ is used. This means
# that `if interval` evaluates to False when length is zero.
i = pybedtools.create_interval_from_list(['chr1', '1', '1'])
assert len(i) == 0
assert i
def test_chromsizes_in_5prime_3prime():
# standard 5'
a = pybedtools.example_bedtool('a.bed')\
.each(featurefuncs.five_prime, 1, 10, add_to_name="_TSS",
genome=pybedtools.chromsizes("hg19"))\
.saveas()
assert a == fix(
"""
chr1 0 11 feature1_TSS 0 +
chr1 99 110 feature2_TSS 0 +
chr1 490 501 feature3_TSS 0 -
chr1 899 910 feature4_TSS 0 +
"""), str(a)
# add genomes sizes; last feature should be truncated
a = pybedtools.example_bedtool('a.bed')\
.each(featurefuncs.five_prime, 1, 10, add_to_name="_TSS",
genome=dict(chr1=(0, 900)))\
.saveas()
assert a == fix(
"""
chr1 0 11 feature1_TSS 0 +
chr1 99 110 feature2_TSS 0 +
chr1 490 501 feature3_TSS 0 -
chr1 899 900 feature4_TSS 0 +
"""), str(a)
# same thing but for 3'.
# Note that the last feature chr1:949-960 is completely truncated because
# it would entirely fall outside of the chromosome
a = pybedtools.example_bedtool('a.bed')\
.each(featurefuncs.three_prime, 1, 10, add_to_name="_TSS",
genome=dict(chr1=(0, 900)))\
.saveas()
assert a == fix(
"""
chr1 99 110 feature1_TSS 0 +
chr1 199 210 feature2_TSS 0 +
chr1 140 151 feature3_TSS 0 -
chr1 900 900 feature4_TSS 0 +
"""), str(a)
# be a lot harsher with the chromsizes to ensure features on both strands
# get truncated correctly
a = pybedtools.example_bedtool('a.bed')\
.each(featurefuncs.three_prime, 1, 10, add_to_name="_TSS",
genome=dict(chr1=(0, 120)))\
.saveas()
assert a == fix(
"""
chr1 99 110 feature1_TSS 0 +
chr1 120 120 feature2_TSS 0 +
chr1 120 120 feature3_TSS 0 -
chr1 120 120 feature4_TSS 0 +
"""), str(a)
def test_new_head():
"""
The new BedTool.head no longer iterates using IntervalIterator but instead
just prints the lines of the file directly.
"""
tmp = pybedtools.BedTool._tmp()
with open(tmp, 'w') as fout:
fout.write(
'chr1\t5\t10\n'
'chr1\t-1\t15\n')
a = pybedtools.BedTool(tmp)
# previously would crash with OverflowError, can't convert negative value
# to CHRPOS
a.head()
# however, printing should still complain:
with pytest.raises(pybedtools.cbedtools.MalformedBedLineError):
print(a)
|