Skip to content

Commit 34f7938

Browse files
committed
implement vcml_to_vcml (refresh) and add entrypoints
1 parent 417a4b8 commit 34f7938

File tree

4 files changed

+153
-19
lines changed

4 files changed

+153
-19
lines changed

vcell-native/src/main/java/org/vcell/libvcell/Entrypoints.java

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
import org.json.simple.JSONValue;
1010

1111
import java.io.File;
12+
import java.nio.file.Path;
1213
import java.util.concurrent.ConcurrentHashMap;
1314

1415
import static org.vcell.libvcell.SolverUtils.sbmlToFiniteVolumeInput;
1516
import static org.vcell.libvcell.SolverUtils.vcmlToFiniteVolumeInput;
17+
import static org.vcell.libvcell.ModelUtils.vcml_to_sbml;
18+
import static org.vcell.libvcell.ModelUtils.sbml_to_vcml;
19+
import static org.vcell.libvcell.ModelUtils.vcml_to_vcml;
1620

1721

1822
public class Entrypoints {
@@ -52,6 +56,7 @@ public String toJson() {
5256
documentation = """
5357
Converts VCML file into Finite Volume Input files.
5458
vcml_content: text of VCML XML document
59+
simulation_name: name of the simulation to convert
5560
output_dir_path: path to the output directory (expected to be subdirectory of the workspace)
5661
Returns a JSON string with success status and message"""
5762
)
@@ -106,4 +111,96 @@ public static CCharPointer entrypoint_sbmlToFiniteVolumeInput(
106111
logger.info("Returning from sbmlToFiniteVolumeInput: " + json);
107112
return createString(json);
108113
}
109-
}
114+
115+
@CEntryPoint(
116+
name = "vcmlToSbml",
117+
documentation = """
118+
Converts VCML file into an SBML file.
119+
vcml_content: text of VCML XML document
120+
application_name: name of the application to export
121+
sbml_file_path: path to the SBML file to write
122+
validate_sbml: whether to validate the SBML file
123+
Returns a JSON string with success status and message"""
124+
)
125+
public static CCharPointer entrypoint_vcmlToSbml(
126+
IsolateThread ignoredThread,
127+
CCharPointer vcml_content,
128+
CCharPointer application_name,
129+
CCharPointer sbml_file_path,
130+
boolean validate_sbml) {
131+
ReturnValue returnValue;
132+
try {
133+
String vcmlContentStr = CTypeConversion.toJavaString(vcml_content);
134+
String applicationName = CTypeConversion.toJavaString(application_name);
135+
Path sbmlFilePath = new File(CTypeConversion.toJavaString(sbml_file_path)).toPath();
136+
vcml_to_sbml(vcmlContentStr, applicationName, sbmlFilePath, validate_sbml);
137+
returnValue = new ReturnValue(true, "Success");
138+
}catch (Throwable t) {
139+
logger.error("Error translating vcml application to sbml", t);
140+
returnValue = new ReturnValue(false, t.getMessage());
141+
}
142+
// return result as a json string
143+
String json = returnValue.toJson();
144+
logger.info("Returning from vcellToSbml: " + json);
145+
return createString(json);
146+
}
147+
148+
@CEntryPoint(
149+
name = "sbmlToVcml",
150+
documentation = """
151+
Converts SBML file into a VCML file.
152+
sbml_content: text of SBML XML document
153+
vcml_file_path: path to the VCML file to write
154+
validate_sbml: whether to validate the SBML file
155+
Returns a JSON string with success status and message"""
156+
)
157+
public static CCharPointer entrypoint_sbmlToVcml(
158+
IsolateThread ignoredThread,
159+
CCharPointer sbml_content,
160+
CCharPointer vcml_file_path,
161+
boolean validate_sbml) {
162+
ReturnValue returnValue;
163+
try {
164+
String sbmlContentStr = CTypeConversion.toJavaString(sbml_content);
165+
Path vcmlFilePath = new File(CTypeConversion.toJavaString(vcml_file_path)).toPath();
166+
sbml_to_vcml(sbmlContentStr, vcmlFilePath, validate_sbml);
167+
returnValue = new ReturnValue(true, "Success");
168+
}catch (Throwable t) {
169+
logger.error("Error translating sbml to vcml", t);
170+
returnValue = new ReturnValue(false, t.getMessage());
171+
}
172+
// return result as a json string
173+
String json = returnValue.toJson();
174+
logger.info("Returning from sbmlToVcell: " + json);
175+
return createString(json);
176+
}
177+
178+
@CEntryPoint(
179+
name = "vcmlToVcml",
180+
documentation = """
181+
Updates a VCML file into a fully populated VCML file.
182+
vcml_content: text of VCML XML document
183+
vcml_file_path: path to the VCML file to write
184+
Returns a JSON string with success status and message"""
185+
)
186+
public static CCharPointer entrypoint_vcmlToVcml(
187+
IsolateThread ignoredThread,
188+
CCharPointer vcml_content,
189+
CCharPointer vcml_file_path) {
190+
ReturnValue returnValue;
191+
try {
192+
String vcmlContentStr = CTypeConversion.toJavaString(vcml_content);
193+
Path vcmlFilePath = new File(CTypeConversion.toJavaString(vcml_file_path)).toPath();
194+
vcml_to_vcml(vcmlContentStr, vcmlFilePath);
195+
returnValue = new ReturnValue(true, "Success");
196+
}catch (Throwable t) {
197+
logger.error("Error refreshing vcml", t);
198+
returnValue = new ReturnValue(false, t.getMessage());
199+
}
200+
// return result as a json string
201+
String json = returnValue.toJson();
202+
logger.info("Returning from vcellToVcml: " + json);
203+
return createString(json);
204+
}
205+
206+
}

