ó
¦™xTc           @   sQ  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 m	 Z	 d  d l
 m Z m 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 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 ƒ Z d „  Z  d d „ Z! d S(   iÿÿÿÿ(   t   division(   t   crystal(   t   sgtbx(   t   xray(   t   utils(   t   flex(   t   Sorryt	   multi_outN(   t   reflection_file_utils(   t   crystal_symmetry_from_any(   t   xray_structure(   t   pdb(   t   twin_analyses(   t   f_model(   t   twin_f_model(   t   StringIOsm  twin_utils{
  input{
    unit_cell=None
    .type=unit_cell
    space_group=None
    .type=space_group
    xray_data{
      file_name=None
      .type=path
      obs_labels=None
      .type=str
      free_flag=None
      .type=str
    }
    model{
      file_name=None
      .type=path
    }
  }
  parameters{
    twinning{
      twin_law=None
      .type=str
      max_delta=3.0
      .type=float
      detwin_mode=*algebraic proportional
      .type=choice
    }
  }
  output{
    logfile=twin_tools.log
    .type=str
    map_coeffs_root=MAP_COEFFS
    .type=str
    obs_and_calc="obs_and_calc.mtz"
    .type=str
  }
}
c         C   s    d t  |  ƒ } d t ƒ  GHd  S(   Nt   -sd  
%(command_name)s
%(command_under)s

A command line utility to compute map coefficients for twinned data.
Bulk solvent parameters and twin fractions are determined automatically.
If no twin law is specified, map coefficents for all twin laws will
be computed.

The keywords are sumarized below:

twin_utils{
  input{
    unit_cell=None
    space_group=None
    xray_data{
      file_name=None
      obs_labels=None
      free_flag=None
      }
    model{
      file_name=None
      }
  }
  parameters{
    twinning{
      twin_law=None
      max_delta=3.0
    }
  }
  output{
    logfile=twin_tools.log
    map_coeffs_root=MAP_COEFFS
  }
}

A typical run looks like this:

%(command_name)s data.file=mydata.mtz model.file=mymodel.pdb \
  obs_labels=FP,SIGFP free_flag=TEST

If no unit cell is specified, the unit cell of the reflection
file or the model will be used.
(   t   lent   vars(   t   command_namet   command_under(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/twinning/twin_map_utils.pyt
   print_help=   s    ,s   phenix.twin_map_utilsc   '      C   sB
  t  j } d  } t |  ƒ d k sE d |  k sE d |  k sE d |  k rU t d | ƒ né	t ƒ  } d |  k r† | j d d d	 t  j ƒ n  t ƒ  } t ƒ  } | j d d
 d	 | ƒ g  } t j	 d d ƒ } x1|  D])} d  }	 t
 }
 t j j | ƒ rVy; t j j d | ƒ }	 |	 d  k	 r+| j |	 ƒ t }
 n  Wqµt k
 rB‚  qµt k
 rRqµXn_ y8 | j d | ƒ }	 |	 d  k	 r�| j |	 ƒ t }
 n  Wn$ t k
 r¤‚  n t k
 r´n X|
 sÍ | d IJ| d I| IJ| d IJ| Jt d | ƒ ‚ qÍ qÍ Wt j d | ƒ } | j ƒ  } t j d | j j j j ƒ } t j d | j j j j ƒ } d  } | j j j  | j j j! g j" d  ƒ d k  r´t# j$ d | j j j  d | j j j! ƒ } n  t# j% d  | | g | g ƒ } | j  ƒ  | j j _  t& j' d | j! ƒ  ƒ | j j _! t j( d | ƒ } | j) d | ƒ | j j j  d  k rNt d ƒ ‚ n  | j j j! d  k rrt d ƒ ‚ n  | j j j j d  k r™t d ƒ ‚ n  | j j j j d  k rÀt d ƒ ‚ n  t# j$ d | j j j  d | j j j! ƒ } t* j+ d | d t d g  ƒ } d  } d  } t, j- ƒ  j ƒ  } | j j j j | _ | j j j j. | _/ | j j j j | j0 _ | j j j j1 | j0 _2 t, j3 d  | d! | d" | ƒ } | j4 ƒ  } | j5 ƒ  rÅ| j6 ƒ  } n  | j7 ƒ  s×t8 ‚ | j9 d# | ƒ } | J| d$ IJ| j: d# t; j< | j= ƒ  d% k ƒ ƒ } | d  k r?| j> d& t ƒ } n  | j? ƒ  d  k	 sWt8 ‚ | J| d' IJ| d( IJ| j@ d) | ƒ | J| jA ƒ  jB ƒ  d k rªt d* ƒ ‚ n  tC j d | j j j j ƒ jD d | ƒ } | d+ IJ| d, IJ| j@ d) | ƒ | JtE jF | d- | j jG jH jI d | ƒ} | J| d. IJ| d/ IJ| j) d | ƒ | J| d0 IJ| d1 IJg  } d } | j jG jH jJ d  k	 rÏt& jK | j jG jH jJ ƒ } tE jJ | d  d  d  d  d  ƒ } | g | _L n  xì| jL D]á} | d% 7} t& jM | jN ƒ jO ƒ  } tP jQ d2 | d3 | d4 | d5 | jN d6 | j jG jH jR d | ƒ } | d7 IJ| jS d8 t
 ƒ | jT ƒ  | jU ƒ  | jV ƒ  | jW ƒ  | jX d9 d: ƒ } | jX d9 d; ƒ } | jX d9 d< ƒ }  | jY d= d> ƒ }! |! jZ d? | d= d@ ƒ }! |! jZ d? |  d= dA ƒ }! | j j[ j\ dB t] | ƒ dC }" | J| dD |" | f IJ| J|! j^ ƒ  j_ d |" ƒ | j j[ j` d  k	 rÙ| jY d= dE ƒ }! |! jZ d? | ja ƒ  d= dF ƒ }! | j j[ j` }" |! j^ ƒ  j_ d |" ƒ qÙqÙWt | jL ƒ d k r 
