use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class ClientDocumentManager method getBioModelFromDatabaseXML.
private BioModel getBioModelFromDatabaseXML(XMLHolder<BioModel> bioModelXMLHolder) throws DataAccessException {
try {
BioModel bm = bioModelXMLHolder.getDocument();
if (bm == null) {
bm = XmlHelper.XMLToBioModel(new XMLSource(bioModelXMLHolder.getXmlString()));
}
cacheSimulations(bm.getSimulations());
// bm.refreshDependencies();
return bm;
} catch (XmlParseException e) {
e.printStackTrace();
throw new DataAccessException(e.getMessage());
}
}
use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class BnglExtensionFilter method askUser.
@Override
public void askUser(ChooseContext ctx) throws UserCancelException {
BioModel bioModel = ctx.chosenContext.getBioModel();
boolean hasReactions = bioModel.getModel().getReactionSteps().length > 0 ? true : false;
if (hasReactions) {
// mixed
String errMsg = "Simple Reactions cannot be exported to .bngl format.";
errMsg += "<br>Some information will be lost.";
errMsg += "<br><br>Continue anyway?";
errMsg = "<html>" + errMsg + "</html>";
int dialogButton = JOptionPane.YES_NO_OPTION;
int returnCode = JOptionPane.showConfirmDialog(ctx.topLevelWindowManager.getComponent(), errMsg, "Exporting to .bngl", dialogButton);
if (returnCode != JOptionPane.YES_OPTION) {
throw UserCancelException.CANCEL_FILE_SELECTION;
}
}
}
use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class SbmlExtensionFilter method askUser.
@Override
public void askUser(ChooseContext c) throws UserCancelException {
BioModel bioModel = c.chosenContext.getBioModel();
JFrame currentWindow = c.currentWindow;
selectedSimWOSBE = null;
selectedSimContext = c.chosenContext;
// get user choice of structure and its size and computes absolute sizes of compartments using the StructureSizeSolver.
Structure[] structures = bioModel.getModel().getStructures();
// get the nonspatial simulationContexts corresponding to names in applicableAppNameList
// This is needed in ApplnSelectionAndStructureSizeInputPanel
String strucName = null;
double structSize = 1.0;
int structSelection = -1;
int option = JOptionPane.CANCEL_OPTION;
ApplnSelectionAndStructureSizeInputPanel applnStructInputPanel = null;
while (structSelection < 0) {
applnStructInputPanel = new ApplnSelectionAndStructureSizeInputPanel();
applnStructInputPanel.setSimContext(c.chosenContext);
applnStructInputPanel.setStructures(structures);
if (applnStructInputPanel.isNeedStructureSizes()) {
applnStructInputPanel.setPreferredSize(new java.awt.Dimension(350, 400));
applnStructInputPanel.setMaximumSize(new java.awt.Dimension(350, 400));
option = DialogUtils.showComponentOKCancelDialog(currentWindow, applnStructInputPanel, "Specify Structure Size to Export:");
structSelection = applnStructInputPanel.getStructSelectionIndex();
if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) {
break;
} else if (option == JOptionPane.OK_OPTION && structSelection < 0) {
DialogUtils.showErrorDialog(currentWindow, "Please select a structure and set its size");
}
} else {
// adapt to legacy logic ...
structSelection = 0;
option = JOptionPane.OK_OPTION;
}
}
if (option == JOptionPane.OK_OPTION) {
applnStructInputPanel.applyStructureNameAndSizeValues();
strucName = applnStructInputPanel.getSelectedStructureName();
selectedSimContext = applnStructInputPanel.getSelectedSimContext();
GeometryContext geoContext = selectedSimContext.getGeometryContext();
if (!isSpatial) {
// calculate structure Sizes only if appln is not spatial
structSize = applnStructInputPanel.getStructureSize();
// Invoke StructureSizeEvaluator to compute absolute sizes of compartments if all sizes are not set
if ((geoContext.isAllSizeSpecifiedNull() && geoContext.isAllVolFracAndSurfVolSpecifiedNull()) || ((strucName == null || structSize <= 0.0) && (geoContext.isAllSizeSpecifiedNull() && geoContext.isAllVolFracAndSurfVolSpecified())) || (!geoContext.isAllSizeSpecifiedPositive() && geoContext.isAllVolFracAndSurfVolSpecifiedNull()) || (!geoContext.isAllSizeSpecifiedPositive() && !geoContext.isAllVolFracAndSurfVolSpecified()) || (geoContext.isAllSizeSpecifiedNull() && !geoContext.isAllVolFracAndSurfVolSpecified())) {
DialogUtils.showErrorDialog(currentWindow, "Cannot export to SBML without compartment sizes being set. This can be automatically " + " computed if the absolute size of at least one compartment and the relative sizes (Surface-to-volume-ratio/Volume-fraction) " + " of all compartments are known. Sufficient information is not available to perform this computation." + "\n\nThis can be fixed by going back to the application '" + selectedSimContext.getName() + "' and setting structure sizes in the 'StructureMapping' tab.");
throw UserCancelException.CANCEL_XML_TRANSLATION;
}
if (!geoContext.isAllSizeSpecifiedPositive() && geoContext.isAllVolFracAndSurfVolSpecified()) {
Structure chosenStructure = selectedSimContext.getModel().getStructure(strucName);
StructureMapping chosenStructMapping = selectedSimContext.getGeometryContext().getStructureMapping(chosenStructure);
try {
StructureSizeSolver.updateAbsoluteStructureSizes(selectedSimContext, chosenStructure, structSize, chosenStructMapping.getSizeParameter().getUnitDefinition());
} catch (Exception e) {
throw new ProgrammingException("exception updating sizes", e);
}
}
} else {
if (!geoContext.isAllUnitSizeParameterSetForSpatial()) {
DialogUtils.showErrorDialog(currentWindow, "Cannot export to SBML without compartment size ratios being set." + "\n\nThis can be fixed by going back to the application '" + selectedSimContext.getName() + "' and setting structure" + " size ratios in the 'StructureMapping' tab.");
throw UserCancelException.CANCEL_XML_TRANSLATION;
}
}
// Select simulation whose overrides need to be exported
// If simContext doesn't have simulations, don't pop up simulationSelectionPanel
Simulation[] sims = bioModel.getSimulations(selectedSimContext);
// display only those simulations that have overrides in the simulationSelectionPanel.
Vector<Simulation> orSims = new Vector<Simulation>();
for (int s = 0; (sims != null) && (s < sims.length); s++) {
if (sims[s].getMathOverrides().hasOverrides()) {
orSims.addElement(sims[s]);
}
}
Simulation[] overriddenSims = orSims.toArray(new Simulation[orSims.size()]);
if (overriddenSims.length > 0) {
SimulationSelectionPanel simSelectionPanel = new SimulationSelectionPanel();
simSelectionPanel.setPreferredSize(new java.awt.Dimension(600, 400));
simSelectionPanel.setMaximumSize(new java.awt.Dimension(600, 400));
simSelectionPanel.setSimulations(overriddenSims);
int simOption = DialogUtils.showComponentOKCancelDialog(currentWindow, simSelectionPanel, "Select Simulation whose overrides should be exported:");
if (simOption == JOptionPane.OK_OPTION) {
selectedSimWOSBE = simSelectionPanel.getSelectedSimulation();
// if (chosenSimulation != null) {
// CARRY hashTable.put("selectedSimulation", chosenSimulation);
// }
} else if (simOption == JOptionPane.CANCEL_OPTION || simOption == JOptionPane.CLOSED_OPTION) {
// Hence canceling the entire export to SBML operation.
throw UserCancelException.CANCEL_XML_TRANSLATION;
}
}
} else if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) {
// Hence canceling the entire export to SBML operation.
throw UserCancelException.CANCEL_XML_TRANSLATION;
}
if (selectedSimWOSBE != null) {
String selectedFileName = c.filename;
// rename file to contain exported simulation.
String ext = FilenameUtils.getExtension(selectedFileName);
String base = FilenameUtils.getBaseName(selectedFileName);
String path = FilenameUtils.getPath(selectedFileName);
base += "_" + TokenMangler.mangleToSName(selectedSimWOSBE.getName());
selectedFileName = path + base + ext;
c.selectedFile.renameTo(new File(selectedFileName));
}
}
use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class SedmlExtensionFilter method writeBioModel.
@Override
public void writeBioModel(DocumentManager documentManager, BioModel bioModel, File exportFile, SimulationContext ignored) throws Exception {
String resultString;
// export the entire biomodel to a SEDML file (for now, only non-spatial,non-stochastic applns)
int sedmlLevel = 1;
int sedmlVersion = 1;
String sPath = FileUtils.getFullPathNoEndSeparator(exportFile.getAbsolutePath());
String sFile = FileUtils.getBaseName(exportFile.getAbsolutePath());
String sExt = FileUtils.getExtension(exportFile.getAbsolutePath());
SEDMLExporter sedmlExporter = null;
if (bioModel instanceof BioModel) {
sedmlExporter = new SEDMLExporter(bioModel, sedmlLevel, sedmlVersion);
resultString = sedmlExporter.getSEDMLFile(sPath);
} else {
throw new RuntimeException("unsupported Document Type " + bioModel.getClass().getName() + " for SedML export");
}
if (sExt.equals("sedx")) {
sedmlExporter.createManifest(sPath, sFile);
String sedmlFileName = sPath + FileUtils.WINDOWS_SEPARATOR + sFile + ".sedml";
XmlUtil.writeXMLStringToFile(resultString, sedmlFileName, true);
sedmlExporter.addSedmlFileToList(sFile + ".sedml");
sedmlExporter.addSedmlFileToList("manifest.xml");
sedmlExporter.createZipArchive(sPath, sFile);
return;
} else {
XmlUtil.writeXMLStringToFile(resultString, exportFile.getAbsolutePath(), true);
}
}
use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class ClientDocumentManager method substituteFieldFuncNames.
public void substituteFieldFuncNames(VCDocument vcDocument, VersionableTypeVersion originalOwner) throws DataAccessException, MathException, ExpressionException {
Vector<ExternalDataIdentifier> errorCleanupExtDataIDV = new Vector<ExternalDataIdentifier>();
try {
if (originalOwner == null || originalOwner.getVersion().getOwner().compareEqual(getUser())) {
// Substitution for FieldFunc not needed for new doc or if we own doc
return;
}
// Get Objects from Document that might need to have FieldFuncs replaced
Vector<Object> fieldFunctionContainer_mathDesc_or_simContextV = new Vector<Object>();
if (vcDocument instanceof MathModel) {
fieldFunctionContainer_mathDesc_or_simContextV.add(((MathModel) vcDocument).getMathDescription());
} else if (vcDocument instanceof BioModel) {
SimulationContext[] simContextArr = ((BioModel) vcDocument).getSimulationContexts();
for (int i = 0; i < simContextArr.length; i += 1) {
fieldFunctionContainer_mathDesc_or_simContextV.add(simContextArr[i]);
}
}
// Get original Field names
Vector<String> origFieldFuncNamesV = new Vector<String>();
for (int i = 0; i < fieldFunctionContainer_mathDesc_or_simContextV.size(); i += 1) {
Object fieldFunctionContainer = fieldFunctionContainer_mathDesc_or_simContextV.elementAt(i);
FieldFunctionArguments[] fieldFuncArgsArr = null;
if (fieldFunctionContainer instanceof MathDescription) {
fieldFuncArgsArr = FieldUtilities.getFieldFunctionArguments((MathDescription) fieldFunctionContainer);
} else if (fieldFunctionContainer instanceof SimulationContext) {
fieldFuncArgsArr = ((SimulationContext) fieldFunctionContainer).getFieldFunctionArguments();
}
for (int j = 0; j < fieldFuncArgsArr.length; j += 1) {
if (!origFieldFuncNamesV.contains(fieldFuncArgsArr[j].getFieldName())) {
origFieldFuncNamesV.add(fieldFuncArgsArr[j].getFieldName());
}
}
}
if (origFieldFuncNamesV.size() == 0) {
// No FieldFunctions to substitute
return;
}
FieldDataDBOperationResults copyNamesFieldDataOpResults = fieldDataDBOperation(FieldDataDBOperationSpec.createCopyNoConflictExtDataIDsSpec(getUser(), origFieldFuncNamesV.toArray(new String[0]), originalOwner));
errorCleanupExtDataIDV.addAll(copyNamesFieldDataOpResults.oldNameNewIDHash.values());
// Copy Field Data on Data Server FileSystem
for (String fieldname : origFieldFuncNamesV) {
KeyValue sourceSimDataKey = copyNamesFieldDataOpResults.oldNameOldExtDataIDKeyHash.get(fieldname);
if (sourceSimDataKey == null) {
throw new DataAccessException("Couldn't find original data key for FieldFunc " + fieldname);
}
ExternalDataIdentifier newExtDataID = copyNamesFieldDataOpResults.oldNameNewIDHash.get(fieldname);
getSessionManager().fieldDataFileOperation(FieldDataFileOperationSpec.createCopySimFieldDataFileOperationSpec(newExtDataID, sourceSimDataKey, originalOwner.getVersion().getOwner(), FieldDataFileOperationSpec.JOBINDEX_DEFAULT, getUser()));
}
// Finally substitute new Field names
for (int i = 0; i < fieldFunctionContainer_mathDesc_or_simContextV.size(); i += 1) {
Object fieldFunctionContainer = fieldFunctionContainer_mathDesc_or_simContextV.elementAt(i);
if (fieldFunctionContainer instanceof MathDescription) {
MathDescription mathDesc = (MathDescription) fieldFunctionContainer;
FieldUtilities.substituteFieldFuncNames(mathDesc, copyNamesFieldDataOpResults.oldNameNewIDHash);
} else if (fieldFunctionContainer instanceof SimulationContext) {
SimulationContext simContext = (SimulationContext) fieldFunctionContainer;
simContext.substituteFieldFuncNames(copyNamesFieldDataOpResults.oldNameNewIDHash);
}
}
fireFieldDataDB(new FieldDataDBEvent(this));
} catch (Exception e) {
e.printStackTrace();
// Cleanup
for (int i = 0; i < errorCleanupExtDataIDV.size(); i += 1) {
try {
fieldDataDBOperation(FieldDataDBOperationSpec.createDeleteExtDataIDSpec(errorCleanupExtDataIDV.elementAt(i)));
} catch (Exception e2) {
// ignore, we tried to cleanup
}
try {
fieldDataFileOperation(FieldDataFileOperationSpec.createDeleteFieldDataFileOperationSpec(errorCleanupExtDataIDV.elementAt(i)));
} catch (Exception e1) {
// ignore, we tried to cleanup
}
}
throw new RuntimeException("Error copying Field Data \n" + e.getMessage());
}
}
Aggregations