ó
±™xTc           @   s  d  d l  m Z d  d l m Z m Z 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 Z d e Z d „  Z d e e e ƒ  d	 „ Z e j d d
 „ Z d e f d „  ƒ  YZ d „  Z d e j f d „  ƒ  YZ e d k re e j d ƒ n  d S(   iÿÿÿÿ(   t   division(   t   Sorryt   Usaget   null_out(   t   adopt_init_args(   t   runtime_utilsNs<  
preserve_chain_id = False
  .type = bool
  .help = The default behavior is to group heteroatoms with the nearest     macromolecule chain, whose ID is inherited.  This parameter disables     the change of chain ID, and preserves the original chain ID.
waters_only = False
  .type = bool
  .help = Rearrange waters, but leave all other ligands alone.
sort_waters_by = none *b_iso
  .type = choice
  .help = Ordering of waters - by default it will sort them by the isotropic     B-factor.
  .caption = Don't_sort Isotropic_B
set_hetatm_record = True
  .type = bool
  .help = Convert ATOM to HETATM where appropriate.
  .short_caption = Set HETATM label
ignore_selection = None
  .type = atom_selection
  .help = Selection of atoms to skip.  Any residue group which overlaps with     this selection will be preserved with the original chain ID and numbering.
renumber = True
  .type = bool
  .help = Renumber heteroatoms once they are in new chains.
sequential_numbering = True
  .type = bool
  .help = If True, the heteroatoms will be renumbered starting from the next     available residue number after the end of the associated macromolecule     chain.  Otherwise, numbering will start from 1.
distance_cutoff = 6.0
  .type = float
  .help = Cutoff for identifying nearby macromolecule chains.  This should be     kept relatively small for speed reasons, but it may miss waters that are     far out in solvent channels.
  .input_size = 80
remove_waters_outside_radius = False
  .type = bool
  .help = Remove waters more than the specified distnace cutoff to the     nearest polymer chain (to avoid PDB complaints).
  .short_caption = Max. water-to-polymer distance
loose_chain_id = X
  .type = str
  .help = Chain ID assigned to heteroatoms that can't be mapped to a nearby     macromolecule chain.
  .short_caption = Loose chain ID
  .input_size = 48
sè  
file_name = None
  .type = path
  .short_caption = Model file
  .help = Input file
  .style = bold file_type:pdb input_file OnChange:extract_symmetry
output_file = None
  .type = path
  .style = bold file_type:pdb output_file new_file
unit_cell = None
  .type = unit_cell
space_group = None
  .type = space_group
ignore_symmetry = False
  .type = bool
  .help = Don't take symmetry-related chains into account when determining     the nearest macromolecule.
preserve_remarks = False
  .type = bool
  .help = Propagate all REMARK records to the output file.
verbose = False
  .type = bool
remove_hetatm_ter_records = True
  .type = bool
  .short_caption = Remove TER records after HETATM chains
  .help = The official PDB format only allows TER records at the end of     polymer chains, whereas the CCTBX PDB-handling tools will insert TER     after each chain of any type.  If this parameter is True, the extra     TER records will be removed.
%s
include scope libtbx.phil.interface.tracking_params
c           C   s   t  j j t ƒ S(   N(   t   libtbxt   philt   parset   master_params(    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/sort_hetatms.pyt   master_phil`   s    c   M   
   C   s=  | d k r9 d d l } | j j t ƒ j ƒ  j ƒ  } n  d d l m } d d l	 m
 } |  j ƒ  }	 |	 j ƒ  x |	 D] }
 |
 j |
 _ qv W|  j ƒ  j ƒ  }	 |	 j ƒ  } | t | j ƒ  ƒ k sÈ t ‚ | j | t ƒ } | j | t ƒ } | j ƒ  } | j ƒ  } | j ƒ  } | j d | j ƒ } | j ƒ  } | j ƒ  } g  } g  } | j d k	 ry|  j ƒ  } | j | j ƒ } n  t |  j  ƒ  ƒ d k s—t ‚ xq |  j! ƒ  j" ƒ  D]] } | j# ƒ  sÈ| j$ ƒ  rú| j% | ƒ | j ƒ  j& ƒ  } | j' | t( ƒ qª| j% | ƒ qªWt | ƒ d k rS| d IJ| r1|  St) d	 |  d
 t | ƒ d d ƒ Sn  d } | j* j+ ƒ  } | j* j, ƒ  } g  } | j- | ƒ g  } g  } g  } x× | D]Ï } | j% | j. | j ƒ  j& ƒ  ƒ ƒ | j/ | j0 ƒ  ƒ | j% | j* j1 d | j2 ƒ ƒ | j3 r"| j2 | k r"| d IJn  | j% | j2 ƒ | j4 re| j5 ƒ  d j6 ƒ  }  | j% |  d ƒ q£| j% d ƒ q£Wg  }! g  }" | j7 }# |# d k r d }# n  | j* j1 d |# ƒ }$ g  }% xÆ| D]¾} | j2 }& x~| j5 ƒ  D]p}' | d 7} | j3 rl|& | k r,| j8 |& ƒ }( | |( j9 |' j0 ƒ  ƒ n- | d |& |' j: ƒ  f IJ|$ j9 |' j0 ƒ  ƒ | j; |' ƒ qÞn  t }) |' j ƒ  }* |* j& ƒ  } |' j< ƒ  }+ | j= r»|+ d j> d  k r»t( }) n% x" | D] }, | |, rÂt( }) PqÂqÂW|) sÞ|' j0 ƒ  }- x/ t? |- j ƒ  |* ƒ D] \ }. }/ |/ j |. _ qW|! j% |- ƒ |" j% |& ƒ | j; |' ƒ qÞqÞWt | j5 ƒ  ƒ d k rÂ|% j% | j0 ƒ  ƒ qÂqÂWx |% D] } | j/ | ƒ q‹W| j@ ƒ  }0 d }1 | j3 s	xFtA |! ƒ D]5\ }2 }' |" |2 }& |' j< ƒ  }+ t |+ ƒ d k rqÊn  |+ d j> d! k }3 |' j ƒ  }* |* j& ƒ  } tB jC }4 d }5 d }6 x#t? | |* ƒ D]\ }, }
 | jD rut( |
 _E n  | |, r…qTn  | |, }7 | |, }8 | jF |, d ƒ jG ƒ  }9 x² |8 jH ƒ  D]¤ \ }: }; | |: s¾| |: råq¾n  | |: }< xp |; D]h }= |9 jI | jF |: |= d ƒ ƒ }> |> |< }? |0 jJ |7 |? ƒ }@ |@ |4 k  rö|@ }4 |> jG ƒ  }6 |: }5 qöqöWq¾WqTW|5 d k rß|3 rµ| jK rµ| d |' jL ƒ  IJ|1 t |' j ƒ  ƒ 7}1 qÊqÿ| d |& |' j: ƒ  f IJ|$ j9 |' ƒ qÊxt? | | ƒ D]ü \ }A }B |5 |A k rï| r€|6 jM ƒ  rF| d |' j ƒ  d jL ƒ  |B j2 |4 f IJq€| d d |' j ƒ  d jL ƒ  |B j2 |4 tN |6 ƒ f IJn  |6 jM ƒ  sÚxK |' j ƒ  D]: }
 |0 jO d |
 jP ƒ }C |6 |C }D |0 jQ d |D ƒ |
 _P q™Wn  |B j9 |' ƒ PqïqïWtR d |5 ƒ ‚ qÊWn  xã | D]Û } g  }E xq | j5 ƒ  D]c }' |' j< ƒ  }F |F d j> d" k r&	| jS |' j ƒ  jT ƒ  ƒ }G |E j% |' |G f ƒ | j; |' ƒ q&	q&	Wt |E ƒ d k r	| jU d k rÁ	|E jV d „  ƒ n  x$ |E D] \ }H }G | j9 |H ƒ qÈ	Wq	q	W| jW rH
xP t? | | ƒ D]< \ } }I |I }J x' | j5 ƒ  D] }' |J |' _X |J d 7}J q$
Wq
Wn  x; | D]3 } x | j5 ƒ  D] }K t( |K _Y qb
W| j/ | ƒ qO
Wt |$ j5 ƒ  ƒ d k r®
| j/ |$ ƒ n  t | j ƒ  ƒ }L |L | |1 k ré
tR d | |L f ƒ ‚ n  |1 d k r| d |1 IJ| d IJn  | r| St) d	 | d
 t | ƒ d | ƒ Sd S(#   s  
  Rearrange a PDB hierarchy so that heteroatoms are grouped with the closest
  macromolecule, accounting for symmetry.  Assorted ligands will be arranged
  first in each new chain, followed by waters.  See the PHIL block
  sorting_params_str for options.
  iÿÿÿÿN(   t   pdb(   t   flext   distance_cutoffi   i    s0   No heteroatoms - hierarchy will not be modified.t   pdb_hierarchyt   n_mm_chainst   n_het_residuest   ids$   Warning: chain ID '%s' is duplicatedt    s=   Warning: no corresponding macromolecule chain match for %s %st   HOHt   WATt   DODs3   Water %s is not near any polymer chain, will deletes7   Residue group %s %s is not near any macromolecule chains4   Residue group %s added to chain %s (distance = %.3f)s#   Residue group %s added to chain %s s   (distance = %.3f, symop = %s)t	   site_cartt	   site_fracs   Can't find chain for i_seq=%dt   nonec         S   s   t  |  d | d ƒ S(   Ni   (   t   cmp(   t   xt   y(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/sort_hetatms.pyt   <lambda>0  s    s#   Atom counts do not match: %d --> %ds$   WARNING: %d atoms removed from models5     You must refine this model again before deposition!(   R   R   (   R   R   R   (   R   R   (Z   t   Nonet
   iotbx.philR   R   t   sorting_params_strt   fetcht   extractt   iotbxR   t   scitbx.array_familyR   t   atomst   reset_i_seqt   i_seqt   tmpt	   deep_copyt   sizet   lent
   scattererst   AssertionErrort   boolt   Falset   hd_selectiont
   sites_fract
   sites_cartt   pair_asu_tableR   t   asu_mappingst   tablet   ignore_selectiont   atom_selection_cachet	   selectiont   modelst
   only_modelt   chainst
   is_proteint   is_nat   appendt   extract_tmp_as_size_tt   set_selectedt   Truet   sort_hetatms_resultt	   hierarchyt   roott   modelt   append_modelt   size_tt   append_chaint   detached_copyt   chainR   t   preserve_chain_idt   sequential_numberingt   residue_groupst   resseq_as_intt   loose_chain_idt   indext   append_residue_groupt   residt   remove_residue_groupt   atom_groupst   waters_onlyt   resnamet   zipt	   unit_cellt	   enumeratet   syst   maxintt   set_hetatm_recordt   heterot	   get_rt_mxt   inverset   itemst   multiplyt   distancet   remove_waters_outside_radiust   id_strt
   is_unit_mxt   strt   fractionalizet   xyzt   orthogonalizet   RuntimeErrort   meant	   extract_bt   sort_waters_byt   sortt   renumbert   resseqt   link_to_previous(M   R   t   xray_structuret   paramst   verboset   return_pdb_hierarchyt   logR"   R   R   t	   pdb_atomst   atomt   n_atomsR5   t   mm_selectionR/   R0   t   new_sites_cartR2   R3   t	   asu_tablet	   mm_chainst   hetatm_chainst	   sel_cacheRI   t   i_seqsR   t   new_hierarchyt	   new_modelt   new_chain_i_seqst   new_hetatm_chainst   new_hetatm_chain_idst   new_start_resseqt   last_resseqt   hetatm_residue_groupst   hetatm_residue_chain_idsRN   t   loose_residuest   preserve_chainst   chain_idt   rgt   i_chaint   keep_residue_groupt   rg_atomsRS   R&   t   rg_copyt   new_atomt   old_atomRW   t	   n_deletedt   kt   is_watert   closest_distancet   closest_i_seqt   closest_rt_mxt   site_it   asu_dictt   rt_mx_i_invt   j_seqt   j_sym_groupst   site_jt   j_sym_groupt   rt_mxt   site_jit   dxyzt   j_seqst   hetatm_chainR   t   new_site_fract   waters_and_b_isot   agst   b_isot   watert   start_resseqRo   t   residue_groupt   n_atoms_new(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/sort_hetatms.pyt   sort_hetatmsc   sœ   !
	

"				
			
"	
		




&
			c         C   sõ  d d  l  } t |  ƒ d k s* d |  k rU t d | j j t ƒ j d d ƒ ƒ ‚ n  d d l m } d d l	 m
 } | j j d	 |  d
 t d d ƒ } | j j ƒ  } t | ƒ | j | j d d ƒ} | j d ƒ | j ƒ  }	 | j }
 | j } |	 d  k r6| j r6|
 d  k s | d  k r„t d d ƒ ‚ q„nN |
 d  k r]|	 d  k	 r]|	 j ƒ  }
 n  | d  k r„|	 d  k	 r„|	 j ƒ  } n  d  } | j s®| j d |
 d | ƒ } n  | j j } | j j d | ƒ } | d  k rä| } n  t d | d | d | d | j d t d | ƒ } | j  d  k rRt! j" j# t! j" j$ | j ƒ ƒ d d | _  n  t% | j  d ƒ } | j& rº| j j' j( ƒ  } t | ƒ d k rº| j) d j* | ƒ ƒ | j) d ƒ qºn  | j+ j, d | ƒ } | j- rˆd } } x° | j. ƒ  D]’ } | d d !d k r8| d k r(| j) d | ƒ n  d } } qïn8 | j/ d  ƒ rT| d! 7} n | j/ d" ƒ rp| d! 7} n  | j) d | ƒ qïWn | j) | ƒ | j) d# ƒ | j0 ƒ  | d$ | j  IJ| j1 ƒ  t2 d t! j" j3 | j  ƒ d% | j4 d& | j5 ƒ S('   Niÿÿÿÿi    s   --helps%  mmtbx.sort_hetatms model.pdb [options]

Rearrange non-macromolecular heteroatoms (waters and other ligands, by default)
into new chains having the same ID as the closest macromolecule chain.  Will
also renumber residues and sort waters by B-factor with default settings.

Full parameters:

%s
t   prefixs     (   t   file_reader(   t   crystalt   argst   master_phil_stringt   pdb_file_deft	   file_namet
   force_typeR   s9   Crystal symmetry information is required; please specify s)   the space_group and unit_cell parameters.t   space_group_infoRW   t   crystal_symmetryR   Rq   Rr   Rs   Rt   Ru   s   _sorted.pdbt   ws   
i   t   TERs   %s
t   HETATMi   t   ATOMt   ENDs   Wrote %sR   R   (6   R   R*   R   R   R   R	   t   as_strR"   R¯   t   cctbxR°   t   process_command_line_with_filest   workR!   t   validate_paramst   any_fileR´   t   check_file_typeR·   t   space_groupRW   R   t   ignore_symmetryR   R¶   t   symmetryt   file_objectRB   t   xray_structure_simpleR­   Rs   R.   t   output_filet   ost   patht   splitextt   basenamet   opent   preserve_remarkst   inputt   remark_sectiont   writet   joinR   t   as_pdb_stringt   remove_hetatm_ter_recordst
   splitlinest
   startswitht   closet   flushRA   t   abspathR   R   (   R±   t   outt   sorting_paramsR"   R¯   R°   t   cmdlineRr   t   pdb_int   pdb_symmRÄ   RW   t
   final_symmR   Rq   t   resultt   ft   remarkst   pdb_strt   n_hetatmt   n_atomt   line(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/sort_hetatms.pyt   runN  s”    
(	
									&		



	RA   c           B   s    e  Z d d d  „ Z d „  Z RS(   c         C   s5   t  |  t ƒ  ƒ | | g j d  ƒ d k s1 t ‚ d  S(   Ni   (   R   t   localst   countR   R,   (   t   selfR   R   R   R´   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/sort_hetatms.pyt   __init__¦  s    c         C   s.   |  j  d f g d |  j f d |  j f g f S(   Ns   Modified models   Number of macromolecule chainss   Number of heteroatom residues(   R´   R   R   (   Rë   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/sort_hetatms.pyt
   finish_job«  s    N(   t   __name__t
   __module__R   Rì   Rí   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/sort_hetatms.pyRA   ¥  s   c         C   s"   |  j  d  k r t d ƒ ‚ n  t S(   Ns%   Model file (file_name) not specified.(   R´   R   R   R@   (   Rr   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/sort_hetatms.pyRÁ   °  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±   RY   t   stdout(   Rë   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/sort_hetatms.pyRè   ¶  s    (   Rî   Rï   Rè   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/sort_hetatms.pyRð   µ  s   t   __main__i   (   t
   __future__R    t   libtbx.utilsR   R   R   R   R   R   t   libtbx.philRÊ   RY   R   R	   R
   R   R.   R@   R­   Rò   Rè   t   objectRA   RÁ   t   target_with_save_resultRð   Rî   t   argv(    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/command_line/sort_hetatms.pyt   <module>   s&   1!
	åW	