각 줄마다 다음과 같은 형식의 계층 데이터를 나타내는 csv 파일이 있습니다. '문란', '클래스', '주문', '가족', '속', '종' Subspecies ','unique_gi 'csv를 Newick 트리로 변환
대표 전화 Newick tree format으로 변경하고 싶습니다. 새로운 방법이나 파이썬 패키지가 놀랄 것입니다. 고맙습니다!
각 줄마다 다음과 같은 형식의 계층 데이터를 나타내는 csv 파일이 있습니다. '문란', '클래스', '주문', '가족', '속', '종' Subspecies ','unique_gi 'csv를 Newick 트리로 변환
대표 전화 Newick tree format으로 변경하고 싶습니다. 새로운 방법이나 파이썬 패키지가 놀랄 것입니다. 고맙습니다!
간단한 파이썬을 사용하여 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
고마워요! 훌륭하게 작동합니다. –
@MarkWatson, 'python newick_tree.py file.csv'가 올바른 명령 줄입니까? – user3184877
크로스 게시 : https://www.biostars.org/p/ 114387 – Pierre