Skip to content

Commit

Permalink
added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
SotirisTouliopoulos committed Aug 18, 2024
1 parent 4aed30d commit 70440bf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
21 changes: 20 additions & 1 deletion dingo/illustrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ def plot_corr_matrix(corr_matrix, reactions, removed_reactions=[], format="svg")


def plot_dendrogram(dissimilarity_matrix, reactions , plot_labels=False, t=2.0, linkage="ward"):
"""A Python function to plot the dendrogram of a dissimilarity matrix.
Keyword arguments:
dissimilarity_matrix -- A matrix produced from the "cluster_corr_reactions" function
reactions -- A list with the model's reactions
plot_labels -- A boolean variable that if True plots the reactions labels in the dendrogram
t -- A threshold that defines a threshold that cuts the dendrogram
at a specific height and colors the occuring clusters accordingly
linkage -- linkage defines the type of linkage.
Available linkage types are: single, average, complete, ward.
"""

fig = ff.create_dendrogram(dissimilarity_matrix,
labels=reactions,
linkagefun=lambda x: hierarchy.linkage(x, linkage),
Expand All @@ -160,6 +172,13 @@ def plot_dendrogram(dissimilarity_matrix, reactions , plot_labels=False, t=2.0,


def plot_graph(G, pos):
"""A Python function to plot a graph created from a correlation matrix.
Keyword arguments:
G -- A graph produced from the "graph_corr_matrix" function.
pos -- A layout for the corresponding graph.
"""

fig = go.Figure()

for u, v, data in G.edges(data=True):
Expand All @@ -182,7 +201,7 @@ def plot_graph(G, pos):
text=[node_name],
textposition='top center',
name = node_name,
showlegend=True))
showlegend=False))

fig.update_layout(width=800, height=800)
fig.show()
63 changes: 42 additions & 21 deletions dingo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,21 @@ def correlated_reactions(steady_states, reactions=[], pearson_cutoff = 0.90, ind



def cluster_corr_reactions(correlation_matrix, reactions, method,
linkage="ward", t = 4.0,
min_cluster_size = 2, n_clusters=8):

def cluster_corr_reactions(correlation_matrix, reactions, linkage="ward",
t = 4.0, correction=True):
"""A Python function for hierarchical clustering of the correlation matrix
Keyword arguments:
correlation_matrix -- A numpy 2D array of a correlation matrix
reactions -- A list with the model's reactions
linkage -- linkage defines the type of linkage.
Available linkage types are: single, average, complete, ward.
t -- A threshold that defines a threshold that cuts the dendrogram
at a specific height and produces clusters
correction -- A boolean variable that if True converts the values of the
the correlation matrix to absolute values.
"""

# function to return a nested list with grouped reactions based on clustering
def clusters_list(reactions, labels):
clusters = []
Expand All @@ -362,29 +373,39 @@ def clusters_list(reactions, labels):
clusters.append(cluster)
return clusters

if method == "hierarchical":
dissimilarity_matrix = 1 - abs(correlation_matrix)
Z = hierarchy.linkage(dissimilarity_matrix, linkage)
labels = hierarchy.fcluster(Z, t, criterion='distance')

clusters = clusters_list(reactions, labels)
return dissimilarity_matrix, labels, clusters
if correction == True:
dissimilarity_matrix = 1 - abs(correlation_matrix)
else:
dissimilarity_matrix = 1 - correlation_matrix
Z = hierarchy.linkage(dissimilarity_matrix, linkage)
labels = hierarchy.fcluster(Z, t, criterion='distance')

clusters = clusters_list(reactions, labels)
return dissimilarity_matrix, labels, clusters



def graph_corr_matrix(correlation_matrix, reactions, correction=True,
clusters=[], subgraph_nodes = 5):
"""A Python function that creates the main graph and its subgraphs
from a correlation matrix.
def graph_corr_matrix(correlation_matrix, reactions, correction=True, clusters=[]):
Keyword arguments:
correlation_matrix -- A numpy 2D array of a correlation matrix.
reactions -- A list with the model's reactions.
correction -- A boolean variable that if True converts the values of the
the correlation matrix to absolute values.
clusters -- A nested list with clustered reactions created from the "" function.
subgraph_nodes -- A variable that specifies a cutoff for a graph's nodes.
It filters subgraphs with low number of nodes..
"""

graph_matrix = correlation_matrix.copy()
np.fill_diagonal(graph_matrix, 0)

if correction == True:
for cluster in clusters:
for reaction_a in cluster:
for reaction_b in cluster:
reaction_a_index = reactions.index(reaction_a)
reaction_b_index = reactions.index(reaction_b)
graph_matrix[reaction_a_index, reaction_b_index] = abs(graph_matrix[reaction_a_index, reaction_b_index])
graph_matrix[reaction_b_index, reaction_a_index] = abs(graph_matrix[reaction_b_index, reaction_a_index])
graph_matrix = abs(graph_matrix)

G = nx.from_numpy_array(graph_matrix)
G = nx.relabel_nodes(G, lambda x: reactions[x])
Expand All @@ -393,7 +414,6 @@ def graph_corr_matrix(correlation_matrix, reactions, correction=True, clusters=[
unconnected_nodes = list(nx.isolates(G))
G.remove_nodes_from(unconnected_nodes)
G_nodes = G.nodes()
print(G_nodes)

graph_list = []
layout_list = []
Expand All @@ -405,11 +425,12 @@ def graph_corr_matrix(correlation_matrix, reactions, correction=True, clusters=[
H_nodes_list = []

for i in range(len(subgraphs)):
if len(subgraphs[i].nodes()) > 4 and len(subgraphs[i].nodes()) != len(G_nodes):
if len(subgraphs[i].nodes()) > subgraph_nodes and len(subgraphs[i].nodes()) != len(G_nodes):
H = G.subgraph(subgraphs[i].nodes())
for cluster in clusters:
if H.has_node(cluster[0]) and H.nodes() not in H_nodes_list:
H_nodes_list.append(H.nodes())

pos = nx.spring_layout(H)
graph_list.append(H)
layout_list.append(pos)
Expand Down

0 comments on commit 70440bf

Please sign in to comment.