use of cbit.vcell.model.ReactionStep in project vcell by virtualcell.
the class PotentialMapping method getTotalMembraneCurrent.
/**
* Insert the method's description here.
* Creation date: (2/19/2002 12:56:13 PM)
* @return cbit.vcell.parser.Expression
* @param simContext cbit.vcell.mapping.SimulationContext
* @param membrane cbit.vcell.model.Membrane
*/
private static Expression getTotalMembraneCurrent(SimulationContext simContext, Membrane membrane, MathMapping_4_8 mathMapping_4_8) throws ExpressionException {
MembraneMapping membraneMapping = (MembraneMapping) simContext.getGeometryContext().getStructureMapping(membrane);
if (!membraneMapping.getCalculateVoltage()) {
return new Expression(0.0);
}
//
// gather current terms
//
Expression currentExp = new Expression(0.0);
ReactionSpec[] reactionSpecs = simContext.getReactionContext().getReactionSpecs();
StructureMappingParameter sizeParameter = membraneMapping.getSizeParameter();
Expression area = null;
if (simContext.getGeometry().getDimension() == 0 && (sizeParameter.getExpression() == null || sizeParameter.getExpression().isZero())) {
System.out.println("size not set for membrane \"" + membrane.getName() + "\", refer to Structure Mapping in Application \"" + mathMapping_4_8.getSimulationContext().getName() + "\"");
area = membraneMapping.getNullSizeParameterValue();
} else {
area = new Expression(sizeParameter, mathMapping_4_8.getNameScope());
}
for (int i = 0; i < reactionSpecs.length; i++) {
//
if (reactionSpecs[i].isExcluded()) {
continue;
}
if (reactionSpecs[i].getReactionStep().getKinetics() instanceof DistributedKinetics) {
ReactionStep rs = reactionSpecs[i].getReactionStep();
DistributedKinetics distributedKinetics = (DistributedKinetics) rs.getKinetics();
if (rs.getStructure() == membrane) {
if (!distributedKinetics.getCurrentDensityParameter().getExpression().isZero()) {
//
// change sign convension from inward current to outward current (which is consistent with voltage convension)
//
currentExp = Expression.add(currentExp, Expression.negate(Expression.mult(new Expression(distributedKinetics.getCurrentDensityParameter(), mathMapping_4_8.getNameScope()), area)));
}
}
} else {
ReactionStep rs = reactionSpecs[i].getReactionStep();
LumpedKinetics lumpedKinetics = (LumpedKinetics) rs.getKinetics();
if (rs.getStructure() == membrane) {
if (!lumpedKinetics.getLumpedCurrentParameter().getExpression().isZero()) {
//
if (mathMapping_4_8.getResolved(membraneMapping)) {
throw new RuntimeException("math generation for total currents within spatial electrophysiology not yet implemented");
}
Expression lumpedCurrentSymbolExp = new Expression(lumpedKinetics.getLumpedCurrentParameter(), mathMapping_4_8.getNameScope());
currentExp = Expression.add(currentExp, Expression.negate(lumpedCurrentSymbolExp));
}
}
}
}
return currentExp.flatten();
}
use of cbit.vcell.model.ReactionStep in project vcell by virtualcell.
the class RbmNetworkGenerator method generateModel.
public static void generateModel(BioModel bioModel, String netfile) throws Exception {
Model model = bioModel.getModel();
Map<String, SpeciesContext> speciesMap = new HashMap<String, SpeciesContext>();
Map<String, ReactionStep> reactionMap = new HashMap<String, ReactionStep>();
List<ReactionLine> reactionLineList = new ArrayList<ReactionLine>();
BufferedReader br = new BufferedReader(new StringReader(netfile));
int reversibleCount = 0;
int reactionCount = 0;
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
line = line.trim();
if (line.equals(BEGIN_PARAMETERS)) {
while (true) {
String line2 = br.readLine();
line2 = line2.trim();
if (line2.length() == 0) {
continue;
}
if (line2.equals(END_PARAMETERS)) {
break;
}
StringTokenizer st = new StringTokenizer(line2);
String token1 = st.nextToken();
String token2 = st.nextToken();
String token3 = st.nextToken();
ModelParameter mp = model.new ModelParameter(token2, new Expression(token3), Model.ROLE_UserDefined, bioModel.getModel().getUnitSystem().getInstance_TBD());
model.addModelParameter(mp);
}
} else if (line.equals(BEGIN_SPECIES)) {
while (true) {
String line2 = br.readLine();
line2 = line2.trim();
if (line2.length() == 0) {
continue;
}
if (line2.equals(END_SPECIES)) {
break;
}
StringTokenizer st = new StringTokenizer(line2);
// no
String token1 = st.nextToken();
// pattern
String token2 = st.nextToken();
// initial condition
String token3 = st.nextToken();
String newname = token2.replaceAll("\\.", "_");
newname = newname.replaceAll("[\\(,][a-zA-Z]\\w*", "");
newname = newname.replaceAll("~|!\\d*", "");
newname = newname.replaceAll("\\(\\)", "");
newname = newname.replaceAll("\\)", "");
SpeciesContext sc = model.createSpeciesContext(model.getStructure(0));
sc.setName(newname);
bioModel.getVCMetaData().setFreeTextAnnotation(sc, token2);
bioModel.getVCMetaData().setFreeTextAnnotation(sc.getSpecies(), token2);
speciesMap.put(token1, sc);
}
} else if (line.equals(BEGIN_REACTIONS)) {
while (true) {
String line2 = br.readLine();
line2 = line2.trim();
if (line2.length() == 0) {
continue;
}
if (line2.equals(END_REACTIONS)) {
break;
}
++reactionCount;
StringTokenizer st = new StringTokenizer(line2);
String token1 = st.nextToken();
// reactants
String token2 = st.nextToken();
// products
String token3 = st.nextToken();
// rate
String token4 = st.nextToken();
String token5 = st.nextToken();
boolean bFoundReversible = false;
Expression rate = new Expression(token4);
for (ReactionLine rl : reactionLineList) {
if (token2.equals(rl.products) && token3.equals(rl.reactants) && token5.equals(rl.ruleLabel + "r")) {
ReactionStep rs = reactionMap.get(rl.no);
((MassActionKinetics) rs.getKinetics()).getReverseRateParameter().setExpression(rate);
reactionLineList.remove(rl);
bFoundReversible = true;
break;
}
}
if (bFoundReversible) {
++reversibleCount;
continue;
}
ReactionLine rl = new ReactionLine(token1, token2, token3, token5);
reactionLineList.add(rl);
SimpleReaction reaction = model.createSimpleReaction(model.getStructure(0));
reactionMap.put(token1, reaction);
reaction.setModel(model);
bioModel.getVCMetaData().setFreeTextAnnotation(reaction, line2);
MassActionKinetics kinetics = new MassActionKinetics(reaction);
reaction.setKinetics(kinetics);
st = new StringTokenizer(token2, ",");
while (st.hasMoreTokens()) {
String t = st.nextToken();
SpeciesContext sc = speciesMap.get(t);
if (sc != null) {
boolean bExists = false;
for (ReactionParticipant rp : reaction.getReactionParticipants()) {
if (rp instanceof Reactant && rp.getSpeciesContext() == sc) {
rp.setStoichiometry(rp.getStoichiometry() + 1);
bExists = true;
break;
}
}
if (!bExists) {
reaction.addReactant(sc, 1);
}
}
}
st = new StringTokenizer(token3, ",");
while (st.hasMoreTokens()) {
String t = st.nextToken();
SpeciesContext sc = speciesMap.get(t);
if (sc != null) {
boolean bExists = false;
for (ReactionParticipant rp : reaction.getReactionParticipants()) {
if (rp instanceof Product && rp.getSpeciesContext() == sc) {
rp.setStoichiometry(rp.getStoichiometry() + 1);
bExists = true;
break;
}
}
if (!bExists) {
reaction.addProduct(sc, 1);
}
}
}
kinetics.getForwardRateParameter().setExpression(rate);
}
}
}
System.out.println(model.getNumSpecies() + " species added");
System.out.println(model.getNumReactions() + " reactions added");
System.out.println(reversibleCount + " reversible reactions found");
if (reactionCount != model.getNumReactions() + reversibleCount) {
throw new RuntimeException("Reactions are not imported correctly!");
}
}
use of cbit.vcell.model.ReactionStep in project vcell by virtualcell.
the class StochMathMapping_4_8 method refreshSpeciesContextMappings.
/**
* Insert the method's description here.
* Creation date: (10/26/2006 11:47:26 AM)
* @exception cbit.vcell.parser.ExpressionException The exception description.
* @exception cbit.vcell.mapping.MappingException The exception description.
* @exception cbit.vcell.math.MathException The exception description.
*/
private void refreshSpeciesContextMappings() throws cbit.vcell.parser.ExpressionException, MappingException, cbit.vcell.math.MathException {
//
// create a SpeciesContextMapping for each speciesContextSpec.
//
// set initialExpression from SpeciesContextSpec.
// set diffusing5
// set variable (only if "Constant" or "Function", else leave it as null)-----why commented?
//
//
// have to put geometric paras into mathsymbolmapping, since species initial condition needs the volume size symbol.
// and the parameters later on were added into contants or functions in refreshMathDescription()
//
StructureMapping[] structureMappings = getSimulationContext().getGeometryContext().getStructureMappings();
for (int i = 0; i < structureMappings.length; i++) {
StructureMapping sm = structureMappings[i];
StructureMapping.StructureMappingParameter parm = sm.getParameterFromRole(StructureMapping.ROLE_Size);
getMathSymbol(parm, sm);
}
getSpeciesContextMappingList().removeAllElements();
SpeciesContextSpec[] speciesContextSpecs = getSimulationContext().getReactionContext().getSpeciesContextSpecs();
for (int i = 0; i < speciesContextSpecs.length; i++) {
SpeciesContextSpec scs = speciesContextSpecs[i];
SpeciesContextMapping scm = new SpeciesContextMapping(scs.getSpeciesContext());
scm.setPDERequired(getSimulationContext().isPDERequired(scs.getSpeciesContext()));
scm.setHasEventAssignment(getSimulationContext().hasEventAssignment(scs.getSpeciesContext()));
scm.setHasHybridReaction(false);
for (ReactionSpec reactionSpec : getSimulationContext().getReactionContext().getReactionSpecs()) {
if (!reactionSpec.isExcluded() && reactionSpec.hasHybrid(getSimulationContext(), scs.getSpeciesContext())) {
scm.setHasHybridReaction(true);
}
}
// scm.setAdvecting(isAdvectionRequired(scs.getSpeciesContext()));
if (scs.isConstant()) {
SpeciesContextSpec.SpeciesContextSpecParameter initCountParm = scs.getInitialCountParameter();
SpeciesContextSpec.SpeciesContextSpecParameter initConcParm = scs.getInitialConcentrationParameter();
Expression initCondInCount = null;
// initial condition is concentration
if (initConcParm != null && initConcParm.getExpression() != null) {
initCondInCount = getExpressionConcToAmt(new Expression(initConcParm, getNameScope()), speciesContextSpecs[i].getSpeciesContext());
} else {
initCondInCount = new Expression(initCountParm, getNameScope());
}
// initCondInCount.bindExpression(this);
initCondInCount = getSubstitutedExpr(initCondInCount, true, true);
scm.setDependencyExpression(initCondInCount);
}
//
// test if participant in fast reaction step, request elimination if possible
//
scm.setFastParticipant(false);
ReactionSpec[] reactionSpecs = getSimulationContext().getReactionContext().getReactionSpecs();
for (int j = 0; j < reactionSpecs.length; j++) {
ReactionSpec reactionSpec = reactionSpecs[j];
if (reactionSpec.isExcluded()) {
continue;
}
ReactionStep rs = reactionSpec.getReactionStep();
if (rs instanceof SimpleReaction && rs.countNumReactionParticipants(scs.getSpeciesContext()) > 0) {
if (reactionSpec.isFast()) {
scm.setFastParticipant(true);
}
}
}
getSpeciesContextMappingList().addElement(scm);
}
}
use of cbit.vcell.model.ReactionStep in project vcell by virtualcell.
the class ModelOptimizationSpec method calculateTimeDependentModelObjects.
/**
* Insert the method's description here.
* Creation date: (11/29/2005 5:10:51 PM)
* @return cbit.vcell.parser.SymbolTableEntry[]
*/
public static SymbolTableEntry[] calculateTimeDependentModelObjects(SimulationContext simulationContext) {
Graph digraph = new Graph();
//
// add time
//
Model model = simulationContext.getModel();
Node timeNode = new Node("t", model.getTIME());
digraph.addNode(timeNode);
//
// add all species concentrations (that are not fixed with a constant initial condition).
//
SpeciesContextSpec[] scs = simulationContext.getReactionContext().getSpeciesContextSpecs();
for (int i = 0; scs != null && i < scs.length; i++) {
SpeciesContextSpecParameter initParam = scs[i].getInitialConditionParameter();
Expression iniExp = initParam == null ? null : initParam.getExpression();
if (!scs[i].isConstant() || (iniExp != null && !iniExp.isNumeric())) {
String speciesContextScopedName = scs[i].getSpeciesContext().getNameScope().getAbsoluteScopePrefix() + scs[i].getSpeciesContext().getName();
Node speciesContextNode = new Node(speciesContextScopedName, scs[i].getSpeciesContext());
digraph.addNode(speciesContextNode);
digraph.addEdge(new Edge(speciesContextNode, timeNode));
}
}
//
// add all model (global) parameters that are not simple constants
//
ModelParameter[] modelParams = model.getModelParameters();
for (int i = 0; modelParams != null && i < modelParams.length; i++) {
Expression exp = modelParams[i].getExpression();
if (exp != null) {
String[] symbols = exp.getSymbols();
if (symbols != null && symbols.length > 0) {
//
// add parameter to graph as a node (if not already there).
//
String parameterScopedName = modelParams[i].getNameScope().getAbsoluteScopePrefix() + modelParams[i].getName();
Node parameterNode = digraph.getNode(parameterScopedName);
if (parameterNode == null) {
parameterNode = new Node(parameterScopedName, modelParams[i]);
digraph.addNode(parameterNode);
}
//
for (int k = 0; symbols != null && k < symbols.length; k++) {
SymbolTableEntry ste = exp.getSymbolBinding(symbols[k]);
if (ste == null) {
throw new RuntimeException("Error, symbol '" + symbols[k] + "' not bound in parameter '" + modelParams[i].getName() + "'");
}
String symbolScopedName = ste.getNameScope().getAbsoluteScopePrefix() + ste.getName();
Node symbolNode = digraph.getNode(symbolScopedName);
if (symbolNode == null) {
symbolNode = new Node(symbolScopedName, ste);
digraph.addNode(symbolNode);
}
digraph.addEdge(new Edge(parameterNode, symbolNode));
}
}
}
}
//
// add all reaction parameters that are not simple constants
//
ReactionStep[] reactionSteps = model.getReactionSteps();
for (int i = 0; reactionSteps != null && i < reactionSteps.length; i++) {
Parameter[] parameters = reactionSteps[i].getKinetics().getKineticsParameters();
for (int j = 0; parameters != null && j < parameters.length; j++) {
Expression exp = parameters[j].getExpression();
if (exp != null) {
String[] symbols = exp.getSymbols();
if (symbols != null && symbols.length > 0) {
//
// add parameter to graph as a node (if not already there).
//
String parameterScopedName = parameters[j].getNameScope().getAbsoluteScopePrefix() + parameters[j].getName();
Node parameterNode = digraph.getNode(parameterScopedName);
if (parameterNode == null) {
parameterNode = new Node(parameterScopedName, parameters[j]);
digraph.addNode(parameterNode);
}
//
for (int k = 0; symbols != null && k < symbols.length; k++) {
SymbolTableEntry ste = exp.getSymbolBinding(symbols[k]);
if (ste == null) {
throw new RuntimeException("Error, symbol '" + symbols[k] + "' not bound in parameter '" + parameters[j].getName() + "'");
}
String symbolScopedName = ste.getNameScope().getAbsoluteScopePrefix() + ste.getName();
Node symbolNode = digraph.getNode(symbolScopedName);
if (symbolNode == null) {
symbolNode = new Node(symbolScopedName, ste);
digraph.addNode(symbolNode);
}
digraph.addEdge(new Edge(parameterNode, symbolNode));
}
}
}
}
}
//
for (Structure structure : model.getStructures()) {
if (structure instanceof Membrane && ((MembraneMapping) simulationContext.getGeometryContext().getStructureMapping(structure)).getCalculateVoltage()) {
MembraneVoltage membraneVoltage = ((Membrane) structure).getMembraneVoltage();
String membraneVoltageScopedName = membraneVoltage.getNameScope().getAbsoluteScopePrefix() + membraneVoltage.getName();
Node membraneVoltageNode = digraph.getNode(membraneVoltageScopedName);
if (membraneVoltageNode == null) {
membraneVoltageNode = new Node(membraneVoltageScopedName, membraneVoltage);
digraph.addNode(membraneVoltageNode);
}
digraph.addEdge(new Edge(membraneVoltageNode, timeNode));
}
}
Node[] timeDependentNodes = digraph.getDigraphAttractorSet(timeNode);
SymbolTableEntry[] steArray = new SymbolTableEntry[timeDependentNodes.length];
for (int i = 0; i < steArray.length; i++) {
steArray[i] = (SymbolTableEntry) timeDependentNodes[i].getData();
}
return steArray;
}
use of cbit.vcell.model.ReactionStep in project vcell by virtualcell.
the class SBPAXHMMIrrevLawBuilder method addKinetics.
public void addKinetics(KineticContext context) {
try {
ReactionStep reaction = context.getReaction();
HMM_IRRKinetics kinetics = new HMM_IRRKinetics((SimpleReaction) reaction);
NameScope modelScope = reaction.getModel().getNameScope();
ModelParameter kMichaelis = context.getParameter(SBOList.MICHAELIS_CONST_FORW);
if (kMichaelis != null) {
KineticsParameter kmParameter = kinetics.getKmParameter();
kmParameter.setExpression(new Expression(kMichaelis, modelScope));
kmParameter.setUnitDefinition(kMichaelis.getUnitDefinition());
}
ModelParameter kcat = context.getParameter(SBOList.CATALYTIC_RATE_CONST_FORW);
if (kcat != null && context.getCatalysts().size() == 1) {
KineticsParameter vmaxParameter = kinetics.getVmaxParameter();
Catalyst catalyst = context.getCatalysts().iterator().next();
vmaxParameter.setExpression(Expression.mult(new Expression(kcat, modelScope), new Expression(catalyst.getSpeciesContext(), modelScope)));
// vmaxParameter.setUnitDefinition(vMax.getUnitDefinition());
} else {
ModelParameter vMax = context.getParameter(SBOList.MAXIMAL_VELOCITY_FORW);
if (vMax != null) {
KineticsParameter vmaxParameter = kinetics.getVmaxParameter();
vmaxParameter.setExpression(new Expression(vMax, modelScope));
vmaxParameter.setUnitDefinition(vMax.getUnitDefinition());
}
}
} catch (ExpressionException e) {
e.printStackTrace();
}
}
Aggregations