Search in sources :

Example 1 with Model

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

the class ViewGeneratedReactionsPanel method updateShape.

public void updateShape(int selectedRow) {
    GeneratedReactionTableRow reactionTableRow = tableModel.getValueAt(selectedRow);
    String inputString = reactionTableRow.getExpression();
    System.out.println(selectedRow + ": " + inputString);
    // ReactionRule newReactionRule = (ReactionRule)RbmUtils.parseReactionRule(inputString, bioModel);
    Model tempModel = null;
    try {
        tempModel = new Model("MyTempModel");
        tempModel.addFeature("c0");
    } catch (ModelException | PropertyVetoException e1) {
        e1.printStackTrace();
    }
    if (owner != null && owner.getSimulationContext() != null) {
        List<MolecularType> mtList = owner.getSimulationContext().getModel().getRbmModelContainer().getMolecularTypeList();
        try {
            tempModel.getRbmModelContainer().setMolecularTypeList(mtList);
        } catch (PropertyVetoException e1) {
            e1.printStackTrace();
            throw new RuntimeException("Unexpected exception setting " + MolecularType.typeName + " list: " + e1.getMessage(), e1);
        }
    } else {
        // This should not be possible
        throw new RuntimeException("Owner or SimulationContext are null.");
    }
    int arrowIndex = inputString.indexOf("<->");
    boolean bReversible = true;
    if (arrowIndex < 0) {
        arrowIndex = inputString.indexOf("->");
        bReversible = false;
    }
    String left = inputString.substring(0, arrowIndex).trim();
    String right = inputString.substring(arrowIndex + (bReversible ? 3 : 2)).trim();
    if (left.length() == 0 && right.length() == 0) {
        return;
    }
    // we recover the original rule that generated the flattened reaction we now try to transform back into a fake rule
    BNGReaction reactionObject = reactionTableRow.getReactionObject();
    String name = reactionObject.getRuleName();
    if (name.contains(GeneratedReactionTableModel.reverse)) {
        name = name.substring(GeneratedReactionTableModel.reverse.length());
    }
    if (name.endsWith(ReactionRule.DirectHalf)) {
        name = name.substring(0, name.indexOf(ReactionRule.DirectHalf));
    }
    if (name.endsWith(ReactionRule.InverseHalf)) {
        name = name.substring(0, name.indexOf(ReactionRule.InverseHalf));
    }
    // get the name of the original structure from the original rule and make here another structure with the same name
    String strStructure = null;
    Structure ruleStructure;
    SimulationContext sc = owner.getSimulationContext();
    ReactionRule rr = sc.getModel().getRbmModelContainer().getReactionRule(name);
    if (rr != null && rr.getStructure() != null) {
        strStructure = rr.getStructure().getName();
    }
    if (strStructure != null) {
        if (tempModel.getStructure(strStructure) == null) {
            try {
                if (rr.getStructure().getTypeName().equals(Structure.TYPE_NAME_MEMBRANE)) {
                    tempModel.addMembrane(strStructure);
                } else {
                    tempModel.addFeature(strStructure);
                }
            } catch (ModelException | PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        ruleStructure = tempModel.getStructure(strStructure);
    } else {
        throw new RuntimeException("Failed to recover a Structure name from the Reaction Rule: " + name);
    }
    // making the fake rules just for display purpose, actually they are the flattened reactions resulted from bngl
    // the name is probably not unique, it's likely that many flattened reactions are derived from the same rule
    ReactionRule reactionRule = tempModel.getRbmModelContainer().createReactionRule(name, ruleStructure, bReversible);
    String regex = "[^!]\\+";
    String[] patterns = left.split(regex);
    for (String spString : patterns) {
        try {
            spString = spString.trim();
            // if compartments are present, we're cheating big time making some fake compartments just for compartment name display purposes
            SpeciesPattern speciesPattern = (SpeciesPattern) RbmUtils.parseSpeciesPattern(spString, tempModel);
            strStructure = RbmUtils.parseCompartment(spString, tempModel);
            speciesPattern.resolveBonds();
            Structure structure;
            if (strStructure != null) {
                if (tempModel.getStructure(strStructure) == null) {
                    if (sc.getModel().getStructure(strStructure).getTypeName().equals(Structure.TYPE_NAME_MEMBRANE)) {
                        tempModel.addMembrane(strStructure);
                    } else {
                        tempModel.addFeature(strStructure);
                    }
                }
                structure = tempModel.getStructure(strStructure);
            } else {
                // if nothing explicit for a participant, we use by default the structure of the rule
                structure = ruleStructure;
            }
            reactionRule.addReactant(new ReactantPattern(speciesPattern, structure));
        } catch (Throwable ex) {
            ex.printStackTrace();
            // error (red circle)
            SpeciesPatternLargeShape spls = new SpeciesPatternLargeShape(20, 20, -1, shapePanel, true, issueManager);
            reactantPatternShapeList.clear();
            productPatternShapeList.clear();
            reactantPatternShapeList.add(spls);
            shapePanel.repaint();
            return;
        }
    }
    patterns = right.split(regex);
    for (String spString : patterns) {
        try {
            spString = spString.trim();
            SpeciesPattern speciesPattern = (SpeciesPattern) RbmUtils.parseSpeciesPattern(spString, tempModel);
            strStructure = RbmUtils.parseCompartment(spString, tempModel);
            speciesPattern.resolveBonds();
            Structure structure;
            if (strStructure != null) {
                if (tempModel.getStructure(strStructure) == null) {
                    if (sc.getModel().getStructure(strStructure).getTypeName().equals(Structure.TYPE_NAME_MEMBRANE)) {
                        tempModel.addMembrane(strStructure);
                    } else {
                        tempModel.addFeature(strStructure);
                    }
                }
                structure = tempModel.getStructure(strStructure);
            } else {
                structure = ruleStructure;
            }
            // BNGLParser parser = new BNGLParser(new StringReader(sp));
            // ASTSpeciesPattern astSpeciesPattern = parser.SpeciesPattern();
            // BnglObjectConstructionVisitor constructionVisitor = new BnglObjectConstructionVisitor(tempModel, null, false);
            // SpeciesPattern speciesPattern = (SpeciesPattern) astSpeciesPattern.jjtAccept(constructionVisitor, null);
            // for(MolecularTypePattern mtp : speciesPattern.getMolecularTypePatterns()) {
            // mtp.setParticipantMatchLabel("*");
            // }
            // System.out.println(speciesPattern.toString());
            reactionRule.addProduct(new ProductPattern(speciesPattern, structure));
        } catch (Throwable ex) {
            ex.printStackTrace();
            // error (red circle)
            SpeciesPatternLargeShape spls = new SpeciesPatternLargeShape(20, 20, -1, shapePanel, true, issueManager);
            reactantPatternShapeList.clear();
            productPatternShapeList.clear();
            reactantPatternShapeList.add(spls);
            shapePanel.repaint();
            return;
        }
    }
    // ----------------------------------------------------------------------------------------------------
    List<ReactantPattern> rpList = reactionRule.getReactantPatterns();
    reactantPatternShapeList.clear();
    int xOffset = 20;
    int xOffsetRound = 20;
    if (rpList != null && rpList.size() > 0) {
        for (int i = 0; i < rpList.size(); i++) {
            SpeciesPattern sp = rpList.get(i).getSpeciesPattern();
            for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
                mtp.setParticipantMatchLabel("*");
            }
            SpeciesPatternLargeShape sps = new SpeciesPatternLargeShape(xOffset, 20, -1, sp, shapePanel, reactionRule, issueManager);
            // SpeciesPatternRoundShape sps = new SpeciesPatternRoundShape(xOffsetRound, 20, -1, sp, shapePanel, reactionRule);
            if (i < rpList.size() - 1) {
                sps.addEndText("+");
            } else {
                if (reactionRule.isReversible()) {
                    sps.addEndText("<->");
                } else {
                    sps.addEndText("->");
                }
            }
            xOffset = sps.getRightEnd() + 45;
            xOffsetRound = sps.getRightEnd() + 45;
            reactantPatternShapeList.add(sps);
        }
    }
    // space for the <-> sign
    xOffset += 15;
    List<ProductPattern> ppList = reactionRule.getProductPatterns();
    productPatternShapeList.clear();
    if (ppList != null && ppList.size() > 0) {
        for (int i = 0; i < ppList.size(); i++) {
            SpeciesPattern sp = ppList.get(i).getSpeciesPattern();
            for (MolecularTypePattern mtp : sp.getMolecularTypePatterns()) {
                mtp.setParticipantMatchLabel("*");
            }
            SpeciesPatternLargeShape sps = new SpeciesPatternLargeShape(xOffset, 20, -1, sp, shapePanel, reactionRule, issueManager);
            // SpeciesPatternRoundShape sps = new SpeciesPatternRoundShape(xOffset, 20, -1, sp, shapePanel, reactionRule);
            if (i < ppList.size() - 1) {
                sps.addEndText("+");
            }
            xOffset = sps.getRightEnd() + 45;
            productPatternShapeList.add(sps);
        }
    }
    Dimension preferredSize = new Dimension(xOffset + 90, 50);
    shapePanel.setPreferredSize(preferredSize);
    shapePanel.repaint();
}
Also used : SpeciesPatternLargeShape(cbit.vcell.graph.SpeciesPatternLargeShape) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern) Structure(cbit.vcell.model.Structure) ReactantPattern(cbit.vcell.model.ReactantPattern) ModelException(cbit.vcell.model.ModelException) ReactionRule(cbit.vcell.model.ReactionRule) ProductPattern(cbit.vcell.model.ProductPattern) Dimension(java.awt.Dimension) SimulationContext(cbit.vcell.mapping.SimulationContext) Point(java.awt.Point) PropertyVetoException(java.beans.PropertyVetoException) MolecularType(org.vcell.model.rbm.MolecularType) BNGReaction(cbit.vcell.bionetgen.BNGReaction) Model(cbit.vcell.model.Model) VCellSortTableModel(cbit.vcell.client.desktop.biomodel.VCellSortTableModel) BioModel(cbit.vcell.biomodel.BioModel) MolecularTypePattern(org.vcell.model.rbm.MolecularTypePattern)

