Skip to content

Commit

Permalink
graph comparison only showing genes in intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
proost committed Jan 25, 2018
1 parent b89ea04 commit 6df2725
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
17 changes: 14 additions & 3 deletions conekt/helpers/cytoscape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = []
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions conekt/templates/expression_graph.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ <h1>Co-expression cluster: <strong>{{ cluster.name }}</strong> <small>({{ cluste
<p><strong>View as: </strong>{{ ca.cluster_actions(cluster.id, exclude=["graph"]) }}</p>
{% elif cluster_one %}
<h1>Comparing clusters: <strong>{{ cluster_one.name }}</strong> vs <strong>{{ cluster_two.name }}</strong></h1>
<span class="text-muted">(<strong>Note:</strong> Only genes which have a homolog in another cluster are shown)</span>
{% elif sequence %}
<h1><abbr title="Click to show help" href="{{ url_for('help.help_topic', topic='ecc') }}" data-target="#helpModal">ECC</abbr> network: <strong>{{ sequence.name }}</strong></h1>
{% elif graph_data %}
Expand Down

0 comments on commit 6df2725

Please sign in to comment.