@@ -101,8 +101,10 @@ def assign_clades(clade_designations, all_muts, tree, ref=None):
101101 mapping of node to clades
102102 '''
103103
104- clade_membership = {}
105- basal_clade_nodes = {}
104+ # We add the names of nodes these dictionaries to store which clade they belong to.
105+ # all nodes in a clade should appear in `clade_membership` while only one node should
106+ # appear in `basal_clade_nodes`
107+ (clade_membership , basal_clade_nodes ) = ({}, {})
106108 parents = get_parent_name_by_child_name_for_tree (tree )
107109
108110 # count leaves
@@ -133,7 +135,7 @@ def assign_clades(clade_designations, all_muts, tree, ref=None):
133135 node .sequences [gene ][pos ] = d
134136
135137
136- # second pass to assign 'clade_annotation' to basal nodes within each clade
138+ # store names of basal nodes of each clade in `basal_clade_nodes` and `clade_membership` dicts.
137139 # if multiple nodes match, assign annotation to largest
138140 # otherwise occasional unwanted cousin nodes get assigned the annotation
139141 for clade_name , clade_alleles in clade_designations .items ():
@@ -147,7 +149,7 @@ def assign_clades(clade_designations, all_muts, tree, ref=None):
147149 basal_clade_nodes [target_node .name ] = clade_name
148150 clade_membership [target_node .name ] = clade_name # basal nodes are members of the clade
149151
150- # third pass to propagate 'clade_membership'
152+ # propagate 'clade_membership' to children nodes
151153 # don't propagate if encountering 'clade_annotation'
152154 for node in tree .find_clades (order = 'preorder' ):
153155 for child in node :
@@ -157,29 +159,6 @@ def assign_clades(clade_designations, all_muts, tree, ref=None):
157159 return (basal_clade_nodes , clade_membership )
158160
159161
160- def create_node_data_structure (basal_clade_nodes , clade_membership , args ):
161- node_data = {}
162-
163- if (not args .label_name and not args .trait_name ):
164- print ("WARNING: running `augur clades` without specifying --label-name and/or" )
165- print (" --trait-name is deprecated. To preserve backwards compatibility" )
166- print (" we will use 'clade' and 'clade_membership', respectively." )
167- print (" (Note that 'clade' is now exported as a 'branch_label')" )
168-
169- label_name = "clade"
170- trait_name = "clade_membership"
171- else :
172- label_name = args .label_name
173- trait_name = args .trait_name
174-
175- if trait_name :
176- node_data ['nodes' ] = {node : {trait_name : clade } for node ,clade in clade_membership .items ()}
177- if label_name :
178- node_data ['branch_labels' ] = {node : {label_name : clade } for node ,clade in basal_clade_nodes .items ()}
179-
180- return node_data
181-
182-
183162def get_reference_sequence_from_root_node (all_muts , root_name ):
184163 # attach sequences to root
185164 ref = {}
@@ -202,8 +181,7 @@ def register_arguments(parser):
202181 parser .add_argument ('--mutations' , nargs = '+' , help = 'JSON(s) containing ancestral and tip nucleotide and/or amino-acid mutations ' )
203182 parser .add_argument ('--reference' , nargs = '+' , help = 'fasta files containing reference and tip nucleotide and/or amino-acid sequences ' )
204183 parser .add_argument ('--clades' , type = str , help = 'TSV file containing clade definitions by amino-acid' )
205- parser .add_argument ('--trait-name' , type = str , help = 'name to use to store clade membership (set for every node belonging to the clade)' )
206- parser .add_argument ('--label-name' , type = str , help = 'name to use for branch labels (set on basal branches for each clade)' )
184+ parser .add_argument ('--attribute-name' , type = str , default = "clade" , help = "name to use for clade membership & branch labels" , required = False )
207185 parser .add_argument ('--output-node-data' , type = str , help = 'name of JSON file to save clade assignments to' )
208186
209187
@@ -230,8 +208,12 @@ def run(args):
230208
231209 (basal_clade_nodes , clade_membership ) = assign_clades (clade_designations , all_muts , tree , ref )
232210
233- node_data = create_node_data_structure (basal_clade_nodes , clade_membership , args )
211+ # create node_data for export as a JSON
212+ node_data = {
213+ 'nodes' : {node : {args .attribute_name : clade } for node ,clade in clade_membership .items ()},
214+ 'branch_labels' : {node : {args .attribute_name : clade } for node ,clade in basal_clade_nodes .items ()}
215+ }
234216
235217 out_name = get_json_name (args )
236218 write_json (node_data , out_name )
237- print ("clades written to" , out_name , file = sys .stdout )
219+ print (f "clades written to { out_name } using attribute name { args . attribute_name } " , file = sys .stdout )
0 commit comments