Search in sources :

Example 1 with StructureTopology

use of cbit.vcell.model.Model.StructureTopology in project vcell by virtualcell.

the class DBReactionWizardPanel method setupRX.

/**
 * Insert the method's description here.
 * Creation date: (8/5/2003 2:50:56 PM)
 * @param dbfr cbit.vcell.dictionary.ReactionDescription
 */
private void setupRX(ReactionDescription dbfr) {
    resolvedReaction = dbfr;
    if (resolvedReaction != null) {
        if (speciesAssignmentJCB != null) {
            for (int i = 0; i < speciesAssignmentJCB.length; i += 1) {
                speciesAssignmentJCB[i].removeActionListener(this);
            }
        }
        if (structureAssignmentJCB != null) {
            for (int i = 0; i < structureAssignmentJCB.length; i += 1) {
                structureAssignmentJCB[i].removeActionListener(this);
            }
        }
        getReactionCanvas1().setReactionCanvasDisplaySpec(resolvedReaction.toReactionCanvasDisplaySpec());
        getRXParticipantsJPanel().removeAll();
        // java.awt.Insets zeroInsets = new java.awt.Insets(0,0,0,0);
        java.awt.Insets fourInsets = new java.awt.Insets(4, 4, 4, 4);
        java.awt.GridBagConstraints gbc = new java.awt.GridBagConstraints();
        gbc.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gbc.insets = fourInsets;
        gbc.gridx = 0;
        gbc.gridy = 0;
        javax.swing.JLabel rxjlabel = new javax.swing.JLabel("RX Elements");
        // rxjlabel.setForeground(java.awt.Color.white);
        // rxjlabel.setOpaque(true);
        // rxjlabel.setBackground(java.awt.Color.white);
        getRXParticipantsJPanel().add(rxjlabel, gbc);
        // gbc.insets = zeroInsets;
        for (int i = 0; i < resolvedReaction.elementCount(); i += 1) {
            gbc.gridy = i + 1;
            javax.swing.JLabel jlabel = new javax.swing.JLabel(resolvedReaction.getReactionElement(i).getPreferredName() + (resolvedReaction.isFluxReaction() && resolvedReaction.getFluxIndexOutside() == i ? " (Outside)" : "") + (resolvedReaction.isFluxReaction() && resolvedReaction.getFluxIndexInside() == i ? " (Inside)" : ""));
            // jlabel.setOpaque(true);
            // jlabel.setBackground(java.awt.Color.white);
            // jlabel.setForeground(java.awt.Color.black);
            getRXParticipantsJPanel().add(jlabel, gbc);
        }
        // gbc.insets = fourInsets;
        gbc.gridx = 1;
        gbc.gridy = 0;
        speciesAssignmentJCB = new javax.swing.JComboBox[resolvedReaction.elementCount()];
        DefaultListCellRenderer speciesListCellRenderer = new DefaultListCellRenderer() {

            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                // TODO Auto-generated method stub
                return super.getListCellRendererComponent(list, (value instanceof Species ? "Existing " + ((Species) value).getCommonName() : value), index, isSelected, cellHasFocus);
            }
        };
        javax.swing.JLabel rspjlabel = new javax.swing.JLabel("Assign to Model Species");
        // rspjlabel.setForeground(java.awt.Color.white);
        // rspjlabel.setOpaque(true);
        // rspjlabel.setBackground(java.awt.Color.white);
        getRXParticipantsJPanel().add(rspjlabel, gbc);
        // getRXParticipantsJPanel().add(new javax.swing.JLabel("Resolve to Model Species"),gbc);
        speciesOrder = new Species[getModel().getSpecies().length + 1];
        speciesOrder[0] = null;
        for (int j = 0; j < getModel().getSpecies().length; j += 1) {
            speciesOrder[j + 1] = getModel().getSpecies(j);
        }
        for (int i = 0; i < resolvedReaction.elementCount(); i += 1) {
            javax.swing.JComboBox jcb = new javax.swing.JComboBox();
            jcb.setRenderer(speciesListCellRenderer);
            speciesAssignmentJCB[i] = jcb;
            jcb.addItem("New Species");
            for (int j = 1; j < speciesOrder.length; j += 1) {
                jcb.addItem(/*"Existing "+*/
                speciesOrder[j]);
            }
            gbc.gridy = i + 1;
            getRXParticipantsJPanel().add(jcb, gbc);
            jcb.setEnabled(false);
        }
        gbc.gridx = 2;
        gbc.gridy = 0;
        structureAssignmentJCB = new javax.swing.JComboBox[resolvedReaction.elementCount()];
        DefaultListCellRenderer structureListCellRenderer = new DefaultListCellRenderer() {

            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                // TODO Auto-generated method stub
                return super.getListCellRendererComponent(list, (value instanceof Structure ? ((Structure) value).getName() : value), index, isSelected, cellHasFocus);
            }
        };
        javax.swing.JLabel rstjlabel = new javax.swing.JLabel("Assign to Model Compartment");
        // rstjlabel.setForeground(java.awt.Color.white);
        // rstjlabel.setOpaque(true);
        // rstjlabel.setBackground(java.awt.Color.white);
        getRXParticipantsJPanel().add(rstjlabel, gbc);
        // getRXParticipantsJPanel().add(new javax.swing.JLabel("Resolve to Model Compartment"),gbc);
        StructureTopology structTopology = getModel().getStructureTopology();
        for (int i = 0; i < resolvedReaction.elementCount(); i += 1) {
            javax.swing.JComboBox jcb = new javax.swing.JComboBox();
            jcb.setRenderer(structureListCellRenderer);
            structureAssignmentJCB[i] = jcb;
            if (resolvedReaction.isFluxReaction() && resolvedReaction.isFlux(i) && resolvedReaction.getFluxIndexOutside() == i) {
                jcb.addItem(structTopology.getOutsideFeature((Membrane) getStructure()));
                jcb.setEnabled(false);
            } else if (resolvedReaction.isFluxReaction() && resolvedReaction.isFlux(i) && resolvedReaction.getFluxIndexInside() == i) {
                jcb.addItem((structTopology).getInsideFeature((Membrane) getStructure()));
                jcb.setEnabled(false);
            } else {
                jcb.addItem(getStructure());
                if (getStructure() instanceof Membrane) {
                    jcb.addItem(structTopology.getOutsideFeature((Membrane) getStructure()));
                    jcb.addItem(structTopology.getInsideFeature((Membrane) getStructure()));
                } else {
                    jcb.setEnabled(false);
                }
            }
            gbc.gridy = i + 1;
            getRXParticipantsJPanel().add(jcb, gbc);
        }
        for (int i = 0; i < resolvedReaction.elementCount(); i += 1) {
            speciesAssignmentJCB[i].addActionListener(this);
            structureAssignmentJCB[i].addActionListener(this);
        }
    }
}
Also used : GridBagConstraints(java.awt.GridBagConstraints) StructureTopology(cbit.vcell.model.Model.StructureTopology) GridBagConstraints(java.awt.GridBagConstraints) DefaultListCellRenderer(javax.swing.DefaultListCellRenderer) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) DBFormalSpecies(cbit.vcell.model.DBFormalSpecies) Species(cbit.vcell.model.Species) DBNonFormalUnboundSpecies(cbit.vcell.dictionary.DBNonFormalUnboundSpecies) JList(javax.swing.JList)

