bx-python / scripts / maf_print_scores.py

#!/usr/bin/env python

"""
Read a MAF from standard input and print the score of each block. It can 
optionally recalculate each score using the hox70 matrix, and normalize the 
score by the number of columns in the alignment.

TODO: Should be able to read an arbitrary scoring matrix.

usage: %prog [options]
   -r, --recalculate: don't use the score from the maf, recalculate (using hox70 matrix)
   -l, --lnorm: divide (normalize) score by alignment text length
"""

from __future__ import division

import sys
from bx.cookbook import doc_optparse
from bx.align import maf
from bx.align import score
from optparse import OptionParser

def main():

    # Parse command line arguments
    options, args = doc_optparse.parse( __doc__ )

    try:
        lnorm = bool( options.lnorm )
        recalculate = bool( options.recalculate )
    except:
        doc_optparse.exit()

    hox70 = score.build_scoring_scheme( """  A    C    G    T
                                      91 -114  -31 -123
                                    -114  100 -125  -31
                                     -31 -125  100 -114
                                    -123  -31 -114   91 """, 400, 30, default=0 )

    maf_reader = maf.Reader( sys.stdin )

    for m in maf_reader: 
        if m.text_size == 0:
            print "NA"
            continue
        s = m.score
        # Recalculate?
        if recalculate:
            s = hox70.score_alignment( m )
        # Normalize?
        if lnorm:
            s = s / m.text_size
        # Print
        print s

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 ProjectModifiedEvent.java.
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.