简体   繁体   English

python ete2 TreeNode检查它是否具有属性

[英]python ete2 TreeNode check if it has attribute

I have a tree t which some of its nodes has an attribute 'a' I want to test if a given node has this attribute, what I did is 我有一棵树,它的某些节点具有属性“ a”,我想测试给定节点是否具有此属性,我所做的是

if not n.a:
   print "no a " 

but I'm getting an error treenode n has no attribute a 但我遇到一个错误treenode n没有属性a

Any way to test it ? 有什么测试方法吗?

You could just use the generic pythonic way 您可以使用通用的pythonic方式

if not hasattr(node, "a"): 
   print "a attribute not found in node:", node

If "a" is registered as a regular feature in your ETE tree , you could also use the following approach: 如果在您的ETE树中将“ a”注册为常规功能,则也可以使用以下方法:

from ete2 import Tree
t = Tree()
t.populate(5)
t.children[0].add_features(a = "My annotation")

for node in t.traverse():
    if "a" in node.features:
        print node.get_ascii(attributes=["a", "name"])

which would print something like this: 它将打印出这样的内容:

                     /-aaaaaaaaac
-My annotation, NoName
                    |      /-aaaaaaaaad
                     \NoName
                           \-aaaaaaaaae

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM