Search in sources :

Example 1 with SBMLImporter

use of org.vcell.sbml.vcell.SBMLImporter in project vcell by virtualcell.

the class SimulationServiceImpl method computeModel.

public SimulationInfo computeModel(SBMLModel model, SimulationSpec simSpec) throws ThriftDataAccessException, TException {
    try {
        SBMLImporter importer = new SBMLImporter(model.getFilepath(), vcLogger(), true);
        BioModel bioModel = importer.getBioModel();
        return computeModel(bioModel, simSpec, null);
    } catch (Exception exc) {
        exc.printStackTrace(System.out);
        return null;
    }
}
Also used : SBMLImporter(org.vcell.sbml.vcell.SBMLImporter) BioModel(cbit.vcell.biomodel.BioModel) XMLStreamException(javax.xml.stream.XMLStreamException) ThriftDataAccessException(org.vcell.vcellij.api.ThriftDataAccessException) SbmlException(org.vcell.sbml.SbmlException) SBMLException(org.sbml.jsbml.SBMLException) XmlParseException(cbit.vcell.xml.XmlParseException) SolverException(cbit.vcell.solver.SolverException) TException(org.apache.thrift.TException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException)

Example 2 with SBMLImporter

use of org.vcell.sbml.vcell.SBMLImporter in project vcell by virtualcell.

the class BNGWindowManager method importSbml.

/**
 * Comment
 */
