Metabolic/Dynamics/Network.pm

filter: Statementstmt 91.0% 71 / 78 Branchbran 68.7% 22 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 3 totaltotal 83.3% 100 / 120 CC 22 SCAR 23.7
CC
22
Cov
85%
mfile
16.7
ODEs
6.3
time_derivative
2.0
CRAP 10.7
« Metabolic/Reaction.pm ↑ Summary Metabolic/Substrate.pm »
1 package Bio::Metabolic::Dynamics::Network;
2  
3 1 require 5.005_62;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Math::Symbolic::VectorCalculus;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10  
11 1 our $VERSION = '0.06';
12  
13 =begin comment
14  
15 _time_derivative_by_substrate_number returns the temporal change of a substrate
16 (specified by an index). This change is determined by teh reaction rates.
17  
18 =end comment
19  
20 =cut
21  
22 8 ▶sub Bio::Metabolic::Network::_time_derivative_by_substrate_number {
Subroutine _time_derivative_by_substrate_number: called CC=2 SCAR=6.9
23 8 my $network = shift;
24 8 my $s = shift;
25  
26 8 my $matrix = $network->matrix;
27 8 my $reactions = $network->reactions;
28  
29 8 my $sterm = Math::Symbolic::Constant->zero();
30  
31 8 my @rcols = PDL::which( $matrix->slice("($s),:") != 0 )->list;
32 8 foreach my $r (@rcols) {
33 13 my $rterm = Math::Symbolic::Operator->new(
34 '*',
35 Math::Symbolic::Constant->new( $matrix->at( $s, $r ) ),
36 $reactions->[$r]->rate
37 );
38  
39 # my $rterm = Symbolic::Function::Operator::Mult->new([Symbolic::Function::Constant->new($matrix->at($s,$r)),
40 # $reactions->[$r]->rate])->simplify;
41 13 $sterm =
42 Math::Symbolic::Operator->new( '+', $rterm->simplify, $sterm )
43 ->simplify;
44  
45 # $sterm = Symbolic::Function::Operator::Add->new([$rterm,$sterm])->simplify;
46 }
47  
48 8 return $sterm;
49 }
50  
51 =head2 Method time_derivative
52  
53 This method returns a Math::Symbolic tree representing a function which determines the temporal
54 change of the concentration of the substrate passed as the argument.
55 This function is determined by the reaction rates.
56  
57 =cut
58  
59 2 ▶sub Bio::Metabolic::Network::time_derivative {
Subroutine time_derivative: called CC=2 SCAR=7.0
Pod undocumented
60 2 my $network = shift;
61 2 my $substrate = shift;
62  
63 2 my $s = $network->substrates->which($substrate);
64  
65 return
66 2 ▶ defined $s ? $network->_time_derivative_by_substrate_number($s) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $s ? : 2 0 2
67 }
68  
69 =head2 Method ODEs
70  
71 This method returns an array of Math::Symbolic trees. One for each substrate.
72 The optional argument can be a Bio::Metabolic::Substrate::Cluster, an arrayref or an array
73 of Bio::Metabolic::Substrate objects.
74 If no argument is specified, it defaults to what the method substrates() returns.
75  
76 =cut
77  
78 2 ▶sub Bio::Metabolic::Network::ODEs {
Subroutine ODEs: called CC=6 SCAR=18.3
Pod undocumented
79 2 my $network = shift;
80  
81 2 my @sindices = ();
82 2 my @substrates = ();
83 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 1 2
84 1 ▶ if ( ref( $_[0] ) eq 'Bio::Metabolic::Substrate::Cluster' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'Bio::Metabolic::Substrate::Cluster') { } 0 1 1
elsif (ref $_[0] eq 'ARRAY') { } 0 1 1
85 0 @substrates = shift->list;
86 }
87 elsif ( ref( $_[0] ) eq 'ARRAY' ) {
88 0 @substrates = @{ shift() };
89 }
90 else {
91 1 @substrates = @_;
92 }
93 }
94 else {
95 1 @substrates = $network->substrates->list;
96 }
97  
98 # if (@_) {
99 # my @substrates = shift->list;
100 2 ▶ while ( my $sub = shift(@substrates) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = shift @substrates) 6 2 8
101 6 push( @sindices, $network->substrates->which($sub) );
102 }
103  
104 2 my @time_derivatives = ();
105  
106 2 ▶ for ( my $s = 0 ; $s < @sindices ; $s++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s < @sindices) 6 2 8
107 6 push( @time_derivatives,
108 $network->_time_derivative_by_substrate_number( $sindices[$s] ) );
109  
110 # push (@time_derivatives,$sterm);
111 }
112  
113 2 return @time_derivatives;
114 }
115  
116 =head2 Method mfile
117  
118 Dumps a strong which can be used as an mfile for Matlab.
119 The optional argument can be a Bio::Metabolic::Substrate::Cluster, an arrayref or an array
120 of Bio::Metabolic::Substrate objects.
121 If no argument is specified, it defaults to what the method substrates() returns.
122  
123 The corresponding substrate concentrations are considered to be integration variables.
124  
125 All parameters within the participating reactions must have a value.
126 The method croaks if one parameter value is not defined.
127  
128 =cut
129  
130 1 ▶sub Bio::Metabolic::Network::mfile {
Subroutine mfile: called CC=15 SCAR=28.2
Pod undocumented
131 1 my $network = shift;
132  
133 # my $substrates = shift;
134  
135 1 my @substrates = ();
136 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 0 1
137 1 ▶ if ( ref( $_[0] ) eq 'Bio::Metabolic::Substrate::Cluster' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'Bio::Metabolic::Substrate::Cluster') { } 0 1 1
elsif (ref $_[0] eq 'ARRAY') { } 0 1 1
138 0 @substrates = shift->list;
139 }
140 elsif ( ref( $_[0] ) eq 'ARRAY' ) {
141 0 @substrates = @{ shift() };
142 }
143 else {
144 1 @substrates = @_;
145 }
146 }
147 else {
148 0 @substrates = $network->substrates->list;
149 }
150  
151 # my @substrates = ref($_[0]) ? ref($_[0]) eq 'Bio::Metabolic::Substrate::Cluster' ? $_[0]->list : @{$_[0]} : @_;
152  
153 1 my @odes = $network->ODEs(@substrates);
154  
155 # my @odes = &$odesub($network, @substrates);
156  
157 # my $varlist = Math::Symbolic::VectorCalculus::_combined_signature(@odes);
158  
159 my %varchecklist = map ( ( $_, 1 ),
160 1 @{ Math::Symbolic::VectorCalculus::_combined_signature(@odes) } );
161  
162 1 my %parameters = (); # collect all parameter values;
163 1 foreach my $reaction ( @{ $network->reactions } ) {
164 1 foreach my $param ( keys( %{ $reaction->parameters } ) ) {
165 $parameters{ $reaction->parameter($param)->{name} } =
166 2 $reaction->parameter($param)->value();
167 }
168 }
169  
170 1 my $mfile = "function f=func(t,y)\n";
171  
172 1 my @varnames = map ( $_->name, @substrates );
173 1 ▶ for ( my $vnr = 0 ; $vnr < @varnames ; $vnr++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vnr < @varnames) 2 1 3
174 2 $mfile .= $varnames[$vnr] . "=y(" . eval( $vnr + 1 ) . ");\n";
175 delete( $varchecklist{ $varnames[$vnr] } )
176 2 ▶ if defined( $varchecklist{ $varnames[$vnr] } );
Branch: 1 branch
BranchTrueFalseTotal
if defined $varchecklist{$varnames[$vnr]} 2 0 2
177 }
178  
179 1 foreach my $param ( keys(%varchecklist) ) {
180 croak("undefined parameter $param in method mfile")
181 2 ▶ unless defined $parameters{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parameters{$param} 0 2 2
182 2 $mfile .= $param . "=" . $parameters{$param} . "\n";
183  
184 2 foreach my $ode (@odes) {
185 4 $ode->implement( $param => $parameters{$param} );
186 }
187  
188 2 ▶ delete( $varchecklist{$param} ) if defined( $varchecklist{$param} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $varchecklist{$param} 2 0 2
189 }
190  
191 1 ▶ for ( my $snr = 0 ; $snr < @odes ; $snr++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($snr < @odes) 2 1 3
192 2 my ( $codeline, $leftovers ) =
193 Math::Symbolic::Compiler->compile_to_code( $odes[$snr], \@varnames );
194 2 ▶ croak("cannot handle leftover trees in method mfile") if @$leftovers;
Branch: 1 branch
BranchTrueFalseTotal
if @$leftovers 0 2 2
195  
196 2 ▶ for ( my $vnr = 0 ; $vnr < @varnames ; $vnr++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vnr < @varnames) 4 2 6
197 4 $codeline =~ s/\$_\[$vnr\]/$varnames[$vnr]/g;
198 }
199  
200 2 $mfile .= "f(" . eval( $snr + 1 ) . ",1)=" . $codeline . ";\n";
201 }
202  
203 1 return $mfile;
204 }
205  
« Metabolic/Reaction.pm ↑ Summary Metabolic/Substrate.pm »