def __format__(self, format_spec): """Serialize the tree as a string in the specified file format. This method supports the format() built-in function added in Python 2.6/3.0. The format_spec should be a lower case string supported by Bio.Phylo.write as an output file format. """ if format_spec: from StringIO import StringIO from Bio.Phylo import _io handle = StringIO() _io.write([self], handle, format_spec) return handle.getvalue() else: # Follow python convention and default to using __str__ return str(self)
def __format__(self, format_spec): """Serialize the tree as a string in the specified file format. This method supports Python's ``format`` built-in function. :param format_spec: a lower-case string supported by ``Bio.Phylo.write`` as an output file format. """ if format_spec: from io import StringIO from Bio.Phylo import _io handle = StringIO() _io.write([self], handle, format_spec) return handle.getvalue() else: # Follow python convention and default to using __str__ return str(self)
def parseChain(lnfchains, dold2newname={}, nfchainout=None, inchainfmt='nexus', outchainfmt='newick', verbose=False): """parse a Nexus-format tree chain and re-write it in a Newick format; edit the tree on the fly, substituting tip labels or grafting trees on tips""" if verbose: print "parseChain('%s')" % repr(lnfchains) ntree = 0 buffsize = 50 treebuffer = [] if nfchainout: nfout = nfchainout else: nfout = n2outChainFileName(lnfchains[0], dirout, inchainfmt, outchainfmt) dhandles = {} for k, nfchain in enumerate(lnfchains): dhandles[k] = PhyloIO.parse(nfchain, inchainfmt) # intertwines the trees from separate chains, respecting their sequence # so that the burn-in can still be defined as the first portion of trees in the sample. sometreeleft = True ichains = range(len(lnfchains)) while sometreeleft: for k in ichains: #~ sys.stdout.write('\rk'+str(k)) ; sys.stdout.flush() try: tree = dhandles[k].next() #~ print tree #~ sys.stdout.flush() except StopIteration: sometreeleft = False # stops for all chains here, even if they had different length (which shoulld not be) break # the for k loop if dold2newname: for tip in tree.get_terminals(): nutipname = dold2newname.get(tip.name) if nutipname: if isinstance(nutipname, str): tip.name = nutipname else: if isinstance(nutipname, tree2.Node): nusubtree = tree2toBioPhylo(nutipname) elif isinstance(nutipname, BaseTree.TreeElement): nusubtree = nutipname else: raise ValueError, "replacement value for a leaf must be either a string (to edit leaf label) of a tree object instance of classes tree2.Node or Bio.Phylo.TreeElement (or derivates)" subtreelen = nusubtree.total_branch_length( ) / nusubtree.count_terminals() if subtreelen: # substract the subtree length to its branch length tip.branch_length = max( 0.0, tip.branch_length - subtreelen) # attach subtree to tree tip.clades = nusubtree.root.clades tip.name = '' treebuffer.append(tree) ntree += 1 #~ sys.stdout.write('\rntree'+str(ntree)) ; sys.stdout.flush() if len(treebuffer) >= buffsize: if verbose: sys.stdout.write('\r' + str(ntree)) sys.stdout.flush() if ntree <= buffsize: PhyloIO.write(treebuffer, nfout, outchainfmt) else: treeappend(treebuffer, nfout, outchainfmt) treebuffer = [] if treebuffer: treeappend(treebuffer, nfout, outchainfmt) print '%s%s ...done (%d trees)' % (('\n' if verbose else ''), nfout, ntree) return None