use of org.vcell.model.rbm.MolecularType in project vcell by virtualcell.
the class ViewGeneratedReactionsPanel method updateShape.
public void updateShape(int selectedRow) {
GeneratedReactionTableRow reactionTableRow = tableModel.getValueAt(selectedRow);
String inputString = reactionTableRow.getExpression();
System.out.println(selectedRow + ": " + inputString);
// ReactionRule newReactionRule = (ReactionRule)RbmUtils.parseReactionRule(inputString, bioModel);
Model tempModel = null;
try {
tempModel = new Model("MyTempModel");
tempModel.addFeature("c0");
} catch (ModelException | PropertyVetoException e1) {
e1.printStackTrace();
}
if (owner != null && owner.getSimulationContext() != null) {
List<MolecularType> mtList = owner.getSimulationContext().getModel().getRbmModelContainer().getMolecularTypeList();
try {
tempModel.getRbmModelContainer().setMolecularTypeList(mtList);
} catch (PropertyVetoException e1) {
e1.printStackTrace();
throw new RuntimeException("Unexpected exception setting " + MolecularType.typeName + " list: " + e1.getMessage(), e1);
}
} else {
// This should not be possible
throw new RuntimeException("Owner or SimulationContext are null.");
}
int arrowIndex = inputString.indexOf("<->");
boolean bReversible = true;
if (arrowIndex < 0) {
arrowIndex = inputString.indexOf("->");
bReversible = false;
}
String left = inputString.substring(0, arrowIndex).trim();
String right = inputString.substring(arrowIndex + (bReversible ? 3 : 2)).trim();
if (left.length() == 0 && right.length() == 0) {
return;
}
// we recover the original rule that generated the flattened reaction we now try to transform back into a fake rule
BNGReaction reactionObject = reactionTableRow.getReactionObject();
String name = reactionObject.getRuleName();
if (name.contains(GeneratedReactionTableModel.reverse)) {
name = name.substring(GeneratedReactionTableModel.reverse.length());
}
if (name.endsWith(ReactionRule.DirectHalf)) {
name = name.substring(0, name.indexOf(ReactionRule.DirectHalf));
}
if (name.endsWith(ReactionRule.InverseHalf)) {
name = name.substring(0, name.indexOf(ReactionRule.InverseHalf));
}
// get the name of the original structure from the original rule and make here another structure with the same name
String strStructure = null;
Structure ruleStructure;
SimulationContext sc = owner.getSimulationContext();
ReactionRule rr = sc.getModel().getRbmModelContainer().getReactionRule(name);
if (rr != null && rr.getStructure() != null) {
strStructure = rr.getStructure().getName();
}
if (strStructure != null) {
if (tempModel.getStructure(strStructure) == null) {
try {
if (rr.getStructure().getTypeName().equals(Structure.TYPE_NAME_MEMBRANE)) {
tempModel.addMembrane(strStructure);
} else {
tempModel.addFeature(strStructure);
}
} catch (ModelException | PropertyVetoException e) {
e.printStackTrace();
}
}
ruleStructure = tempModel.getStructure(strStructure);
} else {
throw new RuntimeException("Failed to recover a Structure name from the Reaction Rule: " + name);
}
// making the fake rules just for display purpose, actually they are the flattened reactions resulted from bngl
// the name is probably not unique, it's likely that many flattened reactions are derived from the same rule
ReactionRule reactionRule = tempModel.getRbmModelContainer().createReactionRule(name, ruleStructure, bReversible);
String regex = "[^!]\\+";
String[] patterns = left.split(regex);
for (String spString : patterns) {
try {
spString = spString.trim();
// if compartments are present, we're cheating big time making some fake compartments just for compartment name display purposes
SpeciesPattern speciesPattern = (SpeciesPattern) RbmUtils.parseSpeciesPattern(spString, tempModel);
strStructure = RbmUtils.parseCompartment(spString, tempModel);
speciesPattern.resolveBonds();
Structure structure;
if (strStructure != null) {
if (tempModel.getStructure(strStructure) == null) {
if (sc.getModel().getStructure(strStructure).getTypeName().equals(Structure.TYPE_NAME_MEMBRANE)) {
tempModel.addMembrane(strStructure);
} else {
tempModel.addFeature(strStructure);
}
}
structure = tempModel.getStructure(strStructure);
} else {
// if nothing explicit for a participant, we use by default the structure of the rule
structure = ruleStructure;
}
reactionRule.addReactant(new ReactantPattern(speciesPattern, structure));
} catch (Throwable ex) {
ex.printStackTrace();
// error (red circle)
SpeciesPatternLargeShape spls = new SpeciesPatternLargeShape(20, 20, -1, shapePanel, true, issueManager);
reactantPatternShapeList.clear();
productPatternShapeList.clear();
reactantPatternShapeList.add(spls);
shapePanel.repaint();
return;
}
}
patterns = right.split(regex);
for (String spString : patterns) {
try {
spString = spString.trim();
SpeciesPattern speciesPattern = (SpeciesPattern) RbmUtils.parseSpeciesPattern(spString, tempModel);
strStructure = RbmUtils.parseCompartment(spString, tempModel);
speciesPattern.resolveBonds();
Structure structure;
if (strStructure != null) {
if (tempModel.getStructure(strStructure) == null) {
if (sc.getModel().getStructure(strStructure).getTypeName().equals(Structure.TYPE_NAME_MEMBRANE)) {
tempModel.addMembrane(strStructure);
} else {
tempModel.addFeature(strStructure);
}
}
structure = tempModel.getStructure(strStructure);
} else {
structure = ruleStructure;
}
// BNGLParser parser = new BNGLParser(new StringReader(sp));
// ASTSpeciesPattern astSpeciesPattern = parser.SpeciesPattern();
// BnglObjectConstructionVisitor constructionVisitor = new BnglObjectConstructionVisitor(tempModel, null, false);
// SpeciesPattern speciesPattern = (SpeciesPattern) astSpeciesPattern.jjtAccept(constructionVisitor, null);
// for(MolecularTypePattern mtp : speciesPattern.getMolecularTypePatterns()) {
// mtp.setParticipantMatchLabel("*");
// }
// System.out.println(speciesPattern.toString());
reactionRule.addProduct(new ProductPattern(speciesPattern, structure));
} catch (Throwable ex) {
ex.printStackTrace();
// error (red circle)
SpeciesPatternLargeShape spls = new SpeciesPatternLargeShape(20, 20, -1, shapePanel, true, issueManager);
reactantPatternShapeList.clear();
productPatternShapeList.clear();
reactantPatternShapeList.add(spls);
shapePanel.repaint();
return;
}
}
// ----------------------------------------------------------------------------------------------------
List<ReactantPattern> rpList = reactionRule.getReactantPatterns();
reactantPatternShapeList.clear();
int xOffset = 20;
int xOffsetRound = 20;
if (rpList != null && rpList.size() > 0) {
for (int i = 0; i < rpList.size(); i++) {
SpeciesPattern sp = rpList.get(i).getSpeciesPattern();
for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
mtp.setParticipantMatchLabel("*");
}
SpeciesPatternLargeShape sps = new SpeciesPatternLargeShape(xOffset, 20, -1, sp, shapePanel, reactionRule, issueManager);
// SpeciesPatternRoundShape sps = new SpeciesPatternRoundShape(xOffsetRound, 20, -1, sp, shapePanel, reactionRule);
if (i < rpList.size() - 1) {
sps.addEndText("+");
} else {
if (reactionRule.isReversible()) {
sps.addEndText("<->");
} else {
sps.addEndText("->");
}
}
xOffset = sps.getRightEnd() + 45;
xOffsetRound = sps.getRightEnd() + 45;
reactantPatternShapeList.add(sps);
}
}
// space for the <-> sign
xOffset += 15;
List<ProductPattern> ppList = reactionRule.getProductPatterns();
productPatternShapeList.clear();
if (ppList != null && ppList.size() > 0) {
for (int i = 0; i < ppList.size(); i++) {
SpeciesPattern sp = ppList.get(i).getSpeciesPattern();
for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
mtp.setParticipantMatchLabel("*");
}
SpeciesPatternLargeShape sps = new SpeciesPatternLargeShape(xOffset, 20, -1, sp, shapePanel, reactionRule, issueManager);
// SpeciesPatternRoundShape sps = new SpeciesPatternRoundShape(xOffset, 20, -1, sp, shapePanel, reactionRule);
if (i < ppList.size() - 1) {
sps.addEndText("+");
}
xOffset = sps.getRightEnd() + 45;
productPatternShapeList.add(sps);
}
}
Dimension preferredSize = new Dimension(xOffset + 90, 50);
shapePanel.setPreferredSize(preferredSize);
shapePanel.repaint();
}
use of org.vcell.model.rbm.MolecularType in project vcell by virtualcell.
the class XmlReader method getRbmMolecularTypePattern.
private MolecularTypePattern getRbmMolecularTypePattern(Element e, Model newModel) {
RbmModelContainer mc = newModel.getRbmModelContainer();
Element e1 = e.getChild(XMLTags.RbmMolecularTypeTag, vcNamespace);
String molecularTypeName = e1.getAttributeValue(XMLTags.NameAttrTag);
MolecularType mt = mc.getMolecularType(molecularTypeName);
if (mt == null) {
System.out.println("XMLReader: getRbmMolecularTypePattern: encountered reference to non-existing MolecularType.");
return null;
}
// we insert the component patterns below
MolecularTypePattern tp = new MolecularTypePattern(mt, false);
String index = e.getAttributeValue(XMLTags.RbmIndexAttrTag);
if (index != null && !index.isEmpty()) {
tp.setIndex(Integer.parseInt(index));
}
String match = e.getAttributeValue(XMLTags.RbmParticipantPatternMatchTag);
if (match != null && !match.isEmpty()) {
tp.setParticipantMatchLabel(match);
}
List<MolecularComponentPattern> cpl = new ArrayList<MolecularComponentPattern>();
List<Element> children = e.getChildren(XMLTags.RbmMolecularComponentPatternTag, vcNamespace);
for (Element e2 : children) {
MolecularComponentPattern cp = getRbmMolecularComponentPattern(e2, tp, mt, newModel);
if (cp != null) {
cpl.add(cp);
}
}
tp.setComponentPatterns(cpl);
return tp;
}
use of org.vcell.model.rbm.MolecularType in project vcell by virtualcell.
the class Xmlproducer method getXML.
private Element getXML(MolecularTypePattern param) {
Element e = new Element(XMLTags.RbmMolecularTypePatternTag);
if (param.hasExplicitParticipantMatch()) {
e.setAttribute(XMLTags.RbmParticipantPatternMatchTag, mangle(param.getParticipantMatchLabel()));
}
e.setAttribute(XMLTags.RbmIndexAttrTag, Integer.toString(param.getIndex()));
Element e1 = new Element(XMLTags.RbmMolecularTypeTag);
MolecularType mt = param.getMolecularType();
e1.setAttribute(XMLTags.NameAttrTag, mangle(mt.getName()));
e.addContent(e1);
for (MolecularComponentPattern cp : param.getComponentPatternList()) {
e.addContent(getXML(cp));
}
return e;
}
use of org.vcell.model.rbm.MolecularType in project vcell by virtualcell.
the class XmlReader method getRbmMolecularTypeList.
private void getRbmMolecularTypeList(Element param, Model newModel) {
RbmModelContainer mc = newModel.getRbmModelContainer();
List<MolecularType> mtl = mc.getMolecularTypeList();
List<Element> children = param.getChildren(XMLTags.RbmMolecularTypeTag, vcNamespace);
for (Element element : children) {
MolecularType t = getRbmMolecularType(element, newModel);
if (t != null) {
mtl.add(t);
}
}
}
use of org.vcell.model.rbm.MolecularType in project vcell by virtualcell.
the class Xmlproducer method getXML.
// ============================================================================================================
public Element getXML(RbmModelContainer rbmModelContainer) {
Element rbmModelContainerElement = new Element(XMLTags.RbmModelContainerTag);
List<MolecularType> molecularTypeList = rbmModelContainer.getMolecularTypeList();
if (!molecularTypeList.isEmpty()) {
Element molecularTypeListElement = new Element(XMLTags.RbmMolecularTypeListTag);
for (MolecularType mt : molecularTypeList) {
molecularTypeListElement.addContent(getXML(mt));
}
rbmModelContainerElement.addContent(molecularTypeListElement);
}
List<RbmObservable> observablesList = rbmModelContainer.getObservableList();
if (!observablesList.isEmpty()) {
Element observablesListElement = new Element(XMLTags.RbmObservableListTag);
for (RbmObservable oo : observablesList) {
observablesListElement.addContent(getXML(oo));
}
rbmModelContainerElement.addContent(observablesListElement);
}
List<ReactionRule> reactionList = rbmModelContainer.getReactionRuleList();
if (!reactionList.isEmpty()) {
Element reactionListElement = new Element(XMLTags.RbmReactionRuleListTag);
for (ReactionRule rr : reactionList) {
reactionListElement.addContent(getXML(rr));
}
rbmModelContainerElement.addContent(reactionListElement);
}
return rbmModelContainerElement;
}
Aggregations