ó
 ™xTc           @   sª  d  d l  m Z d  d l Z d  d l Z d  d l m Z m Z d  d l m Z d  d l	 Z
 d  d l m Z d  d l m Z d  d l m Z e d  e Z i d	 d
 d d d d d d d d d d d d d d g d 6d d g d 6d g d 6d g d  6Z i e d d! 6e d d" 6e d d# 6e d  d$ 6Z d% d% d& d' d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; d< d= g Z d> d? d@ d% dA d% dB d% dC d% dD d% dE d% dF d% dG d% dH d% dI d% dJ d% dK d% g Z dL „  Z dM „  Z dN e f dO „  ƒ  YZ e ƒ  a dP e f dQ „  ƒ  YZ dR „  Z dS „  Z dT „  Z e  dU „ Z! e  e  dV „ Z" e  dW „ Z# e$ e$ e$ e% e% dX e  e$ e  dY „	 Z& dZ „  Z' e( d[ k r¦e' e j) d\ ƒ n  d S(]   iÿÿÿÿ(   t   divisionN(   t   letterst   digits(   t   Sorry(   t   dihedral_angle(   t   cdl_database(   t   bond_angle_registryi   t   ALAt   ARGt   ASNt   ASPt   CYSt   GLNt   GLUt   HISt   LEUt   LYSt   METt   PHEt   SERt   THRt   TRPt   TYRt   NonPGIV_nonxprot   ILEt   VALt   IleVal_nonxprot   GLYt   Gly_nonxprot   PROt   Pro_nonxprot   NonPGIV_xprot   IleVal_xprot   Gly_xprot   Pro_xprot    t   mCNAt   sCNAt   mNABt   sNABt   mNACt   sNACt   mBACt   sBACt   mACOt   sACOt   mACNt   sACNt   mOCNt   sOCNt   mCNt   sCNt   mNAt   sNAt   mABt   sABt   mACt   sACt   mCOt   sCOs   statistical typet   numbers   C(-1) - N(0)  - Ca(0)s   N(0)  - Ca(0) - Cb(0)s   N(0)  - Ca(0) - C(0)s   Cb(0) - Ca(0) - C(0)s   Ca(0) - C(0)  - O(0)s   Ca(0) - C(0)  - N(+1)s   O(0)  - C(0)  - N(+1)s   C(-1) - N(0)s   N(0)  - Ca(0)s   Ca(0) - Cb(0)s   Ca(0) - C(0)s   C(0)  - O(0)c         C   sA   d } x4 t  d ƒ D]& } | |  j | | j | d 7} q W| S(   Ni    i   i   (   t   ranget   xyz(   t   at   bt   d2t   i(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt	   distance2g   s    $c      	   C   sœ   d } x� t  |  ƒ D]� \ } } | d d k r | d k rD d } n | d k  rY d } n d } | | t | t | d |  | |  | d f 7} q q W| S(	   NR#   i   i    s     %s, %s : %s %s
