将CSV转换为Newick树 [英] Convert csv to Newick tree
问题描述
因此,我有一个csv文件,其中每一行均以以下形式表示分层数据: 'Phylum','Class','Order','Family','Genus','Species','Subspecies','unique_gi'
So I have a csv file where each line represents hierarchical data in the form: 'Phylum','Class','Order','Family','Genus','Species','Subspecies','unique_gi'
我想将其转换为经典的 Newick树格式,无距离.一种新颖的方法或一个python软件包都将是惊人的.谢谢!
I would like to convert this to the classic Newick tree format sans distances. Either a novel method or a python package would be amazing. Thank you!
推荐答案
您可以使用一些简单的Python从CSV构建树,然后将其写到Newick树中.不确定这是否是您要尝试的操作.
You could use some simple Python to build out a tree from the CSV, and then write it out to a Newick tree. Not sure if this is what you're trying to do or not.
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
Also, this library seems cool, and lets you visualize your trees: http://biopython.org/wiki/Phylo
这篇关于将CSV转换为Newick树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!