| 1 | #!/usr/bin/python2.6 |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | Read a MAF from standard input and print counts of alignments, bases, or |
|---|
| 5 | columns. |
|---|
| 6 | |
|---|
| 7 | usage: %prog [options] |
|---|
| 8 | -c, --cols: count alignment columns rather than number of alignments |
|---|
| 9 | -b, --bases: count bases in first species rather than number of alignments |
|---|
| 10 | -s, --skip=N: when counting bases, skip this base |
|---|
| 11 | -e, --each: print a count for each alignment rather than whole file |
|---|
| 12 | -r, --ref=N: reference sequence (first by default, 0..n) |
|---|
| 13 | """ |
|---|
| 14 | |
|---|
| 15 | from bx.cookbook import doc_optparse |
|---|
| 16 | import sys |
|---|
| 17 | |
|---|
| 18 | import bx.align.maf |
|---|
| 19 | |
|---|
| 20 | def __main__(): |
|---|
| 21 | |
|---|
| 22 | options, args = doc_optparse.parse( __doc__ ) |
|---|
| 23 | |
|---|
| 24 | try: |
|---|
| 25 | if options.cols: action = "cols" |
|---|
| 26 | elif options.bases: action = "bases" |
|---|
| 27 | else: action = "aligns" |
|---|
| 28 | print_each = bool( options.each ) |
|---|
| 29 | if options.ref: ref = int( options.ref ) |
|---|
| 30 | else: ref = 0 |
|---|
| 31 | if options.skip: skip = options.skip |
|---|
| 32 | else: skip = None |
|---|
| 33 | except: |
|---|
| 34 | doc_optparse.exit() |
|---|
| 35 | |
|---|
| 36 | maf_reader = bx.align.maf.Reader( sys.stdin ) |
|---|
| 37 | |
|---|
| 38 | count = 0 |
|---|
| 39 | |
|---|
| 40 | for m in maf_reader: |
|---|
| 41 | |
|---|
| 42 | if action == "aligns": |
|---|
| 43 | count += 1 |
|---|
| 44 | elif action == "cols": |
|---|
| 45 | count += m.text_size |
|---|
| 46 | elif action == "bases": |
|---|
| 47 | if skip: |
|---|
| 48 | count += ( m.components[ref].size - m.components[ref].text.count( skip ) ) |
|---|
| 49 | else: |
|---|
| 50 | count += m.components[ref].size |
|---|
| 51 | |
|---|
| 52 | if print_each: |
|---|
| 53 | print count |
|---|
| 54 | count = 0 |
|---|
| 55 | |
|---|
| 56 | if not print_each: print count |
|---|
| 57 | |
|---|
| 58 | if __name__ == "__main__": __main__() |
|---|