ó
±™xTc           @   sJ  d  d l  m Z d  d l Z d  d l Z d  d l 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 Z d Z d Z e j j e d e ƒZ d	 e Z e j j e d e ƒZ e Z d
 „  Z e j d „ Z d e j f d „  ƒ  YZ d d „ Z d „  Z  d „  Z! e" d k rFe d e j# d ƒ n  d S(   iÿÿÿÿ(   t   divisionN(   t   flex(   t   runtime_utils(   t   Sorrys^
  
phenix.fmodel: a tool to compute structure factors, Fmodel:

  Fmodel = scale * exp(AnisoScale) * (Fcalc + k_sol * exp(-b_sol*s^2/4) * Fmask)

  where:

  - Fmodel - total model structure factor (complex value)
  - AnisoScale = -ht*A(-1)*b_cart*A(-1)th/4
  - h - column vector with Miller indices
  - A - orthogonalization matrix
  - b_cart - anisotropic scale matrix
  - t and (-1) denotes transposition and inversion operations
  - scale - overall scale factor
  - Fcalc - structure factors calculated from atomic model
  - k_sol and b_sol - Flat Bulk solvent model parameters
  - Fmask - structure factors calculated from bulk solvent mask

Usage examples:

  1) phenix.fmodel model.pdb high_resolution=1.5

     will result in a file containing complete set of Fmodel = Fcalc computed
     from atomic model up to 1.5A resolution.

  2) phenix.fmodel model.pdb scale=2 k_sol=0.35 b_sol=50 b_cart="1 2 3 0 4 7" high_res=1.5 low_res=10

     will result in a file containing complete set of Fmodel computed using the
     above formula in resolution range 1.5-20.0A.

  3) phenix.fmodel model.pdb high_resolution=1.5 algorithm=direct

     is similar to "1)" but the Fcalc are computed using direct summation algorithm.

  4) phenix.fmodel model.pdb high_res=1.5 format=cns label=FOBS type=real r_free=0.1

     will result in CNS formatted file containing complete set of amplitudes of
     Fmodel = Fcalc computed up to 1.5A resolution, labelled as FOBS, and free-R
     flags with 10% of test reflections. This is a typical command to simulate Fobs.

  5) phenix.fmodel model.pdb high_res=1.5 scattering_table=neutron

     will result in a file containing complete set of Fmodel = Fcalc computed
     from atomic model up to 1.5A resolution using neutron scattering table.

  6) phenix.fmodel model.pdb parameters.txt

     will result in a structure factor file, where Fmodel were computed using
     parameters defined in parameters.txt file. The parameters.txt file can
     contain all or any subset of parameters listed below. Note, that each {
     must have a matching one }.

  7) phenix.fmodel model.pdb reflection_data.mtz

     will result in a file containing a set of Fmodel = Fcalc that will match
     the set of Miller indices of the data in reflection_data.mtz file.

  8) phenix.fmodel model.pdb reflection_data.mtz data_column_label="FOBS,SIGMA"

     similar to "7)", where the specific data array is selected.

  9) phenix.fmodel model.pdb reflection_data.mtz twin_law="l,-k,h" twin_fraction=0.3

     generates twin data set (real type) with given twin law and fraction.

See below for complete list of available parameters.
s8  fmodel
  .short_caption = F(model) options
  .expert_level = 1
  .style = auto_align box
{
  k_sol = 0.0
    .type = float
    .help = Bulk solvent k_sol values
    .short_caption=Bulk solvent K_sol value
  b_sol = 0.0
    .type = float
    .help = Bulk solvent b_sol values
    .short_caption=Bulk solvent B_sol value
  b_cart = 0 0 0 0 0 0
    .type = floats(6)
    .help = Anisotropic scale matrix
    .input_size = 200
    .short_caption = Anisotropic scale matrix
  scale = 1.0
    .type = float
    .help = Overall scale factor
}
structure_factors_accuracy
  .short_caption = Structure factors accuracy
  .style = auto_align box
{
  include scope mmtbx.f_model.sf_and_grads_accuracy_master_params
}
mask
  .short_caption = Bulk solvent mask
  .style = auto_align box
{
  include scope mmtbx.masks.mask_master_params
}
t   process_includessÈ  high_resolution = None
  .type = float
  .expert_level=1
  .style = noauto bold
low_resolution = None
  .type = float
  .expert_level=1
  .style = noauto
r_free_flags_fraction = None
  .type = float
  .expert_level=1
  .style = noauto
