File Coverage

blib/lib/OBO/APO/GoaAssociationSet.pm
Criterion Covered Total %
statement 89 92 96.7
branch 24 32 75.0
condition n/a
subroutine 10 10 100.0
pod 6 6 100.0
total 129 140 92.1


line stmt bran cond sub pod time code
1             # $Id: GoaAssociationSet.pm 2010-09-29 erick.antezana $
2             #
3             # Module : GoaAssociationSet.pm
4             # Purpose : GOA association set.
5             # License : Copyright (c) 2006, 2007, 2008 ONTO-perl. All rights reserved.
6             # This program is free software; you can redistribute it and/or
7             # modify it under the same terms as Perl itself.
8              
9             package OBO::APO::GoaAssociationSet;
10             our @ISA = qw(OBO::Util::Set); # TODO change inheritence
11              
12             =head1 NAME
13              
14             OBO::APO::GoaAssociationSet - A GoaAssociationSet implementation
15            
16             =head1 SYNOPSIS
17              
18             use OBO::APO::GoaAssociationSet;
19             use OBO::APO::GoaAssociation;
20             use strict;
21              
22             my $my_set = OBO::APO::GoaAssociationSet->new();
23              
24             # three new goa_association's
25             my $goa_association1 = OBO::APO::GoaAssociation->new();
26             my $goa_association2 = OBO::APO::GoaAssociation->new();
27             my $goa_association3 = OBO::APO::GoaAssociation->new();
28              
29             $goa_association1->assc_id("APO:vm");
30             $goa_association2->assc_id("APO:ls");
31             $goa_association3->assc_id("APO:ea");
32              
33             # remove from my_set
34             $my_set->remove($goa_association1);
35             $my_set->add($goa_association1);
36             $my_set->remove($goa_association1);
37              
38             ### set versions ###
39             $my_set->add($goa_association1);
40             $my_set->add($goa_association2);
41             $my_set->add($goa_association3);
42              
43             my $goa_association4 = OBO::APO::GoaAssociation->new();
44             my $goa_association5 = OBO::APO::GoaAssociation->new();
45             my $goa_association6 = OBO::APO::GoaAssociation->new();
46              
47             $goa_association4->assc_id("APO:ef");
48             $goa_association5->assc_id("APO:sz");
49             $goa_association6->assc_id("APO:qa");
50              
51             $my_set->add_all($goa_association4, $goa_association5, $goa_association6);
52              
53             $my_set->add_all($goa_association4, $goa_association5, $goa_association6);
54              
55             # remove from my_set
56             $my_set->remove($goa_association4);
57              
58             my $goa_association7 = $goa_association4;
59             my $goa_association8 = $goa_association5;
60             my $goa_association9 = $goa_association6;
61              
62             my $my_set2 = OBO::APO::GoaAssociationSet->new();
63              
64             $my_set->add_all($goa_association4, $goa_association5, $goa_association6);
65             $my_set2->add_all($goa_association7, $goa_association8, $goa_association9, $goa_association1, $goa_association2, $goa_association3);
66              
67             $my_set2->clear();
68              
69             =head1 DESCRIPTION
70              
71             A set (OBO::Util::Set) of goa_association records.
72              
73             =head1 COPYRIGHT AND LICENSE
74              
75             Copyright (C) 2006 by ONTO-perl
76              
77             This library is free software; you can redistribute it and/or modify
78             it under the same terms as Perl itself, either Perl version 5.8.7 or,
79             at your option, any later version of Perl 5 you may have available.
80              
81             =cut
82              
83 3     3   387 use OBO::Util::Set;
  3         5  
  3         76  
84 3     3   9 use strict;
  3         3  
  3         70  
85 3     3   10 use warnings;
  3         7  
  3         60  
86 3     3   11 use Carp;
  3         3  
  3         1855  
