view tools/maf/maf_split_by_species.py @ 2:c2a356708570

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:42 -0500
parents 9071e359b9a3
children
line wrap: on
line source

#!/usr/bin/env python

"""
Read a maf and split blocks by unique species combinations 
"""
import sys
from galaxy import eggs
import pkg_resources; pkg_resources.require( "bx-python" )
from bx.align import maf
from galaxy.tools.util import maf_utilities
from galaxy.util import string_as_bool

assert sys.version_info[:2] >= ( 2, 4 )

def __main__():    
    try:
        maf_reader = maf.Reader( open( sys.argv[1] ) )
    except Exception, e:
        maf_utilities.tool_fail( "Error opening MAF: %s" % e )
    try:
        out = maf.Writer( open( sys.argv[2], "w") )
    except Exception, e:
        maf_utilities.tool_fail( "Error opening file for output: %s" % e )
    try:
        collapse_columns = string_as_bool( sys.argv[3] )
    except Exception, e:
        maf_utilities.tool_fail( "Error determining collapse columns value: %s" % e )
    
    start_count = 0
    end_count = 0
    for start_count, start_block in enumerate( maf_reader ):
        for block in maf_utilities.iter_blocks_split_by_species( start_block ):
            if collapse_columns:
                block.remove_all_gap_columns()
            out.write( block )
            end_count += 1
    out.close()
    
    if end_count:
        print "%i alignment blocks created from %i original blocks." % ( end_count, start_count + 1 )
    else:
        print "No alignment blocks were created."

if __name__ == "__main__": __main__()