| 1 | #!/usr/bin/python2.6 |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | Remove any blocks from a maf that overlap any of a set of intervals. |
|---|
| 5 | |
|---|
| 6 | usage: %prog interval files... < maf |
|---|
| 7 | """ |
|---|
| 8 | |
|---|
| 9 | import psyco_full |
|---|
| 10 | |
|---|
| 11 | from bx.cookbook import doc_optparse |
|---|
| 12 | |
|---|
| 13 | import bx.align.maf |
|---|
| 14 | from bx import intervals |
|---|
| 15 | import sys |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | def __main__(): |
|---|
| 19 | |
|---|
| 20 | # Parse Command Line |
|---|
| 21 | |
|---|
| 22 | options, args = doc_optparse.parse( __doc__ ) |
|---|
| 23 | |
|---|
| 24 | try: |
|---|
| 25 | assert len( args ) > 0 |
|---|
| 26 | except: |
|---|
| 27 | doc_optparse.exit() |
|---|
| 28 | |
|---|
| 29 | # Load Intervals |
|---|
| 30 | |
|---|
| 31 | intersector = intervals.Intersecter() |
|---|
| 32 | |
|---|
| 33 | for f in args: |
|---|
| 34 | for line in file( f ): |
|---|
| 35 | if line.startswith( "#" ) or line.isspace(): continue |
|---|
| 36 | fields = line.split() |
|---|
| 37 | intersector.add_interval( intervals.Interval( int( fields[0] ), int( fields[1] ) ) ) |
|---|
| 38 | |
|---|
| 39 | # Start MAF on stdout |
|---|
| 40 | |
|---|
| 41 | out = bx.align.maf.Writer( sys.stdout ) |
|---|
| 42 | |
|---|
| 43 | # Iterate over input MAF |
|---|
| 44 | |
|---|
| 45 | for maf in bx.align.maf.Reader( sys.stdin ): |
|---|
| 46 | # Find overlap with reference component |
|---|
| 47 | intersections = intersector.find( maf.components[0].start, maf.components[0].end ) |
|---|
| 48 | # Write only if no overlap |
|---|
| 49 | if len( intersections ) == 0: |
|---|
| 50 | out.write( maf ) |
|---|
| 51 | |
|---|
| 52 | # Close output MAF |
|---|
| 53 | |
|---|
| 54 | out.close() |
|---|
| 55 | |
|---|
| 56 | if __name__ == "__main__": __main__() |
|---|