1 | #!/usr/bin/python2.6 |
---|
2 | |
---|
3 | """ |
---|
4 | Convert wiggle data to a binned array. This assumes the input data is on a |
---|
5 | single chromosome and does no sanity checks! |
---|
6 | |
---|
7 | usage: %prog score_file out_file < wiggle_data |
---|
8 | -c, --comp=type: compression type (none, zlib, lzo) |
---|
9 | """ |
---|
10 | |
---|
11 | from __future__ import division |
---|
12 | |
---|
13 | import sys |
---|
14 | import psyco_full |
---|
15 | import bx.wiggle |
---|
16 | from bx.binned_array import BinnedArray |
---|
17 | from bx_extras.fpconst import isNaN |
---|
18 | from bx.cookbook import doc_optparse |
---|
19 | from bx import misc |
---|
20 | |
---|
21 | def main(): |
---|
22 | |
---|
23 | # Parse command line |
---|
24 | options, args = doc_optparse.parse( __doc__ ) |
---|
25 | try: |
---|
26 | if options.comp: |
---|
27 | comp_type = options.comp |
---|
28 | else: |
---|
29 | comp_type = None |
---|
30 | score_fname = args[0] |
---|
31 | out_fname = args[1] |
---|
32 | except: |
---|
33 | doc_optparse.exit() |
---|
34 | |
---|
35 | scores = BinnedArray() |
---|
36 | |
---|
37 | ## last_chrom = None |
---|
38 | for i, ( chrom, pos, val ) in enumerate( bx.wiggle.Reader( misc.open_compressed( score_fname ) ) ): |
---|
39 | #if last_chrom is None: |
---|
40 | # last_chrom = chrom |
---|
41 | #else: |
---|
42 | # assert chrom == last_chrom, "This script expects a 'wiggle' input on only one chromosome" |
---|
43 | scores[pos] = val |
---|
44 | # Status |
---|
45 | if i % 10000 == 0: print i, "scores processed" |
---|
46 | |
---|
47 | out = open( out_fname, "w" ) |
---|
48 | if comp_type: |
---|
49 | scores.to_file( out, comp_type=comp_type ) |
---|
50 | else: |
---|
51 | scores.to_file( out ) |
---|
52 | out.close() |
---|
53 | |
---|
54 | if __name__ == "__main__": main() |
---|