ó
Î™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 m	 Z	 d  d l
 m Z d  d l Z d  d l Z e j j d d	 e ƒZ d
 e j d „ Z e j d „ Z d d e j d „ Z d „  Z e d k re e j d ƒ n  d S(   iÿÿÿÿ(   t   divisionN(   t   pdb(   t   reflection_file_reader(   t   file_reader(   t   base_pairing(   t   individual_sitess·  
flip_base {
  pdb_file = None
    .type = path
    .help = '''input PDB file'''
  reflection_file = None
    .type = path
    .help = '''Reflection file'''
  out_pdb_file = None
    .type = str
    .help = '''input PDB file'''
  chain = None
    .type = str
    .help = '''Chain of the residue that is to be flipped'''
  alt_loc = None
    .type = str
    .help = '''Alternate location of the residue that is to be flipped'''
  res_num = None
    .type = int
    .help = '''Residue number of the residue that is to be flipped'''
  n_refine_cycles = 3
    .type = int
    .help = '''Number of real-space refinement cycles'''
  help = False
    .type = bool
    .help = '''Show help message'''
}
t   process_includest    c         C   s>   d } |  d k r+ d d d |  | } n  | GHt  j ƒ  d  S(   Ns  
******************************************************************************
Usage :
  python.phenix flipbase.py xxxx.mtz yyyy.pdb chain=A res_num=1

Will flip base of chain A residue 1 of yyyy.pdb and do a real-space
refinement using xxxx.mtz.

Required :
  pdb_file           input PDB file
  reflection_file    Reflection file
  chain              Chain of the residue that is to be flipped
  res_num            Residue number of the residue that is to be flipped

Options :
  out_pdb_file       input PDB file
  alt_loc            Alternate location of the residue that is to be flipped
  n_refine_cycles    Number of real-space refinement cycles
  help               Show help message
******************************************************************************

R   t   *iO   s   

!!!!!  %s  !!!!!
(   t   syst   exit(   t   msgt   logt   s(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/flipbase.pyt   usage(   s
     c         C   sX   t  j d |  ƒ j ƒ  } | d } | j d d ƒ } | j ƒ  | d IJ| j ƒ  } | S(   Nt	   file_namei    t   resolution_factorg      Ð?s   
Using sigma scaled map.
(   R   t   any_reflection_filet   as_miller_arrayst   fft_mapt   apply_sigma_scalingt   real_map_unpadded(   t   reflection_file_nameR   t   miller_arrayst   maR   t
   target_map(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/flipbase.pyt   get_target_mapC   s    	


i   c	         C   sÎ  | j  ƒ  }	 t }
 x�|  j ƒ  D]�} | j j ƒ  | k r@ q n  xk| j ƒ  D]]} | j ƒ  | k rk qM n  | j ƒ  r— | r— d } t | d ƒ ‚ n  x| j	 ƒ  D]} | rË | | j
 j ƒ  k rË q¤ n  t j | d d ƒ|	 j | j ƒ  j ƒ  | j ƒ  j ƒ  ƒ | j |	 ƒ } | j ƒ  j ƒ  } | d j d d ƒ IJxP t | ƒ D]B } | d | d	 IJt j d
 | d | d |  d | d | ƒ }
 qJW| d j d d ƒ IJq¤ WqM Wq W|
 sÇt d ƒ ‚ n  |
 j S(   Ns>   Specified residue has alternate conformations. Please specify s   alt_loc on the command linet   anglei´   s   real-space refinement BEGINiO   R   s!   real-space refinement cycle %i...i   t   map_datat   xray_structuret   pdb_hierarchyt   geometry_restraints_managert	   selections   real-space refinement FINISHEDs   Specified residue not found(   t
   sites_cartt   Falset   chainst   idt   stript   residue_groupst   resseq_as_intt   have_conformerst   RuntimeErrort   atom_groupst   altlocR   t	   flip_baset   set_selectedt   atomst   extract_i_seqt   extract_xyzt   replace_sites_cartt   centert   rangeR   t   easyR   (   R   R   R   R   t   chaint   res_numt   alt_loct   n_refine_cyclesR   R!   t   erot   cht   rgR   t   residuet   selet   i(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/flipbase.pyt   flip_and_refineM   s@    	  	" c         C   s  t  j j j d t ƒ } g  } xÑ |  D]É } t j j | ƒ rÏ t j	 | ƒ } | j
 d k r{ | j | j d d | ƒ ƒ n  | j
 d k rª | j | j d d | ƒ ƒ qî | j
 d k rî | j | j ƒ qî q% | j d | ƒ } | j | ƒ q% Wt j d | ƒ } | j ƒ  } | j } | j j d  k r8t d	 ƒ n  | j j d  k rWt d
 ƒ n  | j j d  k rvt d ƒ n  | j j d  k r•t d ƒ n  | j j d  k rÎ| j j j d d ƒ }	 |	 | j _ n  | j } | j rôt ƒ  t j ƒ  n  | j }
 | j } t j } | d IJt | | ƒ } t j j  d t! ƒ j" |
 g ƒ d } t j# j$ d | j% d t! ƒ d t& ƒ } | j' j( } | j) ƒ  j* ƒ  | j+ d t! ƒ } t, | | d | d | d | j d | j d | j- d | j. d | ƒ} | j/ | j ƒ | d | j IJd  S(   Nt   master_philR   t   args   pdb_file="%s"t   hkls   reflection_file="%s"t   philt   sourcess   PDB file not provided!s   Reflection file not provided!s   chain not provided!s   res_num file not provided!s   .pdbs   _baseflip.pdbs   
gettinsg target_map...
R   i    t   geometryt   show_energiest   normalizationt   show_summaryR   R   R5   R6   R7   R8   s   
Out written to %s(0   t   libtbxRC   t   command_linet   argument_interpreterR@   t   ost   patht   isfileR   t   any_filet	   file_typet   appendt   processt   file_objectt   fetcht   extractR,   t   pdb_filet   NoneR   t   reflection_fileR5   R6   t   out_pdb_filet   replacet   helpR	   R
   t   stdoutR   t   mmtbxt   utilst   process_pdb_file_srvR"   t   process_pdb_filest
   restraintst   managerR   t   Truet   all_chain_proxiesR   R.   t   reset_i_seqR   R?   R7   R8   t   write_pdb_file(   t   argst   interpreterRD   RA   t
   input_filet   arg_philt	   work_philt   work_paramst   paramst   fnt   pdb_file_nameR   R   R   t   ppft   grmR   R   t   flip_hierarchy(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/flipbase.pyt   runu   sn      						
						t   __main__i   (   t
   __future__R    RL   R	   t   iotbxR   R   R   t   mmtbx.secondary_structureR   t   mmtbx.refinement.real_spaceR   R]   t   libtbx.phil.command_lineRI   RC   t   parseRc   R@   t   stderrR   R   RW   R\   R?   Rs   t   __name__t   argv(    (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/real_space/flipbase.pyt   <module>   s$   	 	A