| J| dG IJ| dH IJta jb d2 | d3 | d4 | ƒ }# |# jS d | ƒ |# jX d9 dI ƒ }$ |# jX d9 dJ ƒ }% |$ jY d= d> ƒ }! |! jZ d? |% d= dK ƒ }! | j j[ j\ dL }" |! j^ ƒ  j_ d |" ƒ | j j[ j` d  k	 r 
| jY d= dE ƒ }! |! jZ d? |# ja ƒ  d= dF ƒ }! | j j[ j` }" |! j^ ƒ  j_ d |" ƒ q 
n  | J| J| dM IJtc | j j[ jd dN ƒ }& |& | je ƒ  IJ| Jd  S(O   Ni    s   --helps   --hs   -hR   s   --quiett   labelt   stdoutt   file_objectt
   log_buffert
   home_scopet	   map_coefst	   file_namet   args2   ##----------------------------------------------##s   ## Unknown file or keyword:s   Unknown file or keyword: %st   sourcesi   t	   unit_cellt   space_group_infot   groupt   python_objectt   outs   unit cell not specifieds   space group not specifieds   Xray data not specifieds"   pdb file with  model not specifiedt   crystal_symmetryt   force_symmetryt   reflection_filest   reflection_file_servert
   parameterst   logt   datas"   Attempting to extract Free R flagsi   t   use_lattice_symmetrys   Summary info of observed datas   =============================t   fs   No data availables   Atomic model summarys   ====================t   lattice_symmetry_max_deltas   Preliminary data analysess   ==========================sG   Overall and bulk solvent scale paranmeters and twin fraction estimationsG   =======================================================================t   f_obst   r_free_flagsR
   t   twin_lawt   detwin_modes   --- bulk solvent scaling ---t   update_f_part1t   map_types   mFo-DFcs   2mFo-DFct   gradientt   column_root_labelt   FWTt   miller_arrayt   DFWTt   GRADt   _s   .mtzs   Writing %s for twin law %st   FOBSt   FMODELs   No twin laws were founds8   Performing maximum likelihood based bulk solvent scalings   2mFobs-DFmodels   mFobs-DFmodelt   DELFWTs   _ML.mtzs
   All done 
t   w(f   t   sysR   t   NoneR   R   R   t   registerR   t   master_paramst!   command_line_argument_interpretert   Falset   ost   patht   isfilet   iotbxt   philt   parset   appendt   Truet   KeyboardInterruptt	   Exceptiont   processR   t   fetcht   extractR	   t   extract_fromt
   twin_utilst   inputt	   xray_dataR   t   modelR   t   space_groupt   countR   t   symmetryt   select_crystal_symmetryR   R    t   formatt   showR   R'   R   t   data_and_flags_master_paramst
   obs_labelst   labelsR/   t	   free_flagR   t   determine_data_and_flagst   extract_datat   is_xray_intensity_arrayt	   f_sq_as_ft   is_xray_amplitude_arrayt   AssertionErrort   extract_flagst   customized_copyR   t   boolR*   t   generate_r_free_flagst   observation_typet   show_summaryt   indicest   sizeR   t   xray_structure_simpleR   t	   twin_lawsR(   t   twinningt	   max_deltaR0   t   rt_mxt	   operatorst   change_of_basis_opt   operatort   as_hklR   t   twin_model_managerR1   t   update_solvent_and_scalet   r_valuest   targett   show_k_sol_b_sol_b_cart_targett   show_essentialt   map_coefficientst   as_mtz_datasett   add_miller_arrayt   outputt   map_coeffs_roott   strt
   mtz_objectt   writet   obs_and_calcR   t   managert   opent   logfilet   getvalue('   t   argsR   R)   t   paramst   string_buffert   string_buffer_plotst   phil_objectst   argument_interpreterR   t   command_line_paramst   arg_is_processedt   effective_paramst   hkl_xst   pdb_xst   phil_xst   combined_xst
   new_paramst   xray_data_serverR7   t
   free_flagst
   tmp_paramst
   tmp_objectRV   Rp   t   twin_modelst   operator_countt   tmp_lawR0   t   operator_hklt
   twin_modelt   wfofct   wtfofct   gradt   mtz_datasett   namet   f_model_objectt   tfofct   fofcR‰   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/twinning/twin_map_utils.pyt   runl   s�   	6					    

		!	!			
'

!	

	




			




						!				

										
("   t
   __future__R    t   cctbxR   R   R   t   mmtbxR   t   cctbx.array_familyR   t   libtbx.utilsR   R   t
   iotbx.philRH   R   R	   t	   iotbx.pdbR
   R   t   mmtbx.scalingR   R   t   mmtbx.twinningR   t	   cStringIOR   R?   RE   RI   RJ   RB   R   Rª   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/twinning/twin_map_utils.pyt   <module>   s*   	'		/