Example 2 with StructureTopology

use of cbit.vcell.model.Model.StructureTopology 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) {
            // Structure toNewStruct = userResolvedRxElements.toStructureArr[i];
            // SpeciesContext[] toNewSC = pasteToModel.getSpeciesContexts(toNewStruct);
            // SpeciesContext[] usersSC = userResolvedRxElements.fromSpeciesContextArr;
            // boolean bFound = false;
            // for (int j = 0; j < toNewSC.length; j++) {
            // boolean structeql = toNewSC[j].getStructure().getName().equals(usersSC[i].getStructure().getName());
            // boolean specieseql = toNewSC[j].getSpecies().getCommonName().equals(usersSC[i].getSpecies().getCommonName());
            // System.out.println(toNewSC[j]+" "+structeql+" "+usersSC[i]+" "+specieseql);
            // if(structeql &&  specieseql) {
            // bFound = true;
            // break;
            // }
            // }
            // if(!bFound) {
            // throw new Exception("Expecting speciesContext '"+usersSC[i].getSpecies().getCommonName()+"' to exist already in structure "+toNewStruct.getName());
            // }
            // 
            // //					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>>();
        }
        // }
        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)
            SpeciesContext newSc = null;
            for (int j = 0; j < userResolvedRxElements.fromSpeciesContextArr.length; j++) {
                String forceName = userResolvedRxElements.finalNames.get(j).getText();
                if (userResolvedRxElements.fromSpeciesContextArr[j] == copyFromRxParticipantArr[i].getSpeciesContext()) {
                    if (userResolvedRxElements.toSpeciesArr[j] == null) {
                        newSc = pasteSpecies(parent, copyFromRxParticipantArr[i].getSpecies(), null, pasteToModel, pasteToStruct, bNew, /*bUseDBSpecies,*/
                        speciesHash, UserResolvedRxElements.getPreferredReactionElement(userResolvedRxElements, copyFromRxParticipantArr[i]));
                        changeName(userResolvedRxElements, newSc, j, pasteToModel, forceName);
                        reactionsAndSpeciesContexts.put(newSc, copyFromRxParticipantArr[i].getSpeciesContext());
                    } else {
                        if (forceName != null && forceName.length() > 0 && pasteToModel.getSpeciesContext(forceName) != null) {
                            if (pasteToModel.getSpeciesContext(forceName).getStructure().getName() == userResolvedRxElements.toStructureArr[j].getName()) {
                                throw new Exception("Paste custom name error:\nSpeciesContext name '" + forceName + "' in structure '" + userResolvedRxElements.toStructureArr[j].getName() + "' already used");
                            }
                        }
                        newSc = pasteToModel.getSpeciesContext(userResolvedRxElements.toSpeciesArr[j], userResolvedRxElements.toStructureArr[j]);
                        if (newSc == null) {
                            newSc = pasteSpecies(parent, copyFromRxParticipantArr[i].getSpecies(), null, pasteToModel, pasteToStruct, bNew, /*bUseDBSpecies,*/
                            speciesHash, UserResolvedRxElements.getPreferredReactionElement(userResolvedRxElements, copyFromRxParticipantArr[i]));
                            changeName(userResolvedRxElements, newSc, j, pasteToModel, forceName);
                        } else if (forceName != null && forceName.length() > 0) {
                            throw new Exception("Paste custom name error:\nCan't rename existing speciesContext '" + newSc.getName() + "' in structure '" + newSc.getStructure().getName() + "' to '" + forceName + "'");
                        }
                        reactionsAndSpeciesContexts.put(newSc, copyFromRxParticipantArr[i].getSpeciesContext());
                    // String rootSC = ReactionCartoonTool.speciesContextRootFinder(copyFromRxParticipantArr[i].getSpeciesContext());
                    // SpeciesContext[] matchSC = pasteToModel.getSpeciesContexts();
                    // for(int k=0;matchSC != null && k<matchSC.length;k++){
                    // String matchRoot = ReactionCartoonTool.speciesContextRootFinder(matchSC[k]);
                    // if(matchRoot != null && matchRoot.equals(rootSC) && matchSC[k].getStructure().getName().equals(pasteToStruct.getName())){
                    // newSc = matchSC[k];
                    // reactionsAndSpeciesContexts.put(newSc, matchSC[k]);
                    // break;
                    // }
                    // }
                    }
                    if (newSc == null) {
                        throw new Exception("Couldn't assign speciesContext='" + copyFromRxParticipantArr[i].getSpeciesContext().getName() + "' to species='" + userResolvedRxElements.toSpeciesArr[j].getCommonName() + "' in structure='" + userResolvedRxElements.toStructureArr[j].getName() + "', species/structure not exist");
                    }
                }
            }
            // String rootSC = ReactionCartoonTool.speciesContextRootFinder(copyFromRxParticipantArr[i].getSpeciesContext());
            // SpeciesContext newSc = null;
            // //				if(!bNew) {
            // 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);
}
Also used : Issue(org.vcell.util.Issue) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) Product(cbit.vcell.model.Product) FluxReaction(cbit.vcell.model.FluxReaction) SpeciesContext(cbit.vcell.model.SpeciesContext) Reactant(cbit.vcell.model.Reactant) Feature(cbit.vcell.model.Feature) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) Species(cbit.vcell.model.Species) Vector(java.util.Vector) SimpleReaction(cbit.vcell.model.SimpleReaction) KineticsProxyParameter(cbit.vcell.model.Kinetics.KineticsProxyParameter) StructureTopology(cbit.vcell.model.Model.StructureTopology) Hashtable(java.util.Hashtable) BioModelEntityObject(cbit.vcell.model.BioModelEntityObject) StructureSize(cbit.vcell.model.Structure.StructureSize) PropertyVetoException(java.beans.PropertyVetoException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ModelException(cbit.vcell.model.ModelException) UserCancelException(org.vcell.util.UserCancelException) ModelParameter(cbit.vcell.model.Model.ModelParameter) Expression(cbit.vcell.parser.Expression) MembraneVoltage(cbit.vcell.model.Membrane.MembraneVoltage) ReactionStep(cbit.vcell.model.ReactionStep) Model(cbit.vcell.model.Model) CommentStringTokenizer(org.vcell.util.CommentStringTokenizer) Kinetics(cbit.vcell.model.Kinetics) ReactionParticipant(cbit.vcell.model.ReactionParticipant) Catalyst(cbit.vcell.model.Catalyst)

Example 3 with StructureTopology

use of cbit.vcell.model.Model.StructureTopology in project vcell by virtualcell.

the class ReactionCartoonTool method lineAction.

private void lineAction(SpeciesContext speciesContextStart, Structure endStructure) throws PropertyVetoException, Exception {
    Point startPos = edgeShape.getStart();
    Point endPos = edgeShape.getEnd();
    Model model = getModel();
    StructureTopology structTopology = model.getStructureTopology();
    Structure startStructure = speciesContextStart.getStructure();
    ReactionStep reaction = null;
    Structure reactionStructure = null;
    if (endStructure != startStructure) {
        if (startStructure instanceof Feature && endStructure instanceof Feature) {
            // FeatureStart-speciesContext ==> FeatureEnd with NO membrane in between : create lumped reaction in FeatureStart and pdt in FeatureEnd
            reactionStructure = startStructure;
            reaction = model.createSimpleReaction(reactionStructure);
            reaction.addReactant(speciesContextStart, 1);
            SpeciesContext endSpeciesContext = model.createSpeciesContext(endStructure);
            reaction.addProduct(endSpeciesContext, 1);
            reaction.setKinetics(new GeneralLumpedKinetics(reaction));
            positionShapeForObject(endStructure, endSpeciesContext, endPos);
        } else if (startStructure instanceof Feature && endStructure instanceof Membrane) {
            // Feature-speciesContext ==> Membrane : create lumped reaction in membrane, pdt in membrane
            reactionStructure = endStructure;
            reaction = model.createSimpleReaction(reactionStructure);
            reaction.addReactant(speciesContextStart, 1);
            SpeciesContext endSpeciesContext = model.createSpeciesContext(endStructure);
            reaction.addProduct(endSpeciesContext, 1);
            reaction.setKinetics(new GeneralLumpedKinetics(reaction));
            positionShapeForObject(endStructure, endSpeciesContext, endPos);
        } else if (startStructure instanceof Membrane && endStructure instanceof Feature) {
            // Membrane-speciesContext ==> Feature : create reaction in Membrane, pdt in Feature
            reactionStructure = startStructure;
            reaction = model.createSimpleReaction(reactionStructure);
            reaction.addReactant(speciesContextStart, 1);
            SpeciesContext endSpeciesContext = model.createSpeciesContext(endStructure);
            reaction.addProduct(endSpeciesContext, 1);
            positionShapeForObject(endStructure, endSpeciesContext, endPos);
        } else if (startStructure instanceof Membrane && endStructure instanceof Membrane) {
            // MembraneStart-speciescontext ==> MembraneEnd : create lumped reaction in MembraneStart, pdt in MembraneEnd.
            reactionStructure = startStructure;
            reaction = model.createSimpleReaction(reactionStructure);
            reaction.addReactant(speciesContextStart, 1);
            SpeciesContext endSpeciesContext = model.createSpeciesContext(endStructure);
            reaction.addProduct(endSpeciesContext, 1);
            reaction.setKinetics(new GeneralLumpedKinetics(reaction));
            positionShapeForObject(endStructure, endSpeciesContext, endPos);
        }
    } else {
        // startStructure and endStructure are the same
        // Feature1 ==> Feature1 OR Membrane1 ==> Membrane1
        reaction = model.createSimpleReaction(startStructure);
        reaction.addReactant(speciesContextStart, 1);
    }
    positionShapeForObject(reactionStructure, reaction, new Point(((8 * startPos.x + 2 * endPos.x) / 10), (8 * startPos.y + 2 * endPos.y) / 10));
    getReactionCartoon().notifyChangeEvent();
    getGraphModel().clearSelection();
    getGraphModel().select(reaction);
}
Also used : StructureTopology(cbit.vcell.model.Model.StructureTopology) ReactionStep(cbit.vcell.model.ReactionStep) Model(cbit.vcell.model.Model) BioModel(cbit.vcell.biomodel.BioModel) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) GraphModel(cbit.gui.graph.GraphModel) Membrane(cbit.vcell.model.Membrane) Point(java.awt.Point) SpeciesContext(cbit.vcell.model.SpeciesContext) GeneralLumpedKinetics(cbit.vcell.model.GeneralLumpedKinetics) Structure(cbit.vcell.model.Structure) Feature(cbit.vcell.model.Feature)

