
    Ri7                        d 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 ddl	m
Z
 dd	l	mZ dd
lmZ  e e ej                          d        \  ZZeZi Zi Zi Zi Z ed      D ]   Zee   Zee   Zeee<   eee<   eee<   eee<   " d Zd Zd Zd ZddZ ddZ! G d de"      Z# G d d      Z$ G d de$      Z% G d de$      Z&y)a  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.
    N)nucleic_letters_3to1)nucleic_letters_3to1_extended)protein_letters_3to1)protein_letters_3to1_extended)PDBException)
calc_angle)calc_dihedral)Seqc                     | d   S )N    )xs    K/home/agent/.friday_env/lib/python3.12/site-packages/Bio/PDB/Polypeptide.py<lambda>r   A   s
    1Q4     )key   c                     t         |    S )zyIndex to corresponding one letter amino acid name.

    >>> index_to_one(0)
    'A'
    >>> index_to_one(19)
    'Y'
    )dindex_to_1)indexs    r   index_to_oner   S   s     ur   c                     t         |    S )z`One letter code to index.

    >>> one_to_index('A')
    0
    >>> one_to_index('Y')
    19
    )d1_to_indexss    r   one_to_indexr   ^        q>r   c                     t         |    S )zIndex to corresponding three letter amino acid name.

    >>> index_to_three(0)
    'ALA'
    >>> index_to_three(19)
    'TYR'
    )dindex_to_3)is    r   index_to_threer!   i   r   r   c                     t         |    S )zjThree letter code to index.

    >>> three_to_index('ALA')
    0
    >>> three_to_index('TYR')
    19
    )d3_to_indexr   s    r   three_to_indexr$   t   r   r   c                     t        | t              s| j                         d} | j                         } |r| t        v S | t
        v 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
    <3s)
isinstancestrget_resnameupperr   r   residuestandards     r   is_aar.      sG    & gs#((*3/mmoG...777r   c                     t        | t              s| j                         d} | j                         } |r| t        v S | t
        v S )a
  Return True if residue object/string is a nucleic acid.

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

    :param standard: flag to check for the 8 (DNA + RNA) canonical bases.
        Default is False.
    :type standard: boolean

    >>> is_nucleic('DA ')
    True

    >>> is_nucleic('A  ')
    True

    Known three letter codes for modified nucleotides are supported,

    >>> is_nucleic('A2L')
    True
    >>> is_nucleic('A2L', standard=True)
    False
    r&   )r'   r(   r)   r*   r   r   r+   s     r   
is_nucleicr0      sG    . gs#((*3/mmoG...777r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	Polypeptidez5A polypeptide is simply a list of L{Residue} objects.c                 D    g }| D ]  }|d   }|j                  |        |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cas       r   get_ca_listzPolypeptide.get_ca_list   s4      	CTBNN2	 r   c                    g }t        |       }t        |      D ]  }| |   }	 |d   j                         }|d   j                         }|d   j                         }|dkD  r+| |d	z
     }	 |d   j                         }	t        |	|||      }
nd}
||d	z
  k  r+| |d	z      }	 |d   j                         }t        ||||      }nd}|j	                  |
|f       |
|j
                  d<   ||j
                  d<    |S # t        $ r3 |j	                  d       d|j
                  d<   d|j
                  d<   Y w xY w# t        $ r d}
Y w xY w# t        $ r d}Y w xY w)
z+Return the list of phi/psi dihedral angles.Nr4   C)NNNPHIPSIr   r   )lenrange
get_vector	Exceptionr5   xtrar	   )r6   ppllngr    r8   nr9   crpcpphirnnnpsis                 r   get_phi_psi_listzPolypeptide.get_phi_psi_list   s   $is &	"Aq'C
H'')Y))+H'') 1u!a%[C++-B'Ar15C
 C!G}!a%[C++-B'2q"5C
 JJSz"!CHHUO!CHHUOM&	"N 
C   

<("&"& ! C ! Cs5   9C7)!D6!E78D32D36EEEEc                 <   | j                         }g }t        t        |      dz
        D ]o  }||   ||dz      ||dz      ||dz      f}d |D        \  }}}}t        ||||      }	|j	                  |	       ||dz      j                         }
|	|
j                  d<   q |S )z?List of tau torsions angles for all 4 consecutive Calpha atoms.   r      c              3   <   K   | ]  }|j                           y wNrB   .0as     r   	<genexpr>z+Polypeptide.get_tau_list.<locals>.<genexpr>   s     @alln@   TAU)r:   rA   r@   r	   r5   
get_parentrD   )r6   r7   tau_listr    	atom_listv1v2v3v4taur8   s              r   get_tau_listzPolypeptide.get_tau_list   s    ""$s7|a'( 	"A WQU^WQU^WQQRU^TI@i@NBBBB/COOC !a%.++-C!CHHUO	" r   c                 *   g }| j                         }t        t        |      dz
        D ]f  }||   ||dz      ||dz      f}d |D        \  }}}t        |||      }|j	                  |       ||dz      j                         }	||	j                  d<   h |S )z8List of theta angles for all 3 consecutive Calpha atoms.rR   r   c              3   <   K   | ]  }|j                           y wrT   rU   rV   s     r   rY   z-Polypeptide.get_theta_list.<locals>.<genexpr>  s     <Q!,,.<rZ   THETA)r:   rA   r@   r   r5   r\   rD   )
r6   
theta_listr7   r    r^   r_   r`   ra   thetar8   s
             r   get_theta_listzPolypeptide.get_theta_list  s    
