| 1 | #!/usr/bin/python2.6 |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | Given two bed files print the number of bases covered 1) by both, 2) only by |
|---|
| 5 | the first, and 3) only by the second. |
|---|
| 6 | |
|---|
| 7 | usage: %prog bed_file_1 bed_file_2 |
|---|
| 8 | """ |
|---|
| 9 | import sys |
|---|
| 10 | from warnings import warn |
|---|
| 11 | from bx.bitset import BinnedBitSet |
|---|
| 12 | from bx.bitset_builders import * |
|---|
| 13 | from bx.cookbook import doc_optparse |
|---|
| 14 | |
|---|
| 15 | def coverage( bitsets ): |
|---|
| 16 | total = 0 |
|---|
| 17 | for chrom in bitsets: |
|---|
| 18 | total += bitsets[chrom].count_range( 0, bitsets[chrom].size ) |
|---|
| 19 | return total |
|---|
| 20 | |
|---|
| 21 | options, args = doc_optparse.parse( __doc__ ) |
|---|
| 22 | try: |
|---|
| 23 | in_fname, in2_fname = args |
|---|
| 24 | except: |
|---|
| 25 | doc_optparse.exit() |
|---|
| 26 | |
|---|
| 27 | bits1 = binned_bitsets_from_file( open( in_fname ) ) |
|---|
| 28 | bits2 = binned_bitsets_from_file( open( in2_fname ) ) |
|---|
| 29 | |
|---|
| 30 | bits1_covered = coverage( bits1 ) |
|---|
| 31 | bits2_covered = coverage( bits2 ) |
|---|
| 32 | |
|---|
| 33 | bitsets = dict() |
|---|
| 34 | |
|---|
| 35 | for key in bits1: |
|---|
| 36 | if key in bits2: |
|---|
| 37 | bits1[key].iand( bits2[key] ) |
|---|
| 38 | bitsets[key] = bits1[key] |
|---|
| 39 | |
|---|
| 40 | both_covered = coverage( bitsets ) |
|---|
| 41 | |
|---|
| 42 | print "in both: \t%d" % both_covered |
|---|
| 43 | print "only in %s:\t%d" % ( in_fname, bits1_covered - both_covered ) |
|---|
| 44 | print "only in %s:\t%d" % ( in2_fname, bits2_covered - both_covered ) |
|---|