ó
º™xTc           @   sj  d  d l  m Z d  d l Z d  d l m Z d  d l m Z d  d l Z	 d  d l
 m Z d  d l m Z d  d l Td  d l Z 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 m Z d  d l m Z d  d l Z d  d l Z e  Z! e j" j# d ƒ Z$ d e% f d „  ƒ  YZ& d „  Z' e( d d e) e) d „ Z* d „  Z+ d S(   iÿÿÿÿ(   t   divisionN(   t   amino_acid_codes(   t
   group_args(   t   rotalyze(   t   rotatable_bonds(   t   *(   t   flex(   t   Sorry(   t   secondary_structure(   t   rotate_point_around_axis(   t   make_sub_header(   t   utilssJ  
 file = None
   .type = path
   .short_caption = Reference model
   .style = bold file_type:pdb hidden
   .multiple = True
 use_starting_model_as_reference = False
   .type = bool
   .short_caption = use starting model as reference
 sigma = 1.0
   .type = float
 limit = 15.0
   .type = float
 hydrogens = False
   .type = bool
 main_chain = True
   .type = bool
 side_chain = True
   .type = bool
 fix_outliers = True
   .type = bool
 strict_rotamer_matching = False
   .type = bool
 auto_shutoff_for_ncs = False
   .type = bool
 SSM_alignment = True
   .type = bool
 similarity = .80
   .type = float
   .short_caption = Sequence similarity cutoff
 secondary_structure_only = False
   .type = bool
 reference_group
  .multiple=True
  .optional=True
  .short_caption=Reference group
  .style = noauto auto_align menu_item parent_submenu:reference_model
{
  reference=None
    .type=atom_selection
    .short_caption=Reference selection
  selection=None
    .type=atom_selection
    .short_caption=Restrained selection
  file_name=None
    .type=path
    .optional=True
    .short_caption = Reference model for this restraint group
    .style = bold hidden
    .help = this is to used internally to disambiguate cases where multiple             reference models contain the same chain ID. This normally does             not need to be set by the user
}
t   reference_modelc           B   s­   e  Z d d e d d d d  „ Z d „  Z d „  Z d „  Z d „  Z d d „ Z	 d „  Z
 d d d e e e d „ Z d „  Z d d	 „ Z d
 „  Z d e d „ Z d „  Z RS(   c         C   ss  | | g j  d  ƒ d k s! t ‚ | d  k r< t j |  _ n	 | |  _ | |  _ | |  _ | j ƒ  | j	 ƒ  j
 ƒ  } |  j d  k r  t j t | ƒ t ƒ |  _ n  | |  _ | d  k rÓ t j d | d |  j ƒ } n  | d  k r*g  } d }	 x< | D]1 }
 d |	 } | j | ƒ |	 d 7}	 |
 j ƒ  qò Wn  t j d | d | d |  j ƒ |  _ t j d |  j ƒ |  _ i  |  _ i  |  _ i  |  _ i  |  _ | |  _ x³ t | | ƒ D]¢ \ } }
 |
 j	 ƒ  j
 ƒ  |  j | <|
 |  j | <t j d |
 ƒ |  j | <|  j d |  j | d |  j | d |
 d	 |  j j d
 |  j j d |  j j  ƒ |  j | <q£Wd  |  _! d  |  _" |  j# ƒ  |  j ƒ  d  S(   Ni   t   reference_file_listt   logs   ref%dt   reference_hierarchy_listt   pdb_hierarchyt   dihedral_proxiest
   sites_cartt   include_hydrogenst   include_main_chaint   include_side_chain($   t   countt   Nonet   AssertionErrort   syst   stdoutR   t   paramst	   selectiont   reset_i_seq_if_necessaryt   atomst   extract_xyzR   t   boolt   lent   TrueR   R   t   process_reference_filest   appendt   get_reference_dihedral_proxiest   dihedral_proxies_reft   build_name_hasht   i_seq_name_hasht   sites_cart_reft   pdb_hierarchy_reft   i_seq_name_hash_reft   reference_dihedral_hashR   t   zipt   build_dihedral_hasht	   hydrogenst
   main_chaint
   side_chaint	   match_mapt	   proxy_mapt#   build_reference_dihedral_proxy_hash(   t   selfR   R   R   t   has_hdR   R   R   R   t   ref_countert	   hierarchyt   keyt   file(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt   __init__O   sh    			
		

												
c         C   s   | d t  | | d | ƒ S(   Ni   i   (   t   exp(   R5   t   xt   weightt   top(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt   top_out_function•   s    c         C   s#   d | | t  | | d | ƒ S(   Ni   (   R<   (   R5   R=   R>   R?   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt   top_out_gradient˜   s    c         C   s;   d | | d | | d | d t  | | d | ƒ S(   Ni   (   R<   (   R5   R=   R>   R?   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt   top_out_curvature›   s    c      	   C   s  g  } g  } d } xñ | j  ƒ  D]ã } xÚ | j ƒ  D]Ì } xÃ | j ƒ  D]µ } t | j ƒ  ƒ d k rE | j ƒ  d }	 t j j |	 d ƒ }
 | j ƒ  } t	 j
 | ƒ } |
 d k rú t	 j | | ƒ rú | j |
 ƒ | j t d | d | ƒ ƒ | d 7} qú qE qE Wq2 Wq Wd j | ƒ | f S(   Ni    i   t   Xt   i_seqt   rgt    (   t   modelst   chainst   residue_groupsR!   t   unique_resnamesR   t   one_letter_given_three_lettert   getR   R   t
   get_i_seqst   is_residue_in_selectionR$   R   t   join(   R5   R   R   t   seqt   resultt   countert   modelt   chainRE   t   resnamet   olcR   t   i_seqs(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt   extract_sequence_and_sitesž   s     c   3      C   s‰  | d  k r t j } n  t j d | ƒ } t j d | ƒ } | j ƒ  } i  } i  }	 x> | D]6 }
 | |
 } t j d | ƒ | |
 <| j ƒ  |	 |
 <q[ Wi  } x |  j D] } i  | | <q¥ Wt | j	 ƒ d k rðg  } t
 d | d | d |  j d | d | ƒ } d } xG| j ƒ  D]Ø } | | } x˜ | D]� } | d } | d } | | k rat d	 d
 ƒ ‚ n  | j | ƒ | d 7} | d | d 7} | d | d 7} | d | d 7} | d 7} q(Wt j j | ƒ } t j | ƒ j ƒ  j	 | _	 qWn^ x[ | j	 D]P } | j d  k rút |  j ƒ d k r7t d d ƒ ‚ n  |  j d | _ qúqúWx4	| j	 D])	} d  } d  } d  } d  } d  } d  } t j d | j j ƒ  ƒ } t j d | j j ƒ  ƒ } x¹| D]±} | j ƒ  j d ƒ j d ƒ j d ƒ rG| d  k r8| j ƒ  j d ƒ j d ƒ j d ƒ d j d ƒ j d ƒ } qGt d ƒ ‚ n  | j ƒ  j d ƒ j d ƒ j d ƒ s•| j ƒ  j d ƒ j d ƒ j d ƒ r¿| j ƒ  j d ƒ j d ƒ j d ƒ d j d ƒ } t | ƒ d k r| d  k r
