5-eda.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #######################################################################################
  12. ## ADJUSTABLE PARAMETERS ##
  13. #######################################################################################
  14. # set specific system names to visualize
  15. #visualize=[]
  16. #show=["ALL", "no_PHQ"]
  17. #######################################################################################
  18. ## JOB CONTROL ##
  19. #######################################################################################
  20. # set output dir
  21. out_dir = os.path.splitext(os.path.basename(sys.argv[0]))[0]
  22. out_file = out_dir + "/combined"
  23. # create palette
  24. colors = itertools.cycle(["red", "blue", "green", "orange"])
  25. # create output dir
  26. if not os.path.exists(out_dir):
  27. os.makedirs(out_dir)
  28. # parse files
  29. in_files=os.listdir("1-concat/eda/")
  30. out_files=os.listdir(out_dir)
  31. systems = []
  32. get_systems=os.listdir("1-concat/combined/")
  33. for filename in get_systems:
  34. if filename.endswith(".pdb"):
  35. systems.append(re.sub('-.*','',filename))
  36. my_pdb=glob.glob("1-concat/eda/combined*.pdb")
  37. # if system_names file is present, set names from there
  38. if os.path.exists('system_names'):
  39. exec(open('system_names').read())
  40. else:
  41. systems=sorted(systems)
  42. # load pdb
  43. structure = parsePDB(my_pdb[0])
  44. structure_select = structure.select("resnum 88:203")
  45. # load trajectory
  46. if os.path.exists(out_file + '_prody.dcd'):
  47. trajectory = parseDCD(out_file + '_prody.dcd')
  48. trajectory.setCoords(structure_select)
  49. trajectory.setAtoms(structure_select)
  50. else:
  51. combined_dcd = glob.glob('1-concat/eda/combined-*.dcd')
  52. trajectory = parseDCD(combined_dcd[0])
  53. trajectory.setCoords(structure)
  54. trajectory.setAtoms(structure_select)
  55. trajectory.superpose()
  56. # save aligned dcd
  57. writeDCD(out_file + '_prody.dcd', trajectory)
  58. # calculate frame increments
  59. num_frames = DCDFile.numFrames(trajectory)
  60. num_systems = len(systems)
  61. frame_incr = int(num_frames / num_systems - 1)
  62. res_nums = structure_select.getResnums()
  63. first_res = res_nums[0]
  64. last_res = res_nums[-1]
  65. # calculate covariance and modes
  66. if os.path.exists(out_file + '.eda.npz'):
  67. eda_trajectory = loadModel(out_file + '.eda.npz')
  68. else:
  69. eda_trajectory = EDA(combined_dcd[0])
  70. eda_trajectory.buildCovariance(trajectory, aligned=True)
  71. eda_trajectory.calcModes(n_modes=10)
  72. saveModel(eda_trajectory, filename=out_file)
  73. for mode in eda_trajectory[:4]:
  74. print(calcFractVariance(mode))
  75. # write VMD files
  76. writeNMD(out_file, eda_trajectory[:10], structure_select)
  77. # create EDA rmsf figures
  78. def my_showSqFlucts(modes, *args, **kwargs):
  79. """Show square fluctuations using :func:`~matplotlib.pyplot.plot`. See
  80. also :func:`.calcSqFlucts`."""
  81. import matplotlib.pyplot as plt
  82. sqf = calcSqFlucts(modes)
  83. if not 'label' in kwargs:
  84. kwargs['label'] = str(modes)
  85. show = plt.plot(res_nums,sqf, *args, **kwargs)
  86. plt.xlabel('Residue #')
  87. plt.ylabel('Square fluctuations')
  88. plt.xlim([first_res,last_res])
  89. plt.title(str(modes))
  90. return show
  91. for i in range(0, 4):
  92. j = i + 1
  93. plt.figure(figsize=(8, 6))
  94. my_showSqFlucts(eda_trajectory[i])
  95. title('RMSF (EDA Mode ' + str(j) + ')')
  96. plt.savefig(out_file + '_rmsf_mode_' + str(j) + '.png', dpi=300, format='png')
  97. plt.close('all[')
  98. # create cross-corr figure
  99. def my_showCrossCorr(modes, *args, **kwargs):
  100. import matplotlib.pyplot as plt
  101. arange = np.arange(first_res - 1 ,last_res)
  102. cross_correlations = np.zeros((arange[-1]+2, arange[-1]+2))
  103. cross_correlations[arange[0]+1:,
  104. arange[0]+1:] = calcCrossCorr(modes)
  105. kwargs['interpolation'] = 'bilinear'
  106. kwargs['origin'] = 'lower'
  107. show = plt.imshow(cross_correlations, *args, **kwargs), plt.colorbar()
  108. plt.axis([arange[0]+0.5, arange[-1]+1.5, arange[0]+0.5, arange[-1]+1.5])
  109. plt.title('Cross-correlations for {0}'.format(str(modes)))
  110. plt.xlabel('Residue')
  111. plt.ylabel('Residue')
  112. return show
  113. plt.figure(figsize=(8, 6))
  114. my_showCrossCorr(eda_trajectory)
  115. title('Cross-correlation matrix')
  116. plt.savefig(out_file + '_corr.png', dpi=300, format='png')
  117. plt.close('all')
  118. # create projection figures
  119. plt.figure(figsize=(8, 6))
  120. showProjection(trajectory[:frame_incr], eda_trajectory[0,1], color='red', marker='.', label=systems[0])
  121. showProjection(trajectory[frame_incr + 1:frame_incr * 2 + 1], eda_trajectory[0,1], color='green', marker='.', label=systems[1]);
  122. legend(markerscale=2, numpoints=1, handletextpad=0.25)
  123. title('EDA')
  124. plt.savefig(out_file + '_proj1_2.png', dpi=300, format='png')
  125. plt.close('all')
  126. plt.figure(figsize=(8, 6))
  127. showProjection(trajectory[:frame_incr], eda_trajectory[2,3], color='red', marker='.', label=systems[0])
  128. showProjection(trajectory[frame_incr + 1:frame_incr * 2 + 1], eda_trajectory[2,3], color='green', marker='.', label=systems[1]);
  129. legend(markerscale=2, numpoints=1, handletextpad=0.25)
  130. title('EDA')
  131. plt.savefig(out_file + '_proj3_4.png', dpi=300, format='png')
  132. plt.close('all')