Python:计算两个蛋白或小分子之间的RMSD

简介: Python:计算两个蛋白或小分子之间的RMSD

Python脚本:计算两个蛋白或小分子之间的RMSD

用法:

image.png

rmsd.py

# Root-mean-square deviation (RMSD) for proteins and/or ligands
# in PDB files.
#
class Pdb(object):
    """ Object that allows operations with protein files in PDB format. """
    def __init__(self, file_cont = [], pdb_code = ""):
        self.cont = []
        self.atom = []
        self.hetatm = []
        self.fileloc = ""
        if isinstance(file_cont, list):
            self.cont = file_cont[:]
        elif isinstance(file_cont, str):
            try:
                with open(file_cont, 'r') as pdb_file:
                    self.cont = [row.strip() for row in pdb_file.read().split('\n') if row.strip()]
            except FileNotFoundError as err:
                print(err)
        if self.cont:
             self.atom = [row for row in self.cont if row.startswith('ATOM')]
             self.hetatm = [row for row in self.cont if row.startswith('HETATM')]
             self.conect = [row for row in self.cont if row.startswith('CONECT')]
    def rmsd(self, sec_molecule, ligand=False, atoms="no_h"):
        """
        Calculates the Root Mean Square Deviation (RMSD) between two
        protein or ligand molecules in PDB format.
        Requires that both molecules have the same number of atoms in the
        same numerical order.
        Keyword arguments:
            sec_molecule (PdbObj): the second molecule as PdbObj object.
            ligand (bool): If true, calculates the RMSD between two
                ligand molecules (based on HETATM entries), else RMSD
                between two protein molecules (ATOM entries) is calculated.
            hydrogen (bool): If True, hydrogen atoms will be included in the
                    RMSD calculation.
            atoms (string) [all/c/no_h/ca]: "all" includes all atoms in the RMSD calculation,
                "c" only considers carbon atoms, "no_h" considers all but hydrogen atoms,
                and "ca" compares only C-alpha protein atoms.
        Returns:
            Calculated RMSD value as float or None if RMSD not be
            calculated.
        """
        rmsd = None
        if not ligand:
            coords1, coords2 = self.atom, sec_molecule.atom
        else:
            coords1, coords2 = self.hetatm, sec_molecule.hetatm
        if atoms == "c":
            coords1 = [row for row in coords1 if row[77:].startswith('C')]
            coords2 = [row for row in coords2 if row[77:].startswith('C')]
        elif atoms == "no_h":
            coords1 = [row for row in coords1 if not row[77:].startswith('H')]
            coords2 = [row for row in coords2 if not row[77:].startswith('H')]
        elif atoms == "ca":
            coords1 = self.calpha()
            coords2 = sec_molecule.calpha()
        if all((coords1, coords2, len(coords1) == len(coords2))):
            total = 0
            for (i, j) in zip(coords1, coords2):
                total += ( float(i[30:38]) - float(j[30:38]) )**2 +\
                         ( float(i[38:46]) - float(j[38:46]) )**2 +\
                         ( float(i[46:54]) - float(j[46:54]) )**2      
            rmsd = round(( total / len(coords1) )**0.5, 4)
        return rmsd
if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(
        description='The RMSD measures the average distance between atoms \n'\
            'of 2 protein or ligand structures.\n'\
            'By default, all atoms but hydrogen atoms of the protein are included in the RMSD calculation.\n'\
            'NOTE: Both structures must contain the same number of atoms in similar order.',
        epilog='Example:\n'\
                'rmsd.py ~/Desktop/pdb1.pdb ~/Desktop/pdb2.pdb\n'\
                '0.7377',
        formatter_class=argparse.RawTextHelpFormatter
        )
    parser.add_argument('PDBfile1')
    parser.add_argument('PDBfile2')
    parser.add_argument('-l', '--ligand', action='store_true', help='Calculates RMSD between ligand (HETATM) atoms.')
    parser.add_argument('-c', '--carbon', action='store_true', help='Calculates the RMSD between carbon atoms only.')
    parser.add_argument('-ca', '--calpha', action='store_true', help='Calculates the RMSD between alpha-carbon atoms only.')
    parser.add_argument('-v', '--version', action='version', version='rmsd v. 1.0')
    args = parser.parse_args()
    pdb1 = Pdb(args.PDBfile1)
    pdb2 = Pdb(args.PDBfile2)
    if args.carbon and args.calpha:
        print('\nERROR: Please provide EITHER -c OR -ca, not both.\n')
        parser.print_help()
        quit()
    if args.ligand and args.carbon:
        print(pdb1.rmsd(sec_molecule=pdb2, ligand=True, atoms="c"))
    elif args.ligand and args.calpha:
        print(pdb1.calpha())
        print(pdb1.rmsd(sec_molecule=pdb2, ligand=True, atoms="ca"))
    elif args.ligand:
        print(pdb1.rmsd(sec_molecule=pdb2, ligand=True, atoms="no_h"))
    elif args.calpha:
        print(pdb1.rmsd(sec_molecule=pdb2, ligand=False, atoms="ca"))
    elif args.carbon:
        print(pdb1.rmsd(sec_molecule=pdb2, ligand=False, atoms="c"))
    else:
        print(pdb1.rmsd(sec_molecule=pdb2, ligand=False, atoms="no_h"))


