0% found this document useful (0 votes)
57 views

Import Numpy As NP

The document contains Python code for analyzing molecular dynamics simulation data. It defines several functions for: - Loading and preprocessing RMSF and RMSD data files - Plotting RMSF profiles of protein domains/regions over time - Analyzing MM-PBSA energy components across complexes - Creating 3D scatter plots of residue RMSF over simulation time The functions read data files, perform calculations, and generate plots to analyze protein flexibility and binding free energies over the course of MD simulations.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Import Numpy As NP

The document contains Python code for analyzing molecular dynamics simulation data. It defines several functions for: - Loading and preprocessing RMSF and RMSD data files - Plotting RMSF profiles of protein domains/regions over time - Analyzing MM-PBSA energy components across complexes - Creating 3D scatter plots of residue RMSF over simulation time The functions read data files, perform calculations, and generate plots to analyze protein flexibility and binding free energies over the course of MD simulations.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

import numpy as np

import matplotlib.pyplot as plt


import matplotlib.axes as ax
import os
import pandas as pd
from matplotlib import font_manager
from mpl_toolkits.mplot3d import axes3d

font_dirs = ['palantino/']
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
for font_file in font_files:
font_manager.fontManager.addfont(font_file)

# set font
plt.rcParams['font.family'] = 'Palatino Linotype'
def rmsf_fab_heavy():
fig = plt.figure(dpi=300,figsize=(8.667,6.667))
color = iter(['#e41a1c','#377eb8','#4daf4a','#984ea3'])
label = iter(['Complex 1', 'Complex 2', 'Complex 3', 'Complex 4'])
for file in sorted(os.listdir('rmsf')):
if "_ab.xvg" in file:
complex = file.replace("_ab.xvg","")
c = next(color)
file = "rmsf/"+file
x,y = np.loadtxt(file,comments=['#',"@"],unpack=True)
x=x[0:226]
y=y[0:226]
plt.plot(x,y,c=c,label=next(label),linewidth=1,alpha=0.8)
plt.title('a)')
plt.xlabel('Residue')
plt.ylabel('RMSF (nm)')
plt.xticks(np.arange(0, 228, 20))
plt.minorticks_on()
plt.xlim([-5, 235])
plt.ylim([0, 1.25])
plt.legend(loc="upper left")
plt.savefig('fab_heavy.png')
plt.show()

def rmsf_fab_light():
fig = plt.figure(dpi=300,figsize=(8.667,6.667))
color = iter(['#e41a1c','#377eb8','#4daf4a','#984ea3'])
label = iter(['Complex 1', 'Complex 2', 'Complex 3', 'Complex 4'])
for file in sorted(os.listdir('rmsf')):
if "_ab.xvg" in file:
complex = file.replace("_ab.xvg","")
c = next(color)
file = "rmsf/"+file
x,y = np.loadtxt(file,comments=['#',"@"],unpack=True)
x=x[226:]
y=y[226:]
plt.plot(x,y,c=c,label=next(label),linewidth=1)
plt.title('b)')
plt.xlabel('Residue')
plt.ylabel('RMSF (nm)')
plt.xticks(np.arange(0, 228, 20))
plt.minorticks_on()
plt.xlim([-5, 235])
plt.ylim([0, 1.25])
plt.legend(loc="upper left")
plt.savefig('fab_light.png')
plt.show()

def rmsf_gp1():
fig = plt.figure(dpi=300,figsize=(8.667,6.667))
color = iter(['#e41a1c','#377eb8','#4daf4a','#984ea3'])
label = iter(['Complex 1', 'Complex 2', 'Complex 3', 'Complex 4'])
last = iter([280,279,278,278])
for file in sorted(os.listdir('rmsf')):
if "gp.xvg" in file:
complex = file.replace("_gp.xvg","")
file = "rmsf/"+file
x,y = np.loadtxt(file,comments=['#',"@"],unpack=True)
index=next(last)
x=x[0:index]
y=y[0:index]
plt.plot(x,y,c=next(color),label=next(label),linewidth=None)
plt.title('c)')
plt.xlabel('Residue')
plt.ylabel('RMSF (nm)')
plt.xticks(np.arange(0, 310, 20))
plt.minorticks_on()
plt.xlim([25, 320])
plt.ylim([0, 1.25])

