Search in sources :

Example 1 with IXPathToVariableIDResolver

use of org.jlibsedml.execution.IXPathToVariableIDResolver in project vcell by virtualcell.

the class CLIUtils method generateReportsAsCSV.

public HashMap<String, File> generateReportsAsCSV(SedML sedml, HashMap<String, ODESolverResultSet> resultsHash, File outDirForCurrentSedml, String outDir, String sedmlLocation) throws DataAccessException, IOException {
    // finally, the real work
    HashMap<String, File> reportsHash = new HashMap<>();
    List<Output> ooo = sedml.getOutputs();
    for (Output oo : ooo) {
        if (!(oo instanceof Report)) {
            System.out.println("Ignoring unsupported output `" + oo.getId() + "` while CSV generation.");
        // BioModel bm = null;
        // 
        // VCDataIdentifier vcId = new VCDataIdentifier() {
        // public User getOwner() {	return new User("nouser", null);		}
        // public KeyValue getDataKey() { return null; }
        // public String getID()  {	return "mydata";					}
        // };
        // ExportFormat format = ExportFormat.HDF5;
        // 
        // Object[] variables = {"ala", "bala" };
        // String[] variableNames = new String[variables.length];
        // VariableSpecs variableSpecs = new VariableSpecs(variableNames, ExportConstants.VARIABLE_MULTI);
        // 
        // double[] timePoints = {0.0, 0.1, 0.2};
        // TimeSpecs timeSpecs = new TimeSpecs(0, 100, timePoints, ExportConstants.TIME_RANGE);
        // 
        // int geoMode = ExportConstants.GEOMETRY_FULL;
        // SpatialSelection[] selections = new SpatialSelection[0];
        // int axis = 3;
        // int sliceNumber = 0;
        // GeometrySpecs geometrySpecs = new GeometrySpecs(selections, axis, sliceNumber, geoMode);
        // 
        // ExportConstants.DataType dataType = ExportConstants.DataType.PDE_VARIABLE_DATA;
        // boolean switchRowsColumns = false;
        // ExportSpecs.SimNameSimDataID[] simNameSimDataIDs = { null, null };
        // int[] exportMultipleParamScans = { };
        // csvRoiLayout csvLayout = null;
        // boolean isHDF5 = true;
        // FormatSpecificSpecs formatSpecificSpecs = new ASCIISpecs(format, dataType, switchRowsColumns, simNameSimDataIDs, exportMultipleParamScans, csvLayout, isHDF5);
        // 
        // String simulationName = null;
        // String contextName = null;
        // ExportSpecs exportSpecs = new ExportSpecs(vcId, format, variableSpecs, timeSpecs, geometrySpecs, formatSpecificSpecs, simulationName, contextName);
        // 
        // SimulationContext sc = bm.getSimulationContext(0);
        // 
        // OutputFunctionContext ofc = sc.getOutputFunctionContext();
        // 
        // ArrayList<AnnotatedFunction> outputFunctionsList = ofc.getOutputFunctionsList();
        // 
        // AnnotatedFunction[] af = outputFunctionsList.toArray(new AnnotatedFunction[0]);
        // 
        // OutputContext outputContext = new OutputContext(af);
        // 
        // 
        // ExportServiceImpl exportServiceImpl = new ExportServiceImpl();
        // ASCIIExporter ae = new ASCIIExporter(exportServiceImpl);
        // 
        // 
        // DataSetControllerImpl dsControllerImpl = new DataSetControllerImpl(null, new File("C:\\TEMP\\eee"), null);
        // 
        // 
        // 
        // DataServerImpl dataServerImpl = new DataServerImpl(dsControllerImpl, exportServiceImpl);
        // 
        // 
        // FileDataContainerManager fileDataContainerManager = new FileDataContainerManager();
        // 
        // JobRequest jobRequest = JobRequest.createExportJobRequest(vcId.getOwner());
        // 
        // ae.makeASCIIData(outputContext, jobRequest, vcId.getOwner(), dataServerImpl, exportSpecs, fileDataContainerManager);
        // 
        // ClientServerManager csm = null;
        // ClientExportController cec = new ClientExportController(csm);
        // if(csm != null && cec != null) {
        // try {
        // cec.startExport(outputContext, exportSpecs);
        // } catch (RemoteProxyException e) {
        // e.printStackTrace();
        // }
        // }
        } else {
            System.out.println("Generating report `" + oo.getId() + "`.");
            try {
                StringBuilder sb = new StringBuilder();
                // we go through each entry (dataset) in the list of datasets
                // for each dataset, we use the data reference to obtain the data generator
                // ve get the list of variables associated with the data reference
                // each variable has an id (which is the data reference above, the task and the sbml symbol urn
                // for each variable we recover the task, from the task we get the sbml model
                // we search the sbml model to find the vcell variable name associated with the urn
                List<DataSet> datasets = ((Report) oo).getListOfDataSets();
                for (DataSet dataset : datasets) {
                    DataGenerator datagen = sedml.getDataGeneratorWithId(dataset.getDataReference());
                    ArrayList<String> varIDs = new ArrayList<>();
                    assert datagen != null;
                    ArrayList<Variable> vars = new ArrayList<>(datagen.getListOfVariables());
                    int mxlen = 0;
                    boolean supportedDataset = true;
                    // get target values
                    HashMap values = new HashMap<Variable, double[]>();
                    for (Variable var : vars) {
                        AbstractTask task = sedml.getTaskWithId(var.getReference());
                        Model model = sedml.getModelWithId(task.getModelReference());
                        Simulation sim = sedml.getSimulation(task.getSimulationReference());
                        IXPathToVariableIDResolver variable2IDResolver = new SBMLSupport();
                        // must get variable ID from SBML model
                        String sbmlVarId = "";
                        if (var.getSymbol() != null) {
                            // it is a predefined symbol
                            sbmlVarId = var.getSymbol().name();
                            // TIME is t, etc.
                            if ("TIME".equals(sbmlVarId)) {
                                // this is VCell reserved symbold for time
                                sbmlVarId = "t";
                            }
                        // TODO
                        // check spec for other symbols
                        } else {
                            // it is an XPATH target in model
                            String target = var.getTarget();
                            sbmlVarId = variable2IDResolver.getIdFromXPathIdentifer(target);
                        }
                        if (task instanceof RepeatedTask) {
                            supportedDataset = false;
                        } else {
                            varIDs.add(var.getId());
                            assert task != null;
                            if (sim instanceof UniformTimeCourse) {
                                // we want to keep the last outputNumberOfPoints only
                                int outputNumberOfPoints = ((UniformTimeCourse) sim).getNumberOfPoints();
                                double outputStartTime = ((UniformTimeCourse) sim).getOutputStartTime();
                                if (outputStartTime > 0) {
                                    ODESolverResultSet results = resultsHash.get(task.getId());
                                    int column = results.findColumn(sbmlVarId);
                                    double[] tmpData = results.extractColumn(column);
                                    double[] data = new double[outputNumberOfPoints + 1];
                                    for (int i = tmpData.length - outputNumberOfPoints - 1, j = 0; i < tmpData.length; i++, j++) {
                                        data[j] = tmpData[i];
                                    }
                                    mxlen = Integer.max(mxlen, data.length);
                                    values.put(var, data);
                                } else {
                                    ODESolverResultSet results = resultsHash.get(task.getId());
                                    int column = results.findColumn(sbmlVarId);
                                    double[] data = results.extractColumn(column);
                                    mxlen = Integer.max(mxlen, data.length);
                                    values.put(var, data);
                                }
                            } else {
                                System.err.println("only uniform time course simulations are supported");
                            }
                        }
                    }
                    updateDatasetStatusYml(sedmlLocation, oo.getId(), dataset.getId(), Status.SUCCEEDED, outDir);
                    if (!supportedDataset) {
                        System.err.println("Dataset " + dataset.getId() + " references unsupported RepeatedTask and is being skipped");
                        continue;
                    }
                    // get math
                    String mathMLStr = datagen.getMathAsString();
                    Expression expr = new Expression(mathMLStr);
                    SymbolTable st = new SimpleSymbolTable(varIDs.toArray(new String[vars.size()]));
                    expr.bindExpression(st);
                    // compute and write result, padding with NaN if unequal length or errors
                    double[] row = new double[vars.size()];
                    // Handling row labels that contains ","
                    if (dataset.getId().startsWith("__data_set__")) {
                        if (dataset.getLabel().contains(","))
                            sb.append("\"" + dataset.getLabel() + "\"").append(",");
                        else
                            sb.append(dataset.getLabel()).append(",");
                    } else {
                        if (dataset.getId().contains(","))
                            sb.append("\"" + dataset.getId() + "\"").append(",");
                        else
                            sb.append(dataset.getId()).append(",");
                    }
                    if (dataset.getLabel().contains(","))
                        sb.append("\"" + dataset.getLabel() + "\"").append(",");
                    else
                        sb.append(dataset.getLabel()).append(",");
                    DataGenerator dg = sedml.getDataGeneratorWithId(dataset.getDataReference());
                    if (dg != null && dg.getName() != null && !dg.getName().isEmpty()) {
                        // name may contain spaces or other things
                        sb.append("\"" + dg.getName() + "\"").append(",");
                    } else {
                        // dg may be null, name may be null
                        sb.append("").append(",");
                    }
                    for (int i = 0; i < mxlen; i++) {
                        for (int j = 0; j < vars.size(); j++) {
                            double[] varVals = ((double[]) values.get(vars.get(j)));
                            if (i < varVals.length) {
                                row[j] = varVals[i];
                            } else {
                                row[j] = Double.NaN;
                            }
                        }
                        double computed = Double.NaN;
                        try {
                            computed = expr.evaluateVector(row);
                        } catch (Exception e) {
                        // do nothing, we leave NaN and don't warn/log since it could flood
                        }
                        sb.append(computed).append(",");
                    }
                    sb.deleteCharAt(sb.lastIndexOf(","));
                    sb.append("\n");
                }
                File f = new File(outDirForCurrentSedml, oo.getId() + ".csv");
                PrintWriter out = new PrintWriter(f);
                out.print(sb.toString());
                out.flush();
                out.close();
                reportsHash.put(oo.getId(), f);
            } catch (Exception e) {
                e.printStackTrace(System.err);
                reportsHash.put(oo.getId(), null);
            }
        }
    }
    return reportsHash;
}
Also used : IXPathToVariableIDResolver(org.jlibsedml.execution.IXPathToVariableIDResolver) ExportOutput(cbit.vcell.export.server.ExportOutput) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) SymbolTable(cbit.vcell.parser.SymbolTable) SimpleSymbolTable(cbit.vcell.parser.SimpleSymbolTable) RemoteProxyException(cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException) ExpressionException(cbit.vcell.parser.ExpressionException) DataAccessException(org.vcell.util.DataAccessException) SBMLSupport(org.jlibsedml.modelsupport.SBMLSupport) SimpleSymbolTable(cbit.vcell.parser.SimpleSymbolTable) Expression(cbit.vcell.parser.Expression) BioModel(cbit.vcell.biomodel.BioModel)

Aggregations

BioModel (cbit.vcell.biomodel.BioModel)1 ExportOutput (cbit.vcell.export.server.ExportOutput)1 RemoteProxyException (cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException)1 Expression (cbit.vcell.parser.Expression)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 SimpleSymbolTable (cbit.vcell.parser.SimpleSymbolTable)1 SymbolTable (cbit.vcell.parser.SymbolTable)1 ODESolverResultSet (cbit.vcell.solver.ode.ODESolverResultSet)1 IXPathToVariableIDResolver (org.jlibsedml.execution.IXPathToVariableIDResolver)1 SBMLSupport (org.jlibsedml.modelsupport.SBMLSupport)1 DataAccessException (org.vcell.util.DataAccessException)1