ó
šxTc           @   s  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 a a e d „ Z d „  Z d e j e	 j f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ e d „ Z d „  Z d d „ Z d „  Z d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   divisionN(   t   object_oriented_patterns(   t	   interface(   t   Sorryc         C   sÎ   t  d  k rÊ t j j t ƒ a  t a t j	 j
 d ƒ } t j j | ƒ rÊ t j j d | ƒ } t  j d | d t ƒ \ a  } |  r¾ t | ƒ d k r¾ x( | D] } t j j d | j ƒ qš Wn  t a qÊ n  t  S(   Ns   include/phaser_defaults.paramst	   file_namet   sourcet   track_unused_definitionsi    s      WARNING: unknown parameter %s
(   t   master_phil_t   Nonet   iotbxt   philt   read_defaultt   __file__t   master_philt   master_params_t   libtbxt   envt   under_buildt   ost   patht   isfilet   parset   fetcht   Truet   lent   syst   stderrt   write(   t   validatet   defaults_filet   defaults_philt   unusedt   param(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyR      s    c           C   s   t  ƒ  S(   N(   R   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt   master_params"   s    t   _index_phaserc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s–   t  |  d ƒ s i  |  _ n  | |  j k rk |  j | ƒ } t | t ƒ rU | d } n  t | ƒ |  j | <n  |  j | } | j d  k r‹ g  S| j Sd  S(   Nt   _stylesi    (   t   hasattrR#   t   get_scope_by_namet
   isinstancet   listt   phaser_stylet   modeR   (   t   selft
   scope_namet
   phil_scopet   style(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt   get_phaser_modes&   s    c         C   sÛ   t  |  d ƒ s i  |  _ n  | |  j k rÐ g  } |  j d ƒ } x~ | j D]s } | j d  k	 r‚ | j j ƒ  } d | k r‚ qI q‚ n  | j ƒ  } |  j | ƒ } t	 | | ƒ rI | j
 | ƒ qI qI W| |  j | <n  |  j | S(   Nt   _keywords_by_scopes   phaser.keywordss   phaser:noindex(   R$   t   _keywords_by_modeR%   t   objectsR-   R   t   splitt	   full_pathR.   t   match_modest   append(   R*   R)   t   current_keywordst   keyword_philt   objectt   object_stylesR+   t   keyword_modes(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt   get_mode_keywords4   s     	c         C   sŒ   |  j  d ƒ } t | t ƒ r@ g  | D] } | j ƒ  ^ q% } n | j ƒ  g } g  } x0 | D]( } | j d  k	 r\ | j | j ƒ q\ q\ W| S(   Ns   phaser.ensemble(   R%   R&   R'   t   extractt   model_idR   R5   (   R*   t   ensemble_philt   ept	   ensemblest	   model_idst   ense(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt   get_ensemble_idsF   s    "(   t   __name__t
   __module__R.   R;   RC   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyR"   %   s   		R(   c           B   s   e  Z d  „  Z d „  Z RS(   c         C   s³   | j  d  k	 r¯ x� | j  j ƒ  D]‰ } | j d ƒ r | j d ƒ } t | ƒ d k r[ q q¨ t | ƒ d k r„ t |  | d t ƒ q¨ t |  | d | d j d ƒ ƒ q q Wn  d  S(   Ns   phaser:t   :i   i   t   ,(   R-   R   R2   t
   startswithR   t   setattrR   (   R*   t   phil_objectt
   style_wordt   words(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt   __init__S   s    c         C   s   d  S(   N(   R   (   R*   t   name(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt   __getattr___   s    (   RD   RE   RM   RO   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyR(   R   s   	c         C   sn   | d  k r | S|  | k r  t SxG | D]? } | d k r= t S| j d ƒ r' |  j | d  ƒ rf t Sq' q' Wt S(   Nt   *iÿÿÿÿ(   R   R   t   endswithRH   t   False(   t   current_modeR:   t   inherit_modet
   valid_mode(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyR4   b   s    c         C   sn   d d l  m } d } xQ |  j ƒ  D]C } y | j d | ƒ \ } } Wn t k
 r[ q# q# X| d 7} q# W| S(   Niÿÿÿÿ(   t	   tbx_utilsi    t   texti   (   t   phaserRV   t   remark_sectiont"   parse_phaser_model_identity_remarkt
   ValueError(   t   pdb_inRV   t   found_remarkst   rt   midt   identity(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt   check_for_model_identity_remarkq   s    c         C   s‡
  | d  k r9 t j ƒ  } t j t | d t j j ƒ} n  |  j j	 } t
 j d ƒ } |  j j j j d  k r{ t d ƒ ‚ n0 | j |  j j j j ƒ d  k	 r« t d ƒ ‚ n  t | | j d ƒ ƒ r|  j j d  k rè t d | ƒ ‚ q|  j j d  k rt d | ƒ ‚ qnôt | | j d ƒ ƒ rt |  j j ƒ d	 k rSt d
 | ƒ ‚ n' t |  j j ƒ d k rzt d ƒ ‚ n  |  j j d	 } t | j ƒ d k r®t d ƒ ‚ n  xS|  j j D]B} | j } d  | j | j g k rõt d d ƒ ‚ n  x| j D]ú } | j } | j j d ƒ }	 t |	 ƒ d k r5n  | j d  k	 r¢| j d  k	 rft d d ƒ ‚ qù| j d k  s„| j d k rùt d d d d ƒ ‚ qùqÿ| j d  k	 ré| j d k  sÏ| j d k rùt d d d ƒ ‚ qùqÿt d d ƒ ‚ qÿWq»Wn  t }
 t | | j d ƒ ƒ rég  } t |  j j  ƒ d	 k rd|  j j! d  k rw|  j j" d  k rwt d  d! ƒ ‚ n² |  j j! d  k	 r®|  j j" d  k	 r®t d" d# ƒ ‚ n{ |  j j! d  k	 röt |  j j! ƒ t |  j j  ƒ k r)t d$ ƒ ‚ q)n3 t |  j j" ƒ t |  j j  ƒ k r)t d% ƒ ‚ n  | j# d& ƒ t |  j j$ ƒ d	 k rdt d' d( ƒ ‚ qdn  x‚t% |  j j$ ƒ D]k\ } } | j& } | d  k r¯t d) | d ƒ ‚ n  | | k rÎt d* | ƒ ‚ n  | j# | ƒ t | j' ƒ d	 k rà| j( d  k	 rt d+ d, | ƒ ‚ n  x±t% | j' ƒ D]³ \ } } | j) d  k rbt d- d. | | d f ƒ ‚ n  | j* s©| j+ | j, g j- d  ƒ d k rÙt d/ d0 | j) ƒ ‚ qÙq&t | j' ƒ d k r&t d1 d2 d3 d4 ƒ ‚ q&q&Wnê | j( d  k	 rÊ| } | j. d  k rt d5 | ƒ ‚ n  | j/ d  k s_t | j/ ƒ d6 k s_| j0 d  k s_t | j0 ƒ d6 k rzt d7 d8 d9 | ƒ ‚ n  d  | j1 | j2 g k s°| j1 d	 k rÊ| j2 d	 k rÊt d: d; | ƒ ‚ qÊn  | j3 d  k	 rwt4 }
 qwqwWn  t | | j d< ƒ ƒ rËt |  j j  ƒ d	 k r.|  j j5 d k r.n' t |  j j ƒ d	 k rUt d= ƒ ‚ n  xs t% |  j j ƒ D]\ \ } } | j6 d  k ršt d> | d ƒ ‚ qh| j7 d  k rht d? d. | d ƒ ‚ qhqhWn  t | | j d@ ƒ ƒ rÐ	|
 rìn3|  j j8 d  k	 rt4 }
 n|  j j9 j: d  k	 rMt |  j j9 j; ƒ d	 k r	t dA dB ƒ ‚ q	nÒ t |  j j9 j; ƒ d	 k r	x´ t% |  j j9 j; ƒ D]š \ } } dC dD dE dF g } g  | D] } t< | | ƒ ^ q£} | j- d  ƒ d6 k ròt dG dH dI | d ƒ ‚ n  | j= d  k r~t dJ | d ƒ ‚ q~q~Wn  x® t% |  j j9 j> ƒ D]” \ } } | j? d  k s_	| j= d  k rz	t dK dL | d ƒ ‚ n  dM dN l@ mA } y | jB | j? ƒ } Wq5	tC k
 rÈ	} t dO | j? ƒ ‚ q5	Xq5	Wn  t | | j dP ƒ ƒ r@