""$s7|a'( 	&A WQU^WQU^DI<)<JBBr2r*Ee$!a%.++-C %CHHW	& r   c                 H    dj                  d | D              }t        |      S )znReturn the AA sequence as a Seq object.

        :return: polypeptide sequence
        :rtype: L{Seq}
         c              3   d   K   | ](  }t        j                  |j                         d        * yw)XN)r   getr)   )rW   r8   s     r   rY   z+Polypeptide.get_sequence.<locals>.<genexpr>  s*      
JM)--coo.?E
s   .0)joinr
   )r6   r   s     r   get_sequencezPolypeptide.get_sequence  s*     GG 
QU
 
 1vr   c                 l    | d   j                         d   }| d   j                         d   }d| d| d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=z end=>)get_id)r6   startends      r   __repr__zPolypeptide.__repr__  sC     Q #2hoo"$UG5Q77r   N)
__name__
__module____qualname____doc__r:   rO   rd   rj   rq   rx   r   r   r   r2   r2      s$    ?
+Z	8r   r2   c                   $    e Zd ZdZd Zd ZddZy)
_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                     || _         y)z`Initialize the base class.

        :param radius: distance
        :type radius: float
        Nradiusr6   r   s     r   __init__z_PPBuilder.__init__2  s     r   c                     t        ||      ry|s5d|j                  v r't        j                  d|j	                         z         yy)z0Check if the residue is an amino acid (PRIVATE).)r-   Tr4   z5Assuming residue %s is an unknown modified amino acidF)r.   
child_dictwarningswarnr)   )r6   r,   standard_aa_onlys      r   _acceptz_PPBuilder._accept:  sL    #34!dg.@.@&@ MMG%%'(  r   c                 J   | j                   }| j                  }|j                         }|dk(  r|d   }|j                         }n*|dk(  r|j                         }n|dk(  r|g}nt	        d      g }|D ]  }	t        |	      }
	 t        |
      } |||      st        |
      } |||      sd}|
D ]a  } |||      rR |||      rI |||      r@|,t               }|j                  |       |j                  |       |j                  |       nd}|}c  |S # t        $ r Y w xY w)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_connectedr   	get_levelget_listr   iternextStopIterationr2   r5   )r6   entityaa_onlyis_connectedacceptlevelmodel
chain_listpp_listchainchain_itprev_resppnext_ress                 r   build_peptidesz_PPBuilder.build_peptidesK  sD    ))  "C<1IE)Jc\*Jc\ JLMM 	$EE{H> 73#H~H !73
 B$ $8W-x1$Xx8z(]		(+r*IIh' B#$	$4 ) ! s   (D	D"!D"N)r   )ry   rz   r{   r|   r   r   r   r   r   r   r~   r~   )  s    "1r   r~   c                       e Zd ZdZddZd Zy)CaPPBuilderz)Use CA--CA distance to find polypeptides.c                 0    t         j                  | |       yzInitialize the class.Nr~   r   r   s     r   r   zCaPPBuilder.__init__      D&)r   c                 &   ||fD ]  }|j                  d      r y |d   }|d   }|j                         r|j                         }n|g}|j                         r|j                         }n|g}|D ]  }|D ]  }	||	z
  | j                  k  s  y   y)Nr4   FT)has_idis_disordereddisordered_get_listr   )
r6   r   r   rrG   pnlistplistrM   r   s
             r   r   zCaPPBuilder._is_connected  s    H% 	A88D>	 TNTN??))+ECE??))+ECE 	 B  Gt{{* 	  r   N)g333333@)ry   rz   r{   r|   r   r   r   r   r   r   r     s    3*r   r   c                   $    e Zd ZdZddZd Zd Zy)	PPBuilderz'Use C--N distance to find polypeptides.c                 0    t         j                  | |       yr   r   r   s     r   r   zPPBuilder.__init__  r   r   c                     |j                  d      sy|j                  d      sy| j                  }|d   }|d   }|j                         r|j                         }n|g}|j                         r|j                         }n|g}|D ]  }|D ]  }	|j	                         }
|	j	                         }|
|k(  s|
dk(  s|dk(  s3 |||	      s=|j                         r|j                  |       |j                         r|j                  |
         y  y)Nr=   Fr<    T)r   
_test_distr   r   
get_altlocdisordered_select)r6   r   r   	test_distrH   rG   clistr   rM   ccn_altlocc_altlocs               r   r   zPPBuilder._is_connected  s   s#s#OO	SMSM??))+ECE??))+ECE 	$B $ ==?==?x'8s?h#o R( ??,//9??,//9#$	$  r   c                 *    ||z
  | j                   k  ryy)z4Return 1 if distance between atoms<radius (PRIVATE).r   r   r   )r6   rH   rG   s      r   r   zPPBuilder._test_dist  s    ET[[ r   N)g?)ry   rz   r{   r|   r   r   r   r   r   r   r   r     s    1*!Fr   r   )F)'r|   r   Bio.Data.PDBDatar   r   r   r   Bio.PDB.PDBExceptionsr   Bio.PDB.vectorsr   r	   Bio.Seqr
   zipsorteditemsaa3aa1standard_aa_namesr   r   r#   r   rA   r    n1n3r   r   r!   r$   r.   r0   listr2   r~   r   r   r   r   r   <module>r      s  +Z  1 : 1 : . & )  1+113HIS  
r A	QB	QBKOKNKOKN888@k8$ k8\S Sl* :/
 /r   