示例#1
0
/**
 * Creates the simulations for SEDML.
 */
std::string CSEDMLExporter::createScanTask(CCopasiDataModel& dataModel, const std::string & modelId)
{
  // need L1V2 to export repeated tasks
  if (mpSEDMLDocument->getVersion() != 2) return "";

  CScanTask* pTask =  dynamic_cast<CScanTask*>((*dataModel.getTaskList())["Scan"]);

  if (pTask == NULL) return "";

  CScanProblem* pProblem = dynamic_cast<CScanProblem*>(pTask->getProblem());
  size_t numItems = pProblem->getNumberOfScanItems();

  if (numItems == 0)
    return "";

  if (pProblem->getSubtask() != CCopasiTask::steadyState &&
      pProblem->getSubtask() != CCopasiTask::timeCourse)
    {
      CCopasiMessage(CCopasiMessage::WARNING, "This version of COPASI only allows the export of time course or steady state scans.");
      return "";
    }

  std::string subTaskId;

  if (pProblem->getSubtask() == CCopasiTask::steadyState)
    {
      subTaskId = createSteadyStateTask(dataModel, modelId);
    }
  else
    {
      subTaskId = mpTimecourseTask->getId();
    }

  SedRepeatedTask* task = mpSEDMLDocument->createRepeatedTask();
  std::string taskId = SEDMLUtils::getNextId("task", mpSEDMLDocument->getNumTasks());
  task->setId(taskId);
  task->setResetModel(!pProblem->getContinueFromCurrentState());

  // craete ranges / changes
  for (size_t i = 0; i < numItems; ++i)
    {
      CCopasiParameterGroup* current = pProblem->getScanItem(i);
      CScanProblem::Type type = (CScanProblem::Type)(*current->getParameter("Type")->getValue().pUINT);

      // ignore random items
      if (type == CScanProblem::SCAN_RANDOM)
        {
          CCopasiMessage(CCopasiMessage::WARNING, "This version of COPASI cannot export random scan items, they will be ignored.");
          continue;
        }

      int numSteps = (*current->getParameter("Number of steps")->getValue().pUINT);

      // handle repeats
      if (type == CScanProblem::SCAN_REPEAT)
        {
          SedUniformRange *range = task->createUniformRange();
          range->setId(SEDMLUtils::getNextId("range", task->getNumRanges()));
          range->setStart(0);
          range->setEnd(numSteps);
          range->setNumberOfPoints(numSteps);
          range->setType("linear");

          if (task->isSetRangeId())
            task->setRangeId(range->getId());

          continue;
        }

      // handle scans
      if (type == CScanProblem::SCAN_LINEAR)
        {
          double min = (*current->getParameter("Minimum")->getValue().pDOUBLE);
          double max = (*current->getParameter("Maximum")->getValue().pDOUBLE);
          bool log = (*current->getParameter("log")->getValue().pBOOL);

          SedUniformRange *range = task->createUniformRange();
          range->setId(SEDMLUtils::getNextId("range", task->getNumRanges()));
          range->setStart(min);
          range->setEnd(max);
          range->setNumberOfPoints(numSteps);
          range->setType(log ? "log" : "linear");

          const CRegisteredObjectName& cn = (*current->getParameter("Object")->getValue().pCN);
          std::string xpath = SEDMLUtils::getXPathForObject(*static_cast<const CCopasiObject*>(dataModel.getObject(cn)));

          if (xpath.empty())
            {
              CCopasiMessage(CCopasiMessage::WARNING, "This version of COPASI cannot export the selected scan object, it will be ignored.");
              continue;
            }

          SedSetValue *change = task->createTaskChange();
          change->setModelReference(modelId);

          if (xpath == SEDML_TIME_URN)
            {
              change->setSymbol(xpath);
            }
          else
            {
              change->setTarget(xpath);
            }

          change->setRange(range->getId());
          change->setMath(SBML_parseFormula(range->getId().c_str()));

          continue;
        }
    }

  if (!task->isSetRangeId() && task->getNumRanges() > 0)
    task->setRangeId(task->getRange(0)->getId());

  // create subtask
  SedSubTask* subTask = task->createSubTask();
  subTask->setOrder(1);
  subTask->setTask(subTaskId);

  return taskId;
}
LIBSEDML_CPP_NAMESPACE_USE

