Skip to content

Commit b00f5f9

Browse files
Ion MoraruIon Moraru
authored andcommitted
Address all dependencies from SBMLExporter method refactoring
1 parent dfea301 commit b00f5f9

File tree

5 files changed

+27
-70
lines changed

5 files changed

+27
-70
lines changed

vcell-core/src/main/java/cbit/vcell/biomodel/ModelUnitConverter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
import cbit.vcell.parser.SymbolTable;
2424
import cbit.vcell.parser.SymbolTableEntry;
2525
import cbit.vcell.units.VCUnitDefinition;
26+
import cbit.vcell.xml.XMLSource;
2627
import cbit.vcell.xml.XmlHelper;
2728
import cbit.vcell.xml.XmlParseException;
2829

2930
public class ModelUnitConverter {
3031

3132
public static BioModel createBioModelWithNewUnitSystem(BioModel oldBioModel, ModelUnitSystem newUnitSystem) throws ExpressionException, XmlParseException {
3233
// new BioModel has new unit system applied to all built-in units ... but expressions still need to be corrected (see below).
33-
BioModel newBioModel = XmlHelper.cloneBioModelWithNewUnitSystem(oldBioModel, newUnitSystem);
34+
String biomodelXMLString = XmlHelper.bioModelToXML(oldBioModel);
35+
XMLSource newXMLSource = new XMLSource(biomodelXMLString);
36+
BioModel newBioModel = XmlHelper.XMLToBioModel(newXMLSource, true, newUnitSystem);
3437
Model newModel = newBioModel.getModel();
3538
Model oldModel = oldBioModel.getModel();
3639

vcell-core/src/main/java/cbit/vcell/xml/XmlHelper.java

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -215,41 +215,26 @@ public static String exportSBML(VCDocument vcDoc, int level, int version, int pk
215215
}
216216
if (vcDoc instanceof BioModel) {
217217
try {
218-
// check if model to be exported to SBML has units compatible with SBML default units (default units in SBML can be assumed only until SBML Level2)
219-
ModelUnitSystem forcedModelUnitSystem = simContext.getModel().getUnitSystem();
220-
if (level < 3 && !ModelUnitSystem.isCompatibleWithDefaultSBMLLevel2Units(forcedModelUnitSystem)) {
221-
forcedModelUnitSystem = ModelUnitSystem.createDefaultSBMLLevel2Units();
222-
} else if (forcedModelUnitSystem.getVolumeSubstanceUnit().getSymbol() != "molecules"){
223-
// need to replace volumeSubstanceUnit; molecules is the only one that allows the exporter to create valid unit conversions of parameters
224-
String volumeSubstanceSymbol = "molecules";
225-
String membraneSubstanceSymbol = forcedModelUnitSystem.getMembraneSubstanceUnit().getSymbol();
226-
String lumpedReactionSubstanceSymbol = forcedModelUnitSystem.getLumpedReactionSubstanceUnit().getSymbol();
227-
String lengthSymbol = forcedModelUnitSystem.getLengthUnit().getSymbol();
228-
String areaSymbol = forcedModelUnitSystem.getAreaUnit().getSymbol();
229-
String volumeSymbol = forcedModelUnitSystem.getVolumeUnit().getSymbol();
230-
String timeSymbol = forcedModelUnitSystem.getTimeUnit().getSymbol();
231-
forcedModelUnitSystem = ModelUnitSystem.createVCModelUnitSystem(volumeSubstanceSymbol, membraneSubstanceSymbol, lumpedReactionSubstanceSymbol, volumeSymbol, areaSymbol, lengthSymbol, timeSymbol);
232-
}
233-
// create new Biomodel with new (SBML compatible) unit system
234-
BioModel modifiedBiomodel = ModelUnitConverter.createBioModelWithNewUnitSystem(simContext.getBioModel(), forcedModelUnitSystem);
218+
// clone BioModel
219+
BioModel clonedBioModel = cloneBioModel(simContext.getBioModel());
235220
// extract the simContext from new Biomodel. Apply overrides to *this* modified simContext
236-
SimulationContext simContextFromModifiedBioModel = modifiedBiomodel.getSimulationContext(simContext.getName());
237-
SimulationContext clonedSimContext = applyOverridesForSBML(modifiedBiomodel, simContextFromModifiedBioModel, simJob);
221+
SimulationContext simContextFromClonedBioModel = clonedBioModel.getSimulationContext(simContext.getName());
222+
SimulationContext clonedSimContext = applyOverridesForSBML(clonedBioModel, simContextFromClonedBioModel, simJob);
238223
// extract sim (in simJob) from modified Biomodel, if not null
239224
SimulationJob modifiedSimJob = null;
240225
if (simJob != null) {
241-
Simulation simFromModifiedBiomodel = clonedSimContext.getSimulation(simJob.getSimulation().getName());
242-
modifiedSimJob = new SimulationJob(simFromModifiedBiomodel, simJob.getJobIndex(), null);
226+
Simulation simFromClonedBiomodel = clonedSimContext.getSimulation(simJob.getSimulation().getName());
227+
modifiedSimJob = new SimulationJob(simFromClonedBiomodel, simJob.getJobIndex(), null);
243228
}
244-
SBMLExporter sbmlExporter = new SBMLExporter(modifiedBiomodel, level, version, isSpatial);
245-
sbmlExporter.setSelectedSimContext(simContextFromModifiedBioModel);
229+
SBMLExporter sbmlExporter = new SBMLExporter(clonedBioModel, level, version, isSpatial);
230+
sbmlExporter.setSelectedSimContext(clonedSimContext);
246231
sbmlExporter.setSelectedSimulationJob(modifiedSimJob);
247-
String ret = sbmlExporter.getSBMLFile();
232+
String sbmlSTring = sbmlExporter.getSBMLString();
248233

249234
// cleanup the string of all the "sameAs" statements
250-
ret = SBMLAnnotationUtil.postProcessCleanup(ret);
251-
return ret;
252-
} catch (ExpressionException | SbmlException | SBMLException | XMLStreamException e) {
235+
sbmlSTring = SBMLAnnotationUtil.postProcessCleanup(sbmlSTring);
236+
return sbmlSTring;
237+
} catch (SbmlException | SBMLException | XMLStreamException e) {
253238
e.printStackTrace(System.out);
254239
throw new XmlParseException(e);
255240
}
@@ -808,15 +793,14 @@ public static BioModel XMLToBioModel(XMLSource xmlSource) throws XmlParseExcepti
808793
return XMLToBioModel(xmlSource, true, null);
809794
}
810795

811-
/** @deprecated */
812-
public static BioModel cloneBioModelWithNewUnitSystem(BioModel origBiomodel, ModelUnitSystem forcedModelUnitSystem) throws XmlParseException {
796+
public static BioModel cloneBioModel(BioModel origBiomodel) throws XmlParseException {
813797
String biomodelXMLString = bioModelToXML(origBiomodel);
814798
XMLSource newXMLSource = new XMLSource(biomodelXMLString);
815-
return XMLToBioModel(newXMLSource, true, forcedModelUnitSystem);
799+
return XMLToBioModel(newXMLSource);
816800
}
817801

818802

819-
static BioModel XMLToBioModel(XMLSource xmlSource, boolean printkeys, ModelUnitSystem forcedModelUnitSystem) throws XmlParseException {
803+
public static BioModel XMLToBioModel(XMLSource xmlSource, boolean printkeys, ModelUnitSystem forcedModelUnitSystem) throws XmlParseException {
820804

821805
//long l0 = System.currentTimeMillis();
822806
BioModel bioModel = null;

vcell-core/src/main/java/org/vcell/sedml/SEDMLExporter.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -230,20 +230,11 @@ private void translateBioModelToSedML(String savePath) {
230230
Map<Pair <String, String>, String> l2gMap = null; // local to global translation map
231231
if (vcBioModel instanceof BioModel) {
232232
try {
233-
// check if model to be exported to SBML has units compatible with SBML default units (default units in SBML can be assumed only until SBML Level2)
234-
ModelUnitSystem forcedModelUnitSystem = simContext.getModel().getUnitSystem();
235-
if (level < 4 && !ModelUnitSystem.isCompatibleWithDefaultSBMLLevel2Units(forcedModelUnitSystem)) {
236-
forcedModelUnitSystem = ModelUnitSystem.createDefaultSBMLLevel2Units();
237-
}
238-
// create new Biomodel with new (SBML compatible) unit system
239-
BioModel modifiedBiomodel = ModelUnitConverter.createBioModelWithNewUnitSystem(simContext.getBioModel(), forcedModelUnitSystem);
240-
// extract the simContext from new Biomodel. Apply overrides to *this* modified simContext
241-
SimulationContext simContextFromModifiedBioModel = modifiedBiomodel.getSimulationContext(simContext.getName());
242-
SBMLExporter sbmlExporter = new SBMLExporter(modifiedBiomodel, level, version, isSpatial);
243-
sbmlExporter.setSelectedSimContext(simContextFromModifiedBioModel);
233+
SBMLExporter sbmlExporter = new SBMLExporter(vcBioModel, level, version, isSpatial);
234+
sbmlExporter.setSelectedSimContext(simContext);
244235
sbmlExporter.setSelectedSimulationJob(null); // no sim job
245236
try {
246-
sbmlString = sbmlExporter.getSBMLFile();
237+
sbmlString = sbmlExporter.getSBMLString();
247238
} catch (RuntimeException e) {
248239
if (simContext.getGeometry().getDimension() > 0 && simContext.getApplicationType() == Application.NETWORK_DETERMINISTIC ) {
249240
continue; // we skip importing 3D deterministic applications if SBML exceptions
@@ -252,7 +243,7 @@ private void translateBioModelToSedML(String savePath) {
252243
}
253244
}
254245
l2gMap = sbmlExporter.getLocalToGlobalTranslationMap();
255-
} catch (ExpressionException | SbmlException e) {
246+
} catch (SbmlException e) {
256247
e.printStackTrace(System.out);
257248
throw new XmlParseException(e);
258249
}

vcell-core/src/test/java/org/vcell/sbml/SBMLSpatialTest.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,8 @@ public void test() throws Exception {
4949
boolean isSpatial = sc1.getGeometry().getDimension()>0;
5050
SBMLExporter exporter = new SBMLExporter(bioModel1,3,1,isSpatial);
5151
sc1.refreshMathDescription(null, NetworkGenerationRequirements.ComputeFullNoTimeout);
52-
// sc1.setMathDescription(sc1.createNewMathMapping(null, NetworkGenerationRequirements.ComputeFullNoTimeout).getMathDescription());
5352
exporter.setSelectedSimContext(sc1);
54-
VCellSBMLDoc sbmlDoc = exporter.convertToSBML();
55-
for (UnitDefinition unitDefn : sbmlDoc.model.getListOfUnitDefinitions()){
56-
for (Unit unit : unitDefn.getListOfUnits()){
57-
System.out.println(unit.getKind());
58-
if (!unit.isSetKind()){
59-
throw new RuntimeException("kind of unit "+unit.printUnit()+" of UnitDefn "+UnitDefinition.printUnits(unitDefn)+" is not set");
60-
}
61-
}
62-
}
63-
// sbmlDoc.document.setConsistencyChecks(CHECK_CATEGORY.UNITS_CONSISTENCY, false);
64-
// int numErrors = sbmlDoc.document.checkConsistency();
65-
// System.out.println("consistency check, num errors = "+numErrors);
66-
// if (numErrors>0){
67-
// SBMLErrorLog errorLog = sbmlDoc.document.getListOfErrors();
68-
// for (int err=0; err<errorLog.getErrorCount(); err++){
69-
// System.err.println("ERROR IN EXPORTED SBML: "+errorLog.getError(err).getMessage());
70-
// }
71-
// //Assert.fail("generated SBML document was found to be inconsistent");
72-
// }
73-
String sbmlString = sbmlDoc.xmlString;
53+
String sbmlString = exporter.getSBMLString();
7454
File tempFile = File.createTempFile("sbmlSpatialTest_SBML_", ".sbml.xml");
7555
FileUtils.write(tempFile, sbmlString);
7656
System.out.println(tempFile);

vcell-sbmlsim/src/main/java/org/vcell/sbmlsim/service/SimulationServiceImpl.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,12 @@ public VcmlToSbmlResults getSBML(File vcmlFile, String applicationName, File out
482482
try {
483483
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(vcmlFile));
484484
SimulationContext simContext = bioModel.getSimulationContext(applicationName);
485-
SBMLExporter exporter = new SBMLExporter(simContext,3,1,simContext.getGeometry().getDimension()>0);
486-
VCellSBMLDoc sbmlDoc = exporter.convertToSBML();
487-
FileUtils.write(outputFile, sbmlDoc.xmlString);
485+
String sbml = XmlHelper.exportSBML(bioModel, 3, 1, 0, simContext.getGeometry().getDimension()>0, simContext, null);
486+
FileUtils.write(outputFile, sbml);
488487
VcmlToSbmlResults results = new VcmlToSbmlResults();
489488
results.setSbmlFilePath(outputFile);
490489
return results;
491-
} catch (SBMLException | XmlParseException | SbmlException | XMLStreamException e) {
490+
} catch (XmlParseException e) {
492491
e.printStackTrace();
493492
throw new Exception("failed to generate SBML document: "+e.getMessage());
494493
}

0 commit comments

Comments
 (0)