
    Ri%                     t    d Z ddlZddlmZ ddlmZ ddlmZ dZ	ddZ
 G d d	      Zd
 Zd Z G d d      Zy)a  Classify protein backbone structure with 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:

http://github.com/csblab/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'.

    >>> from Bio.PDB.PDBParser import PDBParser
    >>> from Bio.PDB.FragmentMapper import FragmentMapper
    >>> parser = PDBParser()
    >>> structure = parser.get_structure("1a8o", "PDB/1A8O.pdb")
    >>> model = structure[0]
    >>> fm = FragmentMapper(model, lsize=10, flength=5, fdir="PDB")
    >>> chain = model['A']
    >>> res152 = chain[152]
    >>> res157 = chain[157]
    >>> res152 in fm # is res152 mapped? (fragment of a C-alpha polypeptide)
    False
    >>> res157 in fm # is res157 mapped? (fragment of a C-alpha polypeptide)
    True

    N)PDBException)	PPBuilder)SVDSuperimposerzlib_%s_z_%s.txtc           
         |dz   t         z   | |fz  }t        |      5 }g }d}|D ]  }|d   dk(  s|d   dk(  r|j                         }|d   dk(  r#t        ||      }	|j	                  |	       |dz  }Ot        j                  |dd D 
cg c]  }
t        |
       c}
      }	j                  d|        	 d	d	d	       |S c c}
w # 1 sw Y   S xY w)
a  Read a fragment spec file (PRIVATE).

    Read a fragment spec file available from
    http://github.com/csblab/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
    /r   *
   z------   XXXN)	_FRAGMENT_FILEopensplitFragmentappendnparrayfloatadd_residue)sizelengthdirfilenamefpflistfidlineslfxcoords               N/home/agent/.friday_env/lib/python3.12/site-packages/Bio/PDB/FragmentMapper.py_read_fragmentsr#   9   s      c	N*tVn<H	h (2 	(DAw#~aDB!u VS)QqHH1Q81eAh89EMM%'	(	(( L 9#(( Ls   A,C	CCCCc                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)r   z)Represent a polypeptide C-alpha fragment.c                 v    || _         d| _        g | _        t        j                  |dfd      | _        || _        y)zInitialize fragment object.

        :param length: length of the fragment
        :type length: int

        :param fid: id for the fragment
        :type fid: int
        r   r   dN)r   counterresname_listr   zeros	coords_car   )selfr   r   s      r"   __init__zFragment.__init__d   s7     61+s3    c                     | j                   S )zcGet residue list.

        :return: the residue names
        :rtype: [string, string,...]
        )r(   r+   s    r"   get_resname_listzFragment.get_resname_listv   s        r-   c                     | j                   S )zcGet identifier for the fragment.

        :return: id for the fragment
        :rtype: int
        )r   r/   s    r"   get_idzFragment.get_id~   s     xxr-   c                     | j                   S )zGet the CA coordinates in the fragment.

        :return: the CA coords in the fragment
        :rtype: NumPy (Nx3) array
        )r*   r/   s    r"   
get_coordszFragment.get_coords   s     ~~r-   c                     | j                   | j                  k\  rt        d      | j                  j	                  |       || j
                  | j                   <   | j                   dz   | _         y)zAdd a residue.

        :param resname: residue name (eg. GLY).
        :type resname: string

        :param ca_coord: the c-alpha coordinates of the residues
        :type ca_coord: NumPy array with length 3
        zFragment boundary exceeded.r
   N)r'   r   r   r(   r   r*   )r+   resnameca_coords      r"   r   zFragment.add_residue   sV     <<4;;&<==  )'/t||$||a'r-   c                     | j                   S )zReturn length of the fragment.)r   r/   s    r"   __len__zFragment.__len__   s    {{r-   c                     t               }|j                  | j                  |j                         |j                          |j	                         S )zReturn rmsd between two fragments.

        :return: rmsd between fragments
        :rtype: float

        Examples
        --------
        This is an incomplete but illustrative example::

            rmsd = fragment1 - fragment2

        )r   setr*   runget_rms)r+   othersups      r"   __sub__zFragment.__sub__   s7     0	{{}r-   c                 8    d| j                   | j                  fz  S )zRepresent the fragment object as a string.

        Returns <Fragment length=L id=ID> where L=length of fragment
        and ID the identifier (rank in the library).
        z<Fragment length=%i id=%i>)r   r   r/   s    r"   __repr__zFragment.__repr__   s     ,t{{DHH.EEEr-   N)__name__