int
main (int argc, char* argv[])
{
  if (argc != 2)
  {
    cout << endl << "Usage: create_sedml output-filename"
    << endl << endl;
    return 2;
  }

  // create the document
  SedDocument doc(1,3);
  doc.setAnnotation("<test xmlns='http://test.org/test/annotation' attribute='test' />");

  {
    SedDataDescription* dataDesc =  doc.createDataDescription();
    dataDesc->setId("oscli");
    dataDesc->setName("Oscli Timecourse data");
    dataDesc->setSource("oscli.numl");

    DimensionDescription* dimDesc = dataDesc->createDimensionDescription();
    
    CompositeDescription* timeDesc = new CompositeDescription();
    timeDesc->setIndexType("double");
    timeDesc->setId("time");
    timeDesc->setName("time");

    CompositeDescription* speciesDesc = timeDesc->createCompositeDescription();
    speciesDesc->setIndexType("string");
    speciesDesc->setId("SpeciesIds");
    speciesDesc->setName("SpeciesIds");

    AtomicDescription* concentrationDesc = speciesDesc->createAtomicDescription();
    concentrationDesc->setValueType("double");
    concentrationDesc->setName("Concentrations");

    dimDesc->appendAndOwn(timeDesc);

    SedDataSource* dataSource = dataDesc->createDataSource();
    dataSource->setId("dataS1");

    SedSlice* slice = dataSource->createSlice();
    slice->setReference("SpeciesIds");
    slice->setValue("S1");

    dataSource = dataDesc->createDataSource();
    dataSource->setIndexSet("time");
  }

  {
    // create a first model referencing an sbml file
    SedModel *model = doc.createModel();
    model->setId("model1");
    model->setSource("file.xml");
    model->setLanguage("urn:sedml:sbml");

    // create a second model modifying a variable of that other sbml file
    model = doc.createModel();
    model->setId("model2");
    model->setSource("model1");
    model->setLanguage("urn:sedml:sbml");

    // change a paramerter 'k' to 0.1
    SedChangeAttribute* change = model->createChangeAttribute();
    change->setTarget("/sbml:sbml/sbml:model/sbml:listOfParameters/sbml:parameter[@id='k']/@value");
    change->setNewValue("0.1");

    // remove species 's1'
    SedRemoveXML* remove = model->createRemoveXML();
    remove->setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='S1']");

    // add another species 's1' using addxml
    SedAddXML* addXml = model->createAddXML();
    addXml->setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies");
    addXml->setNewXML(XMLNode::convertStringToXMLNode("<species xmlns='http://www.sbml.org/sbml/level2' id='S1' boundaryCondition='false' initialConcentration='0' compartment='compartment' />"));

    // replace 's1' one more time by a better one 
    SedChangeXML* changeXML = model->createChangeXML();
    changeXML->setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='S1']");
    changeXML->setNewXML(XMLNode::convertStringToXMLNode("<species xmlns='http://www.sbml.org/sbml/level2'  id='S1' boundaryCondition='false' initialConcentration='10' compartment='compartment' />"));

    // now for something tricky we want to update the initialConcentration of 'S2' to be
    // half what it was in the original model
    SedComputeChange* compute = model->createComputeChange();
    compute->setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id=&quot;S2&quot;]/@initialConcentration");
    SedVariable *variable = compute->createVariable();
    variable->setId("S2");
    variable->setModelReference("model1");
    variable->setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='S2']");
    compute->setMath(SBML_parseFormula("S2 / 2"));
  }

  // create simulation
  SedUniformTimeCourse* tc = doc.createUniformTimeCourse();
  tc->setId("sim1");
  tc->setInitialTime(0.0);
  tc->setOutputStartTime(0.0);
  tc->setOutputEndTime(10.0);
  tc->setNumberOfPoints(1000);
  // need to set the correct KISAO Term
  SedAlgorithm* alg = tc->createAlgorithm();
  alg->setKisaoID("KISAO:0000019");
  SedAlgorithmParameter* algParam = alg->createAlgorithmParameter();
  algParam ->setNotes("<p xmlns='http://www.w3.org/1999/xhtml'>This is the absolute tolerance</p>");
  algParam ->setKisaoID("KISAO:0000211");
  algParam ->setValue("1e-12");
  // add annotation
  tc->setAnnotation("<test xmlns='http://test.org/test/simulation/annotation' attribute='uniform time course' />");
  alg->setAnnotation("<test xmlns='http://test.org/test/simulation/annotation' attribute='algorithm' />");

  // create one step
  SedOneStep* step = doc.createOneStep();
  step->setId("sim2");
  step->setStep(0.1);
  alg = step->createAlgorithm();
  alg->setKisaoID("KISAO:0000019");


  // create steady state
  SedSteadyState* steady = doc.createSteadyState();
  steady->setId("sim3");
  alg = steady->createAlgorithm();
  alg->setKisaoID("KISAO:0000282");

  // create a task that uses the simulation and the model above
  SedTask* task = doc.createTask();
  task->setId("task1");
  task->setModelReference("model1");
  task->setSimulationReference("sim1");

  task = doc.createTask();
  task->setId("task2");
  task->setModelReference("model1");
  task->setSimulationReference("sim2");

  SedRepeatedTask* repeat = doc.createRepeatedTask();
  repeat->setId("task3");
  repeat->setRangeId("index");
  repeat->setResetModel(false);

  SedUniformRange* index = repeat->createUniformRange();
  index->setId("index");
  index->setStart(0);
  index->setEnd(10);
  index->setNumberOfPoints(100);
  index->setType("linear");

  SedFunctionalRange* current = repeat->createFunctionalRange();
  current->setId("current");
  current->setRange("index");
  current->setMath(SBML_parseFormula("piecewise(8, lt(index, 1), 0.1, and(geq(index, 4), lt(index, 6)), 8)"));

  SedVectorRange* vrange = repeat->createVectorRange();
  vrange->setId("vRange");
  vrange->addValue(8);
  vrange->addValue(4);
  vrange->addValue(0.1);


  SedSetValue* setValue = repeat->createTaskChange();
  setValue->setTarget("/sbml:sbml/sbml:model/sbml:listOfParameters/sbml:parameter[@id='J0_v0']");
  setValue->setRange("current");
  setValue->setModelReference("model1");
  setValue->setMath(SBML_parseFormula("current"));

  SedSubTask* subTask = repeat->createSubTask();
  subTask->setTask("task1");
  subTask->setOrder(1);
  

  // add a DataGenerator to hold the output for time
  SedDataGenerator* dg = doc.createDataGenerator();
  dg->setId("time");
  dg->setName("time");
  SedVariable* var = dg->createVariable();
  var->setId("v0");
  var->setName("time");
  var->setTaskReference("task1");
  var->setSymbol("urn:sedml:symbol:time");
  dg->setMath(SBML_parseFormula("v0"));

  // and one for S1
  dg = doc.createDataGenerator();
  dg->setId("S1");
  dg->setName("S1");
  var = dg->createVariable();
  var->setId("v1");
  var->setName("S1");
  var->setTaskReference("task1");
  var->setTarget("/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='S1']");
  dg->setMath(SBML_parseFormula("v1"));

  // add a report
  SedReport* report = doc.createReport();
  report->setId("r1");
  report->setName("report 1");
  SedDataSet* set = report->createDataSet();
  set->setId("ds1");
  set->setLabel("time");
  set->setDataReference("time");
  set = report->createDataSet();
  set->setId("ds2");
  set->setLabel("S1");
  set->setDataReference("S1");

  // add a 2d plot
  SedPlot2D* plot = doc.createPlot2D();
  plot->setId("p1");
  plot->setName("S1 Timecourse");
  SedCurve* curve = plot->createCurve();
  curve->setId("c1");
  curve->setName("S1");
  curve->setLogX(false);
  curve->setLogY(false);
  curve->setXDataReference("time");
  curve->setYDataReference("S1");

  // add a 3D Plot
  SedPlot3D* plot2 = doc.createPlot3D();
  plot2->setId("p2");
  plot2->setName("dunno");
  SedSurface* surf = plot2->createSurface();
  surf->setId("surf1");
  surf->setName("S1");
  surf->setLogX(false);
  surf->setLogY(false);
  surf->setLogZ(false);
  surf->setXDataReference("time");
  surf->setYDataReference("S1");
  surf->setZDataReference("S1");
  
  // write the document
  writeSedML(&doc, argv[1]);
  
  return 0;
}