o
    Rŀg                     @   s   d 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r.ddl	m
Z
 ddlmZ ed	d
dZi Zded< ded< ded< ded< G dd
 d
ed ZG dd deZdS )z)Residue class, used by Structure objects.    )TYPE_CHECKING)TypeVar)DisorderedEntityWrapper)Entity)PDBConstructionException)Atom)Chain	_ResidueTResidue)bound   N   CA   C   Oc                	   @   sv   e Zd ZdZdd Zdd Z	ddeded	ed
efddZdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )r
   z4Represents a residue. A Residue object stores atoms.c                 C   s.   d| _ d| _|| _|| _d| _t| | dS )Initialize the class.Rr   N)level
disorderedresnamesegidinternal_coordr   __init__)selfidr   r    r   C/var/www/html/myenv/lib/python3.10/site-packages/Bio/PDB/Residue.pyr   "   s   zResidue.__init__c                 C   s*   |   }|  \}}}||||f}d| S )zReturn the residue full id.z&<Residue %s het=%s resseq=%s icode=%s>)get_resnameget_idr   r   hetflagresseqicodefull_idr   r   r   __repr__+   s   zResidue.__repr__Fr   othercompare_coordinatesreturnc                 C   s8   t |t| s	dS | j|jko| j|jkot| ||S )a  Compare this residue to the other residue using a strict definition of equality.

        The residues are equal if they have the same name, identifier,
        and their constituent atoms are strictly equal.

        :param other: The residue to compare this residue to
        :type other: Residue
        :param compare_coordinates: Whether to compare the coordinates of the atoms
        :type compare_coordinates: bool
        :return: Whether the residues are strictly equal
        :rtype: bool
        F)
isinstancetyper   r   r   strictly_equals)r   r(   r)   r   r   r   r-   2   s   
zResidue.strictly_equalsc                 C   s6   |  }| |rtd| d|  t| | dS )z}Add an Atom object.

        Checks for adding duplicate atoms, and raises a
        PDBConstructionException if so.
        zAtom z defined twice in residue N)r!   has_idr   r   add)r   atomatom_idr   r   r   r/   J   s   
zResidue.addc                 C   s
   d| _ dS )zSet the disordered flag.r   Nr   r   r   r   r   flag_disorderedW   s   
zResidue.flag_disorderedc                 C      | j S )z2Return 1 if the residue contains disordered atoms.r2   r3   r   r   r   is_disordered[      zResidue.is_disorderedc                 C   r5   )zReturn the residue name.)r   r3   r   r   r   r    _   r7   zResidue.get_resnamec                 C   s:   |   }g }|D ]}| r|| 7 }q|| q|S )z5Return the list of all atoms, unpack DisorderedAtoms.)get_listr6   disordered_get_listappend)r   	atom_listundisordered_atom_listr0   r   r   r   get_unpacked_listc   s   zResidue.get_unpacked_listc                 C   r5   )zReturn the segment identifier.)r   r3   r   r   r   	get_segidn   r7   zResidue.get_segidc                 c   s    | E dH  dS )zReturn atoms.Nr   r3   r   r   r   	get_atomsr   s   zResidue.get_atomsN)F)__name__
__module____qualname____doc__r   r'   r	   boolr-   r/   r4   r6   r    r=   r>   r?   r   r   r   r   r
      s*    	
)r   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 )DisorderedResiduezDisorderedResidue is a wrapper around two or more Residue objects.

    It is used to represent point mutations (e.g. there is a Ser 60 and a Cys 60
    residue, each with 50 % occupancy).
    c                 C   s   t | | dS )r   N)r   r   )r   r   r   r   r   r   ~   s   zDisorderedResidue.__init__c                 C   s4   | j r|  }|  \}}}||||f}d| S dS )z*Return disordered residue full identifier.z0<DisorderedResidue %s het=%s resseq=%i icode=%s>z<Empty DisorderedResidue>)
child_dictr    r!   r"   r   r   r   r'      s   zDisorderedResidue.__repr__c                 C   sV   |   }| dkr$| }| \}}}|| td||||f || dS )zAdd atom to residue.r   z2Blank altlocs in duplicate residue %s (%s, %i, %s)N)disordered_getr6   r    r!   r/   r   )r   r0   residuer   hetr$   r%   r   r   r   r/      s   

zDisorderedResidue.addc                 C   s   |   D ]}|  qdS )z,Sort the atoms in the child Residue objects.N)r9   sort)r   rH   r   r   r   rJ      s   
zDisorderedResidue.sortc                 C   s>   |  }|  }|| | |rJ || |< | | dS )zrAdd a residue object and use its resname as key.

        Arguments:
         - residue - Residue object

        N)r    
get_parent
set_parentdisordered_has_iddisordered_select)r   rH   r   chainr   r   r   disordered_add   s   
z DisorderedResidue.disordered_addc                 C   s^   | j | }| j|u }| j |= |  |r%| j r%tt| j }| | dS | j s-d| _dS dS )zRemove a child residue from the DisorderedResidue.

        Arguments:
         - resname - name of the child residue to remove, as a string.

        N)rF   selected_childdetach_parentnextiterrN   )r   r   rH   is_selectedchildr   r   r   disordered_remove   s   



z#DisorderedResidue.disordered_removeN)
r@   rA   rB   rC   r   r'   r/   rJ   rP   rW   r   r   r   r   rE   w   s    
rE   N)rC   typingr   r   Bio.PDB.Entityr   r   Bio.PDB.PDBExceptionsr   Bio.PDB.Atomr   Bio.PDB.Chainr   r	   _atom_name_dictr
   rE   r   r   r   r   <module>   s"   X