| 
0
 | 
     1 #!/usr/bin/env python
 | 
| 
 | 
     2 
 | 
| 
 | 
     3 """
 | 
| 
 | 
     4 Read a maf and output a single block fasta file, concatenating blocks
 | 
| 
 | 
     5 
 | 
| 
 | 
     6 usage %prog species1,species2 maf_file out_file
 | 
| 
 | 
     7 """
 | 
| 
 | 
     8 #Dan Blankenberg
 | 
| 
 | 
     9 import sys
 | 
| 
 | 
    10 from galaxy import eggs
 | 
| 
 | 
    11 import pkg_resources; pkg_resources.require( "bx-python" )
 | 
| 
 | 
    12 from bx.align import maf
 | 
| 
 | 
    13 from galaxy.tools.util import maf_utilities
 | 
| 
 | 
    14 
 | 
| 
 | 
    15 assert sys.version_info[:2] >= ( 2, 4 )
 | 
| 
 | 
    16 
 | 
| 
 | 
    17 def __main__():
 | 
| 
 | 
    18     try:
 | 
| 
 | 
    19         species = maf_utilities.parse_species_option( sys.argv[1] )
 | 
| 
 | 
    20     except Exception, e:
 | 
| 
 | 
    21         maf_utilities.tool_fail( "Error determining species value: %s" % e )
 | 
| 
 | 
    22     try:
 | 
| 
 | 
    23         input_filename = sys.argv[2]
 | 
| 
 | 
    24     except Exception, e:
 | 
| 
 | 
    25         maf_utilities.tool_fail( "Error reading MAF filename: %s" % e )
 | 
| 
 | 
    26     try:
 | 
| 
 | 
    27         file_out = open( sys.argv[3], 'w' )
 | 
| 
 | 
    28     except Exception, e:
 | 
| 
 | 
    29         maf_utilities.tool_fail( "Error opening file for output: %s" % e )
 | 
| 
 | 
    30     
 | 
| 
 | 
    31     if species:
 | 
| 
 | 
    32         print "Restricted to species: %s" % ', '.join( species )
 | 
| 
 | 
    33     else:
 | 
| 
 | 
    34         print "Not restricted to species."
 | 
| 
 | 
    35     
 | 
| 
 | 
    36     if not species:
 | 
| 
 | 
    37         try:
 | 
| 
 | 
    38             species = maf_utilities.get_species_in_maf( input_filename )
 | 
| 
 | 
    39         except Exception, e:
 | 
| 
 | 
    40             maf_utilities.tool_fail( "Error determining species in input MAF: %s" % e )
 | 
| 
 | 
    41     
 | 
| 
 | 
    42     for spec in species:
 | 
| 
 | 
    43         file_out.write( ">" + spec + "\n" )
 | 
| 
 | 
    44         try:
 | 
| 
 | 
    45             for start_block in maf.Reader( open( input_filename, 'r' ) ):
 | 
| 
 | 
    46                 for block in maf_utilities.iter_blocks_split_by_species( start_block ):
 | 
| 
 | 
    47                     block.remove_all_gap_columns() #remove extra gaps
 | 
| 
 | 
    48                     component = block.get_component_by_src_start( spec ) #blocks only have one occurrence of a particular species, so this is safe
 | 
| 
 | 
    49                     if component:
 | 
| 
 | 
    50                         file_out.write( component.text )
 | 
| 
 | 
    51                     else:
 | 
| 
 | 
    52                         file_out.write( "-" * block.text_size )
 | 
| 
 | 
    53         except Exception, e:
 | 
| 
 | 
    54             maf_utilities.tool_fail( "Your MAF file appears to be malformed: %s" % e )
 | 
| 
 | 
    55         file_out.write( "\n" )
 | 
| 
 | 
    56     file_out.close()
 | 
| 
 | 
    57 
 | 
| 
 | 
    58 
 | 
| 
 | 
    59 if __name__ == "__main__": __main__()
 |