Skip to content

Commit

Permalink
Merge pull request #6 from sys-bio/fix-bugs-and-add-new-features
Browse files Browse the repository at this point in the history
fix-bugs-and-add-new-features
  • Loading branch information
adelhpour authored Nov 12, 2024
2 parents ee343a8 + 6371ec6 commit 091479a
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 93 deletions.
23 changes: 14 additions & 9 deletions src/autolayout/libsbmlnetwork_autolayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void locateGlyphs(Model *model, Layout *layout, const bool &useNameAsTextLabel)
autoLayoutAlgorithm->setWidth(layout);
autoLayoutAlgorithm->setHeight(layout);
autoLayoutAlgorithm->apply();
updateCompartmentExtents(model, layout);
updateCompartmentsExtents(model, layout);
updateLayoutDimensions(layout);
delete autoLayoutAlgorithm;
if (!adjustLayoutDimensions(layout)) {
Expand Down Expand Up @@ -57,7 +57,7 @@ void locateReactions(Model *model, Layout *layout, const bool &useNameAsTextLabe
autoLayoutAlgorithm->setWidth(layout);
autoLayoutAlgorithm->setHeight(layout);
autoLayoutAlgorithm->apply();
updateCompartmentExtents(model, layout);
updateCompartmentsExtents(model, layout);
updateLayoutDimensions(layout);
delete autoLayoutAlgorithm;
}
Expand Down Expand Up @@ -189,22 +189,27 @@ initializeCompartmentGlyphExtents(BoundingBox *compartmentGlyphBoundingBox, Boun
compartmentGlyphBoundingBox->setHeight(speciesGlyphBoundingBox->height() + 2 * padding);
}

void updateCompartmentExtents(Model *model, Layout *layout) {
updateCompartmentExtentsUsingItsElementsExtents(model, layout);
updateCompartmentExtentsUsingItsPresetAttributes(layout);
void updateCompartmentsExtents(Model *model, Layout *layout) {
updateCompartmentsExtentsUsingTheirElementsExtents(model, layout);
updateCompartmentsExtentsUsingTheirPresetAttributes(layout);
}

void updateCompartmentExtentsUsingItsElementsExtents(Model *model, Layout *layout) {
void updateCompartmentsExtentsUsingTheirElementsExtents(Model *model, Layout *layout) {
std::vector<std::string> extentsInitializedCompartmentGlyphIds;
for (int i = 0; i < layout->getNumSpeciesGlyphs(); i++) {
Compartment *compartment = findSpeciesGlyphCompartment(model, layout->getSpeciesGlyph(i));
if (compartment) {
std::vector < CompartmentGlyph * > compartmentGlyphs = getAssociatedCompartmentGlyphsWithCompartmentId(
layout, compartment->getId());
for (int j = 0; j < compartmentGlyphs.size(); j++) {
CompartmentGlyph *compartmentGlyph = compartmentGlyphs.at(j);
if (i == 0)
if (std::find(extentsInitializedCompartmentGlyphIds.begin(),
extentsInitializedCompartmentGlyphIds.end(), compartmentGlyph->getId()) ==
extentsInitializedCompartmentGlyphIds.end()) {
initializeCompartmentGlyphExtents(compartmentGlyph->getBoundingBox(),
layout->getSpeciesGlyph(i)->getBoundingBox());
extentsInitializedCompartmentGlyphIds.push_back(compartmentGlyph->getId());
}
updateCompartmentExtentsUsingItsElementsExtents(compartmentGlyph->getBoundingBox(),
layout->getSpeciesGlyph(i)->getBoundingBox());
}
Expand Down Expand Up @@ -280,7 +285,7 @@ void updateCompartmentExtentsUsingItsElementsExtents(BoundingBox *compartmentGly
}
}

void updateCompartmentExtentsUsingItsPresetAttributes(Layout *layout) {
void updateCompartmentsExtentsUsingTheirPresetAttributes(Layout *layout) {
for (int i = 0; i < layout->getNumCompartmentGlyphs(); i++) {
CompartmentGlyph *compartmentGlyph = layout->getCompartmentGlyph(i);
if (LIBSBMLNETWORK_CPP_NAMESPACE::getUserData(compartmentGlyph, "locked") == "true") {
Expand Down Expand Up @@ -324,7 +329,7 @@ const bool adjustLayoutDimensions(Layout *layout) {
double widthGap = desiredWidth - layout->getDimensions()->width();
double desiredHeight = getLayoutDimensionsDesiredHeight(layout);
double heightGap = desiredHeight - layout->getDimensions()->height();
if (widthGap < 0.1 * desiredWidth && heightGap < 0.1 * desiredHeight) {
if (widthGap <= 0.1 * desiredWidth && heightGap <= 0.1 * desiredHeight) {
setLayoutDimensionsDesiredWidth(layout, layout->getDimensions()->width());
setLayoutDimensionsDesiredHeight(layout, layout->getDimensions()->height());
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/autolayout/libsbmlnetwork_autolayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ void setSpeciesGlyphDimensions(Model *model, SpeciesGlyph* speciesGlyph);

void initializeCompartmentGlyphExtents(BoundingBox* compartmentGlyphBoundingBox, BoundingBox* speciesGlyphBoundingBox);

void updateCompartmentExtents(Model *model, Layout *layout);
void updateCompartmentsExtents(Model *model, Layout *layout);

void updateCompartmentExtentsUsingItsElementsExtents(Model *model, Layout *layout);
void updateCompartmentsExtentsUsingTheirElementsExtents(Model *model, Layout *layout);

void updateCompartmentExtentsUsingItsElementsExtents(BoundingBox* compartmentGlyphBoundingBox, BoundingBox* speciesGlyphBoundingBox);

void updateCompartmentExtentsUsingItsElementsExtents(BoundingBox* compartmentGlyphBoundingBox, Curve* reactionCurve);

void updateCompartmentExtentsUsingItsPresetAttributes(Layout *layout);
void updateCompartmentsExtentsUsingTheirPresetAttributes(Layout *layout);

void updateLayoutDimensions(Layout* layout);

Expand Down
71 changes: 61 additions & 10 deletions src/bindings/python/ctypes/libsbmlnetwork.py.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -490,29 +490,44 @@ class LibSBMLNetwork:
"""
return lib.c_api_makeSpeciesGlyphVisible(self.sbml_object, str(species_id).encode(), str(reaction_id).encode(), visible, reaction_glyph_index, layout_index)

def makeSpeciesGlyphsVisible(self, species_ids=None, visible=True, layout_index=0):
def makeSpeciesGlyphsVisible(self, species=None, visible=True, layout_index=0):
"""
Makes the SpeciesGlyphs of Species with the given species_ids in the Layout object with the given index in the given SBMLDocument visible or invisible

:Parameters:

- species_ids (list of strings): a list of strings that determines the ids of the Species
- species = (list of lists or list of strings, optional): a list (default: None) that determines the list of Species. The list contains:
- a list of lists where each list includes:
- 'id' (str): the ID of the Species
- 'reaction_id' (str): the ID of the Reaction
- 'reaction_glyph_index' (int, optional): the index (default: 0) of the ReactionGlyph in the given SBMLDocument
- visible = (boolean, optional): a boolean (default: True) that determines whether to make the SpeciesGlyphs visible or invisible
- layout_index (int, optional): an integer (default: 0) that determines the index of the Layout object in the given SBMLDocument

:Returns:

true on success and false if the SpeciesGlyphs could not be hidden
"""
species_ids_ptr = None
species_ids_len = 0
if species_ids is not None:
species_ids_len = len(species_ids)
species_ids_ptr = (ctypes.c_char_p * len(species_ids))()
for i in range(len(species_ids)):
species_ids_ptr[i] = ctypes.c_char_p(species_ids[i].encode())
species_ptr = None
if species is not None:
species_ptr = (ctypes.POINTER(ctypes.c_char_p) * len(species))()
for i in range(len(species)):
a_species_ptr = (ctypes.c_char_p * 3)()
if isinstance(species[i], list):
if len(species[i]) == 3:
print(species[i])
a_species_ptr[0] = ctypes.c_char_p(str(species[i][0]).encode())
a_species_ptr[1] = ctypes.c_char_p(str(species[i][1]).encode())
a_species_ptr[2] = ctypes.c_char_p(str(species[i][2]).encode())
elif len(species[i]) == 2:
a_species_ptr[0] = ctypes.c_char_p(str(species[i][0]).encode())
a_species_ptr[1] = ctypes.c_char_p(str(species[i][1]).encode())
a_species_ptr[2] = ctypes.c_char_p(str(0).encode())
else:
raise Exception("The species parameter should be a list of lists with species id, reaction id, and reaction glyph index or a list of lists with species id and reaction id.")
species_ptr[i] = a_species_ptr

return lib.c_api_makeSpeciesGlyphsVisible(self.sbml_object, species_ids_ptr, species_ids_len, visible, layout_index)
return lib.c_api_makeSpeciesGlyphsVisible(self.sbml_object, species_ptr, len(species_ptr), visible, layout_index)

def getCanvasWidth(self, layout_index=0):
"""
Expand Down Expand Up @@ -9145,6 +9160,42 @@ class LibSBMLNetwork:
"""
return lib.c_api_removeGeometricShape(self.sbml_object, str(id).encode(), geometric_shape_index, graphical_object_index, layout_index)

def getGeometricShapeId(self, id, geometric_shape_index=0, graphical_object_index=0, layout_index=0):
"""
Returns the id of the GeometricShape object with the given index associated with the model entity with the given id in the given SBMLDocument

:Parameters:

- id (string): a string that determines the id of the model entity
- geometric_shape_index (int, optional): an integer (default: 0) that determines the index of the GeometricShape object associated with the model entity with the given id in the given SBMLDocument
- graphical_object_index (int, optional): an integer (default: 0) that determines the index of the GraphicalObject in the given SBMLDocument
- layout_index (int, optional): an integer (default: 0) that determines the index of the Layout object in the given SBMLDocument

:Returns:

a string that determines the id of the GeometricShape object associated with the model entity with the given id in the given SBMLDocument
"""
lib.c_api_getGeometricShapeId.restype = ctypes.c_char_p
return ctypes.c_char_p(lib.c_api_getGeometricShapeId(self.sbml_object, str(id).encode(), geometric_shape_index, graphical_object_index, layout_index)).value.decode()

def setGeometricShapeId(self, id, geometric_shape_id, geometric_shape_index=0, graphical_object_index=0, layout_index=0):
"""
Sets the id of the GeometricShape object with the given index associated with the model entity with the given id in the given SBMLDocument

:Parameters:

- id (string): a string that determines the id of the model entity
- geometric_shape_id (string): a string that determines the id of the GeometricShape object to be added to the model entity
- geometric_shape_index (int, optional): an integer (default: 0) that determines the index of the GeometricShape object associated with the model entity with the given id in the given SBMLDocument
- graphical_object_index (int, optional): an integer (default: 0) that determines the index of the GraphicalObject in the given SBMLDocument
- layout_index (int, optional): an integer (default: 0) that determines the index of the Layout object in the given SBMLDocument

:Returns:

true on success and false if the id of the GeometricShape object could not be set
"""
return lib.c_api_setGeometricShapeId(self.sbml_object, str(id).encode(), str(geometric_shape_id).encode(), geometric_shape_index, graphical_object_index, layout_index)

def getGeometricShapeType(self, id, geometric_shape_index=0, graphical_object_index=0, layout_index=0):
"""
Returns the type of the GeometricShape object with the given index associated with the model entity with the given id in the given SBMLDocument
Expand Down
25 changes: 20 additions & 5 deletions src/c_api/libsbmlnetwork_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,19 @@ namespace LIBSBMLNETWORK_CPP_NAMESPACE {
return makeSpeciesGlyphVisible(document, layoutIndex, speciesId, reactionId, reactionGlyphIndex, visible);
}

int c_api_makeSpeciesGlyphsVisible(SBMLDocument* document, const char** speciesIds, const int speciesIdsSize, const bool visible, int layoutIndex) {
std::set<std::string> speciesIdsSet = std::set<std::string>();
for (int i = 0; i < speciesIdsSize; i++)
speciesIdsSet.insert(speciesIds[i]);
int c_api_makeSpeciesGlyphsVisible(SBMLDocument* document, const char ***species, const int speciesSize, const bool visible, int layoutIndex) {
std::set<std::tuple<std::string, std::string, int> > speciesSet = std::set<std::tuple<std::string, std::string, int> >();
if (species) {
for (int i = 0; i < speciesSize; i++) {
const char **aSpecies = species[i];
const char *speciesId = aSpecies[0];
const char *reactionId = aSpecies[1];
int reactionGlyphIndex = atoi(aSpecies[2]);
speciesSet.insert(std::make_tuple(speciesId, reactionId, reactionGlyphIndex));
}
}

return makeSpeciesGlyphsVisible(document, layoutIndex, speciesIdsSet, visible);
return makeSpeciesGlyphsVisible(document, layoutIndex, speciesSet, visible);
}

double c_api_getCanvasWidth(SBMLDocument* document, int layoutIndex) {
Expand Down Expand Up @@ -2422,6 +2429,14 @@ namespace LIBSBMLNETWORK_CPP_NAMESPACE {
return setGeometricShapeType(document, getGraphicalObject(document, layoutIndex, id, graphicalObjectIndex), shape);
}

const char* c_api_getGeometricShapeId(SBMLDocument* document, const char* id, int geometricShapeIndex, int graphicalObjectIndex, int layoutIndex) {
return strdup(getGeometricShapeId(document, getGraphicalObject(document, layoutIndex, id, graphicalObjectIndex), geometricShapeIndex).c_str());
}

int c_api_setGeometricShapeId(SBMLDocument* document, const char* id, const char* geometricShapeId, int geometricShapeIndex, int graphicalObjectIndex, int layoutIndex) {
return setGeometricShapeId(document, getGraphicalObject(document, layoutIndex, id, graphicalObjectIndex), geometricShapeIndex, geometricShapeId);
}

const char* c_api_getCompartmentsGeometricShapeType(SBMLDocument* document) {
return strdup(getCompartmentGeometricShapeType(document).c_str());
}
Expand Down
23 changes: 21 additions & 2 deletions src/c_api/libsbmlnetwork_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ namespace LIBSBMLNETWORK_CPP_NAMESPACE {

/// @brief Hide the SpeciesGlyphs with the given species Ids in the Layout object with the given index in the ListOfLayouts of the SBML document.
/// @param document a pointer to the SBMLDocument object.
/// @param speciesIds an array of strings containing the ids of the species to hide the SpeciesGlyphs for.
/// @param species an array of strings containing the ids of the species, ids of the reactions, and the indices of the reaction glyphs that the species are participants of.
/// @param speciesIdsSize the size of speciesIds
/// @param visible a boolean value to determine whether make SpeciesGlyph visible or invisible.
/// @param layoutIndex the index number of the Layout to return.
/// @return integer value indicating success/failure of the function.
LIBSBMLNETWORK_EXTERN int c_api_makeSpeciesGlyphsVisible(SBMLDocument* document, const char** speciesIds, const int speciesIdsSize, bool visible = true, int layoutIndex = 0);
LIBSBMLNETWORK_EXTERN int c_api_makeSpeciesGlyphsVisible(SBMLDocument* document, const char ***species, const int speciesSize, const bool visible, int layoutIndex);

/// @brief Returns the value of the "width" attribute of the Dimensions object of the Layout object
/// with the given index in the ListOfLayouts of the SBML document.
Expand Down Expand Up @@ -4765,6 +4765,25 @@ namespace LIBSBMLNETWORK_CPP_NAMESPACE {
/// @return integer value indicating success/failure of the function.
LIBSBMLNETWORK_EXTERN int c_api_setGeometricShapeType(SBMLDocument* document, const char* id, const char* shape, int graphicalObjectIndex = 0, int layoutIndex = 0);

/// @brief Returns the id of the geometric shape of the RenderGroup of the Style that matches this id of model entity associated with GraphicalObject.
/// @param document a pointer to the SBMLDocument object.
/// @param id the id of a model entity.
/// @param geometricShapeIndex an int representing the index of the Transformation2D to retrieve.
/// @param graphicalObjectIndex the index of the GraphicalObject to return.
/// @param layoutIndex the index number of the Layout to return.
/// @return the id of the geometric shape of the RenderGroup of the Style for this GraphicalObject, or @c "" if the object is @c NULL
LIBSBMLNETWORK_EXTERN const char* c_api_getGeometricShapesId(SBMLDocument* document, const char* id, int geometricShapeIndex = 0, int graphicalObjectIndex = 0, int layoutIndex = 0);

/// @brief Sets the id of the geometric shape of the RenderGroup of the Style that matches this id of model entity associated with GraphicalObject.
/// @param document a pointer to the SBMLDocument object.
/// @param id the id of a model entity.
/// @param geometricShapeId a string value indicating the id of the geometric shape to be set.
/// @param geometricShapeIndex an int representing the index of the Transformation2D to retrieve.
/// @param graphicalObjectIndex the index of the GraphicalObject to return.
/// @param layoutIndex the index number of the Layout to return.
/// @return integer value indicating success/failure of the function.
LIBSBMLNETWORK_EXTERN int c_api_setGeometricShapesId(SBMLDocument* document, const char* id, const char* geometricShapeId, int geometricShapeIndex = 0, int graphicalObjectIndex = 0, int layoutIndex = 0);

/// @brief Sets the geometric shape as the single geometric shape of the RenderGroup of the Style of all SpeciesGlyph objects in this Layout object.
/// @param document a pointer to the SBMLDocument object.
/// @param shape a string value indicating the shape of the geometric shape to be set.
Expand Down
6 changes: 2 additions & 4 deletions src/libsbmlnetwork_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1131,10 +1131,8 @@ Curve* getCurve(GraphicalObject* graphicalObject) {
}

int removeCurve(GraphicalObject* graphicalObject) {
if (isReactionGlyph(graphicalObject)) {
removeReactionGlyphCurve((ReactionGlyph *) graphicalObject);
return 0;
}
if (isReactionGlyph(graphicalObject))
return removeReactionGlyphCurve((ReactionGlyph*)graphicalObject);

return -1;
}
Expand Down
Loading

0 comments on commit 091479a

Please sign in to comment.