Metabolic/Reaction.pm

filter: Statementstmt 78.6% 96 / 122 Branchbran 68.0% 34 / 50 Conditioncond 55.5% 10 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 78.9% 15 / 19 Podpod 84.6% 11 / 13 totaltotal 72.1% 168 / 233 CC 38 SCAR 26.5
CC
38
Cov
74%
reaction_to_string
56.0
new
18.4
equals
5.0
CRAP 14.2
« Metabolic/Network.pm ↑ Summary Metabolic/Dynamics/Network.pm »
1  
2 =head1 NAME
3  
4 Bio::Metabolic::Reaction - Perl extension for biochemical reactions
5  
6 =head1 SYNOPSIS
7  
8 use Bio::Metabolic::Reaction;
9  
10 my $r1 = Bio::Metabolic::Reaction->new('r1',[$sub1,$sub2],[$sub3,$sub4]);
11  
12 $r1->kinetics('linear');
13  
14  
15 =head1 DESCRIPTION
16  
17 This class implements objects representing a biochemical reaction.
18 A biochemical reaction in this context is defined by its input and its output
19 substrates, i.e. by two Bio::Metabolic::Network::Cluster objects.
20 Further, every instance of this class is associated with a mathematical expression
21 which determines the dynamical behaviour of the reaction, i.e. the reaction rate.
22  
23 =head2 EXPORT
24  
25 None
26  
27 =head2 OVERLOADED OPERATORS
28  
29 String Conversion
30 $string = "$reaction";
31 print "\$reaction = '$reaction'\n";
32  
33 Equality
34 if ($reaction1 == $reaction2)
35 if ($reaction1 != $reaction2)
36  
37  
38 =head1 AUTHOR
39  
40 Oliver Ebenhöh, oliver.ebenhoeh@rz.hu-berlin.de
41  
42 =head1 SEE ALSO
43  
44 Bio::Metabolic Bio::Metabolic::Substrate Bio::Metabolic::Substrate::Cluster.
45  
46 =cut
47  
48 package Bio::Metabolic::Reaction;
49  
50 5 require 5.005_62;
51 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 5 require Exporter;
55  
56 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 5 ▶use Bio::Metabolic::Substrate::Cluster;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 #use Math::Symbolic;
61  
62 use overload
63  
64 # "\"\"" => \&reaction_to_string,
65 5 ▶ "\"\"" => \&to_compact_string,
Subroutine BEGIN: called CC=1 SCAR=0.2
66 "==" => \&equals,
67 0 ▶ "!=" => sub { return 1 -equals(@_) };
Subroutine __ANON__: not called CC=1 SCAR=0.2
68  
69 5 our @ISA = qw(Exporter);
70  
71 # Items to export into callers namespace by default. Note: do not export
72 # names by default without a very good reason. Use EXPORT_OK instead.
73 # Do not simply export all your public functions/methods/constants.
74  
75 # This allows declaration use Bio::Metabolic::Reaction ':all';
76 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
77 # will save memory.
78 5 our %EXPORT_TAGS = (
79 'all' => [
80 qw(
81  
82 )
83 ]
84 );
85  
86 5 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
87  
88 5 our @EXPORT = qw(
89  
90 );
91  
92 5 our %OutputFormat = (
93 -1 => "%20s",
94 1 => "%-20s"
95 );
96  
97 5 our $OutputArrow = " => ";
98  
99 5 our $VERSION = '0.06';
100  
101 #our $DEBUG = 1;
102  
103 =head1 METHODS
104  
105 =head2 Constructor new
106  
107 There are three ways to call the constructor method:
108  
109 1.) $r = Bio::Metabolic::Reaction->new($name, $inlist, $outlist);
110  
111 Here, the first argument must hold the name. The following two arguments
112 provide arrayrefs of Bio::Metabolic::Substrate objects, defining the input and
113 output substrates of the biochemical reaction.
114 If a reactant occurs twice (e.g. X + X -> Y), it has to appear twice in the
115 corresponding list.
116  
117 2.) $r = Bio::Metabolic::Reaction->new($name, $substrates, $coefficients)
118  
119 The first argument must specify the name. The second argument can be an
120 arrayref of Bio::Metabolic::Substrate objects or a Bio::Metabolic::Substrate::Cluster
121 object, defining all participating reactants.
122 The third argument must hold numbers specifying the multiplicity of the
123 reactants in the same order as the reactants have been specified.
124  
125 Example: Let $X and $Y be Bio::Metabolic::Substrate objects defining the
126 reactants X and Y. The reaction X + X -> Y can be defined either by
127  
128 $r1 = Bio::Metabolic::Reaction->new('r1', [$X,$X], [$Y]);
129  
130 or by
131  
132 $r2 = Bio::Metabolic::Reaction->new('r2', [$X,$Y], [-2,1]);
133  
134 3.) The constructor can be called as an object method, using another reaction as
135 template: $r2 = $r1->new().
136  
137 =cut
138  
139 13 ▶sub new { # defines a new reaction.
Subroutine new: called CC=17 SCAR=29.1
Pod documented
140 13 my $proto = shift;
141 13 ▶ my $pkg = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1120
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
142  
143 13 ▶ my %attr = ref($proto) eq 'Bio::Metabolic::Reaction' ? %$proto : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $proto eq 'Bio::Metabolic::Reaction' ? : 1 12 13
144  
145 13 my $name = shift;
146  
147 13 ▶ $attr{name} = $name if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 12 1 13
148 croak("name must be specified in constructor new()")
149 13 ▶ unless defined $attr{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr{"name"} 0 13 13
150  
151 # my ($insubs,$outsubs) = @_;
152 13 my $insubs = shift;
153 13 my $outsubs = shift;
154 13 ▶ if ( defined $outsubs
Branch: 2 branches
BranchTrueFalseTotal
if (defined $outsubs and ref $outsubs eq 'ARRAY' and join('', @$outsubs) =~ /^[0-9\-\.]*$/) { } 2 11 13
elsif (defined $insubs) { } 10 1 11
Condition83%5/6
defined $outsubs and ref $outsubs eq "ARRAY" and join("", @$outsubs) =~ /^[0-9\-\.]*$/
!ll&&!rl&&r
1102
defined $outsubs and ref $outsubs eq "ARRAY"
!ll&&!rl&&r
1012
MC/DC66%2/3
defined $outsubs and ref $outsubs eq "ARRAY" and join("", @$outsubs) =~ /^[0-9\-\.]*$/
defined $outsubs ref $outsubs eq "ARRAY" join("", @$outsubs) =~ /^[0-9\-\.]*$/
Truth table75%3/4
A and B and C
A= defined $outsubsB= ref $outsubs eq "ARRAY"C= join("", @$outsubs) =~ /^[0-9\-\.]*$/
ABCresult
0XX0
10X0
1100
1111
155 && ref($outsubs) eq 'ARRAY'
156 && join( '', @$outsubs ) =~ /^[0-9\-\.]*$/ )
157 {
158  
159 # list with stoichiometric coefficients
160 $attr{substrates} =
161 2 ▶ ref($insubs) eq 'Bio::Metabolic::Substrate::Cluster'
Branch: 1 branch
BranchTrueFalseTotal
ref $insubs eq 'Bio::Metabolic::Substrate::Cluster' ? : 0 2 2
162 ? $insubs
163 : Bio::Metabolic::Substrate::Cluster->new(@$insubs);
164 2 my @slist = $attr{substrates}->list;
165  
166 2 ▶ croak(
Branch: 1 branch
BranchTrueFalseTotal
unless @slist == @$outsubs 0 2 2
167 "numbers of substrates and stoichiometric coefficients don't agree in constructor new()"
168 )
169 unless @slist == @$outsubs;
170  
171 2 $attr{stoichiometry} = { map( ( $_->name, 0 ), @slist ) };
172 2 ▶ for ( my $i = 0 ; $i < @slist ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @slist) 6 2 8
173 6 $attr{stoichiometry}->{ $slist[$i]->name } = $outsubs->[$i];
174 }
175 }
176 elsif ( defined $insubs ) {
177 $attr{substrates} =
178 10 Bio::Metabolic::Substrate::Cluster->new( @$insubs, @$outsubs );
179  
180 10 my %tmphash = ( -1 => $insubs, 1 => $outsubs );
181 10 ▶ while ( my ( $dir, $list ) = each %tmphash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %tmphash) 20 10 30
182 20 foreach my $subs (@$list) {
183 23 $attr{stoichiometry}->{ $subs->name } += $dir;
184 }
185 }
186 }
187  
188 # $attr{-1} = Bio::Metabolic::Substrate::Cluster->new(@$insubs) if @$insubs;
189 # croak ("in-metabolites have to be specified in constructor new()") unless defined $attr->{-1};
190 # $attr{1} = Bio::Metabolic::Substrate::Cluster->new(@$insubs) if @$outsubs;
191 # croak ("out-metabolites have to be specified in constructor new()") unless defined $attr->{1};
192  
193 croak(
194 "participating compounds have not been specified in constructor new()")
195 13 ▶ unless defined $attr{substrates} && defined $attr{stoichiometry};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr{"substrates"} and defined $attr{"stoichiometry"} 0 13 13
Condition33%1/3
defined $attr{"substrates"} and defined $attr{"stoichiometry"}
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $attr{"substrates"} and defined $attr{"stoichiometry"}
defined $attr{"substrates"} defined $attr{"stoichiometry"}
Truth table33%1/3
A and B
A= defined $attr{"substrates"}B= defined $attr{"stoichiometry"}
ABresult
0X0
100
111
196  
197 0 ▶ my %extra_attr = @_ ? %{ shift() } : ();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 13 13
198  
199 13 my $new_reaction = bless {
200 %attr,
201 'parameters' => {},
202 'rate' => undef,
203 %extra_attr,
204 }, $pkg;
205  
206 13 return $new_reaction;
207 }
208  
209 =head2 Method copy
210  
211 copy() is exactly the same as $r2 = $r1->new();
212  
213 =cut
214  
215 0 ▶sub copy {
Subroutine copy: not called CC=1 SCAR=6.9
Pod documented
216 0 return shift->new();
217 }
218  
219 =head2 Method name
220  
221 Optional argument: sets the object's name. Returns the object's name.
222  
223 =cut
224  
225 7 ▶sub name {
Subroutine name: called CC=2 SCAR=7.1
Pod documented
226 7 my $self = shift;
227 7 ▶ $self->{name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 7 7
228 7 return $self->{name};
229 }
230  
231 =head2 Method substrates
232  
233 Returns all participating compounds as a Bio::Metabolic::Substrate::Cluster object.
234 Optional argument: sets the substrates.
235  
236 =cut
237  
238 150 ▶sub substrates {
Subroutine substrates: called CC=2 SCAR=7.1
Pod documented
239 150 my $self = shift;
240 150 ▶ $self->{substrates} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 150 150
241 150 return $self->{substrates};
242 }
243  
244 =head2 Method stoichiometry
245  
246 Returns a hashref. Keys are the substrate names, values the stoichiometric
247 coefficients (negative for input substrates, positive for output substrates).
248 It the absolute value differs from one, the corresponding substrate is
249 consumed/produced more than once.
250 Optional argument: sets the stoichiometries.
251  
252 =cut
253  
254 238 ▶sub stoichiometry {
Subroutine stoichiometry: called CC=2 SCAR=7.1
Pod documented
255 238 my $self = shift;
256 238 ▶ $self->{stoichiometry} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 238 238
257 238 return $self->{stoichiometry};
258 }
259  
260 =head2 Method st_coefficient
261  
262 Argument must be a substrate. Returns the stoichiometric coefficient
263 (cf. method stoichiometry()) of the specified substrate.
264  
265 =cut
266  
267 208 ▶sub st_coefficient {
Subroutine st_coefficient: called CC=1 SCAR=0.0
Pod documented
268 208 my $self = shift;
269 208 my $substrate = shift;
270 208 return $self->stoichiometry->{ $substrate->name };
271 }
272  
273 =head2 Method in
274  
275 Returns the input substrates as a Bio::Metabolic::Substrate::Cluster object.
276  
277 =cut
278  
279 15 ▶sub in {
Subroutine in: called CC=2 SCAR=6.9
Pod documented
280 15 my $self = shift;
281 15 my @slist = $self->substrates->list;
282 15 ▶ my @inlist = map ( $self->st_coefficient($_) < 0 ? ($_) : (), @slist );
Branch: 1 branch
BranchTrueFalseTotal
$self->st_coefficient($_) < 0 ? : 15 22 37
283  
284 15 return Bio::Metabolic::Substrate::Cluster->new(@inlist);
285  
286 # return shift->{-1};
287 }
288  
289 =head2 Method out
290  
291 Returns the output substrates as a Bio::Metabolic::Substrate::Cluster object.
292  
293 =cut
294  
295 14 ▶sub out {
Subroutine out: called CC=2 SCAR=6.9
Pod documented
296 14 my $self = shift;
297 14 my @slist = $self->substrates->list;
298 14 ▶ my @outlist = map ( $self->st_coefficient($_) > 0 ? ($_) : (), @slist );
Branch: 1 branch
BranchTrueFalseTotal
$self->st_coefficient($_) > 0 ? : 21 14 35
299  
300 14 return Bio::Metabolic::Substrate::Cluster->new(@outlist);
301  
302 # return shift->{1};
303 }
304  
305 =head2 Method dir
306  
307 Take one parameter which must be -1 or 1. Returns the input or output
308 substrates, respectively.
309  
310 =cut
311  
312 22 ▶sub dir {
Subroutine dir: called CC=4 SCAR=14.1
Pod documented
313 22 my $reaction = shift;
314 22 my $dir = shift;
315  
316 22 ▶ croak("Direction must be either -1 our 1") unless $dir == -1 || $dir == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir == -1 or $dir == 1 0 22 22
Condition66%2/3
$dir == -1 or $dir == 1
l!l&&r!l&&!r
11110
MC/DC0%0/2
$dir == -1 or $dir == 1
$dir == -1 $dir == 1
Truth table66%2/3
A or B
A= $dir == -1B= $dir == 1
ABresult
1X1
011
000
317  
318 22 ▶ return $dir == -1 ? $reaction->in : $reaction->out;
Branch: 1 branch
BranchTrueFalseTotal
$dir == -1 ? : 11 11 22
319 }
320  
321 =head2 Method get_substrate_list
322  
323 Returns input and output substrates as one list.
324  
325 =cut
326  
327 0 ▶sub get_substrate_list {
Subroutine get_substrate_list: not called CC=1 SCAR=6.9
Pod documented
328 0 my $reaction = shift;
329  
330 0 return $reaction->substrates->list;
331  
332 # return (@{$reaction->{-1}},@{$reaction->{1}});
333 }
334  
335 #sub substrates {
336 # return Bio::Metabolic::Substrate::Cluster->new(shift->get_substrate_list);
337 #}
338  
339 0 ▶sub reaction_to_string {
Subroutine reaction_to_string: not called CC=7 SCAR=40.3
Pod undocumented
340 0 my $reaction = shift;
341  
342 # print "reaction_to_string called.\n";
343  
344 0 my $retstr;
345 0 my $subsdir = {
346 -1 => [],
347 1 => []
348 };
349 0 foreach my $dir ( -1, 1 ) {
350 0 foreach my $substrate ( $reaction->dir($dir)->list ) {
351 push(
352 0 @{ $subsdir->{$dir} },
353 0 sprintf( $OutputFormat{$dir}, "$substrate" )
354 );
355 }
356 }
357 0 my $dir;
358 0 my $bstr = $OutputArrow;
359 0 ▶ while ( @{ $subsdir->{-1} } || @{ $subsdir->{1} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$subsdir->{-1};} or @{$subsdir->{1};}) 0 0 0
Condition0%0/3
@{$subsdir->{-1};} or @{$subsdir->{1};}
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$subsdir->{-1};} or @{$subsdir->{1};}
@{$subsdir->{-1};} @{$subsdir->{1};}
Truth table0%0/3
A or B
A= @{$subsdir->{-1};}B= @{$subsdir->{1};}
ABresult
1X1
011
000
360 my $left =
361 0 @{ $subsdir->{-1} }
362 0 ? shift( @{ $subsdir->{-1} } )
363 0 ▶ : sprintf( $OutputFormat{-1}, "" );
Branch: 1 branch
BranchTrueFalseTotal
@{$subsdir->{'-1'};} ? : 0 0 0
364 my $right =
365 0 @{ $subsdir->{1} }
366 0 ? shift( @{ $subsdir->{1} } )
367 0 ▶ : sprintf( $OutputFormat{1}, "" );
Branch: 1 branch
BranchTrueFalseTotal
@{$subsdir->{'1'};} ? : 0 0 0
368 0 $retstr .= $left . $bstr . $right . "\n";
369 0 $bstr =~ s/./ /g;
370  
371 # print $retstr;
372 }
373 0 return $retstr;
374 }
375  
376 6 ▶sub to_compact_string {
Subroutine to_compact_string: called CC=4 SCAR=13.9
Pod undocumented
377 6 my $reaction = shift;
378  
379 6 my $retstr;
380 6 my $subsdir = {
381 -1 => [],
382 1 => []
383 };
384 6 foreach my $dir ( -1, 1 ) {
385 12 foreach my $substrate ( $reaction->dir($dir)->list ) {
386 15 ▶ for (
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= abs $reaction->st_coefficient($substrate)) 17 15 32
387 my $i = 1 ;
388 $i <= abs( $reaction->st_coefficient($substrate) ) ;
389 $i++
390 )
391 {
392 17 push( @{ $subsdir->{$dir} }, "$substrate" );
393 }
394 }
395 }
396  
397 $retstr =
398 6 join( "+", @{ $subsdir->{-1} } ) . "->"
399 6 . join( "+", @{ $subsdir->{1} } ) . "\n";
400  
401 # if ( defined( $reaction->rate ) ) {
402 # $retstr .= "rate: " . $reaction->rate . "\n";
403 # foreach my $param ( keys( %{ $reaction->parameters } ) ) {
404 # if ( ref( $reaction->parameter($param) )
405 # && ref( $reaction->parameter($param) ) eq
406 # 'Math::Symbolic::Variable'
407 # && defined $reaction->parameter($param)->value )
408 # {
409 # $retstr .= "\t"
410 # . $reaction->parameter($param) . "="
411 # . $reaction->parameter($param)->value . "\n";
412 # }
413 # }
414 # }
415  
416 6 return $retstr;
417 }
418  
419 =head2 Method equals()
420  
421 compares two Bio::Metabolic::Reaction objects.
422 Returns 1 if all substrates occur with the same stoichiometric coefficient,
423 0 otherwise.
424  
425 =cut
426  
427 33 ▶sub equals {
Subroutine equals: called CC=5 SCAR=16.1
Pod documented
428 33 my ( $r1, $r2 ) = @_;
429  
430 33 my $sl1 = $r1->substrates;
431 33 my $sl2 = $r2->substrates;
432  
433 33 my @sl1 = $sl1->list;
434 33 my @sl2 = $sl2->list;
435  
436 33 ▶ return 0 unless @sl1 == @sl2;
Branch: 1 branch
BranchTrueFalseTotal
unless @sl1 == @sl2 15 18 33
437  
438 18 foreach my $sub (@sl1) {
439 44 ▶ return 0 unless $sl2->has($sub);
Branch: 1 branch
BranchTrueFalseTotal
unless $sl2->has($sub) 4 40 44
440 40 ▶ return 0 unless $r1->st_coefficient($sub) == $r2->st_coefficient($sub);
Branch: 1 branch
BranchTrueFalseTotal
unless $r1->st_coefficient($sub) == $r2->st_coefficient($sub) 0 40 40
441 }
442  
443 14 return 1;
444 }
445  
446 5 1;
447 __END__
« Metabolic/Network.pm ↑ Summary Metabolic/Dynamics/Network.pm »