T1 = '((a,b),(c,d));'
T2 = '(((a,b),c),(d,e),f);'
PRIMATES = '(((((Homo:0.21,Pongo:0.21)N1:0.28,Macaca:0.49)N2:0.13,Ateles:0.62)N3:0.38,Galago:1.00)N4:0.0);'

def parseNewick (newick, name='Root'):
        tree = [name]
        i = 0
        while i < len(newick) - 1:
                i = i + 1
                if startsClade(newick[i]):
                        clade = getClade(newick[i:])
                        node_name = getWord(newick[i + len(clade)])
                        tree.append(parseNewick(clade, node_name))
                        i = i + len(clade) + len(node_name)
                elif startsLeaf(newick[i]):
                        leaf = getWord(newick[i:])
                        tree.append(leaf)
                        i = i + len(leaf)
        return tree

def startsLeaf(c):
        return c.isalpha()

def startsClade(c):
        return c == '('

def endsClade(c):
        return c == ')'

def getClade(newick):
        desc = 0
        for i, c in enumerate(newick):
                if startsClade(c):
                        desc += 1
                elif endsClade(c):
                        desc -= 1
                if desc == 0:
                        return newick[:i+1]

def getWord (string):
        for i, c in enumerate(string):
                if not c.isalnum():
                        return string[:i]
        return string