@@ -118,13 +118,13 @@ def order_nodes(node):
118
118
return od
119
119
120
120
121
- def read_tree ( fname ):
122
- tree = Phylo .read (fname , 'newick' )
121
+ def read_trees ( filenames ):
122
+ trees = [ Phylo .read (fname , 'newick' ) for fname in filenames ]
123
123
# augur export requires unique node names (both terminal and external) as these
124
124
# are used to associate metadata/node-data with nodes. Any duplication is fatal.
125
125
# The exception to this is unlabelled node names, which auspice will handle but
126
126
# won't be associated with any metadata within export.
127
- node_names = [clade .name for clade in tree .root .find_clades ()]
127
+ node_names = [clade .name for tree in trees for clade in tree .root .find_clades ()]
128
128
if None in node_names :
129
129
raise AugurError (f"Tree contains unnamed nodes. If these are internal nodes you may wish to run " +
130
130
"`augur refine --tree <newick> --output-tree <newick>` to name them." )
@@ -133,7 +133,7 @@ def read_tree(fname):
133
133
dups = [name for name , count in Counter (node_names ).items () if count > 1 ]
134
134
raise AugurError (f"{ len (dups )} node names occur multiple times in the tree: " +
135
135
", " .join ([f"{ v !r} " for v in dups [0 :5 ]]) + ("..." if len (dups )> 5 else "" ))
136
- return (tree , node_names )
136
+ return (trees , node_names )
137
137
138
138
139
139
def node_div (T , node_attrs ):
@@ -751,7 +751,12 @@ def _recursively_set_data(node):
751
751
node ['branch_attrs' ] = branch_attrs [node ['name' ]]
752
752
for child in node .get ("children" , []):
753
753
_recursively_set_data (child )
754
- _recursively_set_data (data_json ["tree" ])
754
+
755
+ if isinstance (data_json ["tree" ], list ):
756
+ for subtree in data_json ['tree' ]:
757
+ _recursively_set_data (subtree )
758
+ else :
759
+ _recursively_set_data (data_json ["tree" ])
755
760
756
761
757
762
def set_node_attrs_on_tree (data_json , node_attrs , additional_metadata_columns ):
@@ -840,7 +845,11 @@ def _recursively_set_data(node):
840
845
for child in node .get ("children" , []):
841
846
_recursively_set_data (child )
842
847
843
- _recursively_set_data (data_json ["tree" ])
848
+ if isinstance (data_json ["tree" ], list ):
849
+ for subtree in data_json ['tree' ]:
850
+ _recursively_set_data (subtree )
851
+ else :
852
+ _recursively_set_data (data_json ["tree" ])
844
853
845
854
def node_data_prop_is_normal_trait (name ):
846
855
# those traits / keys / attrs which are not "special" and can be exported
@@ -894,7 +903,7 @@ def register_parser(parent_subparsers):
894
903
required = parser .add_argument_group (
895
904
title = "REQUIRED"
896
905
)
897
- required .add_argument ('--tree' ,'-t' , metavar = "newick" , required = True , help = "Phylogenetic tree, usually output from `augur refine`" )
906
+ required .add_argument ('--tree' ,'-t' , metavar = "newick" , nargs = '+' , action = 'extend' , required = True , help = "Phylogenetic tree(s) , usually output from `augur refine`" )
898
907
required .add_argument ('--output' , metavar = "JSON" , required = True , help = "Output file (typically for visualisation in auspice)" )
899
908
900
909
config = parser .add_argument_group (
@@ -1192,7 +1201,7 @@ def run(args):
1192
1201
metadata_file = {}
1193
1202
1194
1203
# parse input files
1195
- (T , node_names ) = read_tree (args .tree )
1204
+ (trees , node_names ) = read_trees (args .tree )
1196
1205
node_data , node_attrs , node_data_names , metadata_names , branch_attrs = \
1197
1206
parse_node_data_and_metadata (node_names , node_data_file , metadata_file )
1198
1207
config = get_config (args )
@@ -1224,7 +1233,8 @@ def run(args):
1224
1233
set_filters (data_json , config )
1225
1234
1226
1235
# set tree structure
1227
- data_json ["tree" ] = convert_tree_to_json_structure (T .root , node_attrs , node_div (T , node_attrs ))
1236
+ trees_json = [convert_tree_to_json_structure (tree .root , node_attrs , node_div (tree , node_attrs )) for tree in trees ]
1237
+ data_json ["tree" ] = trees_json [0 ] if len (trees_json )== 1 else trees_json
1228
1238
set_node_attrs_on_tree (data_json , node_attrs , additional_metadata_columns )
1229
1239
set_branch_attrs_on_tree (data_json , branch_attrs )
1230
1240
0 commit comments