i   s     %-25s%s: %9.2f %9.2f
s     %-25s%s:   %9.4f %9.4f
i   (   t	   enumeratet   headers(   t   restraints_valuest   outlRB   t   itemt   s(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   restraints_showm   s    		t   RestraintsRegistryc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   i  |  _  d  S(   N(   t   n(   t   self(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   __init__   s    c         C   sz   | |  k rc |  | | k rv |  j  j | d ƒ |  j  | c d 7<t j |  | |  | | ƒ qv n t j |  | | ƒ d  S(   Ni   (   RL   t
   setdefaultt   dictt   __setitem__(   RM   t   keyRH   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyRQ   ‚   s    !(   t   __name__t
   __module__RN   RQ   (    (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyRK   ~   s   	t   ThreeProteinResiduesc           B   sŒ   e  Z d  „  Z d „  Z d „  Z d „  Z d e d „ Z e d „ Z d „  Z	 d „  Z
 e e d	 „ Z e e d
 e e d „ Z e d „ Z RS(   c         C   s:   | |  _  | d  k r! d  |  _ n | j |  _ g  |  _ d  S(   N(   t   geometryt   Nonet   bond_params_tablet   errors(   RM   RV   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyRN   Ž   s
    	c         C   s
   |  j  ƒ  S(   N(   t   show(   RM   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   __repr__˜   s    c         C   sO   d } x( |  D]  } | d | j  | j f 7} q W| d |  j d t ƒ 7} | S(   NRU   s    %s(%s)s    %st   return_value(   t   resnamet   resseqt
   are_linkedt   True(   RM   RG   t   residue(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyRZ   ›   s
    c         C   sP   d } xC |  D]; } | d 7} x( | j  ƒ  D] } | d | j ƒ  7} q* Wq W| S(   NRU   s   
REMARKs   
%s(   t   atomst   format_atom_record(   RM   RG   Ra   t   atom(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   show_detailed¢   s    
g     €F@c         C   s  t  } xÝ t |  ƒ D]Ï \ } } | d k r1 q n  t | ƒ \ } } t |  | d ƒ \ } }	 | d }
 | d } | d } | d } |
 | | | g } t d g  | D] } | j ^ q¤ d t ƒ } d t | ƒ | k r t } Pq q W| r
| r
d G| G| GH|  GHq
n  | S(   Ni    i   i   t   sitest   degg     €f@s   cis peptide bond(   t   FalseRD   t
   get_c_ca_nR   R>   R`   t   abs(   RM   t   limitt   verboset   cis_peptide_bondRB   Ra   t   ccn1t   outl1t   ccn2t   outl2t   ca1RL   t   ct   ca2t   omega_atomsRd   t   omega(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt	   cis_groupª   s(     



+c         C   sh  xWt  |  ƒ D]E\ } } | d k r+ q n  t | ƒ \ } } t |  | d ƒ \ } } | d  k rš x0 | D]( } | |  j k rj |  j j | ƒ qj qj WPn  | d  k rÝ x0 | D]( } | |  j k r­ |  j j | ƒ q­ q­ WPn  | d }	 | d }
 |  j d  k r-t |	 |
 ƒ } | d k  r$t } qHt } n |  j j	 |
 j
 |	 j
 ƒ } | s Pq q Wt S| rd| St S(   Ni    i   i   i   (   RD   Ri   RW   RY   t   appendRX   RC   R`   Rh   t   lookupt   i_seq(   RM   R\   RB   Ra   Rn   Ro   Rp   Rq   t   lineRL   Rs   RA   t   bond(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyR_   ¿   s:     

 		 c         C   sy   t  j |  | ƒ x t |  ƒ d k r/ |  d =q Wt |  ƒ d k ru x0 |  j ƒ  sq |  d =t |  ƒ d k rE PqE qE Wn  d  S(   Ni   i    i   (   t   listRx   t   lenR_   (   RM   Ra   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyRx   ß   s     c         C   s  i  } xS d g D]H } x? |  d j  ƒ  D]- } | j j ƒ  | k r' | | d | <Pq' q' Wq Wx_ d d d d d g D]H } x? |  d j  ƒ  D]- } | j j ƒ  | k r‰ | | d	 | <Pq‰ q‰ Wqr WxS d g D]H } x? |  d
 j  ƒ  D]- } | j j ƒ  | k rß | | d | <Pqß qß WqÈ W| S(   Nt   Ci    s
   %s_minus_1t   Nt   CAt   CBt   Oi   s   %s_ii   s	   %s_plus_1(   Rb   t   namet   strip(   RM   Rb   R„   Rd   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt
   get_i_seqsè   s"    c         C   sf  t  |  d ƒ \ } } t  |  d ƒ \ } } t  |  d ƒ \ } } t | ƒ d k sZ t ‚ t | ƒ d k sr t ‚ t | ƒ d k sŠ t ‚ | d | d | d | d g } t d g  | D] } | j ^ q¹ d t ƒ }	 | d | d | d | d g }
 t d g  |
 D] } | j ^ qd t ƒ } | r:d G| G|	 GHn  | rJ|	 | f St |	 ƒ t | ƒ f } | S(   Ni    i   i   i   Rf   Rg   s   psi, phi(   Ri   R~   t   AssertionErrorR   R>   R`   t   round_to_ten(   RM   t   exactRl   t   backbone_i_minus_1t   junkt
   backbone_it   backbone_i_plus_1t	   phi_atomsRd   t   phit	   psi_atomst   psiRR   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   get_cdl_key  s,    ++ 
g      ð?c      
   C   s�  | s. | d d k r. | GHd s' t  ‚ d  Sn  |  j ƒ  } xNt | ƒ D]@\ }	 }
 |	 d k  re qG n  t |	 d d k r qG n  t |	 d } g  } | d k r± d d d	 g } n;| d
 k rÏ d d	 d g } n| d k rí d d	 d g } nÿ | d k rd d	 d g } ná | d k r)d	 d d g } nÃ | d k rGd	 d d g } n¥ | d k red d d g } n‡ | d k r€d d g } nl | d k r›d d	 g } nQ | d k r¶d	 d g } n6 | d k rÑd	 d g } n | d k rìd d g } n  d | k r
d | k r
qG n  d | k r(d | k r(qG n  x/ t t | ƒ ƒ D] } | | | j | | <q;Wt | ƒ d k r³t j | ƒ } | j	 ƒ  | j
 t | ƒ d  ƒ } | d  k rÄ| j
 t | ƒ d  ƒ } n  | d  k rd } x, | D]$ } | d | | | j ƒ  f 7} qÝWt d | ƒ ‚ n  | rVd | j | j | j | |	 d | |	 d d f GHn  | j ƒ  | |	 t t | ƒ <| rŠ| |	 | _ n  | r‡| d | |	 d d | _ q‡qG t | ƒ d k r{|  j j | Œ  } | sãt  ‚ | rd | | j | j | |	 d | |	 d d f GHn  | j ƒ  | |	 t t | ƒ <| rR| |	 | _ n  | r‡| d | |	 d d | _ q‡qG d sG t  ‚ qG Wd  S(   Ni    t   Ii   RI   i   t   CNAt	   C_minus_1t   N_it   CA_it   NABt   CB_it   NACt   C_it   BACt   ACOt   O_it   ACNt   N_plus_1t   OCNt   CNt   NAt   ABt   ACt   COi   R#   s   
    %-10s %ssl   CDL angle to be changed not set in model.
  Possible problems:
    Residue on special positions.

  Check:%ss7    i_seqs %-15s initial %12.3f %12.3f final %12.3f %12.3f(   R‡   R†   RD   t   columnsR=   R~   Rz   t   copyt   deepcopyt   reverset   gett   tupleRW   t   quoteR   t   i_seqst   angle_idealt   weightt   sortt   registryRX   Ry   t   distance_ideal(   RM   t   restraint_valuest   cdl_proxiest   idealt   esdt
   esd_factort   averageRl   Rb   RB   t   valuet   codet   namest   jt   rnamest   angle_proxyRG   RR   R|   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   apply_updates  sª    	                
"
  #
  #c         C   sP  | r | GH| j  GHn  x3| j  D](} t | ƒ d k re |  j j | Œ  } | | | j  | | _ q  t | ƒ d k r  t t j | ƒ ƒ } | j ƒ  t	 | ƒ } x£ |  j
 j D]h } t t j | j ƒ ƒ } | j ƒ  t	 | ƒ } | | k sû | | k r¯ | | | j  | | _ Pq¯ q¯ W| G| GH| | GH| j  | GHd sHt ‚ q  q  Wd  S(   Ni   i   i    (   RL   R~   RX   Ry   R³   R}   R¨   R©   Rª   R¬   RV   t   angle_proxiesR®   R±   R¯   R‡   (   RM   t   averagesRl   RR   R|   t   rkeyt   anglet   akey(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   apply_average_updatess  s,    

		(   RS   RT   RN   R[   RZ   Re   Rh   Rw   R_   Rx   R†   R’   R`   RÀ   RÆ   (    (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyRU   �   s   	
			 			Mc         C   sD   | d k r t  } n t } x" | D] } |  | | k r" | Sq" Wd  S(   NR   (   t   before_pro_groupst   not_before_pro_groupsRW   (   t   resname1t   resname2Ry   RR   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   get_res_type_groupŽ  s    	c         C   sž   g  } g  } x… d d d g D]t } xk |  j  ƒ  D], } | j j ƒ  | k r/ | j | ƒ Pq/ q/ Wx' |  j  ƒ  D] } | j | j ƒ  ƒ ql Wd  } Pq W| | f S(   NR   R�   R€   (   Rb   R„   R…   Rx   Rc   RW   (   t
   atom_groupt   tmpRG   R„   Rd   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyRi   ˜  s    c         C   s4   t  t t |  ƒ d ƒ ƒ d } | d k r0 d S| S(   Ni
   i´   iLÿÿÿ(   t   intt   roundt   float(   t   dt   t(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyRˆ   §  s      c   
      C   sí   d d l  m } t |  d j |  d j ƒ } | d  k r= d  S| rÏ d d g } |  j d | ƒ } x… t d d ƒ D]W } | j | | d | d d	 | ƒ} | j | Œ  } | j	 | | ƒ }	 | j
 |	 ƒ qq Wn |  j ƒ  } t | | } | S(
   Niÿÿÿÿ(   t   utilsi   i   t   2R‰   i   i    t   column(   t$   mmtbx.conformation_dependent_libraryRÓ   RË   R]   RW   R’   R=   t   get_grid_valuest	   get_indext   interpolate_2dRx   R   (
   t   threest   interpolateRÓ   t   res_type_groupR´   RR   RB   t   gridt   indext   r(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   get_restraint_values¬  s"    
 #c   
      c   s·  t  j j } t | ƒ } x˜|  j ƒ  D]Š} | r@ d | j GHn  xl| j ƒ  D]^} | rh d | j GHn  x4| j ƒ  D]&} | r� d | j GHn  x | r£ | d =q“ Wxô | j	 ƒ  D]æ }	 | rê |	 j
 d
 k rê d |	 j
 |	 j ƒ  f GHqê n  | rd | |	 j
 ƒ GHn  | |	 j
 ƒ d k r q± n  | rUt j | |	 ƒ t | ƒ d	 k rb| d =qbn | j |	 ƒ t | ƒ d	 k rzq± n  t | ƒ d	 k s’t ‚ | Vq± Wqu Wt | ƒ } qM Wq% Wd  S(   Ns   model: "%s"s   chain: "%s"s     conformer: altloc="%s"i    t   HOHs$       residue: resname="%s" resid="%s"s   residue class : %st   common_amino_acidi   (   Rá   (   Râ   (   t   iotbxt   pdbt   common_residue_names_get_classRU   t   modelst   idt   chainst
   conformerst   altloct   residuesR]   t   residR}   Rx   R~   R‡   (
   t	   hierarchyRV   t   include_non_linkedRl   t	   get_classRÚ   t   modelt   chaint	   conformerRa   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   generate_protein_threesÂ  sB       	   c         C   s.   t  ƒ  } x |  j D] } | | | j <q W| S(   N(   R   RÁ   R®   (   RV   Rl   t   ba_registryRÄ   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   setup_restraintsæ  s    	g      ð?c      
   C   sº  t  ƒ  a | r+ | s t ‚ | j ƒ  } n  | rj |  j ƒ  } x* t | ƒ D] \ } } | | | _ qJ Wn  d  } d } d } x³ t |  | ƒ D]¢ } | j	 ƒ  rÁ |
 rŒ d rŒ d d GH| GHqŒ qŒ n  t
 | d | ƒ} | d  k rå qŒ n  | d d k r| d 7} n
 | d 7} | j | | d | d | d	 | ƒqŒ Wt j rK| j t ƒ n  | j ƒ  |
 r¶| r¶| j r¶|	 r€|	 j d
 ƒ n  x3 | j D]% } |	 rª|	 j d | ƒ qŠ| GHqŠWn  | S(   Ni    s   cis i   RÛ   R“   i   R¶   R·   R¸   s7     Residues not completely updated with CDL restraints

s   %s
(   RK   R²   R‡   t
   sites_cartRb   RD   R>   RW   Ró   Rw   Rà   RÀ   RL   RÆ   t   reset_internalsRY   t   write(   Rí   RV   t   current_geometryRö   Rµ   R¶   R·   R¸   RÛ   t   logRl   t	   pdb_atomst   j_seqRd   RÚ   t   average_updatest   total_updatesR´   R{   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   update_restraintsî  sP    			 
		 
c         C   sŽ   t  rR x: t d d ƒ D]) } | Gt | ƒ Gt | t | ƒ ƒ GHq Wd sR t ‚ n  t j j |  ƒ } | j ƒ  } | j	 ƒ  j
 ƒ  t | ƒ d  S(   NiDÿÿÿi¼   i    (   Rh   R=   Rˆ   Rj   R‡   Rã   Rä   t   inputt   construct_hierarchyRb   t   reset_serialRÿ   (   t   filenameRB   t   pdb_inpRí   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   runD  s    't   __main__i   (*   t
   __future__R    t   sysR¨   t   stringR   R   t   libtbx.utilsR   t	   iotbx.pdbRã   t   scitbx.mathR   t1   mmtbx.conformation_dependent_library.cdl_databaseR   t8   mmtbx.conformation_dependent_library.bond_angle_registryR   t   chararcters_36RÈ   RÇ   R§   RE   RC   RJ   RP   RK   R²   R}   RU   RË   Ri   Rˆ   Rh   Rà   Ró   Rõ   RW   R`   Rÿ   R  RS   t   argv(    (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/conformation_dependent_library/__init__.pyt   <module>   sà   


					ÿ 	
		"	L	
