Search in sources :

Example 1 with SEDMLExporter

use of org.vcell.sedml.SEDMLExporter in project vcell by virtualcell.

the class OmexExtensionFilter 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 (all supported applications)
    int sedmlLevel = 1;
    int sedmlVersion = 2;
    String sPath = FileUtils.getFullPathNoEndSeparator(exportFile.getAbsolutePath());
    String sFile = FileUtils.getBaseName(exportFile.getAbsolutePath());
    String sExt = FileUtils.getExtension(exportFile.getAbsolutePath());
    SEDMLExporter sedmlExporter = null;
    if (bioModel != null) {
        Object[] options = { "VCML", "SBML" };
        int choice = // parent component
        JOptionPane.showOptionDialog(// parent component
        null, // message,
        "VCML or SBML?", // title
        "Choose an option", // optionType
        JOptionPane.YES_NO_OPTION, // messageType
        JOptionPane.QUESTION_MESSAGE, // Icon
        null, options, // initialValue
        "VCML");
        boolean bForceVCML = choice == 0 ? true : false;
        sedmlExporter = new SEDMLExporter(bioModel, sedmlLevel, sedmlVersion, null);
        resultString = sedmlExporter.getSEDMLFile(sPath, sFile, bForceVCML, false, true);
        // convert biomodel to vcml and save to file.
        String vcmlString = XmlHelper.bioModelToXML(bioModel);
        String vcmlFileName = Paths.get(sPath, sFile + ".vcml").toString();
        File vcmlFile = new File(vcmlFileName);
        XmlUtil.writeXMLStringToFile(vcmlString, vcmlFile.getAbsolutePath(), true);
    } else {
        throw new RuntimeException("unsupported Document Type " + Objects.requireNonNull(bioModel).getClass().getName() + " for SedML export");
    }
    if (sExt.equals("omex")) {
        doSpecificWork(sedmlExporter, resultString, sPath, sFile);
        return;
    } else {
        XmlUtil.writeXMLStringToFile(resultString, exportFile.getAbsolutePath(), true);
    }
}
Also used : SEDMLExporter(org.vcell.sedml.SEDMLExporter) File(java.io.File)

Example 2 with SEDMLExporter

use of org.vcell.sedml.SEDMLExporter 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 != null) {
        Object[] options = { "VCML", "SBML" };
        int choice = // parent component
        JOptionPane.showOptionDialog(// parent component
        null, // message,
        "VCML or SBML?", // title
        "Choose an option", // optionType
        JOptionPane.YES_NO_OPTION, // messageType
        JOptionPane.QUESTION_MESSAGE, // Icon
        null, options, // initialValue
        "VCML");
        boolean bForceVCML = choice == 0 ? true : false;
        sedmlExporter = new SEDMLExporter(bioModel, sedmlLevel, sedmlVersion, null);
        resultString = sedmlExporter.getSEDMLFile(sPath, sFile, bForceVCML, false, false);
    } else {
        throw new RuntimeException("unsupported Document Type " + Objects.requireNonNull(bioModel).getClass().getName() + " for SedML export");
    }
    if (sExt.equals("sedml")) {
        doSpecificWork(sedmlExporter, resultString, sPath, sFile);
        return;
    } else {
        XmlUtil.writeXMLStringToFile(resultString, exportFile.getAbsolutePath(), true);
    }
}
Also used : SEDMLExporter(org.vcell.sedml.SEDMLExporter)

Example 3 with SEDMLExporter

use of org.vcell.sedml.SEDMLExporter 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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Variable(cbit.vcell.math.Variable) SolverDescription(cbit.vcell.solver.SolverDescription) KeyValue(org.vcell.util.document.KeyValue) SEDMLExporter(org.vcell.sedml.SEDMLExporter) ArrayList(java.util.ArrayList) SimulationJobStatusPersistent(cbit.vcell.server.SimulationJobStatusPersistent) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) CombineArchive(org.sbml.libcombine.CombineArchive) PropertyVetoException(java.beans.PropertyVetoException) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) XmlParseException(cbit.vcell.xml.XmlParseException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) PropertyVetoException(java.beans.PropertyVetoException) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) BioModel(cbit.vcell.biomodel.BioModel) File(java.io.File) XMLSource(cbit.vcell.xml.XMLSource)

Aggregations

SEDMLExporter (org.vcell.sedml.SEDMLExporter)3 File (java.io.File)2 BioModel (cbit.vcell.biomodel.BioModel)1 FieldFunctionArguments (cbit.vcell.field.FieldFunctionArguments)1 Variable (cbit.vcell.math.Variable)1 Expression (cbit.vcell.parser.Expression)1 SimulationJobStatusPersistent (cbit.vcell.server.SimulationJobStatusPersistent)1 Simulation (cbit.vcell.solver.Simulation)1 SolverDescription (cbit.vcell.solver.SolverDescription)1 XMLSource (cbit.vcell.xml.XMLSource)1 XmlParseException (cbit.vcell.xml.XmlParseException)1 PropertyVetoException (java.beans.PropertyVetoException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 RDFHandlerException (org.openrdf.rio.RDFHandlerException)1 CombineArchive (org.sbml.libcombine.CombineArchive)1 DataAccessException (org.vcell.util.DataAccessException)1