ó
±™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 Z d  d l	 m
 Z
 d  d l m Z d  d l m Z d  d l Z d  d l Z d Z d	 Z d
 „  Z d „  Z e j d „ Z d „  Z d „  Z d e
 j f d „  ƒ  YZ e d k re e j d ƒ n  d S(   iÿÿÿÿ(   t   divisionN(   t   reflection_file_utils(   t   file_reader(   t   runtime_utils(   t   Sorry(   t   StringIOs	  
phenix.reciprocal_space_arrays:
compute various arrays such as Fcalc, Fmask, Fmodel, Fbulk, and more.

Inputs:
  - File with reflection data (Fobs or Iobs), R-free flags, and optionally HL
    coefficients. It can be in most of known formats and spread across
    multiple files;
  - label(s) selecting which reflection data arrays should be used (in case
    there are multiple choices in input file, there is no need to provide labels
    otherwise);
  - Model file (PDB or mmCIF) with input model.

Usage examples:
  1. phenix.reciprocal_space_arrays model.pdb data.hkl f_obs_label="IOBS"
  2. phenix.reciprocal_space_arrays model.pdb data.hkl r_free_flags_label="FREE"

Output:
  MTZ file with data arrays. Depending on the input data, the following arrays
  may be present:

  - FOBS         : data from input reflection file
  - SIGFOBS      : corresponding sigmas
  - R_FREE_FLAGS : R-free flags (0 - work, 1 - test)
  - FMODEL       : total model structure factor. See phenix.fmodel for details
  - PHIFMODEL    : corresponding phases
  - FCALC        : Fcalc from atomic model
  - PHIFCALC     : corresponding phases
  - FMASK        : Fmask from bulk-solvent mask. See phenix.fmodel for details
  - PHIFMASK     : corresponding phases
  - FBULK        : bulk-solvent contribution. See phenix.fmodel for details
  - PHIFBULK     : corresponding phases
  - FB_CART      : overall anisotropic scale factor
  - FOM          : figures of merit
  - ALPHA        : ML parameter used in m&D calculation for 2mFo-DFc maps
  - BETA         : ML parameter used in m&D calculation for 2mFo-DFc maps
  - HLA          : HL coefficients from from input reflection file
  - HLB          : HL coefficients from from input reflection file
  - HLC          : HL coefficients from from input reflection file
  - HLD          : HL coefficients from from input reflection file
  - HLmodelA     : HL coefficients from the model (C=D=0)
  - HLmodelB     : HL coefficients from the model (C=D=0)
  - HLmodelC     : HL coefficients from the model (C=D=0)
  - HLmodelD     : HL coefficients from the model (C=D=0)
  - HLcombA      : combined HL: model + input
  - HLcombB      : combined HL: model + input
  - HLcombC      : combined HL: model + input
  - HLcombD      : combined HL: model + input
  - RESOLUTION   : resolution per reflection
sË  hkl_file = None
  .type = path
  .short_caption = Experimental data
  .style = bold file_type:hkl input_file process_hkl child:fobs:f_obs_label     child:rfree:r_free_flags_label child:space_group:space_group     child:unit_cell:unit_cell     child:hl_coeffs:hendrickson_lattman_coefficients_label
pdb_file = None
  .type = path
  .short_caption = Model file
  .style = bold file_type:pdb input_file
f_obs_label = None
  .type = str
  .short_caption = Data labels
  .input_size = 160
  .style = bold renderer:draw_fobs_label_widget
r_free_flags_label = None
  .type = str
  .short_caption = R-free flags
  .input_size = 160
  .style = bold renderer:draw_rfree_label_widget
remove_f_obs_outliers = True
  .type = bool
  .short_caption = Remove F-obs outliers
bulk_solvent_and_scaling = True
  .type = bool
  .short_caption = Bulk solvent correction and scaling
hendrickson_lattman_coefficients_label = None
  .type = str
  .short_caption = Hendrickson-Lattman coefficients
  .input_size = 160
  .style = renderer:draw_hl_label_widget
output_file_name = None
  .type = path
  .style = bold new_file
space_group = None
  .type = space_group
unit_cell = None
  .type = unit_cell
include scope libtbx.phil.interface.tracking_params
c         C   s@   |  d IJt  j j t d t ƒ} | j d d d |  ƒ |  J| S(   Ns   Default params::
t   process_includest   prefixs     t   out(   t   iotbxt   philt   parset   master_params_strt   Truet   show(   t   logt   parsed(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/reciprocal_space_arrays.pyt   defaultsm   s
    
c         C   su   |  d  k	 rm | j ƒ  s6 |  j ƒ  rQ |  j ƒ  }  qQ n |  j ƒ  sQ |  j ƒ  }  n  |  j ƒ  j d | d d ƒ Sd  Sd  S(   Nt   othert   data_substitutei    (   i    i    i    i    (   t   Nonet   anomalous_flagt   average_bijvoet_matest   generate_bijvoet_matest
   map_to_asut   matching_set(   t   experimental_phasest   f_obs(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/reciprocal_space_arrays.pyt   extract_experimental_phasest   s    c         C   s?  t  |  ƒ d k r- | t IJt d | ƒ d  St d | ƒ } t j j d |  d t j d | ƒ } | j j	 ƒ  } | j
 } t  | ƒ d k rÜ | j d  k r¨ t d ƒ ‚ qÜ t j | j d d ƒ} | j d ƒ | j g } n  | j } | d  k rO| j d  k	 r@| j d  k	 r@d d	 l m } | j d
 | j d | j ƒ } qOt d ƒ ‚ n  t  | j ƒ d k r‘| j d  k r‚t d ƒ ‚ qš| j g }	 n	 | j }	 t j d | d t d | d t ƒ  ƒ }
 t j j ƒ  j	 ƒ  } | j | _  | j! | j" _# t j j$ d |
 d | d t d t ƒ  ƒ } | j% } d GHd G| j& ƒ  j  GH| j" } d G| j& ƒ  j  GHt j j' j	 ƒ  } | j( | _  t j j) d |
 d | d t ƒ  d d d d  d t d t ƒ } | d  k	 rÑd G| j& ƒ  j  GHn  t* d | d | ƒ } | d  k r| j+ d  t, j- | j. ƒ  j/ ƒ  t0 ƒ ƒ } n  t j j d! |	 d | d t j ƒ } t  | j1 j2 ƒ  ƒ d" k rmt d# ƒ ‚ n  | j1 j3 d | d$ t ƒ } | j ƒ  j4 | j ƒ  ƒ s¶t d% d& ƒ ‚ n  d' GHd( G| j5 ƒ  j/ ƒ  GHt j6 j7 d) | d* | d | d+ | ƒ } | j8 d, t d- | j9 d. | j: ƒ d/ GH| j& ƒ  j; ƒ  d0 GH| j< d  k	 rN| j< } n¤ t= j> j? |	 d ƒ } t= j> j? | d j@ ƒ  ƒ } y | | jA d1 ƒ  } Wn tB k
 r°| } n Xy | | jA d1 ƒ  } Wn tB k
 rá| } n Xd2 | | f } d3 G| GHd4 | GHd5 | GHtC | d6 ƒ } | jD d7 | ƒ | jE ƒ  d8 GH| S(9   Ni    R   t   argst   master_paramss   No reflection file found.t
   force_typet   hkliÿÿÿÿ(   t   crystalt   space_group_infot	   unit_cells   No crystal symmetry found.s   No model file found.t   crystal_symmetryt   force_symmetryt   reflection_filest   errt   reflection_file_servert
   parameterst
   keep_goings   Input data:s     Iobs or Fobs:s     Free-R flags:t   parameter_scopet    t   working_point_groupt   symmetry_safety_checkt   ignore_all_zeross     HL coefficients:R   R   t   datat   pdb_file_namesi   s/   Multiple model file not supported in this tool.t   weak_symmetrys:   The unit cells in the model and reflections files are not s   isomorphous.s   Input model:s     number of atoms:t   xray_structuret   r_free_flagst   abcdt   update_f_part1t   remove_outlierst   bulk_solvent_and_scalings   Overall statistics:s   Output data:t   .s	   %s_%s.mtzs     file name:s     to see the contnt of %s:s       phenix.mtz.dump %st   wR   s	   All done.(F   t   lent   legendR   t   mmtbxt   utilst   process_command_line_argst   syst   stdoutt   paramst   extractR&   t   hkl_fileR   R   R   t   any_filet   assert_file_typet   file_objectR$   t   space_groupR#   t   cctbxR!   t   symmetryR1   t   pdb_fileR   R(   R   R   t   data_and_flags_master_paramst   f_obs_labelt   labelst   r_free_flags_labelR4   t   labelt   determine_data_and_flagsR   t   infot   experimental_phases_paramst&   hendrickson_lattman_coefficients_labelt   determine_experimental_phasesR   t   arrayt   flext   boolR0   t   sizet   Falset   pdb_inpt   xray_structures_simplet   xray_structure_simplet   is_similar_tot
   scattererst   f_modelt   managert   update_all_scalest   remove_f_obs_outliersR8   t   show_allt   output_file_namet   ost   patht   basenamet	   file_namet   indext
   ValueErrort   opent   exportt   close(   R   R   R   t   processed_argsRB   R&   t   hkl_inR$   R!   R1   t   rfsR)   t   determine_data_and_flags_resultR   R4   t   experimental_phases_resultR   t   mmtbx_pdb_fileR3   t   fmodelRe   t   pdb_file_bnt   hkl_file_bnt   pdb_file_prefixt   hkl_file_prefixR   (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/reciprocal_space_arrays.pyt   run   sÚ    
										-				
  
 
			
c         C   sÏ   |  j  d  k r t d ƒ ‚ n­ |  j d  k r< t d ƒ ‚ n� |  j d  k rZ t d ƒ ‚ nq |  j d  k rx t d ƒ ‚ nS |  j d  k rž t d d d ƒ ‚ n- |  j d  k s¼ |  j d  k rË t d ƒ ‚ n  t	 S(	   Ns   No reflections file provided.s   No model file provided.s   No output file name provided.s   No data label selected.s>   No R-free flags label selected.  This program requires R-free s?   flags to run and will not generate them automatically; use the s5   reflection file editor to add them to your data file.s+   Missing or incomplete symmetry information.(
   RD   R   R   RK   Re   RM   RO   RH   R#   R   (   RB   (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/reciprocal_space_arrays.pyt   validate_params÷   s    c         C   sK   g  g  } } |  d  k	 rA t j j |  ƒ rA | j |  d f ƒ n  | | f S(   Ns   Data and phases(   R   Rf   Rg   t   isfilet   append(   t   resultt   output_filest   stats(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/reciprocal_space_arrays.pyt
   finish_job  s    t   launcherc           B   s   e  Z d  „  Z RS(   c         C   s   t  d |  j d t j ƒ S(   NR   R   (   Rz   R   R@   RA   (   t   self(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/reciprocal_space_arrays.pyRz     s    (   t   __name__t
   __module__Rz   (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/reciprocal_space_arrays.pyR‚     s   t   __main__i   (   t
   __future__R    t   mmtbx.utilsR=   t   mmtbx.f_modelR	   R   R   t
   iotbx.philt   libtbxR   t   libtbx.utilsR   t	   cStringIOR   R@   Rf   R<   R   R   R   RA   Rz   R{   R�   t   target_with_save_resultR‚   R„   t   argv(    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/reciprocal_space_arrays.pyt   <module>   s&   4+		x		