python - Constructing a phylogentic tree -


i have list of list of lists this

matches = [[['rootrank', 'root'], ['domain', 'bacteria'], ['phylum', 'firmicutes'], ['class', 'clostridia'], ['order', 'clostridiales'], ['family', 'lachnospiraceae'], ['genus', 'lachnospira']],             [['rootrank', 'root'], ['domain', 'bacteria'], ['phylum', '"proteobacteria"'], ['class', 'gammaproteobacteria'], ['order', '"vibrionales"'], ['family', 'vibrionaceae'], ['genus', 'catenococcus']],             [['rootrank', 'root'], ['domain', 'archaea'], ['phylum', '"euryarchaeota"'], ['class', '"methanomicrobia"'], ['order', 'methanomicrobiales'], ['family', 'methanomicrobiaceae'], ['genus', 'methanoplanus']]] 

and want construct phylogenetic tree them. wrote node class (based partially on this code):

class node(object):     """generic n-ary tree node object     children additive; no provision deleting them."""      def __init__(self, parent, category=none, name=none):         self.parent = parent         self.category = category         self.name = name         self.childlist = []          if  parent none:             self.birthorder  =  0         else:             self.birthorder  =  len(parent.childlist)             parent.childlist.append(self)      def fullpath(self):         """returns list of children root self"""         result  =  []         parent  =  self.parent         kid     =  self          while parent:             result.insert(0, kid)             parent, kid  =  parent.parent, parent          return result      def id(self):         return '{0}|{1}'.format(self.category, self.name) 

and try construct tree this:

node = none match in matches:     branch in match:         category, name = branch         node = node(node, category, name)         print [n.id() n in node.fullpath()] 

this works first match, when start second match appended @ end of tree instead of starting again @ top. how that? tried variations on searching id, can't work.

the issue node bottommost node in tree, , appending node. need store root node. since ['rootrank', 'root'] appears @ beginning of each of lists, i'd recommend pulling out , using root. can like:

rootnode = node(none, 'rootrank', 'root') match in matches:     node = rootnode     branch in match:         category, name = branch         node = node(node, category, name)         print [n.id() n in node.fullpath()] 

this make matches list more readable, , gives expected output.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -