4-rmsf.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. from prody import *
  3. from pylab import *
  4. import matplotlib.pyplot as plt
  5. import itertools
  6. import os
  7. import glob
  8. import re
  9. ion()
  10. prody.confProDy(auto_show=False)
  11. # set output dir
  12. out_dir = os.path.splitext(os.path.basename(sys.argv[0]))[0]
  13. out_file = out_dir + "/combined"
  14. # create output dir
  15. if not os.path.exists(out_dir):
  16. os.makedirs(out_dir)
  17. # create palette
  18. colors = itertools.cycle(["red", "blue", "green", "orange"])
  19. # parse files
  20. in_files=os.listdir("1-concat/eda/")
  21. out_files=os.listdir(out_dir)
  22. systems = []
  23. get_systems=os.listdir("1-concat/combined/")
  24. for filename in get_systems:
  25. if filename.endswith(".pdb"):
  26. systems.append(re.sub('-.*','',filename))
  27. my_pdb=glob.glob("1-concat/eda/combined*.pdb")
  28. # if system_names file is present, set names from there
  29. if os.path.exists('system_names'):
  30. exec(open('system_names').read())
  31. else:
  32. systems=sorted(systems)
  33. # load pdb
  34. structure = parsePDB(my_pdb[0])
  35. # load trajectory
  36. if os.path.exists(out_file + '_prody.dcd'):
  37. trajectory = parseDCD(out_file + '_prody.dcd')
  38. trajectory.setCoords(structure)
  39. trajectory.setAtoms(structure.all)
  40. else:
  41. dcd = glob.glob('1-concat/eda/combined-*.dcd')
  42. trajectory = parseDCD(dcd[0])
  43. trajectory.setCoords(structure)
  44. trajectory.setAtoms(structure.all)
  45. trajectory.superpose()
  46. # save aligned dcd
  47. writeDCD(out_file + '_prody.dcd', trajectory)
  48. # calculate frame increments
  49. num_frames = DCDFile.numFrames(trajectory)
  50. num_systems = len(systems)
  51. frame_incr = int(num_frames / num_systems - 1)
  52. res_nums = AtomGroup.getResnums(structure)
  53. first_res = res_nums[0]
  54. last_res = res_nums[-1]
  55. # create rmsf figure
  56. first_frame = 0
  57. last_frame = frame_incr
  58. plt.figure(figsize=(12, 6))
  59. for system in systems:
  60. rmsf = calcRMSF(trajectory[first_frame:last_frame])
  61. print(rmsf)
  62. plt.plot(res_nums, rmsf, label=system, color=next(colors), linewidth=1.5)
  63. first_frame = last_frame + 1
  64. last_frame = first_frame + frame_incr
  65. title('RMSF', size=20)
  66. plt.xlabel('Residue #', size=16)
  67. plt.ylabel('Angstroms', size=16)
  68. plt.xlim([first_res,last_res])
  69. ax = subplot(1,1,1)
  70. handles, labels = ax.get_legend_handles_labels()
  71. labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: t[0]))
  72. plt.legend(handles, labels, loc='upper left')
  73. ax.grid(True)
  74. plt.savefig(out_file + '_rmsf.png', dpi=300, format='png')
  75. plt.close('all[')