Search in sources :

Example 51 with XMLSource

use of cbit.vcell.xml.XMLSource in project vcell by virtualcell.

the class SBMLSpatialTest method test.

// @Test
public void test() throws Exception {
    // BioModel bioModel1 = BioModelTest.getExampleWithImage();
    URL vcmlURL = SBMLSpatialTest.class.getResource("Solver_Suite_6_2.vcml");
    File vcmlFile = new File(vcmlURL.toURI());
    BioModel bioModel1 = XmlHelper.XMLToBioModel(new XMLSource(vcmlFile));
    bioModel1.refreshDependencies();
    // for (int i = 0; i<bioModel1.getNumSimulationContexts(); i++){
    for (int i = 5; i == 5; i++) {
        SimulationContext sc1 = bioModel1.getSimulationContext(i);
        if (sc1.getApplicationType() != Application.NETWORK_DETERMINISTIC) {
            System.err.println(sc1.getName() + " is not a network determistic application");
            continue;
        }
        boolean isSpatial = sc1.getGeometry().getDimension() > 0;
        SBMLExporter exporter = new SBMLExporter(bioModel1, 3, 1, isSpatial);
        sc1.refreshMathDescription(null, NetworkGenerationRequirements.ComputeFullNoTimeout);
        exporter.setSelectedSimContext(sc1);
        String sbmlString = exporter.getSBMLString();
        File tempFile = File.createTempFile("sbmlSpatialTest_SBML_", ".sbml.xml");
        FileUtils.write(tempFile, sbmlString);
        System.out.println(tempFile);
        try {
            VCLogger argVCLogger = new TLogger();
            SBMLImporter importer = new SBMLImporter(tempFile.getAbsolutePath(), argVCLogger, isSpatial);
            BioModel bioModel2 = importer.getBioModel();
            File tempFile2 = File.createTempFile("sbmlSpatialTest_Biomodel_", ".vcml.xml");
            FileUtils.write(tempFile2, XmlHelper.bioModelToXML(bioModel2));
            System.out.println(tempFile2);
            // if (true) { throw new RuntimeException("stop"); }
            bioModel2.refreshDependencies();
            SimulationContext sc2 = bioModel2.getSimulationContext(0);
            // sc2.refreshMathDescription(null, NetworkGenerationRequirements.ComputeFullNoTimeout);
            sc2.setMathDescription(sc2.createNewMathMapping(null, NetworkGenerationRequirements.ComputeFullNoTimeout).getMathDescription());
            if (!sc1.getMathDescription().isValid()) {
                throw new RuntimeException("sc1.math is not valid");
            }
            if (!sc2.getMathDescription().isValid()) {
                throw new RuntimeException("sc2.math is not valid");
            }
            MathCompareResults mathCompareResults = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(), sc1.getMathDescription(), sc2.getMathDescription());
            if (!mathCompareResults.isEquivalent()) {
                System.out.println("MATH DESCRIPTION 1 <UNCHANGED>");
                System.out.println(sc1.getMathDescription().getVCML_database());
                System.out.println("MATH DESCRIPTION 2 <UNCHANGED>");
                System.out.println(sc2.getMathDescription().getVCML_database());
                // if (mathCompareResults.decision  == Decision.MathDifferent_SUBDOMAINS_DONT_MATCH){
                // BioModel bioModel1_copy = XmlHelper.XMLToBioModel(new XMLSource(vcmlFile));
                // bioModel1_copy.refreshDependencies();
                // SimulationContext sc1_copy = bioModel1_copy.getSimulationContext(i);
                // VCImage image = sc1_copy.getGeometry().getGeometrySpec().getImage();
                // if (image!=null){
                // ArrayList<VCPixelClass> pcList = new ArrayList<VCPixelClass>();
                // for (VCPixelClass pc : image.getPixelClasses()){
                // pcList.add(new VCPixelClass(pc.getKey(),SBMLExporter.DOMAIN_TYPE_PREFIX+pc.getPixelClassName(),pc.getPixel()));
                // }
                // image.setPixelClasses(pcList.toArray(new VCPixelClass[0]));
                // }
                // for (GeometryClass gc : sc1_copy.getGeometry().getGeometryClasses()){
                // System.out.println("name before "+gc.getName());
                // gc.setName(SBMLExporter.DOMAIN_TYPE_PREFIX+gc.getName());
                // System.out.println("name after "+gc.getName());
                // }
                // sc1_copy.checkValidity();
                // bioModel1_copy.refreshDependencies();
                // sc1_copy.getGeometry().precomputeAll(new GeometryThumbnailImageFactoryAWT(), true, true);
                // sc1_copy.setMathDescription(sc1_copy.createNewMathMapping(null, NetworkGenerationRequirements.ComputeFullNoTimeout).getMathDescription());
                // MathCompareResults mathCompareResults_renamedSubdomains = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(),sc1_copy.getMathDescription(), sc2.getMathDescription());
                // if (!mathCompareResults_renamedSubdomains.isEquivalent()){
                // System.out.println("MATH DESCRIPTION 1 <RENAMED>");
                // System.out.println(sc1_copy.getMathDescription().getVCML_database());
                // Assert.fail(mathCompareResults_renamedSubdomains.decision+" "+mathCompareResults_renamedSubdomains.details);
                // }
                // }else{
                System.err.println(mathCompareResults.decision + " " + mathCompareResults.details);
            // }
            } else {
                System.out.println("MATHS WERE EQUIVALENT");
            }
        } finally {
            tempFile.delete();
        }
    }
    // loop over determinstic applications
    System.out.println("done");
}
Also used : SBMLImporter(org.vcell.sbml.vcell.SBMLImporter) SBMLExporter(org.vcell.sbml.vcell.SBMLExporter) SimulationContext(cbit.vcell.mapping.SimulationContext) URL(java.net.URL) BioModel(cbit.vcell.biomodel.BioModel) MathCompareResults(cbit.vcell.math.MathCompareResults) File(java.io.File) XMLSource(cbit.vcell.xml.XMLSource) VCLogger(cbit.util.xml.VCLogger)

