PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/maf/maf_by_block_number.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 46 lines | 35 code | 4 blank | 7 comment | 9 complexity | fcfbd12d42530b0f6579ba9333b68162 MD5 | raw file
 1#!/usr/bin/env python
 2#Dan Blankenberg
 3"""
 4Reads a list of block numbers and a maf. Produces a new maf containing the
 5blocks specified by number.
 6"""
 7
 8import sys
 9from galaxy import eggs
10import pkg_resources; pkg_resources.require( "bx-python" )
11from galaxy.tools.util import maf_utilities
12import bx.align.maf
13
14assert sys.version_info[:2] >= ( 2, 4 )
15
16def __main__():
17    input_block_filename = sys.argv[1].strip()
18    input_maf_filename = sys.argv[2].strip()
19    output_filename1 = sys.argv[3].strip()
20    block_col = int( sys.argv[4].strip() ) - 1
21    if block_col < 0:
22        print >> sys.stderr, "Invalid column specified"
23        sys.exit(0)
24    species = maf_utilities.parse_species_option( sys.argv[5].strip() )
25    
26    maf_writer = bx.align.maf.Writer( open( output_filename1, 'w' ) )
27    #we want to maintain order of block file and write blocks as many times as they are listed
28    failed_lines = []
29    for ctr, line in enumerate( open( input_block_filename, 'r' ) ):
30        try:
31            block_wanted = int( line.split( "\t" )[block_col].strip() )
32        except:
33            failed_lines.append( str( ctr ) )
34            continue
35        try:
36            for count, block in enumerate( bx.align.maf.Reader( open( input_maf_filename, 'r' ) ) ):
37                if count == block_wanted:
38                    if species:
39                        block = block.limit_to_species( species )
40                    maf_writer.write( block )
41                    break
42        except:
43            print >>sys.stderr, "Your MAF file appears to be malformed."
44            sys.exit()
45    if len( failed_lines ) > 0: print "Failed to extract from %i lines (%s)." % ( len( failed_lines ), ",".join( failed_lines ) )
46if __name__ == "__main__": __main__()