Package Bio :: Package PDB :: Module MMCIF2Dict
[hide private]
[frames] | no frames]

Source Code for Module Bio.PDB.MMCIF2Dict

  1  # Copyright (C) 2002, Thomas Hamelryck (thamelry@binf.ku.dk) 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  import os.path 
  7  import warnings 
  8  import Bio.PDB.mmCIF.MMCIFlex 
  9  from UserDict import UserDict 
 10   
 11  __doc__="Turn an mmCIF file into a dictionary." 
 12   
 13   
14 -class MMCIF2Dict(UserDict):
15 # The token identifiers 16 NAME=1 17 LOOP=2 18 DATA=3 19 SEMICOLONS=4 20 DOUBLEQUOTED=5 21 QUOTED=6 22 SIMPLE=7 23
24 - def __init__(self, filename):
25 # this dict will contain the name/data pairs 26 self.data={} 27 # entry for garbage 28 self.data[None]=[] 29 if not os.path.isfile(filename): 30 raise IOError("File not found.") 31 Bio.PDB.mmCIF.MMCIFlex.open_file(filename) 32 self._make_mmcif_dict() 33 Bio.PDB.mmCIF.MMCIFlex.close_file()
34
35 - def _make_mmcif_dict(self):
36 # local copies 37 NAME=self.NAME 38 LOOP=self.LOOP 39 DATA=self.DATA 40 SEMICOLONS=self.SEMICOLONS 41 DOUBLEQUOTED=self.DOUBLEQUOTED 42 QUOTED=self.QUOTED 43 SIMPLE=self.SIMPLE 44 get_token=Bio.PDB.mmCIF.MMCIFlex.get_token 45 # are we looping? 46 loop_flag=0 47 # list of names in loop 48 temp_list=[] 49 # last encountered name 50 current_name=None 51 # get first token/value pair 52 token, value=get_token() 53 # print token, value 54 mmcif_dict=self.data 55 # loop until EOF (token==0) 56 while token: 57 if token==NAME: 58 if loop_flag: 59 # Make lists for all the names in the loop 60 while token==NAME: 61 # create a list for each name encountered in loop 62 new_list=mmcif_dict[value]=[] 63 temp_list.append(new_list) 64 token, value=get_token() 65 # print token, value 66 loop_flag=0 67 # nr of data items parsed 68 data_counter=0 69 # corresponding data name 70 pos=0 71 nr_fields=len(temp_list) 72 # Now fill all lists with the data 73 while token>3: 74 pos=data_counter%nr_fields 75 data_counter=data_counter+1 76 temp_list[pos].append(value) 77 token, value=get_token() 78 # print token, value 79 if pos!=nr_fields-1: 80 warnings.warn("ERROR: broken name-data pair " 81 "(data missing)!", RuntimeWarning) 82 # The last token was not used, so 83 # don't set token to None! (this means the 84 # last parsed token goes through the loop again) 85 else: 86 # simple name-data pair (no loop) 87 # so next token should be the data 88 next_token, data=get_token() 89 # print token, value 90 mmcif_dict[value]=data 91 if next_token<4: 92 warnings.warn("ERROR: broken name-data pair " 93 "(name-non data pair)!", RuntimeWarning) 94 # print token, value 95 else: 96 # get next token 97 token=None 98 elif token==LOOP: 99 loop_flag=1 100 temp_list=[] 101 # get next token 102 token=None 103 elif token==DATA: 104 mmcif_dict[value[0:5]]=value[5:] 105 token=None 106 else: 107 # we found some complete garbage 108 warnings.warn("ERROR: broken name-data pair " 109 "(missing name)!\n%s %s" % (token, value), 110 RuntimeWarning) 111 mmcif_dict[None].append(value) 112 # get next token 113 token=None 114 if token==None: 115 token, value=get_token()
116 # print token, value 117
118 - def __getitem__(self, key):
119 return self.data[key]
120 121 122 if __name__=="__main__": 123 124 import sys 125 126 if len(sys.argv)!=2: 127 print "Usage: python MMCIF2Dict filename." 128 129 filename=sys.argv[1] 130 131 mmcif_dict=MMCIF2Dict(filename) 132 133 input="" 134 print "Now type a key ('q' to end, 'k' for a list of all keys):" 135 while(input!="q"): 136 input=raw_input("MMCIF dictionary key ==> ") 137 if input=="q": 138 sys.exit() 139 if input=="k": 140 for key in mmcif_dict.keys(): 141 print key 142 continue 143 try: 144 value=mmcif_dict[input] 145 if type(value)==type([]): 146 for item in value: 147 print item 148 else: 149 print value 150 except KeyError: 151 print "No such key found." 152