ó
ŽïIc           @   s  d  d l  Z  d  d l Z d  d l m Z d  d l Td Z d Z d d „ Z d d d	 „  ƒ  YZ d
 „  Z	 d „  Z
 d d d „  ƒ  YZ e d k rd  d l Z e ƒ  Z e j d e j d ƒ Z e d Z e e d d d ƒ Z x= e j e d ƒ D]& Z e Ge j e ƒ re e GHqî Hqî Wn  d S(   iÿÿÿÿN(   t   SVDSuperimposer(   t   *sO  
Classify protein backbone structure according to Kolodny et al's fragment
libraries. It can be regarded as a form of objective secondary structure
classification. Only fragments of length 5 or 7 are supported (ie. there is a
'central' residue).

Full reference:

Kolodny R, Koehl P, Guibas L, Levitt M.
Small libraries of protein fragments model native protein structures accurately.
J Mol Biol. 2002 323(2):297-307.

The definition files of the fragments can be obtained from:

U{http://csb.stanford.edu/~rachel/fragments/}

You need these files to use this module.

The following example uses the library with 10 fragments of length 5.
The library files can be found in directory 'fragment_data'.

    >>> model=structure[0]
    >>> fm=FragmentMapper(lsize=10, flength=5, dir="fragment_data")
    >>> fm.map(model)
    >>> fragment=fm[residue]
s   lib_%s_z_%s.txtt   .c         C   sõ   | d t  |  | f } t | d ƒ } g  } d } x± | j ƒ  D]£ } | d d k s@ | d d k rl q@ n  | j ƒ  } | d d k r´ t | | ƒ }	 | j |	 ƒ | d 7} q@ n  t j t t	 | d d !ƒ ƒ }
 |	 j
 d	 |
 ƒ q@ W| j ƒ  | S(
   sx  
    Read a fragment spec file (available from 
    U{http://csb.stanford.edu/rachel/fragments/} 
    and return a list of Fragment objects.

    @param size: number of fragments in the library
    @type size: int

    @param length: length of the fragments
    @type length: int

    @param dir: directory where the fragment spec files can be found
    @type dir: string
    t   /t   ri    R   s   
i   s   ------i   t   XXX(   t   _FRAGMENT_FILEt   opent	   readlinest   splitt   Fragmentt   appendt   numpyt   arrayt   mapt   floatt   add_residuet   close(   t   sizet   lengtht   dirt   filenamet   fpt   flistt   fidt   lt   slt   ft   coord(    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyt   _read_fragments.   s"     

R
   c           B   sV   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s3   
    Represent a polypeptide C-alpha fragment.
    c         C   sC   | |  _  d |  _ g  |  _ t j | d f d ƒ |  _ | |  _ d S(   s‘   
        @param length: length of the fragment
        @type length: int

        @param fid: id for the fragment
        @type fid: int
        i    i   t   dN(   R   t   countert   resname_listR   t   zerost	   coords_caR   (   t   selfR   R   (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyt   __init__Z   s
    				c         C   s   |  j  S(   sQ   
        @return: the residue names
        @rtype: [string, string,...]
        (   R    (   R#   (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyt   get_resname_listk   s    c         C   s   |  j  S(   sB   
        @return: id for the fragment
        @rtype: int
        (   R   (   R#   (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyt   get_idr   s    c         C   s   |  j  S(   s\   
        @return: the CA coords in the fragment
        @rtype: Numeric (Nx3) array
        (   R"   (   R#   (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyt
   get_coordsy   s    c         C   sU   |  j  |  j k r! t d ƒ ‚ n  |  j j | ƒ | |  j |  j  <|  j  d |  _  d S(   sÌ   
        @param resname: residue name (eg. GLY).
        @type resname: string

        @param ca_coord: the c-alpha coorinates of the residues
        @type ca_coord: Numeric array with length 3
        s   Fragment boundary exceeded.i   N(   R   R   t   PDBExceptionR    R   R"   (   R#   t   resnamet   ca_coord(    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyR   €   s
    c         C   s   |  j  S(   sA   
        @return: length of fragment
        @rtype: int
        (   R   (   R#   (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyt   __len__Ž   s    c         C   s3   t  ƒ  } | j |  j | j ƒ | j ƒ  | j ƒ  S(   s®   
        Return rmsd between two fragments.

        Example:
            >>> rmsd=fragment1-fragment2

        @return: rmsd between fragments
        @rtype: float
        (   R    t   setR"   t   runt   get_rms(   R#   t   othert   sup(    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyt   __sub__•   s    
	
c         C   s   d |  j  |  j f S(   sƒ   
        Returns <Fragment length=L id=ID> where L=length of fragment
        and ID the identifier (rank in the library).
        s   <Fragment length=%i id=%i>(   R   R   (   R#   (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyt   __repr__¤   s    (   t   __name__t
   __module__t   __doc__R$   R%   R&   R'   R   R+   R1   R2   (    (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyR
   V   s   							c   
      C   sá   g  } xÔ t  d t |  ƒ | d ƒ D]µ } t | d ƒ } x t  d | ƒ D] } |  | | } | j ƒ  } | j d ƒ r… | d } n t d ƒ ‚ | j ƒ  r¬ t d ƒ ‚ n  | j ƒ  }	 | j | |	 ƒ qI W| j	 | ƒ q$ W| S(   sÛ   
    Dice up a peptide in fragments of length "length".

    @param pp: a list of residues (part of one peptide)
    @type pp: [L{Residue}, L{Residue}, ...]

    @param length: fragment length
    @type length: int
    i    i   iÿÿÿÿt   CAt
   CHAINBREAK(
   t   ranget   lenR
   t   get_resnamet   has_idR(   t   is_disorderedt	   get_coordR   R   (
   t   ppR   t	   frag_listt   iR   t   jt   residueR)   t   caR*   (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyt   _make_fragment_list¬   s    
$c   	      C   s   g  } x€ |  D]x } g  } xD t  d t | ƒ ƒ D]- } | | } | | } | j | | f ƒ q/ W| j ƒ  | d d } | j | ƒ q W| S(   sN  
    Map all frgaments in flist to the closest
    (in RMSD) fragment in reflist.

    Returns a list of reflist indices.

    @param flist: list of protein fragments
    @type flist: [L{Fragment}, L{Fragment}, ...]

    @param reflist: list of reference (ie. library) fragments
    @type reflist: [L{Fragment}, L{Fragment}, ...]
    i    i   (   R8   R9   R   t   sort(	   R   t   reflistt   mappedR   t   rankR@   t   rft   rmst   fragment(    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyt   _map_fragment_listÈ   s    


t   FragmentMapperc           B   s;   e  Z d  Z d d d d „ Z d „  Z d „  Z d „  Z RS(   sK   
    Map polypeptides in a model to lists of representative fragments.
    i   i   R   c         C   s…   | d k r d |  _  n$ | d k r0 d |  _  n t d ƒ ‚ | |  _ | |  _ t | | | ƒ |  _ | |  _ |  j |  j ƒ |  _ d S(   s  
        @param model: the model that will be mapped
        @type model: L{Model}

        @param lsize: number of fragments in the library
        @type lsize: int

        @param flength: length of fragments in the library
        @type flength: int

        @param fdir: directory where the definition files are
            found (default=".")
        @type fdir: string
        i   i   i   i   s!   Fragment length should be 5 or 7.N(	   t   edgeR(   t   flengtht   lsizeR   RF   t   modelt   _mapt   fd(   R#   RQ   RP   RO   t   fdir(    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyR$   æ   s    			c         C   sú   t  ƒ  } | j | ƒ } i  } xÕ | D]Í } y³ t | |  j ƒ } t | |  j ƒ } xˆ t d t | ƒ ƒ D]q } | | }	 | |  j k  r qh qh | t | ƒ |  j k r¬ qh qh | |  j }
 |
 d k sË t	 ‚ | |
 | |	 <qh WWq% d k
 rñ q% Xq% W| S(   s[   
        @param model: the model that will be mapped
        @type model: L{Model}
        i    R7   (
   t	   PPBuildert   build_peptidesRD   RO   RL   RF   R8   R9   RN   t   AssertionError(   R#   RQ   t   ppbt   pplRS   R>   R   t   mflistR@   t   rest   index(    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyRR     s&    	
c         C   s   |  j  j | ƒ S(   s'   
        @type res: L{Residue}
        (   RS   t   has_key(   R#   R[   (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyR]   !  s    c         C   s   |  j  | S(   sm   
        @type res: L{Residue}

        @return: fragment classification
        @rtype: L{Fragment}
        (   RS   (   R#   R[   (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyt   __getitem__'  s    (   R3   R4   R5   R$   RR   R]   R^   (    (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyRM   â   s
   	 	t   __main__t   Xi   i    i
   i   t   levitt_datat   R(    (    (   t   osR   t   Bio.SVDSuperimposerR    t   Bio.PDBR5   R   R   R
   RD   RL   RM   R3   t   syst	   PDBParsert   pt   get_structuret   argvt   st   mt   fmt	   Selectiont   unfold_entitiesR   R]   (    (    (    s‰   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/PDB/FragmentMapper.pyt   <module>   s*   
(V		O	
