ó
±™xTc           @   sõ  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	 Z d  d l
 m Z d  d l m Z d  d l m Z 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  d l Z d  d l Z d Z d e Z d e j e j f d „  ƒ  YZ d „  Z  d d d „ Z! d d d „ Z# d e j$ j% f d „  ƒ  YZ$ d d „ Z& e j' e( e) e( d „ Z* d „  Z+ d e j, f d „  ƒ  YZ- e. d k rñe* d e j/ d  d! e( d" e( ƒ n  d S(#   iÿÿÿÿ(   t   division(   t   dummy_validation(   t
   molprobityN(   t   file_reader(   t   object_oriented_patterns(   t   make_header(   t   Sorryt   null_out(   t   runtime_utils(   t   easy_pickle(   t   easy_mp(   t   AutosÇ  
  structure
    .multiple = True
    .optional = True
    .short_caption = Structure files
    .style = auto_align box
  {
    name = None
      .type = str
      .input_size = 400
      .short_caption = Structure name
      .help = Structure name (will become column label)
      .style = bold
    pdb_file = None
      .type = path
      .short_caption = PDB file
      .style = bold file_type:pdb
      .help = PDB file
    mtz_file = None
      .type = path
      .short_caption = MTZ file
      .style = bold file_type:mtz OnChange:extract_table_one_labels
      .help = MTZ file
    data_labels = None
      .type = str
      .input_size = 160
      .style = renderer:draw_table_one_label_widget
    r_free_flags.label = None
      .type = str
      .short_caption = R-free flags label
      .input_size = 160
      .style = renderer:draw_table_one_label_widget
      .help = R-free flags label
    wavelength = None
      .type = float
    cif_file = None
      .type = path
      .multiple = True
      .optional = True
      .short_caption = CIF file
      .help = Restraints file
    cif_directory = None
      .type = path
      .short_caption = CIF directory
      .style = directory
      .help = Directory containing restraints - all CIF files found will be         used.
    data_type = *xray neutron
      .type = choice
    unmerged_data = None
      .type = path
      .help = Secondary reflections file with unmerged intensities, for         calculation of merging statistics.
      .style = bold file_type:hkl OnChange:extract_unmerged_intensities         help_page:unmerged_data.htm
    unmerged_labels = None
      .type = str
      .input_size = 160
      .style = renderer:draw_unmerged_intensities_widget
  }
sà  
table_one {
  %s
  processing {
    re_compute_r_factors = True
      .type = bool
      .short_caption = Always re-compute R-factors
      .style = bold
    n_bins = 10
      .type = int
      .short_caption = Number of resolution bins
    count_anomalous_pairs_separately = False
      .type = bool
      .short_caption = Count anomalous pairs separately
      .help = If true, the program will treat F+ and F- (if present) as         independent reflections when calculating data statistics.  (Not         recommended.)
    ligand_selection = None
      .type = str
      .short_caption = Ligand atom selection
      .help = If specified, will determine which atoms or residues are         counted as ligands (instead of automatic behavior).
      .input_size = 400
  }
  multiprocessing {
    include scope libtbx.easy_mp.parallel_phil_str_no_threading
  }
  output {
    directory = None
      .type = path
      .help = This is only used by the PHENIX GUI.
      .short_caption = Output directory
      .style = bold output_dir
    job_title = None
      .type = str
      .input_size = 400
      .style = bold
    show_missing_fields = True
      .type = bool
    format = txt csv *rtf
      .type = choice(multi=True)
      .caption = Text CSV RTF
      .short_caption = Output formats
      .style = bold
    base_name = Table1
      .type = str
      .short_caption = Base file name
      .style = bold
    verbose = True
      .type = str
    text_field_separation = 2
      .type = int
  }
}t   _c           B   s   e  Z d  Z e d „ Z RS(   s÷   
  Injector dummy class to add as_table1_column() method to the main molprobity
  object.  This extracts statistics from the various validation objects and
  unmerged data, checks for consistency, and returns an iotbx.table_one.column
  object.
  c      O   C   s$  d7 } |  j } | d7 k r' t ƒ  } n  t ƒ  } t ƒ  } | j }	 | j }
 | j } | j } |  j ƒ  } | \ } } |  j d7 k	 r|  j j	 } |  j j
 d } | j }	 d } | j | d | k sÝ | j | d | k  rt d d d d | | | | j | j f ƒ ‚ qn  |  j ƒ  } |  j ƒ  } d7 } |  j d7 k	 rã|  j j d k rb|  j j } n  | p‚|  j j ƒ  o‚| t k } t d	 |  j j d
 |  j j d | d | d | d | d | ƒ \ } } } | rã| j }
 qãn  t j j d | d |  j ƒ  d |  j ƒ  j ƒ  d | d | d | j d |	 d | j d | d | j d | j d | j  d | j! d | j" d | j# d |
 d  | d! | d" | d# | j$ d$ | j% d% |  j& j' j( d& |  j& j) j( d' |  j& j* j( d( |  j& j+ j( d) |  j& j, d* |  j- ƒ  d+ |  j. ƒ  d, |  j/ ƒ  d- |  j0 ƒ  d. |  j1 ƒ  d/ |  j2 ƒ  d0 |  j3 ƒ  d1 |  j& j' j4 d2 |  j& j) j4 d3 |  j& j* j4 d4 |  j& j+ j4 d5 | d6 | j5 ƒ 'j6 d | j7 | j8 f d | j9 d | j d | j: d  | j; d | j" d | j# d | j  d | j! d | j d | j d | j d# | j$ d$ | j% d! | j< d" | j= ƒ S(8   s€   
    Extract information for display in the traditional 'Table 1' of
    crystallographic statistics in structure articles.
    iÿÿÿÿgü©ñÒMbP?i   s5   Resolution limits for unmerged data in the structure s   '%s' do not cover the s?   full range present in the merged data: %g - %g (merged) versus s   %g - %g (unmerged)i    t
   r_work_pdbt
   r_free_pdbt   r_work_fmodelt   r_free_fmodelt   outt   structure_namet   re_compute_r_factorst   labelt   space_groupt	   unit_cellt
   wavelengtht	   d_max_mint
   n_refl_allt   n_reflt   multiplicityt   completenesst   i_over_sigmat   wilson_bt   r_symt   r_meast   cc_one_halft   cc_start   n_refl_refinet   n_freet   r_workt   r_freet   cc_workt   cc_freet   n_atomst   n_macro_atomst   n_ligand_atomst	   n_watersst
   n_residuest	   bond_rmsdt
   angle_rmsdt   rama_favoredt   rama_allowedt   rama_outlierst   rota_outlierst
   clashscoret   adp_meant   adp_mean_mmt   adp_mean_ligt   adp_mean_watt   n_tls_groupst   anomalous_flagN(>   t   Nonet
   data_statsR   R   R#   R$   R   R   t   mergingt   overallt   binst   n_uniqt   d_mint   d_maxR   R%   R&   t   header_infoR9   t   is_phenix_refinementR   t   rfactor_sanity_checkt   iotbxt	   table_onet   columnt   space_group_infoR   t
   parameterst   n_obst   mean_redundancyt   i_over_sigma_meanR   t   r_mergeR    R!   R"   R'   R(   t   model_statst   allt   n_non_hdt   macromoleculest   ligandst   watert	   n_proteint	   rms_bondst
   rms_anglesR0   R1   R2   R3   R4   t   b_meanR:   t   add_outer_shellt   d_max_outert   d_min_outert   n_refl_outert   n_refl_refine_outert   n_free_outert   r_work_outert   r_free_outer(   t   selfR   R   t   logR   t   outer_shellR<   t   merging_statst   merging_outert   n_refl_uniqR#   R$   R   R   RB   RA   t   epsilonR%   R&   R9   t   use_header_valuest   warned(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyt   as_table1_column’   sÄ    																																	(   t   __name__t
   __module__t   __doc__R   Rj   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyR   ‹   s   c   	      C   sˆ   t  } |  d  k	 rn |  | } } | d | IJ| |  k sE | | k r{ t } | d | |  | | | f IJq{ n | | } } | | | f S(   Ns=   Using resolution limits reported in PDB file for structure %ssª  *** WARNING: Resolution limits in the PDB file for structure '%s'
             are inconsistent with resolution limits in the MTZ file:
             %.2f - %.2f (in PDB file)
             %.2f - %.2f (in MTZ file)
    This is not a fatal error, since the data processing may output more data
    than are used in refinement, but you should check to make sure that you
    have not accidentally specified the wrong model file.
(   t   FalseR;   t   True(	   t	   d_max_pdbt	   d_min_pdbt   d_max_f_obst   d_min_f_obsR   R   Ri   RB   RA   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyt   resolution_sanity_check  s    gü©ñÒMbP?gü©ñÒMbp?c	         C   s  t  }	 |  d  k	 rè | d  k	 rè | s> | d IJ|  | }
 } n | d IJ| | }
 } t |  | ƒ } t | | ƒ } | | k s� | | k r° t }	 | d |  | | | f IJq| | k sÈ | | k r| d |  | | | f IJqn | d | IJ| | }
 } |
 | |	 f S(   Ns   Using R-factors in PDB headers   Using re-computed R-factorssÛ  *** WARNING: R-factors reported in the PDB file do not match those calculated
             by phenix.model_vs_data:
             r_work=%.4f r_free=%.4f (in PDB file)
             r_work=%.4f r_free=%.4f (from phenix.model_vs_data)
    This is not a fatal error, but if we can't recalculate the reported
    R-factors, others probably won't be able to either.  This may be due to
    inconsistent use of a twin operator; phenix.model_vs_data may apply one
    automatically, but only if it significantly improves R-work.  Alternately,
    it can indicate that the model was further modified after the last round
    of refinement, that the wrong MTZ file was used, or that a different
    resolution limit was used for refinement.
sŒ      NOTE: small discrepancy in R-factors reported in PDB file versus those
          calculated by phenix.model_vs_data:
             r_work=%.4f r_free=%.4f (in PDB file)
             r_work=%.4f r_free=%.4f (from phenix.model_vs_data)
    This is probably not a big deal, but please double-check that you used the
    correct data file and have not changed the resolution limit for refinement.
s,   Using re-computed R-factors for structure %s(   Rn   R;   t   absRo   (   R   R   R   R   R   R   R   t   tolerance_ignoret   tolerance_warnRi   R%   R&   t   delta_r_workt   delta_r_free(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyRE   #  s,    


c         C   s¼  | d  k r t ƒ  } n  d |  j d |  j d |  j d |  j d |  j j d | d | d d	 d
 d d g g  |  j D] } d | ^ qw } | d  k	 r® | j d | ƒ n  |  j	 d  k	 rá | j
 d |  j	 d |  j g ƒ n  |  j d k r| j d d g ƒ n  |  j d  k	 r t j |  j ƒ } xv | D]k } t j j |  j | ƒ }	 t j j |	 ƒ rdq.n  t j |	 ƒ }
 |
 j d k r.| j d |
 j ƒ q.q.Wn  t j j j d | d | ƒ S(   Ns   pdb.file_name="%s"s   xray_data.file_name="%s"s   xray_data.labels="%s"s%   xray_data.r_free_flags.file_name="%s"s!   xray_data.r_free_flags.label="%s"s	   n_bins=%ds#   count_anomalous_pairs_separately=%ss
   coot=Falses
   maps=Falses   probe_dots=Falses&   use_pdb_header_resolution_cutoffs=Trues   --quiets   monomers.file_name="%s"s   ligand_selection="%s"s   unmerged_data.file_name="%s"s   unmerged_data.labels="%s"t   neutrons   scattering_table=neutrons   keep_hydrogens=Truet   cift   argsR   (   R;   R   t   pdb_filet   mtz_filet   data_labelst   r_free_flagsR   t   cif_filet   appendt   unmerged_datat   extendt   unmerged_labelst	   data_typet   cif_directoryt   ost   listdirt   patht   joint   isdirR   t   any_filet	   file_typet	   file_namet   mmtbxt   command_lineR   t   run(   t   paramst   n_binst    count_anomalous_pairs_separatelyt   ligand_selectionRb   R{   t   molprobity_argst   filesR�   t	   full_patht   f(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyt   run_single_structureW  sB     



!
 RG   c           B   sQ   e  Z e j j j d  d d g Z e j d „ Z d „  Z	 d „  Z
 d d „ Z RS(   t
   output_dirR“   t   output_filesc      
   C   s#  t  j j j |  d | j j d | j j ƒt j	 ƒ  |  _
 | |  _ g  |  _ t d d | ƒt j d t t |  j j ƒ ƒ d |  j d | j j d | j j ƒ } x{ t | j | ƒ D]g \ } } | d	 IJ| d
 | j IJ| j j d | j d | j d | j j d | ƒ } |  j | ƒ q´ Wd  S(   Nt   text_field_separationR•   s$   Running data analysis and validationR   t   iterablet   funct	   processest   methodt    s!   Collecting stats for structure %sR   R   R   Rb   (   RF   RG   t   tablet   __init__t   outputRž   t
   processingR•   Rˆ   t   getcwdRœ   R“   R�   R   R
   t   parallel_mapt   ranget   lent	   structureR›   t   multiprocessingt   nproct
   technologyt   zipt   namet
   validationRj   R   R   t
   add_column(   Ra   R“   R   t   resultsR¬   t   resultRH   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyR¥   ‚  s*    				
			c      	   C   s>   t  d |  j j | d |  j j j d |  j d |  j j j ƒ S(   NR“   R”   R•   R–   (   R›   R“   R¬   R§   R”   R•   R–   (   Ra   t   i_struct(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyR›   š  s
    	c         C   s\   xU | D]M } d | | f } t  |  d | ƒ } | | ƒ |  j j | d | f ƒ q Wd  S(   Ns   %s.%ss   save_%ss   Table as '%s'(   t   getattrR�   R‚   (   Ra   t	   file_baset   formatst   formatR�   R¢   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyt   save_multiple¡  s
    
c         C   s   |  j  g  f S(   N(   R�   (   Ra   t   job(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyt
   finish_job¨  s    N(   Rk   Rl   RF   RG   R¤   t	   __slots__t   syst   stdoutR¥   R›   R»   R;   R½   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyRG   ~  s   		R¬   c   
      C   st  xmt  |  j ƒ D]\\ } } | j d k rB t d | d ƒ ‚ n  | j | j j g j d ƒ d k r t	 j
 | j d d ƒ} | j d ƒ | j } | j } | j d k rü | d | IJ| j d | d d d	 t d
 | d d ƒ } | j ƒ  j ƒ  | _ n  | j j d k rl| d | IJ| j d | d d d d d t d
 | d ƒ }	 |	 d j ƒ  j ƒ  | j _ qlq q Wd S(   s·   
  Guess MTZ file column labels for experimental data and R-free flags.  Only
  invoked when this program is run from the command line, but the Phenix GUI
  does something similar.
  s#   Missing MTZ file for structure #%d.i   i    t
   force_typet   hkls$   Attempting to guess labels for %s...R�   t   labelst   ignore_all_zerost   parameter_scopet   parameter_nameR   s*   Attempting to guess R-free label for %s...R   t   test_flag_valuet   disable_suitability_tests   .r_free_flagsN(   t	   enumerateR¬   R~   R;   R   R   R€   R   t   countR   R�   t   assert_file_typet   file_serverR�   t   get_xray_dataRo   t   infot   label_stringt   get_r_free_flagsRn   (
   R“   R   RÅ   t   iR¬   R~   t   serverR�   t   datat   rfree(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyt   extract_labels«  s4    $					c         C   s�  t  j j t d t ƒ} t |  ƒ d k rz | d IJ| d IJ| j d | ƒ | d IJ| d IJ| d IJ| d	 IJd  S| rš | d
 IJt j	 d ƒ n  t  j j t ƒ } t  j j
 j d | d d ƒ } g  } g  }	 d  }
 d  } d  } g  } xN|  D]F} t j j | ƒ r™t j | ƒ } | j d k r>| j | j ƒ q;| j d k rY| j }
 q;| j d k rt| j } q;| j d k r;| j | j ƒ q;qõ | j d ƒ rÖt j j d j | j d ƒ d ƒ ƒ } qõ n  | j d ƒ rö| d d } n  y | j d | ƒ } Wn t k
 r-| d | IJqõ X|	 j | ƒ qõ W| j d | |	 ƒ } | j ƒ  } |
 d  k	 r7t | j j ƒ d k rœt d d d ƒ ‚ n  | d  k r»t d d ƒ ‚ n  d  j g  | D] } d! | ^ qÈƒ } t  j j t  ƒ } | j ƒ  j d } |
 | _! | | _" | | _# | j j j | ƒ n  | rSt$ | j d | ƒn  | r‰| j j% j& d  k r‰t j' ƒ  | j j% _& q‰n  t( | ƒ | j j) j* d  k rºd | j j) _* n  | j+ d" | ƒ } | j j% j, r| d# IJ| d$ IJ| j d | ƒ | d% IJ| d# IJn  | j d t- d& d' ƒ ƒ t | j d | ƒ} t. j/ d( | j j% j0 | ƒ | j1 d) | j j% j0 d* | j j% j+ ƒ | S(+   Nt   process_includesi    s^  ************************************************************************
  phenix.table_one - statistics harvesting for publication
