bx-python / scripts /

#!/usr/bin/env python

Read a maf and print the text as a fasta file, concatenating blocks. A 
specific subset of species can be chosen. 

usage %prog [options] species1,species2,... < maf_file > fasta_file
    --fill="expression": Insert this between blocks
    --wrap=columns: Wrap FASTA to this many columns

from optparse import OptionParser

import textwrap
import sys
from bx.align import maf

def __main__():
    # Parse command line arguments

    parser = OptionParser()
    parser.add_option( "--fill", action="store", default=None, type="string", help="" )
    parser.add_option( "--wrap", action="store", default=None, type="int", help="" )
    parser.add_option( "--nowrap", action="store_true", default=False, dest="nowrap", help="" )

    ( options, args ) = parser.parse_args()

    species = []
    for arg in args: species.extend(arg.split(','))

    fill = ""
    if options.fill: fill = eval( options.fill )

    wrap = 50
    if   (options.wrap != None): wrap = options.wrap
    elif (options.nowrap):       wrap = 0

    # create the concatenated sequences

    texts = {}
    for s in species: texts[s] = []
    maf_reader = maf.Reader( sys.stdin )
    for m in maf_reader:
        for s in species:
            c = m.get_component_by_src_start( s ) 
            if c: texts[s].append( c.text )
            else: texts[s].append( "-" * m.text_size )
    for s in species:
        print ">" + s
        print_n( fill.join( texts[s] ), wrap )

def print_n( s, n, f = sys.stdout ):
    if (n <= 0):
        print >> f, s
        p = 0
        while p < len( s ):
            print >> f, s[p:min(p+n,len(s))]
            p += n

if __name__ == "__main__": __main__()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.