LOOS  v2.3.2
ensembles.py
1 """
2 PyLOOS functions applicable to ensembles (trajectories or trajectory-like collections)
3 
4 """
5 import loos
6 import numpy
7 import numpy.linalg
8 
9 
10 ## Python version of averageStructure (using loos.pyloos.Trajectory-like objects)
11 # The subset defined in the trajectory controls what is averaged over. The trajectory
12 # may actually be a VirtualTrajectory or an AlignedVirtualTrajectory.
13 
14 def averageStructure(traj):
15  """Returns the average structure for a trajectory.
16  >>> avg = loos.pyloos.averageStructure(traj)
17  """
18  avg = numpy.zeros((len(traj.frame()), 3))
19  for frame in traj:
20  coords = frame.getCoords()
21  avg += coords
22  avg /= len(traj)
23 
24  structure = traj.frame().copy()
25  for i in range(len(structure)):
26  structure[i].coords(loos.GCoord(avg[i][0], avg[i][1], avg[i][2]))
27 
28  return(structure)
29 
30 ## Returns the coordinates for an entire trajectory as an MxN numpy matrix
31 # where M is 3*natoms and N is the length of the trajectory. The subset
32 # in the trajectory controls what is extracted. The trajectory
33 # may actually be a VirtualTrajectory or an AlignedVirtualTrajectory.
34 
35 def extractCoords(traj):
36  """
37  Extracts coords from a trajectory as a NumPy matrix
38  >>> A = loos.pyloos.extractCoords(traj)
39  """
40  m = len(traj.frame()) * 3
41  n = len(traj)
42 
43  A = numpy.zeros((m, n))
44  for i in range(n):
45  coords = traj[i].getCoords()
46  A[:,i] = numpy.reshape(coords, (1,m))
47  return(A)
48 
49 
50 
51 ## Returns a tuple containing the SVD result for a trajectory, along with the average structure.
52 # The tuple is,
53 # <tt>(left-singular-vectors, singular-values, right-singular-vectors, average)</tt>
54 #
55 # The subset set in the trajectory controls what is used for the SVD. The
56 # trajectory may be a VirtualTrajectory or an AlignedVirtualTrajectory.
57 # The following example computes an SVD using backbone atoms for a trajectory
58 # that has been iteratively aligned using C-alphas
59 # \code
60 #traj = loos.pyloos.Trajectory('foo.dcd', model, subset = 'backbone')
61 #aligned = loos.pyloos.AlignedVirtualTrajectory(traj, alignwith = 'name == "CA"')
62 #(L,S,V,avg) = loos.pyloos.svd(aligned)
63 # \endcode
64 
65 
66 def svd(traj):
67  """
68  Returns a tuple containing SVD results along with the average structure for a trajectory
69  >>> (L,S,V,avg) = loos.pyloos.svd(traj)
70  """
71  A = extractCoords(traj)
72  avg = numpy.average(A, 1)
73  avg = avg[:,numpy.newaxis]
74  A -= avg
75  (U,s,V) = numpy.linalg.svd(A)
76 
77  structure = traj.frame().copy()
78  for i in range(len(structure)):
79  structure[i].coords(loos.GCoord(avg[i*3], avg[i*3+1], avg[i*3+2]))
80 
81  return(U,s,V,structure)
def averageStructure(traj)
Python version of averageStructure (using loos.pyloos.Trajectory-like objects) The subset defined in ...
Definition: ensembles.py:14
def svd(traj)
Returns a tuple containing the SVD result for a trajectory, along with the average structure...
Definition: ensembles.py:66
def extractCoords(traj)
Returns the coordinates for an entire trajectory as an MxN numpy matrix where M is 3*natoms and N is ...
Definition: ensembles.py:35