Example 52 with XMLSource

use of cbit.vcell.xml.XMLSource in project vcell by virtualcell.

the class ModelUnitConverter method createBioModelWithNewUnitSystem.

public static BioModel createBioModelWithNewUnitSystem(BioModel oldBioModel, ModelUnitSystem newUnitSystem) throws ExpressionException, XmlParseException {
    // new BioModel has new unit system applied to all built-in units ... but expressions still need to be corrected (see below).
    String biomodelXMLString = XmlHelper.bioModelToXML(oldBioModel);
    XMLSource newXMLSource = new XMLSource(biomodelXMLString);
    BioModel newBioModel = XmlHelper.XMLToBioModel(newXMLSource, true, newUnitSystem);
    Model newModel = newBioModel.getModel();
    Model oldModel = oldBioModel.getModel();
    for (Parameter p : newBioModel.getModel().getModelParameters()) {
        convertVarsWithUnitFactors(oldBioModel.getModel(), newBioModel.getModel(), p);
    }
    for (ReactionStep reactionStep : newBioModel.getModel().getReactionSteps()) {
        SymbolTable oldSymbolTable = oldBioModel.getModel().getReactionStep(reactionStep.getName());
        SymbolTable newSymbolTable = reactionStep;
        for (Parameter p : reactionStep.getKinetics().getUnresolvedParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
        for (Parameter p : reactionStep.getKinetics().getKineticsParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
        Kinetics kinetics = reactionStep.getKinetics();
        KineticsParameter kineticsParameter = null;
        if (kinetics.getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate) != null) {
            kineticsParameter = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate);
        } else if (kinetics.getKineticsParameterFromRole(Kinetics.ROLE_LumpedReactionRate) != null) {
            kineticsParameter = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_LumpedReactionRate);
        } else {
            throw new RuntimeException("Role 'reaction rate' or role 'lumped reaction rate' expected");
        }
        Expression rateExpression = kineticsParameter.getExpression();
        jscl.math.Expression jsclExpression = null;
        String jsclExpressionString = rateExpression.infix_JSCL();
        try {
            jsclExpression = jscl.math.Expression.valueOf(jsclExpressionString);
        } catch (jscl.text.ParseException e) {
            e.printStackTrace(System.out);
            System.out.println("JSCL couldn't parse \"" + jsclExpressionString + "\"");
            return null;
        }
        jscl.math.Generic g1 = jsclExpression.expand().simplify();
        Expression newRate = new Expression(SymbolUtils.getRestoredStringJSCL(g1.toString()));
        newRate.bindExpression(reactionStep);
        // reactionStep.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate).setExpression(newRate.flatten());
        if (reactionStep.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate) != null) {
            reactionStep.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate).setExpression(newRate.flatten());
        }
    }
    for (ReactionRule reactionRule : newBioModel.getModel().getRbmModelContainer().getReactionRuleList()) {
        SymbolTable oldSymbolTable = oldBioModel.getModel().getRbmModelContainer().getReactionRule(reactionRule.getName()).getKineticLaw().getScopedSymbolTable();
        SymbolTable newSymbolTable = reactionRule.getKineticLaw().getScopedSymbolTable();
        for (Parameter p : reactionRule.getKineticLaw().getUnresolvedParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
        for (Parameter p : reactionRule.getKineticLaw().getLocalParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
    }
    for (SimulationContext simContext : newBioModel.getSimulationContexts()) {
        SimulationContext oldSimContext = oldBioModel.getSimulationContext(simContext.getName());
        // ArrayList<Parameter> parameterList = new ArrayList<Parameter>();
        for (StructureMapping mapping : simContext.getGeometryContext().getStructureMappings()) {
            Structure oldStructure = oldModel.getStructure(mapping.getStructure().getName());
            StructureMapping oldMapping = oldSimContext.getGeometryContext().getStructureMapping(oldStructure);
            for (Parameter p : mapping.computeApplicableParameterList()) {
                convertVarsWithUnitFactors(oldMapping, mapping, p);
            }
        }
        for (SpeciesContextSpec spec : simContext.getReactionContext().getSpeciesContextSpecs()) {
            SpeciesContext oldSpeciesContext = oldModel.getSpeciesContext(spec.getSpeciesContext().getName());
            SpeciesContextSpec oldSpec = oldSimContext.getReactionContext().getSpeciesContextSpec(oldSpeciesContext);
            for (Parameter p : spec.computeApplicableParameterList()) {
                convertVarsWithUnitFactors(oldSpec, spec, p);
            }
        }
        for (int i = 0; i < simContext.getElectricalStimuli().length; i++) {
            ElectricalStimulus newElectricalStimulus = simContext.getElectricalStimuli()[i];
            ElectricalStimulus oldElectricalStimulus = oldSimContext.getElectricalStimuli()[i];
            for (Parameter p : newElectricalStimulus.getParameters()) {
                convertVarsWithUnitFactors(oldElectricalStimulus.getNameScope().getScopedSymbolTable(), newElectricalStimulus.getNameScope().getScopedSymbolTable(), p);
            }
        }
        // convert events : trigger and delay parameters and event assignments
        for (int i = 0; simContext.getBioEvents() != null && oldSimContext.getBioEvents() != null && i < simContext.getBioEvents().length; i++) {
            BioEvent newBioEvent = simContext.getBioEvents()[i];
            BioEvent oldBioEvent = oldSimContext.getBioEvent(newBioEvent.getName());
            for (Parameter p : newBioEvent.getEventParameters()) {
                convertVarsWithUnitFactors(oldBioEvent.getNameScope().getScopedSymbolTable(), newBioEvent.getNameScope().getScopedSymbolTable(), p);
            }
            // for each event assignment expression
            for (int e = 0; e < newBioEvent.getEventAssignments().size(); e++) {
                ScopedSymbolTable newSymbolTable = newBioEvent.getNameScope().getScopedSymbolTable();
                ScopedSymbolTable oldSymbolTable = oldBioEvent.getNameScope().getScopedSymbolTable();
                EventAssignment newEventAssignment = newBioEvent.getEventAssignments().get(e);
                EventAssignment oldEventAssignment = oldBioEvent.getEventAssignments().get(e);
                VCUnitDefinition oldTargetUnit = oldEventAssignment.getTarget().getUnitDefinition();
                VCUnitDefinition newTargetUnit = newEventAssignment.getTarget().getUnitDefinition();
                Expression eventAssgnExpr = newEventAssignment.getAssignmentExpression();
                convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldTargetUnit, newTargetUnit, eventAssgnExpr);
            }
        }
        /**
         * @TODO: If rate rule variable unit is TBD, we still need to handle the rate expression unit.
         */
        // convert rate rules
        RateRule[] rateRules = simContext.getRateRules();
        if (rateRules != null && rateRules.length > 0) {
            for (RateRule rateRule : rateRules) {
                RateRule oldRateRule = oldSimContext.getRateRule(rateRule.getName());
                ScopedSymbolTable oldSymbolTable = oldRateRule.getSimulationContext();
                ScopedSymbolTable newSymbolTable = rateRule.getSimulationContext();
                VCUnitDefinition oldTargetUnit = oldRateRule.getRateRuleVar().getUnitDefinition();
                VCUnitDefinition newTargetUnit = rateRule.getRateRuleVar().getUnitDefinition();
                Expression rateRuleExpr = rateRule.getRateRuleExpression();
                convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldTargetUnit, newTargetUnit, rateRuleExpr);
            }
        }
        AssignmentRule[] assignmentRules = simContext.getAssignmentRules();
        if (assignmentRules != null && assignmentRules.length > 0) {
            for (AssignmentRule assignmentRule : assignmentRules) {
                AssignmentRule oldAssignRule = oldSimContext.getAssignmentRule(assignmentRule.getName());
                ScopedSymbolTable oldSymbolTable = oldAssignRule.getSimulationContext();
                ScopedSymbolTable newSymbolTable = assignmentRule.getSimulationContext();
                VCUnitDefinition oldTargetUnit = oldAssignRule.getAssignmentRuleVar().getUnitDefinition();
                VCUnitDefinition newTargetUnit = assignmentRule.getAssignmentRuleVar().getUnitDefinition();
                Expression assignmentRuleExpr = assignmentRule.getAssignmentRuleExpression();
                convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldTargetUnit, newTargetUnit, assignmentRuleExpr);
            }
        }
    }
    // end  for - simulationContext
    return newBioModel;
}
Also used : SpeciesContext(cbit.vcell.model.SpeciesContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) StructureMapping(cbit.vcell.mapping.StructureMapping) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) RateRule(cbit.vcell.mapping.RateRule) ScopedSymbolTable(cbit.vcell.parser.ScopedSymbolTable) SimulationContext(cbit.vcell.mapping.SimulationContext) SpeciesContext(cbit.vcell.model.SpeciesContext) Structure(cbit.vcell.model.Structure) ReactionRule(cbit.vcell.model.ReactionRule) EventAssignment(cbit.vcell.mapping.BioEvent.EventAssignment) AssignmentRule(cbit.vcell.mapping.AssignmentRule) ScopedSymbolTable(cbit.vcell.parser.ScopedSymbolTable) SymbolTable(cbit.vcell.parser.SymbolTable) SimulationContext(cbit.vcell.mapping.SimulationContext) ElectricalStimulus(cbit.vcell.mapping.ElectricalStimulus) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) ReactionStep(cbit.vcell.model.ReactionStep) Model(cbit.vcell.model.Model) Parameter(cbit.vcell.model.Parameter) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) Kinetics(cbit.vcell.model.Kinetics) BioEvent(cbit.vcell.mapping.BioEvent) XMLSource(cbit.vcell.xml.XMLSource)

