Source

bx-python / scripts / maf_split_by_src.py

#!/usr/bin/env python2.3

"""
Read a MAF from stdin and break into several mafs based on the source of 
each block. If the `component` option is provided then only that component
will be used to determine the new file for each block, otherwise the src
for *all* components will be used.

TODO: Should be able to specify component by species/prefix?

usage: %prog [options] < maf
    -o, --outprefix: prepend this to the name of each generate maf
    -c, --component: use only this component (by index!) to split
"""

import sys, string
import bx.align.maf
from optparse import OptionParser

import psyco_full

INF="inf"

def __main__():

    # Parse command line arguments

    parser = OptionParser()
    parser.add_option( "-o", "--outprefix", action="store", default="" )
    parser.add_option( "-c", "--component", action="store", default=None )
    ( options, args ) = parser.parse_args()

    out_prefix = options.outprefix
    comp = options.component
    if comp is not None:
       comp = int( comp )

    maf_reader = bx.align.maf.Reader( sys.stdin )

    writers = {}

    for m in maf_reader:
       
        if comp is None: 
            writer_key = string.join( [ c.src for c in m.components ], '_' )
        else:
            writer_key = m.components[ comp ].src

        if not writers.has_key( writer_key ):
            writer = bx.align.maf.Writer( file( "%s%s.maf" % ( out_prefix, writer_key ), "w" ) )
            writers[ writer_key ] = writer
        else:
            writer = writers[ writer_key ] 

        writer.write( m )

    for key in writers:
        writers[ key ].close()

if __name__ == "__main__": __main__()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.