File size: 16,244 Bytes
7e59e4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | // Copyright (C) 2021 - 2025 by Pedro Mendes, Rector and Visitors of the
// University of Virginia, University of Heidelberg, and University
// of Connecticut School of Medicine.
// All rights reserved.
#include "catch.hpp"
extern std::string getTestFile(const std::string & fileName);
#include <copasi/CopasiTypes.h>
#include <copasi/utilities/CDirEntry.h>
#include <copasi/commandline/COptions.h>
TEST_CASE("create a reaction with numerically named species", "[copasi][creation]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != nullptr);
REQUIRE(dm->newModel(NULL, true));
auto * model = dm->getModel();
REQUIRE(model != nullptr);
auto * reaction = model->createReaction("r1");
REQUIRE(reaction != nullptr);
REQUIRE(reaction->setReactionScheme("2 \"2\" -> 3 \"3\""));
const auto& chem = reaction->getChemEq();
REQUIRE(chem.getSubstrates().size() == 1);
REQUIRE(chem.getSubstrates()[0].getMetabolite() != nullptr);
REQUIRE(chem.getSubstrates()[0].getMultiplicity() == 2);
REQUIRE(chem.getSubstrates()[0].getMetabolite()->getObjectName() == "2");
REQUIRE(chem.getProducts().size() == 1);
REQUIRE(chem.getProducts()[0].getMetabolite() != nullptr);
REQUIRE(chem.getProducts()[0].getMultiplicity() == 3);
REQUIRE(chem.getProducts()[0].getMetabolite()->getObjectName() == "3");
CRootContainer::removeDatamodel(dm);
}
TEST_CASE("create a new model with invalid value", "[copasi][creation]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != NULL);
REQUIRE(dm->newModel(NULL, true));
auto * model = dm->getModel();
REQUIRE(model != nullptr);
auto* exp = new CExpression("expr", dm);
exp->setInfix("1,1");
exp->compile();
auto val = exp->calcValue();
pdelete(exp);
CRootContainer::removeDatamodel(dm);
}
TEST_CASE("create a new model with invalid trigger", "[copasi][creation]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != NULL);
REQUIRE(dm->newModel(NULL, true));
auto * model = dm->getModel();
REQUIRE(model != nullptr);
auto * event = model->createEvent("e0");
event->setTriggerExpression(CRegisteredCommonName(std::string("<") + std::string(model->getValueReference()->getStringCN().c_str()) + std::string("> > 10")));
event->compile({dm});
REQUIRE(model->compileIfNecessary(NULL) == true);
event->setTriggerExpression(CRegisteredCommonName(std::string(model->getValueReference()->getStringCN().c_str())));
event->compile({dm});
REQUIRE(model->compileIfNecessary(NULL) == false);
CRootContainer::removeDatamodel(dm);
}
TEST_CASE("create a model with inhibited reaciton", "[copasi][creation]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != NULL);
REQUIRE(dm->newModel(NULL, true));
auto * m = dm->getModel();
auto * pFunc = new CKinFunction("test");
REQUIRE(pFunc->setInfix("(1-a*I)*k*A*B") == true);
auto & vars = pFunc->getVariables();
for (auto & var : vars)
{
if (var.getObjectName() == "A" || var.getObjectName() == "B")
var.setUsage(CFunctionParameter::Role::SUBSTRATE);
else if (var.getObjectName() == "I")
var.setUsage(CFunctionParameter::Role::MODIFIER);
else
var.setUsage(CFunctionParameter::Role::PARAMETER);
}
auto * db = CRootContainer::getFunctionList();
REQUIRE(db->add(pFunc, true));
auto * r = m->createReaction("R1");
REQUIRE(r->setReactionScheme("S1 + S2 -> S3") == true);
auto * info = new CReactionInterface();
info->init(*r);
info->setFunctionAndDoMapping("test");
// does not work because of the modifier
REQUIRE(info->isValid() == false);
// create modifier
REQUIRE(r->setReactionScheme("S1 + S2 -> S3; I") == true);
info->init(*r);
info->setFunctionAndDoMapping("test");
// now it should work
REQUIRE(info->isValid() == true);
REQUIRE(info->writeBackToReaction(r));
REQUIRE(r->compile() == true);
REQUIRE(r->getFunction()->getObjectName() == "test");
delete info;
CRootContainer::removeDatamodel(dm);
}
TEST_CASE("changing from global to local variable", "[copasi][creation]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != NULL);
REQUIRE(dm->newModel(NULL, true));
auto * m = dm->getModel();
auto * p = m->createModelValue("k_global");
auto * r = m->createReaction("R");
r->setReactionScheme("A -> B");
{
CReactionInterface info;
info.init(*r);
REQUIRE(info.isLocalValue(0) == true);
REQUIRE(r->isLocalParameter(0));
REQUIRE(info.writeBackToReaction(r, true) == true);
info.setMapping(0, "k_global");
REQUIRE(info.writeBackToReaction(r, true) == true);
REQUIRE(info.isLocalValue(0) == false);
REQUIRE(r->isLocalParameter(0) == false);
}
{
CReactionInterface info;
info.init(*r);
REQUIRE(info.isLocalValue(0) == false);
info.setLocalValue(0, 2.0);
REQUIRE(info.isLocalValue(0) == true);
REQUIRE(info.writeBackToReaction(r, true) == true);
REQUIRE(r->isLocalParameter(0) == true);
}
CRootContainer::removeDatamodel(dm);
}
TEST_CASE("changing initial concentrations", "[copasi][manipulation]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != nullptr);
REQUIRE(dm->loadModel(getTestFile("test-data/brusselator.cps"), NULL) == true);
// add species
auto * model = dm->getModel();
auto * pMetab = model->createMetabolite("speciesB", "compartment");
// change initial concentration
for (auto & metab : model->getMetabolites())
{
if (metab.getObjectDisplayName() != "speciesB")
continue;
model->updateInitialValues(metab.getInitialConcentrationReference(), false);
metab.setInitialConcentration(0.4);
model->updateInitialValues(metab.getInitialConcentrationReference(), false);
}
// retrieve metab again
for (auto & metab : model->getMetabolites())
{
if (metab.getObjectDisplayName() != "speciesB")
continue;
REQUIRE(metab.getInitialConcentration() == 0.4);
}
CRootContainer::removeDatamodel(dm);
}
TEST_CASE("generating expressions", "[copasi][manipulation]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != nullptr);
REQUIRE(dm->loadModel(getTestFile("test-data/brusselator.cps"), NULL) == true);
auto * model = dm->getModel();
{
CExpressionGenerator gen("Species", "Transient Concentrations", "Sum");
auto expression = gen.generateExpressionFor(model);
REQUIRE(expression == "{[X]} + {[Y]} + {[A]} + {[B]} + {[D]} + {[E]}");
gen.setOperation("Product");
expression = gen.generateExpressionFor(model);
REQUIRE(expression == "{[X]} * {[Y]} * {[A]} * {[B]} * {[D]} * {[E]}");
gen.setOperation("Sum of Squares");
expression = gen.generateExpressionFor(model);
REQUIRE(expression == "{[X]}^2 + {[Y]}^2 + {[A]}^2 + {[B]}^2 + {[D]}^2 + {[E]}^2");
gen.setOperation("Sum of Absolutes");
expression = gen.generateExpressionFor(model);
REQUIRE(expression == "ABS({[X]}) + ABS({[Y]}) + ABS({[A]}) + ABS({[B]}) + ABS({[D]}) + ABS({[E]})");
}
}
TEST_CASE("use binary min and max", "[copasi][sbml]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != NULL);
REQUIRE(dm->newModel(NULL, true));
auto * model = dm->getModel();
auto* p1 = model->createModelValue("one", 1);
auto * p2 = model->createModelValue("two", 2);
auto * p3 = model->createModelValue("three", 3);
auto * p4 = model->createModelValue("four", 4);
// n-ary min max not yet supported, but we can
// allow binary for now
auto *maxP = model->createModelValue("p_MAX", 0);
{
maxP->setStatus(CModelEntity::Status::ASSIGNMENT);
std::stringstream str;
str << "MAX(<" << p1->getValueReference()->getCN() << ">,"
//<< "<" << p2->getValueReference()->getCN() << ">,"
//<< "<" << p3->getValueReference()->getCN() << ">,"
<< "<" << p4->getValueReference()->getCN() << ">)";
bool result = maxP->setExpression(str.str());
REQUIRE(result == true);
}
auto * minP = model->createModelValue("p_MIN", 0);
{
maxP->setStatus(CModelEntity::Status::ASSIGNMENT);
std::stringstream str;
str << "MIN(<" << p1->getValueReference()->getCN() << ">,"
//<< "<" << p2->getValueReference()->getCN() << ">,"
//<< "<" << p3->getValueReference()->getCN() << ">,"
<< "<" << p4->getValueReference()->getCN() << ">)";
bool success = maxP->setExpression(str.str());
REQUIRE(success == true);
}
model->applyInitialValues();
CRootContainer::removeDatamodel(dm);
}
TEST_CASE("set up opt problem subtype", "[copasi][optimization]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != NULL);
REQUIRE(dm->newModel(NULL, true));
auto * model = dm->getModel();
{
auto & task = (*dm->getTaskList())["Optimization"];
auto * problem = dynamic_cast< COptProblem * >(task.getProblem());
REQUIRE(problem != NULL);
problem->setSubtaskType(CTaskEnum::Task::timeCourse);
}
{
auto & task = (*dm->getTaskList())["Optimization"];
auto * problem = dynamic_cast< COptProblem * >(task.getProblem());
REQUIRE(problem != NULL);
REQUIRE((problem->getSubtaskType() == CTaskEnum::Task::timeCourse));
}
CRootContainer::removeDatamodel(dm);
}
#include <copasi/config.h>
#include <copasi/MIRIAM/CModelMIRIAMInfo.h>
TEST_CASE("import sbml model and test miriam info", "[copasi][sbml][miriam]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != NULL);
REQUIRE(dm->importSBML(getTestFile("test-data/miriam.xml"), NULL) == true);
auto * model = dm->getModel();
REQUIRE(model != NULL);
CMIRIAMInfo * info = CAnnotation::allocateMiriamInfo(dm->getModel());
info->load(dm->getModel());
auto miriam = info->getRDFGraph()->toXmlString();
REQUIRE(!miriam.empty());
CRootContainer::removeDatamodel(dm);
}
TEST_CASE("manually create miriam using libsbml", "[copasi][miriam]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != NULL);
dm->newModel(NULL, true);
CMIRIAMInfo * info = CAnnotation::allocateMiriamInfo(dm->getModel());
info->load(dm->getModel());
CDataVector< CCreator >::iterator it = info->getCreators().begin();
CDataVector< CCreator >::iterator end = info->getCreators().end();
for (; it != end; ++it)
info->removeCreator(&*it);
REQUIRE(info->save());
CCreator * pCreator = info->createCreator("");
pCreator->setFamilyName("LastName");
pCreator->setGivenName("FirstName");
pCreator->setEmail("email@address.com");
pCreator->setORG("COPASI org");
REQUIRE(info->save());
auto miriam = info->getRDFGraph()->toXmlString();
REQUIRE(!miriam.empty());
// now lets construct a graph from that string
CRDFGraph * pGraph = CRDFGraph::fromString(miriam);
// and serialize
auto miriam2 = pGraph->toXmlString();
REQUIRE(miriam == miriam2);
pdelete(pGraph);
CRootContainer::removeDatamodel(dm);
}
TEST_CASE("copy experimental data to", "[copasi][data]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != nullptr);
REQUIRE(dm->loadFromFile(getTestFile("test-data/schaber2.cps"), NULL) == true);
std::string targetDir = getTestFile("test-data/experimental_data");
// create target directory if it does not exist
if (!CDirEntry::exist(targetDir))
{
REQUIRE(CDirEntry::createDir(targetDir) == true);
REQUIRE(CDirEntry::exist(targetDir) == true);
}
auto fileName = dm->getFileName();
// get parameter estimation task
auto * task = dynamic_cast< CFitTask * >(&dm->getTaskList()->operator[](CTaskEnum::TaskName[CTaskEnum::Task::parameterFitting]));
REQUIRE(task != nullptr);
auto * problem = dynamic_cast< CFitProblem * >(task->getProblem());
REQUIRE(problem != nullptr);
auto & expSet = problem->getExperimentSet();
bool compiled = expSet.compile(&dm->getModel()->getMathContainer());
REQUIRE(compiled == true);
auto originalFilenames = expSet.getFileNamesOnly();
REQUIRE(originalFilenames.size() == 1);
auto newFileName = targetDir + "/schaber2.cps";
dm->saveModel(newFileName, NULL, true);
auto filenamesAfterSave = expSet.getFileNamesOnly();
REQUIRE(filenamesAfterSave.size() == 1);
dm->copyExperimentalDataTo(targetDir, "test_", true);
newFileName = targetDir + "/schaber3.cps";
dm->saveModel(newFileName, NULL, true);
auto filenamesAfterSave2 = expSet.getFileNamesOnly();
REQUIRE(filenamesAfterSave2.size() == 1);
// restore filenames
REQUIRE(expSet.setFileNames(filenamesAfterSave) == true);
auto filenamesAfterSaveRestored = expSet.getFileNamesOnly();
REQUIRE(filenamesAfterSaveRestored.size() == 1);
REQUIRE(filenamesAfterSaveRestored[0] == filenamesAfterSave[0]);
CRootContainer::removeDatamodel(dm);
// load the last model, verify that the experimental data is copied to the new location
{
dm = CRootContainer::addDatamodel();
REQUIRE(dm != nullptr);
REQUIRE(dm->loadFromFile(newFileName, NULL) == true);
auto * model = dm->getModel();
REQUIRE(model != nullptr);
auto * task = dynamic_cast< CFitTask * >(&dm->getTaskList()->operator[](CTaskEnum::TaskName[CTaskEnum::Task::parameterFitting]));
REQUIRE(task != nullptr);
auto * problem = dynamic_cast< CFitProblem * >(task->getProblem());
REQUIRE(problem != nullptr);
auto & expSet = problem->getExperimentSet();
auto filenames = expSet.getFileNamesOnly();
REQUIRE(filenames.size() == 1);
REQUIRE(filenames[0].find("test_") != std::string::npos);
dm->copyExperimentalDataTo(targetDir, "test3_", false);
// write them again, to see that the filenames are as expected
REQUIRE(dm->loadFromFile(newFileName, NULL) == true);
dm->copyExperimentalDataTo(targetDir, "test3_", true);
// and without overwrite
REQUIRE(dm->loadFromFile(newFileName, NULL) == true);
dm->copyExperimentalDataTo(targetDir, "test3_", false);
CRootContainer::removeDatamodel(dm);
}
// remove files
REQUIRE(CDirEntry::removeFiles("*", targetDir) == true);
REQUIRE(CDirEntry::remove(targetDir) == true);
// verify that the experimental data directory is removed
REQUIRE(CDirEntry::exist(targetDir) == false);
}
#ifdef COPASI_USE_RAPTOR
#include <copasi/MIRIAM/CRDFParser.h>
#include <copasi/MIRIAM/CRDFGraph.h>
#include <copasi/MIRIAM/CRDFWriter.h>
TEST_CASE("miriam parsing using libsbml", "[copasi][miriam]")
{
auto * dm = CRootContainer::addDatamodel();
REQUIRE(dm != NULL);
REQUIRE(dm->loadModel(getTestFile("test-data/miriam.cps"), NULL) == true);
auto * model = dm->getModel();
REQUIRE(model != NULL);
// first a small node from metabolite
auto miriam = model->getMetabolites()[0].getMiriamAnnotation();
REQUIRE(!miriam.empty());
auto * graph1 = CRDFParser::graphFromXml(miriam);
auto * graph2 = CRDFGraph::fromString(miriam);
REQUIRE(graph1->getAboutNode()->getId() == graph2->getAboutNode()->getId());
REQUIRE(graph1->getNameSpaceMap().size() == graph2->getNameSpaceMap().size());
REQUIRE(graph1->getTriplets().size() == graph2->getTriplets().size());
REQUIRE(graph1->getBlankNodeMap().size() == graph2->getBlankNodeMap().size());
// try walking over the graph printing it
auto* start = graph1->getAboutNode();
auto incoming = graph1->getIncomingTriplets(start);
auto parents = graph1->getParentSubjects(start);
// now ensure that serializing to string works
CRDFWriter writer;
auto * raptorSerialization = writer.write(graph2);
auto serialization = graph2->toXmlString();
free(raptorSerialization);
pdelete(graph1);
pdelete(graph2);
// now the whole model node
miriam = model->getMiriamAnnotation();
REQUIRE(!miriam.empty());
graph1 = CRDFParser::graphFromXml(miriam);
graph2 = CRDFGraph::fromString(miriam);
REQUIRE(graph1->getAboutNode()->getId() == graph2->getAboutNode()->getId());
REQUIRE(graph1->getNameSpaceMap().size() == graph2->getNameSpaceMap().size());
REQUIRE(graph1->getTriplets().size() == graph2->getTriplets().size());
REQUIRE(graph1->getBlankNodeMap().size() == graph2->getBlankNodeMap().size());
raptorSerialization = writer.write(graph2);
serialization = graph2->toXmlString();
free(raptorSerialization);
pdelete(graph1);
pdelete(graph2);
CRootContainer::removeDatamodel(dm);
}
#endif // COPASI_USE_RAPTOR
|