ó
±xTc        
   @   sč   d  Z  d d l m Z d d l m Z m Z d d l m Z m Z m	 Z	 m
 Z
 d d l m Z d d l m Z d d l Z d Z e e e e e e e e e d	 	 Z d
   Z d e f d     YZ e j d  Z d   Z d   Z d S(   sš  
Generic wrapper for bootstrapping high-level applications which rely on some
combination of model and data, with special attention to geometry restraints
interpretation and f_model setup.  All of the bookkeeping required to
disambiguate crystal symmetry and Miller array conventions is performed
automatically.  This is superficially similar to the setup for phenix.refine
(and re-uses many of the methods in mmtbx.utils), but is somewhat simpler
(single-dataset only) and more general-purpose.
i’’’’(   t   division(   t   make_headert   make_sub_header(   t   Sorryt   Usaget	   multi_outt   null_out(   t   Auto(   t   StringIONs°  
input {
  include scope mmtbx.utils.xray_data_str
  %(phases)s
  %(unmerged)s
  pdb {
    include scope mmtbx.utils.pdb_params
  }
  monomers {
    include scope mmtbx.utils.cif_params
  }
  sequence = None
    .type = path
  scattering_table = wk1995  it1992  *n_gaussian  neutron
    .type = choice
  wavelength = None
    .type = float
  energy = None
    .type = float
  %(phases_flag)s
  %(twin_law)s
}
%(pdb_interpretation)s
c
         C   si  d d l  }
 i d d 6d d 6d d 6d d 6d d 6} | rH d	 | d <n | r[ d
 | d <n  | rx d | d <d | d <n  | r d | d <n  | s | rą d } | d k	 r¶ d | } n  d | | d <| rą | d c d 7<qą n  t | } d | |  f } |	 r| d k st  | S|
 j j | d t } | d k	 re|
 j j d |  } | j d |  } n  | S(   sF  
  Generate a complete PHIL parameter block with generic input parameters plus
  user-specified options.  The result is suitable for input for the class
  load_model_and_data.  Depending on the target application, the exact input
  options can be adjusted.

  :param phil_string: application-specific parameters
  :param enable_twin_law: allow twinned f_model calculation
  :param enable_automatic_twin_detection: allow automatic detection of twinning
      and setup of the f_model object
  :param enable_experimental_phases: use Hendrickson-Lattman coefficients
  :param enable_pdb_interpretation_params: show options for modifying the
      behavior of mmtbx.monomer_library.pdb_interpretation
  :param enable_stop_for_unknowns: modify behavior of restraint interpretation
      when unknown atoms are encountered.  Default is None; if True, the
      program will not raise an error; if False, the program will raise an
      error which may be suppressed by the user
  :param enable_full_geometry_params: include parameters for specifying custom
      geometry resraints
  :param enable_unmerged_data: accept separate unmerged intensities
  :param enable_cdl: change default setting for conformation-dependent library
  :param as_phil_string: return parameter string instead of PHIL object
  :returns: PHIL object (unless as_phil_string=True)
  i’’’’Nt    t   phasest   unmergedt   phases_flagt   twin_lawt   pdb_interpretations7   
      skip_twin_detection = False
        .type = boolsD   
      twin_law = None
        .type = str
        .input_size = 100se   
      experimental_phases {
        include scope mmtbx.utils.experimental_phases_params_str
      }s   
      use_experimental_phases = Auto
        .type = bool
        .short_caption = Use experimental phases (Hendrickson-Lattman coefficients)
      sz   
      unmerged_data {
        file_name = None
          .type = path
        labels = None
          .type = str
      }s6   
        stop_for_unknowns = %s
          .type = bools{   
      pdb_interpretation {
        include scope mmtbx.monomer_library.pdb_interpretation.master_params
        %s
      }s¤  
        geometry_restraints
          .alias = refinement.geometry_restraints
        {
          edits
            .short_caption = Custom geometry restraints
          {
            include scope mmtbx.monomer_library.pdb_interpretation.geometry_restraints_edits_str
          }
          remove {
            include scope mmtbx.monomer_library.pdb_interpretation.geometry_restraints_remove_str
          }
        }s   
    %s
    %s
  t   process_includess   pdb_interpretation.cdl=%st   source(   t
   iotbx.philt   Nonet   cmdline_input_phil_base_strt   AssertionErrort   philt   parset   Truet   fetch(   t   phil_stringt   enable_twin_lawt   enable_automatic_twin_detectiont   enable_experimental_phasest    enable_pdb_interpretation_paramst   enable_stop_for_unknownst   enable_full_geometry_paramst   enable_unmerged_datat
   enable_cdlt   as_phil_stringt   iotbxt   phil_extra_dictt   stop_for_unknowns_paramst   cmdline_input_phil_strt   master_phil_strt   master_philt   wp(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/__init__.pyt    generate_master_phil_with_inputs/   sD    #



c           C   s   t  d d d t  S(   sG   
  Generate minimal PHIL input string with no additional parameters.
  R   R	   R   (   R*   R   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/__init__.pyt   generic_simple_input_phil   s    t   load_model_and_datac           B   s\   e  Z d  Z e j e e e e e e e e e e e d  Z d   Z	 d   Z
 e d  Z RS(   sÖ  
  Class for processing command-line input and creating necessary objects.
  The master_phil object should include cmdline_input_phil_str above, plus
  any application-specific parameters.  Programs which use this can be invoked
  using simple file arguments or explicit parameters, e.g.

    mmtbx.some_program model.pdb data.mtz

  This class performs the following functions (mostly using other wrappers
  elsewhere in mmtbx.utils):
    1. Process all arguments and extract as Python parameters
    2. Read in data and R-free flags
    3. Filter data and flags to be consistent if necessary
    4. Read in PDB file, either using the iotbx.pdb API, or if process_pdb_file
       is True, mmtbx.monomer_library.pdb_interpretation (using any CIF files
       included in the inputs)
    5. Extract the pdb_hierarchy and xray_structure objects.
    6. Create an mmtbx.f_model.manager object using the data, flags, and
       xray_structure.
  If at any point the inputs are ambiguous, hopefully the program will stop
  and raise an interpretable error.

  Parameters
  ----------
  args: list of command-line arguments
  master_phil: PHIL master (can optionally be an unparsed string)
  out: filehandle-like object
  process_pdb_file: run full restraints generation
  require_data: raise error if no experimental data supplied
  create_fmodel: setup mmtbx.f_model.manager object
  prefer_anomalous: preferentially use anomalous data if present
  force_non_anomalous: merge anomalous data if present
  set_wavelength_from_model_header: interpret PDB or mmCIF header to set     experimental wavelength
  set_inelastic_form_factors: table to use (if any) for setting anomalous     scattering form factors
  usage_string: console output for no arguments or --help
  create_log_buffer: store log output for later output to file
  remove_unknown_scatterers: delete atoms with scattering type 'X' (only     used when process_pdb_file=False)
  generate_input_phil: specifies that the master_phil object is a string     containing onky the app-specific options, and automatically add the     standard input parameters

  Attributes
  ----------
  args : list of str
  cif_file_names : libtbx.phil.scope_extract_list
  cif_objects : list of ...
  crystal_symmetry : cctbx.crystal.symmetry
  f_obs : cctbx.miller.array
  fmodel : mmtbx.f_model.manager
  geometry : cctbx.geometry_restraints.manager.manager
  hl_coeffs : ...
  intensity_flag : bool
  log : file
  master_phil : libtbx.phil.scope
  miller_arrays : list of cctbx.miller.array
  params : libtbx.phil.scope_extract
  pdb_file_names : libtbx.phil.scope_extract_list
  pdb_hierarchy : iotbx.pdb.hierarchy.root
  pdb_inp : iotbx.pdb.input
  processed_pdb_file : mmtbx.monomer_library.pdb_interpretation.process
  r_free_flags : cctbx.miller.array
  raw_data : cctbx.miller.array
  raw_flags : cctbx.miller.array
  sequence : ...
  test_flag_value : int
  unknown_residues_flag : bool
  unknown_residues_error_message : str
  unmerged_i_obs : ...
  working_phil : libtbx.phil.scope
  xray_structure : cctbx.xray.structure.structure

  Examples
  --------
  >>> from mmtbx.command_line import load_model_and_data
  >>> cmdline = load_model_and_data(
  ...  args=["model.pdb", "data.mtz"],
  ...  master_phil=master_phil,
  ...  prefer_anomalous=True,
  ...  set_wavelength_from_model_header=True,
  ...  set_inelastic_form_factors="sasaki",
  ...  )
  >>> assert cmdline.pdb_hierarchy is not None
  >>> assert cmdline.fmodel is not None
  >>> assert cmdline.params.input.wavelength is not None
  c   4      C   s|  d d  l  } d d  l } d d  l } d d l m } d d l m } d d  l } | r} t | t  sk t	  t
 d |  } n  t | t  r” | j j |  } n  | d  k	 ró t |  d k sĖ d | k ró t d | | j d d	  f   qó n  | r	| s	t	  n  |
 dl k st	  | |  _ | |  _ d  |  _ |  _ d  |  _ |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _  d  |  _! d  |  _" g  |  _# | |  _$ d | k sŚd | k rét%   |  _$ nJ | r3t&   |  _$ |  j$ j' d d d |  |  j$ j' d d d t(    n  t) d d |  j$ | j j* d | d | d d d d d d d d  } | j+ |  _, |  j, j-   } t | j. j/ j0  d k r¾t1 d   n  | j. j2 j0 |  _3 | j. j/ j0 |  _4 d  |  _5 } x6 | j. j/ j0 D]% } | j6 |  } | d  k	 r’Pq’q’Wd  } } } | j. j7 j0 d  k rc| rt1 d   qn°t8 d  d |  j$ | j9 | j. j7 j0  } | j: d!  | j; } | j! d j5   } | d  k sē| j<   d  k sē| j=   d  k rS| d  k	 rDd d" l> m? } |  j$ d# IJ| j@ d$ | d d	  | d% | d& | jA g  } qSt1 d'   n  | d  k rk| j; } n  | jB jC d( | d) | j. j7 d* d+ d, d- d. | d/ | d0 |  j$  } | j |  _ | j |  _ | j |  _ | j  |  _  | j |  _ | j |  _ | j; j! |  _! |  j j5   } t |  j3  d k rmxB |  j3 D]4 } | jD jE jF d1 |  } |  j# jG | | f  q2Wn  | d  k	 r| } n  d d2 lH mI }  |  d3 | d4 |  |  _5 |  j5 d  k	 r1|  j d  k	 r1|  j jJ d% |  j5  jK   jL |  j jM    |  _ |  j jJ d% |  j5  jK   jL |  j jM    |  _ n  d5 }! tN | j. d6  rÆ| j. jO }" |" tP tQ g k rÆ| j. jR j0 }# |# d  k r| j. j7 j0 }# | }$ n | j9 |#  }$ |$ j: d!  |  j$ |$ j; _S |  j5 j<   }% |% jT   }& | jB jU d( |$ j; d) | j. jR d0 |  j$ d7 d8 d9 |& d: tP  }' |' d  k	 r¬|' jV   }' |' jW   rd|  j jW   s|' jX   }' qn |  j jW   r|' jY   }' n  |' jZ d; |  j d< dm  |  _" d= }! q¬qÆn  t[ |  _\ t[ |  _] | rq	t^ | d> d   }( |( d  k rż| jD j_ j` j-   }( n  t8 d? d |  j$ | jB ja d@ | j. j/ j0  }) |) jb d |  j$  |) jc }* | jB jd dA |  j# dB |( d% |  j5 dC | j. je dD k dE t^ |( dE t[  d0 |  j$  }+ |+ jf dF |* dG t[ dH |  j5 d  k oĘ|  \ |  _ |  _ |  j jg jh dI t[ dJ t[  }, |, d  k	 r	tP |  _\ |, |  _] n  |  j ji dK t[  |  _ |  j d  k	 sD	t	  |  j j   |  _ |  j jg }- |- j |  _ n| jB jj dL | j. j/ j0 dA |  j# d% |  j5 d0 |  j$  }. |. j |  _ |  j jk   |  _ | r7
