Search in sources :

Example 1 with UnitSystemSelectionPanel

use of cbit.vcell.client.desktop.biomodel.UnitSystemSelectionPanel 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)

Aggregations

VCLogger (cbit.util.xml.VCLogger)1 BioModel (cbit.vcell.biomodel.BioModel)1 UnitSystemSelectionPanel (cbit.vcell.client.desktop.biomodel.UnitSystemSelectionPanel)1 DistributedKinetics (cbit.vcell.model.DistributedKinetics)1 Kinetics (cbit.vcell.model.Kinetics)1 LumpedKinetics (cbit.vcell.model.LumpedKinetics)1 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)1 ReactionStep (cbit.vcell.model.ReactionStep)1 ExternalDocInfo (cbit.vcell.xml.ExternalDocInfo)1 File (java.io.File)1 IOException (java.io.IOException)1 SBMLImporter (org.vcell.sbml.vcell.SBMLImporter)1 UserCancelException (org.vcell.util.UserCancelException)1