ó
µ™xTc           @   sŸ   d  d l  m Z d  d l Z d  d l m Z d  d l m Z d  d l m	 Z	 d  d l
 Z
 e j j d ƒ Z e j j d ƒ Z d e f d	 „  ƒ  YZ d
 „  Z d S(   iÿÿÿÿ(   t   divisionN(   t   flex(   t   easy_pickle(   t   Sorrys\  
 reference_file = None
   .type = path
   .optional = true
   .short_caption = DEN reference model
   .style = file_type:pdb input_file
 gamma = 0.5
   .type = float
 kappa = 0.1
   .type = float
 weight = 30.0
   .type = float
 sigma = 0.44
   .type = float
 optimize = True
   .type = bool
   .short_caption = Optimize DEN parameters
   .help = If selected, will run a grid search to determine optimal values     of the weight and gamma parameters for DEN refinement.  This is very     slow, but highly recommended, and can be parallelized across multiple     CPU cores.
 opt_gamma_values = 0.0, 0.2, 0.4, 0.6, 0.8, 1.0
   .type = floats
   .short_caption = Gamma values for optimization
 opt_weight_values = 3.0, 10.0, 30.0, 100.0, 300.0
   .type = floats
   .short_caption = Weight values for optimization
 num_cycles = 10
   .type = int
   .short_caption = Number of cycles
 kappa_burn_in_cycles = 2
   .type = int
   .short_caption = Number of cycles where kappa is set to 0.0
 bulk_solvent_and_scale = True
   .type = bool
   .short_caption = Refine bulk solvent and anisou scale after     each DEN cycle
 refine_adp = True
   .type = bool
   .short_caption = Refine B-factors
 final_refinement_cycle = False
   .type = bool
 verbose = False
   .type = bool
 annealing_type = *torsion                    cartesian
   .type = choice(multi=False)
   .help = select strategy to apply DEN restraints
 minimize_c_alpha_only = False
   .type = bool
   .short_caption = Restrain only C alpha positions in pre-dynamics     coordinate minimization
 output_kinemage = False
   .type = bool
   .help = output kinemage representation of starting DEN restraints
 restraint_network
    .style = box auto_align
 {
  lower_distance_cutoff = 3.0
    .type = float
  upper_distance_cutoff = 15.0
    .type = float
  sequence_separation_low = 0
    .type = int
  sequence_separation_limit = 10
    .type = int
  exclude_hydrogens = True
    .type = bool
  ndistance_ratio = 1.0
    .type = float
  export_den_pairs = False
    .type = bool
    .expert_level = 3
  den_network_file = None
    .type = path
    .optional = True
    .expert_level = 3
 }
s  
 scale= 150.0
   .type = float
 relax_ncycle = 0
   .type = int
 post_ncycle = 0
   .type = int
 minimum_start = True
   .type = bool
 exponent = *2 4
   .type = choice(multi=False)
 atom_select = None
   .type = atom_selection
   .multiple = True
   .optional = True
t   den_restraintsc           B   sÅ   e  Z d d 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 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s'  | d  k r t j } n  | |  _ | |  _ t | j ƒ  ƒ d k rU t d d ƒ ‚ n  | d  k	 r“ t | j ƒ  ƒ d k r“ t d d d ƒ ‚ q“ n  | |  _ t	 |  j j
 ƒ  ƒ } t j g  | D] } | j ^ q¾ ƒ } | j d ƒ |  _ | d  k r'|  j d d IJ| |  _ t |  _ |  j |  _ n_ | |  _ t |  _ t	 |  j j
 ƒ  ƒ }	 t j g  |	 D] } | j ^ q[ƒ }
 |
 j d ƒ |  _ |  j r¬|  j r¬t d	 d
 ƒ ‚ n& |  j rÒ|  j rÒt d d ƒ ‚ n  d d  l } | j j d ƒ |  _ | |  _ | j |  _ | j |  _ | j |  _ | j |  _ | j |  _ | j |  _ | j  |  _  | j! j" |  _" | j! j# |  _# | j! j$ |  _$ | j! j% |  _% | j! j& |  _& | j! j' |  _' | j! j( |  _( | j! j) |  _) d  |  _* d  |  _+ d |  _, t- t. |  j d ƒ ƒ |  _/ |  j d  k r#|  j j0 ƒ  |  _ n  d  S(   Ni   s3   More than one model in input model. DEN refinement s%   is only available for a single model.s(   More than one model in reference model. s   DEN refinement s       s1   No input DEN reference model...restraining model s   to starting structures2   Reference model contains SEGIDs that do not match s   the working model.s0   Working model contains SEGIDs that do not match s   the reference model.iÿÿÿÿt   mmtbx_den_restraints_exti    i   (1   t   Nonet   syst   stdoutt   logt   den_proxiest   lent   modelsR   t   pdb_hierarchyt   listt   atoms_with_labelsR   t
   std_stringt   segidt   all_eqt   use_model_segidt   pdb_hierarchy_reft   Truet   restrain_to_starting_modelt   use_ref_segidt   Falset   boost.pythont   pythont
   import_extt   extt   paramst   kappat   kappa_burn_in_cyclest   gammat   weightt   sigmat
   num_cyclest   annealing_typet   restraint_networkt   ndistance_ratiot   lower_distance_cutofft   upper_distance_cutofft   sequence_separation_lowt   sequence_separation_limitt   exclude_hydrogenst   den_network_filet   export_den_pairst   current_cyclet   den_atom_pairst   den_pair_countt   intt   roundt   torsion_mid_pointt   shared_den_simple_proxy(   t   selfR   R   R   R	   R
   t   atom_labelst   at   segidst   ref_atom_labelst
   ref_segidst   boost(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyt   __init__k   sr     			%
				%				c         C   sF  d d l  m } |  j d | ƒ |  _ |  j d |  j ƒ |  _ | j d |  j ƒ |  _ | j d | ƒ |  _	 | j d |  j ƒ |  _
 | j d | ƒ |  _ | j d |  j ƒ |  _ |  j d |  j d |  j ƒ \ |  _ |  _ |  j ƒ  |  j d  k	 r|  j ƒ  |  _ n |  j ƒ  |  _ |  j ƒ  |  _ |  j ƒ  |  j rB|  j ƒ  n  d  S(   Niÿÿÿÿ(   t   utilsR   t
   resid_hash(   t   mmtbx.torsion_restraintsR=   t   count_atoms_per_chaint   atoms_per_chainR   t   atoms_per_chain_reft   build_resid_hasht   resid_hash_reft   build_i_seq_hasht
   i_seq_hasht   i_seq_hash_reft   build_name_hasht	   name_hasht   name_hash_reft   find_atom_pairst   ref_atom_pairst   ref_distance_hasht   remove_non_matching_pairsR,   R   t   load_den_networkR/   t   select_random_den_restraintst   random_ref_atom_pairst   get_den_atom_pairst   check_den_pair_consistencyR-   t   dump_den_network(   R5   R   R=   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyt   build_den_proxiesµ   s$    

	c         C   s&   |  j  d k r" t d d ƒ ‚ n  d  S(   Ni    s'   No DEN pairs matched to working model. s   Please check inputs.(   R0   R   (   R5   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyRS   Ó   s    c      	   C   s™  |  j  r d } n d } |  j d | IJi  } i  } i  } i  } |  j d } |  j d }	 |  j |  j }
 x!| j ƒ  d j ƒ  D]	} d  } xV | j	 ƒ  D]H } | j
 ƒ  rÁ | j ƒ  rÁ q› q› | d  k rÖ | } q› |  j d IJq› W| d  k	 r‚ g  | | j <g  | | j <x{t | j ƒ  ƒ D]d\ } } xU| j ƒ  | | |
 d !D]8} | j ƒ  | j ƒ  } | |  j k  sH| |  j k rˆqHn  xõ t | j ƒ  ƒ D]á \ } } |  j rÅ| j ƒ  rÅq›qÅn  x´ | j ƒ  D]¦ } | j | j k rðqÒn  |  j r| j ƒ  rqÒqn  t | j | j ƒ } | | k rÒ| |	 k rÒ| | j j | j | j f ƒ | d | | j | j f <qÒqÒWq›WqHWq Wq‚ q‚ W| | f S(	   Nt   startingt	   references'   Finding DEN atom pairs from %s model...i   i    s/   warning, multiple conformers found, using firsti   g      à?(   R   R	   R'   R(   R*   R)   R   t   chainsR   t
   conformerst
   is_proteint   is_nat   idt	   enumeratet   residuest   resseq_as_intt   atomsR+   t   element_is_hydrogent   i_seqt   distance_squaredt   xyzt   append(   R5   R   R>   t   reference_txtt
   atom_pairst   distance_hasht   atom_pairs_testt   distance_hash_testt   low_dist_sqt   high_dist_sqt   residue_ranget   chaint   found_conformert	   conformert   it   res1t   res2t
   separationt   jt   atom1t   atom2t   dist(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyRK   Ø   sZ    		
	"				 4c   	      C   sü   d |  _  |  j d IJi  } xÆ |  j j ƒ  D]µ } g  | | <x¢ t |  j | ƒ D]� \ } } |  j | d } |  j | d } |  j j | ƒ } |  j j | ƒ } | d  k rP | d  k rP | | j	 | ƒ |  j  d 7_  qP qP Wq, W| |  _ |  j
 ƒ  d  S(   Ni    s   Removing non-matching pairs...i   (   R0   R	   RL   t   keysR]   RJ   RF   t   getR   Re   RS   (	   R5   t   temp_atom_pairsRn   Rq   t   pairt	   ref_atom1t	   ref_atom2t   model_atom1t   model_atom2(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyRN     s    	
 	c         C   sÙ   i  } xÌ | j  ƒ  d j ƒ  D]´ } t } x: | j ƒ  D], } | j ƒ  r\ | j ƒ  r\ q6 q6 t } q6 W| r |  j r» d } x- | j ƒ  D] } | j	 ƒ  sˆ | d 7} qˆ qˆ W| | | j
 <qÑ | j ƒ  | | j
 <q q W| S(   Ni    i   (   R   RX   R   RY   RZ   R[   R   R+   R`   Ra   R\   t
   atoms_size(   R5   R   RA   Rn   Ro   Rp   t   countert   atom(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyR@     s     
	c         C   sÒ   d d l  m } |  j d IJi  } x¨ |  j j ƒ  D]— } g  | | <t |  j | ƒ } t |  j | |  j ƒ } | | k r… | } n  | j	 | t
 | ƒ ƒ } x* | D]" } | | j |  j | | ƒ q¤ Wq3 W| S(   Niÿÿÿÿ(   R   s   Selecting random DEN pairs...(   t   cctbx.array_familyR   R	   RL   Ry   R   R2   RB   R&   t   random_selectionR1   Re   (   R5   R   t   random_pairsRn   t   pair_list_sizet   num_restraintsR…   Rq   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyRP   3  s    
	$c   
      C   sµ   i  } |  j  ƒ  x„ |  j j ƒ  D]s } g  | | <x` |  j | D]Q } | d } | d } |  j | } |  j | } | | f } | | j | ƒ q> Wq  Wd }	 t j d |	 | ƒ d  S(   Ni    i   t   dens   %s.pkl(   t   get_selection_stringsR/   Ry   t   selection_string_hashRe   R   t   dump(
   R5   t   den_dumpRn   R|   t   i_seq_1t   i_seq_2t   select_1t   select_2t	   dump_pairt   output_prefix(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyRT   D  s    



c         C   sÛ   d |  _  i  } t j |  j ƒ } |  j j ƒ  } x¤ | j ƒ  D]– } g  | | <xƒ | | D]w } d | d | d f } | j d | ƒ j ƒ  } | j	 ƒ  d k r¯ t
 d ƒ ‚ n  | | j | ƒ |  j  d 7_  qX Wq= W| S(   Ni    s   (%s) or (%s)i   t   stringi   s.   input DEN network does not match current model(   R0   R   t   loadR,   R   t   atom_selection_cacheRy   t	   selectiont
   iselectiont   sizeR   Re   (   R5   R/   t   network_pairst	   sel_cacheRn   R|   R”   R˜   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyRO   U  s     	
c         C   sß   i  } t  |  j j ƒ  ƒ } t j g  | D] } | j ^ q( ƒ } x� | D]‡ } | j } | j ƒ  } | j } | j	 } | j
 }	 | j }
 d | | | | f d |
 } |	 d k rÁ | d |	 7} n  | | | j <qG W| |  _ d  S(   Ns8   name '%s' and resname '%s' and chain '%s' and resid '%s's    and segid '%s't    s    and altid '%s'(   R   R   R   R   R   R   t   chain_idt   residt   resnamet   namet   altlocRb   R‹   (   R5   R‹   R6   R7   R8   Rn   Rž   RŸ   t   atomnameR¡   R   t   selection_string(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyRŠ   h  s"    %					c         C   s´   d |  _  i  } xž |  j j ƒ  D]� } g  | | <xz |  j | D]k } |  j |  j | d } |  j |  j | d } t j | | g ƒ } | | j | ƒ |  j  d 7_  q= Wq W| S(   Ni    i   (   R0   RQ   Ry   RF   RJ   R   t   size_tRe   (   R5   R/   Rn   R|   t   i_seq_at   i_seq_bt   i_seqs(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyRR   |  s    	
c   	      C   sá   |  j  d |  j d } |  j d IJxµ |  j j ƒ  D]¤ } x› |  j | D]Œ } |  j |  j | d } |  j |  j | d } |  j | | f } t | ƒ } |  j	 j
 d | d | d | d	 | ƒ } |  j j | ƒ qI Wq5 Wd  S(
   Ng      ð?i   s   building DEN restraints...i    i   R§   t   eq_distancet   eq_distance_startR!   (   R!   R"   R	   R/   Ry   RG   RI   RM   t   tupleR   t   den_simple_proxyR
   Re   (	   R5   t
   den_weightRn   R|   R¥   R¦   t   distance_idealR§   t   proxy(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyt   build_den_restraints‰  s    	c         C   s.   g  } x! |  j  D] } | j | j ƒ q W| S(   N(   R
   Re   R¨   (   R5   t   current_eq_distancest   dp(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyt   get_current_eq_distances™  s    c         C   s1   x* t  |  j ƒ D] \ } } | | | _ q Wd  S(   N(   R]   R
   R¨   (   R5   t   eq_distancesRq   R±   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyt   import_eq_distancesŸ  s    c         C   s   |  j  j | |  j | |  j ƒ S(   N(   R   t   den_simple_residual_sumR
   R!   (   R5   t
   sites_cartt   gradient_array(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyt   target_and_gradients£  s
    	c         C   sG   |  j  |  j k r |  j } n d } |  j j | |  j |  j | ƒ d  S(   Ng        (   R.   R   R   R   t   den_update_eq_distancesR
   R    (   R5   R¶   t   kappa_local(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyt   update_eq_distances¬  s    c         C   sW   |  j  j } |  j  j } g  } x2 | D]* } x! | D] } | j | | f ƒ q2 Wq% W| S(   N(   R   t   opt_gamma_valuest   opt_weight_valuesRe   (   R5   t   gamma_arrayt   weight_arrayt   gridt   gt   w(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyt   get_optimization_grid·  s    c   
   	   C   s:  |  j  d IJ|  j  d t |  j ƒ IJ|  j  d d IJxû |  j D]ð } | j } | | d	 } | | d
 } t | | ƒ } | d } |  j | d	 j d ƒ r¸ |  j | d	 d  } n |  j | d	 } |  j | d
 j d ƒ rû |  j | d
 d  }	 n |  j | d
 }	 |  j  d | |	 | | j | j f IJqB Wd  S(   Ns   DEN restraints summary:s$   
total number of DEN restraints: %s
s   %s | %s | %s | %s | %s s       atom 1     s       atom 2     s
   model dists
     eq dist s   eq dist starti    i   g      à?s       iüÿÿÿs+   %s | %s |   %6.3f   |   %6.3f   |   %6.3f  (   s       atom 1     s       atom 2     s
   model dists
     eq dist s   eq dist start(	   R	   R   R
   R§   Rc   RI   t   endswithR¨   R©   (
   R5   R¶   R±   R§   t   a_xyzt   b_xyzt   distance_sqt   distancet   name1t   name2(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyt   show_den_summaryÁ  s8    

    	
c         C   sÏ   d d l  m } t d d ƒ } d } | d 7} | j | ƒ x‚ |  j D]w } | j } | j } | | d } | | d }	 | |	 g }
 | j d	 d
 d | d d d |	 d d  ƒ } | j | ƒ qF W| j	 ƒ  d  S(   Niÿÿÿÿ(   t
   validations   den_restraints.kinRÂ   s
   @kinemage
s/   @vectorlist {DEN} color= magenta master= {DEN}
i    i   t	   start_keyt   At	   start_xyzt   end_keyt   Bt   end_xyzt   width(
   t   mmtbx.kinemageRÌ   t   filet   writeR
   R§   R¨   t   kin_vecR   t   close(   R5   R¶   RÌ   t   ft
   vec_headerR±   R§   R¨   t   site_at   site_bt   sitest   vec(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyt   output_kinemageá  s$    
			c         C   sU   |  j  d  k	 s t ‚ t d |  j d |  j d |  j d |  j d |  j  j | | ƒ ƒ S(   NR   R   R   R	   R
   (	   R
   R   t   AssertionErrorR   R   R   R   R	   t   proxy_select(   R5   t   n_seqR—   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyt   select  s    				N(   t   __name__t
   __module__R   R<   RU   RS   RK   RN   R@   RP   RT   RO   RŠ   RR   R¯   R²   R´   R¸   R»   RÃ   RË   Rß   Rã   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyR   i   s,   E			6														
	 	#c         C   s<   |  d | d d |  d | d d |  d | d d S(   Ni    i   i   (    (   R7   t   b(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyRc     s    (   t
   __future__R    t
   iotbx.philt   iotbxR„   R   t   libtbxR   t   libtbx.utilsR   R   t   philt   parset
   den_paramst   den_params_developmentt   objectR   Rc   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/den/__init__.pyt   <module>   s   	N			ÿ ¥