1 | #!/usr/bin/python2.6 |
---|
2 | """ |
---|
3 | For each interval in `bed1` count the number of intersecting regions in `bed2`. |
---|
4 | |
---|
5 | usage: %prog bed1 bed2 |
---|
6 | """ |
---|
7 | |
---|
8 | import sys |
---|
9 | from bx.intervals import * |
---|
10 | |
---|
11 | bed1,bed2 = sys.argv[1:3] |
---|
12 | |
---|
13 | ranges = {} |
---|
14 | for line in open( bed2 ): |
---|
15 | fields = line.strip().split() |
---|
16 | chrom, start, end, = fields[0], int(fields[1]), int(fields[2]) |
---|
17 | if chrom not in ranges: ranges[chrom] = Intersecter() |
---|
18 | ranges[chrom].add_interval( Interval( start, end ) ) |
---|
19 | |
---|
20 | for line in open( bed1 ): |
---|
21 | fields = line.strip().split() |
---|
22 | chrom, start, end = fields[0], int(fields[1]), int(fields[2]) |
---|
23 | other = " ".join(fields[3:]) |
---|
24 | out = " ".join(fields[:3] +[other]) |
---|
25 | if chrom in ranges: |
---|
26 | print out, len( ranges[chrom].find( start, end ) ) |
---|
27 | else: |
---|
28 | print out, 0 |
---|
29 | |
---|