Example 53 with XMLSource

use of cbit.vcell.xml.XMLSource in project vcell by virtualcell.

the class SEDMLExporter method main.

public static void main(String[] args) {
    // if (args.length != 1) {
    // System.out.println("Usage:\n\t path_of_vcml_file\n" );
    // System.exit(1);
    // }
    // String pathName = args[0];
    String pathName = "c:\\dan\\SEDML\\SEDML2.vcml";
    try {
        String biomodelXmlStr = XmlUtil.getXMLString(pathName);
        BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(biomodelXmlStr.toString()));
        bioModel.refreshDependencies();
        // invoke the SEDMLEXporter
        SEDMLExporter sedmlExporter = new SEDMLExporter(bioModel, 1, 1, null);
        String absolutePath = "c:\\dan\\SEDML";
        String sedmlStr = sedmlExporter.getSEDMLFile(absolutePath, "SEDML2", false, false, false);
        // String absolutePath = ResourceUtil.getUserHomeDir().getAbsolutePath();
        String outputName = absolutePath + "\\" + TokenMangler.mangleToSName(bioModel.getName()) + ".sedml";
        XmlUtil.writeXMLStringToFile(sedmlStr, outputName, true);
    } catch (IOException e) {
        e.printStackTrace(System.out);
        throw new RuntimeException("Unable to read VCML file '" + pathName + "' into string.");
    } catch (XmlParseException e) {
        e.printStackTrace(System.out);
        throw new RuntimeException("Unable to convert VCML file '" + pathName + "' to a biomodel.");
    }
}
Also used : BioModel(cbit.vcell.biomodel.BioModel) IOException(java.io.IOException) XmlParseException(cbit.vcell.xml.XmlParseException) XMLSource(cbit.vcell.xml.XMLSource)

