gpt4 book ai didi

python - 将 csv 转换为 Newick 树

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:09 24 4
gpt4 key购买 nike

所以我有一个 csv 文件,其中每一行代表以下形式的分层数据:'门','类','目','科','属','种','亚种','unique_gi'

我想将其转换为经典 Newick tree format没有距离。无论是新颖的方法还是 python 包都会很棒。谢谢!

最佳答案

您可以使用一些简单的 Python 从 CSV 构建树,然后将其写入 Newick 树。不确定这是否是您想要做的。

import csv
from collections import defaultdict
from pprint import pprint

def tree(): return defaultdict(tree)

def tree_add(t, path):
for node in path:
t = t[node]

def pprint_tree(tree_instance):
def dicts(t): return {k: dicts(t[k]) for k in t}
pprint(dicts(tree_instance))

def csv_to_tree(input):
t = tree()
for row in csv.reader(input, quotechar='\''):
tree_add(t, row)
return t

def tree_to_newick(root):
items = []
for k in root.iterkeys():
s = ''
if len(root[k].keys()) > 0:
sub_tree = tree_to_newick(root[k])
if sub_tree != '':
s += '(' + sub_tree + ')'
s += k
items.append(s)
return ','.join(items)

def csv_to_weightless_newick(input):
t = csv_to_tree(input)
#pprint_tree(t)
return tree_to_newick(t)

if __name__ == '__main__':
# see https://docs.python.org/2/library/csv.html to read CSV file
input = [
"'Phylum','Class','Order','Family','Genus','Species','Subspecies','unique_gi'",
"'Phylum','Class','Order','example'",
"'Another','Test'",
]

print csv_to_weightless_newick(input)

示例输出:

$ python ~/tmp/newick_tree.py
(((example,((((unique_gi)Subspecies)Species)Genus)Family)Order)Class)Phylum,(Test)Another

此外,这个库看起来很酷,可以让您可视化您的树:http://biopython.org/wiki/Phylo

关于python - 将 csv 转换为 Newick 树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26146623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com