Search in sources :

Example 1 with ReactantPattern

use of cbit.vcell.model.ReactantPattern 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 ReactantPattern

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

the class XmlReader method getRbmReactantPatternsList.

private void getRbmReactantPatternsList(Element e, ReactionRule r, Model newModel) {
    if (e != null) {
        List<Element> rpChildren = e.getChildren(XMLTags.RbmReactantPatternTag, vcNamespace);
        for (Element rpElement : rpChildren) {
            Structure structure = null;
            String structureName = rpElement.getAttributeValue(XMLTags.StructureAttrTag);
            if (structureName == null || structureName.isEmpty()) {
                // the tag is missing
                throw new RuntimeException("XMLReader: structure missing for reaction rule pattern.");
            } else {
                structure = newModel.getStructure(structureName);
            }
            Element spe = rpElement.getChild(XMLTags.RbmSpeciesPatternTag, vcNamespace);
            SpeciesPattern s = getSpeciesPattern(spe, newModel);
            if (s != null) {
                r.addReactant(new ReactantPattern(s, structure), false);
            }
        }
        // older models have the species pattern saved directly and using the structure or the rule
        List<Element> spChildren = e.getChildren(XMLTags.RbmSpeciesPatternTag, vcNamespace);
        for (Element element : spChildren) {
            SpeciesPattern s = getSpeciesPattern(element, newModel);
            if (s != null) {
                r.addReactant(new ReactantPattern(s, r.getStructure()), false);
            }
        }
    }
}
Also used : Element(org.jdom.Element) Structure(cbit.vcell.model.Structure) ParticleSpeciesPattern(cbit.vcell.math.ParticleSpeciesPattern) VolumeParticleSpeciesPattern(cbit.vcell.math.VolumeParticleSpeciesPattern) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern) ReactantPattern(cbit.vcell.model.ReactantPattern)

Example 3 with ReactantPattern

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

the class Xmlproducer method getXML.

private Element getXML(ReactionRule param) {
    Element e = new Element(XMLTags.RbmReactionRuleTag);
    e.setAttribute(XMLTags.NameAttrTag, mangle(param.getName()));
    e.setAttribute(XMLTags.StructureAttrTag, mangle(param.getStructure().getName()));
    e.setAttribute(XMLTags.RbmReactionRuleLabelTag, mangle(param.getName()));
    boolean reversible = param.isReversible();
    e.setAttribute(XMLTags.RbmReactionRuleReversibleTag, String.valueOf(reversible));
    // if (param.getKineticLaw().getLocalParameterValue(RbmKineticLawParameterType.MassActionForwardRate)!=null){
    // e.setAttribute(XMLTags.RbmMassActionKfTag, mangleExpression(param.getKineticLaw().getLocalParameterValue(RbmKineticLawParameterType.MassActionForwardRate)));
    // }
    // if (param.getKineticLaw().getLocalParameterValue(RbmKineticLawParameterType.MassActionReverseRate)!=null){
    // e.setAttribute(XMLTags.RbmMassActionKrTag, mangleExpression(param.getKineticLaw().getLocalParameterValue(RbmKineticLawParameterType.MassActionReverseRate)));
    // }
    // if (param.getKineticLaw().getLocalParameterValue(RbmKineticLawParameterType.MichaelisMentenKcat)!=null){
    // e.setAttribute(XMLTags.RbmMichaelisMentenKcatTag, mangleExpression(param.getKineticLaw().getLocalParameterValue(RbmKineticLawParameterType.MichaelisMentenKcat)));
    // }
    // if (param.getKineticLaw().getLocalParameterValue(RbmKineticLawParameterType.MichaelisMentenKm)!=null){
    // e.setAttribute(XMLTags.RbmMichaelisMentenKmTag, mangleExpression(param.getKineticLaw().getLocalParameterValue(RbmKineticLawParameterType.MichaelisMentenKm)));
    // }
    // if (param.getKineticLaw().getLocalParameterValue(RbmKineticLawParameterType.SaturableKs)!=null){
    // e.setAttribute(XMLTags.RbmSaturableKsTag, mangleExpression(param.getKineticLaw().getLocalParameterValue(RbmKineticLawParameterType.SaturableKs)));
    // }
    // if (param.getKineticLaw().getLocalParameterValue(RbmKineticLawParameterType.SaturableVmax)!=null){
    // e.setAttribute(XMLTags.RbmSaturableVmaxTag, mangleExpression(param.getKineticLaw().getLocalParameterValue(RbmKineticLawParameterType.SaturableVmax)));
    // }
    List<ReactantPattern> reactantPatterns = param.getReactantPatterns();
    if (!reactantPatterns.isEmpty()) {
        Element reactantPatternsListElement = new Element(XMLTags.RbmReactantPatternsListTag);
        for (ReactantPattern rp : reactantPatterns) {
            reactantPatternsListElement.addContent(getXML(rp));
        }
        e.addContent(reactantPatternsListElement);
    }
    List<ProductPattern> productPatterns = param.getProductPatterns();
    if (!productPatterns.isEmpty()) {
        Element productPatternsListElement = new Element(XMLTags.RbmProductPatternsListTag);
        for (ProductPattern pp : productPatterns) {
            productPatternsListElement.addContent(getXML(pp));
        }
        e.addContent(productPatternsListElement);
    }
    // apparently the molecularTypeMapping list is not being used
    // Add kinetics
    e.addContent(getXML(param.getKineticLaw()));
    return e;
}
Also used : ProductPattern(cbit.vcell.model.ProductPattern) Element(org.jdom.Element) ReactantPattern(cbit.vcell.model.ReactantPattern)

