Search in sources :

Example 41 with Membrane

use of cbit.vcell.model.Membrane in project vcell by virtualcell.

the class StructurePasteMappingPanel method initialize.

private void initialize() {
    Structure structFrom = rsCopy.getFromStructure();
    List<String> membranesTo = new ArrayList<>();
    List<String> compartmentsTo = new ArrayList<>();
    membranesTo.add(CHOOSE_STRUCT);
    membranesTo.add(MAKE_NEW);
    compartmentsTo.add(CHOOSE_STRUCT);
    compartmentsTo.add(MAKE_NEW);
    for (Structure struct : modelTo.getStructures()) {
        if (struct == structTo) {
            continue;
        }
        if (struct instanceof Membrane) {
            membranesTo.add(struct.getName());
        } else {
            compartmentsTo.add(struct.getName());
        }
    }
    for (Structure from : rsCopy.getStructuresArr()) {
        if (from == structFrom) {
            // skip the structure from where we copy, it's already mapped
            continue;
        }
        JComboBox<String> comboTo = new JComboBox<>();
        if (from instanceof Membrane) {
            comboTo.setModel(new DefaultComboBoxModel(membranesTo.toArray()));
            int index = membranesTo.indexOf(from.getName());
            comboTo.setSelectedIndex(index >= 0 ? index : 0);
        } else {
            comboTo.setModel(new DefaultComboBoxModel(compartmentsTo.toArray()));
            int index = compartmentsTo.indexOf(from.getName());
            comboTo.setSelectedIndex(index >= 0 ? index : 0);
        }
        structureMap.put(from, comboTo);
        comboTo.addActionListener(eventHandler);
    }
    JPanel structPanel = new JPanel();
    // structPanel.setPreferredSize(new Dimension(220, 180));
    structPanel.setLayout(new GridBagLayout());
    // header
    int gridy = 0;
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = gridy;
    // top, left, bottom, right
    gbc.insets = new Insets(10, 10, 10, 10);
    gbc.weightx = 1;
    gbc.anchor = GridBagConstraints.WEST;
    structPanel.add(new JLabel("<html><b>Structures to Paste</b></html>"), gbc);
    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = gridy;
    gbc.insets = new Insets(10, 10, 10, 10);
    gbc.weightx = 1;
    gbc.anchor = GridBagConstraints.WEST;
    // gbc.fill = GridBagConstraints.HORIZONTAL;
    structPanel.add(new JLabel("<html><b>Mapped to: </b></html>"), gbc);
    // the first row shows the source and destination structures (not customizable)
    gridy++;
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = gridy;
    gbc.insets = new Insets(1, 10, 1, 10);
    gbc.weightx = 1;
    gbc.anchor = GridBagConstraints.WEST;
    structPanel.add(new JLabel("<html>" + structFrom.getName() + "</html>"), gbc);
    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = gridy;
    gbc.insets = new Insets(1, 12, 1, 10);
    gbc.weightx = 1;
    gbc.anchor = GridBagConstraints.WEST;
    structPanel.add(new JLabel("<html>" + structTo.getName() + "</html>"), gbc);
    for (Map.Entry<Structure, JComboBox<String>> entry : structureMap.entrySet()) {
        // the first row shows the source and destination structures (not customizable)
        gridy++;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = gridy;
        gbc.insets = new Insets(1, 10, 2, 10);
        gbc.weightx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        structPanel.add(new JLabel("<html>" + entry.getKey().getName() + "</html>"), gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = gridy;
        gbc.insets = new Insets(2, 10, 1, 10);
        gbc.weightx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        structPanel.add(entry.getValue(), gbc);
    }
    gridy++;
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = gridy;
    gbc.gridwidth = 4;
    gbc.weightx = 1;
    // fake cell used for filling all the vertical empty space
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.WEST;
    structPanel.add(new JLabel(""), gbc);
    // JPanel issuesPanel = new JPanel();
    // issuesPanel.setLayout(new BorderLayout());
    // issuesPanel.add(issuesPane, BorderLayout.CENTER);
    JScrollPane upper = new JScrollPane(structPanel);
    upper.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    upper.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    JScrollPane lower = new JScrollPane(issuesPane);
    lower.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    lower.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    setLayout(new BorderLayout());
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upper, lower);
    splitPane.setOneTouchExpandable(true);
    splitPane.setDividerLocation(150);
    add(splitPane, BorderLayout.CENTER);
    recalculateIssues();
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) JComboBox(javax.swing.JComboBox) GridBagLayout(java.awt.GridBagLayout) ArrayList(java.util.ArrayList) JLabel(javax.swing.JLabel) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) BorderLayout(java.awt.BorderLayout) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) JSplitPane(javax.swing.JSplitPane) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 42 with Membrane

use of cbit.vcell.model.Membrane in project vcell by virtualcell.

the class ReactionCartoonTool method lineAction.

private void lineAction(ReactionStep reactionStart, Structure endStructure) throws Exception {
    Structure startStructure = reactionStart.getStructure();
    if (StructureUtil.reactionHereCanHaveParticipantThere(startStructure, endStructure)) {
        // if reactionStart is a SimpleRxn OR FluxRxn without a product, add speciesContext as pdt
        if ((reactionStart instanceof SimpleReaction) || ((reactionStart instanceof FluxReaction) && !reactionStart.hasProduct())) {
            SpeciesContext speciesContext = getModel().createSpeciesContext(endStructure);
            reactionStart.addProduct(speciesContext, 1);
            positionShapeForObject(endStructure, speciesContext, edgeShape.getEnd());
        }
        if (((startStructure instanceof Feature && endStructure instanceof Feature) || (startStructure instanceof Membrane && endStructure instanceof Membrane)) && startStructure != endStructure) {
        // ============ change kinetics to lumped or warn user ?????????
        // reactionStart.setKinetics(new GeneralLumpedKinetics(reactionStart));
        }
        getReactionCartoon().notifyChangeEvent();
    }
}
Also used : SimpleReaction(cbit.vcell.model.SimpleReaction) Membrane(cbit.vcell.model.Membrane) FluxReaction(cbit.vcell.model.FluxReaction) SpeciesContext(cbit.vcell.model.SpeciesContext) Structure(cbit.vcell.model.Structure) Feature(cbit.vcell.model.Feature)

