Skip to content

Commit 1bc6bcc

Browse files
author
Philipp Schneider
committed
normalized line endings
1 parent f44b419 commit 1bc6bcc

11 files changed

+662
-658
lines changed

LICENSE

+201-201
Large diffs are not rendered by default.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# CNAhelpers
2-
A bunch of functions that come in handy when working with CNA
1+
# CNAhelpers
2+
A bunch of functions that come in handy when working with CNA
33
another branch

cell2csv.m

+93-93
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,93 @@
1-
function cell2csv(fileName, cellArray, separator, excelYear, decimal)
2-
% Writes cell array content into a *.csv file.
3-
%
4-
% CELL2CSV(fileName, cellArray[, separator, excelYear, decimal])
5-
%
6-
% fileName = Name of the file to save. [ e.g. 'text.csv' ]
7-
% cellArray = Name of the Cell Array where the data is in
8-
%
9-
% optional:
10-
% separator = sign separating the values (default = ',')
11-
% excelYear = depending on the Excel version, the cells are put into
12-
% quotes before they are written to the file. The separator
13-
% is set to semicolon (;) (default = 1997 which does not change separator to semicolon ;)
14-
% decimal = defines the decimal separator (default = '.')
15-
%
16-
% by Sylvain Fiedler, KA, 2004
17-
% updated by Sylvain Fiedler, Metz, 06
18-
% updated by Philipp Schneider, 2019
19-
% fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler
20-
% added the choice of decimal separator, 11/2010, S.Fiedler
21-
% modfiedy and optimized by Jerry Zhu, June, 2014, [email protected]
22-
% now works with empty cells, numeric, char, string, row vector, and logical cells.
23-
% row vector such as [1 2 3] will be separated by two spaces, that is "1 2 3"
24-
% One array can contain all of them, but only one value per cell.
25-
% 2x times faster than Sylvain's codes (8.8s vs. 17.2s):
26-
% tic;C={'te','tm';5,[1,2];true,{}};C=repmat(C,[10000,1]);cell2csv([datestr(now,'MMSS') '.csv'],C);toc;
27-
28-
%% Checking for optional Variables
29-
if ~exist('separator', 'var')
30-
separator = ',';
31-
end
32-
33-
if ~exist('excelYear', 'var')
34-
excelYear = 1997;
35-
end
36-
37-
if ~exist('decimal', 'var')
38-
decimal = '.';
39-
end
40-
41-
%% Setting separator for newer excelYears
42-
if excelYear > 2000
43-
separator = ';';
44-
end
45-
46-
% convert cell
47-
cellArray = cellfun(@StringX, cellArray, 'UniformOutput', false);
48-
cellArray = regexprep(cellArray,'(?<!%)%(?!%)','%%'); % to replace escape characters
49-
50-
%% Write file
51-
datei = fopen(fileName, 'w');
52-
[nrows,ncols] = size(cellArray);
53-
for row = 1:nrows
54-
fprintf(datei,[sprintf(['%s' separator],cellArray{row,1:ncols-1}) cellArray{row,ncols} '\n']);
55-
end
56-
% Closing file
57-
fclose(datei);
58-
59-
% sub-function
60-
function x = StringX(x)
61-
% If ischar, do nothing
62-
if ischar(x)
63-
% If zero, then empty cell
64-
elseif isempty(x)
65-
x = '';
66-
% If numeric -> String, e.g. 1, [1 2]
67-
elseif isnumeric(x) && isrow(x)
68-
x = num2str(x);
69-
if decimal ~= '.'
70-
x = strrep(x, '.', decimal);
71-
end
72-
% If logical -> 'true' or 'false'
73-
elseif islogical(x)
74-
if x == 1
75-
x = 'TRUE';
76-
else
77-
x = 'FALSE';
78-
end
79-
% If matrix array -> a1 a2 a3. e.g. [1 2 3]
80-
% also catch string or char here
81-
elseif isrow(x) && ~iscell(x)
82-
x = num2str(x);
83-
% everthing else, such as [1;2], {1}
84-
else
85-
x = 'NA';
86-
end
87-
88-
% If newer version of Excel -> Quotes 4 Strings
89-
if excelYear > 2000
90-
x = ['"' x '"'];
91-
end
92-
end % end sub-function
93-
end % end function
1+
function cell2csv(fileName, cellArray, separator, excelYear, decimal)
2+
% Writes cell array content into a *.csv file.
3+
%
4+
% CELL2CSV(fileName, cellArray[, separator, excelYear, decimal])
5+
%
6+
% fileName = Name of the file to save. [ e.g. 'text.csv' ]
7+
% cellArray = Name of the Cell Array where the data is in
8+
%
9+
% optional:
10+
% separator = sign separating the values (default = ',')
11+
% excelYear = depending on the Excel version, the cells are put into
12+
% quotes before they are written to the file. The separator
13+
% is set to semicolon (;) (default = 1997 which does not change separator to semicolon ;)
14+
% decimal = defines the decimal separator (default = '.')
15+
%
16+
% by Sylvain Fiedler, KA, 2004
17+
% updated by Sylvain Fiedler, Metz, 06
18+
% updated by Philipp Schneider, 2019
19+
% fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler
20+
% added the choice of decimal separator, 11/2010, S.Fiedler
21+
% modfiedy and optimized by Jerry Zhu, June, 2014, [email protected]
22+
% now works with empty cells, numeric, char, string, row vector, and logical cells.
23+
% row vector such as [1 2 3] will be separated by two spaces, that is "1 2 3"
24+
% One array can contain all of them, but only one value per cell.
25+
% 2x times faster than Sylvain's codes (8.8s vs. 17.2s):
26+
% tic;C={'te','tm';5,[1,2];true,{}};C=repmat(C,[10000,1]);cell2csv([datestr(now,'MMSS') '.csv'],C);toc;
27+
28+
%% Checking for optional Variables
29+
if ~exist('separator', 'var')
30+
separator = ',';
31+
end
32+
33+
if ~exist('excelYear', 'var')
34+
excelYear = 1997;
35+
end
36+
37+
if ~exist('decimal', 'var')
38+
decimal = '.';
39+
end
40+
41+
%% Setting separator for newer excelYears
42+
if excelYear > 2000
43+
separator = ';';
44+
end
45+
46+
% convert cell
47+
cellArray = cellfun(@StringX, cellArray, 'UniformOutput', false);
48+
cellArray = regexprep(cellArray,'(?<!%)%(?!%)','%%'); % to replace escape characters
49+
50+
%% Write file
51+
datei = fopen(fileName, 'w');
52+
[nrows,ncols] = size(cellArray);
53+
for row = 1:nrows
54+
fprintf(datei,[sprintf(['%s' separator],cellArray{row,1:ncols-1}) cellArray{row,ncols} '\n']);
55+
end
56+
% Closing file
57+
fclose(datei);
58+
59+
% sub-function
60+
function x = StringX(x)
61+
% If ischar, do nothing
62+
if ischar(x)
63+
% If zero, then empty cell
64+
elseif isempty(x)
65+
x = '';
66+
% If numeric -> String, e.g. 1, [1 2]
67+
elseif isnumeric(x) && isrow(x)
68+
x = num2str(x);
69+
if decimal ~= '.'
70+
x = strrep(x, '.', decimal);
71+
end
72+
% If logical -> 'true' or 'false'
73+
elseif islogical(x)
74+
if x == 1
75+
x = 'TRUE';
76+
else
77+
x = 'FALSE';
78+
end
79+
% If matrix array -> a1 a2 a3. e.g. [1 2 3]
80+
% also catch string or char here
81+
elseif isrow(x) && ~iscell(x)
82+
x = num2str(x);
83+
% everthing else, such as [1;2], {1}
84+
else
85+
x = 'NA';
86+
end
87+
88+
% If newer version of Excel -> Quotes 4 Strings
89+
if excelYear > 2000
90+
x = ['"' x '"'];
91+
end
92+
end % end sub-function
93+
end % end function

