ó
ž{ Ic           @   sÛ   d  d l  Z  d  d l Z d  d l Z d  d l Z y
 e Z Wn1 e k
 rm d  d l m Z d  d l m Z	 n Xd Z
 d Z d Z d Z d e f d „  ƒ  YZ d	 f  d
 „  ƒ  YZ d e j f d „  ƒ  YZ d e d „ Z d S(   iÿÿÿÿN(   t   Set(   t   ImmutableSeti   s   [&t   ]t	   TreeErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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
   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   __init__$   s    			N(   R   R   t   __doc__t   NoneR   (    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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' 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' 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
 | ƒ | j |  _ | rÓ | j ƒ  j d d ƒ j d d ƒ } | j d ƒ } |  j d | j d |  j | ƒ d ƒ n  d S(	   s   Ntree(self,tree).s   
R   s   t   ;t	   parent_idt   treei    N(   t   Nodest   ChainR   t	   dataclasst   _Tree__values_are_supportt   max_supportt   weightt   rootedt   namet   Nodet   addt   idt   roott   stript   replacet   rstript   _add_subtreet   _parse(	   R   R   R   R   R   t   datat   values_are_supportR   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyR   7   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(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyR$   J   sB    "c         C   sÆ  | d k r t d ƒ ‚ n  x¤| D]œ} t | d ƒ t k rz|  j ƒ  } t | d d t ƒ r‘ | d d j t ƒ r‘ | d j	 d ƒ | _
 n  t | d ƒ d k rã | d d | _ | d d d k	 rA| d d | _ qAn^ t | d ƒ d k rA|  j s-| d d d k	 r>| d d | _ q>qA| d d | _ n  t j | ƒ } |  j | | ƒ |  j | j | d ƒ q" |  j ƒ  } t | d d t ƒ rÍ| d d j t ƒ rÍ| d j	 d ƒ | _
 n  | d | _ t | ƒ d k rŸt | d ƒ d k r>| d d | _ | d d d k	 rœ| d d | _ qœqŸt | d ƒ d k rŸ|  j sˆ| d d d k	 r™| d d | _ q™qœ| d d | _ qŸn  t j | ƒ } |  j | | ƒ q" Wd S(   sK   Adds leaf or tree (in newick format) to a parent_id. (self,parent_id,tree).s   Need node_id to connect to.i    i   iÿÿÿÿi   N(   R   R   t   typet   listR   t
   isinstancet   strt
   startswithR-   t   popR
   t   lenR	   R   R   R   R   R   R#   R   R   (   R   R   R   t   stt   ndt   snt   leaf(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyR#   t   sF    .	.	c         C   s  | d k r d St | k rÊ | j t ƒ } | j t ƒ } | d k r_ t d t t f ƒ ‚ n  | | | d !} | |  | | d } g  | j d ƒ D] } | j ƒ  r– t | ƒ ^ q– } | j | ƒ n4 g  | j d ƒ D] } | j ƒ  rÚ t | ƒ ^ qÚ } | S(   s9   Extracts values (support/branchlength) from xx[:yyy], xx.R   iÿÿÿÿs7   Error in tree description: Found %s without matching %si   R)   N(	   R   R-   R,   t   NODECOMMENT_ENDR   t   splitR    t   floatR1   (   R   t   textt   nc_startt   nc_endR2   t   tt   values(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyR.   ž   s    44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   RO   t   nRE   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyRQ   ±   s    c         C   s-   | |  j  k r" t d | ƒ ‚ n  |  j  | S(   sS   Return the instance of node_id.
        
        node = node(self,node_id)
        s   Unknown node_id: %d(   t   chainR   (   R   t   node_id(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyRO   »   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   RS   R%   R0   R   R   R   R   R?   R   R1   R   (   R   R   RR   R   t   idst   parent_datat   iRO   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyRH   Ä   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(   RS   t   itemsR%   R   R   (   R   R   R   RO   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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(   RY   R   R   t   get_terminalst   unlinkt   killRB   RO   RP   R   R   R%   t   collapse(   R   R   R   R8   RP   t   new_bl(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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(   sv   Return a list of all otus downwards from a node (self, node_id).

        nodes = get_taxa(self,node_id=None)
        s   Unknown node_id: %d.N(	   R   R   RS   R   RP   R%   R   t   extendt   get_taxa(   R   RT   R=   RP   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyRa   ÿ   s    c         C   s5   g  |  j  ƒ  D]$ } |  j | ƒ j g  k r | ^ q S(   s$   Return a list of all terminal nodes.(   t   all_idsRO   RP   (   R   RW   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyRZ     s    c         C   s   |  j  | ƒ j g  k S(   s(   Returns True if node is a terminal node.(   RO   RP   (   R   RO   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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    (   RB   RO   RP   (   R   RO   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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(   Rc   t   FalseRO   RP   (   R   RO   RR   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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   RB   RQ   Rc   (   R   RO   RR   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   count_terminals&  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(	   si   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>RO   N(   t   TrueRQ   Rc   Ra   R!   RH   R1   RB   RO   R%   R   R\   RP   (	   R   t   space_equals_underscoreRR   t   taxat   generaRM   t   genust   knt
   nodes2kill(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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   RO   R%   R   R8   (   R   R   RO   t   blen(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   sum_branchlengthJ  s    c         C   sä   |  j  | ƒ j g  k r+ |  j  | ƒ j j Sy6 t g  |  j  | ƒ j D] } |  j | ƒ ^ qD ƒ SWn| | GH|  j  | ƒ j GHx, |  j  | ƒ j D] } | G|  j | ƒ GHq Wg  |  j  | ƒ j D] } |  j | ƒ ^ q¿ GH‚  n Xd S(   sW   Return subtree as a set of nested sets.

        sets = set_subtree(self,node)
        N(   RO   RP   R%   R   t	   frozensett   set_subtree(   R   RO   RR   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyRu   Z  s    6-c         C   s"   |  j  |  j ƒ | j  | j ƒ k S(   sX   Compare tree and tree2 for identity.

        result = is_identical(self,tree2)
        (   Ru   R   (   R   t   tree2(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   is_identicalm  s    c      
   C   s   t  |  j ƒ  ƒ t  | j ƒ  ƒ } t  | j ƒ  ƒ t  |  j ƒ  ƒ } | r­ | sV | r­ | rz d d j | ƒ |  j f GHn  | rž d d j | ƒ | j f GHn  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 rQ| j | ƒ j rQ| j | ƒ j j	 rQ| j | ƒ j j	 | k rQt  | j | ƒ ƒ | j | ƒ j j	 f ^ qQ} g  }	 x¸ | D]° \ }
 } x¡ | D]™ \ } } |
 j
 | ƒ rû| j
 |
 ƒ rû|
 | @| |
 |
 | } } } | r”| j
 | ƒ ph| j
 | ƒ r”|	 j |
 | | | | | | f ƒ q”qûqûWqèW|	 S(   s‚   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   setRa   t   joinR   R   Rb   RO   RP   R%   R	   t   issubsetR1   (   R   Rv   t	   thresholdt   strictt   missing2t   missing1RR   t   t1t   t2t   conflictt   st1t   sup1t   st2t   sup2t	   intersectt   notin1t   notin2(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   is_compatiblet  s,    ""rr  %0c         C   se   |  j  g |  j |  j  | ƒ } |  j  g |  j |  j  | ƒ } g  | D] } | | k rE | ^ qE d S(   sy   Return the common ancestor that connects two nodes.
        
        node_id = common_ancestor(self,node1,node2)
        iÿÿÿÿ(   R   t   trace(   R   t   node1t   node2t   l1t   l2RR   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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   Rs   (   R   R‹   RŒ   t   ca(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   distancež  s    c         C   s­   t  | t ƒ r! t | g ƒ } n t | ƒ } |  j } xp t |  j | ƒ ƒ } | | k r^ | SxD |  j | j D]. } t |  j | ƒ ƒ j | ƒ ro | } Pqo qo 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>   R?   Rx   R   Ra   RS   RP   t
   issuperset(   R   t
   taxon_listt	   taxon_setRT   t   subclade_taxat   subnode(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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   RB   RO   RP   t   is_bifurcatingRj   Re   (   R   RO   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyR˜   ¼  s    -<c         C   sW   xP |  j  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(   RS   t   keysRO   R%   R   R	   (   R   RR   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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(   RQ   RO   R%   R	   RI   (   R   t   nrepRR   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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(   RQ   RO   R%   R	   Rj   Re   (   R   RO   RR   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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(   RB   R0   R?   R   R   RZ   t   randomt   choiceRH   t   gaussRO   R%   R   R`   t   removet   shufflet   zipR   (   R   t   ntaxR“   R   t   branchlength_sdt	   bifurcateRW   t	   terminalst   newsplitt   new_terminalst   ntt   blRO   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt	   randomizeê  s0    0
	c   
   
   C   s€  d g } x?|  j  ƒ  D]1} |  j | ƒ } | j st | j t | ƒ d	 t | j ƒ t | j ƒ d	 d	 d	 d	 f ƒ q | j j } | s d	 } n  | j j } | d k r¶ d	 } d	 } n |  j
 d
 | ƒ } | j j } | d k ré d	 } n  | j j } | d k r
d	 } n  | j t | ƒ | t | j ƒ t | j ƒ | | | | f ƒ q Wd j g  | D] }	 d |	 ^ qXƒ GHd G|  j GHd S(   s#   Quick and dirty lists of all nodes.t   #R   R8   RP   t   brlens
   blen (sum)R	   R
   t   -RO   s   
s$   %3s %32s %15s %15s %8s %10s %8s %20ss   
Root: N(   R­   R   R8   RP   R®   s
   blen (sum)R	   R
   (   Rb   RO   R%   R1   R?   R8   RP   R   R   R   Rs   R	   R
   Ry   R   (
   R   t   tableRW   RR   t   txt   blengtht   sum_blengthR	   R
   t   l(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   display  s,    		@				A%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 | ƒ^ q$}	 | j d d j |	 ƒ ƒ | rj| d Sd j | ƒ d Sd S(   s   Return a paup compatible tree line.
       
        to_string(self,support_as_branchlengths=False,branchlengths_only=False,plain=True)
        c            sÈ   ˆ  j  r d Sˆ  j r5 | r' d ˆ  j Sd |  j Sn ˆ  j rI d |  j S| rZ d |  j S|  j d k	 rŒ |  j d k	 rŒ d |  j |  j f S|  j d k	 r¦ d |  j S|  j d k	 rÀ d |  j Sd Sd S(	   s/   Creates nicely formatted support/branchlengths.R   s   :%1.2fs   :%1.5fs   %1.2f:%1.5fs   0.00000:%1.5fs   %1.2f:0.00000s   0.00:0.00000N(   t   plaint   support_as_branchlengthsR   R	   t   branchlengths_onlyR   R   (   R%   t   terminal(   R   (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   make_info_string5  s"    			c            s’   | d k rˆ g  |  D] } ˆ  j  d | ƒ | f ^ q } | j ƒ  | d k sY | d k rf | j ƒ  n  | r t | Œ  d } qŽ g  } n |  } | S(   s=   Sorts node numbers according to the number of terminal nodes.t   leftt   LEFTt   rightt   RIGHTRO   i   (   R»   R¼   R½   R¾   (   Rg   t   sortt   reverseR£   (   t   nodest	   ladderizeRR   t   succnode_terminalst	   succnodes(   R   (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   ladderize_nodesM  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(   RO   RP   R%   R   Rj   Ry   (   RO   RÂ   RÄ   RE   R6   (   RÅ   Rº   t	   newickizeR   (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyRÆ   \  s
    /%R   t   a_treet   =i   s   [&W%s]i   s   [&R]RÂ   s   (%s)R*   iÿÿÿÿRh   R   N(   Re   R·   R¸   R¶   R   R   R1   R   R?   t   roundRI   R   RO   R   RP   Ry   (
   R   R·   R¸   R¶   t   plain_newickRÂ   t   treelineRÄ   RE   R6   (    (   RÅ   Rº   RÆ   R   s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt	   to_string)  s.    				
		,	%c         C   s   |  j  d t ƒ S(   s.   Short version of to_string(), gives plain treeR¶   (   RÌ   Rj   (   R   (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   __str__x  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(   RO   RP   R1   R%   R   R	   R`   (   RO   t   branchest   b(   t   _get_branchesR   (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyRÐ     s
    7i   i    i   i   s,   Support mismatch in bifurcating root: %f, %fN(   R   t   unrootedRB   RO   RP   RA   t   indexR   R1   R   RI   (   R   RÏ   t   rootbranchest   b1t   b2t	   newbranch(    (   RÐ   R   s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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Ñ   RA   R   t   linkRO   R%   R   R	   (   t   parentt   childRW   t   branchRÏ   t   child_branchesRP   (   t   _connect_subtreeR   (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/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—   RB   RO   RP   R×   RØ   RÑ   R8   RA   R   Rb   R   R   R   R   R   R1   Ry   R\   (
   R   t   outgroupt   outgroup_nodeRW   RÏ   t   root_brancht   ingroup_nodeRR   R   t   oldroot(    (   RÞ   R   s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   root_with_outgroupŸ  sD    K
,
'Fg      à?c   
      C   su  | r | r t  d ƒ ‚ n  | p$ | s6 t  d ƒ ‚ n  | d k r» y` |  j |  j ƒ j } t g  | D]! } t |  j | ƒ ƒ | f ^ qd ƒ } |  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)
        sA   Specify either list of boostrap trees or consensus tree, not boths8   Specify either list of boostrap trees or consensus tree.i   s   Error determining outgroup.R{   Rß   iÿÿÿÿN(   R   R   RO   R   RP   t   minRB   Ra   Rä   t	   consensusR   Rš   RQ   R—   R%   R	   (
   R   t   bstreest   constreeR{   Rß   RÄ   RR   t   smallestt   pnodet   cnode(    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   merge_with_supportã  s*    4N()   R   R   R   R   Re   R   R   R$   R#   R.   RQ   RO   RH   RY   R_   Ra   RZ   Rc   Rd   Rf   Rg   Rj   Rq   Rs   Ru   Rw   R‰   R   R‘   R—   R˜   Rš   Rœ   R   R¬   Rµ   RÌ   RÍ   R×   Rä   Rì   (    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyR   *   sF   
	**	
			
												
$	O		#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]n }
 |	 j |
 ƒ } | j
 ƒ  t | ƒ } | | k r| | c t |	 j ƒ | 7<q¹ t |	 j ƒ | | | <q¹ Wq_ Wx0 | j ƒ  D]" \ } } | | k  r<| | =q<q<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]Ö \ } } xT | | d D]7 } ˆ  j | ƒ j j j ˆ  j | ƒ j j ƒ rbPqbqbWt j d
 ƒ t  ˆ  j | ƒ j j ƒ d k røˆ  j | ƒ j j j ƒ  ˆ  j | ƒ j _ n d ˆ  j | ƒ j _ ˆ  j | | ƒ qGWd ˆ  j | d	 ƒ j _ ˆ  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ß   R   s   consensus_%2.1fR%   c            s2   t  ˆ  j |  ƒ j j ƒ t  ˆ  j | ƒ j j ƒ S(   N(   RB   RO   R%   R   (   t   xt   y(   Ræ   (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   <lambda>:  s    iÿÿÿÿs   corrupt tree structure?N(   RB   R   R   R   Rx   Ra   R   Rä   RQ   R   R¿   R?   RI   R   RX   R   R   R   R%   R	   t   evalR   R   RO   Rb   RØ   R’   t   syst   exitRA   RÙ   (   t   treesR{   Rß   t   totalR   R   t   cladest   alltaxat   cRM   t   st_nodeR•   R9   t   sRO   t   consensus_idsRW   t   currentRÚ   (    (   Ræ   s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyRæ     sX    

 -!-(   Rñ   Rž   t   copyR   Rx   t	   NameErrort   setsR    R   Rt   t   PRECISION_BRANCHLENGTHt   PRECISION_SUPPORTR-   RG   t	   ExceptionR   R   R   R   R   Ræ   (    (    (    s‚   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Trees.pyt   <module>   s    $
ÿ ÿ ß