PageRenderTime 36ms CodeModel.GetById 24ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/maf/maf_to_fasta_concat.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 59 lines | 44 code | 8 blank | 7 comment | 11 complexity | f88c307004579c192680dcbd9721284a MD5 | raw file
 1#!/usr/bin/env python
 2
 3"""
 4Read a maf and output a single block fasta file, concatenating blocks
 5
 6usage %prog species1,species2 maf_file out_file
 7"""
 8#Dan Blankenberg
 9import sys
10from galaxy import eggs
11import pkg_resources; pkg_resources.require( "bx-python" )
12from bx.align import maf
13from galaxy.tools.util import maf_utilities
14
15assert sys.version_info[:2] >= ( 2, 4 )
16
17def __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
59if __name__ == "__main__": __main__()