Skip to content

Commit

Permalink
added clade_color and clade_shape to cytoscape json
Browse files Browse the repository at this point in the history
  • Loading branch information
proost committed Sep 20, 2017
1 parent c343474 commit 136c5d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
17 changes: 16 additions & 1 deletion planet/helpers/cytoscape.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from planet.models.relationships.sequence_interpro import SequenceInterproAssociation
from planet.models.sequences import Sequence
from planet.models.species import Species
from utils.color import family_to_shape_and_color
from planet.models.clades import Clade
from utils.color import family_to_shape_and_color, index_to_shape_and_color


class CytoscapeHelper:
Expand Down Expand Up @@ -76,15 +77,24 @@ def add_family_data_nodes(network, family_method_id):

data = {}

clades = Clade.query.order_by(Clade.species_count).all()
clade_list = [c.name for c in clades]

for s in sequence_families:
data[s.sequence_id] = {}
data[s.sequence_id]["name"] = s.family.name
data[s.sequence_id]["id"] = s.gene_family_id
data[s.sequence_id]["url"] = url_for('family.family_view', family_id=s.gene_family_id)
if s.family.clade is not None:
clade_index = clade_list.index(s.family.clade.name)
color, shape = index_to_shape_and_color(clade_index)
data[s.sequence_id]["clade_color"] = color
data[s.sequence_id]["clade_shape"] = shape
data[s.sequence_id]["clade"] = s.family.clade.name
data[s.sequence_id]["clade_count"] = s.family.clade.species_count
else:
data[s.sequence_id]["clade_color"] = "#CCC"
data[s.sequence_id]["clade_shape"] = "rectangle"
data[s.sequence_id]["clade"] = "None"
data[s.sequence_id]["clade_count"] = 0

Expand All @@ -111,13 +121,18 @@ def add_family_data_nodes(network, family_method_id):
node["data"]["family_id"] = data[node["data"]["gene_id"]]["id"]
node["data"]["family_url"] = data[node["data"]["gene_id"]]["url"]
node["data"]["family_clade"] = data[node["data"]["gene_id"]]["clade"]
node["data"]["family_clade_color"] = data[node["data"]["gene_id"]]["clade_color"]
node["data"]["family_clade_shape"] = data[node["data"]["gene_id"]]["clade_shape"]
node["data"]["family_clade_count"] = data[node["data"]["gene_id"]]["clade_count"]
else:
node["data"]["family_name"] = None
node["data"]["family_id"] = None
node["data"]["family_url"] = None
node["data"]["family_color"] = "#CCC"
node["data"]["family_shape"] = "rectangle"

node["data"]["family_clade_color"] = "#CCC"
node["data"]["family_clade_shape"] = "rectangle"
node["data"]["family_clade"] = "None"
node["data"]["family_clade_count"] = 1

Expand Down
16 changes: 16 additions & 0 deletions utils/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ def label_coocurrence(ListOfListOfLabels):
return lc


def index_to_shape_and_color(index):
"""
Returns a tuple (color, shape) from an index
:param index: integer number
:return: tuple (color, shape)
"""

color_index = index % len(__COLORS)
shape_index = index // len(__COLORS)
shape_index = shape_index if shape_index < len(__SHAPES) else 0

return __COLORS[color_index], __SHAPES[shape_index]


def family_to_shape_and_color(input_dictionary):
"""
Takes a dictionary, where key:gene ID, value: ["fam1", "fam2",...]
Expand Down

0 comments on commit 136c5d3

Please sign in to comment.