use of cbit.vcell.mapping.ParameterContext.LocalProxyParameter in project vcell by virtualcell.
the class ElectricalStimulusPanel method getProtocolParameterExprPreview.
public static Expression getProtocolParameterExprPreview(Expression expr, SymbolTable symbolTable, ReservedSymbol timeSymbol) throws Exception {
Expression protocolParameterExp = new Expression(expr);
protocolParameterExp = MathUtilities.substituteModelParameters(protocolParameterExp, symbolTable);
String[] symbols = protocolParameterExp.getSymbols();
for (int i = 0; symbols != null && i < symbols.length; i++) {
SymbolTableEntry ste = protocolParameterExp.getSymbolBinding(symbols[i]);
if (!ste.equals(timeSymbol) && !(ste instanceof LocalProxyParameter && ((LocalProxyParameter) ste).getTarget().equals(timeSymbol))) {
throw new ExpressionException("Expression symbol '" + ste.getName() + "' doesn't match timeSymbol " + timeSymbol.getName());
}
}
return protocolParameterExp;
}
use of cbit.vcell.mapping.ParameterContext.LocalProxyParameter in project vcell by virtualcell.
the class ReactionRulePropertiesTableModel method isCellEditable.
/**
* Insert the method's description here.
* Creation date: (2/24/01 12:27:46 AM)
* @return boolean
* @param rowIndex int
* @param columnIndex int
*/
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (!bEditable) {
return false;
}
Object o = getValueAt(rowIndex);
if (!(o instanceof Parameter)) {
return false;
}
Parameter parameter = (Parameter) o;
if (reactionRule != null && reactionRule.getKineticLaw().getLocalParameter(RbmKineticLawParameterType.MassActionReverseRate) == parameter) {
if (!reactionRule.isReversible()) {
// disable Kr if rule is not reversible
return false;
}
}
switch(columnIndex) {
case COLUMN_NAME:
return parameter.isNameEditable();
case COLUMN_DESCRIPTION:
return false;
case COLUMN_IS_GLOBAL:
// if the parameter is reaction rate param or a ReservedSymbol in the model, it should not be editable
if ((parameter instanceof LocalParameter) && (((LocalParameter) parameter).getRole() != RbmKineticLaw.RbmKineticLawParameterType.UserDefined)) {
return false;
}
if (parameter instanceof UnresolvedParameter) {
return false;
}
if (parameter instanceof LocalProxyParameter) {
LocalProxyParameter kpp = (LocalProxyParameter) parameter;
SymbolTableEntry ste = kpp.getTarget();
if ((ste instanceof Model.ReservedSymbol) || (ste instanceof SpeciesContext) || (ste instanceof ModelQuantity)) {
return false;
}
}
return true;
case COLUMN_VALUE:
return parameter.isExpressionEditable();
case COLUMN_UNITS:
return parameter.isUnitEditable();
}
return false;
}
use of cbit.vcell.mapping.ParameterContext.LocalProxyParameter in project vcell by virtualcell.
the class ReactionRulePropertiesTableModel method setValueAt.
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Object o = getValueAt(rowIndex);
if (!(o instanceof Parameter)) {
return;
}
Parameter parameter = (Parameter) o;
// try {
switch(columnIndex) {
case COLUMN_NAME:
{
try {
if (aValue instanceof String) {
String newName = (String) aValue;
if (!parameter.getName().equals(newName)) {
if (parameter instanceof LocalParameter) {
reactionRule.getKineticLaw().renameParameter(parameter.getName(), newName);
} else if (parameter instanceof LocalProxyParameter) {
parameter.setName(newName);
}
fireTableRowsUpdated(rowIndex, rowIndex);
}
}
} catch (ExpressionException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter name:\n" + e.getMessage());
} catch (PropertyVetoException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter name:\n" + e.getMessage());
}
break;
}
case COLUMN_IS_GLOBAL:
{
if (aValue.equals(Boolean.FALSE)) {
// check box has been <unset> (<true> to <false>) : change param from global to local
if ((parameter instanceof LocalProxyParameter) && ((((LocalProxyParameter) parameter).getTarget() instanceof Model.ReservedSymbol) || (((LocalProxyParameter) parameter).getTarget() instanceof SpeciesContext) || (((LocalProxyParameter) parameter).getTarget() instanceof ModelQuantity))) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter : \'" + parameter.getName() + "\' is a " + ((LocalProxyParameter) parameter).getTarget().getClass() + " in the model; cannot convert it to a local kinetic parameter.");
} else {
try {
reactionRule.getKineticLaw().convertParameterType(parameter, false);
} catch (PropertyVetoException pve) {
pve.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Unable to convert parameter : \'" + parameter.getName() + "\' to local kinetics parameter : " + pve.getMessage());
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Unable to convert parameter : \'" + parameter.getName() + "\' to local kinetics parameter : " + e.getMessage());
}
}
} else {
// check box has been <set> (<false> to <true>) : change param from local to global
if ((parameter instanceof LocalParameter) && (((LocalParameter) parameter).getRole() != RbmKineticLaw.RbmKineticLawParameterType.UserDefined)) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter : \'" + parameter.getName() + "\' is a pre-defined kinetics parameter (not user-defined); cannot convert it to a model level (global) parameter.");
} else {
ModelParameter mp = reactionRule.getModel().getModelParameter(parameter.getName());
// model already had the model parameter 'param', but check if 'param' value is different from
// model parameter with same name. If it is, the local value will be overridden by global (model) param
// value, and user should be warned.
String choice = "Ok";
if (mp != null && !(mp.getExpression().compareEqual(parameter.getExpression()))) {
String msgStr = "Model already has a global parameter named : \'" + parameter.getName() + "\'; with value = \'" + mp.getExpression().infix() + "\'; This local parameter \'" + parameter.getName() + "\' with value = \'" + parameter.getExpression().infix() + "\' will be overridden by the global value. \nPress \'Ok' to override " + "local value with global value of \'" + parameter.getName() + "\'. \nPress \'Cancel\' to retain new local value.";
choice = PopupGenerator.showWarningDialog(ownerTable, msgStr, new String[] { "Ok", "Cancel" }, "Ok");
}
if (choice.equals("Ok")) {
try {
// Now 'parameter' is a local kinetic parameter. If it is not numeric, and if its expression
// contains other local kinetic parameters, warn user that 'parameter' cannot be promoted because
// of its expression containing other local parameters.
boolean bPromoteable = true;
if (!parameter.getExpression().isNumeric()) {
String[] symbols = parameter.getExpression().getSymbols();
for (int i = 0; i < symbols.length; i++) {
if (reactionRule.getKineticLaw().getLocalParameter(symbols[i]) != null) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter \'" + parameter.getName() + "\' contains other local kinetic parameters; Cannot convert it to global until the referenced parameters are global.");
bPromoteable = false;
}
}
}
if (bPromoteable) {
reactionRule.getKineticLaw().convertParameterType(parameter, true);
}
} catch (PropertyVetoException pve) {
pve.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Cannot convert parameter \'" + parameter.getName() + "\' to global parameter : " + pve.getMessage());
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Cannot convert parameter \'" + parameter.getName() + "\' to global parameter : " + e.getMessage());
}
}
}
}
fireTableRowsUpdated(rowIndex, rowIndex);
break;
}
case COLUMN_VALUE:
{
try {
if (aValue instanceof ScopedExpression) {
// }
throw new RuntimeException("unexpected value type ScopedExpression");
} else if (aValue instanceof String) {
String newExpressionString = (String) aValue;
if (parameter instanceof LocalParameter) {
LocalParameter localParameter = (LocalParameter) parameter;
reactionRule.getKineticLaw().setParameterValue(localParameter, new Expression(newExpressionString), true);
} else if (parameter instanceof LocalProxyParameter) {
parameter.setExpression(new Expression(newExpressionString));
}
}
reactionRule.getKineticLaw().resolveUndefinedUnits();
fireTableRowsUpdated(rowIndex, rowIndex);
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error:\n" + e.getMessage());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Expression error:\n" + e.getMessage());
}
break;
}
case COLUMN_UNITS:
{
try {
if (aValue instanceof String && parameter instanceof LocalParameter && ((LocalParameter) parameter).getRole() == RbmKineticLaw.RbmKineticLawParameterType.UserDefined) {
String newUnitString = (String) aValue;
LocalParameter kineticsParm = (LocalParameter) parameter;
ModelUnitSystem modelUnitSystem = reactionRule.getModel().getUnitSystem();
if (!kineticsParm.getUnitDefinition().getSymbol().equals(newUnitString)) {
kineticsParm.setUnitDefinition(modelUnitSystem.getInstance(newUnitString));
reactionRule.getKineticLaw().resolveUndefinedUnits();
fireTableRowsUpdated(rowIndex, rowIndex);
}
}
} catch (VCUnitException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter unit:\n" + e.getMessage());
}
break;
}
}
// }catch (java.beans.PropertyVetoException e){
// e.printStackTrace(System.out);
// }
}
use of cbit.vcell.mapping.ParameterContext.LocalProxyParameter in project vcell by virtualcell.
the class RulebasedTransformer method transform.
private void transform(SimulationContext originalSimContext, SimulationContext transformedSimulationContext, ArrayList<ModelEntityMapping> entityMappings, MathMappingCallback mathMappingCallback) throws PropertyVetoException {
Model newModel = transformedSimulationContext.getModel();
Model originalModel = originalSimContext.getModel();
ModelEntityMapping em = null;
// list of rules created from the reactions; we apply the symmetry factor computed by bionetgen only to these
Set<ReactionRule> fromReactions = new HashSet<>();
for (SpeciesContext newSpeciesContext : newModel.getSpeciesContexts()) {
final SpeciesContext originalSpeciesContext = originalModel.getSpeciesContext(newSpeciesContext.getName());
// map new and old species contexts
em = new ModelEntityMapping(originalSpeciesContext, newSpeciesContext);
entityMappings.add(em);
if (newSpeciesContext.hasSpeciesPattern()) {
// it's perfect already and can't be improved
continue;
}
try {
MolecularType newmt = newModel.getRbmModelContainer().createMolecularType();
newModel.getRbmModelContainer().addMolecularType(newmt, false);
MolecularTypePattern newmtp_sc = new MolecularTypePattern(newmt);
SpeciesPattern newsp_sc = new SpeciesPattern();
newsp_sc.addMolecularTypePattern(newmtp_sc);
newSpeciesContext.setSpeciesPattern(newsp_sc);
RbmObservable newo = new RbmObservable(newModel, "O0_" + newmt.getName() + "_tot", newSpeciesContext.getStructure(), RbmObservable.ObservableType.Molecules);
MolecularTypePattern newmtp_ob = new MolecularTypePattern(newmt);
SpeciesPattern newsp_ob = new SpeciesPattern();
newsp_ob.addMolecularTypePattern(newmtp_ob);
newo.addSpeciesPattern(newsp_ob);
newModel.getRbmModelContainer().addObservable(newo);
// map new observable to old species context
em = new ModelEntityMapping(originalSpeciesContext, newo);
entityMappings.add(em);
} catch (ModelException e) {
e.printStackTrace();
throw new RuntimeException("unable to transform species context: " + e.getMessage());
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ReactionSpec[] reactionSpecs = transformedSimulationContext.getReactionContext().getReactionSpecs();
for (ReactionSpec reactionSpec : reactionSpecs) {
if (reactionSpec.isExcluded()) {
// we create rules only from those reactions which are not excluded
continue;
}
ReactionStep rs = reactionSpec.getReactionStep();
String name = rs.getName();
String mangled = TokenMangler.fixTokenStrict(name);
mangled = newModel.getReactionName(mangled);
Kinetics k = rs.getKinetics();
if (!(k instanceof MassActionKinetics)) {
throw new RuntimeException("Only Mass Action Kinetics supported at this time, reaction \"" + rs.getName() + "\" uses kinetic law type \"" + rs.getKinetics().getName() + "\"");
}
boolean bReversible = rs.isReversible();
ReactionRule rr = new ReactionRule(newModel, mangled, rs.getStructure(), bReversible);
fromReactions.add(rr);
MassActionKinetics massActionKinetics = (MassActionKinetics) k;
List<Reactant> rList = rs.getReactants();
List<Product> pList = rs.getProducts();
// counting the stoichiometry - 2A+B means 3 reactants
int numReactants = 0;
for (Reactant r : rList) {
numReactants += r.getStoichiometry();
if (numReactants > 2) {
String message = "NFSim doesn't support more than 2 reactants within a reaction: " + name;
throw new RuntimeException(message);
}
}
int numProducts = 0;
for (Product p : pList) {
numProducts += p.getStoichiometry();
if (bReversible && numProducts > 2) {
String message = "NFSim doesn't support more than 2 products within a reversible reaction: " + name;
throw new RuntimeException(message);
}
}
RateLawType rateLawType = RateLawType.MassAction;
RbmKineticLaw kineticLaw = new RbmKineticLaw(rr, rateLawType);
try {
String forwardRateName = massActionKinetics.getForwardRateParameter().getName();
Expression forwardRateExp = massActionKinetics.getForwardRateParameter().getExpression();
String reverseRateName = massActionKinetics.getReverseRateParameter().getName();
Expression reverseRateExp = massActionKinetics.getReverseRateParameter().getExpression();
LocalParameter fR = kineticLaw.getLocalParameter(RbmKineticLawParameterType.MassActionForwardRate);
fR.setName(forwardRateName);
LocalParameter rR = kineticLaw.getLocalParameter(RbmKineticLawParameterType.MassActionReverseRate);
rR.setName(reverseRateName);
if (rs.hasReactant()) {
kineticLaw.setParameterValue(fR, forwardRateExp, true);
}
if (rs.hasProduct()) {
kineticLaw.setParameterValue(rR, reverseRateExp, true);
}
//
for (KineticsParameter reaction_p : massActionKinetics.getKineticsParameters()) {
if (reaction_p.getRole() == Kinetics.ROLE_UserDefined) {
LocalParameter rule_p = kineticLaw.getLocalParameter(reaction_p.getName());
if (rule_p == null) {
//
// after lazy parameter creation we didn't find a user-defined rule parameter with this same name.
//
// there must be a global symbol with the same name, that the local reaction parameter has overridden.
//
ParameterContext.LocalProxyParameter rule_proxy_parameter = null;
for (ProxyParameter proxyParameter : kineticLaw.getProxyParameters()) {
if (proxyParameter.getName().equals(reaction_p.getName())) {
rule_proxy_parameter = (LocalProxyParameter) proxyParameter;
}
}
if (rule_proxy_parameter != null) {
// we want to convert to local
boolean bConvertToGlobal = false;
kineticLaw.convertParameterType(rule_proxy_parameter, bConvertToGlobal);
} else {
// could find neither local parameter nor proxy parameter
throw new RuntimeException("user defined parameter " + reaction_p.getName() + " from reaction " + rs.getName() + " didn't map to a reactionRule parameter");
}
} else if (rule_p.getRole() == RbmKineticLawParameterType.UserDefined) {
kineticLaw.setParameterValue(rule_p, reaction_p.getExpression(), true);
rule_p.setUnitDefinition(reaction_p.getUnitDefinition());
} else {
throw new RuntimeException("user defined parameter " + reaction_p.getName() + " from reaction " + rs.getName() + " mapped to a reactionRule parameter with unexpected role " + rule_p.getRole().getDescription());
}
}
}
} catch (ExpressionException e) {
e.printStackTrace();
throw new RuntimeException("Problem attempting to set RbmKineticLaw expression: " + e.getMessage());
}
rr.setKineticLaw(kineticLaw);
KineticsParameter[] kpList = k.getKineticsParameters();
ModelParameter[] mpList = rs.getModel().getModelParameters();
ModelParameter mp = rs.getModel().getModelParameter(kpList[0].getName());
ReactionParticipant[] rpList = rs.getReactionParticipants();
for (ReactionParticipant p : rpList) {
if (p instanceof Reactant) {
int stoichiometry = p.getStoichiometry();
for (int i = 0; i < stoichiometry; i++) {
SpeciesPattern speciesPattern = new SpeciesPattern(rs.getModel(), p.getSpeciesContext().getSpeciesPattern());
ReactantPattern reactantPattern = new ReactantPattern(speciesPattern, p.getStructure());
rr.addReactant(reactantPattern);
}
} else if (p instanceof Product) {
int stoichiometry = p.getStoichiometry();
for (int i = 0; i < stoichiometry; i++) {
SpeciesPattern speciesPattern = new SpeciesPattern(rs.getModel(), p.getSpeciesContext().getSpeciesPattern());
ProductPattern productPattern = new ProductPattern(speciesPattern, p.getStructure());
rr.addProduct(productPattern);
}
}
}
// commented code below is probably obsolete, we verify (above) in the reaction the number of participants,
// no need to do it again in the corresponding rule
// if(rr.getReactantPatterns().size() > 2) {
// String message = "NFSim doesn't support more than 2 reactants within a reaction: " + name;
// throw new RuntimeException(message);
// }
// if(rr.getProductPatterns().size() > 2) {
// String message = "NFSim doesn't support more than 2 products within a reaction: " + name;
// throw new RuntimeException(message);
// }
newModel.removeReactionStep(rs);
newModel.getRbmModelContainer().addReactionRule(rr);
}
for (ReactionRuleSpec rrs : transformedSimulationContext.getReactionContext().getReactionRuleSpecs()) {
if (rrs == null) {
continue;
}
ReactionRule rr = rrs.getReactionRule();
if (rrs.isExcluded()) {
// delete those rules which are disabled (excluded) in the Specifications / Reaction table
newModel.getRbmModelContainer().removeReactionRule(rr);
continue;
}
}
// now that we generated the rules we can delete the reaction steps they're coming from
for (ReactionStep rs : newModel.getReactionSteps()) {
newModel.removeReactionStep(rs);
}
try {
// we invoke bngl just for the purpose of generating the xml file, which we'll then use to extract the symmetry factor
generateNetwork(transformedSimulationContext, fromReactions, mathMappingCallback);
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Finished RuleBased Transformer.");
}
use of cbit.vcell.mapping.ParameterContext.LocalProxyParameter in project vcell by virtualcell.
the class ReactionRulePropertiesTableModel method propertyChange.
/**
* This method gets called when a bound property is changed.
* @param evt A PropertyChangeEvent object describing the event source
* and the property that has changed.
*/
public void propertyChange(java.beans.PropertyChangeEvent evt) {
if (evt.getSource() == this && evt.getPropertyName().equals("reactionRule")) {
ReactionRule oldValue = (ReactionRule) evt.getOldValue();
if (oldValue != null) {
oldValue.removePropertyChangeListener(this);
oldValue.getKineticLaw().removePropertyChangeListener(this);
for (int i = 0; i < oldValue.getKineticLaw().getLocalParameters().length; i++) {
oldValue.getKineticLaw().getLocalParameters()[i].removePropertyChangeListener(this);
}
for (int i = 0; i < oldValue.getKineticLaw().getProxyParameters().length; i++) {
oldValue.getKineticLaw().getProxyParameters()[i].removePropertyChangeListener(this);
}
}
ReactionRule newValue = (ReactionRule) evt.getNewValue();
if (newValue != null) {
newValue.addPropertyChangeListener(this);
newValue.getKineticLaw().addPropertyChangeListener(this);
for (int i = 0; i < newValue.getKineticLaw().getLocalParameters().length; i++) {
newValue.getKineticLaw().getLocalParameters()[i].addPropertyChangeListener(this);
}
for (int i = 0; i < newValue.getKineticLaw().getProxyParameters().length; i++) {
newValue.getKineticLaw().getProxyParameters()[i].addPropertyChangeListener(this);
}
autoCompleteSymbolFilter = reactionRule.getAutoCompleteSymbolFilter();
}
refreshData();
}
if (evt.getSource() == reactionRule && evt.getPropertyName().equals(ReactionRule.PROPERTY_NAME_KINETICLAW)) {
RbmKineticLaw oldValue = (RbmKineticLaw) evt.getOldValue();
if (oldValue != null) {
oldValue.removePropertyChangeListener(this);
for (int i = 0; i < oldValue.getLocalParameters().length; i++) {
oldValue.getLocalParameters()[i].removePropertyChangeListener(this);
}
for (int i = 0; i < oldValue.getProxyParameters().length; i++) {
oldValue.getProxyParameters()[i].removePropertyChangeListener(this);
}
}
RbmKineticLaw newValue = (RbmKineticLaw) evt.getNewValue();
if (newValue != null) {
newValue.addPropertyChangeListener(this);
for (int i = 0; i < newValue.getLocalParameters().length; i++) {
newValue.getLocalParameters()[i].addPropertyChangeListener(this);
}
for (int i = 0; i < newValue.getProxyParameters().length; i++) {
newValue.getProxyParameters()[i].addPropertyChangeListener(this);
}
}
refreshData();
}
if (evt.getSource() instanceof RbmKineticLaw && (evt.getPropertyName().equals("localParameters") || evt.getPropertyName().equals("proxyParameters"))) {
Parameter[] oldParams = (Parameter[]) evt.getOldValue();
Parameter[] newParams = (Parameter[]) evt.getNewValue();
for (int i = 0; oldParams != null && i < oldParams.length; i++) {
oldParams[i].removePropertyChangeListener(this);
}
for (int i = 0; newParams != null && i < newParams.length; i++) {
newParams[i].addPropertyChangeListener(this);
}
refreshData();
}
if (evt.getSource() instanceof LocalParameter || evt.getSource() instanceof LocalProxyParameter) {
refreshData();
}
if (evt.getSource() instanceof Parameter) {
fireTableRowsUpdated(0, getRowCount() - 1);
}
}
Aggregations