PageRenderTime 32ms CodeModel.GetById 25ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/maf/maf_split_by_species.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 44 lines | 34 code | 6 blank | 4 comment | 7 complexity | 7401437a81702d5b8495e204415704fe MD5 | raw file
 1#!/usr/bin/env python
 2
 3"""
 4Read a maf and split blocks by unique species combinations 
 5"""
 6import sys
 7from galaxy import eggs
 8import pkg_resources; pkg_resources.require( "bx-python" )
 9from bx.align import maf
10from galaxy.tools.util import maf_utilities
11from galaxy.util import string_as_bool
12
13assert sys.version_info[:2] >= ( 2, 4 )
14
15def __main__():    
16    try:
17        maf_reader = maf.Reader( open( sys.argv[1] ) )
18    except Exception, e:
19        maf_utilities.tool_fail( "Error opening MAF: %s" % e )
20    try:
21        out = maf.Writer( open( sys.argv[2], "w") )
22    except Exception, e:
23        maf_utilities.tool_fail( "Error opening file for output: %s" % e )
24    try:
25        collapse_columns = string_as_bool( sys.argv[3] )
26    except Exception, e:
27        maf_utilities.tool_fail( "Error determining collapse columns value: %s" % e )
28    
29    start_count = 0
30    end_count = 0
31    for start_count, start_block in enumerate( maf_reader ):
32        for block in maf_utilities.iter_blocks_split_by_species( start_block ):
33            if collapse_columns:
34                block.remove_all_gap_columns()
35            out.write( block )
36            end_count += 1
37    out.close()
38    
39    if end_count:
40        print "%i alignment blocks created from %i original blocks." % ( end_count, start_count + 1 )
41    else:
42        print "No alignment blocks were created."
43
44if __name__ == "__main__": __main__()