File Coverage

blib/lib/Bio/Metabolic/Substrate/Cluster.pm
Criterion Covered Total %
statement 66 70 94.2
branch 16 24 66.6
condition 2 3 66.6
subroutine 14 15 93.3
pod 9 9 100.0
total 107 121 88.4


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Bio::Metabolic::Substrate::Cluster - Perl extension for lists of
5             metabolic compounds
6              
7             =head1 SYNOPSIS
8              
9             use Bio::Metabolic::Substrate::Cluster;
10              
11             $cl = Bio::Metabolic::Substrate::Cluster->new(@substrate_list);
12              
13             $clcopy = $cl->copy;
14              
15             @list = $cl->list;
16              
17             if ($cl->has($substrate)) { ... }
18              
19             $substrate_nr = $cl->which($substrate);
20              
21             $cl->add_substrates(@substrate_list);
22              
23             $removed = $cl->remove_substrates(@substrate_list);
24              
25             =head1 DESCRIPTION
26              
27             This class implements the object class for clusters of biochemical compounds.
28             Essentially, a Bio::Metabolic::Substrate::Cluster object is an arrayref to list
29             of Bio::Metabolic::Substrate objects.
30              
31             =head2 EXPORT
32              
33             None
34              
35             =head2 OVERLOADED OPERATORS
36              
37             String Conversion
38             $string = "$cluster";
39             print "\$cluster = '$cluster'\n";
40              
41             Addition
42             $clbig = $cl1 + $cl2;
43              
44             =head2 CLASS METHODS
45              
46             $cl = Bio::Metabolic::Substrate::Cluster->new(@substrate_list);
47              
48             The constructor method creating a Bio::Metabolic::Substrate::Cluster
49             object from a list of Bio::Metabolic::Substrate objects.
50              
51              
52             =head2 OBJECT METHODS
53              
54             $clcopy = $cl->copy;
55              
56             creates an exact copy of a cluster
57              
58             @list = $cl->list;
59              
60             returns a list of Bio::Metabolic::Substrate objects
61              
62              
63             $cl->has($substrate))
64              
65             returns 1 if $substrate is a member of $cl, 0 otherwise
66              
67              
68             $substrate_nr = $cl->which($substrate);
69              
70             returns the index of the element representing $substrate
71              
72             EXAMPLE: @list = $cl->list;
73             if ($cl->has($substrate)) {
74             $ind = $cl->which($substrate);
75             print "BOO!\n" if ($substrate == $list[$ind]); # prints "BOO!"
76             }
77              
78              
79             $cl->add_substrates(@substrate_list);
80              
81             adds the list of Bio::Metabolic::Substrate objects to the cluster $cl
82              
83              
84             $removed = $cl->remove_substrates(@substrate_list);
85              
86             removes the list of Bio::Metabolic::Substrate objects if present.
87             Returns a cluster containing all removed substrates.
88              
89              
90              
91             =head1 AUTHOR
92              
93             Oliver Ebenhöh, oliver.ebenhoeh@rz.hu-berlin.de
94              
95             =head1 SEE ALSO
96              
97             Bio::Metabolic, Bio::Metabolic::Substrate.
98              
99             =cut
100              
101             package Bio::Metabolic::Substrate::Cluster;
102              
103             require 5.005_62;
104 5     5   29 use strict;
  5         11  
  5         187  
105 5     5   29 use warnings;
  5         10  
  5         176  
106              
107             require Exporter;
108              
109 5     5   23 use Carp;
  5         10  
  5         738  
110              
111             use overload
112 5         45 "+" => \&add_clusters,
113 5     5   29 "\"\"" => \&cluster_to_string;
  5         8  
