o
    Rŀg                     @   sJ   d Z ddlZddlmZ ddlmZ ddlmZ dd Z	G dd	 d	Z
dS )
u  Protein Structural Alignment using Combinatorial Extension.

Python code written by Joao Rodrigues. C++ code and Python/C++ interface
adapted from open-source Pymol and originally written by Jason Vertrees.
The original license and notices are available in `cealign` folder.

Reference
---------
Shindyalov, I.N., Bourne P.E. (1998).
"Protein structure alignment by incremental combinatorial extension (CE)
of the optimal path". Protein Engineering. 11 (9): 739–747. PMID 9796821.
    N)run_cealign)PDBException)QCPSuperimposerc                 C   s
   | j d S )N   )id)r r   C/var/www/html/myenv/lib/python3.10/site-packages/Bio/PDB/cealign.py<lambda>   s   
 r
   c                   @   s4   e Zd ZdZdddZdd Zdd	 ZdddZdS )	CEAlignerz7Protein Structure Alignment by Combinatorial Extension.      c                 C   s6   |dksJ d|dksJ d|| _ || _d| _dS )a  Superimpose one set of atoms onto another using structural data.

        Structures are superimposed using guide atoms, CA and C4', for protein
        and nucleic acid molecules respectively.

        Parameters
        ----------
        window_size : float, optional
            CE algorithm parameter. Used to define paths when building the
            CE similarity matrix. Default is 8.
        max_gap : float, optional
            CE algorithm parameter. Maximum gap size. Default is 30.
        r   z"window_size must be greater than 0z"max_gap must be positive (or zero)N)window_sizemax_gaprms)selfr   r   r   r   r	   __init__!   s
   
zCEAligner.__init__c                 C   s   g }t | D ](}t |tdD ]}d|v r!||d j  qd|v r/||d j  qq|s>d|j d}t||S )zReturn the coordinates of guide atoms in the structure.

        We use guide atoms (C-alpha and C4' atoms) since it is much faster than
        using all atoms in the calculation without a significant loss in
        accuracy.
        )keyCAzC4'z
Structure z does not have any guide atoms.)sorted
get_chains_RESID_SORTERappendcoordtolistr   r   )r   	structurecoordschainresidmsgr   r   r	   get_guide_coord_from_structure7   s   z(CEAligner.get_guide_coord_from_structurec                 C   sB   |  || _t| j| jd k rt| j}d| d}t|dS )zCDefine a reference structure onto which all others will be aligned.   z*Too few atoms in the reference structure (*). Try reducing the window_size parameter.N)r    refcoordlenr   r   )r   r   n_atomsr   r   r   r	   set_referenceL   s   

zCEAligner.set_referenceTc                    sP  d_ | t jd k rt }d| d}t|tj jj}t|d d fdd|D }d\}}|D ]9}	|	\}
}t	fd	d|
D }t	 fd
d|D }t
 }||| |  |j |k ry|j }|j|jf}q@|du rtd|r|\}}| D ]}| D ]}| D ]}||| qqq|_ dS )a  Align the input structure onto the reference structure.

        Parameters
        ----------
        transform: bool, optional
            If True (default), apply the rotation/translation that minimizes
            the RMSD between the two structures to the input structure. If
            False, the structure is not modified but the optimal RMSD will
            still be calculated.
        Nr!   z'Too few atoms in the mobile structure (r"   r   c                    s,   g | ]\}}t | krt|t|fqS r   )r$   tuple).0pApB)longest_lengthr   r	   
<listcomp>t   s    &z#CEAligner.align.<locals>.<listcomp>)g    .ANc                    s   g | ]} j | qS r   )r#   r(   i)r   r   r	   r,   ~   s    c                    s   g | ]} | qS r   r   r-   )r   r   r	   r,      s    z$Failed to find a suitable alignment.)r   r    r$   r   r   r   r#   r   nparrayr   setrunrottranRuntimeErrorr   get_unpacked_list	transform)r   r   r7   r%   r   pathslongest_paths	best_rmsdbest_upathidxAidxBcoordsAcoordsBalnrotmtxtrvecr   r   atomr   )r   r+   r   r	   alignX   sF   




zCEAligner.alignN)r   r   )T)__name__
__module____qualname____doc__r   r    r&   rE   r   r   r   r	   r      s    
r   )rI   numpyr/   Bio.PDB.ccealignr   Bio.PDB.PDBExceptionsr   Bio.PDB.qcprotr   r   r   r   r   r   r	   <module>   s   