1
- from collections import deque , defaultdict
1
+ from collections import defaultdict
2
2
from pathlib import Path
3
- from typing import Dict , Set
3
+ from typing import Dict , FrozenSet
4
4
5
5
import dendropy
6
+ import seaborn as sns
6
7
from gtdblib .file .itol .collapse import iTolCollapseFile
7
8
from gtdblib .file .itol .dataset_color_strip import iTolDatasetColorStripFile
8
9
from gtdblib .file .itol .label import iTolLabelFile
11
12
from gtdblib .taxonomy .taxonomy import Taxonomy
12
13
from gtdblib .util .color import TABLEAU_20 , rgb_to_hex
13
14
14
- from gtdb_itol_decorate .newick import parse_label
15
-
16
- import seaborn as sns
17
-
18
- from gtdb_itol_decorate .util import canonical_gid
19
-
20
-
21
- def get_lca_str (node : dendropy .Node ):
22
- if node .is_leaf ():
23
- return node .taxon .label
24
- if len (node .child_nodes ()) < 2 :
25
- return node .child_nodes ()[0 ].leaf_nodes ()[0 ].taxon .label
26
- left = node .child_nodes ()[0 ].leaf_nodes ()[0 ]
27
- right = node .child_nodes ()[1 ].leaf_nodes ()[0 ]
28
- return f'{ left .taxon .label } |{ right .taxon .label } '
29
-
15
+ from gtdb_itol_decorate .gtdb import remove_polyphyletic_suffix
16
+ from gtdb_itol_decorate .newick import get_lca_str
30
17
31
18
32
- def get_phylum_colours (d_phylum_to_lca ):
33
- """Generate a list of colours for each phylum. Order is inferred through a
34
- depth first search, to ensure no clade colours are side by side.
19
+ def get_phylum_colours (phyla : FrozenSet [str ]):
20
+ """Generate a list of colours for each phylum.
35
21
"""
36
22
colours = TABLEAU_20
37
23
24
+ phyla_mono = {remove_polyphyletic_suffix (x ) for x in phyla }
38
25
d_phylum_to_colour = dict ()
39
- for phylum in d_phylum_to_lca . keys () :
26
+ for phylum in phyla_mono :
40
27
d_phylum_to_colour [phylum ] = colours [len (d_phylum_to_colour ) % len (colours )]
41
28
42
29
# Generate a colour palette for each phylum (increasing brightness)
43
30
out = dict ()
44
- for phylum , colour in d_phylum_to_colour .items ():
31
+ for phylum in phyla :
32
+ colour = d_phylum_to_colour [remove_polyphyletic_suffix (phylum )]
45
33
cur_pal = sns .light_palette (colour , 6 , reverse = True )
46
34
out [phylum ] = [rgb_to_hex (* [round (y * 255 ) for y in x ]) for x in cur_pal ]
47
35
return out
@@ -62,7 +50,6 @@ def get_phylum_to_lca(tree: dendropy.Tree):
62
50
return out
63
51
64
52
65
-
66
53
def write_color_datastrip (d_phylum_to_lca , d_phylum_palette , path ):
67
54
file = iTolDatasetColorStripFile (path , 'Phylum Labels' , '#000000' , strip_width = 100 , show_internal = True )
68
55
@@ -83,7 +70,8 @@ def get_internal_nodes_with_labels(tree: dendropy.Tree):
83
70
out [';' .join (node .tax_label )].append (get_lca_str (node ))
84
71
return out
85
72
86
- def write_internal_node_labels ( d_label_to_lca ,path : Path ):
73
+
74
+ def write_internal_node_labels (d_label_to_lca , path : Path ):
87
75
file = iTolLabelFile (path )
88
76
89
77
# 1. Add internal node labels (trivial)
@@ -93,7 +81,6 @@ def write_internal_node_labels( d_label_to_lca,path: Path):
93
81
file .write ()
94
82
95
83
96
-
97
84
def write_tree_colours (tree , d_taxon_to_phylum , path , d_color_palette ):
98
85
file = iTolTreeColorsFile (path )
99
86
@@ -122,7 +109,6 @@ def write_tree_colours(tree, d_taxon_to_phylum, path, d_color_palette):
122
109
file .write ()
123
110
124
111
125
-
126
112
def write_collapse_file (d_int_label_to_lca , path , taxon_prefix ):
127
113
file = iTolCollapseFile (path )
128
114
for taxon , lst_lr_nodes in d_int_label_to_lca .items ():
@@ -132,6 +118,7 @@ def write_collapse_file(d_int_label_to_lca, path, taxon_prefix):
132
118
file .insert (lr_node )
133
119
file .write ()
134
120
121
+
135
122
def write_popup_file (tree : dendropy .Tree , d_tax : Dict [str , Taxonomy ], path : Path ):
136
123
file = iTolPopupFile (path )
137
124
@@ -153,4 +140,4 @@ def write_popup_file(tree: dendropy.Tree, d_tax: Dict[str, Taxonomy], path: Path
153
140
file .insert (gid , row .s .value , '' .join (lines ))
154
141
155
142
file .write ()
156
- return
143
+ return
0 commit comments