Graph/IntSpan.pm untested

filter: Statementstmt 0.0% 0 / 348 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 196 MC/DCmcdc 0.0% 0 / 118 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 34 totaltotal 0.0% 0 / 801 CC 35 SCAR 27.1
CC
35
Cov
0%
_copy_run_list
42.0
equal
30.0
insert
30.0
CRAP 15.1
1 # $Id: IntSpan.pm,v 1.2 2007-02-07 17:52:39 briano Exp $
2 # $Log: not supported by cvs2svn $
3 # Revision 1.1.1.1 2005/11/09 18:20:47 scottcain
4 # initial import
5 #
6 # Revision 1.2 2003/08/28 22:27:16 shuai
7 # *** empty log message ***
8 #
9 # Revision 1.1 2003/05/09 21:07:56 shuai
10 # Initial revision
11 #
12 # Revision 1.1 1999/10/08 22:49:16 blast
13 # Initial revision
14 #
15 # Revision 1.6 1996/06/03 18:28:29 swm
16 # runs clean under -w
17 # moved test code to t/*.t
18 #
19 # Revision 1.5 1996/05/30 13:34:47 swm
20 # added valid(), min() and max()
21 # documentation fixes
22 #
23 # Revision 1.4 1996/02/22 20:06:04 swm
24 # added $Bio::GMOD::Blast::Graph::IntSpan::Empty_String
25 # made IntSpan an Exporter
26 # documentation fixes
27  
28 package Bio::GMOD::Blast::Graph::IntSpan;
29 BEGIN {
30 $Bio::GMOD::Blast::Graph::IntSpan::AUTHORITY = 'cpan:RBUELS';
31 }
32 BEGIN {
33 $Bio::GMOD::Blast::Graph::IntSpan::VERSION = '0.06';
34 }
35 $Bio::GMOD::Blast::Graph::IntSpan::VERSION = 1.03;
36  
37 require Exporter;
38 @ISA = qw(Exporter);
39  
40 use strict;
41 use integer;
42  
43  
44  
45 $Bio::GMOD::Blast::Graph::IntSpan::Empty_String = '-';
46  
47  
48 sub new
49 {
50 my($class, $set_spec) = @_;
51  
52 my $set = bless { }, $class;
53 $set->{empty_string} = \$Bio::GMOD::Blast::Graph::IntSpan::Empty_String;
54 copy $set $set_spec;
55 }
56  
57  
58 sub valid
59 {
60 my($class, $run_list) = @_;
61  
62 my $set = new Bio::GMOD::Blast::Graph::IntSpan;
63 eval { _copy_run_list $set $run_list };
64 return $@ ? 0 : 1;
65 }
66  
67  
68 sub copy
69 {
70 my($set, $set_spec) = @_;
71  
72 SWITCH:
73 {
74 $set_spec or _copy_empty ($set ), last;
75 ref $set_spec or _copy_run_list($set, $set_spec), last;
76 ref $set_spec eq 'ARRAY' and _copy_array ($set, $set_spec), last;
77 _copy_set ($set, $set_spec) ;
78 }
79  
80 $set;
81 }
82  
83  
84 sub _copy_empty # makes $set the empty set
85 {
86 my $set = shift;
87  
88 $set->{negInf} = 0;
89 $set->{posInf} = 0;
90 $set->{edges } = [];
91 }
92  
93  
94 sub _copy_array # copies an array into a set
95 {
96 my($set, $array) = @_;
97 my($element, @edges);
98  
99 $set->{negInf} = 0;
100 $set->{posInf} = 0;
101  
102 for $element (sort { $Bio::GMOD::Blast::Graph::IntSpan::a <=> $Bio::GMOD::Blast::Graph::IntSpan::b } @$array)
103 {
104 next if @edges and $edges[-1] == $element; # skip duplicates
105  
106 if (@edges and $edges[-1] == $element-1)
107 {
108 $edges[-1] = $element;
109 }
110 else
111 {
112 push @edges, $element-1, $element;
113 }
114 }
115  
116 $set->{edges} = \@edges;
117 }
118  
119  
120 sub _copy_set # copies one set to another
121 {
122 my($dest, $src) = @_;
123  
124 $dest->{negInf} = $src->{negInf};
125 $dest->{posInf} = $src->{posInf};
126 $dest->{edges } = [ @{$src->{edges }} ];
127 }
128  
129  
130 sub _copy_run_list # parses a run list
131 {
132 my($set, $runList) = @_;
133 my($run, @edges);
134  
135 _copy_empty($set);
136  
137 $runList =~ s/\s|_//g;
138 return if $runList eq '-'; # empty set
139  
140 my($first, $last) = (1, 0); # verifies order of infinite runs
141  
142 for $run (split(/,/ , $runList))
143 {
144 die "Bad order: $runList\n" if $last;
145  
146 RUN:
147 {
148 $run =~ /^ (-?\d+) $/x and do
149 {
150 push(@edges, $1-1, $1);
151 last RUN;
152 };
153  
154 $run =~ /^ (-?\d+) - (-?\d+) $/x and do
155 {
156 die "Bad order: $runList\n" if $1 > $2;
157 push(@edges, $1-1, $2);
158 last RUN;
159 };
160  
161 $run =~ /^ \( - (-?\d+) $/x and do
162 {
163 die "Bad order: $runList\n" unless $first;
164 $set->{negInf} = 1;
165 push @edges, $1;
166 last RUN;
167 };
168  
169 $run =~ /^ (-?\d+) - \) $/x and do
170 {
171 push @edges, $1-1;
172 $set->{posInf} = 1;
173 $last = 1;
174 last RUN;
175 };
176  
177 $run =~ /^ \( - \) $/x and do
178 {
179 die "Bad order: $runList\n" unless $first;
180 $last = 1;
181 $set->{negInf} = 1;
182 $set->{posInf} = 1;
183 last RUN;
184 };
185  
186 die "Bad syntax: $runList\n";
187 }
188  
189 $first = 0;
190 }
191  
192 $set->{edges} = [ @edges ];
193  
194 _cleanup $set or die "Bad order: $runList\n";
195 }
196  
197  
198 # check for overlapping runs
199 # delete duplicate edges
200 sub _cleanup
201 {
202 my $set = shift;
203 my $edges = $set->{edges};
204  
205 my $i=0;
206 while ($i < $#$edges)
207 {
208 my $cmp = $$edges[$i] <=> $$edges[$i+1];
209 {
210 $cmp == -1 and $i++ , last;
211 $cmp == 0 and splice(@$edges, $i, 2), last;
212 $cmp == 1 and return 0;
213 }
214 }
215  
216 1;
217 }
218  
219  
220 sub run_list
221 {
222 my $set = shift;
223  
224 return ${$set->{empty_string}} if empty $set;
225  
226 my @edges = @{$set->{edges}};
227 my @runs;
228  
229 $set->{negInf} and unshift @edges, '(';
230 $set->{posInf} and push @edges, ')';
231  
232 while(@edges)
233 {
234 my($lower, $upper) = splice @edges, 0, 2;
235  
236 if ($lower ne '(' and $upper ne ')' and $lower+1==$upper)
237 {
238 push @runs, $upper;
239 }
240 else
241 {
242 $lower ne '(' and $lower++;
243 push @runs, "$lower-$upper";
244 }
245 }
246  
247 join(',', @runs);
248 }
249  
250  
251 sub elements
252 {
253 my $set = shift;
254  
255 ($set->{negInf} or $set->{posInf}) and die "elements: infinite set\n";
256  
257 my @elements;
258 my @edges = @{$set->{edges}};
259 while (@edges)
260 {
261 my $lower = shift(@edges) + 1;
262 my $upper = shift(@edges);
263 push @elements, $lower..$upper;
264 }
265  
266 wantarray ? @elements : \@elements;
267 }
268  
269  
270 sub _real_set # converts a set specification into a set
271 {
272 my($set_spec) = shift;
273  
274 SWITCH:
275 {
276 $set_spec or return new Bio::GMOD::Blast::Graph::IntSpan;
277 ref $set_spec or return new Bio::GMOD::Blast::Graph::IntSpan $set_spec;
278 ref $set_spec eq 'ARRAY' and return new Bio::GMOD::Blast::Graph::IntSpan $set_spec;
279 }
280  
281 $set_spec;
282 }
283  
284  
285 sub union
286 {
287 my($a, $set_spec) = @_;
288 my $b = _real_set($set_spec);
289  
290 my $s = new Bio::GMOD::Blast::Graph::IntSpan;
291 $s->{negInf} = $a->{negInf} || $b->{negInf};
292  
293 my $eA = $a->{edges};
294 my $eB = $b->{edges};
295 my $eS = $s->{edges};
296  
297 my $inA = $a->{negInf};
298 my $inB = $b->{negInf};
299  
300 my $iA = 0;
301 my $iB = 0;
302 while ($iA<@$eA and $iB<@$eB)
303 {
304 my $xA = $$eA[$iA];
305 my $xB = $$eB[$iB];
306  
307 if ($xA < $xB)
308 {
309 $iA++;
310 $inA = ! $inA;
311 not $inB and push(@$eS, $xA);
312 }
313 elsif ($xB < $xA)
314 {
315 $iB++;
316 $inB = ! $inB;
317 not $inA and push(@$eS, $xB);
318 }
319 else
320 {
321 $iA++;
322 $iB++;
323 $inA = ! $inA;
324 $inB = ! $inB;
325 $inA == $inB and push(@$eS, $xA);
326 }
327 }
328  
329 $iA < @$eA and ! $inB and push(@$eS, @$eA[$iA..$#$eA]);
330 $iB < @$eB and ! $inA and push(@$eS, @$eB[$iB..$#$eB]);
331  
332 $s->{posInf} = $a->{posInf} || $b->{posInf};
333 $s;
334 }
335  
336  
337 sub intersect
338 {
339 my($a, $set_spec) = @_;
340 my $b = _real_set($set_spec);
341  
342 my $s = new Bio::GMOD::Blast::Graph::IntSpan;
343 $s->{negInf} = $a->{negInf} && $b->{negInf};
344  
345 my $eA = $a->{edges};
346 my $eB = $b->{edges};
347 my $eS = $s->{edges};
348  
349 my $inA = $a->{negInf};
350 my $inB = $b->{negInf};
351  
352 my $iA = 0;
353 my $iB = 0;
354 while ($iA<@$eA and $iB<@$eB)
355 {
356 my $xA = $$eA[$iA];
357 my $xB = $$eB[$iB];
358  
359 if ($xA < $xB)
360 {
361 $iA++;
362 $inA = ! $inA;
363 $inB and push(@$eS, $xA);
364 }
365 elsif ($xB < $xA)
366 {
367 $iB++;
368 $inB = ! $inB;
369 $inA and push(@$eS, $xB);
370 }
371 else
372 {
373 $iA++;
374 $iB++;
375 $inA = ! $inA;
376 $inB = ! $inB;
377 $inA == $inB and push(@$eS, $xA);
378 }
379 }
380  
381 $iA < @$eA and $inB and push(@$eS, @$eA[$iA..$#$eA]);
382 $iB < @$eB and $inA and push(@$eS, @$eB[$iB..$#$eB]);
383  
384 $s->{posInf} = $a->{posInf} && $b->{posInf};
385 $s;
386 }
387  
388  
389 sub diff
390 {
391 my($a, $set_spec) = @_;
392 my $b = _real_set($set_spec);
393  
394 my $s = new Bio::GMOD::Blast::Graph::IntSpan;
395 $s->{negInf} = $a->{negInf} && ! $b->{negInf};
396  
397 my $eA = $a->{edges};
398 my $eB = $b->{edges};
399 my $eS = $s->{edges};
400  
401 my $inA = $a->{negInf};
402 my $inB = $b->{negInf};
403  
404 my $iA = 0;
405 my $iB = 0;
406 while ($iA<@$eA and $iB<@$eB)
407 {
408 my $xA = $$eA[$iA];
409 my $xB = $$eB[$iB];
410  
411 if ($xA < $xB)
412 {
413 $iA++;
414 $inA = ! $inA;
415 not $inB and push(@$eS, $xA);
416 }
417 elsif ($xB < $xA)
418 {
419 $iB++;
420 $inB = ! $inB;
421 $inA and push(@$eS, $xB);
422 }
423 else
424 {
425 $iA++;
426 $iB++;
427 $inA = ! $inA;
428 $inB = ! $inB;
429 $inA != $inB and push(@$eS, $xA);
430 }
431 }
432  
433 $iA < @$eA and not $inB and push(@$eS, @$eA[$iA..$#$eA]);
434 $iB < @$eB and $inA and push(@$eS, @$eB[$iB..$#$eB]);
435  
436 $s->{posInf} = $a->{posInf} && ! $b->{posInf};
437 $s;
438 }
439  
440  
441 sub xor
442 {
443 my($a, $set_spec) = @_;
444 my $b = _real_set($set_spec);
445  
446 my $s = new Bio::GMOD::Blast::Graph::IntSpan;
447 $s->{negInf} = $a->{negInf} ^ $b->{negInf};
448  
449 my $eA = $a->{edges};
450 my $eB = $b->{edges};
451 my $eS = $s->{edges};
452  
453 my $iA = 0;
454 my $iB = 0;
455 while ($iA<@$eA and $iB<@$eB)
456 {
457 my $xA = $$eA[$iA];
458 my $xB = $$eB[$iB];
459  
460 if ($xA < $xB)
461 {
462 $iA++;
463 push(@$eS, $xA);
464 }
465 elsif ($xB < $xA)
466 {
467 $iB++;
468 push(@$eS, $xB);
469 }
470 else
471 {
472 $iA++;
473 $iB++;
474 }
475 }
476  
477 $iA < @$eA and push(@$eS, @$eA[$iA..$#$eA]);
478 $iB < @$eB and push(@$eS, @$eB[$iB..$#$eB]);
479  
480 $s->{posInf} = $a->{posInf} ^ $b->{posInf};
481 $s;
482 }
483  
484  
485 sub complement
486 {
487 my $set = shift;
488 my $comp = new Bio::GMOD::Blast::Graph::IntSpan $set;
489  
490 $comp->{negInf} = ! $comp->{negInf};
491 $comp->{posInf} = ! $comp->{posInf};
492 $comp;
493 }
494  
495  
496 sub superset
497 {
498 my($a, $set_spec) = @_;
499 my $b = _real_set($set_spec);
500  
501 empty(diff($b, $a));
502 }
503  
504  
505 sub subset
506 {
507 my($a, $b) = @_;
508  
509 empty(diff($a, $b));
510 }
511  
512  
513 sub equal
514 {
515 my($a, $set_spec) = @_;
516 my $b = _real_set($set_spec);
517  
518 return 0 unless $a->{negInf} == $b->{negInf};
519 return 0 unless $a->{posInf} == $b->{posInf};
520  
521 my $aEdge = $a->{edges};
522 my $bEdge = $b->{edges};
523 return 0 unless @$aEdge == @$bEdge;
524  
525 my $i;
526 for ($i=0; $i<@$aEdge; $i++)
527 {
528 return 0 unless $$aEdge[$i] == $$bEdge[$i];
529 }
530  
531 1;
532 }
533  
534  
535 sub equivalent
536 {
537 my($a, $set_spec) = @_;
538 my $b = _real_set($set_spec);
539  
540 cardinality($a) == cardinality($b);
541 }
542  
543  
544 sub cardinality
545 {
546 my $set = shift;
547  
548 ($set->{negInf} or $set->{posInf}) and return -1;
549  
550 my $cardinality = 0;
551 my @edges = @{$set->{edges}};
552 while (@edges)
553 {
554 my $lower = shift @edges;
555 my $upper = shift @edges;
556 $cardinality += $upper - $lower;
557 }
558  
559 $cardinality;
560 }
561  
562  
563 sub empty
564 {
565 my $set = shift;
566  
567 not $set->{negInf} and not @{$set->{edges}} and not $set->{posInf};
568 }
569  
570  
571 sub finite
572 {
573 my $set = shift;
574  
575 not $set->{negInf} and not $set->{posInf};
576 }
577  
578  
579 sub neg_inf
580 {
581 my $set = shift;
582  
583 $set->{negInf};
584 }
585  
586  
587 sub pos_inf
588 {
589 my $set = shift;
590  
591 $set->{posInf};
592 }
593  
594  
595 sub infinite
596 {
597 my $set = shift;
598  
599 $set->{negInf} or $set->{posInf};
600 }
601  
602  
603 sub universal
604 {
605 my $set = shift;
606  
607 $set->{negInf} and not @{$set->{edges}} and $set->{posInf};
608 }
609  
610  
611 sub member
612 {
613 my($set, $n) = @_;
614  
615 my $inSet = $set->{negInf};
616  
617 my $edge = $set->{edges};
618 my $i;
619  
620 for ($i=0; $i<@$edge; $i++)
621 {
622 if ($inSet)
623 {
624 return 1 if $n <= $$edge[$i];
625 $inSet = 0;
626 }
627 else
628 {
629 return 0 if $n <= $$edge[$i];
630 $inSet = 1;
631 }
632 }
633  
634 $inSet;
635 }
636  
637  
638 sub insert
639 {
640 my($set, $n) = @_;
641  
642 my $inSet = $set->{negInf};
643  
644 my $edge = $set->{edges};
645 my $i;
646  
647 for ($i=0; $i<@$edge; $i++)
648 {
649 if ($inSet)
650 {
651 return if $n <= $$edge[$i];
652 $inSet = 0;
653 }
654 else
655 {
656 last if $n <= $$edge[$i];
657 $inSet = 1;
658 }
659 }
660  
661 return if $inSet;
662  
663 splice @{$set->{edges}}, $i, 0, $n-1, $n;
664 _cleanup($set);
665 }
666  
667 sub remove
668 {
669 my($set, $n) = @_;
670  
671 my $inSet = $set->{negInf};
672  
673 my $edge = $set->{edges};
674 my $i;
675  
676 for ($i=0; $i<@$edge; $i++)
677 {
678 if ($inSet)
679 {
680 last if $n <= $$edge[$i];
681 $inSet = 0;
682 }
683 else
684 {
685 return if $n <= $$edge[$i];
686 $inSet = 1;
687 }
688 }
689  
690 return unless $inSet;
691  
692 splice @{$set->{edges}}, $i, 0, $n-1, $n;
693 _cleanup($set);
694 }
695  
696  
697 sub min
698 {
699 my $set = shift;
700  
701 empty $set and return undef;
702 neg_inf $set and return undef;
703 $set->{edges}->[0]+1;
704 }
705  
706  
707 sub max
708 {
709 my $set = shift;
710  
711 empty $set and return undef;
712 pos_inf $set and return undef;
713 $set->{edges}->[-1];
714 }
715  
716  
717 1
718  
719  
720 __END__