use of cbit.vcell.model.ReactionStep in project vcell by virtualcell.
the class ReactionCartoon method cleanupAll.
public void cleanupAll() {
if (getStructureSuite() != null) {
for (Structure structure : getStructureSuite().getStructures()) {
structure.removePropertyChangeListener(this);
if (structure instanceof Membrane) {
Membrane membrane = (Membrane) structure;
if (membrane.getMembraneVoltage() != null) {
membrane.getMembraneVoltage().removePropertyChangeListener(this);
}
}
}
}
if (getModel() != null) {
getModel().removePropertyChangeListener(this);
SpeciesContext[] structSpeciesContext = getModel().getSpeciesContexts();
if (structSpeciesContext != null) {
for (int i = 0; i < structSpeciesContext.length; i++) {
structSpeciesContext[i].removePropertyChangeListener(this);
structSpeciesContext[i].getSpecies().removePropertyChangeListener(this);
}
}
ReactionStep[] reactionSteps = getModel().getReactionSteps();
if (reactionSteps != null) {
for (int i = 0; i < reactionSteps.length; i++) {
reactionSteps[i].removePropertyChangeListener(this);
}
}
}
}
use of cbit.vcell.model.ReactionStep in project vcell by virtualcell.
the class ReactionCartoonMolecule method refreshAll.
@Override
protected void refreshAll(boolean reallocateShapes) {
try {
if (getModel() == null || getStructureSuite() == null) {
return;
}
System.out.println("ReactionCartoonMolecule, RefreshAll()");
for (Structure structure : structureSuite.getStructures()) {
Diagram diagram = getModel().getDiagram(structure);
if (diagram != null) {
// Maintain consistency between rule participant nodes, signatures and
// species pattern when a molecule is being modified.
rebindAll(diagram);
}
}
// calculate species context weight (number of reactions for which it's a participant)
Map<SpeciesContext, Integer> scWeightMap = new HashMap<>();
// all the species contexts that are catalysts
Set<SpeciesContext> scCatalystSet = new HashSet<>();
// calculate species context length (number of species patterns it contains, 1 if has no species patterns)
for (ReactionStep rs : getModel().getReactionSteps()) {
ReactionParticipant[] rpList = rs.getReactionParticipants();
for (int i = 0; i < rpList.length; i++) {
ReactionParticipant rp = rpList[i];
SpeciesContext sc = rp.getSpeciesContext();
int increment = 1;
if (rp instanceof Catalyst) {
scCatalystSet.add(sc);
}
if (scWeightMap.containsKey(sc)) {
int weight = scWeightMap.get(sc);
weight += increment;
scWeightMap.put(sc, weight);
} else {
scWeightMap.put(sc, increment);
}
}
}
Set<Shape> unwantedShapes = new HashSet<Shape>();
Set<RuleParticipantSignature> unwantedSignatures = new HashSet<RuleParticipantSignature>();
unwantedShapes.addAll(getShapes());
unwantedSignatures.addAll(ruleParticipantSignatures);
ContainerContainerShape containerShape = (ContainerContainerShape) getShapeFromModelObject(getModel());
List<ReactionContainerShape> reactionContainerShapeList = new ArrayList<ReactionContainerShape>();
List<Structure> structureList = new ArrayList<Structure>(getStructureSuite().getStructures());
// create all ReactionContainerShapes (one for each Structure)
for (Structure structure : structureList) {
if (structure instanceof Membrane) {
Membrane membrane = (Membrane) structure;
ReactionContainerShape membraneShape = (ReactionContainerShape) getShapeFromModelObject(membrane);
if (membraneShape == null) {
membraneShape = new ReactionContainerShape(membrane, structureSuite, this);
addShape(membraneShape);
membrane.getMembraneVoltage().removePropertyChangeListener(this);
membrane.getMembraneVoltage().addPropertyChangeListener(this);
} else {
membraneShape.setStructureSuite(structureSuite);
}
membrane.removePropertyChangeListener(this);
membrane.addPropertyChangeListener(this);
membraneShape.refreshLabel();
unwantedShapes.remove(membraneShape);
reactionContainerShapeList.add(membraneShape);
} else if (structure instanceof Feature) {
Feature feature = (Feature) structure;
ReactionContainerShape featureShape = (ReactionContainerShape) getShapeFromModelObject(feature);
if (featureShape == null) {
featureShape = new ReactionContainerShape(feature, structureSuite, this);
addShape(featureShape);
} else {
featureShape.setStructureSuite(structureSuite);
}
feature.removePropertyChangeListener(this);
feature.addPropertyChangeListener(this);
featureShape.refreshLabel();
unwantedShapes.remove(featureShape);
reactionContainerShapeList.add(featureShape);
}
}
if (containerShape == null) {
containerShape = new ContainerContainerShape(this, getModel(), reactionContainerShapeList);
addShape(containerShape);
} else {
containerShape.setReactionContainerShapeList(reactionContainerShapeList);
}
containerShape.refreshLabel();
unwantedShapes.remove(containerShape);
// add all species context shapes within the structures
for (Structure structure : getStructureSuite().getStructures()) {
ReactionContainerShape reactionContainerShape = (ReactionContainerShape) getShapeFromModelObject(structure);
structure.removePropertyChangeListener(this);
structure.addPropertyChangeListener(this);
for (SpeciesContext structSpeciesContext : getModel().getSpeciesContexts(structure)) {
SpeciesContextShape ss = (SpeciesContextShape) getShapeFromModelObject(structSpeciesContext);
if (ss == null) {
ss = new SpeciesContextShape(structSpeciesContext, this);
ss.truncateLabelName(false);
structSpeciesContext.getSpecies().removePropertyChangeListener(this);
structSpeciesContext.getSpecies().addPropertyChangeListener(this);
reactionContainerShape.addChildShape(ss);
addShape(ss);
ss.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
}
if (speciesSizeOption == SpeciesSizeOptions.weight) {
// this number sets the diameter of the shape
Integer weight = scWeightMap.get(structSpeciesContext);
if (weight != null) {
// we cap the diameter of the shape to something reasonable
weight = Math.min(weight, 16);
}
ss.setFilters(highlightCatalystOption ? scCatalystSet.contains(structSpeciesContext) : false, weight);
} else if (speciesSizeOption == SpeciesSizeOptions.length) {
Integer length = null;
if (structSpeciesContext.getSpeciesPattern() != null && !structSpeciesContext.getSpeciesPattern().getMolecularTypePatterns().isEmpty()) {
length = structSpeciesContext.getSpeciesPattern().getMolecularTypePatterns().size() * 2;
length = Math.min(length, 16);
}
ss.setFilters(highlightCatalystOption ? scCatalystSet.contains(structSpeciesContext) : false, length);
} else {
ss.setFilters(highlightCatalystOption ? scCatalystSet.contains(structSpeciesContext) : false, null);
}
structSpeciesContext.removePropertyChangeListener(this);
structSpeciesContext.addPropertyChangeListener(this);
ss.refreshLabel();
unwantedShapes.remove(ss);
}
}
// add all reactionSteps that are in this structure (ReactionContainerShape), and draw the lines
getModel().removePropertyChangeListener(this);
getModel().addPropertyChangeListener(this);
//
for (ReactionRule rr : getModel().getRbmModelContainer().getReactionRuleList()) {
rr.removePropertyChangeListener(this);
rr.addPropertyChangeListener(this);
Structure structure = rr.getStructure();
if (getStructureSuite().areReactionsShownFor(structure)) {
ReactionContainerShape reactionContainerShape = (ReactionContainerShape) getShapeFromModelObject(structure);
ReactionRuleFullDiagramShape rrShape = (ReactionRuleFullDiagramShape) getShapeFromModelObject(rr);
if (rrShape == null) {
rrShape = new ReactionRuleFullDiagramShape(rr, this);
addShape(rrShape);
rrShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
reactionContainerShape.addChildShape(rrShape);
rrShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
}
rrShape.refreshLabel();
unwantedShapes.remove(rrShape);
//
// add reaction participants as edges and SignatureShapes as needed
//
List<ReactionRuleParticipant> participants = rr.getReactionRuleParticipants();
List<RuleParticipantEdgeDiagramShape> ruleEdges = new ArrayList<>();
for (ReactionRuleParticipant participant : participants) {
participant.getSpeciesPattern().removePropertyChangeListener(this);
participant.getSpeciesPattern().addPropertyChangeListener(this);
Structure speciesStructure = participant.getStructure();
Structure reactionStructure = rr.getStructure();
if (getStructureSuite().getStructures().contains(speciesStructure) && getStructureSuite().areReactionsShownFor(reactionStructure)) {
//
// find existing RuleParticipantSignatureShape in cartoon
//
RuleParticipantShortSignature ruleParticipantShortSignature = null;
for (RuleParticipantSignature signature : ruleParticipantSignatures) {
if (signature instanceof RuleParticipantLongSignature && signature.getStructure() == participant.getStructure()) {
System.out.println("ReactionCartoonMolecule, refreshAll(), RuleParticipantLongSignature");
break;
}
}
for (RuleParticipantSignature signature : ruleParticipantSignatures) {
if (signature instanceof RuleParticipantShortSignature && signature.getStructure() == participant.getStructure() && signature.compareByCriteria(participant.getSpeciesPattern(), GroupingCriteria.molecule)) {
ruleParticipantShortSignature = (RuleParticipantShortSignature) signature;
break;
}
}
//
// if didn't find signature in cartoons list of signatures, then create one (and create a shape for it).
//
RuleParticipantSignatureShortDiagramShape signatureShape = null;
if (ruleParticipantShortSignature == null) {
ruleParticipantShortSignature = RuleParticipantShortSignature.fromReactionRuleParticipant(participant, this);
ruleParticipantSignatures.add(ruleParticipantShortSignature);
signatureShape = new RuleParticipantSignatureShortDiagramShape(ruleParticipantShortSignature, this);
addShape(signatureShape);
ReactionContainerShape participantContainerShape = (ReactionContainerShape) getShapeFromModelObject(participant.getStructure());
signatureShape.getSpaceManager().setRelPos(participantContainerShape.getRandomPosition());
participantContainerShape.addChildShape(signatureShape);
signatureShape.getSpaceManager().setRelPos(participantContainerShape.getRandomPosition());
} else {
signatureShape = (RuleParticipantSignatureShortDiagramShape) getShapeFromModelObject(ruleParticipantShortSignature);
}
unwantedShapes.remove(signatureShape);
unwantedSignatures.remove(ruleParticipantShortSignature);
signatureShape.refreshLabel();
signatureShape.setVisible(true);
//
// add edge for ReactionRuleParticipant if not already present.
//
RuleParticipantEdgeDiagramShape ruleParticipantShape = (RuleParticipantEdgeDiagramShape) getShapeFromModelObject(participant);
if (ruleParticipantShape == null || ruleParticipantShape.getRuleParticipantSignatureShape() != signatureShape) {
if (participant instanceof ReactantPattern && signatureShape.isVisible()) {
ruleParticipantShape = new ReactantPatternEdgeDiagramShape((ReactantPattern) participant, rrShape, signatureShape, this);
} else if (participant instanceof ProductPattern && signatureShape.isVisible()) {
ruleParticipantShape = new ProductPatternEdgeDiagramShape((ProductPattern) participant, rrShape, signatureShape, this);
} else {
throw new RuntimeException("unsupported ReactionRuleParticipant " + participant.getClass());
}
addShape(ruleParticipantShape);
}
if (!containerShape.getChildren().contains(ruleParticipantShape)) {
containerShape.addChildShape(ruleParticipantShape);
}
unwantedShapes.remove(ruleParticipantShape);
ruleParticipantShape.refreshLabel();
// all the edges for this rule
ruleEdges.add(ruleParticipantShape);
}
}
// a product edge (a closed loop) between the rule diagram shape and the signature diagram shape
for (RuleParticipantEdgeDiagramShape ours : ruleEdges) {
// reset them all
ours.setSibling(false);
}
for (RuleParticipantEdgeDiagramShape ours : ruleEdges) {
for (RuleParticipantEdgeDiagramShape theirs : ruleEdges) {
if (ours == theirs) {
// don't compare with self
continue;
}
if (ours.getRuleParticipantSignatureShape() == theirs.getRuleParticipantSignatureShape()) {
ours.setSibling(true);
theirs.setSibling(true);
}
}
}
}
}
ruleParticipantSignatures.removeAll(unwantedSignatures);
for (ReactionStep reactionStep : getModel().getReactionSteps()) {
reactionStep.removePropertyChangeListener(this);
reactionStep.addPropertyChangeListener(this);
Structure structure = reactionStep.getStructure();
if (getStructureSuite().areReactionsShownFor(structure)) {
ReactionContainerShape reactionContainerShape = (ReactionContainerShape) getShapeFromModelObject(structure);
if (reactionContainerShape == null) {
System.out.println("Reaction container shape is null for structure " + structure + " for reaction step " + reactionStep);
}
ReactionStepShape reactionStepShape = (ReactionStepShape) getShapeFromModelObject(reactionStep);
if (reactionStepShape == null) {
if (reactionStep instanceof SimpleReaction) {
reactionStepShape = new SimpleReactionShape((SimpleReaction) reactionStep, this);
} else if (reactionStep instanceof FluxReaction) {
reactionStepShape = new FluxReactionShape((FluxReaction) reactionStep, this);
} else {
throw new RuntimeException("unknown type of ReactionStep '" + reactionStep.getClass().toString());
}
addShape(reactionStepShape);
reactionStepShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
reactionContainerShape.addChildShape(reactionStepShape);
reactionStepShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
}
reactionStepShape.refreshLabel();
unwantedShapes.remove(reactionStepShape);
// add reaction participants as edges
for (ReactionParticipant participant : reactionStep.getReactionParticipants()) {
participant.removePropertyChangeListener(this);
participant.addPropertyChangeListener(this);
Structure speciesStructure = participant.getStructure();
Structure reactionStructure = reactionStep.getStructure();
if (getStructureSuite().getStructures().contains(speciesStructure) && getStructureSuite().areReactionsShownFor(reactionStructure)) {
SpeciesContext speciesContext = getModel().getSpeciesContext(participant.getSpecies(), speciesStructure);
// add speciesContextShapes that are not in this structure, but are referenced from the reactionParticipants
// these are only when reactionParticipants are from features that are outside of the membrane being displayed
SpeciesContextShape speciesContextShape = (SpeciesContextShape) getShapeFromModelObject(speciesContext);
if (speciesContextShape == null) {
speciesContextShape = new SpeciesContextShape(speciesContext, this);
speciesContextShape.truncateLabelName(false);
reactionContainerShape.addChildShape(speciesContextShape);
addShape(speciesContextShape);
speciesContextShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
}
speciesContextShape.refreshLabel();
unwantedShapes.remove(speciesContextShape);
ReactionParticipantShape reactionParticipantShape = (ReactionParticipantShape) getShapeFromModelObject(participant);
if (reactionParticipantShape == null) {
if (participant instanceof Reactant) {
reactionParticipantShape = new ReactantShape((Reactant) participant, reactionStepShape, speciesContextShape, this);
} else if (participant instanceof Product) {
reactionParticipantShape = new ProductShape((Product) participant, reactionStepShape, speciesContextShape, this);
} else if (participant instanceof Catalyst) {
reactionParticipantShape = new CatalystShape((Catalyst) participant, reactionStepShape, speciesContextShape, this);
} else {
throw new RuntimeException("unsupported ReactionParticipant " + participant.getClass());
}
addShape(reactionParticipantShape);
}
if (!containerShape.getChildren().contains(reactionParticipantShape)) {
containerShape.addChildShape(reactionParticipantShape);
}
unwantedShapes.remove(reactionParticipantShape);
reactionParticipantShape.refreshLabel();
}
}
}
}
for (Shape unwantedShape : unwantedShapes) {
removeShape(unwantedShape);
}
// update diagrams
for (Structure structure : structureSuite.getStructures()) {
Diagram diagram = getModel().getDiagram(structure);
if (diagram != null) {
applyDefaults(diagram);
}
}
fireGraphChanged(new GraphEvent(this));
} catch (Throwable e) {
handleException(e);
}
}
use of cbit.vcell.model.ReactionStep in project vcell by virtualcell.
the class SBMLImporter method processAssignmentRules.
// called after the reactions are parsed, we check if the rule expression references any reaction by name
// if yes, we replace the reaction name with the expression of the reaction rate
private void processAssignmentRules() throws ExpressionException, PropertyVetoException {
if (assignmentRulesHash == null || assignmentRulesHash.isEmpty()) {
System.out.println("SBML Import: no assignment rules.");
return;
}
SimulationContext simContext = vcBioModel.getSimulationContext(0);
HashMap<String, Expression> destMap = new HashMap<>();
for (Map.Entry<String, Expression> entry : assignmentRulesHash.entrySet()) {
String vcVariableName = entry.getKey();
Expression oldExpression = entry.getValue();
// check if the expression has unresolved symbols
String[] symbols = oldExpression.getSymbols();
if (symbols != null) {
for (String symbol : symbols) {
SymbolTableEntry ste = simContext.getEntry(symbol);
if (ste == null) {
// a symbol in the expression is not species or global; we check if it's a reaction name
ReactionStep candidate = vcBioModel.getModel().getReactionStep(symbol);
if (candidate != null) {
// we replace the reaction name with the expression of the reaction rate, if possible
// convert user defined local parameters to global, otherwise the expression won't bind
convertLocalParameters(candidate);
Kinetics kinetics = candidate.getKinetics();
KineticsParameter reactionRate = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_LumpedReactionRate);
if (reactionRate != null && reactionRate.getExpression() != null) {
Expression rrExpression = reactionRate.getExpression();
oldExpression.substituteInPlace(new Expression(symbol), new Expression(rrExpression.flatten()));
} else {
throw new RuntimeException("Unsupported use of a Reaction name '" + symbol + "' in the expression of an AssignmentRule variable '" + vcVariableName + "'.");
}
} else {
System.out.println("Symbol '" + symbol + "' still unresolved.");
}
}
}
}
Expression newExpression = new Expression(oldExpression);
destMap.put(vcVariableName, newExpression);
}
assignmentRulesHash = destMap;
}
use of cbit.vcell.model.ReactionStep in project vcell by virtualcell.
the class SBMLImporter method processParameters.
private void processParameters() throws ExpressionException, PropertyVetoException {
SimulationContext simContext = vcBioModel.getSimulationContext(0);
Model vcModel = simContext.getModel();
ModelParameter[] mps = vcModel.getModelParameters();
if (mps == null || mps.length == 0) {
System.out.println("SBML Import: no model parameters.");
return;
}
for (ModelParameter mp : mps) {
Expression expression = mp.getExpression();
if (expression.isNumeric()) {
continue;
}
// check if the expression has unresolved symbols
String[] symbols = expression.getSymbols();
if (symbols != null) {
for (String symbol : symbols) {
SymbolTableEntry ste = simContext.getEntry(symbol);
if (ste == null) {
ReactionStep candidate = vcBioModel.getModel().getReactionStep(symbol);
if (candidate != null) {
convertLocalParameters(candidate);
// we replace the reaction name with the expression of the reaction rate, if possible
Kinetics kinetics = candidate.getKinetics();
KineticsParameter reactionRate = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_LumpedReactionRate);
if (reactionRate != null && reactionRate.getExpression() != null) {
Expression rrExpression = reactionRate.getExpression();
expression.substituteInPlace(new Expression(symbol), new Expression(rrExpression.flatten()));
} else {
throw new SBMLImportException("Unsupported use of a Reaction name '" + symbol + "' in the expression of Global Parameter '" + mp.getDisplayName() + "'.");
}
} else {
System.out.println("Symbol '" + symbol + "' still unresolved.");
}
}
}
}
}
}
use of cbit.vcell.model.ReactionStep in project vcell by virtualcell.
the class SBMLImporter method processRateRules.
// called after the reactions are parsed, we check if the rule expression references any reaction by name
// if yes, we replace the reaction name with the expression of the reaction rate
private void processRateRules() throws ExpressionException, PropertyVetoException {
if (rateRulesHash == null || rateRulesHash.isEmpty()) {
// System.out.println("SBML Import: no rate rules.");
return;
}
SimulationContext simContext = vcBioModel.getSimulationContext(0);
HashMap<String, Expression> destMap = new HashMap<>();
for (Map.Entry<String, Expression> entry : rateRulesHash.entrySet()) {
String vcVariableName = entry.getKey();
Expression vcRateRuleExpr = entry.getValue();
// check if the expression has unresolved symbols
String[] symbols = vcRateRuleExpr.getSymbols();
if (symbols != null) {
for (String symbol : symbols) {
SymbolTableEntry ste = simContext.getEntry(symbol);
if (ste == null) {
ReactionStep candidate = vcBioModel.getModel().getReactionStep(symbol);
if (candidate != null) {
convertLocalParameters(candidate);
// we replace the reaction name with the expression of the reaction rate, if possible
Kinetics kinetics = candidate.getKinetics();
KineticsParameter reactionRate = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_LumpedReactionRate);
if (reactionRate != null && reactionRate.getExpression() != null) {
Expression rrExpression = reactionRate.getExpression();
vcRateRuleExpr.substituteInPlace(new Expression(symbol), new Expression(rrExpression.flatten()));
} else {
throw new RuntimeException("Unsupported use of a Reaction name '" + symbol + "' in the expression of RateRule variable '" + vcVariableName + "'.");
}
} else {
System.out.println("Symbol '" + symbol + "' still unresolved.");
}
}
}
}
Expression newExpression = new Expression(vcRateRuleExpr);
destMap.put(vcVariableName, newExpression);
}
rateRulesHash = destMap;
for (Map.Entry<String, Expression> entry : rateRulesHash.entrySet()) {
String vcVariableName = entry.getKey();
Expression vcRateRuleExpr = entry.getValue();
// check if the expression has unresolved symbols
String[] symbols = vcRateRuleExpr.getSymbols();
if (symbols != null) {
for (String symbol : symbols) {
SymbolTableEntry ste = simContext.getEntry(symbol);
if (ste == null) {
throw new SBMLException("RateRule variable '" + vcVariableName + "' expression symbol '" + symbol + "' still unresolved.");
}
}
}
}
}
Aggregations