Module: Bio::PDB::Utils

Included in:
Bio::PDB, Chain, Model, Record::ATOM, Residue
Defined in:
lib/bio/db/pdb/utils.rb

Overview

Utility methods for PDB data. The methods in this mixin should be applicalbe to all PDB objects.

Bio::PDB::Utils is included by Bio::PDB, Bio::PDB::Model, Bio::PDB::Chain, Bio::PDB::Residue, and Bio::PDB::Heterogen classes.

Constant Summary

ElementMass =

Returns the coords of the centre of gravity for any AtomFinder implementing object Blleurgh! - working out what element it is from the atom name is tricky - this'll work in most cases but not metals etc… a proper element field is included in some PDB files but not all.

{
  'H' => 1,
  'C' => 12,
  'N' => 14,
  'O' => 16,
  'S' => 32,
  'P' => 31
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.acos(x) ⇒ Object

acos



168
169
170
# File 'lib/bio/db/pdb/utils.rb', line 168

def acos(x)
  Math.atan2(Math.sqrt(1 - x**2),x)
end

.calculatePlane(coord1, coord2, coord3) ⇒ Object

calculates plane



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/bio/db/pdb/utils.rb', line 174

def calculatePlane(coord1, coord2, coord3)
  a = coord1.y * (coord2.z - coord3.z) +
      coord2.y * (coord3.z - coord1.z) + 
      coord3.y * (coord1.z - coord2.z)
  b = coord1.z * (coord2.x - coord3.x) +
      coord2.z * (coord3.x - coord1.x) + 
      coord3.z * (coord1.x - coord2.x)
  c = coord1.x * (coord2.y - coord3.y) +
      coord2.x * (coord3.y - coord1.y) + 
      coord3.x * (coord1.y - coord2.y)
  d = -1 *
      (
       (coord1.x * (coord2.y * coord3.z - coord3.y * coord2.z)) +
       (coord2.x * (coord3.y * coord1.z - coord1.y * coord3.z)) +
       (coord3.x * (coord1.y * coord2.z - coord2.y * coord1.z))
       )

  return [a,b,c,d]
end

.convert_to_xyz(obj) ⇒ Object

Implicit conversion into Vector or Bio::PDB::Coordinate



139
140
141
142
143
144
145
146
147
148
# File 'lib/bio/db/pdb/utils.rb', line 139

def convert_to_xyz(obj)
  unless obj.is_a?(Vector)
    begin
      obj = obj.xyz
    rescue NameError
      obj = Vector.elements(obj.to_a)
    end
  end
  obj
end

.dihedral_angle(coord1, coord2, coord3, coord4) ⇒ Object

Calculates dihedral angle.



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/bio/db/pdb/utils.rb', line 124

def dihedral_angle(coord1, coord2, coord3, coord4)
  (a1,b1,c1,d) = calculatePlane(coord1,coord2,coord3)
  (a2,b2,c2)   = calculatePlane(coord2,coord3,coord4)
  
  torsion = acos((a1*a2 + b1*b2 + c1*c2)/(Math.sqrt(a1**2 + b1**2 + c1**2) * Math.sqrt(a2**2 + b2**2 + c2**2)))
  
  if ((a1*coord4.x + b1*coord4.y + c1*coord4.z + d) < 0)
    -torsion
  else
    torsion
  end
end

.distance(coord1, coord2) ⇒ Object

Calculates distance between coord1 and coord2.



116
117
118
119
120
# File 'lib/bio/db/pdb/utils.rb', line 116

def distance(coord1, coord2)
  coord1 = convert_to_xyz(coord1)
  coord2 = convert_to_xyz(coord2)
  (coord1 - coord2).r
end

.rad2deg(r) ⇒ Object

radian to degree



162
163
164
# File 'lib/bio/db/pdb/utils.rb', line 162

def rad2deg(r)
  (r/Math::PI)*180
end

.to_xyz(obj) ⇒ Object

(Deprecated) alias of convert_to_xyz(obj)



152
153
154
# File 'lib/bio/db/pdb/utils.rb', line 152

def self.to_xyz(obj)
  convert_to_xyz(obj)
end

Instance Method Details

#centreOfGravityObject

calculates centre of gravitiy



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bio/db/pdb/utils.rb', line 91

def centreOfGravity()
  x = y = z = total = 0
  
  self.each_atom{ |atom|
    element = atom.element[0,1]
    mass    = ElementMass[element]
    total += mass
    x += atom.x * mass
    y += atom.y * mass
    z += atom.z * mass
  }
  
  x = x / total
  y = y / total
  z = z / total
  
  Coordinate[x,y,z]
end

#finder(findtype, &block) ⇒ Object

Every class in the heirarchy implements finder, this takes a class which determines which type of object to find, the associated block is then run in classic .find style.

The method might be deprecated. You'd better using find_XXX directly.



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/bio/db/pdb/utils.rb', line 201

def finder(findtype, &block) #:yields: obj
  if findtype == Bio::PDB::Atom
    return self.find_atom(&block)
  elsif findtype == Bio::PDB::Residue
    return self.find_residue(&block)
  elsif findtype == Bio::PDB::Chain
    return self.find_chain(&block)
  elsif findtype == Bio::PDB::Model
    return self.find_model(&block)
  else
    raise TypeError, "You can't find a #{findtype}"
  end
end

#geometricCentre(method = :each_atom) ⇒ Object

Returns the coordinates of the geometric centre (average co-ord) of any AtomFinder (or .atoms) implementing object

If you want to get the geometric centre of hetatms, call geometricCentre(:each_hetatm).



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bio/db/pdb/utils.rb', line 59

def geometricCentre(method = :each_atom)
  x = y = z = count = 0
  
  self.__send__(method) do |atom|
    x += atom.x
    y += atom.y
    z += atom.z
    count += 1
  end
  
  x = (x / count)
  y = (y / count)
  z = (z / count)
 
  Coordinate[x,y,z]
end