1 | #!/usr/bin/python2.6 |
---|
2 | |
---|
3 | """ |
---|
4 | Read a set of ranges and a nib file, print portions of nib overlapping |
---|
5 | those ranges to stdout |
---|
6 | |
---|
7 | TODO: General sequence handling would be nice, as well as merging with |
---|
8 | 'nib_intervals_to_fasta.py'. |
---|
9 | |
---|
10 | usage: %prog nib_dir < range_file |
---|
11 | """ |
---|
12 | |
---|
13 | from bx.cookbook import doc_optparse |
---|
14 | import bx.seq.nib |
---|
15 | import string |
---|
16 | import sys |
---|
17 | |
---|
18 | def __main__(): |
---|
19 | |
---|
20 | options, args = doc_optparse.parse( __doc__ ) |
---|
21 | |
---|
22 | try: |
---|
23 | nib_dir = args[0] |
---|
24 | except: |
---|
25 | doc_optparse.exit() |
---|
26 | |
---|
27 | nibs = {} |
---|
28 | |
---|
29 | for line in sys.stdin: |
---|
30 | fields = line.split() |
---|
31 | chrom, start, end = fields[0], int( fields[1] ), int( fields[2] ) |
---|
32 | print ">", chrom, start, end |
---|
33 | if chrom in nibs: |
---|
34 | nib = nibs[chrom] |
---|
35 | else: |
---|
36 | nibs[chrom] = nib = bx.seq.nib.NibFile( file( "%s/%s.nib" % ( nib_dir, chrom ) ) ) |
---|
37 | print_wrapped( nib.get( start, end - start ) ) |
---|
38 | |
---|
39 | def print_wrapped( s ): |
---|
40 | l = len( s ) |
---|
41 | c = 0 |
---|
42 | while c < l: |
---|
43 | b = min( c + 50, l ) |
---|
44 | print s[c:b] |
---|
45 | c = b |
---|
46 | |
---|
47 | if __name__ == "__main__": __main__() |
---|