|  j jl   jm dM  }/ |/ jn tP  t |/  k r7
|  j jo |/  |  _ |  j jp d% |  j5  |  _ q7
n  |  j jq   jr   |  j d  k rw
|  j js d% |  j5  |  _ n  | j. jt d  k	 rĆ
| j. ju d  k	 rŖ
t1 dN   n  dO | j. jt | j. _u n  |	 r&| j. ju d  k r&|  j jv   }0 |0 d  k	 r&|  j$ dP IJ|  j$ dQ |0 IJ|0 | j. _u q&n  | d  k	 rā|  j jw dR |  j jx   dS | j. je  | j. ju d  k	 r|
 d  k	 r|  j jy dT | j. ju dS |
  n  t8 dU d |  j$ |  j jw   jz d |  j$  |  j j@ d$ |  j$  n  | r”| d  k	 r”t8 dV d |  j$ t^ | j. dW d   }1 t^ | j. dX d   }2 |2 tQ k rb|  j" d  k	 rwt1 dY dZ   qwn |1 d  k	 rwtQ }2 n  |2 tQ k rä|  j$ d[ IJ| jB j{ d\ |  j g d] | j. je d^ |  j d_ |  j dW |1 d` |! d0 |  j$  |  _ n |2 d  k	 r|  j" d  k	 rt1 dY dZ   n  | jB j| d^ |  j da |  j d_ |  j dX | j. j} db |  j" d` |!  |  _ |  j j~ dc d  d0 |  j$ dd tP de tP  |  j jM   j d |  j$  n  | j. j d  k	 rć| j9 | j. j df dg dh tP }3 |3 jA |  _ n  d  |  _ tN | j. di  rU| j. j j0 d  k	 rUt d^ |  j d1 | j. j j0 dj | j. j j d0 |  j$  |  _ qUn  | |  _ |  j$ dP IJ|  j$ dk IJd  S(n   Ni’’’’(   t   crystal_symmetry_from_any(   t   file_readerR   i    s   --helps   %s

