1 | #!/usr/bin/python2.6 |
---|
2 | |
---|
3 | """ |
---|
4 | Find continuous regions that are covered by the first bed file (`bed_file_1`) |
---|
5 | but not by the second bed file (`bed_file_2`) |
---|
6 | |
---|
7 | usage: %prog bed_file_1 bed_file_2 |
---|
8 | """ |
---|
9 | |
---|
10 | import sys |
---|
11 | from warnings import warn |
---|
12 | from bx.bitset_builders import binned_bitsets_from_file |
---|
13 | from bx.cookbook import doc_optparse |
---|
14 | |
---|
15 | def print_bits_as_bed( bits ): |
---|
16 | end = 0 |
---|
17 | while 1: |
---|
18 | start = bits.next_set( end ) |
---|
19 | if start == bits.size: break |
---|
20 | end = bits.next_clear( start ) |
---|
21 | print "%s\t%d\t%d" % ( chrom, start, end ) |
---|
22 | |
---|
23 | options, args = doc_optparse.parse( __doc__ ) |
---|
24 | try: |
---|
25 | in_fname, in2_fname = args |
---|
26 | except: |
---|
27 | doc_optparse.exit() |
---|
28 | |
---|
29 | # Read first bed into some bitsets |
---|
30 | |
---|
31 | bitsets1 = binned_bitsets_from_file( open( in_fname ) ) |
---|
32 | bitsets2 = binned_bitsets_from_file( open( in2_fname ) ) |
---|
33 | |
---|
34 | for chrom in bitsets1: |
---|
35 | if chrom not in bitsets1: |
---|
36 | continue |
---|
37 | bits1 = bitsets1[chrom] |
---|
38 | if chrom in bitsets2: |
---|
39 | bits2 = bitsets2[chrom] |
---|
40 | bits2.invert() |
---|
41 | bits1.iand( bits2 ) |
---|
42 | print_bits_as_bed( bits1 ) |
---|
43 | |
---|