use of cbit.vcell.model.ProxyParameter in project vcell by virtualcell.
the class BioModelParametersTableModel method propertyChange.
@Override
public void propertyChange(java.beans.PropertyChangeEvent evt) {
super.propertyChange(evt);
if (evt.getSource() instanceof EditableSymbolTableEntry) {
int changeRow = getRowIndex((EditableSymbolTableEntry) evt.getSource());
if (changeRow >= 0) {
fireTableRowsUpdated(changeRow, changeRow);
}
} else {
String propertyName = evt.getPropertyName();
if (evt.getSource() == bioModel.getModel()) {
if (propertyName.equals(Model.PROPERTY_NAME_MODEL_PARAMETERS)) {
ModelParameter[] oldValue = (ModelParameter[]) evt.getOldValue();
if (oldValue != null) {
for (EditableSymbolTableEntry parameter : oldValue) {
parameter.removePropertyChangeListener(this);
}
}
ModelParameter[] newValue = (ModelParameter[]) evt.getNewValue();
if (newValue != null) {
for (EditableSymbolTableEntry parameter : newValue) {
parameter.addPropertyChangeListener(this);
}
}
refreshData();
} else if (propertyName.equals(Model.PROPERTY_NAME_SPECIES_CONTEXTS)) {
SpeciesContext[] oldValue = (SpeciesContext[]) evt.getOldValue();
if (oldValue != null) {
for (SpeciesContext sc : oldValue) {
sc.removePropertyChangeListener(this);
}
}
SpeciesContext[] newValue = (SpeciesContext[]) evt.getNewValue();
if (newValue != null) {
for (SpeciesContext sc : newValue) {
sc.addPropertyChangeListener(this);
}
}
refreshData();
} else if (propertyName.equals(Model.PROPERTY_NAME_REACTION_STEPS)) {
ReactionStep[] oldValue = (ReactionStep[]) evt.getOldValue();
if (oldValue != null) {
for (ReactionStep reactionStep : oldValue) {
reactionStep.removePropertyChangeListener(this);
reactionStep.getKinetics().removePropertyChangeListener(this);
for (KineticsParameter kineticsEditableSymbolTableEntry : reactionStep.getKinetics().getKineticsParameters()) {
kineticsEditableSymbolTableEntry.removePropertyChangeListener(this);
}
for (ProxyParameter proxyEditableSymbolTableEntry : reactionStep.getKinetics().getProxyParameters()) {
proxyEditableSymbolTableEntry.removePropertyChangeListener(this);
}
for (UnresolvedParameter unresolvedEditableSymbolTableEntry : reactionStep.getKinetics().getUnresolvedParameters()) {
unresolvedEditableSymbolTableEntry.removePropertyChangeListener(this);
}
}
}
ReactionStep[] newValue = (ReactionStep[]) evt.getNewValue();
if (newValue != null) {
for (ReactionStep reactionStep : newValue) {
reactionStep.addPropertyChangeListener(this);
reactionStep.getKinetics().addPropertyChangeListener(this);
for (KineticsParameter kineticsEditableSymbolTableEntry : reactionStep.getKinetics().getKineticsParameters()) {
kineticsEditableSymbolTableEntry.addPropertyChangeListener(this);
}
for (ProxyParameter proxyEditableSymbolTableEntry : reactionStep.getKinetics().getProxyParameters()) {
proxyEditableSymbolTableEntry.addPropertyChangeListener(this);
}
for (UnresolvedParameter unresolvedEditableSymbolTableEntry : reactionStep.getKinetics().getUnresolvedParameters()) {
unresolvedEditableSymbolTableEntry.addPropertyChangeListener(this);
}
}
}
refreshData();
} else if (evt.getPropertyName().equals(RbmModelContainer.PROPERTY_NAME_REACTION_RULE_LIST)) {
List<ReactionRule> oldValue = (List<ReactionRule>) evt.getOldValue();
if (oldValue != null) {
for (ReactionRule rs : oldValue) {
rs.removePropertyChangeListener(this);
}
}
List<ReactionRule> newValue = (List<ReactionRule>) evt.getNewValue();
if (newValue != null) {
for (ReactionRule rs : newValue) {
rs.addPropertyChangeListener(this);
}
}
refreshData();
}
} else if (evt.getSource() == bioModel) {
if (propertyName.equals(BioModel.PROPERTY_NAME_SIMULATION_CONTEXTS)) {
SimulationContext[] oldValue = (SimulationContext[]) evt.getOldValue();
for (SimulationContext simulationContext : oldValue) {
simulationContext.removePropertyChangeListener(this);
simulationContext.getGeometryContext().removePropertyChangeListener(this);
for (StructureMapping mapping : simulationContext.getGeometryContext().getStructureMappings()) {
mapping.removePropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : mapping.getParameters()) {
parameter.removePropertyChangeListener(this);
}
}
simulationContext.getReactionContext().removePropertyChangeListener(this);
for (SpeciesContextSpec spec : simulationContext.getReactionContext().getSpeciesContextSpecs()) {
spec.removePropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : spec.getParameters()) {
parameter.removePropertyChangeListener(this);
}
}
for (ElectricalStimulus elect : simulationContext.getElectricalStimuli()) {
elect.removePropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : elect.getParameters()) {
parameter.removePropertyChangeListener(this);
}
}
for (SpatialObject spatialObject : simulationContext.getSpatialObjects()) {
spatialObject.removePropertyChangeListener(this);
}
for (SpatialProcess spatialProcess : simulationContext.getSpatialProcesses()) {
spatialProcess.removePropertyChangeListener(this);
for (LocalParameter p : spatialProcess.getParameters()) {
p.removePropertyChangeListener(this);
}
}
for (SimulationContextParameter p : simulationContext.getSimulationContextParameters()) {
p.removePropertyChangeListener(this);
}
}
SimulationContext[] newValue = (SimulationContext[]) evt.getNewValue();
for (SimulationContext simulationContext : newValue) {
simulationContext.addPropertyChangeListener(this);
simulationContext.getGeometryContext().addPropertyChangeListener(this);
for (StructureMapping mapping : simulationContext.getGeometryContext().getStructureMappings()) {
mapping.addPropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : mapping.getParameters()) {
parameter.addPropertyChangeListener(this);
}
}
simulationContext.getReactionContext().addPropertyChangeListener(this);
for (SpeciesContextSpec spec : simulationContext.getReactionContext().getSpeciesContextSpecs()) {
spec.addPropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : spec.getParameters()) {
parameter.addPropertyChangeListener(this);
}
}
for (ElectricalStimulus elect : simulationContext.getElectricalStimuli()) {
elect.addPropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : elect.getParameters()) {
parameter.addPropertyChangeListener(this);
}
}
for (SpatialObject spatialObject : simulationContext.getSpatialObjects()) {
spatialObject.addPropertyChangeListener(this);
}
for (SpatialProcess spatialProcess : simulationContext.getSpatialProcesses()) {
spatialProcess.addPropertyChangeListener(this);
for (LocalParameter p : spatialProcess.getParameters()) {
p.addPropertyChangeListener(this);
}
}
for (SimulationContextParameter p : simulationContext.getSimulationContextParameters()) {
p.addPropertyChangeListener(this);
}
}
refreshData();
}
} else if (evt.getSource() instanceof GeometryContext && evt.getPropertyName().equals(GeometryContext.PROPERTY_STRUCTURE_MAPPINGS)) {
StructureMapping[] oldValue = (StructureMapping[]) evt.getOldValue();
if (oldValue != null) {
for (StructureMapping mapping : oldValue) {
mapping.removePropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : mapping.getParameters()) {
parameter.removePropertyChangeListener(this);
}
}
}
StructureMapping[] newValue = (StructureMapping[]) evt.getNewValue();
if (newValue != null) {
for (StructureMapping mapping : newValue) {
mapping.addPropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : mapping.getParameters()) {
parameter.addPropertyChangeListener(this);
}
}
}
refreshData();
} else if (evt.getSource() instanceof ReactionStep && (evt.getPropertyName().equals(ReactionStep.PROPERTY_NAME_KINETICS))) {
Kinetics oldValue = (Kinetics) evt.getOldValue();
if (oldValue != null) {
oldValue.removePropertyChangeListener(this);
for (KineticsParameter kineticsEditableSymbolTableEntry : oldValue.getKineticsParameters()) {
kineticsEditableSymbolTableEntry.removePropertyChangeListener(this);
}
for (ProxyParameter proxyEditableSymbolTableEntry : oldValue.getProxyParameters()) {
proxyEditableSymbolTableEntry.removePropertyChangeListener(this);
}
for (UnresolvedParameter unresolvedEditableSymbolTableEntry : oldValue.getUnresolvedParameters()) {
unresolvedEditableSymbolTableEntry.removePropertyChangeListener(this);
}
}
Kinetics newValue = (Kinetics) evt.getNewValue();
if (newValue != null) {
newValue.addPropertyChangeListener(this);
for (KineticsParameter kineticsEditableSymbolTableEntry : newValue.getKineticsParameters()) {
kineticsEditableSymbolTableEntry.addPropertyChangeListener(this);
}
for (ProxyParameter proxyEditableSymbolTableEntry : newValue.getProxyParameters()) {
proxyEditableSymbolTableEntry.addPropertyChangeListener(this);
}
for (UnresolvedParameter unresolvedEditableSymbolTableEntry : newValue.getUnresolvedParameters()) {
unresolvedEditableSymbolTableEntry.addPropertyChangeListener(this);
}
}
refreshData();
} else if (evt.getSource() instanceof SimulationContext && evt.getPropertyName().equals(SimulationContext.PROPERTY_NAME_SPATIALPROCESSES)) {
SpatialProcess[] oldValue = (SpatialProcess[]) evt.getOldValue();
if (oldValue != null) {
for (SpatialProcess process : oldValue) {
process.removePropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : process.getParameters()) {
parameter.removePropertyChangeListener(this);
}
}
}
SpatialProcess[] newValue = (SpatialProcess[]) evt.getNewValue();
if (newValue != null) {
for (SpatialProcess process : newValue) {
process.addPropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : process.getParameters()) {
parameter.addPropertyChangeListener(this);
}
}
}
refreshData();
} else if (evt.getSource() instanceof SimulationContext && evt.getPropertyName().equals(SimulationContext.PROPERTY_NAME_SPATIALOBJECTS)) {
SpatialObject[] oldValue = (SpatialObject[]) evt.getOldValue();
if (oldValue != null) {
for (SpatialObject spatialObject : oldValue) {
spatialObject.removePropertyChangeListener(this);
}
}
SpatialObject[] newValue = (SpatialObject[]) evt.getNewValue();
if (newValue != null) {
for (SpatialObject spatialObject : newValue) {
spatialObject.addPropertyChangeListener(this);
}
}
refreshData();
} else if (evt.getSource() instanceof SpatialObject && evt.getPropertyName().equals(SpatialObject.PROPERTY_NAME_QUANTITYCATEGORIESENABLED)) {
refreshData();
} else if (evt.getSource() instanceof SimulationContext && evt.getPropertyName().equals(SimulationContext.PROPERTY_NAME_SIMULATIONCONTEXTPARAMETERS)) {
SimulationContextParameter[] oldValue = (SimulationContextParameter[]) evt.getOldValue();
if (oldValue != null) {
for (SimulationContextParameter param : oldValue) {
param.removePropertyChangeListener(this);
}
}
SimulationContextParameter[] newValue = (SimulationContextParameter[]) evt.getNewValue();
if (newValue != null) {
for (SimulationContextParameter param : newValue) {
param.addPropertyChangeListener(this);
}
}
refreshData();
} else if (evt.getSource() instanceof Kinetics && (evt.getPropertyName().equals(Kinetics.PROPERTY_NAME_KINETICS_PARAMETERS))) {
EditableSymbolTableEntry[] oldValue = (EditableSymbolTableEntry[]) evt.getOldValue();
if (oldValue != null) {
for (int i = 0; i < oldValue.length; i++) {
oldValue[i].removePropertyChangeListener(this);
}
}
EditableSymbolTableEntry[] newValue = (EditableSymbolTableEntry[]) evt.getNewValue();
if (newValue != null) {
for (int i = 0; i < newValue.length; i++) {
newValue[i].addPropertyChangeListener(this);
}
}
refreshData();
// } else if(evt.getSource() instanceof ReactionRuleEmbedded) {
// ReactionRuleEmbedded reactionRule = (ReactionRuleEmbedded) evt.getSource();
// int changeRow = getRowIndex(reactionRule);
// if (changeRow >= 0) {
// fireTableRowsUpdated(changeRow, changeRow);
// }
}
}
}
use of cbit.vcell.model.ProxyParameter in project vcell by virtualcell.
the class ParameterTableModel 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("reactionStep")) {
ReactionStep oldValue = (ReactionStep) evt.getOldValue();
if (oldValue != null) {
oldValue.removePropertyChangeListener(this);
oldValue.getKinetics().removePropertyChangeListener(this);
for (int i = 0; i < oldValue.getKinetics().getKineticsParameters().length; i++) {
oldValue.getKinetics().getKineticsParameters()[i].removePropertyChangeListener(this);
}
for (int i = 0; i < oldValue.getKinetics().getProxyParameters().length; i++) {
oldValue.getKinetics().getProxyParameters()[i].removePropertyChangeListener(this);
}
}
ReactionStep newValue = (ReactionStep) evt.getNewValue();
if (newValue != null) {
newValue.addPropertyChangeListener(this);
newValue.getKinetics().addPropertyChangeListener(this);
for (int i = 0; i < newValue.getKinetics().getKineticsParameters().length; i++) {
newValue.getKinetics().getKineticsParameters()[i].removePropertyChangeListener(this);
newValue.getKinetics().getKineticsParameters()[i].addPropertyChangeListener(this);
}
for (int i = 0; i < newValue.getKinetics().getProxyParameters().length; i++) {
newValue.getKinetics().getProxyParameters()[i].removePropertyChangeListener(this);
newValue.getKinetics().getProxyParameters()[i].addPropertyChangeListener(this);
}
autoCompleteSymbolFilter = reactionStep.getAutoCompleteSymbolFilter();
}
refreshData();
}
if (evt.getSource() == reactionStep && evt.getPropertyName().equals(ReactionStep.PROPERTY_NAME_KINETICS)) {
Kinetics oldValue = (Kinetics) evt.getOldValue();
if (oldValue != null) {
oldValue.removePropertyChangeListener(this);
for (int i = 0; i < oldValue.getKineticsParameters().length; i++) {
oldValue.getKineticsParameters()[i].removePropertyChangeListener(this);
}
for (int i = 0; i < oldValue.getProxyParameters().length; i++) {
oldValue.getProxyParameters()[i].removePropertyChangeListener(this);
}
}
Kinetics newValue = (Kinetics) evt.getNewValue();
if (newValue != null) {
newValue.addPropertyChangeListener(this);
for (int i = 0; i < newValue.getKineticsParameters().length; i++) {
newValue.getKineticsParameters()[i].removePropertyChangeListener(this);
newValue.getKineticsParameters()[i].addPropertyChangeListener(this);
}
for (int i = 0; i < newValue.getProxyParameters().length; i++) {
newValue.getProxyParameters()[i].removePropertyChangeListener(this);
newValue.getProxyParameters()[i].addPropertyChangeListener(this);
}
}
// get rid of all Catalysts when changing kynetic type
if (reactionStep != null && reactionStep.hasCatalyst() && newValue != null && newValue != oldValue) {
List<ReactionParticipant> rpList = new ArrayList<>(Arrays.asList(reactionStep.getReactionParticipants()));
for (ReactionParticipant rp : rpList) {
if (rp instanceof Catalyst) {
try {
reactionStep.removeReactionParticipant(rp);
} catch (PropertyVetoException | ExpressionException | ModelException e) {
// do nothing if it fails
e.printStackTrace();
}
}
}
}
refreshData();
}
if (evt.getSource() instanceof Kinetics && (evt.getPropertyName().equals("kineticsParameters") || 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].removePropertyChangeListener(this);
newParams[i].addPropertyChangeListener(this);
}
if (evt.getPropertyName().equals("proxyParameters")) {
List<Parameter> addedList = new ArrayList<>(Arrays.asList(newParams));
addedList.removeAll(Arrays.asList(oldParams));
List<Parameter> removedList = new ArrayList<>(Arrays.asList(oldParams));
removedList.removeAll(Arrays.asList(newParams));
if (reactionStep != null) {
for (Parameter removed : removedList) {
if (removed instanceof ProxyParameter && ((ProxyParameter) removed).getTarget() instanceof SpeciesContext) {
SpeciesContext sc = (SpeciesContext) ((ProxyParameter) removed).getTarget();
if (reactionStep.getReactant(sc.getName()) == null && reactionStep.getProduct(sc.getName()) == null && reactionStep.getCatalyst(sc.getName()) != null) {
try {
reactionStep.removeReactionParticipant(reactionStep.getCatalyst(sc.getName()));
} catch (PropertyVetoException | ExpressionException | ModelException e) {
// we do nothing
e.printStackTrace();
}
}
}
}
for (Parameter added : addedList) {
if (added instanceof ProxyParameter && ((ProxyParameter) added).getTarget() instanceof SpeciesContext) {
SpeciesContext sc = (SpeciesContext) ((ProxyParameter) added).getTarget();
if (reactionStep.getReactant(sc.getName()) == null && reactionStep.getProduct(sc.getName()) == null && reactionStep.getCatalyst(sc.getName()) == null) {
try {
reactionStep.addCatalyst(sc);
} catch (PropertyVetoException | ModelException e) {
// we do nothing
e.printStackTrace();
}
}
}
}
}
}
refreshData();
}
if (evt.getSource() instanceof Parameter) {
fireTableRowsUpdated(0, getRowCount() - 1);
}
}
use of cbit.vcell.model.ProxyParameter 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.model.ProxyParameter in project vcell by virtualcell.
the class MathMapping_4_8 method getMathSymbol0.
/**
* Substitutes appropriate variables for speciesContext bindings
*
* @return cbit.vcell.parser.Expression
* @param origExp cbit.vcell.parser.Expression
* @param structureMapping cbit.vcell.mapping.StructureMapping
*/
private String getMathSymbol0(SymbolTableEntry ste, StructureMapping structureMapping) throws MappingException {
String steName = ste.getName();
if (ste instanceof Kinetics.KineticsParameter) {
Integer count = localNameCountHash.get(steName);
if (count == null) {
throw new MappingException("KineticsParameter " + steName + " not found in local name count");
}
if (count > 1 || steName.equals("J")) {
return steName + "_" + ste.getNameScope().getName();
// return getNameScope().getSymbolName(ste);
} else {
return steName;
}
}
if (ste instanceof MathMapping_4_8.ProbabilityParameter) {
// be careful here, to see if we need mangle the reaction name
MathMapping_4_8.ProbabilityParameter probParm = (MathMapping_4_8.ProbabilityParameter) ste;
return probParm.getName();
}
if (ste instanceof MathMapping_4_8.SpeciesConcentrationParameter) {
MathMapping_4_8.SpeciesConcentrationParameter concParm = (MathMapping_4_8.SpeciesConcentrationParameter) ste;
return concParm.getSpeciesContextSpec().getSpeciesContext().getName() + MATH_FUNC_SUFFIX_SPECIES_CONCENTRATION;
}
if (ste instanceof MathMapping_4_8.SpeciesCountParameter) {
MathMapping_4_8.SpeciesCountParameter countParm = (MathMapping_4_8.SpeciesCountParameter) ste;
return countParm.getSpeciesContextSpec().getSpeciesContext().getName() + MATH_VAR_SUFFIX_SPECIES_COUNT;
}
if (ste instanceof MathMapping_4_8.EventAssignmentInitParameter) {
MathMapping_4_8.EventAssignmentInitParameter eventInitParm = (MathMapping_4_8.EventAssignmentInitParameter) ste;
return eventInitParm.getName() + MATH_FUNC_SUFFIX_EVENTASSIGN_INIT;
}
if (ste instanceof Model.ReservedSymbol) {
return steName;
}
if (ste instanceof Membrane.MembraneVoltage) {
return steName;
}
if (ste instanceof Structure.StructureSize) {
Structure structure = ((Structure.StructureSize) ste).getStructure();
StructureMapping.StructureMappingParameter sizeParameter = simContext.getGeometryContext().getStructureMapping(structure).getSizeParameter();
return getMathSymbol(sizeParameter, structureMapping);
}
if (ste instanceof ProxyParameter) {
ProxyParameter pp = (ProxyParameter) ste;
return getMathSymbol0(pp.getTarget(), structureMapping);
}
//
Model model = simContext.getModel();
if (ste instanceof ModelParameter) {
ModelParameter mp = (ModelParameter) ste;
if (simContext.getGeometry().getDimension() == 0) {
return mp.getName();
} else {
if (mp.getExpression().getSymbols() == null) {
return mp.getName();
}
// check if global param variant name exists in globalVarsHash. If so, return it, else, throw exception.
Hashtable<String, Expression> smVariantsHash = globalParamVariantsHash.get(mp);
String variantName = mp.getName() + "_" + TokenMangler.fixTokenStrict(structureMapping.getStructure().getName());
if (smVariantsHash.get(variantName) != null) {
return variantName;
} else {
// global param variant doesn't exist in the hash, so get the substituted expression for global param and
// gather all symbols (speciesContexts) that do not match with arg 'structureMapping' to display a proper error message.
Expression expr = null;
try {
expr = substituteGlobalParameters(mp.getExpression());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
throw new RuntimeException("Could not substitute expression for global parameter '" + mp.getName() + "' with expression '" + "'" + e.getMessage());
}
// find symbols (typically speciesContexts) in 'exp' that do not match with the arg 'structureMapping'
String[] symbols = expr.getSymbols();
String msg = "";
if (symbols != null) {
Vector<String> spContextNamesVector = new Vector<String>();
for (int j = 0; j < symbols.length; j++) {
SpeciesContext sc = model.getSpeciesContext(symbols[j]);
if (sc != null) {
if (!sc.getStructure().compareEqual(structureMapping.getStructure())) {
spContextNamesVector.addElement(sc.getName());
}
}
}
for (int i = 0; (spContextNamesVector != null && i < spContextNamesVector.size()); i++) {
if (i == 0) {
msg += "'" + spContextNamesVector.elementAt(i) + ", ";
} else if (i == spContextNamesVector.size() - 1) {
msg += spContextNamesVector.elementAt(i) + "'";
} else {
msg += spContextNamesVector.elementAt(i) + ", ";
}
}
}
throw new RuntimeException("Global parameter '" + mp.getName() + "' is not defined in compartment '" + structureMapping.getStructure().getName() + "', but was referenced in that compartment." + "\n\nExpression '" + mp.getExpression().infix() + "' for global parameter '" + mp.getName() + "' expands to '" + expr.infix() + "' " + "and contains species " + msg + " that is/are not in adjacent compartments.");
}
// return (mp.getName()+"_"+TokenMangler.fixTokenStrict(structureMapping.getStructure().getName()));
}
}
if (ste instanceof SpeciesContextSpec.SpeciesContextSpecParameter) {
SpeciesContextSpec.SpeciesContextSpecParameter scsParm = (SpeciesContextSpec.SpeciesContextSpecParameter) ste;
if (scsParm.getRole() == SpeciesContextSpec.ROLE_InitialConcentration) {
return ((SpeciesContextSpec) (scsParm.getNameScope().getScopedSymbolTable())).getSpeciesContext().getName() + MATH_FUNC_SUFFIX_SPECIES_INIT_CONCENTRATION;
}
if (scsParm.getRole() == SpeciesContextSpec.ROLE_InitialCount) {
return ((SpeciesContextSpec) (scsParm.getNameScope().getScopedSymbolTable())).getSpeciesContext().getName() + MATH_FUNC_SUFFIX_SPECIES_INIT_COUNT;
}
if (scsParm.getRole() == SpeciesContextSpec.ROLE_DiffusionRate) {
return ((SpeciesContextSpec) (scsParm.getNameScope().getScopedSymbolTable())).getSpeciesContext().getName() + "_diffusionRate";
}
if (scsParm.getRole() == SpeciesContextSpec.ROLE_BoundaryValueXm) {
return ((SpeciesContextSpec) (scsParm.getNameScope().getScopedSymbolTable())).getSpeciesContext().getName() + "_boundaryXm";
}
if (scsParm.getRole() == SpeciesContextSpec.ROLE_BoundaryValueXp) {
return ((SpeciesContextSpec) (scsParm.getNameScope().getScopedSymbolTable())).getSpeciesContext().getName() + "_boundaryXp";
}
if (scsParm.getRole() == SpeciesContextSpec.ROLE_BoundaryValueYm) {
return ((SpeciesContextSpec) (scsParm.getNameScope().getScopedSymbolTable())).getSpeciesContext().getName() + "_boundaryYm";
}
if (scsParm.getRole() == SpeciesContextSpec.ROLE_BoundaryValueYp) {
return ((SpeciesContextSpec) (scsParm.getNameScope().getScopedSymbolTable())).getSpeciesContext().getName() + "_boundaryYp";
}
if (scsParm.getRole() == SpeciesContextSpec.ROLE_BoundaryValueZm) {
return ((SpeciesContextSpec) (scsParm.getNameScope().getScopedSymbolTable())).getSpeciesContext().getName() + "_boundaryZm";
}
if (scsParm.getRole() == SpeciesContextSpec.ROLE_BoundaryValueZp) {
return ((SpeciesContextSpec) (scsParm.getNameScope().getScopedSymbolTable())).getSpeciesContext().getName() + "_boundaryZp";
}
if (scsParm.getRole() == SpeciesContextSpec.ROLE_VelocityX) {
return ((SpeciesContextSpec) (scsParm.getNameScope().getScopedSymbolTable())).getSpeciesContext().getName() + "_velocityX";
}
if (scsParm.getRole() == SpeciesContextSpec.ROLE_VelocityY) {
return ((SpeciesContextSpec) (scsParm.getNameScope().getScopedSymbolTable())).getSpeciesContext().getName() + "_velocityY";
}
if (scsParm.getRole() == SpeciesContextSpec.ROLE_VelocityZ) {
return ((SpeciesContextSpec) (scsParm.getNameScope().getScopedSymbolTable())).getSpeciesContext().getName() + "_velocityZ";
}
}
if (ste instanceof ElectricalDevice.ElectricalDeviceParameter) {
ElectricalDevice.ElectricalDeviceParameter edParm = (ElectricalDevice.ElectricalDeviceParameter) ste;
ElectricalDevice electricalDevice = (ElectricalDevice) edParm.getNameScope().getScopedSymbolTable();
if (electricalDevice instanceof MembraneElectricalDevice) {
String nameWithScope = ((MembraneElectricalDevice) electricalDevice).getMembraneMapping().getMembrane().getNameScope().getName();
if (edParm.getRole() == ElectricalDevice.ROLE_TotalCurrent) {
return "I_" + nameWithScope;
}
if (edParm.getRole() == ElectricalDevice.ROLE_TransmembraneCurrent) {
return "F_" + nameWithScope;
}
// }else if (electricalDevice instanceof CurrentClampElectricalDevice) {
// if (edParm.getRole()==ElectricalDevice.ROLE_TotalCurrentDensity){
// return "I_"+((CurrentClampElectricalDevice)electricalDevice).getCurrentClampStimulus().getNameScope().getName();
// }
// if (edParm.getRole()==ElectricalDevice.ROLE_TransmembraneCurrentDensity){
// return "F_"+((CurrentClampElectricalDevice)electricalDevice).getCurrentClampStimulus().getNameScope().getName();
// }
// }else if (electricalDevice instanceof VoltageClampElectricalDevice) {
// if (edParm.getRole()==ElectricalDevice.ROLE_TotalCurrentDensity){
// return "I_"+((VoltageClampElectricalDevice)electricalDevice).getVoltageClampStimulus().getNameScope().getName();
// }
// if (edParm.getRole()==ElectricalDevice.ROLE_TransmembraneCurrentDensity){
// return "F_"+((VoltageClampElectricalDevice)electricalDevice).getVoltageClampStimulus().getNameScope().getName();
// }
}
}
if (ste instanceof LocalParameter && ((LocalParameter) ste).getNameScope() instanceof ElectricalStimulus.ElectricalStimulusNameScope) {
LocalParameter esParm = (LocalParameter) ste;
String nameWithScope = esParm.getNameScope().getName();
if (esParm.getRole() == ElectricalStimulusParameterType.TotalCurrent) {
return "I_" + nameWithScope;
} else if (esParm.getRole() == ElectricalStimulusParameterType.Voltage) {
return "V_" + nameWithScope;
}
}
StructureTopology structTopology = model.getStructureTopology();
if (ste instanceof StructureMapping.StructureMappingParameter) {
StructureMapping.StructureMappingParameter smParm = (StructureMapping.StructureMappingParameter) ste;
Structure structure = ((StructureMapping) (smParm.getNameScope().getScopedSymbolTable())).getStructure();
int role = smParm.getRole();
if (role == StructureMapping.ROLE_VolumeFraction) {
return "VolFract_" + (structTopology.getInsideFeature((Membrane) structure)).getNameScope().getName();
} else {
String nameWithScope = structure.getNameScope().getName();
if (role == StructureMapping.ROLE_SurfaceToVolumeRatio) {
return "SurfToVol_" + nameWithScope;
} else if (role == StructureMapping.ROLE_InitialVoltage) {
return smParm.getName();
} else if (role == StructureMapping.ROLE_SpecificCapacitance) {
return "C_" + nameWithScope;
} else if (role == StructureMapping.ROLE_AreaPerUnitArea) {
return "AreaPerUnitArea_" + nameWithScope;
} else if (role == StructureMapping.ROLE_AreaPerUnitVolume) {
return "AreaPerUnitVolume_" + nameWithScope;
} else if (role == StructureMapping.ROLE_VolumePerUnitArea) {
return "VolumePerUnitArea_" + nameWithScope;
} else if (role == StructureMapping.ROLE_VolumePerUnitVolume) {
return "VolumePerUnitVolume_" + nameWithScope;
} else if (role == StructureMapping.ROLE_Size) {
if (simContext.getGeometry().getDimension() == 0) {
// if geometry is compartmental, make sure compartment sizes are set if referenced in model.
if (smParm.getExpression() == null || smParm.getExpression().isZero()) {
throw new MappingException("\nIn non-spatial application '" + getSimulationContext().getName() + "', " + "size of structure '" + structure.getName() + "' must be assigned a " + "positive value if referenced in the model.\n\nPlease go to 'Structure Mapping' tab to check the size.");
}
}
return "Size_" + nameWithScope;
}
}
}
//
if (ste instanceof SpeciesContext) {
SpeciesContext sc = (SpeciesContext) ste;
SpeciesContextMapping scm = getSpeciesContextMapping(sc);
//
if (structureMapping instanceof FeatureMapping) {
//
if (scm.getVariable() != null && !scm.getVariable().getName().equals(steName)) {
return scm.getVariable().getName();
}
//
// for reactions within a spatially resolved membrane, may need "_INSIDE" or "_OUTSIDE" for jump condition
//
// if the membrane is distributed, then always use the plain variable.
//
} else if (structureMapping instanceof MembraneMapping) {
Membrane membrane = ((MembraneMapping) structureMapping).getMembrane();
//
if (sc.getStructure() instanceof Membrane || getResolved(structureMapping) == false) {
if (scm.getVariable() != null && !(scm.getVariable().getName().equals(steName))) {
return scm.getVariable().getName();
}
//
// if the speciesContext is outside the membrane
//
} else {
SpeciesContextSpec scs = simContext.getReactionContext().getSpeciesContextSpec(sc);
if (sc.getStructure() == structTopology.getInsideFeature(membrane) || sc.getStructure() == structTopology.getOutsideFeature(membrane)) {
if (getResolved(structureMapping) && !scs.isConstant()) {
if (!scs.isDiffusing()) {
throw new MappingException("Enable diffusion in Application '" + simContext.getName() + "'. This must be done for any species (e.g '" + sc.getName() + "') in flux reactions.\n\n" + "To save or run simulations, set the diffusion rate to a non-zero " + "value in Initial Conditions or disable those reactions in Specifications->Reactions.");
}
return scm.getVariable().getName() + (sc.getStructure() == structTopology.getInsideFeature(membrane) ? "_INSIDE" : "_OUTSIDE");
} else {
return scm.getSpeciesContext().getName();
}
} else {
throw new MappingException(sc.getName() + " shouldn't be involved with structure " + structureMapping.getStructure().getName());
}
}
}
}
return getNameScope().getSymbolName(ste);
}
use of cbit.vcell.model.ProxyParameter in project vcell by virtualcell.
the class BioModelParametersTableModel method bioModelChange.
@Override
protected void bioModelChange(PropertyChangeEvent evt) {
super.bioModelChange(evt);
BioModel oldValue = (BioModel) evt.getOldValue();
if (oldValue != null) {
for (EditableSymbolTableEntry parameter : oldValue.getModel().getModelParameters()) {
parameter.removePropertyChangeListener(this);
}
for (SpeciesContext sc : oldValue.getModel().getSpeciesContexts()) {
sc.removePropertyChangeListener(this);
}
for (ReactionStep reactionStep : oldValue.getModel().getReactionSteps()) {
reactionStep.removePropertyChangeListener(this);
Kinetics kinetics = reactionStep.getKinetics();
kinetics.removePropertyChangeListener(this);
for (KineticsParameter kineticsEditableSymbolTableEntry : kinetics.getKineticsParameters()) {
kineticsEditableSymbolTableEntry.removePropertyChangeListener(this);
}
for (ProxyParameter proxyEditableSymbolTableEntry : kinetics.getProxyParameters()) {
proxyEditableSymbolTableEntry.removePropertyChangeListener(this);
}
for (UnresolvedParameter unresolvedEditableSymbolTableEntry : kinetics.getUnresolvedParameters()) {
unresolvedEditableSymbolTableEntry.removePropertyChangeListener(this);
}
}
for (SimulationContext simulationContext : oldValue.getSimulationContexts()) {
simulationContext.removePropertyChangeListener(this);
simulationContext.getGeometryContext().removePropertyChangeListener(this);
for (StructureMapping mapping : simulationContext.getGeometryContext().getStructureMappings()) {
mapping.removePropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : mapping.getParameters()) {
parameter.removePropertyChangeListener(this);
}
}
simulationContext.getReactionContext().removePropertyChangeListener(this);
for (SpeciesContextSpec spec : simulationContext.getReactionContext().getSpeciesContextSpecs()) {
spec.removePropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : spec.getParameters()) {
parameter.removePropertyChangeListener(this);
}
}
for (ElectricalStimulus elect : simulationContext.getElectricalStimuli()) {
elect.removePropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : elect.getParameters()) {
parameter.removePropertyChangeListener(this);
}
}
for (SpatialObject spatialObject : simulationContext.getSpatialObjects()) {
spatialObject.removePropertyChangeListener(this);
}
for (SpatialProcess spatialProcess : simulationContext.getSpatialProcesses()) {
spatialProcess.removePropertyChangeListener(this);
for (LocalParameter p : spatialProcess.getParameters()) {
p.removePropertyChangeListener(this);
}
}
for (SimulationContextParameter p : simulationContext.getSimulationContextParameters()) {
p.removePropertyChangeListener(this);
}
}
}
BioModel newValue = (BioModel) evt.getNewValue();
if (newValue != null) {
for (ModelParameter modelEditableSymbolTableEntry : newValue.getModel().getModelParameters()) {
modelEditableSymbolTableEntry.addPropertyChangeListener(this);
}
for (SpeciesContext sc : newValue.getModel().getSpeciesContexts()) {
sc.addPropertyChangeListener(this);
}
for (ReactionStep reactionStep : newValue.getModel().getReactionSteps()) {
reactionStep.addPropertyChangeListener(this);
Kinetics kinetics = reactionStep.getKinetics();
kinetics.addPropertyChangeListener(this);
for (KineticsParameter kineticsEditableSymbolTableEntry : kinetics.getKineticsParameters()) {
kineticsEditableSymbolTableEntry.addPropertyChangeListener(this);
}
for (ProxyParameter proxyEditableSymbolTableEntry : kinetics.getProxyParameters()) {
proxyEditableSymbolTableEntry.addPropertyChangeListener(this);
}
for (UnresolvedParameter unresolvedEditableSymbolTableEntry : kinetics.getUnresolvedParameters()) {
unresolvedEditableSymbolTableEntry.addPropertyChangeListener(this);
}
}
for (SimulationContext simulationContext : newValue.getSimulationContexts()) {
simulationContext.addPropertyChangeListener(this);
simulationContext.getGeometryContext().addPropertyChangeListener(this);
for (StructureMapping mapping : simulationContext.getGeometryContext().getStructureMappings()) {
mapping.addPropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : mapping.getParameters()) {
parameter.addPropertyChangeListener(this);
}
}
simulationContext.getReactionContext().addPropertyChangeListener(this);
for (SpeciesContextSpec spec : simulationContext.getReactionContext().getSpeciesContextSpecs()) {
spec.addPropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : spec.getParameters()) {
parameter.addPropertyChangeListener(this);
}
}
for (ElectricalStimulus elect : simulationContext.getElectricalStimuli()) {
elect.addPropertyChangeListener(this);
for (EditableSymbolTableEntry parameter : elect.getParameters()) {
parameter.addPropertyChangeListener(this);
}
}
for (SpatialObject spatialObject : simulationContext.getSpatialObjects()) {
spatialObject.addPropertyChangeListener(this);
}
for (SpatialProcess spatialProcess : simulationContext.getSpatialProcesses()) {
spatialProcess.addPropertyChangeListener(this);
for (LocalParameter p : spatialProcess.getParameters()) {
p.addPropertyChangeListener(this);
}
}
for (SimulationContextParameter p : simulationContext.getSimulationContextParameters()) {
p.addPropertyChangeListener(this);
}
}
}
}
Aggregations