ó
�™xTc           @   s°   d  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	 Z	 d „  Z
 d d d e d „ Z d e d d d „ Z d d d	 „ Z d
 e f d „  ƒ  YZ d S(   sd   
Utilities for re-refining (or otherwise working with) structures downloaded
directly from the PDB.
iÿÿÿÿ(   t   division(   t   easy_run(   t   null_outt   SorryNc         C   s²   d d l  m } t |  ƒ j ƒ  } d  } } xd | D]\ } | j d ƒ r3 d | k r3 | j | ƒ } | d  k	 r� | j d ƒ d j ƒ  } Pq� q3 q3 W| d k r¨ d } n  | | f S(	   Niÿÿÿÿ(   t   remark_3_interpretations
   REMARK   3t   PROGRAMt   :i   t   PHENIXs   PHENIX.REFINE(	   t	   iotbx.pdbR   t   opent	   readlinest   Nonet
   startswitht   get_programt   splitt   strip(   t   pdb_fileR   t   linest   programt   program_fullt   line(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/utils.pyR      s    
	c   
      C   s[  d d l  m } d d l  m } | d k rJ | r> t j } qJ t ƒ  } n  d |  } d |  } d |  }	 | j d |  g d | ƒ t j	 j
 | ƒ s™ t ‚ | j d d	 |  g d | ƒ t j	 j
 d |  ƒ sÞ t d
 |  ƒ ‚ n  | j d | d | d d |	 g ƒ t j	 j
 |	 ƒ s9t d |	 d j t j ƒ f ƒ ‚ n  t j	 j | ƒ t j	 j |	 ƒ f S(   sr   
  Copy data from local repository if defined and available, or download it
  from the PDB, and run cif_as_mtz.
  iÿÿÿÿ(   t	   fetch_pdb(   t
   cif_as_mtzs   %s.pdbs	   %s-sf.cifs   %s.mtzt   argst   logs   -xs+   Structure factors are not available for %s.s   --symmetry=%ss   --merges   --output_file_name=%ss!   Missing %s!
cif_as_mtz stderr:
%ss   
N(   t   mmtbx.command_lineR   R   R   t   syst   stdoutR   t   run2t   ost   patht   isfilet   AssertionErrorR   t   runt   RuntimeErrort   joint
   import_outt   stderr_linest   abspath(
   t   pdb_idt   pdb_dirt   sf_dirR   t   verboseR   R   R   t   cif_filet   mtz_file(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/utils.pyt   fetch_pdb_data   s.    
 


	c         C   s}  d d l  m } d d l m } | d k r8 t j } n  d } } }	 }
 | |  d d ƒ} | j } | j d d d d d t	 d	 d
 d t
 d d d t
 d t
 ƒ } t | ƒ d k rÆ t d |  ƒ ‚ n  | d } | d k	 r(xc | D]. } | j ƒ  j ƒ  } t | k rã | } Pqã qã Wt d | ƒ ‚ n | d | j ƒ  j ƒ  IJ| j d d d d d t
 d	 d
 d t
 d d ƒ } t | ƒ d k r‘| d } n  | j d d d d d d d t	 d	 d
 d t
 d d ƒ } t | ƒ d k rì| d \ }	 }
 n  | d k	 rO| j d | ƒ } |	 d k	 r+|	 j d | ƒ }	 n  | d k	 rO| j d | ƒ } qOn  | j d | d |	 d |
 d | d | d | ƒ S(   sŠ   
  Guess an appropriate data array to use for refinement, plus optional
  Hendrickson-Lattman coefficients and R-free flags if present.
  iÿÿÿÿ(   t   reflection_file_utils(   t   any_filet
   force_typet   hklt	   file_namet   labelst   ignore_all_zerost   parameter_scopet    t   return_all_valid_arrayst   minimum_scorei   t   prefer_amplitudest   prefer_anomalousi    s   No data arrays found in %s.s   Can't find label string '%s'!s   Defaulting to using %si   t   labelt   test_flag_valuet   disable_suitability_testt   crystal_symmetryt   obst   r_free_flagst   phasesR   t   merge_anomalousN(   t   iotbxR.   t   iotbx.file_readerR/   R   R   R   t   file_servert   get_xray_datat   Falset   Truet   lenR   t   infot   label_stringt   array_labelt   get_experimental_phasest   get_r_free_flagst   customized_copyt   process_raw_data(   R,   R   RB   t   preferred_labelsR>   R.   R/   RA   t   datat   flagst
   flag_valuet   hkl_int
   hkl_servert   data_arrayst   arrayt   array_labelst	   hl_arrayst   flags_and_values(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/utils.pyt   find_data_arrays<   sv     			
					c         C   s:  d d l  m } d d l m } | d k r8 t j } n  | j d |  ƒ } | j ƒ  } d } xZ | D]R }	 |	 j	 d ƒ rc |	 j
 ƒ  j ƒ  j ƒ  }
 |
 d } t | ƒ d k sµ t ‚ qc qc W| d k	 r6|  g } g  } xÓ | D]Ë } | j ƒ  | j ƒ  k rÿ qÛ n  t j j | | ƒ } t j j | ƒ s5t j ƒ  } n  t j j | d | ƒ } t j j | ƒ sv| j d	 | g ƒ n  t j j | ƒ sŒPn  | j | ƒ | j | ƒ qÛ Wt | ƒ d k r2t j j | d
 | ƒ } | d | d j | ƒ f IJt j d d j | ƒ | f ƒ t j |  ƒ t j | |  ƒ n  | Sd S(   sÁ   
  Assembles complete structures from split PDB files (e.g. ribosomes),
  preserving the original file name.  Return value is a list of IDs which
  were added to the current model (or None).
  iÿÿÿÿ(   R   (   t   pdbR2   t   SPLITi   i    s   %s.pdbR   s
   %s_new.pdbs#   Joining multi-part structure: %s %st    s%   iotbx.pdb.join_fragment_files %s > %sN(   R   R   RC   R]   R   R   R   t   inputt   title_sectionR   R   t   lowerR   RI   R    R   R   R#   t   isdirt   getcwdR   R   t   appendR   t   callt   removet   rename(   R   R'   t   base_dirR   R   R]   t   pdb_int   titlet	   other_idsR   t   fieldst	   pdb_filest   combined_idst   other_idt
   dest_dir_2t
   pdb_file_2t   pdb_all(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/utils.pyt   combine_split_structure�   sL    
 
	
	t   filter_pdb_filec           B   s&   e  Z d  Z d d e e e d „ Z RS(   s  
  Processing of PDB files to remove common pathologies and enable automatic
  refinement behavior.  In particular, delete unknown atoms and ligands,
  reduce the occupancy of Se atoms from 1 to trigger occupancy refinement,
  and optionally remove zero-occupancy atoms.
  c         C   su  d d l  m } d d  l } | d  k r4 t ƒ  } n  | | d d ƒ}	 |	 j d ƒ |	 j j }
 t |
 j	 ƒ  ƒ d k r† t
 d ƒ ‚ n  d } |
 j ƒ  } |
 j ƒ  } | j d ƒ } | j d	 ƒ } | j ƒ  d k } | j t ƒ |  _ | j t ƒ |  _ | j t ƒ |  _ | } t } |  j d k sO|  j d k r:| sO|  j d k rm| rmt } | d  k rm| } qmn  |  j d k rÇ| rÇ| d
 |  j IJx0 | j ƒ  D] } | d | | j ƒ  IJq¢Wn  |  j d k rBd } | röd } | | M} n  | sB| | |  j IJx/ | j ƒ  D] } | d | | j ƒ  IJqWqBn  |
 j | ƒ } |  j d k rß| rßxv | j ƒ  D]e } | j d k rs| j ƒ  j d k rs| j d k rØ| sÉ| d | j ƒ  IJn  d | _ qØqsqsWn  | rqt | d ƒ } | d j |	 j j j ƒ  ƒ IJ| d j |	 j j j  ƒ  ƒ IJ| | j! j" d |	 j j# ƒ  ƒ IJ| | j$ ƒ  IJ| j% ƒ  n  d  S(   Niÿÿÿÿ(   R/   R0   R]   i   s(   Multi-MODEL PDB files are not supported.i    s-   not (element X or resname UNX or resname UNL)s   element SE and resname MSEs-   Warning: %d unknown atoms or ligands removed:s     %ss;   Warning: %d atoms with zero occupancy present in structure:s.   Warning: %d atoms with zero occupancy removed:t   SEt   MSEg      ð?s   Set occupancy of %s to 0.99g®Gáz®ï?t   ws   
R>   (&   RD   R/   R   R   R   t   assert_file_typet   file_objectt	   hierarchyRI   t   modelsR   t   atomst   atom_selection_cachet	   selectiont   extract_occt   countRG   t	   n_unknownRH   t   n_semett
   n_zero_occt
   iselectiont   id_strt   selectt   elementt   fetch_labelst   resnamet   occR	   R#   R`   Ra   t   remark_sectionR]   t   format_cryst1_recordR>   t   as_pdb_stringt   close(   t   selfR   t   output_fileR   t   quiett
   set_se_occt    remove_atoms_with_zero_occupancyR/   RC   Rj   R{   R‚   t	   all_atomst   cachet	   known_selt	   semet_selt   zero_occ_selt   keep_selt   modifiedt   i_seqt   msgt   hierarchy_filteredt   atomt   f(    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/utils.pyt   __init__¸   sp    
"$N(   t   __name__t
   __module__t   __doc__R   RG   RH   R¡   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/utils.pyRu   ±   s   (   R¤   t
   __future__R    t   libtbxR   t   libtbx.utilsR   R   R   R   R   R   RG   R-   R\   Rt   t   objectRu   (    (    (    s?   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/wwpdb/utils.pyt   <module>   s   	G,