OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
trajectory_analysis.py
Go to the documentation of this file.
1 """
2 Some functions for analyzing trajectories
3 
4 Author: Niklaus Johner
5 """
6 
7 import ost.mol.alg
8 import ost.geom
9 from ost import LogError
10 import os
11 
12 def smooth(vec,n):
13 #Function to smooth a vector or a list of floats
14 #for each element it takes the average over itself and the
15 #n elements on each side, so over (2n+1) elements
16  try:
17  vec2=vec.copy()
18  except:
19  vec2=vec[:]
20  for i in range(n):
21  v=0.0
22  count=1.0
23  v+=vec[i]
24  for j in range(n):
25  count+=1
26  v+=vec[i+j+1]
27  for j in range(i):
28  count+=1
29  v+=vec[i-(j+1)]
30  vec2[i]=v/float(count)
31  for i in range(1,n+1):
32  v=0.0
33  count=1.0
34  v+=vec[-i]
35  for j in range(n):
36  count+=1
37  v+=vec[-(i+j+1)]
38  for j in range(i-1):
39  count+=1
40  v+=vec[-i+j+1]
41  vec2[-i]=v/float(count)
42  for i in range(n,len(vec2)-n):
43  v=vec[i]
44  for j in range(n):
45  v+=vec[i+j+1]
46  v+=vec[i-j-1]
47  vec2[i]=v/float(2.*n+1.)
48  return vec2
49 
50 
51 """
52 From here on the module needs numpy
53 """
54 
55 def RMSD_Matrix_From_Traj(t,sele,first=0,last=-1):
56  """
57  This function calculates a matrix M such that M[i,j] is the
58  RMSD of the EntityView sele between frames i and j of the trajectory t
59  aligned on sele.
60  Its inputs are:
61  t : the trajectory (CoordGroupHandle)
62  sele : the EntityView used for alignment and RMSD calculation
63  first=0 : the first frame of t to be used
64  last=-1 : the last frame of t to be used
65  Returns a numpy NxN matrix, where n is the number of frames.
66  """
67  try:
68  import numpy as npy
69  if last==-1:last=t.GetFrameCount()
70  n_frames=last-first
71  rmsd_matrix=npy.identity(n_frames)
72  for i in range(n_frames):
73  t=ost.mol.alg.SuperposeFrames(t,sele,begin=first,end=last,ref=i)
74  eh=t.GetEntity()
75  t.CopyFrame(i)
76  rmsd_matrix[i,:]=ost.mol.alg.AnalyzeRMSD(t,sele,sele)
77  if i==0:
78  last=last-first
79  first=0
80  return rmsd_matrix
81  except ImportError:
82  LogError("Function needs numpy, but I could not import it.")
83  raise
84 
85 
86 def PairwiseDistancesFromTraj(t,sele,first=0,last=-1,seq_sep=1):
87  """
88  This function calculates the distances between any pair of atoms in the
89  EntityView sele with sequence separation larger than seq_sep from a trajectory t.
90  It return a matrix containing one line for each atom pair and N columns, where
91  N is the length of the trajectory.
92  Its inputs are:
93  t : the trajectory (CoordGroupHandle)
94  sele : the EntityView used to determine the atom pairs
95  first=0 : the first frame of t to be used
96  last=-1 : the last frame of t to be used
97  seq_sep=1 : The minimal sequence separation between
98  Returns a numpy NpairsxNframes matrix.
99  """
100  try:
101  import numpy as npy
102  if last==-1:last=t.GetFrameCount()
103  n_frames=last-first
104  n_var=0
105  for i,a1 in enumerate(sele.atoms):
106  for j,a2 in enumerate(sele.atoms):
107  if not j-i<seq_sep:n_var+=1
108  #n_var=sele.GetAtomCount()
109  #n_var=(n_var-1)*(n_var)/2.
110  dist_matrix=npy.zeros(n_frames*n_var)
111  dist_matrix=dist_matrix.reshape(n_var,n_frames)
112  k=0
113  for i,a1 in enumerate(sele.atoms):
114  for j,a2 in enumerate(sele.atoms):
115  if j-i<seq_sep:continue
116  dist_matrix[k]=ost.mol.alg.AnalyzeDistanceBetwAtoms(t,a1.GetHandle(),a2.GetHandle())[first:last]
117  k+=1
118  return dist_matrix
119  except ImportError:
120  LogError("Function needs numpy, but I could not import it.")
121  raise
122 
124  """
125  This function calculates an distance matrix M(NxN) from
126  the pairwise distances matrix D(MxN), where N is the number
127  of frames in the trajectory and M the number of atom pairs.
128  M[i,j] is the distance between frame i and frame j
129  calculated as a p-norm of the differences in distances
130  from the two frames (distance-RMSD for p=2).
131  Inputs:
132  distances : a pairwise distance matrix as obtained from PairwiseDistancesFromTraj()
133  Returns a numpy NxN matrix, where N is the number of frames.
134  """
135  try:
136  import numpy as npy
137  n1=distances.shape[0]
138  n2=distances.shape[1]
139  dist_mat=npy.identity(n2)
140  for i in range(n2):
141  for j in range(n2):
142  if j<=i:continue
143  d=(((abs(distances[:,i]-distances[:,j])**p).sum())/float(n1))**(1./p)
144  dist_mat[i,j]=d
145  dist_mat[j,i]=d
146  return dist_mat
147  except ImportError:
148  LogError("Function needs numpy, but I could not import it.")
149  raise
150 
151 def DistRMSDFromTraj(t,sele,ref_sele,radius=7.0,average=False,seq_sep=4,first=0,last=-1):
152  """
153  This function calculates the distance RMSD from a trajectory.
154  The distances selected for the calculation are all the distances
155  between pair of atoms that from residues that are at least seq_sep apart
156  in the sequence and that are smaller than radius in ref_sel.
157  The number and order of atoms in ref_sele and sele should be the same.
158  Its inputs are:
159  t : the trajectory (CoordGroupHandle)
160  sele : the EntityView used to determine the distances from t
161  radius=7 : the upper limit of distances in ref_sele considered for the calculation
162  seq_sep=4 : The minimal sequence separation between atom pairs considered for the calculation
163  average=false : use the average distance in the trajectory as reference instead of the distance obtained from ref_sele
164  first=0 : the first frame of t to be used
165  last=-1 : the last frame of t to be used
166  Returns a numpy vecor dist_rmsd(Nframes).
167  """
168  if not sele.GetAtomCount()==ref_sele.GetAtomCount():
169  print 'Not same number of atoms in the two views'
170  return
171  try:
172  import numpy as npy
173  if last==-1:last=t.GetFrameCount()
174  n_frames=last-first
175  dist_rmsd=npy.zeros(n_frames)
176  pair_count=0.0
177  for i,a1 in enumerate(ref_sele.atoms):
178  for j,a2 in enumerate(ref_sele.atoms):
179  if j<=i:continue
180  r1=a1.GetResidue()
181  c1=r1.GetChain()
182  r2=a2.GetResidue()
183  c2=r2.GetChain()
184  if c1==c2 and abs(r2.GetNumber().num-r1.GetNumber().num)<seq_sep:continue
185  d=ost.geom.Distance(a1.pos,a2.pos)
186  if d<radius:
187  a3=sele.atoms[i]
188  a4=sele.atoms[j]
189  d_traj=ost.mol.alg.AnalyzeDistanceBetwAtoms(t,a3.GetHandle(),a4.GetHandle())[first:last]
190  if average:d=npy.mean(d_traj)
191  for k,el in enumerate(d_traj):
192  dist_rmsd[k]+=(el-d)**2.0
193  pair_count+=1.0
194  return (dist_rmsd/float(pair_count))**0.5
195  except ImportError:
196  LogError("Function needs numpy, but I could not import it.")
197  raise
198 
199