将CSV转换为Newick树

将CSV转换为Newick树,第1张

将CSV转换为Newick树

您可以使用一些简单的Python从CSV构建树,然后将其写到Newick树中。不确定这是否是您要尝试的 *** 作。

import csvfrom collections import defaultdictfrom pprint import pprintdef 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 tdef 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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5667421.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存