Search in sources :

Example 1 with BNGLParser

use of org.vcell.model.bngl.BNGLParser in project vcell by virtualcell.

the class SpeciesGraphIsomorphismInspector method main.

// public static class SpeciesPatternExpression {
// private final ASTSpeciesPattern astSpeciesPattern;
// private final MolecularPatternExpression[] molecularPatternExpression;
// 
// public SpeciesPatternExpression(String speciesPatternString) throws ParseException{
// BNGLParser parser = new BNGLParser(new StringReader(speciesPatternString));
// this.astSpeciesPattern = parser.SpeciesPattern();
// .... create
// this.molecularPatternExpression = new MolecularPatternExpression[num];
// ddldl
// }
// 
// public MolecularPatternExpression[] getMolecularPatternExpressions(){
// return this.molecularPatternExpression;
// }
// }
// 
// public static class MolecularPatternExpression {
// 
// private final ComponentPatternExpression[] componentPatternExpression;
// 
// public MolecularPatternExpression(String speciesPatternString) throws ParseException{
// BNGLParser parser = new BNGLParser(new StringReader(speciesPatternString));
// this.astSpeciesPattern = parser.SpeciesPattern();
// }
// 
// public String getName(){
// return ...
// }
// 
// }
// 
// public static class ComponentPatternExpression {
// ASTMolecularComponentPattern pattern;
// 
// private final MolecularPatternExpression[] molecularPatternExpression;
// 
// public MolecularPatternExpression(String speciesPatternString) throws ParseException{
// BNGLParser parser = new BNGLParser(new StringReader(speciesPatternString));
// this.astSpeciesPattern = parser.SpeciesPattern();
// }
// 
// public String getName(){
// return pattern.getName();
// }
// 
// public boolean hasBond(){
// for (int i=0;i<pattern.jjtGetNumChildren(); i++){
// Node child = pattern.jjtGetChild(i);
// if (child instanceof ASTBondState){
// ((ASTBondState)child).getState()
// }
// }
// 
// }
// 
// public boolean hasState(){
// 
// }
// }
// =================================================================================================================
public static void main(String[] argv) {
    try {
        // match (exact)
        String a = "EGF(rb!1).EGF(rb!2).EGFR(ecd!1,tmd!3,y1068~p,y1173~u).EGFR(ecd!2,tmd!3,y1068~u,y1173~p)";
        // match (iso)
        String b = "EGF(rb!1).EGF(rb!2).EGFR(ecd!1,tmd!3,y1068~u,y1173~p).EGFR(ecd!2,tmd!3,y1068~p,y1173~u)";
        // no match (slightly different in state)
        String c = "EGF(rb!1).EGF(rb!2).EGFR(ecd!1,tmd!3,y1068~u,y1173~p).EGFR(ecd!2,tmd!3,y1068~p,y1173~v)";
        // String c = "EGF(rb~Y!1).EGF(rb~pY!1).EGFR(ecd!2,tmd!3,y1068~u,y1173~p).EGFR(ecd!2,tmd!3,y1068~p,y1173~u)";
        // no match (very different)
        String d = "A(s,t!+,v!1).B(s~Y,t~Y!+,u~Y!?,v~Y!1)";
        // String d = "A(v!1).B(v~Y!1)";
        List<BNGSpecies> list = new ArrayList<>();
        BNGLParser parser = new BNGLParser(new StringReader(a));
        // SpeciesPatternExpression spExpression = new SpeciesPatternExpression(a);
        BNGSpecies aa = new BNGComplexSpecies(a, new Expression("0.0"), 1);
        BNGSpecies bb = new BNGComplexSpecies(b, new Expression("0.0"), 2);
        BNGSpecies cc = new BNGComplexSpecies(c, new Expression("0.0"), 3);
        BNGSpecies dd = new BNGComplexSpecies(d, new Expression("0.0"), 4);
        list.add(aa);
        list.add(bb);
        list.add(cc);
        list.add(dd);
        SpeciesGraphIsomorphismInspector sii = new SpeciesGraphIsomorphismInspector();
        BNGSpecies ours = aa;
        for (BNGSpecies theirs : list) {
            if (sii.isIsomorphism(ours, theirs)) {
                System.out.println("Found match: " + ours.getName() + " and " + theirs.getName());
            } else {
                System.out.println("Mismatch: " + ours.getName() + " and " + theirs.getName());
            }
        }
        System.out.println("done");
    } catch (Throwable e) {
        System.out.println("Uncaught exception in SpeciesIsomorphismInspector.main()");
        e.printStackTrace(System.out);
    }
}
Also used : Expression(cbit.vcell.parser.Expression) BNGLParser(org.vcell.model.bngl.BNGLParser) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader)

Example 2 with BNGLParser

