Search in sources :

Example 6 with Diagram

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

the class ReactionCartoonTool method addStructure.

private void addStructure(boolean bMembrane, RXContainerDropTargetInfo selectedContainerDropTargetInfo) {
    Integer insertFlag = selectedContainerDropTargetInfo.insertFlag;
    try {
        Structure myStructure = null;
        if (bMembrane) {
            myStructure = ReactionCartoonTool.this.getModel().createMembrane();
        } else {
            myStructure = ReactionCartoonTool.this.getModel().createFeature();
        }
        ArrayList<Structure> diagramStructures = new ArrayList<Structure>(ReactionCartoonTool.this.getReactionCartoon().getStructureSuite().getStructures());
        diagramStructures.remove(myStructure);
        if (new Integer(RXContainerDropTargetInfo.INSERT_BEGINNING).equals(insertFlag)) {
            diagramStructures.add(0, myStructure);
        } else if (new Integer(RXContainerDropTargetInfo.INSERT_END).equals(insertFlag)) {
            diagramStructures.add(myStructure);
        } else {
            diagramStructures.add(diagramStructures.indexOf(selectedContainerDropTargetInfo.dropShape.getStructure()), myStructure);
        }
        ((AllStructureSuite) getReactionCartoon().getStructureSuite()).setModelStructureOrder(true);
        // ReactionCartoonTool.this.getModel().setStructures(structures.toArray(new Structure[0]));
        ArrayList<Diagram> newDiagramOrderList = new ArrayList<Diagram>();
        for (Structure structure : diagramStructures) {
            newDiagramOrderList.add(getModel().getDiagram(structure));
        }
        getModel().setDiagrams(newDiagramOrderList.toArray(new Diagram[0]));
        lastRXContainerDropTargetInfoMap = updateRXContainerDropTargetInfoMap(new Point(-1, -1));
        resetDropTargets(false, mode == Mode.STRUCTURE);
        getGraphPane().repaint();
    } catch (Exception e2) {
        e2.printStackTrace();
        DialogUtils.showErrorDialog(getGraphPane(), "Error adding structure: " + e2.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) AllStructureSuite(cbit.vcell.graph.structures.AllStructureSuite) Point(java.awt.Point) Structure(cbit.vcell.model.Structure) PropertyVetoException(java.beans.PropertyVetoException) UtilCancelException(org.vcell.util.UtilCancelException) ExpressionException(cbit.vcell.parser.ExpressionException) UserCancelException(org.vcell.util.UserCancelException) Diagram(cbit.vcell.model.Diagram)

Example 7 with Diagram

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

the class ReactionCartoonFull method refreshAll.

@Override
protected void refreshAll(boolean reallocateShapes) {
    try {
        if (getModel() == null || getStructureSuite() == null) {
            return;
        }
        System.out.println("ReactionCartoonFull, RefreshAll()");
        for (Structure structure : structureSuite.getStructures()) {
            Diagram diagram = getModel().getDiagram(structure);
            if (diagram != null) {
                // Maintain consistency between rule participant nodes, signatures and
                // species pattern when a molecule is being modified.
                rebindAll(diagram);
            }
        }
        // calculate species context weight (number of reactions for which it's a participant)
        Map<SpeciesContext, Integer> scWeightMap = new HashMap<>();
        // all the species contexts that are catalysts
        Set<SpeciesContext> scCatalystSet = new HashSet<>();
        // calculate species context length (number of species patterns it contains, 1 if has no species patterns)
        for (ReactionStep rs : getModel().getReactionSteps()) {
            ReactionParticipant[] rpList = rs.getReactionParticipants();
            for (int i = 0; i < rpList.length; i++) {
                ReactionParticipant rp = rpList[i];
                SpeciesContext sc = rp.getSpeciesContext();
                // int increment = rp.getStoichiometry();
                int increment = 1;
                if (rp instanceof Catalyst) {
                    scCatalystSet.add(sc);
                }
                if (scWeightMap.containsKey(sc)) {
                    int weight = scWeightMap.get(sc);
                    weight += increment;
                    scWeightMap.put(sc, weight);
                } else {
                    scWeightMap.put(sc, increment);
                }
            }
        }
        Set<Shape> unwantedShapes = new HashSet<Shape>();
        Set<RuleParticipantSignature> unwantedSignatures = new HashSet<RuleParticipantSignature>();
        unwantedShapes.addAll(getShapes());
        unwantedSignatures.addAll(ruleParticipantSignatures);
        ContainerContainerShape containerShape = (ContainerContainerShape) getShapeFromModelObject(getModel());
        List<ReactionContainerShape> reactionContainerShapeList = new ArrayList<ReactionContainerShape>();
        List<Structure> structureList = new ArrayList<Structure>(getStructureSuite().getStructures());
        // create all ReactionContainerShapes (one for each Structure)
        for (Structure structure : structureList) {
            if (structure instanceof Membrane) {
                Membrane membrane = (Membrane) structure;
                ReactionContainerShape membraneShape = (ReactionContainerShape) getShapeFromModelObject(membrane);
                if (membraneShape == null) {
                    membraneShape = new ReactionContainerShape(membrane, structureSuite, this);
                    addShape(membraneShape);
                    membrane.getMembraneVoltage().removePropertyChangeListener(this);
                    membrane.getMembraneVoltage().addPropertyChangeListener(this);
                } else {
                    membraneShape.setStructureSuite(structureSuite);
                }
                membrane.removePropertyChangeListener(this);
                membrane.addPropertyChangeListener(this);
                membraneShape.refreshLabel();
                unwantedShapes.remove(membraneShape);
                reactionContainerShapeList.add(membraneShape);
            } else if (structure instanceof Feature) {
                Feature feature = (Feature) structure;
                ReactionContainerShape featureShape = (ReactionContainerShape) getShapeFromModelObject(feature);
                if (featureShape == null) {
                    featureShape = new ReactionContainerShape(feature, structureSuite, this);
                    addShape(featureShape);
                } else {
                    featureShape.setStructureSuite(structureSuite);
                }
                feature.removePropertyChangeListener(this);
                feature.addPropertyChangeListener(this);
                featureShape.refreshLabel();
                unwantedShapes.remove(featureShape);
                reactionContainerShapeList.add(featureShape);
            }
        }
        if (containerShape == null) {
            containerShape = new ContainerContainerShape(this, getModel(), reactionContainerShapeList);
            addShape(containerShape);
        } else {
            containerShape.setReactionContainerShapeList(reactionContainerShapeList);
        }
        containerShape.refreshLabel();
        unwantedShapes.remove(containerShape);
        // add all species context shapes within the structures
        for (Structure structure : getStructureSuite().getStructures()) {
            ReactionContainerShape reactionContainerShape = (ReactionContainerShape) getShapeFromModelObject(structure);
            structure.removePropertyChangeListener(this);
            structure.addPropertyChangeListener(this);
            for (SpeciesContext structSpeciesContext : getModel().getSpeciesContexts(structure)) {
                SpeciesContextShape ss = (SpeciesContextShape) getShapeFromModelObject(structSpeciesContext);
                if (ss == null) {
                    ss = new SpeciesContextShape(structSpeciesContext, this);
                    ss.truncateLabelName(false);
                    structSpeciesContext.getSpecies().removePropertyChangeListener(this);
                    structSpeciesContext.getSpecies().addPropertyChangeListener(this);
                    reactionContainerShape.addChildShape(ss);
                    addShape(ss);
                    ss.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
                }
                if (speciesSizeOption == SpeciesSizeOptions.weight) {
                    // this number sets the diameter of the shape
                    Integer weight = scWeightMap.get(structSpeciesContext);
                    if (weight != null) {
                        // we cap the diameter of the shape to something reasonable
                        weight = Math.min(weight, 16);
                    }
                    ss.setFilters(highlightCatalystOption ? scCatalystSet.contains(structSpeciesContext) : false, weight);
                } else if (speciesSizeOption == SpeciesSizeOptions.length) {
                    Integer length = null;
                    if (structSpeciesContext.getSpeciesPattern() != null && !structSpeciesContext.getSpeciesPattern().getMolecularTypePatterns().isEmpty()) {
                        length = structSpeciesContext.getSpeciesPattern().getMolecularTypePatterns().size() * 2;
                        length = Math.min(length, 16);
                    }
                    ss.setFilters(highlightCatalystOption ? scCatalystSet.contains(structSpeciesContext) : false, length);
                } else {
                    ss.setFilters(highlightCatalystOption ? scCatalystSet.contains(structSpeciesContext) : false, null);
                }
                structSpeciesContext.removePropertyChangeListener(this);
                structSpeciesContext.addPropertyChangeListener(this);
                ss.refreshLabel();
                unwantedShapes.remove(ss);
            }
        }
        // add all reactionSteps that are in this structure (ReactionContainerShape), and draw the lines
        getModel().removePropertyChangeListener(this);
        getModel().addPropertyChangeListener(this);
        // 
        for (ReactionRule rr : getModel().getRbmModelContainer().getReactionRuleList()) {
            rr.removePropertyChangeListener(this);
            rr.addPropertyChangeListener(this);
            Structure structure = rr.getStructure();
            if (getStructureSuite().areReactionsShownFor(structure)) {
                ReactionContainerShape reactionContainerShape = (ReactionContainerShape) getShapeFromModelObject(structure);
                ReactionRuleFullDiagramShape rrShape = (ReactionRuleFullDiagramShape) getShapeFromModelObject(rr);
                if (rrShape == null) {
                    rrShape = new ReactionRuleFullDiagramShape(rr, this);
                    addShape(rrShape);
                    rrShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
                    reactionContainerShape.addChildShape(rrShape);
                    rrShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
                }
                rrShape.refreshLabel();
                unwantedShapes.remove(rrShape);
                // 
                // add reaction participants as edges and SignatureShapes as needed
                // 
                List<ReactionRuleParticipant> participants = rr.getReactionRuleParticipants();
                List<RuleParticipantEdgeDiagramShape> ruleEdges = new ArrayList<>();
                for (ReactionRuleParticipant participant : participants) {
                    participant.getSpeciesPattern().removePropertyChangeListener(this);
                    participant.getSpeciesPattern().addPropertyChangeListener(this);
                    Structure speciesStructure = participant.getStructure();
                    Structure reactionStructure = rr.getStructure();
                    if (getStructureSuite().getStructures().contains(speciesStructure) && getStructureSuite().areReactionsShownFor(reactionStructure)) {
                        // 
                        // find existing RuleParticipantSignatureShape in cartoon
                        // 
                        RuleParticipantLongSignature ruleParticipantLongSignature = null;
                        for (RuleParticipantSignature signature : ruleParticipantSignatures) {
                            if (signature instanceof RuleParticipantLongSignature && signature.getStructure() == participant.getStructure() && signature.compareByCriteria(participant.getSpeciesPattern(), GroupingCriteria.full)) {
                                ruleParticipantLongSignature = (RuleParticipantLongSignature) signature;
                                break;
                            }
                        }
                        for (RuleParticipantSignature signature : ruleParticipantSignatures) {
                            if (signature instanceof RuleParticipantShortSignature && signature.getStructure() == participant.getStructure()) {
                                System.out.println("ReactionCartoonFull, refreshAll(), RuleParticipantShortSignature");
                            }
                        }
                        // 
                        // if didn't find signature in cartoons list of signatures, then create one (and create a shape for it).
                        // 
                        RuleParticipantSignatureFullDiagramShape signatureShape = null;
                        if (ruleParticipantLongSignature == null) {
                            ruleParticipantLongSignature = RuleParticipantLongSignature.fromReactionRuleParticipant(participant, this);
                            ruleParticipantSignatures.add(ruleParticipantLongSignature);
                            signatureShape = new RuleParticipantSignatureFullDiagramShape(ruleParticipantLongSignature, this);
                            addShape(signatureShape);
                            ReactionContainerShape participantContainerShape = (ReactionContainerShape) getShapeFromModelObject(participant.getStructure());
                            signatureShape.getSpaceManager().setRelPos(participantContainerShape.getRandomPosition());
                            participantContainerShape.addChildShape(signatureShape);
                            signatureShape.getSpaceManager().setRelPos(participantContainerShape.getRandomPosition());
                        } else {
                            signatureShape = (RuleParticipantSignatureFullDiagramShape) getShapeFromModelObject(ruleParticipantLongSignature);
                        }
                        unwantedShapes.remove(signatureShape);
                        unwantedSignatures.remove(ruleParticipantLongSignature);
                        signatureShape.refreshLabel();
                        signatureShape.setVisible(true);
                        // 
                        // add edge for ReactionRuleParticipant if not already present.
                        // 
                        RuleParticipantEdgeDiagramShape ruleParticipantShape = (RuleParticipantEdgeDiagramShape) getShapeFromModelObject(participant);
                        if (ruleParticipantShape == null || ruleParticipantShape.getRuleParticipantSignatureShape() != signatureShape) {
                            if (participant instanceof ReactantPattern && signatureShape.isVisible()) {
                                ruleParticipantShape = new ReactantPatternEdgeDiagramShape((ReactantPattern) participant, rrShape, signatureShape, this);
                            } else if (participant instanceof ProductPattern && signatureShape.isVisible()) {
                                ruleParticipantShape = new ProductPatternEdgeDiagramShape((ProductPattern) participant, rrShape, signatureShape, this);
                            } else {
                                throw new RuntimeException("unsupported ReactionRuleParticipant " + participant.getClass());
                            }
                            addShape(ruleParticipantShape);
                        }
                        if (!containerShape.getChildren().contains(ruleParticipantShape)) {
                            containerShape.addChildShape(ruleParticipantShape);
                        }
                        unwantedShapes.remove(ruleParticipantShape);
                        ruleParticipantShape.refreshLabel();
                        // all the edges for this rule
                        ruleEdges.add(ruleParticipantShape);
                    }
                }
                // a product edge (a closed loop) between the rule diagram shape and the signature diagram shape
                for (RuleParticipantEdgeDiagramShape ours : ruleEdges) {
                    // reset them all
                    ours.setSibling(false);
                }
                for (RuleParticipantEdgeDiagramShape ours : ruleEdges) {
                    for (RuleParticipantEdgeDiagramShape theirs : ruleEdges) {
                        if (ours == theirs) {
                            // don't compare with self
                            continue;
                        }
                        if (ours.getRuleParticipantSignatureShape() == theirs.getRuleParticipantSignatureShape()) {
                            ours.setSibling(true);
                            theirs.setSibling(true);
                        }
                    }
                }
            }
        }
        ruleParticipantSignatures.removeAll(unwantedSignatures);
        for (ReactionStep reactionStep : getModel().getReactionSteps()) {
            reactionStep.removePropertyChangeListener(this);
            reactionStep.addPropertyChangeListener(this);
            Structure structure = reactionStep.getStructure();
            if (getStructureSuite().areReactionsShownFor(structure)) {
                ReactionContainerShape reactionContainerShape = (ReactionContainerShape) getShapeFromModelObject(structure);
                if (reactionContainerShape == null) {
                    System.out.println("Reaction container shape is null for structure " + structure + " for reaction step " + reactionStep);
                }
                ReactionStepShape reactionStepShape = (ReactionStepShape) getShapeFromModelObject(reactionStep);
                if (reactionStepShape == null) {
                    if (reactionStep instanceof SimpleReaction) {
                        reactionStepShape = new SimpleReactionShape((SimpleReaction) reactionStep, this);
                    } else if (reactionStep instanceof FluxReaction) {
                        reactionStepShape = new FluxReactionShape((FluxReaction) reactionStep, this);
                    } else {
                        throw new RuntimeException("unknown type of ReactionStep '" + reactionStep.getClass().toString());
                    }
                    addShape(reactionStepShape);
                    reactionStepShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
                    reactionContainerShape.addChildShape(reactionStepShape);
                    reactionStepShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
                }
                reactionStepShape.refreshLabel();
                unwantedShapes.remove(reactionStepShape);
                // add reaction participants as edges
                for (ReactionParticipant participant : reactionStep.getReactionParticipants()) {
                    participant.removePropertyChangeListener(this);
                    participant.addPropertyChangeListener(this);
                    Structure speciesStructure = participant.getStructure();
                    Structure reactionStructure = reactionStep.getStructure();
                    if (getStructureSuite().getStructures().contains(speciesStructure) && getStructureSuite().areReactionsShownFor(reactionStructure)) {
                        SpeciesContext speciesContext = getModel().getSpeciesContext(participant.getSpecies(), speciesStructure);
                        // add speciesContextShapes that are not in this structure, but are referenced from the reactionParticipants
                        // these are only when reactionParticipants are from features that are outside of the membrane being displayed
                        SpeciesContextShape speciesContextShape = (SpeciesContextShape) getShapeFromModelObject(speciesContext);
                        if (speciesContextShape == null) {
                            speciesContextShape = new SpeciesContextShape(speciesContext, this);
                            speciesContextShape.truncateLabelName(false);
                            reactionContainerShape.addChildShape(speciesContextShape);
                            addShape(speciesContextShape);
                            speciesContextShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
                        }
                        speciesContextShape.refreshLabel();
                        unwantedShapes.remove(speciesContextShape);
                        ReactionParticipantShape reactionParticipantShape = (ReactionParticipantShape) getShapeFromModelObject(participant);
                        if (reactionParticipantShape == null) {
                            if (participant instanceof Reactant) {
                                reactionParticipantShape = new ReactantShape((Reactant) participant, reactionStepShape, speciesContextShape, this);
                            } else if (participant instanceof Product) {
                                reactionParticipantShape = new ProductShape((Product) participant, reactionStepShape, speciesContextShape, this);
                            } else if (participant instanceof Catalyst) {
                                reactionParticipantShape = new CatalystShape((Catalyst) participant, reactionStepShape, speciesContextShape, this);
                            } else {
                                throw new RuntimeException("unsupported ReactionParticipant " + participant.getClass());
                            }
                            addShape(reactionParticipantShape);
                        }
                        if (!containerShape.getChildren().contains(reactionParticipantShape)) {
                            containerShape.addChildShape(reactionParticipantShape);
                        }
                        unwantedShapes.remove(reactionParticipantShape);
                        reactionParticipantShape.refreshLabel();
                    }
                }
            }
        }
        for (Shape unwantedShape : unwantedShapes) {
            removeShape(unwantedShape);
        }
        // update diagrams
        for (Structure structure : structureSuite.getStructures()) {
            Diagram diagram = getModel().getDiagram(structure);
            if (diagram != null) {
                applyDefaults(diagram);
            }
        }
        fireGraphChanged(new GraphEvent(this));
    } catch (Throwable e) {
        handleException(e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(cbit.vcell.model.Product) FluxReaction(cbit.vcell.model.FluxReaction) SpeciesContext(cbit.vcell.model.SpeciesContext) Feature(cbit.vcell.model.Feature) Reactant(cbit.vcell.model.Reactant) HashSet(java.util.HashSet) ReactionStep(cbit.vcell.model.ReactionStep) ReactionRuleParticipant(cbit.vcell.model.ReactionRuleParticipant) ReactionParticipant(cbit.vcell.model.ReactionParticipant) Catalyst(cbit.vcell.model.Catalyst) RuleParticipantSignature(cbit.vcell.model.RuleParticipantSignature) Shape(cbit.gui.graph.Shape) GraphEvent(cbit.gui.graph.GraphEvent) RuleParticipantShortSignature(cbit.vcell.model.RuleParticipantShortSignature) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) ReactantPattern(cbit.vcell.model.ReactantPattern) RuleParticipantLongSignature(cbit.vcell.model.RuleParticipantLongSignature) SimpleReaction(cbit.vcell.model.SimpleReaction) ReactionRule(cbit.vcell.model.ReactionRule) ProductPattern(cbit.vcell.model.ProductPattern) Point(java.awt.Point) Diagram(cbit.vcell.model.Diagram)

Example 8 with Diagram

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

the class ReactionCartoonMolecule method refreshAll.

@Override
protected void refreshAll(boolean reallocateShapes) {
    try {
        if (getModel() == null || getStructureSuite() == null) {
            return;
        }
        System.out.println("ReactionCartoonMolecule, RefreshAll()");
        for (Structure structure : structureSuite.getStructures()) {
            Diagram diagram = getModel().getDiagram(structure);
            if (diagram != null) {
                // Maintain consistency between rule participant nodes, signatures and
                // species pattern when a molecule is being modified.
                rebindAll(diagram);
            }
        }
        // calculate species context weight (number of reactions for which it's a participant)
        Map<SpeciesContext, Integer> scWeightMap = new HashMap<>();
        // all the species contexts that are catalysts
        Set<SpeciesContext> scCatalystSet = new HashSet<>();
        // calculate species context length (number of species patterns it contains, 1 if has no species patterns)
        for (ReactionStep rs : getModel().getReactionSteps()) {
            ReactionParticipant[] rpList = rs.getReactionParticipants();
            for (int i = 0; i < rpList.length; i++) {
                ReactionParticipant rp = rpList[i];
                SpeciesContext sc = rp.getSpeciesContext();
                int increment = 1;
                if (rp instanceof Catalyst) {
                    scCatalystSet.add(sc);
                }
                if (scWeightMap.containsKey(sc)) {
                    int weight = scWeightMap.get(sc);
                    weight += increment;
                    scWeightMap.put(sc, weight);
                } else {
                    scWeightMap.put(sc, increment);
                }
            }
        }
        Set<Shape> unwantedShapes = new HashSet<Shape>();
        Set<RuleParticipantSignature> unwantedSignatures = new HashSet<RuleParticipantSignature>();
        unwantedShapes.addAll(getShapes());
        unwantedSignatures.addAll(ruleParticipantSignatures);
        ContainerContainerShape containerShape = (ContainerContainerShape) getShapeFromModelObject(getModel());
        List<ReactionContainerShape> reactionContainerShapeList = new ArrayList<ReactionContainerShape>();
        List<Structure> structureList = new ArrayList<Structure>(getStructureSuite().getStructures());
        // create all ReactionContainerShapes (one for each Structure)
        for (Structure structure : structureList) {
            if (structure instanceof Membrane) {
                Membrane membrane = (Membrane) structure;
                ReactionContainerShape membraneShape = (ReactionContainerShape) getShapeFromModelObject(membrane);
                if (membraneShape == null) {
                    membraneShape = new ReactionContainerShape(membrane, structureSuite, this);
                    addShape(membraneShape);
                    membrane.getMembraneVoltage().removePropertyChangeListener(this);
                    membrane.getMembraneVoltage().addPropertyChangeListener(this);
                } else {
                    membraneShape.setStructureSuite(structureSuite);
                }
                membrane.removePropertyChangeListener(this);
                membrane.addPropertyChangeListener(this);
                membraneShape.refreshLabel();
                unwantedShapes.remove(membraneShape);
                reactionContainerShapeList.add(membraneShape);
            } else if (structure instanceof Feature) {
                Feature feature = (Feature) structure;
                ReactionContainerShape featureShape = (ReactionContainerShape) getShapeFromModelObject(feature);
                if (featureShape == null) {
                    featureShape = new ReactionContainerShape(feature, structureSuite, this);
                    addShape(featureShape);
                } else {
                    featureShape.setStructureSuite(structureSuite);
                }
                feature.removePropertyChangeListener(this);
                feature.addPropertyChangeListener(this);
                featureShape.refreshLabel();
                unwantedShapes.remove(featureShape);
                reactionContainerShapeList.add(featureShape);
            }
        }
        if (containerShape == null) {
            containerShape = new ContainerContainerShape(this, getModel(), reactionContainerShapeList);
            addShape(containerShape);
        } else {
            containerShape.setReactionContainerShapeList(reactionContainerShapeList);
        }
        containerShape.refreshLabel();
        unwantedShapes.remove(containerShape);
        // add all species context shapes within the structures
        for (Structure structure : getStructureSuite().getStructures()) {
            ReactionContainerShape reactionContainerShape = (ReactionContainerShape) getShapeFromModelObject(structure);
            structure.removePropertyChangeListener(this);
            structure.addPropertyChangeListener(this);
            for (SpeciesContext structSpeciesContext : getModel().getSpeciesContexts(structure)) {
                SpeciesContextShape ss = (SpeciesContextShape) getShapeFromModelObject(structSpeciesContext);
                if (ss == null) {
                    ss = new SpeciesContextShape(structSpeciesContext, this);
                    ss.truncateLabelName(false);
                    structSpeciesContext.getSpecies().removePropertyChangeListener(this);
                    structSpeciesContext.getSpecies().addPropertyChangeListener(this);
                    reactionContainerShape.addChildShape(ss);
                    addShape(ss);
                    ss.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
                }
                if (speciesSizeOption == SpeciesSizeOptions.weight) {
                    // this number sets the diameter of the shape
                    Integer weight = scWeightMap.get(structSpeciesContext);
                    if (weight != null) {
                        // we cap the diameter of the shape to something reasonable
                        weight = Math.min(weight, 16);
                    }
                    ss.setFilters(highlightCatalystOption ? scCatalystSet.contains(structSpeciesContext) : false, weight);
                } else if (speciesSizeOption == SpeciesSizeOptions.length) {
                    Integer length = null;
                    if (structSpeciesContext.getSpeciesPattern() != null && !structSpeciesContext.getSpeciesPattern().getMolecularTypePatterns().isEmpty()) {
                        length = structSpeciesContext.getSpeciesPattern().getMolecularTypePatterns().size() * 2;
                        length = Math.min(length, 16);
                    }
                    ss.setFilters(highlightCatalystOption ? scCatalystSet.contains(structSpeciesContext) : false, length);
                } else {
                    ss.setFilters(highlightCatalystOption ? scCatalystSet.contains(structSpeciesContext) : false, null);
                }
                structSpeciesContext.removePropertyChangeListener(this);
                structSpeciesContext.addPropertyChangeListener(this);
                ss.refreshLabel();
                unwantedShapes.remove(ss);
            }
        }
        // add all reactionSteps that are in this structure (ReactionContainerShape), and draw the lines
        getModel().removePropertyChangeListener(this);
        getModel().addPropertyChangeListener(this);
        // 
        for (ReactionRule rr : getModel().getRbmModelContainer().getReactionRuleList()) {
            rr.removePropertyChangeListener(this);
            rr.addPropertyChangeListener(this);
            Structure structure = rr.getStructure();
            if (getStructureSuite().areReactionsShownFor(structure)) {
                ReactionContainerShape reactionContainerShape = (ReactionContainerShape) getShapeFromModelObject(structure);
                ReactionRuleFullDiagramShape rrShape = (ReactionRuleFullDiagramShape) getShapeFromModelObject(rr);
                if (rrShape == null) {
                    rrShape = new ReactionRuleFullDiagramShape(rr, this);
                    addShape(rrShape);
                    rrShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
                    reactionContainerShape.addChildShape(rrShape);
                    rrShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
                }
                rrShape.refreshLabel();
                unwantedShapes.remove(rrShape);
                // 
                // add reaction participants as edges and SignatureShapes as needed
                // 
                List<ReactionRuleParticipant> participants = rr.getReactionRuleParticipants();
                List<RuleParticipantEdgeDiagramShape> ruleEdges = new ArrayList<>();
                for (ReactionRuleParticipant participant : participants) {
                    participant.getSpeciesPattern().removePropertyChangeListener(this);
                    participant.getSpeciesPattern().addPropertyChangeListener(this);
                    Structure speciesStructure = participant.getStructure();
                    Structure reactionStructure = rr.getStructure();
                    if (getStructureSuite().getStructures().contains(speciesStructure) && getStructureSuite().areReactionsShownFor(reactionStructure)) {
                        // 
                        // find existing RuleParticipantSignatureShape in cartoon
                        // 
                        RuleParticipantShortSignature ruleParticipantShortSignature = null;
                        for (RuleParticipantSignature signature : ruleParticipantSignatures) {
                            if (signature instanceof RuleParticipantLongSignature && signature.getStructure() == participant.getStructure()) {
                                System.out.println("ReactionCartoonMolecule, refreshAll(), RuleParticipantLongSignature");
                                break;
                            }
                        }
                        for (RuleParticipantSignature signature : ruleParticipantSignatures) {
                            if (signature instanceof RuleParticipantShortSignature && signature.getStructure() == participant.getStructure() && signature.compareByCriteria(participant.getSpeciesPattern(), GroupingCriteria.molecule)) {
                                ruleParticipantShortSignature = (RuleParticipantShortSignature) signature;
                                break;
                            }
                        }
                        // 
                        // if didn't find signature in cartoons list of signatures, then create one (and create a shape for it).
                        // 
                        RuleParticipantSignatureShortDiagramShape signatureShape = null;
                        if (ruleParticipantShortSignature == null) {
                            ruleParticipantShortSignature = RuleParticipantShortSignature.fromReactionRuleParticipant(participant, this);
                            ruleParticipantSignatures.add(ruleParticipantShortSignature);
                            signatureShape = new RuleParticipantSignatureShortDiagramShape(ruleParticipantShortSignature, this);
                            addShape(signatureShape);
                            ReactionContainerShape participantContainerShape = (ReactionContainerShape) getShapeFromModelObject(participant.getStructure());
                            signatureShape.getSpaceManager().setRelPos(participantContainerShape.getRandomPosition());
                            participantContainerShape.addChildShape(signatureShape);
                            signatureShape.getSpaceManager().setRelPos(participantContainerShape.getRandomPosition());
                        } else {
                            signatureShape = (RuleParticipantSignatureShortDiagramShape) getShapeFromModelObject(ruleParticipantShortSignature);
                        }
                        unwantedShapes.remove(signatureShape);
                        unwantedSignatures.remove(ruleParticipantShortSignature);
                        signatureShape.refreshLabel();
                        signatureShape.setVisible(true);
                        // 
                        // add edge for ReactionRuleParticipant if not already present.
                        // 
                        RuleParticipantEdgeDiagramShape ruleParticipantShape = (RuleParticipantEdgeDiagramShape) getShapeFromModelObject(participant);
                        if (ruleParticipantShape == null || ruleParticipantShape.getRuleParticipantSignatureShape() != signatureShape) {
                            if (participant instanceof ReactantPattern && signatureShape.isVisible()) {
                                ruleParticipantShape = new ReactantPatternEdgeDiagramShape((ReactantPattern) participant, rrShape, signatureShape, this);
                            } else if (participant instanceof ProductPattern && signatureShape.isVisible()) {
                                ruleParticipantShape = new ProductPatternEdgeDiagramShape((ProductPattern) participant, rrShape, signatureShape, this);
                            } else {
                                throw new RuntimeException("unsupported ReactionRuleParticipant " + participant.getClass());
                            }
                            addShape(ruleParticipantShape);
                        }
                        if (!containerShape.getChildren().contains(ruleParticipantShape)) {
                            containerShape.addChildShape(ruleParticipantShape);
                        }
                        unwantedShapes.remove(ruleParticipantShape);
                        ruleParticipantShape.refreshLabel();
                        // all the edges for this rule
                        ruleEdges.add(ruleParticipantShape);
                    }
                }
                // a product edge (a closed loop) between the rule diagram shape and the signature diagram shape
                for (RuleParticipantEdgeDiagramShape ours : ruleEdges) {
                    // reset them all
                    ours.setSibling(false);
                }
                for (RuleParticipantEdgeDiagramShape ours : ruleEdges) {
                    for (RuleParticipantEdgeDiagramShape theirs : ruleEdges) {
                        if (ours == theirs) {
                            // don't compare with self
                            continue;
                        }
                        if (ours.getRuleParticipantSignatureShape() == theirs.getRuleParticipantSignatureShape()) {
                            ours.setSibling(true);
                            theirs.setSibling(true);
                        }
                    }
                }
            }
        }
        ruleParticipantSignatures.removeAll(unwantedSignatures);
        for (ReactionStep reactionStep : getModel().getReactionSteps()) {
            reactionStep.removePropertyChangeListener(this);
            reactionStep.addPropertyChangeListener(this);
            Structure structure = reactionStep.getStructure();
            if (getStructureSuite().areReactionsShownFor(structure)) {
                ReactionContainerShape reactionContainerShape = (ReactionContainerShape) getShapeFromModelObject(structure);
                if (reactionContainerShape == null) {
                    System.out.println("Reaction container shape is null for structure " + structure + " for reaction step " + reactionStep);
                }
                ReactionStepShape reactionStepShape = (ReactionStepShape) getShapeFromModelObject(reactionStep);
                if (reactionStepShape == null) {
                    if (reactionStep instanceof SimpleReaction) {
                        reactionStepShape = new SimpleReactionShape((SimpleReaction) reactionStep, this);
                    } else if (reactionStep instanceof FluxReaction) {
                        reactionStepShape = new FluxReactionShape((FluxReaction) reactionStep, this);
                    } else {
                        throw new RuntimeException("unknown type of ReactionStep '" + reactionStep.getClass().toString());
                    }
                    addShape(reactionStepShape);
                    reactionStepShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
                    reactionContainerShape.addChildShape(reactionStepShape);
                    reactionStepShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
                }
                reactionStepShape.refreshLabel();
                unwantedShapes.remove(reactionStepShape);
                // add reaction participants as edges
                for (ReactionParticipant participant : reactionStep.getReactionParticipants()) {
                    participant.removePropertyChangeListener(this);
                    participant.addPropertyChangeListener(this);
                    Structure speciesStructure = participant.getStructure();
                    Structure reactionStructure = reactionStep.getStructure();
                    if (getStructureSuite().getStructures().contains(speciesStructure) && getStructureSuite().areReactionsShownFor(reactionStructure)) {
                        SpeciesContext speciesContext = getModel().getSpeciesContext(participant.getSpecies(), speciesStructure);
                        // add speciesContextShapes that are not in this structure, but are referenced from the reactionParticipants
                        // these are only when reactionParticipants are from features that are outside of the membrane being displayed
                        SpeciesContextShape speciesContextShape = (SpeciesContextShape) getShapeFromModelObject(speciesContext);
                        if (speciesContextShape == null) {
                            speciesContextShape = new SpeciesContextShape(speciesContext, this);
                            speciesContextShape.truncateLabelName(false);
                            reactionContainerShape.addChildShape(speciesContextShape);
                            addShape(speciesContextShape);
                            speciesContextShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
                        }
                        speciesContextShape.refreshLabel();
                        unwantedShapes.remove(speciesContextShape);
                        ReactionParticipantShape reactionParticipantShape = (ReactionParticipantShape) getShapeFromModelObject(participant);
                        if (reactionParticipantShape == null) {
                            if (participant instanceof Reactant) {
                                reactionParticipantShape = new ReactantShape((Reactant) participant, reactionStepShape, speciesContextShape, this);
                            } else if (participant instanceof Product) {
                                reactionParticipantShape = new ProductShape((Product) participant, reactionStepShape, speciesContextShape, this);
                            } else if (participant instanceof Catalyst) {
                                reactionParticipantShape = new CatalystShape((Catalyst) participant, reactionStepShape, speciesContextShape, this);
                            } else {
                                throw new RuntimeException("unsupported ReactionParticipant " + participant.getClass());
                            }
                            addShape(reactionParticipantShape);
                        }
                        if (!containerShape.getChildren().contains(reactionParticipantShape)) {
                            containerShape.addChildShape(reactionParticipantShape);
                        }
                        unwantedShapes.remove(reactionParticipantShape);
                        reactionParticipantShape.refreshLabel();
                    }
                }
            }
        }
        for (Shape unwantedShape : unwantedShapes) {
            removeShape(unwantedShape);
        }
        // update diagrams
        for (Structure structure : structureSuite.getStructures()) {
            Diagram diagram = getModel().getDiagram(structure);
            if (diagram != null) {
                applyDefaults(diagram);
            }
        }
        fireGraphChanged(new GraphEvent(this));
    } catch (Throwable e) {
        handleException(e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(cbit.vcell.model.Product) FluxReaction(cbit.vcell.model.FluxReaction) SpeciesContext(cbit.vcell.model.SpeciesContext) Feature(cbit.vcell.model.Feature) Reactant(cbit.vcell.model.Reactant) HashSet(java.util.HashSet) ReactionStep(cbit.vcell.model.ReactionStep) ReactionRuleParticipant(cbit.vcell.model.ReactionRuleParticipant) ReactionParticipant(cbit.vcell.model.ReactionParticipant) Catalyst(cbit.vcell.model.Catalyst) RuleParticipantSignature(cbit.vcell.model.RuleParticipantSignature) Shape(cbit.gui.graph.Shape) GraphEvent(cbit.gui.graph.GraphEvent) RuleParticipantShortSignature(cbit.vcell.model.RuleParticipantShortSignature) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) ReactantPattern(cbit.vcell.model.ReactantPattern) RuleParticipantLongSignature(cbit.vcell.model.RuleParticipantLongSignature) SimpleReaction(cbit.vcell.model.SimpleReaction) ReactionRule(cbit.vcell.model.ReactionRule) ProductPattern(cbit.vcell.model.ProductPattern) Point(java.awt.Point) Diagram(cbit.vcell.model.Diagram)

Example 9 with Diagram

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

the class ReactionCartoonRule method refreshAll.

@Override
protected void refreshAll(boolean reallocateShapes) {
    try {
        if (getModel() == null || getStructureSuite() == null) {
            return;
        }
        System.out.println("ReactionCartoonRule, RefreshAll()");
        for (Structure structure : structureSuite.getStructures()) {
            Diagram diagram = getModel().getDiagram(structure);
            if (diagram != null) {
                // Maintain consistency between rule participant nodes, signatures and
                // species pattern when a molecule is being modified.
                rebindAll(diagram);
            }
        }
        Set<Shape> unwantedShapes = new HashSet<Shape>();
        Set<RuleParticipantSignature> unwantedSignatures = new HashSet<RuleParticipantSignature>();
        Set<ReactionRuleShortSignature> unwantedRuleSignatures = new HashSet<ReactionRuleShortSignature>();
        unwantedShapes.addAll(getShapes());
        unwantedSignatures.addAll(ruleParticipantSignatures);
        unwantedRuleSignatures.addAll(reactionRuleShortSignatures);
        // ContainerContainerShape containerShape = (ContainerContainerShape) getShapeFromModelObject(getModel());
        // List<ReactionContainerShape> reactionContainerShapeList = new ArrayList<ReactionContainerShape>();
        // List<Structure> structureList = new ArrayList<Structure>(getStructureSuite().getStructures());
        // 
        // // create all ReactionContainerShapes (one for each Structure)
        // for(Structure structure : structureList) {
        // if (structure instanceof Membrane) {
        // Membrane membrane = (Membrane) structure;
        // ReactionContainerShape membraneShape =
        // (ReactionContainerShape) getShapeFromModelObject(membrane);
        // if (membraneShape == null) {
        // membraneShape = new ReactionContainerShape(membrane, structureSuite, this);
        // addShape(membraneShape);
        // membrane.getMembraneVoltage().removePropertyChangeListener(this);
        // membrane.getMembraneVoltage().addPropertyChangeListener(this);
        // } else {
        // membraneShape.setStructureSuite(structureSuite);
        // }
        // membrane.removePropertyChangeListener(this);
        // membrane.addPropertyChangeListener(this);
        // membraneShape.refreshLabel();
        // unwantedShapes.remove(membraneShape);
        // reactionContainerShapeList.add(membraneShape);
        // }else if (structure instanceof Feature){
        // Feature feature = (Feature) structure;
        // ReactionContainerShape featureShape = (ReactionContainerShape) getShapeFromModelObject(feature);
        // if (featureShape == null) {
        // featureShape = new ReactionContainerShape(feature, structureSuite, this);
        // addShape(featureShape);
        // } else {
        // featureShape.setStructureSuite(structureSuite);
        // }
        // feature.removePropertyChangeListener(this);
        // feature.addPropertyChangeListener(this);
        // featureShape.refreshLabel();
        // unwantedShapes.remove(featureShape);
        // reactionContainerShapeList.add(featureShape);
        // }
        // }
        // if (containerShape == null) {
        // containerShape = new ContainerContainerShape(this, getModel(), reactionContainerShapeList);
        // addShape(containerShape);
        // } else {
        // containerShape.setReactionContainerShapeList(reactionContainerShapeList);
        // }
        // containerShape.refreshLabel();
        // unwantedShapes.remove(containerShape);
        // // add all species context shapes within the structures
        // for(Structure structure : getStructureSuite().getStructures()) {
        // ReactionContainerShape reactionContainerShape =	(ReactionContainerShape) getShapeFromModelObject(structure);
        // structure.removePropertyChangeListener(this);
        // structure.addPropertyChangeListener(this);
        // for(SpeciesContext structSpeciesContext : getModel().getSpeciesContexts(structure)) {
        // SpeciesContextShape ss = (SpeciesContextShape) getShapeFromModelObject(structSpeciesContext);
        // if (ss == null) {
        // ss = new SpeciesContextShape(structSpeciesContext, this);
        // ss.truncateLabelName(false);
        // structSpeciesContext.getSpecies().removePropertyChangeListener(this);
        // structSpeciesContext.getSpecies().addPropertyChangeListener(this);
        // reactionContainerShape.addChildShape(ss);
        // addShape(ss);
        // ss.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
        // }
        // structSpeciesContext.removePropertyChangeListener(this);
        // structSpeciesContext.addPropertyChangeListener(this);
        // ss.refreshLabel();
        // unwantedShapes.remove(ss);
        // }
        // }
        // // add all reactionSteps that are in this structure (ReactionContainerShape), and draw the lines
        // getModel().removePropertyChangeListener(this);
        // getModel().addPropertyChangeListener(this);
        // //
        // // =================================== Rules ================================================
        // //
        // for(ReactionRule rr : getModel().getRbmModelContainer().getReactionRuleList()) {
        // rr.removePropertyChangeListener(this);
        // rr.addPropertyChangeListener(this);
        // Structure structure = rr.getStructure();
        // if(getStructureSuite().areReactionsShownFor(structure)) {
        // ReactionContainerShape reactionContainerShape =	(ReactionContainerShape) getShapeFromModelObject(structure);
        // 
        // ReactionRuleShortSignature ruleSignature = null;
        // int matches = 0;	// any rule must be part of one and only one ReactionRuleShortSignature object
        // for(ReactionRuleShortSignature rrss : reactionRuleShortSignatures) {
        // if(rrss.compareBySignature(rr)) {
        // ruleSignature = rrss;
        // matches++;
        // // 							break;		// once this code gets stable uncomment the break and remove the matches counter
        // } else {
        // // remove the rule if it's in the list even though the signature doesn't match
        // rrss.remove(rr);
        // }
        // }
        // if(matches > 1) {
        // throw new RuntimeException("Rule " + rr.getDisplayName() + " found in multiple ReactionRuleShortSignatures");
        // }
        // 
        // ReactionRuleShortDiagramShape rrShape = null;
        // if(ruleSignature == null) {
        // ruleSignature = new ReactionRuleShortSignature(rr);
        // reactionRuleShortSignatures.add(ruleSignature);
        // rrShape = new ReactionRuleShortDiagramShape(ruleSignature, this);
        // addShape(rrShape);
        // rrShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
        // reactionContainerShape.addChildShape(rrShape);
        // rrShape.getSpaceManager().setRelPos(reactionContainerShape.getRandomPosition());
        // } else {
        // ruleSignature.add(rr);	// add the rule if it's not there
        // rrShape = (ReactionRuleShortDiagramShape) getShapeFromModelObject(ruleSignature);
        // }
        // 
        // rrShape.refreshLabel();
        // unwantedShapes.remove(rrShape);
        // unwantedRuleSignatures.remove(ruleSignature);
        // if(matches != 0) {
        // continue;		// don't make other shapes or edges for this rule, the rule signature already has them from a previous rule
        // }
        // 
        // //
        // // add reaction participants as edges and SignatureShapes as needed
        // //
        // List<ReactionRuleParticipant> participants = rr.getReactionRuleParticipants();
        // List<RuleParticipantEdgeDiagramShape> ruleEdges = new ArrayList<> ();
        // for(ReactionRuleParticipant participant : participants) {
        // participant.getSpeciesPattern().removePropertyChangeListener(this);
        // participant.getSpeciesPattern().addPropertyChangeListener(this);
        // Structure speciesStructure = participant.getStructure();
        // Structure reactionStructure = rr.getStructure();
        // 
        // if(getStructureSuite().getStructures().contains(speciesStructure) && getStructureSuite().areReactionsShownFor(reactionStructure)) {
        // //
        // // find existing RuleParticipantSignatureShape in cartoon
        // //
        // RuleParticipantShortSignature ruleParticipantShortSignature = null;
        // for (RuleParticipantSignature signature : ruleParticipantSignatures) {
        // if (signature instanceof RuleParticipantLongSignature && signature.getStructure() == participant.getStructure()) {
        // System.out.println("ReactionCartoonMolecule, refreshAll(), RuleParticipantLongSignature");
        // break;
        // }
        // }
        // for (RuleParticipantSignature signature : ruleParticipantSignatures) {
        // if (signature instanceof RuleParticipantShortSignature && signature.getStructure() == participant.getStructure() &&
        // signature.compareByCriteria(participant.getSpeciesPattern(), GroupingCriteria.molecule)) {
        // ruleParticipantShortSignature = (RuleParticipantShortSignature)signature;
        // break;
        // }
        // }
        // //
        // // if didn't find signature in cartoons list of signatures, then create one (and create a shape for it).
        // //
        // RuleParticipantSignatureShortDiagramShape signatureShape = null;
        // if (ruleParticipantShortSignature == null) {
        // ruleParticipantShortSignature = RuleParticipantShortSignature.fromReactionRuleParticipant(participant, this);
        // ruleParticipantSignatures.add(ruleParticipantShortSignature);
        // signatureShape = new RuleParticipantSignatureShortDiagramShape(ruleParticipantShortSignature, this);
        // addShape(signatureShape);
        // ReactionContainerShape participantContainerShape =	(ReactionContainerShape) getShapeFromModelObject(participant.getStructure());
        // signatureShape.getSpaceManager().setRelPos(participantContainerShape.getRandomPosition());
        // participantContainerShape.addChildShape(signatureShape);
        // signatureShape.getSpaceManager().setRelPos(participantContainerShape.getRandomPosition());
        // } else {
        // signatureShape = (RuleParticipantSignatureShortDiagramShape) getShapeFromModelObject(ruleParticipantShortSignature);
        // }
        // 
        // unwantedShapes.remove(signatureShape);
        // unwantedSignatures.remove(ruleParticipantShortSignature);
        // signatureShape.refreshLabel();
        // signatureShape.setVisible(true);
        // 
        // //
        // // add edge for ReactionRuleParticipant if not already present.
        // //
        // RuleParticipantEdgeDiagramShape ruleParticipantShape = (RuleParticipantEdgeDiagramShape) getShapeFromModelObject(participant);
        // if (ruleParticipantShape == null || ruleParticipantShape.getRuleParticipantSignatureShape() != signatureShape) {
        // if (participant instanceof ReactantPattern && signatureShape.isVisible()) {
        // ruleParticipantShape = new ReactantPatternEdgeDiagramShape((ReactantPattern) participant, (ElipseShape)rrShape, signatureShape, this);
        // } else if (participant instanceof ProductPattern && signatureShape.isVisible()) {
        // ruleParticipantShape = new ProductPatternEdgeDiagramShape((ProductPattern) participant, (ElipseShape)rrShape, signatureShape, this);
        // } else {
        // throw new RuntimeException("unsupported ReactionRuleParticipant " + participant.getClass());
        // }
        // addShape(ruleParticipantShape);
        // }
        // if(!containerShape.getChildren().contains(ruleParticipantShape)) {
        // containerShape.addChildShape(ruleParticipantShape);
        // }
        // unwantedShapes.remove(ruleParticipantShape);
        // ruleParticipantShape.refreshLabel();
        // ruleEdges.add(ruleParticipantShape);	// all the edges for this rule
        // }
        // }
        // // now let's see if any reactant and product pair have the same signature - means we need to draw a reactant and
        // // a product edge (a closed loop) between the rule diagram shape and the signature diagram shape
        // for(RuleParticipantEdgeDiagramShape ours : ruleEdges) {
        // ours.setSibling(false);		// reset them all
        // }
        // for(RuleParticipantEdgeDiagramShape ours : ruleEdges) {
        // for(RuleParticipantEdgeDiagramShape theirs : ruleEdges) {
        // if(ours == theirs) {
        // continue;			// don't compare with self
        // }
        // if(ours.getRuleParticipantSignatureShape() == theirs.getRuleParticipantSignatureShape()) {
        // ours.setSibling(true);
        // theirs.setSibling(true);
        // }
        // }
        // }
        // }
        // }
        ruleParticipantSignatures.removeAll(unwantedSignatures);
        reactionRuleShortSignatures.removeAll(unwantedRuleSignatures);
        // }
        for (Shape unwantedShape : unwantedShapes) {
            removeShape(unwantedShape);
        }
    // // update diagrams
    // for(Structure structure : structureSuite.getStructures()) {
    // Diagram diagram = getModel().getDiagram(structure);
    // if (diagram != null) {
    // applyDefaults(diagram);
    // }
    // }
    // fireGraphChanged(new GraphEvent(this));
    } catch (Throwable e) {
        handleException(e);
    }
}
Also used : RuleParticipantSignature(cbit.vcell.model.RuleParticipantSignature) Shape(cbit.gui.graph.Shape) ReactionRuleShortSignature(cbit.vcell.model.ReactionRuleShortSignature) Structure(cbit.vcell.model.Structure) Diagram(cbit.vcell.model.Diagram) HashSet(java.util.HashSet)

Example 10 with Diagram

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

the class XmlReader method getModel.

/**
 * This method creates a Model object from a XML element.
 * Creation date: (3/14/2001 6:14:37 PM)
 * @return cbit.vcell.model.Model
 * @param param org.jdom.Element
 */
private Model getModel(Element param) throws XmlParseException {
    if (param == null) {
        throw new XmlParseException("Invalid 'NULL' XML 'model' element arrived!");
    }
    // Get version, if any
    Model newmodel = null;
    Version version = getVersion(param.getChild(XMLTags.VersionTag, vcNamespace));
    // if forcedModelUnitSystem has been set, ues that (could be overriding unit system for SBML export)
    if (forcedModelUnitSystem != null) {
        newmodel = new Model(version, forcedModelUnitSystem);
    } else {
        Element unitSystemNode = param.getChild(XMLTags.ModelUnitSystemTag, vcNamespace);
        if (unitSystemNode != null) {
            ModelUnitSystem modelUnitSystem = getUnitSystem(unitSystemNode);
            newmodel = new Model(version, modelUnitSystem);
        } else {
            newmodel = new Model(version);
        }
    }
    try {
        // Set attributes
        newmodel.setName(unMangle(param.getAttributeValue(XMLTags.NameAttrTag)));
        // Add annotation
        String annotationText = param.getChildText(XMLTags.AnnotationTag, vcNamespace);
        if (annotationText != null && annotationText.length() > 0) {
            newmodel.setDescription(unMangle(annotationText));
        }
        // Add global parameters
        Element globalParamsElement = param.getChild(XMLTags.ModelParametersTag, vcNamespace);
        if (globalParamsElement != null) {
            ModelParameter[] modelParams = getModelParams(globalParamsElement, newmodel);
            // add global/model param to model - done inside getModelParam by passing newModel
            newmodel.setModelParameters(modelParams);
        }
        // Add Species (Compounds)
        Iterator<Element> iterator = param.getChildren(XMLTags.SpeciesTag, vcNamespace).iterator();
        ArrayList<Species> speciesList = new ArrayList<Species>();
        while (iterator.hasNext()) {
            org.jdom.Element temp = (Element) iterator.next();
            speciesList.add(getSpecies(temp));
        }
        newmodel.setSpecies(speciesList.toArray(new Species[speciesList.size()]));
        // Add Structures
        LinkedList<Structure> newstructures = new LinkedList<Structure>();
        // (features)
        List<Element> children = param.getChildren(XMLTags.FeatureTag, vcNamespace);
        for (Element featureElement : children) {
            newstructures.add(getFeature(featureElement));
        }
        // (Membrane)
        children = param.getChildren(XMLTags.MembraneTag, vcNamespace);
        for (Element memElement : children) {
            newstructures.add(getMembrane(newmodel, memElement, newstructures));
        }
        if (newstructures.size() > 0) {
            Structure[] structarray = new Structure[newstructures.size()];
            newstructures.toArray(structarray);
            // Add all the retrieved structures
            newmodel.setStructures(structarray);
        }
        // retrieve the RbmModelContainer, if present - must be done before we retrieve species context!
        Element element = param.getChild(XMLTags.RbmModelContainerTag, vcNamespace);
        if (element != null) {
            getRbmModelContainer(element, newmodel);
        } else {
            lg.info("RbmModelContainer is missing.");
        }
        // Add SpeciesContexts
        children = param.getChildren(XMLTags.SpeciesContextTag, vcNamespace);
        SpeciesContext[] newspeccon = new SpeciesContext[children.size()];
        int scCounter = 0;
        for (Element scElement : children) {
            newspeccon[scCounter] = getSpeciesContext(scElement, newmodel);
            scCounter++;
        }
        newmodel.setSpeciesContexts(newspeccon);
        // Retrieve rateRules and add to model
        // Element rateRuleVarsElement = param.getChild(XMLTags.RateRuleVariablesTag, vcNamespace);
        // if(rateRuleVarsElement != null){
        // RateRuleVariable[] rateRuleVars = getRateRuleVariables(rateRuleVarsElement, newmodel);
        // newmodel.setRateRuleVariables(rateRuleVars);
        // }
        // Add Reaction steps (if available)
        // (Simplereaction)
        // Create a varHash with reserved symbols and global parameters, if any, to pass on to Kinetics
        // must create new hash for each reaction and flux, since each kinetics uses new variables hash
        iterator = param.getChildren(XMLTags.SimpleReactionTag, vcNamespace).iterator();
        ArrayList<ReactionStep> reactionStepList = new ArrayList<ReactionStep>();
        while (iterator.hasNext()) {
            org.jdom.Element temp = iterator.next();
            reactionStepList.add(getSimpleReaction(temp, newmodel));
        }
        // (fluxStep)
        iterator = param.getChildren(XMLTags.FluxStepTag, vcNamespace).iterator();
        while (iterator.hasNext()) {
            org.jdom.Element temp = iterator.next();
            reactionStepList.add(getFluxReaction(temp, newmodel));
        }
        newmodel.setReactionSteps(reactionStepList.toArray(new ReactionStep[reactionStepList.size()]));
        // Add Diagrams
        children = param.getChildren(XMLTags.DiagramTag, vcNamespace);
        if (children.size() > 0) {
            Diagram[] newdiagrams = new Diagram[children.size()];
            int diagramCounter = 0;
            for (Element diagramElement : children) {
                newdiagrams[diagramCounter] = getDiagram(diagramElement, newmodel);
                diagramCounter++;
            }
            reorderDiagramsInPlace_UponRead(docVCellSoftwareVersion, newdiagrams, newmodel.getStructureTopology());
            // if(docVCellSoftwareVersion != null && !docVCellSoftwareVersion.isValid() && docVCellSoftwareVersion.getMajorVersion()<=5 && docVCellSoftwareVersion.getMinorVersion() <=2){
            // //In Vcell 5.2 and previous we need to order diagrams topologically, in 5.3 and later the diagrams are displayed as they are ordered when read from document
            // final StructureTopology structureTopology = newmodel.getStructureTopology();
            // Arrays.sort(newdiagrams, new Comparator<Diagram>() {
            // @Override
            // public int compare(Diagram o1, Diagram o2) {
            // return getStructureLevel(o1.getStructure(), structureTopology) - getStructureLevel(o2.getStructure(), structureTopology);
            // }
            // });
            // }
            newmodel.setDiagrams(newdiagrams);
        }
    } catch (java.beans.PropertyVetoException e) {
        e.printStackTrace();
        throw new XmlParseException(e);
    } catch (ModelException e) {
        e.printStackTrace();
    }
    // model param expresions are not bound when they are read in, since they could be functions of each other or structures/speciesContexts.
    // Hence bind the model param exprs at the end, after reading all model level quantities.
    ModelParameter[] modelParameters = newmodel.getModelParameters();
    for (int i = 0; modelParameters != null && i < modelParameters.length; i++) {
        try {
            modelParameters[i].getExpression().bindExpression(newmodel);
        } catch (ExpressionBindingException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error binding global parameter '" + modelParameters[i].getName() + "' to model." + e.getMessage());
        }
    }
    return newmodel;
}
Also used : Element(org.jdom.Element) ArrayList(java.util.ArrayList) SpeciesContext(cbit.vcell.model.SpeciesContext) PropertyVetoException(java.beans.PropertyVetoException) Version(org.vcell.util.document.Version) RedistributionVersion(cbit.vcell.solvers.mb.MovingBoundarySolverOptions.RedistributionVersion) SimulationVersion(org.vcell.util.document.SimulationVersion) VCellSoftwareVersion(org.vcell.util.document.VCellSoftwareVersion) Structure(cbit.vcell.model.Structure) DBFormalSpecies(cbit.vcell.model.DBFormalSpecies) Species(cbit.vcell.model.Species) DBSpecies(cbit.vcell.model.DBSpecies) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem) ModelException(cbit.vcell.model.ModelException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) LinkedList(java.util.LinkedList) Diagram(cbit.vcell.model.Diagram) ModelParameter(cbit.vcell.model.Model.ModelParameter) ReactionStep(cbit.vcell.model.ReactionStep) MathModel(cbit.vcell.mathmodel.MathModel) Model(cbit.vcell.model.Model) PathwayModel(org.vcell.pathway.PathwayModel) RelationshipModel(org.vcell.relationship.RelationshipModel) BioModel(cbit.vcell.biomodel.BioModel) Element(org.jdom.Element)

Aggregations

Diagram (cbit.vcell.model.Diagram)15 Structure (cbit.vcell.model.Structure)12 ArrayList (java.util.ArrayList)9 ReactionStep (cbit.vcell.model.ReactionStep)7 SpeciesContext (cbit.vcell.model.SpeciesContext)7 Shape (cbit.gui.graph.Shape)4 Feature (cbit.vcell.model.Feature)4 Species (cbit.vcell.model.Species)4 Point (java.awt.Point)4 PropertyVetoException (java.beans.PropertyVetoException)4 AllStructureSuite (cbit.vcell.graph.structures.AllStructureSuite)3 Catalyst (cbit.vcell.model.Catalyst)3 FluxReaction (cbit.vcell.model.FluxReaction)3 Membrane (cbit.vcell.model.Membrane)3 ReactionParticipant (cbit.vcell.model.ReactionParticipant)3 RuleParticipantSignature (cbit.vcell.model.RuleParticipantSignature)3 SimpleReaction (cbit.vcell.model.SimpleReaction)3 HashSet (java.util.HashSet)3 KeyValue (org.vcell.util.document.KeyValue)3 GraphEvent (cbit.gui.graph.GraphEvent)2