Source

bx-python / scripts / maf_count.py

#!/usr/bin/env python2.3

"""
Read a MAF from standard input and print counts of alignments, bases, or 
columns. 

usage: %prog [options]
   -c, --cols: count alignment columns rather than number of alignments
   -b, --bases: count bases in first species rather than number of alignments
   -s, --skip=N: when counting bases, skip this base
   -e, --each: print a count for each alignment rather than whole file
   -r, --ref=N: reference sequence (first by default, 0..n)
"""

from bx.cookbook import doc_optparse
import sys

import bx.align.maf

def __main__():

    options, args = doc_optparse.parse( __doc__ )

    try:
        if options.cols: action = "cols"
        elif options.bases: action = "bases"
        else: action = "aligns"
        print_each = bool( options.each )
        if options.ref: ref = int( options.ref )
        else: ref = 0
        if options.skip: skip = options.skip
        else: skip = None
    except:
        doc_optparse.exit()

    maf_reader = bx.align.maf.Reader( sys.stdin )

    count = 0

    for m in maf_reader:
        
        if action == "aligns": 
            count += 1
        elif action == "cols": 
            count += m.text_size
        elif action == "bases":
            if skip:
                count += ( m.components[ref].size - m.components[ref].text.count( skip ) )
            else:
                count += m.components[ref].size

        if print_each: 
            print count
            count = 0

    if not print_each: print count

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.