Example 2 with Model

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

the class GeneratedReactionTableModel method getModel.

private Model getModel() {
    if (model == null) {
        try {
            model = new Model("MyTempModel");
            model.addFeature("c0");
        } catch (ModelException | PropertyVetoException e1) {
            e1.printStackTrace();
        }
    }
    return model;
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) ModelException(cbit.vcell.model.ModelException) AutoCompleteTableModel(org.vcell.util.gui.AutoCompleteTableModel) VCellSortTableModel(cbit.vcell.client.desktop.biomodel.VCellSortTableModel) Model(cbit.vcell.model.Model)

Example 3 with Model

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

the class PathwayMapping method createSpeciesContextFromBioPaxObject.

private SpeciesContext createSpeciesContextFromBioPaxObject(BioModel bioModel, PhysicalEntity bioPaxObject, boolean addSubunits) throws Exception {
    String name;
    if (bioPaxObject.getName().size() == 0) {
        name = getSafetyName(bioPaxObject.getID());
    } else {
        name = getSafetyName(bioPaxObject.getName().get(0));
    }
    Model model = bioModel.getModel();
    SpeciesContext freeSpeciesContext = model.getSpeciesContext(name);
    if (freeSpeciesContext == null) {
        // create the new speciesContex Object, and link it to the corresponding pathway object
        if (model.getSpecies(name) == null) {
            freeSpeciesContext = model.createSpeciesContext(model.getStructures()[0]);
        } else {
            freeSpeciesContext = new SpeciesContext(model.getSpecies(name), model.getStructures()[0]);
        }
        freeSpeciesContext.setName(name);
        RelationshipObject newRelationship = new RelationshipObject(freeSpeciesContext, bioPaxObject);
        bioModel.getRelationshipModel().addRelationshipObject(newRelationship);
    } else {
        // if it is in the bioModel, then check whether it links to pathway object or not
        HashSet<RelationshipObject> linkedReObjects = bioModel.getRelationshipModel().getRelationshipObjects(freeSpeciesContext);
        if (linkedReObjects != null) {
            boolean flag = true;
            for (RelationshipObject reObject : linkedReObjects) {
                if (reObject.getBioPaxObject() == bioPaxObject) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                RelationshipObject newSpeciesContext = new RelationshipObject(freeSpeciesContext, bioPaxObject);
                bioModel.getRelationshipModel().addRelationshipObject(newSpeciesContext);
            }
        } else {
            RelationshipObject newSpeciesContext = new RelationshipObject(freeSpeciesContext, bioPaxObject);
            bioModel.getRelationshipModel().addRelationshipObject(newSpeciesContext);
        }
    }
    if (!freeSpeciesContext.hasSpeciesPattern()) {
        SpeciesPattern sp = generateSpeciesPattern(bioModel, bioPaxObject, addSubunits);
        if (sp != null && !sp.getMolecularTypePatterns().isEmpty()) {
            freeSpeciesContext.setSpeciesPattern(sp);
        }
    }
    return freeSpeciesContext;
}
Also used : BioModel(cbit.vcell.biomodel.BioModel) Model(cbit.vcell.model.Model) SpeciesContext(cbit.vcell.model.SpeciesContext) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern)

