ó
xTc           @   s  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  d l m	 Z	 d d d     YZ
 d d  Z d	   Z d
   Z d d  Z d d  Z d d  Z d d  Z d   Z d d  Z d d  Z d   Z d   Z d   Z d   Z d d d  Z e d k rx) e e j  d k  rLe j j d  q$We e j d e j d e j d e j d  n  d S(   i’’’’(   t   pdb(   t   mmtN(   t   clustal_alignment_parset   Vectorc           B   s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   xt   yt   z(   t   selfR   R   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   __init__	   s    		c         C   s3   t  j |  j |  j |  j |  j |  j |  j  S(   N(   t   matht   sqrtR   R   R   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   L2Norm   s    c         C   s7   |  j    } t |  j | |  j | |  j |  } | S(   N(   R   R   R   R   R   (   R   t   normt   u(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt
   unitvector   s    'c         C   s+   t  |  j | |  j | |  j |  } | S(   N(   R   R   R   R   (   R   t   at   r(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   __mul__   s    '(   t   __name__t
   __module__R   R   R   R   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyR      s   			t    c         C   sā   t  j d |   } | j   } g  } x· | j   d j   D] } t |  d k rt | j   | j j   k rt q; n  xc | j   D]U } xL | j	   D]> } | j
 d k r | j | | j   d | j   f  q q Wq Wq; W| S(   Nt	   file_namei    s    CA (   R    t   inputt   construct_hierarchyt   modelst   chainst   lent   uppert   idt   residue_groupst   atomst   namet   appendt   unique_resnamest   resseq_as_int(   t   pdbnamet   chainidt   mypdbt   roott   calphast   chaint   residuet   atm(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   listCalphas   s    -5c         C   sc  t  |   } g  } xU t |   D]G \ } } | j g   x+ t d |  D] } | | j | d   qH Wq Wxņ t |   D]ä \ } } xÕ t | |  D]Ä } |  | } t | d j d | d j d | d j d | d j d | d j d | d j d  } | d | d | | j   f | | | <| d | d | d | j   f | | | <q Wqw W| S(   Ni    i   i   i’’’’(	   R   t	   enumerateR    t   ranget   insertt   NoneR   t   xyzR   (   t   Calphast   nrt   matrixt   iR   t   jt   bt   D(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   MakeCaplhaDistanceMatrix/   s    
 #(4c         C   sŖ   t  |   } d } g  } x$ t d |  D] } | j d  q( Wxd t | |  D]S } xJ t | | |  D]5 } |  | | | d d k  ri | | c d 7<qi qi WqO W| S(   Ni   i    i   g      "@i   (   R   R-   R    (   R3   t   dimt   gapt   cvR4   R5   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   GetContactVectorE   s    c         C   s+   t  |  |  } t |  } t |  } | S(   N(   R+   R8   R<   (   R#   R$   R'   t   DmR;   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   GetContactVectorFromPDBU   s    c         C   s^   t  |  |  } xH | D]@ } d | d j d | d j d | d j d | d f GHq Wd  S(   Ns   %f 	 %f 	 %f 	 %si    i   i   (   R+   R0   (   R#   R$   R'   t   ca(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   PrintCalphasFromPDB^   s    c         C   sF   t  |  |  } x0 t d t |   D] } d | | | f GHq% Wd  S(   Ni    s   %d 	 %d(   R>   R-   R   (   R#   R$   R;   R4   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   PrintContactVectorFromPDBe   s    c         C   sY  t  |  |  } t |  } t j j t j j |    d } t | d d  } t |  } d } x% t d |  D] } | d | 7} qr W| d 7} x t d |  D]y } | d | 7} xX t d |  D]G }	 | | |	 d j	   }
 | d	 |
 7} | j
 d
 | |	 |
 f  qČ W| d 7} q¤ W| j   t | d d  } | j
 |  | j   | S(   Ni    s   listcontactmatrix.txtt   wR   s   j= %d 	s   
s   i= %d	i   s    %f	s   %d	 %d	 %f
s   contactmatrix.txt(   R+   R8   t   ost   patht   splitextt   basenamet   openR   R-   R   t   writet   close(   R#   R$   R'   R=   t
   nameprefixt   outfileR9   t   sR4   R5   t   lengtht   mfile(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   PrintContactMatrixl   s*    "


c   	      C   s0  t  |  |  } t |  d k r4 d | |  f GHd  St  | |  } t |  d k rh d | | f GHd  St |  t |  } | d k r“ xN t d |  D] } | j d  q Wn' x$ t d |  D] } | j d  qÄ Wd } x? t d t |   D]( } | t j | | | | d  7} q÷ Wt j |  S(   Ni    s   invalid chain %s in %sg        i   (   R>   R   R/   R-   R    R	   t   powR
   (	   t   pdb1namet   chain1idt   pdb2namet   chain2idt   cv1t   cv2t   dR4   RL   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   GetContactVectorDistance   s$    &c         C   sI  t  j } | j   } t | j  } t j d |   } | j   } d } xś | j   d j	   D]ā } t
 |  d k r | j   | j j   k r q_ n  d }	 xP | j   D]B }
 |
 j   d | k r« |	 | j |
 j   d | j  7}	 q« q« Wt
 |	  d k r_ | d t j j |   d  j   d | j d |	 d 7} q_ q_ W| S(	   NR   R   i    t   >i   t   _Chains   
s   

(   R   t   PROTEINt   get_translationt   sett   three_letter_codesR    R   R   R   R   R   R   R   R   R!   t   gett   unknown_one_letterRC   RD   RF   (   t   pdbfnameR$   t   mst   transt   knownR%   R&   t   seqstrR(   t   substrt   res(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   GetSequenceFromPDB®   s"    	-*?c         C   s   t  |  |  t |  |  f S(   N(   Rh   R>   (   Ra   R$   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   GetSequenceContactVectorĖ   s    c         C   s   d } t  |  |  } t  | |  } | | k r4 d St d d  } | j | |  | j   | d d d f } t j | d t d	 t j j   d
 }	 t d d  }
 t	 |
 j
    } |
 j   | d k rÓ d  Sd t | d
 j    t | d
 j d
  } t j | t j d  } | S(   Ns#   E:\Programs\clustalw2\clustalw2.exeg        s   clustinput.txtRB   s   -INFILE=clustinput.txts   -ALIGNs   -OUTFILE=clust.alnt   shellt   stdouti    s	   clust.alnR   R   g      š?g       @(   NR   (   Rh   RG   RH   RI   t
   subprocesst   Popent   Falset   PIPEt   communicateR   t   readR/   t   floatt   identity_countR   t
   alignmentsR	   t   tant   pi(   RQ   RR   RS   RT   t   clustalwt   seq1t   seq2t   fileoutt   commandt   outputt   alnfilet   alit   percentdiffR7   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   GetSequenceDistanceŃ   s&    
		(
/c         C   s2   t  |  | | |  } t |  | | |  } | | S(   N(   R   RX   (   RQ   RR   RS   RT   t   seqdt   cvd(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   GetCVSeqDistanceō   s    c         C   so   t  |   } d } xM t d |  D]< } x3 t d |  D]" } |  | | } | | | 7} q8 Wq" Wt j |  S(   Ng        i    (   R   R-   R	   R
   (   R3   R9   RL   R4   R5   RW   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   FrobeniusNormż   s    c         C   s   g  } x t  d |  D]{ } | j g   xe t  d |  D]T } | | j | d   |  | | d | | d d | | | d | | | <q9 Wq W| S(   Ni    i   i   (   R-   R    R.   R/   (   t   matrix1t   matrix2t   dim2t   offsett
   diffmatrixR4   R5   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   GetDiffMatrix  s    Bc         C   sW  t  |  |  } t |  } t |  } t  | |  } t |  } t |  }	 d | |	 f GHg  }
 d  d  t d d d  d f } t | |	  } |	 | k rī t  |  |  } t |  } t |  }	 t  | |  } t |  } t |  } n  | d |	 } xī t d |  D]Ż } |
 j g   x+ t d |	  D] } |
 | j | |  q/Wxc t d |  D]R } | |	 k r| | |	 k  r| | |	 | n | } |
 | j | |	 |  q]Wx3 t d |	  D]" } |
 | j | | |	 |  qĆWqWxc t d | |	  D]N } t	 |
 | |	 |  } t
 |  |	 | |	 | |	 |	 } d | | f GHqWd  S(   Ns   dims= %d  %di    i   s   %d 	%f(   R+   R8   R   R/   R   t   absR-   R    R.   R   R   (   RQ   RS   RR   RT   t   calphas1t   Dm1t   dim1t   calphas2t   Dm2R   t   Dm3t   nullelementt   diffdimt   dim3R4   R5   t   dm1R   R   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   GetDiffMatrixNormGraph  s>    4$$t   __main__i   i   i   i   i   (    (   t   iotbxR    t   phaserR   R	   RC   t   sysRl   t   iotbx.bioinformaticsR   R   R+   R8   R<   R>   R@   RA   RO   RX   Rh   Ri   R   R   R   R   R   R   R   t   argvR    (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/ContactMetric.pyt   <module>   s0   $			%		#				/