| 1 | #!/usr/bin/python2.6 |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | Read a MAF from stdin and break into several mafs based on the source of |
|---|
| 5 | each block. If the `component` option is provided then only that component |
|---|
| 6 | will be used to determine the new file for each block, otherwise the src |
|---|
| 7 | for *all* components will be used. |
|---|
| 8 | |
|---|
| 9 | TODO: Should be able to specify component by species/prefix? |
|---|
| 10 | |
|---|
| 11 | usage: %prog [options] < maf |
|---|
| 12 | -o, --outprefix: prepend this to the name of each generate maf |
|---|
| 13 | -c, --component: use only this component (by index!) to split |
|---|
| 14 | """ |
|---|
| 15 | |
|---|
| 16 | import sys, string |
|---|
| 17 | import bx.align.maf |
|---|
| 18 | from optparse import OptionParser |
|---|
| 19 | |
|---|
| 20 | import psyco_full |
|---|
| 21 | |
|---|
| 22 | INF="inf" |
|---|
| 23 | |
|---|
| 24 | def __main__(): |
|---|
| 25 | |
|---|
| 26 | # Parse command line arguments |
|---|
| 27 | |
|---|
| 28 | parser = OptionParser() |
|---|
| 29 | parser.add_option( "-o", "--outprefix", action="store", default="" ) |
|---|
| 30 | parser.add_option( "-c", "--component", action="store", default=None ) |
|---|
| 31 | ( options, args ) = parser.parse_args() |
|---|
| 32 | |
|---|
| 33 | out_prefix = options.outprefix |
|---|
| 34 | comp = options.component |
|---|
| 35 | if comp is not None: |
|---|
| 36 | comp = int( comp ) |
|---|
| 37 | |
|---|
| 38 | maf_reader = bx.align.maf.Reader( sys.stdin ) |
|---|
| 39 | |
|---|
| 40 | writers = {} |
|---|
| 41 | |
|---|
| 42 | for m in maf_reader: |
|---|
| 43 | |
|---|
| 44 | if comp is None: |
|---|
| 45 | writer_key = string.join( [ c.src for c in m.components ], '_' ) |
|---|
| 46 | else: |
|---|
| 47 | writer_key = m.components[ comp ].src |
|---|
| 48 | |
|---|
| 49 | if not writers.has_key( writer_key ): |
|---|
| 50 | writer = bx.align.maf.Writer( file( "%s%s.maf" % ( out_prefix, writer_key ), "w" ) ) |
|---|
| 51 | writers[ writer_key ] = writer |
|---|
| 52 | else: |
|---|
| 53 | writer = writers[ writer_key ] |
|---|
| 54 | |
|---|
| 55 | writer.write( m ) |
|---|
| 56 | |
|---|
| 57 | for key in writers: |
|---|
| 58 | writers[ key ].close() |
|---|
| 59 | |
|---|
| 60 | if __name__ == "__main__": __main__() |
|---|