Full parameters:
%st   prefixs     t   sasakit   henkes   --quiets
   quiet=Truet   labelt   stdoutt   file_objectt
   log_buffers   Collecting inputst   outt   argsR(   t   pdb_file_defs   input.pdb.file_namet   reflection_file_defs   input.xray_data.file_namet   cif_file_defs   input.monomers.file_namet   seq_file_defs   input.sequences+   At least one PDB file is required as input.s3   At least one reflections file is required as input.s   Processing X-ray datat   hkl(   t   reflection_file_servers4   No symmetry in X-ray data file - using PDB symmetry:t   ft   crystal_symmetryt   reflection_filess5   No crystal symmetry information found in input files.R=   t
   parameterst   data_parameter_scopes   input.xray_datat   flags_parameter_scopes   input.xray_data.r_free_flagst   prefer_anomaloust   force_non_anomaloust   logt	   file_name(   t   combine_model_and_data_symmetryt   model_symmetryt   data_symmetryt   mlt   experimental_phasest   parameter_scopes   input.experimental_phasest   working_point_groupt   symmetry_safety_checkt   othert   data_substitutet   mlhlR   s   Processing PDB file(s)t
   file_namest   cif_objectst   pdb_interpretation_paramst   use_neutron_distancest   neutront   stop_for_unknownst   raw_recordst   stop_if_duplicate_labelst   allow_missing_symmetryt   ignore_unknown_scattering_typest%   ignore_unknown_nonbonded_energy_typest   show_energiest   pdb_file_namess   not element Xs/   Both wavelength and energy have been specified!g5Ż÷T67Č@R	   s%   Using wavelength = %g from PDB headert   d_mint   tablet   photons   xray_structure summarys   F(model) initializationt   skip_twin_detectionR   s4   Automatic twin law determination not supported when s   experimental phases are used.s(   Twinning will be detected automatically.t   xray_structurest   scattering_tablet   f_obst   r_free_flagst   target_namet   xray_structuret   hl_coefft   paramst   optimize_maskt   showt
   force_typet   seqt   raise_sorry_if_errorst   unmerged_datat   data_labelss   End of input processing(   NR0   R1   (   i    i    i    i    (   t(   mmtbx.monomer_library.pdb_interpretationt   mmtbx.monomer_library.servert   mmtbx.utilsR#   R-   R.   R   t
   isinstancet
   basestringR   R*   t   strR   R   R   t   lenR   t   as_strR7   R(   t   processed_pdb_filet   pdb_inpt   pdb_hierarchyRi   t   geometryt   sequencet   fmodelRf   Rg   t   intensity_flagt   raw_datat	   raw_flagst   test_flag_valuet   miller_arrayst	   hl_coeffsRT   RF   R   R   t   registerR   R   t   process_command_line_with_filest   workt   working_philt   extractt   inputt   pdbRG   R   t   monomerst   cif_file_namesR_   R?   t   extract_fromt	   xray_dataR   t   any_filet   check_file_typet   file_servert   space_groupt	   unit_cellt   iotbx.reflection_file_utilsR=   t   show_summaryR4   t   utilst   determine_data_and_flagst   monomer_libraryt   servert   read_cift   appendt   iotbx.symmetryRH   t   customized_copyt   eliminate_sys_absentt   set_infot   infot   hasattrt   use_experimental_phasesR   R   RL   t   errt   build_derived_point_groupt   determine_experimental_phasest
   map_to_asut   anomalous_flagt   average_bijvoet_matest   generate_bijvoet_matest   matching_sett   Falset   unknown_residues_flagt   unknown_residues_error_messaget   getattrR   t   master_paramst   combine_unique_pdb_filest   report_non_uniqueRY   t   process_pdb_file_srvRe   t   process_pdb_filest   all_chain_proxiest   fatal_problems_messaget   geometry_restraints_managert   pdb_filet   construct_hierarchyt   atom_selection_cachet	   selectiont   countt   selectt   extract_xray_structuret   atomst   reset_i_seqt   xray_structure_simplet   energyt
   wavelengtht   extract_wavelengtht   scattering_type_registryR`   t   set_inelastic_form_factorsRm   t   fmodel_simplet   fmodel_managerR   t   update_all_scalest$   show_rfactors_targets_scales_overallt   unmerged_i_obsRq   t   load_and_validate_unmerged_datat   labelsRk   (4   t   selfR7   R(   R6   t   process_pdb_filet   require_datat   create_fmodelRD   RE   t    set_wavelength_from_model_headerRČ   t   usage_stringt   create_log_buffert   remove_unknown_scattererst   generate_input_philt   mmtbxR-   R.   R#   t   cmdlineRk   t   pdb_symmt   pdb_file_namet   data_and_flagst   hkl_symmt   hkl_int
   hkl_servert   symmR=   RG   t   cif_objt   use_symmetryRH   Rh   t   flagt   phases_filet	   phases_inR   t   point_groupR   t   pdb_interp_paramst   pdb_combinedt   pdb_raw_recordst   processed_pdb_files_srvt	   error_msgt   chain_proxiest   pdb_file_objectt	   known_selRÅ   Rc   R   t   seq_file(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/__init__.pyt   __init__ż   s    																													%					
												c         C   sS   t  |  j  j d k s t  t | d  } |  j j d d d d d |  |  j S(   sČ   
    Open a log file and write out the existing output buffer, returning the
    multi_out pseudo-filehandle.

    :param file_name: log file to create
    :returns: libtbx.utils.multi_out object
    R   t   wt	   old_labelR5   t	   new_labelRF   t   new_file_object(   t   typeRF   t   __name__R   t   opent   replace_stringio(   RŠ   RG   t   log_file(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/__init__.pyt   start_log_file2  s    c         C   s   |  j  d k	 s t  |  j  j d d  } |  j d k	 rR | j |  j d d n  |  j d k	 rz | j |  j d d n  | j   j |  d S(   s   
    Write the processed amplitudes, optional Hendrickson-Lattman coefficients,
    and R-free flags to the designated MTZ file.
    t   column_root_labelt   Ft   HLt
   FreeR_flagN(	   Rf   R   R   t   as_mtz_datasetR   t   add_miller_arrayRg   t
   mtz_objectt   write(   RŠ   RG   t   mtz_data(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/__init__.pyt   save_data_mtzB  s    

c      	   C   sy   | d k r |  j } n  d d l } d d l } | j j d |  j d t  } | j j d |  j	 d |  j
 d | d |  S(	   s   
    Instantiate an mmtbx.model.manager object with the current pdb hierarchy,
    xray structure, and geometry restraints.
    i’’’’NR~   t   normalizationRi   R}   t   restraints_managerRF   (   R   RF   t   mmtbx.restraintst   mmtbx.modelt
   restraintst   managerR~   R   t   modelRi   R}   (   RŠ   RF   RŁ   R  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/__init__.pyt   create_model_managerQ  s     				(   R÷   t
   __module__t   __doc__t   sysR3   R   R   R®   Rń   Rū   R  R  (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/__init__.pyR,   ¤   s"   X’ (		c         C   s>  d d l  m } | j d | d | d |  } | j   d
 k	 rĢ | j   d
 k	 rĢ | j |   s:|  j   j   } | j   j   } | | k r³ | j   j |  j    r³ | Sn  t	 d d | |   q:nn | j   d
 k	 r:|  j   j   } | j   j   } | | k r:t
 d d	 |  j   | j   f   q:n  | S(   sL   
  Read in (and verify) unmerged intensities, e.g. from scalepack or XDS.
  i’’’’(   t   merging_statisticsRG   Rr   RF   s	   Data files   Unmerged datas6   Incompatible space groups in merged and unmerged data:s   %s versus %sN(   R#   R  t   select_dataR   R   R   t   is_similar_symmetryR§   t   is_similar_tot   show_symmetry_errorR   t   space_group_info(   Rf   RG   Rr   RF   R  RĶ   t   pg_f_obst   pg_i_obs(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/__init__.pyRĪ   b  s,    		c         C   s|   d d  l  } | j   } | j   } | j d | d d  | j d | d d  t d |  | j   | | j   f   d  S(   Ni’’’’R>   R/   s     s/   Incompatible symmetry definitions:
%s:
%s
%s
%s(   t	   cStringIOR   R   R   t   getvalue(   t   file1t   file2t   symm1t   symm2R  t	   symm_out1t	   symm_out2(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/__init__.pyR    s    c         C   s   |  j  j j d k r$ t d   nw |  j  j j d k rH t d   nS |  j  j j d k rl t d   n/ |  j  j j j d k r t d d d   n  t	 S(   s:   
  Check for completeness of mandatory input parameters
  s   No PDB file defined.s   No reflection file defined.s%   No labels chosen for reflection data.s9   R-free flags not defined.  If you are trying to run this sB   program with a reflections file that is missing R-free flags, use s7   the reflection file editor to generate a new tests set.N(
   R   R   RG   R   R   R   RĻ   Rg   R2   R   (   Rk   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/__init__.pyt   validate_input_params  s    (   R  t
   __future__R    t   libtbx.str_utilsR   R   t   libtbx.utilsR   R   R   R   t   libtbxR   R  R   R  R   R   R®   R   R*   R+   t   objectR,   R3   RĪ   R  R!  (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/__init__.pyt   <module>   s,   "c	’ Ą		