From 6df2725291a193a0b3d27cecc2d47679fbff4a2e Mon Sep 17 00:00:00 2001 From: Sebastian Proost Date: Thu, 25 Jan 2018 02:25:01 +0100 Subject: [PATCH] graph comparison only showing genes in intersection --- conekt/helpers/cytoscape.py | 17 ++++++++++++++--- conekt/templates/expression_graph.html | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/conekt/helpers/cytoscape.py b/conekt/helpers/cytoscape.py index c418aa8..edd7665 100644 --- a/conekt/helpers/cytoscape.py +++ b/conekt/helpers/cytoscape.py @@ -515,7 +515,7 @@ def tag_ecc_singles(network): return output_network @staticmethod - def merge_networks(network_one, network_two): + def merge_networks(network_one, network_two, prune=True): """ Function to merge two networks. A compound/parent node is created for each network and based on the family_id, edges between homologous/orthologous genes are added. @@ -524,6 +524,7 @@ def merge_networks(network_one, network_two): :param network_one: Dictionary (cytoscape.js structure) of the first network :param network_two: Dictionary (cytoscape.js structure) of the second network + :param prune: if True (will only retain nodes with a homolog in the other network :return: Cytoscape.js compatible network with both networks merged and homologs/orthologs connected """ nodes = [] @@ -542,17 +543,27 @@ def merge_networks(network_one, network_two): # draw edges between nodes from different networks # TODO: optimize this to avoid nested loop + + nodes_to_keep = ["compound_node_one", "compound_node_two"] # Nodes to keep when prune is enabled + for node_one in network_one["nodes"]: for node_two in network_two["nodes"]: # if nodes are from the same family add an edge between them if node_one["data"]["family_id"] is not None \ and node_one["data"]["family_id"] == node_two["data"]["family_id"]: + nodes_to_keep.append(node_one["data"]["id"]) + nodes_to_keep.append(node_two["data"]["id"]) edges.append({'data': {'source': node_one["data"]["id"], 'target': node_two["data"]["id"], 'color': "#33D", 'homology': True}}) - - return {'nodes': nodes, 'edges': edges} + if not prune: + return {'nodes': nodes, 'edges': edges} + else: + # Prune is enabled, only return nodes which are in both lists (and compound nodes) + nodes_to_keep = list(set(nodes_to_keep)) + return {'nodes': [n for n in nodes if n["data"]["id"] in nodes_to_keep], + 'edges': [e for e in edges if e["data"]["source"] in nodes_to_keep and e["data"]["target"]]} @staticmethod def get_families(network): diff --git a/conekt/templates/expression_graph.html b/conekt/templates/expression_graph.html index 1a9e166..a25a796 100644 --- a/conekt/templates/expression_graph.html +++ b/conekt/templates/expression_graph.html @@ -30,6 +30,7 @@

Co-expression cluster: {{ cluster.name }} ({{ cluste

View as: {{ ca.cluster_actions(cluster.id, exclude=["graph"]) }}

{% elif cluster_one %}

Comparing clusters: {{ cluster_one.name }} vs {{ cluster_two.name }}

+ (Note: Only genes which have a homolog in another cluster are shown) {% elif sequence %}

ECC network: {{ sequence.name }}

{% elif graph_data %}