use of cbit.vcell.mapping.ParameterContext.LocalParameter in project vcell by virtualcell.
the class RbmKineticLaw method clone.
public static void clone(RbmKineticLaw toKineticLaw, ReactionRule fromRule, ReactionRule toRule) {
RbmKineticLaw fromKineticLaw = fromRule.getKineticLaw();
Model toModel = toRule.getModel();
Model fromModel = fromRule.getModel();
try {
for (LocalParameter fromLocalParameter : fromKineticLaw.parameterContext.getLocalParameters()) {
if (fromLocalParameter.getRole() != RbmKineticLawParameterType.UserDefined) {
// find built-in parameter with same role and copy expression.
// copy name also in case it was changed.
LocalParameter toLocalParameter = toKineticLaw.parameterContext.getLocalParameterFromRole(fromLocalParameter.getRole());
String toName = new String(fromLocalParameter.getName());
if (!toName.equals(toLocalParameter.getName())) {
// new name must be different from existing name, no point to rename otherwise
toLocalParameter.setName(toName);
}
Expression fromExpression = fromLocalParameter.getExpression();
if (fromExpression != null) {
Expression toExpression = new Expression(fromExpression);
toKineticLaw.setParameterValue(toLocalParameter, toExpression, true);
}
// TODO: units are not updated!
}
}
for (LocalParameter fromLocalParameter : fromKineticLaw.parameterContext.getLocalParameters()) {
if (fromLocalParameter.getRole() == RbmKineticLawParameterType.UserDefined) {
LocalParameter toLocalParameter = toKineticLaw.getLocalParameter(fromLocalParameter.getName());
if (toLocalParameter == 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 : fromKineticLaw.getProxyParameters()) {
if (proxyParameter.getName().equals(fromLocalParameter.getName())) {
rule_proxy_parameter = (LocalProxyParameter) proxyParameter;
}
}
if (rule_proxy_parameter != null) {
// we want to convert to local
boolean bConvertToGlobal = false;
toKineticLaw.convertParameterType(rule_proxy_parameter, bConvertToGlobal);
} else {
// could find neither local parameter nor proxy parameter
throw new RuntimeException("user defined parameter " + fromLocalParameter.getName() + " from reactionRule " + fromRule.getName() + " didn't map to a reactionRule parameter");
}
} else if (toLocalParameter.getRole() == RbmKineticLawParameterType.UserDefined) {
Expression expression = fromLocalParameter.getExpression();
if (expression != null) {
toKineticLaw.setParameterValue(toLocalParameter, expression, true);
}
// TODO: make copy constructor for VCUnitDefinition
// VCUnitDefinition toUnitDefinition = new VCUnitDefinition(fromLocalParameter.getUnitDefinition());
// toLocalParameter.setUnitDefinition(toUnitDefinition);
} else {
throw new RuntimeException("user defined parameter " + fromLocalParameter.getName() + " from reactionRule " + fromRule.getName() + " mapped to a reactionRule parameter with unexpected role " + toLocalParameter.getRole().getDescription());
}
}
}
// TODO: some params were assumed to be local but they are global in the 'from'
// need to find if we have a global with same name, rename at need and make it global
} catch (PropertyVetoException | ExpressionException e) {
e.printStackTrace();
throw new RuntimeException("Problem attempting to set RbmKineticLaw expression: " + e.getMessage());
}
}
use of cbit.vcell.mapping.ParameterContext.LocalParameter in project vcell by virtualcell.
the class RbmKineticLaw method duplicate.
public static void duplicate(RbmKineticLaw to, ReactionRule fromRule) {
RbmKineticLaw from = fromRule.getKineticLaw();
try {
for (LocalParameter fromLocalParameter : from.parameterContext.getLocalParameters()) {
if (fromLocalParameter.getRole() != RbmKineticLawParameterType.UserDefined) {
// find built-in parameter with same role and copy expression.
// copy name also in case it was changed.
LocalParameter toLocalParameter = to.parameterContext.getLocalParameterFromRole(fromLocalParameter.getRole());
toLocalParameter.setName(fromLocalParameter.getName());
Expression expression = fromLocalParameter.getExpression();
if (expression != null) {
to.setParameterValue(toLocalParameter, new Expression(expression), true);
}
}
}
for (LocalParameter fromLocalParameter : from.parameterContext.getLocalParameters()) {
if (fromLocalParameter.getRole() == RbmKineticLawParameterType.UserDefined) {
LocalParameter toLocalParameter = to.getLocalParameter(fromLocalParameter.getName());
if (toLocalParameter == 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 : from.getProxyParameters()) {
if (proxyParameter.getName().equals(fromLocalParameter.getName())) {
rule_proxy_parameter = (LocalProxyParameter) proxyParameter;
}
}
if (rule_proxy_parameter != null) {
// we want to convert to local
boolean bConvertToGlobal = false;
to.convertParameterType(rule_proxy_parameter, bConvertToGlobal);
} else {
// could find neither local parameter nor proxy parameter
throw new RuntimeException("user defined parameter " + fromLocalParameter.getName() + " from reactionRule " + fromRule.getName() + " didn't map to a reactionRule parameter");
}
} else if (toLocalParameter.getRole() == RbmKineticLawParameterType.UserDefined) {
Expression expression = fromLocalParameter.getExpression();
if (expression != null) {
to.setParameterValue(toLocalParameter, expression, true);
}
toLocalParameter.setUnitDefinition(fromLocalParameter.getUnitDefinition());
} else {
throw new RuntimeException("user defined parameter " + fromLocalParameter.getName() + " from reactionRule " + fromRule.getName() + " mapped to a reactionRule parameter with unexpected role " + toLocalParameter.getRole().getDescription());
}
}
}
} catch (PropertyVetoException | ExpressionException e) {
e.printStackTrace();
throw new RuntimeException("Problem attempting to set RbmKineticLaw expression: " + e.getMessage());
}
}
use of cbit.vcell.mapping.ParameterContext.LocalParameter in project vcell by virtualcell.
the class RbmKineticLaw method refreshUnits.
protected void refreshUnits() {
if (bRefreshingUnits) {
return;
}
try {
bRefreshingUnits = true;
ModelUnitSystem modelUnitSystem = reactionRule.getModel().getUnitSystem();
LocalParameter rateParm = getLocalParameter(RbmKineticLawParameterType.RuleRate);
if (reactionRule.getStructure() instanceof Feature) {
rateParm.setUnitDefinition(modelUnitSystem.getVolumeReactionRateUnit());
} else if (reactionRule.getStructure() instanceof Membrane) {
rateParm.setUnitDefinition(modelUnitSystem.getMembraneReactionRateUnit());
}
switch(this.rateLawType) {
case MassAction:
{
LocalParameter forwardRateParm = getLocalParameter(RbmKineticLawParameterType.MassActionForwardRate);
LocalParameter reverseRateParm = getLocalParameter(RbmKineticLawParameterType.MassActionReverseRate);
// since units for kinetic parameters are set from model's unit system, if model is null (possible when model is not yet set on reactionStep when reading from XML)
// don't worry about setting units on kinetic parameters. Call this method when model is set on reactionStep (rebindToModel()).
Model model = reactionRule.getModel();
if (model != null) {
// if (reactionRule.getStructure() instanceof Membrane){
// rateParm.setUnitDefinition(modelUnitSystem.getMembraneReactionRateUnit());
// if (currentDensityParm!=null){
// currentDensityParm.setUnitDefinition(modelUnitSystem.getCurrentDensityUnit());
// }
// KineticsParameter chargeValenceParm = getChargeValenceParameter();
// if (chargeValenceParm!=null){
// chargeValenceParm.setUnitDefinition(modelUnitSystem.getInstance_DIMENSIONLESS());
// }
// }else if (getReactionStep().getStructure() instanceof Feature){
// rateParm.setUnitDefinition(modelUnitSystem.getVolumeReactionRateUnit());
// }else{
// throw new RuntimeException("unexpected structure type "+getReactionStep().getStructure()+" in MassActionKinetics.refreshUnits()");
// }
cbit.vcell.units.VCUnitDefinition kfUnits = rateParm.getUnitDefinition();
cbit.vcell.units.VCUnitDefinition krUnits = rateParm.getUnitDefinition();
for (ReactantPattern reactantPattern : reactionRule.getReactantPatterns()) {
VCUnitDefinition reactantUnit = modelUnitSystem.getConcentrationUnit(reactantPattern.getStructure());
kfUnits = kfUnits.divideBy(reactantUnit);
}
for (ProductPattern productPattern : reactionRule.getProductPatterns()) {
VCUnitDefinition productUnit = modelUnitSystem.getConcentrationUnit(productPattern.getStructure());
krUnits = krUnits.divideBy(productUnit);
}
if (forwardRateParm != null && !kfUnits.compareEqual(forwardRateParm.getUnitDefinition())) {
forwardRateParm.setUnitDefinition(kfUnits);
}
if (reverseRateParm != null && !krUnits.compareEqual(reverseRateParm.getUnitDefinition())) {
reverseRateParm.setUnitDefinition(krUnits);
}
}
break;
}
}
// end switch (rateLawType)
} finally {
bRefreshingUnits = false;
}
}
use of cbit.vcell.mapping.ParameterContext.LocalParameter in project vcell by virtualcell.
the class ReactionRule method deriveInverseRule.
// returns a new rule consisting of the "inverse" part of the reversible old rule
public static ReactionRule deriveInverseRule(ReactionRule oldRule) throws ExpressionBindingException, PropertyVetoException {
Model m = oldRule.getModel();
Structure s = oldRule.getStructure();
boolean bR = false;
String newName = oldRule.getDisplayName() + InverseHalf;
ReactionRule newRule = new ReactionRule(m, newName, s, bR);
RbmKineticLaw oldLaw = oldRule.getKineticLaw();
RateLawType rateLawType = oldLaw.getRateLawType();
if (rateLawType != RateLawType.MassAction) {
throw new RuntimeException("Only Mass Action Kinetics supported at this time, " + ReactionRule.typeName + " \"" + oldRule.getName() + "\" uses kinetic law type \"" + rateLawType.toString() + "\"");
}
RbmKineticLaw newLaw = new RbmKineticLaw(newRule, rateLawType);
newRule.setKineticLaw(newLaw);
LocalParameter oldfr = oldLaw.getLocalParameter(RbmKineticLawParameterType.MassActionReverseRate);
Expression exp = oldfr.getExpression();
newLaw.setLocalParameterValue(RbmKineticLawParameterType.MassActionForwardRate, exp);
for (ReactantPattern oldrp : oldRule.getReactantPatterns()) {
SpeciesPattern newsp = new SpeciesPattern(m, oldrp.getSpeciesPattern());
ProductPattern newrp = new ProductPattern(newsp, oldrp.getStructure());
newRule.addProduct(newrp, false, false);
}
for (ProductPattern oldpp : oldRule.getProductPatterns()) {
SpeciesPattern newsp = new SpeciesPattern(m, oldpp.getSpeciesPattern());
ReactantPattern newpp = new ReactantPattern(newsp, oldpp.getStructure());
newRule.addReactant(newpp, false, false);
}
newLaw.bind(newRule);
return newRule;
}
use of cbit.vcell.mapping.ParameterContext.LocalParameter in project vcell by virtualcell.
the class StimulusTable method getSQLValueList.
/**
* This method was created in VisualAge.
* @return java.lang.String
* @param key KeyValue
* @param modelName java.lang.String
*/
public String getSQLValueList(InsertHashtable hash, KeyValue Key, KeyValue simContextKey, ElectricalStimulus stimulus) throws DataAccessException {
KeyValue structureKey = hash.getDatabaseKey(stimulus.getElectrode().getFeature());
if (structureKey == null) {
structureKey = stimulus.getElectrode().getFeature().getKey();
if (structureKey == null) {
throw new DataAccessException("no key for structure " + stimulus.getElectrode().getFeature());
}
}
Expression exp = null;
int stimulusType = UNKNOWN_STIMULUS;
if (stimulus instanceof CurrentDensityClampStimulus) {
stimulusType = CURRENTDENSITY_CLAMP_STIMULUS;
LocalParameter currentDensityParameter = ((CurrentDensityClampStimulus) stimulus).getCurrentDensityParameter();
exp = currentDensityParameter.getExpression();
} else if (stimulus instanceof TotalCurrentClampStimulus) {
stimulusType = TOTALCURRENT_CLAMP_STIMULUS;
LocalParameter totalCurrentParameter = ((TotalCurrentClampStimulus) stimulus).getCurrentParameter();
exp = totalCurrentParameter.getExpression();
} else if (stimulus instanceof VoltageClampStimulus) {
stimulusType = VOLTAGE_CLAMP_STIMULUS;
LocalParameter voltageParameter = ((VoltageClampStimulus) stimulus).getVoltageParameter();
exp = voltageParameter.getExpression();
} else {
throw new DataAccessException("storage for Stimulus type " + stimulus.getClass().getName() + " not yet supported");
}
java.io.StringWriter esParameterWriter = new java.io.StringWriter();
java.io.PrintWriter pw = new java.io.PrintWriter(esParameterWriter);
stimulus.parameterVCMLWrite(pw);
pw.flush();
pw.close();
StringBuffer buffer = new StringBuffer();
buffer.append("(");
buffer.append(Key + ",");
buffer.append(structureKey + ",");
buffer.append(simContextKey + ",");
buffer.append("'" + stimulus.getName() + "',");
buffer.append(stimulusType + ",");
buffer.append("'" + TokenMangler.getSQLEscapedString(exp.infix()) + "',");
buffer.append(stimulus.getElectrode().getPosition().getX() + ",");
buffer.append(stimulus.getElectrode().getPosition().getY() + ",");
buffer.append(stimulus.getElectrode().getPosition().getZ() + ",");
buffer.append("'" + TokenMangler.getSQLEscapedString(esParameterWriter.getBuffer().toString()) + "'");
buffer.append(")");
return buffer.toString();
}
Aggregations