Parsing undefined variables in the Simbiology toolbox

The short version is that I have a model with 800+ reactions in it, plus a lot of rules, parameters, etc. and the first 85% was built by someone else completely through the GUI.
Right now there are a lot of undefined variables in the rules, and I need to define them as parameters. It's painfully tedious through the GUI, but I can't figure out how to parse the undefined variable names out of the model so that I can use a script to run addparameter() to define them.
This is my first time using a toolbox and the code capture tool has been helpful, however in this particular instance if I update the undefined variable from the GUI menu, it only displays addparameter() with the name string already included, as well as set() to update the rule.
This is what I'm thinking right now:
%Loop through rules
for i = low:high
% Parse undefined variables from m1.Rules(i)
name_string = ??
p = addparameter(m1, 'name_string', 1.0);
% Assign variable name to object.
rule1 = m1.Rules(i);
% Configure properties.
set(rule1, 'Rule', 'equation' );
end
Any help would be appreciated, either advice on my approach or a different one all together.
Thanks for reading.
-Adam
Edit: I have made some progress and there is a solution in sight.
Here is my current code.
% Load SimBiology project.
out = sbioloadproject();
m1 = out.m1;
r1 = get(m1.Rules, 'Rule');
n_r = length(r1);
for i = 1:n_r
pName = get(m1.Parameters, 'Name');
cr1 = char(r1(i))
v1 = regexp(cr1,'\w*','match');
sz = length(v1);
for j = 1:sz
% compare elements in v1 to elements in pName
% use addparameter() for unfound variable names
end
end
Any advice on comparing the character names is appreciated.

 Accepted Answer

Hi Adam,
I think you're taking a reasonable enough approach, but there are several additional things you need to consider:
  1. Do your rules ever refer to species or compartments? If so, you want to make sure you don't add a parameter for a variable that should really refer to a species or compartment.
  2. Are all the names in your model valid MATLAB variable names? SimBiology supports names that contain spaces, but you must put them in brackets. If you need to consider this case, you will need to generalize your regular expression.
  3. Do any of the rules use the keyword "time"? This refers to the simulation time, so you should not (and in fact cannot) add a parameter named "time."
  4. Do any of the rules make use of MATLAB functions? If you have a rule that references the function "exp", you don't want to add a parameter with this name.
In my proposed solution below, I try to account for all of the above issues. In the case of names that match functions, I display a message with the name but do not add the parameter. You will probably need to review these names one by one to decide.
allRuleText = get(m1.Rules, 'Rule');
numRules = length(allRuleText);
for i = 1:numRules
pName = get(m1.Parameters, 'Name');
ruleText = allRuleText{i};
varsInRules = regexp(ruleText, '(?<=\[)[^\[\]]*(?=\])|\w*', 'match');
allObjects = sbioselect(m1, 'Type', {'species', 'compartment', 'parameter'});
allNames = [get(allObjects, 'Name'); 'time'];
missingVars = setdiff(varsInRules, allNames);
numMissingVars = length(missingVars);
for j = 1:numMissingVars
varName = missingVars{j};
if ~isempty(which(varName))
['Skipping name that matches a function: ' varName]
else
m1.addparameter(varName);
end
end
end

1 Comment

Thank you! It works perfectly. I had run into some of those problems and my comparison was not nearly as clean.

Sign in to comment.

More Answers (0)

Communities

More Answers in the  SimBiology Community

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!