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

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:42 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Read a maf and split blocks by unique species combinations
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 from bx.align import maf
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 from galaxy.tools.util import maf_utilities
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 from galaxy.util import string_as_bool
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 maf_reader = maf.Reader( open( sys.argv[1] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 maf_utilities.tool_fail( "Error opening MAF: %s" % e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 out = maf.Writer( open( sys.argv[2], "w") )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 maf_utilities.tool_fail( "Error opening file for output: %s" % e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 collapse_columns = string_as_bool( sys.argv[3] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 maf_utilities.tool_fail( "Error determining collapse columns value: %s" % e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 start_count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 end_count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 for start_count, start_block in enumerate( maf_reader ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 for block in maf_utilities.iter_blocks_split_by_species( start_block ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 if collapse_columns:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 block.remove_all_gap_columns()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 out.write( block )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 end_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 if end_count:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 print "%i alignment blocks created from %i original blocks." % ( end_count, start_count + 1 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 print "No alignment blocks were created."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 if __name__ == "__main__": __main__()