目录
相关文章
|
12月前
|
Python
Python中Cp、Cpk、Pp、Ppk的计算与应用
总的来说,Cp、Cpk、Pp、Ppk是衡量过程能力的重要工具,它们可以帮助我们了解和改进生产过程,提高产品质量。
1476 13
|
12月前
|
存储 人工智能 算法
使用Python计算从位置x到y的最少步数
本文通过Python代码结合广度优先搜索(BFS)算法,解决从起点到终点的最少步数问题。以二维网格为例,机器人只能上下左右移动,目标是最短路径。BFS按层遍历,确保首次到达终点即为最短路径。文中提供完整Python实现,包括队列与访问标记数组的使用,并输出示例结果。此外,还探讨了双向BFS、Dijkstra及A*算法等优化方法,帮助读者深入理解最短路径问题及其高效解决方案。
312 0
|
Python
Python中的函数是**一种命名的代码块,用于执行特定任务或计算
Python中的函数是**一种命名的代码块,用于执行特定任务或计算
287 18
|
Python
使用Python计算字符串的SHA-256散列值
使用Python计算字符串的SHA-256散列值
457 7
|
机器学习/深度学习 算法 编译器
Python程序到计算图一键转化,详解清华开源深度学习编译器MagPy
【10月更文挑战第26天】MagPy是一款由清华大学研发的开源深度学习编译器,可将Python程序一键转化为计算图,简化模型构建和优化过程。它支持多种深度学习框架,具备自动化、灵活性、优化性能好和易于扩展等特点,适用于模型构建、迁移、部署及教学研究。尽管MagPy具有诸多优势,但在算子支持、优化策略等方面仍面临挑战。
715 3
|
Python
【10月更文挑战第15天】「Mac上学Python 26」小学奥数篇12 - 图形变换与坐标计算
本篇将通过 Python 和 Cangjie 双语实现图形变换与坐标计算。这个题目帮助学生理解平面几何中的旋转、平移和对称变换,并学会用编程实现坐标变化。
360 1
|
数据可视化 Python
【10月更文挑战第12天】「Mac上学Python 23」小学奥数篇9 - 基础概率计算
本篇将通过 Python 和 Cangjie 双语实现基础概率的计算,帮助学生学习如何解决简单的概率问题,并培养逻辑推理和编程思维。
244 1
|
机器学习/深度学习 移动开发 Python
【10月更文挑战第11天】「Mac上学Python 22」小学奥数篇8 - 排列组合计算
本篇将通过 Python 和 Cangjie 双语讲解如何计算排列与组合。这道题目旨在让学生学会使用排列组合公式解决实际问题,并加深对数学知识和编程逻辑的理解。
308 4
|
Python
【10月更文挑战第10天】「Mac上学Python 19」小学奥数篇5 - 圆和矩形的面积计算
本篇将通过 Python 和 Cangjie 双语解决简单的几何问题:计算圆的面积和矩形的面积。通过这道题,学生将掌握如何使用公式解决几何问题,并学会用编程实现数学公式。
671 60
|
Python
使用python计算两个日期之前的相差天数,周数
使用python计算两个日期之前的相差天数,周数
903 0

推荐镜像

更多