File: maf_covered_ranges.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 (53 lines) | stat: -rwxr-xr-x 1,453 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python3

"""
usage: %prog species1,species2,... nrequired < maf
"""

import sys

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

SPAN = 100
MIN = 100


def main():
    options, args = doc_optparse.parse(__doc__)

    try:
        species = args[0].split(",")
        nrequired = int(args[1])
    except Exception:
        doc_optparse.exit()

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

    interval_start = None
    interval_end = None

    for m in maf_reader:
        ref = m.components[0]
        # Does this alignment have enough of the required species
        if nrequired <= len([comp for comp in m.components if comp.src.split(".")[0] in species]):
            if interval_start is None:
                interval_start = ref.start
                interval_end = ref.end
            else:
                if ref.start - interval_end < SPAN:
                    interval_end = ref.end
                else:
                    if interval_end - interval_start >= MIN:
                        print(ref.src.split(".")[1], interval_start, interval_end)
                    interval_start = ref.start
                    interval_end = ref.end
        else:
            if interval_start is not None and interval_end - interval_start >= MIN:
                print(ref.src.split(".")[1], interval_start, interval_end)
            interval_start = None
            interval_end = None


if __name__ == "__main__":
    main()