Example 4 with Model

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

the class PathwayMapping method createSpeciesContextFromTableRow.

private SpeciesContext createSpeciesContextFromTableRow(BioModel bioModel, PhysicalEntity bioPaxObject, double stoich, String id, String location, boolean addSubunits) throws Exception {
    // use user defined id as the name of the speciesContext
    String safeId = getSafetyName(id);
    String name;
    if (bioPaxObject.getName().size() == 0) {
        name = getSafetyName(bioPaxObject.getID());
    } else {
        name = getSafetyName(bioPaxObject.getName().get(0));
    }
    Model model = bioModel.getModel();
    SpeciesContext freeSpeciesContext = model.getSpeciesContext(safeId);
    if (freeSpeciesContext == null) {
        // create the new speciesContex Object, and link it to the corresponding pathway object
        if (model.getSpecies(name) == null) {
            freeSpeciesContext = model.createSpeciesContext(model.getStructure(location));
        } else {
            freeSpeciesContext = new SpeciesContext(model.getSpecies(name), model.getStructure(location));
        }
        freeSpeciesContext.setName(safeId);
        RelationshipObject newRelationship = new RelationshipObject(freeSpeciesContext, bioPaxObject);
        bioModel.getRelationshipModel().addRelationshipObject(newRelationship);
    } else {
        // if it is in the bioModel, then check whether it links to pathway object or not
        HashSet<RelationshipObject> linkedReObjects = bioModel.getRelationshipModel().getRelationshipObjects(freeSpeciesContext);
        if (linkedReObjects != null) {
            boolean flag = true;
            for (RelationshipObject reObject : linkedReObjects) {
                if (reObject.getBioPaxObject() == bioPaxObject) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                RelationshipObject newSpeciesContext = new RelationshipObject(freeSpeciesContext, bioPaxObject);
                bioModel.getRelationshipModel().addRelationshipObject(newSpeciesContext);
            }
        } else {
            RelationshipObject newSpeciesContext = new RelationshipObject(freeSpeciesContext, bioPaxObject);
            bioModel.getRelationshipModel().addRelationshipObject(newSpeciesContext);
        }
    }
    // else if it's proteine, small molecule, dna, etc the sp will consist of one molecule
    if (!freeSpeciesContext.hasSpeciesPattern()) {
        SpeciesPattern sp = generateSpeciesPattern(bioModel, bioPaxObject, addSubunits);
        if (sp != null && !sp.getMolecularTypePatterns().isEmpty()) {
            freeSpeciesContext.setSpeciesPattern(sp);
            sp.initializeBonds(MolecularComponentPattern.BondType.None);
        }
    }
    return freeSpeciesContext;
}
Also used : BioModel(cbit.vcell.biomodel.BioModel) Model(cbit.vcell.model.Model) SpeciesContext(cbit.vcell.model.SpeciesContext) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern)

