Skip to content

Commit

Permalink
First stab at getting reaction nodes set.
Browse files Browse the repository at this point in the history
  • Loading branch information
luciansmith committed Nov 7, 2024
1 parent 139552b commit 4db3cec
Show file tree
Hide file tree
Showing 16 changed files with 385 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
libroadrunner_deps_owner: [ "sys-bio" ]
libroadrunner_deps_repo: [ "libroadrunner-deps" ]
libroadrunner_deps_name: [ "libroadrunner-deps" ]
libroadrunner_deps_release_version: [ "v2.2.7" ]
libroadrunner_deps_release_version: [ "v2.2.9" ]
python_version: [ "3.12" ]

runs-on: ${{ matrix.platform.os_name }}
Expand Down
9 changes: 9 additions & 0 deletions src/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,16 @@ enum layout_type {
lt_linewidth,
lt_linecolor,
lt_shape,
lt_reactionArc,
lt_unknown
};

enum arc_type {
at_rxn = 0,
at_spec,
at_b1,
at_b2,
at_none
};

#endif // ENUMS_H
8 changes: 8 additions & 0 deletions src/formula.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ void Formula::AddCurlyBrackets()
m_components.push_back(newvar);
}

void Formula::AddVectorOfTwoValues(double x, double y)
{
AddNum(x);
AddMathThing(',');
AddNum(y);
AddCurlyBrackets();
}