|  j j jD jE dQ k r@
t |  j j jD jF ƒ d	 k r@
t dR dS dT dU dV ƒ ‚ q@
n  | d[ k rt
|  j jG d  k rt
t dY dZ ƒ ‚ qt
n  |  jH jI rƒ
n  t4 S(\   NR   s   [^a-zA-Z0-9_\-]{1,}s+   Please define a root filename for this run.s8   The root filename must be alphanumeric (underscores OK).s   phaser.hklins/   You must specify a reflections file in mode %s.s3   You must specify reflection data labels in mode %s.s   phaser.crystali    s"   Crystal data required for mode %s.i   s(   Only one crystal supported at this time.s+   Only one wavelength supported at this time.s5   For each crystal, you may supply heavy-atom sites as s-   EITHER a PDB file OR 'ha' file, but not both.RG   i   s4   Both the energy and the wavelength for this dataset s;   have been provided - please enter only one of these values.g      à?s5   The specified data collection wavelength (%g) is not s   suitable for s;   SAD phasing; please make sure you have entered the correct s2   value in Angstroms, or provide the energy instead.iè  i N  s1   The specified energy is not a suitable value for s9   value in eV (not KeV), or provide the wavelength instead.s4   Please specify either the wavelength (in Angstroms) s'   or the energy (in eV) for this dataset.s   phaser.ensembles5   You must specify either the RMSD or the identity for s   the single-model input.s2   Either the model RMSDs or the sequence identities s   must be defined, but not both.s(   You must specify an RMSD for each model.s4   You must specify a sequence identity for each model.t   ense_1s2   You may not specify the single-model input search s9   parameter (phaser.model) when the ensemble scope is used.s"   Missing model ID for ensemble #%d.s   Duplicate model ID '%s'.s/   Please specify either PDB files or an MTZ file s   for ensemble %s, but not both.s0   Missing PDB file for ensemble %s coordinate set s   %d.s,   Please specify EITHER an RMSD OR fractional s   identity for PDB file %s.s1   Reading of model variances from PDB REMARK lines s;   is only allowed when a single multi-model PDB file is used s<   as input.  (Hint: you can use the Ensembler tool to prepare s   a multi-model ensemble.)s,   You must specify labels for map data for %s.i   s2   You must specify the extent and center of the map s<   used for ensemble %s; this must both take the form of three s%   decimal numbers between zero and one.s2   Please specify protein and/or nucleic acid masses s   for ensemble %s.s   phaser.searchs   No search procedure defined.s.   No model IDs supplied for search component %d.s7   Please specify a number of copies for search component s   phaser.compositions4   Please specify EITHER fractional solvent content OR s$   individual components, but not both.t   sequence_filet   sequencet   nrest   mws2   Please specify ONE source of mass information for s=   chain #%d: sequence file, sequence string, # of residues, or s   molecular weight (Da).s4   Please specify a number of copies for component #%d.s.   Please specify an element and number of atoms s   for heavy atom %d.iÿÿÿÿ(   t   tiny_pses   Invalid element symbol '%s'!s   phaser.keywords.sgalternativeR'   s4   You have set Phaser to try listed alternative space s=   groups, but no additional space groups were defined.  Please sC   enter additional space group symbols (click the 'More' button next s?   to the control on the "Search tab"), or change the space group s   search settings.t   MR_FTFt   MR_BTFs8   You must specify a previous solution to start from when s   running a translation search.(   Rh   Ri   (J   R   R   R   t   phil_interfacet   indexR	   R
   R   RX   R)   t   ret   compilet   keywordst   generalt   rootR   t   searchR4   R.   t   hklint   labinR   t   crystalt   datasett   xtal_idt   pdb_filet   ha_filet   wave_idR2   t
   wavelengtht   energyRR   t   modelt
   model_rmsdt   model_identityR5   t   ensemblet	   enumerateR=   t   coordinatest	   map_hklint   pdbt   read_variance_from_pdb_remarkst   rmsdR`   t   countt
   map_labelst
   map_extentt
   map_centret   map_protein_mwt   map_nucleic_mwt   fractional_compR   t   search_copiesR@   t   copiest   seq_filet   compositiont   solventt   chaint   getattrt   numt   atomt   elementt   cctbx.eltbxRg   t   tablet   RuntimeErrort   sgalternativet   selectt   testt   solutiont   density_modificationt   dm(   t   paramsRk   t   working_philR)   t	   not_alnumt   xtalt   xtalidRu   t   waveidt   labelst   have_compositiont   have_modelst   iR   R=   t   jt   coordst   enst	   componentR’   t	   mass_attrRN   t   massesR•   Rg   R˜   t   e(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt   validate_params}   sD   !		$$		!$$	"""c         C   s•   g  } xˆ t  j |  ƒ D]w } | j | ƒ r t j d | ƒ } | d  k	 r� | j t  j j |  | ƒ d | j	 d ƒ | j	 d ƒ f f ƒ q� q q W| S(   Ns   \.([0-9])\.([0-9])\.pdbs"   Solution %s, Component %s ensemblei   i   (
   R   t   listdirRH   Rl   Rq   R   R5   R   t   joint   group(   t   dir_nameRp   t   ensemble_filesR   t   match(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt   get_ensemble_pdb_output4  s    !t   phaser_resultc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   sL   | |  _  | j j |  _ | j j j j |  _ | j j j |  _ | |  _ d  S(   N(   t   phaser_objectRX   R)   Rn   Ro   Rp   t   handt   run_dir(   R*   R    Rº   R¼   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyRM   A  s
    	c         C   s   |  j  S(   N(   Rº   (   R*   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt   get_phaser_objectH  s    c         C   sG  g  } |  j  j d ƒ rTt j j |  j |  j d ƒ } t j j |  j |  j d ƒ } t j j | ƒ r– t j j | ƒ d k r– | j	 | d f ƒ n  t j j | ƒ rÖ t j j | ƒ d k rÖ | j	 | d f ƒ n  t j j |  j |  j d ƒ } t j j | ƒ r5t j j | ƒ d k r5| j	 | d f ƒ n  | j
 t |  j |  j ƒ ƒ n¨|  j  j d	 ƒ rü|  j } | d k } | d k } | rÓt j j |  j |  j d ƒ } t j j |  j |  j d ƒ } t j j | ƒ rt j j | ƒ d k r| j	 | d f ƒ n  t j j | ƒ rKt j j | ƒ d k rK| j	 | d f ƒ n  t j j |  j |  j d ƒ } t j j | ƒ rªt j j | ƒ d k rª| j	 | d f ƒ n  | rùt j j |  j |  j d ƒ } t j j |  j |  j d ƒ }	 t j j | ƒ r.t j j | ƒ d k r.| j	 | d f ƒ n  t j j |	 ƒ rnt j j |	 ƒ d k rn| j	 |	 d f ƒ n  t j j |  j |  j d ƒ }
 t j j |
 ƒ rÐt j j |
 ƒ d k rÐ| j	 |
 d f ƒ qÐqùqü| rüt j j |  j |  j d ƒ } t j j |  j |  j d ƒ } t j j | ƒ rWt j j | ƒ d k rW| j	 | d f ƒ n  t j j | ƒ r—t j j | ƒ d k r—| j	 | d f ƒ n  t j j |  j |  j d ƒ } t j j | ƒ rùt j j | ƒ d k rù| j	 | d f ƒ qùqün  t j j |  j |  j d ƒ } t j j | ƒ rC| j	 | d f ƒ n  | S(   Nt   MRs   .1.mtzs   .1.pdbi    s   Phases and mapss   Output models	   .1_dm.mtzs   Density-modified mapt   EPt   botht   offt   ons   Heavy-atom sitess   .1.hand.mtzs   .1.hand.pdbs   Phases and maps (inverse hand)s   Heavy-atom sites (inverse hand)s#   Density-modified map (inverse hand)s	   _data.mtzs   Processed input data(   RÀ   RÁ   (   RÀ   RÂ   (   R)   RH   R   R   R³   R¼   Rp   R   t   getsizeR5   t   extendR¸   R»   (   R*   t   output_filest   mtz_fileRw   t   dm_fileR»   t   use_original_handt   use_inverse_handt	   mtz_file2t	   pdb_file2t   dm_file2t	   data_file(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt   get_output_filesK  sd    ***	*********c         C   sÍ   |  j  ƒ  } g  } t |  j d ƒ rM |  j j ƒ  } | j d d | f ƒ n  t |  j d ƒ rˆ |  j j ƒ  } | j d d | f ƒ n; t |  j d ƒ rÃ |  j j ƒ  } | j d d | f ƒ n  | | f S(   Nt	   getTopLLGs   Log-likelihood gains   %.2ft   getTopTFZeqt   TFZs   %.1ft	   getTopTFZ(   RÎ   R$   Rº   RÏ   R5   RÐ   RÒ   (   R*   RÅ   t   statst   llgt   tfz(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt
   finish_job  s    (   RD   RE   RM   R½   RÎ   RÖ   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyR¹   @  s   			4(    t
   __future__R    t
   iotbx.philR	   R   R   t   oopt   libtbx.philR   Rj   t   libtbx.utilsR   t   libtbx.load_envR   Rl   R   R   R   R   RR   R   R!   t   injectorRk   R"   R8   R(   R4   Ra   R±   R¸   R¹   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/phenix_interface/__init__.pyt   <module>   s$   
	-	·	