use of cbit.vcell.mapping.BioEvent in project vcell by virtualcell.
the class ModelUnitConverter method createBioModelWithNewUnitSystem.
public static BioModel createBioModelWithNewUnitSystem(BioModel oldBioModel, ModelUnitSystem newUnitSystem) throws ExpressionException, XmlParseException {
// new BioModel has new unit system applied to all built-in units ... but expressions still need to be corrected (see below).
String biomodelXMLString = XmlHelper.bioModelToXML(oldBioModel);
XMLSource newXMLSource = new XMLSource(biomodelXMLString);
BioModel newBioModel = XmlHelper.XMLToBioModel(newXMLSource, true, newUnitSystem);
Model newModel = newBioModel.getModel();
Model oldModel = oldBioModel.getModel();
for (Parameter p : newBioModel.getModel().getModelParameters()) {
convertVarsWithUnitFactors(oldBioModel.getModel(), newBioModel.getModel(), p);
}
for (ReactionStep reactionStep : newBioModel.getModel().getReactionSteps()) {
SymbolTable oldSymbolTable = oldBioModel.getModel().getReactionStep(reactionStep.getName());
SymbolTable newSymbolTable = reactionStep;
for (Parameter p : reactionStep.getKinetics().getUnresolvedParameters()) {
convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
}
for (Parameter p : reactionStep.getKinetics().getKineticsParameters()) {
convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
}
Kinetics kinetics = reactionStep.getKinetics();
KineticsParameter kineticsParameter = null;
if (kinetics.getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate) != null) {
kineticsParameter = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate);
} else if (kinetics.getKineticsParameterFromRole(Kinetics.ROLE_LumpedReactionRate) != null) {
kineticsParameter = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_LumpedReactionRate);
} else {
throw new RuntimeException("Role 'reaction rate' or role 'lumped reaction rate' expected");
}
Expression rateExpression = kineticsParameter.getExpression();
jscl.math.Expression jsclExpression = null;
String jsclExpressionString = rateExpression.infix_JSCL();
try {
jsclExpression = jscl.math.Expression.valueOf(jsclExpressionString);
} catch (jscl.text.ParseException e) {
e.printStackTrace(System.out);
System.out.println("JSCL couldn't parse \"" + jsclExpressionString + "\"");
return null;
}
jscl.math.Generic g1 = jsclExpression.expand().simplify();
Expression newRate = new Expression(SymbolUtils.getRestoredStringJSCL(g1.toString()));
newRate.bindExpression(reactionStep);
// reactionStep.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate).setExpression(newRate.flatten());
if (reactionStep.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate) != null) {
reactionStep.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate).setExpression(newRate.flatten());
}
}
for (ReactionRule reactionRule : newBioModel.getModel().getRbmModelContainer().getReactionRuleList()) {
SymbolTable oldSymbolTable = oldBioModel.getModel().getRbmModelContainer().getReactionRule(reactionRule.getName()).getKineticLaw().getScopedSymbolTable();
SymbolTable newSymbolTable = reactionRule.getKineticLaw().getScopedSymbolTable();
for (Parameter p : reactionRule.getKineticLaw().getUnresolvedParameters()) {
convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
}
for (Parameter p : reactionRule.getKineticLaw().getLocalParameters()) {
convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
}
}
for (SimulationContext simContext : newBioModel.getSimulationContexts()) {
SimulationContext oldSimContext = oldBioModel.getSimulationContext(simContext.getName());
// ArrayList<Parameter> parameterList = new ArrayList<Parameter>();
for (StructureMapping mapping : simContext.getGeometryContext().getStructureMappings()) {
Structure oldStructure = oldModel.getStructure(mapping.getStructure().getName());
StructureMapping oldMapping = oldSimContext.getGeometryContext().getStructureMapping(oldStructure);
for (Parameter p : mapping.computeApplicableParameterList()) {
convertVarsWithUnitFactors(oldMapping, mapping, p);
}
}
for (SpeciesContextSpec spec : simContext.getReactionContext().getSpeciesContextSpecs()) {
SpeciesContext oldSpeciesContext = oldModel.getSpeciesContext(spec.getSpeciesContext().getName());
SpeciesContextSpec oldSpec = oldSimContext.getReactionContext().getSpeciesContextSpec(oldSpeciesContext);
for (Parameter p : spec.computeApplicableParameterList()) {
convertVarsWithUnitFactors(oldSpec, spec, p);
}
}
for (int i = 0; i < simContext.getElectricalStimuli().length; i++) {
ElectricalStimulus newElectricalStimulus = simContext.getElectricalStimuli()[i];
ElectricalStimulus oldElectricalStimulus = oldSimContext.getElectricalStimuli()[i];
for (Parameter p : newElectricalStimulus.getParameters()) {
convertVarsWithUnitFactors(oldElectricalStimulus.getNameScope().getScopedSymbolTable(), newElectricalStimulus.getNameScope().getScopedSymbolTable(), p);
}
}
// convert events : trigger and delay parameters and event assignments
for (int i = 0; simContext.getBioEvents() != null && oldSimContext.getBioEvents() != null && i < simContext.getBioEvents().length; i++) {
BioEvent newBioEvent = simContext.getBioEvents()[i];
BioEvent oldBioEvent = oldSimContext.getBioEvent(newBioEvent.getName());
for (Parameter p : newBioEvent.getEventParameters()) {
convertVarsWithUnitFactors(oldBioEvent.getNameScope().getScopedSymbolTable(), newBioEvent.getNameScope().getScopedSymbolTable(), p);
}
// for each event assignment expression
for (int e = 0; e < newBioEvent.getEventAssignments().size(); e++) {
ScopedSymbolTable newSymbolTable = newBioEvent.getNameScope().getScopedSymbolTable();
ScopedSymbolTable oldSymbolTable = oldBioEvent.getNameScope().getScopedSymbolTable();
EventAssignment newEventAssignment = newBioEvent.getEventAssignments().get(e);
EventAssignment oldEventAssignment = oldBioEvent.getEventAssignments().get(e);
VCUnitDefinition oldTargetUnit = oldEventAssignment.getTarget().getUnitDefinition();
VCUnitDefinition newTargetUnit = newEventAssignment.getTarget().getUnitDefinition();
Expression eventAssgnExpr = newEventAssignment.getAssignmentExpression();
convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldTargetUnit, newTargetUnit, eventAssgnExpr);
}
}
/**
* @TODO: If rate rule variable unit is TBD, we still need to handle the rate expression unit.
*/
// convert rate rules
RateRule[] rateRules = simContext.getRateRules();
if (rateRules != null && rateRules.length > 0) {
for (RateRule rateRule : rateRules) {
RateRule oldRateRule = oldSimContext.getRateRule(rateRule.getName());
ScopedSymbolTable oldSymbolTable = oldRateRule.getSimulationContext();
ScopedSymbolTable newSymbolTable = rateRule.getSimulationContext();
VCUnitDefinition oldTargetUnit = oldRateRule.getRateRuleVar().getUnitDefinition();
VCUnitDefinition newTargetUnit = rateRule.getRateRuleVar().getUnitDefinition();
Expression rateRuleExpr = rateRule.getRateRuleExpression();
convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldTargetUnit, newTargetUnit, rateRuleExpr);
}
}
AssignmentRule[] assignmentRules = simContext.getAssignmentRules();
if (assignmentRules != null && assignmentRules.length > 0) {
for (AssignmentRule assignmentRule : assignmentRules) {
AssignmentRule oldAssignRule = oldSimContext.getAssignmentRule(assignmentRule.getName());
ScopedSymbolTable oldSymbolTable = oldAssignRule.getSimulationContext();
ScopedSymbolTable newSymbolTable = assignmentRule.getSimulationContext();
VCUnitDefinition oldTargetUnit = oldAssignRule.getAssignmentRuleVar().getUnitDefinition();
VCUnitDefinition newTargetUnit = assignmentRule.getAssignmentRuleVar().getUnitDefinition();
Expression assignmentRuleExpr = assignmentRule.getAssignmentRuleExpression();
convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldTargetUnit, newTargetUnit, assignmentRuleExpr);
}
}
}
// end for - simulationContext
return newBioModel;
}
use of cbit.vcell.mapping.BioEvent 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)
}
use of cbit.vcell.mapping.BioEvent in project vcell by virtualcell.
the class SimContextTable method getAppComponentsForDatabase.
/**
* getXMLStringForDatabase : this returns the XML string for the container element <AppComponents> for application-related protocols
* and other extra specifications. For now, BioEvents falls under this category, so the BioEvents element (list of bioevents)
* is obtained from the simContext (via the XMLProducer) and added as content to <AppComponents> element. The <AppComponents>
* element is converted to XML string which is the return value of this method. This string is stored in the database in the
* SimContextTable. Instead of creating new fields for each possible application component, it is convenient to store them
* all under a blanket XML element <AppComponents>.
* @param simContext
* @return
*/
public static String getAppComponentsForDatabase(SimulationContext simContext) {
Element appComponentsElement = new Element(XMLTags.ApplicationComponents);
// for now, create the element only if application is stochastic. Can change it later.
if (simContext.isStoch()) {
// add 'randomizeInitCondition' flag only if simContext is non-spatial
if (simContext.getGeometry().getDimension() == 0) {
Element appRelatedFlagsElement = new Element(XMLTags.ApplicationSpecificFlagsTag);
if (simContext.isRandomizeInitCondition()) {
appRelatedFlagsElement.setAttribute(XMLTags.RandomizeInitConditionTag, "true");
} else {
appRelatedFlagsElement.setAttribute(XMLTags.RandomizeInitConditionTag, "false");
}
appComponentsElement.addContent(appRelatedFlagsElement);
}
}
if (simContext.isInsufficientIterations()) {
appComponentsElement.setAttribute(XMLTags.InsufficientIterationsTag, "true");
} else {
appComponentsElement.setAttribute(XMLTags.InsufficientIterationsTag, "false");
}
if (simContext.isInsufficientMaxMolecules()) {
appComponentsElement.setAttribute(XMLTags.InsufficientMaxMoleculesTag, "true");
} else {
appComponentsElement.setAttribute(XMLTags.InsufficientMaxMoleculesTag, "false");
}
if (simContext.isUsingMassConservationModelReduction()) {
appComponentsElement.setAttribute(XMLTags.MassConservationModelReductionTag, "true");
} else {
appComponentsElement.setAttribute(XMLTags.MassConservationModelReductionTag, "false");
}
Xmlproducer xmlProducer = new Xmlproducer(false);
NetworkConstraints constraints = simContext.getNetworkConstraints();
if (constraints != null) {
appComponentsElement.addContent(xmlProducer.getXML(constraints, simContext));
}
// first fill in bioevents from simContext
BioEvent[] bioEvents = simContext.getBioEvents();
if (bioEvents != null && bioEvents.length > 0) {
try {
Element bioEventsElement = xmlProducer.getXML(bioEvents);
appComponentsElement.addContent(bioEventsElement);
} catch (XmlParseException e) {
throw new RuntimeException("Error generating XML for bioevents : " + e.getMessage(), e);
}
}
SimulationContextParameter[] appParams = simContext.getSimulationContextParameters();
if (appParams != null && appParams.length > 0) {
try {
Element appParamsElement = xmlProducer.getXML(appParams);
appComponentsElement.addContent(appParamsElement);
} catch (Exception e) {
throw new RuntimeException("Error generating XML for application parameters : " + e.getMessage(), e);
}
}
SpatialObject[] spatialObjects = simContext.getSpatialObjects();
if (spatialObjects != null && spatialObjects.length > 0) {
try {
Element spatialObjectsElement = xmlProducer.getXML(spatialObjects);
appComponentsElement.addContent(spatialObjectsElement);
} catch (XmlParseException e) {
throw new RuntimeException("Error generating XML for spatialObjects : " + e.getMessage(), e);
}
}
SpatialProcess[] spatialProcesses = simContext.getSpatialProcesses();
if (spatialProcesses != null && spatialProcesses.length > 0) {
try {
Element spatialProcessesElement = xmlProducer.getXML(spatialProcesses);
appComponentsElement.addContent(spatialProcessesElement);
} catch (XmlParseException e) {
throw new RuntimeException("Error generating XML for spatialProcesses : " + e.getMessage(), e);
}
}
// microscope measurements
Element element = xmlProducer.getXML(simContext.getMicroscopeMeasurement());
appComponentsElement.addContent(element);
// rate rules
RateRule[] rateRules = simContext.getRateRules();
if (rateRules != null && rateRules.length > 0) {
try {
Element rateRulesElement = xmlProducer.getXML(rateRules);
appComponentsElement.addContent(rateRulesElement);
} catch (XmlParseException e) {
throw new RuntimeException("Error generating XML for bioevents : " + e.getMessage(), e);
}
}
AssignmentRule[] assignmentRules = simContext.getAssignmentRules();
if (assignmentRules != null && assignmentRules.length > 0) {
try {
Element assignmentRulesElement = xmlProducer.getXML(assignmentRules);
appComponentsElement.addContent(assignmentRulesElement);
} catch (XmlParseException e) {
throw new RuntimeException("Error generating XML for bioevents : " + e.getMessage(), e);
}
}
// ReactionRuleSpecs
ReactionRuleSpec[] reactionRuleSpecs = simContext.getReactionContext().getReactionRuleSpecs();
if (reactionRuleSpecs != null && reactionRuleSpecs.length > 0) {
Element reactionRuleSpecsElement = xmlProducer.getXML(reactionRuleSpecs);
appComponentsElement.addContent(reactionRuleSpecsElement);
}
String appComponentsXMLStr = null;
if (appComponentsElement.getContent() != null) {
appComponentsXMLStr = XmlUtil.xmlToString(appComponentsElement);
}
return appComponentsXMLStr;
}
Aggregations