2013-08-03 64 views
1

我想从给定的树文件中删除一组节点。python ete2从树中删除节点

Input : tree file+ txt file contains the name of nodes that should only exists in the tree. 

这是我的代码:`

def find_id(file,id): 

    f= open(file) 
    s = open(file).read() 
    if re.search(id,s,re.MULTILINE): 
     found= True 
    else: 
     found= False 
    return found 

def remove_nodes(treeFile,idFile): 
     t= Tree(treeFile,format=8) 
     removed=[] 
     for node in t: 
       #print node.name 
       if not find_id(idFile,'^'+node.name+'\s') and node.is_leaf(): 
        n= node.delete() 
        removed.append(n) 
     print removed 
     t.write(format=1, outfile="newtree.nw") 

    remove_nodes('arthropods.nw','taxidMap.txt')` 

arthropods.nw是newick树文件,这是一个摘录:

((260574)58772(874683,874682,874681,874680,874685,874684,1096898,874676,874677,874678,874679)89902(((((61988,390855,109756,62003,374072,244964,146864,251422,388540,438507,681530)61987,(244997,1068629,485196,681527,126872,111303,58784,134582,89817,231264)58783)109754,((289475,390856,118505)118504)118506)61986(((((756952,756950,756951,171369,1053728,231396)171368,(980235)980234,(118484)118483,(126927)126926,(1147029,863609,89974,1255757... 

taxidMap.txt:

135631 NC_015190 
29137 NC_003314 
29139 NC_003322 
... 

问题是当我打印列表“删除”它的gi给我一个没有列表,并且我意识到树中的节点数仍然是输入文件 中任何建议的名称数量的>个数。 在此先感谢

+0

你可以发表你正在使用的'Tree'对象的一些代码吗? – Houdini

+0

正如fransua先生所说,这里是树类http://pythonhosted.org/ete2/reference/reference_tree.html – AWRAM

回答

2

我不确定代码的其余部分是否工作找不到输入文件的示例。 但是我发现这一点,可以改变:

- removed.append(n) 
+ removed.append(node) 

n是实际上等于无,作为删除功能不返回任何东西。

PD:由@houdini的方式,所用的树类记载有:http://pythonhosted.org/ete2/reference/reference_tree.html

编辑:

好,根据你输入的文件,我会改变你的codelike这样的:

from ete2 import Tree 
import re 

def find_id(file,id): 

    f= open(file) 
    s = open(file).read() 
    if re.search(id,s,re.MULTILINE): 
     found= True 
    else: 
     found= False 
    return found 

def remove_nodes(treeFile,idFile): 

    t= Tree(treeFile,format=8) 
    print t.get_ascii() 
    removed=[] 
    for node in t.iter_descendants(): 
     # print node.name 
     if not find_id(idFile,'^'+node.name+'\s'): 
      node.delete(prevent_nondicotomic=False) 
      removed.append(node) 

    print [n.name for n in removed] 
    print t.get_ascii() 
    t.write(format=1, outfile="newtree.nw") 

remove_nodes('arthropods.nw','taxidMap.txt') 

我的树文件是:

(58772,89902,((61988,390855)29139,((62003,374072)244964,146864,251422)388540,29137)61987); 

和M IDS Y的列表文件:

29137 NC_003314 
29139 NC_003322 
62003 NC_004444 

这里输出:

 /-58772 
    | 
    |--89902 
    | 
    |   /-61988 
-NoName /29139 
    | |  \-390855 
    | | 
    | |   /-62003 
    | |  /244964 
     \61987 |  \-374072 
      |-388540 
      |  |--146864 
      |  | 
      |  \-251422 
      | 
      \-29137 
['58772', '89902', '61987', '388540', '61988', '390855', '244964', '146864', '251422', '374072'] 

     /-29139 
    | 
-NoName-29137 
    | 
     \-62003 

EDIT2:

要删除只长叶子,只是删除了iter_descendants,部分,就像你在做:

def remove_nodes(treeFile,idFile): 

    t= Tree(treeFile,format=8) 
    print t 
    removed=[] 
    for node in t: 
     # print node.name 
     if not find_id(idFile,'^'+node.name+'\s'): 
      node.delete(prevent_nondicotomic=False) 
      removed.append(node) 

    print [n.name for n in removed] 
    print t 
    t.write(format=1, outfile="newtree.nw") 

但是在这个例子中,我使用的结果相当不错:S ...也许有更多的节点保持它会更好。

/-58772 
    | 
    |--89902 
    | 
    |  /-61988 
--| /-| 
    | | \-390855 
    | | 
    | |  /-62003 
    | | /-| 
    \-| | \-374072 
    |--| 
    | |--146864 
    | | 
    | \-251422 
    | 
     \-29137 
['58772', '89902', '61988', '390855', '374072', '146864', '251422'] 

     /-29139 
    | 
-- /-|-- /- /-62003 
    | 
     \-29137 
+0

我添加了输入文件 – AWRAM

+0

@ awram,好的我编辑根据。希望这可以帮助。 – fransua

+0

非常感谢。我看到树的拓扑结构已完全更改我想删除节点,如果它只是一个叶子 – AWRAM