Skip to content

Commit

Permalink
feat: alternative path of DLKcat in/output
Browse files Browse the repository at this point in the history
  • Loading branch information
edkerk committed Oct 13, 2024
1 parent d461d3a commit 7569a81
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 274 deletions.
148 changes: 73 additions & 75 deletions doc/src/geckomat/gather_kcats/readDLKcatOutput.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="

Input:
model an ecModel in GECKO 3 format (with ecModel.ec structure)
outFile name and path of the DLKcat output file. If nothing is
provided, an attempt will be made to read
data/DLKcat.tsv from the obj.params.path folder
specified in the modelAdapter.
outFile name and path of the DLKcat output file. (Optional,
default is data/DLKcat.tsv from the obj.params.path
folder specified in the modelAdapter)
modelAdapter a loaded model adapter (Optional, will otherwise use the
default model adapter).

Expand Down Expand Up @@ -74,77 +73,76 @@ <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" sr
0006 <span class="comment">%</span>
0007 <span class="comment">% Input:</span>
0008 <span class="comment">% model an ecModel in GECKO 3 format (with ecModel.ec structure)</span>
0009 <span class="comment">% outFile name and path of the DLKcat output file. If nothing is</span>
0010 <span class="comment">% provided, an attempt will be made to read</span>
0011 <span class="comment">% data/DLKcat.tsv from the obj.params.path folder</span>
0012 <span class="comment">% specified in the modelAdapter.</span>
0013 <span class="comment">% modelAdapter a loaded model adapter (Optional, will otherwise use the</span>
0014 <span class="comment">% default model adapter).</span>
0015 <span class="comment">%</span>
0016 <span class="comment">% Output:</span>
0017 <span class="comment">% kcatList structure array with list of DLKcat derived kcat values,</span>
0018 <span class="comment">% with separate entries for each kcat value</span>
0019 <span class="comment">% source 'DLKcat'</span>
0020 <span class="comment">% rxns reaction identifiers</span>
0021 <span class="comment">% genes gene identifiers</span>
0022 <span class="comment">% substrate substrate names</span>
0023 <span class="comment">% kcat predicted kcat value in /sec</span>
0024 <span class="comment">%</span>
0025 <span class="comment">% Usage:</span>
0026 <span class="comment">% kcatList = readDLKcatOutput(model, outFile, modelAdapter)</span>
0027
0028 <span class="keyword">if</span> nargin &lt; 3 || isempty(modelAdapter)
0029 modelAdapter = ModelAdapterManager.getDefault();
0030 <span class="keyword">if</span> isempty(modelAdapter)
0031 error(<span class="string">'Either send in a modelAdapter or set the default model adapter in the ModelAdapterManager.'</span>)
0032 <span class="keyword">end</span>
0033 <span class="keyword">end</span>
0034 params = modelAdapter.params;
0035
0036 <span class="keyword">if</span> nargin&lt;2 || isempty(outFile)
0037 fID = fopen(fullfile(params.path,<span class="string">'data'</span>,<span class="string">'DLKcat.tsv'</span>),<span class="string">'r'</span>);
0038 <span class="keyword">else</span>
0039 fID = fopen(outFile);
0040 <span class="keyword">end</span>
0041 DLKcatOutput = textscan(fID,<span class="string">'%s %s %s %s %s %s'</span>,<span class="string">'Delimiter'</span>,<span class="string">'\t'</span>,<span class="string">'HeaderLines'</span>,1);
0042 fclose(fID);
0043
0044 <span class="comment">% Check that DLKcat output file and model match (not fool proof, but good enough)</span>
0045 [rxns, genes, subs, kcats] = deal(DLKcatOutput{[1,2,3,6]});
0046
0047 <span class="comment">% Check if it contains any kcat values</span>
0048 <span class="keyword">if</span> all(cellfun(@isempty,kcats)) || all(strcmpi(kcats,<span class="string">'NA'</span>))
0049 error(<span class="string">'DLKcat file does not contain any kcat values, please run runDLKcat() first.'</span>)
0050 <span class="keyword">end</span>
0051
0052 <span class="comment">% Check that all substrates are in the model</span>
0053 matchMets = ismember(subs,model.metNames);
0054 <span class="keyword">if</span> ~all(matchMets)
0055 errorText = <span class="string">'DLKcat was likely run with an input file that was generated from another ecModel, as the following substrates from DLKcat output cannot be found in model.metNames:'</span>;
0056 dispEM(errorText,true,subs(~matchMets),false)
0057 <span class="keyword">end</span>
0058
0059 <span class="comment">% Check that all reactions are in model.ec.rxns</span>
0060 matchRxns = ismember(rxns,model.ec.rxns);
0061 <span class="keyword">if</span> ~all(matchRxns)
0062 errorText = <span class="string">'DLKcat was likely run with an input file that was generated from another ecModel, as the following reactions from DLKcat output cannot be found in model.metNames:'</span>;
0063 dispEM(errorText,true,rxns(~matchRxns),false)
0064 <span class="keyword">end</span>
0065
0066 <span class="comment">% Filter out entries with no numeric value</span>
0067 noOutput = cellfun(@isempty,regexpi(kcats,<span class="string">'[0-9]'</span>));
0068 kcats = str2double(kcats(~noOutput));
0069 rxns(noOutput) = [];
0070 genes(noOutput) = [];
0071 subs(noOutput) = [];
0072
0073 <span class="comment">% Make kcatList structure</span>
0074 kcatList.source = <span class="string">'DLKcat'</span>;
0075 kcatList.rxns = rxns;
0076 kcatList.genes = genes;
0077 kcatList.substrates = subs;
0078 kcatList.kcats = kcats;
0079 <span class="keyword">end</span></pre></div>
0009 <span class="comment">% outFile name and path of the DLKcat output file. (Optional,</span>
0010 <span class="comment">% default is data/DLKcat.tsv from the obj.params.path</span>
0011 <span class="comment">% folder specified in the modelAdapter)</span>
0012 <span class="comment">% modelAdapter a loaded model adapter (Optional, will otherwise use the</span>
0013 <span class="comment">% default model adapter).</span>
0014 <span class="comment">%</span>
0015 <span class="comment">% Output:</span>
0016 <span class="comment">% kcatList structure array with list of DLKcat derived kcat values,</span>
0017 <span class="comment">% with separate entries for each kcat value</span>
0018 <span class="comment">% source 'DLKcat'</span>
0019 <span class="comment">% rxns reaction identifiers</span>
0020 <span class="comment">% genes gene identifiers</span>
0021 <span class="comment">% substrate substrate names</span>
0022 <span class="comment">% kcat predicted kcat value in /sec</span>
0023 <span class="comment">%</span>
0024 <span class="comment">% Usage:</span>
0025 <span class="comment">% kcatList = readDLKcatOutput(model, outFile, modelAdapter)</span>
0026
0027 <span class="keyword">if</span> nargin &lt; 3 || isempty(modelAdapter)
0028 modelAdapter = ModelAdapterManager.getDefault();
0029 <span class="keyword">if</span> isempty(modelAdapter)
0030 error(<span class="string">'Either send in a modelAdapter or set the default model adapter in the ModelAdapterManager.'</span>)
0031 <span class="keyword">end</span>
0032 <span class="keyword">end</span>
0033 params = modelAdapter.params;
0034
0035 <span class="keyword">if</span> nargin&lt;2 || isempty(outFile)
0036 fID = fopen(fullfile(params.path,<span class="string">'data'</span>,<span class="string">'DLKcat.tsv'</span>),<span class="string">'r'</span>);
0037 <span class="keyword">else</span>
0038 fID = fopen(outFile);
0039 <span class="keyword">end</span>
0040 DLKcatOutput = textscan(fID,<span class="string">'%s %s %s %s %s %s'</span>,<span class="string">'Delimiter'</span>,<span class="string">'\t'</span>,<span class="string">'HeaderLines'</span>,1);
0041 fclose(fID);
0042
0043 <span class="comment">% Check that DLKcat output file and model match (not fool proof, but good enough)</span>
0044 [rxns, genes, subs, kcats] = deal(DLKcatOutput{[1,2,3,6]});
0045
0046 <span class="comment">% Check if it contains any kcat values</span>
0047 <span class="keyword">if</span> all(cellfun(@isempty,kcats)) || all(strcmpi(kcats,<span class="string">'NA'</span>))
0048 error(<span class="string">'DLKcat file does not contain any kcat values, please run runDLKcat() first.'</span>)
0049 <span class="keyword">end</span>
0050
0051 <span class="comment">% Check that all substrates are in the model</span>
0052 matchMets = ismember(subs,model.metNames);
0053 <span class="keyword">if</span> ~all(matchMets)
0054 errorText = <span class="string">'DLKcat was likely run with an input file that was generated from another ecModel, as the following substrates from DLKcat output cannot be found in model.metNames:'</span>;
0055 dispEM(errorText,true,subs(~matchMets),false)
0056 <span class="keyword">end</span>
0057
0058 <span class="comment">% Check that all reactions are in model.ec.rxns</span>
0059 matchRxns = ismember(rxns,model.ec.rxns);
0060 <span class="keyword">if</span> ~all(matchRxns)
0061 errorText = <span class="string">'DLKcat was likely run with an input file that was generated from another ecModel, as the following reactions from DLKcat output cannot be found in model.metNames:'</span>;
0062 dispEM(errorText,true,rxns(~matchRxns),false)
0063 <span class="keyword">end</span>
0064
0065 <span class="comment">% Filter out entries with no numeric value</span>
0066 noOutput = cellfun(@isempty,regexpi(kcats,<span class="string">'[0-9]'</span>));
0067 kcats = str2double(kcats(~noOutput));
0068 rxns(noOutput) = [];
0069 genes(noOutput) = [];
0070 subs(noOutput) = [];
0071
0072 <span class="comment">% Make kcatList structure</span>
0073 kcatList.source = <span class="string">'DLKcat'</span>;
0074 kcatList.rxns = rxns;
0075 kcatList.genes = genes;
0076 kcatList.substrates = subs;
0077 kcatList.kcats = kcats;
0078 <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>
93 changes: 53 additions & 40 deletions doc/src/geckomat/gather_kcats/runDLKcat.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../
<div class="box"><strong>runDLKcat</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 runDLKcat(modelAdapter) </strong></div>
<div class="box"><strong>function runDLKcat(modelAdapter,filePath) </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"> runDLKcat
Expand All @@ -36,6 +36,8 @@ <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="
Input
modelAdapter a loaded model adapter. (Optional, will otherwise use
the default model adapter)
filePath path to the DLKcat.tsv file. (Optional, will otherwise
assume data/DLKcat.tsv)

