bx-python / scripts / bed_diff_basewise_summary.py

#!/usr/bin/env python

Given two bed files print the number of bases covered 1) by both, 2) only by 
the first, and 3) only by the second.

usage: %prog bed_file_1 bed_file_2
import sys
from warnings import warn
from bx.bitset import BinnedBitSet
from bx.bitset_builders import *
from bx.cookbook import doc_optparse

def coverage( bitsets ):
    total = 0
    for chrom in bitsets:
        total += bitsets[chrom].count_range( 0, bitsets[chrom].size )
    return total    

options, args = doc_optparse.parse( __doc__ )
    in_fname, in2_fname = args

bits1 = binned_bitsets_from_file( open( in_fname ) )
bits2 = binned_bitsets_from_file( open( in2_fname ) )

bits1_covered = coverage( bits1 )
bits2_covered = coverage( bits2 )

bitsets = dict()

for key in bits1:
    if key in bits2:
        bits1[key].iand( bits2[key] )
        bitsets[key] = bits1[key]

both_covered = coverage( bitsets )

print "in both:  \t%d" % both_covered
print "only in %s:\t%d" % ( in_fname, bits1_covered - both_covered )
print "only in %s:\t%d" % ( in2_fname, bits2_covered - both_covered )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.