Example 4 with StructureTopology

use of cbit.vcell.model.Model.StructureTopology in project vcell by virtualcell.

the class GeometryContext method fixMembraneMappings.

private void fixMembraneMappings() throws PropertyVetoException {
    StructureTopology structTopology = getModel().getStructureTopology();
    for (int j = 0; j < fieldStructureMappings.length; j++) {
        if (fieldStructureMappings[j] instanceof MembraneMapping) {
            MembraneMapping membraneMapping = (MembraneMapping) fieldStructureMappings[j];
            Membrane membrane = membraneMapping.getMembrane();
            Feature insideFeature = structTopology.getInsideFeature(membrane);
            Feature outsideFeature = structTopology.getOutsideFeature(membrane);
            // 
            if (insideFeature != null && outsideFeature != null) {
                FeatureMapping insideFM = (FeatureMapping) getStructureMapping(insideFeature);
                FeatureMapping outsideFM = (FeatureMapping) getStructureMapping(outsideFeature);
                GeometryClass insideGeometryClass = insideFM.getGeometryClass();
                GeometryClass outsideGeometryClass = outsideFM.getGeometryClass();
                // 
                if (insideFM != null && insideGeometryClass != null && outsideFM != null && outsideGeometryClass != null) {
                    // inside/outside both mapped to same domain ... membrane must be there too.
                    if (insideGeometryClass == outsideGeometryClass) {
                        membraneMapping.setGeometryClass(insideGeometryClass);
                    // inside/outside mapped to different subvolumes (try to map membrane to adjacent surfaceClass)
                    } else if (insideGeometryClass instanceof SubVolume && outsideGeometryClass instanceof SubVolume) {
                        GeometryClass[] geometryClasses = getGeometry().getGeometryClasses();
                        boolean bFound = false;
                        for (int i = 0; i < geometryClasses.length; i++) {
                            if (geometryClasses[i] instanceof SurfaceClass) {
                                SurfaceClass surfaceClass = (SurfaceClass) geometryClasses[i];
                                if (surfaceClass.isAdjacentTo((SubVolume) insideGeometryClass) && surfaceClass.isAdjacentTo((SubVolume) outsideGeometryClass)) {
                                    membraneMapping.setGeometryClass(surfaceClass);
                                    bFound = true;
                                }
                            }
                        }
                        if (!bFound) {
                            membraneMapping.setGeometryClass(null);
                        }
                    // inside/outside mapped to different membranes (membrane cannot be mapped ... must be cleared).
                    } else if (insideGeometryClass instanceof SurfaceClass && outsideGeometryClass instanceof SurfaceClass) {
                        membraneMapping.setGeometryClass(null);
                    // inside mapped to surface and outside mapped to subvolume (if adjacent, map membrane to surface ... else clear).
                    } else if (insideGeometryClass instanceof SurfaceClass && outsideGeometryClass instanceof SubVolume) {
                        SurfaceClass surface = (SurfaceClass) insideGeometryClass;
                        SubVolume subVolume = (SubVolume) outsideGeometryClass;
                        if (surface.isAdjacentTo(subVolume)) {
                            membraneMapping.setGeometryClass(surface);
                        } else {
                            membraneMapping.setGeometryClass(null);
                        }
                    // inside mapped to subvolume and outside mapped to surface (if adjacent, map membrane to surface ... else clear).
                    } else if (insideGeometryClass instanceof SubVolume && outsideGeometryClass instanceof SurfaceClass) {
                        SurfaceClass surface = (SurfaceClass) outsideGeometryClass;
                        SubVolume subVolume = (SubVolume) insideGeometryClass;
                        if (surface.isAdjacentTo(subVolume)) {
                            membraneMapping.setGeometryClass(surface);
                        } else {
                            membraneMapping.setGeometryClass(null);
                        }
                    }
                }
            }
        }
    }
}
Also used : GeometryClass(cbit.vcell.geometry.GeometryClass) StructureTopology(cbit.vcell.model.Model.StructureTopology) SurfaceClass(cbit.vcell.geometry.SurfaceClass) SubVolume(cbit.vcell.geometry.SubVolume) CompartmentSubVolume(cbit.vcell.geometry.CompartmentSubVolume) Membrane(cbit.vcell.model.Membrane) Feature(cbit.vcell.model.Feature)

