Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
coordlib.py
Go to the documentation of this file.
1 import math
2 import sys
3 
4 def toDeg(ang):
5  """toDeg(ang) - converts an angle in radians to an angle in degrees"""
6  return ang / math.pi * 180.0
7 
8 def toRad(ang):
9  """toRad(ang) - converts an angle in degrees to an angle in radians"""
10  return ang / 180.0 * math.pi
11 
12 def dist_sq(atom1, atom2):
13  return (atom2[0]-atom1[0])**2 + (atom2[1]-atom1[1])**2 + \
14  (atom2[2]-atom1[2])**2
15 
16 def dist(atom1, atom2):
17  #Given the positions of two atoms, returns the distance between them dist.
18  return (dist_sq(atom1, atom2))**0.5
19 
20 def coord_angle(atom1, atom2, atom3):
21  """
22  A = [xA, yA, zA], and B and C are defined in the same fashion. Given
23  these 3 coordinates, returns the angle between the AB and BC bond vectors.
24  """
25  BA = makeVector(atom2, atom1)
26  BC = makeVector(atom2, atom3)
27  return toDeg(vangle(BA,BC))
28 
29 def torsion(A,B,C,D):
30  #calculates the B-C torsion angle of any four atoms A,B,C, and D
31  AB = makeVector(A,B)
32  BC = makeVector(B,C)
33  CD = makeVector(C,D)
34  phistp = stp(AB,BC,CD)
35  norm1 = cross(AB,BC)
36  norm2 = cross(BC,CD)
37  phi = toDeg(vangle(norm1, norm2))
38  if phistp >= 0:
39  return phi
40  else:
41  return -phi
42 
43 def makeVector(atom1, atom2):
44  """vector(atom1, atom2) - return the mathematical vector from A to B
45  Given two points, A and B, this function returns the vector going
46  from point A to point B, represented as a list of cartesian
47  components. A and be must be lists of coordinates in x, y, and
48  z. That is:
49 
50  vector([x1, y1, z1], [x2, y2, z2]) ==> [x2-x1, y2-y1, z2-z1]
51  """
52  return [atom2[0]-atom1[0], atom2[1]-atom1[1], atom2[2]-atom1[2]]
53 
54 def vabs(vec):
55  """vabs(vec) - return the absolute value of a given vector
56  The absolute value of a three-dimensional vector from point A to
57  point B is defined as the distance between points A and B. This
58  function evaluates that value for a vector as defined above.
59  """
60  return math.sqrt(vec[0]**2 + vec[1]**2 + vec[2]**2)
61 
62 def vunit(vec):
63  #returns the unit vector corresponding to vec
64  return vmult(vec, 1/vabs(vec))
65 
66 def vmult(vec, n):
67  #[x,y,z] -> [n*x, n* y, n*z]
68  v2 = []
69  for item in vec:
70  v2.append(n*item)
71  return v2
72 
73 def vadd(v1, v2):
74  #[x1, y1, z1] + [x2, y2, z2] = [x1 + x2, y1 + y2, z1 + z2]
75  v3 = []
76  for i in range(0, len(v1)):
77  v3.append(v1[i] + v2[i])
78  return v3
79 
80 def vavg(vlist):
81  #averages two vectors or lists
82  if vlist == []:
83  print 'error: Cannot average an empty list of objects'
84  sys.exit(1)
85  sum=vlist[0]
86  for i in range(1, len(vlist)):
87  sum=vadd(sum,vlist[i])
88  return vmult(sum, 1.0/len(vlist))
89 
90 def rss(v1,v2):
91  """
92  gets the root sum of squares for two vectors (the distance
93  between)
94  """
95  s_sq=0
96  for i in range(0, len(v1)):
97  s_sq=s_sq+(v1[i]-v2[i])**2
98  return math.sqrt(s_sq)
99 
100 def dot(v1, v2):
101  """dot(v1, v2) - return the inner (dot) product of two vectors
102  This function evaluates the dot product between two vectors, defined
103  as the following:
104 
105  dot([x1, y1, z1], [x2, y2, z2]) ==> x1*x2 + y1*y2 + z1*z2
106 
107  It can be shown that this is also equal to:
108 
109  dot(v1, v2) ==> |v1| * |v2| * cos(theta)
110 
111  Where theta is the angle between the vectors.
112  """
113  return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]
114 
115 def cross(v1, v2):
116  """cross(v1, v2) - returns the cross product of two vectors
117  This function evaluates the cross product between two cartesian
118  vectors in 3d. For v1 = [x1, y1, z1], v2 = [x2, y2, z2], and
119  cartesian unit vectors i, j, and k, this is defined as the
120  determinant of the following matrix:
121 
122  | i j k |
123  | x1 y1 z1 |
124  | x2 y2 z2 |
125 
126  More simply,
127 
128  cross([x1, y1, z1], [x2, y2, z2]) ==>
129  [y1*z2 - z1*y2, z1*x2 - x1*z2, x1*y2 - y1*x2]
130 
131  It can also be shown that
132 
133 -- |cross(v1, v2)| == |v1| * |v2| * sin(theta)
134 
135  Where theta is the angle between the two vectors.
136  """
137  return [v1[1]*v2[2] - v1[2]*v2[1],
138  v1[2]*v2[0] - v1[0]*v2[2],
139  v1[0]*v2[1] - v1[1]*v2[0]]
140 
141 def stp(v1, v2, v3):
142  """stp(v1, v2, v3) - return the scalar triple product of three vectors
143  This function will return the scalar triple product of three vectors,
144  defined most simply as dot(v3, cross(v1, v2)). It can be shown that
145  |stp(v1, v2, v3)| is the volume of the parallelpiped with sides
146  defined by the vectors. It follows that the following relationships
147  apply:
148 
149  stp(v1, v2, v3) == stp(v2, v3, v1) == stp(v3, v1, v2)
150  """
151  return dot(v3, cross(v1, v2))
152 
153 def vangle(v1, v2):
154  """vangle(v1, v2) - returns the angle (in radians) between two vectors
155  If v1 and v2 are two vectors, this function uses the cosine
156  relationship given above (in dot) to determine the angle between
157  those vectors when one vector is projected on the plane of the other.
158  Note that this value can only be the range of the arccos function,
159  so theta will be in the range 0 to Pi.
160  """
161  v1abs = vabs(v1)
162  v2abs = vabs(v2)
163  if v1abs == 0:
164  return 0
165  elif v2abs == 0:
166  return 0
167  else:
168  vcos = dot(v1, v2) / vabs(v1) / vabs(v2)
169  #Dealing with cases where the cosine is very close to -1 or 1
170  if vcos >= 1.0: return 0.0
171  elif vcos <= -1.0: return math.pi
172  else:
173  return math.acos(vcos)
174 
def stp
Definition: coordlib.py:141
def cross
Definition: coordlib.py:115
def vavg
Definition: coordlib.py:80
def toRad
Definition: coordlib.py:8
def torsion
Definition: coordlib.py:29
def vmult
Definition: coordlib.py:66
def rss
Definition: coordlib.py:90
Fstring::size_type len(Fstring const &s)
Length.
Definition: Fstring.hh:2207
def vadd
Definition: coordlib.py:73
def toDeg
Definition: coordlib.py:4
def dot
Definition: coordlib.py:100
def dist_sq
Definition: coordlib.py:12
def vabs
Definition: coordlib.py:54
def coord_angle
Definition: coordlib.py:20
def makeVector
Definition: coordlib.py:43
def dist
Definition: coordlib.py:16
def vangle
Definition: coordlib.py:153
def vunit
Definition: coordlib.py:62