Graph/WrapPartitionsFixed.pm untested

filter: Statementstmt 0.0% 0 / 221 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 301 CC 10 SCAR 22.3
CC
10
Cov
0%
getPartitionIndexFromExtendedValue
42.0
reduce
12.0
getMaxAnnotationWidthForFont
6.0
CRAP 9.3
1 package Bio::GMOD::Blast::Graph::WrapPartitionsFixed;
2 BEGIN {
3 $Bio::GMOD::Blast::Graph::WrapPartitionsFixed::AUTHORITY = 'cpan:RBUELS';
4 }
5 BEGIN {
6 $Bio::GMOD::Blast::Graph::WrapPartitionsFixed::VERSION = '0.06';
7 }
8 #####################################################################
9 #
10 # Cared for by Shuai Weng <shuai@genome.stanford.edu>
11 #
12 # Originally created by John Slenk <jces@genome.stanford.edu>
13 #
14 # You may distribute this module under the same terms as perl itself
15 #-----------------------------------------------------------------
16  
17 use Bio::GMOD::Blast::Graph::BaseObj;
18 use Bio::GMOD::Blast::Graph::MyUtils;
19 use Bio::GMOD::Blast::Graph::MyDebug qw( dmsg dmsgs assert );
20 use Bio::GMOD::Blast::Graph::List;
21 use Bio::GMOD::Blast::Graph::ListSet;
22 use Bio::GMOD::Blast::Graph::HitWrapper;
23 use Bio::GMOD::Blast::Graph::MapSpace;
24 use Bio::GMOD::Blast::Graph::MapDefs
25 qw( $bucketZeroMax $bucketOneMax $bucketTwoMax $bucketThreeMax
26 $bucketFourMax $kNumberOfPartitions );
27  
28 @ISA = qw( Bio::GMOD::Blast::Graph::BaseObj );
29  
30 # partition 0 is for the 'best' hits, 5 for the 'worst'.
31 # P=1e-4 -- P=1e-10
32 # P=1e-10 -- P=1e-50
33 # P=1e-50 -- P=1e-100
34 # P=1e-100 -- P=1e-200
35 # P<1e-200
36  
37 my $kPartitionSet =
38 Bio::GMOD::Blast::Graph::MyUtils::makeVariableName( "partition", "set" ); # ListSet.
39 my $kPartitionCounts =
40 Bio::GMOD::Blast::Graph::MyUtils::makeVariableName( "partition", "counts" );
41 my $kRemovalPartitionIndex =
42 Bio::GMOD::Blast::Graph::MyUtils::makeVariableName( "removal", "partition", "index" );
43 my $kSpace =
44 Bio::GMOD::Blast::Graph::MyUtils::makeVariableName( "space" );
45  
46  
47 sub init
48 {
49 my( $self, $wrapListRef, $debugP ) = @_;
50 my( $dex );
51  
52 # optional; is off if $debugP is undef.
53 ## MyDebug::debugP( $debugP );
54  
55 $self->{ $kPartitionSet } = new Bio::GMOD::Blast::Graph::ListSet();
56 $self->{ $kPartitionCounts } = {}; # { dex => [ count before, after ] }.
57 $self->{ $kSpace } = new Bio::GMOD::Blast::Graph::MapSpace();
58  
59 $self->partitionWrappers( $wrapListRef );
60  
61  
62 }
63  
64 # assumes that the wrappers are still
65 # in sorted order. that way, each of
66 # the partitions will itself be sorted.
67 sub partitionWrappers
68 {
69 my( $self ) = shift;
70 my( $list ) = shift;
71  
72 my( $wrap );
73 my( $dex );
74 my( $part );
75  
76 my( $enum ) = $list->getEnumerator();
77 while( defined( $wrap = $enum->getNextElement() ) )
78 {
79 #dmsg( "partitionWrappers(): wrap = ", $wrap->toString() );
80  
81 # the problem here is that we only look at the exponent.
82 # 1.0 and 0.0 are at opposite ends, yet they have the
83 # same exponent, namely zero, so 1.0 gets put into
84 # the wrong bucket. so rather than pass in only the exponent,
85 # i pass in the whole thing. this is a different meaning
86 # of the term 'value' than elsewhere.
87 $part = $self->getPartitionFromExtendedValue( $wrap->getP() );
88  
89 $part->addElement( $wrap );
90 }
91  
92 # now set the "before" counts.
93 for( $dex = 0; $dex < $kNumberOfPartitions; $dex++ )
94 {
95 $part = $self->getPartitionAt( $dex );
96 $self->addPartitionCountAt( $dex, $part->getCount() );
97 }
98 }
99  
100 # returns 0...(kNumberOfPartitions-1)
101 # [[ where 0 is for the 'highest' values, oddly enough.
102 # this whole higher/lower more/less significant thing sucks. ]]
103 sub getPartitionIndexFromExtendedValue
104 {
105 my( $self, $value ) = @_;
106 my( $dex );
107 my( $step );
108 my( $exp );
109  
110 if( $value <= $bucketZeroMax ) { $dex = 0; }
111 elsif( $value <= $bucketOneMax ) { $dex = 1; }
112 elsif( $value <= $bucketTwoMax ) { $dex = 2; }
113 elsif( $value <= $bucketThreeMax ) { $dex = 3; }
114 elsif( $value <= $bucketFourMax ) { $dex = 4; }
115 else { confess( 'invalid value $value' ); }
116  
117 #dmsg( "getPartitionIndexFromExtendedValue( $value ): $dex" );
118  
119 return( $dex );
120 }
121  
122 sub getPartitionFromExtendedValue
123 {
124 my( $self, $value ) = @_;
125 my( $dex );
126 my( $part );
127  
128 $dex = $self->getPartitionIndexFromExtendedValue( $value );
129 $part = $self->getPartitionAt( $dex );
130  
131 return( $part );
132 }
133  
134 sub emptyP
135 {
136 my( $self ) = shift;
137 my( $emptyP );
138  
139 $emptyP = $self->{ $kPartitionSet }->emptyP();
140  
141 return( $emptyP );
142 }
143  
144 sub nextRemovalPartitionIndex
145 {
146 my( $self ) = shift;
147 my( $thisDex );
148 my( $nextDex );
149  
150 # assume we start with 0
151 # and want to use it before
152 # moving along, capiche?
153  
154 $thisDex = $self->getRemovalPartitionIndex();
155  
156 $nextDex = ($thisDex + 1) % $kNumberOfPartitions;
157 $self->putRemovalPartitionIndex( $nextDex );
158  
159 return( $thisDex );
160 }
161  
162 # this is so gross. sorry.
163 sub reduce
164 {
165 #dmsg( "reduce()..." );
166  
167 my( $self ) = shift;
168 my( $partSet );
169 my( $setEnum );
170 my( $list );
171 my( $key );
172 my( $listEnum );
173 my( %listEnums );
174  
175 $partSet = $self->getPartitions();
176 #dmsg( "reduce(): part count =", $partSet->getCount() );
177 $setEnum = $partSet->getEnumerator();
178 #dmsg( "reduce(): enum =", $setEnum );
179  
180 while( defined($list = $setEnum->getNextElement()) )
181 {
182 $listEnum = $list->getEnumerator();
183 $key = $setEnum->getCurrentKey();
184 $listEnums{ $key } = $listEnum;
185 #dmsg( "reduce(): added $key $listEnum count=", $listEnum->getCount() );
186 }
187  
188 my( $space ) = $self->{ $kSpace };
189 my( @keys );
190 my( @kill );
191 my( $dex );
192 my( $wrap );
193 my( $debugCount ) = 0;
194  
195 # my apologies for how gross this code is!
196  
197 # while we have room left,
198 # and there are still lists with data,
199 # do one loop over the lists.
200 # make sure to keep the list enumerations
201 # sorted because we'll be deleting them
202 # as they run out.
203  
204 for( @keys = sort keys( %listEnums );
205 (scalar(@keys) > 0) && (!$space->getFullP());
206 @keys = sort keys( %listEnums ) )
207 {
208 # during one loop, stop if we run out of space.
209  
210 #dmsg( "reduce(): #keys = ", scalar(@keys) );
211  
212 for( $dex = 0;
213 ($dex < scalar(@keys)) && (!$space->getFullP());
214 $dex++ )
215 {
216 #dmsg( " reduce(): dex = $dex, space =", $space->getSpaceRemaining() );
217  
218 # get the current list to read.
219 $key = $keys[ $dex ];
220 $listEnum = $listEnums{ $key };
221 #dmsg( " reduce(): key=$key enum=$listEnum count=", $listEnum->getCount() );
222  
223 # process it's current element.
224 if( defined( $wrap = $listEnum->getNextElement() ) )
225 {
226 #dmsg( " reduce(): wrap =", $wrap->toString() );
227  
228 if( ! $space->wrapperFitsP( $wrap ) )
229 {
230 #dmsg( " reduce(): doesn't fit" );
231 $listEnum->previousIndex();
232 $space->putFullP( 1 );
233 }
234 else
235 {
236 $space->updateFromWrapper( $wrap );
237 #dmsg( " reduce(): does fit, added #", ++$debugCount, " from $key @ ", $listEnum->getIndex() );
238 }
239 }
240 else
241 {
242 #dmsg( " reduce(): no more elements for key $key, removing" );
243 delete $listEnums{ $key }
244 }
245 }
246 }
247  
248 # if the lists have any elements left in them, shorten them.
249 @keys = sort keys( %listEnums );
250 #dmsgs( "reduce(): final keys = ", @keys );
251 foreach $key ( @keys )
252 {
253 $listEnum = $listEnums{ $key };
254 $dex = $listEnum->getIndex();
255  
256 # the index is of the last element successfully added to the space.
257 #dmsg( "reduce(): $key $dex (max=", $listEnum->getCount(), ")" );
258  
259 $list = $listEnum->getList();
260 $list->truncateAt( $dex+1 );
261 #dmsg( "reduce(): $key $dex list=", $list->toString() );
262 }
263  
264 # now set the "after" counts.
265 for( $dex = 0; $dex < $kNumberOfPartitions; $dex++ )
266 {
267 $part = $self->getPartitionAt( $dex );
268 $self->addPartitionCountAt( $dex, $part->getCount() );
269 }
270  
271 #dmsg( "...reduce()" );
272 }
273  
274 sub getHeight
275 {
276 my( $self ) = shift;
277  
278 return( $self->{ $kSpace }->getSpaceUsed() );
279 }
280  
281 #
282 # boring stuff.
283 #
284  
285 sub getRemovalPartitionIndex
286 {
287 my( $self ) = shift;
288 return( $self->{ $kRemovalPartitionIndex } );
289 }
290  
291 sub putRemovalPartitionIndex
292 {
293 my( $self, $pdex ) = @_;
294 $self->{ $kRemovalPartitionIndex } = $pdex;
295 }
296  
297 sub getPartitions
298 {
299 my( $self ) = shift;
300 return( $self->{ $kPartitionSet } );
301 }
302  
303 # as long as N < $kNumberOfPartitions,
304 # this will return a List, however,
305 # the list might have zero elements
306 # in it (but you will always get
307 # list, not undef).
308 sub getPartitionAt
309 {
310 my( $self, $n ) = @_;
311  
312 $part = $self->{ $kPartitionSet }->getListAt( $n );
313  
314 return( $part );
315 }
316  
317 sub putValueRange
318 {
319 my( $self, $sr ) = @_;
320 #dmsg( "putValueRange( $sr )" );
321 $self->{ $kValueRange } = $sr;
322 }
323  
324 sub getValueRange
325 {
326 my( $self ) = shift;
327 my( $range ) = $self->{ $kValueRange };
328 assert( defined($range), "call calculateValueRange() first" );
329 return( $range );
330 }
331  
332 sub getMaxAnnotationWidthForFont
333 {
334 my( $self ) = shift;
335 my( $fontWidth ) = shift;
336 my( $pdex );
337 my( $part );
338 my( $enum );
339 my( $wrap );
340 my( $max );
341 my( $str );
342 my( $width );
343  
344 $max = 0;
345  
346 for( $pdex = 0; $pdex < $kNumberOfPartitions; $pdex++ )
347 {
348 $part = $self->getPartitionAt( $pdex );
349 $enum = $part->getEnumerator();
350  
351 for( $wrap = $enum->getNextElement();
352 defined( $wrap );
353 $wrap = $enum->getNextElement() )
354 {
355  
356 $str = $wrap->getGraphAnnotation();
357 $width = length( $str ) * $fontWidth;
358 if( $width > $max )
359 {
360 $max = $width;
361 }
362 #dmsg( "font width=$width, max=$max" );
363 }
364 }
365  
366 return( $max );
367 }
368  
369 sub toString
370 {
371 my( $self ) = shift;
372 my( $str );
373 my( @strs );
374 my( $dex );
375 my( $step );
376  
377 $step = $self->getPartitionValueStep();
378  
379 for( $dex = 0; $dex < $kNumberOfPartitions; $dex++ )
380 {
381 $str = $dex * $step + $self->getLowValue();
382 $str .= '-';
383 $str .= ($dex+1) * $step - 1 + $self->getLowValue();
384 push( @strs, Bio::GMOD::Blast::Graph::MyUtils::makeDumpString( $dex, $str ) );
385 }
386  
387 $str = Bio::GMOD::Blast::Graph::MyUtils::makeDumpString( $self, $kNumberOfPartitions, @strs );
388  
389 return( $str );
390 }
391  
392 sub addPartitionCountAt
393 {
394 my( $self, $dex, $count ) = @_;
395 my( $ref );
396  
397 $ref = $self->{ $kPartitionCounts }->{ $dex };
398 if( !defined( $ref ) )
399 {
400 $self->{ $kPartitionCounts }->{ $dex } = [ $count ];
401 }
402 else
403 {
404 push( @{$ref}, $count );
405 }
406 }
407  
408 sub getPartitionElementsCountAfter
409 {
410 my( $self ) = shift;
411 my( $count );
412 my( $dex );
413 my( $pairRef );
414  
415 for( $dex = 0; $dex < $kNumberOfPartitions; $dex++ )
416 {
417 $pairRef = $self->getPartitionElementsCountsRefAt( $dex );
418 $count += $$pairRef[ 1 ];
419 }
420  
421 return( $count );
422 }
423  
424 sub getPartitionElementsCountBefore
425 {
426 my( $self ) = shift;
427 my( $count );
428 my( $dex );
429 my( $pairRef );
430  
431 for( $dex = 0; $dex < $kNumberOfPartitions; $dex++ )
432 {
433 $pairRef = $self->getPartitionElementsCountsRefAt( $dex );
434 $count += $$pairRef[ 0 ];
435 }
436  
437 return( $count );
438 }
439  
440 sub getPartitionElementsCountsRefAt
441 {
442 my( $self, $dex ) = @_;
443 my( $ref );
444  
445 $ref = $self->{ $kPartitionCounts }->{ $dex };
446  
447 return( $ref );
448 }
449  
450 1;
451  
452  
453  
454  
455 __END__