| 1 | #!/usr/bin/python2.6 |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | Find regions of first bed file that overlap regions in a second bed file. This |
|---|
| 5 | program performs a base-by-base intersection, so only runs of bases that are |
|---|
| 6 | covered in both of the inputs will be output. |
|---|
| 7 | |
|---|
| 8 | usage: %prog bed_file_1 bed_file_2 |
|---|
| 9 | """ |
|---|
| 10 | |
|---|
| 11 | import sys |
|---|
| 12 | from warnings import warn |
|---|
| 13 | from bx.bitset import * |
|---|
| 14 | from bx.bitset_builders import * |
|---|
| 15 | from bx.cookbook import doc_optparse |
|---|
| 16 | |
|---|
| 17 | options, args = doc_optparse.parse( __doc__ ) |
|---|
| 18 | try: |
|---|
| 19 | in_fname, in2_fname = args |
|---|
| 20 | except: |
|---|
| 21 | doc_optparse.exit() |
|---|
| 22 | |
|---|
| 23 | bits1 = binned_bitsets_from_file( open( in_fname ) ) |
|---|
| 24 | bits2 = binned_bitsets_from_file( open( in2_fname ) ) |
|---|
| 25 | |
|---|
| 26 | bitsets = dict() |
|---|
| 27 | |
|---|
| 28 | for key in bits1: |
|---|
| 29 | if key in bits2: |
|---|
| 30 | bits1[key].iand( bits2[key] ) |
|---|
| 31 | bitsets[key] = bits1[key] |
|---|
| 32 | |
|---|
| 33 | for chrom in bitsets: |
|---|
| 34 | bits = bitsets[chrom] |
|---|
| 35 | end = 0 |
|---|
| 36 | while 1: |
|---|
| 37 | start = bits.next_set( end ) |
|---|
| 38 | if start == bits.size: break |
|---|
| 39 | end = bits.next_clear( start ) |
|---|
| 40 | print "%s\t%d\t%d" % ( chrom, start, end ) |
|---|