void Formula::AddConversionFactor(const Variable* cf)
{
if (IsEmpty()) return;
Expand Down
1 change: 1 addition & 0 deletions src/formula.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Formula : public Annotated
void AddEllipses();
void AddParentheses();
void AddCurlyBrackets();
void AddVectorOfTwoValues(double x, double y);
void AddConversionFactor(const Variable* cf);
void AddInvTimeConversionFactor(const Variable* tcf);

Expand Down
194 changes: 188 additions & 6 deletions src/layoutWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
#include "registry.h"
#include "stringx.h"
#include "regex"
#include "reaction.h"
#ifdef LIBSBML_HAS_PACKAGE_DISTRIB
#include <sbml/packages/layout/sbml/Layout.h>
#include <sbml/packages/layout/extension/LayoutModelPlugin.h>
#endif
#include <libsbmlnetwork_sbmldocument.h>
#include <libsbmlnetwork_sbmldocument_layout.h>
#include <libsbmlnetwork_sbmldocument_render.h>
#include <libsbmlnetwork_render_helpers.h>
#include <sbmlnetwork/libsbmlnetwork_sbmldocument.h>
#include <sbmlnetwork/libsbmlnetwork_sbmldocument_layout.h>
#include <sbmlnetwork/libsbmlnetwork_sbmldocument_render.h>
#include <sbmlnetwork/libsbmlnetwork_render_helpers.h>

using namespace std;
using namespace libsbml;
Expand All @@ -19,6 +20,9 @@ LayoutWrapper::LayoutWrapper(Variable* parent, layout_type type)
: Variable()
, m_parent(parent)
, m_layout_type(type)
, m_speciesId("")
, m_speciesIndex(-1)
, m_arctype(at_none)
{
m_module = parent->GetNamespace();
m_displayname = "";
Expand All @@ -37,6 +41,9 @@ LayoutWrapper::LayoutWrapper(layout_type type, const string& group)
: Variable()
, m_parent(NULL)
, m_layout_type(type)
, m_speciesId("")
, m_speciesIndex(-1)
, m_arctype(at_none)
{
m_displayname = "";
m_formulatype = formulaINITIAL;
Expand Down Expand Up @@ -224,7 +231,18 @@ Variable* LayoutWrapper::GetParent()
string LayoutWrapper::GetNameDelimitedBy(string cc) const
{
if (m_parent) {
return m_parent->GetNameDelimitedBy(cc) + cc + LayoutTypeToString(m_layout_type);
if (m_layout_type == lt_reactionArc) {
stringstream ret;
ret << m_parent->GetNameDelimitedBy(cc) << cc << m_speciesId;
if (m_speciesIndex > 0) {
ret << cc << m_speciesIndex;
}
ret << cc << ArcTypeToString(m_arctype);
return ret.str();
}
else {
return m_parent->GetNameDelimitedBy(cc) + cc + LayoutTypeToString(m_layout_type);
}
}
else {
return Variable::GetNameDelimitedBy(cc);
Expand All @@ -242,7 +260,7 @@ string LayoutWrapper::CreateLayoutParamsAntimonySyntax(const string& indent) con
string ret = "";
if (!m_valFormula.IsEmpty()) {
string strval = m_valFormula.ToDelimitedStringWithEllipses(".");
if (strval[0] == '#' || (strval.find('-') != string::npos) || (strval.find(' ') != string::npos)) {
if (strval[0] == '#' || (strval[0] != '{' && ((strval.find('-') != string::npos) || (strval.find(' ') != string::npos)))) {
strval = '"' + strval + '"';
}
ret = indent + GetNameDelimitedBy(".") + " = " + strval + "\n";
Expand Down Expand Up @@ -287,6 +305,98 @@ bool LayoutWrapper::TransferLayoutInformationTo(SBMLDocument* sbml) const
ret2 = LIBSBMLNETWORK_CPP_NAMESPACE::setDimensionHeight(sbml, sid, yval, false);
}
break;
case lt_reactionArc:
{
bool reactant = true;
int curveIndex = 0;
int speciesIndex = -1;
AntimonyReaction* rxn = m_parent->GetReaction();
ReactantList* rlist = rxn->GetLeft();
for (size_t i = 0; i < rlist->Size(); i++) {
const Variable* element = rlist->GetNthReactant(i);
double stoich = rlist->GetStoichiometryFor(i);
if (element->GetNameDelimitedBy(".") == m_speciesId) {
speciesIndex = curveIndex;
if (m_speciesIndex > 0) {
speciesIndex += m_speciesIndex;
}
break;
}
curveIndex += int(floor(stoich)) - 1;
}
rlist = rxn->GetRight();
for (size_t i = 0; i < rlist->Size(); i++) {
const Variable* element = rlist->GetNthReactant(i);
double stoich = rlist->GetStoichiometryFor(i);
if (element->GetNameDelimitedBy(".") == m_speciesId) {
speciesIndex = curveIndex;
if (m_speciesIndex > 0) {
speciesIndex += m_speciesIndex;
}
break;
}
curveIndex += int(floor(stoich)) - 1;
}
assert(speciesIndex != -1);
string role = LIBSBMLNETWORK_CPP_NAMESPACE::getSpeciesReferenceRole(sbml, sid, 0, speciesIndex);
switch (m_arctype) {
case at_spec:
if (startsAtReaction(role)) {
if (!isnan(xval)) {
LIBSBMLNETWORK_CPP_NAMESPACE::setSpeciesReferenceCurveSegmentStartPointX(sbml, sid, speciesIndex, 0, xval);
}
if (!isnan(yval)) {
LIBSBMLNETWORK_CPP_NAMESPACE::setSpeciesReferenceCurveSegmentStartPointY(sbml, sid, speciesIndex, 0, yval);
}
}
else {
assert(!startsAtReaction(role));
if (!isnan(xval)) {
LIBSBMLNETWORK_CPP_NAMESPACE::setSpeciesReferenceCurveSegmentEndPointX(sbml, sid, speciesIndex, 0, xval);
}
if (!isnan(yval)) {
LIBSBMLNETWORK_CPP_NAMESPACE::setSpeciesReferenceCurveSegmentEndPointY(sbml, sid, speciesIndex, 0, yval);
}
}
break;
case at_rxn:
if (startsAtReaction(role)) {
if (!isnan(xval)) {
LIBSBMLNETWORK_CPP_NAMESPACE::setSpeciesReferenceCurveSegmentEndPointX(sbml, sid, speciesIndex, 0, xval);
}
if (!isnan(yval)) {
LIBSBMLNETWORK_CPP_NAMESPACE::setSpeciesReferenceCurveSegmentEndPointY(sbml, sid, speciesIndex, 0, yval);
}
}
else {
assert(!startsAtReaction(role));
if (!isnan(xval)) {
LIBSBMLNETWORK_CPP_NAMESPACE::setSpeciesReferenceCurveSegmentStartPointX(sbml, sid, speciesIndex, 0, xval);
}
if (!isnan(yval)) {
LIBSBMLNETWORK_CPP_NAMESPACE::setSpeciesReferenceCurveSegmentStartPointY(sbml, sid, speciesIndex, 0, yval);
}
}
break;
case at_b1:
if (!isnan(xval)) {
LIBSBMLNETWORK_CPP_NAMESPACE::setSpeciesReferenceCurveSegmentBasePoint1X(sbml, sid, speciesIndex, 0, xval);
}
if (!isnan(yval)) {
LIBSBMLNETWORK_CPP_NAMESPACE::setSpeciesReferenceCurveSegmentBasePoint1Y(sbml, sid, speciesIndex, 0, yval);
}
break;
case at_b2:
if (!isnan(xval)) {
LIBSBMLNETWORK_CPP_NAMESPACE::setSpeciesReferenceCurveSegmentBasePoint2X(sbml, sid, speciesIndex, 0, xval);
}
if (!isnan(yval)) {
LIBSBMLNETWORK_CPP_NAMESPACE::setSpeciesReferenceCurveSegmentBasePoint2Y(sbml, sid, speciesIndex, 0, yval);
}
break;
}
break;
}
default:
assert(false); //Only the above two are 'IsPair' true
break;
Expand Down Expand Up @@ -715,6 +825,7 @@ bool LayoutWrapper::HasLayoutPositionInfo() const
case lt_position:
case lt_x:
case lt_y:
case lt_reactionArc:
return true;
case lt_size:
case lt_height:
Expand All @@ -734,3 +845,74 @@ bool LayoutWrapper::HasLayoutPositionInfo() const
assert(false);
return false;
}

bool LayoutWrapper::setSpeciesId(const std::string* name)
{
if (name) {
m_speciesId = *name;
return false;
}
return true;
}

void LayoutWrapper::setSpeciesIndex(int index)
{
m_speciesIndex = index;
}

bool LayoutWrapper::setArcType(const std::string* type)
{
if (CaselessStrCmp(true, *type, "pos")) {
m_arctype = at_spec;
return false;
}
if (CaselessStrCmp(true, *type, "position")) {
m_arctype = at_spec;
return false;
}
if (CaselessStrCmp(true, *type, "b1")) {
m_arctype = at_b1;
return false;
}
if (CaselessStrCmp(true, *type, "b2")) {
m_arctype = at_b2;
return false;
}
if (CaselessStrCmp(true, *type, "rxn")) {
m_arctype = at_rxn;
return false;
}

return true;
}

bool LayoutWrapper::setArcType(arc_type type)
{
m_arctype = type;
return false;
}

Variable* LayoutWrapper::GetSubVariable(const std::string* name)
{
if (m_layout_type != lt_reactionArc) {
//No subvariables of any other layout wrapper type.
return NULL;
}
if (setArcType(name)) {
return NULL;
}
return this;
}

Variable* LayoutWrapper::GetSubVariable(double val)
{
if (m_speciesIndex != -1) {
return NULL;
}
if (val < 0) {
return NULL;
}
m_speciesIndex = round(val);

return this;
}
11 changes: 10 additions & 1 deletion src/layoutWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class LayoutWrapper : public Variable
protected:
Variable* m_parent;
layout_type m_layout_type;
std::string m_speciesId;
int m_speciesIndex;
arc_type m_arctype;
public:
LayoutWrapper(Variable* parent, layout_type type);
LayoutWrapper(layout_type type, const std::string& group);
Expand All @@ -28,8 +31,14 @@ class LayoutWrapper : public Variable
virtual bool Synchronize(Variable* clone, const Variable* conversionFactor);
virtual std::string CreateLayoutParamsAntimonySyntax(const std::string& indent) const;
virtual bool TransferLayoutInformationTo(libsbml::SBMLDocument* sbml) const;
bool TransferLayoutInformationTo(libsbml::SBMLDocument* sbml, const std::string& group) const;
virtual bool TransferLayoutInformationTo(libsbml::SBMLDocument* sbml, const std::string& group) const;
virtual bool HasLayoutPositionInfo() const;
virtual bool setSpeciesId(const std::string* name);
virtual void setSpeciesIndex(int index);
virtual bool setArcType(const std::string* type);
virtual bool setArcType(arc_type type);
virtual Variable* GetSubVariable(const std::string* name);
virtual Variable* GetSubVariable(double val);
};


Expand Down
Loading

0 comments on commit 4db3cec

Please sign in to comment.