Example 54 with XMLSource

use of cbit.vcell.xml.XMLSource in project vcell by virtualcell.

the class SBMLStandaloneImporter method attemptImport.

private BioModel attemptImport(File sbmlFile) throws ExecutableException, IOException, ClassNotFoundException, XmlParseException {
    if (subProcess == null || subProcess.getStatus() != LiveProcessStatus.RUNNING) {
        subProcess = createProcess();
    }
    toChildProcess.writeObject(sbmlFile);
    Object back = fromChildProcess.readObject();
    if (back instanceof String) {
        String xml = (String) back;
        if (lg.isDebugEnabled()) {
            try (FileWriter fw = new FileWriter(sbmlFile.getAbsolutePath() + ".dump")) {
                fw.append(xml);
            }
        }
        XMLSource source = new XMLSource(xml);
        return XmlHelper.XMLToBioModel(source);
    }
    if (back instanceof Exception) {
        try {
            throw (Exception) back;
        } catch (SBMLImportException sie) {
            throw sie;
        } catch (Exception e) {
            throw new ExecutableException("child process exception", e);
        }
    }
    throw new ExecutableException("unexpected object from child process:  " + back.getClass().getName() + " " + back.toString());
}
Also used : ExecutableException(org.vcell.util.exe.ExecutableException) FileWriter(java.io.FileWriter) XMLSource(cbit.vcell.xml.XMLSource) ExecutableException(org.vcell.util.exe.ExecutableException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) XmlParseException(cbit.vcell.xml.XmlParseException) IOException(java.io.IOException)

