ó
j™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 m Z d	 „  Z d
 „  Z d e f d „  ƒ  YZ d d d „ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z e d k rXd  d l m Z e j d ƒ e j d ƒ x e d ƒ D] Z e ƒ  q?Wd GHn  d S(   iÿÿÿÿ(   t   division(   t   eigensystem(   t   superpose_kearsley_rotation(   t   matrix(   t   math(   t   flex(   t   differential_evolution(   t   euler_anglesc         C   s   t  j t d |  d | ƒ ƒ S(   sÓ   
  Kearsley, S.K. (1989). Acta Cryst. A45, 208-210.
  On the orthogonal transformation used for structural comparison

  Added by Peter H. Zwart, Nov 3rd, 2006.
  Converted to C++ by Gabor Bunkoczi, Apr 2008.
  t   reference_sitest   other_sites(   R   t   sqrR   (   R   R	   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyt   kearsley_rotation   s    c         C   s   |  j  ƒ  | j  ƒ  k s t ‚ t j | j |  ƒ ƒ } t j | | j ƒ  j ƒ  ƒ } t	 | j
 ƒ  ƒ } t	 | j ƒ  ƒ } t	 t j | d  ƒ j t j | d d !ƒ ƒ ƒ } t j t	 | d  ƒ | ƒ } t	 | | ƒ } xn t d ƒ D]` }	 t j t j | |	 ƒ ƒ }
 |
 d k rå x/ t d ƒ D] } | |	 d | c |
 <q Wqå qå Wt	 t j | d  ƒ j t j | d d !ƒ ƒ ƒ } t j | d  | ƒ } | j ƒ  | S(   s{  
Kabsch, W. (1976). Acta Cryst. A32, 922-923.
A solution for the best rotation to relate two sets of vectors

Based on a prototype by Erik McKee and Reetal K. Pai.

This implementation does not handle degenerate situations correctly
(e.g. if all atoms are on a line or plane) and should therefore not
be used in applications. It is retained here for development purposes
only.
  i   i   i    (   t   sizet   AssertionErrorR   R
   t   transpose_multiplyR   t   real_symmetrict	   transposet   as_sym_mat3t   listt   valuest   vectorst   colt   crosst   xrangeR   t   sqrtt   fabs(   R   R	   t   stst   eigst   valst   vecst   a3t   at   bt   it   dt   jt   b3(    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyt   kabsch_rotation   s     2#2t   least_squares_fitc           B   s)   e  Z d  d „ Z d d „ Z d „  Z RS(   t   kearsleyc         C   sî   | d k s t ‚ | d  k r' d } n  | |  _ | |  _ | j ƒ  |  _ | j ƒ  |  _ d  |  _ | d k r� t | |  j | |  j ƒ |  _ n  | d k r¾ t	 | |  j | |  j ƒ |  _ n  t
 j |  j ƒ |  j t
 j |  j ƒ |  _ d  S(   NR'   t   kabsch(   NR'   R(   (   t   NoneR   R   R	   t   meant   reference_shiftt   other_shiftt   rR   R%   R   R   t   t(   t   selfR   R	   t   method(    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyt   __init__6   s$     				

c         C   sI   | r* |  j  j |  j j | ƒ |  j j S|  j  j |  j |  j j Sd  S(   N(   R-   t   elemsR	   t   concatenateR.   (   R/   t   additional_sites(    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyt   other_sites_best_fitI   s    c         C   s   t  j d |  j |  j f ƒ S(   Nt	   tuple_r_t(   R   t   rtR-   R.   (   R/   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyR7   P   s    N(   t   __name__t
   __module__R1   R)   R5   R7   (    (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyR&   4   s   g      à?c         C   s-  |  j  ƒ  | j  ƒ  k o# d k n s. t ‚ | d k rU t j |  j  ƒ  t ƒ } n  |  j | ƒ } | j | ƒ } t d | d | ƒ } | j ƒ  } | | j	 ƒ  } t j
 | ƒ }	 |	 t d | | j  ƒ  ƒ }
 | |
 k } | j t ƒ d k rö | S| j | ƒ } | j | ƒ } t d | d | ƒ } | S(   s"   
  Reference: Chothia & Lesk???
  i    R   R	   i   N(   R   R   R)   R   t   boolt   Truet   selectR&   R5   t   normst   sortedt   intt   count(   t   sites_fixedt   sites_movingt	   selectiont   frac_discardt   sites_fixed_alnt   sites_moving_alnt   lsq_fit_objt   sites_moving_newt   deltast   deltas_sortedt   cutoff(    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyt	   sieve_fitT   s*    .		t
   nsd_enginec           B   s,   e  Z d d d  „ Z d „  Z d d „ Z RS(   c         C   sF   | |  _  | |  _ | |  _ |  j d  k rB |  j |  j  ƒ |  _ n  d  S(   N(   t   fixedt   d_fixedt   d_movingR)   t   get_mean_distance(   R/   RN   RO   RP   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyR1   {   s
    			c         C   s�   | j  ƒ  } d } d } xp t | ƒ D]b } xY t | d | ƒ D]D } t j | | ƒ t j | | ƒ } | | j ƒ  7} | d 7} q? Wq% W| | } | | S(   Ni    i   (   R   t   rangeR   t   doublet   norm(   R/   t   xyzt   NR"   R@   t   iit   jjt   dd(    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyRQ   ƒ   s    $
c         C   s  |  j  d  k r$ |  j | ƒ |  _  n  | d  k	 r< | |  _  n  d } d } x> | D]6 } |  j | } t j | j ƒ  ƒ } | | | 7} qO Wx: |  j D]/ } | | } t j | j ƒ  ƒ } | | 7} q“ W| |  j j ƒ  |  j } | | j ƒ  |  j  } t	 j
 | | d ƒ } | S(   Ni    g       @(   RP   R)   RQ   RN   R   t   minR=   R   RO   t   smathR   (   R/   t   movingRP   t
   tot_rho_mft
   tot_rho_fmt   siteRY   t   result(    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyt   nsd�   s$    
N(   R8   R9   R)   R1   RQ   Ra   (    (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyRM   z   s   	t   nsd_rigid_body_fitterc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s]  | |  _  | |  _ t |  j  ƒ |  _ t j |  j j ƒ |  _ t j |  j j |  j ƒ ƒ |  _ |  j j	 ƒ  |  _
 |  j  j	 ƒ  |  _ |  j |  j
 |  _ |  j |  j d |  _ d |  _ t j } | | f | | f | | f |  j |  j f |  j |  j f |  j |  j f g |  _ d  |  _ t j |  d d d d d d d d	 d
 d d t d d ƒ|  _ d  S(   Ni   i   t   population_sizet   fg333333ë?t   crgffffffî?t   n_crossi   t   epsg{®Gáz„?t   show_progresst   show_progress_nth_cyclei   (   RN   R\   RM   t   nsdeR[   R   RO   RQ   RP   R*   t   m_comt   f_comt   n_movR"   t   nt   pit   domainR)   t   xt   det    differential_evolution_optimizert   Falset	   optimizer(   R/   RN   R\   Ro   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyR1   ©   s    		!		W	*c         C   ss   | d | d | d f } | d | d | d f } t  j | d | d | d ƒ } | |  j |  j | } | S(   Ni    i   i   i   i   i   (   t   eulert
   zyz_matrixRm   Rl   (   R/   t   vectort   shiftt   abgR   t   new_xyz(    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyt   move_pointsÂ   s
    !c         C   s%   |  j  | ƒ } |  j j | ƒ } | S(   N(   R|   Rj   Ra   (   R/   Rx   t   nxyzR`   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyt   targetÊ   s    c         C   s   | G| Gt  | ƒ G| GHd  S(   N(   R   (   R/   t
   min_targett   mean_targett   best_vectort   txt(    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyt   print_statusÏ   s    c         C   s   |  j  |  j ƒ } | S(   N(   R|   Rq   (   R/   R}   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyt   best_shiftedÒ   s    (   R8   R9   R1   R|   R~   Rƒ   R„   (    (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyRb   ¨   s
   				c          C   sÆ  t  j ƒ  }  t  j ƒ  } t  j ƒ  } d } x¨ t d ƒ D]š } t  j d ƒ d d } | j ƒ  | k ru | j ƒ  } n  t  j d ƒ d } | j t | ƒ ƒ |  j t | | d ƒ ƒ | j t | | d ƒ ƒ q7 Wt | ƒ } | j | ƒ } | j |  ƒ }	 | j | ƒ }
 t	 | ƒ d k  s&t
 ‚ |	 |
 k s8t
 ‚ t j d d	 d
 ƒ } | |  d } t | | ƒ } | j ƒ  } | d | j ƒ  !| } | j ƒ  } t  j | ƒ } | d | d k  sÂt
 ‚ d  S(   Ni    i
   i   i   g      ð?i   g�íµ ÷Æ°>gffffffæ?gÍÌÌÌÌÌô?gÍÌÌÌÌÌ @i   i   i   g       @(   i   i   i   (   R   t   vec3_doubleRR   t   random_doubleRT   t   appendR   RM   Ra   t   absR   Rv   Rw   Rb   R„   R   R=   t   max(   t   moving1t   moving2RN   t	   max_noiseRW   t   noiseRU   t   neR   R    t   cR   t   fixed_rt   fitterR}   RY   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyt   tst_nsd×   s4    t   __main__(   t   randomi    i
   t   OKN(    t
   __future__R    t   scitbx.linalgR   t   scitbx.mathR   t   scitbxR   t   stdlibR   t   scitbx.array_familyR   R[   R   Rr   R   Rv   R   R%   t   objectR&   R)   RL   RM   Rb   R’   R8   R”   t   seedt   set_random_seedRR   RW   (    (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/superpose.pyt   <module>   s.   		"#./	