From f62509f37cc53127f81400b67edf4935eef3d363 Mon Sep 17 00:00:00 2001 From: dengzq1234 Date: Wed, 8 Nov 2023 14:44:48 +0100 Subject: [PATCH 1/3] fix ncbi annotation using other properties --- ete4/ncbi_taxonomy/ncbiquery.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ete4/ncbi_taxonomy/ncbiquery.py b/ete4/ncbi_taxonomy/ncbiquery.py index 2c19ef4e0..1b545f342 100644 --- a/ete4/ncbi_taxonomy/ncbiquery.py +++ b/ete4/ncbi_taxonomy/ncbiquery.py @@ -475,7 +475,10 @@ def annotate_tree(self, t, taxid_attr="name", tax2name=None, taxids = set() for n in t.traverse(): try: - tid = int(getattr(n, taxid_attr)) + if taxid_attr == 'species': + tid = int(getattr(n, taxid_attr)) + else: + tid = int(n.props.get(taxid_attr)) except (ValueError, AttributeError, TypeError): pass else: @@ -501,7 +504,7 @@ def annotate_tree(self, t, taxid_attr="name", tax2name=None, for n in t.traverse('postorder'): try: - node_taxid = int(getattr(n, taxid_attr)) + node_taxid = int(n.props.get(taxid_attr)) except (ValueError, AttributeError, TypeError): node_taxid = None @@ -509,13 +512,13 @@ def annotate_tree(self, t, taxid_attr="name", tax2name=None, if node_taxid: if node_taxid in merged_conversion: node_taxid = merged_conversion[node_taxid] - n.add_props(sci_name = tax2name.get(node_taxid, getattr(n, taxid_attr, '')), + n.add_props(sci_name = tax2name.get(node_taxid, n.props.get(taxid_attr, '')), common_name = tax2common_name.get(node_taxid, ''), lineage = tax2track.get(node_taxid, []), rank = tax2rank.get(node_taxid, 'Unknown'), named_lineage = [tax2name.get(tax, str(tax)) for tax in tax2track.get(node_taxid, [])]) elif n.is_leaf: - n.add_props(sci_name = getattr(n, taxid_attr, 'NA'), + n.add_props(sci_name = n.props.get(taxid_attr, 'NA'), common_name = '', lineage = [], rank = 'Unknown', From 6bf640d2355b03a1cf01fc8f5c93660b78b43fb4 Mon Sep 17 00:00:00 2001 From: dengzq1234 Date: Wed, 8 Nov 2023 15:14:02 +0100 Subject: [PATCH 2/3] correct the way to get species attribtues --- ete4/ncbi_taxonomy/ncbiquery.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/ete4/ncbi_taxonomy/ncbiquery.py b/ete4/ncbi_taxonomy/ncbiquery.py index 1b545f342..043cee5bb 100644 --- a/ete4/ncbi_taxonomy/ncbiquery.py +++ b/ete4/ncbi_taxonomy/ncbiquery.py @@ -475,10 +475,7 @@ def annotate_tree(self, t, taxid_attr="name", tax2name=None, taxids = set() for n in t.traverse(): try: - if taxid_attr == 'species': - tid = int(getattr(n, taxid_attr)) - else: - tid = int(n.props.get(taxid_attr)) + tid = int(getattr(n, taxid_attr, n.props.get(taxid_attr))) except (ValueError, AttributeError, TypeError): pass else: @@ -504,7 +501,7 @@ def annotate_tree(self, t, taxid_attr="name", tax2name=None, for n in t.traverse('postorder'): try: - node_taxid = int(n.props.get(taxid_attr)) + node_taxid = int(getattr(n, taxid_attr, n.props.get(taxid_attr))) except (ValueError, AttributeError, TypeError): node_taxid = None From 10712afa22c59a4ed010da9b452233dc74f8a6ee Mon Sep 17 00:00:00 2001 From: dengzq1234 Date: Wed, 8 Nov 2023 15:44:16 +0100 Subject: [PATCH 3/3] keep consistent to get 'species' from attr then props --- ete4/ncbi_taxonomy/ncbiquery.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ete4/ncbi_taxonomy/ncbiquery.py b/ete4/ncbi_taxonomy/ncbiquery.py index 043cee5bb..db946af7e 100644 --- a/ete4/ncbi_taxonomy/ncbiquery.py +++ b/ete4/ncbi_taxonomy/ncbiquery.py @@ -509,13 +509,13 @@ def annotate_tree(self, t, taxid_attr="name", tax2name=None, if node_taxid: if node_taxid in merged_conversion: node_taxid = merged_conversion[node_taxid] - n.add_props(sci_name = tax2name.get(node_taxid, n.props.get(taxid_attr, '')), + n.add_props(sci_name = tax2name.get(node_taxid, getattr(n, taxid_attr, n.props.get(taxid_attr, ''))), common_name = tax2common_name.get(node_taxid, ''), lineage = tax2track.get(node_taxid, []), rank = tax2rank.get(node_taxid, 'Unknown'), named_lineage = [tax2name.get(tax, str(tax)) for tax in tax2track.get(node_taxid, [])]) elif n.is_leaf: - n.add_props(sci_name = n.props.get(taxid_attr, 'NA'), + n.add_props(sci_name = getattr(n, taxid_attr, n.props.get(taxid_attr, 'NA')), common_name = '', lineage = [], rank = 'Unknown',