
BQc           @   s   d  d l  Z  d  d l Te  j d  Z e  j d  Z d f  d     YZ d f  d     YZ d	 f  d
     YZ d f  d     YZ d   Z	 d f  d     YZ
 d   Z d S(   iN(   t   *sm   ^(ATOM|HETATM) *([0-9]+) *(\S+) +([A-Z]+) +([a-zA-Z]?) *([-0-9]+) *(-?[0-9.]+) *(-?[0-9.]+) *(-?[0-9.]+)(.*$)sV   ^REMARK +350 +BIOMT[1-3] *(\d+) +(-?[0-9.]+) +(-?[0-9.]+) +(-?[0-9.]+) +(-?[0-9.]+) *$t   Atomc           B   s>   e  Z d  Z d d d d d  Z d   Z d   Z d   Z RS(   s;   This object represents information concerning a single atomt   Xi    c         C   s  | d k r| j d  |  _ t | j d   |  _ | j d  |  _ | j d  j   |  _ | j d  |  _ t | j d   |  _	 t
 | j d   |  _ t
 | j d   |  _ t
 | j d	   |  _ t |  j |  j |  j g  |  _ d |  _ d |  _ | j d
  |  _ n | d k r| |  _ | |  _ | \ |  _ |  _ |  _ | |  _ d |  _ d |  _	 d |  _ d |  _ d |  _ d |  _ d |  _ n  t |  j  |  _ d |  _ d |  _ g  |  _ d S(   s8   Fill in information using a match object from a pdb filei   i   i   i   i   i   i   i   i	   i
   t   SSNi    R   s               Ct   HETATMN(   t   Nonet   groupt   typet   intt   numt   namet   stript   rest   chaint   resnumt   floatt   xt   yt   zt   arrayt   coordst	   occupancyt   bfactort   tailt   chainobjt   resobjt   conect(   t   selft   pdbmatchR   R
   R	   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   __init__   s<    !													c         C   s    d |  j  |  j |  j |  j f S(   Ns   <Atom %d, %s; res %d; chain %s>(   R	   R
   R   R   (   R   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   __repr__4   s    c         C   s   |  j  d k s |  j d k r d |  j j d  |  j |  j j d  |  j |  j |  j	 |  j
 d |  j
 d |  j
 d |  j f
 } np d |  j j d  |  j |  j j d  |  j |  j |  j	 |  j
 d |  j
 d |  j
 d |  j  |  j |  j f } | S(	   s+   Get the pdb line corresponding to this atoms%   %s%5d  %s%3s%2s%4d%12.3f%8.3f%8.3f%s
i   i   i    i   i   s/   %s%5d  %s%3s%2s%4d%12.3f%8.3f%8.3f%6.2f%6.2f%s
N(   R   R   R   R   t   ljustR	   R
   R   R   R   R   R   (   R   t   line(    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   pdbline7   s    6.67c         C   s@   d |  j  } x" |  j D] } | d | j  7} q W| d 7} | S(   s   Get a CONECT line for this atoms	   CONECT%5ds   %5ds   
(   R	   R   (   R   t   strt   a(    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   conlineE   s
    
N(   t   __name__t
   __module__t   __doc__R   R   R   R!   R$   (    (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyR      s
   %		t   Residuec           B   s/   e  Z d  Z d d  Z d   Z d d  Z RS(   s   Residue or nucleotide objectc         C   sp   | d  k r3 g  |  _ d  |  _ d  |  _ d  |  _ n9 | |  _ | d j |  _ | d j |  _ | d j |  _ d  S(   Ni    (   R   t   atomsR	   R   R
   R   R   (   R   R)   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyR   P   s    				c         C   s   d |  j  |  j f S(   Ns   <Residue %d: %s>(   R	   R
   (   R   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyR   \   s    i    c         C   ss   | rH t  j |  } xW |  j D]" } | j | j  d k r | Sq Wn' x$ |  j D] } | j | k rR | SqR Wd S(   s   Return the first atom with the given name found in the residue. If regexp is true, then get the atom whose name matches the regular expressionN(   t   ret   compileR)   t   searchR
   R   (   R   R
   t   regexpt   rNR#   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   getAtomByName_   s    N(   R%   R&   R'   R   R   R   R/   (    (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyR(   N   s   	t   Chainc           B   s,   e  Z d  Z d d  Z d   Z d   Z RS(   s   Chain objectc         C   sA   | d  k r! g  |  _ d  |  _ n | |  _ |  j d j |  _ d  S(   Ni    (   R   t   residuesR
   R   (   R   R1   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyR   o   s
    		c         C   s   d |  j  S(   Ns
   <Chain %s>(   R
   (   R   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyR   v   s    c         C   s   g  |  _  | d j |  _ d } x | D] } | j |  j k r] t d |  j | j f   n  | sv | j | j k r | r |  j  j |  n  t   } | j | _ | j | _ | j | _ | g | _	 d } q& | j	 j |  q& W|  j  j |  d S(   Ni    s3   Atmlist does not all have the same chain name %s %si   (
   R1   R   R
   t
   ValueErrorR   R	   t   appendR(   R   R)   (   R   t   atmlistt   startR#   R   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   fromAtomListy   s$    		 	N(   R%   R&   R'   R   R   R   R6   (    (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyR0   m   s   	t   BasePairc           B   s#   e  Z d  Z d d  Z d   Z RS(   s=   DNA base pair object. Contains 2 residues and a list of atomsc         C   s   g  |  _  | d  k r! g  |  _ n- | |  _ x! | D] } |  j  j | j   q1 Wt |  j  d k r t |  j  d k r d GHn  d  S(   Ni    i   s9   WARNING: creating base-pair with neither 0 nor 2 residues(   R)   R   R1   t   extendt   len(   R   R1   t   r(    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyR      s    		*c         C   s"   d |  j  d j |  j  d j f S(   Ns   <BasePair: %s %s>i    i   (   R1   R
   (   R   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyR      s    N(   R%   R&   R'   R   R   R   (    (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyR7      s   c         C   s  |  j  d d k r& | j  d d k p|  j  d d k rL | j  d d k p|  j  d d k rr | j  d d k p|  j  d d k r | j  d d k p|  j  d k r | j  d k p|  j  d k r | j  d k p|  j  d k r | j  d	 k p|  j  d	 k o| j  d k S(
   s5   Check if two DNA/RNA residues make a correct basepairit   At   Tt   Gt   Ct   THYt   ADNt   CYTt   GUA(   R
   (   t   r1t   r2(    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   goodPair   s    &&&&t	   Structurec           B   s   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
 d d  Z d   Z d d  Z d   Z d
 d  Z d   Z RS(   s8   Class containing an molecule or multi-molecule structurec         C   s*   |  j    | d  k r& |  j |  n  d  S(   N(   t   initvarsR   t   structFromFile(   R   t   infile(    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyR      s    
c         C   s`   xY t  t |  j   D]B } | d |  j | _ x% |  j | j D] } | d | _ qA Wq Wd S(   s   Renumber the residuesi   N(   t   rangeR9   R1   R	   R)   R   (   R   t   cR#   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   renumRes   s    c         C   s#  g  |  _  i  } x |  j D] } | j | j   k rh | | j j j |  | | j j j | j  na t   | | j <|  j  j | | j  | j | | j _ | g | | j _ | j | | j _ x | j D] } | j | _ q Wq Wg  |  _ g  |  j D] } |  j j | j  ^ q d  S(   N(	   t   chainsR1   R   t   keysR3   R)   R8   R0   R
   (   R   t
   chainnamesR:   R#   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   resetFromResidues   s    		c         C   s   g  |  _  g  |  _ x |  j D] } |  j  j | j   x= | j  D]2 } | j | _ x | j D] } | j | _ q[ Wq? Wg  | j  D] } |  j j | j  ^ q q Wd S(   Ni    (   R1   R)   RM   R8   R
   R   (   R   RK   R:   R#   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   resetFromChains   s    	 	-c         C   s   t  g  |  j D] } | j ^ q  } t | d d } t j |  \ } } t |  } | | } | d d  | f } t t | d d  d f | d d  d f  | d d  d f  } | d k  r d d d g } | d d  | f } n  | | f S(   s   Perform a principal component analysis on the atom coordinates. Returns eigenvals and eigenvecs. Sorted from largest to smallest eigenvaluet   rowvari    Ni   i   (	   R   R)   R   t   covt   linalgt   eigt   argsortt   dott   cross(   R   R#   t   Mt   covmatt   evalt   evect   indt   check(    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   PCA   s    %
Hc         C   sA   t  |  j } x+ |  j D]  } t | j |  d | _ q Wd S(   s4   Rotate entire structure by the given rotation matrixi    N(   t   matrixR<   R)   R   R   (   R   RY   t   rotmatR#   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   rotateM   s    c         C   s+   x$ |  j  D] } | j | k r
 | Sq
 Wd  S(   N(   R)   R	   R   (   R   R	   R#   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt	   atomByBum  s    c         C   s+   x$ |  j  D] } | j | k r
 | Sq
 Wd  S(   N(   RM   R
   (   R   R
   RK   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   chainByName  s    c         C   s:   g  |  _  g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ d S(   s   Initialize various variablesN(   t
   startlinest   endlinesRM   R1   R)   t
   transforms(   R   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyRG     s    					i    i   c   	      C   s  | r t  | d  } n t  | d  } | j d |  x |  j D] } | j |  qB Wt |  j  d k r g  |  j D] } | j | j    ^ qx n xL |  j D]A } x8 | j D]- } x$ | j D] } | j | j    q Wq Wq Wg  |  j D]* } | j d  k r | j | j    ^ q g  |  j D]0 } t | j
  d k r+| j | j    ^ q+x |  j D] } | j |  qiW| j d  | j   d  S(   NR#   t   ws   MODEL     %4d
i    s   ENDMDL
(   t   opent   writeRe   R9   RM   R)   R!   R1   R   R   R   R$   Rf   t   close(	   R   t   outfileR3   t   identt   OFt   lR#   RK   R:   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt	   outputPDB!  s$    ,8>c         C   s  t  |  } | j   } | j   g  |  _ d } x| D]} t j |  } | d  k r6t | j d   } g  t	 d d  D] } t
 | j |   ^ q~ }	 t
 | j d   }
 | t |  j  k r |  j j |	 g |
 g g  n8 |  j | d d j |	  |  j | d d j |
  |  j j |  q8 n  t j |  } | d  k r\| sj|  j j |  q8 | d  d k r| | _ q8 | d  d k rF|  j t | d d	 !  } | d  k rd
 Gt | d d	 ! GHqVx| t	 d  D][ } | d	 d | d d | !} y# | j j |  j t |    Wqt k
 r>PqXqWq8 |  j j |  q8 n  t |  } | st | g  } t | g  } |  j j |  |  j j |  d } n | j |  j d j k rSt | g  } |  j j |  | j |  j d j k r@t | g  } | j j   d k rP|  j j |  qPqc| j j |  n | j j |  |  j j |  | j j   d k r| | _ n  | | _ g  |  _ x9 |  j D]. } t | d  | d <t | d  | d <qWq8 Wd  S(   Ni    i   i   i   i   t   TERi   t   CONECTi   s   Cannot find atom number : i   i   it    (    Ri   t	   readlinesRk   Rg   t
   transformRt   matchR   R   R   RJ   R   R9   R3   Re   t   atomRt   tert	   atomByNumR   R2   Rf   R   R(   R0   R1   RM   R   R)   R   R   R   R   R   (   R   RI   t   IFt   linest   startedR    t   mt   tnt   it   matrowt   shiftt   newchaint   atmR#   R"   t   newatomt   newrest   t(    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyRH   F  sv    
	1#			 c         C   s8   x1 t  t |  j   D] } | | |  j | _ q Wd S(   s3   Renumber all atoms from 1 or another starting pointN(   RJ   R9   R)   R	   (   R   R5   R   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt
   renumAtoms  s    c         C   s+   x$ |  j  D] } | j | k r
 | Sq
 Wd S(   s*   Get the atom with the corresponding numberN(   R)   R	   R   (   R   R	   R#   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyRy     s    c         C   s   | r |  j  } n+ g  |  j  D] } | j d k r | ^ q } d d d g } xN t d  D]@ } t g  | D] } | j | ^ qi  t t |   | | <qY W| S(   sj   Get the center of mass of all the atoms in the structure. If countHet is true, include heteroatoms as wellt   ATOMi    i   (   R)   R   RJ   t   sumR   R   R9   (   R   t   countHett   alistR#   t   comR   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   getCOM  s    +>c         C   s:   |  j  d d  } x! |  j D] } | j | | _ q Wd S(   s'   Move entire structure to shift COM to 0R   i   N(   R   R)   R   (   R   R   R#   (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   recenter  s    N(   R%   R&   R'   R   R   RL   RP   RQ   R_   Rb   Rc   Rd   RG   Rp   RH   R   Ry   R   R   (    (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyRF      s    								%	Q	c          C   s%   t    }  d g |  _ d g |  _ |  S(   sy   Make a barebones structure with nothing in it, but containing appropriate start/end stuff so that it can be output to pdbs   HEADER    structure
s   END
(   RF   Re   Rf   (   t   struct(    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   makeBareStruct  s    	(   R*   t   numpyR+   Rw   Ru   R   R(   R0   R7   RE   RF   R   (    (    (    sN   /home/lenafabr/Stanford/Spakowitz/proj/shearableWLC/public/scripts/pdbutils.pyt   <module>   s   
	A'	 