B
    ž3Rc›  ã               @   s.   d dl mZ d dlmZ d dlZddd„ZdS )	é    )ÚChem)ÚrdFMCSNTútmp.pdbc             C   s^  t  | ¡} t  |¡}| ¡  ¡ }|  ¡  ¡ }t | |g¡}t  |j¡}t|  	|¡ƒ}|j
|dd}	|| }g }
x@|	D ]8}|t|ƒ }|| }|
 t ||  ¡ t|ƒ ¡¡ qtW t|
ƒ}|rZ|	t |
¡ }tt||ƒƒ}dd„ |  ¡ D ƒ}|  ¡ d  ¡  ¡ }xLt| ¡ ƒD ]<\}}| ¡  |¡ || ¡ kr| ¡  |||  ¡ qW t  ||¡ |S )a  
    Calculate the Root-mean-square deviation (RMSD) between two prealigned ligands, 
    even when atom names between the two ligands are not matching.
    The symmetry of the molecules is taken into consideration (e.g. tri-methyl groups). 
    Moreover, if one ligand structure has missing atoms (e.g. undefined electron density in the crystal structure), 
    the RMSD is calculated for the maximum common substructure (MCS).

    Parameters
    ----------
    lig1 : RDKit molecule
    lig2 : RDKit molecule
    rename_lig2 : bool, optional
        True to rename the atoms of lig2 according to the atom names of lig1
    output_filename : str, optional
        If rename_lig2 is set to True, a PDB file with the renamed lig2 atoms is written as output.
        This may be useful to check that the RMSD has been "properly" calculated, 
        i.e. that the atoms have been properly matched for the calculation of the RMSD.
    
    Returns
    -------
    rmsd : float
        Root-mean-square deviation between the two input molecules
    F)Zuniquifyc             S   s   g | ]}|  ¡  ¡ ‘qS © )ÚGetPDBResidueInfoZGetName)Ú.0Úatom1r   r   ú.share/RDKit/Contrib/CalcLigRMSD/CalcLigRMSD.pyú
<listcomp>D   s    zCalcLigRMSD.<locals>.<listcomp>r   )r   ZRemoveHsZGetConformerZGetPositionsr   ZFindMCSZMolFromSmartsZsmartsStringÚlistZGetSubstructMatchZGetSubstructMatchesÚappendÚnpZsqrtÚsumÚlenÚminZargminÚdictÚzipZGetAtomsr   ZGetResidueNameÚ	enumerateZSetResidueNameÚkeysZSetNameZMolToPDBFile)Zlig1Zlig2Zrename_lig2Zoutput_filenameZcoordinates_lig2Zcoordinates_lig1ZresZref_molZmas1Z	mas2_listZ	list_rmsdZmatch1Zcoordinates_lig2_tmpZdiffZlig_rmsdZmas2Zcorrespondence_key2_item1Zatom_names_lig1Zlig1_ResNameÚir   r   r   r	   ÚCalcLigRMSD   s4    


$r   )Tr   )Zrdkitr   Z
rdkit.Chemr   Znumpyr   r   r   r   r   r	   Ú<module>
   s   