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

举报
DrugAI 发表于 2021/07/15 03:50:38 2021/07/15
【摘要】 Python脚本:计算两个蛋白或小分子之间的RMSD 用法: python rmsd.py protein1.pdb protein2.pdb   rmsd.py   # Root-mean-square deviation (RMSD) for proteins and/or ligands# in PDB files.# class Pdb...

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

用法:

python rmsd.py protein1.pdb protein2.pdb

 

rmsd.py

 


  
  1. # Root-mean-square deviation (RMSD) for proteins and/or ligands
  2. # in PDB files.
  3. #
  4. class Pdb(object):
  5. """ Object that allows operations with protein files in PDB format. """
  6. def __init__(self, file_cont = [], pdb_code = ""):
  7. self.cont = []
  8. self.atom = []
  9. self.hetatm = []
  10. self.fileloc = ""
  11. if isinstance(file_cont, list):
  12. self.cont = file_cont[:]
  13. elif isinstance(file_cont, str):
  14. try:
  15. with open(file_cont, 'r') as pdb_file:
  16. self.cont = [row.strip() for row in pdb_file.read().split('\n') if row.strip()]
  17. except FileNotFoundError as err:
  18. print(err)
  19. if self.cont:
  20. self.atom = [row for row in self.cont if row.startswith('ATOM')]
  21. self.hetatm = [row for row in self.cont if row.startswith('HETATM')]
  22. self.conect = [row for row in self.cont if row.startswith('CONECT')]
  23. def rmsd(self, sec_molecule, ligand=False, atoms="no_h"):
  24. """
  25. Calculates the Root Mean Square Deviation (RMSD) between two
  26. protein or ligand molecules in PDB format.
  27. Requires that both molecules have the same number of atoms in the
  28. same numerical order.
  29. Keyword arguments:
  30. sec_molecule (PdbObj): the second molecule as PdbObj object.
  31. ligand (bool): If true, calculates the RMSD between two
  32. ligand molecules (based on HETATM entries), else RMSD
  33. between two protein molecules (ATOM entries) is calculated.
  34. hydrogen (bool): If True, hydrogen atoms will be included in the
  35. RMSD calculation.
  36. atoms (string) [all/c/no_h/ca]: "all" includes all atoms in the RMSD calculation,
  37. "c" only considers carbon atoms, "no_h" considers all but hydrogen atoms,
  38. and "ca" compares only C-alpha protein atoms.
  39. Returns:
  40. Calculated RMSD value as float or None if RMSD not be
  41. calculated.
  42. """
  43. rmsd = None
  44. if not ligand:
  45. coords1, coords2 = self.atom, sec_molecule.atom
  46. else:
  47. coords1, coords2 = self.hetatm, sec_molecule.hetatm
  48. if atoms == "c":
  49. coords1 = [row for row in coords1 if row[77:].startswith('C')]
  50. coords2 = [row for row in coords2 if row[77:].startswith('C')]
  51. elif atoms == "no_h":
  52. coords1 = [row for row in coords1 if not row[77:].startswith('H')]
  53. coords2 = [row for row in coords2 if not row[77:].startswith('H')]
  54. elif atoms == "ca":
  55. coords1 = self.calpha()
  56. coords2 = sec_molecule.calpha()
  57. if all((coords1, coords2, len(coords1) == len(coords2))):
  58. total = 0
  59. for (i, j) in zip(coords1, coords2):
  60. total += ( float(i[30:38]) - float(j[30:38]) )**2 +\
  61. ( float(i[38:46]) - float(j[38:46]) )**2 +\
  62. ( float(i[46:54]) - float(j[46:54]) )**2
  63. rmsd = round(( total / len(coords1) )**0.5, 4)
  64. return rmsd
  65. if __name__ == '__main__':
  66. import argparse
  67. parser = argparse.ArgumentParser(
  68. description='The RMSD measures the average distance between atoms \n'\
  69. 'of 2 protein or ligand structures.\n'\
  70. 'By default, all atoms but hydrogen atoms of the protein are included in the RMSD calculation.\n'\
  71. 'NOTE: Both structures must contain the same number of atoms in similar order.',
  72. epilog='Example:\n'\
  73. 'rmsd.py ~/Desktop/pdb1.pdb ~/Desktop/pdb2.pdb\n'\
  74. '0.7377',
  75. formatter_class=argparse.RawTextHelpFormatter
  76. )
  77. parser.add_argument('PDBfile1')
  78. parser.add_argument('PDBfile2')
  79. parser.add_argument('-l', '--ligand', action='store_true', help='Calculates RMSD between ligand (HETATM) atoms.')
  80. parser.add_argument('-c', '--carbon', action='store_true', help='Calculates the RMSD between carbon atoms only.')
  81. parser.add_argument('-ca', '--calpha', action='store_true', help='Calculates the RMSD between alpha-carbon atoms only.')
  82. parser.add_argument('-v', '--version', action='version', version='rmsd v. 1.0')
  83. args = parser.parse_args()
  84. pdb1 = Pdb(args.PDBfile1)
  85. pdb2 = Pdb(args.PDBfile2)
  86. if args.carbon and args.calpha:
  87. print('\nERROR: Please provide EITHER -c OR -ca, not both.\n')
  88. parser.print_help()
  89. quit()
  90. if args.ligand and args.carbon:
  91. print(pdb1.rmsd(sec_molecule=pdb2, ligand=True, atoms="c"))
  92. elif args.ligand and args.calpha:
  93. print(pdb1.calpha())
  94. print(pdb1.rmsd(sec_molecule=pdb2, ligand=True, atoms="ca"))
  95. elif args.ligand:
  96. print(pdb1.rmsd(sec_molecule=pdb2, ligand=True, atoms="no_h"))
  97. elif args.calpha:
  98. print(pdb1.rmsd(sec_molecule=pdb2, ligand=False, atoms="ca"))
  99. elif args.carbon:
  100. print(pdb1.rmsd(sec_molecule=pdb2, ligand=False, atoms="c"))
  101. else:
  102. print(pdb1.rmsd(sec_molecule=pdb2, ligand=False, atoms="no_h"))

 

 

 

文章来源: drugai.blog.csdn.net,作者:DrugAI,版权归原作者所有,如需转载,请联系作者。

原文链接:drugai.blog.csdn.net/article/details/81008659

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。