
{ Ic           @   sX  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z d d l m Z m Z y d d l Z Wn e k
 r e Z n Xe Z y
 e Z Wn! e k
 r d d l m Z n Xd Z d	 d
 d d d d d d d d d d d g Z d d d d d d g Z d Z d Z d Z d g Z d  Z d Z  d! Z! d" Z" d# e# f d$     YZ$ d% d= d&     YZ% d' d> d(     YZ& e d)  Z' d*   Z( d+ d, g d-  Z) d.   Z* d/   Z+ d0   Z, d1   Z- d2   Z. d3   Z/ d4   Z0 d5   Z1 d6   Z2 d7 d? d8     YZ3 d9 d@ d:     YZ4 d; e5 f d<     YZ6 d S(A   s   Nexus class. Parse the contents of a nexus file.
Based upon 'NEXUS: An extensible file format for systematic information'
Maddison, Swofford, Maddison. 1997. Syst. Biol. 46(4):590-621
iN(   t   IUPAC(   t	   IUPACData(   t   Seq(   t   Treet   NodeData(   t   SetiF   t   charstatelabelst
   charlabelst	   taxlabelst   taxsett   charsett   charpartitiont   taxpartitiont   matrixt   treet   utreet	   translatet   codonpossett   titlet   treest   datat
   characterst   taxat   setst   codonss   ()[]{}/\,;:=*'"`+-<>t?   abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_s    	
t   &t   charst   codonpositionssH   #NEXUS
begin data; dimensions ntax=0 nchar=0; format datatype=dna; end; t
   NexusErrorc           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/Nexus.pyR   3   s    t
   CharBufferc           B   sh   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z RS(   s7   Helps reading NEXUS-words and characters from a buffer.c         C   s%   | r t  |  |  _ n	 g  |  _ d  S(   N(   t   listt   buffer(   t   selft   string(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   __init__7   s    c         C   s   |  j  r |  j  d Sd  Sd  S(   Ni    (   R"   t   None(   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/Nexus.pyt   peek=   s    	c         C   s.   d j  |  j  j   } | r& | d Sd  Sd  S(   Nt    i    (   t   joinR"   t   stripR&   (   R#   t   b(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   peek_nonwhitespaceC   s    c         C   s!   |  j  r |  j  j d  Sd  Sd  S(   Ni    (   R"   t   popR&   (   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/Nexus.pyt   nextJ   s    	c         C   s=   x6 t  r8 |  j   } | d  k r% Pn  | t k r | Sq Wd  S(   N(   t   TrueR.   R&   t
   WHITESPACE(   R#   t   p(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   next_nonwhitespaceP   s    	c         C   s.   x' |  j  d t k r) |  j  d |  _  q Wd  S(   Ni    i   (   R"   R0   (   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/Nexus.pyt   skip_whitespaceY   s    c         C   sm   xf | D]Z } y |  j  j |  } Wn t k
 r6 q Xd j |  j  |   } |  j  | |  _  | Sq Wd  Sd  S(   NR(   (   R"   t   indext
   ValueErrorR)   R&   (   R#   t   targett   tt   post   found(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt
   next_until]   s    c         C   s    d j  |  j t |    | k S(   NR(   (   R)   R"   t   len(   R#   t   word(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt	   peek_wordj   s    c         C   s  g  } t  } |  j   } | s" d S| j |  | d k rD t } n | t k rT | Sx t r|  j   } | d k r | j |  j    |  j   d k r |  j   } q| rPqqW | r | j |  j    qW | s | t k s | t k r PqW | j |  j    qW Wd j	 |  S(   s   Return the next NEXUS word from a string.

        This deals with single and double quotes, whitespace and punctuation.
        t   'R(   N(
   t   FalseR2   R&   t   appendR/   t   PUNCTUATIONR'   R.   R0   R)   (   R#   R<   t   quotedt   firstt   ct   skip(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt	   next_wordm   s0    		c         C   s   d j  |  j  S(   s.   Return the rest of the string without parsing.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/Nexus.pyt   rest   s    (   R   R   t   __doc__R%   R'   R,   R.   R2   R3   R:   R=   RF   RG   (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyR    5   s   										 t
   StepMatrixc           B   sP   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d d  Z	 RS(	   sb   Calculate a stepmatrix for weighted parsimony.

    See Wheeler (1990), Cladistics 6:269-275.
    c         C   s   i  |  _  g  | D] } | ^ q |  _ |  j j   | rK |  j j |  n  xW |  j D]L } xC g  |  j D] } | | k rh | ^ qh D] } |  j | | d  q WqU Wd  S(   Ni    (   R   t   symbolst   sortR@   t   set(   R#   RJ   t   gapt   st   xt   y(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyR%      s    	/c         C   s1   | | k r | | } } n  | |  j  | | <d  S(   N(   R   (   R#   RO   RP   t   value(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyRL      s    c         C   s7   | | k r | | } } n  |  j  | | c | 7<d  S(   N(   R   (   R#   RO   RP   RQ   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   add   s    c         C   s   t  d   |  j j    S(   Nc         S   s   |  | S(   N(    (   RO   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/Nexus.pyt   <lambda>   s    (   t   reduceR   t   values(   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/Nexus.pyt   sum   s    c         C   sQ   |  j    } | d k rM x2 |  j D]$ } |  j | t |  |  j | <q" Wn  |  S(   Ni    (   RV   R   t   float(   R#   t   totalt   k(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   transformation   s
    %c         C   sL   xE |  j  D]: } |  j  | d k r
 t j |  j  |  |  j  | <q
 q
 W|  S(   Ni    (   R   t   matht   log(   R#   RY   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt	   weighting   s    %t   your_name_herec         C   s  d | t  |  j  f } | d d j |  j  7} x |  j D] } | d j d  | 7} x |  j D] } | | k r | d 7} qd | | k r | | } } n | | } } |  j | | d k r | d 7} qd | d	 j d
  |  j | | 7} qd W| d 7} q= W| d 7} | S(   Ns   usertype %s stepmatrix=%d
s           %s
s           s   [%s]i   s	    .       i    s	   inf.     s   %2.2fi
   s   
s   ;
(   R;   RJ   R)   t   ljustR   (   R#   t   nameR   RO   RP   t   x1t   y1(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   smprint   s     &
(
   R   R   RH   R%   RL   RR   RV   RZ   R]   Rc   (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyRI      s   	
					c         C   s   | rI |  j  d d  } d j g  | D] } | t k r% | ^ q%  } nB |  j  d d  } t |  j t t t   r d | d } n  | S(   s  Return a taxon identifier according to NEXUS standard.

    Wrap quotes around names with punctuation or whitespace, and double
    single quotes.

    mrbayes=True: write names without quotes, whitespace or punctuation
    for the mrbayes software package.
    t    t   _R(   R>   s   ''(   t   replaceR)   t   MRBAYESSAFERL   t   intersectionR0   RA   (   R`   t   mrbayest   safeRD   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   safename   s    	1c         C   s^   |  s
 d SxM |  j d  r+ |  j d  sI |  j d  rY |  j d  rY |  d d !}  q W|  S(   s6   Remove quotes and/or double quotes around identifiers.R>   t   "i   iN(   R&   t
   startswitht   endswith(   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/Nexus.pyt
   quotestrip   s
    ?t   -t   ?c         C   s   t  |   } | d k r d S| d } x* | d k rR |  | | k rR | d 8} q) Wd } x* | | k  r |  | | k r | d 7} q\ W| | k r | d k r d S| | f Sd S(   sk   Return position of first and last character which is not in skiplist.

    Skiplist defaults to ['-','?']).i    i   iN(   NN(   ii(   R;   R&   (   t   sequencet   skiplistt   lengtht   endt   start(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   get_start_end   s    
c         C   sE   g  |  D]  } |  | r |  | | f ^ q } | j    t |   d S(   s9   Returns a sorted list of keys of p sorted by values of p.i   (   RK   t   zip(   R1   t   pnt   startpos(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _sort_keys_by_values   s    -
c         C   s    t  t |    }  |  j   |  S(   sM   Check that all values in list are unique and return a pruned and sorted list.(   R!   RL   RK   (   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/Nexus.pyt   _make_unique  s    
c         C   s   x | |  k r | j  d  d j d  rw d j | j  d  d   d t t d | j  d  d d  d  } q | d 7} q W| S(   s=   Returns a unique name if label is already in previous_labels.t   .it   copys   .copyt   0i   i   (   t   splitRm   R)   t   strt   eval(   t   previous_labelst   label(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _unique_label	  s
    Lc         C   s-   t  g  |  D] } | |  | j   f ^ q
  S(   s?   Converts a Seq-object matrix to a plain sequence-string matrix.(   t   dictt   tostring(   R   R7   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _seqmatrix2strmatrix  s    c         C   s  |  s
 d St  t |    }  |  j   g  } |  } | j | d d  xKt |  d k rd } x,t |  D]\ } } | | d | | k r qp qp | d k r t |  d k r | | d | | | d k r | | d } qp | |  } t |  d k r#| j t | d d   n` | d k rY| j d | d d | d d f  n* | j d | d d | d d | f  | | } Pqp WqK Wd	 j |  S(
   s   Transform [1 2 3 5 6 7 8 12 15 18 20] (baseindex 0, used in the Nexus class)
    into '2-4 6-9 13-19\3 21' (baseindex 1, used in programs like Paup or MrBayes.).
    R(   ig      ?i   i    i   s   %d-%ds   %d-%d\%dRd   (   R!   RL   RK   R@   R;   t	   enumerateR   R)   (   t	   orig_listt	   shortlistt   clistt   stept   iRO   t   sub(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _compact4nexus  s.    
>
**
	c      	   C   s  |  s
 d S|  d d } t j |  d d  } t t g  |  D] } | d j ^ q<   d k } | rs d | _ n  d | _ d | _ t | _	 d | _
 x> | j j   D]- \ } } | | j d | | f <| j | =q Wx> | j j   D]- \ } } | | j d | | f <| j | =q Wi i t | j  | 6d 6| _ x!|  d D]\ } }	 g  | j D] }
 |
 |	 j k rZ|
 ^ qZ} g  | j D] }
 |
 | k r|
 ^ q} g  |	 j D] }
 |
 | k r|
 ^ q} x^ | D]V }
 | j |
 c t |	 j |
 j   j |	 j | j  j |	 j | j  | j  7<qWx7 | D]/ }
 | j |
 c t | j |	 j | j  7<q3Wxr | D]j }
 t | j | j | j  t |	 j |
 j   j |	 j | j  j |	 j | j  | j  | j |
 <qmW| j j |  xN |	 j j   D]= \ } } g  | D] } | | j ^ q| j d | | f <qW|	 j r| j sZi  | _ n  | j j t g  |	 j j   D]" \ } } d | | f | f ^ qv  n  t | j | j |	 j  | j d | <|	 j r2| j si  | _ n  | j j t g  |	 j j   D] \ } } | j | | f ^ q  n  | j |	 j 7_ | j t |  7_ qDWx- | j d D] } | j d | | j | <qkW| S(   s   Combine matrices in [(name,nexus-instance),...] and return new nexus instance.

    combined_matrix=combine([(name1,nexus_instance1),(name2,nexus_instance2),...]
    Character sets, character partitions and taxon sets are prefixed, readjusted
    and present in the combined matrix. 
    i    i   R&   s   %s.%st   combinedN(   R&   R   t   deepcopyR;   RL   t   datatypeR   t   statelabelsR?   t
   interleaveR   t   charsetst   itemst   taxsetst   ranget   nchart   charpartitionsR   R   R   R   Rf   RM   t   missingt   alphabett   extendt   updateR   t   ntax(   t   matricesR`   R   t   nt   mixed_datatypest   cnt   cst   tnt   tst   mR7   t   botht   combined_onlyt   m_onlyRO   R   R   RD   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   combine8  s\    2				 +((T-O5		K'		Hc         C   s  t  |   } g  } g  } d } t } d } xft r| j   } | d k rO Pn  | | k rq | pd | rq d } n | r | p | r | d k s | d k r | } n | r | d k r | j   t k r | d k r | r t } qA| d 7} nM | rA| d k rA| rt } qA| d 8} | d k  r- t d   q- q- n  | d k r- | d	 k r| r| j d j	 |   g  } q| j |  q- q- W| r| j d
 j	 |   n  | d k rt d   n  | S(   s  Delete []-delimited comments out of a file and break into lines separated by ';'.
    
    stripped_text=_kill_comments_and_break_lines(text):
    Nested and multiline comments are allowed. [ and ] symbols within single
    or double quotes are ignored, newline ends a quote, all symbols with quotes are
    treated the same (thus not quoting inside comments like [this character ']' ends a comment])
    Special [&...] and [\...] comments remain untouched, if not inside standard comment.
    Quotes inside special [& and [\ are treated as normal characters,
    but no nesting inside these special comments allowed (like [&   [\   ]]).
    ';' ist deleted from end of line.
   
    NOTE: this function is very slow for large files, and obsolete when using C extension cnexus
    R(   i    Rl   R>   t   [i   t   ]s#   Nexus formatting error: unmatched ]t   ;s   
s#   Nexus formatting error: unmatched [N(
   R    R?   R/   R.   R&   R'   t   SPECIALCOMMENTSR   R@   R)   (   t   textt   contentst   newtextt   newlinet
   quotelevelt   speciallevelt	   commlevelR7   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _kill_comments_and_break_lines{  sF    		,	%		
	c         C   s   g  } x |  D]w } | j  d d  j  d d  j   } | j   j d  r\ | j |  q | j  d d  } | r | j |  q q W| S(   s   Adjust linebreaks to match ';', strip leading/trailing whitespace.

    list_of_commandlines=_adjust_lines(input_text)
    Lines are adjusted so that no linebreaks occur within a commandline 
    (except matrix command line)
    s   
s   
s   R   Rd   (   Rf   R*   t   lowerRm   R@   (   t   linest   formatted_linesR|   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _adjust_lines  s    $c         C   s
  |  j  d  } x | d k r|  j  d  } | d k  rL t d |    n | | k  rk t d |    n  g  |  | d | !D] } | ^ q} } | j   d j |  } | | j   } | | j   k r | j   } n  |  |  | |  | d }  |  j  d  } q W|  S(	   s>   Replaces ambigs in xxx(ACG)xxx format by IUPAC ambiguity code.t   (it   )i    s    Missing closing parenthesis in: s    Missing opening parenthesis in: i   R(   (   t   findR   RK   R)   t   upperR   (   t   seqt   rev_ambig_valuest   openingt   closingRO   t   ambigt
   ambig_code(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _replace_parenthesized_ambigs  s     $
t   Commandlinec           B   s   e  Z d  Z d   Z RS(   s/   Represent a commandline as command and options.c         C   s$  i  |  _  g  } d  |  _ y% | j   j d d  \ |  _ } Wn= t k
 r| | j   d |  _ d j | j   d  } n X|  j j   j   |  _ |  j t k r | j   |  _  njt	 |  d k r y1| j
 d d  j   } g  t t	 |   D]K } | | d k r | d k r | t	 |  k r | d | | d f ^ q } g  } x | D] } | j |  qTWg  t t	 |   D] } | | k r~| ^ q~} x1 | D]) }	 | |	 d |  j  | |	 d j   <qWx% | D] }
 d  |  j  | |
 j   <qWWq t k
 rt d |   q Xn  d  S(	   Ns   
i   i    Rd   t   =s    = i   s    Incorrect formatting in line: %s(   t   optionsR&   t   commandR*   R   R5   R)   R   t   SPECIAL_COMMANDSR;   Rf   R   R   R   (   R#   t   lineR   R   R   t   valued_indicest   indicest   slt   token_indicest   optt   token(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyR%     s2    		%d1'(   R   R   RH   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/Nexus.pyR     s   t   Blockc           B   s   e  Z d  Z d d  Z RS(   sA   Represent a NEXUS block with block name and list of commandlines.c         C   s   | |  _  g  |  _ d  S(   N(   R   t   commandlines(   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/Nexus.pyR%     s    	N(   R   R   RH   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/Nexus.pyR      s   t   Nexusc           B   s  e  Z d  d g Z d< d  Z d   Z d   Z e e e  Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z  d   Z! d   Z" e# d d   Z$ d d!  Z% d"   Z& d< d#  Z' d$   Z( d%   Z) d&   Z* d'   Z+ d< d< d< e, g  g  d< d( e, d) 	 Z- d< d< g  g  d< e, e, d< e, e. e, e. d*  Z/ g  g  e, e. e, d+  Z0 d< d, d-  Z1 d< d.  Z2 d< g  g  d/  Z3 g  e. d0  Z4 d1 g  g  d2  Z5 d< g  g  d3  Z6 d< g  g  d4  Z7 d5   Z8 d6 e, d7  Z9 d< d< d8  Z: d9   Z; e, d:  Z< d< e. d;  Z= RS(=   t   original_taxon_ordert   __dict__c         C   sB  d |  _  d |  _ g  |  _ g  |  _ d  |  _ d  |  _ d |  _ t |  _	 d |  _
 d |  _ d  |  _ d  |  _ d  |  _ d  |  _ t |  _ t |  _ t |  _ d  |  _ d  |  _ g  |  _ i  |  _ i  |  _ i  |  _ i  |  _ g  |  _ d  |  _ g  |  _ i  |  _ i  |  _ d  |  _ d |  j d <| r1|  j  |  n |  j  t!  d  S(   Ni    t   dnaRq   Rp   R   t   gapmode("   R   R   t   unaltered_taxlabelsR   R&   R   R   R   R?   t   respectcaseR   RM   RJ   t   equatet	   matchchart   labelst	   transposeR   t   tokenst	   eliminateR   t   unknown_blocksR   R   R   t   taxpartitionsR   R   t
   structuredRL   R   R   t   readt   DEFAULTNEXUS(   R#   t   input(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyR%   
  sD    																														c         C   s   |  j  S(   s2   Included for backwards compatibility (DEPRECATED).(   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/Nexus.pyt   get_original_taxon_order2  s    c         C   s   | |  _  d S(   s2   Included for backwards compatibility (DEPRECATED).N(   R   (   R#   RQ   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   set_original_taxon_order5  s    c   
      C   sA  y. t  t j j |  d  j   } | |  _ Wn t t t f k
 r t	 | t
  rh | } d |  _ q t | d  r | j   } t | d  r | j r | j |  _ q d |  _ q | j   d  GHt d | d    n X| j   } | j d	  r| d
 } n  t rct j |  } | d k s2| d k rEt d |   n  t | j t d    } n t t |   } xU t |  D]G \ } } y1 | d
  j   d	 k r| d
 j   | | <n  WqqXqW|  j |  } x^ y | j   \ } }	 Wn t k
 r	Pn X| t k r)|  j | |	  q|  j | |	  qWd S(   s@   Read and parse NEXUS imput (a filename, file-handle, or string).t   rUt   input_stringR   R`   t   Unknown_nexus_filei2   s   Unrecognized input: %s ...id   s   #NEXUSi   R   R   s   Unmatched %si   N(   t   opent   ost   patht
   expanduserR   t   filenamet	   TypeErrort   IOErrort   AttributeErrort
   isinstanceR   t   hasattrR`   R*   R   Rm   t   Ct   cnexust   scanfileR   R   t   chrR   R   R   t   _get_nexus_blockR.   t   StopIterationt   KNOWN_NEXUS_BLOCKSt   _parse_nexus_blockt   _unknown_nexus_block(
   R#   R   t   file_contentst   decommentedR   R   t   clt   nexus_block_genR   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/Nexus.pyR   :  sL    !c         c   s   t  } g  } x | r | j d  } | j   j d  rq | s^ t } | j   d j   } q t d |   q | j   j d  r | r t  } | | f Vg  } q t d   q | r | j |  q q Wd S(   s+   Generator for looping through Nexus blocks.i    t   begini   s!   Illegal block nesting in block %sRu   s   Unmatched 'end'.N(   R?   R-   R   Rm   R/   R   R   R@   (   R#   R  t   inblockt
   blocklinesR  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/Nexus.pyR   q  s"    		c         C   s6   t    } | j j |  | | _ |  j j |  d  S(   N(   R   R   R@   R   R   (   R#   R   R   t   block(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyR    s    		c         C   s   |  j  | |  |  j d } x\ | j D]Q } y! t |  d | j  | j  Wq' t k
 rw   t d | j   q' Xq' Wd S(   s$   Parse a known Nexus Block (PRIVATE).iRe   s   Unknown command: %s N(   t   _apply_block_structureR   R   t   getattrR   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/Nexus.pyR     s    !c         C   s   d  S(   N(    (   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/Nexus.pyt   _title  s    c         C   sH   d | k r" t  | d  |  _ n  d | k rD t  | d  |  _ n  d  S(   NR   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/Nexus.pyt   _dimensions  s    c         C   s  d | k r t  |  _ n  d | k r | d |  _ |  j j d  rU |  j j d  sy |  j j d  r |  j j d  r |  j d d !j d d  |  _ n  |  j s |  j j   |  j j   |  _ t	 t
 |  j   |  _ q n  d	 | k rA| d	 j   |  _ |  j d
 k s|  j d k r`t j t j  |  _ t j t j  |  _ t j t j  |  _ n |  j d k rt j t j  |  _ t j t j  |  _ t j t j  |  _ n |  j d k rt j t j  |  _ i d d 6d d 6t j t j  d 6|  _ t j t j  d |  _ n1 |  j d k r9t d   n t d |  j   d j |  j j    |  j |  _ |  j s|  j j   |  j j   |  _ n  t g  |  j j    D]* } | d d k r| d | d f ^ q } i  |  _! xV | j    D]E \ } } g  | D] } | ^ q} | j"   | |  j! d j |  <qWn  |  j d% k r|  j j# |  _ |  j$ |  j k r|  j |  j% |  j |  j$ <n  |  j% |  j |  j% <n- |  j d k r|  j sd d g |  _ qn  d | k r| d d |  _$ n  d | k r| d d |  _% n  d | k r-| d |  _& n  d | k rM| d d |  _' n  d | k ri| d |  _( n  d | k rt d    t  |  _) n  d! | k r| d! d  k s| d! j   d" k rt  |  _+ qn  d# | k rt  |  _, n  d$ | k rt- |  _, n  d  S(&   NR   RJ   Rl   R>   i   iRd   R(   R   R   t
   nucleotidet   rnat   proteint   DNt   Bt   EQt   Zt   Xt   *t   standards'   Datatype standard is not yet supported.s   Unsupported datatype: i    t   1R   R   RM   R   R   R   R   s   TRANSPOSE is not supported!R   t   yesR   t   notokens(   R   R  R  (.   R/   R   RJ   Rm   Rn   t   symboldRf   R   R   R!   RL   R   R   R   R    t   ambiguous_dnaR   R   t   ambiguous_dna_valuest   ambiguous_valuest   unambiguous_dna_letterst   unambiguous_letterst   ambiguous_rnat   ambiguous_rna_valuest   unambiguous_rna_lettersR  t   protein_lettersR   R)   t   keyst   valid_charactersR   R   t   rev_ambiguous_valuesRK   t   lettersR   RM   R   R   R   R   R&   R   R   R?   (   R#   R   R   t   revRY   t   vRD   t   key(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _format  s    $$"	*"	"F	
	&c         C   s   | |  _  d  S(   N(   RL   (   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/Nexus.pyt   _set  s    c         C   s   | |  _  d  S(   N(   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/Nexus.pyt   _options  s    c         C   s   | |  _  d  S(   N(   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/Nexus.pyt
   _eliminate  s    c         C   s   d S(   s&  Get taxon labels (PRIVATE).

        As the taxon names are already in the matrix, this is superfluous
        except for transpose matrices, which are currently unsupported anyway.
        Thus, we ignore the taxlabels command to make handling of duplicate
        taxon names easier.
        N(    (   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/Nexus.pyt
   _taxlabels  s    c         C   sS   t  g  |  j D] } | j d d  | f ^ q  } | j d d  } | j |  S(   s.   Check for presence of taxon in self.taxlabels.Rd   Re   (   R   R   Rf   t   get(   R#   t   taxonR7   t   nextaxat   nexid(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _check_taxlabels  s    4c         C   s   i  |  _  t |  } x t r y | j   } | d  k r= Pn  |  j | d t } t | j    } | |  j  | <| j   } | d  k r Pn | d k r t	 d |   n  Wq t	 k
 r   q t	 d |   q Xq Wd  S(   Nt   set_typet   ,s   Missing ',' in line %s.s   Format error in line %s.(
   R   R    R/   RF   R&   t   _resolvet   CHARSETRo   R2   R   (   R#   R   t   optst   wt
   identifiert   stateRD   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _charlabels  s&    		c         C   s   |  j  |  d  S(   N(   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/Nexus.pyt   _charstatelabels.  s    c         C   s   d  S(   N(    (   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/Nexus.pyt   _statelabels2  s    c      	   C   s^  |  j  s |  j r# t d   n  i  |  _ d } t } g  | j d  D]$ } | j   d k rH | j   ^ qH } t |  } x8y | j   } WnN t	 k
 r | |  j  k  r t d   q | |  j  k r t d   q Pn X| d 7} | |  j  k r"|  j
 st d   q"d } t } n  t |  } t | j    } | j   j   } d }	 |  j
 r| rd j | j    }	 qd j | j   j    }	 nV d j | j    }	 x> t |	  |  j k  r| j   } |	 d j | j    7}	 qWt t |	 |  j  |  j  }
 | d k r&|
 } nq |  j rxe |
 j   j |  j  } | d	 k rZPn  t |
 j   |  | | |
 j   | d |  j  }
 q2Wn  xx t |
 j    D]d \ } } | |  j k r| |  j k r| |  j k rt d
 | | | | d | d !f   qqW| r`|  j j |  t |  j j    |  } |
 |  j | <|  j! j |  q t |  j! | d  |  } |  j" |  } | r|  j | c |
 7<q t d |   q WxY |  j D]N } t |  j |  |  j k rt d |  j t |  j |  | f   qqW|  j j    } | j#   |  j! } | j#   | | k sZt$ d   d  S(   Ns+   Dimensions must be specified before matrix!i    s   
R(   s   Not enough taxa in matrix.s   Too many taxa in matrix.i   s7   Too many taxa in matrix - should matrix be interleaved?isL   Taxon %s: Illegal character %s in line: %s (check dimensions / interleaving)i
   sZ   Taxon %s not in first block of interleaved matrix. Check matrix dimensions and interleave.s;   Matrx Nchar %d does not match data length (%d) for taxon %ssc   ERROR: TAXLABELS must be identical with MATRIX. Please Report this as a bug, and send in data file.(%   R   R   R   R   R/   R   R*   t   iterR.   R   R   R?   R    Ro   RF   RG   R)   R;   R   R   R'  R   R   R   R   R   R&  RM   R   R   R@   R   R%  R   R5  RK   t   AssertionError(   R#   R   t   taxcountt   first_matrix_blockR|   R   t   lineitert	   linecharst   idR   t	   iupac_seqt   refseqR1   R   RD   t   taxon_presentR2  t
   matrixkeyst   taxlabelssort(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _matrix7  s    	:
					=-)*


c         C   s   i  |  _  t |  } x t r yp t | j    } t | j    } | |  j  | <| j   } | d  k rn Pn | d k r t d |   n  Wq t k
 r   q t d |   q Xq Wd  S(   NR7  s   Missing ',' in line %s.s   Format error in line %s.(	   R   R    R/   t   intRF   Ro   R2   R&   R   (   R#   R   R:  R<  R   RD   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt
   _translate  s     		c         C   s   |  j  |  d S(   sA   Some software (clustalx) uses 'utree' to denote an unrooted tree.N(   t   _tree(   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/Nexus.pyt   _utree  s    c      	   C   s  t  |  } | j   } | j   d k rA t d | d    n  t } d } x | j   d k r| j   } | j   } | d k r t d | | d  f   n  | j   } | j d  }	 | j   }
 | d	 k r t } qP | d
 k r t } qP | d k rP t	 |	  } qP qP Wt
 d | d | d | d | j   j    } |  j rx | j   D]z } y; t |  j t | j |  j j   | j |  j _ WqVt t f k
 rt d | j |  j j   qVXqVWn  |  j j |  d  S(   NR   s$   Syntax error in tree description: %si2   g      ?R   R   s7   Illegal special comment [%s...] in tree description: %sR   t   Rt   Ut   WR`   t   weightt   rootedR   s.   Unable to substitue %s using 'translate' data.(   R    RF   R2   R   R?   R,   R.   R:   R/   RW   R   RG   R*   R   t   get_terminalsRk   RN  t   nodeR   R2  R5   t   KeyErrorR   R@   (   R#   R   R:  R`   RV  RU  R   t   symbolt   specialRQ   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/Nexus.pyRP    s>    		-	;$c         C   sS   t  d  } | | _ x' | D] } | j j t | |   q W|  j j |  d  S(   NR(   (   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/Nexus.pyR
    s
    	c         C   s2   |  j  | d t \ } } t |  |  j | <d  S(   NR6  (   t   _get_indicest   TAXSETR}   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/Nexus.pyt   _taxset  s    c         C   s2   |  j  | d t \ } } t |  |  j | <d  S(   NR6  (   R\  R9  R}   R   (   R#   R   R`   t   sites(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _charset  s    c   
      C   s   i  } t  } t |  } |  j |  } | s@ t d |   n  d } x t r | j   } | d  k sz | d k r | r |  j | d t d d \ } }	 t	 |	  | | <d } | d  k r Pq qI | d k r | } n  | | 7} qI W| |  j
 | <d  S(   Ns%   Formatting error in taxpartition: %s R(   R7  R6  t	   separatort   :R>   (   R?   R    t   _name_n_vectorR   R/   R.   R&   R\  R]  R}   R   (
   R#   R   R   R   R:  R`   R   R;  t   subnamet
   subindices(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _taxpartition  s&    	!
c         C   s   |  j  j   } |  j |  g  |  j  j   D] } | | k r, | ^ q, } | g  k sh t |  d k r{ t d |   n | d |  _ d S(   s   Read codon positions from a codons block as written from McClade.

        Here codonposset is just a fancy name for a character partition with
        the name CodonPositions and the partitions N,1,2,3
        i   s$   Formatting Error in codonposset: %s i    N(   R   R%  t   _charpartitionR;   R   R   (   R#   R   t   prev_partitionsR   t	   codonname(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _codonposset  s    .c         C   s   d  S(   N(    (   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/Nexus.pyt   _codeset  s    c   
      C   s   i  } t  } t |  } |  j |  } | s@ t d |   n  d } x t r | j   } | d  k sz | d k r | r |  j | d t d d \ } }	 t	 |	  | | <d } | d  k r Pq qI | d k r | } n  | | 7} qI W| |  j
 | <d  S(   Ns&   Formatting error in charpartition: %s R(   R7  R6  Ra  Rb  R>   (   R?   R    Rc  R   R/   R.   R&   R\  R9  R}   R   (
   R#   R   R   R   R:  R`   R   R;  Rd  Re  (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyRg    s&    	!
R   c         C   s_   t  |  } |  j | d | } |  j | d | } | d k rU t d |   n  | | f S(   s   Parse the taxset/charset specification (PRIVATE).

        e.g. '1 2   3 - 5 dog cat   10 - 20 \ 3'
        --> [0,1,2,3,4,'dog','cat',9,12,15,18]
        Ra  R6  s   Formatting error in line: %s N(   R    Rc  t   _parse_listR&   R   (   R#   R   R6  Ra  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/Nexus.pyR\    s    c         C   s  | j    } | j   } | d k r3 | j   } n  | sL t d |   n  t |  } | j d k r | j   } | j   } | j   } | j   d k r t d t   q | j   d k r t d | t f   q n  | j   | k rt d |   n  | S(   s6   Extract name and check that it's not in vector format.R  s   Formatting error in line: %s R   t   vectors$   Unsupported VECTOR format in line %sR  s   Unknown qualifier %s in line %s(   RG   RF   R   Ro   R,   R2   R   R   (   R#   R:  Ra  RG   R`   R   t	   qualifiert   close(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyRc  *  s(    c         C   s  g  } | j    ryxt r| j   } | s4 Pn  |  j | d | } | j    d k rl| } d } | j   } |  j | j   d | } | t k r | j    d k r | j   }	 t | j    } n  | j t | | d |   qt	 |  t
 k st	 |  t
 k r$t d |   n  |  j j |  } |  j j |  } |  j | | d !}
 | j |
  q t	 |  t
 k r| j |  q | j |  q WWqt k
 r  qd SXn  | S(   s   Parse a NEXUS list (PRIVATE).

        e.g. [1, 2, 4-8\2, dog, cat] --> [1,2,4,6,8,17,21],
        (assuming dog is taxon no. 17 and cat is taxon no. 21).
        R6  Rp   i   s   \s:   Name if character sets not allowed in range definition: %sN(   R,   R/   RF   R8  R2   R9  RN  R   R   t   typeR!   R   R   R4   R@   R&   (   R#   t   options_bufferR6  t
   plain_listR<  Rv   Ru   R   t   hyphent	   backslasht   taxrange(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyRl  B  sB    	 $c         C   s  t  |  } | s! t d   n  | t k ry t |  } Wn t k
 r |  j r | |  j j   k r x^ |  j D] } |  j | | k ru | Squ Wq |  j r | |  j k r |  j | St d |   qX| |  j k r | d St d | |  j f   n | t	 k ry t |  } Wn] t k
 r~|  j
 |  } | rH| S|  j rk| |  j k rk|  j | St d |   qX| d k r| |  j k r|  j | d St d | |  j f   n t d |   d	 S(
   s!  Translate identifier in list into character/taxon index.

        Characters (which are referred to by their index in Nexus.py):
            Plain numbers are returned minus 1 (Nexus indices to python indices)
            Text identifiers are translaterd into their indices (if plain character indentifiers),
            the first hit in charlabels is returned (charlabels don't need to be unique)
            or the range of indices is returned (if names of character sets).
        Taxa (which are referred to by their unique name in Nexus.py):
            Plain numbers are translated in their taxon name, underscores and spaces are considered equal.
            Names are returned unchanged (if plain taxon identifiers), or the names in
            the corresponding taxon set is returned
        s0   INTERNAL ERROR: Need type to resolve identifier.s    Unknown character identifier: %si   s-   Illegal character identifier: %d>nchar (=%d).s   Unknown taxon identifier: %si    s(   Illegal taxon identifier: %d>ntax (=%d).s   Unknown set specification: %s.N(   Ro   R   R9  RN  R5   R   RU   R   R   R]  R5  R   R   R   (   R#   R<  R6  R   RY   t   taxlabels_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/Nexus.pyR8  n  sF    c         C   s   d  S(   N(    (   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/Nexus.pyt	   _stateset  s    c         C   s   d  S(   N(    (   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/Nexus.pyt
   _changeset  s    c         C   s   d  S(   N(    (   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/Nexus.pyt   _treeset  s    c         C   s   d  S(   N(    (   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/Nexus.pyt   _treepartition  s    R(   c
         C   s  | s |  j  } n  | s d S| s. |  j } n  | r<i  }
 x | D] } g  | } | j g  t |  j  D] } | | | k rg | ^ qg  t |  } | d | d } | j d  } | d k r | |  d | d } n | d | } | |
 | <|  j d | d	 | d
 | d | d | d | d | d t d |	  	qA W|
 S|  j d } |  j d | d	 | d
 | d | d | d | d | d t d |	  	| Sd S(   s   Writes a nexus file for each partition in charpartition.

        Only non-excluded characters and non-deleted taxa are included,
        just the data block is written.
        Ns   
Partition: s   
R~   i    Re   s   .dataR   R   t	   blocksizeR   t   excludet   deletet   commentt   append_setsRi   (	   R   R   R   R   R   R}   t   rfindt   write_nexus_dataR?   (   R#   R   R   R{  R   R|  R}  R   R~  Ri   t
   pfilenamesR1   t   total_excludeRD   t   pcommentt   dott	   pfilenamet   fn(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   write_nexus_data_partitions  s6    
9
!c      
   C   s  | s |  j  } n  | s d S| s. |  j } n  g  | D] } |  j |  s5 | ^ q5 r t d d j t |  j t |  j      n  | r| |  j k r t d |   q|  j | } t	 |  } i  } x> | D]3 } g  | | D] } | | k r | ^ q | | <q Wn  g  |  j D]$ } | | k r| | k r| ^ q} t
 |  j | d | d |  } t |  } t | | d  } | s| r| d d k rd St | t  ry t | d	  } Wqt k
 rt d
 |   qXn$ t t d  r	| } n t d   |	 s+| j d  n  | rI| j d | d  n  | j d  | j d | | f  | j d |  j  |  j r| j d  n  |  j r| j d |  j  n  |  j r| j d |  j  n  |  j r| j d |  j  n  |  j r| j d |  j  n  |  j r:| j d |  j  n  | sF| rV| j d  n  | j d  |  j r|  j d |  } | j   } | j   | j d d j g  | D]$ } d | d t  | |  f ^ q d  n  | j d  | s| rd } q|  j! } n  t" g  | D] } t t  | d  |  ^ q } | rd } x| D] } | j d! | | f  t | |  d k rx^ | D]V } | j t  | d  | j# | d   | j | | | | t | |  !d"  qW| j d"  n | j d#  | t | |  7} qRWn*| rx!t$ d | |  D]j } xT | D]L } | j t  | d  | j# | d   | j | | | | | !d"  qDW| j d"  q7Wn x | D] } | | k  r| j t  | d  | d"  n& | j t  | d  | j# | d   x: t$ d | |  D]& } | j | | | | | !d"  qWqW| j d$  |
 r| r| j |  j% d | d | d  | d% t&   | j |  j% d | d | d  | d& t'   q| j |  j% d | d | d  |   n  | | k r| S| j(   | Sd S('   s  Writes a nexus file with data and sets block to a file or handle.

        Character sets and partitions are appended by default, and are
        adjusted according to excluded characters (i.e. character sets
        still point to the same sites (not necessarily same positions),
        without including the deleted characters.

        filename - Either a filename as a string (which will be opened,
                   written to and closed), or a handle object (which will
                   be written to but NOT closed).
        omit_NEXUS - Boolean.  If true, the '#NEXUS' line normally at the
                   start of the file is ommited.

        Returns the filename/handle used to write the data.
        Ns   Unknown taxa: %ss   , s   Unknown partition: R|  R}  i    R(   R;  s   Could not open %s for writing.t   writes,   Neither a filename nor a handle was supplieds   #NEXUS
R   s   ]
s   begin data;
s   	dimensions ntax=%d nchar=%d;
s   	format datatype=s    respectcases	    missing=s    gap=s    matchchar=s    labels=s    equate=s    interleaves   ;
s   charlabels s   %s %si   s   matrix
iF   Ri   s	   [%s: %s]
s   
s	   [empty]

