use of cbit.vcell.model.ReactionParticipant in project vcell by virtualcell.
the class ReactionCartoonTool method detailsDeleteSpecies.
private static DeleteSpeciesInfo detailsDeleteSpecies(Component requester, SpeciesContext[] speciesContextArr, ReactionStep[] toBeDeletedReactStepArr, ReactionCartoon rxCartoon) throws Exception, UserCancelException {
if (speciesContextArr == null || speciesContextArr.length == 0) {
return null;
}
// Warn user that there may be some BioModel components that reference speciesContext to be removed
// Get ReactionParticipant list
Collection<Shape> rxPartColl = rxCartoon.getShapes();
HashMap<SpeciesContext, HashSet<ReactionParticipant>> rxPartHashMap = new HashMap<SpeciesContext, HashSet<ReactionParticipant>>();
for (Shape objShape : rxPartColl) {
if (objShape instanceof ReactionParticipantShape) {
ReactionParticipant objReactionParticipant = ((ReactionParticipantShape) objShape).getReactionParticipant();
if (Arrays.asList(speciesContextArr).contains(objReactionParticipant.getSpeciesContext())) {
if (!rxPartHashMap.containsKey(objReactionParticipant.getSpeciesContext())) {
rxPartHashMap.put(objReactionParticipant.getSpeciesContext(), new HashSet<ReactionParticipant>());
}
if (!rxPartHashMap.get(objReactionParticipant.getSpeciesContext()).contains(objReactionParticipant)) {
rxPartHashMap.get(objReactionParticipant.getSpeciesContext()).add(objReactionParticipant);
}
}
}
}
int reactionParticipantCount = 0;
for (HashSet<ReactionParticipant> objReactPart : rxPartHashMap.values()) {
reactionParticipantCount += objReactPart.size();
}
// find bioModel and get SymbolTable references to SpeciesContext
BioModelEditor bioModelEditor = (BioModelEditor) BeanUtils.findTypeParentOfComponent(requester, BioModelEditor.class);
if (bioModelEditor == null) {
throw new Exception("Error deleting Speciescontext, Can't find BiomodelEditor");
}
BioModel bioModel = bioModelEditor.getBioModelWindowManager().getVCDocument();
HashMap<SpeciesContext, HashSet<SymbolTableEntry>> referencingSymbolsHashMap = new HashMap<SpeciesContext, HashSet<SymbolTableEntry>>();
for (int i = 0; i < speciesContextArr.length; i++) {
List<SymbolTableEntry> referencingSymbolsList = bioModel.findReferences(speciesContextArr[i]);
if (referencingSymbolsList != null && referencingSymbolsList.size() > 0) {
if (!referencingSymbolsHashMap.containsKey(speciesContextArr[i])) {
referencingSymbolsHashMap.put(speciesContextArr[i], new HashSet<SymbolTableEntry>());
}
referencingSymbolsHashMap.get(speciesContextArr[i]).addAll(referencingSymbolsList);
}
}
int referencingSymbolsCount = 0;
for (HashSet<SymbolTableEntry> objSimTableEntry : referencingSymbolsHashMap.values()) {
referencingSymbolsCount += objSimTableEntry.size();
}
// Warn user about delete
HashMap<SpeciesContext, Boolean> bUnresolvableHashMap = new HashMap<SpeciesContext, Boolean>();
for (int i = 0; i < speciesContextArr.length; i++) {
bUnresolvableHashMap.put(speciesContextArr[i], Boolean.FALSE);
}
String[][] rowData = null;
if (rxPartHashMap.size() == 0 && referencingSymbolsHashMap.size() == 0) {
rowData = new String[speciesContextArr.length][4];
for (int i = 0; i < speciesContextArr.length; i++) {
rowData[i][0] = speciesContextArr[i].getName();
rowData[i][1] = "";
rowData[i][2] = "";
rowData[i][3] = "";
}
Arrays.sort(rowData, new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
return o1[0].compareToIgnoreCase(o2[0]);
}
});
} else {
// find SpeciesContext that had no reference warnings
Vector<SpeciesContext> speciesContextNoReferences = new Vector<SpeciesContext>();
for (int i = 0; i < speciesContextArr.length; i++) {
if (!rxPartHashMap.containsKey(speciesContextArr[i]) && !referencingSymbolsHashMap.containsKey(speciesContextArr[i])) {
speciesContextNoReferences.add(speciesContextArr[i]);
}
}
rowData = new String[reactionParticipantCount + referencingSymbolsCount + speciesContextNoReferences.size()][4];
int count = 0;
for (SpeciesContext objSpeciesContext : speciesContextNoReferences) {
rowData[count][0] = objSpeciesContext.getName();
rowData[count][1] = "";
rowData[count][2] = "";
rowData[count][3] = "";
count++;
}
for (SpeciesContext objSpeciesContext : rxPartHashMap.keySet()) {
Iterator<ReactionParticipant> iterRxPart = rxPartHashMap.get(objSpeciesContext).iterator();
while (iterRxPart.hasNext()) {
rowData[count][0] = objSpeciesContext.getName();
rowData[count][1] = "";
rowData[count][2] = "Reaction Diagram stoichiometry '" + iterRxPart.next().getReactionStep().getName() + "'";
rowData[count][3] = "";
count++;
}
}
for (SpeciesContext objSpeciesContext : referencingSymbolsHashMap.keySet()) {
Iterator<SymbolTableEntry> iterSymbolTableEntry = referencingSymbolsHashMap.get(objSpeciesContext).iterator();
while (iterSymbolTableEntry.hasNext()) {
rowData[count][0] = objSpeciesContext.getName();
rowData[count][1] = "";
SymbolTableEntry objSymbolTableEntry = iterSymbolTableEntry.next();
boolean bKineticsParameter = objSymbolTableEntry instanceof KineticsParameter;
if (bKineticsParameter) {
KineticsParameter kp = (KineticsParameter) objSymbolTableEntry;
boolean isOK = kp.isRegenerated();
for (int i = 0; toBeDeletedReactStepArr != null && i < toBeDeletedReactStepArr.length; i++) {
if (toBeDeletedReactStepArr[i] == kp.getKinetics().getReactionStep()) {
// OK to delete this Speciescontext if were deleting the reaction that contained the reference
isOK = true;
}
}
rowData[count][1] = (isOK ? "" : RXSPECIES_ERROR);
bUnresolvableHashMap.put(objSpeciesContext, bUnresolvableHashMap.get(objSpeciesContext) || !isOK);
}
boolean bReaction = objSymbolTableEntry.getNameScope() instanceof ReactionNameScope;
rowData[count][2] = (bReaction ? "Reaction" : objSymbolTableEntry.getNameScope().getClass().getName()) + "( " + objSymbolTableEntry.getNameScope().getName() + " )";
rowData[count][3] = (bKineticsParameter ? "Parameter" : objSymbolTableEntry.getClass().getName()) + "( " + objSymbolTableEntry.getName() + " )";
count++;
}
}
// for (SymbolTableEntry referencingSTE : referencingSymbols) {
// System.out.println("REFERENCE "+referencingSTE.getClass().getName()+"("+referencingSTE.getName()+") nameScope "+referencingSTE.getNameScope().getClass().getName()+"("+referencingSTE.getNameScope().getName()+")");
// }
Arrays.sort(rowData, new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
return o1[0].compareToIgnoreCase(o2[0]);
}
});
}
if (rowData == null || rowData.length == 0) {
return null;
}
return new DeleteSpeciesInfo(rxPartHashMap, bUnresolvableHashMap, rowData);
}
use of cbit.vcell.model.ReactionParticipant in project vcell by virtualcell.
the class BioCartoonTool method askUserResolveMembraneConnections.
private static HashMap<ReactionParticipant, Structure> askUserResolveMembraneConnections(Component requester, Structure[] allStructures, Structure currentStruct, Structure fromRxnStruct, Structure toRxnStruct, ReactionParticipant[] copyFromRxParticipantArr, StructureTopology toStructureTopology, StructureTopology structTopology) {
HashMap<ReactionParticipant, Structure> userMap = new HashMap<>();
boolean bMembrane = toRxnStruct instanceof Membrane;
if (!bMembrane) {
for (int i = 0; i < copyFromRxParticipantArr.length; i += 1) {
userMap.put(copyFromRxParticipantArr[i], null);
for (Structure struct : allStructures) {
if (fromRxnStruct.getName().equals(copyFromRxParticipantArr[i].getStructure().getName())) {
userMap.put(copyFromRxParticipantArr[i], toRxnStruct);
}
}
}
// return userMap;
} else {
for (int i = 0; i < copyFromRxParticipantArr.length; i += 1) {
Structure pasteToStruct = currentStruct;
Membrane oldMembr = (Membrane) fromRxnStruct;
pasteToStruct = matchMembraneAdjacentStructure(allStructures, currentStruct, copyFromRxParticipantArr[i].getStructure(), structTopology, toStructureTopology, oldMembr, pasteToStruct);
userMap.put(copyFromRxParticipantArr[i], pasteToStruct);
}
}
JScrollPane jScrollPane = null;
JPanel rxMapperPanel = null;
Hashtable<JLabel, ReactionParticipant> mapLabelToPart = null;
boolean bUnselected;
do {
bUnselected = false;
if (jScrollPane == null) {
rxMapperPanel = new JPanel();
rxMapperPanel.setLayout(new BoxLayout(rxMapperPanel, BoxLayout.Y_AXIS));
// ((BoxLayout)rxMapperPanel.getLayout())
int height = 0;
final int[] widthlabels = new int[] { 0 };
int widthcombo = 0;
mapLabelToPart = new Hashtable<>();
for (ReactionParticipant rxPart : userMap.keySet()) {
if (!bMembrane || (rxPart.getStructure() instanceof Feature)) {
JPanel row = new JPanel();
JLabel rxpartLabel = new JLabel(rxPart.getName());
row.add(rxpartLabel);
mapLabelToPart.put(rxpartLabel, rxPart);
JComboBox<Structure> structJC = new JComboBox<>();
structJC.setRenderer(new ListCellRenderer<Structure>() {
@Override
public Component getListCellRendererComponent(JList<? extends Structure> list, Structure value, int index, boolean isSelected, boolean cellHasFocus) {
// TODO Auto-generated method stub
JLabel label = new JLabel(value.getName());
widthlabels[0] = Math.max(widthlabels[0], label.getPreferredSize().width);
return label;
}
});
height += structJC.getPreferredSize().getHeight();
widthcombo = Math.max(widthcombo, structJC.getPreferredSize().width);
try {
Feature dummyFeature = new Feature(DUMMY_CHOOSE);
structJC.addItem(dummyFeature);
} catch (Exception e) {
e.printStackTrace();
}
for (Structure struct : allStructures) {
if (!bMembrane || (struct instanceof Feature)) {
structJC.addItem(struct);
}
}
structJC.setSelectedItem((userMap.get(rxPart) == null ? 0 : userMap.get(rxPart)));
row.add(structJC);
rxMapperPanel.add(row);
}
}
height += 25;
rxMapperPanel.setSize(widthcombo + widthlabels[0], height);
rxMapperPanel.setPreferredSize(new Dimension(widthcombo + widthlabels[0], height));
jScrollPane = new JScrollPane(rxMapperPanel);
jScrollPane.setPreferredSize(new Dimension(150, 100));
}
if (rxMapperPanel.getComponentCount() != 0) {
int result = DialogUtils.showComponentOKCancelDialog(requester, jScrollPane, "Assign Compartments for RX '" + copyFromRxParticipantArr[0].getReactionStep().getName() + "' Participants");
if (result != JOptionPane.OK_OPTION) {
throw UserCancelException.CANCEL_GENERIC;
}
for (int i = 0; i < rxMapperPanel.getComponentCount(); i++) {
JLabel label0 = (JLabel) (((Container) rxMapperPanel.getComponent(i)).getComponent(0));
JComboBox<Structure> struct0 = (JComboBox<Structure>) (((Container) rxMapperPanel.getComponent(i)).getComponent(1));
if (((Structure) struct0.getSelectedItem()).getName().equals(DUMMY_CHOOSE)) {
bUnselected = true;
DialogUtils.showWarningDialog(requester, "Choose a valid compartment for each ReactionParticipant");
break;
}
userMap.put(mapLabelToPart.get(label0), (Structure) struct0.getSelectedItem());
}
}
} while (bUnselected);
return userMap;
}
use of cbit.vcell.model.ReactionParticipant in project vcell by virtualcell.
the class BioCartoonTool method pasteReactionSteps0.
/**
* pasteReactionSteps0 : does the actual pasting. First called with a cloned model, to track issues. If user still wants to proceed, the paste
* is performed on the original model.
*
* Insert the method's description here.
* Creation date: (5/10/2003 3:55:25 PM)
* @param pasteToModel cbit.vcell.model.Model
* @param pasteToStructure cbit.vcell.model.Structure
* @param bNew boolean
*/
private static final PasteHelper pasteReactionSteps0(HashMap<String, HashMap<ReactionParticipant, Structure>> rxPartMapStructure, Component parent, IssueContext issueContext, ReactionStep[] copyFromRxSteps, Model pasteToModel, Structure pasteToStructure, boolean bNew, /*boolean bUseDBSpecies,*/
UserResolvedRxElements userResolvedRxElements) throws Exception {
HashMap<BioModelEntityObject, BioModelEntityObject> reactionsAndSpeciesContexts = new HashMap<>();
if (copyFromRxSteps == null || copyFromRxSteps.length == 0 || pasteToModel == null || pasteToStructure == null) {
throw new IllegalArgumentException("CartoonTool.pasteReactionSteps Error " + (copyFromRxSteps == null || copyFromRxSteps.length == 0 ? "reactionStepsArr empty " : "") + (pasteToModel == null ? "model is null " : "") + (pasteToStructure == null ? "struct is null " : ""));
}
if (!pasteToModel.contains(pasteToStructure)) {
throw new IllegalArgumentException("CartoonTool.pasteReactionSteps model " + pasteToModel.getName() + " does not contain structure " + pasteToStructure.getName());
}
// Check PasteToModel has preferred targets if set
if (userResolvedRxElements != null) {
for (int i = 0; i < userResolvedRxElements.toSpeciesArr.length; i++) {
if (userResolvedRxElements.toSpeciesArr[i] != null) {
if (!pasteToModel.contains(userResolvedRxElements.toSpeciesArr[i])) {
throw new RuntimeException("PasteToModel does not contain preferred Species " + userResolvedRxElements.toSpeciesArr[i]);
}
}
if (userResolvedRxElements.toStructureArr[i] != null) {
if (!pasteToModel.contains(userResolvedRxElements.toStructureArr[i])) {
throw new RuntimeException("PasteToModel does not contain preferred Structure " + userResolvedRxElements.toStructureArr[i]);
}
}
}
}
int counter = 0;
Structure currentStruct = pasteToStructure;
String copiedStructName = copyFromRxSteps[counter].getStructure().getName();
StructureTopology structTopology = (copyFromRxSteps[counter].getModel() == null ? pasteToModel.getStructureTopology() : copyFromRxSteps[counter].getModel().getStructureTopology());
IdentityHashMap<Species, Species> speciesHash = new IdentityHashMap<Species, Species>();
IdentityHashMap<SpeciesContext, SpeciesContext> speciesContextHash = new IdentityHashMap<SpeciesContext, SpeciesContext>();
Vector<Issue> issueVector = new Vector<Issue>();
do {
// create a new reaction, instead of cloning the old one; set struc
ReactionStep copyFromReactionStep = copyFromRxSteps[counter];
String newName = copyFromReactionStep.getName();
while (pasteToModel.getReactionStep(newName) != null) {
newName = org.vcell.util.TokenMangler.getNextEnumeratedToken(newName);
}
ReactionStep newReactionStep = null;
if (copyFromReactionStep instanceof SimpleReaction) {
newReactionStep = new SimpleReaction(pasteToModel, currentStruct, newName, copyFromReactionStep.isReversible());
} else if (copyFromReactionStep instanceof FluxReaction && currentStruct instanceof Membrane) {
newReactionStep = new FluxReaction(pasteToModel, (Membrane) currentStruct, null, newName, copyFromReactionStep.isReversible());
}
pasteToModel.addReactionStep(newReactionStep);
reactionsAndSpeciesContexts.put(newReactionStep, copyFromReactionStep);
Structure toRxnStruct = newReactionStep.getStructure();
Structure fromRxnStruct = copyFromReactionStep.getStructure();
if (!fromRxnStruct.getClass().equals(pasteToStructure.getClass())) {
throw new Exception("Cannot copy reaction from " + fromRxnStruct.getTypeName() + " to " + pasteToStructure.getTypeName() + ".");
}
// add appropriate reactionParticipants to newReactionStep.
StructureTopology toStructureTopology = pasteToModel.getStructureTopology();
ReactionParticipant[] copyFromRxParticipantArr = copyFromReactionStep.getReactionParticipants();
if (rxPartMapStructure == null) {
// null during 'issues' trial
rxPartMapStructure = new HashMap<String, HashMap<ReactionParticipant, Structure>>();
}
if (rxPartMapStructure.get(copyFromReactionStep.getName()) == null) {
// Ask user to assign species to compartments for each reaction to be pasted
rxPartMapStructure.put(copyFromReactionStep.getName(), askUserResolveMembraneConnections(parent, pasteToModel.getStructures(), currentStruct, fromRxnStruct, toRxnStruct, copyFromRxParticipantArr, toStructureTopology, structTopology));
}
for (int i = 0; i < copyFromRxParticipantArr.length; i += 1) {
Structure pasteToStruct = currentStruct;
// if(toRxnStruct instanceof Membrane){
pasteToStruct = rxPartMapStructure.get(copyFromReactionStep.getName()).get(copyFromRxParticipantArr[i]);
// if(pasteToStruct == null){
// for(ReactionParticipant myRXPart:rxPartMapStructure.get(copyFromReactionStep.getName()).keySet()){
// if(myRXPart.getSpeciesContext().getName().equals(copyFromRxParticipantArr[i].getSpeciesContext().getName())){
// pasteToStruct = rxPartMapStructure.get(copyFromReactionStep.getName()).get(myRXPart);
// break;
// }
// }
// }
// }
// this adds the speciesContexts and species (if any) to the model)
String rootSC = ReactionCartoonTool.speciesContextRootFinder(copyFromRxParticipantArr[i].getSpeciesContext());
SpeciesContext newSc = null;
SpeciesContext[] matchSC = pasteToModel.getSpeciesContexts();
for (int j = 0; matchSC != null && j < matchSC.length; j++) {
String matchRoot = ReactionCartoonTool.speciesContextRootFinder(matchSC[j]);
if (matchRoot != null && matchRoot.equals(rootSC) && matchSC[j].getStructure().getName().equals(pasteToStruct.getName())) {
newSc = matchSC[j];
reactionsAndSpeciesContexts.put(newSc, matchSC[j]);
break;
}
}
if (newSc == null) {
newSc = pasteSpecies(parent, copyFromRxParticipantArr[i].getSpecies(), rootSC, pasteToModel, pasteToStruct, bNew, /*bUseDBSpecies,*/
speciesHash, UserResolvedRxElements.getPreferredReactionElement(userResolvedRxElements, copyFromRxParticipantArr[i]));
reactionsAndSpeciesContexts.put(newSc, copyFromRxParticipantArr[i].getSpeciesContext());
}
// record the old-new speciesContexts (reactionparticipants) in the IdHashMap, this is useful, esp for 'Paste new', while replacing proxyparams.
SpeciesContext oldSc = copyFromRxParticipantArr[i].getSpeciesContext();
if (speciesContextHash.get(oldSc) == null) {
speciesContextHash.put(oldSc, newSc);
}
if (copyFromRxParticipantArr[i] instanceof Reactant) {
newReactionStep.addReactionParticipant(new Reactant(null, newReactionStep, newSc, copyFromRxParticipantArr[i].getStoichiometry()));
} else if (copyFromRxParticipantArr[i] instanceof Product) {
newReactionStep.addReactionParticipant(new Product(null, newReactionStep, newSc, copyFromRxParticipantArr[i].getStoichiometry()));
} else if (copyFromRxParticipantArr[i] instanceof Catalyst) {
newReactionStep.addCatalyst(newSc);
}
}
// // If 'newReactionStep' is a fluxRxn, set its fluxCarrier
// if (newReactionStep instanceof FluxReaction) {
// if (fluxCarrierSp != null) {
// ((FluxReaction)newReactionStep).setFluxCarrier(fluxCarrierSp, pasteToModel);
// } else {
// throw new RuntimeException("Could not set FluxCarrier species for the flux reaction to be pasted");
// }
// }
// For each kinetic parameter expression for new kinetics, replace the proxyParams from old kinetics with proxyParams in new kinetics
// i.e., if the proxyParams are speciesContexts, replace with corresponding speciesContext in newReactionStep;
// if the proxyParams are structureSizes or MembraneVoltages, replace with corresponding structure quantity in newReactionStep
Kinetics oldKinetics = copyFromReactionStep.getKinetics();
KineticsParameter[] oldKps = oldKinetics.getKineticsParameters();
KineticsProxyParameter[] oldKprps = oldKinetics.getProxyParameters();
Hashtable<String, Expression> paramExprHash = new Hashtable<String, Expression>();
for (int i = 0; oldKps != null && i < oldKps.length; i++) {
Expression newExpression = new Expression(oldKps[i].getExpression());
for (int j = 0; oldKprps != null && j < oldKprps.length; j++) {
// check if kinetic proxy parameter is in kinetic parameter expression
if (newExpression.hasSymbol(oldKprps[j].getName())) {
SymbolTableEntry ste = oldKprps[j].getTarget();
Model pasteFromModel = copyFromReactionStep.getModel();
if (ste instanceof SpeciesContext) {
// if newRxnStruct is a feature/membrane, get matching spContexts from old reaction and replace them in new rate expr.
SpeciesContext oldSC = (SpeciesContext) ste;
SpeciesContext newSC = speciesContextHash.get(oldSC);
if (newSC == null) {
// check if oldSc is present in paste-model; if not, add it.
if (!pasteToModel.equals(pasteFromModel)) {
if (pasteToModel.getSpeciesContext(oldSC.getName()) == null) {
// if paste-model has oldSc struct, paste it there,
Structure newSCStruct = pasteToModel.getStructure(oldSC.getStructure().getName());
if (newSCStruct != null) {
newSC = pasteSpecies(parent, oldSC.getSpecies(), null, pasteToModel, newSCStruct, bNew, /*bUseDBSpecies,*/
speciesHash, UserResolvedRxElements.getPreferredReactionElement(userResolvedRxElements, oldSC));
speciesContextHash.put(oldSC, newSC);
} else {
// oldStruct wasn't found in paste-model, paste it in newRxnStruct and add warning to issues list
newSC = pasteSpecies(parent, oldSC.getSpecies(), null, pasteToModel, toRxnStruct, bNew, /*bUseDBSpecies,*/
speciesHash, UserResolvedRxElements.getPreferredReactionElement(userResolvedRxElements, oldSC));
speciesContextHash.put(oldSC, newSC);
Issue issue = new Issue(oldSC, issueContext, IssueCategory.CopyPaste, "SpeciesContext '" + oldSC.getSpecies().getCommonName() + "' was not found in compartment '" + oldSC.getStructure().getName() + "' in the model; the species was added to the compartment '" + toRxnStruct.getName() + "' where the reaction was pasted.", Issue.SEVERITY_WARNING);
issueVector.add(issue);
}
}
}
// if models are the same and newSc is null, then oldSc is not a rxnParticipant. Leave it as is in the expr.
}
if (newSC != null) {
reactionsAndSpeciesContexts.put(newSC, oldSC);
newExpression.substituteInPlace(new Expression(ste.getName()), new Expression(newSC.getName()));
}
// SpeciesContext sc = null;
// Species newSp = model.getSpecies(oldSc.getSpecies().getCommonName());
// if (oldSc.getStructure() == (oldRxnStruct)) {
// sc = model.getSpeciesContext(newSp, newRxnStruct);
// } else {
// if (newRxnStruct instanceof Membrane) {
// // for a membrane, we need to make sure that inside-outside spContexts used are appropriately replaced.
// if (oldSc.getStructure() == ((Membrane)oldRxnStruct).getOutsideFeature()) {
// // old speciesContext is outside (old) membrane, new spContext should be outside new membrane
// sc = model.getSpeciesContext(newSp, ((Membrane)newRxnStruct).getOutsideFeature());
// } else if (oldSc.getStructure() == ((Membrane)oldRxnStruct).getInsideFeature()) {
// // old speciesContext is inside (old) membrane, new spContext should be inside new membrane
// sc = model.getSpeciesContext(newSp, ((Membrane)newRxnStruct).getInsideFeature());
// }
// }
// }
// if (sc != null) {
// newExpression.substituteInPlace(new Expression(ste.getName()), new Expression(sc.getName()));
// }
} else if (ste instanceof StructureSize) {
Structure str = ((StructureSize) ste).getStructure();
// if the structure size used is same as the structure in which the reaction is present, change the structSize to appropriate new struct
if (str.compareEqual(fromRxnStruct)) {
newExpression.substituteInPlace(new Expression(ste.getName()), new Expression(toRxnStruct.getStructureSize().getName()));
} else {
if (fromRxnStruct instanceof Membrane) {
if (str.equals(structTopology.getOutsideFeature((Membrane) fromRxnStruct))) {
newExpression.substituteInPlace(new Expression(ste.getName()), new Expression(structTopology.getOutsideFeature((Membrane) toRxnStruct).getStructureSize().getName()));
} else if (str.equals(structTopology.getInsideFeature((Membrane) fromRxnStruct))) {
newExpression.substituteInPlace(new Expression(ste.getName()), new Expression(structTopology.getInsideFeature((Membrane) toRxnStruct).getStructureSize().getName()));
}
}
}
} else if (ste instanceof MembraneVoltage) {
Membrane membr = ((MembraneVoltage) ste).getMembrane();
// if the MembraneVoltage used is same as that of the membrane in which the reaction is present, change the MemVoltage
if ((fromRxnStruct instanceof Membrane) && (membr.compareEqual(fromRxnStruct))) {
newExpression.substituteInPlace(new Expression(ste.getName()), new Expression(((Membrane) toRxnStruct).getMembraneVoltage().getName()));
}
} else if (ste instanceof ModelParameter) {
// see if model has this global parameter (if rxn is being pasted into another model, it won't)
if (!pasteToModel.equals(pasteFromModel)) {
ModelParameter oldMp = (ModelParameter) ste;
ModelParameter mp = pasteToModel.getModelParameter(oldMp.getName());
boolean bNonNumeric = false;
String newMpName = oldMp.getName();
if (mp != null) {
// new model has a model parameter with same name - are they the same param?
if (!mp.getExpression().equals(oldMp.getExpression())) {
// no, they are not the same param, so mangle the 'ste' name and add as global in the other model
while (pasteToModel.getModelParameter(newMpName) != null) {
newMpName = TokenMangler.getNextEnumeratedToken(newMpName);
}
// if expression if numeric, add it as such. If not, set it to 0.0 and add it as global
Expression exp = oldMp.getExpression();
if (!exp.flatten().isNumeric()) {
exp = new Expression(0.0);
bNonNumeric = true;
}
ModelParameter newMp = pasteToModel.new ModelParameter(newMpName, exp, Model.ROLE_UserDefined, oldMp.getUnitDefinition());
String annotation = "Copied from model : " + pasteFromModel.getNameScope();
newMp.setModelParameterAnnotation(annotation);
pasteToModel.addModelParameter(newMp);
// if global param name had to be changed, make sure newExpr is updated as well.
if (!newMpName.equals(oldMp.getName())) {
newExpression.substituteInPlace(new Expression(oldMp.getName()), new Expression(newMpName));
}
}
} else {
// no global param with same name was found in other model, so add it to other model.
// if expression if numeric, add it as such. If not, set it to 0.0 and add it as global
Expression exp = oldMp.getExpression();
if (!exp.flatten().isNumeric()) {
exp = new Expression(0.0);
bNonNumeric = true;
}
ModelParameter newMp = pasteToModel.new ModelParameter(newMpName, exp, Model.ROLE_UserDefined, oldMp.getUnitDefinition());
String annotation = "Copied from model : " + pasteFromModel.getNameScope();
newMp.setModelParameterAnnotation(annotation);
pasteToModel.addModelParameter(newMp);
}
// if a non-numeric parameter was encountered in the old model, it was added as a numeric (0.0), warn user of change.
if (bNonNumeric) {
Issue issue = new Issue(oldMp, issueContext, IssueCategory.CopyPaste, "Global parameter '" + oldMp.getName() + "' was non-numeric; it has been added " + "as global parameter '" + newMpName + "' in the new model with value = 0.0. " + "Please update its value, if required, before using it.", Issue.SEVERITY_WARNING);
issueVector.add(issue);
}
}
}
}
// end - if newExpr.hasSymbol(ProxyParam)
}
// now if store <param names, new expression> in hashTable
if (paramExprHash.get(oldKps[i].getName()) == null) {
paramExprHash.put(oldKps[i].getName(), newExpression);
}
}
// end for - oldKps (old kinetic parameters)
// use this new expression to generate 'vcml' for the (new) kinetics (easier way to transfer all kinetic parameters)
String newKineticsStr = oldKinetics.writeTokensWithReplacingProxyParams(paramExprHash);
// convert the kinetics 'vcml' to tokens.
CommentStringTokenizer kineticsTokens = new CommentStringTokenizer(newKineticsStr);
// skip the first token;
kineticsTokens.nextToken();
// second token is the kinetic type; use this to create a dummy kinetics
String kineticType = kineticsTokens.nextToken();
Kinetics newkinetics = KineticsDescription.fromVCMLKineticsName(kineticType).createKinetics(newReactionStep);
// use the remaining tokens to construct the new kinetics
newkinetics.fromTokens(newKineticsStr);
// bind newkinetics to newReactionStep and add it to newReactionStep
newkinetics.bind(newReactionStep);
newReactionStep.setKinetics(newkinetics);
counter += 1;
if (counter == copyFromRxSteps.length) {
break;
}
if (!copiedStructName.equals(fromRxnStruct.getName())) {
if (currentStruct instanceof Feature) {
currentStruct = structTopology.getMembrane((Feature) currentStruct);
} else if (currentStruct instanceof Membrane) {
currentStruct = structTopology.getInsideFeature((Membrane) currentStruct);
}
}
copiedStructName = fromRxnStruct.getName();
} while (true);
return new PasteHelper(issueVector, rxPartMapStructure, reactionsAndSpeciesContexts);
}
use of cbit.vcell.model.ReactionParticipant in project vcell by virtualcell.
the class BioCartoonTool method pasteReactionSteps.
/**
* pasteReactionSteps : this method clones the model argument and calls the private pasteReationSteps0 method with the cloned model to see if
* there are any issues with the paste operation. If so, the issue list is popped up in a warning dialog, and user is given the option of proceeding
* with the paste or cancelling the operation.
*
* @param reactionStepsArr : reactions to be pasted
* @param model : model where reactions are to be pasted
* @param struct : strucure in 'model' where the reactions should be pasted
* @param bNew : is it 'paste' or 'paste new' reaction (new reaction Participants are created if 'bNew' is <true>).
* @param guiRequestComponent : the parent component for the warning dialog that pops up the issues, if any, encountered in the pasting process
* @throws Exception
*/
public static final void pasteReactionSteps(Component requester, ReactionStep[] reactionStepsArrOrig, Model pasteModel, Structure struct, boolean bNew, /*boolean bUseDBSpecies,*/
UserResolvedRxElements userResolvedRxElements, RXPasteInterface rxPasteInterface) {
PasteHelper[] pasteHelper = new PasteHelper[1];
AsynchClientTask issueTask = new AsynchClientTask("Checking Issues...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
Model clonedModel = (Model) org.vcell.util.BeanUtils.cloneSerializable(pasteModel);
clonedModel.refreshDependencies();
IssueContext issueContext = new IssueContext(ContextType.Model, clonedModel, null);
pasteHelper[0] = pasteReactionSteps0(null, requester, issueContext, reactionStepsArrOrig, clonedModel, clonedModel.getStructure(struct.getName()), bNew, /*bUseDBSpecies,*/
UserResolvedRxElements.createCompatibleUserResolvedRxElements(userResolvedRxElements, clonedModel));
if (pasteHelper[0].issues.size() != 0) {
if (!printIssues(pasteHelper[0].issues, requester)) {
throw UserCancelException.CANCEL_GENERIC;
}
}
if (pasteHelper[0].rxPartMapStruct != null) {
// Convert rxPartMapStruct instances from cloned to pasteModel
HashMap<String, HashMap<ReactionParticipant, Structure>> new_rxPartMapStruct = new HashMap<>();
for (int i = 0; reactionStepsArrOrig != null && i < reactionStepsArrOrig.length; i++) {
new_rxPartMapStruct.put(reactionStepsArrOrig[i].getName(), new HashMap<ReactionParticipant, Structure>());
for (ReactionParticipant rxPart : pasteHelper[0].rxPartMapStruct.get(reactionStepsArrOrig[i].getName()).keySet()) {
ReactionParticipant[] origRXParts = reactionStepsArrOrig[i].getReactionParticipants();
for (int j = 0; j < origRXParts.length; j++) {
if (origRXParts[j].getName().equals(rxPart.getName())) {
new_rxPartMapStruct.get(reactionStepsArrOrig[i].getName()).put(origRXParts[j], pasteModel.getStructure(pasteHelper[0].rxPartMapStruct.get(reactionStepsArrOrig[i].getName()).get(rxPart).getName()));
}
}
}
}
pasteHelper[0].rxPartMapStruct = new_rxPartMapStruct;
}
}
};
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);
pasteHelper[0] = pasteReactionSteps0(pasteHelper[0].rxPartMapStruct, requester, issueContext, reactionStepsArrOrig, pasteModel, struct, bNew, /*bUseDBSpecies,*/
userResolvedRxElements);
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.ReactionParticipant in project vcell by virtualcell.
the class ReactionCartoonTool method mouseReleased.
@Override
public void mouseReleased(MouseEvent event) {
if (getReactionCartoon() == null) {
return;
}
try {
if (dragStructTimer != null) {
dragStructTimer.stop();
}
endPointWorld = getReactionCartoon().getResizeManager().unzoom(event.getPoint());
Shape endShape = getReactionCartoon().pickWorld(endPointWorld);
if (event.isPopupTrigger() && mode == Mode.SELECT) {
// win, linux popup
popupMenu(getReactionCartoon().getSelectedShape(), event.getX(), event.getY());
return;
}
if (mode == Mode.SELECT && bStartRxContainerLabel) {
resetDropTargets(null, mode == Mode.STRUCTURE);
if (endShape != null && endShape instanceof ReactionContainerShape) {
Rectangle labelOutlineRectangle = ((ReactionContainerShape) endShape).getLabelOutline(endShape.getAbsX(), endShape.getAbsY());
boolean bLabel = labelOutlineRectangle.contains(startPointWorld.x, startPointWorld.y);
getGraphPane().setCursor((bLabel ? Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) : Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)));
}
bStartRxContainerLabel = false;
RXContainerDropTargetInfo trueRXContainerDropTargetInfo = getSelectedContainerDropTargetInfo();
lastRXContainerDropTargetInfoMap = null;
if (trueRXContainerDropTargetInfo == null) {
// turn off rxDropTargetRectangles
getGraphPane().repaint();
return;
}
StructureSuite structureSuite = null;
structureSuite = getReactionCartoon().getStructureSuite();
if (structureSuite != null) {
Structure[] originalOrderedStructArr = structureSuite.getStructures().toArray(new Structure[0]);
// find where user wants to put the structure
try {
if (trueRXContainerDropTargetInfo != null) {
ArrayList<Structure> newStructOrderList = new ArrayList<Structure>(Arrays.asList(originalOrderedStructArr));
newStructOrderList.remove(((ReactionContainerShape) startShape).getStructure());
int indexEnd = newStructOrderList.indexOf(((ReactionContainerShape) trueRXContainerDropTargetInfo.dropShape).getStructure());
int indexClosestNeighbor = (trueRXContainerDropTargetInfo.closestNeighborShape == null ? (trueRXContainerDropTargetInfo.insertFlag == RXContainerDropTargetInfo.INSERT_BEGINNING ? 0 : newStructOrderList.size()) : newStructOrderList.indexOf(((ReactionContainerShape) trueRXContainerDropTargetInfo.closestNeighborShape).getStructure()));
if (indexClosestNeighbor < indexEnd) {
newStructOrderList.add(indexEnd, ((ReactionContainerShape) startShape).getStructure());
} else {
newStructOrderList.add(indexClosestNeighbor, ((ReactionContainerShape) startShape).getStructure());
}
if (structureSuite instanceof AllStructureSuite) {
((AllStructureSuite) structureSuite).setModelStructureOrder(true);
}
ArrayList<Diagram> newDiagramOrderList = new ArrayList<Diagram>();
for (Structure structure : newStructOrderList) {
newDiagramOrderList.add(getModel().getDiagram(structure));
}
getModel().setDiagrams(newDiagramOrderList.toArray(new Diagram[0]));
}
} catch (Exception e) {
e.printStackTrace();
}
}
return;
}
// else do select and move
switch(mode) {
case SELECT:
{
getGraphPane().setCursor(Cursor.getDefaultCursor());
if (bMoving) {
getGraphPane().invalidate();
((JViewport) getGraphPane().getParent()).revalidate();
getGraphPane().repaint();
saveDiagram();
} else if (bRectStretch) {
Point absLoc = rectShape.getSpaceManager().getRelPos();
Dimension size = rectShape.getSpaceManager().getSize();
// remove temporary rectangle
getReactionCartoon().removeShape(rectShape);
rectShape = null;
Rectangle rect = new Rectangle(absLoc.x, absLoc.y, size.width, size.height);
boolean bShift = (event.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK;
boolean bCntrl = (event.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK;
selectEventFromWorld(rect, bShift, bCntrl);
getGraphPane().repaint();
}
bMoving = false;
movingShape = null;
bRectStretch = false;
rectShape = null;
break;
}
case LINEDIRECTED:
{
getGraphPane().setCursor(Cursor.getDefaultCursor());
if (bLineStretch && getDragDistanceSquared() >= MIN_DRAG_DISTANCE_TO_CREATE_NEW_ELEMENTS_SQUARED) {
bLineStretch = false;
// set label and color for line depending on which shape the edge started.
// (rather than attachment area on ReactionStepShape)
LineType lineType = getLineTypeFromDirection(startShape, endPointWorld);
if (endShape instanceof SimpleReactionShape) {
SimpleReaction simpleReaction = (SimpleReaction) endShape.getModelObject();
Object startShapeObject = null;
if (startShape != null) {
startShapeObject = startShape.getModelObject();
}
if (startShapeObject instanceof SpeciesContext) {
SpeciesContext speciesContext = (SpeciesContext) startShapeObject;
lineAction(speciesContext, simpleReaction);
} else if (startShapeObject instanceof Structure) {
Structure structure = (Structure) startShapeObject;
lineActon(structure, simpleReaction);
}
} else if (endShape instanceof SpeciesContextShape) {
SpeciesContext speciesContextEnd = (SpeciesContext) endShape.getModelObject();
Object startShapeObject = null;
if (startShape != null) {
startShapeObject = startShape.getModelObject();
}
if (startShapeObject instanceof SimpleReaction) {
SimpleReaction simpleReaction = (SimpleReaction) startShapeObject;
lineAction(simpleReaction, speciesContextEnd);
} else if (startShapeObject instanceof FluxReaction) {
FluxReaction fluxReaction = (FluxReaction) startShapeObject;
lineAction(fluxReaction, speciesContextEnd);
} else if (startShapeObject instanceof SpeciesContext) {
SpeciesContext speciesContextStart = (SpeciesContext) startShapeObject;
if (!speciesContextStart.equals(speciesContextEnd)) {
lineAction(speciesContextStart, speciesContextEnd);
}
} else if (startShapeObject instanceof Structure) {
Structure startStructure = (Structure) startShapeObject;
lineAction(startStructure, speciesContextEnd);
}
} else if (endShape instanceof FluxReactionShape) {
FluxReaction fluxReaction = (FluxReaction) endShape.getModelObject();
fluxReaction.setModel(getModel());
Object startShapeObject = null;
if (startShape != null) {
startShapeObject = startShape.getModelObject();
}
if (startShapeObject instanceof SpeciesContext) {
SpeciesContext speciesContext = (SpeciesContext) startShapeObject;
lineAction(speciesContext, fluxReaction);
} else if (startShapeObject instanceof Structure) {
Structure structure = (Structure) startShapeObject;
lineActon(structure, fluxReaction);
}
// remove temporary edge
getReactionCartoon().removeShape(edgeShape);
edgeShape = null;
} else if (endShape instanceof ReactionContainerShape) {
Structure endStructure = (Structure) endShape.getModelObject();
Object startObject = null;
if (startShape != null) {
startObject = startShape.getModelObject();
}
if (startObject instanceof SimpleReaction) {
SimpleReaction reaction = (SimpleReaction) startObject;
lineAction(reaction, endStructure);
} else if (startObject instanceof FluxReaction) {
FluxReaction reaction = (FluxReaction) startObject;
lineAction(reaction, endStructure);
} else if (startObject instanceof SpeciesContext) {
SpeciesContext speciesContextStart = (SpeciesContext) startObject;
lineAction(speciesContextStart, endStructure);
} else if (startObject instanceof Structure) {
Structure startStructure = (Structure) startObject;
lineAction(startStructure, endStructure, endShape);
}
}
}
saveDiagram();
break;
}
case LINECATALYST:
{
getGraphPane().setCursor(Cursor.getDefaultCursor());
if (bLineStretch && getDragDistanceSquared() >= MIN_DRAG_DISTANCE_TO_CREATE_NEW_ELEMENTS_SQUARED) {
bLineStretch = false;
// set label and color for line depending on which shape the edge started.
// (rather than attachment area on ReactionStepShape)
Object startObject = startShape.getModelObject();
Object endObject = endShape.getModelObject();
ReactionStep reactionStep = null;
SpeciesContext speciesContext = null;
if (startObject instanceof ReactionStep) {
reactionStep = (ReactionStep) startObject;
if (endObject instanceof SpeciesContext) {
if (StructureUtil.reactionHereCanHaveParticipantThere(reactionStep.getStructure(), ((SpeciesContext) endObject).getStructure())) {
speciesContext = (SpeciesContext) endObject;
}
} else if (endObject instanceof Structure) {
Structure endStructure = (Structure) endObject;
if (StructureUtil.reactionHereCanHaveParticipantThere(reactionStep.getStructure(), endStructure)) {
speciesContext = getReactionCartoon().getModel().createSpeciesContext(endStructure);
getReactionCartoon().notifyChangeEvent();
Point endPos = edgeShape.getEnd();
positionShapeForObject(endStructure, speciesContext, endPos);
}
}
} else if (startObject instanceof SpeciesContext) {
speciesContext = (SpeciesContext) startObject;
if (endObject instanceof ReactionStep) {
if (StructureUtil.reactionHereCanHaveParticipantThere(((ReactionStep) endObject).getStructure(), speciesContext.getStructure())) {
reactionStep = (ReactionStep) endObject;
}
} else if (endObject instanceof Structure) {
Structure endStructure = (Structure) endObject;
if (StructureUtil.reactionHereCanHaveParticipantThere(endStructure, speciesContext.getStructure())) {
reactionStep = getReactionCartoon().getModel().createSimpleReaction(endStructure);
getReactionCartoon().notifyChangeEvent();
Point endPos = edgeShape.getEnd();
positionShapeForObject(endStructure, reactionStep, endPos);
}
}
} else if (startObject instanceof Structure) {
Structure startStructure = (Structure) startObject;
if (endObject instanceof ReactionStep) {
reactionStep = (ReactionStep) endObject;
if (StructureUtil.reactionHereCanHaveParticipantThere(reactionStep.getStructure(), startStructure)) {
speciesContext = getReactionCartoon().getModel().createSpeciesContext(startStructure);
getReactionCartoon().notifyChangeEvent();
positionShapeForObject(startStructure, speciesContext, startPointWorld);
}
} else if (endObject instanceof SpeciesContext) {
speciesContext = (SpeciesContext) endObject;
if (StructureUtil.reactionHereCanHaveParticipantThere(startStructure, speciesContext.getStructure())) {
reactionStep = getReactionCartoon().getModel().createSimpleReaction(startStructure);
getReactionCartoon().notifyChangeEvent();
positionShapeForObject(startStructure, reactionStep, startPointWorld);
}
} else if (endObject instanceof Structure) {
Structure endStructure = (Structure) endObject;
if (StructureUtil.reactionHereCanHaveParticipantThere(startStructure, endStructure)) {
speciesContext = getReactionCartoon().getModel().createSpeciesContext(endStructure);
reactionStep = getReactionCartoon().getModel().createSimpleReaction(startStructure);
getReactionCartoon().notifyChangeEvent();
Point endPos = edgeShape.getEnd();
positionShapeForObject(endStructure, speciesContext, endPos);
positionShapeForObject(startStructure, reactionStep, startPointWorld);
} else if (StructureUtil.reactionHereCanHaveParticipantThere(endStructure, startStructure)) {
speciesContext = getReactionCartoon().getModel().createSpeciesContext(startStructure);
reactionStep = getReactionCartoon().getModel().createSimpleReaction(endStructure);
getReactionCartoon().notifyChangeEvent();
positionShapeForObject(startStructure, speciesContext, startPointWorld);
Point endPos = edgeShape.getEnd();
positionShapeForObject(endStructure, reactionStep, endPos);
}
}
}
if (reactionStep != null && speciesContext != null) {
Catalyst catalyst = null;
for (ReactionParticipant participant : reactionStep.getReactionParticipants()) {
if (participant instanceof Catalyst && participant.getSpeciesContext().equals(speciesContext)) {
catalyst = (Catalyst) participant;
}
}
if (catalyst == null) {
reactionStep.addCatalyst(speciesContext);
getReactionCartoon().notifyChangeEvent();
}
// add reactionParticipant to model
} else {
getGraphPane().repaint();
}
}
saveDiagram();
break;
}
default:
{
break;
}
}
} catch (Exception e) {
System.out.println("CartoonTool.mouseReleased: uncaught exception");
e.printStackTrace(System.out);
}
resetMouseActionHistory();
getGraphPane().repaint();
}
Aggregations