File: maf_count.py

package info (click to toggle)
python-bx 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,000 kB
  • sloc: python: 17,136; ansic: 2,326; makefile: 24; sh: 8
file content (67 lines) | stat: -rwxr-xr-x 1,618 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python3

"""
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)
"""

import sys

import bx.align.maf
from bx.cookbook import doc_optparse


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 Exception:
        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__()