use of org.vcell.model.bngl.BNGLParser in project vcell by virtualcell.

the class RbmUtils method parseMolecularType.

public static MolecularType parseMolecularType(String inputString) throws ParseException {
    try {
        BNGLParser parser = new BNGLParser(new StringReader(inputString));
        ASTMolecularTypePattern astMolecularPattern = parser.MolecularTypePattern();
        BnglObjectConstructionVisitor constructionVisitor = new BnglObjectConstructionVisitor();
        MolecularType molecularType = (MolecularType) astMolecularPattern.jjtAccept(constructionVisitor, null);
        return molecularType;
    } catch (Throwable ex) {
        ex.printStackTrace();
        throw new ParseException(ex.getMessage());
    }
}
Also used : ParticleMolecularType(cbit.vcell.math.ParticleMolecularType) BNGLParser(org.vcell.model.bngl.BNGLParser) ASTMolecularTypePattern(org.vcell.model.bngl.ASTMolecularTypePattern) StringReader(java.io.StringReader) ParseException(org.vcell.model.bngl.ParseException)

Example 3 with BNGLParser

use of org.vcell.model.bngl.BNGLParser in project vcell by virtualcell.

the class RbmUtils method importBnglFile.

public static ASTModel importBnglFile(BufferedReader br) throws ParseException {
    try {
        // remove all the comments
        reactionRuleNames.clear();
        // just the prologue
        StringBuilder sPrologBuilder = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        ArrayList<BnglComment> comments = new ArrayList<BnglComment>();
        int lineNumber = 0;
        boolean bEscapingExpressionBegin = false;
        boolean inProlog = true;
        BnglLocation location = BnglLocation.OutsideBlock;
        Set<String> compartments = new HashSet<>();
        Map<String, String> anchors = new HashMap<>();
        while (true) {
            String line = br.readLine();
            if (line == null) {
                break;
            }
            line = line.trim();
            line = applySyntaxCorrections(line);
            // we capture all the prologue and insert in in the BioModel notes
            if (line.startsWith("begin model") || line.startsWith("begin parameters")) {
                inProlog = false;
            }
            if (inProlog == true) {
                sPrologBuilder.append(line);
                sPrologBuilder.append("\n");
                sb.append("\n");
                lineNumber++;
                continue;
            }
            if (line.startsWith("version")) {
                // we include the version in the prologue even if it shows up later in the code
                sPrologBuilder.append(line);
                sPrologBuilder.append("\n");
                sb.append("\n");
                lineNumber++;
                continue;
            }
            if (line.length() == 0 || line.charAt(0) == '#') {
                if (line.length() > 0 && line.charAt(0) == '#') {
                    BnglComment bc = new BnglComment("empty", lineNumber, location, line);
                    comments.add(bc);
                }
                sb.append("\n");
                lineNumber++;
                continue;
            }
            if (line.endsWith(":\\")) {
                // we'll try to treat single line labels as comments
                sb.append("\n");
                lineNumber++;
                continue;
            } else if (line.endsWith("\\")) {
                // concatenation with next line, we make one single continuous line
                String line2 = br.readLine();
                if (line2 == null) {
                    break;
                }
                line2 = line2.trim();
                // we don't try to get too fancy here, we'll just assume there are 2 lines of code somewhere
                // within a block and we simply concatenate them; if it's more than 2 we're out of luck
                int concatenationTokenIndex = line.lastIndexOf("\\");
                line = line.substring(0, concatenationTokenIndex);
                if (line.endsWith(" ")) {
                    line = line + line2;
                } else {
                    line = line + " " + line2;
                }
                // we add an empty line so that the total number of lines in the document won't change
                sb.append("\n");
                lineNumber++;
            }
            if (line.startsWith("end parameters") || line.startsWith("end seed species")) {
                // TODO: position sensitive, do not move this 'if' down
                bEscapingExpressionBegin = false;
            }
            // remove comments which follow some code
            int commentIndex = line.indexOf('#');
            if (commentIndex >= 0) {
                BnglComment bc = new BnglComment("item ", lineNumber, location, line.substring(commentIndex));
                comments.add(bc);
                line = line.substring(0, commentIndex);
            }
            int labelEndIndex = line.indexOf(":");
            if (labelEndIndex > 0 && line.substring(0, labelEndIndex).contains("@")) {
                // this is not a label, is a compartment
                ;
            } else {
                if (labelEndIndex == -1) {
                    // there may still be a label present, just without the ":"
                    StringTokenizer st = new StringTokenizer(line);
                    String nextToken = st.nextToken();
                    String prefix = "";
                    if (nextToken.matches("[0-9]+")) {
                        // we transform them in proper labels by adding a letter prefix and the ":" suffix
                        if (location == BnglLocation.ReactionRule) {
                            // labels can't start with a number, so we add "r" as a prefix
                            prefix = "r";
                        } else {
                            // this just for consistency, below we'll actually remove all labels except the reaction rule ones
                            prefix = "l";
                        }
                        line = line.replaceFirst(nextToken, prefix + nextToken + ":");
                        labelEndIndex = line.indexOf(":");
                    }
                }
                // remove labels except for reaction rule labels
                if (labelEndIndex >= 0 && line.length() > labelEndIndex) {
                    // we're certain they cannot be inside a comment since comments were removed already (above)
                    if (location == BnglLocation.ReactionRule) {
                        String reactionRuleName = line.substring(0, labelEndIndex);
                        if (reactionRuleNames.indexOf(reactionRuleName) != -1) {
                            // already in use
                            line = line.substring(labelEndIndex);
                            reactionRuleName = reactionRuleName + "_" + lineNumber;
                            line = reactionRuleName + line;
                        }
                        reactionRuleNames.add(reactionRuleName);
                    } else {
                        line = line.substring(labelEndIndex + 1);
                    }
                }
            }
            // there may be some blanks hanging around
            line = line.trim();
            if (line.length() == 0) {
                // line may be empty now
                sb.append("\n");
                lineNumber++;
                continue;
            }
            if (bEscapingExpressionBegin) {
                StringTokenizer st = new StringTokenizer(line);
                String nextToken = st.nextToken();
                if (Character.isDigit(nextToken.charAt(0))) {
                    sb.append(nextToken + " ");
                    nextToken = st.nextToken();
                }
                sb.append(nextToken);
                sb.append(" {");
                while (st.hasMoreTokens()) {
                    sb.append(st.nextToken());
                }
                sb.append("}");
                sb.append("\n");
            } else {
                sb.append(line + "\n");
                if (location == BnglLocation.Compartment) {
                    // make a list with all the compartment names, we'll need them during phase 2 to remove the "containing
                    // compartment" (which we can't deal with) and to build the escape expression for the volume using the  { }
                    StringTokenizer st = new StringTokenizer(line);
                    // first token is compartment name
                    String firstToken = st.nextToken();
                    if (!compartments.add(firstToken)) {
                        System.out.println("BnglPreprocessor: Duplicate compartment name!" + firstToken);
                    }
                }
            }
            if (line.startsWith("begin parameters") || line.startsWith("begin seed species")) {
                // TODO: position sensitive, do not move this 'if' up
                bEscapingExpressionBegin = true;
            }
            location = markBlock(location, line);
            lineNumber++;
        }
        br.close();
        ListIterator<BnglComment> it = comments.listIterator();
        while (it.hasNext()) {
            BnglComment bc = it.next();
            System.out.println(bc.entityName + " " + bc.location + " " + bc.comment);
        }
        // pass 2, deal with comments and labels
        String cleanedBngl = sb.toString();
        // System.out.println(cleanedBngl);
        br = new BufferedReader(new StringReader(cleanedBngl));
        sb = new StringBuilder();
        lineNumber = 0;
        location = BnglLocation.OutsideBlock;
        while (true) {
            String line = br.readLine();
            if (line == null) {
                // end of document
                break;
            }
            if (line.isEmpty()) {
                sb.append("\n");
                lineNumber++;
                continue;
            }
            BnglLocation newLocation = markBlock(location, line);
            if ((newLocation == BnglLocation.ReactionRule) && (location == BnglLocation.ReactionRule)) {
                int labelEndIndex = line.indexOf(":");
                if (labelEndIndex == -1) {
                    // no label on this reaction rule, so we make one
                    String reactionRuleName = generateReactionRuleName();
                    if (reactionRuleNames.indexOf(reactionRuleName) != -1) {
                        // already in use
                        reactionRuleName = reactionRuleName + "_" + lineNumber;
                    }
                    reactionRuleNames.add(reactionRuleName);
                    line = reactionRuleName + ":" + line;
                    sb.append(line + "\n");
                } else {
                    // rule has label, we keep it as it is
                    sb.append(line + "\n");
                }
            } else if ((newLocation == BnglLocation.Compartment) && (location == BnglLocation.Compartment)) {
                // remove the optional "containing compartment" if specified
                System.out.println(line);
                StringTokenizer st = new StringTokenizer(line);
                String lastToken = "";
                while (st.hasMoreTokens()) {
                    lastToken = st.nextToken();
                }
                if (compartments.contains(lastToken)) {
                    line = line.substring(0, line.lastIndexOf(lastToken));
                }
                line = line.trim();
                // start again and add the { } around the expression of volume
                st = new StringTokenizer(line);
                // name
                String nextToken = st.nextToken();
                sb.append(nextToken + " ");
                // dimension
                nextToken = st.nextToken();
                sb.append(nextToken + " ");
                sb.append("{");
                while (st.hasMoreTokens()) {
                    sb.append(st.nextToken());
                }
                sb.append("}");
                sb.append("\n");
            } else {
                // all other cases nothing special to do
                sb.append(line + "\n");
            }
            location = newLocation;
            lineNumber++;
        }
        br.close();
        cleanedBngl = sb.toString();
        // System.out.println(cleanedBngl);
        // BufferedWriter writer = null;
        // try	{
        // writer = new BufferedWriter( new FileWriter( "c:\\TEMP\\workRules.bngl"));
        // writer.write( cleanedBngl);
        // }
        // catch ( IOException e) {
        // }
        // finally {
        // try {
        // if ( writer != null)
        // writer.close( );
        // }
        // catch ( IOException e) {
        // }
        // }
        // BufferedReader br1 = new BufferedReader(new FileReader("c:\\TEMP\\workRules.bngl"));
        // try {
        // StringBuilder sb1 = new StringBuilder();
        // String line = br1.readLine();
        // while (line != null) {
        // sb1.append(line);
        // sb1.append("\n");
        // line = br1.readLine();
        // }
        // cleanedBngl = sb1.toString();
        // } finally {
        // br.close();
        // }
        // System.out.println(cleanedBngl);
        BNGLParser parser = new BNGLParser(new StringReader(cleanedBngl));
        ASTModel astModel = parser.Model();
        String prologString = sPrologBuilder.toString();
        astModel.setProlog(prologString);
        return astModel;
    // BnglObjectConstructionVisitor constructionVisitor = new BnglObjectConstructionVisitor(rbmModelContainer);
    // astModel.jjtAccept(constructionVisitor, rbmModelContainer);
    } catch (Throwable ex) {
        // ex.printStackTrace();
        if (ex instanceof ParseException) {
            ParseException pe = (ParseException) ex;
            throw pe;
        } else {
            throw new ParseException(ex.getMessage());
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StringTokenizer(java.util.StringTokenizer) BNGLParser(org.vcell.model.bngl.BNGLParser) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) ParseException(org.vcell.model.bngl.ParseException) ASTModel(org.vcell.model.bngl.ASTModel) HashSet(java.util.HashSet)

Example 4 with BNGLParser

use of org.vcell.model.bngl.BNGLParser in project vcell by virtualcell.

the class RbmUtils method parseSpeciesPattern.

public static SpeciesPattern parseSpeciesPattern(String originalInputString, Model model) throws ParseException {
    String inputString = new String(originalInputString);
    try {
        if (inputString.startsWith("@") && inputString.contains(":")) {
            // throw new ParseException("RbmUtils: Unable to parse SpeciesPattern with compartment information.");
            // clean up the compartment information and parse the pure sp expression
            inputString = inputString.substring(inputString.lastIndexOf(":") + 1);
        }
        BNGLParser parser = new BNGLParser(new StringReader(inputString));
        ASTSpeciesPattern astSpeciesPattern = parser.SpeciesPattern();
        BnglObjectConstructionVisitor constructionVisitor = new BnglObjectConstructionVisitor(model, null, true);
        SpeciesPattern speciesPattern = (SpeciesPattern) astSpeciesPattern.jjtAccept(constructionVisitor, null);
        return speciesPattern;
    } catch (Throwable ex) {
        ex.printStackTrace();
        throw new ParseException(ex.getMessage());
    }
}
Also used : ASTSpeciesPattern(org.vcell.model.bngl.ASTSpeciesPattern) BNGLParser(org.vcell.model.bngl.BNGLParser) StringReader(java.io.StringReader) ParseException(org.vcell.model.bngl.ParseException) ParticleSpeciesPattern(cbit.vcell.math.ParticleSpeciesPattern) ASTSpeciesPattern(org.vcell.model.bngl.ASTSpeciesPattern)

Aggregations

StringReader (java.io.StringReader)4 BNGLParser (org.vcell.model.bngl.BNGLParser)4 ParseException (org.vcell.model.bngl.ParseException)3 ArrayList (java.util.ArrayList)2 ParticleMolecularType (cbit.vcell.math.ParticleMolecularType)1 ParticleSpeciesPattern (cbit.vcell.math.ParticleSpeciesPattern)1 Expression (cbit.vcell.parser.Expression)1 BufferedReader (java.io.BufferedReader)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 StringTokenizer (java.util.StringTokenizer)1 ASTModel (org.vcell.model.bngl.ASTModel)1 ASTMolecularTypePattern (org.vcell.model.bngl.ASTMolecularTypePattern)1 ASTSpeciesPattern (org.vcell.model.bngl.ASTSpeciesPattern)1