Skip to content

Commit

Permalink
Fixed #60 partially; 3 tests still not working since jlibsedml doesn'…
Browse files Browse the repository at this point in the history
…t support functional range
  • Loading branch information
shalinshah1993 committed Jul 25, 2018
1 parent 3a15642 commit b64b096
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 52 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/simulator/sedml/ProcessSedMLResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ private IRawSedmlSimulationResults flattenResultsList(List<IRawSedmlSimulationRe
mergeTimeCols(a, b),
mergeDataCols(a.getData(), b.getData()),
results.get(0).getColumnHeaders()))).get();
return flat;
return flat;
}

/**
Expand Down Expand Up @@ -398,7 +398,9 @@ private double[][] extractNonTimeData(double[][] data) {
private double[] mergeTimeCols(IRawSedmlSimulationResults a, IRawSedmlSimulationResults b) {
// Get end time point for taskA
double[] timeA = ((MultTableSEDMLWrapper)a).getMultiTable().getTimePoints();
double timeBegin = timeA[timeA.length - 1];
// Following tellurim we concat all the iterations of repeated tasks
// so start time from 0
double timeBegin = 0d;

// Add end time point to taskB
double[] timeB = Arrays.stream(((MultTableSEDMLWrapper)b).getMultiTable().getTimePoints()).map(row -> row + timeBegin).toArray();
Expand Down
172 changes: 126 additions & 46 deletions src/main/java/org/simulator/sedml/SedMLSBMLSimulatorExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.jlibsedml.AbstractTask;
Expand All @@ -55,7 +56,6 @@
import org.jlibsedml.execution.IProcessedSedMLSimulationResults;
import org.jlibsedml.execution.IRawSedmlSimulationResults;
import org.jlibsedml.execution.ModelResolver;

import org.jlibsedml.modelsupport.BioModelsModelsRetriever;
import org.jlibsedml.modelsupport.KisaoOntology;
import org.jlibsedml.modelsupport.KisaoTerm;
Expand All @@ -70,7 +70,6 @@
import org.simulator.math.odes.MultiTable;
import org.simulator.math.odes.RosenbrockSolver;
import org.simulator.sbml.SBMLinterpreter;

import de.binfalse.bflog.LOGGER;
import net.biomodels.kisao.IKiSAOQueryMaker;
import net.biomodels.kisao.impl.KiSAOQueryMaker;
Expand Down Expand Up @@ -251,7 +250,7 @@ protected IRawSedmlSimulationResults executeSimulation(String modelStr, UniformT
}
return null;
}

protected IRawSedmlSimulationResults executeSimulation(String modelStr, OneStep sim) {

AbstractDESSolver solver = getSolverForKisaoID(sim.getAlgorithm().getKisaoID());
Expand Down Expand Up @@ -314,12 +313,76 @@ protected IRawSedmlSimulationResults executeSimulation(String modelStr, SteadySt
}
return null;
}

protected IRawSedmlSimulationResults executeSimulation(String modelStr,
Simulation sim, Map<String, double[]> mapChangesToList, int element) {

AbstractDESSolver solver = getSolverForKisaoID(sim.getAlgorithm().getKisaoID());
File tmp = null;
try {
// get a JSBML object from the model string.
tmp = File.createTempFile("Sim", "sbml");
FileUtils.writeStringToFile(tmp, modelStr, "UTF-8");
Model model = (new SBMLReader()).readSBML(tmp).getModel();

// If there are any changes make them before execution
if (mapChangesToList != null) {
// Find all the setValues and set them to current element in range
for(String change: mapChangesToList.keySet()) {
model.getParameter(change).setValue(mapChangesToList.get(change)[element]);;
}
}

// now run simulation
SBMLinterpreter interpreter = null;
if (amountHash != null) {
interpreter = new SBMLinterpreter(model, 0, 0, 1, amountHash);
} else {
interpreter = new SBMLinterpreter(model);
}
solver.setIncludeIntermediates(false);

if(sim instanceof OneStep) {
OneStep finalSim = (OneStep) sim;
// A step-size randomly taken since SED-ML L1V2 says simulator decides this
// A better way to decide step size is essential
solver.setStepSize(finalSim.getStep() / ONE_STEP_SIM_STEPS);
MultiTable mts = solver.solve(interpreter, interpreter.getInitialValues(), 0.0, finalSim.getStep());

// adapt the MultiTable to jlibsedml interface.
// return only 1 point for OneStep simulation: start and end
return new MultTableSEDMLWrapper(mts.filter(new double[] { finalSim.getStep() }));
}
else if(sim instanceof UniformTimeCourse) {
UniformTimeCourse finalSim = (UniformTimeCourse) sim;
solver.setStepSize((finalSim.getOutputEndTime() - finalSim.getOutputStartTime()) / (finalSim.getNumberOfPoints()));
MultiTable mts = solver.solve(interpreter, interpreter.getInitialValues(), finalSim.getOutputStartTime(),
finalSim.getOutputEndTime());

// adapt the MultiTable to jlibsedml interface.
return new MultTableSEDMLWrapper(mts);
}
else if(sim instanceof SteadyState) {
// set default stepSize and call solver. Solver will automatically find
// steadyState and terminate when steadyState is reached.
MultiTable mts = solver.steadystate(interpreter, interpreter.getInitialValues(), STEADY_STATE_STEPS);

// adapt the MultiTable to jlibsedml interface.
return new MultTableSEDMLWrapper(mts);
}


} catch (Exception e) {
LOGGER.warn(e.getMessage());
}
return null;
}

/**
* This method is a wrapper to the runSimulations method from
* {@link AbstractSedmlExecutor} to add additional support for repeatedTasks. It
* identifies the type of task, before running the simulations.
* @throws OWLOntologyCreationException
* @throws OWLOntologyCreationException
*/
public Map<AbstractTask, List<IRawSedmlSimulationResults>> run() throws OWLOntologyCreationException {

Expand All @@ -342,50 +405,60 @@ public Map<AbstractTask, List<IRawSedmlSimulationResults>> run() throws OWLOntol
Map<String, Range> range = repTask.getRanges();
List<IRawSedmlSimulationResults> repTaskResults = new ArrayList<IRawSedmlSimulationResults>();

// Store state of all the existing changes by subTasks
List<SetValue> modelState = new ArrayList<SetValue>();

// Find all the variable from listOfChanges and create tasks
if (range.size() > 0 && subTasks.size() > 0) {

// Load original model from one of the subtasks and update its state
org.jlibsedml.Model curModel = sedml.getModelWithId(sedml.getTaskWithId(repTask.getSubTasks().
values().iterator().next().getTaskId()).getModelReference());

// 2. Get the Xpath parameter in setValue and a double[] before simulations
Map<String, double[]> mapChangesToList = new HashMap<String, double[]>();
if (repTask.getChanges().size() > 0) {

for (SetValue change : repTask.getChanges()) {
String xPath = change.getTargetXPath().getTargetAsString();
xPath = xPath.substring(xPath.indexOf("'") + 1);
xPath = xPath.substring(0, xPath.indexOf("'"));

if(change.getRangeReference() != null) {
double[] setValueRange = getRangeListRange(range.get(change.getRangeReference()));
mapChangesToList.put(xPath, setValueRange);
}else {
LOGGER.warn("No range specified for SetValue element" + change.getId() + " of repeated task " + repTask.getId());
return null;
}
}
}

// Iterate over master range
Range masterRange = range.get(repTask.getRange());
for (int element = 0; element < masterRange.getNumElements(); element++) {

List<MultTableSEDMLWrapper> stResults = new ArrayList<MultTableSEDMLWrapper>();

// 1. Check for resetModel, original SBML model file is re-read
if (repTask.getResetModel()) {
curModel = sedml.getModelWithId(sedml.getTaskWithId(repTask.getSubTasks().
values().iterator().next().getTaskId()).getModelReference());
}

// 3. Execute subTasks in sorted order with the current state of model
for (Entry<String, SubTask> st : subTasks.entrySet()) {
SubTask subTask = st.getValue();
AbstractTask relatedTask = sedml.getTaskWithId(subTask.getTaskId());

// subtasks refer to same model but can refer to different simulations
Simulation sim = sedml.getSimulation(relatedTask.getSimulationReference());
String changedModel = modelResolver.getModelString(curModel);

// A subTask can also be a repeatedTask in which case
// recurse all repeatedTasks subTasks to add all of them
if (relatedTask instanceof RepeatedTask) {
// TODO: Handle nested repeatedTask
LOGGER.warn("Warning! Nested repeatedTask found and ignored.");

} else {
// Load original model and update its state
org.jlibsedml.Model curModel = sedml.getModelWithId(relatedTask.getModelReference());

// 1. Check for resetModel, if so clear all the SetValues
if (repTask.getResetModel()) {
modelState.clear();
}

// 2. (optional) Check if any new changes are added
modelState.addAll(repTask.getChanges());

// Set model to previously stored state by applying all the current
// changes. If resetModel=true then this will be empty and we get fresh
// model with no modification
if (modelState.size() > 0) {
for (SetValue change : modelState) {
curModel.addChange(change);
}
}

// 3. Execute subTasks in sorted order with the current state of model
Simulation sim = sedml.getSimulation(relatedTask.getSimulationReference());
String changedModel = modelResolver.getModelString(curModel);

IRawSedmlSimulationResults output = null;
// Quickly run error checks before final execution
if (!supportsLanguage(curModel.getLanguage())) {
Expand All @@ -404,13 +477,11 @@ public Map<AbstractTask, List<IRawSedmlSimulationResults>> run() throws OWLOntol
if(!canExecuteSimulation(sim))
sim = tryAlternateAlgo(sim);

if (sim instanceof OneStep) {
output = executeSimulation(changedModel, (OneStep) sim);
} else if (sim instanceof SteadyState) {
output = executeSimulation(changedModel, (SteadyState) sim);
} else if (sim instanceof UniformTimeCourse) {
output = executeSimulation(changedModel, (UniformTimeCourse) sim);
}
// Execute simulations with changes, simulation type
if(mapChangesToList.size() > 0)
output = executeSimulation(changedModel, sim, mapChangesToList, element);
else
output = executeSimulation(changedModel, sim, null, -1);

if (output == null) {
LOGGER.warn("Simulation failed during execution: "
Expand All @@ -428,7 +499,7 @@ public Map<AbstractTask, List<IRawSedmlSimulationResults>> run() throws OWLOntol
.reduce((a, b) -> new MultTableSEDMLWrapper(new MultiTable(mergeTimeCols(a, b),
mergeDataCols(a.getData(), b.getData()), stResults.get(0).getColumnHeaders())))
.get();

// Add big subTask result to list of repTask results
repTaskResults.add(reducedStResults);
}
Expand Down Expand Up @@ -463,13 +534,13 @@ public Map<AbstractTask, List<IRawSedmlSimulationResults>> run() throws OWLOntol
if(!canExecuteSimulation(sim))
sim = tryAlternateAlgo(sim);

// Identify simulation type and run it. Store the results in a Map
if (sim instanceof OneStep) {
// Identify simulation type and run it
if(sim instanceof OneStep) {
results = executeSimulation(changedModel, (OneStep) sim);
} else if (sim instanceof SteadyState) {
results = executeSimulation(changedModel, (SteadyState) sim);
} else if (sim instanceof UniformTimeCourse) {
}else if(sim instanceof UniformTimeCourse) {
results = executeSimulation(changedModel, (UniformTimeCourse) sim);
}else if(sim instanceof SteadyState) {
results = executeSimulation(changedModel, (SteadyState) sim);
}

if (results == null) {
Expand All @@ -479,10 +550,19 @@ public Map<AbstractTask, List<IRawSedmlSimulationResults>> run() throws OWLOntol
res.put(stdTask, new ArrayList<IRawSedmlSimulationResults>(Arrays.asList(results)));
}
}

return res;
}

private double[] getRangeListRange(Range range) {
// TODO Auto-generated method stub
double[] rangeList = new double[range.getNumElements()];
for(int index = 0; index < range.getNumElements(); index++) {
rangeList[index] = range.getElementAt(index);
}

return rangeList;
}

/**
* Merge two 2D arrays into one 2D array in X-direction
*
Expand Down Expand Up @@ -510,7 +590,7 @@ private double[] mergeTimeCols(MultTableSEDMLWrapper a, MultTableSEDMLWrapper b)
// Get end time point for taskA
double[] timeA = a.getMultiTable().getTimePoints();
double timeBegin = timeA[timeA.length - 1];

// Add end time point to taskB
double[] timeB = Arrays.stream(b.getMultiTable().getTimePoints()).map(row -> row + timeBegin).toArray();

Expand Down
12 changes: 8 additions & 4 deletions src/test/java/org/simulator/sedml/SEDMLExecutorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public final void testBasicSEDMLExecutorForMiriamURNDefinedModel() throws XMLExc


@Test
public final void testIkappab() throws XMLException, OWLOntologyCreationException, IOException{
public final void testIkappab() throws XMLException, OWLOntologyCreationException, IOException {
String resource = "/sedml/L1V2/ikappab/ikappab.xml";
testSpecificationExample(resource);
}
Expand All @@ -160,13 +160,15 @@ public final void testLorenzSBML() throws XMLException, OWLOntologyCreationExcep
}

@Test
@Ignore
public final void testOscliNestedPulse() throws XMLException, OWLOntologyCreationException, IOException {
String resource = "/sedml/L1V2/oscli-nested-pulse/oscli-nested-pulse.xml";
testSpecificationExample(resource);
}

@Test
@Ignore
@Ignore
//Contains nested repeated task https://github.com/shalinshah1993/SBSCL/issues/55
public final void testParameterScan2D() throws XMLException, OWLOntologyCreationException, IOException {
String resource = "/sedml/L1V2/parameter-scan-2d/parameter-scan-2d.xml";
testSpecificationExample(resource);
Expand All @@ -179,12 +181,14 @@ public final void testRepeatedScanOscli() throws XMLException, OWLOntologyCreati
}

@Test
@Ignore
public final void testRepeatedSteadyScanOscli() throws XMLException, OWLOntologyCreationException, IOException {
String resource = "/sedml/L1V2/repeated-steady-scan-oscli/repeated-steady-scan-oscli.xml";
testSpecificationExample(resource);
}

@Test
@Ignore
public final void testRepeatedStochasticRuns() throws XMLException, OWLOntologyCreationException, IOException {
String resource = "/sedml/L1V2/repeated-stochastic-runs/repeated-stochastic-runs.xml";
testSpecificationExample(resource);
Expand Down Expand Up @@ -302,8 +306,8 @@ public final void testRepressilator1() throws XMLException, OWLOntologyCreationE
// plot all processed results as per curve descriptions
String title = wanted.getId() + "(" + wanted.getName() + ")";
// UNCOMMENT THIS TO SAVE PLOT
//PlotProcessedSedmlResults p = new PlotProcessedSedmlResults(pr, curves, title);
//assertNotNull(p);
// PlotProcessedSedmlResults p = new PlotProcessedSedmlResults(pr, curves, title);
// assertNotNull(p);
// p.savePlot(resource, wanted.getId());
}
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b64b096

Please sign in to comment.