Skip to content

Commit

Permalink
refactor: add & remove COBRA prefixes (#565)
Browse files Browse the repository at this point in the history
* feat: dedicated funcs to add and rem ID prefixes

* refactor: add and rem COBRA prefixes
  • Loading branch information
edkerk authored Oct 12, 2024
1 parent e6f2837 commit 792d982
Show file tree
Hide file tree
Showing 12 changed files with 1,563 additions and 1,258 deletions.
28 changes: 17 additions & 11 deletions core/checkModelStruct.m
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,27 @@ function checkModelStruct(model,throwErrors,trimWarnings)
end

%Validate format of ids
fields={'rxns';'mets';'comps';'genes'};
fieldNames = {'reaction';'metabolite';'compartment';'gene'};
fields = {'rxns';'mets';'comps';'genes'};
fieldNames = {'reaction';'metabolite';'compartment';'gene'};
fieldPrefix = {'R_';'M_';'C_';'G_'};
for i=1:numel(fields)
try
numIDs = startsWith(model.(fields{i}),digitsPattern(1));
if any(numIDs)
EM = ['The following ' fieldNames{i} ' IDs start with a digit, ' ...
'which does not comply with SBML specifications. This does '...
'not impact RAVEN functionality, but before exporting the ' ...
'model to SBML you should either correct these ' ...
'incompatibilities, or you can run exportModel with ' ...
'COBRAstyle as true: exportModel(model, ''filename.xml'', true); '];
dispEM(EM,throwErrors,{model.(fields{i}){numIDs}},trimWarnings);
end
catch
numIDs = [];
end
if any(numIDs)
EM = ['The following ' fieldNames{i} ' identifiers start with a ' ...
'digit, which does not comply with SBML specifications. This ' ...
'does not impact RAVEN functionality, but before exporting ' ...
'the model to SBML you should either (a) correct these ' ...
'identifiers directly; or (b) run "model = addCOBRAstylePrefixes ' ...
'(model,''' fields{i} ''');" to have COBRA style "' fieldPrefix{i} ...
'" prefixes added to all ' fieldNames{i} ' identifiers; or (c) ' ...
'let exportModel add COBRA style prefixes to all identifiers ' ...
'in the model by setting COBRAstyle as true. Example: ' ...
'"exportModel(model, ''filename.xml'', true);"'];
dispEM(EM,throwErrors,{model.(fields{i}){numIDs}},trimWarnings);
end
end

Expand Down
410 changes: 208 additions & 202 deletions doc/core/checkModelStruct.html

Large diffs are not rendered by default.

106 changes: 53 additions & 53 deletions doc/index.html

Large diffs are not rendered by default.

1,378 changes: 682 additions & 696 deletions doc/io/exportModel.html

Large diffs are not rendered by default.

437 changes: 204 additions & 233 deletions doc/io/importModel.html

Large diffs are not rendered by default.

125 changes: 125 additions & 0 deletions doc/struct_conversion/addCOBRAstylePrefixes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Description of addCOBRAstylePrefixes</title>
<meta name="keywords" content="addCOBRAstylePrefixes">
<meta name="description" content="addCOBRAstylePrefixes">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="generator" content="m2html v1.5 &copy; 2003-2005 Guillaume Flandin">
<meta name="robots" content="index, follow">
<link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../index.html">Home</a> &gt; <a href="index.html">struct_conversion</a> &gt; addCOBRAstylePrefixes.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for struct_conversion&nbsp;<img alt=">" border="0" src="../right.png"></a></td></tr></table>-->

<h1>addCOBRAstylePrefixes
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>addCOBRAstylePrefixes</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function model=addCOBRAstylePrefixes(model,fields,printWarnings) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment"> addCOBRAstylePrefixes
This function adds COBRA style identifier prefixes, which are:
&quot;R_&quot; for model.rxns,
&quot;M_&quot; for model.mets,
&quot;C_&quot; for model.comps;
&quot;G_&quot; for model.genes (and also represented in model.grRules).
If all entries in a field already have the prefix, then no additional
prefix will be added.

Input:
model model whose identifiers should be modified
fields cell array with model field names to which the prefix
should be added, possible values: {'rxns', 'mets',
'comps', 'genes'} (optional, by default prefixes are
added to all listed model fields will be checked).
printWarnings if warnings should be shown (optional, default true).

Output:
model modified model

Usage: model=addCOBRAstylePrefixes(model,fields,printWarnings)</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../matlabicon.gif)">
</ul>
This function is called by:
<ul style="list-style-image:url(../matlabicon.gif)">
</ul>
<!-- crossreference -->



<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function model=addCOBRAstylePrefixes(model,fields,printWarnings)</a>
0002 <span class="comment">% addCOBRAstylePrefixes</span>
0003 <span class="comment">% This function adds COBRA style identifier prefixes, which are:</span>
0004 <span class="comment">% &quot;R_&quot; for model.rxns,</span>
0005 <span class="comment">% &quot;M_&quot; for model.mets,</span>
0006 <span class="comment">% &quot;C_&quot; for model.comps;</span>
0007 <span class="comment">% &quot;G_&quot; for model.genes (and also represented in model.grRules).</span>
0008 <span class="comment">% If all entries in a field already have the prefix, then no additional</span>
0009 <span class="comment">% prefix will be added.</span>
0010 <span class="comment">%</span>
0011 <span class="comment">% Input:</span>
0012 <span class="comment">% model model whose identifiers should be modified</span>
0013 <span class="comment">% fields cell array with model field names to which the prefix</span>
0014 <span class="comment">% should be added, possible values: {'rxns', 'mets',</span>
0015 <span class="comment">% 'comps', 'genes'} (optional, by default prefixes are</span>
0016 <span class="comment">% added to all listed model fields will be checked).</span>
0017 <span class="comment">% printWarnings if warnings should be shown (optional, default true).</span>
0018 <span class="comment">%</span>
0019 <span class="comment">% Output:</span>
0020 <span class="comment">% model modified model</span>
0021 <span class="comment">%</span>
0022 <span class="comment">% Usage: model=addCOBRAstylePrefixes(model,fields,printWarnings)</span>
0023
0024 <span class="keyword">if</span> nargin&lt;2 || isempty(fields)
0025 fields = {<span class="string">'rxns'</span>,<span class="string">'mets'</span>,<span class="string">'comps'</span>,<span class="string">'genes'</span>};
0026 <span class="keyword">end</span>
0027 <span class="keyword">if</span> nargin&lt;3 || isempty(printWarnings)
0028 printWarnings = true;
0029 <span class="keyword">end</span>
0030
0031 modelFields = {<span class="string">'rxns'</span>,<span class="string">'R_'</span>;
0032 <span class="string">'mets'</span>,<span class="string">'M_'</span>;
0033 <span class="string">'comps'</span>,<span class="string">'C_'</span>;
0034 <span class="string">'genes'</span>,<span class="string">'G_'</span>};
0035
0036 toChangeIdx = find(ismember(modelFields(:,1),fields));
0037 <span class="keyword">for</span> i=1:numel(toChangeIdx)
0038 currName = modelFields{toChangeIdx(i),1};
0039 currPrefix = modelFields{toChangeIdx(i),2};
0040 <span class="keyword">if</span> isfield(model,currName)
0041 currField = model.(currName);
0042 <span class="keyword">else</span>
0043 <span class="keyword">continue</span>;
0044 <span class="keyword">end</span>
0045 <span class="keyword">if</span> ~all(startsWith(currField,currPrefix))
0046 currField = strcat(currPrefix, currField);
0047 <span class="keyword">if</span> strcmp(currName,<span class="string">'genes'</span>)
0048 model.grRules = regexprep(model.grRules, <span class="string">'(\&lt;[0-9_a-zA-Z])'</span>, <span class="string">'G_$1'</span>);
0049 model.grRules = regexprep(model.grRules, <span class="string">' G_or '</span>, <span class="string">' or '</span>);
0050 model.grRules = regexprep(model.grRules, <span class="string">' G_and '</span>, <span class="string">' and '</span>);
0051 <span class="keyword">end</span>
0052 model.(currName) = currField;
0053 <span class="keyword">elseif</span> printWarnings
0054 warning([<span class="string">'All identifiers in &quot;model.'</span> currName <span class="string">'&quot; already start '</span> <span class="keyword">...</span>
0055 <span class="string">'with &quot;'</span> currPrefix <span class="string">'&quot;, no additional prefix added.'</span>])
0056 <span class="keyword">end</span>
0057 <span class="keyword">end</span>
0058 <span class="keyword">end</span></pre></div>
<hr><address>Generated by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" title="Matlab Documentation in HTML">m2html</a></strong> &copy; 2005</address>
</body>
</html>
2 changes: 1 addition & 1 deletion doc/struct_conversion/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h1>Index for struct_conversion</h1>

<h2>Matlab files in this directory:</h2>
<table>
<tr><td><img src="../matlabicon.gif" alt="" border="">&nbsp;<a href="editMiriam.html">editMiriam</a></td><td>editMiriam </td></tr><tr><td><img src="../matlabicon.gif" alt="" border="">&nbsp;<a href="extractMiriam.html">extractMiriam</a></td><td>extractMiriam </td></tr><tr><td><img src="../matlabicon.gif" alt="" border="">&nbsp;<a href="ravenCobraWrapper.html">ravenCobraWrapper</a></td><td>ravenCobraWrapper </td></tr><tr><td><img src="../matlabicon.gif" alt="" border="">&nbsp;<a href="standardizeModelFieldOrder.html">standardizeModelFieldOrder</a></td><td>standardizeModelFieldOrder </td></tr></table>
<tr><td><img src="../matlabicon.gif" alt="" border="">&nbsp;<a href="addCOBRAstylePrefixes.html">addCOBRAstylePrefixes</a></td><td>addCOBRAstylePrefixes </td></tr><tr><td><img src="../matlabicon.gif" alt="" border="">&nbsp;<a href="editMiriam.html">editMiriam</a></td><td>editMiriam </td></tr><tr><td><img src="../matlabicon.gif" alt="" border="">&nbsp;<a href="extractMiriam.html">extractMiriam</a></td><td>extractMiriam </td></tr><tr><td><img src="../matlabicon.gif" alt="" border="">&nbsp;<a href="ravenCobraWrapper.html">ravenCobraWrapper</a></td><td>ravenCobraWrapper </td></tr><tr><td><img src="../matlabicon.gif" alt="" border="">&nbsp;<a href="removeCOBRAstylePrefixes.html">removeCOBRAstylePrefixes</a></td><td>removeCOBRAstylePrefixes </td></tr><tr><td><img src="../matlabicon.gif" alt="" border="">&nbsp;<a href="standardizeModelFieldOrder.html">standardizeModelFieldOrder</a></td><td>standardizeModelFieldOrder </td></tr></table>



Expand Down
133 changes: 133 additions & 0 deletions doc/struct_conversion/removeCOBRAstylePrefixes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Description of removeCOBRAstylePrefixes</title>
<meta name="keywords" content="removeCOBRAstylePrefixes">
<meta name="description" content="removeCOBRAstylePrefixes">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="generator" content="m2html v1.5 &copy; 2003-2005 Guillaume Flandin">
<meta name="robots" content="index, follow">
<link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../index.html">Home</a> &gt; <a href="index.html">struct_conversion</a> &gt; removeCOBRAstylePrefixes.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for struct_conversion&nbsp;<img alt=">" border="0" src="../right.png"></a></td></tr></table>-->

<h1>removeCOBRAstylePrefixes
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>removeCOBRAstylePrefixes</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function model=removeCOBRAstylePrefixes(model,fields,forceRemove) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment"> removeCOBRAstylePrefixes
This function removes COBRA style identifier prefixes, which are:
&quot;R_&quot; for model.rxns, model.rxnNames and model.id,
&quot;M_&quot; for model.mets and model.metNames,
&quot;C_&quot; for model.comps;
&quot;G_&quot; for model.genes (and also represented in model.grRules).
By default, the prefixes are only removed if all entries in a
particular field has the prefix.

Input:
model model whose identifiers should be modified
fields cell array with model field names from which the
identifiers should be removed, possible values:
'rxns', 'mets', 'comps', 'genes', 'metNames',
'rxnNames', 'id'. (optional, by default all listed
model fields will be checked).
forceRemove if prefixes should be removed even if not all entries
in a model field have the prefix (optional, default
false)

Output:
model modified model

Usage: model=removeCOBRAstylePrefixes(model,fields,forceRemove)</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../matlabicon.gif)">
</ul>
This function is called by:
<ul style="list-style-image:url(../matlabicon.gif)">
</ul>
<!-- crossreference -->



<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function model=removeCOBRAstylePrefixes(model,fields,forceRemove)</a>
0002 <span class="comment">% removeCOBRAstylePrefixes</span>
0003 <span class="comment">% This function removes COBRA style identifier prefixes, which are:</span>
0004 <span class="comment">% &quot;R_&quot; for model.rxns, model.rxnNames and model.id,</span>
0005 <span class="comment">% &quot;M_&quot; for model.mets and model.metNames,</span>
0006 <span class="comment">% &quot;C_&quot; for model.comps;</span>
0007 <span class="comment">% &quot;G_&quot; for model.genes (and also represented in model.grRules).</span>
0008 <span class="comment">% By default, the prefixes are only removed if all entries in a</span>
0009 <span class="comment">% particular field has the prefix.</span>
0010 <span class="comment">%</span>
0011 <span class="comment">% Input:</span>
0012 <span class="comment">% model model whose identifiers should be modified</span>
0013 <span class="comment">% fields cell array with model field names from which the</span>
0014 <span class="comment">% identifiers should be removed, possible values:</span>
0015 <span class="comment">% 'rxns', 'mets', 'comps', 'genes', 'metNames',</span>
0016 <span class="comment">% 'rxnNames', 'id'. (optional, by default all listed</span>
0017 <span class="comment">% model fields will be checked).</span>
0018 <span class="comment">% forceRemove if prefixes should be removed even if not all entries</span>
0019 <span class="comment">% in a model field have the prefix (optional, default</span>
0020 <span class="comment">% false)</span>
0021 <span class="comment">%</span>
0022 <span class="comment">% Output:</span>
0023 <span class="comment">% model modified model</span>
0024 <span class="comment">%</span>
0025 <span class="comment">% Usage: model=removeCOBRAstylePrefixes(model,fields,forceRemove)</span>
0026
0027 <span class="keyword">if</span> nargin&lt;2 || isempty(fields)
0028 fields = {<span class="string">'rxns'</span>,<span class="string">'mets'</span>,<span class="string">'comps'</span>,<span class="string">'genes'</span>,<span class="string">'metNames'</span>,<span class="string">'rxnNames'</span>,<span class="string">'id'</span>};
0029 <span class="keyword">end</span>
0030 <span class="keyword">if</span> nargin&lt;3 || isempty(forceRemove)
0031 forceRemove = true;
0032 <span class="keyword">end</span>
0033
0034 modelFields = {<span class="string">'rxns'</span>, <span class="string">'R_'</span>;
0035 <span class="string">'mets'</span>, <span class="string">'M_'</span>;
0036 <span class="string">'comps'</span>, <span class="string">'C_'</span>;
0037 <span class="string">'genes'</span>, <span class="string">'G_'</span>;
0038 <span class="string">'metNames'</span>, <span class="string">'M_'</span>;
0039 <span class="string">'rxnNames'</span>, <span class="string">'R_'</span>;
0040 <span class="string">'id'</span>, <span class="string">'M_'</span>};
0041
0042 toChangeIdx = find(ismember(modelFields(:,1),fields));
0043 <span class="keyword">for</span> i=1:numel(toChangeIdx)
0044 currName = modelFields{toChangeIdx(i),1};
0045 currPrefix = modelFields{toChangeIdx(i),2};
0046 currField = model.(currName);
0047
0048 <span class="keyword">if</span> forceRemove
0049 hasPrefix = true;
0050 <span class="keyword">else</span>
0051 hasPrefix = all(startsWith(currField,currPrefix));
0052 <span class="keyword">end</span>
0053 <span class="keyword">if</span> hasPrefix
0054 currField = regexprep(currField,[<span class="string">'^'</span> currPrefix],<span class="string">''</span>);
0055 <span class="keyword">if</span> strcmp(currName,<span class="string">'genes'</span>)
0056 model.grRules=regexprep(model.grRules,<span class="string">'^G_'</span>,<span class="string">''</span>);
0057 model.grRules=regexprep(model.grRules,<span class="string">'\(G_'</span>,<span class="string">'('</span>);
0058 model.grRules=regexprep(model.grRules,<span class="string">' G_'</span>,<span class="string">' '</span>);
0059 <span class="keyword">end</span>
0060 <span class="keyword">end</span>
0061 model.(currName) = currField;
0062 <span class="keyword">end</span>
0063 <span class="keyword">end</span></pre></div>
<hr><address>Generated by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" title="Matlab Documentation in HTML">m2html</a></strong> &copy; 2005</address>
</body>
</html>
16 changes: 1 addition & 15 deletions io/exportModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,7 @@ function exportModel(model,fileName,COBRAstyle,supressWarnings,sortIds)
if isfield(model,'ec')
error('ecModels cannot be exported with COBRAstyle flag, manually correct any non-compliant identifiers if needed.')
end
if ~all(startsWith(model.rxns,'R_'))
model.rxns = strcat('R_',model.rxns);
end
if ~all(startsWith(model.mets,'M_'))
model.mets = strcat('M_',model.mets);
end
if ~all(startsWith(model.comps,'C_'))
model.comps = strcat('C_',model.comps);
end
if isfield(model,'genes') && ~all(startsWith(model.genes,'G_'))
model.genes = strcat('G_',model.genes);
model.grRules = regexprep(model.grRules, '(\<[0-9_a-zA-Z])', 'G_$1');
model.grRules = regexprep(model.grRules, ' G_or ', ' or ');
model.grRules = regexprep(model.grRules, ' G_and ', ' and ');
end
model = addCOBRAstylePrefixes(model,[],false);
end

%Check the model structure
Expand Down
Loading

0 comments on commit 792d982

Please sign in to comment.