简体   繁体   English

如何使用存储在列表中的字符串使用python软件包ete2创建树?

[英]How can I create a tree using the python package ete2 using strings stored in a list?

I am trying to make a phylogenetic tree using the python package ete2 from synthetic data output from a cellular automaton model of mine. 我正在尝试使用python软件包ete2从我的细胞自动机模型输出的合成数据中制作出系统树。 The data consists of pairs listed as (parent, child) where each member of the pair is a unique integer representing a mutation event. 数据由列为(父,子)的对组成,其中对的每个成员都是代表突变事件的唯一整数。 I have recast each of the members of the pair as strings and preceded them with 'r', so now: 我已经将字符串对中的每个成员重铸为字符串,并在它们之前加上“ r”,所以现在:

('r1' ,'r2') would represent a parent called 'r1' giving rise to a child called 'r2'. (“ r1”,“ r2”)将代表一个称为“ r1”的父代,从而产生一个名为“ r2”的子代。 So the output file looks like: 因此输出文件如下所示:

[['r1' 'r2']
 ['r1' 'r3']
 ['r1' 'r4']
 ['r1' 'r5']
 ['r1' 'r6']
 ['r1' 'r7']
 ['r1' 'r8']
 ['r1' 'r9']
 ['r2' 'r10']
 ['r1' 'r11']
 ['r1' 'r12']
 ['r8' 'r13']
 ['r1' 'r14']
 ['r4' 'r15']
 ['r1' 'r16']
 ['r1' 'r17']
 ['r1' 'r18']
 ['r1' 'r19']]

I want to iterate over the list to make the tree using 'add_child' but keep getting errors. 我想遍历列表以使用'add_child'来制作树,但始终会出错。 My current code is: 我当前的代码是:

t = Tree() # Creates an empty tree
r1 = t.add_child(name="r1")

for row in range(0, len(pairs_list)):
    a = str(pairs_list[row,1])
    b = str(pairs_list[row,0])
    a = b.add_child(name = a)

and I get the error: 我得到错误:

Traceback (most recent call last):
  File "treetest.py", line 33, in <module>
    a = b.add_child(name = a)
AttributeError: 'str' object has no attribute 'add_child'

If I replace the 'b' in the last line of my code with r1 (or something else) it works find, but of course that doesn't represent the data... thanks in advance, universe. 如果我用r1(或其他方式)替换代码最后一行中的“ b”,则可以找到它,但是当然不代表数据……在此先感谢Universe。

Something like this: 像这样:

t = Tree() # Creates an empty tree
r1 = t.add_child(name="r1")
lookup = {"r1": r1}

def sort_pairs(pair):
    # Extract integer after "r".
    return int(pair[0][1:])

for pair in sorted(pairs_list, key=sort_pairs):
    parentname = pair[0]
    childname = pair[1]
    if childname not in lookup:
        if parentname in lookup:
            # Add child.
            newchild = lookup[parentname].add_child(name = childname)
            lookup.add(childname, newchild)
        else:
            raise RuntimeError('Must not happen.')

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

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