|
1 | 1 | package org.vcell.libvcell; |
2 | 2 |
|
| 3 | +import cbit.image.ImageException; |
3 | 4 | import cbit.util.xml.VCLogger; |
4 | 5 | import cbit.util.xml.VCLoggerException; |
5 | 6 | import cbit.util.xml.XmlUtil; |
6 | 7 | import cbit.vcell.biomodel.BioModel; |
| 8 | +import cbit.vcell.biomodel.ModelUnitConverter; |
| 9 | +import cbit.vcell.geometry.GeometryException; |
7 | 10 | import cbit.vcell.geometry.GeometrySpec; |
8 | 11 | import cbit.vcell.mapping.MappingException; |
9 | 12 | import cbit.vcell.mapping.SimulationContext; |
10 | 13 | import cbit.vcell.mongodb.VCMongoMessage; |
| 14 | +import cbit.vcell.parser.ExpressionException; |
11 | 15 | import cbit.vcell.xml.XMLSource; |
12 | 16 | import cbit.vcell.xml.XmlHelper; |
13 | 17 | import cbit.vcell.xml.XmlParseException; |
14 | 18 | import org.vcell.sbml.SbmlException; |
| 19 | +import org.vcell.sbml.vcell.SBMLAnnotationUtil; |
15 | 20 | import org.vcell.sbml.vcell.SBMLExporter; |
16 | 21 | import org.vcell.sbml.vcell.SBMLImporter; |
| 22 | +import org.vcell.util.Pair; |
17 | 23 |
|
18 | 24 | import javax.xml.stream.XMLStreamException; |
19 | 25 | import java.io.ByteArrayInputStream; |
@@ -66,17 +72,43 @@ record LoggerMessage(VCLogger.Priority priority, VCLogger.ErrorType errorType, S |
66 | 72 | } |
67 | 73 |
|
68 | 74 |
|
69 | | - public static void vcml_to_sbml(String vcml_content, String applicationName, Path sbmlPath) |
70 | | - throws XmlParseException, IOException, XMLStreamException, SbmlException, MappingException { |
| 75 | + public static void vcml_to_sbml(String vcml_content, String applicationName, Path sbmlPath, boolean roundTripValidation) |
| 76 | + throws XmlParseException, IOException, XMLStreamException, SbmlException, MappingException, ImageException, GeometryException, ExpressionException { |
71 | 77 | GeometrySpec.avoidAWTImageCreation = true; |
72 | 78 | VCMongoMessage.enabled = false; |
73 | 79 |
|
74 | 80 | BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(vcml_content)); |
75 | 81 | bioModel.updateAll(false); |
76 | | - SimulationContext simContext = bioModel.getSimulationContext(applicationName); |
77 | | - boolean validateSBML = true; |
78 | | - SBMLExporter sbmlExporter = new SBMLExporter(simContext, 3, 1, validateSBML); |
| 82 | + |
| 83 | + if (applicationName == null || applicationName.isEmpty()) { |
| 84 | + throw new RuntimeException("Error: Application name is null or empty"); |
| 85 | + } |
| 86 | + |
| 87 | + if (bioModel.getSimulationContext(applicationName) == null) { |
| 88 | + throw new RuntimeException("Error: Simulation context not found for application name: " + applicationName); |
| 89 | + } |
| 90 | + |
| 91 | + // change the unit system to SBML preferred units if not already. |
| 92 | + final BioModel sbmlPreferredUnitsBM; |
| 93 | + if (!bioModel.getModel().getUnitSystem().compareEqual(ModelUnitConverter.createSbmlModelUnitSystem())) { |
| 94 | + sbmlPreferredUnitsBM = ModelUnitConverter.createBioModelWithSBMLUnitSystem(bioModel); |
| 95 | + if(sbmlPreferredUnitsBM == null) { |
| 96 | + throw new RuntimeException("Unable to clone BioModel with SBML unit system"); |
| 97 | + } |
| 98 | + } else { |
| 99 | + sbmlPreferredUnitsBM = bioModel; |
| 100 | + } |
| 101 | + |
| 102 | + SimulationContext simContext = sbmlPreferredUnitsBM.getSimulationContext(applicationName); |
| 103 | + |
| 104 | + int sbml_level = 3; |
| 105 | + int sbml_version = 1; |
| 106 | + SBMLExporter sbmlExporter = new SBMLExporter(simContext, sbml_level, sbml_version, roundTripValidation); |
79 | 107 | String sbml_string = sbmlExporter.getSBMLString(); |
| 108 | + |
| 109 | + // cleanup the string of all the "sameAs" statements |
| 110 | + sbml_string = SBMLAnnotationUtil.postProcessCleanup(sbml_string); |
| 111 | + |
80 | 112 | XmlUtil.writeXMLStringToFile(sbml_string, sbmlPath.toFile().getAbsolutePath(), true); |
81 | 113 | } |
82 | 114 |
|
|
0 commit comments