check_mass_balance.m

+46-46
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
function [mass_imbalanced_reacs, charge_imbalanced_reacs] = check_mass_balance(cnap)
2-
% check mass balance of a stoichiometric model
3-
%
4-
5-
%% 1. find exchange reactions, chemical formulas and occurring elements
6-
exchange_reacs = all(sign(cnap.stoichMat)<=0,1) | all(sign(cnap.stoichMat)>=0,1);
7-
chemFormula = CNAgetGenericSpeciesData_as_array(cnap,'fbc_chemicalFormula');
8-
if isempty(chemFormula)
9-
warning('field fbc_chemicalFormula not found. Trying to identify chemical formulas spec notes directly.');
10-
chemFormula = regexp(cnap.specNotes,'(?<=\[).*(?=\])','match');
11-
chemFormula = [chemFormula{:}]';
12-
end
13-
charge = cell2mat(CNAgetGenericSpeciesData_as_array(cnap,'fbc_charge'))';
14-
if isempty(charge)
15-
warning('field fbc_charge not found. Trying to identify charges from spec notes directly.');
16-
charge = regexp(cnap.specNotes,'(?<=<).*(?=>)','match');
17-
charge = cellfun(@str2num,[charge{:}]);
18-
end
19-
elements = regexp(chemFormula,'[A-Z][a-z]*','match');
20-
elements = unique([elements{:}]);
21-
% if no element counter was set, set a '1' after the element
22-
chemFormula = regexprep(chemFormula, ['(?<=[' strjoin(elements,'|') '])(?=[^a-z0-9]|$)'],'1','emptymatch');
23-
%% 2. make a matrix with all species and their elements
24-
species_element_matrix = zeros(cnap.nums,numel(elements));
25-
for i = 1:numel(elements)
26-
elem_count = regexp(chemFormula, ['(?<=' elements{i} ')[0-9]*'],'match');
27-
elem_count(cellfun(@isempty,elem_count)) = {'0'};
28-
elem_count = cellfun(@str2num,[elem_count{:}]);
29-
species_element_matrix(:,i) = elem_count;
30-
end
31-
%% 3. check if reactions are balanced
32-
disp('checking for reactions with mass imbalance...');
33-
mass_balances = cnap.stoichMat'*species_element_matrix;
34-
mass_imbalanced_reacs = find(any(abs(mass_balances)>1e-4,2) & ~exchange_reacs');
35-
disp(['model has ' num2str(sum(any(abs(mass_balances)>1e-4,2) & exchange_reacs')) ' mass-imbalanced exchange reactions.'])
36-
disp(['model has ' num2str(numel(mass_imbalanced_reacs)) ' mass-imbalanced metabolic reactions:']);
37-
disp(cellstr(cnap.reacID(mass_imbalanced_reacs,:)));
38-
39-
disp('checking for reactions with charge imbalance...');
40-
charge_balances = cnap.stoichMat'*charge';
41-
charge_imbalanced_reacs = find(any(abs(charge_balances)>1e-4,2) & ~exchange_reacs');
42-
disp(['model has ' num2str(sum(any(abs(charge_balances)>1e-4,2) & exchange_reacs')) ' charge-imbalanced exchange reactions.'])
43-
disp(['model has ' num2str(numel(charge_imbalanced_reacs)) ' charge-imbalanced metabolic reactions:']);
44-
disp(cellstr(cnap.reacID(charge_imbalanced_reacs,:)));
45-
end
46-
1+
function [mass_imbalanced_reacs, charge_imbalanced_reacs] = check_mass_balance(cnap)
2+
% check mass balance of a stoichiometric model
3+
%
4+
5+
%% 1. find exchange reactions, chemical formulas and occurring elements
6+
exchange_reacs = all(sign(cnap.stoichMat)<=0,1) | all(sign(cnap.stoichMat)>=0,1);
7+
chemFormula = CNAgetGenericSpeciesData_as_array(cnap,'fbc_chemicalFormula');
8+
if isempty(chemFormula)
9+
warning('field fbc_chemicalFormula not found. Trying to identify chemical formulas spec notes directly.');
10+
chemFormula = regexp(cnap.specNotes,'(?<=\[).*(?=\])','match');
11+
chemFormula = [chemFormula{:}]';
12+
end
13+
charge = cell2mat(CNAgetGenericSpeciesData_as_array(cnap,'fbc_charge'))';
14+
if isempty(charge)
15+
warning('field fbc_charge not found. Trying to identify charges from spec notes directly.');
16+
charge = regexp(cnap.specNotes,'(?<=<).*(?=>)','match');
17+
charge = cellfun(@str2num,[charge{:}]);
18+
end
19+
elements = regexp(chemFormula,'[A-Z][a-z]*','match');
20+
elements = unique([elements{:}]);
21+
% if no element counter was set, set a '1' after the element
22+
chemFormula = regexprep(chemFormula, ['(?<=[' strjoin(elements,'|') '])(?=[^a-z0-9]|$)'],'1','emptymatch');
23+
%% 2. make a matrix with all species and their elements
24+
species_element_matrix = zeros(cnap.nums,numel(elements));
25+
for i = 1:numel(elements)
26+
elem_count = regexp(chemFormula, ['(?<=' elements{i} ')[0-9]*'],'match');
27+
elem_count(cellfun(@isempty,elem_count)) = {'0'};
28+
elem_count = cellfun(@str2num,[elem_count{:}]);
29+
species_element_matrix(:,i) = elem_count;
30+
end
31+
%% 3. check if reactions are balanced
32+
disp('checking for reactions with mass imbalance...');
33+
mass_balances = cnap.stoichMat'*species_element_matrix;
34+
mass_imbalanced_reacs = find(any(abs(mass_balances)>1e-4,2) & ~exchange_reacs');
35+
disp(['model has ' num2str(sum(any(abs(mass_balances)>1e-4,2) & exchange_reacs')) ' mass-imbalanced exchange reactions.'])
36+
disp(['model has ' num2str(numel(mass_imbalanced_reacs)) ' mass-imbalanced metabolic reactions:']);
37+
disp(cellstr(cnap.reacID(mass_imbalanced_reacs,:)));
38+
39+
disp('checking for reactions with charge imbalance...');
40+
charge_balances = cnap.stoichMat'*charge';
41+
charge_imbalanced_reacs = find(any(abs(charge_balances)>1e-4,2) & ~exchange_reacs');
42+
disp(['model has ' num2str(sum(any(abs(charge_balances)>1e-4,2) & exchange_reacs')) ' charge-imbalanced exchange reactions.'])
43+
disp(['model has ' num2str(numel(charge_imbalanced_reacs)) ' charge-imbalanced metabolic reactions:']);
44+
disp(cellstr(cnap.reacID(charge_imbalanced_reacs,:)));
45+
end
46+

clr.m

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
handles = double(findall(0,'type','figure'));
2+
delete(handles(~strcmp(get(handles,'Tag'),'CellNetAnalyzer')));
3+
cnan = rmfield(cnan,'open_projects');
4+
cnan.open_projects = struct;
15
a=[setdiff(who,'cnan');{'a'}];
26
clear(a{:});
37
clc;

combvec.m

+47-47
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
function y = combvec(varargin)
2-
%COMBVEC Create all combinations of vectors.
3-
%
4-
% <a href="matlab:doc combvec">combvec</a>(A1,A2,...) takes any number of inputs A, where each Ai has
5-
% Ni columns, and return a matrix of (N1*N2*...) column vectors, where
6-
% the columns consist of all combinations found by combining one column
7-
% vector from each Ai.
8-
%
9-
% For instance, here the four combinations of two 2-column matrices are
10-
% found.
11-
%
12-
% a1 = [1 2 3; 4 5 6];
13-
% a2 = [7 8; 9 10];
14-
% a3 = <a href="matlab:doc combvec">combvec</a>(a1,a2)
15-
16-
% Mark Beale, 12-15-93
17-
% Copyright 1992-2010 The MathWorks, Inc.
18-
19-
if isempty(varargin)
20-
y = [];
21-
else
22-
y = varargin{1};
23-
for i=2:length(varargin)
24-
z = varargin{i};
25-
y = [copy_blocked(y,size(z,2)); copy_interleaved(z,size(y,2))];
26-
end
27-
end
28-
29-
%=========================================================
30-
function b = copy_blocked(m,n)
31-
32-
[mr,mc] = size(m);
33-
b = zeros(mr,mc*n);
34-
ind = 1:mc;
35-
for i = [0:(n-1)]*mc
36-
b(:,ind+i) = m;
37-
end
38-
%=========================================================
39-
40-
function b = copy_interleaved(m,n)
41-
42-
[mr,mc] = size(m);
43-
b = zeros(mr*n,mc);
44-
ind = 1:mr;
45-
for i=[0:(n-1)]*mr
46-
b(ind+i,:) = m;
47-
end
1+
function y = combvec(varargin)
2+
%COMBVEC Create all combinations of vectors.
3+
%
4+
% <a href="matlab:doc combvec">combvec</a>(A1,A2,...) takes any number of inputs A, where each Ai has
5+
% Ni columns, and return a matrix of (N1*N2*...) column vectors, where
6+
% the columns consist of all combinations found by combining one column
7+
% vector from each Ai.
8+
%
9+
% For instance, here the four combinations of two 2-column matrices are
10+
% found.
11+
%
12+
% a1 = [1 2 3; 4 5 6];
13+
% a2 = [7 8; 9 10];
14+
% a3 = <a href="matlab:doc combvec">combvec</a>(a1,a2)
15+
16+
% Mark Beale, 12-15-93
17+
% Copyright 1992-2010 The MathWorks, Inc.
18+
19+
if isempty(varargin)
20+
y = [];
21+
else
22+
y = varargin{1};
23+
for i=2:length(varargin)
24+
z = varargin{i};
25+
y = [copy_blocked(y,size(z,2)); copy_interleaved(z,size(y,2))];
26+
end
27+
end
28+
29+
%=========================================================
30+
function b = copy_blocked(m,n)
31+
32+
[mr,mc] = size(m);
33+
b = zeros(mr,mc*n);
34+
ind = 1:mc;
35+
for i = [0:(n-1)]*mc
36+
b(:,ind+i) = m;
37+
end
38+
%=========================================================
39+
40+
function b = copy_interleaved(m,n)
41+
42+
[mr,mc] = size(m);
43+
b = zeros(mr*n,mc);
44+
ind = 1:mr;
45+
for i=[0:(n-1)]*mr
46+
b(ind+i,:) = m;
47+
end
4848
b = reshape(b,mr,n*mc);

0 commit comments

Comments
 (0)