Coverage for /home/deng/Projects/ete4/hackathon/ete4/ete4/smartview/renderer/layouts/spongilla_layouts.py: 28%
124 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-03-21 09:19 +0100
« prev ^ index » next coverage.py v7.2.7, created at 2024-03-21 09:19 +0100
1from ..treelayout import TreeLayout
2from ..faces import RectFace, TextFace
3from collections import OrderedDict
4import json
5from pathlib import Path
9__all__ = [ "LayoutSciName", "LayoutPreferredName", "LayoutAutoName",
10 "LayoutCuratedName", "LayoutEukOgs", "LayoutSeeds" ]
13sciName2color = {}
14taxid2color = {}
15try:
16 with open(Path(__file__).parent / 'spongilla_taxa_color_codes.csv') as t:
17 for line in t:
18 if not line.startswith('#'):
19 info = line.split('\t')
20 sciName2color[(info[0])] = info[3].strip()
21 taxid2color[int(info[1])] = info[3].strip()
22except:
23 pass
26def summary(nodes):
27 "Return a list of names summarizing the given list of nodes"
28 return list(OrderedDict((first_name(node), None) for node in nodes).keys())
30def first_name(tree):
31 "Return the name of the first node that has a name"
33 sci_names = []
34 for node in tree.traverse('preorder'):
35 if node.is_leaf:
36 sci_name = node.props.get('sci_name')
37 sci_names.append(sci_name)
39 return next(iter(sci_names))
43class LayoutSciName(TreeLayout):
44 def __init__(self, name="Scientific name"):
45 super().__init__(name, aligned_faces=True)
47 def set_node_style(self, node):
48 if node.is_leaf:
50 sci_name = node.props.get('sci_name')
51 prot_id = node.name.split('.', 1)[1]
53 if node.props.get('sci_name') in sciName2color.keys():
54 color = sciName2color[node.props.get('sci_name')]
55 else:
56 color = 'black'
58 node.add_face(TextFace(sci_name, color = color, padding_x=2),
59 column=0, position="branch_right")
61 if len(prot_id) > 40:
62 prot_id = prot_id[0:37] + " ..."
64 node.add_face(TextFace(prot_id, color = 'Gray', padding_x=2), column = 2, position = "aligned")
67 else:
68 # Collapsed face
69 names = summary(node.children)
70 texts = names if len(names) < 6 else (names[:3] + ['...'] + names[-2:])
71 for i, text in enumerate(texts):
73 if text in sciName2color.keys():
74 color = sciName2color[text]
75 else:
76 color = 'black'
77 node.add_face(TextFace(text, padding_x=2, color = color),
78 position="branch_right", column=1, collapsed_only=True)
82class LayoutPreferredName(TreeLayout):
84 def __init__(self, name="Preferred name", text_color="#fb3640"):
85 super().__init__(name, aligned_faces=True)
87 self.text_color = text_color
89 def set_node_style(self, node):
90 if node.is_leaf:
91 if node.props.get('Pname'):
92 pname= node.props.get('Pname')
93 pname_face = TextFace(pname, color=self.text_color)
94 node.add_face(pname_face, column = 3, position = "aligned")
95 else:
96 target_leaf = node.get_leaves()[0]
97 if target_leaf.props.get('Pname'):
98 pname= target_leaf.props.get('Pname')
99 pname_face = TextFace(pname, color=self.text_color)
100 node.add_face(pname_face, column = 3, position = "aligned", collapsed_only=True)
104class LayoutAutoName(TreeLayout):
106 def __init__(self, name="Auto name", text_color="grey"):
107 super().__init__(name, aligned_faces=True)
109 self.text_color = text_color
111 def set_node_style(self, node):
113 if node.is_leaf:
114 if node.props.get('auto_name'):
115 spongAutoName = " ".join(node.props.get('auto_name').split("_"))
116 if len(spongAutoName) > 30:
117 spongAutoName = spongAutoName[0:27] + " ..."
118 spongAutoName_face = TextFace(spongAutoName, color=self.text_color)
119 node.add_face(spongAutoName_face, column = 4, position = "aligned")
120 else:
121 target_leaf = node.get_leaves()[0]
122 if target_leaf.props.get('auto_name'):
123 spongAutoName = " ".join(target_leaf.props.get('auto_name').split("_"))
124 if len(spongAutoName) > 30:
125 spongAutoName = spongAutoName[0:27] + " ..."
126 spongAutoName_face = TextFace(spongAutoName, color=self.text_color)
127 node.add_face(spongAutoName_face, column = 4, position = "aligned", collapsed_only=True)
131class LayoutCuratedName(TreeLayout):
133 def __init__(self, name="Preferred name", text_color="black"):
134 super().__init__(name, aligned_faces=True)
136 self.text_color = text_color
138 def set_node_style(self, node):
139 if node.is_leaf:
140 if node.props.get('curated_name') and node.props.get('curated_name') != 'NA':
141 spongCuratedName = " ".join(node.props.get('curated_name').split("_"))
142 if len(spongCuratedName) > 30:
143 spongCuratedName = spongCuratedName[0:27] + " ..."
144 spongCuratedName_face = TextFace(spongCuratedName, color=self.text_color)
145 node.add_face(spongCuratedName_face, column = 5, position = "aligned")
146 else:
147 target_leaf = node.get_leaves()[0]
148 if target_leaf.props.get('curated_name') and target_leaf.props.get('curated_name') != 'NA':
149 spongCuratedName = " ".join(target_leaf.props.get('curated_name').split("_"))
150 if len(spongCuratedName) > 30:
151 spongCuratedName = spongCuratedName[0:27] + " ..."
152 spongCuratedName_face = TextFace(spongCuratedName, color=self.text_color)
153 node.add_face(spongCuratedName_face, column = 5, position = "aligned", collapsed_only=True)
157class LayoutEukOgs(TreeLayout):
159 def __init__(self, name="OGs euk", text_color="grey"):
160 super().__init__(name, aligned_faces=True)
162 self.text_color = text_color
164 def set_node_style(self, node):
166 if node.is_leaf:
167 if node.props.get('OG_euk'):
168 OG = node.props.get('OG_euk')
169 og_face = TextFace(OG, color=self.text_color)
170 node.add_face(og_face, column = 6, position = "aligned")
171 else:
172 target_leaf = node.get_leaves()[0]
173 if target_leaf.props.get('OG_euk'):
174 OG = target_leaf.props.get('OG_euk')
175 og_face = TextFace(OG, color=self.text_color)
176 node.add_face(og_face, column = 6, position = "aligned", collapsed_only=True)
180class LayoutSeeds(TreeLayout):
182 def __init__(self, name="Seeds", text_color="grey"):
183 super().__init__(name)
186 def set_node_style(self, node):
188 if node.is_leaf:
189 if node.props.get('taxid') == '6055' and "seed" in node.props.keys():
190 node.sm_style["bgcolor"] = "#A3423C"
192 elif node.props.get('taxid') == '6055' :
193 node.sm_style["bgcolor"] = "#DE834D"