ó
šxTc           @   sƒ   d  d l  Z  d  d l Z d  d l Z d e  j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d e f d	 „  ƒ  YZ
 d S(
   iÿÿÿÿNt   TemplateExceptionc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyR       s   t   Matchc           B   sh   e  Z d  Z d Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z e d
 „  ƒ Z RS(   s4   
    Matches solutions and symmetry equivalents
    g      @g333333ã?c   
      C   sI  t  j ƒ  } x0 | j ƒ  D]" \ } } | j | | |  j ƒ q Wt j j j ƒ  |  _	 t  j
 ƒ  |  _ t  j ƒ  |  _ t  j ƒ  } xc | j j ƒ  D]R \ } } | j | ƒ | j ƒ  |  j	 | <| j ƒ  |  j | <| j ƒ  |  j | <qŽ Wt  j | t  j d g d g d g ƒ t  j ƒ  ƒ }	 t  j ƒ  |  _ |  j j |	 ƒ | j ƒ  |  _ d  S(   Ni    (   i    i    i    (   t   phasert   ENSEt   itemst   addENSE_PDB_RMSt   MINIMAL_MODEL_ERRORt   scitbxt   stlt   mapt   stl_string_doublet   _extent_fort   map_string_dvect3t   _centre_fort   map_string_vect_of_dvect3t   _symm_eulers_fort
   data_ptgrpt   PDBt   setup_moleculest   mean_rad_prptt   centret   symm_eulerst   DataAt	   data_reflt   data_resharpt   ResultMRt   _resultt   setDataAt   keyst   _known_ensembles(
   t   selft   pdb_file_fort	   unit_cellt   inputt   namet   pdb_filet   defaultst   modlidt   data_pdbt   data_a(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   __init__   s&    c         C   s3   t  | j ƒ d k r/ | j d j ƒ  |  j k St S(   Ni   i    (   t   lent   KNOWNt	   getModlidR   t   False(   R    t   template(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   is_template_valid/   s    c         C   s�   t  g  | D] } |  j d | ƒ ^ q
 ƒ s7 t d ‚ n  |  j j | ƒ |  j j |  j |  j |  j |  j	 g  ƒ } d | k  r‰ t d ‚ n  d  S(   NR/   s   Invalid template foundi    s   Non-unique template set(
   t   allR0   R    R   t	   setDotSolt   calcDuplicateSolutionsR   R   R   t   AVERAGE_RESOLUTION(   R    t	   templatest   tt   equiv_count(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   check_templates7   s    +		c         C   sÅ   g  } x¸ | j  D]­ } | j ƒ  |  j k rA t d | j ƒ  ‚ n  t j | j | g g  | j | j | j	 | j
 | j | j | j | j | j | j g  t j ƒ  t j ƒ  d ƒ } | j | ƒ q W| S(   Ns   Unknown ensemble: %ss   P 1(   R,   R-   R   R    R   t   mr_sett
   ANNOTATIONt   TFt   TFZt   LLGt   ORIG_LLGt   Rt   ORIG_Rt   PAKt   ORIG_NUMt   KEEPt   EQUIVt   MapCoefst   map_str_float1Dt   append(   R    R9   t   singlest   mr_ndimt
   single_set(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   disassembleJ   s0    			c         C   s  |  j  d | ƒ |  j d | ƒ } g  | D] } | j ^ q) } g  } x# t | ƒ D] \ } } | | _ qQ Wt | ƒ }	 x[| D]S} |	 | _ | | g }
 |  j j |
 ƒ |  j j |  j |  j	 |  j
 |  j g  ƒ } d | k  r} | d k sñ t ‚ |  j j ƒ  } g  | D] } | j s| ^ q} t | ƒ d k s:t ‚ | d j t | ƒ k  sYt ‚ | | d j j } | d j |	 k rš| j | | | f ƒ qÐ| | j k s¯t ‚ | j | | d j | f ƒ q} q} Wx& t | | ƒ D] \ } } | | _ qäW| S(   NR5   R9   i    i   (   R8   RK   RB   t	   enumerateR+   R   R2   R3   R   R   R   R4   t   AssertionErrort	   getDotSolRC   RD   RG   t   zip(   R    R9   R5   t   single_setst   st   original_template_numst   matched_pairst   indexR6   t
   last_indext   solution_setR7   t   matched_solutionst   solt   equivst
   orig_index(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   match_single_copiesj   sB    			"c         C   só   t  g  | D] } t | ƒ d k ^ q
 ƒ r7 t d ‚ n  g  } x— t t | Œ  ƒ D]ƒ \ } } t j d t g  | D] } | j ^ qo d ƒ g  d d d d d d d | d d g  t j	 ƒ  t j
 ƒ  d ƒ } | j | ƒ qP Wt | ƒ d k sï t ‚ | S(   Ni   s   Invalid matching pairst    i    g      à?s   P 1(    (   t   anyR+   R    RL   RO   R   R9   t   sumR,   RE   RF   RG   RM   (   R    t   matching_pairst   pt   mr_setsRT   t   setsRQ   R9   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   assemble¢   s2    +"			c         C   sP   |  j  j |  j d | ƒ ƒ |  j  j |  j |  j |  j |  j g  ƒ } | d k S(   NR_   i   (   R   R2   Rc   R3   R   R   R   R4   (   R    R_   R7   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   match_originsÃ   s    		c         C   sY   t  j j | ƒ } t | j ƒ  ƒ d k r6 t d ‚ n  |  d | d | j ƒ  d j ƒ  ƒ S(   Ni    s   Bad mtz fileR!   R"   (   t   iotbxt   mtzt   objectR+   t   crystalst
   ValueErrort   unit_cell_parameters(   t   clsR!   t   mtz_fileRf   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   from_mtzÐ   s    (   R   R   t   __doc__R4   R   R*   R0   R8   RK   R[   Rc   Rd   t   classmethodRm   (    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyR   	   s   				 	8	!	t   EquivalentMatcherc           B   sq   e  Z d  Z d d „ Z d „  Z d „  Z d d „ Z d „  Z d „  Z e	 d	 „  ƒ Z
 e d
 „  ƒ Z d „  Z RS(   s$   
    Matches equivalent mr_sets
    g      @c         C   sh   t  j | t  j d g d g d g ƒ t  j ƒ  ƒ } t  j ƒ  |  _ |  j j | ƒ | |  _ | |  _ d  S(   Ni    (   i    i    i    (	   R   R   R   R   R   R   R   t   _resolutiont   _space_group_hall(   R    t   space_group_hallR"   t
   resolutionR)   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyR*   ã   s    	c   	         sì   t  g  | j D] } | j ƒ  ^ q ƒ } ˆ  j d t | ƒ ƒ si t d d j ‡  f d †  | Dƒ ƒ ‚ n  g  } xv | D]n } t  g  | j D] } | j ƒ  ^ q‰ ƒ } | | k r¶ qv n  |  j d | d | d ˆ  ƒ rv | j | ƒ qv qv W| S(   Nt   namess   Ensemble definition missing: %ss   , c         3   s*   |  ]  } ˆ  j  d  | g ƒ s | Vq d S(   Ru   N(   t
   is_defined(   t   .0t   e(   t   definitions(    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pys	   <genexpr>÷   s    t   leftt   rightRy   (	   t   sortedR,   R-   Rv   t   sett   KeyErrort   joint   is_equivalentRG   (	   R    t   trialt   othersRy   t   nt   trial_ensest   equivalentst   partialt   enses(    (   Ry   s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   equivalents_ofð   s    ((		c         C   se   |  j  j | | g ƒ |  j  j | j ƒ  | j ƒ  | j ƒ  |  j g  ƒ } | d k s[ t ‚ | d k S(   Ni    i   (   i    i   (   R   R2   R3   t
   extent_fort
   centre_fort   symm_eulers_forRq   RM   (   R    Rz   R{   Ry   R7   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyR€     s    					i   c      	   C   sV  | j  } | j  } g  } x· | D]¯ } g  }	 |  j d | g ƒ }
 xo | D]g } | j ƒ  | j ƒ  k rk qG n  |  j d | g ƒ } |  j d |
 d | d | ƒ rG |	 j | ƒ qG qG W|	 r | j | |	 f ƒ q q Wg  } xL t t | ƒ | d d ƒ D]. } |  j d | d | d | ƒ } | rõ Pqõ qõ Wg  | D]$ } |  j d	 | d
 | d | ƒ ^ q.S(   Nt   ndimsRz   R{   Ry   i   iÿÿÿÿR…   t   countt   matcht   leftst   rights(	   R,   t	   as_mr_setR-   R€   RG   t   rangeR+   t   matching_combinationst	   repackage(   R    Rz   R{   Ry   t   minimumt
   left_compst   right_compsR…   t   lcRY   t   mslt   rct   msrt	   solutionsR�   t   m(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   overlap  s0    		#		c      	   C   s?   t  | j ƒ } |  j d | d | d | d | ƒ r7 t St Sd  S(   NRz   R{   Ry   R•   (   R+   R,   Rž   t   TrueR.   (   R    t   largert   smallerRy   t   size(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   containsC  s    		c      	   C   sÕ   d d  l  } g  } x¼ | j | | ƒ D]¨ } t | Œ  \ } } |  j d | ƒ }	 x{ | j | Œ  D]j }
 t t |
 ƒ ƒ | k rƒ q_ n  |  j d |
 ƒ } |  j d |	 d | d | ƒ r_ | j | |
 f ƒ q_ q_ Wq% W| S(   NiÿÿÿÿRŒ   Rz   R{   Ry   (	   t	   itertoolst   combinationsRO   R‘   t   productR+   R}   R€   RG   (   R    R…   R�   Ry   R¤   t
   compositest   combinationRz   R�   t   ms_leftt	   structuret   ms_right(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyR“   S  s    c         C   sk   t  j j | ƒ } t | j ƒ  ƒ d k r6 t d ‚ n  |  d | j ƒ  d j ƒ  d | j ƒ  j ƒ  j	 ƒ  ƒ S(   Ni    s   Bad mtz fileR"   Rs   (
   Re   Rf   Rg   R+   Rh   Ri   Rj   t   space_group_infot   typet   hall_symbol(   Rk   Rl   Rf   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyRm   h  s    c         C   sk   t  |  d ƒ } t  |  d ƒ } |  g  | D] } | | k r* | ^ q* g  | D] } | | k rL | ^ qL f S(   Ni    i   (   R}   (   RŽ   R�   R�   t   l_comt   r_comRQ   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyR”   v  s
    "c         C   sL   t  j d | g  d d d d d d d d d d g  t  j ƒ  t  j ƒ  |  j ƒ S(   NR\   i    (   R   R9   RE   RF   Rr   (   R    RŒ   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyR‘   ‚  s$    		(   R   R   Rn   R*   Rˆ   R€   Rž   R£   R“   Ro   Rm   t   staticmethodR”   R‘   (    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyRp   Þ   s   		(		t   EnsembleDefinitionc           B   sw   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z i  i  d „ Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z RS(   s$   
    Store ensemble definitions
    c         C   sC   t  j j j ƒ  |  _ t j ƒ  |  _ t j ƒ  |  _	 t
 ƒ  |  _ d  S(   N(   R	   R
   R   R   R   R   R   R   R   R   R}   t   _names(   R    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyR*   ž  s    c         C   s†   t  j ƒ  } xc | j j ƒ  D]R \ } } | j | ƒ | j ƒ  |  j | <| j ƒ  |  j | <| j	 ƒ  |  j
 | <q W|  j j | ƒ d  S(   N(   R   R   R   R   R   R   R   R   R   R   R   R³   t   add(   R    R$   R#   R&   R'   R(   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   insert¦  s    c         C   s   |  j  j | ƒ S(   N(   R³   t
   issuperset(   R    Ru   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyRv   ³  s    c         C   s   |  j  S(   N(   R³   (   R    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   knowns¸  s    c         C   s   t  | ƒ j |  j ƒ S(   N(   R}   t
   differenceR³   (   R    Ru   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   unknowns½  s    c         C   s†   t  j ƒ  } x- | j ƒ  D] \ } } | j | | | ƒ q Wx- | j ƒ  D] \ } } | j | | | ƒ qI W|  j d | d | ƒ d  S(   NR$   R#   (   R   R   R   t   addENSE_PDB_IDR   Rµ   (   R    R$   t   identity_fort   rms_forR#   R`   t   it   r(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   defineÂ  s    c         C   s   |  j  S(   N(   R   (   R    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyR‰   Ï  s    c         C   s   |  j  S(   N(   R   (   R    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyRŠ   Ô  s    c         C   s   |  j  S(   N(   R   (   R    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyR‹   Ù  s    c         C   s(   |  j  j ƒ  } | d =| d =| d =| S(   NR   R   R   (   t   __dict__t   copy(   R    t   odict(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   __getstate__Þ  s
    c         C   sG   |  j  j | ƒ t j j j ƒ  |  _ t j ƒ  |  _	 t j
 ƒ  |  _ d  S(   N(   RÀ   t   updateR	   R
   R   R   R   R   R   R   R   R   (   R    RÂ   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   __setstate__ç  s    (   R   R   Rn   R*   Rµ   Rv   R·   R¹   R¿   R‰   RŠ   R‹   RÃ   RÅ   (    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyR²   ™  s   										(   R   t	   iotbx.mtzRe   t
   scitbx.stlR	   t   PhaserExceptionR    Rg   R   Rp   R²   (    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/template.pyt   <module>   s   Õ»