87              
88             =head2 add
89              
90             Usage - $set->add($goa_association)
91             Returns - true if the element was successfully added
92             Args - the element (OBO::APO::GoaAssociation) to be added
93             Function - adds an element to this set
94              
95             =cut
96             sub add {
97 27     27 1 31 my $self = shift;
98 27         22 my $result = 0; # nothing added
99 27 50       39 if (@_) {
100 27         23 my $ele = shift;
101 27 100       35 if ( !$self -> contains($ele) ) {
102 20         14 push @{$self->{SET}}, $ele;
  20         28  
103 20         18 $result = 1; # successfully added
104             }
105             }
106 27         49 return $result;
107             }
108              
109             =head2 add_unique
110              
111             Usage - $set->add_unique($goa_association)
112             Returns - 1 (the element is always added)
113             Args - the element (OBO::APO::GoaAssociation) to be added which is known to be unique!
114             Function - adds an element to this set
115             Remark - this function should be used when the element to be added is known to be unique,
116             this function has a tremendous impact on the performance (compared to simply add())
117              
118             =cut
119             sub add_unique {
120 29     29 1 29 my $self = shift;
121 29         28 my $result = 0; # nothing added
122 29 50       52 if (@_) {
123 29         31 my $ele = shift;
124 29         21 push @{$self->{SET}}, $ele;
  29         70  
125             }
126 29         167 return 1;
127             }
128              
129             =head2 remove
130              
131             Usage - $set->remove($element)
132             Returns - the removed element (OBO::APO::GoaAssociation)
133             Args - the element to be removed (OBO::APO::GoaAssociation)
134             Function - removes an element from this set
135              
136             =cut
137             sub remove {
138 3     3 1 8 my $self = shift;
139 3         4 my $result = undef;
140 3 50       7 if (@_) {
141 3         3 my $ele = shift;
142 3 100       14 if ($self->size() > 0) {
143 2         4 for (my $i = 0; $i < scalar(@{$self->{SET}}); $i++){
  5         11  
144 5         2 my $e = ${$self->{SET}}[$i];
  5         6  
145 5 100       9 if ($ele->equals($e)) {
146 2 100       4 if ($self->size() > 1) {
    50          
147 1         2 my $first_elem = shift (@{$self->{SET}});
  1         3  
148 1         1 ${$self->{SET}}[$i-1] = $first_elem;
  1         3  
149             } elsif ($self->size() == 1) {
150 1         1 shift (@{$self->{SET}});
  1         2  
151             }
152 2         3 $result = $ele;
153 2         3 last;
154             }
155             }
156             }
157             }
158 3         6 return $result;
159             }
160              
161             =head2 remove_duplicates
162              
163             Usage - $set->remove_duplicates()
164             Returns - a set object (OBO::APO::GoaAssociationSet)
165             Args - none
166             Function - eliminates redundency in a GOA association set object (OBO::APO::GoaAssociationSet)
167              
168             =cut
169             sub remove_duplicates {
170 2     2 1 2 my $self = shift;
171 2         2 my @list = @{$self->{SET}};
  2         5  
172 2         3 my @set = ();
173 2         4 while (scalar (@list)) {
174 12         12 my $ele = pop(@list);
175 12         10 my $result = 0;
176 12         17 foreach (@list) {
177 29 100       41 if ($ele->equals($_)) {
178 2         2 $result = 1;
179 2         3 last;
180             }
181             }
182 12 100       33 unshift @set, $ele if $result == 0;
183             }
184 2         2 @{$self->{SET}} = @set;
  2         5  
185 2         3 return $self;
186             }
187              
188              
189             =head2 contains
190              
191             Usage - $set->contains($goa_association)
192             Returns - either 1(true) or 0 (false)
193             Args - the element (OBO::APO::GoaAssociation) to be checked
194             Function - checks if this set constains the given element
195              
196             =cut
197             sub contains {
198 40     40 1 45 my $self = shift;
199 40         29 my $result = 0;
200 40 50       57 if (@_){
201 40         27 my $target = shift;
202 40         27 foreach my $ele (@{$self->{SET}}){
  40         66  
203 116 100       703 if ($target->equals($ele)) {
204 17         15 $result = 1;
205 17         19 last;
206             }
207             }
208             }
209 40         84 return $result;
210             }
211              
212             =head2 equals
213              
214             Usage - $set->equals($another_goa_assocations_set)
215             Returns - either 1 (true) or 0 (false)
216             Args - the set (OBO::APO::GoaAssociationSet) to compare with
217             Function - tells whether this set is equal to the given one
218              
219             =cut
220             sub equals {
221 3     3 1 5 my $self = shift;
222 3         3 my $result = 0; # I guess they'are NOT identical
223 3 50       9 if (@_) {
224 3         3 my $other_set = shift;
225 3         7 my %count = ();
226 3         3 my @this = @{$self->{SET}};
  3         7  
227 3         11 my @that = $other_set->get_set();
228              
229 3 100       10 if ($#this == $#that) {
230 2 50       4 if ($#this != -1) {
231 2         4 foreach (@this, @that) {
232 18         29 $count{ $_->annot_src().
233             $_->aspect().
234             $_->assc_id().
235             $_->date().
236             $_->description().
237             $_->evid_code().
238             $_->go_id().
239             $_->obj_id().
240             $_->obj_src().
241             $_->obj_symb().
242             $_->qualifier().
243             $_->refer().
244             $_->sup_ref().
245             $_->synonym().
246             $_->taxon().
247             $_->type()}++;
248             }
249 2         5 foreach my $count (values %count) {
250 9 50       12 if ($count != 2) {
251 0         0 $result = 0;
252 0         0 last;
253             } else {
254 9         14 $result = 1;
255             }
256             }
257             } else {
258 0         0 $result = 1; # they are equal: empty arrays
259             }
260             }
261             }
262 3         10 return $result;
263             }
264              
265             1;