ó
­™xTc           @   s  d  d l  m Z d  d l Z d  d l Z d  d l m Z d  d l m	 Z	 d  d l m
 Z
 d  d l Z d  d l Z e j j d ƒ r‹ d Z n d Z e j j d	 e ƒ Z d d
 „ Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e j d „ Z d e j d d „ Z d S(   iÿÿÿÿ(   t   divisionN(   t   make_header(   t   Sorry(   t   adopt_init_argst   rosetta_adaptbxs�      rama_potential = *oldfield emsley rosetta
    .type = choice(multi=False)
    .short_caption = Ramachandran potential
    .caption = Oldfield Coot Rosettas�      rama_potential = *oldfield emsley
    .type = choice(multi=False)
    .short_caption = Ramachandran potential
    .caption = Oldfield Cootsz  
  rama_weight = 1.0
    .type = float
    .short_caption = Ramachandran gradients weight
    .expert_level = 1
  scale_allowed = 1.0
    .type = float
    .short_caption = Rescale allowed region pseudo-energy by
  %s
  oldfield
    .short_caption = Oldfield potential parameters
    .style = box auto_align
  {
    esd = 10.0
      .type = float
      .expert_level = 2
      .short_caption = E.S.D.
    weight_scale = 1.0
      .type = float
      .expert_level = 2
    dist_weight_max = 10.0
      .type = float
      .expert_level = 2
    weight = None
      .type = float
      .expert_level = 2
  }
  rama_selection = None
    .type = atom_selection
    .short_caption = Atom selection for Ramachandran restraints
    .expert_level = 1
  rama_exclude_sec_str = False
    .type = bool
    .expert_level = 1
    .short_caption = Exclude secondary structure from Ramachandran restraints
c         C   s  d d  l  } |  d  k r- t j ƒ  j ƒ  }  n  |  j d k rV t d d |  j ƒ ‚ n  | j j d ƒ } d d l	 m
 } d d l m } i  } xê d d	 d
 d g D]Ö } t j j d d | d t j j ƒ } t | d ƒ } | j ƒ  }	 xn | j ƒ  D]` }
 |
 j ƒ  \ } } } t | ƒ d d k r;t | ƒ d d k sAt ‚ |	 j t | ƒ ƒ qô W| |	 d |  j ƒ } | | | <q¡ W| S(   Niÿÿÿÿg        s7   Ramachandran restraint parameter scale_allowed must be s&   a positive number (current value: %g).t!   mmtbx_ramachandran_restraints_ext(   t   lookup_table(   t   flext   alat   glyt   preprot   prot   relative_paths-   chem_data/rotarama_data/%s.rama.combined.datat   testt   ri   i   i´   (   t   boost.pythont   Nonet   master_philt   fetcht   extractt   scale_allowedR   t   pythont
   import_extR   R   t   scitbx.array_familyR   t   libtbxt   envt   find_in_repositoriest   ost   patht   isfilet   opent   doublet	   readlinest   splitt   intt   AssertionErrort   appendt   float(   t   paramst   boostt   extR   R   t   tablest   residue_typet	   file_namet   ft   datat   linet   valt   phit   psit   t(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyt   load_tables=   s0    2c   
      C   s¦   d d l  m } | j d |  d | ƒ } | j ƒ  } t | j ƒ  ƒ } | d IJxO | D]G \ } } | j ƒ  | j ƒ  | d }	 | d | |	 | f IJ|	 } qW Wd  S(   Niÿÿÿÿ(   R   R-   t   n_slotss*         Map Values          Number of pointsi   s   %10.3f - %-10.3f : %d(   R   R   t	   histogramt   data_mint	   enumeratet   slotst
   slot_width(
   R-   R4   t   logR   t   hmt   lc_1t   s_1t   i_1t   n_1t   hc_1(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyt   show_histogramW   s    
t   ramachandran_plot_datac           B   sV   e  Z d  „  Z d d „ Z d d „ Z d „  Z d „  Z d „  Z d „  Z d	 „  Z	 RS(
   c         C   s€  d d l  m } d  |  _ d  |  _ d  |  _ d  |  _ d d g d d g d d g d	 d
 g g } x»| D]³} | \ } } t j j	 d d | d t
 j j ƒ } | d k rº | j ƒ  |  _ n  | d k rØ | j ƒ  |  _ n  | d k rö | j ƒ  |  _ n  | d	 k r| j ƒ  |  _ n  t | d ƒ } xò | j ƒ  D]ä } | j ƒ  } t | ƒ d k r0t | d ƒ t | d ƒ t | d ƒ }	 }
 } |	 |
 | g } | d k r´|  j j | ƒ n  | d k rÓ|  j j | ƒ n  | d k rò|  j j | ƒ n  | d	 k r|  j j | ƒ qq0q0Wqe W|  j d |  j ƒ |  _ |  j d |  j ƒ |  _ |  j d |  j ƒ |  _ |  j d |  j ƒ |  _ d  S(   Niÿÿÿÿ(   R   R	   s   rama500-gly-sym.dataR   s   rama500-pro.dataR
   s   rama500-prepro.datat   generals   rama500-general.dataR   s   chem_data/rotarama_data/%sR   R   i   i    i   i   R-   (   R   R   R   R	   R   R
   RC   R   R   R   R   R   R   t   vec3_doubleR   R    R!   t   lenR%   R$   t   normalize_glyt   normalize_prot   normalize_preprot   normalize_general(   t   selfR   t   filest   rtft   rtR,   R+   t   foR.   t   phi_t   psi_R/   t   triplet(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyt   __init__c   sP    							
    2    g      à?c         C   sI   d d l  m } | j | j | ƒ ƒ } | | | k } | j | | @ƒ S(   Niÿÿÿÿ(   R   (   R   R   t   maxt   select(   RJ   R-   R/   t   selt	   thresholdR   t   vmaxt   sel1(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyt   norm_to_max‡   s    i   c         C   s8   d d l  m } | j | j t d | j ƒ  | ƒ ƒ ƒ S(   Niÿÿÿÿ(   R   i    (   R   R   RT   t   size_tt   ranget   size(   RJ   t   xt   stepR   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyt	   thin_data�   s    c   	      C   s‚   d d l  m } | j ƒ  } | j ƒ  } | j ƒ  } x> | D]6 \ } } } | j | ƒ | j | ƒ | j | ƒ q; W| | | f S(   Niÿÿÿÿ(   R   (   R   R   R   R$   (	   RJ   R-   R   R0   R1   R/   R]   t   yt   z(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyt   split_array‘   s    c      	   C   s˜  |  j  d | ƒ \ } } } | d k | d k  @| d k  @| d k @} | d k | d k  @| d k @| d k  @} | d k  | d k @| d k  @| d k @} | d k  | d k @| d k @| d	 k  @} | d k  | d k @| d	 k @| d k  @}	 | d k  | d k @| d k  @| d k @}
 | d k | d k  @| d k @| d k  @} |  j d | d
 | d | d d ƒ } |  j d | d
 | d | d d ƒ } |  j d | d
 | d | d d ƒ } |  j d | d
 | d | d d ƒ } |  j d | d
 | d |	 d d ƒ } |  j d | d
 | d |
 d d ƒ } |  j d | d
 | d | d d ƒ } | j | ƒ | j | ƒ | j | ƒ | j | ƒ | j | ƒ | j | ƒ |  j | ƒ S(   NR-   i    i´   iûÿÿÿiLÿÿÿiA   iœÿÿÿi¿ÿÿÿi2   R/   RU   RV   gffffffæ?g      à?(   Rb   RY   t   extendR_   (   RJ   R-   R0   R1   R/   t   s0t   s1t   s2t   s3t   s4t   s5t   s6t   d0t   d1t   d2t   d3t   d4t   d5t   d6(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyRI   œ   s,    *******$$$$$$$c         C   s  |  j  d | ƒ \ } } } | d k  | d k @| d k  @| d k @} | d k  | d k @| d k @| d k  @} | d k | d k  @| d k @| d k  @} | d k  | d k @| d	 k  @| d k @} | d
 k | d k  @| d k @| d k  @}	 | d k | d
 k  @| d
 k @| d k  @}
 |  j d | d | d | ƒ } |  j d | d | d | ƒ } |  j d | d | d | ƒ } |  j d | d | d | ƒ } |  j d | d | d |
 ƒ } |  j d | d | d |	 ƒ } | j | ƒ | j | ƒ | j | ƒ | j | ƒ | j | ƒ |  j | ƒ S(   NR-   i    iLÿÿÿi
   i’ÿÿÿi´   iZ   in   ijÿÿÿix   R/   RU   (   Rb   RY   Rc   R_   (   RJ   R-   R0   R1   R/   Re   Rf   Rg   Rj   Ri   Rh   Rl   Rm   Rn   Rq   Ro   Rp   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyRH   ´   s&    ******c         C   so  |  j  d | ƒ \ } } } | d k  | d k @| d k  @| d k @} | d k  | d k @| d k @| d k  @} | d k  | d k @| d k @| d k  @} | d k  | d k @| d k @| d k  @} |  j d | d	 | d
 | ƒ }	 |  j d | d	 | d
 | ƒ }
 |  j d | d	 | d
 | ƒ } |  j d | d	 | d
 | ƒ } |	 j |
 ƒ |	 j | ƒ |	 j | ƒ |  j |	 ƒ S(   NR-   i    i~ÿÿÿiœÿÿÿiLÿÿÿi   iZ   i´   R/   RU   (   Rb   RY   Rc   R_   (   RJ   R-   R0   R1   R/   Re   Rf   Rg   Rh   Rl   Rm   Rn   Ro   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyRG   É   s    ****c         C   s  |  j  d | ƒ \ } } } | d k  | d k @| d k  @| d k @} | d k | d k  @| d k  @| d k @} | d k  | d k @| d k @| d k  @} | d k | d k  @| d k @| d k  @} | d k  | d k @| d k @| d k  @}	 | d k | d k  @| d k @| d k  @}
 |  j d | d | d | ƒ } |  j d | d | d | ƒ } |  j d | d | d | ƒ } |  j d | d | d | ƒ } |  j d | d | d |	 ƒ } |  j d | d | d |
 ƒ } | j | ƒ | j | ƒ | j | ƒ | j | ƒ | j | ƒ |  j | ƒ S(	   NR-   i    iLÿÿÿi¦ÿÿÿi´   iF   R/   RU   (   Rb   RY   Rc   R_   (   RJ   R-   R0   R1   R/   Re   Rf   Rg   Rh   Ri   Rj   Rl   Rm   Rn   Ro   Rp   Rq   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyRF   Ø   s&    ******(
   t   __name__t
   __module__RR   RY   R_   Rb   RI   RH   RG   RF   (    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyRB   b   s   	$				t   lookup_managerc           B   s#   e  Z d  „  Z d d d d „ Z RS(   c         C   s�   t  |  t ƒ  ƒ |  j j d k r1 t ƒ  |  _ nL |  j j d k rn d d  l } | j ƒ  | j j	 ƒ  |  _ n t
 | ƒ |  _ d  S(   Nt   oldfieldt   rosettaiÿÿÿÿ(   R   t   localsR&   t   rama_potentialRB   R)   R   t   initR(   t   ramachandranR3   (   RJ   R&   R   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyRR   î   s    
c         C   sÙ  d d l  m } | d  k	 s" t ‚ d d  l } | j j d ƒ } d d l m }	 m	 }
 | d  k r} | j
 | j ƒ  d ƒ } n  | d  k r˜ | j ƒ  } n  | j ƒ  d } |  j j d k r|  j j } x| D].} | j d k rñ |  j j } n  | j d	 k r|  j j } n  | j d
 k r-|  j j } n  | j d k rK|  j j } n  |
 d | d | d | ƒ } | j d  k r¤d | j d t | d | j ƒ | j } n	 | j } |	 d | d | d d | d d | d | d | d | ƒ } | j | j ƒ  ƒ qÍ WnÊ |  j j d k r\xµ | D]: } | j |  j j d | d | d | d |  j j ƒ ƒ qWnp |  j j d k stt ‚ xU | D]M } |  j | j } | j | j  d | d | d | d |  j j d d ƒ ƒ q{W| j! | ƒ S(   Niÿÿÿÿ(   R   R   (   t   rama_target_and_gradientst   target_phi_psig        i    Ru   R	   R   R
   R   t
   rama_tablet
   sites_cartt   proxyg      ð?i   t   gradient_arrayt
   phi_targett
   psi_targeti   t   weightRv   t   epsilongü©ñÒMbP?(   g        g        g        ("   R   R   R   R#   R   R   R   R   R{   R|   RD   R\   R   t   clearR&   Rx   Ru   R*   R)   R	   R   R
   RC   Rƒ   t   esdt   mint   dist_weight_maxt   weight_scaleR$   t   targett   residue_target_and_gradientst   rama_weightt   compute_gradientst   sum(   RJ   R~   t   proxiesR€   t	   unit_cellt   residuals_arrayR   R'   R(   R{   R|   RŠ   t   opR   R}   R   Rƒ   t   tg(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyt   restraints_residual_sumù   sn    
    	/	

	N(   Rr   Rs   RR   R   R”   (    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyRt   í   s   	c         C   s  d d  l  } d d  l } g  |  j ƒ  D] } | j ^ q% j d ƒ d k sU t d ƒ ‚ | j j d ƒ } | j j	 d |  d | ƒ } | j
 ƒ  } x] | D]U }	 |	 j }
 |
 d k r¹ d	 }
 n  | j d
 |
 d |	 j d |	 j ƒ } | j | ƒ q• W| d IJ| d | j ƒ  IJ| S(   Niÿÿÿÿi    i   s0   Probably all atoms have i_seq = 0 which is wrongR   t   pdb_hierarchyt   atom_selectiont   MSEt   METt   residue_nameR*   t   i_seqst    s'     %d Ramachandran restraints generated.(   t   mmtbx.rotamerR   t   atomst   i_seqt   countR#   R   R   t   rotamert   extract_phi_psit   shared_phi_psi_proxyR™   t   phi_psi_proxyR*   Rš   R$   R\   (   R•   R–   R:   t   mmtbxR'   t   aR(   t   anglesR�   t   angleR™   R   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyt   extract_proxies6  s*    4						
s*   refinement.pdb_interpretation.peptide_linkc         C   s+  t  d d | ƒd d l m } |  j d  k rP | j | j ƒ  j ƒ  t ƒ } nŽ | j	 ƒ  } y | j
 |  j ƒ }	 Wn2 t k
 r¦ }
 t d t |
 ƒ |  j f ƒ ‚ n8 X|	 j t ƒ d k rØ t d | |  j f ƒ ‚ n  |	 } |  j r'| j d | ƒ | j ƒ  } | j ƒ  } | | B} | | M} n  | S(	   Ns,   Extracting atoms for Ramachandran restraintst   outiÿÿÿÿ(   R   sD   Atom selection error:
  %s
Selection string resulting in error:
  %si    sJ   Empty atom selection for %s.rama_selection.
Current selection string:
  %sR:   (   R   R   R   t   rama_selectionR   t   boolR�   R\   t   Truet   atom_selection_cachet	   selectiont	   ExceptionR   t   strRŸ   t   rama_exclude_sec_strt
   initializet   alpha_selectiont   beta_selection(   R&   R•   t   secondary_structure_managert   d_minR:   t
   scope_nameR   R–   t   cachet   selet   et
   alpha_selet	   beta_selet   ss_sele(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyt   process_refinement_settingsO  s*    ! 	
(   t
   __future__R    t   libtbx.load_envR   t
   iotbx.philt   iotbxt   libtbx.str_utilsR   t   libtbx.utilsR   R   t   sysR   R   t
   has_modulet   potential_philt   philt   parseR   R   R3   RA   t   objectRB   Rt   t   stdoutR¨   R¾   (    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/geometry_restraints/ramachandran.pyt   <module>   s*   		#	‹J