diff --git a/ete4/ncbi_taxonomy/ncbiquery.py b/ete4/ncbi_taxonomy/ncbiquery.py index f20e5cd38..b1d37a8e2 100644 --- a/ete4/ncbi_taxonomy/ncbiquery.py +++ b/ete4/ncbi_taxonomy/ncbiquery.py @@ -642,24 +642,23 @@ def load_ncbi_tree_from_dump(tar): return t, synonyms def generate_table(t): - OUT = open("taxa.tab", "w") - for j, n in enumerate(t.traverse()): - if j % 1000 == 0: - print("\r",j,"generating entries...", end=' ') - temp_node = n - track = [] - while temp_node: - track.append(temp_node.name) - temp_node = temp_node.up - if n.up: - print('\t'.join(n.name, n.up.name, n.props.get('taxname'), - n.props.get("common_name", ''), n.props.get("rank"), - ','.join(track)), file=OUT) - else: - print('\t'.join(n.name, "", n.props.get('taxname'), - n.props.get("common_name", ''), n.props.get("rank"), - ','.join(track)), file=OUT) - OUT.close() + with open("taxa.tab", "w") as out: + for j, n in enumerate(t.traverse()): + if j % 1000 == 0: + print("\r",j,"generating entries...", end=' ') + temp_node = n + track = [] + while temp_node: + track.append(temp_node.name) + temp_node = temp_node.up + + n_up_name = n.up.name if n.up else "" + + row = '\t'.join([n.name, n_up_name, n.props.get('taxname'), + n.props.get("common_name", ''), n.props.get("rank"), + ','.join(track)]) + + print(row, file=out) def update_db(dbfile, targz_file=None): basepath = os.path.split(dbfile)[0]