PageRenderTime 42ms CodeModel.GetById 33ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/datatypes/converters/bedgraph_to_array_tree_converter.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 55 lines | 39 code | 13 blank | 3 comment | 11 complexity | 5312d184e5785849b35e6c0b7f45d39e MD5 | raw file
 1#!/usr/bin/env python
 2
 3from __future__ import division
 4
 5import sys
 6from galaxy import eggs
 7import pkg_resources; pkg_resources.require( "bx-python" )
 8from bx.arrays.array_tree import *
 9# from bx.arrays.wiggle import BedReader
10
11BLOCK_SIZE = 100
12
13class 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
39def 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
54if __name__ == "__main__":
55    main()