add_sigmas = False
  .type = bool
  .expert_level=1
  .help = Adds calculated Sigma(F) column to output file.
  .style = noauto
add_random_error_to_amplitudes_percent = None
  .type = float
  .short_caption = Add random error (percent)
  .style = noauto
scattering_table = wk1995  it1992  *n_gaussian  neutron
  .type = choice
  .help = Choices of scattering table for structure factors calculations.      n_gaussian is the standard set of X-ray scattering factors.
  .expert_level=1
  .style = noauto
pdb_file = None
  .type = path
  .multiple = True
  .optional = True
  .short_caption = Model file
  .style = bold noauto file_type:pdb input_file OnChange:update_output_file_name
reference_file = None
  .type = path
  .short_caption = Reference set
  .help = Reflections file containing Miller indices (h,k,l) to use in output     file.
  .style = noauto input_file file_type:mtz OnChange:update_reference_column_labels
data_column_label = None
  .type = str
  .short_caption = Reference file label
  .style = noauto renderer:draw_any_label_widget
%s
random_seed=None
  .type = int
  .help = Random seed
  .expert_level=2
twin_law = None
  .type = str
  .help = Optional twin law if we want to generate a twinned dataset
  .input_size = 120
  .style = noauto
twin_fraction = None
  .type = float
  .help = Twin fraction, ignored if twin_law is not specified
  .style = noauto
wavelength = None
  .type = float
  .input_size = 80
  .help = Wavelength, sets all atoms to anomalous
  .style = noauto
generate_fake_p1_symmetry = False
  .type = bool
  .short_caption = Generate fake symmetry if necessary
  .help = Allows use of PDB files without CRYST1 records as input.  The     crystal symmetry will be assumed to be a P1 box.