| d  k r
| d } | d } qmt d d  ƒ ‚ qpt | ƒ d k ra| d  k rm| d  k rm| d } | d } qmqpt d! ƒ ‚ q¿q¿Wx| D]} | j ƒ  j d ƒ j d ƒ j d ƒ r]| d  k rN| j ƒ  j d ƒ j d ƒ j d ƒ d j d ƒ j d ƒ } | j d ƒ s| j d ƒ r| j ƒ  } n  | j d ƒ s<| j d ƒ rZ| j ƒ  } qZq]t d ƒ ‚ n  | j ƒ  j d ƒ j d ƒ j d ƒ s«| j ƒ  j d ƒ j d ƒ j d ƒ r{| j ƒ  j d ƒ j d ƒ j d ƒ d j d ƒ } t | ƒ d k r3| d  k r | d  k r | d } | d } qƒt d d  ƒ ‚ q†t | ƒ d k rw| d  k rƒ| d  k rƒ| d } | d } qƒq†t d! ƒ ‚ q{q{W| d  k r¢| d  k sÀ| d  k	 rº| d  k	 sÀt ‚ | d  k rð| d  k rð| d  k rð| d  k s&| d  k	 r | d  k	 r | d  k	 r | d  k	 s&t ‚ | j } t j  d" | d# | j g ƒ } t j  d" |	 | d# | j g ƒ }  t j | j | ƒ } t j | j |	 | ƒ } t j! d | d$ | d | ƒ j" ƒ  d j# ƒ  d }! t j! d | | d$ | d | ƒ j" ƒ  d j# ƒ  d }" d  }# | j$ r˜y" t j% d% |" d& |! ƒ \ }# }$ Wq˜t& k
 r”}% t' |% ƒ d' k r‡t' |% ƒ d( k r‡t' |% ƒ d) k r‡|% ‚ q•| d* IJq˜Xn  |# d  k r¾	xÚ|$ j( D]	}& |& d }' |& d }( |' d  k s®|( d  k ræq®n  i  }) i  }* x8 |' j) ƒ  D]* }+ |+ j* ƒ  |+ j+ }, | |, |) |+ j, <qÿWx< |( j) ƒ  D]. }+ |+ j* ƒ  |+ j+ }, | | |, |* |+ j, <q:	WxH |) j ƒ  D]: }
 |* j- |
 ƒ }- |- d  k ry	|* |
 | | |) |
 <qy	qy	Wq®WqX| d+ IJd }. | d  k	 r9
