ó
Ú™xTc        	   @   s³  d  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 m	 Z	 d d l
 Z d d l Z d d l Z e j j d ƒ Z d	 d
 e e d e d d d „ Z e e ƒ  d „ Z e e d „ Z d e f d „  ƒ  YZ d e e d d	 d e ƒ  d „ Z d e f d „  ƒ  YZ e e e d d	 e e e ƒ  d „ Z d „  Z d d e ƒ  d „ Z d e f d „  ƒ  YZ d d „ Z e e j d „ Z  d e e d d d „ Z! d S(   sZ   
Tools for assembling an ensemble of related structures for local rebuilding by
homology.
iÿÿÿÿ(   t   division(   t+   slots_getstate_setstate_default_initializer(   t   Autot   adopt_init_args(   t   null_out(   t   easy_mpNsÔ   
min_identity = 0.95
  .type = float
xray_only = True
  .type = bool
single_chain = False
  .type = bool
min_resolution = 3.0
  .type = float
sort_by_resolution = True
  .type = bool
ligands = None
  .type = str
gffffffî?g      @gH¯¼šò×z>c	         C   s  | d k r t ƒ  } n  d d l m }	 d }
 | d k	 rñ t | ƒ d k rñ x¢ | D]— } | j d d ƒ j d ƒ } xs | D]k } |	 j d | d | d | d	 | ƒ } |
 d k rÀ t g  ƒ }
 n  |
 j	 g  | D] } | j
 ƒ  ^ qÍ ƒ q{ WqS Wn  |	 j d
 |  d | d | d | d	 | d | d | d | ƒ } g  } xE | D]= } | j
 ƒ  } |
 d k	 rj| |
 k rjq:n  | j | ƒ q:W| S(   sv   
  Finds closely related structures in the RCSB database, optionally limited to
  structures with specific ligands.
  iÿÿÿÿ(   t   rcsb_web_servicesi    t    t   ,t   resnamet   d_maxt	   xray_onlyt	   data_onlyt   sequencet   min_identityt   expectt   sort_by_resolutiont   logN(   t   NoneR   t   mmtbx.wwpdbR   t   lent   replacet   splitt   chemical_id_searcht   sett   updatet   lowert   sequence_searcht   append(   R   R   t   min_resolutionR   R   R   R   t   ligandsR   R   t   allowed_sett   lig_strt   resnamesR	   t   idst   pdb_idt   matching_idst   filtered(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt   fetch_similar_pdb_ids!   s@     		 1		c   	      C   sê   d d l  } | j j j |  ƒ g  } x¾ |  D]¶ } y4 | j j j d | d t d t j ƒ  ƒ \ } } Wn= t k
 r¥ } | s„ ‚  qâ | d | IJ| t	 | ƒ IJq, Xt
 | j ƒ  ƒ d k rÏ | d | IJq, | j | | f ƒ q, W| S(	   s_   
  Given a list of PDB IDs, load hierarchies for all of them (ignoring any
  unknown atoms).
  iÿÿÿÿNt   idt   allow_unknownst   local_caches   Error - skipping %s:i   s%   Warning: %s is multi-MODEL - skipping(   t   iotbx.pdb.fetcht   pdbt   fetcht   validate_pdb_idst   load_pdb_structuret   Truet   ost   getcwdt	   Exceptiont   strR   t   modelsR   (	   t   pdb_idst   fault_tolerantR   t   iotbxt   ids_and_hierarchiesR#   t   pdb_hierarchyt   xray_structuret   e(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt   load_pdb_modelsN   s$    c   	   	   C   s  d d l  m } m } t j j |  ƒ s. t ‚ g  } xÖ t j |  ƒ D]Å } t j j |  | ƒ } t j j | ƒ rœ | rœ | j	 t
 d | d | d t ƒ ƒ qD t j j | ƒ rD | rÌ | | ƒ d k rÌ qD n  | | d t ƒ} | j d k r	| j | | j f ƒ q	qD qD W| S(   sq   
  Load all models in the specified directory, returning a list of file names
  and iotbx.file_reader objects.
  iÿÿÿÿ(   t   any_filet   guess_file_typet   dir_namet   limit_extensionst	   recursiveR+   t"   raise_sorry_if_not_expected_format(   t   iotbx.file_readerR=   R>   R0   t   patht   isdirt   AssertionErrort   listdirt   joint   extendt   load_all_models_in_directoryR/   t   isfilet	   file_typeR   t   file_object(	   R?   R@   RA   R=   R>   t   file_names_and_objectst	   file_namet	   full_patht
   input_file(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyRJ   i   s$    		 t   related_chainc           B   s    e  Z d  Z d d d d g Z RS(   s}   
  Container for a protein chain (as hierarchy object) and metadata, extracted
  based on similarity to a target sequence.
  t   source_infot   chain_idR9   t   identity(   t   __name__t
   __module__t   __doc__t	   __slots__(    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyRR   ƒ   s   g{®Gáz”?c	         C   sG  d d l  }	 d d l }
 g  } x"|  j ƒ  j ƒ  D]} | j ƒ  r1 | j d t d d d t ƒ } |	 j j	 d | d | ƒ j
 ƒ  } | j d	 d g ƒ } | | | k r?|
 j j j ƒ  } |
 j j j ƒ  } | j | ƒ | j } | d k	 rö | } n  |
 j j j d
 | ƒ } | j | ƒ x® | j ƒ  D]  } | j ƒ  } |
 j j j d | j d | j ƒ } xZ t | ƒ D]L \ } } | r¤| d k sk| j j ƒ  d k r¤qkn  | j | j ƒ  ƒ qkW| j | ƒ q(W| j  ƒ  } | j! ƒ  | j" ƒ  j# | j$ ƒ  | j% ƒ  ƒ | j& t' d | d | j d | d | ƒ ƒ | r<Pq<q?q1 q1 W| S(   sž   
  Find all chains with at least the specified fractional sequence identity to
  the target, and extract as related_chain objects with new pdb hierarchies.
  iÿÿÿÿNt   padt   substitute_unknownt   Xt   pad_at_startt   seq_at   seq_bt
   skip_charsR'   t   resseqt   icodei    t    t   ARS   RT   R9   RU   (   Rc   Rd   ((   t   mmtbx.alignmentt   iotbx.pdb.hierarchyt
   only_modelt   chainst
   is_proteint   as_padded_sequenceR/   t   Falset	   alignmentt   alignt   extract_alignmentt   calculate_sequence_identityR+   t	   hierarchyt   roott   modelt   append_modelR'   R   t   chaint   append_chaint   residue_groupst   atom_groupst   residue_groupRa   Rb   t	   enumeratet   altloct   stript   append_atom_groupt   detached_copyt   append_residue_groupt   extract_xray_structuret   convert_to_isotropict   atomst   set_adps_from_scattererst
   scattererst	   unit_cellR   RR   (   R9   R   RS   t   first_chain_onlyt   remove_alt_confst   reset_chain_idR   t   min_identity_epsilonR   t   mmtbxR7   t   resultsRt   t	   chain_seqRl   RU   Rq   Rr   RT   t	   new_chainRx   Rw   t   new_rgt   kt
   atom_groupt   xrs(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt   find_similar_chainsŠ   sZ    				!
	
 t   extract_related_modelsc           B   s;   e  Z d  Z e d d e e ƒ  d „ Z d „  Z d „  Z	 RS(   s’   
  Multiprocessing wrapper for pulling matching chains out of a collection of
  PDB hierarchies (either from local files or from a PDB mirror).
  gffffffî?c   
      C   s˜   t  |  t ƒ  ƒ t j d |  j d t t |  j ƒ ƒ d |  j ƒ |  _	 xK t
 |  j	 ƒ D]: \ } }	 t |	 ƒ d k rV | d |  j | d IJqV qV Wd  S(   Nt
   fixed_funct   iterablet	   processesi    s     no matches for %s(   R   t   localsR   t   pool_mapt   examine_modelt   rangeR   t   sources_and_modelst   nproct   _resultsRy   (
   t   selfRš   R   R…   R‡   R   R›   R   RŽ   RŠ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt   __init__É   s    		c         C   s+   g  } x |  j  D] } | j | ƒ q W| S(   N(   Rœ   RI   (   R�   RŠ   t   result_list(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyRŠ   Ú   s     c         C   sV   |  j  | \ } } t d | d |  j d | d |  j d |  j d |  j d d  ƒ } | S(   NR9   R   RS   R   R…   R‡   R   (   Rš   R‘   R   R   R…   R‡   R   (   R�   t   i_modelRS   R9   t   related(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyR˜   ß   s    					N(
   RV   RW   RX   Rk   R   R   R   Rž   RŠ   R˜   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyR’   Ä   s   
	c
         C   s€   t  d |  d | d | ƒ }
 g  |
 D] \ } } | | j f ^ q" } t d | d | d | d | d | d	 | d
 |	 ƒ j ƒ  } | S(   sA   Combines load_all_models_in_directory and extract_related_models.R?   R@   RA   Rš   R   R   R…   R‡   R›   R   (   RJ   Rp   R’   RŠ   (   R?   R   R@   RA   R…   R‡   R   t   return_original_filesR›   R   RN   RO   RM   Rš   R¡   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt    load_related_models_in_directoryë   s    	%c         K   s7   t  d |  d | j d t ƒ  ƒ ƒ } t | | � j ƒ  S(   sÉ   
  Combines load_pdb_models and extract_related_models, with multiprocessing
  support.  Functionally equivalent to load_related_models_in_directory but
  with models from the PDB (local or remote).
  R5   R   (   R<   t   getR   R’   RŠ   (   R5   t   kwdsRš   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt   load_related_models_from_pdb  s    c         C   s[  d d l  m } t | ƒ j d k r7 | j | ƒ } n  t | ƒ j d k r^ | j | ƒ } n  g  } xð t t ƒ D]â \ } } t }	 d }
 | d k	 rÚ | d k rª t	 }	 q%t
 | j ƒ  ƒ }
 |
 t
 | ƒ k r%t	 }	 q%nK | d k	 r%| d k	 r%t
 | j ƒ  ƒ }
 |
 t
 | ƒ k r"t	 }	 q"q%n  |	 rF| d | | |
 f IJqq | j | ƒ qq W| S(   s•   
  Return a list of indices for those crystal symmetries which either match the
  only_space_group parameter, or do not match exclude_space_group.
  iÿÿÿÿ(   t   sgtbxt   infos#     %s is in space group %s, skippingN(   t   cctbxR§   t   typeRV   t   space_group_infoRy   t
   symmetriesRk   R   R/   R3   R   (   t   crystal_symmetriest   source_infost   exclude_space_groupt   only_space_groupR   R§   t	   selectionRŽ   t   symmt   skipt   sg(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt   selection_by_symmetry  s0    		t	   ensemblerc           B   sJ   e  Z d  Z d e d e e d e ƒ  d „ Z d „  Z d „  Z	 d „  Z
 RS(   s½   
  Superpose a collection of single-chain models on a reference chain.  This is
  intentionally very limited in function, but flexible with respect to the
  choice of atoms to superpose.
  g      à?c
   
      C   s$   t  |  t ƒ  ƒ |  j d | ƒ d  S(   Nt   atom_selection_string(   R   R–   t   realign(
   R�   t   reference_hierarchyt   related_chainsR·   t   backbone_onlyt   calpha_onlyR›   t	   sieve_fitt   frac_discardR   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyRž   <  s    
c         C   sº  | |  _  |  j j ƒ  } | j ƒ  |  j  d  k	 rS |  j rJ |  j t k s† t ‚ n3 |  j rh d |  _  n |  j r} d |  _  n	 d |  _  d d l	 m
 } |  j j ƒ  } | j |  j  ƒ |  _ |  j j t ƒ d k sØ t ‚ g  |  _ | j ƒ  } |  j j ƒ  j ƒ  } xŒ | j ƒ  D]~ } | j ƒ  j ƒ  } xc | D][ }	 |  j |	 j sJq.n  | j ƒ  }
 |  j j d |
 |	 j j ƒ  f ƒ | j |	 j ƒ q.WqWt | ƒ d k s©t ‚ | j ƒ  } | j | ƒ |  _ t j d t  t |  j! ƒ ƒ d	 |  j" d
 |  j# ƒ } g  |  _$ x° t% | ƒ D]¢ \ } } |  j! | j& } | d  k ri|  j' d |  j! | j( |  j! | j) f IJqn  |  j$ j | ƒ | j ƒ  } | j* j+ | j ƒ  | j, j+ } | j- | ƒ qWd  S(   Ns   name CAs0   name CA or name CB or name C or name N or name Ot   alliÿÿÿÿ(   t   flexi    s   %s %sR”   R“   R•   s   No LSQ fit for model %s:%s(.   R·   R¹   R�   t   reset_i_seqR   R¼   R»   R/   RF   t   scitbx.array_familyRÀ   t   atom_selection_cacheR±   t   atom_selectiont   countt	   atoms_reft   size_tRg   t
   only_chainRv   t   only_atom_groupt   i_seqt   residR   t   nameR{   R   t   extract_xyzt   selectt   reference_sitesR   R—   R™   Rº   t   align_modelR›   t   selection_movedRy   R9   R   RS   RT   t   rt   elemst   tt   set_xyz(   R�   R·   t	   ref_atomsRÀ   t	   sel_cachet   ref_selt	   ref_chainRx   t   rg_atomst   atomRË   t	   sites_reft   sites_movedRŽ   t   lsq_fitRp   t	   pdb_atomst
   sites_cart(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyR¸   I  sV    	
"				#			
! c         C   sÿ  d d l  m } d d l m } |  j | j } | j ƒ  } | j ƒ  | j ƒ  } | j	 |  j
 ƒ } | j ƒ  j ƒ  } | j ƒ  }	 | j ƒ  }
 x¤ | j ƒ  D]– } x� | j ƒ  j ƒ  D]y } | | j sÍ q´ n  | j ƒ  } d | | j j ƒ  f } | |  j k r´ |
 j | j ƒ |	 j |  j j | ƒ ƒ q´ q´ Wq› Wt |	 ƒ d k r`|  j
 d  k	 s\t ‚ d  St |	 ƒ d k rŠt |	 ƒ t |
 ƒ k s�t ‚ | j ƒ  } | j |
 ƒ } |  j j |	 ƒ } |  j rå| j d | d | d |  j ƒ S| j d	 | d
 | ƒ Sd  S(   Niÿÿÿÿ(   RÀ   (   t	   superposes   %s %si    t   sites_fixedt   sites_movingR¾   RÏ   t   other_sites(    RÂ   RÀ   t   scitbx.mathRá   Rº   R9   R�   RÁ   RÃ   R±   R·   Rg   RÈ   RÇ   Rv   RÉ   RÊ   RË   RÌ   R{   RÆ   R   t   indexR   R   RF   RÍ   RÎ   RÏ   R½   R¾   t   least_squares_fit(   R�   R    RÀ   Rá   t   hierarchy_movingt	   mov_atomsR×   t   mov_atom_selectiont	   mov_chaint   sel_reft   sel_movRx   RÛ   RË   t   ref_namet   xyz_movt	   sites_movRÜ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyRÐ   z  sD    
$0		
	c         C   s;   g  } x( |  j  D] } | j |  j | j ƒ q Wt | ƒ S(   N(   RÑ   R   Rº   R9   t   join_hierarchies(   R�   t   hierarchiesRŽ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt   as_multi_model_hierarchyŸ  s    N(   RV   RW   RX   R   R   Rk   R   Rž   R¸   RÐ   Ró   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyR¶   6  s   	1	%c   	      C   s²   d d  l  } | j j j ƒ  } x� t |  ƒ D] \ } } | d } | d  k	 rZ | | } n  | j j j d t | ƒ ƒ } | j | ƒ | j	 ƒ  j
 ƒ  j ƒ  } | j | ƒ q+ W| S(   Niÿÿÿÿi   R'   (   Rf   R+   Rp   Rq   Ry   R   Rr   R3   Rs   Rg   RÈ   R}   Ru   (	   Rò   t	   model_idsR7   Rq   RŽ   Rp   t   model_idRr   Rt   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyRñ   ¥  s    
c         C   s¦  | d  k rot d | d | j d | j d | j d | j d | ƒ } | j d  k	 r¯ g  | j D] } | j ƒ  j d ƒ ^ q^ } x- | D]" }	 |	 | k r† | j	 |	 ƒ q† q† Wn  t
 | ƒ d k rÅ d  S| d	 t
 | ƒ IJd }
 xB |
 t
 | ƒ k  r#| d
 d j | |
 |
 d !ƒ IJ|
 d 7}
 qâ Wt d | d | d | j d | j d |  j ƒ  j ƒ  j d | d | ƒ } n	 t ƒ  ‚ | d  k	 sŠt ‚ t d |  d | d | j ƒ S(   NR   R   R   R   R   R   R   i    s   %d PDB IDs retrieved:s     %sR   i   R5   R…   R‡   R›   R¹   Rº   (   R   R&   R   R   R   R   t   exclude_idsR   R   t   removeR   RH   R¦   t   single_chainRg   RÈ   R'   t   NotImplementedErrorRF   R¶   R›   (   R¹   t   search_directoryR   t   paramsR›   t   outR5   t   sRö   t   other_idt   iRº   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt   extract_and_superpose²  sD    					+"			Rd   R   c            sà  d d l  m } d d  l ‰ | j ‰  g  } ‡ ‡ f d †  }	 ‡  ‡ f d †  }
 xŠ|  j ƒ  j ƒ  D]v} | j ƒ  rb d } | j ƒ  } xO| t | ƒ k  rÔ|
 | | d ƒ } | d  k	 rÇ| g } x~ t
 d t ˆ ƒ ƒ D]g } | | } | t | ƒ k rg  } Pn  |
 | d | d | ƒ} | d  k	 r8| j | ƒ qØ g  } PqØ Wt | ƒ d k rÇt | ƒ t ˆ ƒ k sst ‚ |	 ƒ  } | j ƒ  j ƒ  } x¨ t | ƒ D]š \ } } | j ƒ  j } | j ƒ  j } | d  k	 râd	 | | } n  | d  k	 r÷| } n  ˆ j j j d
 | d | ƒ } | j | ƒ | j | j ƒ  ƒ q›Wd  } | j ƒ  } | rc| j d ƒ } n | r{| j d ƒ } n  | d  k	 r´| j t ƒ d k s¢t ‚ | j | ƒ } n  | j | ƒ qÇn  | d 7} q‰ Wqb qb W| S(   Niÿÿÿÿ(   t   amino_acid_codesc             sZ   ˆ  j  j j ƒ  }  ˆ  j  j j ƒ  } ˆ  j  j j d ˆ ƒ } |  j | ƒ | j | ƒ |  S(   NR'   (   R+   Rp   Rq   Rr   Rt   Rs   Ru   (   Rq   Rr   Rt   (   R7   R‡   (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt   make_hierarchyê  s    c            sU   xN |  | j  ƒ  D]< } ˆ  j | j ƒ } | ˆ | k sI ˆ | d k r | Sq Wd  S(   Nt   .R\   (   R  R\   (   Rw   R¤   R	   R   (   Rv   t   i_resRÊ   t   next_atom_groupt   seq_code(   t	   aa_3_as_1R   (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt   find_next_matching_atom_groupñ  s
     i    i   R  RÊ   s   %4dRa   Rb   s0   name C or name O or name N or name CA or name CBs   not (element H or element D)(   t	   iotbx.pdbR  Rf   t   one_letter_given_three_letterRg   Rh   Ri   Rv   R   R   R™   R   RF   RÈ   Ry   t   parentRa   Rb   R+   Rp   Rx   R~   R|   R}   RÃ   R±   RÅ   R/   RÎ   (   R9   R   t   renumber_fromt   mainchain_onlyt   remove_hydrogensR‡   t   reset_icodeR  t	   fragmentsR  R  Rt   R  Rv   R�   t   fragmentRÊ   t   j_resR  Rq   RŒ   t   i_groupRa   Rb   t   new_residue_groupt   selR×   (    (   R  R7   R‡   R   sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt%   extract_peptide_fragments_by_sequenceÞ  sn    		
				("   RX   t
   __future__R    t   libtbxR   R   R   t   libtbx.utilsR   R   t   libtbx.philR0   t   syst   philt   parset   master_philR/   Rk   R   R&   R<   RJ   RR   R‘   t   objectR’   R£   R¦   Rµ   R¶   Rñ   t   stdoutR   R  (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/building/make_library.pyt   <module>   sf   		$	2*	!o)