#base = pd.DataFrame(data=np.array([[33,70],[95,105],[158,168],
[176,190]]),columns=['start','end'])
#head = pd.DataFrame(data=np.array([[70,95],[105,158],[168,176],
[214,227]]),columns=['start','end'])

#for ind,row in base.iterrows():


# plt.axvspan(row['start'],row['end'],alpha=0.3, color='#4e38c9', label="_"*ind
+'GP1 base')
#for ind,row in head.iterrows():
# plt.axvspan(row['start'],row['end'],alpha=0.3, color='#a11cb8', label="_"*ind
+'GP1 head')
#plt.axvspan(227,313,alpha=0.3,color='#f60081',label='GP1 glycan cap')
#plt.axvspan(,alpha=)

plt.legend(loc="upper left")
plt.savefig('gp1.png')
plt.show()

def rmsf_gp2():
fig = plt.figure(dpi=300,figsize=(8.667,6.667))
color = iter(['#e41a1c','#377eb8','#4daf4a','#984ea3'])
label = iter(['Complex 1', 'Complex 2', 'Complex 3', 'Complex 4'])
last = iter([280,279,278,278])
for file in sorted(os.listdir('rmsf')):
if "gp.xvg" in file:
complex = file.replace("_gp.xvg","")
file = "rmsf/"+file
x,y = np.loadtxt(file,comments=['#',"@"],unpack=True)
index=next(last)
x=x[index:]
y=y[index:]
plt.plot(x,y,c=next(color),label=next(label),linewidth=None)
plt.title('d)')
plt.xlabel('Residue')
plt.ylabel('RMSF (nm)')
plt.xticks(np.arange(500, 600, 20))
plt.minorticks_on()
plt.xlim([490, 610])
plt.ylim([0, 1.25])

#base = pd.DataFrame(data=np.array([[33,70],[95,105],[158,168],
[176,190]]),columns=['start','end'])
#head = pd.DataFrame(data=np.array([[70,95],[105,158],[168,176],
[214,227]]),columns=['start','end'])

#for ind,row in base.iterrows():


# plt.axvspan(row['start'],row['end'],alpha=0.3, color='#4e38c9', label="_"*ind
+'GP1 base')
#for ind,row in head.iterrows():
# plt.axvspan(row['start'],row['end'],alpha=0.3, color='#a11cb8', label="_"*ind
+'GP1 head')
#plt.axvspan(227,313,alpha=0.3,color='#f60081',label='GP1 glycan cap')
#plt.axvspan(,alpha=)

plt.legend(loc="upper left")
plt.savefig('gp2.png')
plt.show()

def rmsd():
labels = iter(['Complex 1', 'Complex 2', 'Complex 3', 'Complex 4'])
color = iter(['#e41a1c','#377eb8','#4daf4a','#984ea3'])
output_name=iter(['rmsd_complex1.png', 'rmsd_complex2.png', 'rmsd_complex3.png',
'rmsd_complex4.png'])
points=iter(['a)','b)','c)','d)'])
for file in sorted(os.listdir('rmsd')):
fig = plt.figure(dpi=300,figsize=(8.667,6.667))
file = "rmsd/"+file
x1,y1=np.loadtxt(file,comments=['#',"@"],unpack=True)
c=next(color)
plt.plot(x1,y1,c=c,linewidth=1,alpha=0.8,label=next(labels))
plt.title(next(points))
plt.xlabel('Time (ns)')
plt.ylabel('RMSD (nm)')
#plt.xticks(np.arange(0, 228, 20))
plt.minorticks_on()
#plt.xlim([-5, 235])
plt.ylim([0, 1.0])
plt.legend(loc="upper left")
plt.savefig(next(output_name))

def rmsd_all():
labels = iter(['Complex 1', 'Complex 2', 'Complex 3', 'Complex 4'])
color = iter(['#e41a1c','#377eb8','#4daf4a','#984ea3'])
fig = plt.figure(dpi=300,figsize=(8.667,6.667))
for file in sorted(os.listdir('rmsd')):
file = "rmsd/"+file
x1,y1=np.loadtxt(file,comments=['#',"@"],unpack=True)
c=next(color)
plt.plot(x1,y1,c=c,linewidth=1,alpha=0.8,label=next(labels))
plt.xlabel('Time (ns)')
plt.ylabel('RMSD (nm)')
#plt.xticks(np.arange(0, 228, 20))
plt.minorticks_on()
#plt.xlim([-5, 235])
plt.ylim([0, 1.0])
plt.legend(loc="upper left")
plt.savefig('rmsd_all.png')