| d  k	 r9
| d  k	 r9
| d  k	 r9
t. | ƒ t. | ƒ }. |. t. | ƒ t. | ƒ k s9
t ‚ n  xE| D]=}/ | |/ }
 | d  k	 r¶
t | ƒ d k r{
d | } n t | ƒ d k r–
d, } n  t j/ d- d. | d/ |
 ƒ }
 n  |. d k r|
 d0 d1 !}0 d2 t. |0 ƒ |. }1 t j/ d- d. | |1 |
 ƒ }
 n  | | j- |
 ƒ }2 |2 d  k rBt j/ d3 d7 | d5 |
 ƒ }
 n  | | j- |
 ƒ }2 |2 d  k rgq@
q@
| | |
 | | |/ <q@
WqXW| S(8   NR   i    R*   R   R   R   RF   i   s,   multiple reference models for one chain not s   currently supporteds     reference_group {
s      reference = s   
s      selection = s      file_name = s     }
s-   Ambiguous reference group selection - please s%   specify file name for reference groups
   AND|OR|NOTt   (t   )t   CHAINt    iÿÿÿÿt   't   "s0   Cannot specify more than one chain per selectiont   RESSEQt   RESIDt   :s%   Cannot specify more than one residue s   or residue range per selections#   Do not understand residue selectiont   cachet   string_listR   t   reference_chaint   moving_chains$   can't make graph for first structures%   can't make graph for second structures"   secondary structure does not matchs   SSM alignment failed...s   trying simple matching...s     s   (.{5}\D{3})(.{2})(.{4})s   \1s   \3i
   i   s   %4ds   (.{4}).(\D{3})(.{2})(.{4})s   \2s   \4s   \1 s   \1 \2(0   R   R   R   R   t   build_i_seq_hashR'   t   atom_selection_cacheR   R!   t   reference_groupt   get_matching_chainst   keysR   R$   t   iotbxt   philt   parset   reference_model_paramst   fetcht   extractt	   file_namet   ret   splitR   t   uppert	   referencet   stript
   startswitht   lstript   endswitht   rstripR   t'   phil_atom_selections_as_i_seqs_multiplet   hierarchy_from_selectionRG   RH   t   SSM_alignmentt
   _ssm_alignt   RuntimeErrort   strt   pairsR   t   pdb_label_columnst   segidt   nameRL   t   intt   sub(3   R5   R   R*   R   R   t   model_iseq_hasht   model_name_hasht	   sel_cachet   ref_iseq_hasht   sel_cache_refR9   t   ref_hierR2   R:   t   matched_model_chainst   reference_matchest   new_reference_groupst   matchest   matcht   model_chaint	   ref_chaint   new_reference_groups_as_philRE   t   model_res_mint   model_res_maxt   ref_res_mint   ref_res_maxt	   sel_modelt   sel_reft   selt   rest	   sel_atomst   sel_atoms_reft   mod_ht   ref_ht   ssmt	   ssm_alignt   et   pairt	   model_rest   ref_rest   temp_model_atomst   temp_ref_atomst   atomt	   atom_tempt   ref_atomt   offsetRD   t   resnumt   new_numt   cur_ref(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt   process_reference_groups°   sh    
		



		'+''4

'+''4

			
		
			

$
%
	  	c         C   s’   i  |  _  x‚ |  j j ƒ  D]q } i  |  j  | <|  j | } xN | D]F } d } x& | j D] } | |  j | | 7} qV W| |  j  | | <q@ Wq Wd  S(   NRF   (   t   reference_dihedral_proxy_hashR&   Rj   RW   R+   (   R5   t   reft   proxiest   dpR9   RD   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyR4   �  s    	c         C   s˜  | s t  j d | ƒ } n  t  j d | ƒ } t ƒ  }	 x[| D]S}
 y9| s x0 |
 j D]" } | | d k rV t ƒ  ‚ qV qV Wn  | sÕ t } x2 |
 j D]' } | | d d !d k r• t } Pq• q• W| sÕ t ƒ  ‚ qÕ n  | s+t } x2 |
 j D]' } | | d d !d k rë t } Pqë që W| r+t ƒ  ‚ q+n  d	 } x |
 j D] } | | | } q;Wt j	 j
 d
 | d |
 ƒ } | j |	 | <Wq= t k
 r�q= Xq= W|	 S(   NR   s    Hi    i   s    CA s    N  s    C  s    O  RF   R   t   proxy(   s    CA s    N  s    C  s    O  (   s    CA s    N  s    C  s    O  (   R   t   build_element_hashR'   t   dictRW   t   StopIterationt   FalseR"   t   cctbxt   geometry_restraintst   dihedralt   angle_model(   R5   R   R   R   R   R   R   t   i_seq_element_hashR(   t   dihedral_hashR´   RD   t   sc_atomsR9   t   di(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyR.   Œ  sL    				c         C   s[  d  } i  } t j d |  j d |  j ƒ } t j j ƒ  |  _ |  j	 j
 } |  j	 j } |  j d |  j d |  j d |  j	 d |  j ƒ } | |  _ d  } d  } |  j	 j rÎt j j d d ƒ sÍ t d d ƒ ‚ n  i  } i  } xò |  j D]ä } t j d |  j | d	 d  ƒ | | <| | j ƒ  |  j | j ƒ  }	 t j |	 ƒ \ }
 } t j j j d
 |
 d t ƒ } | d  k rã | j ƒ  } | j ƒ  } | d | } |  j | j  ƒ  } t j! d | d | g ƒ | | <qã qã Wn  x}| D]u} d } t } x' | j" D] } |  j# | sñt$ } qñqñW| sqÕn  x" | j" D] } | |  j% | } q'Wd  } d  } xÖ |  j D]Ë } | d  k	 rpqXqXd } t } x¤ | j" D]™ } | r
| | j& | ƒ } | d  k	 rõ|  j' | j& | ƒ } | d  k rèt$ } d  } d  } q| | } q
t$ } d  } d  } n  | d  k	 r†| } q†q†WqXWyž |  j( | | } | d d !| d d !k rÄ| d d !| d d !k rÄ| d d !| d d !k rÄ| d d !| d d !k rÄ| | d d !f | | d d !<n  Wn t) k
 rÛqÕn X|  j	 j r| | d  k r| | | j" d | | k r»| | | j" d | | k r»| | | j" d | | k r»| | | j" d | | k r»t j j* d | j" d | d d d$ d d  d! t+ ƒ } |  j j, | ƒ qJt j j* d | j" d | d d d% d d# d! t+ ƒ } |  j j, | ƒ qÕt j j* d | j" d | d d | d d | d! t+ ƒ } |  j j, | ƒ qÕW| |  _- d  S(&   NR   R   R*   R   R„   t   ksdssps!   ksdssp module is not configured, s-   cannot generate secondary structure referencet   sec_str_from_pdb_filet   recordst
   allow_nones    or Rb   Rc   RF   i   i   i   i%   i+   i8   i   i    i   i   i   RW   t   angle_idealR>   g      ð?t   limitg      >@t   top_outg      @g      .@g      ð?g      9@(.   R   R   t   get_complete_dihedral_proxiesR   R   Rº   R»   t   shared_dihedral_proxyt   reference_dihedral_proxiesR   t   sigmaRÇ   R°   R*   R2   t   secondary_structure_onlyt   libtbxt   envt
   has_moduleR   R   R   t   managert   find_automaticallyt   as_pdb_stringt   run_ksdssp_directRk   t   pdbt   process_recordsR"   t   overall_helix_selectiont   overall_sheet_selectionRg   R{   RW   R   R¹   R(   RL   R+   R,   t	   Exceptiont   dihedral_proxyt   TOP_OUT_FLAGR$   t   residue_match_hash(   R5   t   ss_selectionRÜ   t   complete_dihedral_proxiesRÌ   RÇ   R2   t   ref_ss_mR:   t   pdb_strRÄ   t   stderrRÃ   R×   RØ   t   overall_selectionR‹   R´   t   key_workt   completeRD   R9   t
   file_matcht	   ref_matcht   map_partt   key_partt   reference_anglet   dp_add(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyR%   À  sä    												%	
		
			c         C   s  | d  k r t j } n  | d IJ| d IJ|  j j ƒ  } d „  } | j d | ƒ xs |  j D]h } | d | IJ| d IJxG | D]? } |  j | d | k rƒ | d | |  j | d	 f IJqƒ qƒ Wq^ W| d
 t | ƒ IJ| d t |  j ƒ IJ| d IJd  S(   Ns8   --------------------------------------------------------s!   Reference Model Matching Summary:c         S   s   |  d S(   Ni   (    (   Rœ   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt   get_key_chain_numE  s    R9   s   
reference file: %s
s   Model:              Reference:i    s   %s  <=====>  %si   s%   
Total # of matched residue pairs: %ds)   Total # of reference model restraints: %d(	   R   R   R   RÜ   Rj   t   sortR   R!   RË   (   R5   R   Rj   Rë   R:   R9   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt   show_reference_summary@  s$     

	
#c         C   s   |  j  | _  d  S(   N(   RË   (   R5   t   geometry(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt   add_reference_dihedral_proxiesT  s    c   /      C   s	  |  j  } |  j } | d  k r* |  j } n  t d d | ƒt t ƒ } t j j	 j	 ƒ  } t
 j
 d | ƒ } i  }	 i  }
 x4 | j ƒ  D]& } | | } t
 j
 d | ƒ |	 | <qƒ Wi  } i  } i  } i  } d } x@ | j D]5 } | j | | j ƒ  <| j d k rÕ | d 7} qÕ qÕ WxM |	 j ƒ  D]? } i  | | <x, |	 | j D] } | j | | | j ƒ  <q9WqW| d IJx | j ƒ  D]} x	| j ƒ  D]û } xò | j ƒ  D]ä } t
 j | ƒ } xÌ | j ƒ  D]¾ } yz | j | j ƒ } | j | | ƒ } | d  k	 r<t j d | j d	 | j d
 | j d | j d | j ƒ } | | | <n  Wq½t k
 rz| d | j | j ƒ  | j | j f IJq½Xq½Wq›WqˆWquW| d k r¥| d IJd  S| d | IJ| d IJxO| j ƒ  D]A} | | } i  | | <x$| j ƒ  D]} x| j ƒ  D]ÿ } xö | j ƒ  D]è } t
 j | ƒ } xÐ | j ƒ  D]Â } y~ | j | j ƒ } | j | | ƒ } | d  k	 r¼t j d | j d	 | j d
 | j d | j d | j ƒ } | | | | <n  Wq9t k
 rú| d | j | j ƒ  | j | j f IJq9Xq9WqWqWqñWqÊW| d IJ| j ƒ  } xS| j ƒ  D]E} x<| j ƒ  D].} x%| j ƒ  D]} t | j  ƒ  ƒ d k r­| d | j | j ƒ  d | j ƒ  d j f IJqXn  x¿| j  ƒ  D]±} x¨| j! ƒ  D]š} | j d k rèqÍn  t j d | j d	 | j d
 | j ƒ  d j d | j d | j ƒ } t | j ƒ d k rPd | j } n	 | j } d | j | | j ƒ  f } | j" ƒ  } |  j# j | ƒ }  |  d  k	 rÍ|  d } n qÍ| j | ƒ }! | | j | ƒ }" | j | ƒ }# | | j | ƒ }$ |! d  k	 rÍ|" d  k	 rÍ|# d  k	 rÍ|$ d  k	 rÍ| | d k rÚ| | | d k rÚt$ j% d | d | d t& d d  ƒ }% |% d  k r}qÍn  t |# ƒ t |$ ƒ k s›t' ‚ t |# ƒ t |% ƒ k s¹t' ‚ d }& | j( ƒ  j) ƒ  }' | j ƒ  j* |' ƒ }( xá |% D]Ù }) |) d }* |) d }+ | j( ƒ  j+ d |( ƒ t, j- ƒ  }, |$ |& |# |& }- |- d k  rT|- d 7}- n  xS |+ D]K }. t. d |( |* d d |( |* d d |( |. d |- d  t& ƒ }, |, |( |. <q[W| j/ |' |( ƒ } |& d 7}& qíW| j0 | ƒ qg	|  j1 j2 rg	|! d k rg	|" d k rg	|! |" k rd	t$ j% d | d | d t& d d  ƒ }% |% d  k r@qÍn  d }& | j( ƒ  j) ƒ  }' | j ƒ  j* |' ƒ }( xá |% D]Ù }) |) d }* |) d }+ | j( ƒ  j+ d |( ƒ t, j- ƒ  }, |$ |& |# |& }- |- d k  rÛ|- d 7}- n  xS |+ D]K }. t. d |( |* d d |( |* d d |( |. d |- d  t& ƒ }, |, |( |. <qâW| j/ |' |( ƒ } |& d 7}& qtW| j0 | ƒ qd	qg	qÍqÍWqºWqXWqEWq2Wd  S(!   Ns4   Correcting rotamer outliers to match reference modelt   outR   i    t   OUTLIERi   s+   ** evaluating rotamers for working model **t   chain_idt   resseqRU   t   icodet   altlocs8     %s%5s %s is missing some sidechain atoms, **skipping**s-   No rotamer outliers detected in working models   Number of rotamer outliers: %ds.   
