root/galaxy-central/eggs/bx_python-0.5.0_dev_f74aec067563-py2.6-macosx-10.6-universal-ucs2.egg/EGG-INFO/scripts/get_scores_in_intervals.py @ 3

リビジョン 3, 1.6 KB (コミッタ: kohda, 14 年 前)

Install Unix tools  http://hannonlab.cshl.edu/galaxy_unix_tools/galaxy.html

行番号 
1#!/usr/bin/python2.6
2
3"""
4Read scores in "wiggle" format from `score_file` and intervals in "bed" format
5from `interval_file` and print all scores overlapping intervals.
6
7TODO: Support binned array format scores also.
8
9usage: %prog score_file interval_file [out_file]
10"""
11
12from __future__ import division
13
14import sys
15import psyco_full
16import bx.wiggle
17from bx.binned_array import BinnedArray
18from fpconst import isNaN
19from bx.cookbook import doc_optparse
20from bx import misc
21
22def read_scores( f ):
23    scores_by_chrom = dict()
24    for chrom, pos, val in bx.wiggle.Reader( f ):
25        if chrom not in scores_by_chrom:
26            scores_by_chrom[chrom] = BinnedArray()
27        scores_by_chrom[chrom][pos] = val
28    return scores_by_chrom
29
30def main():
31
32    # Parse command line
33    options, args = doc_optparse.parse( __doc__ )
34    try:
35        score_file = open( args[0] )
36        interval_file = open( args[1] )
37        if len( args ) > 2:
38            out_file = open( args[2], 'w' )
39        else:
40            out_file = sys.stdout
41    except:
42        doc_optparse.exit()
43
44    scores_by_chrom = read_scores( misc.open_compressed( sys.argv[1] ) )
45    for line in open( sys.argv[2] ):
46        fields = line.split()
47        chrom, start, stop = fields[0], int( fields[1] ), int( fields[2] )
48        if chrom in scores_by_chrom:
49            ba = scores_by_chrom[chrom]
50            scores = [ ba[i] for i in range( start, stop ) ]
51        else:
52            scores = []
53        print >> out_file, " ".join( fields ), " ".join( map( str, scores ) )
54
55    score_file.close()
56    interval_file.close()
57    out_file.close()
58
59if __name__ == "__main__": main()
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。