use of cbit.vcell.model.ReactionRule in project vcell by virtualcell.
the class RulebasedTransformer method parseBngOutput.
private void parseBngOutput(SimulationContext simContext, Set<ReactionRule> fromReactions, BNGOutput bngOutput) {
Model model = simContext.getModel();
Document bngNFSimXMLDocument = bngOutput.getNFSimXMLDocument();
bngRootElement = bngNFSimXMLDocument.getRootElement();
Element modelElement = bngRootElement.getChild("model", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
Element listOfReactionRulesElement = modelElement.getChild("ListOfReactionRules", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
List<Element> reactionRuleChildren = new ArrayList<Element>();
reactionRuleChildren = listOfReactionRulesElement.getChildren("ReactionRule", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
for (Element reactionRuleElement : reactionRuleChildren) {
ReactionRuleAnalysisReport rar = new ReactionRuleAnalysisReport();
boolean bForward = true;
String rule_id_str = reactionRuleElement.getAttributeValue("id");
ruleElementMap.put(rule_id_str, reactionRuleElement);
String rule_name_str = reactionRuleElement.getAttributeValue("name");
String symmetry_factor_str = reactionRuleElement.getAttributeValue("symmetry_factor");
Double symmetry_factor_double = Double.parseDouble(symmetry_factor_str);
ReactionRule rr = null;
if (rule_name_str.startsWith("_reverse_")) {
bForward = false;
rule_name_str = rule_name_str.substring("_reverse_".length());
rr = model.getRbmModelContainer().getReactionRule(rule_name_str);
RbmKineticLaw rkl = rr.getKineticLaw();
try {
if (symmetry_factor_double != 1.0 && fromReactions.contains(rr)) {
Expression expression = rkl.getLocalParameterValue(RbmKineticLawParameterType.MassActionReverseRate);
expression = Expression.div(expression, new Expression(symmetry_factor_double));
rkl.setLocalParameterValue(RbmKineticLawParameterType.MassActionReverseRate, expression);
}
} catch (ExpressionException | PropertyVetoException exc) {
exc.printStackTrace();
throw new RuntimeException("Unexpected transform exception: " + exc.getMessage());
}
rulesReverseMap.put(rr, rar);
} else {
rr = model.getRbmModelContainer().getReactionRule(rule_name_str);
RbmKineticLaw rkl = rr.getKineticLaw();
try {
if (symmetry_factor_double != 1.0 && fromReactions.contains(rr)) {
Expression expression = rkl.getLocalParameterValue(RbmKineticLawParameterType.MassActionForwardRate);
expression = Expression.div(expression, new Expression(symmetry_factor_double));
rkl.setLocalParameterValue(RbmKineticLawParameterType.MassActionForwardRate, expression);
}
} catch (ExpressionException | PropertyVetoException exc) {
exc.printStackTrace();
throw new RuntimeException("Unexpected transform exception: " + exc.getMessage());
}
rulesForwardMap.put(rr, rar);
}
rar.symmetryFactor = symmetry_factor_double;
System.out.println("rule id=" + rule_id_str + ", name=" + rule_name_str + ", symmetry factor=" + symmetry_factor_str);
keyMap.put(rule_id_str, rr);
Element listOfReactantPatternsElement = reactionRuleElement.getChild("ListOfReactantPatterns", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
List<Element> reactantPatternChildren = new ArrayList<Element>();
reactantPatternChildren = listOfReactantPatternsElement.getChildren("ReactantPattern", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
for (int i = 0; i < reactantPatternChildren.size(); i++) {
Element reactantPatternElement = reactantPatternChildren.get(i);
String pattern_id_str = reactantPatternElement.getAttributeValue("id");
ReactionRuleParticipant p = bForward ? rr.getReactantPattern(i) : rr.getProductPattern(i);
SpeciesPattern sp = p.getSpeciesPattern();
System.out.println(" reactant id=" + pattern_id_str + ", name=" + sp.toString());
keyMap.put(pattern_id_str, sp);
// list of molecules
extractMolecules(sp, model, reactantPatternElement);
// list of bonds (not implemented)
extractBonds(reactantPatternElement);
}
Element listOfProductPatternsElement = reactionRuleElement.getChild("ListOfProductPatterns", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
List<Element> productPatternChildren = new ArrayList<Element>();
productPatternChildren = listOfProductPatternsElement.getChildren("ProductPattern", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
for (int i = 0; i < productPatternChildren.size(); i++) {
Element productPatternElement = productPatternChildren.get(i);
String pattern_id_str = productPatternElement.getAttributeValue("id");
ReactionRuleParticipant p = bForward ? rr.getProductPattern(i) : rr.getReactantPattern(i);
SpeciesPattern sp = p.getSpeciesPattern();
System.out.println(" product id=" + pattern_id_str + ", name=" + sp.toString());
keyMap.put(pattern_id_str, sp);
extractMolecules(sp, model, productPatternElement);
extractBonds(productPatternElement);
}
// extract the Map for this rule
Element listOfMapElement = reactionRuleElement.getChild("Map", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
List<Element> mapChildren = new ArrayList<Element>();
mapChildren = listOfMapElement.getChildren("MapItem", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
for (Element mapElement : mapChildren) {
String target_id_str = mapElement.getAttributeValue("targetID");
String source_id_str = mapElement.getAttributeValue("sourceID");
System.out.println("Map: target=" + target_id_str + " source=" + source_id_str);
RbmObject target_object = keyMap.get(target_id_str);
RbmObject source_object = keyMap.get(source_id_str);
if (target_object == null)
System.out.println("!!! Missing map target " + target_id_str);
if (source_object == null)
System.out.println("!!! Missing map source " + source_id_str);
if (source_object != null) {
// target_object may be null
System.out.println(" target=" + target_object + " source=" + source_object);
Pair<RbmObject, RbmObject> mapEntry = new Pair<RbmObject, RbmObject>(target_object, source_object);
rar.objmappingList.add(mapEntry);
}
Pair<String, String> idmapEntry = new Pair<String, String>(target_id_str, source_id_str);
rar.idmappingList.add(idmapEntry);
}
// ListOfOperations
Element listOfOperationsElement = reactionRuleElement.getChild("ListOfOperations", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
List<Element> operationsChildren = new ArrayList<Element>();
operationsChildren = listOfOperationsElement.getChildren("StateChange", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
System.out.println("ListOfOperations");
for (Element operationsElement : operationsChildren) {
String finalState_str = operationsElement.getAttributeValue("finalState");
String site_str = operationsElement.getAttributeValue("site");
RbmObject site_object = keyMap.get(site_str);
if (site_object == null)
System.out.println("!!! Missing map object " + site_str);
if (site_object != null) {
System.out.println(" finalState=" + finalState_str + " site=" + site_object);
StateChangeOperation sco = new StateChangeOperation(finalState_str, site_str, site_object);
rar.operationsList.add(sco);
}
}
operationsChildren = listOfOperationsElement.getChildren("AddBond", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
for (Element operationsElement : operationsChildren) {
String site1_str = operationsElement.getAttributeValue("site1");
String site2_str = operationsElement.getAttributeValue("site2");
RbmObject site1_object = keyMap.get(site1_str);
RbmObject site2_object = keyMap.get(site2_str);
if (site1_object == null)
System.out.println("!!! Missing map object " + site1_str);
if (site2_object == null)
System.out.println("!!! Missing map object " + site2_str);
if (site1_object != null && site2_object != null) {
System.out.println(" site1=" + site1_object + " site2=" + site2_object);
AddBondOperation abo = new AddBondOperation(site1_str, site2_str, site1_object, site2_object);
rar.operationsList.add(abo);
}
}
operationsChildren = listOfOperationsElement.getChildren("DeleteBond", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
for (Element operationsElement : operationsChildren) {
String site1_str = operationsElement.getAttributeValue("site1");
String site2_str = operationsElement.getAttributeValue("site2");
RbmObject site1_object = keyMap.get(site1_str);
RbmObject site2_object = keyMap.get(site2_str);
if (site1_object == null)
System.out.println("!!! Missing map object " + site1_str);
if (site2_object == null)
System.out.println("!!! Missing map object " + site2_str);
if (site1_object != null && site2_object != null) {
System.out.println(" site1=" + site1_object + " site2=" + site2_object);
DeleteBondOperation dbo = new DeleteBondOperation(site1_str, site2_str, site1_object, site2_object);
rar.operationsList.add(dbo);
}
}
operationsChildren = listOfOperationsElement.getChildren("Add", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
for (Element operationsElement : operationsChildren) {
String id_str = operationsElement.getAttributeValue("id");
RbmObject id_object = keyMap.get(id_str);
if (id_object == null)
System.out.println("!!! Missing map object " + id_str);
if (id_object != null) {
System.out.println(" id=" + id_str);
AddOperation ao = new AddOperation(id_str, id_object);
rar.operationsList.add(ao);
}
}
operationsChildren = listOfOperationsElement.getChildren("Delete", Namespace.getNamespace("http://www.sbml.org/sbml/level3"));
for (Element operationsElement : operationsChildren) {
String id_str = operationsElement.getAttributeValue("id");
String delete_molecules_str = operationsElement.getAttributeValue("DeleteMolecules");
RbmObject id_object = keyMap.get(id_str);
if (id_object == null)
System.out.println("!!! Missing map object " + id_str);
int delete_molecules_int = 0;
if (delete_molecules_str != null) {
delete_molecules_int = Integer.parseInt(delete_molecules_str);
}
if (id_object != null) {
System.out.println(" id=" + id_str + ", DeleteMolecules=" + delete_molecules_str);
DeleteOperation dop = new DeleteOperation(id_str, id_object, delete_molecules_int);
rar.operationsList.add(dop);
}
}
}
System.out.println("done parsing xml file");
}
use of cbit.vcell.model.ReactionRule in project vcell by virtualcell.
the class BNGExecutorServiceMultipass method preprocessInput.
// parse the compartmental bngl file and produce the "trick"
// where each molecule has an extra Site with the compartments as possible States
// a reserved name will be used for this Site
//
private String preprocessInput(String cBngInputString) throws ParseException, PropertyVetoException, ExpressionBindingException {
// take the cBNGL file (as string), parse it to recover the rules (we'll need them later)
// and create the bngl string with the extra, fake site for the compartments
BioModel bioModel = new BioModel(null);
bioModel.setName("BngBioModel");
model = new Model("model");
bioModel.setModel(model);
model.createFeature();
simContext = bioModel.addNewSimulationContext("BioNetGen app", SimulationContext.Application.NETWORK_DETERMINISTIC);
List<SimulationContext> appList = new ArrayList<SimulationContext>();
appList.add(simContext);
// set convention for initial conditions in generated application for seed species (concentration or count)
BngUnitSystem bngUnitSystem = new BngUnitSystem(BngUnitOrigin.DEFAULT);
InputStream is = new ByteArrayInputStream(cBngInputString.getBytes());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
ASTModel astModel = RbmUtils.importBnglFile(br);
if (astModel.hasCompartments()) {
Structure struct = model.getStructure(0);
if (struct != null) {
try {
model.removeStructure(struct);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
}
BnglObjectConstructionVisitor constructionVisitor = null;
constructionVisitor = new BnglObjectConstructionVisitor(model, appList, bngUnitSystem, true);
astModel.jjtAccept(constructionVisitor, model.getRbmModelContainer());
int numCompartments = model.getStructures().length;
if (numCompartments == 0) {
throw new RuntimeException("No structure present in the bngl file.");
} else if (numCompartments == 1) {
// for single compartment we don't need the 'trick'
compartmentMode = CompartmentMode.hide;
} else {
compartmentMode = CompartmentMode.asSite;
}
// extract all polymer observables for special treatment at the end
for (RbmObservable oo : model.getRbmModelContainer().getObservableList()) {
if (oo.getSequence() == RbmObservable.Sequence.PolymerLengthEqual) {
polymerEqualObservables.add(oo);
} else if (oo.getSequence() == RbmObservable.Sequence.PolymerLengthGreater) {
polymerGreaterObservables.add(oo);
}
}
for (RbmObservable oo : polymerEqualObservables) {
model.getRbmModelContainer().removeObservable(oo);
}
for (RbmObservable oo : polymerGreaterObservables) {
model.getRbmModelContainer().removeObservable(oo);
}
// replace all reversible rules with 2 direct rules
List<ReactionRule> newRRList = new ArrayList<>();
for (ReactionRule rr : model.getRbmModelContainer().getReactionRuleList()) {
if (rr.isReversible()) {
ReactionRule rr1 = ReactionRule.deriveDirectRule(rr);
newRRList.add(rr1);
ReactionRule rr2 = ReactionRule.deriveInverseRule(rr);
newRRList.add(rr2);
} else {
newRRList.add(rr);
}
model.getRbmModelContainer().removeReactionRule(rr);
}
// model.getRbmModelContainer().getReactionRuleList().clear();
model.getRbmModelContainer().setReactionRules(newRRList);
StringWriter bnglStringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(bnglStringWriter);
writer.println(RbmNetworkGenerator.BEGIN_MODEL);
writer.println();
// RbmNetworkGenerator.writeCompartments(writer, model, null);
RbmNetworkGenerator.writeParameters(writer, model.getRbmModelContainer(), false);
RbmNetworkGenerator.writeMolecularTypes(writer, model, compartmentMode);
RbmNetworkGenerator.writeSpeciesSortedAlphabetically(writer, model, simContext, compartmentMode);
RbmNetworkGenerator.writeObservables(writer, model.getRbmModelContainer(), compartmentMode);
// RbmNetworkGenerator.writeFunctions(writer, rbmModelContainer, ignoreFunctions);
RbmNetworkGenerator.writeReactions(writer, model.getRbmModelContainer(), null, false, compartmentMode);
writer.println(RbmNetworkGenerator.END_MODEL);
writer.println();
// we parse the real numbers from the bngl file provided by the caller, the nc in the simContext has the default ones
NetworkConstraints realNC = extractNetworkConstraints(cBngInputString);
simContext.getNetworkConstraints().setMaxMoleculesPerSpecies(realNC.getMaxMoleculesPerSpecies());
simContext.getNetworkConstraints().setMaxIteration(realNC.getMaxIteration());
RbmNetworkGenerator.generateNetworkEx(1, simContext.getNetworkConstraints().getMaxMoleculesPerSpecies(), writer, model.getRbmModelContainer(), simContext, NetworkGenerationRequirements.AllowTruncatedStandardTimeout);
String sInputString = bnglStringWriter.toString();
return sInputString;
}
use of cbit.vcell.model.ReactionRule in project vcell by virtualcell.
the class RbmReactionParticipantTreeCellRenderer method getTreeCellRendererComponent.
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
setBorder(null);
if (value instanceof BioModelNode) {
BioModelNode node = (BioModelNode) value;
Object userObject = node.getUserObject();
obj = userObject;
String text = null;
Icon icon = null;
String toolTip = null;
if (userObject instanceof ReactionRule) {
ReactionRule rr = (ReactionRule) userObject;
text = toHtml(rr);
toolTip = toHtmlWithTip(rr);
icon = rr.isReversible() ? VCellIcons.rbmReactRuleReversIcon : VCellIcons.rbmReactRuleDirectIcon;
} else if (userObject instanceof ReactionRuleParticipantLocal) {
ReactionRuleParticipantLocal rrp = (ReactionRuleParticipantLocal) userObject;
text = toHtml(rrp, true);
toolTip = toHtmlWithTip(rrp, true);
icon = rrp.type == ReactionRuleParticipantType.Reactant ? VCellIcons.rbmReactantIcon : VCellIcons.rbmProductIcon;
} else if (userObject instanceof MolecularTypePattern) {
MolecularTypePattern molecularTypePattern = (MolecularTypePattern) userObject;
text = toHtml(molecularTypePattern, true);
toolTip = toHtmlWithTip(molecularTypePattern, true);
if (owner == null) {
icon = VCellIcons.rbmMolecularTypeSimpleIcon;
;
} else {
Graphics gc = owner.getGraphics();
icon = new MolecularTypeSmallShape(1, 5, molecularTypePattern.getMolecularType(), null, gc, molecularTypePattern.getMolecularType(), null, issueManager);
}
} else if (userObject instanceof MolecularComponentPattern) {
MolecularComponentPattern mcp = (MolecularComponentPattern) userObject;
text = toHtml(mcp, true);
toolTip = toHtmlWithTip(mcp, true);
icon = VCellIcons.rbmComponentGrayIcon;
if (mcp.getMolecularComponent().getComponentStateDefinitions().size() > 0) {
icon = VCellIcons.rbmComponentGrayStateIcon;
}
if (mcp.isbVisible()) {
icon = VCellIcons.rbmComponentGreenIcon;
if (mcp.getMolecularComponent().getComponentStateDefinitions().size() > 0) {
icon = VCellIcons.rbmComponentGreenStateIcon;
}
}
ComponentStatePattern csp = mcp.getComponentStatePattern();
if (csp != null && !csp.isAny()) {
icon = VCellIcons.rbmComponentGreenIcon;
if (mcp.getMolecularComponent().getComponentStateDefinitions().size() > 0) {
icon = VCellIcons.rbmComponentGreenStateIcon;
}
}
BioModelNode parent = (BioModelNode) ((BioModelNode) value).getParent().getParent().getParent();
if (parent == null) {
icon = VCellIcons.rbmComponentErrorIcon;
return this;
}
} else if (userObject instanceof StateLocal) {
StateLocal sl = (StateLocal) userObject;
text = toHtml(sl, true);
toolTip = toHtmlWithTip(sl, true);
icon = VCellIcons.rbmComponentStateIcon;
} else if (userObject instanceof BondLocal) {
BondLocal bl = (BondLocal) userObject;
text = toHtml(bl, sel);
toolTip = toHtmlWithTip(bl, true);
icon = VCellIcons.rbmBondIcon;
} else if (userObject instanceof ParticipantMatchLabelLocal) {
ParticipantMatchLabelLocal pmll = (ParticipantMatchLabelLocal) userObject;
text = toHtml(pmll, sel);
toolTip = toHtmlWithTip(pmll, true);
icon = VCellIcons.rbmBondIcon;
} else {
if (userObject != null) {
System.out.println(userObject.toString());
text = userObject.toString();
} else {
text = "null user object";
}
}
setText(text);
setIcon(icon);
setToolTipText(toolTip == null ? text : toolTip);
}
return this;
}
use of cbit.vcell.model.ReactionRule in project vcell by virtualcell.
the class ReactionRuleParticipantSignaturePropertiesPanel method updateShape.
private void updateShape() {
int maxXOffset = 0;
ruleShapeList.clear();
// all the reactants go in one single ReactionRulePatternLargeShape, all the products the other
int yOffset = yOffsetReactantInitial + GraphConstants.ReactionRuleParticipantDisplay_ReservedSpaceForNameOnYAxis;
for (Map.Entry<String, ReactionRule> entry : reactionRuleMap.entrySet()) {
ReactionRule rr = entry.getValue();
ReactionRulePatternLargeShape reactantShape = new ReactionRulePatternLargeShape(xOffsetInitial, yOffset, -1, shapePanel, rr, true, issueManager);
reactantShape.setWriteName(true);
int xOffset = reactantShape.getRightEnd() + 70;
ReactionRulePatternLargeShape productShape = new ReactionRulePatternLargeShape(xOffset, yOffset, -1, shapePanel, rr, false, issueManager);
xOffset += productShape.getRightEnd();
yOffset += SpeciesPatternLargeShape.defaultHeight + GraphConstants.ReactionRuleParticipantDisplay_ReservedSpaceForNameOnYAxis;
maxXOffset = Math.max(maxXOffset, xOffset);
Pair<ReactionRulePatternLargeShape, ReactionRulePatternLargeShape> p = new Pair<>(reactantShape, productShape);
ruleShapeList.add(p);
}
int maxYOffset = Math.max(yOffsetReactantInitial + SpeciesPatternLargeShape.defaultHeight + GraphConstants.ReactionRuleParticipantDisplay_ReservedSpaceForNameOnYAxis, yOffsetReactantInitial + (SpeciesPatternLargeShape.defaultHeight + GraphConstants.ReactionRuleParticipantDisplay_ReservedSpaceForNameOnYAxis) * ruleShapeList.size());
Dimension preferredSize = new Dimension(maxXOffset, maxYOffset);
shapePanel.setPreferredSize(preferredSize);
containerOfScrollPanel.repaint();
}
use of cbit.vcell.model.ReactionRule in project vcell by virtualcell.
the class ReactionRulePropertiesPanel method setReactionRule.
private void setReactionRule(ReactionRule newValue) {
kpp.setReactionRule(reactionRule);
epp.setReactionRule(reactionRule);
if (reactionRule == newValue) {
return;
}
ReactionRule oldValue = reactionRule;
if (oldValue != null) {
oldValue.removePropertyChangeListener(eventHandler);
}
reactionRule = newValue;
if (newValue != null) {
newValue.addPropertyChangeListener(eventHandler);
}
refreshInterface();
}
Aggregations