1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | from __future__ import division |
---|
4 | |
---|
5 | import sys |
---|
6 | from galaxy import eggs |
---|
7 | import pkg_resources; pkg_resources.require( "bx-python" ) |
---|
8 | from bx.arrays.array_tree import * |
---|
9 | # from bx.arrays.wiggle import BedReader |
---|
10 | |
---|
11 | BLOCK_SIZE = 100 |
---|
12 | |
---|
13 | class BedGraphReader: |
---|
14 | def __init__( self, f ): |
---|
15 | self.f = f |
---|
16 | |
---|
17 | def __iter__( self ): |
---|
18 | return self |
---|
19 | |
---|
20 | def next( self ): |
---|
21 | while True: |
---|
22 | line = self.f.readline() |
---|
23 | if not line: |
---|
24 | raise StopIteration() |
---|
25 | if line.isspace(): |
---|
26 | continue |
---|
27 | if line[0] == "#": |
---|
28 | continue |
---|
29 | if line[0].isalpha(): |
---|
30 | if line.startswith( "track" ) or line.startswith( "browser" ): |
---|
31 | continue |
---|
32 | |
---|
33 | feature = line.strip().split() |
---|
34 | chrom = feature[0] |
---|
35 | chrom_start = int(feature[1]) |
---|
36 | chrom_end = int(feature[2]) |
---|
37 | score = float(feature[3]) |
---|
38 | return chrom, chrom_start, chrom_end, None, score |
---|
39 | def main(): |
---|
40 | |
---|
41 | input_fname = sys.argv[1] |
---|
42 | out_fname = sys.argv[2] |
---|
43 | |
---|
44 | reader = BedGraphReader( open( input_fname ) ) |
---|
45 | |
---|
46 | # Fill array from reader |
---|
47 | d = array_tree_dict_from_reader( reader, {}, block_size = BLOCK_SIZE ) |
---|
48 | |
---|
49 | for array_tree in d.itervalues(): |
---|
50 | array_tree.root.build_summary() |
---|
51 | |
---|
52 | FileArrayTreeDict.dict_to_file( d, open( out_fname, "w" ) ) |
---|
53 | |
---|
54 | if __name__ == "__main__": |
---|
55 | main() |
---|