Python:PDB文件中原子和残基重新编号

举报
DrugAI 发表于 2021/07/15 05:08:14 2021/07/15
【摘要】 Python脚本:PDB文件中原子和残基重新编号   Command: python renumber_pdb.py -i protein.pdb -a -r > output.pdb       renumber_pdb.py # Python 3 script to atoms and residues in ...

Python脚本:PDB文件中原子和残基重新编号

 

Command:

python renumber_pdb.py -i protein.pdb -a -r > output.pdb

 

 

 

renumber_pdb.py


  
  1. # Python 3 script to atoms and residues in a PDB file.
  2. #
  3. # run
  4. # ./renumber.py -h
  5. # for help
  6. #
  7. class Pdb(object):
  8. """ Object that allows operations with protein files in PDB format. """
  9. def __init__(self, file_cont = [], pdb_code = ""):
  10. self.cont = []
  11. self.atom = []
  12. self.hetatm = []
  13. self.fileloc = ""
  14. if isinstance(file_cont, list):
  15. self.cont = file_cont[:]
  16. elif isinstance(file_cont, str):
  17. try:
  18. with open(file_cont, 'r') as pdb_file:
  19. self.cont = [row.strip() for row in pdb_file.read().split('\n') if row.strip()]
  20. except FileNotFoundError as err:
  21. print(err)
  22. if self.cont:
  23. self.atom = [row for row in self.cont if row.startswith('ATOM')]
  24. self.hetatm = [row for row in self.cont if row.startswith('HETATM')]
  25. self.conect = [row for row in self.cont if row.startswith('CONECT')]
  26. def renumber_atoms(self, start=1):
  27. """ Renumbers atoms in a PDB file. """
  28. out = list()
  29. count = start
  30. for row in self.cont:
  31. if len(row) > 5:
  32. if row.startswith(('ATOM', 'HETATM', 'TER', 'ANISOU')):
  33. num = str(count)
  34. while len(num) < 5:
  35. num = ' ' + num
  36. row = '%s%s%s' %(row[:6], num, row[11:])
  37. count += 1
  38. out.append(row)
  39. return out
  40. def renumber_residues(self, start=1, reset=False):
  41. """ Renumbers residues in a PDB file. """
  42. out = list()
  43. count = start - 1
  44. cur_res = ''
  45. for row in self.cont:
  46. if len(row) > 25:
  47. if row.startswith(('ATOM', 'HETATM', 'TER', 'ANISOU')):
  48. next_res = row[22:27].strip() # account for letters in res., e.g., '1A'
  49. if next_res != cur_res:
  50. count += 1
  51. cur_res = next_res
  52. num = str(count)
  53. while len(num) < 3:
  54. num = ' ' + num
  55. new_row = '%s%s' %(row[:23], num)
  56. while len(new_row) < 29:
  57. new_row += ' '
  58. xcoord = row[30:38].strip()
  59. while len(xcoord) < 9:
  60. xcoord = ' ' + xcoord
  61. row = '%s%s%s' %(new_row, xcoord, row[38:])
  62. if row.startswith('TER') and reset:
  63. count = start - 1
  64. out.append(row)
  65. return out
  66. if __name__ == '__main__':
  67. import argparse
  68. parser = argparse.ArgumentParser(
  69. description='Renumber residues in a pdb file',
  70. formatter_class=argparse.RawTextHelpFormatter
  71. )
  72. parser.add_argument('-i', '--input', help='Input PDB file')
  73. parser.add_argument('-s', '--start', help='Number of the first residue in the renumbered file (default = 1)')
  74. parser.add_argument('-a', '--atoms' ,action='store_true', help='Renumbers atoms')
  75. parser.add_argument('-r', '--residues', action='store_true', help='Renumbers residues')
  76. parser.add_argument('-c', '--chainreset', action='store_true', help='Resets the residue renumbering after encountering a new chain.')
  77. parser.add_argument('-v', '--version', action='version', version='v. 1.0')
  78. args = parser.parse_args()
  79. if not args.input:
  80. print('{0}\nPlease provide an input file.\n{0}'.format(50* '-'))
  81. parser.print_help()
  82. quit()
  83. if not args.start:
  84. start = 1
  85. else:
  86. start = int(args.start)
  87. if not args.atoms and not args.residues:
  88. print('{0}\nPlease provide at least the --atoms or --residues flag.\n{0}'.format(50* '-'))
  89. parser.print_help()
  90. quit()
  91. pdb1 = Pdb(args.input)
  92. if args.atoms:
  93. pdb1.cont = pdb1.renumber_atoms(start=start)
  94. if args.residues:
  95. pdb1.cont = pdb1.renumber_residues(start=start, reset=args.chainreset)
  96. for line in pdb1.cont:
  97. print(line)

 

效果:

 

参考:

https://github.com/AspirinCode/protein-science/tree/master/scripts-and-tools/renumber_pdb


 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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