Search in sources :

Example 21 with RbmObservable

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

the class BioModelEditorModelPanel method deleteButtonPressed.

private void deleteButtonPressed() {
    try {
        ArrayList<Object> deleteList = new ArrayList<Object>();
        int selectedIndex = tabbedPane.getSelectedIndex();
        if (selectedIndex == ModelPanelTabID.reaction_diagram.ordinal()) {
            deleteList.addAll(Arrays.asList(reactionCartoonEditorPanel.getReactionCartoon().getSelectedObjects()));
        // } else if (selectedIndex == ModelPanelTabID.structure_diagram.ordinal()) {
        // deleteList.addAll(Arrays.asList(cartoonEditorPanel.getStructureCartoon().getSelectedObjects()));
        } else {
            computeCurrentSelectedTable();
            int[] rows = currentSelectedTable.getSelectedRows();
            if (rows == null || rows.length == 0) {
                return;
            }
            if (currentSelectedTable == speciesTable) {
                for (int r : rows) {
                    if (r < speciesTableModel.getRowCount()) {
                        SpeciesContext speciesContext = speciesTableModel.getValueAt(r);
                        if (speciesContext != null) {
                            deleteList.add(speciesContext);
                        }
                    }
                }
            } else if (currentSelectedTable == molecularTypeTable) {
                // TODO: delete stuff
                for (int r : rows) {
                    if (r < molecularTypeTableModel.getRowCount()) {
                        MolecularType mt = molecularTypeTableModel.getValueAt(r);
                        if (mt != null) {
                            deleteList.add(mt);
                        }
                    }
                }
            } else if (currentSelectedTable == observablesTable) {
                for (int r : rows) {
                    if (r < observableTableModel.getRowCount()) {
                        RbmObservable o = observableTableModel.getValueAt(r);
                        if (o != null) {
                            deleteList.add(o);
                        }
                    }
                }
            } else if (currentSelectedTable == structuresTable) {
                for (int r : rows) {
                    if (r < structureTableModel.getRowCount()) {
                        Structure rowValue = structureTableModel.getValueAt(r);
                        if (rowValue instanceof Feature || rowValue instanceof Membrane) {
                            deleteList.add(rowValue);
                        }
                    }
                }
            } else if (currentSelectedTable == reactionsTable) {
                for (int r : rows) {
                    if (r < reactionTableModel.getRowCount()) {
                        ModelProcess reaction = reactionTableModel.getValueAt(r);
                        if (reaction != null) {
                            deleteList.add(reaction);
                        }
                    }
                }
            }
        }
        if (deleteList.size() == 0) {
            return;
        }
        StringBuilder deleteListText = new StringBuilder();
        for (Object object : deleteList) {
            if (object instanceof SpeciesContext) {
                deleteListText.append("Species\t'" + ((SpeciesContext) object).getName() + "'\n");
            } else if (object instanceof MolecularType) {
                deleteListText.append(((MolecularType) object).getDisplayType() + "\t'" + ((MolecularType) object).getDisplayName() + "'\n");
            } else if (object instanceof RbmObservable) {
                deleteListText.append("Observable\t'" + ((RbmObservable) object).getName() + "'\n");
            } else if (object instanceof ReactionStep) {
                deleteListText.append("Reaction\t'" + ((ReactionStep) object).getName() + "'\n");
            } else if (object instanceof ReactionRule) {
                deleteListText.append("Reaction rule\t'" + ((ReactionRule) object).getName() + "'\n");
            } else if (object instanceof Structure) {
                deleteListText.append("Structure\t'" + ((Structure) object).getName() + "'\n");
            }
        }
        // TODO: once we display reaction rules in the carton editor panel we'll have to change the way we delete reaction rules
        if (deleteList.get(0) instanceof SpeciesContext || deleteList.get(0) instanceof ReactionStep) {
            try {
                ArrayList<SpeciesContext> speciesContextArrList = new ArrayList<SpeciesContext>();
                ArrayList<ReactionStep> reactionStepArrList = new ArrayList<ReactionStep>();
                for (Object obj : deleteList) {
                    if (obj instanceof SpeciesContext) {
                        speciesContextArrList.add((SpeciesContext) obj);
                    } else if (obj instanceof ReactionStep) {
                        reactionStepArrList.add((ReactionStep) obj);
                    } else {
                        throw new Exception("Unexpected delete object " + obj.getClass().getName());
                    }
                }
                ReactionCartoonTool.deleteReactionsAndSpecies(reactionCartoonEditorPanel, reactionStepArrList.toArray(new ReactionStep[0]), speciesContextArrList.toArray(new SpeciesContext[0]));
            } catch (UserCancelException uce) {
                return;
            }
            return;
        } else {
            String confirm = DialogUtils.showOKCancelWarningDialog(this, "Deleting", "You are going to delete the following:\n\n" + deleteListText + "\n Continue?");
            if (confirm.equals(UserMessage.OPTION_CANCEL)) {
                return;
            }
            for (Object object : deleteList) {
                if (object instanceof ReactionRule) {
                    ReactionRule rr = (ReactionRule) object;
                    bioModel.getModel().getRbmModelContainer().removeReactionRule(rr);
                } else if (object instanceof MolecularType) {
                    Map<String, Pair<Displayable, SpeciesPattern>> usedHere = new LinkedHashMap<String, Pair<Displayable, SpeciesPattern>>();
                    MolecularType mt = (MolecularType) object;
                    if (!bioModel.getModel().getRbmModelContainer().isDeleteAllowed(mt, usedHere)) {
                        String errMsg = mt.getDisplayType() + " <b>'" + mt + "'</b> cannot be deleted because it's already being used by:<br>";
                        final int MaxListSize = 7;
                        int count = 0;
                        for (String key : usedHere.keySet()) {
                            System.out.println(key);
                            if (count >= MaxListSize) {
                                errMsg += "<br> ... and more.";
                                break;
                            }
                            Pair<Displayable, SpeciesPattern> o = usedHere.get(key);
                            Displayable e = o.one;
                            SpeciesPattern sp = o.two;
                            errMsg += "<br> - " + e.getDisplayType().toLowerCase() + " <b>" + e.getDisplayName() + "</b>";
                            errMsg += ", " + sp.getDisplayType().toLowerCase() + " " + " <b>" + sp.getDisplayName() + "</b>";
                            count++;
                        }
                        errMsg = "<html>" + errMsg + "</html>";
                        throw new RuntimeException(errMsg);
                    }
                    bioModel.getModel().getRbmModelContainer().removeMolecularType(mt);
                } else if (object instanceof RbmObservable) {
                    RbmObservable o = (RbmObservable) object;
                    bioModel.getModel().getRbmModelContainer().removeObservable(o);
                } else {
                    bioModel.getModel().removeObject(object);
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        DialogUtils.showErrorDialog(this, ex.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) UserCancelException(org.vcell.util.UserCancelException) SpeciesContext(cbit.vcell.model.SpeciesContext) Feature(cbit.vcell.model.Feature) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) Pair(org.vcell.util.Pair) Displayable(org.vcell.util.Displayable) ReactionRule(cbit.vcell.model.ReactionRule) RbmObservable(cbit.vcell.model.RbmObservable) ModelProcess(cbit.vcell.model.ModelProcess) PropertyVetoException(java.beans.PropertyVetoException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ModelException(cbit.vcell.model.ModelException) UserCancelException(org.vcell.util.UserCancelException) MolecularType(org.vcell.model.rbm.MolecularType) ReactionStep(cbit.vcell.model.ReactionStep) RelationshipObject(org.vcell.relationship.RelationshipObject) BioModelEntityObject(cbit.vcell.model.BioModelEntityObject) BioPaxObject(org.vcell.pathway.BioPaxObject) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 22 with RbmObservable

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

the class ObservablesGroupTableModel method refreshData.

private void refreshData() {
    allObservablesList = new ArrayList<ObservablesGroupTableRow>();
    for (int i = 0; i < observabless.length; i++) {
        ObservableGroup og = observabless[i];
        ObservablesGroupTableRow newRow = createTableRow(og, i + 1);
        allObservablesList.add(newRow);
    }
    // if bng fails to produce even one species for an observable, no observable group will be created
    // we'll produce ourselves an "empty" observable group for each such observable
    List<String> rbmObsList = new ArrayList<>();
    List<String> obsGroupList = new ArrayList<>();
    for (RbmObservable o : getModel().getRbmModelContainer().getObservableList()) {
        rbmObsList.add(o.getName());
    }
    for (ObservableGroup og : observabless) {
        obsGroupList.add(og.getObservableGroupName());
    }
    // intersect
    rbmObsList.removeAll(obsGroupList);
    for (String o : rbmObsList) {
        // empty observable group
        ObservableGroup og = new ObservableGroup(o, new BNGSpecies[0], new int[0]);
        // all will have index 0
        ObservablesGroupTableRow newRow = createTableRow(og, 0);
        allObservablesList.add(newRow);
    }
    // apply text search function for particular columns
    List<ObservablesGroupTableRow> observablesObjectList = new ArrayList<>();
    if (searchText == null || searchText.length() == 0) {
        observablesObjectList.addAll(allObservablesList);
    } else {
        String lowerCaseSearchText = searchText.toLowerCase();
        for (ObservablesGroupTableRow ogtr : allObservablesList) {
            String obsName = ogtr.getObservableGroupObject().getObservableGroupName();
            RbmObservable obs = ogtr.getObservable(obsName);
            if (obsName.toLowerCase().contains(lowerCaseSearchText)) {
                // name match
                observablesObjectList.add(ogtr);
                // if found no need to keep looking for this row
                continue;
            }
            String obsDefinition = ObservablesGroupTableRow.toBnglStringEx(obs);
            if (obsDefinition.toLowerCase().contains(lowerCaseSearchText)) {
                // definition match
                observablesObjectList.add(ogtr);
                continue;
            }
            String obsExpression = getExpressionAsString(ogtr.getObservableGroupObject());
            if (obsExpression.toLowerCase().contains(lowerCaseSearchText)) {
                // expression match
                observablesObjectList.add(ogtr);
                continue;
            }
        }
    }
    setData(observablesObjectList);
    GuiUtils.flexResizeTableColumns(ownerTable);
}
Also used : ObservableGroup(cbit.vcell.bionetgen.ObservableGroup) RbmObservable(cbit.vcell.model.RbmObservable) ArrayList(java.util.ArrayList)

Example 23 with RbmObservable

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

the class ObservablesGroupTableModel method getComparator.

public Comparator<ObservablesGroupTableRow> getComparator(final int col, final boolean ascending) {
    final int scale = ascending ? 1 : -1;
    return new Comparator<ObservablesGroupTableRow>() {

        public int compare(ObservablesGroupTableRow o1, ObservablesGroupTableRow o2) {
            String n1 = o1.getObservableGroupObject().getObservableGroupName();
            String n2 = o2.getObservableGroupObject().getObservableGroupName();
            RbmObservable ob1 = o1.getObservable(n1);
            RbmObservable ob2 = o2.getObservable(n2);
            switch(col) {
                case iColOriginalName:
                    return scale * n1.compareToIgnoreCase(n2);
                case iColStructure:
                    String es1 = ObservablesGroupTableRow.toBnglString(o1.getObservable(n1));
                    String es2 = ObservablesGroupTableRow.toBnglString(o2.getObservable(n2));
                    if (es1.startsWith("@") && es1.contains(":")) {
                        // no point to check es2 as well
                        String sn1 = es1.substring(1, es1.indexOf(":"));
                        String sn2 = es2.substring(1, es2.indexOf(":"));
                        return scale * sn1.compareToIgnoreCase(sn2);
                    } else {
                        // we should be here only if we have one single structure, so nothing to sort
                        return 0;
                    }
                case iColDepiction:
                    if (ob1 == null && ob2 == null) {
                        return 0;
                    }
                    // the sp list is always allocated even though it may be empty
                    Integer s1 = ob1.getSpeciesPatternList().size();
                    Integer s2 = ob2.getSpeciesPatternList().size();
                    if (s1 != s2) {
                        // different number of species patterns, sort by number of patterns
                        return scale * s1.compareTo(s2);
                    // this includes the case when one list is empty
                    }
                    if (s1 > 1 && s2 > 1) {
                        // same number of species patterns, more than one - we don't bother to sort
                        return 0;
                    // TODO: add functionality to sort by total number of molecules / sites
                    }
                    // one pattern each, sort by number of molecules, if that equal sort by total number of sites
                    if (s1 == 1 && s2 == 1) {
                        Integer i1 = ob1.getSpeciesPattern(0).getMolecularTypePatterns().size();
                        Integer i2 = ob2.getSpeciesPattern(0).getMolecularTypePatterns().size();
                        if (scale * i1.compareTo(i2) == 0) {
                            // if same number of molecule we try to sort by number of sites of the mt
                            i1 = 0;
                            i2 = 0;
                            for (MolecularTypePattern mtp : ob1.getSpeciesPattern(0).getMolecularTypePatterns()) {
                                i1 += mtp.getMolecularType().getComponentList().size();
                            }
                            for (MolecularTypePattern mtp : ob2.getSpeciesPattern(0).getMolecularTypePatterns()) {
                                i2 += mtp.getMolecularType().getComponentList().size();
                            }
                            return scale * i1.compareTo(i2);
                        } else {
                            return scale * i1.compareTo(i2);
                        }
                    }
                    return 0;
                case iColDefinition:
                    es1 = ObservablesGroupTableRow.toBnglString(o1.getObservable(n1));
                    es2 = ObservablesGroupTableRow.toBnglString(o2.getObservable(n2));
                    if (es1.startsWith("@") && es1.contains(":")) {
                        // multi-structure; no point to check es2 as well
                        String sn1 = es1.substring(es1.indexOf(":") + 1);
                        String sn2 = es2.substring(es2.indexOf(":") + 1);
                        return scale * sn1.compareToIgnoreCase(sn2);
                    } else {
                        // single structure
                        return scale * es1.compareToIgnoreCase(es2);
                    }
                case iColCount:
                    return scale * ob1.getType().name().compareTo(ob2.getType().name());
                default:
                    return 0;
            }
        }
    };
}
Also used : RbmObservable(cbit.vcell.model.RbmObservable) MolecularTypePattern(org.vcell.model.rbm.MolecularTypePattern) Comparator(java.util.Comparator)

Example 24 with RbmObservable

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

the class StochMathMapping method refreshVariables.

/**
 * Map speciesContext to variable, used for structural analysis (slow reactions and fast reactions)
 * Creation date: (10/25/2006 8:59:43 AM)
 * @exception cbit.vcell.mapping.MappingException The exception description.
 */
@Override
protected void refreshVariables() throws MappingException {
    // 
    // stochastic species need  species variables require either a membrane or volume variable
    // 
    Enumeration<SpeciesContextMapping> enum1 = getSpeciesContextMappings();
    while (enum1.hasMoreElements()) {
        SpeciesContextMapping scm = (SpeciesContextMapping) enum1.nextElement();
        SpeciesContextSpec scs = getSimulationContext().getReactionContext().getSpeciesContextSpec(scm.getSpeciesContext());
        // stochastic variable is always a function of size.
        SpeciesCountParameter spCountParm = null;
        try {
            String countName = scs.getSpeciesContext().getName() + BIO_PARAM_SUFFIX_SPECIES_COUNT;
            Expression countExp = new Expression(0.0);
            spCountParm = addSpeciesCountParameter(countName, countExp, PARAMETER_ROLE_SPECIES_COUNT, scs.getInitialCountParameter().getUnitDefinition(), scs.getSpeciesContext());
        } catch (PropertyVetoException pve) {
            pve.printStackTrace();
            throw new MappingException(pve.getMessage());
        }
        // add concentration of species as MathMappingParameter - this will map to species concentration function
        try {
            String concName = scs.getSpeciesContext().getName() + BIO_PARAM_SUFFIX_SPECIES_CONCENTRATION;
            Expression concExp = getExpressionAmtToConc(new Expression(spCountParm, getNameScope()), scs.getSpeciesContext().getStructure());
            concExp.bindExpression(this);
            addSpeciesConcentrationParameter(concName, concExp, PARAMETER_ROLE_SPECIES_CONCENRATION, scs.getSpeciesContext().getUnitDefinition(), scs.getSpeciesContext());
        } catch (Exception e) {
            e.printStackTrace();
            throw new MappingException(e.getMessage(), e);
        }
        // we always add variables, all species are independent variables, no matter they are constant or not.
        String countMathSymbol = getMathSymbol(spCountParm, getSimulationContext().getGeometryContext().getStructureMapping(scs.getSpeciesContext().getStructure()).getGeometryClass());
        scm.setVariable(new StochVolVariable(countMathSymbol));
    }
    // 
    // if the original (untransformed) model has any explicit observables (which are rule-based components), then the transformed model maps these observables to "Concentration" ModelParameters.
    // 
    // for symmetry with the RuleBasedMathMapping, we want to generate a "_Count" version of these observables if applicable.
    // 
    // so if a rule-to-network "transformation" was performed, we want to find those ModelParameters which map to Observables (concentrations) so that we can generate an additional "Count" function (by scaling by compartment size and performing a unit conversion).
    // 
    SimContextTransformation transformation = getTransformation();
    if (transformation != null) {
        ModelEntityMapping[] modelEntityMappings = transformation.modelEntityMappings;
        if (modelEntityMappings != null) {
            for (ModelEntityMapping mem : modelEntityMappings) {
                if (mem.newModelObj instanceof ModelParameter && mem.origModelObj instanceof RbmObservable) {
                    ModelParameter concObservableParameter = (ModelParameter) mem.newModelObj;
                    RbmObservable observable = (RbmObservable) mem.origModelObj;
                    try {
                        Expression countExp = getExpressionConcToExpectedCount(new Expression(concObservableParameter, getNameScope()), observable.getStructure());
                        // countExp.bindExpression(this);
                        addObservableCountParameter(concObservableParameter.getName() + BIO_PARAM_SUFFIX_SPECIES_COUNT, countExp, PARAMETER_ROLE_OBSERVABLE_COUNT, getSimulationContext().getModel().getUnitSystem().getStochasticSubstanceUnit(), observable);
                    } catch (ExpressionException | PropertyVetoException e) {
                        e.printStackTrace();
                        throw new MappingException(e.getMessage(), e);
                    }
                }
            }
        }
    }
}
Also used : ModelEntityMapping(cbit.vcell.mapping.SimContextTransformer.ModelEntityMapping) RbmObservable(cbit.vcell.model.RbmObservable) SimContextTransformation(cbit.vcell.mapping.SimContextTransformer.SimContextTransformation) PropertyVetoException(java.beans.PropertyVetoException) MatrixException(cbit.vcell.matrix.MatrixException) ModelException(cbit.vcell.model.ModelException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) ExpressionException(cbit.vcell.parser.ExpressionException) PropertyVetoException(java.beans.PropertyVetoException) ModelParameter(cbit.vcell.model.Model.ModelParameter) Expression(cbit.vcell.parser.Expression) StochVolVariable(cbit.vcell.math.StochVolVariable)

Example 25 with RbmObservable

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

the class RulebasedMathMapping method addObservables.

private List<ParticleObservable> addObservables(GeometryClass geometryClass, Domain domain, HashMap<SpeciesPattern, VolumeParticleSpeciesPattern> speciesPatternMap) throws MappingException, MathException {
    ArrayList<ParticleObservable> observables = new ArrayList<>();
    // 
    for (MathMappingParameter mathMappingParameter : getMathMappingParameters()) {
        if (mathMappingParameter instanceof ObservableCountParameter) {
            ObservableCountParameter observableCountParameter = (ObservableCountParameter) mathMappingParameter;
            RbmObservable rbmObservable = observableCountParameter.getObservable();
            ParticleObservable.ObservableType particleObservableType = null;
            if (rbmObservable.getType() == RbmObservable.ObservableType.Molecules) {
                particleObservableType = ParticleObservable.ObservableType.Molecules;
            } else {
                particleObservableType = ParticleObservable.ObservableType.Species;
            }
            ParticleObservable particleObservable = new VolumeParticleObservable(getMathSymbol(observableCountParameter, geometryClass), domain, particleObservableType);
            switch(rbmObservable.getSequence()) {
                case Multimolecular:
                    {
                        particleObservable.setSequence(Sequence.Multimolecular);
                        break;
                    }
                case PolymerLengthEqual:
                    {
                        particleObservable.setSequence(Sequence.PolymerLengthEqual);
                        particleObservable.setQuantity(rbmObservable.getSequenceLength());
                        break;
                    }
                case PolymerLengthGreater:
                    {
                        particleObservable.setSequence(Sequence.PolymerLengthGreater);
                        particleObservable.setQuantity(rbmObservable.getSequenceLength());
                        break;
                    }
                default:
                    {
                        throw new RuntimeException("unexpected sequence " + rbmObservable.getSequence());
                    }
            }
            for (SpeciesPattern speciesPattern : rbmObservable.getSpeciesPatternList()) {
                VolumeParticleSpeciesPattern vpsp = speciesPatternMap.get(speciesPattern);
                particleObservable.addParticleSpeciesPattern(vpsp);
            }
            observables.add(particleObservable);
        }
        if (mathMappingParameter instanceof SpeciesCountParameter) {
            SpeciesCountParameter speciesCountParameter = (SpeciesCountParameter) mathMappingParameter;
            ParticleObservable.ObservableType particleObservableType = ParticleObservable.ObservableType.Species;
            ParticleObservable particleObservable = new VolumeParticleObservable(getMathSymbol(speciesCountParameter, geometryClass), domain, particleObservableType);
            particleObservable.setSequence(Sequence.Multimolecular);
            SpeciesPattern speciesPattern = speciesCountParameter.getSpeciesContext().getSpeciesPattern();
            VolumeParticleSpeciesPattern vpsp = speciesPatternMap.get(speciesPattern);
            particleObservable.addParticleSpeciesPattern(vpsp);
            observables.add(particleObservable);
        }
    }
    return observables;
}
Also used : RbmObservable(cbit.vcell.model.RbmObservable) ArrayList(java.util.ArrayList) VolumeParticleSpeciesPattern(cbit.vcell.math.VolumeParticleSpeciesPattern) VolumeParticleObservable(cbit.vcell.math.VolumeParticleObservable) ParticleObservable(cbit.vcell.math.ParticleObservable) VolumeParticleObservable(cbit.vcell.math.VolumeParticleObservable) VolumeParticleSpeciesPattern(cbit.vcell.math.VolumeParticleSpeciesPattern) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern)

Aggregations

RbmObservable (cbit.vcell.model.RbmObservable)50 SpeciesPattern (org.vcell.model.rbm.SpeciesPattern)22 ReactionRule (cbit.vcell.model.ReactionRule)20 SpeciesContext (cbit.vcell.model.SpeciesContext)18 Structure (cbit.vcell.model.Structure)16 ArrayList (java.util.ArrayList)15 MolecularType (org.vcell.model.rbm.MolecularType)14 MolecularTypePattern (org.vcell.model.rbm.MolecularTypePattern)10 ReactionStep (cbit.vcell.model.ReactionStep)9 PropertyVetoException (java.beans.PropertyVetoException)8 SimulationContext (cbit.vcell.mapping.SimulationContext)7 Model (cbit.vcell.model.Model)7 BioModel (cbit.vcell.biomodel.BioModel)6 ModelException (cbit.vcell.model.ModelException)6 RbmModelContainer (cbit.vcell.model.Model.RbmModelContainer)5 ProductPattern (cbit.vcell.model.ProductPattern)5 ReactantPattern (cbit.vcell.model.ReactantPattern)5 BioModelNode (cbit.vcell.desktop.BioModelNode)4 BioPaxObject (org.vcell.pathway.BioPaxObject)4 StructureToolShapeIcon (cbit.vcell.graph.gui.StructureToolShapeIcon)3