__module____qualname____doc__r,   r0   r2   r4   r   r9   r@   rB    r-   r"   r   r   a   s.    3$!($Fr-   r   c                    g }t        t        |       |z
  dz         D ]  }t        |d      }t        |      D ]y  }| ||z      }|j                         }|j	                  d      r|d   }nt        d      |j                         rt        d      |j                         }	|j                  ||	       { |j                  |        |S )zDice up a peptide in fragments of length "length" (PRIVATE).

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

    :param length: fragment length
    :type length: int
    r
   CA
CHAINBREAK)
rangelenr   get_resnamehas_idr   is_disordered	get_coordr   r   )
ppr   	frag_listir   jresiduer6   car7   s
             r"   _make_fragment_listrX      s     I3r7V#a'( VR v 
	-AQiG))+G~~d#T]"<00!"<00||~HMM'8,
	- 	 r-   c                     g }| D ]c  }g }t        t        |            D ]  }||   }||z
  }|j                  ||f       ! |j                          |d   d   }|j                  |       e |S )a  Map flist fragments to closest entry in reflist (PRIVATE).

    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}, ...]
    r   r
   )rL   rM   r   sort)	r   reflistmappedr   rankrT   rfrmsfragments	            r"   _map_fragment_listra      s     F  s7|$ 	#ABb&CKKb	"	# 			71:h  Mr-   c                   *    e Zd ZdZddZd Zd Zd Zy)FragmentMapperzAMap polypeptides in a model to lists of representative fragments.c                     |dk(  rd| _         n|dk(  rd| _         nt        d      || _        || _        t	        |||      | _        || _        | j                  | j                        | _        y)a  Create instance of FragmentMapper.

        :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
                 r   z!Fragment length should be 5 or 7.N)	edger   flengthlsizer#   r[   model_mapfd)r+   rk   rj   ri   fdirs        r"   r,   zFragmentMapper.__init__   se      a<DI\DIBCC
&ugt<
))DJJ'r-   c                    t               }|j                  |      }i }|D ]  }	 t        || j                        }t	        || j
                        }t        t        |            D ]Q  }||   }	|| j                  k  r|t        |      | j                  z
  k\  r4|| j                  z
  }
|
dk\  sJ ||
   ||	<   S  |S # t        $ r}|dk(  rnt        |      dY d}~d}~ww xY w)zjMap (PRIVATE).

        :param model: the model that will be mapped
        :type model: L{Model}
        r   rK   N)
r   build_peptidesrX   ri   ra   r[   rL   rM   rh   r   )r+   rk   ppbpplrm   rR   r   mflistrT   resindexwhys               r"   rl   zFragmentMapper._map  s     k  ' 	6B6+B=+E4<<@s2w 0AQ%C499} s2w23  !"DII$z)z"(-30	62 	   6,&&s+5 6s   BB<<	C!CC!c                     || j                   v S )zeCheck if the given residue is in any of the mapped fragments.

        :type res: L{Residue}
        rm   r+   rt   s     r"   __contains__zFragmentMapper.__contains__4  s    
 dgg~r-   c                      | j                   |   S )z{Get an entry.

        :type res: L{Residue}

        :return: fragment classification
        :rtype: L{Fragment}
        rx   ry   s     r"   __getitem__zFragmentMapper.__getitem__;  s     wws|r-   N)   re   .)rC   rD   rE   rF   r,   rl   rz   r|   rG   r-   r"   rc   rc      s    K(8"Hr-   rc   )r~   )rF   numpyr   Bio.PDB.PDBExceptionsr   Bio.PDB.Polypeptider   Bio.SVDSuperimposerr   r   r#   r   rX   ra   rc   rG   r-   r"   <module>r      sK   #J  . ) /
 #%PXF XFv64R Rr-   