ó
Ù™xTc           @   s)  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z d e	 f d „  ƒ  YZ
 d e	 f d „  ƒ  YZ d	 e	 f d
 „  ƒ  YZ d e	 f d „  ƒ  YZ d e	 f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   divisionN(   t   easy_pickle(   t   Sorryt   motifc           B   s8   e  Z d  i  i  d „ Z g  g  d d d  e d  d „ Z RS(   t    c         C   s(   | |  _  | |  _ | |  _ g  |  _ d  S(   N(   t
   motif_namet   residue_namest   superpose_ordert   residues(   t   selfR   R   R   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   __init__   s    			c   	      C   s2   t  | | | | | | | ƒ } |  j j | ƒ | S(   N(   t   residueR   t   append(	   R	   t   allowed_resnamet   banned_resnamet   cis_or_transt   sequence_movet	   bond_movet   end_of_motift   indext   new_residue(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   add_residue$   s    N(   t   __name__t
   __module__R
   t   Nonet   FalseR   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyR      s   	R   c           B   sG   e  Z d  „  Z e e e d d d „ Z g  g  d d d e d d „ Z RS(   c         C   s   | j  t k r t Sd  S(   N(   t   resnamet   reslistt   True(   R	   R   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   check_resname.   s    R   c         C   s,   t  | | | | | ƒ } |  j j | ƒ | S(   N(   t   bondt   bondsR   (   R	   t   requiredt   bannedt   allow_bifurcatedt   src_atomt	   trg_indext   new_bond(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   add_bond2   s    c         C   sL   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ g  |  _ d  S(   N(   R   R   R   R   R   R   R   R   (   R	   R   R   R   R   R   R   R   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyR
   8   s    							N(   R   R   R   R   R   R&   R   R
   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyR   -   s   			R   c           B   s5   e  Z d e d e d  „ Z e e e d d d „ Z RS(   c         C   s#   |  j  j t | | | | ƒ ƒ d  S(   N(   t	   trg_atomsR   t   target_atom(   R	   t   atomnamet   anyatomt   seqdistt
   anyseqdist(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   add_target_atomH   s    R   c         C   s:   | |  _  | |  _ | |  _ | |  _ g  |  _ | |  _ d  S(   N(   R    R!   R"   R#   R'   R$   (   R	   R    R!   R"   R#   R$   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyR
   L   s    					N(   R   R   R   R   R-   R   R
   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyR   G   s   		R(   c           B   s   e  Z d e d e d  „ Z RS(   c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   R)   R*   R+   R,   (   R	   R)   R*   R+   R,   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyR
   Y   s    			N(   R   R   R   R   R
   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyR(   X   s   t   motif_instancec           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s$   t  |  j ƒ |  j k r t St Sd  S(   N(   t   lenR   t   needed_lengthR   R   (   R	   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   is_completec   s     c         C   sB   x; |  j  j ƒ  D]* } x! | D] } | | j k r t Sq Wq Wt S(   N(   R   t   valuest   measuresR   R   (   R	   t   kinorderR   t   needed_measure(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   has_all_measuresh   s
    c         C   s   | |  j  | <d  S(   N(   R   (   R	   R   R   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   add_to_instanceo   s    c   
      C   sü   g  } xÕ | j  D]Ê } | j  | } |  j | } | j } t | j ƒ } x� | D]‡ } t | j ƒ d k r« | j | ƒ r« d | d | d | d | j | ƒ d }	 n d | d | d | d }	 | j |	 ƒ qO Wq Wd d j	 | ƒ d |  _
 d  S(	   Ni   s   (chain s    and resseq s
    and name s    and altloc t   )s   "s    or (   R   R   t   chaint   strt   resnumR/   t   altst   firstaltR   t   joint   superpose_thus(
   R	   R   t   superpose_listR   t   atomsR   R9   t   resseqt   atomt
   atom_order(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   build_superpositionr   s    	$2c         C   s4   t  | j ƒ |  _ i  |  _ | j |  _ d |  _ d  S(   NR   (   R/   R   R0   R   t   namesR?   (   R	   t   fingerprint(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyR
   ‚   s    	(   R   R   R1   R6   R7   RE   R
   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyR.   b   s
   				c         C   sð   t  } i  } t | ƒ } |  j ƒ  } | j ƒ  x | D] } g  | | j <q5 Wx� | D]• } |  | } x‚ | D]z } t | ƒ }	 t | | |	 ƒ }
 |
 r´ d  }	 | t k rj |
 GHqj qj qj |	 j	 ƒ  rj |	 j
 | ƒ | | j j |	 ƒ qj qj WqS W| S(   N(   R   t   fetch_fingerprintst   keyst   sortR   R.   t   check_for_motifR   R   R1   RE   R   (   t   proteint   motif_name_listt   debugt   found_motifst
   motif_listR   R   t   residR   t	   candidatet   failed(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   check_protein‹   s*    

 c         C   s  d } t  } xê | sø |  j | } | s3 d | j St | | ƒ } | rL | SxF | j D]; } t | | ƒ } | ru | St | | | ƒ }	 |	 rV |	 SqV Wt | j | | ƒ }
 |
 r´ |
 S| j | j | ƒ | j	 rÙ t
 } q | d 7} t | | | ƒ } q Wt j j d |  j d ƒ d S(   Ni    s   Missing residue for index i   s   finished motif s   
(   R   R   R   t   fail_residue_checkR   t   fail_forbidden_bond_checkt   fail_required_bond_checkt   fail_index_matchR7   R   R   t   do_movet   syst   stderrt   writeR   (   R   t   active_residueRR   t   motif_indext   motif_completet   motif_residuet   fail_residue_checksR   t   fail_forbidden_bond_checkst   fail_required_bond_checkst   fail_index_checks(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyRK   ­   s4    			
c         C   sF  | } |  j  r  |  j  } | d k rZ xu | d k rV | j s@ d  S| j } | d 8} q' WnB | d k  rœ x3 | d k  r˜ | j s‚ d  S| j } | d 7} qi Wn  | S|  j r|  j } y | j | SWqBt k
 r	t j j	 d |  j d ƒ t j j	 d ƒ t j
 ƒ  qBXn5 t j j	 d |  j d ƒ t j j	 d ƒ t j
 ƒ  d  S(   Ni    i   s6   
Tried to move to a residue not yet indexed at index "s   "
s3   Please check fingerprint definition. Exiting . . .
s*   
No move specified for residue indexed as"(   R   t   nextresR   t   prevresR   R   t   KeyErrorRZ   R[   R\   t   exitR   (   t   check_residueR]   t   motif_in_progresst   new_rest	   move_distt
   move_index(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyRY   Í   s6    								c         C   s�   | j  ƒ  d d !} |  j rB | j ƒ  |  j k rB | j  ƒ  d Sn  |  j rq | j ƒ  |  j k rq | j  ƒ  d Sn  |  j r™ | |  j | ƒ } | r™ | Sn  d S(   Ni    i   s    resname not in allowed lists    resname in banned list(   t   id_with_resnameR   t   upperR   R   (   R`   R]   R   t   fail_cis_or_trans_check(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyRU   ð   s    			c         C   s”   d | j  k r | j ƒ  d S| j  d } |  d k r] | d k pK | d k r] | j ƒ  d S|  d k r� | d k o~ | d	 k r� | j ƒ  d
 Sd S(   Nt   omegas     has no value for omega dihedralt   transix   iˆÿÿÿs    omega dihedral is not transt   cisiÄÿÿÿi<   s    omega dihedral is not cisi    (   R3   Rn   (   R   R]   Rq   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyRp   ÿ   s    %%c   
      C   sŒ  |  j  s |  j r d S| j s. | j ƒ  d S|  j | j k rU | j ƒ  d |  j St | j |  j ƒ d k r� |  j r� | j ƒ  d |  j Sxä | j D]Ù } | |  j k rš xÁ | j | D]¯ } | j | | } x• |  j D]Š } | j sÿ | j	 | j
 k rÞ | j s| j | j k rÞ |  j } | j } t | | | ƒ }	 |	 rH|	 S| ra| j | | ƒ n  d SqÞ qÞ Wq½ Wqš qš W| j ƒ  d |  j S(   Ni    s    Missing all probe informations(    Missing probe information for src_atom i   s'    Disallowed bifucated bond at src_atom s    No matching bond for (   R!   R    t   probeRn   R#   R/   R"   R'   R*   R)   RC   R,   R+   R$   R   RX   R7   (
   t
   check_bondR]   RR   R#   t	   bond_namet   observed_bondt   trg_atomR   t   trg_residuet   fail_index_matching(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyRW     s.    	&6		 c         C   s»   |  j  s d S|  j | j j ƒ  k r) d S|  j } x‚ | j | D]o } | j | | } xU |  j D]J } | j s‚ | j | j k ra | j s� | j	 | j	 k ra | j
 ƒ  d Sqa Wq@ Wd Sd  S(   Ni    s    Residue has forbidden bond(   R!   R#   Rt   RI   R'   R*   R)   RC   R,   R+   Rn   (   Ru   R]   R#   Rv   Rw   Rx   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyRV   -  s    		6c         C   s–   |  s
 d Sx… | j  j ƒ  D]p } |  | k rU | j  |  | k rU d |  d | j ƒ  S| j  | | k r | |  k r d | j ƒ  d |  Sq Wd Sd  S(   Ni    s   Index s0    already identified, does not match new residue s   Residue s+    already indexed, does not match new index (   R   RI   Rn   (   R   R   Rj   t   observed_index(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyRX   P  s     c         C   s�   t  j ƒ  } t j j d ƒ } | d  k r9 t d ƒ ‚ n  t  j | ƒ |  j d } d G|  j Gd GHt	 j
 d | d |  ƒ d GHt  j | ƒ d  S(	   Ns'   cctbx_project/mmtbx/cablam/fingerprintss(   Problem locating cablam fingerprints dirs   .picklet
   Convertings   to pickle file . . .t	   file_namet   objs
   . . . Done(   t   ost   getcwdt   libtbxt   envt   find_in_repositoriesR   R   t   chdirR   R   t   dump(   R   t   pwdt   fingerprints_dirt   filename(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   make_pickle^  s    		c         C   s†   g  } xy |  D]q } d | d } t  j j d | d t j j ƒ } | d  k re t d | d ƒ ‚ q | j t	 j
 d | ƒ ƒ q W| S(   Ns(   cctbx_project/mmtbx/cablam/fingerprints/s   .picklet   relative_patht   tests/   
Could not find a needed pickle file for motif s    in chem_data.
Exiting.
R}   (   R�   R‚   Rƒ   R   t   patht   isfileR   R   R   R   t   load(   RP   t   fingerprint_listR   RŒ   t
   picklefile(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyRH   r  s    c         C   sN   t  |  ƒ } g  } x5 | D]- } x$ | j j ƒ  D] } | j | ƒ q/ Wq W| S(   N(   RH   R   R2   R   (   RM   t   motifst
   label_listR   t   residue_name(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   get_all_labels„  s    (   t
   __future__R    R   RZ   t   libtbx.load_envR�   R   t   libtbx.utilsR   t   objectR   R   R   R(   R.   RT   RK   RY   RU   Rp   RW   RV   RX   R‰   RH   R”   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_fingerprints.pyt   <module>   s(   
)	"	 	#			 	#			