我想從給定的樹文件中刪除一組節點。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給我一個沒有列表,並且我意識到樹中的節點數仍然是輸入文件 中任何建議的名稱數量的>個數。 在此先感謝
你可以發表你正在使用的'Tree'對象的一些代碼嗎? – Houdini
正如fransua先生所說,這裏是樹類http://pythonhosted.org/ete2/reference/reference_tree.html – AWRAM