def mmpbsa():
color = iter(['#e41a1c','#377eb8','#4daf4a','#984ea3'])
label = iter(['Complex 1', 'Complex 2', 'Complex 3', 'Complex 4'])
mmpbsa = pd.DataFrame(data={'Energy_Component':
['dVDWAALS','dEEL','dEPB','dENPOLAR','dTOTAL']})
#['van der Waals ','Electrostatic ','Polar Solvation','Non-polar ','Total']})
for file in sorted(os.listdir('mmpbsa')):
df=pd.read_csv('mmpbsa/'+file)
df=df[['Energy_Component','Average']]
df=df.iloc[[6,7,10,11,15]]
lab=next(label)
df=df.rename(columns={'Average':lab})
mmpbsa=mmpbsa.merge(df,how='inner', on='Energy_Component')
mmpbsa.plot.bar(rot=0)
print(mmpbsa)
fig = plt.figure(dpi=300,figsize=(8.667,6.667))
plt.bar(mmpbsa['Energy_Component'],mmpbsa['Complex 1'])
plt.show()
plt.savefig('mmpbsa1.png')

def count_files(dirpath):
frames=0
for file in os.listdir(dirpath):
if os.path.isfile(os.path.join(dirpath, file)):
frames += 1
return frames #Ile klatek otrzymamy - Z

def count_res(dirpath, first_res=None, last_res=None):


#counts number of residues to analyze
list_of_resn=[]
for file in os.listdir(dirpath):
file = os.path.join(dirpath,file)
if os.path.isfile(file):
res,rmsf=np.loadtxt(file,comments=['#',"@"],unpack=True)
res=res[first_res_value:last_res_value]
rmsf=rmsf[first_res_value:last_res_value]
list_of_resn.append(res)
for item in list_of_resn[1:]:
if item != list_of_resn[0]:
return False
return len(res),res #NUMBER OF RESIDUES TO ANALYZE - X
def create_array(dirpath, x, first_res=None, last_res=None):
array=np.zeros((1,x))
lista=[]
for file in sorted(os.listdir(dirpath)):
file = os.path.join(dirpath,file)
if os.path.isfile(file):
res,rmsf=np.loadtxt(file,comments=['#',"@"],unpack=True)
res=res[first_res_value:last_res_value]
rmsf=rmsf[first_res_value:last_res_value]
rmsf=np.reshape(rmsf, (1,x))
array = np.append(array,rmsf,axis=0)
array=array[1:,:]
return array

dirpath=input('podaj dirpath: ')

first_res=input('podaj first res: ')


if first_res.isdigit():
first_res=int(first_res)
else:
first_res = None

last_res=input('podaj last res: ')


if last_res.isdigit():
last_res = int(last_res)
else:
last_res = None

if last_res == None and first_res == None:


last_res_value = last_res
first_res_value = first_res
elif last_res == None and first_res !=None:
first_res_value=first_res-1
last_res_value = last_res
elif last_res != None and first_res == None:
first_res_value = first_res
last_res_value = last_res
elif last_res != None and first_res != None:
first_res_value = first_res-1
last_res_value = last_res

x=count_res(dirpath,first_res=first_res_value, last_res=last_res_value)
result=create_array(dirpath,x[0],first_res=first_res_value,last_res=last_res_value)
print(result.shape)
print(result)
print(result[0][0])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

res=x[1]
print(res)
time=count_files(dirpath)
time=time+1
time = [ns for ns in np.arange(1,time,1)]
print(time)
for ns in time:
rmsf_frame=ns-1
rmsf=result[:][rmsf_frame]
ax.scatter(res, ns, rmsf, c=rmsf, alpha=1)
ax.set_xlabel('Residue')
ax.set_ylabel('Time (ns)')
ax.set_zlabel('RMSF (nm)')
ax.set_xticks(np.arange(first_res_value,last_res_value,1))
ax.set_xticks(np.arange)
ax.set_zticks(np.arange(-0.4,0.4,0.05))
ax.set_xticklabels([int(i) for i in x[1]])
ax.set_zlim(-0.5,0.5)
plt.show()

You might also like