
:>"^R7                 @   s  d  Z  d d l m Z d d l m Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z d d l m Z d d	 l m Z m Z d
 d d d d d d d d d d d d d d d d d d d g Z d Z e Z i  Z i  Z i  Z i  Z xS e d d  D]B Z e e Z e e Z e e e <e e e <e e e <e e e <q Wd  d!   Z d" d#   Z d$ d%   Z d& d'   Z d( d)   Z  d* d+   Z! d, d- d.  Z" Gd/ d0   d0 e#  Z$ Gd1 d2   d2 e%  Z& Gd3 d4   d4 e&  Z' Gd5 d6   d6 e&  Z( d S)7a  Polypeptide-related classes (construction and representation).

Simple example with multiple chains,

    >>> from Bio.PDB.PDBParser import PDBParser
    >>> from Bio.PDB.Polypeptide import PPBuilder
    >>> structure = PDBParser().get_structure('2BEG', 'PDB/2BEG.pdb')
    >>> ppb=PPBuilder()
    >>> for pp in ppb.build_peptides(structure):
    ...     print(pp.get_sequence())
    LVFFAEDVGSNKGAIIGLMVGGVVIA
    LVFFAEDVGSNKGAIIGLMVGGVVIA
    LVFFAEDVGSNKGAIIGLMVGGVVIA
    LVFFAEDVGSNKGAIIGLMVGGVVIA
    LVFFAEDVGSNKGAIIGLMVGGVVIA

Example with non-standard amino acids using HETATM lines in the PDB file,
in this case selenomethionine (MSE):

    >>> from Bio.PDB.PDBParser import PDBParser
    >>> from Bio.PDB.Polypeptide import PPBuilder
    >>> structure = PDBParser().get_structure('1A8O', 'PDB/1A8O.pdb')
    >>> ppb=PPBuilder()
    >>> for pp in ppb.build_peptides(structure):
    ...     print(pp.get_sequence())
    DIRQGPKEPFRDYVDRFYKTLRAEQASQEVKNW
    TETLLVQNANPDCKTILKALGPGATLEE
    TACQG

If you want to, you can include non-standard amino acids in the peptides:

    >>> for pp in ppb.build_peptides(structure, aa_only=False):
    ...     print(pp.get_sequence())
    ...     print("%s %s" % (pp.get_sequence()[0], pp[0].get_resname()))
    ...     print("%s %s" % (pp.get_sequence()[-7], pp[-7].get_resname()))
    ...     print("%s %s" % (pp.get_sequence()[-6], pp[-6].get_resname()))
    MDIRQGPKEPFRDYVDRFYKTLRAEQASQEVKNWMTETLLVQNANPDCKTILKALGPGATLEEMMTACQG
    M MSE
    M MSE
    M MSE

In this case the selenomethionines (the first and also seventh and sixth from
last residues) have been shown as M (methionine) by the get_sequence method.
    )print_function)
basestringN)generic_protein)SCOPData)Seq)PDBException)calc_dihedral
calc_angleZALAZCYSZASPZGLUZPHEZGLYZHISZILEZLYSZLEUZMETZASNZPROZGLNZARGZSERZTHRZVALZTRPZTYRZACDEFGHIKLMNPQRSTVWY   c             C   s   t  |  S)zyIndex to corresponding one letter amino acid name.

    >>> index_to_one(0)
    'A'
    >>> index_to_one(19)
    'Y'
    )dindex_to_1)index r   8/tmp/pip-build-ww9dw3qa/biopython/Bio/PDB/Polypeptide.pyindex_to_onek   s    r   c             C   s   t  |  S)z`One letter code to index.

    >>> one_to_index('A')
    0
    >>> one_to_index('Y')
    19
    )d1_to_index)sr   r   r   one_to_indexv   s    r   c             C   s   t  |  S)zIndex to corresponding three letter amino acid name.

    >>> index_to_three(0)
    'ALA'
    >>> index_to_three(19)
    'TYR'
    )dindex_to_3)ir   r   r   index_to_three   s    r   c             C   s   t  |  S)zjThree letter code to index.

    >>> three_to_index('ALA')
    0
    >>> three_to_index('TYR')
    19
    )d3_to_index)r   r   r   r   three_to_index   s    r   c             C   s   t  |  } t | S)a  Three letter code to one letter code.

    >>> three_to_one('ALA')
    'A'
    >>> three_to_one('TYR')
    'Y'

    For non-standard amino acids, you get a KeyError:

    >>> three_to_one('MSE')
    Traceback (most recent call last):
       ...
    KeyError: 'MSE'
    )r   r   )r   r   r   r   r   three_to_one   s    
r   c             C   s   t  |  } t | S)zsOne letter code to three letter code.

    >>> one_to_three('A')
    'ALA'
    >>> one_to_three('Y')
    'TYR'
    )r   r   )r   r   r   r   r   one_to_three   s    
r   Fc             C   sH   t  |  t  s |  j   }  |  j   }  | r7 |  t k S|  t j k Sd S)a  Return True if residue object/string is an amino acid.

    :param residue: a L{Residue} object OR a three letter amino acid code
    :type residue: L{Residue} or string

    :param standard: flag to check for the 20 AA (default false)
    :type standard: boolean

    >>> is_aa('ALA')
    True

    Known three letter codes for modified amino acids are supported,

    >>> is_aa('FME')
    True
    >>> is_aa('FME', standard=True)
    False
    N)
isinstancer   get_resnameupperr   r   protein_letters_3to1)residuestandardr   r   r   is_aa   s    
r    c               @   s^   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z	 d S)Polypeptidez5A polypeptide is simply a list of L{Residue} objects.c             C   s2   g  } x% |  D] } | d } | j  |  q W| S)zGet list of C-alpha atoms in the polypeptide.

        :return: the list of C-alpha atoms
        :rtype: [L{Atom}, L{Atom}, ...]
        CA)append)selfca_listrescar   r   r   get_ca_list   s
    
zPolypeptide.get_ca_listc             C   s  g  } t  |   } xt d |  D]} |  | } y4 | d j   } | d j   } | d j   } Wn< t k
 r | j d	  d | j d <d | j d <w" Yn X| d k r|  | d } y) | d j   }	 t |	 | | |  }
 Wqt k
 rd }
 YqXn d }
 | | d k  rq|  | d } y) | d j   } t | | | |  } Wqwt k
 rmd } YqwXn d } | j |
 | f  |
 | j d <| | j d <q" W| S)
z+Return the list of phi/psi dihedral angles.r   Nr"   CNZPHIZPSI   )NN)lenrange
get_vector	Exceptionr#   xtrar   )r$   ZpplZlngr   r&   nr'   crpcpphiZrnnnpsir   r   r   get_phi_psi_list   sB    
zPolypeptide.get_phi_psi_listc             C   s   |  j    } g  } x t d t |  d  D] } | | | | d | | d | | d f } d d   | D \ } } } } t | | | |  }	 | j |	  | | d j   }
 |	 |
 j d <q, W| S)z?List of tau torsions angles for all 4 consecutive Calpha atoms.r      r+      c             S   s   g  |  ] } | j     q Sr   )r.   ).0ar   r   r   
<listcomp>  s   	 z,Polypeptide.get_tau_list.<locals>.<listcomp>ZTAU)r(   r-   r,   r   r#   
get_parentr0   )r$   r%   Ztau_listr   	atom_listv1v2v3Zv4taur&   r   r   r   get_tau_list  s     .zPolypeptide.get_tau_listc       
      C   s   g  } |  j    } x t d t |  d  D] } | | | | d | | d f } d d   | D \ } } } t | | |  } | j |  | | d j   }	 | |	 j d <q, W| S)z8List of theta angles for all 3 consecutive Calpha atoms.r   r:   r+   c             S   s   g  |  ] } | j     q Sr   )r.   )r;   r<   r   r   r   r=   #  s   	 z.Polypeptide.get_theta_list.<locals>.<listcomp>ZTHETA)r(   r-   r,   r	   r#   r>   r0   )
r$   Z
theta_listr%   r   r?   r@   rA   rB   thetar&   r   r   r   get_theta_list  s     #zPolypeptide.get_theta_listc             C   sI   d } x- |  D]% } | t  j j | j   d  7} q Wt | t  } | S)znReturn the AA sequence as a Seq object.

        :return: polypeptide sequence
        :rtype: L{Seq}
         X)r   r   getr   r   r   )r$   r   r&   seqr   r   r   get_sequence+  s
    #zPolypeptide.get_sequencec             C   s<   |  d j    d } |  d j    d } d | | f } | S)zReturn string representation of the polypeptide.

        Return <Polypeptide start=START end=END>, where START
        and END are sequence identifiers of the outer residues.
        r   r+   z<Polypeptide start=%s end=%s>)Zget_id)r$   startendr   r   r   r   __repr__7  s    zPolypeptide.__repr__N)
__name__
__module____qualname____doc__r(   r8   rD   rF   rK   rO   r   r   r   r   r!      s   -r!   c               @   s=   e  Z d  Z d Z d d   Z d d   Z d d d  Z d	 S)

_PPBuilderzBase class to extract polypeptides.

    It checks if two consecutive residues in a chain are connected.
    The connectivity test is implemented by a subclass.

    This assumes you want both standard and non-standard amino acids.
    c             C   s   | |  _  d S)z`Initialize the base class.

        :param radius: distance
        :type radius: float
        N)radius)r$   rU   r   r   r   __init__L  s    z_PPBuilder.__init__c             C   sO   t  | d | r d S| rG d | j k rG t j d | j    d Sd Sd S)z0Check if the residue is an amino acid (PRIVATE).r   Tr"   z5Assuming residue %s is an unknown modified amino acidFN)r    Z
child_dictwarningswarnr   )r$   r   Zstandard_aa_onlyr   r   r   _acceptT  s    z_PPBuilder._acceptr+   c             C   s  |  j  } |  j } | j   } | d k rC | d } | j   } n? | d k r^ | j   } n$ | d k rv | g } n t d   g  } x | D] }	 t |	  }
 y2 t |
  } x | | |  s t |
  } q WWn t k
 r w Yn Xd } x |
 D]~ } | | |  rj| | |  rj| | |  rj| d k rZt   } | j	 |  | j	 |  | j	 |  n d } | } q Wq W| S)a  Build and return a list of Polypeptide objects.

        :param entity: polypeptides are searched for in this object
        :type entity: L{Structure}, L{Model} or L{Chain}

        :param aa_only: if 1, the residue needs to be a standard AA
        :type aa_only: int
        Sr   Mr*   z+Entity should be Structure, Model or Chain.N)
_is_connectedrY   Z	get_levelZget_listr   iternextStopIterationr!   r#   )r$   entityZaa_onlyZis_connectedacceptlevelmodelZ
chain_listZpp_listchainZchain_itprev_resppnext_resr   r   r   build_peptidese  sB    			
	z_PPBuilder.build_peptidesN)rP   rQ   rR   rS   rV   rY   rh   r   r   r   r   rT   C  s   rT   c               @   s1   e  Z d  Z d Z d d d  Z d d   Z d S)CaPPBuilderz)Use CA--CA distance to find polypeptides.g333333@c             C   s   t  j |  |  d S)zInitialize the class.N)rT   rV   )r$   rU   r   r   r   rV     s    zCaPPBuilder.__init__c       
      C   s   x' | | g D] } | j  d  s d Sq W| d } | d } | j   rY | j   } n	 | g } | j   r} | j   } n	 | g } x6 | D]. } x% | D] }	 | |	 |  j k  r d Sq Wq Wd S)Nr"   FT)has_idis_disordereddisordered_get_listrU   )
r$   re   rg   rr1   pnlistplistr6   rf   r   r   r   r\     s     

		zCaPPBuilder._is_connectedN)rP   rQ   rR   rS   rV   r\   r   r   r   r   ri     s   ri   c               @   s=   e  Z d  Z d Z d d d  Z d d   Z d d   Z d	 S)
	PPBuilderz'Use C--N distance to find polypeptides.g?c             C   s   t  j |  |  d S)zInitialize the class.N)rT   rV   )r$   rU   r   r   r   rV     s    zPPBuilder.__init__c             C   s2  | j  d  s d S| j  d  s& d S|  j } | d } | d } | j   r^ | j   } n	 | g } | j   r | j   } n	 | g } x | D] } x | D] }	 | j   }
 |	 j   } |
 | k s |
 d k s | d k r | | |	  r | j   r	| j |  | j   r"| j |
  d Sq Wq Wd S)Nr*   Fr)    T)rj   
_test_distrk   rl   Z
get_altlocZdisordered_select)r$   re   rg   Z	test_distr2   r1   Zclistro   r6   ccZn_altlocZc_altlocr   r   r   r\     s2    	

		$zPPBuilder._is_connectedc             C   s   | | |  j  k  r d Sd Sd S)z4Return 1 if distance between atoms<radius (PRIVATE).r+   r   N)rU   )r$   r2   r1   r   r   r   rs     s    zPPBuilder._test_distN)rP   rQ   rR   rS   rV   r\   rs   r   r   r   r   rq     s   #rq   ))rS   
__future__r   Z	Bio._py3kr   rW   ZBio.Alphabetr   ZBio.Datar   ZBio.Seqr   ZBio.PDB.PDBExceptionsr   ZBio.PDB.vectorsr   r	   Zstandard_aa_namesZaa1Zaa3r   r   r   r   r-   r   Zn1Zn3r   r   r   r   r   r   r    listr!   objectrT   ri   rq   r   r   r   r   <module>3   sh   	




pV