ó
±™xTc           @   sÙ   d  d l  m Z d  d l m Z m Z d  d l m Z d  d l Z d  d l Z d Z	 d e d „ Z e e e d d „ Z d „  Z d	 e f d
 „  ƒ  YZ d e d d „ Z d d „ Z e d k rÕ e e j d ƒ n  d S(   iÿÿÿÿ(   t   division(   t   Sorryt   null_out(   t   StringIONsŽ  
debug = False
  .type = bool
morph {
  pdb_file = None
    .type = path
    .multiple = True
  cif_file = None
    .type = path
    .multiple = True
    .help = Restraint (.cif) files for ligands etc.
  output_directory = None
    .type = path
    .help = Output directory (defaults to current working directory)
  output_prefix = morph
    .type = str
  serial_format = %03d
    .type = str
  frames = 30
    .type = ints
    .help = Number of frames for each transition (list of integers, or just             a single integer that applies to each transition)
  pause = 0
    .type = int
    .help = Number of frames to pause at each starting structure
  delete_waters = True
    .type = bool
  verbose = False
    .type = bool
  fitting {
    align_atoms = name CA
      .type = str
      .help = Selection of atoms to use in alignment ("None" to disable)
    reference_structure = 0
      .type = int
      .help = Index (C array notation) of model to use as reference in alignment
    sieve_fit = True
      .type = bool
      .help = Perform sieve-fitting of structures
  }
  minimization {
    minimize = True
      .type = bool
    n_min_steps = 60
      .type = int
    exclude_dihedrals = True
      .type = bool
    interpolate_dihedrals = False
      .type = bool
    dihedral_weight = 0.1
      .type = float
  }
}
pdb_interpretation {
  include scope mmtbx.monomer_library.pdb_interpretation.master_params
}
c   %      C   sw  t  |  j j ƒ d k s t ‚ t  |  j j ƒ t  |  j j ƒ d k sL t ‚ | d  k rd t j } n  d d l m	 } m
 } d d l m } g  } xO |  j j D]A } | j | d d ƒ} | j d ƒ | j j }	 | j |	 ƒ q� Wt d | d |  j j d	 | ƒ }
 | j
 ƒ  } | j ƒ  } x[ |  j j D]M } d
 | GH| j d | ƒ } t j d | d | ƒ | j d | d | ƒ q(W|  j j j ršt |  j	 j _ n  | j d | d | d |  j	 d |
 d d t ƒ } | j  } | j! j" ƒ  j# ƒ  g } x1 |
 d D]% } | j" ƒ  j# ƒ  } | j | ƒ q÷W|  j j$ j% d  k	 r³| d IJ| j! j& ƒ  } | j' |  j j$ j% ƒ } | j( t ƒ d k rŠt) d ƒ ‚ n  |  j j$ j* } | | } d } x| t  | ƒ k  r¯| | k r¢| | } t  | ƒ t  | ƒ k o÷d k n st ‚ |  j j$ j+ r\d d l, m- } | j+ d | d | d | ƒ } | j. j/ | | j0 j/ } n t1 d | d | d | ƒ } | j2 ƒ  | j2 ƒ  k s•t ‚ | | | <n  | d 7} q¬Wn  | d IJg  } | j3 ƒ  } xƒ t4 t  |  j j ƒ d ƒ D]e } t5 d | j! d | d | | d | | d d |  j j d |  j j | d | ƒ }  | j |  ƒ qìWd }! |  j j6 d  k	 r‘t7 j8 j9 |  j j6 |  j j: ƒ }" n |  j j: }" xf t; | ƒ D]X \ } }  |  j< d  |" d! |! d" |  j j= d# |  j j> d$ | t  | ƒ d k d% | ƒ }! qªWt? d& |" d' ƒ }# x? t4 d |! ƒ D]. } d( |" |  j j= f }$ |# d) |$ | IJq)W|# j@ ƒ  | d* |" IJd  S(+   Ni   iÿÿÿÿ(   t   pdb_interpretationt   server(   t   file_readert
   force_typet   pdbt   pdb_hierarchiest   delete_waterst   debugs   Loading CIF file %st	   file_namet
   cif_objectt   mon_lib_srvt   ener_libt   paramst   pdb_inpi    t6   substitute_non_crystallographic_unit_cell_if_necessarys#   Superposing on initial structure...s    No atoms in alignment selection!(   t	   superposet   sites_fixedt   sites_movingt	   selections   Ready to morpht   pdb_hierarchyt   restraints_managert   start_coordst
   end_coordst   nstepst   outt   output_baset   serialt   serial_formatt   pauset   pause_at_endt   logs   %s.pmlt   ws   %s_%ss   load %s.pdb, morphs   PyMOL script is %s.pml(A   t   lent   morpht   pdb_filet   AssertionErrort   framest   Nonet   syst   stdoutt   mmtbx.monomer_libraryR   R   t   iotbxR   t   any_filet   check_file_typet   file_objectt	   hierarchyt   appendt   homogenize_structuresR
   R   t   cif_filet   read_cift   mon_lib_servt   process_cif_objectt   minimizationt   interpolate_dihedralst   Falset   peptide_linkt   discard_psi_phit   processt   Truet   all_chain_proxiesR   t   atomst   extract_xyzt   fittingt   align_atomst   atom_selection_cacheR   t   countR   t   reference_structuret	   sieve_fitt   scitbx.mathR   t   rt   elemst   tt	   fit_sitest   sizet   geometry_restraints_managert   ranget   adiabatic_mappingt   output_directoryt   ost   patht   joint   output_prefixt	   enumeratet   write_pdb_filesR   R    t   opent   close(%   R   R   R   R   R   R   R	   R&   t   pdb_inR1   t   new_pdbR   R   R4   R   t   processed_pdb_fileR?   t   static_coordsR   t   sitest   selection_cacheR   t   i_refR   t   jR   R   t   lsq_fitt   sites_moving_newt   morphsR   t   iR%   R   R   t   ft   format_base(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/interpolate.pyt   morph_modelsB   s¾    . 				
		


.			
#	
		
c         C   s   d d l  } | d k r$ t ƒ  } n  g  } i  } x5 |  D]- } t | j ƒ  ƒ d k r7 t d ƒ ‚ q7 q7 Wg  }	 x“ |  D]‹ } t g  ƒ }
 xi | j ƒ  D][ } | j ƒ  } | rÁ | j	 d k rÁ q” n  | j
 | j ƒ  | j | j f } |
 j | ƒ q” W|	 j |
 ƒ qu W|	 d j |	 d Œ  } t | ƒ } | d k rBt d ƒ ‚ n  | d	 t | ƒ IJg  } xÜ t |  ƒ D]Î \ } } t g  ƒ }
 | j ƒ  } d } x” | t | ƒ k  r)| | } | j ƒ  } | j
 | j ƒ  | j | j f } | | k r| r| d
 | j ƒ  | d f IJn  | | =q–| d 7} q–W| j | ƒ qiWi  } g  g } xl t | d ƒ D]Z \ } } | j ƒ  } | j
 | j ƒ  | j | j f } | | | <| d j | j ƒ  ƒ q[WxÜ | d D]Ð } t | ƒ t | d ƒ k sìt ‚ d g t | ƒ } xU | D]M } | j ƒ  } | j
 | j ƒ  | j | j f } | | } | j ƒ  | | <qWt | ƒ t | ƒ k sut ‚ d | k s‡t ‚ | j | ƒ qÄWg  } x{ t | ƒ D]m \ } } | j j d d d | ƒ } | j | ƒ | r«t d | d ƒ } | j | j ƒ  ƒ | j ƒ  q«q«W| S(   s“   
  Eliminate atoms not present in all models.  This ignores residue names, so
  corresponding mainchain atoms should be preserved in most cases.
  iÿÿÿÿNi   s'   Multiple model PDB files not supported.t   HOHt   WATi    s   No atoms left in structure.s   %d atoms in common.s   deleting %s in model %dt   source_infot   liness   processed_%d.pdbR#   (   Ri   Rj   (   t	   iotbx.pdbR)   R   R$   t   modelsR   t   setR@   t   fetch_labelst   resnamet   chain_idt   residt   namet   altloct   addR2   t   intersectiont   RuntimeErrorRV   t   id_strt   format_atom_recordR'   R   t   inputRX   t   writet   as_pdb_stringRY   (   R	   t   delete_heteroatomsR
   R   R"   R-   t	   chain_idst   residues_by_chainR1   t	   atom_setst   atom_sett   atomt   labelst	   atom_infot
   common_sett   n_atomst
   atom_listst   kR@   Re   t   index_lookupt	   atoms_outt   sorted_atomst   pdb_outt   atoms_stringsRZ   Rf   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/interpolate.pyR3   ¤   s‚    	 !
!!
	!
"!
c         C   s’   d d l  m } |  j | ƒ } | j | ƒ } t | ƒ t | ƒ k oQ d k n s\ t ‚ | j d | d | ƒ } | j j | | j j } | S(   sH   
  Simple least-squares superposition of sites on reference structure
  iÿÿÿÿ(   R   i    t   reference_sitest   other_sites(	   RH   R   t   selectR$   R'   t   least_squares_fitRI   RJ   RK   (   R   R   R   R   t   sites_fixed_alnt   sites_moving_alnt   lsq_fit_objt   sites_moved(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/interpolate.pyRL   ï   s    .		R%   c           B   s>   e  Z d  Z d „  Z d „  Z d „  Z d d e d d „ Z RS(   s'   
  Container for interpolated sites.
  c         C   s   | j  ƒ  |  _ g  |  _ d  S(   N(   t	   deep_copyR   t   _frames(   t   selfR   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/interpolate.pyt   __init__  s    c         C   s   |  j  j | ƒ d  S(   N(   R˜   R2   (   R™   t
   sites_cart(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/interpolate.pyt	   add_frame  s    c         C   s3   t  | d ƒ } | j |  j j ƒ  ƒ | j ƒ  d  S(   NR#   (   RX   R|   R   R}   RY   (   R™   R   Rf   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/interpolate.pyt
   _write_pdb  s    s   %04di    c         C   s™  | d  k r t ƒ  } n  d | | f } | } | d k r¬ xo t | ƒ D]^ }	 |  j j ƒ  j |  j d ƒ | | }
 |  j |
 ƒ | d t j	 j
 |
 ƒ IJ| d 7} qG Wn  xb |  j D]W } |  j j ƒ  j | ƒ | | }
 |  j |
 ƒ | d t j	 j
 |
 ƒ IJ| d 7} q¶ W| r•| d k r•xo t | ƒ D]^ }	 |  j j ƒ  j |  j d ƒ | | }
 |  j |
 ƒ | d t j	 j
 |
 ƒ IJ| d 7} q0Wn  | S(   Ns	   %s_%s.pdbi    s
     wrote %si   iÿÿÿÿ(   R)   R   RO   R   R@   t   set_xyzR˜   R�   RR   RS   t   basename(   R™   R   R   R   R    R!   R"   t   file_formatR‰   Ra   R   R^   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/interpolate.pyRW     s2     


N(	   t   __name__t
   __module__t   __doc__Rš   Rœ   R�   R:   R)   RW   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/interpolate.pyR%   ý   s   			i
   c      	   C   s0  d d l  m } d d l m }	 d d l }
 | j ƒ  | j ƒ  k ri t d | j ƒ  | j ƒ  f ƒ ‚ n  | } | j p | j } |	 j	 j	 d t
 d | ƒ } |
 j j d | j ƒ } g  } | d	 } | j ržxÍ | j D]¿ } g  } g  } xD t d
 ƒ D]6 } | j | | j | ƒ | j | | j | ƒ q÷ W|	 j d | d d d d	 ƒ j } |	 j d | d d d d	 ƒ j } | | | } | j | ƒ d | _ qØ Wn  t |  ƒ } | j | ƒ | } | } xY| d k r| d | | IJ| r÷| d IJn  | j ƒ  } | | d | } | | 7} | j rç| j rgx3 t | j ƒ D] \ } } | j | | 7_ qAWn  | j d | d | d | d | ƒ } | rç| j j ƒ  } | d IJ| d I| IJ| d IJ| d IJ| j j d | ƒ qçn  | d | j | ƒ IJ| j | ƒ | d	 8} | } qÆW| j | ƒ | S(   s
  
  Linear interpolation with energy minimization.  The number of minimizer
  cycles should be kept low to prevent each "frame" from snapping back to the
  starting coordinates.  Interpolating the dihedral restraint target angles
  may help remediate this problem.
  iÿÿÿÿ(   t   geometry_minimization(   t   geometry_restraintsNs&   Coordinate size mismatch: %d versus %dt   defaultt   dihedralt   max_iterationsi   i   R^   t   angle_ideali    t   weightgš™™™™™¹?s   Interpolation step %dt    g      ð?R›   RN   t   geometry_restraints_flagst   lbfgs_termination_paramss"   Number of minimization iterations:s    Energies at end of minimization:Rf   s   RMS coordinate change: %.3f(    t   mmtbx.refinementR¤   t   cctbxR¥   t   scitbx.lbfgsRM   Rx   R9   t   exclude_dihedralst   flagsR>   t   lbfgst   termination_parameterst   n_min_stepst   dihedral_proxiesRO   R2   t   i_seqsR§   t   angle_modelRª   R%   Rœ   R—   t   minimizeRV   R©   t	   minimizert   itert   final_target_resultt   showt   rms_difference(   R   R   R   R   R   R   t   verboseR   R¤   R¥   t   scitbxt   grmt   include_dihedralst	   grm_flagst   term_paramst   dihedral_stepst   nt   proxyt   sites1t   sites2R‰   t
   dihe_startt   dihe_endt   deltat   mt   current_xyzt	   final_xyzt   new_xyzt   dxyzt	   minimizedt   n_iter(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/interpolate.pyRP   (  s†    	
	
			
				




c   
   	   C   s1  d d  l  } | d  k r$ t j } n  d } t |  ƒ d k sH d |  k r‘ t ƒ  } | j j t ƒ } | j	 d | ƒ | d | | j
 ƒ  f IJd  S| j j d |  d t d	 d
 d d ƒ } | j } | j ƒ  } t d | j j d ƒ }	 | d | j j IJ| j	 d |	 ƒ |	 j ƒ  t | d | d | j ƒd  S(   NiÿÿÿÿsA   mmtbx.interpolate model1.pdb model2.pdb [...modelX.pdb] [options]i    s   --helpR   s¶  
mmtbx.interpolate - simple morphing with energy minimization

Limitations:
  1. Any atoms not present in all structures will be deleted.
  2. Equivalent atoms must have identical chain IDs and residue numbers;
     however, residue names do not have to be the same.
  3. This method usually doesn't work very well on conformational changes that
     involve major rearrangements and/or refolding.

Usage:
  %s

Full parameter list:
  %s
t   argst   master_phil_stringt   pdb_file_defs   morph.pdb_filet   cif_file_defs   morph.cif_files   %s.effR#   s&   Writing effective parameters to %s.effR   (   t
   iotbx.philR)   R*   R+   R$   R   t   philt   parset   morph_params_strR½   t   getvaluet   process_command_line_with_filest   workt   extractRX   R%   RU   RY   Rh   R   (
   RÔ   R   R-   t	   usage_strt
   params_outt   master_philt   cmdlinet   working_philR   t   eff_out(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/interpolate.pyt   run|  s0     			
t   __main__i   (   t
   __future__R    t   libtbx.utilsR   R   t	   cStringIOR   RR   R*   RÛ   R)   R:   Rh   R>   R3   RL   t   objectR%   RP   Ræ   R¡   t   argv(    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/interpolate.pyt   <module>   s$   :cG	0M(