public void importSbml(String bngSbmlStr) {
    if (bngSbmlStr == null || bngSbmlStr.length() == 0) {
        throw new RuntimeException("SBMl string is empty, cannot import into VCell.");
    }
    // 
    // 1. Convert SBML string from BNG to SBML model, add unitDefintions to SBML model using VCell sbml compatible unit system
    // 2. Import unit modified SBML model into VCell as biomodel
    // 3. Enforce "cleaner" (looking) units on this imported biomodel (can use the units added to the sbml model above)
    // 4. Convert all LumpedKinetics reactions into DistributedKinetics.
    // 4. Convert this biomodel into vcml string and pass it into XMLInfo and then to RequestManager to open document.
    // 
    ModelUnitSystem mus = ModelUnitSystem.createDefaultVCModelUnitSystem();
    ModelUnitSystem sbmlCompatibleVCModelUnitSystem = ModelUnitSystem.createSBMLUnitSystem(mus.getVolumeSubstanceUnit(), mus.getVolumeUnit(), mus.getAreaUnit(), mus.getLengthUnit(), mus.getTimeUnit());
    // display to user to change units if desired.
    UnitSystemSelectionPanel unitSystemSelectionPanel = new UnitSystemSelectionPanel(true);
    unitSystemSelectionPanel.initialize(sbmlCompatibleVCModelUnitSystem);
    int retcode = DialogUtils.showComponentOKCancelDialog(getBngOutputPanel(), unitSystemSelectionPanel, "Select new unit system to import into VCell");
    ModelUnitSystem forcedModelUnitSystem = null;
    while (retcode == JOptionPane.OK_OPTION) {
        try {
            forcedModelUnitSystem = unitSystemSelectionPanel.createModelUnitSystem();
            break;
        } catch (Exception e) {
            e.printStackTrace(System.out);
            DialogUtils.showErrorDialog(getBngOutputPanel(), e.getMessage(), e);
            retcode = DialogUtils.showComponentOKCancelDialog(getBngOutputPanel(), unitSystemSelectionPanel, "Select new unit system to import into VCell");
        }
    }
    if (forcedModelUnitSystem == null) {
        DialogUtils.showErrorDialog(getBngOutputPanel(), "Units are required for import into Virtual Cell.");
    }
    try {
        // SBMLUnitTranslator.addUnitDefinitionsToSbmlModel(bngSbmlStr, forcedModelUnitSystem);
        String modifiedSbmlStr = bngSbmlStr;
        // Create a default VCLogger - SBMLImporter needs it
        cbit.util.xml.VCLogger logger = new cbit.util.xml.VCLogger() {

            @Override
            public void sendMessage(Priority p, ErrorType et, String message) throws Exception {
                System.err.println("LOGGER: msgLevel=" + p + ", msgType=" + et + ", " + message);
                if (p == VCLogger.Priority.HighPriority) {
                    throw new RuntimeException("Import failed : " + message);
                }
            }

            public void sendAllMessages() {
            }

            public boolean hasMessages() {
                return false;
            }
        };
        // import sbml String into VCell biomodel
        File sbmlFile = File.createTempFile("temp", ".xml");
        sbmlFile.deleteOnExit();
        XmlUtil.writeXMLStringToFile(modifiedSbmlStr, sbmlFile.getAbsolutePath(), true);
        org.vcell.sbml.vcell.SBMLImporter sbmlImporter = new SBMLImporter(sbmlFile.getAbsolutePath(), logger, false);
        BioModel bioModel = sbmlImporter.getBioModel();
        // enforce 'cleaner looking' units on vc biomodel (the process of adding unit defintion to sbml model messes up the units, though they are correct units (eg., 1e-6m for um).
        BioModel modifiedBiomodel = ModelUnitConverter.createBioModelWithNewUnitSystem(bioModel, forcedModelUnitSystem);
        // convert any reaction that has GeneralLumpedKinetics to GeneralKinetics
        for (ReactionStep rs : modifiedBiomodel.getModel().getReactionSteps()) {
            Kinetics kinetics = rs.getKinetics();
            if (kinetics instanceof LumpedKinetics) {
                rs.setKinetics(DistributedKinetics.toDistributedKinetics((LumpedKinetics) kinetics));
            }
        }
        // convert biomodel to vcml string
        String vcmlString = XmlHelper.bioModelToXML(modifiedBiomodel);
        ExternalDocInfo externalDocInfo = new ExternalDocInfo(vcmlString);
        if (externalDocInfo != null) {
            getRequestManager().openDocument(externalDocInfo, this, true);
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new RuntimeException("Cound not convert BNG sbml string to VCell biomodel : ", e);
    }
}
Also used : SBMLImporter(org.vcell.sbml.vcell.SBMLImporter) LumpedKinetics(cbit.vcell.model.LumpedKinetics) UnitSystemSelectionPanel(cbit.vcell.client.desktop.biomodel.UnitSystemSelectionPanel) VCLogger(cbit.util.xml.VCLogger) SBMLImporter(org.vcell.sbml.vcell.SBMLImporter) IOException(java.io.IOException) UserCancelException(org.vcell.util.UserCancelException) ExternalDocInfo(cbit.vcell.xml.ExternalDocInfo) BioModel(cbit.vcell.biomodel.BioModel) ReactionStep(cbit.vcell.model.ReactionStep) DistributedKinetics(cbit.vcell.model.DistributedKinetics) LumpedKinetics(cbit.vcell.model.LumpedKinetics) Kinetics(cbit.vcell.model.Kinetics) File(java.io.File) VCLogger(cbit.util.xml.VCLogger) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 3 with SBMLImporter

use of org.vcell.sbml.vcell.SBMLImporter in project vcell by virtualcell.

the class VCellSBMLSolver method importSBML.

private BioModel importSBML(String filename, VCLogger logger, boolean isSpatial) throws ClassNotFoundException, IOException, ExecutableException, XmlParseException, XMLStreamException {
    org.vcell.sbml.vcell.SBMLImporter sbmlImporter = new org.vcell.sbml.vcell.SBMLImporter(filename, logger, false);
    BioModel bioModel = sbmlImporter.getBioModel();
    return bioModel;
}
Also used : SBMLImporter(org.vcell.sbml.vcell.SBMLImporter) BioModel(cbit.vcell.biomodel.BioModel) SBMLImporter(org.vcell.sbml.vcell.SBMLImporter)

Example 4 with SBMLImporter

use of org.vcell.sbml.vcell.SBMLImporter in project vcell by virtualcell.

the class XmlHelper method importSBML.

/**
 *	 Allows the translation process to interact with the user via TranslationMessager
 */
public static VCDocument importSBML(VCLogger vcLogger, XMLSource xmlSource, boolean bSpatial) throws Exception {
    // checks that the source is not empty
    if (xmlSource == null) {
        throw new XmlParseException("Invalid params for importing sbml model.");
    }
    // First try getting xmlfile from xmlSource. If not file, get xmlStr and save it in file
    // (since we send only file name to SBMLImporter). If xmlString is also not present in xmlSource, throw exception.
    File sbmlFile = xmlSource.getXmlFile();
    if (sbmlFile == null) {
        String sbmlStr = xmlSource.getXmlString();
        if (sbmlStr != null) {
            sbmlFile = File.createTempFile("temp", ".xml");
            sbmlFile.deleteOnExit();
            XmlUtil.writeXMLStringToFile(sbmlStr, sbmlFile.getAbsolutePath(), true);
        } else {
            throw new RuntimeException("Error importing from SBML : no SBML source.");
        }
    }
    VCDocument vcDoc = null;
    // if (!bSpatial) {
    SBMLImporter sbmlImporter = new SBMLImporter(sbmlFile.getAbsolutePath(), vcLogger, bSpatial);
    vcDoc = sbmlImporter.getBioModel();
    // } else {
    // SBMLSpatialImporter sbmlSpatialImporter = new SBMLSpatialImporter(sbmlFile.getAbsolutePath(), vcLogger);
    // vcDoc = sbmlSpatialImporter.getBioModel();
    // }
    vcDoc.refreshDependencies();
    System.out.println("Succesful model import: SBML file " + sbmlFile);
    return vcDoc;
}
Also used : VCDocument(org.vcell.util.document.VCDocument) SBMLImporter(org.vcell.sbml.vcell.SBMLImporter) File(java.io.File)

Example 5 with SBMLImporter

use of org.vcell.sbml.vcell.SBMLImporter in project vcell by virtualcell.

the class SBMLImporterTest method testImport.

@Test
public void testImport() throws XMLStreamException, IOException {
    HashMap<Integer, FAULT> faults = new HashMap();
    faults.put(6, FAULT.RESERVED_WORD);
    faults.put(15, FAULT.RESERVED_WORD);
    faults.put(92, FAULT.RESERVED_WORD);
    faults.put(114, FAULT.RESERVED_WORD);
    faults.put(115, FAULT.RESERVED_WORD);
    faults.put(117, FAULT.RESERVED_WORD);
    faults.put(148, FAULT.RESERVED_WORD);
    faults.put(154, FAULT.RESERVED_WORD);
    faults.put(155, FAULT.RESERVED_WORD);
    faults.put(154, FAULT.RESERVED_WORD);
    faults.put(155, FAULT.RESERVED_WORD);
    faults.put(156, FAULT.RESERVED_WORD);
    faults.put(157, FAULT.RESERVED_WORD);
    faults.put(158, FAULT.RESERVED_WORD);
    faults.put(159, FAULT.RESERVED_WORD);
    faults.put(274, FAULT.RESERVED_WORD);
    faults.put(279, FAULT.RESERVED_WORD);
    faults.put(282, FAULT.RESERVED_WORD);
    faults.put(288, FAULT.RESERVED_WORD);
    faults.put(346, FAULT.RESERVED_WORD);
    faults.put(24, FAULT.DELAY);
    faults.put(25, FAULT.DELAY);
    faults.put(34, FAULT.DELAY);
    faults.put(196, FAULT.DELAY);
    faults.put(39, FAULT.NONINTEGER_STOICH);
    faults.put(59, FAULT.NONINTEGER_STOICH);
    faults.put(63, FAULT.NONINTEGER_STOICH);
    faults.put(81, FAULT.NONINTEGER_STOICH);
    faults.put(145, FAULT.NONINTEGER_STOICH);
    faults.put(151, FAULT.NONINTEGER_STOICH);
    faults.put(199, FAULT.NONINTEGER_STOICH);
    faults.put(206, FAULT.NONINTEGER_STOICH);
    faults.put(232, FAULT.NONINTEGER_STOICH);
    faults.put(244, FAULT.NONINTEGER_STOICH);
    faults.put(246, FAULT.NONINTEGER_STOICH);
    faults.put(110, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(178, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(228, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(245, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(252, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(262, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(263, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(264, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(267, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(283, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(300, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(316, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(317, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(319, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(322, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(323, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(337, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(327, FAULT.EXPRESSION_BINDING_EXCEPTION);
    faults.put(340, FAULT.XOR_MISSING);
    faults.put(248, FAULT.EXPRESSION_BINDING_EXCEPTION);
    faults.put(305, FAULT.EXPRESSION_BINDING_EXCEPTION);
    faults.put(353, FAULT.NONINTEGER_STOICH);
    faults.put(367, FAULT.RESERVED_WORD);
    faults.put(382, FAULT.RESERVED_WORD);
    faults.put(383, FAULT.NONINTEGER_STOICH);
    faults.put(384, FAULT.NONINTEGER_STOICH);
    faults.put(385, FAULT.NONINTEGER_STOICH);
    faults.put(386, FAULT.NONINTEGER_STOICH);
    faults.put(387, FAULT.NONINTEGER_STOICH);
    faults.put(388, FAULT.NONINTEGER_STOICH);
    faults.put(392, FAULT.NONINTEGER_STOICH);
    faults.put(401, FAULT.NONINTEGER_STOICH);
    faults.put(402, FAULT.RESERVED_WORD);
    faults.put(403, FAULT.RESERVED_WORD);
    faults.put(405, FAULT.INCONSISTENT_UNIT_SYSTEM);
    faults.put(539, FAULT.JSBML_ERROR);
    File[] sbmlFiles = SBMLUnitTranslatorTest.getBiomodelsCuratedSBMLFiles();
    // File[] sbmlFiles = new File[] {
    // new File("/Users/schaff/Documents/workspace-maven/BioModels_Database-r30_pub-sbml_files/curated/BIOMD0000000001.xml"),
    // new File("/Users/schaff/Documents/workspace-maven/BioModels_Database-r30_pub-sbml_files/curated/BIOMD0000000101.xml"),
    // new File("/Users/schaff/Documents/workspace-maven/sbml-test-suite/cases/semantic/00001/00001-sbml-l3v1.xml")
    // };
    VCLogger vcl = new TLogger();
    int start = 401;
    for (int index = start; index < sbmlFiles.length; index++) {
        File sbmlFile = sbmlFiles[index];
        int sbmlNumber = Integer.parseInt(sbmlFile.getName().substring(6).replace(".xml", ""));
        if (faults.containsKey(sbmlNumber)) {
            System.err.println("skipping this model, " + faults.get(sbmlNumber).name());
            continue;
        }
        System.out.println("testing " + sbmlFile);
        SBMLImporter importer = new SBMLImporter(sbmlFile.getAbsolutePath(), vcl, false);
        BioModel bioModel = importer.getBioModel();
    }
}
Also used : SBMLImporter(org.vcell.sbml.vcell.SBMLImporter) HashMap(java.util.HashMap) BioModel(cbit.vcell.biomodel.BioModel) File(java.io.File) VCLogger(cbit.util.xml.VCLogger) Test(org.junit.Test)

Aggregations

SBMLImporter (org.vcell.sbml.vcell.SBMLImporter)9 BioModel (cbit.vcell.biomodel.BioModel)8 VCLogger (cbit.util.xml.VCLogger)5 File (java.io.File)5 IOException (java.io.IOException)5 XMLStreamException (javax.xml.stream.XMLStreamException)4 SolverException (cbit.vcell.solver.SolverException)3 XmlParseException (cbit.vcell.xml.XmlParseException)3 SbmlException (org.vcell.sbml.SbmlException)3 SimulationContext (cbit.vcell.mapping.SimulationContext)2 XMLSource (cbit.vcell.xml.XMLSource)2 SBMLException (org.sbml.jsbml.SBMLException)2 DataAccessException (org.vcell.util.DataAccessException)2 UnitSystemSelectionPanel (cbit.vcell.client.desktop.biomodel.UnitSystemSelectionPanel)1 MathMapping (cbit.vcell.mapping.MathMapping)1 MathCompareResults (cbit.vcell.math.MathCompareResults)1 MathDescription (cbit.vcell.math.MathDescription)1 Variable (cbit.vcell.math.Variable)1 SimulationTask (cbit.vcell.messaging.server.SimulationTask)1 DistributedKinetics (cbit.vcell.model.DistributedKinetics)1