/usr/lib/python2.7/dist-packages/chempy/cif.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 | #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 string
import re
import copy
from chempy import io, Atom, Bond
from chempy.models import Indexed
# OMG what a horrid file format.
single_quote_re = re.compile(r"'[^']*[']*'") # doesn't yet handle ESC
double_quote_re = re.compile(r'"[^"]*["]*"') # ditto
bracket_quote_re = re.compile(r'\[[^\]]*\]') # ditto
clean_float_re = re.compile(r'[^0-9+\-.eE].*')
clean_int_re = re.compile(r'[^0-9+\-].*')
class CIFRec:
def get_quoted_value(self):
if self.line[0:1] == "'":
mo = single_quote_re.match(self.line)
elif self.line[0:1] == '"':
mo = double_quote_re.match(self.line)
elif self.line[0:1] == '[':
mo = bracket_quote_re.match(self.line)
else:
mo = None
if mo != None:
result = self.line[:mo.end()]
self.line = self.line[mo.end():]
if len(string.strip(self.line[0:1])): # followed by non-whitespace...
self.line = result[-1:] + self.line
result = result[:-1] + self.get_quoted_value()
self.check_line()
else:
result = None # shouldn't happen
return result
def get_delimited_value(self):
result = ''
while 1:
if self.line[0:1] == ';':
self.line = self.line[1:]
self.check_line()
break
result = result + self.line
self.next_line()
return result
def get_next_word(self):
result = None
mo = re.search("\s+", self.line)
if mo == None:
result = self.line
self.next_line()
else:
result = self.line[:mo.start()]
self.line = self.line[mo.end():]
self.check_line()
return result
def trim_leading_whitespace(self):
while self.line != None:
mo = re.match("\s",self.line)
if mo != None:
self.line = self.line[mo.end():]
self.check_line()
else:
break
def get_next_value(self):
self.trim_leading_whitespace()
if self.line != None:
if self.line[0:1] == '_':
return None
elif self.line[0:1] == ';':
self.line = self.line[1:]
self.check_line()
return self.get_delimited_value()
elif string.lower(self.line[0:5]) in ['loop_','data_','save_']:
return None
elif string.lower(self.line[0:6]) == 'GLOBAL':
return None
elif self.line[0:1] in [ "'", '"']: # '[' ]: bracket quote fubar with PDB's mmCIF data???
return self.get_quoted_value()
else:
return self.get_next_word()
return None
def check_line(self):
if self.line != None:
while not len(string.strip(self.line)):
self.next_line()
if self.line == None:
break
def next_line(self):
self.line = None
while len(self.list):
self.line = self.list.pop()
# nuke hash comments
hash = string.find(self.line,'#')
if hash>=0:
self.line = self.line[0:hash]
# and only return non-blank lines
if len(string.strip(self.line)):
break
# print "next_line:", self.line,
def parse_loop_body(self,fields):
len_fields = len(fields)
records = []
record = []
cnt = len_fields
while self.line != None:
value = self.get_next_value()
if value == None:
break
else:
record.append(value)
cnt = cnt - 1
if not cnt:
cnt = len_fields
if len(record) == len_fields:
records.append(record)
record = []
# print "loop_read [%s]=[%s]"%(fields[0],value)
if len(record) == len_fields:
records.append(record)
self.loops.append( (fields,records) )
def parse_loop(self):
# print "parsing loop..."
fields = []
while self.line != None:
if string.lower(self.line[0:5])=='loop_':
break
elif string.strip(self.line)[0:1]=='_':
fields.append(string.lower(string.strip(self.line)))
else:
self.parse_loop_body(fields)
break
self.next_line()
def parse_name_value(self):
data_name = self.get_next_word()
data_value = self.get_next_value()
self.key_value[data_name] = data_value
# print "data_read [%s]=[%s]"%(data_name,data_value)
def parse_normal(self):
while self.line != None:
if self.line[0:1] == '_': # data name
self.parse_name_value()
elif string.lower(self.line[0:5])=='loop_':
print "entering loop",self.line
self.line = self.line[5:]
self.check_line()
self.parse_loop()
print "exiting loop",self.line
else: # shouldn't happen
print "unhandled: [%s]"%self.line
self.next_line()
def to_float(self, key):
value = self.key_value[key]
value = clean_float_re.sub("",value)
return float(value)
def to_str(self, key):
value = self.key_value[key]
if value[0:1]=="'" and value[-1:] =="'":
value = value[1:-1]
if value[0:1]=='"' and value[-1:] =='"':
value = value[1:-1]
return value
def read_symmetry(self):
kv = self.key_value
if ( kv.has_key("_cell_length_a") and
kv.has_key("_cell_length_b") and
kv.has_key("_cell_length_c") and
kv.has_key("_cell_angle_alpha") and
kv.has_key("_cell_angle_beta") and
kv.has_key("_cell_angle_gamma")):
self.model.cell = [
self.to_float("_cell_length_a"),
self.to_float("_cell_length_b"),
self.to_float("_cell_length_c"),
self.to_float("_cell_angle_alpha"),
self.to_float("_cell_angle_beta"),
self.to_float("_cell_angle_gamma")]
# if hasattr(self.model,'cell'):
# print self.model.cell
if kv.has_key("_symmetry_space_group_name_h-m"):
self.model.spacegroup = self.to_str('_symmetry_space_group_name_h-m')
# if hasattr(self.model,'spacegroup'):
# print self.model.spacegroup
def index_to_int(self, index, value):
result = clean_int_re.sub("",value[index])
return int(result)
def index_to_float(self, index, value):
result = clean_float_re.sub("",value[index])
if len(result):
return float(result)
else:
return 0.0
def index_to_str(self, index, value):
return value[index]
def read_chem_comp_atom_model_cartn(self,fields,field_dict,values):
cartn_x = field_dict['_chem_comp_atom_model_cartn_x']
cartn_y = field_dict['_chem_comp_atom_model_cartn_y']
cartn_z = field_dict['_chem_comp_atom_model_cartn_z']
print cartn_x,cartn_y,cartn_z
name = field_dict.get('_chem_comp_atom_atom_id',None)
symbol = field_dict.get('_chem_comp_atom_type_symbol',None)
resn = field_dict.get('_chem_comp_atom.comp_id',None)
partial_charge = field_dict.get('_chem_comp_atom_partial_charge',None)
formal_charge = field_dict.get('chem_comp_atom_charge',None)
str_fields = []
if symbol != None: str_fields.append( ('symbol',symbol) )
if name != None: str_fields.append( ('name',name) )
if resn != None: str_fields.append( ('resn',resn) )
float_fields = []
if partial_charge != None: float_fields.append( ('partial_charge',partial_charge) )
int_fields = []
if formal_charge != None: int_fields.append( ('formal_charge',formal_charge) )
for value in values:
atom = Atom()
atom.coord = [
self.index_to_float(cartn_x,value),
self.index_to_float(cartn_y,value),
self.index_to_float(cartn_z,value)]
self.model.atom.append(atom)
for field in str_fields:
setattr(atom,field[0],value[field[1]])
for field in float_fields:
setattr(atom,field[0],self.index_to_float(field[1],value))
for field in int_fields:
setattr(atom,field[0],self.index_to_int(field[1],value))
def read_chem_comp_atom(self):
self.atom_site_label_index = {}
for loop in self.loops:
(fields, field_dict, values) = loop
if (field_dict.has_key("_chem_comp_atom_model_cartn_x") and
field_dict.has_key("_chem_comp_atom_model_cartn_y") and
field_dict.has_key("_chem_comp_atom_model_cartn_z")):
self.read_chem_comp_atom_model_cartn(fields,field_dict,values)
def read_atom_site_fract(self,fields,field_dict,values):
self.model.fractional = 1
fract_x = field_dict['_atom_site_fract_x']
fract_y = field_dict['_atom_site_fract_y']
fract_z = field_dict['_atom_site_fract_z']
symbol = field_dict.get('_atom_site_type_symbol',None)
name = field_dict.get('_atom_site_label',None)
u = field_dict.get('_atom_site_u_iso_or_equiv',None)
str_fields = []
if symbol != None: str_fields.append( ('symbol',symbol) )
if name != None: str_fields.append( ('name',name) )
float_fields = []
if u != None: float_fields.append( ('u', u))
int_fields = []
for value in values:
atom = Atom()
atom.coord = [
self.index_to_float(fract_x,value),
self.index_to_float(fract_y,value),
self.index_to_float(fract_z,value)]
self.model.atom.append(atom)
for field in str_fields:
setattr(atom,field[0],value[field[1]])
for field in float_fields:
setattr(atom,field[0],self.index_to_float(field[1],value))
for field in int_fields:
setattr(atom,field[0],self.index_to_int(field[1],value))
def read_atom_site_cartn(self,fields,field_dict,values):
cartn_x = field_dict['_atom_site_cartn_x']
cartn_y = field_dict['_atom_site_cartn_y']
cartn_z = field_dict['_atom_site_cartn_z']
group_pdb = field_dict.get('_atom_site_group_pdb',None)
symbol = field_dict.get('_atom_site_type_symbol',None)
name = field_dict.get('_atom_site_label_atom_id',None)
resn = field_dict.get('_atom_site_label_comp_id',None)
resi = field_dict.get('_atom_site_label_seq_id',None)
chain = field_dict.get('_atom_site_label_asym_id',None)
ins_code = field_dict.get('_atom_site_pdbx_pdb_ins_code',None)
# use auth fields preferentially, if provided
auth_resn = field_dict.get('_atom_site_auth_comp_id',None)
auth_resi = field_dict.get('_atom_site_auth_seq_id',None)
auth_name = field_dict.get('_atom_site_auth_atom_id',None)
auth_chain = field_dict.get('_atom_site_auth_asym_id',None)
if auth_resn != None: resn = auth_resn
if auth_resi != None: resi = auth_resi
if auth_name != None: name = auth_name
if auth_chain != None: chain = auth_chain
b = field_dict.get('_atom_site_b_iso_or_equiv',None)
q = field_dict.get('_atom_site_occupancy',None)
ID = field_dict.get('_atom_site_id',None)
str_fields = []
if symbol != None: str_fields.append( ('symbol',symbol) )
if name != None: str_fields.append( ('name',name) )
if resn != None: str_fields.append( ('resn',resn) )
if resi != None: str_fields.append( ('resi',resi) )
if chain != None: str_fields.append( ('chain',chain) )
if ins_code != None: str_fields.append( ('ins_code',ins_code) )
float_fields = []
if q != None: float_fields.append( ('q',q) )
if b != None: float_fields.append( ('b',b) )
int_fields = []
if ID != None: int_fields.append( ('id',ID) )
for value in values:
atom = Atom()
atom.coord = [
self.index_to_float(cartn_x,value),
self.index_to_float(cartn_y,value),
self.index_to_float(cartn_z,value)]
self.model.atom.append(atom)
if group_pdb != None:
if value[group_pdb] == 'ATOM':
atom.hetatm = 0
else:
atom.hetatm = 1
for field in str_fields:
setattr(atom,field[0],value[field[1]])
for field in float_fields:
setattr(atom,field[0],self.index_to_float(field[1],value))
for field in int_fields:
setattr(atom,field[0],self.index_to_int(field[1],value))
# for a in self.model.atom:
# print a.coord
def read_atom_site_aniso(self,fields,field_dict,values):
cnt = 0
name_dict = {}
for atom in self.model.atom:
if hasattr(atom,'name'):
name_dict[atom.name] = cnt
cnt = cnt + 1
label = field_dict['_atom_site_aniso_label']
u11 = field_dict['_atom_site_aniso_u_11']
u22 = field_dict['_atom_site_aniso_u_22']
u33 = field_dict['_atom_site_aniso_u_33']
u12 = field_dict['_atom_site_aniso_u_12']
u13 = field_dict['_atom_site_aniso_u_13']
u23 = field_dict['_atom_site_aniso_u_23']
for value in values:
atom = name_dict[self.index_to_str(label,value)]
self.model.atom[atom].u_aniso = [
self.index_to_float(u11,value),
self.index_to_float(u22,value),
self.index_to_float(u33,value),
self.index_to_float(u12,value),
self.index_to_float(u13,value),
self.index_to_float(u23,value)
]
def read_atom_site(self):
self.atom_site_label_index = {}
for loop in self.loops:
(fields, field_dict, values) = loop
if (field_dict.has_key("_atom_site_fract_x") and
field_dict.has_key("_atom_site_fract_y") and
field_dict.has_key("_atom_site_fract_z")): # fractional coords
self.read_atom_site_fract(fields,field_dict,values)
elif (field_dict.has_key("_atom_site_cartn_x") and
field_dict.has_key("_atom_site_cartn_y") and
field_dict.has_key("_atom_site_cartn_z")): # cartesian coords
self.read_atom_site_cartn(fields,field_dict,values)
elif (field_dict.has_key("_atom_site_aniso_label") and
field_dict.has_key("_atom_site_aniso_u_11") and
field_dict.has_key("_atom_site_aniso_u_22") and
field_dict.has_key("_atom_site_aniso_u_33") and
field_dict.has_key("_atom_site_aniso_u_12") and
field_dict.has_key("_atom_site_aniso_u_13") and
field_dict.has_key("_atom_site_aniso_u_23")): # anisotropics
self.read_atom_site_aniso(fields,field_dict,values)
def read_geom_bond_atom_site_labels(self,fields,field_dict,values):
# create index of atom name
cnt = 0
name_dict = {}
for atom in self.model.atom:
if hasattr(atom,'name'):
name_dict[atom.name] = cnt
cnt = cnt + 1
label_1 = field_dict['_geom_bond_atom_site_label_1']
label_2 = field_dict['_geom_bond_atom_site_label_2']
for value in values:
bond = Bond()
bond.index = [
name_dict[self.index_to_str(label_1,value)],
name_dict[self.index_to_str(label_2,value)]]
bond.order = 1
self.model.bond.append(bond)
def read_geom_bond(self):
self.atom_site_label_index = {}
for loop in self.loops:
(fields, field_dict, values) = loop
if (field_dict.has_key("_geom_bond_atom_site_label_1") and
field_dict.has_key("_geom_bond_atom_site_label_2")):
self.read_geom_bond_atom_site_labels(fields,field_dict,values)
def read_chem_comp_bond_atom_ids(self,fields,field_dict,values):
order_table = { 'sing' : 1, 'doub' : 2, 'trip' :3, 'delo': 4 }
# create index of atom name
cnt = 0
name_dict = {}
for atom in self.model.atom:
if hasattr(atom,'name'):
name_dict[atom.name] = cnt
cnt = cnt + 1
label_1 = field_dict['_chem_comp_bond_atom_id_1']
label_2 = field_dict['_chem_comp_bond_atom_id_2']
order = field_dict.get('_chem_comp_bond_value_order',None)
for value in values:
bond = Bond()
bond.index = [
name_dict[self.index_to_str(label_1,value)],
name_dict[self.index_to_str(label_2,value)]]
if order != None:
order_string = string.lower(self.index_to_str(order,value))
bond.order = order_table.get(order_string[0:4],1)
else:
bond.order = 1
self.model.bond.append(bond)
def read_chem_comp_bond(self):
self.atom_site_label_index = {}
for loop in self.loops:
(fields, field_dict, values) = loop
if (field_dict.has_key("_chem_comp_bond_atom_id_1") and
field_dict.has_key("_chem_comp_bond_atom_id_2")):
self.read_chem_comp_bond_atom_ids(fields,field_dict,values)
def create_field_dicts(self):
new_loops = []
for loop in self.loops:
(fields, values) = loop
field_dict = {}
cnt = 0
for field in fields:
field_dict[field] = cnt
cnt = cnt + 1
new_loops.append( (fields,field_dict,values) )
self.loops = new_loops
def convert_dot_to_underscore(self):
for key in self.key_value.keys():
if string.find(key,".")>=0:
new_key = string.replace(key,".","_")
else:
new_key = key
new_key = string.lower(new_key)
if new_key != key:
self.key_value[new_key] = self.key_value[key]
del self.key_value[key]
new_loops = []
for loop in self.loops:
(fields, values) = loop
fields = map(lambda x:string.replace(x,".","_"),fields)
fields = map(lambda x:string.lower(x),fields)
new_loops.append( (fields,values) )
self.loops = new_loops
def __init__(self,cif_list):
cif_list.reverse()
self.list = cif_list
data_line = self.list.pop()
self.loops = []
self.key_value = {}
self.data_name = string.strip(data_line[5:])
self.next_line()
self.parse_normal()
print ' CIF: For data block "%s"...'%self.data_name
print " CIF: Read %d key/value pair(s)."%len(self.key_value)
print " CIF: Read %d table(s)."%len(self.loops)
self.convert_dot_to_underscore()
self.create_field_dicts()
# now build the molecule record
self.model = Indexed()
# by default, indicate that we want PyMOL to automatically
# detect bonds based on coordinates
self.model.connect_mode = 3
self.read_symmetry()
self.read_atom_site()
self.read_chem_comp_atom()
self.read_geom_bond()
self.read_chem_comp_bond()
def toList(self):
return r
class CIF:
def __init__(*args):
mode = 'r'
if len(args)<2:
raise ValueError
self = args[0]
self.input_line = None
fname = args[1]
if len(args)==3:
mode = args[2]
self.mode = mode
self.at_eof = 0
if mode not in ('w','r','wa','pf','url'):
print " CIF: bad mode"
return None
if mode=='pf': # pseudofile
self.file = fname
elif (mode[0:1]=='r') and (string.find(fname,':')>1):
# does this look like a URL? (but not a DOS path)
from urllib import urlopen
self.file = urlopen(fname)
else:
self.file = open(fname,mode)
def write(self,rec):
lst = rec.toList()
for a in lst:
self.file.write(a)
self.file.write('$$$$\n')
def read(self): # returns CIFRec or None at end of file
cur = []
data_seen = 0
while 1:
if self.input_line == None:
s = self.file.readline()
else:
s = self.input_line
self.input_line = None
if not s: # end of file
if len(cur)>0:
return CIFRec(cur)
else:
return None
elif string.lower(s[0:5]) == r'data_': # signals start of new record
data_seen = 1
if len(cur)>0:
self.input_line = s # save for next time
return CIFRec(cur)
elif data_seen:
cur.append(s)
elif data_seen:
cur.append(s)
def close(self):
self.file.close()
|