** evaluating rotamers for reference model **s   
** fixing outliers **s1   %s%5s %s has multiple conformations, **skipping**R\   t   PROs   %s%s%st   residuet   mon_lib_srvt   remove_clusters_with_all_hR   t   new_xyzg     €v@t   axis_point_1t   axis_point_2t   pointt   anglet   deg(3   R   R*   R   R   R
   t   SidechainAnglesR¹   t   mmtbxt   monomer_libraryt   serverR   Rj   t   resultst   rotamer_namet   id_strRG   RH   RI   t   construct_complete_sidechaint   atom_groupsRL   Rõ   t   measureChiAnglesR   t   idRó   RU   Rô   RÙ   t   residR   R!   t
   conformerst   residuesRv   RÜ   R   t   axes_and_atoms_aa_specificR"   R   R   t   extract_i_seqt   selectt   set_xyzR   t   vec3_doubleR	   t   set_selectedt   set_sites_cartR   t   strict_rotamer_matching(/   R5   t   xray_structureR   t   quietR   R*   t   saRø   t   rt   rot_list_referencet   coot_referenceR9   R8   t
   model_hasht
   model_chist   reference_hasht   reference_chist   model_outlierst   rotRS   RT   t   residue_groupt   all_dictt
   atom_groupt	   atom_dictt   chisR:   t   sites_cart_startt	   conformerR÷   Rò   t   file_keyRå   t	   model_rott   reference_rott   m_chist   r_chist   axis_and_atoms_to_rotateRR   t   residue_iselectiont   sites_cart_residuet   aat   axisR   Rú   t	   angle_degR©   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt   set_rotamer_to_referenceX  sj   		 


					(



					,
								


		


c         C   sJ  g  } t  j j ƒ  } i  } x | D] } | j | j ƒ q" Wt |  j ƒ GHx0 |  j D]% } | j | k rT | j | ƒ qT qT Wx� |  j D]v } t } xM | D]E }	 | |	 k rµ t	 } qš x' | |	 D] }
 | |
 k rÀ t	 } qÀ qÀ Wqš W| s‡ |  j | | | <q‡ q‡ Wt | ƒ GH| |  _ | |  _ |  j
 d d IJ|  j
 d IJ|  j ƒ  d  S(   Ns-   
**Removed reference restraints that overlap s   with torsion NCS restraints**
s#   Updated Reference Model Restraints:(   Rº   R»   RÊ   R$   RW   R!   RË   RÜ   R¹   R"   R   Rí   (   R5   t   ncs_dihedral_proxiest   ncs_match_hasht
   proxy_listt   remaining_proxiest   remaining_match_hashR´   R9   t   found_matcht   key2R‘   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt"   remove_restraints_with_ncs_matches  s4    			
N(   t   __name__t
   __module__R   R¹   R;   R@   RA   RB   RX   R°   R4   R"   R.   R%   Rí   Rï   R4  R<  (    (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyR   M   s0   ?				Í	.	€	Äc   +      C   s±  i  } | d IJ|  j  ƒ  d j ƒ  } t |  j ƒ  ƒ } t j g  | D] } | j ^ qE ƒ }	 |	 j d ƒ }
 t j	 d |  d |
 ƒ } x%| D]} i  } g  | | <| | } | j  ƒ  d j ƒ  } t | j ƒ  ƒ } t j g  | D] } | j ^ qá ƒ } | j d ƒ } t j	 d | d | ƒ } xwt
 | ƒ D]i\ } } t } x: | j ƒ  D], } | j ƒ  rs| j ƒ  rsqMqMt } qMW| s‰q.n  t j | ƒ } | d  k r»| d | j IJq.n  |
 r×d | j | f } n d | j } x°t
 | ƒ D]¢\ } } t } x: | j ƒ  D], } | j ƒ  r6| j ƒ  r6qqt } qW| sLqñn  t j | ƒ } | d  k rmqñn  | r‰d | j | f } n d | j } | j | | j | f } | j | | j | f } | j | | j | f }  t j d	 | d
 | d | d |  d | ƒ }! t |! ƒ t | j ƒ  | j ƒ  ƒ | j k rñ| | f }" | j | f }# | j | f }$ |# | k rg  | |# <n  | |# j |$ ƒ qñqñWq.Wxý | j ƒ  D]ï }" |
 rÏd |" d |" d f }% n d |" d }% d  }& d  }' xs | |" D]g }( |
 rd |( d |( d f }) n d |( d }) |' d  k r>|) }' n  |" d |( d k rô|) }& qôqôW|& d  k rz|% |& f }* n |% |' f }* | | j |* ƒ q¨W| | j ƒ  qŒ W| S(   Ns.   determining reference matches automatically...i    s       R   t	   use_segids0   chain %s has conflicting segid values - skippings   chain '%s' and segid '%s's
   chain '%s'R   t	   sequencest   padded_sequencest
   structuresR   i   (   RG   RH   t   listt   atoms_with_labelsR   t
   std_stringRƒ   t   all_eqR   t   alignment_managert	   enumerateR¹   R  t
   is_proteint   is_naR"   t   get_unique_segidR   R
  R@  RA  RB  t
   _alignmentR!   t   mint   residue_groups_sizet
   similarityR$   Rj   Rì   (+   R   R*   R   R   R   RŽ   RH   t   atom_labelst   at   segidsR?  t   amR:   t	   pair_hasht   hierarchy_reft
   chains_reft   atom_labels_reft
   segids_reft   use_segid_reft   am_reft   it   chain_it   found_conformerR(  t   segid_it   chain_i_strt   jt   chain_jt   segid_jt   chain_j_strt   seq_pairt   seq_pair_paddedt   struct_pairt   residue_match_mapR9   t   pair_keyt	   match_keyt	   chain_strt   exact_matcht   first_matchR‘   t   ref_strt
   match_pair(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyRi   >  sº    
%		

%		




			g      @g      .@c         C   s†  | d  k	 r0 t | t j ƒ r0 | j ƒ  } q0 n  | d  k r] t j t | ƒ t ƒ j ƒ  } n  t | ƒ t | ƒ k s{ t ‚ d | d } t j	 j
 ƒ  } t d | d | ƒ }	 t d |  d | d | ƒ }
 x· |
 D]¯ } x¦ | j D]› } | j } g  } x | D] } | j |	 | ƒ qû Wt j	 j d | d	 d
 d | ƒ } | j } t j	 j d | d	 | d | d | d | ƒ } | j | ƒ qß WqÏ W| S(   Ng      ð?i   R   R   R   t   atom_selectiont   chi_angles_onlyt   sitesRÆ   g        R>   RW   RÇ   RÈ   (   R   t
   isinstanceR   R    t
   iselectionR!   R"   R   Rº   R»   RÊ   t   get_selection_to_sites_mapt   collect_residue_torsion_anglesR&  RW   R$   R¼   R½   RÚ   (   R   R   R   RÌ   RÇ   Rp  t   top_out_potentialR>   t   torsion_proxiest   selection_to_sites_mapt   residue_torsionst   residue_infot   chiRW   Rq  RD   RÁ   RÆ   R´   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt   build_torsion_proxiesª  sD    						c         C   sM   i  } t  | t j ƒ s t ‚ x( t | ƒ D] \ } } |  | | | <q+ W| S(   N(   Rr  R   t   size_tR   RH  (   R   R   t   sites_selection_mapR[  RD   (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyRt  Õ  s
    (,   t
   __future__R    t   mmtbx.alignmentR  t	   iotbx.pdbR   RÎ   R   t   cctbx.geometry_restraintsRº   t   mmtbx.validationR   t   mmtbx.utilsR   t   mmtbx.rotamer.sidechain_anglest   mmtbx.monomer_libraryt   cctbx.array_familyR   t
   iotbx.philRk   t   libtbx.load_envt   libtbx.utilsR   R   t   scitbx.matrixR	   t   libtbx.str_utilsR
   t   mmtbx.torsion_restraintsR   R   Rr   R"   RÛ   Rl   Rm   Rn   t   objectR   Ri   R   R¹   R|  Rt  (    (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/torsion_restraints/reference_model.pyt   <module>   s<   
	5	ÿ ÿ ó	o$