1- import collections
1+ from collections import namedtuple
22import itertools
33import json
44import math
2323)
2424
2525
26- default_mutation_styles = {
26+ DEFAULT_MUTATION_STYLES = {
2727 "size" : 5 ,
2828 "unknown_time" : {
2929 "fill" : "gold" ,
4141 },
4242}
4343
44+ DEFAULT_D3JS_URL = "https://d3js.org/d3.v7.min"
45+
46+ D3jsSource = namedtuple ("D3jsSource" , ["url" , "inline_script" ])
47+ IncludedInfo = namedtuple ("IncludedInfo" , ["nodes" , "edges" , "mutations" , "breakpoints" ])
48+
4449@dataclass
4550class IncludedObjects :
4651 """Stores the IDs of the various objects included in a drawing."""
@@ -99,17 +104,12 @@ def running_in_notebook():
99104 return False # Probably standard Python interpreter
100105
101106
102- DEFAULT_D3JS_URL = "https://d3js.org/d3.v7.min"
103-
104- D3jsSource = collections .namedtuple ("D3jsSource" , ["url" , "inline_script" ])
105- """
106- Result of resolve_d3js_source. Exactly one field is meaningful:
107- - url is a string URL (and inline_script is ""), or
108- - url is None and inline_script is a "<script>…</script>" HTML fragment.
109- """
110-
111-
112- def resolve_d3js_source (d3js ):
107+ def resolve_d3js_source (d3js ) -> D3jsSource :
108+ """
109+ Returns a tuple in which exactly one field is meaningful:
110+ - url is a string URL (and inline_script is ""), or
111+ - url is None and inline_script is a "<script>…</script>" HTML fragment.
112+ """
113113 if d3js is None :
114114 return D3jsSource (url = DEFAULT_D3JS_URL , inline_script = "" )
115115 try :
@@ -691,12 +691,12 @@ def _convert_edges_table(ts, recombination_nodes_to_merge, progressbar=None):
691691 # an edge. Essentially, giving them a false time just for plotting.
692692 middle = (new_edge [3 ] + new_edge [4 ]) / 2
693693 plot_time = middle + random .uniform (- (new_edge [3 ]- middle ),(new_edge [3 ]- middle ))
694- fill = default_mutation_styles ["unknown_time" ]["fill" ]
695- stroke = default_mutation_styles ["unknown_time" ]["stroke" ]
694+ fill = DEFAULT_MUTATION_STYLES ["unknown_time" ]["fill" ]
695+ stroke = DEFAULT_MUTATION_STYLES ["unknown_time" ]["stroke" ]
696696 else :
697697 plot_time = mut .time
698- fill = default_mutation_styles ["known_time" ]["fill" ]
699- stroke = default_mutation_styles ["known_time" ]["stroke" ]
698+ fill = DEFAULT_MUTATION_STYLES ["known_time" ]["fill" ]
699+ stroke = DEFAULT_MUTATION_STYLES ["known_time" ]["stroke" ]
700700 inherited_state = site .ancestral_state if mut .parent == tskit .NULL else ts .mutation (mut .parent ).derived_state
701701 mutations .append ({
702702 "edge" : new_edge [0 ],
@@ -712,7 +712,7 @@ def _convert_edges_table(ts, recombination_nodes_to_merge, progressbar=None):
712712 "derived" : mut .derived_state ,
713713 "fill" : fill ,
714714 "stroke" : stroke ,
715- "size" : default_mutation_styles ["size" ],
715+ "size" : DEFAULT_MUTATION_STYLES ["size" ],
716716 })
717717 mutations_output = pd .DataFrame (mutations , columns = ["edge" ,"source" ,"target" ,"time" ,"plot_time" ,"site_id" ,"position" ,"position_01" ,"ancestral" ,"inherited" ,"derived" ,"fill" ,"stroke" ,"size" ])
718718 return edges_output , mutations_output
@@ -865,11 +865,11 @@ def reset_all_mutation_styles(self):
865865 """Resets mutation styles to default
866866 """
867867 is_unknown = tskit .is_unknown_time (self .mutations .time )
868- self .mutations ["size" ] = default_mutation_styles ["size" ]
869- self .mutations ["fill" ] = default_mutation_styles ["known_time" ]["fill" ]
870- self .mutations ["stroke" ] = default_mutation_styles ["known_time" ]["stroke" ]
871- self .mutations .loc [is_unknown , "fill" ] = default_mutation_styles ["unknown_time" ]["fill" ]
872- self .mutations .loc [is_unknown , "stroke" ] = default_mutation_styles ["unknown_time" ]["stroke" ]
868+ self .mutations ["size" ] = DEFAULT_MUTATION_STYLES ["size" ]
869+ self .mutations ["fill" ] = DEFAULT_MUTATION_STYLES ["known_time" ]["fill" ]
870+ self .mutations ["stroke" ] = DEFAULT_MUTATION_STYLES ["known_time" ]["stroke" ]
871+ self .mutations .loc [is_unknown , "fill" ] = DEFAULT_MUTATION_STYLES ["unknown_time" ]["fill" ]
872+ self .mutations .loc [is_unknown , "stroke" ] = DEFAULT_MUTATION_STYLES ["unknown_time" ]["stroke" ]
873873
874874 def set_mutation_styles (self , styles ):
875875 """Individually control the styling of each mutation.
@@ -1235,8 +1235,8 @@ def _prepare_json(
12351235 "site_id" : list (muts ["site_id" ]),
12361236 "mutation_id" : list (muts .index ),
12371237 "x_pos" : list (x_pos ),
1238- "fill" : default_mutation_styles ["condensed" ]["fill" ],
1239- "stroke" : default_mutation_styles ["condensed" ]["stroke" ],
1238+ "fill" : DEFAULT_MUTATION_STYLES ["condensed" ]["fill" ],
1239+ "stroke" : DEFAULT_MUTATION_STYLES ["condensed" ]["stroke" ],
12401240 "active" : False ,
12411241 "label" : "⨉" + str (muts .shape [0 ]),
12421242 "content" : "<br>" .join (muts .content ),
@@ -1731,7 +1731,7 @@ def subset_graph(self, seed_nodes, depth):
17311731 included_breakpoints .append (current_region ) # make sure to append the last region
17321732 included_breakpoints = pd .DataFrame (included_breakpoints )
17331733
1734- return collections . namedtuple ( ' IncludedInfo' , [ 'nodes' , 'edges' , 'mutations' , 'breakpoints' ]) (
1734+ return IncludedInfo (
17351735 included_nodes , included_edges , included_mutations , included_breakpoints
17361736 )
17371737
0 commit comments