ó
A™xTc           @   s•  d  d l  m Z d  d l m Z d  d l m Z d  d l m Z d  d l m Z d  d l	 m
 Z
 d  d l m Z d  d l m Z d  d	 l Z d  d	 l Z d  d	 l Z e j j d
 ƒ Z d „  Z d e f d „  ƒ  YZ d e j f d „  ƒ  YZ d d „ Z d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d e f d „  ƒ  YZ  d „  Z! d „  Z" d „  Z# d „  Z$ d e f d „  ƒ  YZ% d e% f d  „  ƒ  YZ& d	 S(!   iÿÿÿÿ(   t   division(   t   crystal(   t   sgtbx(   t   flex(   t   matrix(   t   dicts(   t   user_plus_sys_time(   t   adopt_init_argsNt   cctbx_emma_extc         C   s+   t  j |  j ƒ  j ƒ  |  j ƒ  j ƒ  f ƒ S(   N(   R   t   rtt   rt	   as_doublet   t(   t   s(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   sgtbx_rt_mx_as_matrix_rt   s    t   positionc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   t  |  t ƒ  ƒ d  S(   N(   R   t   locals(   t   selft   labelt   site(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   __init__   s    c         C   s   d |  j  f t |  j ƒ S(   Ns   %-4s %7.4f %7.4f %7.4f(   R   t   tupleR   (   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   __repr__   s    (   t   __name__t
   __module__R   R   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR      s   	t   modelc           B   sÑ   e  Z d 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 d „ Z d d „ Z d e e e j e d d „ Z d e e e j e d „ Z RS(   c         C   sF   t  j j |  | ƒ |  j ƒ  g  |  _ | d  k	 rB |  j | ƒ n  d  S(   N(   R   t   special_position_settingst   _copy_constructort   reset_cb_opt
   _positionst   Nonet   add_positions(   R   R   t	   positions(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR      s    	

	c         C   s   |  j  S(   N(   t   _cb_op(   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   cb_op#   s    c         C   s   t  j ƒ  |  _ |  S(   N(   R   t   change_of_basis_opR!   (   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR   &   s    c         C   s   |  j  S(   N(   R   (   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR    *   s    c         C   s   t  |  j ƒ S(   N(   t   lenR   (   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   __len__-   s    c         C   s   t  |  j ƒ S(   N(   R$   R   (   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   size0   s    c         C   s   |  j  | S(   N(   R   (   R   t   key(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   __getitem__3   s    c         C   s2   |  j  j t | j |  j | j ƒ j ƒ  ƒ ƒ d  S(   N(   R   t   appendR   R   t   site_symmetryR   t
   exact_site(   R   t   pos(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   add_position6   s    c         C   s"   x | D] } |  j  | ƒ q Wd  S(   N(   R-   (   R   R    R,   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR   ;   s    c         C   s€   g  } x3 |  j  D]( } | j t | j | | j ƒ ƒ ƒ q Wt t j j |  | ƒ | ƒ } | j	 |  j
 ƒ  ƒ |  j
 ƒ  | _ | S(   N(   R   R)   R   R   R   R   R   R   t   change_basist   new_denominatorsR"   R!   (   R   R"   R    R,   t   result(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR.   ?   s    &	"c         C   sC   |  j  ƒ  j ƒ  j ƒ  } |  j | ƒ } | j  ƒ  j ƒ  s? t ‚ | S(   N(   t   space_group_infot   typeR"   R.   t   is_reference_settingt   AssertionError(   R   R"   R0   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   transform_to_reference_settingI   s    c         C   s%   |  j  ƒ  j ƒ  j ƒ  } |  j | ƒ S(   N(   R1   R2   t   change_of_hand_opR.   (   R   t   ch_op(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   change_handO   s    c         C   s¨   t  t j t j j |  ƒ ƒ ƒ } x€ |  j D]u } |  j | j ƒ } t j	 | ƒ } d } xE | j
 ƒ  D]7 } | d 7} | j t d | j d | d | ƒ ƒ qe Wq+ W| S(   Ni    i   R   s   _%03dR   (   R   R   R   t   symmetryt   cell_equivalent_p1R   R*   R   R   t   sym_equiv_sitest   coordinatesR-   R   R   (   R   t	   new_modelR,   R*   t   equiv_sitest   iR   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   expand_to_p1S   s    
c         C   sµ   | d  k r t j } n  | | IJt j j |  | ƒ |  j ƒ  j ƒ  sŒ | d IJ| d I|  j ƒ  j ƒ  IJ| d I|  j ƒ  j	 ƒ  IJn  x |  j
 ƒ  D] } | | IJq™ W| Jd  S(   Ns   Change of basis:s     c:s     c_inv:(   R   t   syst   stdoutR   R   t   show_summaryR"   t   is_identity_opt   ct   c_invR    (   R   t   titlet   fR,   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   showb   s     

 c         C   sƒ   d d l  m } | d  k r1 | j d d ƒ } n  | j d |  ƒ } x9 |  j ƒ  D]+ } | j | j d | j d | j	 ƒ ƒ qP W| S(   Niÿÿÿÿ(   t   xrayt   scattering_typet   constR   R   R   (
   t   cctbxRJ   R   t	   scatterert	   structureR    t   add_scatterert   customized_copyR   R   (   R   RN   RJ   R0   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   as_xray_structurem   s    	g      ø?i    c         C   s»  |  j  | d | d | d | d | d t ƒ} g  }	 x$ | j D] }
 |
 |  j k r= |	 Sq= WxZ| D]R} | d  k ry qa n  | rš t | j ƒ d k rš qa n  t d |  ƒ } t d |  ƒ } | | _ | g |  j | j | _ | d 7} x6 |  j ƒ  D]( } | j	 t
 d	 | j d
 | j ƒ ƒ qò W| j ƒ  d } xu | j D]j } | j | j } | j	 t
 d	 d | d
 | j | j ƒ ƒ | j	 t
 d	 d | d
 | j | j ƒ ƒ q8W|	 j | ƒ qa W|	 S(   Nt	   tolerancet'   models_are_diffraction_index_equivalentt   break_if_match_with_no_singlesRH   t   specifically_test_inversei    R   i   R   R   s	   ATOM_%03d(   t   best_superpositions_on_othert   Truet   component_model_numbersR   R$   t   singles2R   t   model_numberR    R-   R   R   R   R&   t
   ref_model2R	   t   elemsR)   (   R   t   other_modelRS   RT   RU   RH   t   improved_onlyt   new_model_numbert
   match_listt   new_model_listt   xt   matcht   test_new_modelR=   R,   R?   R   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   combine_with_otherx   s@     	
&

c         C   s|  t  |  d ƒ s! i  |  _ d  } n |  j j | d  ƒ } | d  k rfd d l m } | g }	 g  } | r¥ d d l m }
 | j ƒ  } |
 | j	 ƒ | _	 |	 j
 | ƒ n  x® |	 D]¦ } | j d |  d | d | d | d	 | ƒ } | j ƒ  d
 k r¬ t | j d
 j ƒ } xK | j D]= } t | j ƒ } | d
 k r| | k r| j
 | ƒ qqWq¬ q¬ W| |  j | <n  | sxd  g } n  | S(   Nt
   match_dictiÿÿÿÿ(   t   euclidean_model_matching(   t   deepcopyt   model1t   model2RS   RT   RU   i    (   t   hasattrRg   R   t   getRM   Rh   t   copyRi   R8   R!   R)   t   model_matchest	   n_matchesR$   t   refined_matchest   pairs(   R   R^   RS   RT   RU   RH   RV   Ra   t   emmat	   test_listRi   t   inv_other_modelt
   test_modelt   matchest   best_number_of_matchesRd   t   n(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyRW   ¡   s<    
					 N(   R   R   R   R   R"   R   R    R%   R&   R(   R-   R   R.   R5   R8   R@   RI   RR   RX   RA   RB   Rf   RW   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR      s.   									
				&	i   c         C   sK   d d d g } x5 t  d ƒ D]' } |  | | k r | | | | <q q W| S(   Ni    i   (   t   xrange(   t   continuous_shift_flagst   shiftt   selectort   filtered_shiftR?   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   filter_shiftÑ   s
    t   euclidean_match_symmetryc           B   s,   e  Z d  „  Z d d „ Z d d d „ Z RS(   c         C   s¡   t  |  t ƒ  ƒ t j d t j d t d d d t d | d | ƒ d | j ƒ  d	 | j ƒ  ƒ } | j	 ƒ  |  _
 | j ƒ  |  _ | j ƒ  sŽ t ‚ | j ƒ  |  _ d  S(
   Nt   flagst   use_space_group_symmetryt   use_space_group_ltriÿÿÿÿt   use_seminvariantst   use_normalizer_k2lt   use_normalizer_l2nt   space_group_typet   seminvariant(   R   R   R   t   search_symmetryt   search_symmetry_flagst   FalseRX   R2   t   structure_seminvariantst   subgroupt   rt_mxt   continuous_shiftst   continuous_shifts_are_principalR4   R{   (   R   R1   t   use_k2lt   use_l2nR‰   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR   Ú   s    			i   c         C   s   t  |  j | | ƒ S(   N(   R   R{   (   R   R|   R}   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR   ê   s    t    c         C   sV   | d  k r t j } n  | d | j ƒ  IJ| |  j j ƒ  j ƒ  IJ| |  j IJd  S(   Ns   euclidean_match_symmetry: (   R   RA   RB   t   rstripRŽ   R2   t   lookup_symbolR�   (   R   RG   RH   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyRI   í   s
     N(   R   R   R   R   R   RI   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR€   Ø   s   	c         C   s   t  |  ƒ } | | =| S(   N(   t   range(   Ry   R?   t   singles(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   generate_singlesó   s    c         C   s   t  |  d | d ƒ S(   Ni    (   t   cmp(   t   pair_at   pair_b(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   pair_sort_functionø   s    c        	   C   s"   t  j d d d d d d d d ƒ S(   Nt   exclude_pairsi    t	   add_pairst   eliminate_weak_pairst   refine_adjusted_shift(   R   t   easy(    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   match_refine_timesû   s
    	t   match_refinec           B   s�   e  Z d d  „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d d d
 d „ Z d d e j e d „ Z RS(   c         C   s	  t  |  t ƒ  d d ƒt |  j j ƒ  |  j ƒ |  _ t |  j j ƒ  |  j ƒ |  _	 |  j |  j f g |  _
 t j |	 ƒ |  _ |  j d  k rš t ƒ  |  _ n  |  j ƒ  |  j ƒ  |  j ƒ  t |  j ƒ |  j |  _ |  j
 j t ƒ |  j j ƒ  |  j	 j ƒ  |  j ƒ  d  S(   Nt   excludet   initial_shift(   s   initial_shift(   R   R   R˜   t
   ref_model1R&   t   i_pivot1t   singles1R\   t   i_pivot2RZ   Rr   R   t   colt   adjusted_shiftt   timesR   R¢   R�   Rž   RŸ   R   t
   eucl_symopt   ref_eucl_rtt   sortRœ   t   calculate_rms(   R   RS   R¦   R\   t   match_symmetryt   add_pair_extR§   R©   R­   R¥   R¬   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR     s     


c         C   se   t  ƒ  } |  j j |  j j |  j |  j t j |  j	 ƒ t j |  j
 ƒ ƒ |  j j | j ƒ  7_ d  S(   N(   R   R²   t
   next_pivotR±   R{   R­   R«   R   t   intR¨   RZ   R¬   R�   t   delta(   R   t   timer(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR�     s    			c         C   sá   t  ƒ  } x¹ t |  j ƒ rÄ t |  j ƒ rÄ |  j j |  j t j |  j ƒ t j |  j ƒ ƒ sa Pn  |  j j	 ƒ  |  j j
 ƒ  f } |  j j | ƒ |  j j | d ƒ |  j j | d ƒ |  j ƒ  q W|  j j | j ƒ  7_ d  S(   Ni    i   (   R   R$   R¨   RZ   R²   t	   next_pairR«   R   R´   t
   new_pair_1t
   new_pair_2Rr   R)   t   removeR    R¬   Rž   Rµ   (   R   R¶   t   new_pair(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyRž   ,  s    	!	c         C   s!  t  ƒ  } xù d } d } x? |  j d D]0 } |  j | ƒ } | | k r& | } | } q& q& W| d k rj Pn  | |  j k  r¥ |  j |  j d ƒ } | |  j k  r¥ Pq¥ n  t |  j ƒ d k sÀ t ‚ |  j j | ƒ |  j j | d ƒ |  j	 j | d ƒ |  j
 ƒ  q |  j j | j ƒ  7_ d  S(   Ni    i   (   R   Rr   t   calculate_shortest_distRS   R$   R4   Rº   R¨   R)   RZ   R    R¬   RŸ   Rµ   (   R   R¶   t	   weak_pairt   max_distt   pairt   dist(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyRŸ   A  s*    	 c         C   s+   t  j |  j |  j | j ƒ } | |  j S(   N(   R   Rª   R­   R\   R   R«   (   R   t   i_model2t   c2(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   apply_eucl_opsW  s     c         C   s9   |  j  | d ƒ } t j |  j j | d ƒ | ƒ j ƒ  S(   Ni   i    (   RÃ   R   t   min_sym_equiv_distance_infoR²   t   equiv1t   diff(   R   R¿   RÂ   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   calculate_shortest_diff[  s    c         C   s%   |  j  j ƒ  j } | |  j | ƒ ƒ S(   N(   R¦   t	   unit_cellt   lengthRÇ   (   R   R¿   RÉ   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR¼   `  s    c         C   s4   g  } x' |  j  D] } | j |  j | ƒ ƒ q W| S(   N(   Rr   R)   RÇ   (   R   t   shortest_diffsR¿   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   calculate_shortest_diffsd  s    c   	      C   sá   t  ƒ  } |  j j ƒ  } t j d d d g ƒ } xN |  j ƒ  D]@ } |  j j | d d ƒ} | j | ƒ } | t j | ƒ 7} q= W| t	 |  j
 ƒ } t j | j | ƒ ƒ } t j |  j ƒ | |  _ |  j j | j ƒ  7_ d  S(   Ng        R}   i   (   R   R¦   RÈ   R   Rª   RË   R±   R   t   orthogonalizeR$   Rr   t   fractionalizeR«   R¬   R    Rµ   (	   R   R¶   RÈ   t   sum_diff_cartRÆ   t   diff_allowedt	   diff_cartt   mean_diff_cartt   mean_diff_frac(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR    j  s    	c         C   sr   |  j  j ƒ  j } t j g  |  j ƒ  D] } | | ƒ ^ q% ƒ |  _ t j t j	 |  j ƒ t
 |  j ƒ ƒ |  _ d  S(   N(   R¦   RÈ   RÉ   R   t   doubleRË   t   shortest_distancest   matht   sqrtt   sum_sqR$   Rr   t   rms(   R   RÉ   t   d(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR°   w  s    +i   c   
      C   sÛ  | d  k r t j } n  | d IJ| d IJ| d I|  j j j d d ƒ IJ| d I|  j j j ƒ  j d d ƒ d d !IJ| d	 |  j f IJ| d
 It	 |  j
 ƒ IJxa |  j
 D]V } | d I|  j | d j I| |  j | d j I| d |  j | ƒ f IJq´ WxÂ d |  j |  j f d |  j |  j f f D]– \ } } } | d | It	 | ƒ Id } x` | D]X }	 | | k r…Pn  | | d k r¦| J| d In  | d I| |	 j I| d 7} qoW| Jq9W| Jd  S(   Ns   Match summary:s     Operator:s          rotation:t   formats   %.6gs       translation:i   iÿÿÿÿs"     rms coordinate differences: %.2fs     Pairs:s      i    s   %.3fi   s     Singles model %s:t    (   R   RA   RB   R	   R
   t   mathematica_formR   t	   transposeRØ   R$   Rr   R¦   R   R\   R¼   R¨   RZ   (
   R   RH   t   truncate_singlest   singles_per_lineR¿   t   i_modelt	   ref_modelR—   R?   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyRI   }  s6     

"
%% t   SEc         C   sè   d d l  m } | j d | ƒ } |  j j | ƒ } d d l m } | j ƒ  }	 x( | j ƒ  D] }
 |	 j	 |  j
 |
 ƒ q] W| j |	 ƒ | d  k	 rÔ | j ƒ  } t | d ƒ } | | IJ| j ƒ  | d | IJn  | rä | j ƒ  Sd  S(   Niÿÿÿÿ(   RJ   RK   (   R   t   ws,   
Wrote model 2 mapped to model 1 to file %s (   RM   RJ   RN   R\   RR   t   cctbx.array_familyR   t   vec3_doublet
   sites_fracR)   R	   t   set_sites_fracR   t   as_pdb_filet   opent   closet   as_emma_model(   R   t
   output_pdbRK   RH   t   return_superposed_model2RJ   t   xray_scattererRk   R   t
   new_coordsRc   t
   pdb_stringt   ff(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   get_transformed_model2˜  s     

N(   R   R   R   R   R�   Rž   RŸ   RÃ   RÇ   R¼   RË   R    R°   RI   RA   RB   RX   Rò   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR£     s   											c         C   s?   t  t |  j ƒ t | j ƒ ƒ } | r, | St  |  j | j ƒ S(   N(   R™   R$   Rr   RØ   (   t   match_at   match_bR?   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   match_sort_function¯  s    " c      	   C   s.  t  | ƒ } | d k r d  S| d j } t  | d j ƒ } d g | } xÑ t | d ƒ D]¿ } | | } | | rz qZ n  | j | k  r¤ | j } t  | j ƒ } n  xr t | d | ƒ D]] }	 | |	 }
 | j |
 j k s| r¸ |
 j | d | | t  |
 j ƒ k r¸ d | |	 <q¸ q¸ WqZ Wx2 t | d d d ƒ D] } | | r4| | =q4q4W|  d k r*| d k r*t } x- | D]% } t  | j ƒ d k rwt } PqwqwW| r x4 t  | ƒ d k rÜ| d j d d k rÜ| d =q©WxG t  | ƒ d k rü| d =qàWq*x' t  | d j ƒ d k r&| d =qWn  d  S(   Ni    i   iÿÿÿÿ(   i    i    (   R$   RØ   Rr   Rz   RX   R‹   (   t   space_group_numberRq   t   rms_penalty_per_siteRp   t   best_rmst   best_n_pairst   is_redundantR?   t   match_it   jt   match_jt   trivial_matches_onlyRd   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   weed_refined_matches´  sJ     

 	

   c         C   s0   t  |  j ƒ  ƒ } t  | j ƒ  ƒ } | | | S(   N(   R   RF   RE   (   t   model1_cb_opt   model2_cb_opR®   t   inv_m1t   m2(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   match_rt_from_ref_eucl_rtØ  s    c         C   sµ  t  |  j ƒ  d t d | ƒ} t j g  |  D] } | j ^ q, ƒ } t j g  | D] } | j ^ qQ ƒ } t j | |  j ƒ  |  j	 ƒ  |  j
 ƒ  | | ƒ }	 t ƒ  }
 g  } xt |  j ƒ  ƒ D]ò } xé t | j ƒ  ƒ D]Õ } xÌ | j D]Á } | | | j } t j |	 j | ƒ | | j ƒ } | j ƒ  | k  rä | j ƒ  } t | |  | | |	 | | | | |
 ƒ
 } t |  j ƒ  | j ƒ  | j ƒ | _ | j | ƒ | | ƒ r¥| Sqä qä WqÔ Wq» W| S(   NR‘   R’   (   R€   R1   RX   R   Rå   R   t   extt   add_pairRÈ   t   space_groupt   min_distance_sym_equivR¢   Rz   R&   RŽ   R   RÄ   RÅ   R{   RÀ   R�   R£   R  R"   R®   R	   R)   (   R¦   R\   RS   RT   t   shall_breakR±   R,   t   ref_model1_sitest   ref_model2_sitesR²   t   accumulated_match_refine_timesRq   R§   R©   R­   RÂ   t	   dist_infot   allowed_shiftRd   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   compute_refined_matchesà  sP    %%								t   delegating_model_matchesc           B   sP   e  Z d  Z d e d
 d d „ Z d „  Z d „  Z d d d „ Z d d	 „ Z	 RS(   se  
  emma loops over all pairs of sites from the first and second structure.
  If a pair is closer than tolerance (under Euclidean symmetry) it
  initiates a "match and refine" procedure which incrementally adds the
  "next closest" pair, with a distance below 2*tolerance. Each time
  a pair is added the allowed origin shifts (if any) are refined to
  minimize the rmsd of all the pairs matched so far. When there are
  no more pairs to add, emma goes into a "weeding" procedure. This
  procedure sorts the pairs by distance. If there are distances above
  tolerance, the worst pair is removed, followed by the same allowed
  origin shift refinement as before. The weeding is repeated until
  there are no pairs above tolerance.

  emma was written with substructures in mind, which usually have a few
  (by macromolecular standards) sites placed far apart in space.
  g      ð?gš™™™™™©?c   	      C   sK  | d  k r d „  } n  t |  t ƒ  ƒ | j ƒ  j ƒ  s@ t ‚ | j ƒ  j ƒ  sX t ‚ | j ƒ  } | j ƒ  } | j ƒ  j | j ƒ  ƒ s‘ t ‚ | j	 ƒ  | j	 ƒ  k r¸ | j
 ƒ  } n  | j ƒ  j | j ƒ  ƒ sÙ t ‚ | j	 ƒ  | j	 ƒ  k s÷ t ‚ t | | | | | ƒ |  _ |  j j t ƒ t | j ƒ  j ƒ  j ƒ  |  j | ƒ d  S(   Nc         S   s   t  S(   N(   R‹   (   Rd   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR	  '  s    (   R   R   R   R"   RD   R4   R5   RÈ   t   is_similar_toR  R8   R  Rq   R¯   Rõ   Rÿ   R1   R2   t   number(	   R   Rj   Rk   RS   RT   R	  R÷   R¦   R\   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR   !  s(    !!c         C   s   t  |  j ƒ S(   N(   R$   Rq   (   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyRp   <  s    c         C   s-   t  |  j ƒ d k r d St  |  j d j ƒ S(   Ni    (   R$   Rq   Rr   (   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   n_pairs_best_match?  s     i   i    c         C   sÊ   | d k s t  ‚ d | k o/ |  j ƒ  k  n s: t  ‚ | d k rR |  j } n	 |  j } |  j ƒ  | j ƒ  k rw | St d | ƒ } | d 8} x3 |  j | j D]! } | j | j	 ƒ  | | ƒ q¡ W| S(   Ni   i   i    R   (   i   i   (
   R4   Rp   Rj   Rk   R&   R   Rq   Rr   R-   R    (   R   Rà   t   i_refined_matchest   source_modelR0   R¿   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   consensus_modelC  s    (	
c         C   sß   | d k s t  ‚ d | k o/ |  j ƒ  k  n s: t  ‚ |  j | j } | d k r€ t d |  j ƒ } |  j } | j ƒ  } n t d |  j ƒ } |  j } x= | j ƒ  D]/ } | j	 t
 d | j d | | j j ƒ ƒ q¨ W| S(   Ni   i   i    R   R   R   (   i   i   (   R4   Rp   Rq   R	   R   Rk   Rj   t   inverseR    R-   R   R   R   R]   (   R   Rà   R  R	   R0   R  R,   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   transform_modelR  s    (		-N(
   R   R   t   __doc__R‹   R   R   Rp   R  R  R  (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR    s   		Ro   c           B   s   e  Z d  e e d d „ Z RS(   g      ð?gš™™™™™©?c         C   sA   | r d „  } n d  } t t |  ƒ j | | | | | | ƒ d  S(   Nc         S   s(   t  |  j ƒ d k p' t  |  j ƒ d k S(   Ni    (   R$   R¨   RZ   (   Rd   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR	  j  s    (   R   t   superRo   R   (   R   Rj   Rk   RS   RT   RU   R÷   R	  (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyR   d  s    (   R   R   R‹   R   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyRo   b  s   ('   t
   __future__R    RM   R   R   Rä   R   t   scitbxR   t   scitbx.python_utilsR   t   libtbx.utilsR   t   libtbxR   RA   RÕ   t   boost.pythont   boostt   pythont
   import_extR  R   t   objectR   R   R   R   R€   R˜   Rœ   R¢   R£   Rõ   Rÿ   R  R  R  Ro   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/euclidean_model_matching.pyt   <module>   s2   	¸			­		$		/S