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

1from ..treelayout import TreeLayout 

2from ..faces import RectFace, TextFace 

3from collections import OrderedDict 

4import json 

5from pathlib import Path 

6 

7 

8 

9__all__ = [ "LayoutSciName", "LayoutPreferredName", "LayoutAutoName", 

10 "LayoutCuratedName", "LayoutEukOgs", "LayoutSeeds" ] 

11 

12 

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 

24 

25 

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()) 

29 

30def first_name(tree): 

31 "Return the name of the first node that has a name" 

32 

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) 

38 

39 return next(iter(sci_names)) 

40 

41 

42 

43class LayoutSciName(TreeLayout): 

44 def __init__(self, name="Scientific name"): 

45 super().__init__(name, aligned_faces=True) 

46 

47 def set_node_style(self, node): 

48 if node.is_leaf: 

49 

50 sci_name = node.props.get('sci_name') 

51 prot_id = node.name.split('.', 1)[1] 

52 

53 if node.props.get('sci_name') in sciName2color.keys(): 

54 color = sciName2color[node.props.get('sci_name')] 

55 else: 

56 color = 'black' 

57 

58 node.add_face(TextFace(sci_name, color = color, padding_x=2), 

59 column=0, position="branch_right") 

60 

61 if len(prot_id) > 40: 

62 prot_id = prot_id[0:37] + " ..." 

63 

64 node.add_face(TextFace(prot_id, color = 'Gray', padding_x=2), column = 2, position = "aligned") 

65 

66 

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): 

72 

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) 

79 

80 

81 

82class LayoutPreferredName(TreeLayout): 

83 

84 def __init__(self, name="Preferred name", text_color="#fb3640"): 

85 super().__init__(name, aligned_faces=True) 

86 

87 self.text_color = text_color 

88 

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) 

101 

102 

103 

104class LayoutAutoName(TreeLayout): 

105 

106 def __init__(self, name="Auto name", text_color="grey"): 

107 super().__init__(name, aligned_faces=True) 

108 

109 self.text_color = text_color 

110 

111 def set_node_style(self, node): 

112 

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) 

128 

129 

130 

131class LayoutCuratedName(TreeLayout): 

132 

133 def __init__(self, name="Preferred name", text_color="black"): 

134 super().__init__(name, aligned_faces=True) 

135 

136 self.text_color = text_color 

137 

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) 

154 

155 

156 

157class LayoutEukOgs(TreeLayout): 

158 

159 def __init__(self, name="OGs euk", text_color="grey"): 

160 super().__init__(name, aligned_faces=True) 

161 

162 self.text_color = text_color 

163 

164 def set_node_style(self, node): 

165 

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) 

177 

178 

179 

180class LayoutSeeds(TreeLayout): 

181 

182 def __init__(self, name="Seeds", text_color="grey"): 

183 super().__init__(name) 

184 

185 

186 def set_node_style(self, node): 

187 

188 if node.is_leaf: 

189 if node.props.get('taxid') == '6055' and "seed" in node.props.keys(): 

190 node.sm_style["bgcolor"] = "#A3423C" 

191 

192 elif node.props.get('taxid') == '6055' : 

193 node.sm_style["bgcolor"] = "#DE834D"