Example 5 with Model

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

the class SBMLExporter method addCompartments.

/**
 * addCompartments comment.
 * @throws XMLStreamException
 * @throws SbmlException
 */
protected void addCompartments() throws XMLStreamException, SbmlException {
    Model vcModel = vcBioModel.getModel();
    cbit.vcell.model.Structure[] vcStructures = vcModel.getStructures();
    for (int i = 0; i < vcStructures.length; i++) {
        Compartment sbmlCompartment = sbmlModel.createCompartment();
        sbmlCompartment.setId(TokenMangler.mangleToSName(vcStructures[i].getName()));
        sbmlCompartment.setName(vcStructures[i].getName());
        VCUnitDefinition sbmlSizeUnit = null;
        StructureTopology structTopology = getSelectedSimContext().getModel().getStructureTopology();
        Structure parentStructure = structTopology.getParentStructure(vcStructures[i]);
        if (vcStructures[i] instanceof Feature) {
            sbmlCompartment.setSpatialDimensions(3);
            String outside = null;
            if (parentStructure != null) {
                outside = TokenMangler.mangleToSName(parentStructure.getName());
            }
            if (outside != null) {
                if (outside.length() > 0) {
                    sbmlCompartment.setOutside(outside);
                }
            }
            sbmlSizeUnit = sbmlExportSpec.getVolumeUnits();
            UnitDefinition unitDefn = getOrCreateSBMLUnit(sbmlSizeUnit);
            sbmlCompartment.setUnits(unitDefn);
        } else if (vcStructures[i] instanceof Membrane) {
            Membrane vcMembrane = (Membrane) vcStructures[i];
            sbmlCompartment.setSpatialDimensions(2);
            Feature outsideFeature = structTopology.getOutsideFeature(vcMembrane);
            if (outsideFeature != null) {
                sbmlCompartment.setOutside(TokenMangler.mangleToSName(outsideFeature.getName()));
                sbmlSizeUnit = sbmlExportSpec.getAreaUnits();
                UnitDefinition unitDefn = getOrCreateSBMLUnit(sbmlSizeUnit);
                sbmlCompartment.setUnits(unitDefn);
            } else if (lg.isEnabledFor(Level.WARN)) {
                lg.warn(this.sbmlModel.getName() + " membrame " + vcMembrane.getName() + " has not outside feature");
            }
        }
        sbmlCompartment.setConstant(true);
        StructureMapping vcStructMapping = getSelectedSimContext().getGeometryContext().getStructureMapping(vcStructures[i]);
        try {
            if (vcStructMapping.getSizeParameter().getExpression() != null) {
                sbmlCompartment.setSize(vcStructMapping.getSizeParameter().getExpression().evaluateConstant());
            } else {
            // really no need to set sizes of compartments in spatial ..... ????
            // throw new RuntimeException("Compartment size not set for compartment \"" + vcStructures[i].getName() + "\" ; Please set size and try exporting again.");
            }
        } catch (cbit.vcell.parser.ExpressionException e) {
            // If it is in the catch block, it means that the compartment size was probably not a double, but an assignment.
            // Check if the expression for the compartment size is not null and add it as an assignment rule.
            Expression sizeExpr = vcStructMapping.getSizeParameter().getExpression();
            if (sizeExpr != null) {
                ASTNode ruleFormulaNode = getFormulaFromExpression(sizeExpr);
                AssignmentRule assignRule = sbmlModel.createAssignmentRule();
                assignRule.setVariable(vcStructures[i].getName());
                assignRule.setMath(ruleFormulaNode);
                // If compartmentSize is specified by an assignment rule, the 'constant' field should be set to 'false' (default - true).
                sbmlCompartment.setConstant(false);
                sbmlModel.addRule(assignRule);
            }
        }
        // Add the outside compartment of given compartment as annotation to the compartment.
        // This is required later while trying to read in compartments ...
        Element sbmlImportRelatedElement = null;
        // if (parentStructure != null) {
        // sbmlImportRelatedElement = new Element(XMLTags.VCellRelatedInfoTag, sbml_vcml_ns);
        // Element compartmentElement = new Element(XMLTags.OutsideCompartmentTag, sbml_vcml_ns);
        // compartmentElement.setAttribute(XMLTags.NameAttrTag, TokenMangler.mangleToSName(parentStructure.getName()));
        // sbmlImportRelatedElement.addContent(compartmentElement);
        // }
        // Get annotation (RDF and non-RDF) for reactionStep from SBMLAnnotationUtils
        sbmlAnnotationUtil.writeAnnotation(vcStructures[i], sbmlCompartment, sbmlImportRelatedElement);
        // Now set notes,
        sbmlAnnotationUtil.writeNotes(vcStructures[i], sbmlCompartment);
    }
}
Also used : StructureTopology(cbit.vcell.model.Model.StructureTopology) Compartment(org.sbml.jsbml.Compartment) AssignmentRule(org.sbml.jsbml.AssignmentRule) Element(org.jdom.Element) ExpressionException(cbit.vcell.parser.ExpressionException) Feature(cbit.vcell.model.Feature) StructureMapping(cbit.vcell.mapping.StructureMapping) InteriorPoint(org.sbml.jsbml.ext.spatial.InteriorPoint) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) Model(cbit.vcell.model.Model) BioModel(cbit.vcell.biomodel.BioModel) ASTNode(org.sbml.jsbml.ASTNode) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) UnitDefinition(org.sbml.jsbml.UnitDefinition)

Aggregations

Model (cbit.vcell.model.Model)107 BioModel (cbit.vcell.biomodel.BioModel)53 SpeciesContext (cbit.vcell.model.SpeciesContext)44 Expression (cbit.vcell.parser.Expression)42 Structure (cbit.vcell.model.Structure)35 PropertyVetoException (java.beans.PropertyVetoException)34 SimulationContext (cbit.vcell.mapping.SimulationContext)27 ReactionStep (cbit.vcell.model.ReactionStep)27 ModelParameter (cbit.vcell.model.Model.ModelParameter)23 ExpressionException (cbit.vcell.parser.ExpressionException)22 ArrayList (java.util.ArrayList)22 SpeciesContextSpec (cbit.vcell.mapping.SpeciesContextSpec)21 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)19 MathDescription (cbit.vcell.math.MathDescription)17 Feature (cbit.vcell.model.Feature)16 ModelException (cbit.vcell.model.ModelException)16 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)16 SubVolume (cbit.vcell.geometry.SubVolume)15 Parameter (cbit.vcell.model.Parameter)15 StructureMapping (cbit.vcell.mapping.StructureMapping)14