File: bedtool.py

package info (click to toggle)
python-pybedtools 0.8.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,140 kB
  • sloc: python: 9,589; cpp: 899; makefile: 149; sh: 116
file content (3664 lines) | stat: -rw-r--r-- 128,574 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
from __future__ import print_function, division
import tempfile
from textwrap import dedent
import shutil
import subprocess
import operator
import os
import sys
import random
import string
import pprint
from itertools import islice
import multiprocessing
import six
import gzip
import pysam
from warnings import warn

from .helpers import (
    get_tempdir, _tags, call_bedtools, _flatten_list, _check_sequence_stderr,
    isBAM, isBGZIP, isGZIP, BEDToolsError, _call_randomintersect, SplitOutput)
from . import helpers
from .cbedtools import IntervalFile, IntervalIterator, Interval, create_interval_from_list, BedToolsFileError
from . import filenames
import pybedtools
from . import settings
from . import filenames


_implicit_registry = {}
_other_registry = {}
_bam_registry = {}


def _jaccard_output_to_dict(s, **kwargs):
    """
    jaccard method doesn't return an interval file, rather, it returns a short
    summary of results.  Here, we simply parse it into a dict for convenience.
    """
    if isinstance(s, six.string_types):
        _s = open(s).read()
    elif hasattr(s, 'next') or hasattr(s, '__next__'):
        _s = ''.join([i for i in s])
    else:
        raise ValueError("Unexpected object %r" % s)
    header, data = _s.splitlines()
    header = header.split()
    data = data.split()
    data[0] = int(data[0])
    data[1] = int(data[1])
    data[2] = float(data[2])
    data[3] = int(data[3])
    return dict(list(zip(header, data)))


def _reldist_output_handler(s, **kwargs):
    """
    reldist, if called with -detail, returns a valid BED file with the relative
    distance as the last field.  In that case, return the BedTool immediately.
    If not -detail, then the results are a table, in which case here we parse
    into a dict for convenience.
    """
    if 'detail' in kwargs:
        return BedTool(s)
    if isinstance(s, six.string_types):
        iterable = open(s)
    if hasattr(s, 'next'):
        iterable = s
    header = six.advance_iterator(iterable).split()
    results = {}
    for h in header:
        results[h] = []
    for i in iterable:
        reldist, count, total, fraction = i.split()
        data = [
            float(reldist),
            int(count),
            int(total),
            float(fraction)
        ]
        for h, d in zip(header, data):
            results[h].append(d)
    return results


