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);
}
}
Aggregations