use of org.sbml.jsbml.ASTNode in project vcell by virtualcell.
the class SBMLExporter method addAssignmentRules.
protected void addAssignmentRules() {
cbit.vcell.mapping.AssignmentRule[] vcAssignmentRules = getSelectedSimContext().getAssignmentRules();
if (vcAssignmentRules != null) {
for (cbit.vcell.mapping.AssignmentRule vcRule : vcAssignmentRules) {
SymbolTableEntry ste = vcRule.getAssignmentRuleVar();
if (ste instanceof ModelParameter) {
// we already created the sbml assignment rule in addParameters()
continue;
}
org.sbml.jsbml.AssignmentRule sbmlRule = sbmlModel.createAssignmentRule();
// sbmlRule.setId(vcRule.getName());
// sbmlRule.setName(vcRule.getName());
sbmlRule.setVariable(vcRule.getAssignmentRuleVar().getName());
Expression vcRuleExpression = vcRule.getAssignmentRuleExpression();
ASTNode math = getFormulaFromExpression(vcRuleExpression);
sbmlRule.setMath(math);
}
}
}
use of org.sbml.jsbml.ASTNode in project vcell by virtualcell.
the class SBMLExporter method getFormulaFromExpression.
/**
* getFormulaFromExpression :
* Expression infix strings are not handled gracefully by libSBML, esp when ligical or inequality operators are used.
* This method
* converts the expression into MathML using ExpressionMathMLPrinter;
* converts that into libSBMl-readable formula using libSBML utilties.
* returns the new formula string.
*/
public static ASTNode getFormulaFromExpression(Expression expression, MathType desiredType) {
// switch to libSBML for non-boolean
if (!desiredType.equals(MathType.BOOLEAN)) {
try {
return ASTNode.parseFormula(expression.infix());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e.toString());
}
}
// Convert expression into MathML string
String expMathMLStr = null;
try {
expMathMLStr = cbit.vcell.parser.ExpressionMathMLPrinter.getMathML(expression, false, desiredType);
} catch (java.io.IOException e) {
e.printStackTrace(System.out);
throw new RuntimeException("Error converting expression to MathML string :" + e.getMessage());
} catch (cbit.vcell.parser.ExpressionException e1) {
e1.printStackTrace(System.out);
throw new RuntimeException("Error converting expression to MathML string :" + e1.getMessage());
}
// Use libSBMl routines to convert MathML string to MathML document and a libSBML-readable formula string
ASTNode mathNode = ASTNode.readMathMLFromString(expMathMLStr);
return mathNode;
}
use of org.sbml.jsbml.ASTNode in project vcell by virtualcell.
the class SBMLExporter method createSBMLParamFromSpeciesParam.
/**
* createSBMLParamFromSpeciesParam : creates an SBML parameter for each speciesContextSpecParameter (diffusion coefficient,
* advection coeffs, boundary conditions (X,Y,Z).
*
* @param spContext
* @param scsParam
* @return
* @throws SbmlException
*/
org.sbml.jsbml.Parameter createSBMLParamFromSpeciesParam(SpeciesContext spContext, SpeciesContextSpecParameter scsParam) throws SbmlException {
try {
Expression paramExpr = scsParam.getExpression();
// if scsParam is diff, Vel X, Y, Z parameter and if its expression is null or 0.0, don't create parameter.
int role = scsParam.getRole();
if (((role == SpeciesContextSpec.ROLE_DiffusionRate) || (role == SpeciesContextSpec.ROLE_VelocityX) || (role == SpeciesContextSpec.ROLE_VelocityY) || (role == SpeciesContextSpec.ROLE_VelocityZ)) && ((paramExpr == null) || (paramExpr.isNumeric() && (scsParam.getConstantValue() == 0.0)))) {
return null;
}
// if scsParam is a BoundaryCondition, and paramExpr is null, values are set based on boundary condition type.
if (((role == SpeciesContextSpec.ROLE_BoundaryValueXm) || (role == SpeciesContextSpec.ROLE_BoundaryValueXp) || (role == SpeciesContextSpec.ROLE_BoundaryValueYm) || (role == SpeciesContextSpec.ROLE_BoundaryValueYp) || (role == SpeciesContextSpec.ROLE_BoundaryValueZm) || (role == SpeciesContextSpec.ROLE_BoundaryValueZp)) && (paramExpr == null)) {
StructureMapping sm = getSelectedSimContext().getGeometryContext().getStructureMapping(spContext.getStructure());
Expression initCondnExpr = getSelectedSimContext().getReactionContext().getSpeciesContextSpec(spContext).getInitialConditionParameter().getExpression();
// if BC type is Neumann (flux), its value is 0.0
if ((role == SpeciesContextSpec.ROLE_BoundaryValueXm)) {
if (sm.getBoundaryConditionTypeXm().isDIRICHLET()) {
paramExpr = new Expression(initCondnExpr);
} else if (sm.getBoundaryConditionTypeXm().isNEUMANN()) {
paramExpr = new Expression(0.0);
}
}
if ((role == SpeciesContextSpec.ROLE_BoundaryValueXp)) {
if (sm.getBoundaryConditionTypeXp().isDIRICHLET()) {
paramExpr = new Expression(initCondnExpr);
} else if (sm.getBoundaryConditionTypeXp().isNEUMANN()) {
paramExpr = new Expression(0.0);
}
}
if ((role == SpeciesContextSpec.ROLE_BoundaryValueYm)) {
if (sm.getBoundaryConditionTypeYm().isDIRICHLET()) {
paramExpr = new Expression(initCondnExpr);
} else if (sm.getBoundaryConditionTypeYm().isNEUMANN()) {
paramExpr = new Expression(0.0);
}
}
if ((role == SpeciesContextSpec.ROLE_BoundaryValueYp)) {
if (sm.getBoundaryConditionTypeYp().isDIRICHLET()) {
paramExpr = new Expression(initCondnExpr);
} else if (sm.getBoundaryConditionTypeYp().isNEUMANN()) {
paramExpr = new Expression(0.0);
}
}
if ((role == SpeciesContextSpec.ROLE_BoundaryValueZm)) {
if (sm.getBoundaryConditionTypeZm().isDIRICHLET()) {
paramExpr = new Expression(initCondnExpr);
} else if (sm.getBoundaryConditionTypeZm().isNEUMANN()) {
paramExpr = new Expression(0.0);
}
}
if ((role == SpeciesContextSpec.ROLE_BoundaryValueZp)) {
if (sm.getBoundaryConditionTypeZp().isDIRICHLET()) {
paramExpr = new Expression(initCondnExpr);
} else if (sm.getBoundaryConditionTypeZp().isNEUMANN()) {
paramExpr = new Expression(0.0);
}
}
}
// create SBML parameter
org.sbml.jsbml.Parameter param = sbmlModel.createParameter();
param.setId(TokenMangler.mangleToSName(spContext.getName() + "_" + scsParam.getName()));
UnitDefinition unitDefn = getOrCreateSBMLUnit(scsParam.getUnitDefinition());
param.setUnits(unitDefn);
param.setConstant(scsParam.isConstant());
if (paramExpr.isNumeric()) {
param.setValue(paramExpr.evaluateConstant());
param.setConstant(true);
} else {
// we need to create a parameter and a rule for the non-numeric expr of diffParam
param.setValue(0.0);
param.setConstant(false);
// now add assignment rule in SBML for the diff param
ASTNode assgnRuleMathNode = getFormulaFromExpression(paramExpr);
AssignmentRule assgnRule = sbmlModel.createAssignmentRule();
assgnRule.setVariable(param.getId());
assgnRule.setMath(assgnRuleMathNode);
}
return param;
} catch (ExpressionException e) {
e.printStackTrace(System.out);
throw new RuntimeException("Unable to interpret parameter '" + scsParam.getName() + "' of species : " + spContext.getName());
}
}
use of org.sbml.jsbml.ASTNode in project vcell by virtualcell.
the class SBMLImporter method addFunctionDefinitions.
private void addFunctionDefinitions() {
if (sbmlModel == null) {
throw new SBMLImportException("SBML model is NULL");
}
ListOf<FunctionDefinition> listofFunctionDefinitions = sbmlModel.getListOfFunctionDefinitions();
if (listofFunctionDefinitions == null) {
System.out.println("No Function Definitions");
return;
}
// The function definitions contain lambda function definition.
// Each lambda function has a name, (list of) argument(s), function body
// which is represented as a math element.
lambdaFunctions = new LambdaFunction[(int) sbmlModel.getNumFunctionDefinitions()];
try {
for (int i = 0; i < sbmlModel.getNumFunctionDefinitions(); i++) {
FunctionDefinition fnDefn = (FunctionDefinition) listofFunctionDefinitions.get(i);
String functionName = new String(fnDefn.getId());
ASTNode math = null;
Vector<String> argsVector = new Vector<String>();
Vector<String> secureArgsVector = new Vector<>();
String[] functionArgs = null;
if (fnDefn.isSetMath()) {
math = fnDefn.getMath();
// Function body.
if (math.getNumChildren() == 0) {
System.out.println("(no function body defined)");
continue;
}
// Note that lambda function always should have at least 2 children
for (int j = 0; j < math.getNumChildren() - 1; ++j) {
String baseName = new String(math.getChild(j).getName());
argsVector.addElement(baseName);
secureArgsVector.addElement(baseName + "_" + functionName + FormalArgumentSuffix);
}
functionArgs = secureArgsVector.toArray(new String[0]);
math = math.getChild(math.getNumChildren() - 1);
// formula = libsbml.formulaToString(math);
Expression fnExpr = getExpressionFromFormula(math);
for (int j = 0; j < argsVector.size(); j++) {
fnExpr.substituteInPlace(new Expression(argsVector.get(j)), new Expression(secureArgsVector.get(j)));
}
lambdaFunctions[i] = new LambdaFunction(functionName, fnExpr, functionArgs);
}
}
} catch (Exception e) {
e.printStackTrace(System.out);
throw new SBMLImportException("Error adding Lambda function" + e.getMessage(), e);
}
}
use of org.sbml.jsbml.ASTNode in project vcell by virtualcell.
the class SBMLImporter method addEvents.
protected void addEvents(Map<String, String> sbmlToVcNameMap) {
if (sbmlModel.getNumEvents() > 0) {
// VCell does not support events in spatial model
if (bSpatial) {
throw new SBMLImportException("Events are not supported in a spatial VCell model.");
}
ListOf<Event> listofEvents = sbmlModel.getListOfEvents();
SimulationContext simContext = vcBioModel.getSimulationContext(0);
Model vcModel = simContext.getModel();
Map<String, SymbolTableEntry> entryMap = new HashMap<String, SymbolTableEntry>();
simContext.getEntries(entryMap);
for (int i = 0; i < sbmlModel.getNumEvents(); i++) {
try {
Event event = listofEvents.get(i);
// trigger - adjust for species context and time conversion factors if necessary
Expression triggerExpr = null;
if (event.isSetTrigger()) {
triggerExpr = getExpressionFromFormula(event.getTrigger().getMath());
triggerExpr = adjustExpression(triggerExpr, vcModel);
}
// convert reserved symbols, if any
if (!sbmlToVcNameMap.isEmpty() && triggerExpr != null && triggerExpr.getSymbols() != null && triggerExpr.getSymbols().length > 0) {
String[] symbols = triggerExpr.getSymbols();
for (String oldSymbol : symbols) {
String newSymbol = sbmlToVcNameMap.get(oldSymbol);
if (newSymbol != null) {
triggerExpr.substituteInPlace(new Expression(oldSymbol), new Expression(newSymbol));
}
}
}
// create bioevent
String eventName = event.getId();
if (eventName == null || eventName.length() == 0) {
eventName = TokenMangler.mangleToSName(event.getName());
// vcBioModel.getSimulationContext(0).
if (eventName == null || eventName.length() == 0) {
eventName = vcBioModel.getSimulationContext(0).getFreeEventName(null);
}
}
// delay
BioEvent vcEvent = new BioEvent(eventName, TriggerType.GeneralTrigger, true, vcBioModel.getSimulationContext(0));
if (event.isSetDelay()) {
Expression durationExpr = null;
durationExpr = getExpressionFromFormula(event.getDelay().getMath());
durationExpr = adjustExpression(durationExpr, vcModel);
// convert reserved symbols, if any
if (!sbmlToVcNameMap.isEmpty() && durationExpr != null && durationExpr.getSymbols() != null && durationExpr.getSymbols().length > 0) {
String[] symbols = durationExpr.getSymbols();
for (String oldSymbol : symbols) {
String newSymbol = sbmlToVcNameMap.get(oldSymbol);
if (newSymbol != null) {
durationExpr.substituteInPlace(new Expression(oldSymbol), new Expression(newSymbol));
}
}
}
boolean bUseValsFromTriggerTime = true;
if (event.isSetUseValuesFromTriggerTime()) {
bUseValsFromTriggerTime = event.isSetUseValuesFromTriggerTime();
} else {
if (durationExpr != null && !durationExpr.isZero()) {
bUseValsFromTriggerTime = false;
}
}
if (durationExpr != null && !durationExpr.isZero()) {
bUseValsFromTriggerTime = false;
}
vcEvent.setUseValuesFromTriggerTime(bUseValsFromTriggerTime);
vcEvent.getParameter(BioEventParameterType.TriggerDelay).setExpression(durationExpr);
}
cbit.vcell.mapping.ParameterContext.LocalParameter triggerParameter = vcEvent.getParameter(BioEventParameterType.GeneralTriggerFunction);
triggerParameter.setExpression(triggerExpr);
// event assignments
ArrayList<EventAssignment> vcEvntAssgnList = new ArrayList<EventAssignment>();
for (int j = 0; j < event.getNumEventAssignments(); j++) {
org.sbml.jsbml.EventAssignment sbmlEvntAssgn = event.getEventAssignment(j);
String sbmlVarName = sbmlEvntAssgn.getVariable();
String vcVarName = sbmlVarName;
if (sbmlToVcNameMap.get(sbmlVarName) != null) {
// convert reserved symbols, if any
vcVarName = sbmlToVcNameMap.get(sbmlVarName);
}
SymbolTableEntry ste = simContext.getEntry(vcVarName);
if (ste != null) {
Expression evntAssgnExpr;
ASTNode node = sbmlEvntAssgn.getMath();
if (node == null) {
evntAssgnExpr = new Expression("0.0");
String msg = "Event assignment expression is null for event '" + eventName + "'.";
localIssueList.add(new Issue(vcBioModel, issueContext, IssueCategory.SBMLImport_UnsupportedAttributeOrElement, msg, Issue.Severity.WARNING));
} else {
evntAssgnExpr = getExpressionFromFormula(node);
}
evntAssgnExpr = adjustExpression(evntAssgnExpr, vcModel);
// convert reserved symbols, if any
if (!sbmlToVcNameMap.isEmpty() && evntAssgnExpr != null && evntAssgnExpr.getSymbols() != null && evntAssgnExpr.getSymbols().length > 0) {
String[] symbols = evntAssgnExpr.getSymbols();
for (String oldSymbol : symbols) {
String newSymbol = sbmlToVcNameMap.get(oldSymbol);
if (newSymbol != null) {
evntAssgnExpr.substituteInPlace(new Expression(oldSymbol), new Expression(newSymbol));
}
}
}
EventAssignment vcEvntAssgn = vcEvent.new EventAssignment(ste, evntAssgnExpr);
vcEvntAssgnList.add(vcEvntAssgn);
} else {
logger.sendMessage(VCLogger.Priority.HighPriority, VCLogger.ErrorType.UnsupportedConstruct, "No symbolTableEntry for '" + vcVarName + "'; Cannot add event assignment.");
}
}
vcEvent.setEventAssignmentsList(vcEvntAssgnList);
vcEvent.bind();
vcBioModel.getSimulationContext(0).addBioEvent(vcEvent);
} catch (Exception e) {
e.printStackTrace(System.out);
throw new SBMLImportException(e.getMessage(), e);
}
// end - try/catch
}
// end - for(sbmlEvents)
}
// end - if numEvents > 0)
}
Aggregations