Example 43 with Membrane

use of cbit.vcell.model.Membrane 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;
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) JComboBox(javax.swing.JComboBox) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BoxLayout(javax.swing.BoxLayout) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) Feature(cbit.vcell.model.Feature) PropertyVetoException(java.beans.PropertyVetoException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ModelException(cbit.vcell.model.ModelException) UserCancelException(org.vcell.util.UserCancelException) RbmModelContainer(cbit.vcell.model.Model.RbmModelContainer) Container(java.awt.Container) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) Component(java.awt.Component) ReactionParticipant(cbit.vcell.model.ReactionParticipant)

Example 44 with Membrane

use of cbit.vcell.model.Membrane 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);
}
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 45 with Membrane

use of cbit.vcell.model.Membrane in project vcell by virtualcell.

the class BioCartoonTool method mapStructures.

// map the structures to be pasted to existing structures in the cloned model
// we may need to generate some name iteratively until we solve all naming conflicts
private static Map<Structure, String> mapStructures(Component requester, ReactionSpeciesCopy rsCopy, Model modelTo, Structure structTo, IssueContext issueContext) {
    // use internally only; we exit the dialog when there are no issues left
    Vector<Issue> issueVector = new Vector<>();
    Structure structFrom = rsCopy.getFromStructure();
    Map<Structure, String> fullyMappedStructures = new LinkedHashMap<>();
    fullyMappedStructures.put(structFrom, structTo.getName());
    StructurePasteMappingPanel structureMappingPanel = null;
    do {
        issueVector.clear();
        if (structureMappingPanel == null) {
            structureMappingPanel = new StructurePasteMappingPanel(rsCopy, modelTo, structTo, issueVector, issueContext);
            structureMappingPanel.setPreferredSize(new Dimension(400, 220));
        }
        int result = DialogUtils.showComponentOKCancelDialog(requester, structureMappingPanel, "Assign 'From' structures to 'To' structures");
        if (result != JOptionPane.OK_OPTION) {
            throw UserCancelException.CANCEL_GENERIC;
        }
    } while (structureMappingPanel.hasErrors());
    for (Map.Entry<Structure, JComboBox<String>> entry : structureMappingPanel.getStructureMap().entrySet()) {
        if (entry.getValue().getSelectedItem().equals(StructurePasteMappingPanel.MAKE_NEW)) {
            // we generate a "to" structure name based on the "from" name
            String newNameTo = entry.getKey().getName();
            while (modelTo.getStructure(newNameTo) != null) {
                newNameTo = org.vcell.util.TokenMangler.getNextEnumeratedToken(newNameTo);
                for (Structure sFrom : rsCopy.getStructuresArr()) {
                    if (newNameTo.equals(sFrom.getName())) {
                        // the new name must not match any existing "from" name either
                        newNameTo = org.vcell.util.TokenMangler.getNextEnumeratedToken(newNameTo);
                        break;
                    }
                }
            }
            try {
                // as to avoid risk of duplicates / conflicting names
                if (entry.getKey() instanceof Membrane) {
                    modelTo.addMembrane(newNameTo);
                } else {
                    modelTo.addFeature(newNameTo);
                }
            } catch (ModelException | PropertyVetoException e) {
                throw new RuntimeException("Failed to generate the missing 'from' Structures in the cloned model, " + e.getMessage());
            }
            fullyMappedStructures.put(entry.getKey(), newNameTo);
        } else {
            // name of an existing "to" structure
            fullyMappedStructures.put(entry.getKey(), (String) entry.getValue().getSelectedItem());
        }
    }
    return fullyMappedStructures;
}
Also used : Issue(org.vcell.util.Issue) JComboBox(javax.swing.JComboBox) ModelException(cbit.vcell.model.ModelException) Dimension(java.awt.Dimension) LinkedHashMap(java.util.LinkedHashMap) PropertyVetoException(java.beans.PropertyVetoException) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) Vector(java.util.Vector) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Membrane (cbit.vcell.model.Membrane)77 Feature (cbit.vcell.model.Feature)50 Structure (cbit.vcell.model.Structure)47 Expression (cbit.vcell.parser.Expression)31 SpeciesContext (cbit.vcell.model.SpeciesContext)25 MembraneMapping (cbit.vcell.mapping.MembraneMapping)20 StructureTopology (cbit.vcell.model.Model.StructureTopology)19 ExpressionException (cbit.vcell.parser.ExpressionException)18 PropertyVetoException (java.beans.PropertyVetoException)17 FluxReaction (cbit.vcell.model.FluxReaction)16 Model (cbit.vcell.model.Model)16 ReactionStep (cbit.vcell.model.ReactionStep)16 SimpleReaction (cbit.vcell.model.SimpleReaction)16 ArrayList (java.util.ArrayList)14 StructureMapping (cbit.vcell.mapping.StructureMapping)12 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)12 ReactionParticipant (cbit.vcell.model.ReactionParticipant)12 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)12 SubVolume (cbit.vcell.geometry.SubVolume)11 SurfaceClass (cbit.vcell.geometry.SurfaceClass)11