bx-python / scripts /

#!/usr/bin/env python2.4
For each interval in `bed1` count the number of intersecting regions in `bed2`.

usage: %prog bed1 bed2 

import sys
from bx.intervals import *

bed1,bed2 = sys.argv[1:3]

ranges = {}
for line in open( bed2 ):
    fields = line.strip().split() 
    chrom, start, end, = fields[0], int(fields[1]), int(fields[2])
    if chrom not in ranges: ranges[chrom] = Intersecter()
    ranges[chrom].add_interval( Interval( start, end ) )
for line in open( bed1 ):
    fields = line.strip().split() 
    chrom, start, end = fields[0], int(fields[1]), int(fields[2]) 
    other = " ".join(fields[3:])
    out = " ".join(fields[:3] +[other])
    if chrom in ranges: 
        print out, len( ranges[chrom].find( start, end ) )
        print out, 0
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
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.