Example 55 with XMLSource

use of cbit.vcell.xml.XMLSource in project vcell by virtualcell.

the class ProfileLikelihood method main.

// main
public static void main(String[] args) {
    try {
        File newXML = new File(args[0]);
        XMLSource source = new XMLSource(newXML);
        BioModel biomodel = XmlHelper.XMLToBioModel(source);
        SimulationContext app = biomodel.getSimulationContext("Deterministic");
        AnalysisTask[] task = app.getAnalysisTasks();
        // ParestRun.bFakeOptimization = true;
        final ProfileLikelihoodCallback callback = new ProfileLikelihoodCallback() {

            @Override
            public void report(String msg) {
                System.out.println(msg);
            }
        };
        ProfileLikelihood pl = new ProfileLikelihood(callback);
        pl.run((ParameterEstimationTask) task[0]);
    } catch (XmlParseException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : BioModel(cbit.vcell.biomodel.BioModel) XmlParseException(cbit.vcell.xml.XmlParseException) SimulationContext(cbit.vcell.mapping.SimulationContext) File(java.io.File) XMLSource(cbit.vcell.xml.XMLSource) MappingException(cbit.vcell.mapping.MappingException) XmlParseException(cbit.vcell.xml.XmlParseException) MathException(cbit.vcell.math.MathException)

Aggregations

XMLSource (cbit.vcell.xml.XMLSource)62 BioModel (cbit.vcell.biomodel.BioModel)48 XmlParseException (cbit.vcell.xml.XmlParseException)33 DataAccessException (org.vcell.util.DataAccessException)33 SimulationContext (cbit.vcell.mapping.SimulationContext)28 BigString (org.vcell.util.BigString)26 KeyValue (org.vcell.util.document.KeyValue)25 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)24 Simulation (cbit.vcell.solver.Simulation)22 SQLException (java.sql.SQLException)20 IOException (java.io.IOException)17 User (org.vcell.util.document.User)17 File (java.io.File)15 MathModel (cbit.vcell.mathmodel.MathModel)14 PropertyVetoException (java.beans.PropertyVetoException)14 MathException (cbit.vcell.math.MathException)13 ExpressionException (cbit.vcell.parser.ExpressionException)13 MappingException (cbit.vcell.mapping.MappingException)12 MathDescription (cbit.vcell.math.MathDescription)11 PrintWriter (java.io.PrintWriter)11