vcell-native/src/main/java/org/vcell/libvcell/MainRecorder.java

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import static org.vcell.libvcell.SolverUtils.vcmlToFiniteVolumeInput;
1212
import static org.vcell.libvcell.ModelUtils.sbml_to_vcml;
1313
import static org.vcell.libvcell.ModelUtils.vcml_to_sbml;
14+
import static org.vcell.libvcell.ModelUtils.vcml_to_vcml;
1415

1516
public class MainRecorder {
1617
private static final Logger logger = LogManager.getLogger(MainRecorder.class);
@@ -82,6 +83,24 @@ public static void main(String[] args) {
8283

8384
}
8485

86+
try (FileInputStream f_vcml = new FileInputStream(vcml_file)) {
87+
byte[] data = f_vcml.readAllBytes();
88+
logger.info("Read " + data.length + " bytes from " + vcml_file.getAbsolutePath());
89+
String vcml_str = new String(data);
90+
91+
// create a temporary file for the VCML output
92+
File temp_vcml_file = new File(output_dir, "temp.vcml");
93+
vcml_to_vcml(vcml_str, temp_vcml_file.toPath());
94+
// remove temporary file
95+
if (temp_vcml_file.exists()) {
96+
boolean deleted = temp_vcml_file.delete();
97+
if (!deleted) {
98+
logger.warn("Failed to delete temporary VCML file: " + temp_vcml_file.getAbsolutePath());
99+
}
100+
}
101+
102+
}
103+
85104
// use reflection to load jsbml classes and call their default constructors
86105
Class.forName("org.sbml.jsbml.AlgebraicRule").getDeclaredConstructor().newInstance();
87106
Class.forName("org.sbml.jsbml.Annotation").getDeclaredConstructor().newInstance();
@@ -110,25 +129,25 @@ public static void main(String[] args) {
110129
Class.forName("org.sbml.jsbml.Trigger").getDeclaredConstructor().newInstance();
111130
Class.forName("org.sbml.jsbml.Unit").getDeclaredConstructor().newInstance();
112131
Class.forName("org.sbml.jsbml.UnitDefinition").getDeclaredConstructor().newInstance();
113-
Class.forName("org.sbml.jsbml.xml.parsers.ArraysParser").getDeclaredConstructor().newInstance();
114-
Class.forName("org.sbml.jsbml.xml.parsers.CompParser").getDeclaredConstructor().newInstance();
115-
Class.forName("org.sbml.jsbml.xml.parsers.DistribParser").getDeclaredConstructor().newInstance();
116-
Class.forName("org.sbml.jsbml.xml.parsers.DynParser").getDeclaredConstructor().newInstance();
117-
Class.forName("org.sbml.jsbml.xml.parsers.FBCParser").getDeclaredConstructor().newInstance();
118-
Class.forName("org.sbml.jsbml.xml.parsers.GroupsParser").getDeclaredConstructor().newInstance();
119-
Class.forName("org.sbml.jsbml.xml.parsers.L3LayoutParser").getDeclaredConstructor().newInstance();
120-
Class.forName("org.sbml.jsbml.xml.parsers.LayoutParser").getDeclaredConstructor().newInstance();
121-
Class.forName("org.sbml.jsbml.xml.parsers.MathMLStaxParser").getDeclaredConstructor().newInstance();
122-
Class.forName("org.sbml.jsbml.xml.parsers.MultiParser").getDeclaredConstructor().newInstance();
123-
Class.forName("org.sbml.jsbml.xml.parsers.QualParser").getDeclaredConstructor().newInstance();
124-
Class.forName("org.sbml.jsbml.xml.parsers.RenderParser").getDeclaredConstructor().newInstance();
125-
Class.forName("org.sbml.jsbml.xml.parsers.ReqParser").getDeclaredConstructor().newInstance();
126-
Class.forName("org.sbml.jsbml.xml.parsers.SBMLCoreParser").getDeclaredConstructor().newInstance();
127-
Class.forName("org.sbml.jsbml.xml.parsers.SBMLLevel1Rule").getDeclaredConstructor().newInstance();
128-
Class.forName("org.sbml.jsbml.xml.parsers.SBMLRDFAnnotationParser").getDeclaredConstructor().newInstance();
132+
// Class.forName("org.sbml.jsbml.xml.parsers.ArraysParser").getDeclaredConstructor().newInstance();
133+
// Class.forName("org.sbml.jsbml.xml.parsers.CompParser").getDeclaredConstructor().newInstance();
134+
// Class.forName("org.sbml.jsbml.xml.parsers.DistribParser").getDeclaredConstructor().newInstance();
135+
// Class.forName("org.sbml.jsbml.xml.parsers.DynParser").getDeclaredConstructor().newInstance();
136+
// Class.forName("org.sbml.jsbml.xml.parsers.FBCParser").getDeclaredConstructor().newInstance();
137+
// Class.forName("org.sbml.jsbml.xml.parsers.GroupsParser").getDeclaredConstructor().newInstance();
138+
// Class.forName("org.sbml.jsbml.xml.parsers.L3LayoutParser").getDeclaredConstructor().newInstance();
139+
// Class.forName("org.sbml.jsbml.xml.parsers.LayoutParser").getDeclaredConstructor().newInstance();
140+
// Class.forName("org.sbml.jsbml.xml.parsers.MathMLStaxParser").getDeclaredConstructor().newInstance();
141+
// Class.forName("org.sbml.jsbml.xml.parsers.MultiParser").getDeclaredConstructor().newInstance();
142+
// Class.forName("org.sbml.jsbml.xml.parsers.QualParser").getDeclaredConstructor().newInstance();
143+
// Class.forName("org.sbml.jsbml.xml.parsers.RenderParser").getDeclaredConstructor().newInstance();
144+
// Class.forName("org.sbml.jsbml.xml.parsers.ReqParser").getDeclaredConstructor().newInstance();
145+
// Class.forName("org.sbml.jsbml.xml.parsers.SBMLCoreParser").getDeclaredConstructor().newInstance();
146+
// Class.forName("org.sbml.jsbml.xml.parsers.SBMLLevel1Rule").getDeclaredConstructor().newInstance();
147+
// Class.forName("org.sbml.jsbml.xml.parsers.SBMLRDFAnnotationParser").getDeclaredConstructor().newInstance();
129148
Class.forName("org.sbml.jsbml.xml.parsers.SpatialParser").getDeclaredConstructor().newInstance();
130-
Class.forName("org.sbml.jsbml.xml.parsers.UncertMLXMLNodeReader").getDeclaredConstructor().newInstance();
131-
Class.forName("org.sbml.jsbml.xml.parsers.XMLNodeReader").getDeclaredConstructor().newInstance();
149+
// Class.forName("org.sbml.jsbml.xml.parsers.UncertMLXMLNodeReader").getDeclaredConstructor().newInstance();
150+
// Class.forName("org.sbml.jsbml.xml.parsers.XMLNodeReader").getDeclaredConstructor().newInstance();
132151
} catch (Exception e) {
133152
System.out.println(e.getMessage());
134153
logger.error("Error processing spatial model", e);

vcell-native/src/main/java/org/vcell/libvcell/ModelUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,12 @@ public static void vcml_to_sbml(String vcml_content, String applicationName, Pat
7070
String sbml_string = sbmlExporter.getSBMLString();
7171
XmlUtil.writeXMLStringToFile(sbml_string, sbmlPath.toFile().getAbsolutePath(), true);
7272
}
73+
74+
public static void vcml_to_vcml(String vcml_content, Path vcmlPath) throws XmlParseException, IOException, MappingException {
75+
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(vcml_content));
76+
bioModel.updateAll(false);
77+
// write the BioModel to a VCML file
78+
String vcml_str = XmlHelper.bioModelToXML(bioModel);
79+
XmlUtil.writeXMLStringToFile(vcml_str, vcmlPath.toFile().getAbsolutePath(), true);
80+
}
7381
}

