1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
#
# = bio/db/pdb/utils.rb - Utility modules for PDB
#
# Copyright:: Copyright (C) 2004, 2006
# Alex Gutteridge <alexg@ebi.ac.uk>
# Naohisa Goto <ng@bioruby.org>
# License:: The Ruby License
#
#
# = Bio::PDB::Utils
#
# Bio::PDB::Utils
#
# = Bio::PDB::ModelFinder
#
# Bio::PDB::ModelFinder
#
# = Bio::PDB::ChainFinder
#
# Bio::PDB::ChainFinder
#
# = Bio::PDB::ResidueFinder
#
# Bio::PDB::ResidueFinder
#
# = Bio::PDB::AtomFinder
#
# Bio::PDB::AtomFinder
#
# = Bio::PDB::HeterogenFinder
#
# Bio::PDB::HeterogenFinder
#
# = Bio::PDB::HetatmFinder
#
# Bio::PDB::HetatmFinder
#
require 'matrix'
module Bio
require 'bio/db/pdb' unless const_defined?(:PDB)
class PDB
# 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.
module Utils
# 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).
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
#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.
ElementMass = {
'H' => 1,
'C' => 12,
'N' => 14,
'O' => 16,
'S' => 32,
'P' => 31
}
# calculates centre of gravitiy
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
#--
#Perhaps distance and dihedral would be better off as class methods?
#(rather) than instance methods
#++
# Calculates distance between _coord1_ and _coord2_.
def distance(coord1, coord2)
coord1 = convert_to_xyz(coord1)
coord2 = convert_to_xyz(coord2)
(coord1 - coord2).r
end
module_function :distance
# Calculates dihedral angle.
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
module_function :dihedral_angle
# Implicit conversion into Vector or Bio::PDB::Coordinate
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
module_function :convert_to_xyz
# (Deprecated) alias of convert_to_xyz(obj)
def self.to_xyz(obj)
convert_to_xyz(obj)
end
#--
#Methods required for the dihedral angle calculations
#perhaps these should go in some separate Math module
#++
# radian to degree
def rad2deg(r)
(r/Math::PI)*180
end
module_function :rad2deg
# acos
def acos(x)
Math.atan2(Math.sqrt(1 - x**2),x)
end
module_function :acos
# calculates plane
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
module_function :calculatePlane
# 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.
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
end #module Utils
#--
#The *Finder modules implement a find_* method which returns
#an array of anything for which the block evals true
#(suppose Enumerable#find_all method).
#The each_* style methods act as classic iterators.
#++
# methods to access models
#
# XXX#each_model must be defined.
#
# Bio::PDB::ModelFinder is included by Bio::PDB::PDB.
#
module ModelFinder
# returns an array containing all chains for which given block
# is not +false+ (similar to Enumerable#find_all).
def find_model
array = []
self.each_model do |model|
array.push(model) if yield(model)
end
return array
end
end #module ModelFinder
#--
#The heirarchical nature of the objects allow us to re-use the
#methods from the previous level - e.g. A PDB object can use the .models
#method defined in ModuleFinder to iterate through the models to find the
#chains
#++
# methods to access chains
#
# XXX#each_model must be defined.
#
# Bio::PDB::ChainFinder is included by Bio::PDB::PDB and Bio::PDB::Model.
#
module ChainFinder
# returns an array containing all chains for which given block
# is not +false+ (similar to Enumerable#find_all).
def find_chain
array = []
self.each_chain do |chain|
array.push(chain) if yield(chain)
end
return array
end
# iterates over each chain
def each_chain(&x) #:yields: chain
self.each_model { |model| model.each(&x) }
end
# returns all chains
def chains
array = []
self.each_model { |model| array.concat(model.chains) }
return array
end
end #module ChainFinder
# methods to access residues
#
# XXX#each_chain must be defined.
#
# Bio::PDB::ResidueFinder is included by Bio::PDB::PDB, Bio::PDB::Model,
# and Bio::PDB::Chain.
#
module ResidueFinder
# returns an array containing all residues for which given block
# is not +false+ (similar to Enumerable#find_all).
def find_residue
array = []
self.each_residue do |residue|
array.push(residue) if yield(residue)
end
return array
end
# iterates over each residue
def each_residue(&x) #:yields: residue
self.each_chain { |chain| chain.each(&x) }
end
# returns all residues
def residues
array = []
self.each_chain { |chain| array.concat(chain.residues) }
return array
end
end #module ResidueFinder
# methods to access atoms
#
# XXX#each_residue must be defined.
module AtomFinder
# returns an array containing all atoms for which given block
# is not +false+ (similar to Enumerable#find_all).
def find_atom
array = []
self.each_atom do |atom|
array.push(atom) if yield(atom)
end
return array
end
# iterates over each atom
def each_atom(&x) #:yields: atom
self.each_residue { |residue| residue.each(&x) }
end
# returns all atoms
def atoms
array = []
self.each_residue { |residue| array.concat(residue.atoms) }
return array
end
end #module AtomFinder
# methods to access HETATMs
#
# XXX#each_heterogen must be defined.
#
# Bio::PDB::HetatmFinder is included by Bio::PDB::PDB, Bio::PDB::Model,
# Bio::PDB::Chain, and Bio::PDB::Heterogen.
#
module HetatmFinder
# returns an array containing all HETATMs for which given block
# is not +false+ (similar to Enumerable#find_all).
def find_hetatm
array = []
self.each_hetatm do |hetatm|
array.push(hetatm) if yield(hetatm)
end
return array
end
# iterates over each HETATM
def each_hetatm(&x) #:yields: hetatm
self.each_heterogen { |heterogen| heterogen.each(&x) }
end
# returns all HETATMs
def hetatms
array = []
self.each_heterogen { |heterogen| array.concat(heterogen.hetatms) }
return array
end
end #module HetatmFinder
# methods to access heterogens (compounds or ligands)
#
# XXX#each_chain must be defined.
#
# Bio::PDB::HeterogenFinder is included by Bio::PDB::PDB, Bio::PDB::Model,
# and Bio::PDB::Chain.
#
module HeterogenFinder
# returns an array containing all heterogens for which given block
# is not +false+ (similar to Enumerable#find_all).
def find_heterogen
array = []
self.each_heterogen do |heterogen|
array.push(heterogen) if yield(heterogen)
end
return array
end
# iterates over each heterogens
def each_heterogen(&x) #:yields: heterogen
self.each_chain { |chain| chain.each_heterogen(&x) }
end
# returns all heterogens
def heterogens
array = []
self.each_chain { |chain| array.concat(chain.heterogens) }
return array
end
end #module HeterogenFinder
end #class PDB
end #module Bio
|