def _wraps(prog=None, implicit=None, bam=None, other=None, uses_genome=False,
           make_tempfile_for=None, check_stderr=None, add_to_bedtool=None,
           nonbam=None, force_bam=False, genome_none_if=None, genome_if=None,
           genome_ok_if=None, does_not_return_bedtool=None):
    """
    Do-it-all wrapper, to be used as a decorator.

    *prog* is the name of the BEDTools program that will be called.  The help
    for this program will also be added to the decorated method's docstring.

    *implicit* is the BEDTools program arg that should be filled in
    automatically.

    *bam* will disable the implicit substitution if *bam* is in the kwargs.
    This is typically 'abam' or 'ibam' if the program accepts BAM input.

    *other* is the BEDTools program arg that is passed in as the second input,
    if supported.  Within the semantics of BEDTools, the typical case will be
    that if implicit='a' then other='b'; if implicit='i' then other=None.

    *uses_genome*, if True, will check for 'g' and/or 'genome' args and
    retrieve the corresponding genome files as needed.

    *make_tempfile_for* is used for the sequence methods and indicates which
    kwarg should have a tempfile made for it if it's not provided ('fo' for the
    sequence methods)

    *check_stderr*, if not None, is a function that accepts a string (which
    will be anything written to stdout when calling the wrapped program).  This
    function should return True if the string is OK, and False if it should
    truly be considered an error.  This is needed for wrapping fastaFromBed,
    which will report to stderr that it's creating an index file.

    *add_to_bedtool* is used for sequence methods.  It is a dictionary mapping
    kwargs to attributes to be created in the resulting BedTool.  Typically it
    is {'fo':'seqfn'} which will add the resulting sequence name to the
    BedTool's .seqfn attribute. If *add_to_bedtool* is not None, then the
    returned BedTool will be *self* with the added attribute.  If a key is
    "stdout" (e.g., {"stdout": attr_name}), then save the stdout of the command
    as a tempfile and store the tempfile's name in the attribute.  This is
    required for linksBed and bedToIgv.

    *nonbam* is a kwarg that even if the input file was a BAM, the output will
    *not* be BAM format.  For example, the `-bed` arg for intersectBed will
    cause the output to be in BED format, not BAM.  If not None, this can be a
    string, a list of strings, or the special string "ALL", which means that
    the wrapped program will never return BAM output.

    *force_bam*, if True, will force the output to be BAM.  This is used for
    bedToBam.

    *genome_none_if* is a list of arguments that will ignore the requirement
    for a genome.  This is needed for window_maker, where -b and -g are
    mutually exclusive.

    *genome_ok_if* is a list of arguments that, if they are in
    *genome_none_if*, are still OK to pass in.  This is needed for bedtool
    genomecov, where -g is not needed if -ibam is specified...but it's still OK
    if the user passes a genome arg.

    *genome_if* is a list of arguments that will trigger the requirement for
    a genome; otherwise no genome needs to be specified.

    *does_not_return_bedtool*, if not None, should be a function that handles
    the returned output.  Its signature should be ``func(output, kwargs)``,
    where `output` is the output from the [possibly streaming] call to BEDTools
    and `kwargs` are passed verbatim from the wrapped method call. Some
    examples of methods that use this are jaccard, reldist, fisher, and split
    methods.
    """

    # NOTE: We are calling each BEDTools program to get its help and adding
    # that to the docstring of each method. This is run at import time. However
    # if BEDTools is not on the path at import time, `not_implemented` is set
    # to True and isn't reset later until the module is reloaded.
    #
    # helpers.set_bedtools_path therefore will trigger a module reload.
    not_implemented = False

    # Call the program with -h to get help, which prints to stderr.
    try:
        p = subprocess.Popen(helpers._version_2_15_plus_names(prog) + ['-h'],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        help_str = p.communicate()[1].decode()

        # underscores throw off ReStructuredText syntax of docstrings, so
        # replace 'em
        help_str = help_str.replace('_', '**')

        # indent
        help_str = help_str.split('\n')
        help_str = ['\n\n**Original BEDTools help:**::'] \
            + ['\t' + i for i in help_str]
        help_str = '\n'.join(help_str) + '\n'

    # If the program can't be found, then we'll eventually replace the method
    # with a version that does nothing but raise a NotImplementedError (plus
    # a helpful message).
    except OSError:
        help_str = '"%s" does not appear to be installed '\
            'or on the path, so this method is '\
            'disabled.  Please install a more recent '\
            'version of BEDTools and re-import to '\
            'use this method.' % prog
        not_implemented = True

    def decorator(func):
        """
        Accepts a function to be wrapped; discards the original and returns a
        new, rebuilt-from-scratch function based on the kwargs passed to
        _wraps().
        """
        # Register the implicit (as well as bam and other) args in the global
        # registry.  BedTool.handle_kwargs() will access these at runtime.  The
        # registry is keyed by program name (like intersectBed).
        _implicit_registry[prog] = implicit
        if other is not None:
            _other_registry[prog] = other
        if bam is not None:
            _bam_registry[prog] = bam

        # Here's where we replace an unable-to-be-found program's method with
        # one that only returns a NotImplementedError
        if not_implemented:
            def not_implemented_func(*args, **kwargs):
                raise NotImplementedError(help_str)
            return not_implemented_func

        _add_doc = []
        if implicit:
            _add_doc.append(dedent(
                """
                    For convenience, the file or stream this BedTool points to
                    is implicitly passed as the `-%s` argument to `%s`
                """
                % (implicit, prog)))

        if uses_genome:
            _add_doc.append(dedent(
                """
                    There are two alternatives for supplying a genome.  Use
                    `g="genome.filename"` if you have a genome's chrom sizes
                    saved as a file. This is the what BEDTools expects when
                    using it from the command line. Alternatively, use the
                    `genome="assembly.name"` (for example, `genome="hg19"`) to
                    use chrom sizes for that assembly without having to manage
                    a separate file.  The `genome` argument triggers a call
                    `pybedtools.chromsizes`, so see that method for more
                    details.
                """))

        def wrapped(self, *args, **kwargs):
            """
            A newly created function that will be returned by the _wraps()
            decorator
            """

            # Only one non-keyword argument is supported; this is then assumed
            # to be "other" (e.g., `-b` for intersectBed)
            if len(args) > 0:
                assert len(args) == 1
                kwargs[other] = args[0]

            # Add the implicit values to kwargs.  If the current BedTool is
            # BAM, it will automatically be passed to the appropriate
            # BAM-support arg (like `-abam`).  But this also allows the user to
            # explicitly specify the abam kwarg, which will override the
            # auto-substitution.
            # Note: here, `implicit` is something like "a"; `bam` is something
            # like "abam"
            if (implicit not in kwargs) \
                    and (bam not in kwargs) and (implicit is not None):
                if not self._isbam:
                    kwargs[implicit] = self.fn
                else:
                    # It is a bam file.  If this program supports BAM as the
                    # first input, then we set it here
                    if bam is not None:
                        kwargs[bam] = self.fn

                    # Otherwise, BEDTools can't currently handle it, so raise
                    # an exception.
                    else:
                        raise BEDToolsError(
                            '"%s" currently can\'t handle BAM '
                            'input, please use bam_to_bed() first.' % prog)

            # Should this function handle genome files?
            check_for_genome = uses_genome
            if uses_genome:
                if genome_none_if:
                    for i in genome_none_if:
                        if i in kwargs or i == implicit:
                            check_for_genome = False

                    # for genomecov, if -ibam then -g is optional.  So it's OK
                    # for the user to provide genome or g kwargs, even if
                    # -ibam.
                    if genome_ok_if:
                        for i in genome_ok_if:
                            if i in kwargs or i == implicit:
                                if ('g' in kwargs) or ('genome' in kwargs):
                                    check_for_genome = True
                if genome_if:
                    check_for_genome = False
                    for i in genome_if:
                        if (i in kwargs) or (i == implicit):
                            check_for_genome = True
            if check_for_genome:
                kwargs = self.check_genome(**kwargs)

            # For sequence methods, we may need to make a tempfile that will
            # hold the resulting sequence.  For example, fastaFromBed needs to
            # make a tempfile for 'fo' if no 'fo' was explicitly specified by
            # the user.
            if make_tempfile_for is not None:
                if make_tempfile_for not in kwargs:
                    kwargs[make_tempfile_for] = self._tmp()

            # At runtime, this will parse the kwargs, convert streams to
            # tempfiles if needed, and return all the goodies
            cmds, tmp, stdin = self.handle_kwargs(prog=prog, **kwargs)

            # Decide whether the output is BAM format or not.
            result_is_bam = False

            # By default, if the current BedTool is BAM, then the result should
            # be, too.
            if self._isbam:
                result_is_bam = True

            # If nonbam is "ALL", then this method will never return BAM
            # output.
            if nonbam == 'ALL':
                result_is_bam = False

            # If any of the `nonbam` args are found in kwargs, then result is
            # not a BAM.  Side note: the _nonbam name mangling is necessary to
            # keep the nonbam arg passed into the original _wraps() decorator
            # in scope.
            if nonbam is not None and nonbam != 'ALL':
                if isinstance(nonbam, six.string_types):
                    _nonbam = [nonbam]
                else:
                    _nonbam = nonbam
                for i in _nonbam:
                    if i in kwargs:
                        result_is_bam = False
                        break

            if force_bam:
                result_is_bam = True

            decode_output = not result_is_bam

            # Do the actual call
            stream = call_bedtools(cmds, tmp, stdin=stdin,
                                   check_stderr=check_stderr,
                                   decode_output=decode_output,
                                   )

            if does_not_return_bedtool:
                return does_not_return_bedtool(stream, **kwargs)

            # Post-hoc editing of the BedTool -- for example, this is used for
            # the sequence methods to add a `seqfn` attribute to the resulting
            # BedTool.
            if add_to_bedtool is not None:
                for kw, attr in list(add_to_bedtool.items()):
                    if kw == 'stdout':
                        value = stream
                    else:
                        value = kwargs[kw]
                    setattr(self, attr, value)
                    result = self
            else:
                result = BedTool(stream)

            result._isbam = result_is_bam
            result._cmds = cmds
            del kwargs
            return result

        # Now add the edited docstring (original Python doctring plus BEDTools
        # help) to the newly created method above
        if func.__doc__ is None:
            orig = ''
        else:
            orig = func.__doc__

        wrapped.__doc__ = orig + "\n".join(_add_doc) + help_str

        # Add the original method's name to a new attribute so we can access it
        # when logging history
        wrapped._name = func.__name__

        return wrapped

    return decorator


class BedTool(object):
    TEMPFILES = filenames.TEMPFILES

    def __init__(self, fn=None, from_string=False, remote=False):
        """
        Wrapper around Aaron Quinlan's ``BEDtools`` suite of programs
        (https://github.com/arq5x/bedtools); also contains many useful
        methods for more detailed work with BED files.

        *fn* is typically the name of a BED-like file, but can also be
        one of the following:

            * a string filename
            * another BedTool object
            * an iterable of Interval objects
            * an open file object
            * a "file contents" string (see below)

        If *from_string* is True, then you can pass a string that contains
        the contents of the BedTool you want to create.  This will treat all
        spaces as TABs and write to tempfile, treating whatever you pass as
        *fn* as the contents of the bed file.  This also strips empty lines.

        Typical usage is to point to an existing file::

            a = BedTool('a.bed')

        But you can also create one from scratch from a string::

            >>> s = '''
            ... chrX  1  100
            ... chrX 25  800
            ... '''
            >>> a = BedTool(s, from_string=True)

        Or use examples that come with pybedtools::

             >>> example_files = pybedtools.list_example_files()
             >>> assert 'a.bed' in example_files
             >>> a = pybedtools.example_bedtool('a.bed')

        """
        if remote:
            raise ValueError(
                "Remote BAM no longer supported (since BEDTools does not "
                "support it)")
        self.remote = remote
        self._isbam = False
        self._bam_header = ""
        if from_string:
            bed_contents = fn
            fn = self._tmp()
            fout = open(fn, 'w')
            for line in bed_contents.splitlines():
                if len(line.strip()) == 0:
                    continue
                line = '\t'.join(line.split()) + '\n'
                fout.write(line)
            fout.close()

        else:
            # our work is already done
            if isinstance(fn, BedTool):
                fn = fn.fn

            # from_string=False, so assume it's a filename
            elif isinstance(fn, six.string_types):
                if remote:
                    self._isbam = True
                else:
                    if not os.path.exists(fn):
                        msg = 'File "%s" does not exist' % fn
                        if six.PY2:
                            raise ValueError(msg)
                        raise FileNotFoundError(msg)
                    self._isbam = isBAM(fn)

                # TODO: we dont' really need this, but it's added here for
                # compatibility with existing tests
                if self._isbam:
                    header = pysam.Samfile(fn).header

                    # For example:
                    # {
                    #     'HD': {'VN': '1.0', 'SO': 'coordinate'},
                    #     'SQ': [
                    #         {'LN': 23011544,
                    #          'SN': 'chr2L'},
                    #         {'LN': 21146708,
                    #          'SN': 'chr2R'},
                    #         {'LN': 24543557,
                    #          'SN': 'chr3L'},
                    #         {'LN': 27905053,
                    #          'SN': 'chr3R'},
                    #         {'LN': 1351857,
                    #          'SN': 'chr4'},
                    #         {'LN': 22422827,
                    #          'SN': 'chrX'}
                    #     ]
                    # }


                    txt_header = []
                    for k, v in header.items():
                        if isinstance(v, list):
                            for i in v:
                                if isinstance(i, dict):
                                    txt_header.append(
                                        '\t'.join(
                                            ['@' + k] +
                                            [':'.join(map(str, j)) for j in sorted(i.items(), reverse=True)]))
                                elif isinstance(i, str):
                                    txt_header.append(i)

                        elif isinstance(v, dict):
                            txt_header.append(
                                '\t'.join(
                                    ['@' + k] +
                                    [':'.join(map(str, j)) for j in sorted(v.items(), reverse=True)]))
                        else:
                            raise ValueError("unhandled type in BAM header")
                    self._bam_header = '\n'.join(txt_header) + '\n'

            # If tuple or list, then save as file first
            # (fixes #73)
            elif isinstance(fn, (list, tuple)):
                fn = BedTool(iter(fn)).saveas().fn

            # Otherwise assume iterator, say an open file as from
            # subprocess.PIPE
            else:
                fn = fn

        self.fn = fn
        tag = ''.join(
            [random.choice(string.ascii_lowercase) for _ in range(8)])
        self._tag = tag
        _tags[tag] = self
        self._hascounts = False
        self._file_type = None
        self.history = History()

    @classmethod
    def from_dataframe(self, df, outfile=None,
                       sep='\t', header=False, na_rep='.', index=False,
                       **kwargs):
        """
        Creates a BedTool from a pandas.DataFrame.

        If `outfile` is None, a temporary file will be used. Otherwise it can
        be a specific filename or an open file handle. Additional kwargs will
        be passed to `pandas.DataFrame.to_csv`.

        The fields of the resulting BedTool will match the order of columns in
        the dataframe.
        """
        try:
            import pandas
        except ImportError:
            raise ImportError("pandas must be installed to use dataframes")
        if outfile is None:
            outfile = self._tmp()
        default_kwargs = dict(sep=sep, header=header, na_rep=na_rep,
                              index=index)
        default_kwargs.update(kwargs)
        df.to_csv(outfile, **default_kwargs)

        if isinstance(outfile, six.string_types):
            fn = outfile
        else:
            try:
                fn = outfile.name
            except AttributeError:
                raise ValueError(
                    "`outfile` is not a string and doesn't have a `name` attribute. "
                    "Unable to determine filename.")
        return BedTool(fn)

    def split(self, func, *args, **kwargs):
        """
        Split each feature using a user-defined function.

        Calls the provided function `func` with each interval.  In contrast to
        `each` (which does something similar), this method expects `func` to
        return an *iterable* of Interval objects.

        args and kwargs are passed directly to `func`.

        Returns a new BedTool.
        """
        def generator():
            for orig_interval in self:
                for interval in func(orig_interval, *args, **kwargs):
                    yield interval
        return BedTool(generator())

    def truncate_to_chrom(self, genome):
        """
        Ensure all features fall within chromosome limits.

        Some peak-callers extend peaks such that the boundaries overstep
        chromosome coordinates.  Upon uploading such a file to a genome browser
        like UCSC, this results in an error like::

            Error line 101 of custom track: chromEnd larger than chrom chr2
            size

        Use this method to clean your file, truncating any out-of-bounds
        features to fit within the chromosome coordinates of `genome`.

        `genome` can be either an assembly name ('dm3') or a dictionary where
        keys are chrom and values are (start, stop) tuples.
        """
        if isinstance(genome, dict):
            chromdict = genome
        else:
            assert isinstance(genome, six.string_types)
            chromdict = helpers.chromsizes(genome)

        tmp = self._tmp()
        fout = open(tmp, 'w')
        for chrom, coords in list(chromdict.items()):
            start, stop = coords
            start = str(start)
            stop = str(stop)
            fout.write('\t'.join([chrom, start, stop]) + '\n')
        fout.close()
        return self.intersect(tmp)

    def tabix_intervals(self, interval_or_string, check_coordinates=False):
        """
        Retrieve all intervals within coordinates from a "tabixed" BedTool.

        Given either a string in "chrom:start-stop" format, or an interval-like
        object with chrom, start, stop attributes, return a *streaming* BedTool
        of the features in this BedTool that overlap the provided interval.

        If the coordinates are invalid, an empty generator is returned unless
        `check_coordinates=True` in which case a ValueError will be raised.
        """
        if not self._tabixed():
            raise ValueError(
                "This BedTool has not been indexed for tabix "
                "-- please use the .tabix() method")

        # tabix expects 1-based coords, but BEDTools works with
        # zero-based. pybedtools and pysam also work with zero-based. So we can
        # pass zero-based directly to the pysam tabix interface.
        tbx = pysam.TabixFile(self.fn)

        # If an interval is passed, use its coordinates directly
        if isinstance(interval_or_string, Interval):
            interval = interval_or_string
            chrom, start, end = interval.chrom, interval.start, interval.stop
        # Parse string directly instead of relying on Interval, in order to
        # permit full chromosome fetching
        else:
            match = helpers.coord_re.search(interval_or_string)
            # Assume string is contig if it doesn't fit chrom:start-end format
            if match is None:
                chrom = interval_or_string
                start, end = None, None
            # Otherwise parse the coordinates
            else:
                chrom, start, end = match.group(1, 2, 3)
                start, end = int(start), int(end)

        # Fetch results.
        try:
            results = tbx.fetch(str(chrom), start, end)
        except ValueError:
            if check_coordinates:
                raise
            else:
                results = []

        # pysam.ctabix.TabixIterator does not include newlines when yielding so
        # we need to add them.
        def gen():
            for i in results:
                yield i + '\n'

        # xref #190
        x = BedTool(gen()).saveas()
        tbx.close()
        return x

    def tabix_contigs(self):
        """
        Returns a list of contigs from the tabix index.
        """
        if not self._tabixed():
            raise ValueError(
                "This BedTool has not been indexed for tabix "
                "-- please use the .tabix() method")

        tbx = pysam.TabixFile(self.fn)
        return tbx.contigs

    def tabix(self, in_place=True, force=False, is_sorted=False):
        """
        Prepare a BedTool for use with Tabix.

        Returns a new BedTool that has been BGZIP compressed
        and indexed by tabix.

        Parameters
        ----------

        in_place : bool
            If True (default), then assume the file is already sorted and
            replace the existing file with the BGZIPed version.

        force : bool
            If True (default is False), then overwrite both the index and the
            BGZIP file.

        is_sorted : bool
            If True (default is False), then assume the file is already sorted
            so that BedTool.bgzip() doesn't have to do that work.
        """
        # Return quickly if nothing to do
        if self._tabixed() and not force:
            return self

        # Make sure it's BGZIPed
        fn = self.bgzip(in_place=in_place, force=force)

        pysam.tabix_index(fn, force=force, preset=self.file_type)
        return BedTool(fn)

    def _tabixed(self):
        """
        Verifies that we're working with a tabixed file: a string filename
        pointing to a BGZIPed file with a .tbi file in the same dir.
        """
        if (
            isinstance(self.fn, six.string_types) and
            isBGZIP(self.fn) and
            os.path.exists(self.fn + '.tbi')
        ):
            return True

    def bgzip(self, in_place=True, force=False, is_sorted=False):
        """
        Helper function for more control over "tabixed" BedTools.

        Checks to see if we already have a BGZIP file; if not then prepare one.
        Always leaves the original file alone.  You can always just make a
        BedTool out of an already sorted and BGZIPed file to avoid this step.

        `in_place` will put the BGZIPed file in the same dir (possibly after
        sorting to tempfile).

        If `is_sorted`, then assume the file is already sorted. Otherwise call
        bedtools sort with the `-header` option.

        `force` will overwrite without asking.
        """
        if force:
            force_arg = "-f"
        else:
            force_arg = ""

        # It may already be BGZIPed...
        if isinstance(self.fn, six.string_types) and not force:
            if isBGZIP(self.fn):
                return self.fn

        # If not in_place, then make a tempfile for the BGZIPed version
        if not in_place:
            # Get tempfile name, sorted or not
            if not is_sorted:
                fn = self.sort(header=True).fn
            else:
                fn = self._tmp()

            # Register for later deletion
            outfn = fn + '.gz'
            BedTool.TEMPFILES.append(outfn)

            # Creates tempfile.gz
            pysam.tabix_compress(fn, outfn, force=force)
            return outfn

        # Otherwise, make sure the BGZIPed version has a similar name to the
        # current BedTool's file
        if in_place:
            if not is_sorted:
                fn = self.sort(header=True).saveas().fn
            else:
                fn = self.fn
            outfn = self.fn + '.gz'
            pysam.tabix_compress(fn, outfn, force=force)
            return outfn

    def delete_temporary_history(self, ask=True, raw_input_func=None):
        """
        Use at your own risk!  This method will delete temp files. You will be
        prompted for deletion of files unless you specify *ask=False*.

        Deletes all temporary files created during the history of this BedTool
        up to but not including the file this current BedTool points to.

        Any filenames that are in the history and have the following pattern
        will be deleted::

            <TEMP_DIR>/pybedtools.*.tmp

        (where <TEMP_DIR> is the result from get_tempdir() and is by default
        "/tmp")

        Any files that don't have this format will be left alone.

        (*raw_input_func* is used for testing)
        """
        flattened_history = _flatten_list(self.history)
        to_delete = []
        tempdir = get_tempdir()
        for i in flattened_history:
            fn = i.fn
            if fn.startswith(os.path.join(os.path.abspath(tempdir),
                                          'pybedtools')):
                if fn.endswith('.tmp'):
                    to_delete.append(fn)

        if raw_input_func is None:
            raw_input_func = input

        str_fns = '\n\t'.join(to_delete)
        if ask:
            answer = raw_input_func(
                'Delete these files?\n\t%s\n(y/N) ' % str_fns)

            if not answer.lower()[0] == 'y':
                print('OK, not deleting.')
                return
        for fn in to_delete:
            os.unlink(fn)
        return

    def _log_to_history(method):
        """
        Decorator to add a method and its kwargs to the history.

        Assumes that you only add this decorator to bedtool instances that
        return other bedtool instances
        """
        def decorated(self, *args, **kwargs):

            # this calls the actual method in the first place; *result* is
            # whatever you get back
            result = method(self, *args, **kwargs)

            # add appropriate tags
            parent_tag = self._tag
            result_tag = result._tag

            # log the sucka
            history_step = HistoryStep(method, args, kwargs, self, parent_tag,
                                       result_tag)

            # only add the current history to the new bedtool if there's
            # something to add
            if len(self.history) > 0:
                result.history.append(self.history)

            # but either way, add this history step to the result.
            result.history.append(history_step)

            return result

        decorated.__doc__ = method.__doc__
        return decorated

    def filter(self, func, *args, **kwargs):
        """
        Filter features by user-defined function.

        Takes a function *func* that is called for each feature in the
        `BedTool` object and returns only those for which the function returns
        True.

        *args and **kwargs are passed directly to *func*.

        Returns a streaming BedTool; if you want the filename then use the
        .saveas() method.

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> subset = a.filter(lambda b: b.chrom == 'chr1' and b.start < 150)
        >>> len(a), len(subset)
        (4, 2)

        so it has extracted 2 records from the original 4.

        """
        return BedTool((f for f in self if func(f, *args, **kwargs)))

    def field_count(self, n=10):
        """
        Number of fields in each line of this BedTool (checks `n` lines)

        Return the number of fields in the features this file contains.  Checks
        the first *n* features.

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> a.field_count()
        6
        """
        if self.file_type == 'empty':
            return 0
        i = 0
        fields = set([])
        for feat in self:
            if i > n:
                break
            i += 1
            # TODO: make this more efficient.
            fields.update([len(feat.fields)])
        assert len(fields) == 1, fields
        return list(fields)[0]

    def each(self, func, *args, **kwargs):
        """
        Modify each feature with a user-defined function.

        Applies user-defined function *func* to each feature.  *func* must
        accept an Interval as its first argument; *args and **kwargs will be
        passed to *func*.

        *func* must return an Interval object OR a value that evaluates to
        False, in which case the original feature will be removed from the
        output.  This way, an additional "filter" call is not necessary.

        >>> def truncate_feature(feature, limit=0):
        ...     feature.score = str(len(feature))
        ...     if len(feature) > limit:
        ...         feature.stop = feature.start + limit
        ...         feature.name = feature.name + '.short'
        ...     return feature

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> b = a.each(truncate_feature, limit=100)
        >>> print(b) #doctest: +NORMALIZE_WHITESPACE
        chr1	1	100	feature1	99	+
        chr1	100	200	feature2	100	+
        chr1	150	250	feature3.short	350	-
        chr1	900	950	feature4	50	+
        <BLANKLINE>

        """
        def _generator():
            for f in self:
                result = func(f, *args, **kwargs)
                if result:
                    yield result

        return BedTool(_generator())

    def introns(self, gene="gene", exon="exon"):
        """
        Create intron features (requires specific input format).

        Given a BED12 or a GFF with exons, create a new `BedTool` with just
        introns.  The output is a bed6 file with the score column (5) being one
        of 'intron'/'utr5'/'utr3'
        """
        # iterate over all the features in the gene.
        s = self.sort()
        if self.file_type == "gff":
            exon_iter = BedTool((f for f in s if f[2] == exon)).saveas()
            gene_iter = BedTool((f for f in s if f[2] == gene)).saveas()

        elif self.file_type == "bed":
            if s.field_count() == 12:
                exon_iter = s.bed6().saveas()
                gene_iter = s.saveas()
            else:
                # TODO: bed6. groupby on name and find smallest start,
                # largest stop.
                exon_iter = s
                gene_iter = None
                raise NotImplementedError('.introns() only supported for bed12'
                                          'and GFF')

        else:
            raise NotImplementedError(
                '.introns() only supported for BED and GFF')

        fh = open(BedTool._tmp(), "w")

        # group on the name.
        exon_intervals = IntervalFile(exon_iter.fn)
        for g in gene_iter:
            # search finds all, but we just want the ones that completely
            # overlap this gene.
            exons = [
                e for e in exon_intervals.search(g, same_strand=True)
                if e.start >= g.start and e.end <= g.end]

            for i, exon in enumerate(exons):
                # 5' utr between gene start and first intron
                if i == 0 and exon.start > g.start:
                    utr = {"+": "utr5", "-": "utr3"}[g.strand]
                    print("%s\t%i\t%i\t%s\t%s\t%s"
                          % (g.chrom, g.start, exon.start, g.name, utr,
                             g.strand), file=fh)
                elif i == len(exons) - 1 and exon.end < g.end:
                    utr = {"+": "utr3", "-": "utr5"}[g.strand]
                    print("%s\t%i\t%i\t%s\t%s\t%s"
                          % (g.chrom, exon.end, g.end, g.name, utr, g.strand),
                          file=fh)
                elif i != len(exons) - 1:
                    istart = exon.end
                    iend = exons[i + 1].start
                    print("%s\t%i\t%i\t%s\tintron\t%s"
                          % (g.chrom, istart, iend, g.name, g.strand), file=fh)
        fh.close()
        return BedTool(fh.name)

    def features(self):
        """
        Returns an iterable of features
        """
        if hasattr(self, 'next') or hasattr(self, '__next__'):
            return self
        return iter(self)

    @property
    def file_type(self):
        """
        Return the type of the current file.  One of ('bed','vcf','gff', 'bam',
        'sam', 'empty').

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> print(a.file_type)
        bed
        """
        if not isinstance(self.fn, six.string_types):
            raise ValueError('Checking file_type not supported for '
                             'non-file BedTools. Use .saveas() to '
                             'save as a temp file first.')
        if self._isbam:
            self._file_type = 'bam'
        else:
            try:
                self._file_type = six.advance_iterator(iter(self)).file_type
            except StopIteration:
                self._file_type = 'empty'

        return self._file_type

    def cut(self, indexes, stream=False):
        """
        Analagous to unix `cut`.

        Similar to unix `cut` except indexes are 0-based, must be a list
        and the columns are returned in the order requested.

        This method returns a BedTool of results, which means that the indexes
        returned must be valid GFF/GTF/BED/SAM features.

        If you would like arbitrary columns -- say, just chrom and featuretype
        of a GFF, which would not comprise a valid feature -- then instead of
        this method, simply use indexes on each feature, e.g,

        >>> gff = pybedtools.example_bedtool('d.gff')
        >>> results = [(f[0], f[2]) for f in gff]

        In addition, `indexes` can contain keys of the GFF/GTF attributes, in
        which case the values are returned. e.g. 'gene_name' will return the
        corresponding name from a GTF, or 'start' will return the start
        attribute of a BED Interval.
        """
        if stream:
            return BedTool(([f[attr] for attr in indexes] for f in self))
        else:
            fh = open(self._tmp(), "w")
            for f in self:
                print(
                    "\t".join(map(str, [f[attr] for attr in indexes])),
                    file=fh)
            fh.close()
            return BedTool(fh.name)

    @classmethod
    def _tmp(self):
        '''
        Makes a tempfile and registers it in the BedTool.TEMPFILES class
        variable.  Adds a "pybedtools." prefix and ".tmp" extension for easy
        deletion if you forget to call pybedtools.cleanup().
        '''
        tmpfn = tempfile.NamedTemporaryFile(prefix=settings.tempfile_prefix,
                                            suffix=settings.tempfile_suffix,
                                            delete=False)
        tmpfn = tmpfn.name
        BedTool.TEMPFILES.append(tmpfn)
        return tmpfn

    def __iter__(self):
        """
        Dispatches the right iterator depending on how this BedTool was
        created
        """
        if self._isbam:
            # Note: BAM class takes filename or stream, so self.fn is OK
            # here
            return BAM(self.fn)

        # Plain ol' filename
        if isinstance(self.fn, six.string_types):
            if not os.path.exists(self.fn):
                raise BedToolsFileError("{0} does not exist".format(self.fn))
            if isGZIP(self.fn):
                return IntervalIterator(gzip.open(self.fn, 'rt'))
            else:
                return IntervalIterator(open(self.fn, 'r'))
        # Any other kind of input (streaming string from stdout; iterable of
        # Intervals, iterable of (chrom, start, stop) tuples, etc are handled
        # appropriately by IntervalIterator.
        else:
            return IntervalIterator(self.fn)

    @property
    def intervals(self):
        if isinstance(self.fn, six.string_types):
            return IntervalFile(self.fn)
        else:
            raise ValueError("Please convert to a file-based BedTool using saveas")

    def __repr__(self):
        if isinstance(self.fn, six.string_types):
            if os.path.exists(self.fn) or self.remote:
                return '<BedTool(%s)>' % self.fn
            else:
                return '<BedTool(MISSING FILE: %s)>' % self.fn
        elif isinstance(self.fn, BedTool):
            return repr(self.fn)
        else:
            return '<BedTool(%s)>' % repr(self.fn)

    def __str__(self):
        """
        Returns the string representation of the whole `BedTool`
        """
        items = []
        for i in iter(self):
            i = str(i)
            if isinstance(i, bytes):
                i = i.decode('UTF-8')
            items.append(i)
        return ''.join(items)

    def __len__(self):
        return self.count()

    def __eq__(self, other):
        if isinstance(other, six.string_types):
            other_str = other
        elif isinstance(other, BedTool):
            if (not isinstance(self.fn, six.string_types) or
                not isinstance(
                    other.fn, six.string_types)):
                raise NotImplementedError('Testing equality only supported for'
                                          ' BedTools that point to files')
        if str(self) == str(other):
            return True
        return False

    def __ne__(self, other):
        return not self.__eq__(other)

    def __getitem__(self, key):
        if isinstance(key, slice):
            return islice(self, key.start, key.stop, key.step)
        elif isinstance(key, int):
            return list(islice(self, key, key + 1))[0]
        else:
            raise ValueError('Only slices or integers allowed for indexing '
                             'into a BedTool')

    def __add__(self, other):
        try:
            result = self.intersect(other, u=True)
        except BEDToolsError as e:
            # BEDTools versions <2.20 would raise BEDToolsError
            if (self.file_type == 'empty') or (other.file_type == 'empty'):
                result = pybedtools.BedTool("", from_string=True)
            else:
                raise e
        return result

    def __sub__(self, other):
        try:
            result = self.intersect(other, v=True)
        except BEDToolsError:
            # BEDTools versions <2.20 would raise BEDToolsError

            if (self.file_type == 'empty') and (other.file_type == 'empty'):
                result = pybedtools.BedTool("", from_string=True)
            elif other.file_type == 'empty':
                result = self.saveas()
            elif self.file_type == 'empty':
                result = pybedtools.BedTool("", from_string=True)
        return result

    def head(self, n=10, as_string=False):
        """
        Prints the first *n* lines or returns them if as_string is True

        Note that this only opens the underlying file (gzipped or not), so it
        does not check to see if the file is a valid BED file.

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> a.head(2) #doctest: +NORMALIZE_WHITESPACE
        chr1	1	100	feature1	0	+
        chr1	100	200	feature2	0	+
        <BLANKLINE>

        """
        if not isinstance(self.fn, six.string_types):
            raise NotImplementedError(
                'head() not supported for non file-based BedTools')
        if as_string:
            return ''.join(str(line) for line in self[:n])
        if self._isbam:
            raise NotImplementedError(
                'head() not supported for BAM')
        else:
            if isGZIP(self.fn):
                openfunc = gzip.open
                openmode = 'rt'
            else:
                openfunc = open
                openmode = 'r'
            with openfunc(self.fn, openmode) as fin:
                for i, line in enumerate(fin):
                    if i == (n):
                        break
                    print(line, end=' ')

    def set_chromsizes(self, chromsizes):
        """
        Prepare BedTool for operations that require chromosome coords.

        Set the chromsizes for this genome. If *chromsizes* is a string, it
        will be considered a genome assembly name.  If that assembly name is
        not available in pybedtools.genome_registry, then it will be searched
        for on the UCSC Genome Browser.

        Example usage:

            >>> hg19 = pybedtools.chromsizes('hg19')
            >>> a = pybedtools.example_bedtool('a.bed')
            >>> a = a.set_chromsizes(hg19)
            >>> print(a.chromsizes['chr1'])
            (0, 249250621)

        """
        if isinstance(chromsizes, six.string_types):
            self.chromsizes = pybedtools.chromsizes(chromsizes)
        elif isinstance(chromsizes, dict):
            self.chromsizes = chromsizes
        else:
            raise ValueError("Need to specify chromsizes either as a string"
                             " (assembly name) or a dictionary")
        return self

    def _collapse(self, iterable, fn=None, trackline=None, compressed=False):
        """
        Collapses an iterable into file *fn* (or a new tempfile if *fn* is
        None).

        Returns the newly created filename.
        """
        if fn is None:
            fn = self._tmp()

        # special case: if BAM-format BedTool is provided, no trackline should
        # be supplied, and don't iterate -- copy the file wholesale
        if isinstance(iterable, BedTool) and iterable._isbam:
            if trackline:
                raise ValueError("trackline provided, but input is a BAM "
                                 "file, which takes no track line")
            with open(fn, 'wb') as out_:
                out_.write(open(self.fn, 'rb').read())
            return fn


        if isinstance(iterable, BedTool) and isinstance(iterable.fn, six.string_types):
            if compressed:
                with gzip.open(fn, 'wt') as out_:
                    with open(iterable.fn, 'rt') as in_:
                        if trackline:
                            out_.write(trackline.strip() + '\n')
                        out_.writelines(in_)
            else:
                with open(fn, 'w') as out_:
                    with open(iterable.fn) as in_:
                        if trackline:
                            out_.write(trackline.strip() + '\n')
                        out_.writelines(in_)

        else:
            if compressed:
                with gzip.open(fn, 'wt') as out_:
                    for i in iterable:
                        if isinstance(i, (list, tuple)):
                            i = create_interval_from_list(list(i))
                        out_.write(str(i))
            else:
                with open(fn, 'w') as out_:
                    for i in iterable:
                        if isinstance(i, (list, tuple)):
                            i = create_interval_from_list(list(i))
                        out_.write(str(i))
        return fn

    def handle_kwargs(self, prog, **kwargs):
        """
        Handle most cases of BEDTool program calls, but leave the specifics
        up to individual methods.

        *prog* is a BEDTools program name, e.g., 'intersectBed'.

        *kwargs* are passed directly from the calling method (like
        self.intersect).

        This method figures out, given how this BedTool was constructed, what
        to send to BEDTools programs -- for example, an open file to stdin with
        the `-` argument, or a filename with the `-a` argument.
        """
        pybedtools.logger.debug(
            'BedTool.handle_kwargs() got these kwargs:\n%s',
            pprint.pformat(kwargs))

        # If you pass in a list, how should it be converted to a BedTools arg?
        default_list_delimiter = ' '
        list_delimiters = {
            'annotateBed': ' ',
            'getOverlap': ',',
            'groupBy': ',',
            'multiIntersectBed': ' ',
            'mergeBed': ',',
            'intersectBed': ' ',
            'mapBed': ',',
        }
        stdin = None

        # -----------------------------------------------------------------
        # Decide how to send instream1 to BEDTools.  If there's no implicit
        # instream1 arg, then do nothing.
        #
        try:
            # e.g., 'a' for intersectBed
            if self._isbam:
                inarg1 = _bam_registry[prog]
            else:
                inarg1 = _implicit_registry[prog]

            # e.g., self.fn or 'a.bed' or an iterator...
            instream1 = kwargs[inarg1]

            # If it's a BedTool, then get underlying stream
            if isinstance(instream1, BedTool):
                instream1 = instream1.fn

            # Filename? No pipe, just provide the file
            if isinstance(instream1, six.string_types):
                kwargs[inarg1] = instream1
                stdin = None

            # Open file? Pipe it
            # elif isinstance(instream1, file):
            #     kwargs[inarg1] = 'stdin'
            #     stdin = instream1

            # A generator or iterator: pipe it as a generator of lines
            else:
                kwargs[inarg1] = 'stdin'
                stdin = (str(i) for i in instream1)
        except KeyError:
            pass

        # -----------------------------------------------------------------
        # Decide how to send instream2 to BEDTools.
        try:
            # e.g., 'b' for intersectBed
            inarg2 = _other_registry[prog]

            # e.g., another BedTool
            instream2 = kwargs[inarg2]

            # Get stream if BedTool
            if isinstance(instream2, BedTool):
                instream2 = instream2.fn

            # Filename
            if isinstance(instream2, six.string_types):
                kwargs[inarg2] = instream2

            # If it's a list of strings, then we need to figure out if it's
            # a list of filenames or a list of intervals (see issue #156)
            #
            # Several options:
            #
            #   - assume intervals have tabs but filenames don't
            #   - assume that, upon being split on tabs, an interval is >=3 fields
            #   - try creating an interval out of the first thing, success means interval
            #
            # The last seems the most robust. It does allow filenames with
            # tabs; deciding whether or not such filenames are a good idea is
            # left to the user.
            #
            elif isinstance(instream2, (list, tuple)) and isinstance(instream2[0], six.string_types):
                try:
                    _ = create_interval_from_list(instream2[0].split('\t'))
                    kwargs[inarg2] = self._collapse(instream2)
                except IndexError:
                    kwargs[inarg2] = instream2

            # Otherwise we need to collapse it in order to send to BEDTools
            # programs
            else:
                kwargs[inarg2] = self._collapse(instream2)

        except KeyError:
            pass

        # If stream not specified, then a tempfile will be created
        if kwargs.pop('stream', None):
                tmp = None
        else:
            output = kwargs.pop('output', None)
            if output:
                tmp = output
            else:
                tmp = self._tmp()

        additional_args = kwargs.pop('additional_args', None)

        # Parse the kwargs into BEDTools-ready args
        cmds = [prog]

        # FIXME: the reverse-sort is a temp fix for issue #81.
        for key, value in sorted(list(kwargs.items()), reverse=True):
            if isinstance(value, bool):
                if value:
                    cmds.append('-' + key)
                else:
                    continue
            elif isinstance(value, list) or isinstance(value, tuple):
                value = list(map(str, value))
                try:
                    delim = list_delimiters[prog]
                except KeyError:
                    delim = default_list_delimiter

                if delim == ' ':
                    cmds.append('-' + key)
                    cmds.extend(value)

                # make comma-separated list if that's what's needed
                else:
                    cmds.append('-' + key)
                    cmds.append(delim.join(value))

            else:
                cmds.append('-' + key)
                cmds.append(str(value))

        if additional_args:
            cmds.append(additional_args)

        return cmds, tmp, stdin

    def check_genome(self, **kwargs):
        """
        Handles the different ways of specifying a genome in kwargs:

        g='genome.file' specifies a file directly
        genome='dm3' gets the file from genome registry
        self.chromsizes could be a dict.\
        """

        # If both g and genome are missing, assume self.chromsizes
        if ('g' not in kwargs) and ('genome' not in kwargs):
            if hasattr(self, 'chromsizes'):
                kwargs['g'] = self.chromsizes
            else:
                raise ValueError('No genome specified. Use the "g" or '
                                 '"genome" kwargs, or use the '
                                 '.set_chromsizes() method')

        # If both specified, rather than make an implicit decision, raise an
        # exception
        if 'g' in kwargs and 'genome' in kwargs:
            raise ValueError('Cannot specify both "g" and "genome"')

        # Something like genome='dm3' was specified
        if 'g' not in kwargs and 'genome' in kwargs:
            if isinstance(kwargs['genome'], dict):
                genome_dict = kwargs['genome']
            else:
                genome_dict = pybedtools.chromsizes(kwargs['genome'])
            genome_file = pybedtools.chromsizes_to_file(genome_dict)
            kwargs['g'] = genome_file
            del kwargs['genome']

        # By the time we get here, 'g' is specified.

        # If a dict was provided, convert to tempfile here
        if isinstance(kwargs['g'], dict):
            kwargs['g'] = pybedtools.chromsizes_to_file(kwargs['g'])

        if not os.path.exists(kwargs['g']):
            msg = 'Genome file "%s" does not exist' % (kwargs['g'])
            if six.PY2:
                raise ValueError(msg)
            raise FileNotFoundError(msg)

        return kwargs

    @_log_to_history
    def remove_invalid(self):
        """
        Remove invalid features that may break BEDTools programs.

        >>> a = pybedtools.BedTool("chr1 10 100\\nchr1 10 1",
        ... from_string=True)
        >>> print(a.remove_invalid()) #doctest: +NORMALIZE_WHITESPACE
        chr1	10	100
        <BLANKLINE>

        """
        tmp = self._tmp()
        fout = open(tmp, 'w')

        # If it's a file-based BedTool -- which is likely, if we're trying to
        # remove invalid features -- then we need to parse it line by line.
        if isinstance(self.fn, six.string_types):
            i = IntervalIterator(open(self.fn, 'r'))
        else:
            i = IntervalIterator(self.fn)

        def _generator():
            while True:
                try:
                    feature = next(i)
                    if feature.start <= feature.stop:
                        yield feature
                    else:
                        continue
                except pybedtools.MalformedBedLineError:
                    continue
                except OverflowError:
                    # This can happen if coords are negative
                    continue
                except IndexError:
                    continue
                except StopIteration:
                    break
        return BedTool(_generator())

    def all_hits(self, interval, same_strand=False, overlap=0.0):
        """
        Return all intervals that overlap `interval`.

        Calls the `all_hits` method of an IntervalFile to return all intervals
        in this current BedTool that overlap `interval`.

        Require that overlaps have the same strand with same_strand=True.

        Notes:
                If this current BedTool is generator-based, it will be
                converted into a file first.

                If this current BedTool refers to a BAM file, it will be
                converted to a BED file first using default arguments.  If you
                don't want this to happen, please convert to BED first before
                using this method.
        """
        if not isinstance(interval, Interval):
            raise ValueError("Need an Interval instance")
        fn = self.fn
        if not isinstance(fn, six.string_types):
            fn = self.saveas().fn
        if self._isbam:
            fn = self.bam_to_bed().fn
        interval_file = pybedtools.IntervalFile(fn)
        return interval_file.all_hits(interval, same_strand, overlap)

    def any_hits(self, interval, same_strand=False, overlap=0.0):
        """
        Return whether or not any intervals overlap `interval`.

        Calls the `any_hits` method of an IntervalFile.  If there were any hits
        within `interval` in this BedTool, then return 1; otherwise 0.

        Require that overlaps have the same strand with same_strand=True.

        Notes:
                If this current BedTool is generator-based, it will be
                converted into a file first.

                If this current BedTool refers to a BAM file, it will be
                converted to a BED file first using default arguments.  If you
                don't want this to happen, please convert to BED first before
                using this method.
        """
        if not isinstance(interval, Interval):
            raise ValueError("Need an Interval instance")
        fn = self.fn
        if not isinstance(fn, six.string_types):
            fn = self.saveas().fn
        if self._isbam:
            fn = self.bam_to_bed().fn
        interval_file = pybedtools.IntervalFile(fn)
        return interval_file.any_hits(interval, same_strand, overlap)

    def count_hits(self, interval, same_strand=False, overlap=0.0):
        """
        Return the number of intervals that overlap `interval`.

        Calls the `count_hits` method of an IntervalFile.  Returns the number
        of valid hits in this BedTool that overlap `interval`.

        Require that overlaps have the same strand with same_strand=True.

        Notes:
                If this current BedTool is generator-based, it will be
                converted into a file first.

                If this current BedTool refers to a BAM file, it will be
                converted to a BED file first using default arguments.  If you
                don't want this to happen, please convert to BED first before
                using this method.
        """
        if not isinstance(interval, Interval):
            raise ValueError("Need an Interval instance")
        fn = self.fn
        if not isinstance(fn, six.string_types):
            fn = self.saveas().fn
        if self._isbam:
            fn = self.bam_to_bed().fn
        interval_file = pybedtools.IntervalFile(fn)
        return interval_file.count_hits(interval, same_strand, overlap)

    @_log_to_history
    @_wraps(prog='bed12ToBed6', implicit='i', bam=None, other=None)
    def bed6(self, **kwargs):
        """
        Wraps `bedtools bed12tobed6`.
        """
        pass

    # Alias for backward compatibility
    bed12tobed6 = bed6

    @_log_to_history
    @_wraps(prog='bamToBed', implicit='i', other=None, nonbam='ALL', bam='i')
    def bam_to_bed(self, **kwargs):
        """
        Wraps `bedtools bamtobed`.
        """
    # Alias for backward compatibility
    bamtobed = bam_to_bed

    @_wraps(prog='bedToBam', implicit='i', uses_genome=True, force_bam=True)
    def _bed_to_bam(self):
        """
        Wraps bedToBam and is called internally for BED/GFF/VCF files by
        self.to_bam (which needs to do something different for SAM files...)
        """

    @_log_to_history
    def to_bam(self, **kwargs):
        """
        Wraps `bedtools bedtobam`

        If self.fn is in BED/VCF/GFF format, call BEDTools' bedToBam.  If
        self.fn is in SAM format, then create a header out of the genome file
        and then convert using `samtools`.
        """
        if self.file_type == 'bam':
            return self
        if self.file_type in ('bed', 'gff', 'vcf'):
            return self._bed_to_bam(**kwargs)

        # TODO: to maintain backwards compatibility we go from Interval to
        # AlignedSegment.
        if self.file_type == 'sam':

            # Use pysam, but construct the header out of a provided genome
            # file.

            # construct a genome out of whatever kwargs were passed in
            kwargs = self.check_genome(**kwargs)

            # Build a header that we can use for the output BAM file.
            genome = dict(i.split() for i in open(kwargs['g']))
            SQ = []
            ref_ids = {}
            text_header = ['@HD\tVN:1.0']

            for i, (k, v) in enumerate(genome.items()):
                SQ.append(dict(SN=k, LN=int(v)))
                ref_ids[k] = i
                text_header.append('@SQ\tSN:{0}\tLN:{1}'.format(k, v))


            # Here's the pysam-formatted header
            header = {'HD': {'VN': '1.0'},
                      'SQ': SQ}

            # And the text-format header
            text_header = '\n'.join(text_header) + '\n'

            # The strategy is to write an actual SAM file to disk, along with
            # a header, and then read that back in.
            #
            # Painfully inefficient, but this will change once all py2 tests
            # pass.
            sam_tmp = self._tmp()
            bam_tmp = self._tmp()
            with open(sam_tmp, 'w') as fout:
                fout.write(text_header)
                for interval in self:
                    fout.write('\t'.join(map(str, interval.fields)) + '\n')

            samfile = pysam.AlignmentFile(sam_tmp, 'r')
            bamfile = pysam.AlignmentFile(bam_tmp, 'wb', template=samfile)
            for alignment in samfile:
                bamfile.write(alignment)

            samfile.close()
            bamfile.close()
            new_bedtool = BedTool(bam_tmp)
            new_bedtool._isbam = True
            return new_bedtool
    # Alias for backward compatibility
    bedtobam = to_bam

    @_log_to_history
    @_wraps(prog='intersectBed', implicit='a', other='b', bam='abam',
            nonbam='bed')
    def intersect(self):
        """
        Wraps `bedtools intersect`.
        """

    @_log_to_history
    @_wraps(prog='fastaFromBed', implicit='bed', bam=None, other='fi',
            make_tempfile_for='fo', check_stderr=_check_sequence_stderr,
            add_to_bedtool={'fo': 'seqfn'})
    def sequence(self):
        '''
        Wraps `bedtools getfasta`.

        *fi* is passed in by the user; *bed* is automatically passed in as the
        bedfile of this object; *fo* by default is a temp file.  Use
        save_seqs() to save as a file.

        The end result is that this BedTool will have an attribute, self.seqfn,
        that points to the new fasta file.

        Example usage:

        >>> a = pybedtools.BedTool("""
        ... chr1 1 10
        ... chr1 50 55""", from_string=True)
        >>> fasta = pybedtools.example_filename('test.fa')
        >>> a = a.sequence(fi=fasta)
        >>> print(open(a.seqfn).read())
        >chr1:1-10
        GATGAGTCT
        >chr1:50-55
        CCATC
        <BLANKLINE>

        '''
    # Alias for backwards compatibility
    getfasta = sequence

    @staticmethod
    def seq(loc, fasta):
        """
        Return just the sequence from a region string or a single location
        >>> fn = pybedtools.example_filename('test.fa')
        >>> BedTool.seq('chr1:2-10', fn)
        'GATGAGTCT'
        >>> BedTool.seq(('chr1', 1, 10), fn)
        'GATGAGTCT'
        """
        if isinstance(loc, six.string_types):
            chrom, start_end = loc.split(":")
            start, end = list(map(int, start_end.split("-")))
            start -= 1
        else:
            chrom, start, end = loc[0], loc[1], loc[2]

        loc = BedTool("%s\t%i\t%i" % (chrom, start, end), from_string=True)
        lseq = loc.sequence(fi=fasta)
        return "".join(
            [l.rstrip() for l in open(lseq.seqfn, 'r')
             if l[0] != ">"])

    @_log_to_history
    @_wraps(prog='nucBed', implicit='bed', other='fi',
            check_stderr=_check_sequence_stderr)
    def nucleotide_content(self):
        """
        Wraps `bedtools nuc`.

        Profiles nucleotide content.  The returned BED file contains extra
        information about the nucleotide content
        """
    # Alias for backwards compatibility
    nuc = nucleotide_content

    @_log_to_history
    @_wraps(prog='multiBamCov', implicit='bed')
    def multi_bam_coverage(self):
        """
        Wraps `bedtools multicov`.

        Pass a list of sorted and indexed BAM files as `bams`
        """
    # Alias for backwards compatibility
    multicov = multi_bam_coverage

    @_log_to_history
    @_wraps(prog='subtractBed', implicit='a', other='b', bam=None)
    def subtract(self):
        """
        Wraps `bedtools subtract`.

        Subtracts from another BED file and returns a new BedTool object.

        Example usage:

            >>> a = pybedtools.example_bedtool('a.bed')
            >>> b = pybedtools.example_bedtool('b.bed')

            Do a "stranded" subtraction:

            >>> c = a.subtract(b, s=True)

            Require 50% of features in `a` to overlap:

            >>> c = a.subtract(b, f=0.5)
        """
        kwargs['b'] = b

        if 'a' not in kwargs:
            kwargs['a'] = self.fn

        cmds, tmp, stdin = self.handle_kwargs(prog='subtractBed', **kwargs)
        stream = call_bedtools(cmds, tmp, stdin=stdin)
        return BedTool(stream)

    @_log_to_history
    @_wraps(prog='slopBed', implicit='i', other=None, bam=None,
            uses_genome=True)
    def slop(self):
        """
        Wraps `bedtools slop`.
        """

    @_log_to_history
    @_wraps(prog='shiftBed', implicit='i', other=None, bam=None,
            uses_genome=True)
    def shift(self):
        """
        Wraps `bedtools shift`.

        Shift each feature by user-defined number of bases. Returns a new BedTool object.

        Example usage:

            >>> a = pybedtools.example_bedtool('a.bed')

            Shift every feature by 5bp:

            >>> b = a.shift(genome='hg19', s=5)
            >>> print(b) #doctest: +NORMALIZE_WHITESPACE
            chr1	6	105	feature1	0	+
            chr1	105	205	feature2	0	+
            chr1	155	505	feature3	0	-
            chr1	905	955	feature4	0	+
            <BLANKLINE>

            Shift features on the '+' strand by -1bp and on '-' strand by +3bp:

            >>> b = a.shift(genome='hg19', p=-1, m=3)
            >>> print(b) #doctest: +NORMALIZE_WHITESPACE
            chr1	0	99	feature1	0	+
            chr1	99	199	feature2	0	+
            chr1	153	503	feature3	0	-
            chr1	899	949	feature4	0	+
            <BLANKLINE>

            Shift features by a fraction of their length (0.50):

            >>> b = a.shift(genome='hg19', pct=True, s=0.50)
            >>> print(b) #doctest: +NORMALIZE_WHITESPACE
            chr1	50	149	feature1	0	+
            chr1	150	250	feature2	0	+
            chr1	325	675	feature3	0	-
            chr1	925	975	feature4	0	+
            <BLANKLINE>

        """

    @_log_to_history
    @_wraps(prog='mergeBed', implicit='i', other=None, bam=None)
    def merge(self):
        """
        Wraps `bedtools merge`.

        Merge overlapping features together. Returns a new BedTool object.

        Example usage:

            >>> a = pybedtools.example_bedtool('a.bed')

            Merge:

            >>> c = a.merge()

            Allow merging of features 500 bp apart:

            >>> c = a.merge(d=500)

        """

    @_log_to_history
    @_wraps(prog='closestBed', implicit='a', other='b', bam=None)
    def closest(self):
        """
        Wraps `bedtools closest`.

        Return a new BedTool object containing closest features in *b*.  Note
        that the resulting file is no longer a valid BED format; use the
        special "_closest" methods to work with the resulting file.

        Example usage::

            a = BedTool('in.bed')

            # get the closest feature in 'other.bed' on the same strand
            b = a.closest('other.bed', s=True)

        """

    @_log_to_history
    @_wraps(prog='windowBed', implicit='a', other='b', bam='abam',
            nonbam='bed')
    def window(self):
        """
        Wraps `bedtools window`.

        Example usage::

            >>> a = pybedtools.example_bedtool('a.bed')
            >>> b = pybedtools.example_bedtool('b.bed')
            >>> print(a.window(b, w=1000)) #doctest: +NORMALIZE_WHITESPACE
            chr1	1	100	feature1	0	+	chr1	155	200	feature5	0	-
            chr1	1	100	feature1	0	+	chr1	800	901	feature6	0	+
            chr1	100	200	feature2	0	+	chr1	155	200	feature5	0	-
            chr1	100	200	feature2	0	+	chr1	800	901	feature6	0	+
            chr1	150	500	feature3	0	-	chr1	155	200	feature5	0	-
            chr1	150	500	feature3	0	-	chr1	800	901	feature6	0	+
            chr1	900	950	feature4	0	+	chr1	155	200	feature5	0	-
            chr1	900	950	feature4	0	+	chr1	800	901	feature6	0	+
            <BLANKLINE>
        """

    @_log_to_history
    @_wraps(prog='shuffleBed', implicit='i', other=None, bam=None,
            uses_genome=True)
    def shuffle(self):
        """
        Wraps `bedtools shuffle`.

        Example usage:

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> seed = 1 # so this test always returns the same results
        >>> b = a.shuffle(genome='hg19', chrom=True, seed=seed)
        >>> print(b) #doctest: +NORMALIZE_WHITESPACE
        chr1	46341498	46341597	feature1	0	+
        chr1	45615582	45615682	feature2	0	+
        chr1	102762672	102763022	feature3	0	-
        chr1	17293432	17293482	feature4	0	+
        <BLANKLINE>
        """

    @_log_to_history
    @_wraps(prog='sortBed', implicit='i')
    def sort(self):
        """
        Wraps `bedtools sort`.

        Note that chromosomes are sorted lexograpically, so chr12 will come
        before chr9.

        Example usage:

        >>> a = pybedtools.BedTool('''
        ... chr9 300 400
        ... chr1 100 200
        ... chr1 1 50
        ... chr12 1 100
        ... chr9 500 600
        ... ''', from_string=True)
        >>> print(a.sort()) #doctest: +NORMALIZE_WHITESPACE
        chr1	1	50
        chr1	100	200
        chr12	1	100
        chr9	300	400
        chr9	500	600
        <BLANKLINE>
        """

    @_log_to_history
    @_wraps(prog='annotateBed', implicit='i')
    def annotate(self):
        """
        Wraps  `bedtools annotate`.

        Annotate this BedTool with a list of other files.
        Example usage:

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> b_fn = pybedtools.example_filename('b.bed')
        >>> print(a.annotate(files=b_fn)) #doctest: +NORMALIZE_WHITESPACE
        chr1	1	100	feature1	0	+	0.000000
        chr1	100	200	feature2	0	+	0.450000
        chr1	150	500	feature3	0	-	0.128571
        chr1	900	950	feature4	0	+	0.020000
        <BLANKLINE>
        """

    @_log_to_history
    @_wraps(prog='flankBed', implicit='i', uses_genome=True)
    def flank(self):
        """
        Wraps `bedtools flank`.

        Example usage:

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> print(a.flank(genome='hg19', b=100)) #doctest: +NORMALIZE_WHITESPACE
        chr1	0	1	feature1	0	+
        chr1	100	200	feature1	0	+
        chr1	0	100	feature2	0	+
        chr1	200	300	feature2	0	+
        chr1	50	150	feature3	0	-
        chr1	500	600	feature3	0	-
        chr1	800	900	feature4	0	+
        chr1	950	1050	feature4	0	+
        <BLANKLINE>

        """
        kwargs = self.check_genome(**kwargs)

        if 'i' not in kwargs:
            kwargs['i'] = self.fn

        cmds, tmp, stdin = self.handle_kwargs(prog='flankBed', **kwargs)
        stream = call_bedtools(cmds, tmp, stdin=stdin)
        return BedTool(stream)

    @_log_to_history
    @_wraps(prog='genomeCoverageBed', implicit='i', bam='ibam',
            genome_none_if=['ibam'], genome_ok_if=['ibam'], uses_genome=True,
            nonbam='ALL')
    def genome_coverage(self):
        """
        Wraps `bedtools genomecov`.

        Note that some invocations of `bedtools genomecov` do not result in
        a properly-formatted BED file. For example, the default behavior is to
        report a histogram of coverage. Iterating over the resulting,
        non-BED-format file will raise exceptions in pybedtools' parser.

        Consider using the `BedTool.to_dataframe` method to convert these
        non-BED files into a pandas DataFrame for further use.

        Example usage:

        BAM file input does not require a genome:

        >>> a = pybedtools.example_bedtool('x.bam')
        >>> b = a.genome_coverage(bg=True)
        >>> b.head(3) #doctest: +NORMALIZE_WHITESPACE
        chr2L	9329	9365	1
        chr2L	10212	10248	1
        chr2L	10255	10291	1

        Other input does require a genome:

        >>> a = pybedtools.example_bedtool('x.bed')
        >>> b = a.genome_coverage(bg=True, genome='dm3')
        >>> b.head(3) #doctest: +NORMALIZE_WHITESPACE
        chr2L	9329	9365	1
        chr2L	10212	10248	1
        chr2L	10255	10291	1

        Non-BED format results:
        >>> a = pybedtools.example_bedtool('x.bed')
        >>> b = a.genome_coverage(genome='dm3')
        >>> df = b.to_dataframe(names=['chrom', 'depth', 'n', 'chromsize', 'fraction'])
        """
    # Alias for backwards compatibility
    genomecov = genome_coverage

    @_log_to_history
    @_wraps(prog='coverageBed', implicit='a', other='b', bam='abam',
            nonbam='ALL')
    def coverage(self):
        """
        Wraps `bedtools coverage`.

        Note that starting in version 2.24.0, BEDTools swapped the semantics of
        the "a" and "b" files.

        Example usage:

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> b = pybedtools.example_bedtool('b.bed')
        >>> c = b.coverage(a)
        >>> c.head(3) #doctest: +NORMALIZE_WHITESPACE
        chr1	155	200	feature5	0	-	2	45	45	1.0000000
        chr1	800	901	feature6	0	+	1	1	101	0.0099010
        """

    @_log_to_history
    @_wraps(prog='maskFastaFromBed', implicit='bed', other='fi',
            make_tempfile_for='fo', add_to_bedtool={'fo': 'seqfn'},
            check_stderr=_check_sequence_stderr)
    def mask_fasta(self):
        """
        Wraps `bedtools maskfasta`.

        Masks a fasta file at the positions in a BED file and saves result as
        'out' and stores the filename in seqfn.

        >>> a = pybedtools.BedTool('chr1 100 110', from_string=True)
        >>> fasta_fn = pybedtools.example_filename('test.fa')
        >>> a = a.mask_fasta(fi=fasta_fn, fo='masked.fa.example')
        >>> b = a.slop(b=2, genome='hg19')
        >>> b = b.sequence(fi=a.seqfn)
        >>> print(open(b.seqfn).read())
        >chr1:98-112
        TTNNNNNNNNNNAT
        <BLANKLINE>
        >>> os.unlink('masked.fa.example')
        >>> if os.path.exists('masked.fa.example.fai'):
        ...     os.unlink('masked.fa.example.fai')
        """
    # Alias for backwards compatibility
    maskfasta = mask_fasta

    @_log_to_history
    @_wraps(prog='complementBed', implicit='i', uses_genome=True)
    def complement(self):
        """
        Wraps `bedtools complement`.
        Example usage:

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> a.complement(genome='hg19').head(5) #doctest: +NORMALIZE_WHITESPACE
        chr1	0	1
        chr1	500	900
        chr1	950	249250621
        chr10	0	135534747
        chr11	0	135006516
        """

    @_log_to_history
    @_wraps(prog='getOverlap', implicit='i')
    def overlap(self):
        """
        Wraps `bedtools overlap`.

        Example usage:

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> b = pybedtools.example_bedtool('b.bed')
        >>> c = a.window(b, w=10).overlap(cols=[2,3,8,9])
        >>> print(c) #doctest: +NORMALIZE_WHITESPACE
        chr1	100	200	feature2	0	+	chr1	155	200	feature5	0	-	45
        chr1	150	500	feature3	0	-	chr1	155	200	feature5	0	-	45
        chr1	900	950	feature4	0	+	chr1	800	901	feature6	0	+	1
        <BLANKLINE>
        """

    # TODO: needs test files and doctests written
    @_log_to_history
    @_wraps(prog='pairToBed', implicit='a', other='b', bam='abam',
            nonbam='bedpe')
    def pair_to_bed(self):
        """
        Wraps `bedtools pairtobed`.
        """
    # Alias for backwards compatibility
    pairtobed = pair_to_bed

    @_log_to_history
    @_wraps(prog='pairToPair', implicit='a', other='b')
    def pair_to_pair(self):
        """
        Wraps `bedtools pairtopair`.
        """
    # Alias for backwards compatibility
    pairtopair = pair_to_pair

    @_log_to_history
    @_wraps(prog='groupBy', implicit='i')
    def groupby(self):
        """
        Wraps `bedtools groupby`.

        Example usage:

        >>> a = pybedtools.example_bedtool('gdc.gff')
        >>> b = pybedtools.example_bedtool('gdc.bed')
        >>> c = a.intersect(b, c=True)
        >>> d = c.groupby(g=[1, 4, 5], c=10, o=['sum'])
        >>> print(d) #doctest: +NORMALIZE_WHITESPACE
        chr2L	41	70	0
        chr2L	71	130	2
        chr2L	131	170	4
        chr2L	171	200	0
        chr2L	201	220	1
        chr2L	41	130	2
        chr2L	171	220	1
        chr2L	41	220	7
        chr2L	161	230	6
        chr2L	41	220	7
        <BLANKLINE>

        """

    @_log_to_history
    @_wraps(prog='tagBam', implicit='i', bam='i')
    def tag_bam(self):
        """
        Wraps `bedtools tag`.

        `files` and `labels` should lists of equal length.

        """
    # Alias for backwards compatibility
    tag = tag_bam

    @_log_to_history
    @_wraps(prog='mapBed', implicit='a', other='b')
    def map(self):
        """
        Wraps  `bedtools map`; See also :meth:`BedTool.each`.
        """

    @_log_to_history
    @_wraps(prog='multiIntersectBed', uses_genome=True, genome_if=['empty'])
    def multi_intersect(self):
        """
        Wraps `bedtools multiintersect`.

        Provide a list of filenames as the "i" argument. e.g. if you already
        have BedTool objects then use their `.fn` attribute, like this::

            >>> x = pybedtools.BedTool()
            >>> a = pybedtools.example_bedtool('a.bed')
            >>> b = pybedtools.example_bedtool('b.bed')
            >>> result = x.multi_intersect(i=[a.fn, b.fn])
            >>> print(result)   #doctest: +NORMALIZE_WHITESPACE
            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
            <BLANKLINE>

        """

    # Alias for backwards compatibility
    multiinter = multi_intersect

    @_log_to_history
    @_wraps(prog='randomBed', uses_genome=True)
    def random(self):
        """
        Wraps `bedtools random`.

        Since this method does not operate on an existing file, create
        a BedTool with no arguments and then call this method, e.g.,

        >>> x = BedTool()
        >>> y = x.random(l=100, n=10, genome='hg19')
        """

    @_log_to_history
    @_wraps('bedpeToBam', implicit='i', uses_genome=True, force_bam=True)
    def bedpe_to_bam(self):
        """
        Wraps `bedtools bedpetobam`.
        """
    # Alias for backwards compatibility
    bedpetobam = bedpe_to_bam

    @_log_to_history
    @_wraps(prog='clusterBed', implicit='i')
    def cluster(self):
        """
        Wraps  `bedtools cluster`.
        """

    @_log_to_history
    @_wraps(prog='unionBedGraphs')
    def union_bedgraphs(self):
        """
        Wraps `bedtools unionbedg`.

        Warning: using the `header=True` kwarg will result in a file that is
        not in true BED format, which may break downstream analysis.
        """
    # Alias for backwards compatibility
    unionbedg = union_bedgraphs

    @_log_to_history
    @_wraps(prog='windowMaker', uses_genome=True, genome_none_if=['b'],
            other='b')
    def window_maker(self):
        """
        Wraps `bedtools makewindows`.
        """
    # Alias for backwards compatibility
    makewindows = window_maker

    @_log_to_history
    @_wraps(prog='expandCols', implicit='i')
    def expand(self):
        """
        Wraps `bedtools expand`
        """

    @_log_to_history
    @_wraps(prog='linksBed', implicit='i',
            add_to_bedtool={'stdout': 'links_html'})
    def links(self):
        """
        Wraps `linksBed`.

        The resulting BedTool will have a new attribute `links_html`.  This
        attribute is a temp filename containing the HTML links.
        """

    @_log_to_history
    @_wraps(prog='bedToIgv', implicit='i',
            add_to_bedtool={'stdout': 'igv_script'})
    def igv(self):
        """
        Wraps `bedtools igv`.

        The resulting BedTool will have a new attribute `igv_script`.  This
        attribute is a temp filename containing the IGV script.
        """

    @_log_to_history
    @_wraps(prog='bamToFastq', implicit='i', bam='i', make_tempfile_for='fq',
            add_to_bedtool={'fq': 'fastq'})
    def bam_to_fastq(self):
        """
        Wraps `bedtools bamtofastq`.

        The `fq` argument is required.

        The resulting BedTool will have a new attribute, `fastq`.
        """
    # Alias for backwards compatibility
    bamtofastq = bam_to_fastq

    @_wraps(prog='jaccard', implicit='a', other='b',
            does_not_return_bedtool=_jaccard_output_to_dict)
    def jaccard(self):
        """
        Returns a dictionary with keys (intersection, union, jaccard).
        """

    @_wraps(prog='reldist', implicit='a', other='b',
            does_not_return_bedtool=_reldist_output_handler)
    def reldist(self):
        """
        If detail=False, then return a dictionary with keys (reldist, count,
        total, fraction), which is the summary of the bedtools reldist.

        Otherwise return a BedTool, with the relative distance for each
        interval in A in the last column.
        """

    @_wraps(prog='sample', implicit='i', bam='i')
    def sample(self):
        """
        Wraps 'sample'.
        """

    @_wraps(prog='fisher', implicit='a', other='b', uses_genome=True,
            does_not_return_bedtool=helpers.FisherOutput)
    def fisher(self):
        """
        Wraps 'fisher'. Returns an object representing the output.

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> b = pybedtools.example_bedtool('b.bed')
        >>> f = a.fisher(b, genome='hg19')
        >>> print(f)  # doctest: +NORMALIZE_WHITESPACE
        # 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
        <BLANKLINE>


        >>> f.table['not in -a']['in -b']
        0

        >>> f.table['not in -a']['not in -b']
        13958444

        >>> f.table['in -a']['in -b']
        3

        >>> f.table['in -a']['not in -b']
        1

        >>> f.two_tail
        8.8247e-21
        """

    @_wraps(prog='split', implicit='i',
            does_not_return_bedtool=helpers.SplitOutput)
    def splitbed(self):
        """
        Wraps 'bedtools split'.

        BedTool objects have long had a `split` method which splits intervals
        according to a custom function. Now that BEDTools has a `split` tool,
        the method name conflicts. To maintain backwards compatibility, the
        method wrapping the BEDTools command is called `splitbed`.

        Since this tool does not return a single BED file, the method parses
        the output and returns a SplitOutput object, which includes an
        attribute, `bedtools`, that is a list of BedTool objects created from
        the split files.

        To keep the working directory clean, you may want to consider using
        `prefix=BedTool._tmp()` to get a temp file that will be deleted when
        Python exits cleanly.

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> s = a.splitbed(n=2, p="split")
        >>> assert len(a) == 4, len(a)
        >>> assert len(s.bedtools) == 2
        >>> print(s.bedtools[0]) # doctest: +NORMALIZE_WHITESPACE
        chr1	150	500	feature3	0	-
        <BLANKLINE>
        >>> print(s.bedtools[1]) # doctest: +NORMALIZE_WHITESPACE
        chr1	100	200	feature2	0	+
        chr1	1	100	feature1	0	+
        chr1	900	950	feature4	0	+
        <BLANKLINE>
        """

    @_wraps(prog='spacing', implicit='i')
    def spacing(self):
        """
        Wraps `bedtools spacing`

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> print(a.spacing())  # doctest: +NORMALIZE_WHITESPACE
        chr1	1	100	feature1	0	+	.
        chr1	100	200	feature2	0	+	0
        chr1	150	500	feature3	0	-	-1
        chr1	900	950	feature4	0	+	400
        """


    def count(self):
        """
        Count the number features in this BedTool.

        Number of features in BED file. Does the same thing as len(self), which
        actually just calls this method.

        Only counts the actual features.  Ignores any track lines, browser
        lines, lines starting with a "#", or blank lines.

        Example usage:

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> a.count()
        4
        """
        if hasattr(self, 'next') or hasattr(self, '__next__'):
            return sum(1 for _ in self)
        return sum(1 for _ in iter(self))

    def print_sequence(self):
        """
        Print the sequence that was retrieved by BedTool.sequence.

        See usage example in :meth:`BedTool.sequence`.
        """
        if not hasattr(self, 'seqfn'):
            raise ValueError('Use .sequence(fasta) to get the sequence first')
        f = open(self.seqfn)
        s = f.read()
        f.close()
        return s

    def save_seqs(self, fn):
        """
        Save sequences, after calling BedTool.sequence.

        In order to use this function, you need to have called
        the :meth:`BedTool.sequence()` method.

        A new BedTool object is returned which references the newly saved file.

        Example usage:

        >>> a = pybedtools.BedTool('''
        ... chr1 1 10
        ... chr1 50 55''', from_string=True)
        >>> fasta = pybedtools.example_filename('test.fa')
        >>> a = a.sequence(fi=fasta)
        >>> print(open(a.seqfn).read())
        >chr1:1-10
        GATGAGTCT
        >chr1:50-55
        CCATC
        <BLANKLINE>
        >>> b = a.save_seqs('example.fa')
        >>> assert open(b.fn).read() == open(a.fn).read()
        >>> if os.path.exists('example.fa'):
        ...     os.unlink('example.fa')
        """

        if not hasattr(self, 'seqfn'):
            raise ValueError('Use .sequence(fasta) to get the sequence first')
        fout = open(fn, 'w')
        fout.write(open(self.seqfn).read())
        fout.close()
        new_bedtool = BedTool(self.fn)
        new_bedtool.seqfn = fn
        return new_bedtool

    def randomstats(self, other, iterations, new=False, genome_fn=None,
                    include_distribution=False, **kwargs):
        """
        Dictionary of results from many randomly shuffled intersections.

        Sends args and kwargs to :meth:`BedTool.randomintersection` and
        compiles results into a dictionary with useful stats.  Requires
        numpy.

        If `include_distribution` is True, then the dictionary will include the
        full distribution; otherwise, the distribution is deleted and cleaned
        up to save on memory usage.

        This is one possible way of assigning significance to overlaps between
        two files. See, for example:

            Negre N, Brown CD, Shah PK, Kheradpour P, Morrison CA, et al. 2010
            A Comprehensive Map of Insulator Elements for the Drosophila
            Genome. PLoS Genet 6(1): e1000814. doi:10.1371/journal.pgen.1000814

        Example usage:

        Make chromsizes a very small genome for this example:

        >>> 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)
        ... except ImportError:
        ...     pass

        *results* is a dictionary that you can inspect.

        (Note that the following examples are not run as part of the doctests
        to avoid forcing users to install NumPy just to pass tests)

        The actual overlap::

            print(results['actual'])
            3

        The median of all randomized overlaps::

            print(results['median randomized'])
            2.0

        The percentile of the actual overlap in the distribution of randomized
        overlaps, which can be used to get an empirical p-value::

            print(results['percentile'])
            90.0
        """
        if ('intersect_kwargs' not in kwargs) or \
                (kwargs['intersect_kwargs'] is None):
            kwargs['intersect_kwargs'] = {'u': True}
        try:
            import numpy as np
        except ImportError:
            raise ImportError("Need to install NumPy for stats...")

        def percentileofscore(a, score):
            """
            copied from scipy.stats.percentileofscore, to avoid dependency on
            scipy.
            """
            a = np.array(a)
            n = len(a)

            if not(np.any(a == score)):
                a = np.append(a, score)
                a_len = np.array(list(range(len(a))))
            else:
                a_len = np.array(list(range(len(a)))) + 1.0

            a = np.sort(a)
            idx = [a == score]
            pct = (np.mean(a_len[idx]) / n) * 100.0
            return pct

        if isinstance(other, six.string_types):
            other = BedTool(other)
        else:
            assert isinstance(other, BedTool),\
                'Either filename or another BedTool instance required'

        # Actual (unshuffled) counts.
        i_kwargs = kwargs['intersect_kwargs']
        actual = len(self.intersect(other, **i_kwargs))

        # List of counts from randomly shuffled versions.
        # Length of counts == *iterations*.

        if not new:
            distribution = self.randomintersection(
                other, iterations=iterations, **kwargs)
        else:
            # use new mechanism
            if genome_fn is None:
                raise ValueError(
                    '`genome_fn` must be provided if using the '
                    'new _randomintersection mechanism')
            distribution = self._randomintersection(
                other, iterations=iterations, genome_fn=genome_fn, **kwargs)

        distribution = np.array(list(distribution))

        # Median of distribution
        med_count = np.median(distribution)

        n = float(len(distribution))

        frac_above = sum(distribution > actual) / n
        frac_below = sum(distribution < actual) / n

        normalized = actual / med_count

        lower_thresh = 2.5
        upper_thresh = 97.5
        lower, upper = np.percentile(
            distribution, [lower_thresh, upper_thresh])

        actual_percentile = percentileofscore(distribution, actual)
        d = {
            'iterations': iterations,
            'actual': actual,
            'file_a': self.fn,
            'file_b': other.fn,
            self.fn: len(self),
            other.fn: len(other),
            'self': len(self),
            'other': len(other),
            'frac randomized above actual': frac_above,
            'frac randomized below actual': frac_below,
            'median randomized': med_count,
            'normalized': normalized,
            'percentile': actual_percentile,
            'lower_%sth' % lower_thresh: lower,
            'upper_%sth' % upper_thresh: upper,
        }
        if include_distribution:
            d['distribution'] = distribution
        else:
            del distribution
        return d

    def random_op(self, *args, **kwargs):
        """
        For backwards compatibility; see BedTool.parallel_apply instead.
        """
        return self.parallel_apply(*args, **kwargs)

    def parallel_apply(self, iterations, func, func_args, func_kwargs,
                       processes=1, _orig_pool=None):
        """
        Generalized method for applying a function in parallel.

        Typically used when having to do many random shufflings.

        `func_args` and `func_kwargs` will be passed to `func` each time in
        `iterations`, and these iterations will be split across `processes`
        processes.

        Notes on the function, `func`:

            * the function should manually remove any tempfiles created.  This
              is because the BedTool.TEMPFILES list of auto-created tempfiles
              does not share state across processes, so things will not get
              cleaned up automatically as they do in a single-process
              pybedtools session.

            * this includes deleting any "chromsizes" or genome files --
              generally it will be best to require a genome filename in
              `func_kwargs` if you'll be using any BedTool methods that accept
              the `g` kwarg.

            * the function should be a module-level function (rather than a
              class method) because class methods can't be pickled across
              process boundaries

            * the function can have any signature and have any return value

        `_orig_pool` can be a previously-created multiprocessing.Pool instance;
        otherwise, a new Pool will be created with `processes`
        """
        if processes == 1:
            for it in range(iterations):
                yield func(*func_args, **func_kwargs)
            raise StopIteration

        if _orig_pool:
            p = _orig_pool
        else:
            p = multiprocessing.Pool(processes)
        iterations_each = [iterations / processes] * processes
        iterations_each[-1] += iterations % processes

        # FYI some useful info on apply_async:
        # http://stackoverflow.com/questions/8533318/
        #      python-multiprocessing-pool-when-to-use-apply-apply-async-or-map
        #
        # Here, we don't care about the order, and don't want the subprocesses
        # to block.
        results = [
            p.apply_async(func, func_args, func_kwargs)
            for it in range(iterations)]
        for r in results:
            yield r.get()
        raise StopIteration

    def random_jaccard(self, other, genome_fn=None, iterations=None,
                       processes=1, _orig_pool=None, shuffle_kwargs=None,
                       jaccard_kwargs=None):
        """
        Computes the naive Jaccard statistic (intersection divided by union).

        .. note::

            If you don't need the randomization functionality of this method,
            you can use the simpler BedTool.jaccard method instead.

        See Favorov et al. (2012) PLoS Comput Biol 8(5): e1002529 for more
        info on the Jaccard statistic for intersections.

        If `iterations` is None, then do not perform random shufflings.

        If `iterations` is an integer, perform `iterations` random shufflings,
        each time computing the Jaccard statistic to build an empirical
        distribution.  `genome_fn` will also be needed; optional `processes`
        will split the iteations across multiple CPUs.

        Returns a tuple of the observed Jaccard statistic and a list of the
        randomized statistics (which will be an empty list if `iterations` was
        None).
        """
        if shuffle_kwargs is None:
            shuffle_kwargs = {}
        if jaccard_kwargs is None:
            jaccard_kwargs = {}
        if not genome_fn:
            raise ValueError(
                "Need a genome filename in order to perform randomization")
        return list(
            self.parallel_apply(
                iterations=iterations,
                func=pybedtools.stats.random_jaccard,
                func_args=(self, other),
                func_kwargs=dict(
                    genome_fn=genome_fn,
                    shuffle_kwargs=shuffle_kwargs,
                    jaccard_kwargs=jaccard_kwargs),
                processes=processes,
                _orig_pool=_orig_pool,
            )
        )

    def _randomintersection(self, other, iterations, genome_fn,
                            intersect_kwargs=None, _orig_pool=None,
                            shuffle_kwargs=None, processes=1):
        """
        Re-implementation of BedTool.randomintersection using the new
        `random_op` method
        """
        if shuffle_kwargs is None:
            shuffle_kwargs = {}
        if intersect_kwargs is None:
            intersect_kwargs = dict(u=True)
        if not genome_fn:
            raise ValueError(
                "Need a genome filename in order to perform randomization")
        return list(
            self.parallel_apply(
                iterations=iterations,
                func=pybedtools.stats.random_intersection,
                func_args=(self, other),
                func_kwargs=dict(
                    genome_fn=genome_fn,
                    shuffle_kwargs=shuffle_kwargs,
                    intersect_kwargs=intersect_kwargs),
                processes=processes,
                _orig_pool=_orig_pool,
            )
        )

    def randomintersection_bp(self, other, iterations, genome_fn,
                              intersect_kwargs=None, shuffle_kwargs=None,
                              processes=1, _orig_pool=None):
        """
        Like randomintersection, but return the bp overlap instead of the
        number of intersecting intervals.
        """
        if shuffle_kwargs is None:
            shuffle_kwargs = {}
        if intersect_kwargs is None:
            intersect_kwargs = {}
        if not genome_fn:
            raise ValueError(
                "Need a genome filename in order to perform randomization")
        return list(
            self.parallel_apply(
                iterations=iterations,
                func=pybedtools.stats.random_intersection_bp,
                func_args=(self, other),
                func_kwargs=dict(
                    genome_fn=genome_fn,
                    shuffle_kwargs=shuffle_kwargs,
                    intersect_kwargs=intersect_kwargs),
                processes=processes,
                _orig_pool=_orig_pool,
            )
        )

    def randomintersection(self, other, iterations, intersect_kwargs=None,
                           shuffle_kwargs=None, debug=False,
                           report_iterations=False, processes=None,
                           _orig_processes=None):
        """
        Perform `iterations` shufflings, each time intersecting with `other`.

        Returns a generator of integers where each integer is the number of
        intersections of a shuffled file with *other*. This distribution can
        be used in downstream analysis for things like empirical p-values.

        *intersect_kwargs* and *shuffle_kwargs* are passed to self.intersect()
        and self.shuffle() respectively.  By default for intersect, u=True is
        specified -- but s=True might be a useful option for strand-specific
        work.

        Useful kwargs for *shuffle_kwargs* are chrom, excl, or incl.  If you
        use the "seed" kwarg, that seed will be used *each* time shuffleBed is
        called -- so all your randomization results will be identical for each
        iteration.  To get around this and to allow for tests, debug=True will
        set the seed to the iteration number.  You may also break up the
        intersections across multiple processes with *processes* > 1.

        Example usage:

            >>> chromsizes = {'chr1':(0, 1000)}
            >>> a = pybedtools.example_bedtool('a.bed')
            >>> a = a.set_chromsizes(chromsizes)
            >>> b = pybedtools.example_bedtool('b.bed')
            >>> results = a.randomintersection(b, 10, debug=True)
            >>> print(list(results))
            [2, 1, 2, 1, 1, 1, 1, 1, 1, 2]

        """
        if processes is not None:
            p = multiprocessing.Pool(processes)
            iterations_each = [iterations // processes] * processes
            iterations_each[-1] += iterations % processes
            results = [
                p.apply_async(
                    _call_randomintersect, (self, other, it),
                    dict(intersect_kwargs=intersect_kwargs,
                         shuffle_kwargs=shuffle_kwargs,
                         debug=debug,
                         report_iterations=report_iterations,
                         _orig_processes=processes
                         )
                )
                for it in iterations_each]
            for r in results:
                for value in r.get():
                    yield value
            raise StopIteration

        if shuffle_kwargs is None:
            shuffle_kwargs = {}
        if intersect_kwargs is None:
            intersect_kwargs = {'u': True}

        if 'u' not in intersect_kwargs:
            intersect_kwargs['u'] = True

        resort = intersect_kwargs.get('sorted', False)

        for i in range(iterations):
            if debug:
                shuffle_kwargs['seed'] = i
            if report_iterations:
                if _orig_processes > 1:
                    msg = '\rapprox (total across %s processes): %s' \
                        % (_orig_processes, i * _orig_processes)
                else:
                    msg = '\r%s' % i
                sys.stderr.write(msg)
                sys.stderr.flush()

            # Re-sort if sorted=True in kwargs
            if resort:
                tmp0 = self.shuffle(**shuffle_kwargs)
                tmp = tmp0.sort()
            else:
                tmp = self.shuffle(**shuffle_kwargs)

            tmp2 = tmp.intersect(other, stream=True, **intersect_kwargs)

            yield len(tmp2)

            # Close the open stdouts from subprocess.Popen calls.  Note: doing
            # this in self.__del__ doesn't fix the open file limit bug; it
            # needs to be done here.
            # if resort:
            #     tmp0.fn.close()
            # tmp.fn.close()
            tmp2.fn.close()
            del(tmp)
            del(tmp2)

    @_log_to_history
    def cat(self, *others, **kwargs):
        """
        Concatenate interval files together.

        Concatenates two BedTool objects (or an object and a file) and does an
        optional post-merge of the features.

        *postmerge=True* by default; use *postmerge=False* if you want to keep
        features separate.

        *force_truncate=False* by default; *force_truncate=True* to truncate
        all files to chrom, start, stop.

        When *force_truncate=False* and *postmerge=False*, the output will
        contain the smallest number of fields observed across all inputs. This
        maintains compatibility with BEDTools programs, which assume constant
        number of fields in all lines of a file.

        Other kwargs are sent to :meth:`BedTool.merge` (and assuming that
        *postmerge=True*).

        Example usage:

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> b = pybedtools.example_bedtool('b.bed')
        >>> print(a.cat(b)) #doctest: +NORMALIZE_WHITESPACE
        chr1	1	500
        chr1	800	950
        <BLANKLINE>
        >>> print(a.cat(*[b,b],
        ...   postmerge=False)) #doctest: +NORMALIZE_WHITESPACE
        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	+
        chr1	155	200	feature5	0	-
        chr1	800	901	feature6	0	+
        <BLANKLINE>
        """
        assert len(others) > 0, 'You must specify at least one other bedfile!'
        other_beds = []
        for other in others:
            if isinstance(other, six.string_types):
                other = BedTool(other)
            else:
                assert isinstance(other, BedTool),\
                    'Either filename or another BedTool instance required'
            other_beds.append(other)
        tmp = self._tmp()
        TMP = open(tmp, 'w')

        # postmerge and force_trucate don't get passed on to merge
        postmerge = kwargs.pop('postmerge', True)
        force_truncate = kwargs.pop('force_truncate', False)
        stream_merge = kwargs.get('stream', False)
        if stream_merge and postmerge:
            raise ValueError(
                "The post-merge step in the `cat()` method "
                "perfoms a sort, which uses stream=True.  Using "
                "stream=True for the merge as well will result in a "
                "deadlock!")

        # if filetypes and field counts are the same, don't truncate
        if not force_truncate:
            try:
                a_type = self.file_type

                files = [self] + other_beds
                filetypes = set(
                    [self.file_type]
                    + [i.file_type for i in other_beds]).difference(['empty'])
                field_nums = set(
                    [self.field_count()]
                    + [i.field_count() for i in other_beds]).difference([None]).difference([0])
                same_field_num = len(field_nums) == 1
                same_type = len(set(filetypes)) == 1
            except ValueError:
                raise ValueError(
                    "Can't check filetype or field count -- "
                    "is one of the files you're merging a 'streaming' "
                    "BedTool?  If so, use .saveas() to save to file first")

        if not force_truncate and same_type and same_field_num:
            for f in self:
                TMP.write(str(f))
            for other in other_beds:
                for f in other:
                    TMP.write(str(f))

        # Types match, so we can use the min number of fields observed across
        # all inputs
        elif not force_truncate and same_type:
            minfields = min(field_nums)
            for f in self:
                TMP.write('\t'.join(f.fields[:minfields]) + '\n')
            for other in other_beds:
                for f in other:
                    TMP.write('\t'.join(f.fields[:minfields]) + '\n')

        # Otherwise, use the zero-based chrom/start/stop to create a BED3,
        # which will work when catting a GFF and a BED together.
        else:
            for f in self:
                TMP.write('%s\t%i\t%i\n' % (f.chrom, f.start, f.end))
            for other in other_beds:
                for f in other:
                    TMP.write('%s\t%i\t%i\n' % (f.chrom, f.start, f.end))

        TMP.close()
        c = BedTool(tmp)
        if postmerge:
            d = c.sort(stream=True).merge(**kwargs)

            # Explicitly delete -- needed when using multiprocessing
            os.unlink(tmp)
            return d
        else:
            return c

    @_log_to_history
    def saveas(self, fn=None, trackline=None, compressed=None):
        """
        Make a copy of the BedTool.

        Optionally adds `trackline` to the beginning of the file.

        Optionally compresses output using gzip.

        if the filename extension is .gz, or compressed=True,
        the output is compressed using gzip

        Returns a new BedTool for the newly saved file.

        A newline is automatically added to the trackline if it does not
        already have one.

        Example usage:

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> b = a.saveas('other.bed')
        >>> b.fn
        'other.bed'
        >>> print(b == a)
        True

        >>> b = a.saveas('other.bed', trackline="name='test run' color=0,55,0")
        >>> open(b.fn).readline()
        "name='test run' color=0,55,0\\n"
        >>> if os.path.exists('other.bed'):
        ...     os.unlink('other.bed')
        """
        if fn is None:
            fn = self._tmp()

        # Default to compressed if extension is .gz
        if compressed is None:
            __, extension = os.path.splitext(fn)
            if extension == '.gz':
                compressed = True
            else:
                compressed = False

        fn = self._collapse(self, fn=fn, trackline=trackline,
                            compressed=compressed)
        return BedTool(fn)

    @_log_to_history
    def moveto(self, fn=None):
        """
        Move to a new filename (can be much quicker than BedTool.saveas())

        Move BED file to new filename, `fn`.

        Returns a new BedTool for the new file.

        Example usage:

        >>> # make a copy so we don't mess up the example file
        >>> a = pybedtools.example_bedtool('a.bed').saveas()
        >>> a_contents = str(a)
        >>> b = a.moveto('other.bed')
        >>> b.fn
        'other.bed'
        >>> b == a_contents
        True
        """
        if not isinstance(self.fn, six.string_types):
            fn = self._collapse(self, fn=fn)
        else:
            shutil.move(self.fn, fn)
        return BedTool(fn)

    @_log_to_history
    def random_subset(self, n, seed=None):
        '''
        Return a BedTool containing a random subset.

        Example usage:

        >>> a = pybedtools.example_bedtool('a.bed')
        >>> b = a.random_subset(2)
        >>> len(b)
        2
        '''
        idxs = list(range(len(self)))
        if seed is not None:
            random.seed(seed)
        random.shuffle(idxs)
        idxs = idxs[:n]

        tmpfn = self._tmp()
        tmp = open(tmpfn, 'w')
        for i, f in enumerate(self):
            if i in idxs:
                tmp.write(str(f))
        tmp.close()
        return BedTool(tmpfn)

    def total_coverage(self):
        """
        Return the total number of bases covered by this interval file.

        Does a self.merge() first to remove potentially multiple-counting
        bases.

        Example usage:

        >>> a = pybedtools.example_bedtool('a.bed')

        This does a merge() first, so this is what the total coverage is
        counting:

        >>> print(a.merge()) #doctest: +NORMALIZE_WHITESPACE
        chr1	1	500
        chr1	900	950
        <BLANKLINE>

        >>> print(a.total_coverage())
        549
        """
        b = self.merge()
        total_bp = 0
        for feature in b.features():
            total_bp += len(feature)
        return total_bp

    @_log_to_history
    def with_attrs(self, **kwargs):
        """
        Helper method for adding attributes in the middle of a pipeline.

        Given arbitrary keyword arguments, turns the keys and values into
        attributes.  Useful for labeling BedTools at creation time.

        Example usage:

        >>> # add a "label" attribute to each BedTool
        >>> a = pybedtools.example_bedtool('a.bed')\
                                   .with_attrs(label='transcription factor 1')
        >>> b = pybedtools.example_bedtool('b.bed')\
                                   .with_attrs(label='transcription factor 2')
        >>> for i in [a, b]:
        ...     print('{0} features for {1}'.format(i.count(), i.label))
        4 features for transcription factor 1
        2 features for transcription factor 2

        """
        for key, value in list(kwargs.items()):
            setattr(self, key, value)
        return self

    def as_intervalfile(self):
        """
        Returns an IntervalFile of this BedTool for low-level interface.
        """
        if not isinstance(self.fn, six.string_types):
            fn = self._collapse(self.fn)
        else:
            fn = self.fn
        return IntervalFile(fn)

    def liftover(self, chainfile, unmapped=None, liftover_args=""):
        """
        Returns a new BedTool of the liftedOver features, saving the unmapped
        ones as `unmapped`.  If `unmapped` is None, then discards the unmapped
        features.

        `liftover_args` is a string of additional args that is passed,
        verbatim, to liftOver.

        Needs `liftOver` from UCSC to be on the path and a `chainfile`
        downloaded from UCSC.
        """
        result = BedTool._tmp()
        if unmapped is None:
            unmapped = BedTool._tmp()
        cmds = ['liftOver', liftover_args, self.fn, chainfile, result,
                unmapped]
        os.system(' '.join(cmds))
        return BedTool(result)

    def absolute_distance(self, other, closest_kwargs=None,
                          use_midpoints=False):
        """
        Returns an iterator of the *absolute* distances between features in
        self and other.

        If `use_midpoints` is True, then only use the midpoints of features
        (which will return values where features are overlapping).  Otherwise,
        when features overlap the value will always be zero.

        `closest_kwargs` are passed to self.closest(); either `d` or
        'D` are required in order to get back distance values (`d=True` is
        default)
        """
        from .featurefuncs import midpoint

        if closest_kwargs is None:
            closest_kwargs = {'d': True}

        if 'D' not in closest_kwargs:
            closest_kwargs.update(dict(d=True))

        if use_midpoints:
            mid_self = self.each(midpoint).saveas()
            mid_other = other.each(midpoint).saveas()
            c = mid_self.closest(mid_other, stream=True, **closest_kwargs)
        else:
            c = self.closest(other, stream=True, **closest_kwargs)
        for i in c:
            yield int(i[-1])

    def relative_distance(self, other, genome=None, g=None):
        """
        Returns an iterator of relative distances between features in self and
        other.

        First computes the midpoints of self and other, then returns distances
        of each feature in `other` relative to the distance between `self`
        features.

        Requires either `genome` (dictionary of chromsizes or assembly name) or
        `g` (filename of chromsizes file).
        """
        if (genome is None) and (g is None):
            raise ValueError(
                'Need either `genome` or `g` arg for relative distance')
        if genome and g:
            raise ValueError('Please specify only one of `genome` or `g`')

        if genome:
            g_dict = dict(genome=genome)
        if g:
            g_dict = dict(g=g)

        from .featurefuncs import midpoint

        # This gets the space between features in self.
        c = self.each(midpoint).complement(**g_dict)

        mid_other = other.each(midpoint).saveas()

        hits = c.intersect(other, wao=True, stream=True)
        for i in hits:
            yield float(i[-1]) / len(i)

    def colormap_normalize(self, vmin=None, vmax=None, percentile=False,
                           log=False):
        """
        Returns a normalization instance for use by featurefuncs.add_color().

        Parameters
        ----------
        vmin, vmax : float, int, or None
            `vmin` and `vmax` set the colormap bounds; if None then
            these will be determined from the scores in the BED file.

        log : bool
            If True, put the scores on a log scale; of course be careful
            if you have negative scores

        percentile : bool
            If True, interpret vmin and vmax as a percentile in the range
            [0,100] rather than absolute values.
        """
        field_count = self.field_count()
        if (self.file_type != 'bed') or (field_count < 5):
            raise ValueError('colorizing only works for BED files with score '
                             'fields')
        import matplotlib
        import numpy as np

        if log:
            norm = matplotlib.colors.LogNorm()
        else:
            norm = matplotlib.colors.Normalize()

        scores = np.array([i.score for i in self], dtype=float)
        scores = scores[np.isfinite(scores)]
        norm.autoscale(scores)

        if vmin is not None:
            if percentile:
                vmin = np.percentile(scores, vmin)
            norm.vmin = vmin
        if vmax is not None:
            if percentile:
                vmax = np.percentile(scores, vmax)
            norm.vmax = vmax

        return norm

    def at(self, inds):
        """
        Returns a new BedTool with only intervals at lines `inds`
        """
        length = len(inds)

        def _gen():
            k = 0
            for i, feature in enumerate(self):
                if i == inds[k]:
                    yield feature
                    k += 1
                    if k == length:
                        break
        return BedTool(_gen()).saveas()

    def to_dataframe(self, disable_auto_names=False, *args, **kwargs):
        """
        create a pandas.DataFrame, passing args and kwargs to pandas.read_table

        Parameters
        ----------
        disable_auto_names : bool
            By default, the created dataframe fills in column names
            automatically according to the detected filetype (e.g., "chrom",
            "start", "end" for a BED3 file). Set this argument to True to
            disable this behavior.
        """
        # Complain if BAM or if not a file
        if self._isbam:
            raise ValueError("BAM not supported for converting to DataFrame")
        if not isinstance(self.fn, six.string_types):
            raise ValueError("use .saveas() to make sure self.fn is a file")

        try:
            import pandas
        except ImportError:
            raise ImportError(
                "pandas must be installed to convert to pandas.DataFrame")
        # Otherwise we're good:
        names = kwargs.get('names', None)
        if names is None and not disable_auto_names:
            try:
                _names = \
                    settings._column_names[self.file_type][:self.field_count()]
                if len(_names) < self.field_count():
                    warn(
                        'Default names for filetype %s are:\n%s\nbut file has '
                        '%s fields; you can supply custom names with the '
                        '`names` kwarg'
                        % (self.file_type, _names, self.field_count()))
                    _names = None
            except KeyError:
                _names = None
            kwargs['names'] = _names

        return pandas.read_table(self.fn, *args, **kwargs)

    def tail(self, lines=10, as_string=False):
        """
        Like `head`, but prints last 10 lines of the file by default.

        To avoid consuming iterables, this only works with file-based, non-BAM
        BedTool objects.

        Use `as_string=True` to return a string.
        """
        if self._isbam:
            raise ValueError('tail() not yet implemented for BAM files')
        if not isinstance(self.fn, six.string_types):
            raise ValueError('tail() not implemented for non-file-based '
                             'BedTool objects.  Please use saveas() first.')
        bufsize = 8192
        offset = bufsize
        f = open(self.fn, 'rb')

        # whence=2 arg means relative to end (i.e., go to the end)
        f.seek(0, 2)
        file_size = f.tell()
        data = []
        while True:
            if file_size < bufsize:
                offset = file_size
            f.seek(-offset, 2)
            chunk = f.read(offset)
            data.extend(chunk.splitlines(True))
            if len(data) >= lines or offset == file_size:
                break
            offset += bufsize

        result = ''.join([i.decode() for i in data[-lines:]])
        if as_string:
            return result
        else:
            print(result)


class BAM(object):
    def __init__(self, stream):
        """
        Wraps pysam.Samfile so that it yields pybedtools.Interval objects when
        iterated over.

        The pysam.Samfile can be accessed via the .pysam_bamfile attribute.
        """
        self.stream = stream
        if not isinstance(self.stream, six.string_types):
            raise ValueError("Only files are supported, not streams")
        self.pysam_bamfile = pysam.Samfile(self.stream)

    def _aligned_segment_to_interval(self, r):
        if r.rname >= 0:
            rname = self.pysam_bamfile.getrname(r.rname)
        else:
            rname = "*"

        if r.rnext >= 0:
            if r.rnext == r.rname:
                rnext = "="
            else:
                rnext = self.pysam_bamfile.getrname(r.rnext)
        else:
            rnext = "*"

        # SAM spec says if unavailable should be set to 0. Pysam sets to -1.

        if r.pnext <= 0:
            pnext = "0"
        else:
            # +1 here because cbedtools.pyx expects SAM -- which is 1-based --
            # but pysam uses 0-based.
            pnext = str(r.pnext + 1)

        if r.cigarstring:
            cigarstring = r.cigarstring
        else:
            cigarstring = "*"

        # Rudimentary support.
        # TODO: remove when refactoring to new BAM iterating
        tags = []
        for k, v in r.tags:
            if isinstance(v, int):
                t = 'i'
            elif isinstance(v, float):
                t = 'f'
            else:
                t = 'Z'
            tags.append('{0}:{1}:{2}'.format(k, t, v))

        tags = '\t'.join(tags)

        if r.seq:
            seq = r.seq
        else:
            seq = "*"

        if r.qual:
            qual = r.qual
        else:
            qual = "*"

        fields = [
            r.qname,
            str(r.flag),
            rname,

            # +1 here because cbedtools.pyx expects SAM -- which is 1-based --
            # but pysam uses 0-based.
            str(r.pos + 1),
            str(r.mapq),
            cigarstring,
            rnext,
            pnext,
            str(r.tlen),
            seq,
            qual,
        ]
        if tags:
            fields.append(tags)

        if None in fields:
            raise ValueError("Found 'None' in fields: %s" % fields)
        return create_interval_from_list(fields)

    def __iter__(self):
        return self

    # TODO: this is PAINFUL but it ensures that existing tests work.  Once all
    # tests work, the new behavior will be to yield pysam AlignedSegment
    # objects directly.
    def __next__(self):
        return self._aligned_segment_to_interval(next(self.pysam_bamfile))

    def next(self):
        return self.__next__()


class History(list):
    def __init__(self):
        """
        Represents one or many HistorySteps.  Mostly used for nicely formatting
        a series of HistorySteps.
        """
        list.__init__(self)


class HistoryStep(object):
    def __init__(self, method, args, kwargs, bedtool_instance,
                 parent_tag, result_tag):
        """
        Class to represent one step in the history.

        Mostly used for its __repr__ method, to try and exactly replicate code
        that can be pasted to re-do history steps
        """
        try:
            self.method = method._name
        except AttributeError:
            if six.PY3:
                self.method = method.__name__
            else:
                self.method = method.func_name
        self.args = args
        self.kwargs = kwargs
        self.fn = bedtool_instance.fn
        tag = ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
        self.parent_tag = parent_tag
        self.result_tag = result_tag

    def _clean_arg(self, arg):
        """
        Wrap strings in quotes and convert bedtool instances to filenames.
        """
        if isinstance(arg, pybedtools.BedTool):
            arg = arg.fn
        if isinstance(arg, six.string_types):
            arg = '"%s"' % arg
        return arg

    def __repr__(self):
        # Still not sure whether to use pybedtools.bedtool() or bedtool()
        s = ''
        s += '<HistoryStep> '
        if os.path.exists(self.fn):
            s += 'BedTool("%(fn)s").%(method)s(%%s%%s)' % self.__dict__
        else:
            s += 'BedTool("MISSING FILE: %(fn)s")' % self.__dict__
            s += '.%(method)s(%%s%%s)' % self.__dict__

        # Format args and kwargs
        args_string = ','.join(map(self._clean_arg, self.args))
        kwargs_string = ','.join(
            ['%s=%s' % (i[0], self._clean_arg(i[1]))
             for i in list(self.kwargs.items())])
        # stick a comma on the end if there's something here
        if len(args_string) > 0:
            args_string += ', '

        s = s % (args_string, kwargs_string)
        s += ', parent tag: %s' % self.parent_tag
        s += ', result tag: %s' % self.result_tag
        return s


def example_bedtool(fn):
    """
    Return a bedtool using a bed file from the pybedtools examples directory.
    Use :func:`list_example_files` to see a list of files that are included.
    """
    fn = os.path.join(filenames.data_dir(), fn)
    if not os.path.exists(fn):
        msg = "%s does not exist" % fn
        if six.PY2:
            raise ValueError(msg)
        raise FileNotFoundError(msg)
    return BedTool(fn)

if __name__ == "__main__":
    import doctest
    doctest.testmod(optionflags=doctest.ELLIPSIS |
                    doctest.NORMALIZE_WHITESPACE)