114              
115             our @ISA = qw(Exporter);
116              
117             # Items to export into callers namespace by default. Note: do not export
118             # names by default without a very good reason. Use EXPORT_OK instead.
119             # Do not simply export all your public functions/methods/constants.
120              
121             # This allows declaration use Bio::Metabolic::Substrate::Cluster ':all';
122             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
123             # will save memory.
124             our %EXPORT_TAGS = (
125             'all' => [
126             qw(
127              
128             )
129             ]
130             );
131              
132             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
133              
134             our @EXPORT = qw(
135              
136             );
137             our $VERSION = '0.06';
138              
139             =head1 METHODS
140              
141             =head2 Constructor new
142              
143             Creates a new instance of a Bio::Metabolic::Substrate::Cluster object.
144             Can be invoked as class method or object method. In the first case returns
145             a Bio::Metabolic::Substrate::Cluster genereted from the arguments (array or arrayref).
146             In the second case it returns a clone.
147              
148             $cl = Bio::Metabolic::Substrate::Cluster->new($substrate1, $substrate2, $substrate3 ...);
149              
150             $cl = Bio::Metabolic::Substrate::Cluster->new([$substrate1, $substrate2, $substrate3 ...]);
151              
152             $cl = $proto->new();
153              
154             =cut
155              
156             sub new {
157 21     21 1 603 my $proto = shift;
158 21   66     92 my $pkg = ref($proto) || $proto;
159              
160 21         26 my @substrates;
161 21 100       47 if ( ref($proto) eq 'Bio::Metabolic::Substrate::Cluster' ) {
162 1         4 @substrates = $proto->list;
163             }
164             else {
165 20 100       67 @substrates = ref( $_[0] ) eq 'ARRAY' ? @{ $_[0] } : @_;
  1         3  
166             }
167              
168 21         58 my $new_cluster = $pkg->_new_empty();
169              
170 21         59 $new_cluster->add_substrates(@substrates);
171              
172 21         287 return $new_cluster;
173             }
174              
175             # method _new_empty() for internal use only.
176             # Returns an empty object
177              
178             sub _new_empty {
179 22     22   38 my $pkg = shift;
180 22 50       53 $pkg = ref($pkg) if ref($pkg);
181              
182 22         138 return bless {
183             pos => {},
184             substrates => [],
185             } => $pkg;
186             }
187              
188             # accessor method _position for internal use only
189              
190             sub _position {
191 139     139   602 my $self = shift;
192 139 50       281 $self->{pos} = shift if @_;
193 139         581 return $self->{pos};
194             }
195              
196             =head2 Method copy
197              
198             copy() is exactly the same as $cl2 = $cl1->new();
199              
200             =cut
201              
202             sub copy {
203 1     1 1 1502 my $cluster = shift;
204 1         5 return ref($cluster)->new( $cluster->list );
205             }
206              
207             =head2 Method list
208              
209             list() returns the substrates of the objects as a list in array context, as an arrayref
210             in scalar context.
211              
212             =cut
213              
214             sub list {
215 82     82 1 1620 my $cluster = shift;
216 82 100       252 return wantarray ? @{ $cluster->{substrates} } : $cluster->{substrates};
  34         137  
217             }
218              
219             =head2 Method cluster_to_string
220              
221             cluster_to_string() returns a readable string listing the substrates in the object.
222              
223             =cut
224              
225             sub cluster_to_string {
226 0     0 1 0 my $cluster = shift;
227              
228 0         0 return "(" . join( ",", $cluster->list ) . ")";
229             }
230              
231             =head2 Method has
232              
233             has($sub) returns 1 if the object contains $sub, 0 otherwise.
234              
235             =cut
236              
237             sub has {
238 85     85 1 11974 my $cluster = shift;
239 85         109 my $substrate = shift;
240              
241 85         163 return defined $cluster->_position->{ $substrate->name };
242              
243             # my $hassub = 0;
244             # foreach my $sub ( $cluster->list ) {
245             # return 1 if $sub == $substrate;
246             # }
247              
248             # return 0;
249             }
250              
251             =head2 method add_substrates
252              
253             this method modifies the object in-place, adding the substrates passed as arguments
254             (array or arrayref)
255              
256             =cut
257              
258             sub add_substrates {
259 25     25 1 474 my $cluster = shift;
260 25 50       81 my @substrates = ref( $_[0] ) eq 'ARRAY' ? @{ $_[0] } : @_;
  0         0  
261              
262 25         137 while ( my $substrate = shift(@substrates) ) {
263              
264             # push( @$cluster, $substrate ) unless $cluster->has($substrate);
265 47 100       106 next if $cluster->has($substrate);
266 45         110 my $listref = $cluster->list;
267 45         110 $cluster->_position->{ $substrate->name } = $#$listref + 1;
268 45         216 push( @$listref, $substrate );
269             }
270             }
271              
272             #sub force_add_substrates {
273             # my $cluster = shift;
274             # my @substrates = @_;
275              
276             # while (my $substrate = shift(@substrates)) {
277             # push(@$cluster,$substrate);
278             # }
279             #}
280              
281             =head2 method remove_substrates
282              
283             this method modifies the object in-place, removing the substrates passed as arguments
284             from the list (array or arrayref).
285             Returns the removed substrates as Bio::Metabolic::Substrate::Cluster.
286              
287             =cut
288              
289             sub remove_substrates {
290 1     1 1 3 my $cluster = shift;
291 1 50       6 my @substrates = ref( $_[0] ) eq 'ARRAY' ? @{ $_[0] } : @_;
  0         0  
292              
293 1         2 my @removed = ();
294 1         5 while ( my $substrate = shift(@substrates) ) {
295 2 50       6 next unless $cluster->has($substrate);
296 2         8 my $rempos = $cluster->which($substrate);
297 2         17 my $listref = $cluster->list;
298 2         6 push( @removed, splice( @$listref, $rempos, 1 ) );
299 2         7 delete $cluster->_position->{ $substrate->name };
300 2         5 foreach my $v ( values %{ $cluster->_position } ) {
  2         5  
301 3 50       24 $v-- if $v > $rempos;
302             }
303             }
304              
305             # while ( my $substrate = shift(@substrates) ) {
306             # for ( my $i = 0 ; $i < @$cluster ; $i++ ) {
307             # if ( $cluster->[$i] == $substrate ) {
308             # push( @removed, splice( @$cluster, $i, 1 ) );
309             # }
310             # }
311             # }
312              
313 1         5 return ref($cluster)->new(@removed);
314             }
315              
316             =head2 method add_clusters
317              
318             this method returns a cluster containing substrates from an arbitrary large list of clusters.
319              
320             =cut
321              
322             sub add_clusters {
323 1     1 1 3 my @clusters = @_;
324              
325             # this is due to an extra value passed by the overload Module
326 1 50       8 pop(@clusters)
327             if ( ref( $clusters[ @clusters - 1 ] ) ne ref( $clusters[0] ) );
328              
329 1 50       6 croak("add_clusters needs at least one clusters!") if @clusters == 0;
330              
331 1         5 my $new_cluster = ref( $clusters[0] )->new;
332              
333 1         3 foreach my $cluster (@clusters) {
334 2         6 $new_cluster->add_substrates( $cluster->list );
335             }
336              
337 1         4 return $new_cluster;
338             }
339              
340             =head2 method which
341              
342             If $cl is Bio::Metabolic::Substrate::Cluster, which($substrate) returns the index
343             of the list containing $substrates.
344             I.e.
345             $i = $cl->which($substrate);
346             @list = $cl->list;
347             print "TRUE\n" if $list[$i] == $substrate; # prints 'TRUE'
348              
349             =cut
350              
351             sub which {
352 4     4 1 1053 my $self = shift;
353              
354             # my @slist = shift->list;
355 4         7 my $substrate = shift;
356              
357 4         8 return $self->_position->{ $substrate->name };
358              
359             # my $cnt;
360             # for ( $cnt = 0 ; $cnt < @slist ; $cnt++ ) {
361             # last if $slist[$cnt] == $substrate;
362             # }
363             # $cnt = undef if $cnt == @slist;
364             # return $cnt;
365             }
366              
367             1;
368             __END__