-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteadyState.m
2372 lines (1843 loc) · 94.1 KB
/
steadyState.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% %
% steadyState %
% %
% %
% OUTPUT: Returns the steady state solution of a chemical reaction network %
% (CRN) parametrized by rate constants (and/or sigma's). The free %
% parameters and conservation laws are also listed after the solution. %
% If there are subnetworks that could not be solved because translation %
% is taking too long, solving the subnetwork is skipped and a message %
% saying it could not be solved is displayed. In this case, the %
% parametrization of the steady state of the entire network is not %
% completed but the lists of solved and unsolved subnetworks are %
% displayed. In the case where all subnetworks are solved but the %
% solution of the entire network cannot be parametrized in terms of the %
% free parameters (due to two or more species dependent on each other), %
% the solution returned is in terms of both free parameters and other %
% "nonfree" species. If there are subnetworks containing only 1 %
% reaction, it means that there are species with 0 steady steady; hence, %
% the network has not positive steady state and a message appears saying %
% so. The output variables 'equation', 'species', 'free_parameter', %
% 'conservation_law', and 'model' allow the user to view the following, %
% respectively: %
% - List of parametrization of the steady state of the system %
% - List of steady state species of the network %
% - List of free parameters of the steady state %
% - List of conservation laws of the system %
% - Complete network with all the species listed in the 'species' %
% field of the structure 'model' %
% %
% INPUT: model: a structure, representing the CRN (see README.txt for %
% details on how to fill out the structure) %
% %
% Notes: %
% 1. It is assumed that the CRN has mass action kinetics. %
% 2. The algorithm first decomposes the network intro its finest %
% independent decomposition. Then the steady state of the species %
% in each subnetwork is solved. Finally, these solutions are %
% combined to get the solution to the entire network. %
% 3. Decomposition into the finest independent decomposition comes from %
% [2]. %
% 4. Translation of network comes from [3]. %
% 5. Parametrization of steady state solution comes from [4]. %
% 6. Computation of directed spanning trees toward vertices come from %
% [1]. %
% 7. Sometimes, when the solution could not be expressed in terms of %
% free parameters only, renaming the variables can solve the %
% problem. Some subnetworks may be solved for different species %
% depending on the variable assigned to them since the selection %
% of species to solve is based on alphabetical order. %
% 8. Ideas for some parts of the code was motivated by [5]. %
% %
% References %
% [1] Gabow H and Meyers E (1978) Finding all spanning trees of directed %
% and undirected graphs. SIAM J Comput 7(3):280-287. %
% https://doi.org/10.1137/0207024 %
% [2] Hernandez B, De la Cruz R (2021) Independent decompositions of %
% chemical reaction networks. Bull Math Biol 83(76):1–23. %
% https://doi.org/10.1007/s11538-021-00906-3 %
% [3] Hong H, Hernandez B, Kim J, Kim JK (2022) Computational %
% translation framework identifies biochemical reaction networks %
% with special topologies and their long-term dynamics %
% (submitted) %
% [4] Johnston M, Mueller S, Pantea C (2019) A deficiency-based approach %
% to parametrizing positive equilibria of biochemical reaction %
% systems. Bull Math Biol 81:1143–1172. %
% https://doi.org/10.1007/s11538-018-00562-0 %
% [5] Soranzo N, Altafini C (2009) ERNEST: a toolbox for chemical %
% reaction network theory. Bioinform 25(21):2853–2854. %
% https://doi.org/10.1093/bioinformatics/btp513 %
% %
% Created: 15 July 2022 %
% Last Modified: 28 September 2022 %
% %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
function [equation, species, free_parameter, conservation_law, model] = steadyState(model)
%
% Step 1: Decompose the network into its finest independent decomposition
%
[model, N, R, G, P] = indepDecomp(model);
if numel(P) == 1
fprintf('The network has no nontrival independent decomposition.\n\n')
else
fprintf('The network has %d subnetworks.\n\n', numel(P))
end
% Create a vector of reaction numbers for the total number of reactions
reac_num = [ ];
for i = 1:numel(model.reaction)
if model.reaction(i).reversible == 0
reac_num(end+1) = i;
else
reac_num(end+1) = i;
reac_num(end+1) = i;
end
end
% Get the number of reactions in each subnetwork
subnetwork_reaction_count = cellfun(@numel, P);
% Look for subnetworks with just 1 reaction
single_reaction = find(subnetwork_reaction_count == 1);
% Exit the algorithm if there is a subnetwork with just 1 reaction
if ~isempty(single_reaction)
if numel(single_reaction) == 1
equation = { };
species = [ ];
free_parameter = [ ];
conservation_law = { };
fprintf('Subnetwork %s has 1 reaction only.\n', single_reaction)
fprintf('The network has no positive steady state.\n\n')
else
equation = { };
species = [ ];
free_parameter = [ ];
conservation_law = { };
print_list = sprintf('%d, ', single_reaction);
print_list(end-1:end) = [ ]; % To clean the trailing comma and space at the end of the list
fprintf('Subnetworks %s have 1 reaction only.\n', print_list)
fprintf('The network has no positive steady state.\n\n')
end
return
end
%
% Step 2: Get the conservation laws of the system of ODEs
%
% Get the conservation laws: the kernel of the transpose of the stoichiometric subspace
conservation_law_matrix = null(N', 'r');
% Initialize list of conservation laws
conservation_law = { };
% Get the conservation laws
for i = 1:size(conservation_law_matrix, 2)
% Get the column
conservation_law_ = conservation_law_matrix(:, i);
% Locate the index of nonzero entries of each column
conservation_law_nnz = find(conservation_law_);
% Get the conservation law
for j = 1:length(conservation_law_nnz)
% First entry
if j == 1
% Do not show coefficient if it is 1
if conservation_law_(conservation_law_nnz(j)) == 1
law = ['d' model.species{conservation_law_nnz(j)} '/dt'];
% Just show a negative sign if it is -1
elseif conservation_law_(conservation_law_nnz(j)) == -1
law = ['-d' model.species{conservation_law_nnz(j)} '/dt'];
% All other cases
else
law = [num2str(conservation_law_(conservation_law_nnz(j))) 'd' model.species{conservation_law_nnz(j)} '/dt'];
end
% Succeeding entries
else
% Do not show coefficient if it is 1
if conservation_law_(conservation_law_nnz(j)) == 1
law = [law ' + d' model.species{conservation_law_nnz(j)} '/dt'];
% For negative coefficients
elseif conservation_law_(conservation_law_nnz(j)) < 0
% Just show a negative sign if it is -1
if conservation_law_(conservation_law_nnz(j)) == -1
law = [law ' - d' model.species{conservation_law_nnz(j)} '/dt'];
% All other negative coefficients
else
law = [law ' - ' num2str(abs(conservation_law_(conservation_law_nnz(j)))) 'd' model.species{conservation_law_nnz(j)} '/dt'];
end
% All other cases
else
law = [law ' + ' num2str(conservation_law_(conservation_law_nnz(j))) 'd' model.species{conservation_law_nnz(j)} '/dt'];
end
end
end
% Add the law to the list
conservation_law{end+1} = [law ' = 0'];
end
%
% Step 3: Solve for the steady state of each subnetwork
%
% Initialize list of constants used in parametrization
B_ = [ ];
sigma_ = [ ];
% Initialize list of equations of the steady state solution
equation = { }; % List of all steady state solutions
equation_for_solving = { }; % Same list but formatted for using the 'solve' function
% Initialize list of steady state species
species = [ ];
% Initialize list of unsolved subnetworks
unsolved_subnetwork = [ ];
% Initialize checker if the entire network needs to be solved
% Entire network will be solved only if all subnetworks are solved
do_not_solve_entire_network = 0;
% Go through each subnetwork
for i = 1:numel(P)
% Get the corresponding reactions of the subnetwork from the parent network
model_P(i).id = [model.id ' - Subnetwork ' num2str(i)];
model_P(i).species = { };
reac_P = unique(reac_num(P{i}));
for j = 1:numel(reac_P)
model_P(i).reaction(j) = model.reaction(reac_P(j));
end
% Create a vector of reaction numbers for the total number of reactions in the subnetwork
reac_num_ = [ ];
for j = 1:numel(model_P(i).reaction)
if model_P(i).reaction(j).reversible == 0
reac_num_(end+1) = j;
else
reac_num_(end+1) = j;
reac_num_(end+1) = j;
end
end
% Get the unique reaction numbers
reac_num_unique = unique(reac_num_);
% Initialize list of reactions of the subnetwork
reac_subnetwork = { };
% Create list of reactions of the subnetwork
for j = 1:numel(reac_num_unique)
% If the reaction is reversible
if model_P(i).reaction(reac_num_unique(j)).reversible == 1
% Split the reactant and product complexes
split_complex = split(model_P(i).reaction(reac_num_unique(j)).id, '<->');
% Add to the list the two reactions separately
reac_subnetwork{end+1} = [split_complex{1}, '->', split_complex{2}];
reac_subnetwork{end+1} = [split_complex{2}, '->', split_complex{1}];
else
reac_subnetwork{end+1} = model_P(i).reaction(reac_num_unique(j)).id;
end
end
% Get the reaction numbers from the independent decomposition
P_ = P{i};
if numel(P) == 1
fprintf('- Network -\n\n')
else
fprintf('- Subnetwork %d -\n\n', i)
end
% Show reactions of the subnetwork
for j = 1:numel(P_)
fprintf('R%d: %s\n', P_(j), reac_subnetwork{j})
end
if numel(P) == 1
fprintf('\nSolving the network...\n\n')
else
fprintf('\nSolving Subnetwork %d...\n\n', i)
end
% Solve for the the steady state of the subnetwork
[param, B_, sigma_, k, sigma, tau, model_P(i), skip] = analyticSolution(model_P(i), P_, B_, sigma_);
% If finding a translation takes too long for the subnetwork
if skip == 1
% Case 1: No nontrival independent decomposition
if numel(P) == 1
fprintf('Could not solve the network.\n\n')
% List the subnetwork as unsolved
unsolved_subnetwork(end+1) = i;
% No free parameters found (since we could not solve the network)
free_parameter = [ ];
% Let the checker know that we don't need to solve the entire network
do_not_solve_entire_network = 1;
% Exit the loop
break
% Case 2: There are other subnetworks
else
fprintf('Could not solve Subnetwork %d.\n\n', i)
% List the subnetwork as unsolved
unsolved_subnetwork(end+1) = i;
% Let the checker know that we don't need to solve the entire network
do_not_solve_entire_network = 1;
% Go to the next iteration already
continue
end
end
fprintf('\n')
% Substitute the tau's for the free parameters of the subnetwork
for j = 1:length(param)
if ismember(param(j), tau)
param = subs(param, param(j), model_P(i).species{j});
end
end
% Substitute the sigma's for the free parameters of the subnetwork
for j = 1:length(model_P(i).species)
% Get the numerator and denominator of each solution
[N, D] = numden(param(j));
% Check if it is of the form sigma/k or k/sigma
if ismember(N, sigma) | ismember(D, sigma)
% Solve for sigma in terms of k, then substitute
if ismember(N, sigma)
sigma_solved = solve(param(j) == model_P(i).species{j}, sigma(find(ismember(sigma, N))));
param = subs(param, N, sigma_solved);
elseif ismember(D, sigma)
sigma_solved = solve(param(j) == model_P(i).species{j}, sigma(find(ismember(sigma, D))));
param = subs(param, D, sigma_solved);
end
end
end
% Go through each species of the subnetwork
for j = 1:length(model_P(i).species)
% Get only the steady state species
if model_P(i).species{j} ~= string(param(j))
equation{end+1} = param(j);
equation_for_solving{end+1} = model_P(i).species{j} == param(j);
% Collect all steady state species of the subnetwork
species = [species, cell2sym(model_P(i).species(j))];
end
end
end
%
% Step 4: Use the solutions of each subnetwork to get the steady state of the entire network
%
% If the checker says we don't need to solve the entire network
if do_not_solve_entire_network == 1
% No free parameters found (since we could not solve the network)
free_parameter = [ ];
% Get list of solved subnetworks
solved = setdiff(1:numel(P), unsolved_subnetwork);
print_solved = sprintf('%d, ', solved);
print_solved(end-1:end) = [ ]; % To clean the trailing comma and space at the end of the list
if isempty(solved)
if numel(P) == 1
fprintf('Could not solve the network.\n')
else
fprintf('No subnetwork was solved.\n')
end
elseif length(solved) == 1
fprintf('Solved only Subnetwork %s.\n', print_solved)
else
fprintf('Solved only Subnetworks %s.\n', print_solved)
end
% Get list of unsolved subnetworks
print_unsolved = sprintf('%d, ', unsolved_subnetwork);
print_unsolved(end-1:end) = [ ]; % To clean the trailing comma and space at the end of the list
if isempty(solved)
fprintf('Try to compute the subnetworks manually then combine their solutions.\n\n')
elseif length(unsolved_subnetwork) == 1
if numel(P) == 1
fprintf('Try to compute the network manually.\n\n')
else
fprintf('Try to compute Subnetwork %s manually then combine with previously solved subnetworks.\n\n', print_unsolved)
end
elseif isempty(solved) & length(unsolved_subnetwork) > 1
fprintf('Try to compute Subnetworks %s manually then combine their solutions.\n\n', print_unsolved)
else
fprintf('Try to compute Subnetworks %s manually then combine with previously solved subnetworks.\n\n', print_unsolved)
end
% Display conservation laws
if isempty(conservation_law_matrix)
fprintf('Conservation laws: None \n\n')
elseif length(conservation_law) == 1
fprintf('Conservation law: %s \n\n', conservation_law{1})
else
fprintf('Conservation laws: \n')
for i = 1:length(conservation_law)
disp(conservation_law{i})
end
end
% Exit the algorithm
return
end
if numel(P) == 1
fprintf('Solving positive steady state parametrization of the network...\n\n')
else
fprintf('Solving positive steady state parametrization of the entire network...\n\n')
end
% Get the unique species we have solved so far
[unique_species, ~, unique_numbering] = unique(species);
% Create a list of species already solved or reserved to be solved
species_solved = unique_species;
% Count how many of each species appeared in the initial solution
unique_species_occurrence = histc(unique_numbering, unique(unique_numbering));
% Get index of species that occur more than once
not_unique_species_index = find(unique_species_occurrence > 1);
% Go through each species with duplicate equation
for i = 1:length(not_unique_species_index)
% Look for the equations of the species
equation_number = find(has(species, unique_species(not_unique_species_index(i))));
% Initialize list of species in each equation
species_ = [ ];
% Get all species in each equation
for j = 1:length(equation_number)
% Get all variables involved
all_var = symvar(equation{equation_number(j)});
% Get the species only
species_ = [species_, intersect(cell2sym(model.species), all_var)];
end
% Removed from considerations the reserved species
species_ = setdiff(species_, species_solved);
% List down the variables to be solved: should be the number of equations
% One of them should be the repeated species
% The rest will come from 'species_'
vars = [unique_species(not_unique_species_index(i))];
% Determine the possible additional species to be solved for
additional = nchoosek(species_, length(equation_number) - 1);
% Solve the system of equations
for j = 1:length(additional)
% Add each combination of possible additional species
vars_ = [vars, additional(j,:)];
% Solve for the indicated species
solution = solve([equation_for_solving{equation_number}], vars_);
% Convert the structure of solutions to cell array
solution = struct2cell(solution);
% Initialize solution_
solution_ = { };
% For multiple solutions, we'll just get the first one
for i = 1:length(solution)
solution_{end+1} = solution{i}(1);
end
% This is the solution we'll get
solution = solution_;
% Check if the solution is not empty
if ~isempty(cell2sym(solution))
% Prepare a checker variable
checker = [ ];
% Check if any of the solutions has a negative sign
for k = 1:length(solution)
checker = [checker, findstr(string(solution{k}), '-')];
end
% No need to consider the other combinations of possible additional species
if isempty(checker)
break
end
end
end
% Add the species you just solved to the list of solved species
species_solved = unique([species_solved, vars_]);
% Get the species of the solution and replace these to the list of species
species(equation_number) = vars_;
% Get the solutions and replace these to the list of equations
equation(equation_number) = solution;
end
% Initialize list of equations in terms of the free parameters
equation_final = equation;
% Initialize flag whether to skip the simplification of the solution
skip_simplification = 0;
% Make sure the equations contain only free parameters
for i = 1:length(equation_final)
% Initialize list of dependent variables
dep_var_cycle = {species(i)};
% Keep on substituting until there are no more steady state variables
while has(equation_final{i}, species)
% Get all the variables in the equation
all_var = symvar(equation_final{i});
% Get only the steady state species
dep_var = intersect(all_var, species);
% Replace each steady state species with its corresponding expression
for j = 1:length(dep_var)
equation_final{i} = simplify(subs(equation_final{i}, {dep_var(j)}, equation_final{find(species == dep_var(j))}));
% Check if the dependent variable also has another dependent variable
dep_var_eq = equation_final{find(species == dep_var(j))};
all_var2 = symvar(dep_var_eq);
dep_var2 = intersect(all_var2, species);
% If it has further dependent variables
if ~isempty(dep_var2)
% Check if the variable to be added is already in the list
if ~ismember(dep_var(j), dep_var_cycle)
dep_var_cycle{end+1} = dep_var(j);
else
fprintf('Could not write parametrization in terms of free parameters.\n')
fprintf('At least %s and %s are dependent on each other.\n\n', dep_var(j), dep_var_cycle{end})
% Indicate that displaying the parametrization needs to be skipped
skip_simplification = 1;
end
end
end
% Do not continue the while loop if flag is 1
if skip_simplification == 1
% Exit the loop
break
end
end
% Do not continue to the next equation if flag is 1
if skip_simplification == 1
break
end
end
% Sort the species
[species, index] = sort(species);
% Get the free parameters
free_parameter = string(setdiff(cell2sym(model.species), species));
% If simplification was skipped
if skip_simplification == 1
% Sort the untouched equations according to the species
equation = equation(index);
% Display the untouched steady state solution of the system
fprintf('The solution is:\n\n')
for i = 1:length(equation)
fprintf('%s = %s\n', species(i), equation{i})
end
else
% Otherwise, use the parametrization in terms of free parameters
equation_final = equation_final(index);
% Display the parametrized steady state solution of the system
fprintf('The solution is:\n\n')
for i = 1:length(equation_final)
fprintf('%s = %s\n', species(i), equation_final{i})
end
end
% Display the list of free parameters
if isempty(free_parameter)
fprintf('Free parameters: None \n\n')
elseif length(free_parameter) == 1
fprintf(1, 'Free parameter: ')
for i = 1:length(free_parameter)-1
fprintf(1, '%s, ', char(free_parameter(i)'))
end
fprintf(1, '%s\n\n', char(free_parameter(end)))
else
fprintf(1, 'Free parameters: ')
for i = 1:length(free_parameter)-1
fprintf(1, '%s, ', char(free_parameter(i)'))
end
fprintf(1, '%s\n\n', char(free_parameter(end)))
end
% Display conservation laws
if isempty(conservation_law_matrix)
fprintf('Conservation laws: None \n\n')
elseif length(conservation_law) == 1
fprintf('Conservation law: %s \n\n', conservation_law{1})
else
fprintf('Conservation laws: \n')
for i = 1:length(conservation_law)
disp(conservation_law{i})
end
end
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % %
% % The following are functions used in the algorithm % %
% % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% %
% Function 1 of 13: indepDecomp %
% %
% - Purpose: To decompose a network into its finest independent %
% decomposition %
% - Input: model: empty species list %
% - Outputs %
% - model: completed structure %
% - R: matrix of reaction vectors of the network %
% - G: undirected graph of R %
% - P: partitions representing the decomposition of the reactions %
% - Used in steadyState (Step 1) %
% %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
function [model, N, R, G, P] = indepDecomp(model)
% Create a list of all species indicated in the reactions
[model, m] = modelSpecies(model);
% Form stoichiometric matrix N
[N, ~, ~, r] = stoichMatrix(model, m);
% Get the transpose of N: Each row now represents the reaction vector a reaction
R = N';
% Write R in reduced row echelon form: the transpose of R is used so 'basis_reaction_num' will give the pivot rows of R
% - 'basis_reaction_num' gives the row numbers of R which form a basis for the rowspace of R
[~, basis_reaction_num] = rref(R');
% Form a basis for the rowspace of R
basis = R(basis_reaction_num, :);
% Initialize an undirected graph G
G = graph();
% Construct the vertex set of undirected graph G
% Add vertices to G: these are the reaction vectors that form a basis for the rowspace of R
for i = 1:numel(basis_reaction_num)
% Use the reaction number as label for each vertex
G = addnode(G, strcat('R', num2str(basis_reaction_num(i))));
end
% Initialize matrix of linear combinations
linear_combo = zeros(r, numel(basis_reaction_num));
% Write the nonbasis reaction vectors as a linear combination of the basis vectors
% Do this for the nonbasis reactions vectors
for i = 1:r
if ~ismember(i, basis_reaction_num)
% This gives the coefficients of the linear combinations
% The basis vectors will have a row of zeros
linear_combo(i, :) = basis'\R(i, :)';
end
end
% Round off values to nearest whole number to avoid round off errors
linear_combo = round(linear_combo);
% Get the reactions that are linear combinations of at least 2 basis reactions
% These are the reactions where we'll get the edges
get_edges = find(sum(abs(linear_combo), 2) > 1);
% Initialize an array for sets of vertices that will form the edges
vertex_set = { };
% Identify which vertices form edges in each reaction: get those with non-zero coefficients in the linear combinations
for i = 1:numel(get_edges)
vertex_set{i} = find(linear_combo(get_edges(i), :) ~= 0);
end
% Initialize the edge set
edges = [ ];
% Get all possible combinations (not permutations) of the reactions involved in the linear combinations
for i = 1:numel(vertex_set)
edges = [edges; nchoosek(vertex_set{i}, 2)];
end
% Get just the unique edges
edges = unique(edges, 'rows');
% Add these edges to graph G
for i = 1:size(edges, 1)
G = addedge(G, strcat('R', num2str(basis_reaction_num(edges(i, 1)))), strcat('R', num2str(basis_reaction_num(edges(i, 2)))));
end
% Determine to which component each vertex belongs to
component_numbers = conncomp(G);
% Determine the number of connected components of G: this is the number of partitions R will be decomposed to
num_components = max(component_numbers);
% For the case of only one connected component
if num_components == 1
P = [ ];
end
% Initialize the list of partitions
P = cell(1, num_components);
% Basis vectors: assign them first into their respective partition based on their component number
for i = 1:numel(component_numbers)
P{component_numbers(i)}(end+1) = basis_reaction_num(i);
end
% Nonbasis vectors: they go to the same partition as the basis vectors that form their linear combination
for i = 1:numel(P)
for j = 1:numel(P{i})
% Get the column number representing the basis vectors in 'linear_combo'
col = find(basis_reaction_num == P{i}(j));
% Check which reactions used a particular basis vector and assign them to their respective partition
P{i} = [P{i} find(linear_combo(:, col) ~= 0)'];
end
end
% Get only unique elements in each partition
for i = 1:numel(P)
P{i} = unique(P{i});
end
% If some reactions are missing, then redo starting from the linear combination part
if length(cell2mat(P)) ~= size(R, 1)
% Do not round off the coefficients of the linear combinations
linear_combination = linear_combo;
get_edges = find(sum(abs(linear_combination), 2) > 1);
vertex_set = { };
for i = 1:numel(get_edges)
vertex_set{i} = find(linear_combination(get_edges(i), :) ~= 0);
end
edges = [ ];
for i = 1:numel(vertex_set)
edges = [edges; nchoosek(vertex_set{i}, 2)];
end
edges = unique(edges, 'rows');
for i = 1:size(edges, 1)
G = addedge(G, strcat('R', num2str(basis_reaction_num(edges(i, 1)))), strcat('R', num2str(basis_reaction_num(edges(i, 2)))));
end
component_numbers = conncomp(G);
num_components = max(component_numbers);
if num_components == 1
P = [ ];
end
P = cell(1, num_components);
for i = 1:numel(component_numbers)
P{component_numbers(i)}(end+1) = basis_reaction_num(i);
end
for i = 1:numel(P)
for j = 1:numel(P{i})
col = find(basis_reaction_num == P{i}(j));
P{i} = [P{i} find(linear_combination(:, col) ~= 0)'];
end
end
for i = 1:numel(P)
P{i} = unique(P{i});
end
end
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% %
% Function 2 of 13: analyticSolution %
% %
% - Purpose: To solve for the parametrized steady state solution of a %
% network %
% - Inputs %
% - model: empty species list %
% - P_: list of reaction numbers from the independent decomposition %
% - B_: list of tau's already used in parametrization %
% - sigma_: list of sigma's already used in parametrization %
% - Outputs %
% - param: list of parametrization of the species of the system %
% - B_: updated list of tau's already used in parametrization %
% - sigma_: updated list of sigma's already used in parametrization %
% - k: list of rate constants of the system %
% - sigma: list of sigma's of the system %
% - tau: list of tau's of the system %
% - model: completed structure %
% - skip: logical; indicator for steadyState to skip solving the %
% subnetwork %
% - Used in steadyState (Step 2) %
% - Note: The function uses the class graph_.m (which uses edge.m and %
% vertex.m) %
% %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
function [param, B_, sigma_, k, sigma, tau, model, skip] = analyticSolution(model, P_, B_, sigma_)
% Create a list of all species indicated in the reactions
[model, m] = modelSpecies(model);
% Form stoichiometric matrix N
[N, reactant_complex, product_complex, r] = stoichMatrix(model, m);
% Get just the unique complexes
% index(i) is the index in all_complex of the reactant complex in reaction i
all_complex = unique([reactant_complex product_complex]', 'rows');
% Construct the matrix of complexes
all_complex = all_complex';
% Count the number of complexes
n = size(all_complex, 2);
% Determine the number of linkage and strong linkage classes
[l, sl] = linkageClass(reactant_complex, product_complex);
% Check if weakly reversible
is_weakly_reversible = sl == l;
% Get the rank of the reaction network
s = rank(N);
% Compute the deficiency of the reaction network
deficiency = n - l - s;
% Initialize indicator if steadyStage needs to skip the subnetwork
skip = 0;
% If the network has deficiency 0 and is weakly reversible, no need to translate
if is_weakly_reversible & deficiency == 0
% Get the matrix of reactant and product complexes
stoichiometric_complex_reactant = reactant_complex;
stoichiometric_complex_product = product_complex;
% Create generalized chemical reaction network (GCRN) vertices
GCRN_reactant = [stoichiometric_complex_reactant; stoichiometric_complex_reactant];
GCRN_product = [stoichiometric_complex_product; stoichiometric_complex_product];
GCRN_vertex = unique([GCRN_reactant, GCRN_product]', 'rows');
GCRN_vertex = GCRN_vertex';
else
% Generate the GCRN
[stoichiometric_complex_reactant, stoichiometric_complex_product, kinetic_complex_reactant, kinetic_complex_product, model, skip] = GCRN(model);
% If the subnetwork needs to be skipped
if skip == 1
% Still form rate constants k
% Create rate constants based on reaction number of decomposition
for i = 1:length(P_)
try
k(end+1) = sym(strcat('k', string(P_(i))));
catch
k = sym(strcat('k', string(P_(i))));
end
end
% Empty results since no translation was found
sigma = [ ];
tau = [ ];
param = [ ];
% Exit the function
return
end
% Create GCRN vertices
GCRN_reactant = [stoichiometric_complex_reactant; kinetic_complex_reactant];
GCRN_product = [stoichiometric_complex_product; kinetic_complex_product];
GCRN_vertex = unique([GCRN_reactant, GCRN_product]', 'rows');
GCRN_vertex = GCRN_vertex';
end
% Initialize graph edges
GCRN_graph = zeros(2, size(GCRN_reactant, 2));
for i = 1:size(GCRN_reactant, 2)
% Fill out the reactants
GCRN_graph(1, i) = find(ismember(GCRN_vertex', GCRN_reactant(:, i)', 'rows'));
% Fill out the products
GCRN_graph(2, i) = find(ismember(GCRN_vertex', GCRN_product(:, i)', 'rows'));
end
% Initialize spanning forest
M = zeros(size(stoichiometric_complex_reactant, 1), size(GCRN_vertex, 2) - 1);
% Create spanning forest using kinetic complexes
for i = 2:size(GCRN_vertex, 2)
M(:, i-1) = GCRN_vertex(size(GCRN_vertex, 1)/2+1:size(GCRN_vertex, 1), i) - GCRN_vertex(size(GCRN_vertex, 1)/2+1:size(GCRN_vertex, 1), 1);
end
% Let Mp be M'
Mp = M';
% Create a symbolic matrix H
syms H [size(M, 1) size(M, 2)] matrix
H = symmatrix2sym(H); % To see the elements of H
% We want to solve H in M' * H * M' = M'
% LHS
H_ = M' * H * M';
% Solve for H when LHS = RHS
H_ = solve([H_(:) == Mp(:)]);
% Extract the solution and place them in matrix H
% H is the generalized inverse of M'
for i = 1:length(fieldnames(H_))
H(i) = getfield(H_, string(H(i)));
end
% For the case when H has only 1 entry
if isempty(H(i))
H = 1;
end
% Form B: outputs the answer as rational numbers
% im B = ker M'
B = null(M', 'r');
% Number of vertices
V = size(GCRN_vertex, 2);
% Initialize digraph