Metabolic/Substrate/Cluster.pm

filter: Statementstmt 94.9% 75 / 79 Branchbran 75.0% 21 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.3% 14 / 15 Podpod 100% 9 / 9 totaltotal 88.9% 121 / 136 CC 18 SCAR 12.0
CC
18
Cov
89%
remove_substrates
6.2
add_substrates
4.0
add_clusters
4.0
CRAP 3.3
« Metabolic/Substrate.pm ↑ Summary Metabolic/Dynamics/Substrate.pm »
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 5 require 5.005_62;
104 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 5 require Exporter;
108  
109 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
110  
111 use overload
112 5 ▶ "+" => \&add_clusters,
Subroutine BEGIN: called CC=1 SCAR=0.0
113 5 "\"\"" => \&cluster_to_string;
114  
115 5 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 5 our %EXPORT_TAGS = (
125 'all' => [
126 qw(
127  
128 )
129 ]
130 );
131  
132 5 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
133  
134 5 our @EXPORT = qw(
135  
136 );
137 5 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 89 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
157 89 my $proto = shift;
158 89 ▶ my $pkg = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1880
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
159  
160 89 my @substrates;
161 89 ▶ if ( ref($proto) eq 'Bio::Metabolic::Substrate::Cluster' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto eq 'Bio::Metabolic::Substrate::Cluster') { } 1 88 89
162 1 @substrates = $proto->list;
163 }
164 else {
165 88 ▶ @substrates = ref( $_[0] ) eq 'ARRAY' ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 87 88
166 }
167  
168 89 my $new_cluster = $pkg->_new_empty();
169  
170 89 $new_cluster->add_substrates(@substrates);
171  
172 89 return $new_cluster;
173 }
174  
175 # method _new_empty() for internal use only.
176 # Returns an empty object
177  
178 90 ▶sub _new_empty {
Subroutine _new_empty: called CC=2 SCAR=7.1
179 90 my $pkg = shift;
180 90 ▶ $pkg = ref($pkg) if ref($pkg);
Branch: 1 branch
BranchTrueFalseTotal
if ref $pkg 0 90 90
181  
182 90 return bless {
183 pos => {},
184 substrates => [],
185 } => $pkg;
186 }
187  
188 # accessor method _position for internal use only
189  
190 464 ▶sub _position {
Subroutine _position: called CC=2 SCAR=7.1
191 464 my $self = shift;
192 464 ▶ $self->{pos} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 464 464
193 464 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 1 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
203 1 my $cluster = shift;
204 1 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 376 ▶sub list {
Subroutine list: called CC=2 SCAR=6.9
Pod documented
215 376 my $cluster = shift;
216 376 ▶ return wantarray ? @{ $cluster->{substrates} } : $cluster->{substrates};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 224 152 376
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 0 ▶sub cluster_to_string {
Subroutine cluster_to_string: not called CC=1 SCAR=6.9
Pod documented
226 0 my $cluster = shift;
227  
228 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 265 ▶sub has {
Subroutine has: called CC=1 SCAR=0.0
Pod documented
238 265 my $cluster = shift;
239 265 my $substrate = shift;
240  
241 265 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 156 ▶sub add_substrates {
Subroutine add_substrates: called CC=4 SCAR=14.0
Pod documented
259 156 my $cluster = shift;
260 0 ▶ my @substrates = ref( $_[0] ) eq 'ARRAY' ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 156 156
261  
262 156 ▶ while ( my $substrate = shift(@substrates) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $substrate = shift @substrates) 186 156 342
263  
264 # push( @$cluster, $substrate ) unless $cluster->has($substrate);
265 186 ▶ next if $cluster->has($substrate);
Branch: 1 branch
BranchTrueFalseTotal
if $cluster->has($substrate) 37 149 186
266 149 my $listref = $cluster->list;
267 149 $cluster->_position->{ $substrate->name } = $#$listref + 1;
268 149 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 1 ▶sub remove_substrates {
Subroutine remove_substrates: called CC=6 SCAR=18.3
Pod documented
290 1 my $cluster = shift;
291 0 ▶ my @substrates = ref( $_[0] ) eq 'ARRAY' ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 1 1
292  
293 1 my @removed = ();
294 1 ▶ while ( my $substrate = shift(@substrates) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $substrate = shift @substrates) 2 1 3
295 2 ▶ next unless $cluster->has($substrate);
Branch: 1 branch
BranchTrueFalseTotal
unless $cluster->has($substrate) 0 2 2
296 2 my $rempos = $cluster->which($substrate);
297 2 my $listref = $cluster->list;
298 2 push( @removed, splice( @$listref, $rempos, 1 ) );
299 2 delete $cluster->_position->{ $substrate->name };
300 2 foreach my $v ( values %{ $cluster->_position } ) {
301 3 ▶ $v-- if $v > $rempos;
Branch: 1 branch
BranchTrueFalseTotal
if $v > $rempos 3 0 3
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 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 22 ▶sub add_clusters {
Subroutine add_clusters: called CC=4 SCAR=13.9
Pod documented
323 22 my @clusters = @_;
324  
325 # this is due to an extra value passed by the overload Module
326 22 ▶ pop(@clusters)
Branch: 1 branch
BranchTrueFalseTotal
if ref $clusters[@clusters - 1] ne ref $clusters[0] 1 21 22
327 if ( ref( $clusters[ @clusters - 1 ] ) ne ref( $clusters[0] ) );
328  
329 22 ▶ croak("add_clusters needs at least one clusters!") if @clusters == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @clusters == 0 0 22 22
330  
331 22 my $new_cluster = ref( $clusters[0] )->new;
332  
333 22 foreach my $cluster (@clusters) {
334 65 $new_cluster->add_substrates( $cluster->list );
335 }
336  
337 22 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 45 ▶sub which {
Subroutine which: called CC=1 SCAR=0.0
Pod documented
352 45 my $self = shift;
353  
354 # my @slist = shift->list;
355 45 my $substrate = shift;
356  
357 45 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 5 1;
368 __END__
« Metabolic/Substrate.pm ↑ Summary Metabolic/Dynamics/Substrate.pm »