/usr/lib/python2.7/dist-packages/chempy/models.py is in pymol 1.7.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 | #A* -------------------------------------------------------------------
#B* This file contains source code for the PyMOL computer program
#C* copyright 1998-2000 by Warren Lyford Delano of DeLano Scientific.
#D* -------------------------------------------------------------------
#E* It is unlawful to modify or remove this copyright notice.
#F* -------------------------------------------------------------------
#G* Please see the accompanying LICENSE file for further information.
#H* -------------------------------------------------------------------
#I* Additional authors of this source file include:
#-*
#-*
#-*
#Z* -------------------------------------------------------------------
import chempy
import copy
from cpv import *
import operator
class Base:
@property
def nAtom(self):
return len(self.atom)
@property
def nBond(self):
return len(self.bond)
#------------------------------------------------------------------------------
def update_index(self):
if chempy.feedback['verbose']:
print " "+str(self.__class__)+": updating indexes..."
c = 0
self.index = {}
idx = self.index
for a in self.atom:
idx[id(a)] = c
c = c + 1
#------------------------------------------------------------------------------
def get_residues(self):
list = []
if self.nAtom:
last = self.atom[0]
c = 0
start = 0
for a in self.atom:
if not a.in_same_residue(last):
list.append((start,c))
start = c
last = a
c = c + 1
if (c-start>1):
list.append((start,c))
return list
#------------------------------------------------------------------------------
def get_coord_list(self):
lst = []
for a in self.atom:
lst.append(a.coord)
return lst
#------------------------------------------------------------------------------
def get_mass(self):
sm = 0.0
for a in self.atom:
sm = sm + a.get_mass()
return sm
#------------------------------------------------------------------------------
def get_nuclear_charges(self):
'''Return the sum of nuclear charges of all atoms in a molecule.'''
sm = 0
for a in self.atom:
sm = sm + a.get_number()
return sm
#------------------------------------------------------------------------------
def list(self):
for a in self.atom:
print a.symbol, a.name, a.coord
for a in self.bond:
print a.index
#------------------------------------------------------------------------------
def get_implicit_mass(self):
# mass calculation for implicit models
valence = [0]*len(self.atom)
for a in self.bond:
v = 1.5 if a.order == 4 else a.order
valence[a.index[0]] += v
valence[a.index[1]] += v
h_count = sum(a.get_free_valence(v)
for (a, v) in zip(self.atom, valence))
hydrogen = chempy.Atom()
hydrogen.symbol='H'
return self.get_mass()+hydrogen.get_mass()*h_count
#------------------------------------------------------------------------------
def assign_names(self,preserve=1):
dct = {}
cnt = {}
if preserve:
for a in self.atom:
if a.has('name'):
dct[a['name']] = 1
else:
for a in self.atom:
if hasattr(a,'name'):
del a.name
for a in self.atom:
if not a.has('name'):
if not cnt.has_key(a.symbol):
c = 1
else:
c = cnt[a.symbol]
while 1:
nam = a.symbol+str(c)
c = c + 1
if not dct.has_key(nam):
break
cnt[a.symbol]=c
a.name=nam
dct[nam]=1
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
class Indexed(Base):
#------------------------------------------------------------------------------
def __init__(self):
self.reset()
#------------------------------------------------------------------------------
def reset(self):
self.index = None
self.molecule = chempy.Molecule()
self.atom = []
self.bond = []
#------------------------------------------------------------------------------
def get_min_max(self):
if len(self.atom):
mn = copy.deepcopy(self.atom[0].coord)
mx = copy.deepcopy(self.atom[0].coord)
for a in self.atom:
ac = a.coord
if mn[0]>ac[0]: mn[0]=ac[0]
if mn[1]>ac[1]: mn[1]=ac[1]
if mn[2]>ac[2]: mn[2]=ac[2]
if mx[0]<ac[0]: mx[0]=ac[0]
if mx[1]<ac[1]: mx[1]=ac[1]
if mx[2]<ac[2]: mx[2]=ac[2]
return [mn,mx]
else:
return [[0.0,0.0,0.0],[0.0,0.0,0.0]]
#------------------------------------------------------------------------------
def merge(self,other): # steals atom objects from 'other' and resets 'other'
if chempy.feedback['actions']:
print " "+str(self.__class__)+": merging models..."
nAtom=self.nAtom
self.atom.extend(other.atom)
for b in other.bond:
b.index[0]=b.index[0]+nAtom
b.index[1]=b.index[1]+nAtom
self.bond.append(b)
other.reset()
if self.index:
self.update_index()
#--------------------------------------------------------------------------------
def delete_atom(self,index):
if chempy.feedback['atoms']:
print " "+str(self.__class__)+": deleting atom %d." % index
nAtom=self.nAtom
# update index if it exists
if self.index:
idx = self.index
for k in idx.keys():
if idx[k] > index:
idx[k] = idx[k] - 1
del idx[id(self.atom[index])]
# delete atom
del self.atom[index]
# delete bonds associated with this atom
nBond = len(self.bond)
templist = []
for i in range(nBond):
if index in self.bond[i].index:
templist.append(i)
for i in range(len(templist)):
j = templist[i] - i
del self.bond[j]
# re-index bond table
for b in self.bond:
if b.index[0] > index:
b.index[0] = b.index[0] - 1
if b.index[1] > index:
b.index[1] = b.index[1] - 1
#--------------------------------------------------------------------------------
def delete_list(self,list): # delete a list of indexed atoms
if chempy.feedback['atoms']:
print " "+str(self.__class__)+": deleting atoms %s." % str(list)
nAtom=self.nAtom
lrev = copy.deepcopy(list)
lrev.sort()
lrev.reverse()
# generate cross-reference tables
o2n = {} # old to new
if len(lrev):
nxt = lrev.pop()
else:
nxt = None
shft = 0
for i in range(nAtom):
if i == nxt:
o2n[i]=-1
if len(lrev):
nxt = lrev.pop()
else:
nxt = None
shft = shft - 1
else:
ixs = i + shft
o2n[i] = ixs
if shft:
# delete atoms
new_atom = []
for i in range(nAtom):
if o2n[i]>=0:
new_atom.append(self.atom[i])
self.atom = new_atom
# delete bonds
new_bond = []
for b in self.bond:
b0 = b.index[0]
b1 = b.index[1]
if (o2n[b0]>=0) and (o2n[b1]>=0):
b.index[0] = o2n[b0]
b.index[1] = o2n[b1]
new_bond.append(b)
self.bond = new_bond
# update index if it exists
if self.index:
self.index = {}
idx = self.index
i = 0
for a in self.atom:
idx[id(a)] = i
i = i + 1
#------------------------------------------------------------------------------
def insert_atom(self,index,atom):
if chempy.feedback['atoms']:
print " "+str(self.__class__)+': inserting atom "%s" before %d.' % (
atom.name,index)
nAtom=self.nAtom
self.atom.insert(index,atom)
# re-index bond table
for b in self.bond:
if b.index[0] >= index:
b.index[0] = b.index[0] + 1
if b.index[1] >= index:
b.index[1] = b.index[1] + 1
# update index if it exists
if self.index:
idx = self.index
for k in idx.keys():
if idx[k] >= index:
idx[k] = idx[k] + 1
idx[id(atom)] = index
#------------------------------------------------------------------------------
def index_atom(self,atom):
c = 0
id_at = id(atom)
for a in self.atom:
if id(a)==id_at:
return c
c = c + 1
return -1
#------------------------------------------------------------------------------
def add_atom(self,atom):
if chempy.feedback['atoms']:
print " "+str(self.__class__)+': adding atom "%s".' % atom.name
self.atom.append(atom)
index = self.nAtom - 1
if self.index:
self.index[id(atom)] = index
return index
#------------------------------------------------------------------------------
def add_bond(self,bond):
if chempy.feedback['bonds']:
print " "+str(self.__class__)+": adding bond (%d,%d)." % \
(bond.index[0],bond.index[1])
self.bond.append(bond)
#------------------------------------------------------------------------------
def remove_bond(self,index):
if chempy.feedback['bonds']:
print " "+str(self.__class__)+": removing bond %d." % index
nBond=len(self.Bond)
del self.bond[index]
#------------------------------------------------------------------------------
def convert_to_connected(self):
if chempy.feedback['verbose']:
print " "+str(self.__class__)+": converting to connected model..."
model = Connected()
model.molecule = self.molecule
model.atom = self.atom
model.bond = []
model.index = None
for a in model.atom:
model.bond.append([])
for b in self.bond:
model.bond[b.index[0]].append(b) # note two refs to same object
model.bond[b.index[1]].append(b) # note two refs to same object
self.reset()
return model
#------------------------------------------------------------------------------
def from_molobj(self,molobj):
self.reset()
mol = self.molecule
if len(molobj.title):
mol.title = molobj.title
if len(molobj.comments):
mol.comments = molobj.comments
mol.chiral = molobj.chiral
mol.dim_code = molobj.dimcode
for a in molobj.atom:
at = chempy.Atom()
at.symbol = a.symbol
at.name = a.name
if a.resn != chempy.Atom.defaults['resn']:
at.resn = a.resn
if a.resn_code != chempy.Atom.defaults['resn_code']:
at.resn_code = a.resn_code
at.resi = a.resi
at.resi_number = a.resi_number
at.b = a.b
at.q = a.q
at.alt = a.alt
at.hetatm = a.hetatm
if a.segi != chempy.Atom.defaults['segi']:
at.segi = a.segi
if a.chain != chempy.Atom.defaults['chain']:
at.chain = a.chain
at.color_code = a.color_code
at.coord = a.coord
at.formal_charge = a.formal_charge
at.partial_charge = a.partial_charge
if a.numeric_type != -99:
at.numeric_type = a.numeric_type
if a.text_type != 'UNKNOWN':
at.text_type = a.text_type
at.stereo = a.stereo
if hasattr(a,'flags'):
at.flags = a.flags
if hasattr(a,'vdw'):
at.vdw = a.vdw
self.atom.append(at)
for b in molobj.bond:
bnd = chempy.Bond()
bnd.index = [b.atom[0],b.atom[1]]
bnd.order = b.order
bnd.stereo = b.stereo
self.bond.append(bnd)
#------------------------------------------------------------------------------
def sort(self):
if chempy.feedback['verbose']:
print " "+__name__+": sorting..."
if not self.index:
self.update_index()
old_index = self.index
self.atom.sort()
self.update_index()
xref = {}
new_index = self.index
for a in new_index.keys():
xref[old_index[a]] = new_index[a]
for b in self.bond:
b.index[0] = xref[b.index[0]]
b.index[1] = xref[b.index[1]]
del old_index
del xref
#------------------------------------------------------------------------------
def get_internal_tuples(self):
# generates raw atom sets needed to construct an internal coordinate
# description of the molecule
model = self
# get a connected version too
cmodel = copy.deepcopy(model).convert_to_connected()
center = [0.0,0.0,0.0]
nAtom = model.nAtom
to_go = nAtom
done = {}
if to_go<3:
z_set = [(0),(1,0)]
else:
# get center of molecule
for a in model.atom:
center = add(center,a.coord)
center = scale(center,1.0/nAtom)
# find most central multivalent atom
min_a = -1
c = 0
for a in model.atom:
if len(cmodel.bond[c])>1: # must have at least two neighbors
d = distance(a.coord,center)
if min_a < 0:
min_d = d
min_a = c
elif d<min_d:
min_d=d
min_a=c
c = c + 1
fst = min_a
# make this our first atom
z_set = [( fst, )]
done[fst] = 1
to_go = to_go - 1
# for the second atom, try to choose different multivalent neighbor
nxt = -1
for b in cmodel.bond[fst]:
nbr = b.index[0]
if nbr == fst:
nbr = b.index[1]
if len(cmodel.bond[nbr])>1:
nxt = nbr
break
# safety, choose any neighbor
if nxt<0:
nbr = b.index[0]
if nbr == fst:
nbr = b.index[1]
nxt = nbr
z_set.append((nxt,fst))
done[nxt] = 1
to_go = to_go - 1
# for the third atom, choose a different multivalent neighbor
trd = -1
for b in cmodel.bond[fst]:
nbr = b.index[0]
if nbr == fst:
nbr = b.index[1]
if len(cmodel.bond[nbr])>1:
if not done.has_key(nbr):
trd = nbr
break
# safety, choose any unchosen neighbor
if trd<0:
for b in cmodel.bond[fst]:
nbr = b.index[0]
if nbr == fst:
nbr = b.index[1]
if not done.has_key(nbr):
trd = nbr
break
z_set.append((trd,fst,nxt))
done[trd] = 1
result = 1
to_go = to_go - 1
if to_go:
# now find all torsions in the molecule
tors = {}
for b in model.bond: # use bond as center of torsion
a1 = b.index[0]
a2 = b.index[1]
for c in cmodel.bond[a1]:
a0 = c.index[0]
if a0 not in (a1,a2): # outside atom
for d in cmodel.bond[a2]:
a3 = d.index[0]
if a3 not in (a0,a1,a2): # outside atom
if a0 < a3:
to = (a0,a1,a2,a3)
else:
to = (a3,a2,a1,a0)
tors[to] = 1
a3 = d.index[1]
if a3 not in (a0,a1,a2): # outside atom
if a0 < a3:
to = (a0,a1,a2,a3)
else:
to = (a3,a2,a1,a0)
tors[to] = 1
a0 = c.index[1]
if a0 not in (a1,a2): # outside atom
for d in cmodel.bond[a2]:
a3 = d.index[0]
if a3 not in (a0,a1,a2): # outside atom
if a0 < a3:
to = (a0,a1,a2,a3)
else:
to = (a3,a2,a1,a0)
tors[to] = 1
a3 = d.index[1]
if a3 not in (a0,a1,a2): # outside atom
if a0 < a3:
to = (a0,a1,a2,a3)
else:
to = (a3,a2,a1,a0)
tors[to] = 1
if len(tors.keys()):
# choose remaining atoms based on existing atoms using torsion
while to_go:
for tor in tors.keys():
a0 = tor[0]
a1 = tor[1]
a2 = tor[2]
a3 = tor[3]
dh0 = done.has_key(a0)
dh1 = done.has_key(a1)
dh2 = done.has_key(a2)
dh3 = done.has_key(a3)
if ( (not dh0) and dh1 and dh2 and dh3 ):
z_set.append((a0,a1,a2,a3))
done[a0] = 1
to_go = to_go - 1
elif ( dh0 and dh1 and dh2 and (not dh3) ):
z_set.append((a3,a2,a1,a0))
done[a3] = 1
to_go = to_go - 1
else: # for molecules with no torsions (dichloromethane, etc.)
# we have to generate torsions which include one virtual
# bond
for b in cmodel.bond[fst]:
nbr = b.index[0]
if nbr in [fst,nxt,trd]:
nbr = b.index[1]
if not done.has_key(nbr):
z_set.append((nbr,trd,fst,nxt))
to_go = to_go - 1
done[nbr] = 1
return z_set
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
class Connected(Base):
#------------------------------------------------------------------------------
def __init__(self):
self.reset()
#------------------------------------------------------------------------------
def reset(self):
self.index = None
self.molecule = chempy.Molecule()
self.atom = []
self.bond = []
#------------------------------------------------------------------------------
def convert_to_indexed(self):
if chempy.feedback['verbose']:
print " "+str(self.__class__)+": converting to indexed model..."
indexed = Indexed()
indexed.atom = self.atom
indexed.molecule = self.molecule
c = 0
for a in self.bond:
for b in a:
if b.index[0] == c:
indexed.bond.append(b)
c = c + 1
self.reset()
return indexed
#------------------------------------------------------------------------------
def insert_atom(self,index,atom):
if chempy.feedback['atoms']:
print " "+str(self.__class__)+': inserting atom "%s" before %d.' % (
atom.name,index)
nAtom=self.nAtom
self.atom.insert(index,atom)
# re-index bond table
for a in self.bonds:
for b in a:
if b.index[0] >= index:
b.index[0] = b.index[0] + 1
if b.index[1] >= index:
b.index[1] = b.index[1] + 1
# update index if it exists
if self.index:
idx = self.index
for k in idx.keys():
if idx[k] >= index:
idx[k] = idx[k] + 1
idx[id(atom)] = index
#------------------------------------------------------------------------------
def delete_atom(self,index):
if chempy.feedback['atoms']:
print " "+str(self.__class__)+": deleting atom %d." % index
nAtom=self.nAtom
# update index if it exists
if self.index:
idx = self.index
for k in idx.keys():
if idx[k] > index:
idx[k] = idx[k] - 1
del idx[id(self.atom[index])]
# delete atom
del self.atom[index]
# delete bonds associated with this atom
nBond = len(self.bond)
for a in self.bond:
i = 0
templist = []
for b in a:
if index in b.index:
templist.append(i)
i = i + 1
for i in range(len(templist)):
j = templist[i] - i
del a[j]
# re-index bond table
for b in self.bond:
if b.index[0] > index:
b.index[0] = b.index[0] - 1
if b.index[1] > index:
b.index[1] = b.index[1] - 1
#------------------------------------------------------------------------------
def add_atom(self,atom):
if chempy.feedback['atoms']:
print " "+str(self.__class__)+': adding atom "%s".' % atom.name
self.atom.append(atom)
self.bond.append([])
index = self.nAtom - 1
if self.index:
self.index[id(atom)] = index
return index
#------------------------------------------------------------------------------
def sort(self):
if chempy.feedback['verbose']:
print " "+__name__+": sorting..."
if not self.index:
self.update_index()
old_index = self.index
self.atom.sort()
self.update_index()
xref = {}
new_index = self.index
for a in new_index.keys():
xref[old_index[a]] = new_index[a]
new_bond = [None] * self.nAtom
c = 0
tmp_list = []
for a in self.bond:
for b in a:
if c==b.index[0]:
tmp_list.append(b)
new_bond[xref[c]] = a
c = c + 1
for b in tmp_list:
b.index[0] = xref[b.index[0]]
b.index[1] = xref[b.index[1]]
del self.bond
self.bond = new_bond
del old_index
del xref
|