ó
xTc           @   sS  d  d l  m Z d  d l m Z d  d l m Z d  d l Z d e f d     YZ	 d e f d     YZ
 d	 e f d
     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d   Z e d  Z d   Z e d  Z e d d  Z d e f d     YZ d d d d d  Z d  d!  Z d"   Z d S(#   i’’’’(   t   sequtil(   t
   suffixtree(   t   singleNt	   Alignmentc           B   sJ   e  Z d  Z d   Z d   Z d   Z d   Z d   Z e d    Z	 RS(   s   
  A full alignment
  c         C   s   | |  _  d  S(   N(   t   pairs(   t   selfR   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   __init__   s    c         C   s   t  d   |  j D  S(   Nc         s   s'   |  ] \ } } | | k r d  Vq d S(   i   N(    (   t   .0t   lt   r(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pys	   <genexpr>   s    (   t   sumR   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt
   identities   s    c         C   s   |  j  d |  j | j  S(   NR   (   t	   __class__R   (   R   t   other(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   __add__   s    c         C   s   |  j  j | j   |  S(   N(   R   t   extend(   R   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   __iadd__   s    c         C   s   t  |  j  S(   N(   t   lenR   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   __len__"   s    c         C   sa   t  |  } |  d g  t | |  D]8 } d | k o? | k  n rN | | n d  d  f ^ q"  S(   NR   i    (   R   t   ranget   None(   t   clst   sequencet   startt   stopt   lengtht   i(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   empty'   s    (
   t   __name__t
   __module__t   __doc__R   R   R   R   R   t   classmethodR   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR      s   					t   Fragmentc           B   sA   e  Z d  Z d   Z d   Z d   Z d   Z e d    Z RS(   s1   
  Unique fragment despite identical sequences
  c         C   s   | |  _  | |  _ d  S(   N(   t   indexR   (   R   R!   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   8   s    	c         C   s   t  |  j  S(   N(   R   R   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   >   s    c         C   s   d |  j  |  j f S(   Ns   Fragment(%s, %s)(   R!   R   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   __repr__C   s    c         C   s   t  |  j | j  S(   N(   t   cmpR!   (   R   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   __cmp__H   s    c         C   s   |  j  d | j  k S(   Ni   (   R!   (   t   leftt   right(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   consecutivityM   s    (	   R   R   R   R   R   R"   R$   t   staticmethodR'   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR    3   s   				t   HSSMatchc           B   sS   e  Z d  Z d   Z e d    Z d   Z d   Z d   Z d   Z	 d   Z
 RS(   s   
  Represents an HSS
  c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   fragmentR   R   (   R   R*   R   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   X   s    		c         C   s   |  j  t |  j  S(   N(   R   R   R*   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   _   s    c         C   s   g  } t  |  } xl t t |  j |  j   D]O \ } } | j d | k oW | k  n rf | | n d  |  j j | f  q. Wt	 d |  S(   Ni    R   (
   R   t	   enumerateR   R   R   t   appendR   R*   R   R   (   R   R   R   R   t   frag_it   seq_i(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   overlap_region_alignmente   s    ()c      	   C   sT   t  j d | d d d |  j  |  j d |  t  j d | d |  j d t |   S(   NR   R   i    R   (   R   R   R   R/   R   R   (   R   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt	   alignmentt   s    c         C   sT   |  j  j | j  j k  r( |  j | j k S| j  j |  j  j k  rP | j |  j k St S(   N(   R*   R!   R   R   t   False(   R   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   compatible_with}   s
    c         C   s   |  j  | 7_  d  S(   N(   R   (   R   t   shift(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   remap   s    c         C   s   d |  j  |  j |  j f S(   Ns.   HSSMatch(fragment = %s, length = %s, start=%s)(   R*   R   R   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR"      s    (   R   R   R   R   t   propertyR   R/   R0   R2   R4   R"   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR)   S   s   						t   UnalignedRegionc           B   s  e  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    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z  e d    Z! e d    Z" e d    Z# e d     Z$ e d!    Z% e d"    Z& e d#    Z' e d$    Z( e d%    Z) e d&    Z* RS('   s<   
  A region of sequence and fragments corresponding to it
  c         C   sg   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _ |
 |  _	 | |  _
 d  S(   N(   R%   R&   t	   fragmentst   _applicable_sequencet
   _is_insidet   _simple_fillt   _remapt
   _left_edget   _centralt   _right_edget   _alignable_fragments(   R   R%   R&   R7   t   applicable_sequencet	   is_insidet   simple_fillR4   t	   left_edget   centralt
   right_edget   alignable_fragments(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR      s    										c         C   s   |  j  d |  d |  S(   Nt   ownert   full(   R8   (   R   RH   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR@   ·   s    c         C   s   |  j  d |  d |  S(   NRG   t   position(   R9   (   R   RI   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRA   ¼   s    c         C   s(   |  j  d | j  o' |  j  d | j  S(   NRI   (   RA   R   R   (   R   t   match(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR2   Į   s    c         C   s   |  j  d |   S(   NRG   (   R:   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRB   Ē   s    c         C   s   |  j  d |  d |  S(   NRG   RJ   (   R;   (   R   RJ   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR4   Ģ   s    c         C   s   |  j  d |  d | d |  S(   NRG   R&   R7   (   R<   (   R   R&   R7   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRC   Ń   s    c      	   C   s"   |  j  d |  d | d | d |  S(   NRG   R%   R&   R7   (   R=   (   R   R%   R&   R7   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRD   Ö   s
    	c         C   s   |  j  d |  d | d |  S(   NRG   R%   R7   (   R>   (   R   R%   R7   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRE   ą   s    c         C   s   |  j  d |  d |  S(   NRG   RH   (   R?   (   R   RH   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRF   å   s    c         C   sa   |  d d  d | d | d |  j d |  j d |  j d |  j d |  j d	 |  j d
 |  j d |  j  S(   NR%   R&   R7   R@   RA   RB   R4   RC   RD   RE   RF   (	   R   t   left_applicable_sequencet   left_is_insidet   left_simple_fillt   no_remapt   leftmost_segmentt   central_segmentt   inner_right_segmentt   left_alignable_fragments(   R   R&   R7   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   LeftEdgeź   s    							c         C   s   t  g  | D] } t |  ^ q
  | | k s; t d  n  |  d | d | d | d |  j d |  j d |  j d |  j d	 |  j d
 |  j d |  j	 d |  j
  S(   Ns(   Fragments do not fit into space providedR%   R&   R7   R@   RA   RB   R4   RC   RD   RE   RF   (   R
   R   t
   ValueErrort   central_applicable_sequencet   central_is_insidet   central_simple_fillt   do_remapt   inner_left_segmentRP   RQ   t   central_alignable_fragments(   R   R%   R&   R7   t   f(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   Centralü   s    /							c         C   sa   |  d | d d  d | d |  j d |  j d |  j d |  j d |  j d	 |  j d
 |  j d |  j  S(   NR%   R&   R7   R@   RA   RB   R4   RC   RD   RE   RF   (	   R   t   right_applicable_sequencet   right_is_insidet   right_simple_fillRX   RY   RP   t   rightmost_segmentt   right_alignable_fragments(   R   R%   R7   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt	   RightEdge  s    							c         C   s   t  g  | D] } t |  ^ q
  } | | d } |  d | d | | d | d |  j d |  j d |  j d |  j d	 |  j d
 |  j d |  j d |  j	  S(   Ni   R%   R&   R7   R@   RA   RB   R4   RC   RD   RE   RF   (
   R
   R   t   open_applicable_sequencet   open_is_insideR_   RN   RO   RP   R`   t   open_alignable_fragments(   R   R   R7   R[   t	   len_fragsR%   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   Open#  s    %
							c         C   s   | |  j   S(   N(   R&   (   RG   RH   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRK   7  s    c         C   s   | |  j  |  j !S(   N(   R%   R&   (   RG   RH   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRU   <  s    c         C   s   | |  j  S(   N(   R%   (   RG   RH   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR]   A  s    c         C   s   | S(   N(    (   RG   RH   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRc   F  s    c         C   s   | |  j  k S(   N(   R&   (   RG   RI   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRL   L  s    c         C   s   |  j  | k o |  j k SS(   N(   R%   R&   (   RG   RI   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRV   Q  s    c         C   s   |  j  | k S(   N(   R%   (   RG   RI   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR^   V  s    c         C   s   t  S(   N(   t   True(   RG   RI   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRd   [  s    c         C   s   d  S(   N(    (   RG   RJ   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRN   a  s    c         C   s   | j  d |  j  d  S(   NR3   (   R4   R%   (   RG   RJ   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRX   f  s    c         C   s_   t    } xO t |   D]A } | t |  8} t d | d d d |  } | j d |  q W| S(   NR*   R   i    R   RJ   (   t   AlignmentSeedt   reversedR   R)   R,   (   R7   t   bordert   seedR[   t   m(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   reverse_simple_filll  s    	c         C   sY   t    } xI |  D]A } t d | d d d |  } | j d |  | t |  7} q W| S(   NR*   R   i    R   RJ   (   Ri   R)   R,   R   (   R7   Rk   Rl   R[   Rm   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   forward_simple_fillx  s    	c         C   s   t  j d |  j d |  j  S(   NR7   Rk   (   R6   Rn   R7   R&   (   RG   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRM     s    		c         C   s   t  j d |  j d |  j  S(   NR7   Rk   (   R6   Ro   R7   R%   (   RG   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR_     s    		c         C   so   t  |  j  d d } t j d |  j |  d |  j  } t j d |  j | d |  j  } | j d |  | S(   Ni   i   R7   Rk   Rl   (   R   R7   R6   Ro   R%   Rn   R&   t   merge(   RG   t   halft   seed1t   seed2(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRW     s    		c         C   s4   |  j  d |  s t d  n  t j d | d |  S(   NRI   s   Overflow on right edgeR&   R7   (   RA   RT   R6   RS   (   RG   R&   R7   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRO   ¤  s    c         C   s=   |  j  d |  s t d  n  t j d |  j d | d |  S(   NRI   s   Overflow on right edgeR%   R&   R7   (   RA   RT   R6   R\   R%   (   RG   R&   R7   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRY   ¬  s    		c         C   sX   |  j  d |  s t d  n  |  j  d |  s< t d  n  t j d | d | d |  S(   NRI   s   Overflow on left edges   Overflow on right edgeR%   R&   R7   (   RA   RT   R6   R\   (   RG   R%   R&   R7   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRP   ø  s    	c         C   s=   |  j  d |  s t d  n  t j d | d |  j d |  S(   NRI   s   Overflow on left edgeR%   R&   R7   (   RA   RT   R6   R\   R&   (   RG   R%   R7   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRQ   Ē  s    		c         C   s4   |  j  d |  s t d  n  t j d | d |  S(   NRI   s   Overflow on left edgeR%   R7   (   RA   RT   R6   Rb   (   RG   R%   R7   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR`   Ó  s    c         C   sw   |  j  d k r g  Sd } xT t t |  j  d d D]7 \ } } | t |  7} |  j  | k  r5 |  j | Sq5 W|  j S(   Ni    R   i   (   R&   R+   Rj   R7   R   (   RG   RH   R   R!   R*   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRR   Ü  s    (c         C   s   |  j  S(   N(   R7   (   RG   RH   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRZ   ģ  s    c         C   s|   t  |  } | |  j k r g  S|  j } xJ t |  j d d D]3 \ } } | t  |  7} | | k  r> |  j |  Sq> W|  j S(   NR   i   (   R   R%   R+   R7   (   RG   RH   t   seqlenR   R!   R*   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRa   ń  s    	"c         C   s   |  j  S(   N(   R7   (   RG   RH   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRe     s    (+   R   R   R   R   R@   RA   R2   RB   R4   RC   RD   RE   RF   R   RS   R\   Rb   Rg   R(   RK   RU   R]   Rc   RL   RV   R^   Rd   RN   RX   Rn   Ro   RM   R_   RW   RO   RY   RP   RQ   R`   RR   RZ   Ra   Re   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR6      sN   								
			Ri   c           B   s   e  Z d  Z g  d  Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 e d	    Z d
   Z e d    Z RS(   s+   
  A set of HSS matches for an alignment
  c         C   s.   i  |  _  x | D] } |  j d |  q Wd  S(   NRJ   (   t	   match_forR,   (   R   t
   hssmatchesRm   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR     s    	c         C   s   | |  j  | j <d  S(   N(   Ru   R*   (   R   RJ   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   _append_unchecked  s    c            s   t    f d   |  j D  S(   Nc         3   s!   |  ] }   j  d  |  Vq d S(   R   N(   R2   (   R   Rm   (   RJ   (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pys	   <genexpr>  s    (   t   allt   matches(   R   RJ   (    (   RJ   s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR2     s    c         C   s6   |  j  d |  s" t d |  n  |  j d |  d  S(   NRJ   s   %s incompatible with seed(   R2   RT   Rw   (   R   RJ   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR,      s    c         C   s3   | |  j    k r" t d |  n  |  j | j =d  S(   Ns   Unknown match %s(   t   matchsetRT   Ru   R*   (   R   RJ   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   remove(  s    c            sS   t    f d   | j D  s+ t d  n  x! | j D] }   j d |  q5 Wd  S(   Nc         3   s!   |  ] }   j  d  |  Vq d S(   RJ   N(   R2   (   R   Rm   (   R   (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pys	   <genexpr>2  s    s&   Incompatible matches during seed mergeRJ   (   Rx   Ry   RT   Rw   (   R   Rl   RJ   (    (   R   s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRp   0  s    c            s    t     f d   |  j D  S(   Nc         3   sC   |  ]9 } t  t  | j     t t    | j    Vq d  S(   N(   t   maxt   minR   R   (   R   Rm   (   R   R   (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pys	   <genexpr><  s   (   R
   Ry   (   R   R   R   (    (   R   R   s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   overlaps_with9  s    c         C   s(   x! |  j  D] } | j d |  q
 Wd  S(   NR3   (   Ry   R4   (   R   R3   RJ   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR4   A  s    c         C   s   |  j  j   S(   N(   Ru   t   values(   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRy   G  s    c         C   s   t  |  j  S(   N(   t	   frozensetRy   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRz   M  s    c         C   s   |  j  j   S(   N(   Ru   t   keys(   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR7   R  s    (   R   R   R   R   Rw   R2   R,   R{   Rp   R~   R4   R5   Ry   Rz   R7   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRi   	  s   									t   PartialAlignmentc           B   sk   e  Z d  Z d   Z e d    Z e d    Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z RS(
   s*   
  A partial alignment from hss matches
  c         C   sQ   | |  _  | |  _ i  |  _ x/ | D]' } x | j D] } | |  j | <q2 Wq" Wd  S(   N(   R   Rl   t   unaligned_inR7   (   R   R   Rl   t
   unalignedst   uart   frag(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   ]  s    			c         C   s   t  |  j j    S(   N(   t   setR   R   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   g  s    c         C   s   t  |  j  S(   N(   t   sortedR   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt
   unmatchedsm  s    c         C   s¹   x[ | j  D]P } | j |  j k r5 t d | j  n  |  j j d |  s
 t d |  q
 q
 WxT | j  D]I } |  j | j } | j j | j  |  j | j =|  j j d |  qh Wd  S(   Ns
   %s unknownRJ   s   %s incompatible with seed(	   Ry   R*   R   RT   Rl   R2   R7   R{   R,   (   R   Rl   RJ   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRp   s  s    c            s    t    f d     j j D  S(   Nc         3   s*   |  ]  } | j  d    j  j   Vq d S(   R   N(   R/   R   R   (   R   Rm   (   R   (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pys	   <genexpr>  s   (   R
   Rl   Ry   (   R   (    (   R   s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR     s    c         C   s"   |  j  j d d d t |  j   S(   NR   i    R   (   Rl   R~   R   R   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   overlaps  s    c         C   s;   |  j  j } | s d St | d d   } t | j d  S(   Ni    t   keyc         S   s   |  j  S(   N(   R*   (   Rm   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   <lambda>  s    (   Rl   Ry   R}   R   (   R   Ry   t   first(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   alignment_start  s
    c         C   sJ   t  |  j  } |  j j } | s% | St | d d   } t | j |  S(   NR   c         S   s   |  j  S(   N(   R*   (   Rm   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   £  s    (   R   R   Rl   Ry   R|   R   (   R   R   Ry   t   last(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   alignment_stop  s    c      	   C   s%  t  |  j j d d   } | sJ t d g  |  j D] } | d  f ^ q1  S| d } t j d |  j d d d | j  } | | j d |  j  7} x\ | d D]P } | t j d |  j d | j	 d | j  7} | | j d |  j  7} | } q W| t j d |  j d | j	 d t
 |  j   7} | S(	   NR   c         S   s   |  j  S(   N(   R*   (   Rm   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   ©  s    R   i    R   R   R   i   (   R   Rl   Ry   R   R   R   R   R   R/   R   R   (   R   Ry   t   ct   previousR0   t	   following(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR0   §  s*    )
				
		(   R   R   R   R   R5   R   R   Rp   R   R   R   R   R0   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   X  s   	
					t   HSSFindc           B   s;   e  Z d  Z d   Z d d  Z d d  Z d d  Z RS(   s4   
  Prepares a sequence for quick HSS calculations
  c         C   s   t  j   |  _ t  j d |  j  } x | D] } | j d |  q+ W| j d t    | j   t j d |  j j  |  _	 d  S(   Nt   treet   glypht   root(
   t
   singletreeR   t   ukkonenR,   t   objectt   detachR   t   calculate_leaf_indicesR   t   leaf_labels_under(   R   R   t   buildert   char(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   Ķ  s    
i   c   	      c   s   t  j d |  j d t |   } x_ t |  D]Q \ } \ } \ } } | | k  rX q. n  x$ |  j | D] } | | | f Vqf Wq. Wd  S(   NR   t   iterable(   R   t   matching_statisticsR   t   iterR+   R   (	   R   R   t
   min_lengtht   msit   start2R   t   edget   post   start1(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   overlap_withŚ  s    		%c         C   sR   i  } xE |  j  d | d |  D]+ \ } } t | | j | d   | | <q W| S(   NR   R£   i    (   R©   R|   t   get(   R   R   R£   t
   length_forR   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   frameshift_withé  s    %#c      	   C   sS   |  j  d | j d |  } g  | j   D]' \ } } t d | d | d |  ^ q( S(   NR   R£   R*   R   R   (   R¬   R   t   itemsR)   (   R   R*   R£   R¬   R   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   matches_withó  s
    			(   R   R   R   R   R©   R¬   R®   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   Č  s
   	
c         c   sĘ   xæ |  D]· } g  |  D] } | j  d |  r | ^ q } | sN | f Vd Vq x# t d |  D] } | f | Vq^ Wg  |  D] } | j | j k r{ | ^ q{ } x t d |  D] } | VqÆ Wq Wd  S(   NR   Rv   (    (   R2   t
   reassembleR*   (   Rv   RJ   Rm   t   otherst   compatt   without(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRÆ     s    ++c      
   c   s  | j  d |   } t d |  } | j d |   } t t j g  | D] } | j d | d d  ^ qC g   } x | D] } | j d |  qt Wg  | D] } | j d |  r | ^ q } | j	 d d   d	 t
  | |  } t   }	 x®t d
 |  D]}
 |
 sqõ n  t d
 |
  } | j   |	 k r.qõ n  |	 j | j    t | j  } t |  } t |  } g  | j D] } | | k rr| ^ qr} g  } yL | j | j d | j | j d g  | D] } | | k  ræ| ^ qæ  Wn t k
 rųqõ n Xg  | D]( } | | k  o| k  n r | ^ q } y¾ x· t j | d t j D] } t g  | D] } | | d k  rZ| ^ qZ } t g  | D] } | d | k  r| ^ q } | j | j d | j | j d | j | j d |   qJWWn t k
 rqõ n XyL | j | j d | j | j d g  | D] } | | k  r,| ^ q,  Wn t k
 reqõ n Xx$ | D] } | j d | j    qmW| Vqõ Wd  S(   NRH   R   R*   R£   i   RJ   R   c         S   s   |  j  S(   N(   R   (   Rm   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   $  s    t   reverseRv   R&   R7   R'   i    i’’’’R%   Rl   (    R@   R   RF   t   reducet   operatort   addR®   R4   R2   t   sortRh   R   RÆ   Ri   Rz   R7   R}   R|   R,   RC   Ru   R   RT   R    t   splitR    R'   RD   R   RE   Rp   RB   (   R   t	   unalignedt   max_hss_countt
   applicablet   hssfindt	   alignableR[   Rv   Rm   t   foundt   combRl   t   presentt   min_pt   max_pt   missingt   still_unalignedst   middlet   st   leftsidet	   rightsideR   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   alignment_gap_fill_search  sl    (	+
	(	-5//		-c         C   sk   t  j d t |   d g  t |  D]! \ } } t d | d |  ^ q"  } t d |  d t   d | g  S(   NR   R7   R!   R   Rl   R   (   R6   Rg   R   R+   R    R   Ri   (   R   R7   R   R[   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   empty_partial_alignmenti  s    	7	c         c   sÓ  g  t  |  D]! \ } } t d | d |  ^ q } t d |   } t t j g  | D] } | j d | d |  ^ qS g   } | j d d   d t  | |  } t	   }	 x#t
 d |  D]}
 |
 sĖ q¹ n  t d |
  } | j   |	 k rņ q¹ n  |	 j | j    t	 | j  } t |  } t |  } g  | D] } | | k r3| ^ q3} g  } | j t j d	 | j | j d
 g  | D] } | | k  r}| ^ q}  g  | D]( } | | k  oĆ| k  n r¦| ^ q¦} y~ xw t j | d t j D]] } | j t j d | j | | d j d j d	 | j | | d j d j d
 |   qšWWn t k
 rhq¹ n X| j t j d | j | j d
 g  | D] } | | k  r| ^ q  t d |  d | d |  Vq¹ Wd  S(   NR!   R   R*   R£   R   c         S   s   |  j  S(   N(   R   (   Rm   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR     s    R³   Rv   R&   R7   R'   R%   i    i   i’’’’Rl   R   (   R+   R    R   R“   Rµ   R¶   R®   R·   Rh   R   RÆ   Ri   Rz   R7   R}   R|   R,   R6   RS   Ru   R   R    Rø   R'   R\   R!   R   RT   Rb   R   (   R   R7   t   min_hss_lengthRŗ   R   R[   t   fragsR¼   Rv   R¾   Ræ   Rl   RĄ   RĮ   RĀ   RĆ   R   RÅ   RĘ   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   partial_alignmentsx  sX    4%	
	%	)5		)i    c      	      sŌ   xĶ   j  D]Ā } t t d   j d | d |   } | rX t | d   f d   } n | j   } xU | j D]J } | j d   j  } | j   t	 |  | k  rn | j
 d |  qn qn W  j d |  q
 Wd  S(   NR   R¹   Rŗ   R   c            s>   |  j  d d d t   j   t   f d   |  j D  f S(   NR   i    R   c         3   s*   |  ]  } | j  d    j  j   Vq d S(   R   N(   R/   R   R   (   R   Rm   (   t   partial(    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pys	   <genexpr>Õ  s   (   R~   R   R   R
   Ry   (   RĘ   (   RĪ   (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   Ņ  s    RJ   Rl   (   R   t   listRÉ   R   R|   RB   Ry   R/   R   R   R{   Rp   (   RĪ   Rŗ   t   min_identity_fractionR¹   t   seedst   bestRJ   t   ali(    (   RĪ   s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   complete_partial_alignmentĄ  s     	
t
   Equivalentc           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   sL   
  Establish correspondence between a value and how it should be compared
  c         C   s   | |  _  | |  _ d  S(   N(   t   valuet   compare(   R   RÖ   R×   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   ģ  s    	c         C   s3   t  | |  j  r" |  j | j k S|  j | k Sd  S(   N(   t
   isinstanceR   R×   (   R   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   __eq__ņ  s    c         C   s   |  | k S(   N(    (   R   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   __ne__ū  s    c         C   s   t  |  j  S(   N(   t   hashR×   (   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   __hash__   s    (   R   R   R   R   RŁ   RŚ   RÜ   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyRÕ   ē  s
   				t   Xi   i   i   c      
   C   s&  g  } x© | D]” } g  | D] }	 |	 j    ^ q }
 g  |
 D]+ } | j | rX | d j n d |  ^ q9 } g  t | |  D]! \ }	 } t d |	 d |  ^ qz } | j |  q Wy4 t t d |  d | d | d |  d	 d
   } Wn& t k
 rt	 d |  d |  } n Xt
 d | d |  | S(   Ni    t   UNKRÖ   R×   R   R7   RĖ   Rŗ   R   c         S   s
   |  j    S(   N(   R   (   t   p(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyR   $  s    RĪ   (   t   atom_groupsRŖ   t   resnamet   zipRÕ   R,   R|   RĶ   RT   RŹ   RŌ   (   R   R7   t   one_letter_fort   unknown_one_letterRĖ   t   max_seed_hss_countt   max_completion_hss_countt
   processedst   rgst   rgt   agst   agt   comparest   compt   seqRŅ   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   fragments_to_sequence  s.    
57	t   -c         C   s>   g  t  |   D]- \ } } | | k r t d | d |  ^ q S(   NRÖ   R×   (   R+   RÕ   (   R   t   gap_charR   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   map_to_sequence_frame5  s    c         C   sŃ   |  j  s g  S|  j   } g  | j D] } | d d  k	 r# | ^ q# } | sQ t  | d d j } d  g | d } xW | D]O \ } } | d  k r qz n  | s¤ t  | j | k s¹ t  | j | | j <qz W| S(   Ni    i’’’’i   (   R   R0   R   R   t   AssertionErrorRÖ   (   RĪ   R0   Rß   R   R   t   mappingRī   R   (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   map_back_to_alignment_frame=  s    	,(   t   phaserR    t   scitbxR   t   scitbx.suffixtreeR   R   Rµ   R   R   R    R)   R6   Ri   R   R   RÆ   R   RÉ   RŹ   RĶ   RŌ   RÕ   Rļ   Rņ   Rõ   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/chainalign.pyt   <module>   s0   + C’ tOp9	U	J$")