ó
Y_Sc           @@ sà   d  Z  d d l m Z m Z d d l Z d d l Z d d l m Z e j d d k  rp d d l	 m
 Z
 e Z n  d Z d Z d	 Z d
 Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e j f d „  ƒ  YZ d e d „ Z d S(   s  Tree class to handle phylogenetic trees.

Provides a set of methods to read and write newick-format tree descriptions,
get information about trees (monphyly of taxon sets, congruence between trees,
common ancestors,...) and to manipulate trees (reroot trees, split terminal
nodes).
i    (   t   absolute_importt   print_functionNi   (   t   Nodesi   (   t   zipi   s   [&t   ]t	   TreeErrorc           B@ s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyR   "   s   t   NodeDatac           B@ s#   e  Z d  Z d d d d d „ Z RS(   sH   Stores tree-relevant data associated with nodes (e.g. branches or otus).g        c         C@ s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   taxont   branchlengtht   supportt   comment(   t   selfR	   R
   R   R   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   __init__(   s    			N(   R   R   t   __doc__t   NoneR   (    (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyR   &   s   t   Treec           B@ s¸  e  Z d  Z d( d e d e e d d „ Z d „  Z d( d( d „ Z d „  Z	 d „  Z
 d( d „ Z d	 „  Z d( d
 d d „ Z d „  Z d „  Z d( d „ Z d „  Z d „  Z d „  Z d „  Z d( d „ Z e d „ Z d( d( d „ Z d „  Z d „  Z e d „ Z d „  Z d „  Z d „  Z d( d „ Z d „  Z  d „  Z! d( d „ Z" d( d( d d( e d  „ Z# d! „  Z$ e e e e d( e d" „ Z% d# „  Z& d$ „  Z' d( d% „ Z( d( d( d& d( d' „ Z) RS()   sw   Represents a tree using a chain of nodes with on predecessor (=ancestor)
    and multiple successors (=subclades).
    g      ð?t    c         C@ sö   t  j j |  ƒ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ t  j	 | ƒ  ƒ } |  j
 | ƒ |  _ | rò | j ƒ  j d d ƒ j d d ƒ } | j d ƒ } |  j | ƒ \ }	 }
 |  j | j g  |
 g ƒ | _ |  j d | j d |	 ƒ n  d S(   s   Ntree(self,tree).s   
R   s   t   ;t	   parent_idt   treeN(   R   t   ChainR   t	   dataclasst   _Tree__values_are_supportt   max_supportt   weightt   rootedt   namet   Nodet   addt   roott   stript   replacet   rstript   _parset   _add_nodedatat   datat   _add_subtreet   id(   R   R   R   R   R   R%   t   values_are_supportR   R   t   subtree_infot	   base_info(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyR   <   s    						$c         C@ sg  | j  ƒ  } | j d ƒ | j d ƒ k r= t d | ƒ ‚ n  | j d ƒ d k rH| j t ƒ } | j d ƒ } | d k r• | d k r• | d	 g g S| d k rÈ | d k rÈ | |  |  j | | ƒ g S| d k rÿ | d k rÿ | |  |  j | | d ƒ g S| | k  r*| |  |  j | | d ƒ g S| |  |  j | | ƒ g Sn| j d ƒ } |  j | | d ƒ } | s€d	 g } n  g  } d } d } xŽ t d | ƒ D]} }	 | |	 d k rÅ| d 7} q¢| |	 d k râ| d 8} q¢| |	 d k r¢| d k r¢| j	 | | |	 !ƒ |	 d } q¢q¢W| j	 | | | !ƒ g  | D] }
 |  j
 |
 ƒ ^ q>} | | g Sd	 S(
   sM   Parses (a,b,c...)[[[xx]:]yy] into subcomponents and travels down recursively.t   (t   )s'   Parentheses do not match in (sub)tree: i    t   :iÿÿÿÿi   t   ,N(   R    t   countR   t   findt   NODECOMMENT_STARTR   t   _get_valuest   rfindt   ranget   appendR#   (   R   R   t   nodecommentt   colont   closingt   valt   subtreest   plevelt   prevt   pt   subtreet	   subclades(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyR#   P   sB    "c         C@ sÆ   | d k r t d ƒ ‚ n  x¤ | D]œ } |  j ƒ  } |  j | | ƒ } t | d t ƒ r’ t j | ƒ } |  j | | ƒ |  j	 | j
 | d ƒ q" | d | _ t j | ƒ } |  j | | ƒ q" Wd S(   s4   Adds leaf or tree (in newick format) to a parent_id.s   Need node_id to connect to.i    N(   R   R   R   R$   t
   isinstancet   listR   R   R   R&   R'   R	   (   R   R   R   t   stt   ndt   snt   leaf(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyR&   y   s    c         C@ sM  t  | d d t ƒ rG | d d j t ƒ rG | d j d ƒ | _ n= t  | d d t ƒ r„ | d d | _ | d d | d <n  t | ƒ d k rIt | d ƒ d k rè | d d | _ | d d d k	 rF| d d | _
 qFqIt | d ƒ d k rI|  j s2| d d d k	 rC| d d | _
 qCqF| d d | _ qIn  | S(   sJ   Add data to the node parsed from the comments, taxon and support.
        i   iÿÿÿÿi    i   N(   R@   t   strt
   startswithR1   t   popR   R	   t   lenR   R   R
   R   (   R   RC   RB   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyR$   ‰   s     .	c   	      C@ sS  | d k r d Sd } t | k r | j t ƒ } | j t ƒ } | d k re t d t t f ƒ ‚ n  | | | d !} | |  | | d } n  g  } d } x‚ g  | j d ƒ D] } | j ƒ  ^ q® D]X } | rÄ y | j t | ƒ ƒ Wqt	 k
 r| d k st
 d ƒ ‚ | } qXqÄ qÄ W| r9| j d | ƒ n  | rO| j | ƒ n  | S(	   s9   Extracts values (support/branchlength) from xx[:yyy], xx.R   iÿÿÿÿs7   Error in tree description: Found %s without matching %si   R-   s   Two string taxonomies?i    N(   R   R1   R0   t   NODECOMMENT_ENDR   t   splitR    R5   t   floatt
   ValueErrort   AssertionErrort   insert(	   R   t   textR6   t   nc_startt   nc_endt   valuest   taxonomyt   tt   part(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyR2   Ÿ   s2    /c         c@ s]   | d k r |  j } n  x> |  j | ƒ j D]* } | Vx |  j | ƒ D] } | VqF Wq+ Wd S(   s*   Return all node_ids downwards from a node.N(   R   R   t   nodet   succt   _walk(   R   RW   t   nRD   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyRY   ¾   s    c         C@ s-   | |  j  k r" t d | ƒ ‚ n  |  j  | S(   sK   Return the instance of node_id.

        node = node(self,node_id)
        s   Unknown node_id: %d(   t   chainR   (   R   t   node_id(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyRW   È   s    i   c         C@ sº   | d k r t d ƒ ‚ n  g  } |  j | j } x‚ t | ƒ D]t } t j ƒ  } | r™ |  j ƒ  | _ | j rŠ | j t	 | ƒ | j _ n  | | j _
 n  | j |  j | | ƒ ƒ q> W| S(   s‹   Speciation: generates n (default two) descendants of a node.

        [new ids] = split(self,parent_id=None,n=2,branchlength=1.0):
        s   Missing node_id.N(   R   R   R[   R%   R4   R   R   R   R	   RF   R
   R5   R   (   R   R   RZ   R
   t   idst   parent_datat   iRW   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyRK   Ñ   s    	c         C@ s:   x3 |  j  j ƒ  D]" \ } } | j j | k r | Sq Wd S(   s‹   Returns the first matching taxon in self.data.taxon. Not restricted to terminal nodes.

        node_id = search_taxon(self,taxon)
        N(   R[   t   itemsR%   R	   R   (   R   R	   R'   RW   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   search_taxonå   s    c         C@ sH  |  j  | ƒ } | d k r. t d | ƒ ‚ n| |  j ƒ  k rS t d | ƒ ‚ nñ |  j | ƒ } |  j | ƒ t |  j | ƒ j ƒ d k r@| |  j	 k rÝ |  j |  j	 ƒ j d |  _	 d |  j |  j	 ƒ _
 |  j | ƒ q@|  j | ƒ j d } |  j | ƒ j j
 |  j | ƒ j j
 } |  j | ƒ | |  j | ƒ j _
 n  | Sd S(   s"  Prunes a terminal taxon from the tree.

        id_of_previous_node = prune(self,taxon)
        If taxon is from a bifurcation, the connectiong node will be collapsed
        and its branchlength added to remaining terminal node. This might be no
        longer a meaningful value'
        s   Taxon not found: %ss   Not a terminal taxon: %si   i    g        N(   Ra   R   R   t   get_terminalst   unlinkt   killRI   RW   RX   R   R
   R%   t   collapse(   R   R	   R'   R<   RX   t   new_bl(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   pruneï   s"    	(c         C@ sº   | d k r |  j } n  | |  j k r: t d | ƒ ‚ n  |  j | j g  k r{ |  j | j rt |  j | j j g Sd Sn; g  } x. |  j | j D] } | j |  j | ƒ ƒ q’ W| Sd S(   sf   Return a list of all otus downwards from a node.

        nodes = get_taxa(self,node_id=None)
        s   Unknown node_id: %d.N(	   R   R   R[   R   RX   R%   R	   t   extendt   get_taxa(   R   R\   RA   RX   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyRi     s    c         C@ s5   g  |  j  ƒ  D]$ } |  j | ƒ j g  k r | ^ q S(   s$   Return a list of all terminal nodes.(   t   all_idsRW   RX   (   R   R_   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyRb   !  s    c         C@ s   |  j  | ƒ j g  k S(   s(   Returns True if node is a terminal node.(   RW   RX   (   R   RW   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   is_terminal%  s    c         C@ s   t  |  j | ƒ j ƒ d k S(   s)   Returns True if node is an internal node.i    (   RI   RW   RX   (   R   RW   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   is_internal)  s    c         C@ sI   |  j  | ƒ rA t g  |  j | ƒ j D] } |  j  | ƒ ^ q% k St Sd S(   s;   Returns True if all successors of a node are terminal ones.N(   Rk   t   FalseRW   RX   (   R   RW   RZ   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   is_preterminal-  s    2c         C@ sM   | d k r |  j } n  t g  |  j | ƒ D] } |  j | ƒ r+ | ^ q+ ƒ S(   s@   Counts the number of terminal nodes that are attached to a node.N(   R   R   RI   RY   Rk   (   R   RW   RZ   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   count_terminals4  s    c   	      C@ sM  xFt  rHx9|  j ƒ  D]*} |  j | ƒ r1 q n  |  j | ƒ } g  } xo | D]g } | rn | j d d ƒ } n  y | j d d ƒ d } Wn d } n X| | k rM | j | ƒ qM qM Wt | ƒ d k r | d d |  j | ƒ j	 _
 g  |  j d | ƒ D] } | ^ qú } x | D] } |  j | ƒ qWg  |  j | ƒ _ Pq q WPq Wd S(	   sj   Collapses all subtrees which belong to the same genus (i.e share the same first word in their taxon name.)t    t   _i   i    R   s    <collapsed>RW   N(   t   TrueRY   Rk   Ri   R!   RK   R5   RI   RW   R%   R	   Rd   RX   (	   R   t   space_equals_underscoreRZ   t   taxat   generaRU   t   genust   knt
   nodes2kill(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   collapse_genera:  s.    	
%c         C@ s‡   | d k r |  j } n  | d k r3 t d ƒ ‚ n  d } xG | d k	 r‚ | | k	 r‚ | |  j | ƒ j j 7} |  j | ƒ j } q< W| S(   sƒ   Adds up the branchlengths from root (default self.root) to node.

        sum = sum_branchlength(self,root=None,node=None)
        s   Missing node id.g        N(   R   R   R   RW   R%   R
   R<   (   R   R   RW   t   blen(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   sum_branchlengthW  s    c         @ sò   ˆ  j  | ƒ j g  k r+ ˆ  j  | ƒ j j Sy* t ‡  f d †  ˆ  j  | ƒ j Dƒ ƒ SWn– t | ƒ t ˆ  j  | ƒ j ƒ x7 ˆ  j  | ƒ j D]# } t d | ˆ  j | ƒ f ƒ qŽ Wt g  ˆ  j  | ƒ j D] } ˆ  j | ƒ ^ qË ƒ ‚  n Xd S(   sW   Return subtree as a set of nested sets.

        sets = set_subtree(self,node)
        c         3@ s   |  ] } ˆ  j  | ƒ Vq d  S(   N(   t   set_subtree(   t   .0RZ   (   R   (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pys	   <genexpr>q  s    s   %s %sN(   RW   RX   R%   R	   t	   frozensett   printR|   (   R   RW   RZ   (    (   R   sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyR|   g  s    *
!2c         C@ s"   |  j  |  j ƒ | j  | j ƒ k S(   sX   Compare tree and tree2 for identity.

        result = is_identical(self,tree2)
        (   R|   R   (   R   t   tree2(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   is_identicalz  s    c         C@ sª  t  |  j ƒ  ƒ t  | j ƒ  ƒ } t  | j ƒ  ƒ t  |  j ƒ  ƒ } | r· | sV | r· | r t d d j | ƒ |  j f ƒ n  | r¨ t d d j | ƒ | j f ƒ n  t d ƒ ‚ n  g  |  j ƒ  D]„ } |  j | ƒ j rÄ |  j | ƒ j	 rÄ |  j | ƒ j	 j
 rÄ |  j | ƒ j	 j
 | k rÄ t  |  j | ƒ ƒ |  j | ƒ j	 j
 f ^ qÄ } g  | j ƒ  D]„ } | j | ƒ j r[| j | ƒ j	 r[| j | ƒ j	 j
 r[| j | ƒ j	 j
 | k r[t  | j | ƒ ƒ | j | ƒ j	 j
 f ^ q[} g  }	 x¸ | D]° \ }
 } x¡ | D]™ \ } } |
 j | ƒ r| j |
 ƒ r|
 | @| |
 |
 | } } } | rž| j | ƒ pr| j | ƒ rž|	 j |
 | | | | | | f ƒ qžqqWqòW|	 S(   sz   Compares branches with support>threshold for compatibility.

        result = is_compatible(self,tree2,threshold)
        s'   Taxon/taxa %s is/are missing in tree %sR.   s6   Can't compare trees with different taxon compositions.(   t   setRi   R   t   joinR   R   Rj   RW   RX   R%   R   t   issubsetR5   (   R   R€   t	   thresholdt   strictt   missing2t   missing1RZ   t   t1t   t2t   conflictt   st1t   sup1t   st2t   sup2t	   intersectt   notin1t   notin2(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   is_compatible  s,    ""##rr  %0c         C@ se   |  j  g |  j |  j  | ƒ } |  j  g |  j |  j  | ƒ } g  | D] } | | k rE | ^ qE d S(   sq   Return the common ancestor that connects two nodes.

        node_id = common_ancestor(self,node1,node2)
        iÿÿÿÿ(   R   t   trace(   R   t   node1t   node2t   l1t   l2RZ   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   common_ancestor   s    c         C@ s2   |  j  | | ƒ } |  j | | ƒ |  j | | ƒ S(   sq   Add and return the sum of the branchlengths between two nodes.
        dist = distance(self,node1,node2)
        (   R™   R{   (   R   R•   R–   t   ca(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   distanceª  s    c         C@ s³   t  | t ƒ r! t | g ƒ } n t | ƒ } |  j } xv t r® t |  j | ƒ ƒ } | | k rd | SxD |  j | j D]. } t |  j | ƒ ƒ j | ƒ ru | } Pqu qu Wd Sq9 Wd S(   sŠ   Return node_id of common ancestor if taxon_list is monophyletic, -1 otherwise.

        result = is_monophyletic(self,taxon_list)
        iÿÿÿÿN(	   R@   RF   R‚   R   Rr   Ri   R[   RX   t
   issuperset(   R   t
   taxon_listt	   taxon_setR\   t   subclade_taxat   subnode(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   is_monophyletic²  s    		c         C@ s$  | d k r |  j } n  | |  j k r  t |  j | ƒ j ƒ d k r  |  j |  j | ƒ j d ƒ oŸ |  j |  j | ƒ j d ƒ oŸ |  j |  j | ƒ j d ƒ St |  j | ƒ j ƒ d k rú |  j |  j | ƒ j d ƒ où |  j |  j | ƒ j d ƒ St |  j | ƒ j ƒ d k rt St Sd S(   s?   Return True if tree downstream of node is strictly bifurcating.i   i    i   i   N(   R   R   RI   RW   RX   t   is_bifurcatingRr   Rm   (   R   RW   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyR¢   È  s    -<c         C@ sQ   xJ |  j  D]? } |  j | ƒ j j |  j | ƒ j _ d |  j | ƒ j _ q
 Wd S(   så   Move values stored in data.branchlength to data.support, and set branchlength to 0.0

        This is necessary when support has been stored as branchlength (e.g. paup), and has thus
        been read in as branchlength.
        g        N(   R[   RW   R%   R
   R   (   R   RZ   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   branchlength2support×  s    $c         C@ sT   xM |  j  ƒ  D]? } |  j | ƒ j j r |  j | ƒ j j t | ƒ :_ q q Wd S(   sÆ   Convert absolute support (clade-count) to rel. frequencies.

        Some software (e.g. PHYLIP consense) just calculate how often clades appear, instead of
        calculating relative frequencies.N(   RY   RW   R%   R   RL   (   R   t   nrepRZ   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   convert_absolute_supportâ  s    c         C@ s;   x4 |  j  | ƒ D] } |  j | ƒ j j r t Sq Wt Sd S(   s:   Returns True if any of the nodes has data.support != None.N(   RY   RW   R%   R   Rr   Rm   (   R   RW   RZ   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   has_supportì  s    c         C@ s£  | r | r t  | ƒ } n{ | rY | rY g  t | ƒ D] } d t | d ƒ ^ q6 } n> | rv | rv t d ƒ ‚ n! | t  | ƒ k r— t d ƒ ‚ n  |  j ƒ  |  j ƒ  } x­ t  | ƒ | k  r\t j | ƒ } |  j d | d | ƒ }	 | r?xM |	 D]B }
 t j	 | | ƒ } | d k  r#d } n  | |  j
 |
 ƒ j _ qö Wn  | j |	 ƒ | j | ƒ q° Wt j | ƒ x2 t | | ƒ D]! \ } } | |  j
 | ƒ j _ qzWd S(	   s  Generates a random tree with ntax taxa and/or taxa from taxlabels.

        new_tree = randomize(self,ntax=None,taxon_list=None,branchlength=1.0,branchlength_sd=None,bifurcate=True)
        Trees are bifurcating by default. (Polytomies not yet supported).
        R	   i   s7   Either numer of taxa or list of taxa must be specified.s-   Length of taxon list must correspond to ntax.R   R
   i    N(   RI   R4   RF   R   R   Rb   t   randomt   choiceRK   t   gaussRW   R%   R
   Rh   t   removet   shuffleR   R	   (   R   t   ntaxR   R
   t   branchlength_sdt	   bifurcateR_   t	   terminalst   newsplitt   new_terminalst   ntt   blRW   R   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt	   randomizeô  s0    0
	c   	   
   C@ s•  d g } xWt  |  j ƒ  ƒ D]C} |  j | ƒ } | j sz | j t | ƒ d	 t | j ƒ t | j ƒ d	 d	 d	 d	 f ƒ q | j j } | s• d	 } n  d
 | j j	 } | d k rÀ d	 } d	 } n d
 |  j d | ƒ } | j j } | d k r÷ d	 } n
 d
 | } | j j } | d k r"d	 } n  | j t | ƒ | t | j ƒ t | j ƒ | | | | f ƒ q Wt d j d „  | Dƒ ƒ ƒ t d |  j ƒ d S(   s#   Quick and dirty lists of all nodes.t   #R	   R<   RX   t   brlens
   blen (sum)R   R   t   -s   %0.2fRW   s   
