Skip to content

Commit

Permalink
rearrangeing code to prepare new graphs !
Browse files Browse the repository at this point in the history
  • Loading branch information
proost committed Nov 30, 2017
1 parent 4c88161 commit 29dc9d9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 29 deletions.
20 changes: 20 additions & 0 deletions conekt/controllers/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ def ecc_graph_pair_json(ecc_id):
"""
network, family = SequenceSequenceECCAssociation.get_ecc_pair_network(ecc_id)

network_cytoscape = CytoscapeHelper.parse_network(network)
network_cytoscape = CytoscapeHelper.add_descriptions_nodes(network_cytoscape)
network_cytoscape = CytoscapeHelper.add_family_data_nodes(network_cytoscape, family)
network_cytoscape = CytoscapeHelper.add_lc_data_nodes(network_cytoscape)
network_cytoscape = CytoscapeHelper.add_species_data_nodes(network_cytoscape)
network_cytoscape = CytoscapeHelper.connect_homologs(network_cytoscape)

return json.dumps(network_cytoscape)


@ecc.route('/multi_json/')
def ecc_graph_multi_json():
"""
Returns a JSON object compatible with cytoscape.js that contains the ECC graph for a specific pair of sequences
:param ecc_id: internal ID of the sequence to sequence ECC relationship
:return: JSON object compatible with cytoscape.js
"""
network, family = SequenceSequenceECCAssociation.get_ecc_multi_network(2, [230458, 35796, 162930])

network_cytoscape = CytoscapeHelper.parse_network(network)
network_cytoscape = CytoscapeHelper.add_descriptions_nodes(network_cytoscape)
network_cytoscape = CytoscapeHelper.add_family_data_nodes(network_cytoscape, family)
Expand Down
22 changes: 22 additions & 0 deletions conekt/helpers/cytoscape.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,28 @@ def add_depth_data_nodes(network):

return colored_network

@staticmethod
def connect_homologs(network):
connected_network = deepcopy(network)
"""
Add edges between homologous genes from different targets, family_id needs to be specified !
"""

for i in range(len(connected_network['nodes']) - 1):
for j in range(i + 1, len(connected_network['nodes'])):
if connected_network['nodes'][i]['data']['family_id'] == connected_network['nodes'][j]['data']['family_id'] and connected_network['nodes'][i]['data']['family_id'] is not None:
connected_network['edges'].append({
'data': {'source': connected_network['nodes'][i]['data']['id'],
'target': connected_network['nodes'][j]['data']['id'],
'color': "#33D",
'homology_color': "#33D",
'edge_type': 'homology',
'ecc_pair_color': "#33D",
'homology': True}
})

return connected_network

@staticmethod
def add_connection_data_nodes(network):
"""
Expand Down
71 changes: 43 additions & 28 deletions conekt/models/relationships/sequence_sequence_ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,32 +201,47 @@ def get_ecc_pair_network(ecc_id):
"edge_type": "expression",
'ecc_pair_color': "#3D3"})

"""
Add gene families to sequences
"""
seq_fams = SequenceFamilyAssociation.query.filter(and_(SequenceFamilyAssociation.sequence_id.in_(sequences),
SequenceFamilyAssociation.family.has(method_id=association.gene_family_method_id)
)).all()

seq_to_fam = {sf.sequence_id: sf.gene_family_id for sf in seq_fams}
return {"nodes": nodes, "edges": edges}, association.gene_family_method_id

for i, node in enumerate(nodes):
nodes[i]['family_id'] = seq_to_fam[node['gene_id']] if node['gene_id'] in seq_to_fam.keys() else None

"""
Add edges between homologous genes from different targets
"""

for i in range(len(nodes) - 1):
for j in range(i + 1, len(nodes)):
if nodes[i]['family_id'] == nodes[j]['family_id'] and nodes[i]['family_id'] is not None:
edges.append(
{'source': nodes[i]['id'],
'target': nodes[j]['id'],
'homology_color': "#33D",
'edge_type': 'homology',
'ecc_pair_color': "#33D",
'homology': True}
)

return {"nodes": nodes, "edges": edges}, association.gene_family_method_id
@staticmethod
def get_ecc_multi_network(gf_method_id, sequence_ids):
associations = SequenceSequenceECCAssociation.query.\
filter(SequenceSequenceECCAssociation.gene_family_method_id == gf_method_id).\
filter(and_(SequenceSequenceECCAssociation.query_id.in_(sequence_ids),
SequenceSequenceECCAssociation.target_id.in_(sequence_ids))).\
all()

nodes = []
edges = []
node_sequence_ids = []

for a in associations:
if a.query_id not in node_sequence_ids:
node_sequence_ids.append(a.query_id)
nodes.append({"id": a.query_sequence.name,
"name": a.query_sequence.name,
"species_id": a.query_sequence.species_id,
"species_name": a.query_sequence.species.name,
"gene_id": a.query_id,
"gene_name": a.query_sequence.name,
"network_method_id": a.query_network_method_id,
"node_type": "query"})

if a.target_id not in node_sequence_ids:
node_sequence_ids.append(a.target_id)
nodes.append({"id": a.target_sequence.name,
"name": a.target_sequence.name,
"species_id": a.target_sequence.species_id,
"species_name": a.target_sequence.species.name,
"gene_id": a.target_id,
"gene_name": a.target_sequence.name,
"network_method_id": a.target_network_method_id,
"node_type": "query"})

edges.append({"source": a.query_sequence.name,
"target": a.target_sequence.name,
"ecc_score": a.ecc,
'ecc_pair_color': "#D33",
"edge_type": "ecc"})

return {"nodes": nodes, "edges": edges}, gf_method_id
2 changes: 1 addition & 1 deletion conekt/templates/expression_graph.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ <h1>ECC pair</h1>
{% if sequence %}
<li><a href="#" class="cy-edge-width" attr="ecc_width">ECC</a></li>
{% endif %}
{% if not sequence %}
{% if not sequence and not ecc_pair %}
<li><hr /></li>
<li class="dropdown-header"><abbr title="Click to show help" href="{{ url_for('help.help_topic', topic='hrr') }}" data-target="#helpModal">HRR</abbr> Filter</li>
<li><p><strong>Cutoff:</strong> <span id="cy-edge-score-value" class="text-muted">{{cutoff}}</span></p></li>
Expand Down

0 comments on commit 29dc9d9

Please sign in to comment.