use of org.sbml.libcombine.CombineArchive in project vcell by virtualcell.
the class VcmlOmexConverter method vcmlToOmexConversion.
public static boolean vcmlToOmexConversion(String outputBaseDir) throws XmlParseException, IOException, DataAccessException, SQLException {
// Get VCML file path from -i flag
int sedmlLevel = 1;
int sedmlVersion = 2;
String inputVcmlFile = cliHandler.getInputFilePath();
// Get directory file path from -o flag
String outputDir = cliHandler.getOutputDirPath();
// get VCML name from VCML path
// String vcmlName = inputVcmlFile.split(File.separator, 10)[inputVcmlFile.split(File.separator, 10).length - 1].split("\\.", 5)[0];
// platform independent, strips extension too
String vcmlName = FilenameUtils.getBaseName(inputVcmlFile);
File vcmlFilePath = new File(inputVcmlFile);
// Create biomodel
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(vcmlFilePath));
bioModel.refreshDependencies();
int numSimulations = bioModel.getNumSimulations();
if (outputBaseDir != null && numSimulations == 0) {
CLIStandalone.writeSimErrorList(outputBaseDir, vcmlName + " has no simulations.");
return false;
}
// we extract the simulations with field data from the list of simulations since they are not supported
List<Simulation> simulationsToRemove = new ArrayList<>();
for (Simulation simulation : bioModel.getSimulations()) {
boolean bFieldDataFound = false;
Enumeration<Variable> variables = simulation.getMathDescription().getVariables();
while (variables.hasMoreElements()) {
Variable var = variables.nextElement();
Expression exp = var.getExpression();
FieldFunctionArguments[] ffas = FieldUtilities.getFieldFunctionArguments(exp);
if (ffas != null && ffas.length > 0) {
bFieldDataFound = true;
// we are done with this simulation if we know it has at least one variable with a field data in its expression
break;
}
}
if (bFieldDataFound) {
simulationsToRemove.add(simulation);
CLIStandalone.writeSimErrorList(outputBaseDir, vcmlName + " excluded: FieldData not supported at this time.");
SolverDescription solverDescription = simulation.getSolverTaskDescription().getSolverDescription();
String solverName = solverDescription.getShortDisplayLabel();
CLIStandalone.writeSimErrorList(outputBaseDir, " " + solverName);
}
}
for (Simulation simulation : simulationsToRemove) {
try {
bioModel.removeSimulation(simulation);
} catch (PropertyVetoException e) {
System.out.println("Failed to remove simulation with field data");
e.printStackTrace();
}
}
// we replace the obsolete solver with the fully supported equivalent
for (Simulation simulation : bioModel.getSimulations()) {
if (simulation.getSolverTaskDescription().getSolverDescription().equals(SolverDescription.FiniteVolume)) {
try {
simulation.getSolverTaskDescription().setSolverDescription(SolverDescription.SundialsPDE);
} catch (PropertyVetoException e) {
System.out.println("Failed to replace obsolete solver");
e.printStackTrace();
}
}
}
// NOTE: SEDML exporter exports both SEDML as well as required SBML
List<Simulation> simsToExport = null;
Set<String> solverNames = new LinkedHashSet<>();
if (bHasDataOnly) {
// make list of simulations to export with only sims that have data on the server
simsToExport = new ArrayList<Simulation>();
for (Simulation simulation : bioModel.getSimulations()) {
SolverDescription solverDescription = simulation.getSolverTaskDescription().getSolverDescription();
String solverName = solverDescription.getShortDisplayLabel();
solverNames.add(solverName);
// check server status
KeyValue parentKey = simulation.getSimulationVersion().getParentSimulationReference();
SimulationJobStatusPersistent[] statuses = adminDbTopLevel.getSimulationJobStatusArray(parentKey == null ? simulation.getKey() : parentKey, false);
if (statuses == null)
continue;
if (statuses.length == 0)
continue;
for (int i = 0; i < statuses.length; i++) {
if (statuses[i].hasData()) {
simsToExport.add(simulation);
continue;
}
}
// String dbDriverName = PropertyLoader.getProperty(PropertyLoader.dbDriverName, null);
// String dbConnectURL = PropertyLoader.getProperty(PropertyLoader.dbConnectURL, null);
// String dbSchemaUser = PropertyLoader.getProperty(PropertyLoader.dbUserid, null);
// String dbPassword = PropertyLoader.getSecretValue(PropertyLoader.dbPasswordValue, PropertyLoader.dbPasswordFile);
// DataSetControllerImpl dsControllerImpl = new DataSetControllerImpl(null, new File(outputBaseDir), null);
//
// code used to recover field data
//
// HashMap<User, Vector<ExternalDataIdentifier>> allExternalDataIdentifiers = FieldDataDBOperationDriver.getAllExternalDataIdentifiers();
// Enumeration<Variable> variables = simulation.getMathDescription().getVariables();
// while(variables.hasMoreElements()) {
// Variable var = variables.nextElement();
// Expression exp = var.getExpression();
// FieldFunctionArguments[] ffas = FieldUtilities.getFieldFunctionArguments( exp);
//
// if(ffas != null && ffas.length > 0) {
// FieldDataIdentifierSpec[] aaa = DataSetControllerImpl.getFieldDataIdentifierSpecs_private(
// ffas, simulation.getVersion().getOwner(), true, allExternalDataIdentifiers);
//
// System.out.println((ffas == null) ? "null" : exp.infix() + ", not null:" + ffas.length);
// }
// }
}
}
if (outputBaseDir != null && bHasDataOnly == true && simsToExport.size() == 0) {
CLIStandalone.writeSimErrorList(outputBaseDir, vcmlName + " has no simulations with any results.");
for (String solverName : solverNames) {
CLIStandalone.writeSimErrorList(outputBaseDir, " " + solverName);
}
return false;
}
String rdfString = getMetadata(vcmlName, bioModel);
XmlUtil.writeXMLStringToFile(rdfString, String.valueOf(Paths.get(outputDir, "metadata.rdf")), true);
if (bMakeLogsOnly) {
return false;
}
SEDMLExporter sedmlExporter = new SEDMLExporter(bioModel, sedmlLevel, sedmlVersion, simsToExport);
String sedmlString = sedmlExporter.getSEDMLFile(outputDir, vcmlName, bForceVCML, bHasDataOnly, true);
XmlUtil.writeXMLStringToFile(sedmlString, String.valueOf(Paths.get(outputDir, vcmlName + ".sedml")), true);
// libCombine needs native lib
ResourceUtil.setNativeLibraryDirectory();
NativeLoader.load("combinej");
boolean isDeleted = false;
boolean isCreated;
try {
CombineArchive archive = new CombineArchive();
String[] files;
// TODO: try-catch if no files
File dir = new File(outputDir);
files = dir.list();
for (String sd : files) {
if (sd.endsWith(".sedml")) {
archive.addFile(Paths.get(outputDir, sd).toString(), // target file name
"./" + sd, KnownFormats.lookupFormat("sedml"), // mark file as master
true);
} else if (sd.endsWith(".sbml") || sd.endsWith(".xml")) {
archive.addFile(Paths.get(outputDir, sd).toString(), "./" + sd, KnownFormats.lookupFormat("sbml"), // mark file as master
false);
} else if (sd.endsWith(".rdf")) {
archive.addFile(Paths.get(outputDir, sd).toString(), "./" + sd, "http://identifiers.org/combine.specifications/omex-metadata", // KnownFormats.lookupFormat("xml"),
false);
}
}
archive.addFile(Paths.get(String.valueOf(vcmlFilePath)).toString(), "./" + vcmlName + ".vcml", "http://purl.org/NET/mediatypes/application/vcml+xml", false);
// writing into combine archive
String omexPath = Paths.get(outputDir, vcmlName + ".omex").toString();
File omexFile = new File(omexPath);
// Deleting file if already exists with same name
if (omexFile.exists()) {
omexFile.delete();
}
isCreated = archive.writeToFile(omexPath);
// Removing all other files(like SEDML, XML, SBML) after archiving
for (String sd : files) {
// removing
if (sd.endsWith(".sedml") || sd.endsWith(".sbml") || sd.endsWith("xml") || sd.endsWith("vcml") || sd.endsWith("rdf")) {
isDeleted = Paths.get(outputDir, sd).toFile().delete();
}
}
if (isDeleted)
System.out.println("Removed intermediary files");
} catch (Exception e) {
throw new RuntimeException("createZipArchive threw exception: " + e.getMessage());
}
return isCreated;
}
Aggregations