[3] | 1 | #!/usr/bin/python2.6 |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | Read two lists of intervals (with chromosomes) and count the number of entries |
---|
| 5 | in the second set that intersect any entry in the first set. |
---|
| 6 | |
---|
| 7 | TODO: This could use bitsets rather than the intervals package, would it be |
---|
| 8 | faster? |
---|
| 9 | |
---|
| 10 | usage: %prog bed1 bed2 > out |
---|
| 11 | """ |
---|
| 12 | |
---|
| 13 | from __future__ import division |
---|
| 14 | |
---|
| 15 | import psyco_full |
---|
| 16 | |
---|
| 17 | from bx import intervals |
---|
| 18 | from bx import misc |
---|
| 19 | import string |
---|
| 20 | import sys |
---|
| 21 | |
---|
| 22 | def main(): |
---|
| 23 | |
---|
| 24 | intersecters = {} |
---|
| 25 | |
---|
| 26 | # Read ranges |
---|
| 27 | |
---|
| 28 | for chr, start, end in read_intervals( misc.open_compressed( sys.argv[1] ) ): |
---|
| 29 | if not intersecters.has_key( chr ): intersecters[ chr ] = intervals.Intersecter() |
---|
| 30 | intersecters[ chr ].add_interval( intervals.Interval( start, end ) ) |
---|
| 31 | |
---|
| 32 | # Count intersection |
---|
| 33 | |
---|
| 34 | total = 0 |
---|
| 35 | |
---|
| 36 | for chr, start, end in read_intervals( misc.open_compressed( sys.argv[2] ) ): |
---|
| 37 | if intersecters.has_key( chr ): |
---|
| 38 | intersection = intersecters[ chr ].find( start, end ) |
---|
| 39 | if intersection: |
---|
| 40 | #print chr, intersection |
---|
| 41 | total += 1 |
---|
| 42 | |
---|
| 43 | print total |
---|
| 44 | |
---|
| 45 | def read_intervals( input ): |
---|
| 46 | for line in input: |
---|
| 47 | fields = line.split() |
---|
| 48 | yield fields[0], int( fields[1] ), int( fields[2] ) |
---|
| 49 | |
---|
| 50 | main() |
---|