annotate tools/encode/random_intervals_no_bits.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 #Dan Blankenberg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 #%prog bounding_region_file mask_intervals_file intervals_to_mimic_file out_file mask_chr mask_start mask_end interval_chr interval_start interval_end interval_strand use_mask allow_strand_overlaps
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 import sys, random
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 from copy import deepcopy
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 import pkg_resources
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 import bx.intervals.io
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 import bx.intervals.intersection
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 import psyco_full
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 max_iters = 5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 sys.stderr.write( msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 #Try to add a random region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 def add_random_region( mimic_region, bound, exist_regions, plus_mask, minus_mask, overlaps ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 region_length, region_strand = mimic_region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 plus_count = plus_mask.count_range()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 minus_count = minus_mask.count_range()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 gaps = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 if region_strand == "-":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 gaps = minus_mask.get_gaps( region_length )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 gaps = plus_mask.get_gaps( region_length )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 while True:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 gap_length, gap_start, gap_end = gaps.pop( random.randint( 0, len( gaps ) - 1 ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 start = random.randint( bound.start + gap_start, bound.start + gap_end - region_length - 1 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 except ValueError, ve:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 stop_err( "Exception thrown generating random start value: %s" %str( ve ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 end = start + region_length
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 try_plus_mask = plus_mask.copy()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 try_minus_mask = minus_mask.copy()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 if region_strand == "-":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 try_minus_mask.set_range( start - bound.start, end - bound.start )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 try_plus_mask.set_range( start - bound.start, end - bound.start )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 rand_region = bx.intervals.io.GenomicInterval( None, [bound.chrom, start, end, region_strand], 0, 1, 2, 3, "+", fix_strand=True )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 if try_plus_mask.count_range() == plus_count + region_length or try_minus_mask.count_range() == minus_count + region_length:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 if overlaps in ["strand", "all"]: #overlaps allowed across strands
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 exist_regions.append( rand_region )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 if overlaps == "strand":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 return exist_regions, True, try_plus_mask, try_minus_mask
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 else: #overlaps allowed everywhere
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 return exist_regions, True, plus_mask, minus_mask
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 else: #no overlapping anywhere
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 exist_regions.append( rand_region )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 if region_strand == "-":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 return exist_regions, True, try_minus_mask.copy(), try_minus_mask
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 return exist_regions, True, try_plus_mask, try_plus_mask.copy()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 return exist_regions, False, plus_mask, minus_mask
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 includes_strand = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 region_uid = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 mask_fname = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 intervals_fname = sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 out_fname = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 mask_chr = int( sys.argv[5] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 stop_err( "'%s' is an invalid chrom column for 'Intervals to Mask' dataset, click the pencil icon in the history item to edit column settings." % str( sys.argv[5] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 mask_start = int( sys.argv[6] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 stop_err( "'%s' is an invalid start column for 'Intervals to Mask' dataset, click the pencil icon in the history item to edit column settings." % str( sys.argv[6] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 mask_end = int( sys.argv[7] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 stop_err( "'%s' is an invalid end column for 'Intervals to Mask' dataset, click the pencil icon in the history item to edit column settings." % str( sys.argv[7] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 interval_chr = int( sys.argv[8] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 stop_err( "'%s' is an invalid chrom column for 'File to Mimick' dataset, click the pencil icon in the history item to edit column settings." % str( sys.argv[8] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 interval_start = int( sys.argv[9] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 stop_err( "'%s' is an invalid start column for 'File to Mimick' dataset, click the pencil icon in the history item to edit column settings." % str( sys.argv[9] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 interval_end = int( sys.argv[10] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 stop_err( "'%s' is an invalid end column for 'File to Mimick' dataset, click the pencil icon in the history item to edit column settings." % str( sys.argv[10] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 interval_strand = int( sys.argv[11] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 includes_strand = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 interval_strand = -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 if includes_strand:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 use_mask = sys.argv[12]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 overlaps = sys.argv[13]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 use_mask = sys.argv[11]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 overlaps = sys.argv[12]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 available_regions = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 loc_file = "%s/regions.loc" % sys.argv[-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 for i, line in enumerate( file( loc_file ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 line = line.rstrip( '\r\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 fields = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 #read each line, if not enough fields, go to next line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 build = fields[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 uid = fields[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 description = fields[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 filepath = fields[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 available_regions[uid] = filepath
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 if region_uid not in available_regions:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 stop_err( "Region '%s' is invalid." % region_uid )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 region_fname = available_regions[region_uid].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 #set up bounding regions to hold random intervals
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 bounds = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 for bound in bx.intervals.io.NiceReaderWrapper( open( region_fname, 'r' ), chrom_col=0, start_col=1, end_col=2, fix_strand=True, return_header=False, return_comments=False ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 bounds.append( bound )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 #set up length and number of regions to mimic
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 regions = [ [] for i in range( len( bounds ) ) ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 for region in bx.intervals.io.NiceReaderWrapper( open( intervals_fname, 'r' ), chrom_col=interval_chr, start_col=interval_start, end_col=interval_end, strand_col=interval_strand, fix_strand=True, return_header=False, return_comments=False ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 #loop through bounds, find first proper bounds then add
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 #if an interval crosses bounds, it will be added to the first bound
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 for i in range( len( bounds ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 if bounds[i].chrom != region.chrom:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 intersecter = bx.intervals.intersection.Intersecter()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 intersecter.add_interval( bounds[i] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 if len( intersecter.find( region.start, region.end ) ) > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 regions[i].append( ( region.end - region.start, region.strand ) ) #add region to proper bound and go to next region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 for region in regions:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 region.sort()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 region.reverse()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 #read mask file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 mask = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 if use_mask != "no_mask":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 for region in bx.intervals.io.NiceReaderWrapper( open( mask_fname, 'r' ), chrom_col=mask_chr, start_col=mask_start, end_col=mask_end, fix_strand=True, return_header=False, return_comments=False ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 mask.append( region )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 out_file = open ( out_fname, "w" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 stop_err( "Error opening output file '%s'." % out_fname )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 i = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 i_iters = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 region_count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 best_regions = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 num_fail = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 while i < len( bounds ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 i_iters += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 #order regions to mimic
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 regions_to_mimic = regions[i][0:]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 if len( regions_to_mimic ) < 1: #if no regions to mimic, skip
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 i += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 i_iters = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 #set up region mask
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178 plus_mask = Region( bounds[i].end - bounds[i].start )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 for region in mask:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 if region.chrom != bounds[i].chrom: continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 mask_start = region.start - bounds[i].start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182 mask_end = region.end - bounds[i].start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 if mask_start >= 0 and mask_end > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184 plus_mask.set_range( mask_start, mask_end )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 minus_mask = plus_mask.copy()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186 random_regions = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 num_added = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 for j in range( len( regions[i] ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 random_regions, added, plus_mask, minus_mask = add_random_region( regions_to_mimic[j], bounds[i], random_regions, plus_mask, minus_mask, overlaps )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 if added:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191 num_added += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 if num_added == len( regions_to_mimic ) or i_iters >= max_iters:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 if len( best_regions ) > len( random_regions ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 random_regions = best_regions.copy()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195 num_fail += ( len( regions_to_mimic ) - len( random_regions ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 i_iters = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197 best_regions = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 for region in random_regions:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 print >>out_file, "%s\t%d\t%d\t%s\t%s\t%s" % ( region.chrom, region.start, region.end, "region_" + str( region_count ), "0", region.strand )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200 region_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 i -= 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 if len( best_regions ) < len( random_regions ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204 best_regions = random_regions[:]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 i+=1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208 if num_fail:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209 print "After %i iterations, %i regions could not be added." % (max_iters, num_fail)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210 if use_mask == "use_mask":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 print "The mask you have provided may be too restrictive."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213 class Region( list ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215 A list for on/off regions
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217 def __init__( self, size=0 ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218 for i in range( size ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219 self.append( False )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 def copy( self ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221 return deepcopy( self )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222 def set_range( self, start=0, end=None ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 if start < 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224 start = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 if ( not end and end != 0 ) or end > len( self ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226 end = len( self )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227 for i in range( start, end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 self[i]=True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229 def count_range( self, start=0, end=None ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 if start < 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 start = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 if ( not end and end != 0 ) or end > len( self ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 end = len( self )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234 return self[start:end].count( True )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 def get_gaps( self, min_size = 0 ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236 gaps = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 start = end = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238 while True:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240 start = self[end:].index( False ) + end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244 end = self[start:].index( True ) + start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246 end = len( self )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247 if end > start and end - start >= min_size:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 gaps.append( ( end - start, start, end ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249 gaps.sort()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250 gaps.reverse()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251 return gaps
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253 if __name__ == "__main__": main()