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