use of cbit.vcell.mapping.SpeciesContextSpec in project vcell by virtualcell.
the class StructureAnalyzer method refreshTotalDependancies.
/**
* This method was created by a SmartGuide.
* @param b cbit.vcell.math.Matrix
* @param vars java.lang.String[]
*/
static StructureAnalyzer.Dependency[] refreshTotalDependancies(RationalMatrix nullSpaceMatrix, SpeciesContextMapping[] speciesContextMappings, MathMapping_4_8 argMathMapping, boolean bFast) throws Exception {
// System.out.println("StructureAnalyzer.refreshTotalDependancies()");
SimulationContext argSimContext = argMathMapping.getSimulationContext();
if (nullSpaceMatrix == null) {
// System.out.println("the matrix has full rank, there are no dependencies");
return new StructureAnalyzer.Dependency[0];
}
if (speciesContextMappings.length != nullSpaceMatrix.getNumCols()) {
// throw new Exception("varName array not same dimension as b matrix");
System.out.println("varName array not same dimension as b matrix");
nullSpaceMatrix.show();
for (int i = 0; i < speciesContextMappings.length; i++) {
System.out.println("scm[" + i + "] is " + speciesContextMappings[i].getSpeciesContext().getName());
}
}
// System.out.println("there are "+nullSpaceMatrix.rows+" dependencies");
Vector<Dependency> dependencyList = new Vector<Dependency>();
for (int i = 0; i < nullSpaceMatrix.getNumRows(); i++) {
//
// find first variable
//
Expression exp = null;
Expression constantExp = null;
String constantName = null;
SpeciesContextMapping firstSCM = null;
boolean bFirst = true;
for (int j = 0; j < nullSpaceMatrix.getNumCols(); j++) {
RationalNumber coeff = nullSpaceMatrix.get_elem(i, j);
if (coeff.doubleValue() != 0.0) {
if (bFirst) {
if (coeff.doubleValue() != 1.0) {
System.out.println("i=" + i + " j=" + j);
nullSpaceMatrix.show();
throw new Exception("expecting a coefficient of 1.0, instead coeff = " + coeff.infix());
}
firstSCM = speciesContextMappings[j];
//
// first term of dependancy expression ("K_CalciumCyt")
//
SpeciesContext firstSC = firstSCM.getSpeciesContext();
constantName = "K_" + firstSC.getName() + "_total";
exp = new Expression(constantName);
//
// first term of K expression
//
StructureMapping firstSM = argSimContext.getGeometryContext().getStructureMapping(firstSC.getStructure());
SpeciesContextSpec firstSCS = argSimContext.getReactionContext().getSpeciesContextSpec(firstSC);
SymbolTableEntry scSTE = null;
if (bFast) {
scSTE = firstSCS.getSpeciesContext();
} else {
scSTE = firstSCS.getParameterFromRole(SpeciesContextSpec.ROLE_InitialConcentration);
}
constantExp = Expression.mult(new Expression(coeff), firstSM.getNormalizedConcentrationCorrection(argSimContext, argMathMapping), new Expression(scSTE, argMathMapping.getNameScope()));
bFirst = false;
} else {
//
// add term to dependancy expression (" - 2*IP3Cyt ")
//
SpeciesContextMapping scm = speciesContextMappings[j];
SpeciesContext sc = scm.getSpeciesContext();
StructureMapping sm = argSimContext.getGeometryContext().getStructureMapping(sc.getStructure());
SpeciesContextSpec scs = argSimContext.getReactionContext().getSpeciesContextSpec(sc);
exp = Expression.add(exp, Expression.negate(Expression.mult(new Expression(coeff), sm.getNormalizedConcentrationCorrection(argSimContext, argMathMapping), new Expression(sc, argMathMapping.getNameScope()))));
//
// add term to K expression
//
SymbolTableEntry scSTE = null;
if (bFast) {
scSTE = sc;
} else {
scSTE = scs.getParameterFromRole(SpeciesContextSpec.ROLE_InitialConcentration);
}
constantExp = Expression.add(constantExp, Expression.mult(new Expression(coeff), sm.getNormalizedConcentrationCorrection(argSimContext, argMathMapping), new Expression(scSTE, argMathMapping.getNameScope())));
}
}
}
if (firstSCM != null) {
//
// store totalMass parameter (e.g. K_xyz_total = xyz_init + wzy_init)
//
// MathMapping.MathMappingParameter totalMassParameter = mathMapping.addMathMappingParameter(constantName,constantExp.flatten(),MathMapping.PARAMETER_ROLE_TOTALMASS,cbit.vcell.units.VCUnitDefinition.UNIT_uM);
//
// store dependency parameter (e.g. xyz = K_xyz_total - wzy)
//
StructureMapping sm = argSimContext.getGeometryContext().getStructureMapping(firstSCM.getSpeciesContext().getStructure());
exp = Expression.mult(exp, Expression.invert(sm.getNormalizedConcentrationCorrection(argSimContext, argMathMapping)));
exp = exp.flatten();
// exp.bindExpression(mathMapping_temp);
// firstSCM.setDependencyExpression(exp);
Dependency dependency = new Dependency();
dependency.dependencyExpression = exp;
dependency.speciesContextMapping = firstSCM;
dependency.invariantSymbolName = constantName;
dependency.conservedMoietyExpression = constantExp.flatten();
dependencyList.add(dependency);
}
}
return (StructureAnalyzer.Dependency[]) BeanUtils.getArray(dependencyList, StructureAnalyzer.Dependency.class);
}
use of cbit.vcell.mapping.SpeciesContextSpec in project vcell by virtualcell.
the class BioModelParametersTableModel method getApplicationEditableSymbolTableEntryList.
private List<EditableSymbolTableEntry> getApplicationEditableSymbolTableEntryList(SimulationContext simulationContext) {
ArrayList<EditableSymbolTableEntry> parameterList = new ArrayList<EditableSymbolTableEntry>();
Map<String, SymbolTableEntry> entryMap = new HashMap<String, SymbolTableEntry>();
simulationContext.getEntries(entryMap);
for (SymbolTableEntry ste : entryMap.values()) {
if (ste instanceof EditableSymbolTableEntry && ste.getNameScope() == simulationContext.getNameScope()) {
parameterList.add((EditableSymbolTableEntry) ste);
}
}
for (StructureMapping mapping : simulationContext.getGeometryContext().getStructureMappings()) {
parameterList.addAll(mapping.computeApplicableParameterList());
}
for (SpeciesContextSpec spec : simulationContext.getReactionContext().getSpeciesContextSpecs()) {
parameterList.addAll(spec.computeApplicableParameterList());
}
for (ElectricalStimulus elect : simulationContext.getElectricalStimuli()) {
parameterList.addAll(Arrays.asList(elect.getParameters()));
}
for (SpatialProcess sp : simulationContext.getSpatialProcesses()) {
parameterList.addAll(Arrays.asList(sp.getParameters()));
}
return parameterList;
}
use of cbit.vcell.mapping.SpeciesContextSpec 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);
}
}
}
}
use of cbit.vcell.mapping.SpeciesContextSpec in project vcell by virtualcell.
the class RbmNetworkGenerator method writeBngl_internal.
public static void writeBngl_internal(SimulationContext simulationContext, PrintWriter writer, Map<FakeReactionRuleRateParameter, LocalParameter> kineticsParameterMap, Map<FakeSeedSpeciesInitialConditionsParameter, Pair<SpeciesContext, Expression>> speciesEquivalenceMap, NetworkGenerationRequirements networkGenerationRequirements, CompartmentMode compartmentMode) {
String callerClassName = new Exception().getStackTrace()[1].getClassName();
String networkTransformerClassName = NetworkTransformer.class.getName();
String rulebasedTransformerClassName = RulebasedTransformer.class.getName();
if (!callerClassName.equals(networkTransformerClassName) && !callerClassName.equals(rulebasedTransformerClassName)) {
throw new UnsupportedOperationException("This method may only be called from within a " + networkTransformerClassName + " or " + rulebasedTransformerClassName + " instance.");
}
Model model = simulationContext.getModel();
RbmModelContainer rbmModelContainer = model.getRbmModelContainer();
checkConsistency(model);
// first we prepare the fake parameters we need to maintain the relationship between the species context and the seed species
List<FakeSeedSpeciesInitialConditionsParameter> fakeParameterList = new ArrayList<FakeSeedSpeciesInitialConditionsParameter>();
List<String> seedSpeciesList = new ArrayList<String>();
SpeciesContext[] speciesContexts = model.getSpeciesContexts();
for (int i = 0; i < speciesContexts.length; i++) {
SpeciesContext sc = speciesContexts[i];
if (!sc.hasSpeciesPattern()) {
continue;
}
SpeciesContextSpec scs = simulationContext.getReactionContext().getSpeciesContextSpec(sc);
Expression initialConcentration = scs.getParameter(SpeciesContextSpec.ROLE_InitialConcentration).getExpression();
// fake initial values for the seed species, we need to present them to bngl as parameters
FakeSeedSpeciesInitialConditionsParameter fakeSeedSpeciesParam = new FakeSeedSpeciesInitialConditionsParameter(sc.getName());
Pair<SpeciesContext, Expression> p = new Pair<SpeciesContext, Expression>(sc, initialConcentration);
speciesEquivalenceMap.put(fakeSeedSpeciesParam, p);
String modified;
if (compartmentMode == CompartmentMode.show) {
modified = RbmUtils.toBnglString(sc.getSpeciesPattern(), null, CompartmentMode.hide, 0);
modified = "@" + sc.getStructure().getName() + ":" + modified;
} else if (compartmentMode == CompartmentMode.asSite) {
modified = RbmUtils.toBnglString(sc.getSpeciesPattern(), sc.getStructure(), CompartmentMode.asSite, 0);
} else {
// CompartmentMode.hide
modified = RbmUtils.toBnglString(sc.getSpeciesPattern(), null, CompartmentMode.hide, 0);
}
modified += " " + fakeSeedSpeciesParam.fakeParameterName;
// we label the seed species with the index
modified = (i + 1) + " " + modified;
// we build the seed species list now, we write it later (in the BEGIN SPECIES block)
seedSpeciesList.add(modified);
fakeParameterList.add(fakeSeedSpeciesParam);
}
// second we produce the bngl file
writer.println(BEGIN_MODEL);
writer.println();
for (ReactionRuleSpec rrs : simulationContext.getReactionContext().getReactionRuleSpecs()) {
if (!rrs.isExcluded()) {
ReactionRule reactionRule = rrs.getReactionRule();
RbmKineticLaw kineticLaw = reactionRule.getKineticLaw();
switch(kineticLaw.getRateLawType()) {
case MassAction:
{
FakeReactionRuleRateParameter fakeRateParameterForward = new FakeReactionRuleRateParameter(reactionRule, RbmKineticLawParameterType.MassActionForwardRate);
LocalParameter origForwardRateParameter = kineticLaw.getLocalParameter(RbmKineticLawParameterType.MassActionForwardRate);
kineticsParameterMap.put(fakeRateParameterForward, origForwardRateParameter);
if (reactionRule.isReversible()) {
FakeReactionRuleRateParameter fakeRateParameterReverse = new FakeReactionRuleRateParameter(reactionRule, RbmKineticLawParameterType.MassActionReverseRate);
LocalParameter origReverseRateParameter = kineticLaw.getLocalParameter(RbmKineticLawParameterType.MassActionReverseRate);
kineticsParameterMap.put(fakeRateParameterReverse, origReverseRateParameter);
}
break;
}
default:
{
throw new RuntimeException("kinetic law type " + kineticLaw.getRateLawType().name() + " not yet implemented");
}
}
}
}
if (compartmentMode == CompartmentMode.show) {
RbmNetworkGenerator.writeCompartments(writer, model, simulationContext);
}
writer.println(BEGIN_PARAMETERS);
// the fake parameters used for reaction rule kinetics
for (FakeReactionRuleRateParameter p : kineticsParameterMap.keySet()) {
writer.println(p.fakeParameterName + "\t\t1");
}
// the fake parameters used at initial values for the seed species
for (FakeSeedSpeciesInitialConditionsParameter s : fakeParameterList) {
writer.println(s.fakeParameterName + "\t\t1");
}
writer.println(END_PARAMETERS);
writer.println();
RbmNetworkGenerator.writeMolecularTypes(writer, model, compartmentMode);
// write modified version of seed species while maintaining the connection between the species context and the real seed species
writer.println(BEGIN_SPECIES);
for (String s : seedSpeciesList) {
writer.println(s);
}
writer.println(END_SPECIES);
writer.println();
RbmNetworkGenerator.writeObservables(writer, rbmModelContainer, compartmentMode);
RbmNetworkGenerator.writeReactions_internal(writer, simulationContext, compartmentMode);
writer.println(END_MODEL);
writer.println();
if (callerClassName.equals(networkTransformerClassName)) {
RbmNetworkGenerator.writeNetworkConstraints(writer, rbmModelContainer, simulationContext, networkGenerationRequirements);
} else if (callerClassName.equals(rulebasedTransformerClassName)) {
writer.println();
writer.println("writeXML()");
}
writer.println();
}
use of cbit.vcell.mapping.SpeciesContextSpec in project vcell by virtualcell.
the class RbmUtils method toBnglString.
public static String toBnglString(SimulationContext sc, SpeciesContext speciesContext, CompartmentMode compartmentMode) {
SpeciesContextSpec scs = sc.getReactionContext().getSpeciesContextSpec(speciesContext);
Expression expression = scs.getParameter(SpeciesContextSpec.ROLE_InitialConcentration).getExpression();
SpeciesPattern sp = speciesContext.getSpeciesPattern();
String s = "";
if (compartmentMode == CompartmentMode.show) {
s += "@" + speciesContext.getStructure().getName() + ":";
s += toBnglString(sp, null, CompartmentMode.hide, 0) + "";
} else if (compartmentMode == CompartmentMode.asSite) {
s += toBnglString(sp, speciesContext.getStructure(), CompartmentMode.asSite, 0) + "";
} else {
s += toBnglString(sp, null, CompartmentMode.hide, 0) + "";
}
s += " " + expression.infix();
return s;
}
Aggregations