1 | #!/usr/bin/python2.6 |
---|
2 | |
---|
3 | """ |
---|
4 | Reads a list of intervals and an axt. Produces a new axt containing the |
---|
5 | portions of the original that overlapped the intervals |
---|
6 | |
---|
7 | usage: %prog interval_file refindex [options] < axt_file |
---|
8 | -m, --mincols=10: Minimum length (columns) required for alignment to be output |
---|
9 | """ |
---|
10 | |
---|
11 | import psyco_full |
---|
12 | |
---|
13 | from bx.cookbook import doc_optparse |
---|
14 | |
---|
15 | import bx.align.axt |
---|
16 | from bx import intervals |
---|
17 | import sys |
---|
18 | |
---|
19 | |
---|
20 | def __main__(): |
---|
21 | |
---|
22 | # Parse Command Line |
---|
23 | |
---|
24 | options, args = doc_optparse.parse( __doc__ ) |
---|
25 | |
---|
26 | try: |
---|
27 | range_filename = args[ 0 ] |
---|
28 | refindex = int( args[ 1 ] ) |
---|
29 | if options.mincols: mincols = int( options.mincols ) |
---|
30 | else: mincols = 10 |
---|
31 | except: |
---|
32 | doc_optparse.exit() |
---|
33 | |
---|
34 | # Load Intervals |
---|
35 | |
---|
36 | intersecter = intervals.Intersecter() |
---|
37 | for line in file( range_filename ): |
---|
38 | fields = line.split() |
---|
39 | intersecter.add_interval( intervals.Interval( int( fields[0] ), int( fields[1] ) ) ) |
---|
40 | |
---|
41 | # Start axt on stdout |
---|
42 | |
---|
43 | out = bx.align.axt.Writer( sys.stdout ) |
---|
44 | |
---|
45 | # Iterate over input axt |
---|
46 | |
---|
47 | for axt in bx.align.axt.Reader( sys.stdin ): |
---|
48 | ref_component = axt.components[ refindex ] |
---|
49 | # Find overlap with reference component |
---|
50 | intersections = intersecter.find( ref_component.start, ref_component.end ) |
---|
51 | # Keep output axt ordered |
---|
52 | intersections.sort() |
---|
53 | # Write each intersecting block |
---|
54 | for interval in intersections: |
---|
55 | start = max( interval.start, ref_component.start ) |
---|
56 | end = min( interval.end, ref_component.end ) |
---|
57 | sliced = axt.slice_by_component( refindex, start, end ) |
---|
58 | good = True |
---|
59 | for c in sliced.components: |
---|
60 | if c.size < 1: |
---|
61 | good = False |
---|
62 | if good and sliced.text_size > mincols: out.write( sliced ) |
---|
63 | |
---|
64 | # Close output axt |
---|
65 | |
---|
66 | out.close() |
---|
67 | |
---|
68 | if __name__ == "__main__": __main__() |
---|