Example 4 with ReactantPattern

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

the class RbmUtils method toBnglStringShort.

public static String toBnglStringShort(ReactionRule reactionRule, CompartmentMode compartmentMode) {
    StringBuilder buffer = new StringBuilder();
    List<ReactantPattern> reactants = reactionRule.getReactantPatterns();
    for (int i = 0; i < reactants.size(); ++i) {
        if (i > 0) {
            buffer.append(" + ");
        }
        ReactantPattern rp = reactants.get(i);
        if (compartmentMode == CompartmentMode.show) {
            buffer.append("@" + rp.getStructure().getName() + ":");
            buffer.append(toBnglString(rp.getSpeciesPattern(), null, CompartmentMode.hide, 0));
        } else if (compartmentMode == CompartmentMode.asSite) {
            buffer.append(toBnglString(rp.getSpeciesPattern(), rp.getStructure(), CompartmentMode.asSite, 0));
        } else {
            buffer.append(toBnglString(rp.getSpeciesPattern(), null, CompartmentMode.hide, 0));
        }
    }
    buffer.append(reactionRule.isReversible() ? " <-> " : " -> ");
    List<ProductPattern> products = reactionRule.getProductPatterns();
    for (int i = 0; i < products.size(); ++i) {
        if (i > 0) {
            buffer.append(" + ");
        }
        ProductPattern pp = products.get(i);
        if (compartmentMode == CompartmentMode.show) {
            buffer.append("@" + pp.getStructure().getName() + ":");
            // because we already set the compartment
            buffer.append(toBnglString(pp.getSpeciesPattern(), null, CompartmentMode.hide, 0));
        } else if (compartmentMode == CompartmentMode.asSite) {
            // last arg is index of product
            buffer.append(toBnglString(pp.getSpeciesPattern(), pp.getStructure(), CompartmentMode.asSite, i + 1));
        } else {
            buffer.append(toBnglString(pp.getSpeciesPattern(), null, CompartmentMode.hide, 0));
        }
    }
    return buffer.toString();
}
Also used : ProductPattern(cbit.vcell.model.ProductPattern) ReactantPattern(cbit.vcell.model.ReactantPattern)

Example 5 with ReactantPattern

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

the class SpeciesPatternRoundShape method paintCompartment.