vcell-native/src/test/java/org/vcell/libvcell/EntrypointsTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.vcell.libvcell.SolverUtils.vcmlToFiniteVolumeInput;
2828
import static org.vcell.libvcell.ModelUtils.sbml_to_vcml;
2929
import static org.vcell.libvcell.ModelUtils.vcml_to_sbml;
30+
import static org.vcell.libvcell.ModelUtils.vcml_to_vcml;
3031

3132
public class EntrypointsTest {
3233

@@ -132,6 +133,15 @@ public void test_vcml_to_sbml() throws MappingException, IOException, XmlParseEx
132133
assert(sbml_temp_file.exists());
133134
}
134135

136+
@Test
137+
public void test_vcml_to_vcml() throws MappingException, IOException, XmlParseException, XMLStreamException, SbmlException {
138+
String vcmlContent = getFileContentsAsString("/TinySpatialProject_Application0.vcml");
139+
File parent_dir = Files.createTempDirectory("vcmlToVcml").toFile();
140+
File vcml_temp_file = new File(parent_dir, "temp.vcml");
141+
vcml_to_vcml(vcmlContent, vcml_temp_file.toPath());
142+
assert(vcml_temp_file.exists());
143+
}
144+
135145
@Test
136146
public void testVcmlToFiniteVolumeInput_bad_simname() throws IOException {
137147
String vcmlContent = getFileContentsAsString("/TinySpatialProject_Application0.vcml");

0 commit comments

Comments
 (0)