s   ;
end;
t   include_codonst   codons_only()   R   R   R5  R   R)   RL   t
   differenceR   R   R{   R   t   crop_matrixR;   R   R   R   R   R   t   fileR5   R  R   R   R   RM   R   R   R   R   t   _adjust_charlabelsR%  RK   Rk   R   t   maxR_   R   R  R?   R/   Ro  (   R#   R   R   R|  R}  R{  R   t   interleave_by_partitionR~  t
   omit_NEXUSR  Ri   t   codons_blockR7   t	   partitiont   namest   newpartitionR1   RD   R2  t   undeletet   cropped_matrixt   ntax_adjustedt   nchar_adjustedt   fht   newcharlabelst   clkeysRY   t
   namelengtht   seek(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyR    s    (.44!								
L	1&.&$ &(+.(
c         C   s  |  j  r" |  j r" |  j r" d S| r4 d g } n	 d g } d } g  } xN t |  j  D]= }	 |	 | k r | d 7} | j d  qY | j |	 |  qY W| sxr |  j  j   D]a \ }
 } g  | D] }	 |	 | k r | |	 ^ q } | r | j d t |
  t |  f  q q Wx |  j j   D]l \ }
 } g  | D]$ } | | k r8t | d | ^ q8} | r%| j d	 t |
  d
 j	 |  f  q%q%Wn  x7|  j j   D]&\ }
 } | r|
 t
 k rqn | r|
 t
 k rqn  t |  } i  } xN | D]F } g  | | D] }	 |	 | k r| |	 ^ q} | r| | | <qqW| r| ri|
 t
 k rid } n d } | j d | t |
  d j	 g  | D], } | | k rd | t | |  f ^ q f  qqWx |  j j   D] \ }
 } t |  } i  } xJ | D]B } g  | | D] } | | k r| ^ q} | r| | | <qqW| r| j d t |
  d j	 g  | D]> } | | k rrd t |  d
 j	 t t | |   f ^ qr f  qqW| j d  t |  d k rd Sd j	 |  Sd S(   s   Returns a sets block.R(   s   
begin codonss   
begin setsi    i   is   charset %s = %sRi   s   taxset %s = %sRd   R   R   s
   %s %s = %ss   , s   %s: %ss   taxpartition %s = %ss   end;
i   s   ;
N(   R   R   R   R   R   R@   R   Rk   R   R)   t   CODONPOSITIONSR{   R   t   mapR;   (   R#   R|  R}  Ri   R  R  t   setsbt   offsett   offlistRD   R   t   nst   csetRN   R7   t   tsetR1   R  R  t   snt   nspR   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyR  b  sh    	
)*10-	N)`iF   c         C   s  | sm d |  j  k r] |  j  j d  d j   d k r] d j |  j  j d  d   d } qm |  j  d } n  t | d  } x |  j D]z } | j d	 t |  d
  xV t d t	 |  j
 | j    |  D]/ } | j |  j
 | j   | | | !d
  q Wq W| j   | S(   sA   Writes matrix into a fasta file: (self, filename=None, width=70).R~   it   paupt   nexust   next   dats   .fasR;  t   >s   
i    (   R  R  R  R  (   R   R   R   R)   R   R   R  Rk   R   R;   R   R   Ro  (   R#   R   t   widthR  R2  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/Nexus.pyt   export_fasta  s    1&,1
c         C   s   | sm d |  j  k r] |  j  j d  d j   d k r] d j |  j  j d  d   d } qm |  j  d } n  t | d  } | j d	 |  j |  j f  x; |  j D]0 } | j d
 t	 |  |  j
 | j   f  q W| j   | S(   s   Writes matrix into a PHYLIP file: (self, filename=None).

        Note that this writes a relaxed PHYLIP format file, where the names
        are not truncated, nor checked for invalid characters.R~   iR  R  R  R  s   .phyR;  s   %d %d
s   %s %s
(   R  R  R  R  (   R   R   R   R)   R   R  R   R   R   Rk   R   R   Ro  (   R#   R   R  R2  (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   export_phylip  s    1&.
c         C   s  | s |  j  } n  g  |  j D]$ } | | k r | | k r | ^ q } | sP d St |  d k r g  t t | | d   D] } | | k r} | ^ q} Sg  t | | d j    D]? \ } } | | k r | |  j j | j	   | j	    f ^ q } x}| d D]q}	 g  }
 x\| D]T} | |	 | d j	   } | |  j
 k s|| |  j k rl|  j d j   d k s|| | d k r|
 j |  q| | d k s| d |  j
 k s|  j d j   d k r| d |  j k r|
 j | d |  j j | |  f  q| |  j k rt j |  j |  j t j | d   } | rk|
 j | d d j |  f  qkqqW|
 } qWg  | D] } | d ^ q} | S(   s+   Return a list with all constant characters.i   i    R   R   R(   N(   R   R   R&   R;   R   R   R   R  R1  R   R   RM   R   R   R@   R   RL   Rh   R)   (   R#   R   R}  R|  R7   R  RO   R   t   constantR2  t   newconstantt   sitet   seqsitet	   intersectRN   t   cpos(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyR    s0    47]GO),*
c   	      C   sZ  g  |  j  D] } | | k r
 | ^ q
 } | s2 d Sg  } x | D] } |  j | | j   } |  j j d  d k r | |  j k r |  j } n  | r | |  j k r | | k r | j |  q q? | j	 g  |  j
 | D] } | | k r | ^ q  q? W|  j | k rL| rLt |  d k rLg  | D] } | |  j k r(| ^ q(} n  | j   | S(   s   Summarize character.

        narrow=True:  paup-mode (a c ? --> ac; ? ? ? --> ?)
        narrow=false:           (a c ? --> a c g t -; ? ? ? --> a c g t -)
        R   R   i   N(   R   R&   R   R   R   R1  RM   R   R@   R   R  R;   RK   (	   R#   R  R}  t   narrowR7   R  t   cstatusRD   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/Nexus.pyR    s     ('7'+
R^   c         C   s   t  |  j |  j  } x g  t |  j  D] } | | k r( | ^ q( D]q } |  j | |  } xV t | d   D]D \ } }	 x5 | | d D]% }
 | j |	 j   |
 j   d  q Wqm WqD W| j	   j
   j d |  S(   s   Calculates a stepmatrix for weighted parsimony.

        See Wheeler (1990), Cladistics 6:269-275 and
        Felsenstein (1981), Biol. J. Linn. Soc. 16:183-196
        ii   R`   (   RI   R   RM   R   R   R  R   RR   R   RZ   R]   Rc   (   R#   R`   R|  R}  R   RN   R  R  R   t   b1t   b2(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   weighted_stepmatrix  s    5+c         C   s  | s |  j  } n  g  | D] } |  j |  s | ^ q rk t d d j t j |  j |  j     n  | g  k rg  |  j D]$ } | | k r | | k r | ^ q } | s i  Sg  | D] } | | j   ^ q } t	 |   } g  t
 |  D] \ }	 }
 |	 | k r |
 ^ q } | g  k rPt g  | D] } | t d |  j  f ^ q+ St	 |   } g  t d j |  D] }
 t |
 |  j  ^ qo} t t	 | |   SnB t g  |  j D]. } | | k r| | k r| | | f ^ q Sd S(   s=   Return a matrix without deleted taxa and excluded characters.s   Unknown taxa: %ss   , R(   N(   R   R5  R   R)   R   RL   R  R   R   Rx   R   R   R   R   R  (   R#   R   R}  R|  R7   R  RY   R   t   zipped_mR   RN   t   sitesmt   zipped_sitesm(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyR    s$    (+4#1/1c         C   s  | s |  j  } n  t | | j   d t  } |  j d | d |  } | sQ i  St | | j   d  d k ru | Sg  |  j D] } | | k r | ^ q } | r t g  | D] } | | j   ^ q   } | | j   d j	 }	 n# t g  | D] } | | ^ q   } g  t
 t |   D]& }
 | t j d t |  d  ^ q} t d j t |    } | rg  | D] } t | |	  ^ qm} n  t t | |   S(   s   Return a bootstrapped matrix.i    R}  R|  i   R(   (   R   R   R%  R   R  R;   R   Rx   R   R   R   t   randomt   randintR  R)   R   (   R#   R   R}  R|  t
   seqobjectst   cmR7   R  R  R   R   t   bootstrapsitesmt   bootstrapseqsRN   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt	   bootstrap*  s$     ()#?%c         C   s  | s t  d   n  |  j t |  } | d k  rK |  j |  j |  n  | d k rk | |  j | 7} n  | |  j k r t |  j |  } n | } | |  j j   k s t	 d   t
 | |  j  |  j | <|  j d 7_ |  j j |  |  j j |  d S(   s'   Adds a sequence (string) to the matrix.s   New sequence must have a namei    sV   ERROR. There is a discrepancy between taxlabels and matrix keys. Report this as a bug.i   N(   R   R   R;   t
   insert_gapR   R   R   R   R%  RB  R   R   R   R@   R   (   R#   R`   Rr   t   difft   unique_name(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   add_sequence@  s    !i   c         C   s  t  d  } | d k  s' | |  j k r: t d |   n  | d k rJ d S|  j r t g  |  j D] } |  j | j   ^ q`   } n g  } d g t |  j  g | | | | +t |   } t d j	 |  } g  t
 |  j  D]( \ }	 }
 |
 t | |	 |  j  f ^ q } t |  |  _ |  j | 7_ x< |  j j   D]+ \ }	 } | | | | d | |  j |	 <q9WxX |  j D]M } xD |  j | j   D]/ \ } } | | | | d | |  j | | <qWqrW|  j d | g |  |  _ |  j S(	   s   Add a gap into the matrix and adjust charsets and partitions.
        
        pos=0: first position
        pos=nchar: last position
        c         S   s   |  j    d } xy t |   D]k \ } } | | k rF | | |  | <n  | | k r | s| | d k r |  | d | d k r | } q q q W| d k r t | | |  |  | | +n  |  S(   sr   Adjusts chartacter sets if gaps are inserted, taking care of
            new gaps within a coherent character set.i    i   (   RK   R   R   (   RL   RO   t   dt
   leftgreedyt   addposR   RD   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   _adjust`  s    
*i    s   Illegal gap position: %dNRp   R(   R  t   insert(   R?   R   R   R   Rx   R   R   R;   R  R)   R   R   R   R   R   R   R   R  R   (   R#   R8   R   R  R  R7   R  t   zippedt   mappedR   R2  t   listedRN   R1   t   sp(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyR  Y  s*    	2$># +c         C   sA  | r | r t  d   n  |  j s( d S|  j j   } | j   i  } | r | j   | j t j  d } x | D]K } | | k rt x | | | k r | d 7} q W|  j | | | | <qt qt Wnw | r6| j   | j t j  d } xN | D]< } x | | | k r| d 7} q W|  j | | | | <q Wn |  j S| S(   sR   Return adjusted indices of self.charlabels if characters are excluded or inserted.s)   Can't exclude and insert at the same timei    i   N(   R   R   R&   R%  RK   R@   t   syst   maxint(   R#   R|  R  R   R  t   excountRD   t   icount(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyR    s4    	


c         C   s,   g  t  |  j  D] } | | k r | ^ q S(   s7   Returns all character indices that are not in charlist.(   R   R   (   R#   t   charlistRD   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   invert  s    c         C   s   t  |  j  } | r( | j |  j  n  t g  |  j D] } |  j | j   ^ q5   } g  t |  D]' \ } } t  |  j	 |  rd | ^ qd } | S(   s   Return gap-only sites.(
   RL   RM   RR   R   Rx   R   R   R   R   t   issubset(   R#   t   include_missingRM   R7   R  R   R  t   gaponly(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyR    s    /:c   	      C   s  | s |  j  } n  |  j  |  j g } | s@ | j d d g  n  x |  j D] } |  j | j   } t |  } t | d | \ } } | d k r | d k r | | } n0 | | d  | | | d } | | | | } | t |  k s t d |   t	 | |  j
  |  j | <qJ Wd S(   s{   Replaces all terminal gaps with missing character.
        
        Mixtures like ???------??------- are properly resolved.R   t   NRs   ii   sI   Illegal sequence manipulation in Nexus.termial_gap_to_missing in taxon %sN(   R   RM   R   R   R   R   R;   Rw   RB  R   R   (	   R#   R   t   skip_nRf   R2  Rr   Rt   Rv   Ru   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   terminal_gap_to_missing  s    "N(>   R   R   t	   __slots__R&   R%   R   R   t   propertyR   R   R   R  R   R  R  R,  R-  R.  R/  R0  R5  R>  R?  R@  RM  RO  RQ  RP  R
  R^  R`  Rf  Rj  Rk  Rg  R9  R\  Rc  Rl  R8  Rw  Rx  Ry  Rz  R?   R  R/   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/Nexus.pyR     sv   (			7						Q									Y			"								,8				%	E&	5		(    (    (    (    (7   RH   R   R  R[   R  R   t   Bio.AlphabetR    t   Bio.DataR   t   Bio.SeqR   t   TreesR   R   R   t   ImportErrorR?   R   R/   RL   t	   NameErrorR   R   t
   INTERLEAVER   R   RA   Rg   R0   R   R9  R]  R  R   t	   ExceptionR   R    RI   Rk   Ro   Rw   R{   R}   R   R   R   R   R   R   R   R   R   t   objectR   (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Nexus/Nexus.pyt   <module>   sX   <

	\?							"	C	<		"