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