public void paintCompartment(Graphics g) {
    Color structureColor = Color.black;
    Structure structure = null;
    if (owner instanceof ReactionRule && !speciesShapes.isEmpty()) {
        ReactionRule rr = (ReactionRule) owner;
        ReactantPattern rp = rr.getReactantPattern(sp);
        ProductPattern pp = rr.getProductPattern(sp);
        if (rp != null) {
            structure = rp.getStructure();
        } else if (pp != null) {
            structure = pp.getStructure();
        } else {
            structure = ((ReactionRule) owner).getStructure();
        }
    } else if (owner instanceof SpeciesContext && ((SpeciesContext) owner).hasSpeciesPattern()) {
        structure = ((SpeciesContext) owner).getStructure();
        structureColor = Color.gray;
    } else if (owner instanceof RbmObservable && !speciesShapes.isEmpty()) {
        structure = ((RbmObservable) owner).getStructure();
    } else {
        // other things don't have structure
        return;
    }
    if (structure == null) {
        return;
    }
    // don't have a real structure, so we show them structureless (for now)
    if (structure.getName() == null) {
        return;
    }
    Graphics2D g2 = (Graphics2D) g;
    Color colorOld = g2.getColor();
    Paint paintOld = g2.getPaint();
    Font fontOld = g2.getFont();
    Font font;
    // width of compartment shape, adjusted continuously based on zoom factor
    int w;
    String name = structure.getName();
    int z = shapePanel.getZoomFactor();
    if (z > -3) {
        font = fontOld.deriveFont(Font.BOLD);
        g.setFont(font);
        w = 46 + 3 * z;
        name = buildCompartmentName(g, name, "..", w);
    } else if (z < LargeShapeCanvas.SmallestZoomFactorWithText) {
        font = fontOld.deriveFont(fontOld.getSize2D() * 0.8f);
        g.setFont(font);
        w = 20;
        name = buildCompartmentName(g, name, ".", w);
    } else {
        font = fontOld;
        g.setFont(font);
        w = 44 + 3 * z;
        name = buildCompartmentName(g, name, "..", w);
    }
    // a bit darker for border
    Color darker = Color.gray;
    Rectangle2D border = new Rectangle2D.Double(xPos - 9, yPos - 4, w, 58);
    g2.setColor(darker);
    g2.draw(border);
    Color lighter = new Color(224, 224, 224);
    Rectangle2D filling = new Rectangle2D.Double(xPos - 9, yPos - 3, w, 57);
    g2.setPaint(lighter);
    g2.fill(filling);
    g.setColor(structureColor);
    g2.drawString(name, xPos - 4, yPos + 48);
    g2.setFont(fontOld);
    g2.setPaint(paintOld);
    g2.setColor(colorOld);
}
Also used : ReactionRule(cbit.vcell.model.ReactionRule) ProductPattern(cbit.vcell.model.ProductPattern) Color(java.awt.Color) RbmObservable(cbit.vcell.model.RbmObservable) Rectangle2D(java.awt.geom.Rectangle2D) SpeciesContext(cbit.vcell.model.SpeciesContext) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint) Font(java.awt.Font) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint) Graphics2D(java.awt.Graphics2D) Structure(cbit.vcell.model.Structure) ReactantPattern(cbit.vcell.model.ReactantPattern)

Aggregations

ReactantPattern (cbit.vcell.model.ReactantPattern)22 ProductPattern (cbit.vcell.model.ProductPattern)20 ReactionRule (cbit.vcell.model.ReactionRule)17 SpeciesPattern (org.vcell.model.rbm.SpeciesPattern)12 Structure (cbit.vcell.model.Structure)10 ArrayList (java.util.ArrayList)8 SpeciesContext (cbit.vcell.model.SpeciesContext)7 MolecularType (org.vcell.model.rbm.MolecularType)6 MolecularTypePattern (org.vcell.model.rbm.MolecularTypePattern)6 RbmObservable (cbit.vcell.model.RbmObservable)5 ReactionStep (cbit.vcell.model.ReactionStep)5 ModelException (cbit.vcell.model.ModelException)4 RbmKineticLaw (cbit.vcell.model.RbmKineticLaw)4 Point (java.awt.Point)4 PropertyVetoException (java.beans.PropertyVetoException)4 List (java.util.List)4 SpeciesPatternSmallShape (cbit.vcell.graph.SpeciesPatternSmallShape)3 Model (cbit.vcell.model.Model)3 Product (cbit.vcell.model.Product)3 Reactant (cbit.vcell.model.Reactant)3