简体   繁体   English

二叉树比较从叶到根python ete2工具的节点特征

[英]binary tree compare nodes' feature from leaves to root python ete2 tools

I have a binary tree: 我有一个二叉树:

       root
     /     \
    g      h
   / \    / \
 d   a   e   f
 / \
b   c

each node has 'seq' feature which stores the dna sequence of each node ('AACCGGT') 每个节点都有'seq'功能,用于存储每个节点的dna序列('AACCGGT')

the sisters who are both leaves (b,c and e,f) have each one a score (float ) what I want is to : 两个都是叶子的姐妹(b,c和e,f)每个都有一个得分(漂浮),我想要的是:

  • compare score of leaf which has score with it's sister b.score with c.score 比较得分的叶子得分与它的姐妹b.score与c.score
  • d.score = max (b.score,c.score) d.score = max(b.score,c.score)

  • same for e, f and h e,f和h相同

  • compare a.seq with d.seq ==> d and e will have a score 将a.seq与d.seq ==> d进行比较,e将得分
  • g.score max (e.score, a.score) ... till arrive to the root NOte: every leaf node has 'model feature' I compare b.seq with c.seq based on b.model ==>I got b.score then based on c.model ==> I got c.score g.score max(e.score,a.score)...直到到达根NOte:每个叶节点都有'模型特征'我比较b.seq和c.seq基于b.model ==>我得到了b.score然后基于c.model ==>我得到了c.score

This the function I wrote but I'm not sure if it does what I want and I can't test it because I don't have the align_trna function yet 这是我写的函数,但我不确定它是否符合我的要求,我无法测试它,因为我还没有align_trna函数

def affect_score(n): def affect_score(n):

if (n.score)==0:
            n.score,n.model=affect_score(n.get_children()[0])
        result=n.score
        model=n.model
        if not n.is_root():
            sis=n.get_sisters()[0]
            if sis.score==0:
                sis.score,sis.model=affect_score(sis.get_children()[0])
                n.score=align_trna(n.seq,sis.seq,n.model)
                sis.score,sis.model= align_trna(nseq, sis.seq,sis.model)
                if n.score < sis.score:
                        result=sis.score
                        model=sis.model

        return result,mode

l

Can anyone helps me by telling if I am thinking write ? 任何人都可以告诉我,我是否在想写作? Note that It's my first time working with tree data sturcture and recursion thanks in advance for any suggestion 请注意,这是我第一次使用树数据结构和递归,提前感谢任何建议

Ok let's try to recap something about recursion. 好的,我们试着回顾一下递归的问题。

The first thing to write when doing recursive function is the "exit condition" (the things that terminates the recursion at some point). 在执行递归函数时要写的第一件事是“退出条件”(在某些时候终止递归的东西)。 In your case the exit condition should refer to the leaf node of the tree, and it would be something like: 在您的情况下,退出条件应该引用树的叶节点,它将类似于:

if len(n.get_childreen())==0:
    return n.score

then for every other node you should do your computation: 然后对于每个其他节点,你应该进行计算:

child_score = max(affect_score(n).get_children()[0], affect_score(n).get_children()[1])
seq = compare_seq(n.seq, n.get_sisters()[0].seq)
n.score = max(child_score, seq)

then you also want a special condition on the root since it doesn't have any sisters: 那么你也想在root上有一个特殊的条件,因为它没有任何姐妹:

if n.is_root():
   n.score = max(affect_score(n).get_children()[0], affect_score(n).get_children()[1])

At the end it should look like: 最后它应该看起来像:

def affect_score(n):
    if len(n.get_childreen())==0:
        return n.score
    if n.is_root():
       n.score = max(affect_score(n).get_children()[0], affect_score(n).get_children()[1])
    else:
       child_score = max(affect_score(n).get_children()[0], affect_score(n).get_children()[1])
       seq = compare_seq(n.seq, n.get_sisters()[0].seq)
       n.score = max(child_score, seq)

This is what I understood you want to do! 这就是我理解你想做的事情! You need to add the compare_seq() which is the method that should compare the two sequences, and probably adapt something to your code. 您需要添加compare_seq(),这是应该比较两个序列的方法,并可能适应您的代码。 But in theory it should be almost correct. 但理论上它应该几乎是正确的。

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

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