Search in sources :

Example 1 with SBMLError

use of org.sbml.jsbml.SBMLError in project vcell by virtualcell.

the class MathModel_SBMLExporter method getSBMLString.

/**
 * Insert the method's description here.
 * Creation date: (4/11/2006 11:38:26 AM)
 * @return org.sbml.libsbml.Model
 * @param mathModel cbit.vcell.mathmodel.MathModel
 * @throws XMLStreamException
 * @throws SBMLException
 */
public static String getSBMLString(cbit.vcell.mathmodel.MathModel mathModel, long level, long version) throws cbit.vcell.parser.ExpressionException, java.io.IOException, SBMLException, XMLStreamException {
    if (mathModel.getMathDescription().isSpatial()) {
        throw new RuntimeException("spatial models export to SBML not supported");
    }
    if (mathModel.getMathDescription().hasFastSystems()) {
        throw new RuntimeException("math models with fast systems cannot be exported to SBML");
    }
    if (mathModel.getMathDescription().isNonSpatialStoch()) {
        throw new RuntimeException("stochastic math models cannot be exported to SBML");
    }
    if (!mathModel.getMathDescription().isValid()) {
        throw new RuntimeException("math model has an invalid Math Description, cannot export to SBML");
    }
    String dummyID = "ID_0";
    String compartmentId = "compartment";
    SBMLDocument sbmlDocument = new SBMLDocument((int) level, (int) version);
    Model sbmlModel = sbmlDocument.createModel();
    sbmlModel.setId("MathModel_" + TokenMangler.mangleToSName(mathModel.getName()));
    if (mathModel.getMathDescription().isSpatial()) {
        addGeometry(sbmlModel, mathModel);
    }
    Compartment compartment = sbmlModel.createCompartment();
    compartment.setId(compartmentId);
    // ------ For spatial SBML when implemented -----
    // if (vcMathModel.getMathDescription().isSpatial()){
    // // for spatial model, compartment(s) created in addGeometry(), based on number of subVolumes/surfaceClasses.
    // addGeometry();
    // } else {
    // // for non-spatial mathmodel, only 1 compartment; create it here.
    // String compartmentId = "compartment";
    // org.sbml.libsbml.Compartment compartment = sbmlModel.createCompartment();
    // compartment.setId(compartmentId);
    // }
    MathDescription mathDesc = mathModel.getMathDescription();
    Enumeration<Variable> enumVars = mathDesc.getVariables();
    while (enumVars.hasMoreElements()) {
        Variable vcVar = (Variable) enumVars.nextElement();
        // 
        if (vcVar instanceof cbit.vcell.math.VolVariable) {
        // 
        // skip for now, define later when defining ODEEquations.
        // 
        // org.sbml.libsbml.Species species = model.createSpecies();
        // species.setId(vcVar.getName());
        // species.setCompartment(compartmentId);
        } else if (vcVar instanceof cbit.vcell.math.Constant && ((cbit.vcell.math.Constant) vcVar).getExpression().isNumeric()) {
            Parameter param = sbmlModel.createParameter();
            param.setId(TokenMangler.mangleToSName(vcVar.getName()));
            param.setConstant(true);
            param.setValue(vcVar.getExpression().evaluateConstant());
        } else if (vcVar instanceof cbit.vcell.math.Constant || vcVar instanceof cbit.vcell.math.Function) {
            Parameter param = sbmlModel.createParameter();
            param.setId(TokenMangler.mangleToSName(vcVar.getName()));
            param.setConstant(false);
            // 
            // Function or Constant with expressions - create assignment rule and add to model.
            // 
            ASTNode mathNode = getFormulaFromExpression(vcVar.getExpression(), MathType.REAL);
            AssignmentRule assignmentRule = sbmlModel.createAssignmentRule();
            dummyID = TokenMangler.getNextEnumeratedToken(dummyID);
            assignmentRule.setId(dummyID);
            assignmentRule.setVariable(TokenMangler.mangleToSName(vcVar.getName()));
            assignmentRule.setMath(mathNode);
        // Create a parameter for this function/non-numeric constant, set its value to be 'not-constant',
        // add to model.
        }
    }
    cbit.vcell.math.CompartmentSubDomain subDomain = (cbit.vcell.math.CompartmentSubDomain) mathDesc.getSubDomains().nextElement();
    // System.out.println(model.toSBML());
    Enumeration<Equation> enumEqu = subDomain.getEquations();
    while (enumEqu.hasMoreElements()) {
        cbit.vcell.math.Equation equ = (cbit.vcell.math.Equation) enumEqu.nextElement();
        if (equ instanceof cbit.vcell.math.OdeEquation) {
            // For ODE equations, add the ode variable as a parameter, add rate as a rate rule and init condition as an initial assignment rule.
            Parameter param = sbmlModel.createParameter();
            param.setId(TokenMangler.mangleToSName(equ.getVariable().getName()));
            param.setConstant(false);
            // try to obtain the constant to which the init expression evaluates.
            RateRule rateRule = sbmlModel.createRateRule();
            rateRule.setVariable(TokenMangler.mangleToSName(equ.getVariable().getName()));
            rateRule.setMath(getFormulaFromExpression(equ.getRateExpression(), MathType.REAL));
            InitialAssignment initialAssignment = sbmlModel.createInitialAssignment();
            dummyID = TokenMangler.getNextEnumeratedToken(dummyID);
            initialAssignment.setId(dummyID);
            initialAssignment.setMath(getFormulaFromExpression(equ.getInitialExpression(), MathType.REAL));
            initialAssignment.setVariable(TokenMangler.mangleToSName(equ.getVariable().getName()));
        } else {
            throw new RuntimeException("equation type " + equ.getClass().getName() + " not supported");
        }
    }
    Iterator<Event> vcellEvents = mathDesc.getEvents();
    while (vcellEvents.hasNext()) {
        Event vcellEvent = vcellEvents.next();
        addSbmlEvent(sbmlModel, vcellEvent);
    }
    System.out.println(new SBMLWriter().writeSBMLToString(sbmlDocument));
    // validate the sbml document
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.GENERAL_CONSISTENCY, true);
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.IDENTIFIER_CONSISTENCY, true);
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.MATHML_CONSISTENCY, true);
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.MODELING_PRACTICE, false);
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.OVERDETERMINED_MODEL, true);
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.SBO_CONSISTENCY, false);
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.UNITS_CONSISTENCY, false);
    sbmlDocument.checkConsistency();
    // sbmlDocument.checkConsistencyOffline();
    long internalErrCount = sbmlDocument.getNumErrors();
    if (internalErrCount > 0) {
        StringBuffer sbmlErrbuf = new StringBuffer();
        for (int i = 0; i < internalErrCount; i++) {
            SBMLError sbmlErr = sbmlDocument.getError(i);
            if (sbmlErr.isError() || sbmlErr.isFatal()) {
                sbmlErrbuf.append(sbmlErr.getCategory() + " :: " + sbmlErr.getSeverity() + " :: " + sbmlErr.getMessage() + "\n");
            }
        }
        if (sbmlErrbuf.length() > 0) {
            throw new RuntimeException("SBML Internal consistency checks failed: \n" + sbmlErrbuf.toString());
        }
    }
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.GENERAL_CONSISTENCY, true);
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.IDENTIFIER_CONSISTENCY, true);
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.UNITS_CONSISTENCY, false);
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.MATHML_CONSISTENCY, true);
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.SBO_CONSISTENCY, false);
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.OVERDETERMINED_MODEL, true);
    sbmlDocument.setConsistencyChecks(CHECK_CATEGORY.MODELING_PRACTICE, false);
    sbmlDocument.checkConsistency();
    // sbmlDocument.checkConsistencyOffline();
    long errCount = sbmlDocument.getNumErrors();
    if (errCount > 0) {
        StringBuffer sbmlErrbuf = new StringBuffer();
        for (int i = 0; i < errCount; i++) {
            SBMLError sbmlErr = sbmlDocument.getError(i);
            if (sbmlErr.isError() || sbmlErr.isFatal()) {
                sbmlErrbuf.append(sbmlErr.getCategory() + " :: " + sbmlErr.getSeverity() + " :: " + sbmlErr.getMessage() + "\n");
            }
        }
        if (sbmlErrbuf.length() > 0) {
            throw new RuntimeException("SBML validation failed: \n" + sbmlErrbuf.toString());
        }
    }
    // end of validation
    // start writing
    SBMLWriter sbmlWriter = new SBMLWriter();
    String sbmlStr = sbmlWriter.writeSBMLToString(sbmlDocument);
    // Error check - use libSBML's document.printError to print to outputstream
    System.out.println("\n\nSBML Export Error Report");
    sbmlDocument.printErrors(System.out);
    return sbmlStr;
}
Also used : ReservedVariable(cbit.vcell.math.ReservedVariable) Variable(cbit.vcell.math.Variable) SBMLDocument(org.sbml.jsbml.SBMLDocument) MathDescription(cbit.vcell.math.MathDescription) Compartment(org.sbml.jsbml.Compartment) SBMLError(org.sbml.jsbml.SBMLError) ASTNode(org.sbml.jsbml.ASTNode) RateRule(org.sbml.jsbml.RateRule) AssignmentRule(org.sbml.jsbml.AssignmentRule) Equation(cbit.vcell.math.Equation) InteriorPoint(org.sbml.jsbml.ext.spatial.InteriorPoint) InitialAssignment(org.sbml.jsbml.InitialAssignment) SBMLWriter(org.sbml.jsbml.SBMLWriter) Model(org.sbml.jsbml.Model) MathModel(cbit.vcell.mathmodel.MathModel) Parameter(org.sbml.jsbml.Parameter) Event(cbit.vcell.math.Event) Equation(cbit.vcell.math.Equation)

Aggregations

Equation (cbit.vcell.math.Equation)1 Event (cbit.vcell.math.Event)1 MathDescription (cbit.vcell.math.MathDescription)1 ReservedVariable (cbit.vcell.math.ReservedVariable)1 Variable (cbit.vcell.math.Variable)1 MathModel (cbit.vcell.mathmodel.MathModel)1 ASTNode (org.sbml.jsbml.ASTNode)1 AssignmentRule (org.sbml.jsbml.AssignmentRule)1 Compartment (org.sbml.jsbml.Compartment)1 InitialAssignment (org.sbml.jsbml.InitialAssignment)1 Model (org.sbml.jsbml.Model)1 Parameter (org.sbml.jsbml.Parameter)1 RateRule (org.sbml.jsbml.RateRule)1 SBMLDocument (org.sbml.jsbml.SBMLDocument)1 SBMLError (org.sbml.jsbml.SBMLError)1 SBMLWriter (org.sbml.jsbml.SBMLWriter)1 InteriorPoint (org.sbml.jsbml.ext.spatial.InteriorPoint)1