cobratoolbox icon indicating copy to clipboard operation
cobratoolbox copied to clipboard

bug in printRxnFormula.m

Open handancetin opened this issue 5 years ago • 1 comments

When using printRxnFormula, the function prints wrong genes without replacing and/or operators. Unfortunately, testPrintRxnFormula.m bypasses this bug because of the use of model.grRules instead of model.rules.

You can reproduce the problem by using the code below (the first reaction is chosen arbitrarily) :

>> load('ecoli_core_model.mat');
>> model = rmfield(model, 'grRules'); % removed to use model.rules;

>> printRxnFormula(model, 'rxnAbbrList', model.rxns(1), 'gprFlag', true);
ACALD acald[c] + coa[c] + nad[c]  <=> accoa[c] + h[c] + nadh[c]  (b1817) | (b1773)

>> model.rules{1}
ans =  '(x(6)) | (x(35))'

>> model.genes([6 35])'
ans =  {'b0351'}    {'b1241'}

I believe the problem is caused by the lines 190-192, that is:

rule = regexprep(model.rules{rxnID},'|','or');
rule = regexprep(rule,'&','and');
rule = regexprep(rule,'x\((?<id>[0-9]+)\)','${model.genes{num2str($1)}}');

Escapes are required for the expressions in the first two lines, so, changing | to\|, and & to \& solves this problem in my case. For the wrong gene name output, since num2str() converts numeric values to decimal representations, the function returns model.genes{49} instead of model.genes{1}.

>> model.genes{1}
ans =  'b0008'

>> model.genes{'1'}
ans =  'b1723'

>> model.genes{49}
ans =  'b1723

Using str2num instead of num2str fixes the problem.

To sum up, possible solution is to replace the lines 190-192 with the following:

rule = regexprep(model.rules{rxnID},'\|','or');
rule = regexprep(rule,'\&','and');
rule = regexprep(rule,'x\((?<id>[0-9]+)\)','${model.genes{str2num($1)}}');

Note that the same behavior exists in the proteinFlag section as well, lines 198-200. It is better to check all the uses of num2str() when indexing, in the whole toolbox.

Additionally, I'd like to mention that I am not very familiar with the uses of regular expression functions, my solutions might not be the correct solutions.

Hope this helps, Handan

  • [X] Tried to solve the issue on my own
  • [X] Retried to run my code with the latest version of The COBRA Toolbox
  • [X] Checked that a similar issue has not already been opened

handancetin avatar Jan 16 '20 18:01 handancetin

@HnCetin, thanks for this. Can you open a pull request with your suggestion? Guidelines how to contribute are here.

laurentheirendt avatar Feb 06 '20 05:02 laurentheirendt