************************************************************************

  note: this is somewhat difficult to configure on the command line at
        present; you may find it more convenient to use the PHENIX GUI.

s*   # Parameter template for phenix.table_one:R   s8   # (the 'structure' scope may be copied as many times as s*   #  necessary to handle multiple datasets.)s   # Alternate usage:s2   #   phenix.table_one model.pdb data.mtz [logfile]*s”   
  note: this is somewhat difficult to configure on the command line at
        present; you may find it more convenient to use the PHENIX GUI.
    i   t   master_philt
   home_scopeRG   t   philt   pdbRÂ   t   txts   unmerged_data=t   =i   s   --s   =Truet   args   Ignoring unknown argument %st   sourcess6   You already have a structure defined in the parameter sC   file; to add structures, you should edit the parameters instead of s=   specifying additional PDB and data files on the command line.s7   You have supplied a PDB file, but no corresponding MTZ s   file.s   
s   log_file=%st   python_objectR£   s   #Final effective parameters:s   #---ends   table_one.efft   ws   %s.pklR¸   R¹   (2   t   libtbxRÙ   t   parset   master_phil_strRo   R«   t   showR;   t   timet   sleepR‘   t   argument_interpreterRˆ   RŠ   t   isfileR   R�   RŽ   R‚   t   file_objectR�   t
   startswitht   abspathR‹   t   splitt   processt   RuntimeErrort   fetcht   extractRG   R¬   R   t   structure_params_strR}   R~   Rƒ   RÕ   R¦   t	   directoryR¨   t   validate_paramsR­   R®   Rº   t   verboset   openR	   t   dumpt	   base_nameR»   (   R|   R   t   auto_extract_labelst&   use_current_directory_if_not_specifiedt   warnt   master_paramst   master_parmast   interpretert	   file_philt   cmdline_philR}   R~   Rƒ   t	   log_filesRÝ   Rš   t   arg_philt   working_philR“   t   log_file_strt   structure_paramst   new_structuret
   final_philt   table1(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyR’   Ì  s¬    	




	(&			



	c         C   sù   t  |  j j ƒ d k r' t d ƒ ‚ n  t j j |  j j j ƒ sQ t d ƒ ‚ n  xS t	 |  j j ƒ D]? \ } } d  | j | j g k rd t d d | d ƒ ‚ qd qd W|  j j j d k  rË t d ƒ ‚ n  t  |  j j j ƒ d k rõ t d ƒ ‚ n  t S(	   Ni    s   No structures defined.s(   Please specify a valid output directory.s5   Structure #%d is missing either a PDB file or an MTZ s   file.i   s5   Text field separation must be at least one character.s   No formats selected for output!(   R«   RG   R¬   R   Rˆ   RŠ   RŒ   R¦   Rò   RÉ   R;   R}   R~   Rž   Rº   Ro   (   R“   RÑ   t   struct(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyRó   3  s    t   launcherc           B   s   e  Z d  „  Z RS(   c         C   s]   t  j j |  j ƒ s( t  j |  j ƒ n  t  j |  j ƒ t d t |  j ƒ d t	 j
 d t ƒ S(   NR|   R   Rú   (   Rˆ   RŠ   t   existsRœ   t   makedirst   chdirR’   t   listR|   R¿   RÀ   Rn   (   Ra   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyR’   F  s    (   Rk   Rl   R’   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyR	  E  s   t   __main__R|   i   Rø   Rù   (0   t
   __future__R    t   mmtbx.validationR   R   t   mmtbx.command_line.molprobityR�   RF   R   t   iotbx.table_oneRá   R   t   oopt   libtbx.str_utilsR   t   libtbx.utilsR   R   R   t   libtbx.phil.command_lineR	   R
   R   t   libtbx.philt   os.pathRˆ   Rå   R¿   Rñ   Rã   t   injectorR   Rt   RE   R;   R›   RG   R¤   RÕ   RÀ   Ro   Rn   R’   Ró   t   target_with_save_resultR	  Rk   t   argv(    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/table_one.pyt   <module>   sJ   >7
|	$.#-"c	