NOTE: 1. Requires Docker to be installed, and Docker Desktop running. Visit &quot;https://www.docker.com&quot;
2. Runtime will depend on whether the image is to be downloaded or not.</pre></div>
Expand All @@ -53,7 +55,7 @@ <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^


<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 runDLKcat(modelAdapter)</a>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function runDLKcat(modelAdapter,filePath)</a>
0002 <span class="comment">% runDLKcat</span>
0003 <span class="comment">% Runs DLKcat to predict kcat values from a Docker image. Once DLKcat is succesfully</span>
0004 <span class="comment">% run, the DLKcatFile will be overwritten with the DLKcat</span>
Expand All @@ -63,46 +65,57 @@ <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" sr
0008 <span class="comment">% Input</span>
0009 <span class="comment">% modelAdapter a loaded model adapter. (Optional, will otherwise use</span>
0010 <span class="comment">% the default model adapter)</span>
0011 <span class="comment">%</span>
0012 <span class="comment">% NOTE: 1. Requires Docker to be installed, and Docker Desktop running. Visit &quot;https://www.docker.com&quot;</span>
0013 <span class="comment">% 2. Runtime will depend on whether the image is to be downloaded or not.</span>
0014
0015 <span class="keyword">if</span> nargin &lt; 1 || isempty(modelAdapter)
0016 modelAdapter = ModelAdapterManager.getDefault();
0017 <span class="keyword">if</span> isempty(modelAdapter)
0018 error(<span class="string">'Either send in a modelAdapter or set the default model adapter in the ModelAdapterManager.'</span>)
0019 <span class="keyword">end</span>
0020 <span class="keyword">end</span>
0021
0022 params = modelAdapter.params;
0023 <span class="comment">% Make sure path is full, not relative</span>
0024 [~, params.path] = fileattrib(params.path);
0025 params.path=params.path.Name;
0026
0027 <span class="comment">%% Check and install requirements</span>
0028 <span class="comment">% On macOS, Docker might not be properly loaded if MATLAB is started via</span>
0029 <span class="comment">% launcher and not terminal.</span>
0030 <span class="keyword">if</span> ismac
0031 setenv(<span class="string">'PATH'</span>, strcat(<span class="string">'/usr/local/bin'</span>, <span class="string">':'</span>, getenv(&quot;PATH&quot;)));
0011 <span class="comment">% filePath path to the DLKcat.tsv file. (Optional, will otherwise</span>
0012 <span class="comment">% assume data/DLKcat.tsv)</span>
0013 <span class="comment">%</span>
0014 <span class="comment">% NOTE: 1. Requires Docker to be installed, and Docker Desktop running. Visit &quot;https://www.docker.com&quot;</span>
0015 <span class="comment">% 2. Runtime will depend on whether the image is to be downloaded or not.</span>
0016
0017 <span class="keyword">if</span> nargin &lt; 1 || isempty(modelAdapter)
0018 modelAdapter = ModelAdapterManager.getDefault();
0019 <span class="keyword">if</span> isempty(modelAdapter)
0020 error(<span class="string">'Either send in a modelAdapter or set the default model adapter in the ModelAdapterManager.'</span>)
0021 <span class="keyword">end</span>
0022 <span class="keyword">end</span>
0023 params = modelAdapter.params;
0024 <span class="comment">% Make sure path is full, not relative</span>
0025 [~, params.path] = fileattrib(params.path);
0026 params.path=params.path.Name;
0027
0028 <span class="keyword">if</span> nargin &lt; 2 || isempty(filePath)
0029 filePath = fullfile(params.path,<span class="string">'data'</span>,<span class="string">'DLKcat.tsv'</span>);
0030 <span class="keyword">elseif</span> strcmp(filePath(end),{<span class="string">'\'</span>,<span class="string">'/'</span>})
0031 filePath = fullfile(filePath,<span class="string">'DLKcat.tsv'</span>);
0032 <span class="keyword">end</span>
0033
0034 <span class="comment">% Check if Docker is installed</span>
0035 [checks.docker.status, checks.docker.out] = system(<span class="string">'docker --version'</span>);
0036 <span class="keyword">if</span> checks.docker.status ~= 0
0037 error(<span class="string">'Cannot find Docker, make sure it is installed. If it is, it might be required to start Matlab from the command-line instead of the launcher in order for Docker to be detected and used.'</span>)
0038 <span class="keyword">end</span>
0039
0040 disp(<span class="string">'Running DLKcat prediction, this may take many minutes, especially the first time.'</span>)
0041 status = system([<span class="string">'docker run --rm -v &quot;'</span> fullfile(params.path,<span class="string">'/data'</span>) <span class="string">'&quot;:/data ghcr.io/sysbiochalmers/dlkcat-gecko:0.1 /bin/bash -c &quot;python DLKcat.py /data/DLKcat.tsv /data/DLKcatOutput.tsv&quot;'</span>]);
0042
0043 <span class="keyword">if</span> status == 0 &amp;&amp; exist(fullfile(params.path,<span class="string">'data/DLKcatOutput.tsv'</span>))
0044 delete(fullfile(params.path,<span class="string">'/data/DLKcat.tsv'</span>));
0045 movefile(fullfile(params.path,<span class="string">'/data/DLKcatOutput.tsv'</span>), fullfile(params.path,<span class="string">'/data/DLKcat.tsv'</span>));
0046 disp(<span class="string">'DKLcat prediction completed.'</span>);
0047 <span class="keyword">else</span>
0048 error(<span class="string">'DLKcat encountered an error or it did not create any output file.'</span>)
0033 filePath = checkFileExistence(filePath,1);
0034
0035 copyfile(filePath, fullfile(params.path,<span class="string">'data'</span>,<span class="string">'tempDLKcat.tsv'</span>));
0036
0037
0038 <span class="comment">%% Check and install requirements</span>
0039 <span class="comment">% On macOS, Docker might not be properly loaded if MATLAB is started via</span>
0040 <span class="comment">% launcher and not terminal.</span>
0041 <span class="keyword">if</span> ismac
0042 setenv(<span class="string">'PATH'</span>, strcat(<span class="string">'/usr/local/bin'</span>, <span class="string">':'</span>, getenv(&quot;PATH&quot;)));
0043 <span class="keyword">end</span>
0044
0045 <span class="comment">% Check if Docker is installed</span>
0046 [checks.docker.status, checks.docker.out] = system(<span class="string">'docker --version'</span>);
0047 <span class="keyword">if</span> checks.docker.status ~= 0
0048 error(<span class="string">'Cannot find Docker, make sure it is installed. If it is, it might be required to start Matlab from the command-line instead of the launcher in order for Docker to be detected and used.'</span>)
0049 <span class="keyword">end</span>
0050</pre></div>
0050
0051 disp(<span class="string">'Running DLKcat prediction, this may take many minutes, especially the first time.'</span>)
0052 status = system([<span class="string">'docker run --rm -v &quot;'</span> fullfile(params.path,<span class="string">'/data'</span>) <span class="string">'&quot;:/data ghcr.io/sysbiochalmers/dlkcat-gecko:0.1 /bin/bash -c &quot;python DLKcat.py /data/tempDLKcat.tsv /data/tempDLKcatOutput.tsv&quot;'</span>]);
0053 delete(fullfile(params.path,<span class="string">'/data/tempDLKcat.tsv'</span>));
0054
0055 <span class="keyword">if</span> status == 0 &amp;&amp; exist(fullfile(params.path,<span class="string">'data/tempDLKcatOutput.tsv'</span>))
0056 movefile(fullfile(params.path,<span class="string">'/data/tempDLKcatOutput.tsv'</span>), filePath);
0057 disp(<span class="string">'DKLcat prediction completed.'</span>);
0058 <span class="keyword">else</span>
0059 error(<span class="string">'DLKcat encountered an error or it did not create any output file.'</span>)
0060 <span class="keyword">end</span>
0061</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>
Loading

0 comments on commit 7569a81

Please sign in to comment.