c         s@ s   |  ] } d  | Vq d S(   s$   %3s %32s %15s %15s %8s %10s %8s %20sN(    (   R}   t   l(    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pys	   <genexpr>4  s    s
   
Root:  %sN(   Rµ   s   taxons   prevs   succR¶   s
   blen (sum)s   supports   comment(   t   sortedRj   RW   R%   R5   RF   R<   RX   R	   R
   R   R{   R   R   R   Rƒ   R   (	   R   t   tableR_   RZ   t   txt   blengtht   sum_blengthR   R   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   display  s0    		@			
	*c         @ s‹  | s | r t  } n  | ˆ _ | ˆ _ ˆ  ˆ _ | ˆ _ t  ‡  ‡ f d † ‰ d ‡ f d † ‰ d ‡ ‡ ‡ ‡ f d † ‰ d g } ˆ j r  | j ˆ j ƒ n | j d ƒ | j d ƒ ˆ j d k rõ | j d t	 t
 t ˆ j ƒ d	 ƒ ƒ ƒ n  ˆ j r| j d
 ƒ n  ˆ ˆ j ˆ j ƒ j ƒ } g  | D] }	 ˆ |	 d | ƒ^ q0}
 | j d d j |
 ƒ ƒ | rv| d Sd j | ƒ d Sd S(   s#   Return a paup compatible tree line.c         @ s-  ˆ j  r d } në ˆ j rS | r1 d ˆ j } qý |  j rJ d |  j } qý d } nª ˆ j rl d |  j } n‘ | r‚ d |  j } n{ |  j d
 k	 r¹ |  j d
 k	 r¹ d |  j |  j f } nD |  j d
 k	 rØ d |  j } n% |  j d
 k	 r÷ d |  j } n d } ˆ  r)t |  d	 ƒ r)t |  j	 ƒ | } n  | S(   s/   Creates nicely formatted support/branchlengths.R   s   :%1.2fs   :0.00s   :%1.5fs   %1.2f:%1.5fs   0.00000:%1.5fs   %1.2f:0.00000s   0.00:0.00000R6   N(
   t   plaint   support_as_branchlengthsR   R   t   branchlengths_onlyR
   R   t   hasattrRF   R6   (   R%   t   terminalt   info_string(   t   ignore_commentsR   (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   make_info_stringA  s,    						c         @ sy   | d k ro t  ‡  f d †  |  Dƒ ƒ } | d k s@ | d k rM | j ƒ  n  | rf t | Œ  d } qu g  } n |  } | S(   s=   Sorts node numbers according to the number of terminal nodes.t   leftt   LEFTt   rightt   RIGHTc         3@ s'   |  ] } ˆ  j  d  | ƒ | f Vq d S(   RW   N(   Ro   (   R}   RZ   (   R   (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pys	   <genexpr>b  s    i   (   s   leftRÈ   s   rightRÊ   (   R¹   t   reverseR   (   t   nodest	   ladderizet   succnode_terminalst	   succnodes(   R   (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   ladderize_nodes_  s    	c         @ s±   ˆ j  |  ƒ j sA ˆ j  |  ƒ j j ˆ ˆ j  |  ƒ j d t ƒSˆ  ˆ j  |  ƒ j d | ƒ} g  | D] } ˆ | d | ƒ^ qf } d d j | ƒ ˆ ˆ j  |  ƒ j ƒ f Sd S(   s1   Convert a node tree to a newick tree recursively.RÃ   RÍ   s   (%s)%sR.   N(   RW   RX   R%   R	   Rr   Rƒ   (   RW   RÍ   RÏ   RD   R:   (   RÐ   RÆ   t	   newickizeR   (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyRÑ   m  s
    /%R   t   a_treet   =i   s   [&W%s]i   s   [&R]RÍ   s   (%s)R.   iÿÿÿÿRp   R   N(   Rm   RÀ   RÁ   RÅ   R¿   R   R   R5   R   RF   t   roundRL   R   RW   R   RX   Rƒ   (   R   RÀ   RÁ   R¿   t   plain_newickRÍ   RÅ   t   treelineRÏ   RD   R:   (    (   RÅ   RÐ   RÆ   RÑ   R   sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt	   to_string7  s0    					
		,	%c         C@ s   |  j  d t ƒ S(   s.   Short version of to_string(), gives plain treeR¿   (   R×   Rr   (   R   (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   __str__‰  s    c         @ sÈ  ‡  ‡ f d †  ‰  ˆ  ˆ j  ƒ ˆ _ t ˆ j ˆ j  ƒ j ƒ d k rÄg  ˆ j D] } ˆ j  | d  k rO | ^ qO } ˆ j j ˆ j j | d ƒ ƒ } ˆ j j ˆ j j | d ƒ ƒ } | d | d | d | d g } | d d k rÿ | j | d ƒ n² | d d k r#| j | d ƒ nŽ | d | d k rK| j | d ƒ nf | d d k sk| d d k r‡| j | d | d ƒ n* t	 d t
 | d ƒ t
 | d ƒ f ƒ ‚ ˆ j j | ƒ n  d S(   s?   Defines a unrooted Tree structure, using data of a rooted Tree.c         @ sq   g  } xd ˆ j  |  ƒ j D]P } | j |  | ˆ j  | ƒ j j ˆ j  | ƒ j j g ƒ | j ˆ  | ƒ ƒ q W| S(   N(   RW   RX   R5   R%   R
   R   Rh   (   RW   t   branchest   b(   t   _get_branchesR   (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyRÛ   ’  s
    7i   i    i   i   s,   Support mismatch in bifurcating root: %f, %fN(   R   t   unrootedRI   RW   RX   RH   t   indexR   R5   R   RL   (   R   RÚ   t   rootbranchest   b1t   b2t	   newbranch(    (   RÛ   R   sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   unroot  s$    !/""# $c   
      @ s¥  ‡  ‡ f d †  ‰  | d  k r% ˆ j Sˆ j | ƒ } | d k rD d St ˆ j ˆ j ƒ j ƒ d k r€ | ˆ j ˆ j ƒ j k s | ˆ j k r– ˆ j Sˆ j ƒ  xk t ˆ j ƒ D]N \ } } | | d  k r° ˆ j | ƒ j	 | d  k r° ˆ j j
 | ƒ } Pq° q° Wt d ƒ ‚ | | d k r+| d } n
 | d } x8 ˆ j ƒ  D]* } d  ˆ j | ƒ _	 g  ˆ j | ƒ _ qBWt j d t ƒ  ƒ } ˆ j | ƒ | j ˆ _ ˆ j j | j | | d | d g ƒ ˆ j j | j | d	 d	 g ƒ ˆ  | j | ƒ ˆ  | j | ƒ g  ˆ j ƒ  D]3 } ˆ j | ƒ j	 d  k r| ˆ j k r| ^ q}	 t |	 ƒ d k rxt d
 d j |	 ƒ ƒ ‚ n& t |	 ƒ d k ržˆ j |	 d ƒ n  ˆ j S(   Nc         @ s#  xi t  ˆ j ƒ D]B \ } } |  | d  k r | | d  k r ˆ j j | ƒ } Pq q Wt d |  | f ƒ ‚ ˆ j |  | ƒ | d ˆ j | ƒ j _ | d ˆ j | ƒ j _ g  ˆ j D] } | | d  k r¸ | ^ q¸ } xB | D]: } | | d k r| d } n
 | d } ˆ  | | ƒ qá Wd S(   s0   Hook subtree starting with node child to parent.i   sF   Unable to connect nodes for rooting: nodes %d and %d are not connectedi   i    i   N(	   t	   enumerateRÜ   RH   R   t   linkRW   R%   R
   R   (   t   parentt   childR_   t   branchRÚ   t   child_branchesRX   (   t   _connect_subtreeR   (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyRé   ²  s     ,
iÿÿÿÿi   s%   Unrooted and rooted Tree do not matchi   i    R%   i   g        s&   Isolated nodes in tree description: %sR.   (   R   R   R¡   RI   RW   RX   Râ   Rã   RÜ   R<   RH   R   Rj   R   R   R   R   R'   R5   Rƒ   Rd   (
   R   t   outgroupt   outgroup_nodeR_   RÚ   t   root_brancht   ingroup_nodeRZ   R   t   oldroot(    (   Ré   R   sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   root_with_outgroup°  sD    K
,
'Fg      à?c   	      @ s]  | r | r t  d ƒ ‚ n  | p$ | s6 t  d ƒ ‚ n  | d	 k r£ yH ˆ  j ˆ  j ƒ j } t ‡  f d †  | Dƒ ƒ } ˆ  j | d ƒ } Wq° t  d ƒ ‚ q° Xn ˆ  j | ƒ | rÑ t | d | d | ƒ} n& | j	 ƒ  sê | j
 ƒ  n  | j | ƒ x_ ˆ  j ƒ  D]Q } | j ˆ  j | ƒ ƒ } | d k r| j | ƒ j j ˆ  j | ƒ j _ qqWd	 S(
   sï   Merges clade support (from consensus or list of bootstrap-trees) with phylogeny.

        tree=merge_bootstrap(phylo,bs_tree=<list_of_trees>)
        or
        tree=merge_bootstrap(phylo,consree=consensus_tree with clade support)
        sB   Specify either list of bootstrap trees or consensus tree, not boths9   Specify either list of bootstrap trees or consensus tree.c         3@ s*   |  ]  } t  ˆ  j | ƒ ƒ | f Vq d  S(   N(   RI   Ri   (   R}   RZ   (   R   (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pys	   <genexpr>  s    i   s   Error determining outgroup.R…   Rê   iÿÿÿÿN(   R   R   RW   R   RX   t   minRi   Rï   t	   consensusR¦   R£   RY   R¡   R%   R   (	   R   t   bstreest   constreeR…   Rê   RÏ   t   smallestt   pnodet   cnode(    (   R   sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   merge_with_supportô  s*    N(*   R   R   R   R   Rm   R   R   R#   R&   R$   R2   RY   RW   RK   Ra   Rg   Ri   Rb   Rk   Rl   Rn   Ro   Rr   Ry   R{   R|   R   R“   R™   R›   R¡   R¢   R£   R¥   R¦   R´   R¾   R×   RØ   Râ   Rï   R÷   (    (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyR   /   sH   
	)		
			
								
				
$	R		#Dg      à?c         @ s  t  |  ƒ } | d k r d S|  d j } |  d j } i  } t |  d j ƒ  ƒ } d } xÐ |  D]È }	 | d 7} | t |	 j ƒ  ƒ k r– t d ƒ ‚ n  |	 j d | ƒ x~ |	 j |	 j	 ƒ D]j }
 t
 |	 j |
 ƒ ƒ } t | ƒ } | | k r| | c t |	 j ƒ | 7<q¹ t |	 j ƒ | | | <q¹ Wq_ Wg  | j ƒ  D]' \ } } t | d ƒ | k  r8| ^ q8} x | D] } | | =qlWt d d t | ƒ d | ƒ ‰  x` | j ƒ  D]R \ } } t j d | ƒ  ƒ } | | j _ t t | ƒ ƒ | j _ ˆ  j | ƒ q©Wd ˆ  j ˆ  j	 ƒ j _ | ˆ  j ˆ  j	 ƒ j _ ˆ  j ƒ  } | j ‡  f d	 †  ƒ xç t | d
  ƒ D]Õ \ } } xS | | d D]7 } ˆ  j | ƒ j j j ˆ  j | ƒ j j ƒ r}Pq}q}Wt d ƒ ‚ t  ˆ  j | ƒ j j ƒ d k rˆ  j | ƒ j j j ƒ  ˆ  j | ƒ j _ n d ˆ  j | ƒ j _ ˆ  j | | ƒ qbWd ˆ  j | d
 ƒ j _ | t ˆ  j ƒ  ƒ k r{t d ƒ ‚ n  ˆ  S(   sm   Compute a majority rule consensus tree of all clades with relative frequency>=threshold from a list of trees.i    i   s.   Trees for consensus must contain the same taxaRê   i   R   s   consensus_%2.1fR%   c         @ s2   t  ˆ  j |  ƒ j j ƒ t  ˆ  j | ƒ j j ƒ S(   N(   RI   RW   R%   R	   (   t   xt   y(   Rñ   (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   <lambda>I  s    iÿÿÿÿs   corrupt tree structure?s&   FATAL ERROR: consensus tree is corruptN(   RI   R   R   R   R‚   Ri   R   Rï   RY   R   R¹   RF   RL   R   R`   RÔ   R   R   R   R%   R   t   evalR	   R   RW   Rj   t   sortRã   Rœ   RH   Rä   (   t   treesR…   Rê   t   totalR   R   t   cladest   alltaxat   cRU   t   st_nodeRŸ   R=   t	   delcladest   sRW   t   consensus_idsR_   t   currentRå   (    (   Rñ   sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyRñ     sZ    
 :-!-(   R   t
   __future__R    R   R§   t   sysR   R   t   version_infot   future_builtinsR   t   xrangeR4   t   PRECISION_BRANCHLENGTHt   PRECISION_SUPPORTR1   RJ   t	   ExceptionR   t   objectR   R   R   R   Rñ   (    (    (    sE   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/_nexus/Trees.pyt   <module>   s"   		ÿ ÿ ë