[python] Renumber residues in a PDB file starting from 1

Viewer

copydownloadembedprintName: Renumber residues in a PDB file starting from 1
  1. # Save this file as renum.py and type python3 renum.py
  2. # Note: expects input file to be called input.pdb
  3.  
  4. file1=open("input.pdb","r")
  5. file2=open("output.pdb","w")
  6.  
  7. resold=""
  8. resout=0
  9. for line in file1:
  10.   if line[0:6]=="ATOM  " or line[0:6]=="HETATM":
  11.     string1=line[:22]
  12.     resnum=line[22:27]
  13.     x_i=float(line[30:38])
  14.     y_i=float(line[38:46])
  15.     z_i=float(line[46:54])
  16.     occu=float(line[54:60])
  17.     biso=float(line[60:66])
  18.     string2=line[66:78]
  19.     if resnum!=resold:
  20.        resout+=1
  21.        resold=resnum
  22.     x_o, y_o, z_o = x_i, y_i, z_i
  23.     lineout="{0}{1:4d}{2:12.3f}{3:8.3f}{4:8.3f}{5:6.2f}{6:6.2f}{7}\n".format(string1,resout,x_o,y_o,z_o,occu,biso,string2)
  24.     file2.write(lineout)
  25.  
  26.   else: file2.write(line)
  27.  
  28. file1.close()
  29. file2.close()
  30.  

Editor

You can edit this paste and save as new:


File Description
  • Renumber residues in a PDB file starting from 1
  • Paste Code
  • 01 Dec-2022
  • 785 Bytes
You can Share it: