From 52b5d60c63980f6cd80492749c169c4be841ff8b Mon Sep 17 00:00:00 2001 From: Dillon Barker Date: Fri, 29 Sep 2023 14:17:24 -0500 Subject: [PATCH 1/4] fix broken str.join preventing taxonomy from being built --- ete4/ncbi_taxonomy/ncbiquery.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ete4/ncbi_taxonomy/ncbiquery.py b/ete4/ncbi_taxonomy/ncbiquery.py index f20e5cd38..06412fc3f 100644 --- a/ete4/ncbi_taxonomy/ncbiquery.py +++ b/ete4/ncbi_taxonomy/ncbiquery.py @@ -652,13 +652,13 @@ def generate_table(t): 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) + 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) + print('\t'.join([n.name, "", n.props.get('taxname'), + n.props.get("common_name", ''), n.props.get("rank"), + ','.join(track)]), file=OUT) OUT.close() def update_db(dbfile, targz_file=None): From 6eb5b8f15bd119d40d9fd255de7b6d64b971e5e4 Mon Sep 17 00:00:00 2001 From: Dillon Barker Date: Fri, 29 Sep 2023 14:25:02 -0500 Subject: [PATCH 2/4] refactor generate_table --- ete4/ncbi_taxonomy/ncbiquery.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/ete4/ncbi_taxonomy/ncbiquery.py b/ete4/ncbi_taxonomy/ncbiquery.py index 06412fc3f..8dc3632c2 100644 --- a/ete4/ncbi_taxonomy/ncbiquery.py +++ b/ete4/ncbi_taxonomy/ncbiquery.py @@ -651,14 +651,12 @@ def generate_table(t): 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) + + n_up_name = n.up.name if n.up else "" + + 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) OUT.close() def update_db(dbfile, targz_file=None): From fd0c8ac8f2a20c32381048880b70f9f5a412fdcc Mon Sep 17 00:00:00 2001 From: Dillon Barker Date: Fri, 29 Sep 2023 14:35:21 -0500 Subject: [PATCH 3/4] refactor with context manager --- ete4/ncbi_taxonomy/ncbiquery.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/ete4/ncbi_taxonomy/ncbiquery.py b/ete4/ncbi_taxonomy/ncbiquery.py index 8dc3632c2..36fcdca1a 100644 --- a/ete4/ncbi_taxonomy/ncbiquery.py +++ b/ete4/ncbi_taxonomy/ncbiquery.py @@ -642,22 +642,21 @@ 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 - - n_up_name = n.up.name if n.up else "" - - 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) - 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 "" + + 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) def update_db(dbfile, targz_file=None): basepath = os.path.split(dbfile)[0] From 7532c5b2fe66856f3e13a35a757760e1a9e09b98 Mon Sep 17 00:00:00 2001 From: Dillon Barker Date: Fri, 29 Sep 2023 14:47:42 -0500 Subject: [PATCH 4/4] separate print from join --- ete4/ncbi_taxonomy/ncbiquery.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ete4/ncbi_taxonomy/ncbiquery.py b/ete4/ncbi_taxonomy/ncbiquery.py index 36fcdca1a..b1d37a8e2 100644 --- a/ete4/ncbi_taxonomy/ncbiquery.py +++ b/ete4/ncbi_taxonomy/ncbiquery.py @@ -654,9 +654,11 @@ def generate_table(t): n_up_name = n.up.name if n.up else "" - print('\t'.join([n.name, n_up_name, n.props.get('taxname'), + row = '\t'.join([n.name, n_up_name, n.props.get('taxname'), n.props.get("common_name", ''), n.props.get("rank"), - ','.join(track)]), file=out) + ','.join(track)]) + + print(row, file=out) def update_db(dbfile, targz_file=None): basepath = os.path.split(dbfile)[0]