Example 5 with StructureTopology

use of cbit.vcell.model.Model.StructureTopology in project vcell by virtualcell.

the class ModelDbDriver method getModel.

/**
 * This method was created in VisualAge.
 * @return cbit.vcell.model.Model
 * @param rset java.sql.ResultSet
 */
private Model getModel(QueryHashtable dbc, ResultSet rset, Connection con, User user) throws SQLException, DataAccessException {
    // User owner = new User(ownerName, ownerRef);
    try {
        Model model = modelTable.getModel(rset, con);
        // model.setOwner(owner);
        KeyValue modelKey = model.getVersion().getVersionKey();
        // 
        // set structures for this model
        // 
        StructureTopology structureTopology = model.getStructureTopology();
        Structure[] structures = reactStepDB.getStructuresFromModel(dbc, con, modelKey);
        if (structures != null && structures.length > 0) {
            model.setStructures(structures);
        }
        HashMap<KeyValue, StructureKeys> structureKeysMap = reactStepDB.getStructureParentMapByModel(dbc, con, modelKey);
        ReactStepDbDriver.populateStructureAndElectricalTopology(model, structureKeysMap);
        // 
        // set species for this model
        // 
        SpeciesContext[] speciesContexts = getSpeciesContextFromModel(dbc, con, user, modelKey, structureTopology);
        if (speciesContexts != null) {
            Vector<Species> speciesList = new Vector<Species>();
            for (int i = 0; i < speciesContexts.length; i++) {
                if (!speciesList.contains(speciesContexts[i].getSpecies())) {
                    speciesList.addElement(speciesContexts[i].getSpecies());
                }
            }
            Species[] speciesArray = new Species[speciesList.size()];
            speciesList.copyInto(speciesArray);
            model.setSpecies(speciesArray);
        }
        // 
        if (speciesContexts != null) {
            model.setSpeciesContexts(speciesContexts);
        }
        // 
        // Add global parameters to the model
        // 
        GlobalModelParameterTable.table.setModelParameters(con, model);
        // 
        // add reactionSteps for this model
        // 
        ReactionStep[] reactSteps = reactStepDB.getReactionStepsFromModel(dbc, con, model, modelKey);
        if (reactSteps != null) {
            model.setReactionSteps(reactSteps);
            for (int i = 0; i < reactSteps.length; i++) {
                try {
                    // 
                    // fix any improperly defined reactionSteps (which have parameters that should be catalysts)
                    // name space of kinetic parameters should be unique with respect to SpeciesContexts (so if they overlap, should be a catalyst).
                    // 
                    Kinetics.KineticsParameter[] params = reactSteps[i].getKinetics().getKineticsParameters();
                    for (int j = 0; j < params.length; j++) {
                        SpeciesContext speciesContext = model.getSpeciesContext(params[j].getName());
                        if (speciesContext != null) {
                            reactSteps[i].addCatalyst(speciesContext);
                            if (lg.isWarnEnabled())
                                lg.warn("ModelDbDriver.getModel(), Parameter '" + params[j].getName() + "' in Reaction " + reactSteps[i].getName() + " in Model(" + model.getKey() + ") conflicts with SpeciesContext, added as a catalyst");
                        }
                    }
                } catch (Throwable e) {
                    lg.error(e.getMessage(), e);
                }
                try {
                    reactSteps[i].rebindAllToModel(model);
                } catch (cbit.vcell.parser.ExpressionBindingException e) {
                    throw new DataAccessException("bindingException: " + e.getMessage());
                } catch (cbit.vcell.parser.ExpressionException e) {
                    throw new DataAccessException(e.getMessage());
                } catch (PropertyVetoException e) {
                    throw new DataAccessException("PropertyVetoException: " + e.getMessage());
                } catch (cbit.vcell.model.ModelException e) {
                    throw new DataAccessException(e.getMessage());
                }
            }
        }
        // 
        // add diagrams for this model
        // 
        Diagram[] diagrams = getDiagramsFromModel(dbc, con, modelKey, structureTopology, dbSyntax);
        model.setDiagrams(diagrams);
        // 
        // add rbm
        // 
        ModelTable.readRbmElement(con, model, dbSyntax);
        if (!model.getRbmModelContainer().isEmpty()) {
            for (SpeciesContext sc : model.getSpeciesContexts()) {
                sc.parseSpeciesPatternString(model);
            }
        }
        return model;
    } catch (PropertyVetoException e) {
        lg.error(e.getMessage(), e);
        throw new DataAccessException(e.getMessage());
    }
}
Also used : KeyValue(org.vcell.util.document.KeyValue) SpeciesContext(cbit.vcell.model.SpeciesContext) Structure(cbit.vcell.model.Structure) Species(cbit.vcell.model.Species) Vector(java.util.Vector) DataAccessException(org.vcell.util.DataAccessException) StructureTopology(cbit.vcell.model.Model.StructureTopology) Diagram(cbit.vcell.model.Diagram) PropertyVetoException(java.beans.PropertyVetoException) StructureKeys(cbit.vcell.modeldb.ReactStepDbDriver.StructureKeys) ReactionStep(cbit.vcell.model.ReactionStep) Model(cbit.vcell.model.Model)

Aggregations

StructureTopology (cbit.vcell.model.Model.StructureTopology)29 Membrane (cbit.vcell.model.Membrane)21 Feature (cbit.vcell.model.Feature)19 Structure (cbit.vcell.model.Structure)15 Expression (cbit.vcell.parser.Expression)14 MembraneMapping (cbit.vcell.mapping.MembraneMapping)10 StructureMapping (cbit.vcell.mapping.StructureMapping)8 Model (cbit.vcell.model.Model)8 SpeciesContext (cbit.vcell.model.SpeciesContext)7 ExpressionException (cbit.vcell.parser.ExpressionException)7 StructureMappingParameter (cbit.vcell.mapping.StructureMapping.StructureMappingParameter)6 ReactionStep (cbit.vcell.model.ReactionStep)6 Species (cbit.vcell.model.Species)6 FeatureMapping (cbit.vcell.mapping.FeatureMapping)5 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)5 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)4 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)4 PropertyVetoException (java.beans.PropertyVetoException)4 Vector (java.util.Vector)4 BioModel (cbit.vcell.biomodel.BioModel)3