ó
ª™xTc           @   sÜ   d  d l  m Z d  d l m Z d  d l Z d  d l Z d a d „  Z d „  Z	 d e
 f d „  ƒ  YZ d e
 f d	 „  ƒ  YZ d
 „  Z d e
 f d „  ƒ  YZ d e j f d „  ƒ  YZ d „  Z e d k rØ e ƒ  d GHn  d S(   iÿÿÿÿ(   t   division(   t   scalingNc          C   s„   d d l  m }  d d l m } t d  k r€ |  j d d d d d d	 d
 d d d d d d d d g ƒ } | d d d | ƒ a n  t S(   Niÿÿÿÿ(   t   flex(   t   chebyshev_polynomegy5ÐÍû5,ÀgW¬LdÿÞ¿g89´CRÀgKPûyÃŽß¿gvù&ª×ì?g�²E¸*¡?gÄ-">Â¶?gº6­©…»?gå>]”)¬?gšoa×ª¿g+†Øe§¿ggØéw³?g¦iðEÿ¸¿g*¿w¥ý£?gŒ¸NÓz²¿i   i    i   (   t   cctbx.array_familyR   t   scitbx.mathR   t
   log_p_solct   Nonet   double(   R   R   t   coeffs(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   get_log_p_solc   s    c         C   sC   |  d k  s d |  k  r$ t  d ‚ n  t ƒ  } t j | j |  ƒ ƒ S(   s,   
  Calculate solvent fraction probability
  i    g      ð?s   solvent content out of range(   t
   ValueErrorR
   t   matht   expt   f(   t   scR   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   p_solc_calc   s    	t   density_calculatorc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s9   
  Calculate Matthews coefficient and solvent fraction
  c         C   s)   | j  ƒ  j ƒ  | j ƒ  j ƒ  |  _ d  S(   N(   t	   unit_cellt   volumet   space_groupt   order_zt
   asu_volume(   t   selft   crystal(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   __init__)   s    c         C   s#   | d k r t  d ‚ n  |  j | S(   Ni    s   Incorrect weight(   R   R   (   R   t   weight(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   vm-   s    c         C   s   | d |  j  d | ƒ S(   NgÝ$�•Cã?R   (   R   (   R   R   t   rho_spec(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   macromolecule_fraction2   s    c         C   s   d |  j  d | d | ƒ S(   Ng      ð?R   R   (   R   (   R   R   R   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   solvent_fraction5   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   (    (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyR   %   s
   			t	   componentc           B   sA   e  Z d  Z d „  Z d „  Z d „  Z e e ƒ Z e e ƒ Z RS(   s   
  Macromolecule component
  c         C   s   | |  _  | |  _ d  S(   N(   t   mwR   (   R   R#   R   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyR   >   s    	c         C   s   |  d | d d d ƒ S(   NR#   g      \@R   g®Gáz®ç?(    (   t   clst   nres(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   proteinB   s    c         C   s   |  d | d d d ƒ S(   NR#   g     ps@R   g      à?(    (   R$   R%   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   nucleicE   s    (   R   R    R!   R   R&   R'   t   classmethod(    (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyR"   :   s   			c         C   s¼   |  s t  d ‚ n  t g  |  D]$ } | j d | j d | j ƒ ^ q ƒ } g  } d } x` t r· d | | } | d k s~ t ‚ | d k  rŽ Pn  | j | t d | ƒ f ƒ | d 7} qX W| S(   Ns   Empty component listR   R   i   g      ð?i    R   (	   R   t   sumR   R#   R   t   Truet   AssertionErrort   appendR   (   t
   componentsR   t   ct
   unit_mfract   resultst   countR   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   number_tableL   s    1	t   p_vm_calculatorc           B   sM   e  Z d  Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(	   s/   solvent content/matthews probability calculatori    c         C   s  | j  ƒ  d  k	 r$ | j ƒ  d  k	 s* t ‚ | j  ƒ  j ƒ  |  _ | j ƒ  j ƒ  |  _ | d  k	 rl | |  _ n	 d |  _ | d  k	 r� | |  _	 n	 d |  _	 |  j	 |  j d k s² t ‚ d |  _
 d |  _ d |  _ g  |  _ | d  k rî t j } n  | |  _ | |  _ |  j ƒ  |  j ƒ  |  _ d  S(   Ng        i    g      \@g     ps@g®Gáz®ç?(   R   R   R   R+   R   t   unit_cell_volumeR   t   zt
   n_residuest   n_basest
   mw_residuet   mw_baseR   t   vm_propt   syst   stdoutt   outt   verboset   vm_prop_tablet   guesstimatet
   best_guess(   R   t   crystal_symmetryR6   R7   R=   R>   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyR   `   s*    								
c         C   sO   d  } |  j |  j |  j |  j |  j | } | d k rK |  j | } n  | S(   Ng        (   R   R6   R8   R7   R9   R5   R4   (   R   t   copiest   resultt   den(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyR   z   s
    )c         C   s   d |  j  d | S(   Ng      ð?gÝ$�•Cã?(   R   (   R   R   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   solc‚   s    c         C   s   t  d t t | d ƒ d ƒ ƒ S(   NR   g      ð?i    (   R   t   maxt   min(   R   R   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyR   …   s    c   	      C   sL  d } d } d } xs | d k r‡ | d 7} |  j  | ƒ } |  j | ƒ } |  j | ƒ } | | | | g } | | 7} |  j j | ƒ q W|  j j ƒ  } | | d 8} t | ƒ d k r|  j d IJ|  j d |  j d IJ|  j d _ |  j	 d _	 |  j
 ƒ  n  x> t t | ƒ d ƒ D]& } |  j | d | |  j | d <qWd  S(	   Ng      ð?g        i    i   i   s#   Too many residues to fit in the ASUs1     resetting numer of residues in monomer to %5.0fg      $@(   R   RF   R   R:   R,   t   popt   intR=   R6   R7   R?   t   range(	   R   RF   t   n_copiest   tot_pR   t   p_vmt   entryt   tmpt   ii(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyR?   ˆ   s,    


c         C   se   d } d } xR t  t |  j ƒ ƒ D]; } |  j | d | k r" |  j | d } | d } q" q" W| S(   Ng        i   i   (   RK   t   lenR:   (   R   RG   t   guessRQ   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyR@       s    N(
   R   R    R!   R   R   R   RF   R   R?   R@   (    (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyR3   ^   s   				t   matthews_ruppc           B   s&   e  Z d  Z d d d „ Z d „  Z RS(   s7   Probabilistic estimation of number of copies in the asuc   
      C   sñ  d d l  m } | |  _ | |  _ t } | d  k rU | d  k rU d } d } t } n  t | | | ƒ } | r” d |  _ t	 | j
 ƒ |  _ d |  _ n* t	 | j
 ƒ |  _ | j |  _ | j |  _ | j | j
 d d |  _ | j d d d d d	 d
 d g d d d d d g d d	 d
 g d d d g d d g g ƒ |  _ d |  _ x« t t | j ƒ ƒ D]” } |  j d 7_ g  } xf t t | j | ƒ ƒ D]K }	 |	 d k r½| j t	 | j | |	 ƒ ƒ qŠ| j | j | |	 ƒ qŠW|  j j | ƒ qUWd  S(   Niÿÿÿÿ(   t
   data_plotsi   i    t   titles   Solvent content analysist   column_labelst   Copiess   Solvent contents   Matthews coeff.s   P(solvent content)t   column_formatss   %ds   %.3fs   %.2ft   graph_namest   graph_columnsi   (   t   iotbxRU   t   n_residues_int
   n_bases_int   FalseR   R*   R3   RL   RJ   RA   R6   R7   R:   t   solvent_contentt
   table_datat   tablet   n_possible_contentsRK   RR   R,   t   add_row(
   R   RB   R6   R7   RU   t   best_guess_is_n_residuest   vm_estimatorRQ   t   rowt   jj(    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyR   «   sB    								  !c            sŠ  ‡  f d †  } ˆ  j  d ƒ |  j d  k r^ |  j d  k r^ ˆ  j ƒ  ˆ  j d ƒ ˆ  j ƒ  nŠ g  } |  j d  k	 r™ |  j d k r™ | j d |  j ƒ n  |  j d  k	 rÎ |  j d k rÎ | j d |  j ƒ n  ˆ  j d d j | ƒ ƒ |  j d  k r$|  j d  k r$ˆ  j d	 |  j ƒ | ƒ  nb ˆ  j	 |  j
 d
 t ƒ|  j d k rYˆ  j d ƒ n ˆ  j d |  j ƒ |  j d k r†| ƒ  n  d  S(   Nc              s   ˆ  j  d ƒ d  S(   NsÖ  
 Caution: this estimate is based on the distribution of solvent content across
 structures in the PDB, but it does not take into account the resolution of
 the data (which is strongly correlated with solvent content) or the physical
 properties of the model (such as oligomerization state, et cetera).  If you
 encounter problems with molecular replacement and/or refinement, you may need
 to consider the possibility that the ASU contents are different than expected.
(   t	   show_text(    (   R=   (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   show_warningÒ   s    s(   Solvent content and Matthews coefficients9    Number of residues unknown, assuming 50% solvent contenti    s   %d protein residuess   %d nucleic acid basess'    Crystallized molecule(s) defined as %ss    and s&     Best guess : %4d residues in the ASUt   equal_widthsi   s    Best guess : 1 copy in the ASUs#    Best guess : %4d copies in the ASU(   t   show_headerR]   R   R^   t   newlineRi   R,   t   joinR6   t
   show_tableRb   R_   RL   Rc   (   R   R=   Rj   t   clauses(    (   R=   sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt
   _show_implÑ   s.    	
		
N(   R   R    R!   R   R   Rq   (    (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyRT   ©   s   #c          C   sÑ   d d l  m }  d d l m } d d l m } |  j d d d d ƒ } | ƒ  } t d | d d ƒ } | j | ƒ d | j	 ƒ  k s‹ t
 ‚ | | j d d d ƒs© t
 ‚ t d | ƒ } | j d k sÍ t
 ‚ d  S(   Niÿÿÿÿ(   R   (   t   approx_equal(   t   StringIOR   g33333SS@gfffffæZ@gš™™™™U@iZ   gÍÌÌÌÌŒW@t   space_group_symbolt   P21RB   R6   i™   s   8 copies in the ASUgºI+‡à?t   epsg-Cëâ6?i  (   g33333SS@gfffffæZ@gš™™™™U@iZ   gÍÌÌÌÌŒW@iZ   (   t   cctbxR   t   libtbx.test_utilsRr   t	   cStringIORs   t   symmetryRT   t   showt   getvalueR+   R`   R6   (   R   Rr   Rs   t   symmR=   RD   (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   exercise÷   s    				t   __main__t   OK(   t
   __future__R    t   mmtbxR   R   R;   R   R   R
   R   t   objectR   R"   R2   R3   t   xtriage_analysisRT   R~   R   (    (    (    sD   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/matthews.pyt   <module>   s   				KN	