use of cbit.vcell.mapping.ReactionContext in project vcell by virtualcell.
the class SBMLImporter method addReactions.
/**
* addReactions:
*/
protected void addReactions(VCMetaData metaData) {
if (sbmlModel == null) {
throw new SBMLImportException("SBML model is NULL");
}
ListOf<Reaction> reactions = sbmlModel.getListOfReactions();
final int numReactions = reactions.size();
if (numReactions == 0) {
lg.info("No Reactions");
return;
}
// all reactions
ArrayList<ReactionStep> vcReactionList = new ArrayList<>();
// just the fast ones
ArrayList<ReactionStep> fastReactionList = new ArrayList<>();
Model vcModel = vcBioModel.getSimulationContext(0).getModel();
ModelUnitSystem vcModelUnitSystem = vcModel.getUnitSystem();
SpeciesContext[] vcSpeciesContexts = vcModel.getSpeciesContexts();
try {
for (Reaction sbmlRxn : reactions) {
ReactionStep vcReaction = null;
String rxnName = sbmlRxn.getId();
boolean bReversible = true;
if (sbmlRxn.isSetReversible()) {
bReversible = sbmlRxn.getReversible();
}
// Check of reaction annotation is present; if so, does it have
// an embedded element (flux or simpleRxn).
// Create a fluxReaction or simpleReaction accordingly.
Element sbmlImportRelatedElement = sbmlAnnotationUtil.readVCellSpecificAnnotation(sbmlRxn);
Structure reactionStructure = getReactionStructure(sbmlRxn, vcSpeciesContexts, sbmlImportRelatedElement);
if (sbmlImportRelatedElement != null) {
Element embeddedRxnElement = getEmbeddedElementInAnnotation(sbmlImportRelatedElement, REACTION);
if (embeddedRxnElement != null) {
if (embeddedRxnElement.getName().equals(XMLTags.FluxStepTag)) {
// If embedded element is a flux reaction, set flux
// reaction's strucure, flux carrier, physicsOption
// from the element attributes.
String structName = embeddedRxnElement.getAttributeValue(XMLTags.StructureAttrTag);
CastInfo<Membrane> ci = SBMLHelper.getTypedStructure(Membrane.class, vcModel, structName);
if (!ci.isGood()) {
throw new SBMLImportException("Appears that the flux reaction is occuring on " + ci.actualName() + ", not a membrane.");
}
vcReaction = new FluxReaction(vcModel, ci.get(), null, rxnName, bReversible);
vcReaction.setModel(vcModel);
// Set the fluxOption on the flux reaction based on
// whether it is molecular, molecular & electrical,
// electrical.
String fluxOptionStr = embeddedRxnElement.getAttributeValue(XMLTags.FluxOptionAttrTag);
if (fluxOptionStr.equals(XMLTags.FluxOptionMolecularOnly)) {
((FluxReaction) vcReaction).setPhysicsOptions(ReactionStep.PHYSICS_MOLECULAR_ONLY);
} else if (fluxOptionStr.equals(XMLTags.FluxOptionMolecularAndElectrical)) {
((FluxReaction) vcReaction).setPhysicsOptions(ReactionStep.PHYSICS_MOLECULAR_AND_ELECTRICAL);
} else if (fluxOptionStr.equals(XMLTags.FluxOptionElectricalOnly)) {
((FluxReaction) vcReaction).setPhysicsOptions(ReactionStep.PHYSICS_ELECTRICAL_ONLY);
} else {
localIssueList.add(new Issue(vcReaction, issueContext, IssueCategory.SBMLImport_Reaction, "Unknown FluxOption : " + fluxOptionStr + " for SBML reaction : " + rxnName, Issue.SEVERITY_WARNING));
// logger.sendMessage(VCLogger.Priority.MediumPriority,
// VCLogger.ErrorType.ReactionError,
// "Unknown FluxOption : " + fluxOptionStr +
// " for SBML reaction : " + rxnName);
}
} else if (embeddedRxnElement.getName().equals(XMLTags.SimpleReactionTag)) {
// if embedded element is a simple reaction, set
// simple reaction's structure from element
// attributes
vcReaction = new SimpleReaction(vcModel, reactionStructure, rxnName, bReversible);
}
} else {
vcReaction = new SimpleReaction(vcModel, reactionStructure, rxnName, bReversible);
}
} else {
vcReaction = new SimpleReaction(vcModel, reactionStructure, rxnName, bReversible);
}
// set annotations and notes on vcReactions[i]
sbmlAnnotationUtil.readAnnotation(vcReaction, sbmlRxn);
sbmlAnnotationUtil.readNotes(vcReaction, sbmlRxn);
// the limit on the reactionName length.
if (rxnName.length() > 64) {
String freeTextAnnotation = metaData.getFreeTextAnnotation(vcReaction);
if (freeTextAnnotation == null) {
freeTextAnnotation = "";
}
StringBuffer oldRxnAnnotation = new StringBuffer(freeTextAnnotation);
oldRxnAnnotation.append("\n\n" + rxnName);
metaData.setFreeTextAnnotation(vcReaction, oldRxnAnnotation.toString());
}
// Now add the reactants, products, modifiers as specified by
// the sbmlRxn
addReactionParticipants(sbmlRxn, vcReaction);
KineticLaw kLaw = sbmlRxn.getKineticLaw();
Kinetics kinetics = null;
if (kLaw != null) {
// Convert the formula from kineticLaw into MathML and then
// to an expression (infix) to be used in VCell kinetics
ASTNode sbmlRateMath = kLaw.getMath();
Expression kLawRateExpr = getExpressionFromFormula(sbmlRateMath);
Expression vcRateExpression = new Expression(kLawRateExpr);
// modifier (catalyst) to the reaction.
for (int k = 0; k < vcSpeciesContexts.length; k++) {
if (vcRateExpression.hasSymbol(vcSpeciesContexts[k].getName())) {
if ((vcReaction.getReactant(vcSpeciesContexts[k].getName()) == null) && (vcReaction.getProduct(vcSpeciesContexts[k].getName()) == null) && (vcReaction.getCatalyst(vcSpeciesContexts[k].getName()) == null)) {
// This means that the speciesContext is not a
// reactant, product or modifier : it has to be
// added to the VC Rxn as a catalyst
vcReaction.addCatalyst(vcSpeciesContexts[k]);
}
}
}
// set kinetics on VCell reaction
if (bSpatial) {
// if spatial SBML ('isSpatial' attribute set), create
// DistributedKinetics)
SpatialReactionPlugin ssrplugin = (SpatialReactionPlugin) sbmlRxn.getPlugin(SBMLUtils.SBML_SPATIAL_NS_PREFIX);
// 'spatial'
if (ssrplugin != null && ssrplugin.getIsLocal()) {
kinetics = new GeneralKinetics(vcReaction);
} else {
kinetics = new GeneralLumpedKinetics(vcReaction);
}
} else {
kinetics = new GeneralLumpedKinetics(vcReaction);
}
// set kinetics on vcReaction
vcReaction.setKinetics(kinetics);
// If the name of the rate parameter has been changed by
// user, or matches with global/local param,
// it has to be changed.
resolveRxnParameterNameConflicts(sbmlRxn, kinetics, sbmlImportRelatedElement);
/**
* Now, based on the kinetic law expression, see if the rate
* is expressed in concentration/time or substance/time : If
* the compartment_id of the compartment corresponding to
* the structure in which the reaction takes place occurs in
* the rate law expression, it is in concentration/time;
* divide it by the compartment size and bring in the rate
* law as 'Distributed' kinetics. If not, the rate law is in
* substance/time; bring it in (as is) as 'Lumped' kinetics.
*/
ListOf<LocalParameter> localParameters = kLaw.getListOfLocalParameters();
for (LocalParameter p : localParameters) {
String paramName = p.getId();
KineticsParameter kineticsParameter = kinetics.getKineticsParameter(paramName);
if (kineticsParameter == null) {
// add unresolved for now to prevent errors in kinetics.setParameterValue(kp,vcRateExpression) below
kinetics.addUnresolvedParameter(paramName);
}
}
KineticsParameter kp = kinetics.getAuthoritativeParameter();
if (lg.isDebugEnabled()) {
lg.debug("Setting " + kp.getName() + ": " + vcRateExpression.infix());
}
kinetics.setParameterValue(kp, vcRateExpression);
// If there are any global parameters used in the kinetics,
// and if they have species,
// check if the species are already reactionParticipants in
// the reaction. If not, add them as catalysts.
KineticsProxyParameter[] kpps = kinetics.getProxyParameters();
for (int j = 0; j < kpps.length; j++) {
if (kpps[j].getTarget() instanceof ModelParameter) {
ModelParameter mp = (ModelParameter) kpps[j].getTarget();
HashSet<String> refSpeciesNameHash = new HashSet<String>();
getReferencedSpeciesInExpr(mp.getExpression(), refSpeciesNameHash);
java.util.Iterator<String> refSpIterator = refSpeciesNameHash.iterator();
while (refSpIterator.hasNext()) {
String spName = refSpIterator.next();
org.sbml.jsbml.Species sp = sbmlModel.getSpecies(spName);
ArrayList<ReactionParticipant> rpArray = getVCReactionParticipantsFromSymbol(vcReaction, sp.getId());
if (rpArray == null || rpArray.size() == 0) {
// This means that the speciesContext is not
// a reactant, product or modifier : it has
// to be added as a catalyst
vcReaction.addCatalyst(vcModel.getSpeciesContext(sp.getId()));
}
}
}
}
// model - local params cannot be defined by rules.
for (LocalParameter param : localParameters) {
String paramName = param.getId();
Expression exp = new Expression(param.getValue());
String unitString = param.getUnits();
VCUnitDefinition paramUnit = sbmlUnitIdentifierHash.get(unitString);
if (paramUnit == null) {
paramUnit = vcModelUnitSystem.getInstance_TBD();
}
// check if sbml local param is in kinetic params list;
// if so, add its value.
boolean lpSet = false;
KineticsParameter kineticsParameter = kinetics.getKineticsParameter(paramName);
if (kineticsParameter != null) {
if (lg.isDebugEnabled()) {
lg.debug("Setting local " + kineticsParameter.getName() + ": " + exp.infix());
}
kineticsParameter.setExpression(exp);
kineticsParameter.setUnitDefinition(paramUnit);
lpSet = true;
} else {
UnresolvedParameter ur = kinetics.getUnresolvedParameter(paramName);
if (ur != null) {
kinetics.addUserDefinedKineticsParameter(paramName, exp, paramUnit);
lpSet = true;
}
}
if (!lpSet) {
// check if it is a proxy parameter (specifically,
// speciesContext or model parameter (structureSize
// too)).
KineticsProxyParameter kpp = kinetics.getProxyParameter(paramName);
// and units to local param values
if (kpp != null && kpp.getTarget() instanceof ModelParameter) {
kinetics.convertParameterType(kpp, false);
kineticsParameter = kinetics.getKineticsParameter(paramName);
kinetics.setParameterValue(kineticsParameter, exp);
kineticsParameter.setUnitDefinition(paramUnit);
}
}
}
} else {
// sbmlKLaw was null, so creating a GeneralKinetics with 0.0
// as rate.
kinetics = new GeneralKinetics(vcReaction);
}
// end - if-else KLaw != null
// set the reaction kinetics, and add reaction to the vcell
// model.
kinetics.resolveUndefinedUnits();
// System.out.println("ADDED SBML REACTION : \"" + rxnName +
// "\" to VCModel");
vcReactionList.add(vcReaction);
if (sbmlRxn.isSetFast() && sbmlRxn.getFast()) {
fastReactionList.add(vcReaction);
}
}
// end - for vcReactions
ReactionStep[] array = vcReactionList.toArray(new ReactionStep[vcReactionList.size()]);
vcModel.setReactionSteps(array);
final ReactionContext rc = vcBioModel.getSimulationContext(0).getReactionContext();
for (ReactionStep frs : fastReactionList) {
final ReactionSpec rs = rc.getReactionSpec(frs);
rs.setReactionMapping(ReactionSpec.FAST);
}
} catch (ModelPropertyVetoException mpve) {
throw new SBMLImportException(mpve.getMessage(), mpve);
} catch (Exception e1) {
e1.printStackTrace(System.out);
throw new SBMLImportException(e1.getMessage(), e1);
}
}
use of cbit.vcell.mapping.ReactionContext in project vcell by virtualcell.
the class ITextWriter method writeReactionContext.
// ReactionContext - SpeciesContextSpec: ignored boundary conditions.
protected void writeReactionContext(Section simContextSection, SimulationContext simContext) throws DocumentException {
ReactionContext rc = simContext.getReactionContext();
if (rc == null) {
return;
}
Section rcSection = null;
// add reaction specs
ReactionSpec[] reactionSpecs = rc.getReactionSpecs();
Table reactionSpecTable = null;
for (int i = 0; i < reactionSpecs.length; i++) {
if (i == 0) {
reactionSpecTable = getTable(4, 100, 1, 3, 3);
// reactionSpecTable.setTableFitsPage(true);
reactionSpecTable.addCell(createCell("Reaction Mapping", getBold(DEF_HEADER_FONT_SIZE), 4, 1, Element.ALIGN_CENTER, true));
reactionSpecTable.addCell(createHeaderCell("Name", getBold(), 1));
reactionSpecTable.addCell(createHeaderCell("Type", getBold(), 1));
reactionSpecTable.addCell(createHeaderCell("Enabled (T/F)", getBold(), 1));
reactionSpecTable.addCell(createHeaderCell("Fast (T/F)", getBold(), 1));
reactionSpecTable.endHeaders();
}
String reactionName = reactionSpecs[i].getReactionStep().getName();
String reactionType = reactionSpecs[i].getReactionStep().getDisplayType();
reactionSpecTable.addCell(createCell(reactionName, getFont()));
reactionSpecTable.addCell(createCell(reactionType, getFont()));
reactionSpecTable.addCell(createCell((reactionSpecs[i].isExcluded() ? " F " : " T "), getFont()));
reactionSpecTable.addCell(createCell((reactionSpecs[i].isFast() ? " T " : " F "), getFont()));
}
if (reactionSpecTable != null) {
rcSection = simContextSection.addSection("Reaction Mapping For " + simContext.getName(), simContextSection.numberDepth() + 1);
rcSection.add(reactionSpecTable);
}
// add species context specs
SpeciesContextSpec[] speciesContSpecs = rc.getSpeciesContextSpecs();
Table speciesSpecTable = null;
int[] widths = { 2, 2, 4, 4, 1 };
for (int i = 0; i < speciesContSpecs.length; i++) {
if (i == 0) {
speciesSpecTable = getTable(5, 100, 1, 3, 3);
speciesSpecTable.addCell(createCell("Initial Conditions", getBold(DEF_HEADER_FONT_SIZE), 5, 1, Element.ALIGN_CENTER, true));
speciesSpecTable.addCell(createHeaderCell("Species", getBold(), 1));
speciesSpecTable.addCell(createHeaderCell("Structure", getBold(), 1));
speciesSpecTable.addCell(createHeaderCell("Initial Conc.", getBold(), 1));
speciesSpecTable.addCell(createHeaderCell("Diffusion Const.", getBold(), 1));
speciesSpecTable.addCell(createHeaderCell("Fixed (T/F)", getBold(), 1));
speciesSpecTable.endHeaders();
}
String speciesName = speciesContSpecs[i].getSpeciesContext().getSpecies().getCommonName();
String structName = speciesContSpecs[i].getSpeciesContext().getStructure().getName();
String diff = speciesContSpecs[i].getDiffusionParameter().getExpression().infix();
VCUnitDefinition diffUnit = speciesContSpecs[i].getDiffusionParameter().getUnitDefinition();
SpeciesContextSpecParameter initParam = speciesContSpecs[i].getInitialConditionParameter();
String initConc = initParam == null ? "" : initParam.getExpression().infix();
VCUnitDefinition initConcUnit = initParam == null ? null : initParam.getUnitDefinition();
speciesSpecTable.addCell(createCell(speciesName, getFont()));
speciesSpecTable.addCell(createCell(structName, getFont()));
speciesSpecTable.addCell(createCell(initConc + (initConcUnit == null ? "" : " " + initConcUnit.getSymbolUnicode()), getFont()));
speciesSpecTable.addCell(createCell(diff + (diffUnit == null ? "" : " " + diffUnit.getSymbolUnicode()), getFont()));
speciesSpecTable.addCell(createCell((speciesContSpecs[i].isConstant() ? " T " : " F "), getFont()));
}
if (speciesSpecTable != null) {
if (rcSection == null) {
rcSection = simContextSection.addSection("Reaction Mapping For " + simContext.getName(), simContextSection.numberDepth() + 1);
}
speciesSpecTable.setWidths(widths);
rcSection.add(speciesSpecTable);
}
}
use of cbit.vcell.mapping.ReactionContext in project vcell by virtualcell.
the class ModelProcessSpecsTableModel method setSimulationContext.
/**
* Sets the simulationContext property (cbit.vcell.mapping.SimulationContext) value.
* @param simulationContext The new value for the property.
* @see #getSimulationContext
*/
public void setSimulationContext(SimulationContext simulationContext) {
SimulationContext oldValue = fieldSimulationContext;
if (oldValue != null) {
oldValue.removePropertyChangeListener(this);
ReactionContext reactionContext = oldValue.getReactionContext();
reactionContext.removePropertyChangeListener(this);
ReactionSpec[] oldReactionSpecs = reactionContext.getReactionSpecs();
for (int i = 0; i < oldReactionSpecs.length; i++) {
oldReactionSpecs[i].getReactionStep().removePropertyChangeListener(this);
oldReactionSpecs[i].removePropertyChangeListener(this);
}
ReactionRuleSpec[] oldReactionRuleSpecs = reactionContext.getReactionRuleSpecs();
for (int i = 0; i < oldReactionRuleSpecs.length; i++) {
oldReactionRuleSpecs[i].getReactionRule().removePropertyChangeListener(this);
oldReactionRuleSpecs[i].removePropertyChangeListener(this);
}
}
fieldSimulationContext = simulationContext;
refreshColumns();
if (simulationContext != null) {
simulationContext.addPropertyChangeListener(this);
ReactionContext reactionContext = fieldSimulationContext.getReactionContext();
reactionContext.addPropertyChangeListener(this);
ReactionSpec[] newReactionSpecs = reactionContext.getReactionSpecs();
for (int i = 0; i < newReactionSpecs.length; i++) {
newReactionSpecs[i].getReactionStep().addPropertyChangeListener(this);
newReactionSpecs[i].addPropertyChangeListener(this);
}
ReactionRuleSpec[] newReactionRuleSpecs = reactionContext.getReactionRuleSpecs();
for (int i = 0; i < newReactionRuleSpecs.length; i++) {
newReactionRuleSpecs[i].getReactionRule().addPropertyChangeListener(this);
newReactionRuleSpecs[i].addPropertyChangeListener(this);
}
}
firePropertyChange("simulationContext", oldValue, simulationContext);
}
Aggregations