This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Construct Newick Tree from tab-delimited or csv file of phylogeny, preferably in Python

So I am looking for a way, preferably a Pythonic way (packages are ok), to convert a tab-delimited or csv of hierarchical phylogenies into the classic Newick format for tree visualization.

Each line of the file has ['Phylum','Class','Order','Family','Genus','Species','Subspecies','gi'] as values and I would like to create a Newick tree representaiton. Any help greatly appreciated. Thanks!

newick phylogeny python tree

2 answers

You can use dendropy.

The easiest way I can think of (without thinking too much) is for each line go from the phylum down to gi and create a child node or select a node you already created. Then you can export the tree in Newick format.

Thanks Asaf, the code below in my answer addressed the problem using the node approach.

תודה רבה מטכניון :)

ד"ש לרותי

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)

Log in to answer this question.