use of org.jlibsedml.AbstractTask in project vcell by virtualcell.
the class XmlHelper method sedmlToBioModel.
public static VCDocument sedmlToBioModel(VCLogger transLogger, ExternalDocInfo externalDocInfo, SedML sedml, AbstractTask selectedTask) throws Exception {
if (sedml.getModels().isEmpty()) {
return null;
}
VCDocument doc = null;
try {
// extract the path only from the sedml file
String fullPath = FileUtils.getFullPath(externalDocInfo.getFile().getAbsolutePath());
// Namespace namespace = sedml.getNamespace();
// iterate through all the elements and show them at the console
List<org.jlibsedml.Model> mmm = sedml.getModels();
for (Model mm : mmm) {
System.out.println(mm.toString());
}
List<org.jlibsedml.Simulation> sss = sedml.getSimulations();
for (org.jlibsedml.Simulation ss : sss) {
System.out.println(ss.toString());
}
List<AbstractTask> ttt = sedml.getTasks();
for (AbstractTask tt : ttt) {
System.out.println(tt.toString());
}
List<DataGenerator> ddd = sedml.getDataGenerators();
for (DataGenerator dd : ddd) {
System.out.println(dd.toString());
}
List<Output> ooo = sedml.getOutputs();
for (Output oo : ooo) {
System.out.println(oo.toString());
}
KisaoTerm sedmlKisao = null;
// this will become the vCell simulation
org.jlibsedml.Simulation sedmlSimulation = null;
// the "original" model referred to by the task
org.jlibsedml.Model sedmlOriginalModel = null;
String sedmlOriginalModelName = null;
if (selectedTask == null) {
// no task, just pick the Model and find its sbml file
sedmlOriginalModelName = SEDMLUtil.getName(mmm.get(0));
} else {
if (selectedTask instanceof Task) {
sedmlOriginalModel = sedml.getModelWithId(selectedTask.getModelReference());
sedmlSimulation = sedml.getSimulation(selectedTask.getSimulationReference());
} else if (selectedTask instanceof RepeatedTask) {
RepeatedTask rt = (RepeatedTask) selectedTask;
assert (rt.getSubTasks().size() == 1);
// first (and only) subtask
SubTask st = rt.getSubTasks().entrySet().iterator().next().getValue();
String taskId = st.getTaskId();
AbstractTask t = sedml.getTaskWithId(taskId);
// get model and simulation from subtask
sedmlOriginalModel = sedml.getModelWithId(t.getModelReference());
sedmlSimulation = sedml.getSimulation(t.getSimulationReference());
} else {
throw new RuntimeException("Unexpected task " + selectedTask);
}
sedmlOriginalModelName = sedmlOriginalModel.getId();
sedmlKisao = KisaoOntology.getInstance().getTermById(sedmlSimulation.getAlgorithm().getKisaoID());
}
// UniformTimeCourse [initialTime=0.0, numberOfPoints=1000, outputEndTime=1.0, outputStartTime=0.0,
// Algorithm [kisaoID=KISAO:0000019], getId()=SimSlow]
// identify the vCell solvers that would match best the sedml solver kisao id
List<SolverDescription> solverDescriptions = new ArrayList<>();
for (SolverDescription sd : SolverDescription.values()) {
KisaoTerm solverKisaoTerm = KisaoOntology.getInstance().getTermById(sd.getKisao());
if (solverKisaoTerm == null) {
break;
}
boolean isExactlySame = solverKisaoTerm.equals(sedmlKisao);
if (isExactlySame && !solverKisaoTerm.isObsolete()) {
// we make a list with all the solvers that match the kisao
solverDescriptions.add(sd);
}
}
// from the list of vcell solvers that match the sedml kisao we select the ones that have a matching time step
SolverDescription solverDescription = null;
for (SolverDescription sd : solverDescriptions) {
if (true) {
solverDescription = sd;
break;
}
}
// find out everything else we need about the application we're going to use,
// some of the info will be needed when we parse the sbml file
boolean bSpatial = false;
Application appType = Application.NETWORK_DETERMINISTIC;
Set<SolverDescription.SolverFeature> sfList = solverDescription.getSupportedFeatures();
for (SolverDescription.SolverFeature sf : sfList) {
switch(sf) {
case Feature_Rulebased:
appType = Application.RULE_BASED_STOCHASTIC;
break;
case Feature_Stochastic:
appType = Application.NETWORK_STOCHASTIC;
break;
case Feature_Deterministic:
appType = Application.NETWORK_DETERMINISTIC;
break;
case Feature_Spatial:
bSpatial = true;
break;
default:
break;
}
}
// -------------------------------------------------------------------------------------------
// extract bioModel name from sedx (or sedml) file
String bioModelName = FileUtils.getBaseName(externalDocInfo.getFile().getAbsolutePath());
// if we have repeated task, we ignore them, we just use the normal resolvers for archive and changes
// once the application and simulation are built, we iterate through the repeated tasks and
// add math overrides to the simulation for each repeated task
ArchiveComponents ac = null;
if (externalDocInfo.getFile().getPath().toLowerCase().endsWith("sedx") || externalDocInfo.getFile().getPath().toLowerCase().endsWith("omex")) {
ac = Libsedml.readSEDMLArchive(new FileInputStream(externalDocInfo.getFile().getPath()));
}
ModelResolver resolver = new ModelResolver(sedml);
if (ac != null) {
resolver.add(new ArchiveModelResolver(ac));
}
resolver.add(new FileModelResolver());
resolver.add(new RelativeFileModelResolver(fullPath));
String newMdl = resolver.getModelString(sedmlOriginalModel);
// sbmlSource with all the changes applied
XMLSource sbmlSource = new XMLSource(newMdl);
doc = XmlHelper.importSBML(transLogger, sbmlSource, bSpatial);
BioModel bioModel = (BioModel) doc;
bioModel.setName(bioModelName);
// we already have an application loaded from the sbml file, with initial conditions and stuff
// which may be not be suitable because the sedml kisao may need a different app type
// so we do a "copy as" to the right type and then delete the original we loaded from the sbml file
// the new application we're making from the old one
SimulationContext newSimulationContext = null;
if (bioModel.getSimulationContexts().length == 1) {
SimulationContext oldSimulationContext = bioModel.getSimulationContext(0);
newSimulationContext = SimulationContext.copySimulationContext(oldSimulationContext, sedmlOriginalModelName, bSpatial, appType);
bioModel.removeSimulationContext(oldSimulationContext);
bioModel.addSimulationContext(newSimulationContext);
} else {
// length == 0
newSimulationContext = bioModel.addNewSimulationContext(sedmlOriginalModelName, appType);
}
// making the new vCell simulation based on the sedml simulation
newSimulationContext.refreshDependencies();
MathMappingCallback callback = new MathMappingCallbackTaskAdapter(null);
newSimulationContext.refreshMathDescription(callback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
Simulation newSimulation = new Simulation(newSimulationContext.getMathDescription());
newSimulation.setName(SEDMLUtil.getName(sedmlSimulation));
// TODO: make sure that everything has proper names
// we check the repeated tasks, if any, and add to the list of math overrides
// if(selectedTask instanceof RepeatedTask) {
// for(Change change : ((RepeatedTask) selectedTask).getChanges()) {
// if(!(change instanceof SetValue)) {
// throw new RuntimeException("Only 'SetValue' changes are supported for repeated tasks.");
// }
// SetValue setValue = (SetValue)change;
// // TODO: extract target from XPath
// // ......
// //
// String target = "s0"; // for now we just use a hardcoded thing
// ConstantArraySpec cas;
// Range range = ((RepeatedTask) selectedTask).getRange(setValue.getRangeReference());
// if(range instanceof UniformRange) {
// cas = ConstantArraySpec.createIntervalSpec(target, ((UniformRange) range).getStart(), ((UniformRange) range).getEnd(),
// range.getNumElements(), ((UniformRange) range).getType() == UniformRange.UniformType.LOG ? true : false);
// } else if(range instanceof VectorRange) {
// // List<String> constants = new ArrayList<> ();
// // for(int i=0; i<range.getNumElements(); i++) {
// // constants.add(new Constant(i+"", new Expression(range.getElementAt(i))));
// // }
// // cas = ConstantArraySpec.createListSpec(target, constants);
//
// } else {
// throw new RuntimeException("Only 'Uniform Range' and 'Vector Range' are supported at this time.");
// }
//
// }
// }
// we identify the type of sedml simulation (uniform time course, etc)
// and set the vCell simulation parameters accordingly
SolverTaskDescription simTaskDesc = newSimulation.getSolverTaskDescription();
TimeBounds timeBounds = new TimeBounds();
TimeStep timeStep = new TimeStep();
double outputTimeStep = 0.1;
if (sedmlSimulation instanceof UniformTimeCourse) {
// we translate initial time to zero, we provide output for the duration of the simulation
// because we can't select just an interval the way the SEDML simulation can
double initialTime = ((UniformTimeCourse) sedmlSimulation).getInitialTime();
double outputStartTime = ((UniformTimeCourse) sedmlSimulation).getOutputStartTime();
double outputEndTime = ((UniformTimeCourse) sedmlSimulation).getOutputEndTime();
double outputNumberOfPoints = ((UniformTimeCourse) sedmlSimulation).getNumberOfPoints();
outputTimeStep = (outputEndTime - outputStartTime) / outputNumberOfPoints;
timeBounds = new TimeBounds(0, outputEndTime - initialTime);
} else if (sedmlSimulation instanceof OneStep) {
// for anything other than UniformTimeCourse we just ignore
} else if (sedmlSimulation instanceof SteadyState) {
} else {
}
OutputTimeSpec outputTimeSpec = new UniformOutputTimeSpec(outputTimeStep);
simTaskDesc.setTimeBounds(timeBounds);
simTaskDesc.setTimeStep(timeStep);
simTaskDesc.setOutputTimeSpec(outputTimeSpec);
newSimulation.setSolverTaskDescription(simTaskDesc);
bioModel.addSimulation(newSimulation);
newSimulation.refreshDependencies();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Unable to initialize bioModel for the given selection.");
}
return doc;
}
use of org.jlibsedml.AbstractTask in project vcell by virtualcell.
the class VCellSedMLSolver method main.
// static String inString = "/usr/local/app/vcell/simulation";
// static String outRootString = "/usr/local/app/vcell/simulation/out";
public static void main(String[] args) {
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
// place the sedml file and the sbml file(s) in inDir directory
Options options = getCommandLineOptions();
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
if (cmd.getOptions().length == 0) {
System.out.println("usage: vcell [-h] [-q] -i ARCHIVE [-o OUT_DIR] [-v]");
System.exit(1);
}
if (args[0].contains("-h") || args[0].contains("--help")) {
formatter.printHelp("vcell [-h] [-q] -i ARCHIVE [-o OUT_DIR] [-v]", "\nBioSimulators-compliant command-line interface to the VCell simulation program <http://vcell.org>.\n\n" + "optional arguments:\n\n", options, "");
System.exit(1);
}
IN_ROOT_STRING = cmd.getOptionValue("archive");
OUT_ROOT_STRING = cmd.getOptionValue("out-dir");
} catch (Exception ex) {
System.out.println(ex.getMessage());
System.exit(1);
}
if (IN_ROOT_STRING == null || OUT_ROOT_STRING == null) {
formatter.printHelp("vcell", options);
System.exit(1);
}
// CombineArchive omex = new CombineArchive();
// boolean isInitialized = omex.initializeFromArchive(IN_ROOT_STRING);
// boolean isExtracted = omex.extractTo(tempDir)
File tempDir = Files.createTempDir();
ArrayList<String> sedmlLocations = new ArrayList<>();
try {
System.loadLibrary("combinej");
CombineArchive omex = new CombineArchive();
boolean isInitialized = omex.initializeFromArchive(IN_ROOT_STRING);
boolean isExtracted = omex.extractTo(tempDir.getAbsolutePath());
CaOmexManifest manifest = omex.getManifest();
CaListOfContents contents = manifest.getListOfContents();
System.out.println("Contents fetched");
for (int contentIndex = 0; contentIndex < contents.getNumContents(); contentIndex++) {
CaContent content = (CaContent) contents.get(contentIndex);
if (content.isFormat("sedml")) {
sedmlLocations.add(content.getLocation());
}
}
System.out.println("All SEDML locations fetched");
// unzip(IN_ROOT_STRING, tempDir);
} catch (Exception ex) {
System.err.println("Cannot extract Omex");
System.exit(1);
}
File inDir = tempDir;
File outRootDir = new File(OUT_ROOT_STRING);
// delete the output directory and all its content recursively
if (outRootDir.exists()) {
try {
deleteRecursively(outRootDir);
} catch (IOException e) {
System.err.println("Failed to empty outRootDir.");
System.exit(99);
}
}
if (!outRootDir.exists()) {
outRootDir.mkdirs();
}
// }
for (int sedmlIndex = 0; sedmlIndex < sedmlLocations.size(); sedmlIndex++) {
try {
String completeSedmlPath = tempDir.getAbsolutePath() + "/" + sedmlLocations.get(sedmlIndex);
File sedmlFile = new File(completeSedmlPath);
SedML sedml = Libsedml.readDocument(sedmlFile).getSedMLModel();
if (sedml == null || sedml.getModels().isEmpty()) {
System.err.println("the sedml file '" + sedmlFile.getName() + "'does not contain a valid document");
System.exit(99);
}
VCellSedMLSolver vCellSedMLSolver = new VCellSedMLSolver();
ExternalDocInfo externalDocInfo = new ExternalDocInfo(sedmlFile, true);
for (AbstractTask at : sedml.getTasks()) {
vCellSedMLSolver.doWork(externalDocInfo, at, sedml);
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
// try {
// SedML sedml = Libsedml.readDocument(sedmlFile).getSedMLModel();
// if (sedml == null || sedml.getModels().isEmpty()) {
// System.err.println("the sedml file '" + sedmlFile.getName() + "'does not contain a valid document");
// System.exit(99);
// }
// VCellSedMLSolver vCellSedMLSolver = new VCellSedMLSolver();
// ExternalDocInfo externalDocInfo = new ExternalDocInfo(sedmlFile, true);
// for(AbstractTask at : sedml.getTasks()) {
// vCellSedMLSolver.doWork(externalDocInfo, at, sedml);
// }
// } catch (Exception e) {
// System.err.println(e.getMessage());
// } finally {
deleteDirectory(tempDir);
// }
}
use of org.jlibsedml.AbstractTask in project vcell by virtualcell.
the class VCellSedMLSolver method doWork.
// everything is done here
public void doWork(ExternalDocInfo externalDocInfo, AbstractTask sedmlTask, SedML sedml) throws Exception {
// create the VCDocument (bioModel + application + simulation), do sanity checks
cbit.util.xml.VCLogger sedmlImportLogger = new LocalLogger();
List<AbstractTask> tasks = new ArrayList<AbstractTask>();
tasks.add(sedmlTask);
List<VCDocument> docs = XmlHelper.sedmlToBioModel(sedmlImportLogger, externalDocInfo, sedml, tasks, null, false);
VCDocument doc = docs.get(0);
sanityCheck(doc);
// create the work directory for this task, invoke the solver
String docName = doc.getName();
String outString = VCellSedMLSolver.OUT_ROOT_STRING + "/" + docName + "/" + sedmlTask.getId();
File outDir = new File(outString);
if (!outDir.exists()) {
outDir.mkdirs();
}
BioModel bioModel = (BioModel) doc;
SimulationContext simContext = bioModel.getSimulationContext(0);
MathDescription mathDesc = simContext.getMathDescription();
String vcml = mathDesc.getVCML();
try (PrintWriter pw = new PrintWriter(outString + "/vcmlTrace.xml")) {
pw.println(vcml);
}
Simulation sim = bioModel.getSimulation(0);
SolverTaskDescription std = sim.getSolverTaskDescription();
SolverDescription sd = std.getSolverDescription();
String kisao = sd.getKisao();
if (SolverDescription.CVODE.getKisao().contentEquals(kisao)) {
ODESolverResultSet odeSolverResultSet = solveCvode(outDir, bioModel);
System.out.println("Finished: " + docName + ": - task '" + sedmlTask.getId() + "'.");
} else if (SolverDescription.StochGibson.getKisao().contentEquals(kisao)) {
ODESolverResultSet odeSolverResultSet = solveGibson(outDir, bioModel);
System.out.println("Finished: " + docName + ": - task '" + sedmlTask.getId() + "'.");
} else if (SolverDescription.IDA.getKisao().contentEquals(kisao)) {
ODESolverResultSet odeSolverResultSet = solveIDA(outDir, bioModel);
System.out.println("Finished: " + docName + ": - task '" + sedmlTask.getId() + "'.");
} else {
System.out.println("Unsupported solver: " + kisao);
}
System.out.println("-------------------------------------------------------------------------");
}
use of org.jlibsedml.AbstractTask in project vcell by virtualcell.
the class SedMLResultsProcesser2 method process.
/**
* Processes a set of simulation results and processes them according to the
* output passed into the constructor. There are several reasons that
* processing may fail, for example:
* <ul>
* <li>If cross-references between outputs, variables and data generators
* are not valid.
* <li>If mappings between the data column headers in the raw result, and
* model element identifiers cannot be resolved.
* <li>If the MathML for the data post-processing is invalid or outside the
* scope of SED-ML's MathML usage.
* </ul>
* Failures such as these will be logged and can be accessed by a call to
* getProcessReport() after calling this method.
*
* @param results
* @throws IllegalArgumentException
* if results is <code>null</code>.
*/
public void process(Map<AbstractTask, IRawSedmlSimulationResults> results) {
if (results == null) {
throw new IllegalArgumentException();
}
Map<AbstractTask, double[][]> rawTask2Results = new HashMap<AbstractTask, double[][]>();
int numRows = 0;
numRows = makeDefensiveCopyOfData(results, rawTask2Results, numRows);
List<double[]> processed = new ArrayList<double[]>();
if (output.getAllDataGeneratorReferences().isEmpty()) {
report.messages.add(new ExecutionStatusElement(null, NO_DG_INOUTPUT_MSG, ExecutionStatusType.ERROR));
return;
}
for (String dgId : output.getAllDataGeneratorReferences()) {
double[] mutated = new double[numRows];
processed.add(mutated);
DataGenerator dg = sedml.getDataGeneratorWithId(dgId);
if (dg == null) {
report.messages.add(new ExecutionStatusElement(null, MISSING_DG_MESSAGE + dgId, ExecutionStatusType.ERROR));
return;
}
List<Variable> vars = dg.getListOfVariables();
List<Parameter> params = dg.getListOfParameters();
Map<String, String> Var2Model = new HashMap<String, String>();
Map<String, IRawSedmlSimulationResults> var2Result = new HashMap<String, IRawSedmlSimulationResults>();
Map<String, double[][]> var2Data = new HashMap<String, double[][]>();
String timeID = "";
// map varIds to result, based upon task reference
for (Variable variable : vars) {
String modelID;
if (variable.isVariable()) {
// get the task from which this result variable was
// generated.
modelID = variable2IDResolver.getIdFromXPathIdentifer(variable.getTarget());
String taskRef = variable.getReference();
AbstractTask t = sedml.getTaskWithId(taskRef);
// get results for this task
IRawSedmlSimulationResults res = results.get(t);
// set up lookups to results, raw data and model ID
var2Result.put(variable.getId(), res);
var2Data.put(variable.getId(), rawTask2Results.get(t));
Var2Model.put(variable.getId(), modelID);
// it's a symbol
} else if (variable.isSymbol() && variable.getSymbol().equals(VariableSymbol.TIME)) {
timeID = variable.getId();
var2Data.put(variable.getId(), rawTask2Results.values().iterator().next());
Var2Model.put(variable.getId(), variable.getId());
}
}
// get Parameter values
Map<String, Double> Param2Value = new HashMap<String, Double>();
for (Parameter p : params) {
Param2Value.put(p.getId(), p.getValue());
}
// now parse maths, and replace raw simulation results with
// processed results.
ASTNode node = dg.getMath();
Set<ASTCi> identifiers = node.getIdentifiers();
for (ASTCi var : identifiers) {
if (var.isVector()) {
String varName = var.getName();
IModel2DataMappings coll = var2Result.get(varName).getMappings();
int otherVarInx = coll.getColumnIndexFor(Var2Model.get(varName));
if (otherVarInx < 0 || otherVarInx >= var2Result.get(varName).getNumColumns()) {
report.messages.add(new ExecutionStatusElement(null, NO_DATACOLuMN_FORID_MSG + var, ExecutionStatusType.ERROR));
return;
}
EvaluationContext con = new EvaluationContext();
Double[] data = var2Result.get(varName).getDataByColumnIndex(otherVarInx);
con.setValueFor(varName, Arrays.asList(data));
if (var.getParentNode() == null || var.getParentNode().getParentNode() == null) {
report.messages.add(new ExecutionStatusElement(null, "Could not evaluate [" + var + "] as symbol does not have parent element", ExecutionStatusType.ERROR));
return;
}
if (!var.getParentNode().canEvaluate(con)) {
report.messages.add(new ExecutionStatusElement(null, "Could not evaluate [" + var + "] ", ExecutionStatusType.ERROR));
return;
}
ASTNumber num = var.getParentNode().evaluate(con);
// replace vector operation with calculated value.
var.getParentNode().getParentNode().replaceChild(var.getParentNode(), num);
}
}
// identifiers.add(var.getSpId());
if (identifiersMapToData(identifiers, Var2Model, Param2Value, var2Result, timeID)) {
for (int i = 0; i < numRows; i++) {
EvaluationContext con = new EvaluationContext();
for (String id : Param2Value.keySet()) {
con.setValueFor(id, Param2Value.get(id));
}
for (ASTCi var : identifiers) {
// we've already resolved parameters
if (Param2Value.get(var.getName()) != null) {
continue;
}
int otherVarInx = 0;
if (!var.getName().equals(timeID)) {
IModel2DataMappings coll = var2Result.get(var.getName()).getMappings();
otherVarInx = coll.getColumnIndexFor(Var2Model.get(var.getName()));
if (otherVarInx < 0 || otherVarInx >= var2Result.get(var.getName()).getNumColumns()) {
report.messages.add(new ExecutionStatusElement(null, NO_DATACOLuMN_FORID_MSG + var, ExecutionStatusType.ERROR));
return;
}
}
con.setValueFor(var.getName(), var2Data.get(var.getName())[i][otherVarInx]);
}
if (node.canEvaluate(con)) {
mutated[i] = node.evaluate(con).getValue();
} else {
report.messages.add(new ExecutionStatusElement(null, COULD_NOT_EXECUTE_MATHML_FOR + dgId, ExecutionStatusType.INFO));
}
}
} else {
report.messages.add(new ExecutionStatusElement(null, COULD_NOT_RESOLVE_MATHML_MSG + dgId, ExecutionStatusType.ERROR));
return;
}
}
toReturn = createData(processed, numRows);
}
use of org.jlibsedml.AbstractTask in project vcell by virtualcell.
the class SedMLResultsProcesser2 method makeDefensiveCopyOfData.
// makes copy of result data and returns num rows
private int makeDefensiveCopyOfData(Map<AbstractTask, IRawSedmlSimulationResults> results, Map<AbstractTask, double[][]> rawTask2Results, int numRows) {
// makes a defensive copy of all input data
for (AbstractTask t : results.keySet()) {
IRawSedmlSimulationResults result = results.get(t);
numRows = result.getNumDataRows();
// for look-up of
double[][] toCopy = result.getData();
double[][] original = new double[toCopy.length][];
int in = 0;
for (double[] row : toCopy) {
double[] copyRow = new double[row.length];
System.arraycopy(row, 0, copyRow, 0, row.length);
original[in++] = copyRow;
}
rawTask2Results.put(t, original);
}
return numRows;
}
Aggregations