use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.
the class KineticsTypeTemplatePanel method onSelectedObjectsChange.
@Override
protected void onSelectedObjectsChange(Object[] selectedObjects) {
if (selectedObjects == null || selectedObjects.length != 1) {
return;
}
if (selectedObjects[0] instanceof ReactionStep) {
setReactionStep((ReactionStep) selectedObjects[0]);
} else if (selectedObjects[0] instanceof ReactionSpec) {
setReactionStep(((ReactionSpec) selectedObjects[0]).getReactionStep());
} else if (selectedObjects[0] instanceof KineticsParameter) {
KineticsParameter kineticsParameter = (KineticsParameter) selectedObjects[0];
setReactionStep(kineticsParameter.getKinetics().getReactionStep());
for (int i = 0; i < getParameterTableModel().getRowCount(); i++) {
if (kineticsParameter == getParameterTableModel().getValueAt(i)) {
getScrollPaneTable().setRowSelectionInterval(i, i);
break;
}
}
} else {
setReactionStep(null);
}
}
use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.
the class ParameterTableModel method setValueAt.
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Parameter parameter = getValueAt(rowIndex);
// try {
switch(columnIndex) {
case COLUMN_NAME:
{
try {
if (aValue instanceof String) {
String newName = (String) aValue;
if (!parameter.getName().equals(newName)) {
if (parameter instanceof Kinetics.KineticsParameter) {
reactionStep.getKinetics().renameParameter(parameter.getName(), newName);
} else if (parameter instanceof Kinetics.KineticsProxyParameter) {
parameter.setName(newName);
}
fireTableRowsUpdated(rowIndex, rowIndex);
}
}
} catch (ExpressionException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter name:\n" + e.getMessage());
} catch (java.beans.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 KineticsProxyParameter) && ((((KineticsProxyParameter) parameter).getTarget() instanceof Model.ReservedSymbol) || (((KineticsProxyParameter) parameter).getTarget() instanceof SpeciesContext) || (((KineticsProxyParameter) parameter).getTarget() instanceof ModelQuantity))) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter : \'" + parameter.getName() + "\' is a " + ((KineticsProxyParameter) parameter).getTarget().getClass() + " in the model; cannot convert it to a local kinetic parameter.");
} else {
try {
reactionStep.getKinetics().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 KineticsParameter) && (((KineticsParameter) parameter).getRole() != Kinetics.ROLE_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 = reactionStep.getKinetics().getReactionStep().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 (reactionStep.getKinetics().getKineticsParameter(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) {
reactionStep.getKinetics().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 Kinetics.KineticsParameter) {
reactionStep.getKinetics().setParameterValue((Kinetics.KineticsParameter) parameter, new Expression(newExpressionString));
} else if (parameter instanceof Kinetics.KineticsProxyParameter) {
parameter.setExpression(new Expression(newExpressionString));
}
}
reactionStep.getKinetics().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 Kinetics.KineticsParameter && ((Kinetics.KineticsParameter) parameter).getRole() == Kinetics.ROLE_UserDefined) {
String newUnitString = (String) aValue;
Kinetics.KineticsParameter kineticsParm = (Kinetics.KineticsParameter) parameter;
ModelUnitSystem modelUnitSystem = reactionStep.getModel().getUnitSystem();
if (!kineticsParm.getUnitDefinition().getSymbol().equals(newUnitString)) {
kineticsParm.setUnitDefinition(modelUnitSystem.getInstance(newUnitString));
reactionStep.getKinetics().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.model.Kinetics.KineticsParameter in project vcell by virtualcell.
the class SymbolTableEntryTableCellRenderer method getTableCellRendererComponent.
public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value == null) {
setText("unmapped");
return this;
}
SymbolTableEntry ste = (SymbolTableEntry) value;
if (ste instanceof Model.ReservedSymbol) {
setText(ste.getName());
} else if (ste instanceof SpeciesContext) {
setText("[" + ste.getName() + "]");
} else if (ste instanceof KineticsParameter) {
setText(ste.getNameScope().getName() + ":" + ste.getName());
} else if (ste instanceof ModelParameter) {
setText(ste.getName());
} else if (ste instanceof ReservedVariable) {
setText(ste.getName());
} else {
setText(ste.getNameScope().getAbsoluteScopePrefix() + ste.getName());
}
// setToolTipText("Kinetic parameter \""+ste.getName()+"\" in reaction "+);
return this;
}
use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.
the class MathMapping_4_8 method refreshLocalNameCount.
protected void refreshLocalNameCount() {
localNameCountHash.clear();
ReactionStep[] reactionSteps = simContext.getModel().getReactionSteps();
for (int j = 0; j < reactionSteps.length; j++) {
KineticsParameter[] params = reactionSteps[j].getKinetics().getKineticsParameters();
for (KineticsParameter kp : params) {
String name = kp.getName();
if (localNameCountHash.containsKey(name)) {
localNameCountHash.put(name, localNameCountHash.get(name) + 1);
} else {
localNameCountHash.put(name, 1);
}
}
}
SpeciesContext[] scs = simContext.getModel().getSpeciesContexts();
for (SpeciesContext sc : scs) {
String name = sc.getName();
if (localNameCountHash.containsKey(name)) {
localNameCountHash.put(name, localNameCountHash.get(name) + 1);
} else {
localNameCountHash.put(name, 1);
}
}
Species[] ss = simContext.getModel().getSpecies();
for (Species s : ss) {
String name = s.getCommonName();
if (localNameCountHash.containsKey(name)) {
localNameCountHash.put(name, localNameCountHash.get(name) + 1);
} else {
localNameCountHash.put(name, 1);
}
}
ModelParameter[] mps = simContext.getModel().getModelParameters();
for (ModelParameter mp : mps) {
String name = mp.getName();
if (localNameCountHash.containsKey(name)) {
localNameCountHash.put(name, localNameCountHash.get(name) + 1);
} else {
localNameCountHash.put(name, 1);
}
}
}
use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.
the class StochMathMapping_4_8 method getProbabilityRate.
/**
* Get probability expression for the specific elementary reaction.
* Input: ReactionStep, the reaction. isForwardDirection, if the elementary reaction is forward from the reactionstep.
* Output: Expression. the probability expression.
* Creation date: (9/14/2006 3:22:58 PM)
*/
Expression getProbabilityRate(ReactionStep rs, boolean isForwardDirection) throws MappingException {
ReactionStep reactionStep = rs;
Expression probExp = null;
// get kinetics of the reaction step
Kinetics kinetics = reactionStep.getKinetics();
// to compose the rate constant expression e.g. Kf, Kr
Expression rateConstantExpr = null;
// to compose the stochastic variable(species) expression, e.g. s*(s-1)*(s-2)* speciesFactor.
Expression rxnProbabilityExpr = null;
// to compose the factor that the probability expression multiplies with, which convert the rate expression under stochastic context
Expression factorExpr = null;
// the structure where reaction happens
StructureMapping sm = getSimulationContext().getGeometryContext().getStructureMapping(rs.getStructure());
Model model = getSimulationContext().getModel();
try {
if (// forward reaction
isForwardDirection) {
// for HMMs, it's a bit complicated. Vmax/(Km+s)-->Vmax*Size_s/(Km*Size_s+Ns)
if (kinetics.getKineticsDescription().equals(KineticsDescription.MassAction)) {
KineticsParameter kfp = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_KForward);
rateConstantExpr = new Expression(kfp, getNameScope());
// rateConstantExpr.bindExpression(this);
}
// get convert factor for rate constant( membrane:rateConstant*membrane_Size (factor is membrane_size), feature : rateConstant*(feature_size/KMole)(factor is feature_size/KMOLE)) )
if (sm.getStructure() instanceof Membrane) {
factorExpr = new Expression(sm.getStructure().getStructureSize(), getNameScope());
} else {
factorExpr = new Expression(sm.getStructure().getStructureSize(), getNameScope());
Expression kmoleExpr = new Expression(1.0 / 602.0);
factorExpr = Expression.mult(factorExpr, kmoleExpr);
}
// complete the probability expression by the reactants' stoichiometries if it is Mass Action rate law
if (kinetics.getKineticsDescription().equals(KineticsDescription.MassAction)) {
ReactionParticipant[] reacPart = reactionStep.getReactionParticipants();
for (int i = 0; i < reacPart.length; i++) {
int stoichiometry = 0;
if (reacPart[i] instanceof Reactant) {
stoichiometry = ((Reactant) reacPart[i]).getStoichiometry();
// ******the following part is to form the s*(s-1)(s-2)..(s-stoi+1).portion of the probability rate.
StructureMapping reactSM = getSimulationContext().getGeometryContext().getStructureMapping(reacPart[i].getStructure());
// factor expression for species
Expression speciesFactor = null;
// convert speceis' unit from moles/liter to molecules.
if (reactSM.getStructure() instanceof Membrane) {
speciesFactor = Expression.invert(new Expression(reactSM.getStructure().getStructureSize(), getNameScope()));
} else {
Expression exp1 = new Expression(1.0 / 602.0);
Expression exp2 = new Expression(reactSM.getStructure().getStructureSize(), getNameScope());
speciesFactor = Expression.div(Expression.invert(exp1), exp2);
}
// s*(s-1)(s-2)..(s-stoi+1)
SpeciesCountParameter spCountParam = getSpeciesCountParameter(reacPart[i].getSpeciesContext());
Expression spCount_exp = new Expression(spCountParam, getNameScope());
// species from uM to No. of Particles, form s*(s-1)*(s-2)
Expression tempExpr = new Expression(spCount_exp);
for (int j = 1; j < stoichiometry; j++) {
tempExpr = Expression.mult(tempExpr, Expression.add(spCount_exp, new Expression(-j)));
}
// update total factor with speceies factor
if (stoichiometry == 1) {
factorExpr = Expression.mult(factorExpr, speciesFactor);
} else if (stoichiometry > 1) {
// rxnProbExpr * (structSize^stoichiometry)
Expression powerExpr = Expression.power(speciesFactor, new Expression(stoichiometry));
factorExpr = Expression.mult(factorExpr, powerExpr);
}
if (rxnProbabilityExpr == null) {
rxnProbabilityExpr = new Expression(tempExpr);
} else {
// for more than one reactant
rxnProbabilityExpr = Expression.mult(rxnProbabilityExpr, tempExpr);
}
}
}
}
} else // reverse reaction
{
if (kinetics.getKineticsDescription().equals(KineticsDescription.MassAction)) {
KineticsParameter krp = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_KReverse);
rateConstantExpr = new Expression(krp, getNameScope());
// rateConstantExpr.bindExpression(this);
}
// get convert factor for rate constant( membrane:rateConstant*membrane_Size (factor is membrane_size), feature : rateConstant*(feature_size/KMole)(factor is feature_size/KMOLE)) )
if (sm.getStructure() instanceof Membrane) {
factorExpr = new Expression(sm.getStructure().getStructureSize(), getNameScope());
} else {
factorExpr = new Expression(sm.getStructure().getStructureSize(), getNameScope());
Expression exp = new Expression(1.0 / 602.0);
factorExpr = Expression.mult(factorExpr, exp);
}
// complete the remaining part of the probability expression by the products' stoichiometries.
if (kinetics.getKineticsDescription().equals(KineticsDescription.MassAction)) {
ReactionParticipant[] reacPart = reactionStep.getReactionParticipants();
for (int i = 0; i < reacPart.length; i++) {
int stoichiometry = 0;
if (reacPart[i] instanceof Product) {
stoichiometry = ((Product) reacPart[i]).getStoichiometry();
// ******the following part is to form the s*(s-1)*(s-2)...(s-stoi+1).portion of the probability rate.
StructureMapping reactSM = getSimulationContext().getGeometryContext().getStructureMapping(reacPart[i].getStructure());
// factor expression for species
Expression speciesFactor = null;
// convert speceis' unit from moles/liter to molecules.
if (reactSM.getStructure() instanceof Membrane) {
speciesFactor = Expression.invert(new Expression(reactSM.getStructure().getStructureSize(), getNameScope()));
} else {
Expression exp1 = new Expression(1.0 / 602.0);
Expression exp2 = new Expression(reactSM.getStructure().getStructureSize(), getNameScope());
speciesFactor = Expression.div(Expression.invert(exp1), exp2);
}
// s*(s-1)*(s-2)...(s-stoi+1)
SpeciesCountParameter spCountParam = getSpeciesCountParameter(reacPart[i].getSpeciesContext());
Expression spCount_exp = new Expression(spCountParam, getNameScope());
// species from uM to No. of Particles, form s*(s-1)*(s-2)
Expression tempExpr = new Expression(spCount_exp);
for (int j = 1; j < stoichiometry; j++) {
tempExpr = Expression.mult(tempExpr, Expression.add(spCount_exp, new Expression(-j)));
}
// update total factor with speceies factor
if (stoichiometry == 1) {
factorExpr = Expression.mult(factorExpr, speciesFactor);
} else if (stoichiometry > 1) {
// rxnProbExpr * (structSize^stoichiometry)
Expression powerExpr = Expression.power(speciesFactor, new Expression(stoichiometry));
factorExpr = Expression.mult(factorExpr, powerExpr);
}
if (rxnProbabilityExpr == null) {
rxnProbabilityExpr = new Expression(tempExpr);
} else {
rxnProbabilityExpr = Expression.mult(rxnProbabilityExpr, tempExpr);
}
}
}
}
}
// Now construct the probability expression.
if (rateConstantExpr == null) {
throw new MappingException("Can not find reaction rate constant in reaction: " + reactionStep.getName());
} else if (rxnProbabilityExpr == null) {
probExp = new Expression(rateConstantExpr);
} else if ((rateConstantExpr != null) && (rxnProbabilityExpr != null)) {
probExp = Expression.mult(rateConstantExpr, rxnProbabilityExpr);
}
// simplify the factor
RationalExp factorRatExp = RationalExpUtils.getRationalExp(factorExpr);
factorExpr = new Expression(factorRatExp.infixString());
factorExpr.bindExpression(this);
// get probability rate with converting factor
probExp = Expression.mult(probExp, factorExpr);
probExp = probExp.flatten();
// //
// // round trip to rational expression for simplifying terms like KMOLE/KMOLE ...
// // we don't want to loose the symbol binding ... so we make a temporary symbolTable from the original binding.
// //
// final Expression finalExp = new Expression(probExp);
// SymbolTable symbolTable = new SymbolTable(){
// public void getEntries(Map<String, SymbolTableEntry> entryMap) {
// throw new RuntimeException("should not be called");
// }
// public SymbolTableEntry getEntry(String identifierString) throws ExpressionBindingException {
// return finalExp.getSymbolBinding(identifierString);
// }
// };
// cbit.vcell.matrix.RationalExp ratExp = cbit.vcell.parser.RationalExpUtils.getRationalExp(probExp);
// probExp = new Expression(ratExp.infixString());
// probExp.bindExpression(symbolTable);
} catch (ExpressionException e) {
e.printStackTrace();
}
return probExp;
}
Aggregations