File: nib_chrom_intervals_to_fasta.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 (50 lines) | stat: -rwxr-xr-x 1,043 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
#!/usr/bin/python3

"""
Read a set of ranges and a nib file, print portions of nib overlapping
those ranges to stdout

TODO: General sequence handling would be nice, as well as merging with
      'nib_intervals_to_fasta.py'.

usage: %prog nib_dir < range_file
"""

import sys

import bx.seq.nib
from bx.cookbook import doc_optparse


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

    try:
        nib_dir = args[0]
    except IndexError:
        doc_optparse.exit()

    nibs = {}

    for line in sys.stdin:
        fields = line.split()
        chrom, start, end = fields[0], int(fields[1]), int(fields[2])
        print(">", chrom, start, end)
        if chrom in nibs:
            nib = nibs[chrom]
        else:
            nibs[chrom] = nib = bx.seq.nib.NibFile(open(f"{nib_dir}/{chrom}.nib"))
        print_wrapped(nib.get(start, end - start))


def print_wrapped(s):
    l = len(s)
    c = 0
    while c < l:
        b = min(c + 50, l)
        print(s[c:b])
        c = b


if __name__ == "__main__":
    __main__()