output
  .short_caption = Reflection output
  .expert_level=0
  .style = noauto
{
  format = *mtz cns
    .type = choice
    .short_caption = File format
  label = FMODEL
    .type = str
    .short_caption = Data label
    .input_size = 100
  type = real *complex
    .type = choice
    .short_caption = Output data type
    .help = Numeric type of output data.  'real' is amplitudes only,       'complex' is complete structure factors as complex numbers.
    .expert_level=1
    .style = bold
  obs_type = *amplitudes intensities
    .type = choice
    .help = Experimental observation type to output.  Certain restrictions       apply if intensities are selected.
    .expert_level = 2
  file_name = None
    .type = path
    .short_caption = Output file
    .style = bold noauto new_file
  include scope libtbx.phil.interface.tracking_params
}
anomalous_scatterers
  .short_caption = Anomalous sites
  .style = menu_item noauto
{
  group
    .optional = True
    .multiple = True
    .short_caption = Anomalous scatterer group
    .style = auto_align
  {
    selection = None
      .type = atom_selection
      .short_caption = Atom selection
      .input_size = 400
    f_prime = 0
      .type = float
      .short_caption = f'
    f_double_prime = 0
      .type = float
      .short_caption = f''
  }
}
c         C   s›   | j  ƒ  } xˆ | D]€ } |  j ƒ  j d | j ƒ j ƒ  } | j ƒ  d k rb t d | j ƒ ‚ n  x. | D]& } | j | | _ | j | | _	 qi Wq Wd  S(   Nt   stringi    s@   Empty selection: selection string '%s' does not select any atom.(
   t
   scattererst   atom_selection_cachet	   selectiont
   iselectiont   sizeR   t   f_primet   fpt   f_double_primet   fdp(   t   pdb_hierarchyt   xray_structuret   anomalous_scatterer_groupsR   t   groupR	   t   i_seq(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/fmodel.pyt#   set_fp_fdp_for_anomalous_scatterersö   s    c         C   sî  | t  IJg  } xˆ |  D]€ } t j j | ƒ r y t j j d | ƒ } Wn$ t k
 r^ ‚  q— t k
 rn q— Xt	 | j
 ƒ d k r— | j | ƒ q— q q Wt	 | ƒ d k rt j d | ƒ } | j ƒ  } t	 | j ƒ d k ró |  j | j ƒ n  | j d  k	 r|  j | j ƒ qn  t j j d |  d | d t ƒ } t j j d | j ƒ } | j d | ƒ | d	 d
 IJ| d IJ| j j d | d d ƒ | j j ƒ  } | j d  k	 rÓt j | j ƒ t j  | j ƒ n  | j }	 t	 |	 ƒ d k rú| j }	 n  t j j d |	 ƒ } | j d | ƒ t	 | j! ƒ d k rCt" d ƒ ‚ n  | d	 d
 IJ| d Id j# | j ƒ IJt j j$ d d  d t j% | j& ƒ ƒ }
 d  } t	 | j' ƒ d k r¾t" d ƒ ‚ n  t	 | j' ƒ d k rC| d	 d
 IJ| d Id j# | j( ƒ IJ| j) | j* g j+ d  ƒ d k r1t" d d ƒ ‚ n  | j' d j, ƒ  } t j- g  | D] } | j. ƒ  j/ ƒ  ^ qQƒ } | j0 | d ƒ r’| d } nj g  } xa | D]Y } | j1 d  k	 rÙ| j2 ƒ  j3 ƒ  | j1 k rÙ| } Pn  | j d j# | j2 ƒ  j4 ƒ ƒ qŸW| d  k r*t" d d j# | ƒ d f ƒ ‚ qC| j5 d | d d ƒ n  | d  k	 r‚| j6 ƒ  j7 d t j8 | j. ƒ  j/ ƒ  d ƒ ƒ } n  |
 j9 ƒ  } | d  k rá| d  k	 rá| j: ƒ  } | d  k	 rá| j; rát" d d  d! ƒ ‚ qán  | j; sN| d  k rt" d" d# d! ƒ ‚ qN| j< ƒ  | j= ƒ  g j+ d  ƒ d k rNt" d$ d% d& ƒ ‚ qNn  |
 j> ƒ  } | j? d' | ƒ } | d  k r‡| j: ƒ  } n  | d  k	 r½| j: ƒ  d  k r½| j7 d' | ƒ } q½n  | j@ d | d d ƒ t	 | jA jB ƒ d k r#| jC ƒ  } | jD ƒ  tE d( | d) | d* | jA jB ƒ n^ | jF d  k	 r�| jG d+ k rTt" d, d- ƒ ‚ n  | d. | jF IJ| jH d/ | jF d0 d1 ƒ n  tI | ƒ | d	 d
 IJ| d2 IJ| jJ jK d3 k r¾d4 } n | jJ jK d5 k rÙd6 } n  | jJ jL } | d  k r=t j jM | j d ƒ } t	 | j ƒ d k r,| | } q=| d7 | } n  | | j) g j+ d  ƒ d k rjt" d8 ƒ ‚ n  t j jN d) | d9 | d: | jO d; | d< | jP d= | jQ d | ƒ jR d | d> | jJ jS ƒ | d? I| IJ| d@ IJ| d	 d
 IJ| S(A   Nt	   file_namei    t   sourcest   argst   logt   master_paramst
   file_namest   outt   -iO   s    
Parameters to compute Fmodel::
t   prefixt    s   Model file is not provided.s   
Input model file(s):t   source_infot   linesi   s)   Multiple reflection files found at input.s   Input reflection data:i   s5   high_resolution and low_resolution must be undefined s!   if reflection data file is given.t   ,s7   Multiple data available in input reflection file:
%s
%ss   
s5   Please select one using 'data_column_label=' keyword.t   fs     t   datas:   The input reference data already define crystal symmetry; s4   you may not use this in combination with the option s   generate_fake_p1_symmetry=True.s;   CRYST1 record in input PDB file is incomplete or missing.  sC   If you want the program to generate P1 symmetry automatically, set s:   CRYST1 record in input PDB file is incomplete or missing. s?   If you want the program to generate P1 symmetry automatically, s#   set generate_fake_p1_symmetry=True.t   crystal_symmetryR   R   R   t   neutrons4   Wavelength parameter not supported when the neutron s   scattering table is used.s2   Setting inelastic form factors for wavelength = %gt   photont   tablet   sasakis*   Computing model structure factors, Fmodel:t   cnss   .hklt   mtzs   .mtzt   _et_als6   Input data file or high_resolution has to be provided.t   f_obst
   add_sigmast   paramst   twin_lawt   twin_fractiont   obs_types   Output file name:s	   All done.(T   t   legendt   ost   patht   isfilet   iotbxt   philt   parset   KeyboardInterruptt   RuntimeErrort   lent   objectst   appendt(   fmodel_from_xray_structure_master_paramst   fetcht   extractt   pdb_filet   extendt   reference_filet   Nonet   mmtbxt   utilst   process_command_line_argst   pdbt   combine_unique_pdb_filest   pdb_file_namest   report_non_uniqueR.   t   showt   random_seedt   randomt   seedR   t   set_random_seedt   unique_file_namesR   t   joint   inputt
   std_stringt   raw_recordst   reflection_filest   reflection_file_namest   high_resolutiont   low_resolutiont   countt   as_miller_arrayst   intR#   R
   t   all_eqt   data_column_labelt   infot   label_stringt   labelst   show_comprehensive_summaryt
   map_to_asut   customized_copyt   doublet   crystal_symmetry_from_cryst1R$   t   generate_fake_p1_symmetryt	   unit_cellt   space_group_infot   construct_hierarchyt   extract_xray_structuret   show_summaryt   anomalous_scatterersR   t   atomst   reset_i_seqR   t
   wavelengtht   scattering_tablet   set_inelastic_form_factorst   validate_params_command_linet   outputt   formatR   t   basenamet   fmodel_from_xray_structureR-   R/   R0   t   write_to_fileR1   (   R   R   R   t   argt	   file_philt   cmdline_philR.   t   processed_argst   pdb_combinedRJ   t   pdb_inpt   miller_arrayt   miller_arrayst   mat
   data_sizest
   all_labelst   cryst1R   R   t	   pdb_atomst	   extensiont   ofn(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/fmodel.pyt   run  s   
			
	
!. #$	'		

		


 	 	 			
t   launcherc           B   s   e  Z d  „  Z RS(   c         C   s   t  d t |  j ƒ d t j ƒ S(   NR   R   (   Rˆ   t   listR   t   syst   stdout(   t   self(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/fmodel.pyRˆ   Ÿ  s    (   t   __name__t
   __module__Rˆ   (    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/fmodel.pyR‰   ž  s   c         C   sï   t  |  j ƒ d k r( t d d ƒ ‚ n  |  j d  k rX |  j d  k rÀ t d ƒ ‚ qÀ nh |  j d  k	 rÀ |  j d  k r‰ t d d ƒ ‚ qÀ |  j |  j g j d  ƒ d k rÀ t d d	 ƒ ‚ qÀ n  |  j	 j
 d  k rá t d
 ƒ ‚ n  t |  ƒ d  S(   Ni    s4   You must provide at least one model file to use for s   F(model) calculations.s(   Please specify a high-resolution cutoff.s5   Please select a column label to use in the reference s
   data file.i   s5   High resolution and low resolution must be undefined s!   if reflection data file is given.s   Please specify an output file.(   R;   RA   R   RX   RD   RC   R^   RY   RZ   Rt   R   Rs   (   R.   t   callback(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/fmodel.pyt   validate_params¢  s     !c         C   s  |  j  j d k r* |  j r* t d ƒ ‚ n  |  j d  k	 rp |  j d  k	 rp |  j |  j k  rp t d d ƒ ‚ qp n  |  j  j d k r|  j  j d k r§ t d d ƒ ‚ n  |  j  j j	 ƒ  j
 d ƒ sß t d	 d
 |  j  j ƒ ‚ n  |  j  j d k rt d ƒ ‚ qn  t S(   Nt   complexs;   Sigma values only supported when the output type is 'real'.s3   Low-resolution cutoff must be larger than the high-s   resolution cutoff.t   intensitiess6   Output type must be 'real' when intensities specified s   for obs_type.t   Is1   Output label must start with 'I' (any case) when s-   intensities specified for obs_type (was: %s).R*   s7   Output format must be 'mtz' when intensities specified.(   Rt   t   typeR-   R   RY   RD   RX   R1   t   labelt   uppert
   startswithRu   t   True(   R.   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/fmodel.pyRs   ´  s"    c         C   sP   g  } |  d  k	 rF t j j |  ƒ rF | j t j j |  ƒ d f ƒ n  | g  f S(   Ns   MTZ file(   RD   R3   R4   R5   R=   t   abspath(   t   resultt   output_files(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/fmodel.pyt
   finish_jobÇ  s    "t   __main__R   i   ($   t
   __future__R    R‹   R3   t   mmtbx.utilsRE   t
   iotbx.philR6   t	   iotbx.pdbt   scitbx.array_familyR   t   libtbxR   t   libtbx.utilsR   RN   R2   t%   fmodel_from_xray_structure_params_strR7   R8   R™   t!   fmodel_from_xray_structure_paramst,   fmodel_from_xray_structure_master_params_strR>   t   master_philR   RŒ   Rˆ   t   target_with_save_resultR‰   RD   R‘   Rs   R�   RŽ   t   argv(    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/fmodel.pyt   <module>   s0   D%	z
		š		