use of cbit.vcell.model.ReactionRule in project vcell by virtualcell.
the class BioCartoonTool method pasteReactionsAndRules.
public static final void pasteReactionsAndRules(Component requester, ReactionSpeciesCopy rsCopy, Model pasteModel, Structure structTo, RXPasteInterface rxPasteInterface) {
PasteHelper[] pasteHelper = new PasteHelper[1];
AsynchClientTask issueTask = new AsynchClientTask("Checking Issues...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
Model clonedModel = (Model) org.vcell.util.BeanUtils.cloneSerializable(pasteModel);
clonedModel.refreshDependencies();
// Model clonedModel = pasteModel;
IssueContext issueContext = new IssueContext(ContextType.Model, clonedModel, null);
Vector<Issue> issues = new Vector<>();
checkStructuresCompatibility(rsCopy, clonedModel, structTo, issues, issueContext);
Set<MolecularType> mtFromListStrict = getMoleculesFromStrict(rsCopy, clonedModel, issues, issueContext);
if (issues.size() != 0) {
// at this point we can only have fatal error issues or no issues at all
if (!printIssues(issues, requester)) {
throw UserCancelException.CANCEL_GENERIC;
}
}
// map all the structures of the reactions, rules and their participants to existing structures, at need make new structures
// key is the "from" structure, value is the name of the equivalent "to" structure
// on the first position we have the struct from where we copy (key) and the struct where we paste (value)
Map<Structure, String> structuresMap;
if (rsCopy.getStructuresArr().length > 1) {
// throws CANCEL_GENERIC if the user cancels
structuresMap = mapStructures(requester, rsCopy, clonedModel, structTo, issueContext);
} else {
// if length == 1 it's just structFrom which automatically maps to structTo
structuresMap = new LinkedHashMap<>();
structuresMap.put(rsCopy.getFromStructure(), structTo.getName());
}
pasteMolecules(mtFromListStrict, clonedModel, structuresMap);
List<ReactionRule> rulesTo = pasteRules(rsCopy, clonedModel, structTo, issues, issueContext, structuresMap);
// for(ReactionRule rr : rulesTo) {
// clonedModel.getRbmModelContainer().addReactionRule(rr);
// }
clonedModel.getRbmModelContainer().addReactionRules(rulesTo);
// TODO: make any final verifications in the cloned model here
// if anything is wrong exit here with some helpful message
// .....
// otherwise go directly to populating the real model
mapStructures(structuresMap, pasteModel);
pasteMolecules(mtFromListStrict, pasteModel, structuresMap);
// we repeat all the steps to paste the rules in the real model instead of the clone
rulesTo = pasteRules(rsCopy, pasteModel, structTo, issues, issueContext, structuresMap);
// for(ReactionRule rr : rulesTo) {
// pasteModel.getRbmModelContainer().addReactionRule(rr);
// }
pasteModel.getRbmModelContainer().addReactionRules(rulesTo);
System.out.println("done");
}
};
AsynchClientTask pasteRXTask = new AsynchClientTask("Pasting Reaction...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
IssueContext issueContext = new IssueContext(ContextType.Model, pasteModel, null);
// if (pasteHelper[0].issues.size() != 0) {
// printIssues(pasteHelper[0].issues, requester);
// }
// if(rxPasteInterface != null){
// for(BioModelEntityObject newBioModelEntityObject:pasteHelper[0].reactionsAndSpeciesContexts.keySet()) {
// ReactionCartoonTool.copyRelativePosition(rxPasteInterface.getGraphPane().getGraphModel(), pasteHelper[0].reactionsAndSpeciesContexts.get(newBioModelEntityObject), newBioModelEntityObject);
// }
// ReactionCartoonTool.selectAndSaveDiagram(rxPasteInterface, new ArrayList<BioModelEntityObject>(pasteHelper[0].reactionsAndSpeciesContexts.keySet()));
// // //Setup to allow dispatcher to set focus on a specified component after it closes the ProgressPopup
// setFinalWindow(hashTable, rxPasteInterface.getGraphPane());
// }
}
};
ClientTaskDispatcher.dispatch(requester, new Hashtable<>(), new AsynchClientTask[] { issueTask, pasteRXTask }, false);
}
use of cbit.vcell.model.ReactionRule in project vcell by virtualcell.
the class ReactionCartoonTool method menuAction.
@Override
protected void menuAction(Shape shape, String menuAction) {
if (shape == null) {
return;
}
if (menuAction.equals(CartoonToolMiscActions.Properties.MENU_ACTION)) {
if (shape instanceof FluxReactionShape) {
// showFluxReactionPropertiesDialog((FluxReactionShape) shape);
} else if (shape instanceof SimpleReactionShape) {
// showSimpleReactionPropertiesDialog((SimpleReactionShape) shape);
} else if (shape instanceof ReactantShape) {
// Point locationOnScreen = shape.getSpaceManager().getAbsLoc();
// Point graphPaneLocation = getGraphPane().getLocationOnScreen();
// locationOnScreen.translate(graphPaneLocation.x,
// graphPaneLocation.y);
// showReactantPropertiesDialog((ReactantShape) shape,
// locationOnScreen);
} else if (shape instanceof ProductShape) {
// Point locationOnScreen = shape.getSpaceManager().getAbsLoc();
// Point graphPaneLocation = getGraphPane().getLocationOnScreen();
// locationOnScreen.translate(graphPaneLocation.x,
// graphPaneLocation.y);
// showProductPropertiesDialog((ProductShape) shape,
// locationOnScreen);
} else if (shape instanceof SpeciesContextShape) {
// showEditSpeciesDialog(getGraphPane(), getReactionCartoon()
// .getModel(), ((SpeciesContextShape) shape)
// .getSpeciesContext());
} else if (shape instanceof ReactionContainerShape) {
// ReactionContainerShape rcs = (ReactionContainerShape) shape;
// if (rcs.getStructure() instanceof Feature) {
// //
// // showFeaturePropertyDialog is invoked in two modes:
// //
// // 1) parent!=null and child==null
// // upon ok, it adds a new feature to the supplied parent.
// //
// // 2) parent==null and child!=null
// // upon ok, edits the feature name
// //
// showFeaturePropertiesDialog(getGraphPane(),
// (getReactionCartoon().getModel() == null ? null
// : getReactionCartoon().getModel()), null,
// (Feature) rcs.getStructure());
// } else if (rcs.getStructure() instanceof Membrane) {
// showMembranePropertiesDialog(getGraphPane(), (Membrane) rcs
// .getStructure());
// }
}
} else if (menuAction.equals(CartoonToolMiscActions.AddSpecies.MENU_ACTION)) {
if (shape instanceof ReactionContainerShape) {
getGraphModel().deselectShape(shape);
// showCreateSpeciesContextDialog(getGraphPane(),
// getReactionCartoon().getModel(),
// ((ReactionContainerShape) shape).getStructure(), null);
SpeciesContext speciesContext = getReactionCartoon().getModel().createSpeciesContext(((ReactionContainerShape) shape).getStructure());
getGraphModel().select(speciesContext);
}
} else if (menuAction.equals(CartoonToolEditActions.Copy.MENU_ACTION)) {
if (shape instanceof SpeciesContextShape || shape instanceof ReactionStepShape || // rule participants whose rule is not selected won't
shape instanceof RuleParticipantSignatureDiagramShape || // be copied since standalone they are meaningless
shape instanceof ReactionRuleDiagramShape) {
SpeciesContext[] spArray = getSelectedSpeciesContextArray();
ReactionStep[] rsArray = getSelectedReactionStepArray();
ReactionRule[] rrArray = getSelectedReactionRuleArray();
MolecularType[] mtArray = getSelectedMolecularTypeArray(rrArray, rsArray, spArray);
Structure[] structArray = getSelectedStructuresArray(rrArray, rsArray, spArray, mtArray);
Structure fromStruct = null;
ReactionContainerShape rcs = null;
Shape parentShape = shape.getParent();
if (parentShape instanceof ReactionContainerShape) {
rcs = (ReactionContainerShape) parentShape;
fromStruct = rcs.getStructure();
}
ReactionSpeciesCopy reactionSpeciesCopy = new ReactionSpeciesCopy(spArray, rsArray, rrArray, mtArray, fromStruct, structArray);
VCellTransferable.sendToClipboard(reactionSpeciesCopy);
}
} else if (/*menuAction.equals(CartoonToolEditActions.Paste.MENU_ACTION)
|| */
menuAction.equals(CartoonToolEditActions.PasteNew.MENU_ACTION)) {
if (shape instanceof ReactionContainerShape) {
pasteReactionsAndSpecies(getGraphPane().getRootPane(), ((ReactionContainerShape) shape).getStructure());
}
} else if (menuAction.equals(CartoonToolEditActions.Delete.MENU_ACTION)) {
try {
if (getGraphModel().getSelectedShape() instanceof ReactionContainerShape && menuAction.equals(CartoonToolEditActions.Delete.MENU_ACTION)) {
getModel().removeStructure(((ReactionContainerShape) getGraphModel().getSelectedShape()).getStructure());
return;
}
if (getSelectedReactionStepArray() != null || getSelectedSpeciesContextArray() != null) {
deleteReactionsAndSpecies(getGraphPane(), getSelectedReactionStepArray(), getSelectedSpeciesContextArray());
}
if (getSelectedReactionParticipantArray() != null && menuAction.equals(CartoonToolEditActions.Delete.MENU_ACTION)) {
ReactionParticipant[] reactionParticipantArr = getSelectedReactionParticipantArray();
if (ReactionCartoonTool.checkAllCatalyst(reactionParticipantArr)) {
DialogUtils.showWarningDialog(getGraphPane(), "Catalysts are determined automatically from the reaction kinetic expression.");
return;
}
String response = DialogUtils.showWarningDialog(getGraphPane(), "Delete " + reactionParticipantArr.length + " Reaction Stoichiometries", new String[] { RXSPECIES_DELETE, RXSPECIES_CANCEL }, RXSPECIES_CANCEL);
if (response != null && response.equals(RXSPECIES_DELETE)) {
for (int i = 0; i < reactionParticipantArr.length; i++) {
if (reactionParticipantArr[i] instanceof Catalyst) {
// Catalysts may only be deleted by editing kynetic / proxy parameters
continue;
}
ReactionStep reactionStep = reactionParticipantArr[i].getReactionStep();
reactionStep.removeReactionParticipant(reactionParticipantArr[i]);
}
}
}
} catch (UserCancelException uce) {
return;
} catch (PropertyVetoException e) {
DialogUtils.showErrorDialog(getGraphPane(), e.getMessage());
} catch (Exception e) {
DialogUtils.showErrorDialog(getGraphPane(), e.getMessage(), e);
}
} else if (menuAction.equals(CartoonToolMiscActions.SearchReactions.MENU_ACTION)) {
try {
if (shape instanceof ReactionContainerShape) {
showReactionBrowserDialog(((ReactionContainerShape) shape).getStructure(), null);
}
} catch (Exception e) {
DialogUtils.showErrorDialog(getGraphPane(), e.getMessage(), e);
}
} else if (menuAction.equals(CartoonToolSaveAsImageActions.MenuAction.MENU_ACTION)) {
try {
String resType = null;
if (shape instanceof ReactionContainerShape) {
showSaveReactionImageDialog();
}
} catch (Exception e) {
e.printStackTrace();
DialogUtils.showErrorDialog(getGraphPane(), e.getMessage(), e);
}
} else if (menuAction.equals(CartoonToolMiscActions.Annotate.MENU_ACTION)) {
if (shape instanceof ReactionStepShape) {
// MIRIAMHelper.showMIRIAMAnnotationDialog(((SimpleReactionShape)shape).getReactionStep());
// System.out.println("Menu action annotate activated...");
ReactionStep rs = ((ReactionStepShape) shape).getReactionStep();
VCMetaData vcMetaData = rs.getModel().getVcMetaData();
try {
String newAnnotation = DialogUtils.showAnnotationDialog(getGraphPane(), vcMetaData.getFreeTextAnnotation(rs));
vcMetaData.setFreeTextAnnotation(rs, newAnnotation);
} catch (UtilCancelException e) {
// Do Nothing
} catch (Throwable exc) {
exc.printStackTrace(System.out);
DialogUtils.showErrorDialog(getGraphPane(), "Failed to edit annotation!\n" + exc.getMessage(), exc);
}
}
} else {
// default action is to ignore
}
}
use of cbit.vcell.model.ReactionRule in project vcell by virtualcell.
the class ParticipantSignatureShapePanel method refreshRuleAnalysis.
private void refreshRuleAnalysis() {
reportMap.clear();
modelRuleEntryMap.clear();
bRuleAnalysisFailedMap.clear();
if (reactionRuleMap == null || reactionRuleMap.isEmpty()) {
return;
}
for (Map.Entry<String, ReactionRule> entry : reactionRuleMap.entrySet()) {
String key = entry.getKey();
ReactionRule rr = entry.getValue();
ModelRuleFactory factory = new ModelRuleFactory();
ModelRuleEntry modelRuleEntry = factory.createRuleEntry(rr, 0, ReactionRuleDirection.forward);
try {
RuleAnalysisReport report = RuleAnalysis.analyze(modelRuleEntry, false);
reportMap.put(key, report);
modelRuleEntryMap.put(key, modelRuleEntry);
bRuleAnalysisFailedMap.put(key, false);
} catch (Exception e) {
e.printStackTrace();
System.err.println("exception computing RuleAnalysis report for " + key + ": " + e.getMessage());
bRuleAnalysisFailedMap.put(key, true);
bRuleAnalysisFailed = true;
}
}
}
use of cbit.vcell.model.ReactionRule 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 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();
}
Aggregations