Skip to content

Commit

Permalink
Merge pull request #74 from PyEED/docker-stable-network
Browse files Browse the repository at this point in the history
Docker stable network
  • Loading branch information
NiklasAbraham authored May 10, 2024
2 parents 8592e3c + 87150bc commit 4bd4f05
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pyeed/core/alignmentdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class AlignmentData(

_repo: Optional[str] = PrivateAttr(default="https://github.com/PyEED/pyeed")
_commit: Optional[str] = PrivateAttr(
default="a7defc5c87a2296a2e4b522b07236b2aef6413ac"
default="0314c4c2dab01c5e5e941204f479917b21b585d4"
)

_raw_xml_data: Dict = PrivateAttr(default_factory=dict)
Expand Down
2 changes: 1 addition & 1 deletion pyeed/core/clustalomegadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ClustalOmegaData(

_repo: Optional[str] = PrivateAttr(default="https://github.com/PyEED/pyeed")
_commit: Optional[str] = PrivateAttr(
default="a7defc5c87a2296a2e4b522b07236b2aef6413ac"
default="0314c4c2dab01c5e5e941204f479917b21b585d4"
)

_raw_xml_data: Dict = PrivateAttr(default_factory=dict)
Expand Down
2 changes: 1 addition & 1 deletion pyeed/core/pairwisealignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class PairwiseAlignment(

_repo: Optional[str] = PrivateAttr(default="https://github.com/PyEED/pyeed")
_commit: Optional[str] = PrivateAttr(
default="a7defc5c87a2296a2e4b522b07236b2aef6413ac"
default="0314c4c2dab01c5e5e941204f479917b21b585d4"
)

_raw_xml_data: Dict = PrivateAttr(default_factory=dict)
Expand Down
59 changes: 38 additions & 21 deletions pyeed/network/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ class Config:
default=None,
)

_base_url: Optional[str] = PrivateAttr(
default="http://127.0.0.1:1234/v1",
)

def __init__(
self,
sequences: List[SequenceRecord],
weight: str = "identity",
dimensions: int = 3,
mode = "global"
mode = "global",
base_url: str = "http://127.0.0.1:1234/v1",
):
super().__init__()
self.weight = weight
Expand All @@ -84,6 +89,7 @@ def __init__(
self.sequences = sequences
self.network = nx.Graph()
self._aligner = PairwiseAligner(mode=self.mode)
self._base_url = base_url

self._create_graph()

Expand Down Expand Up @@ -194,20 +200,18 @@ def create_cytoscape_graph(
self, collection: str, title: str, threshold: float = 0.8
):
# assert that the cytoscape API is running and cytoscape is running in the background
assert p4c.cytoscape_ping(), "Cytoscape is not running in the background"
assert p4c.cytoscape_version_info(), "Cytoscape API is not running"
assert p4c.cytoscape_ping(base_url=self._base_url), "Cytoscape is not running in the background"
assert p4c.cytoscape_version_info(base_url=self._base_url), "Cytoscape API is not running"
# create a degree column for the nodes based on the current choosen threshold
self.calculate_degree(threshold=threshold)
# filter the the edges by the threshold
p4c.create_network_from_networkx(
self.network, collection=collection, title=title + "_" + str(threshold)
self.network, collection=collection, title=title + "_" + str(threshold), base_url=self._base_url
)

self.filter_cytoscape_edges_by_parameter(
name="threshold", parameter="identity", min_val=threshold, max_val=1.0
)
self.hide_under_threshold(threshold)
# and yes the layout ignores hidden edges, i did a visual test
p4c.layout_network('grid')
p4c.layout_network('grid', base_url=self._base_url)

def set_layout(
self,
Expand All @@ -219,14 +223,26 @@ def set_layout(
"numIterations": 50,
},
):
p4c.layout_network(layout_name)
p4c.layout_network(layout_name, base_url=self._base_url)
# ['numIterations', 'defaultSpringCoefficient', 'defaultSpringLength', 'defaultNodeMass', 'isDeterministic', 'singlePartition']
p4c.set_layout_properties(
layout_name=layout_name, properties_dict=properties_dict
layout_name=layout_name, properties_dict=properties_dict, base_url=self._base_url
)

p4c.scale_layout(axis="Both Axis", scale_factor=1.0)
p4c.layout_network(layout_name)
p4c.scale_layout(axis="Both Axis", scale_factor=1.0, base_url=self._base_url)
p4c.layout_network(layout_name, base_url=self._base_url)

def hide_under_threshold(self, threshold):
p4c.unhide_all(base_url=self._base_url)

hide_list = []

for u,v,d in self.network.edges(data=True):
if d['identity'] > threshold:
hide_list.append('{} (interacts with) {}'.format(u, v))

p4c.hide_edges(hide_list, base_url=self._base_url)


def filter_cytoscape_edges_by_parameter(self, name: str, parameter: str, min_val: float, max_val: float):
# this is a filter for the network in cytoscape
Expand All @@ -239,6 +255,7 @@ def filter_cytoscape_edges_by_parameter(self, name: str, parameter: str, min_val
type="edges",
apply=True,
hide=True,
base_url=self._base_url,
)

def calculate_degree(self, threshold: float = 0.8):
Expand All @@ -259,13 +276,13 @@ def calculate_degree(self, threshold: float = 0.8):
nx.set_node_attributes(self.network, degree, "degree_with_threshold_{}".format(threshold))

def set_nodes_size(self, column_name: str, min_size: int = 10, max_size: int = 100, style_name: str = "default"):
p4c.set_node_shape_default('ELLIPSE', style_name)
p4c.set_node_size_mapping(**gen_node_size_map(column_name, scheme_c_number_continuous(min_size, max_size), mapping_type='c', style_name=style_name))
p4c.set_node_label_mapping('name', style_name=style_name)
p4c.set_node_font_size_mapping(**gen_node_size_map(column_name, scheme_c_number_continuous(int(min_size/10), int(max_size/10)), style_name=style_name))
p4c.set_node_shape_default('ELLIPSE', style_name, base_url=self._base_url)
p4c.set_node_size_mapping(**gen_node_size_map(column_name, scheme_c_number_continuous(min_size, max_size), mapping_type='c', style_name=style_name, base_url=self._base_url))
p4c.set_node_label_mapping('name', style_name=style_name, base_url=self._base_url)
p4c.set_node_font_size_mapping(**gen_node_size_map(column_name, scheme_c_number_continuous(int(min_size/10), int(max_size/10)), style_name=style_name, base_url=self._base_url))

def color_nodes(self, column_name: str, style_name: str = "default"):
df_nodes = p4c.get_table_columns(table='node')
df_nodes = p4c.get_table_columns(table='node', base_url=self._base_url)

data_color_names = list(set(df_nodes[column_name]))

Expand All @@ -274,11 +291,11 @@ def color_nodes(self, column_name: str, style_name: str = "default"):
# Convert RGB to hex colors for py4cytoscape
hex_colors = ['#' + ''.join([f'{int(c*255):02x}' for c in color[:3]]) for color in colors]

if style_name not in p4c.get_visual_style_names():
p4c.create_visual_style(style_name)
if style_name not in p4c.get_visual_style_names(base_url=self._base_url):
p4c.create_visual_style(style_name, base_url=self._base_url)

p4c.set_node_color_default('#FFFFFF', style_name)
p4c.set_node_color_mapping(column_name, mapping_type='discrete', default_color='#654321', style_name=style_name, table_column_values=data_color_names, colors=hex_colors)
p4c.set_node_color_default('#FFFFFF', style_name, base_url=self._base_url)
p4c.set_node_color_mapping(column_name, mapping_type='discrete', default_color='#654321', style_name=style_name, table_column_values=data_color_names, colors=hex_colors, base_url=self._base_url)

def visualize(self):
"""
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ rich = "^13.0.0"
aiometer = "^0.5.0"
black = "23.12.1"
joblib = "^1.4.0"
python-igraph = "^0.11.5"
requests = "^2.31.0"
pandas = "^2.2.2"
py4cytoscape = "^1.9.0"
sdrdm = {git = "https://github.com/JR-1991/software-driven-rdm.git", rev = "63-terms-and-external-model-handling"}
joblib-progress = "^1.0.5"
Expand Down

0 comments on commit 4bd4f05

Please sign in to comment.