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  from __future__ import print_function 
 9   
10  from Bio._py3k import input as _input 
11   
12  import shlex 
13   
14 -class MMCIF2Dict(dict):
15
16 - def __init__(self, filename):
17 with open(filename) as handle: 18 loop_flag = False 19 key = None 20 tokens = self._tokenize(handle) 21 token = next(tokens) 22 self[token[0:5]]=token[5:] 23 for token in tokens: 24 if token=="loop_": 25 loop_flag = True 26 keys = [] 27 i = 0 28 n = 0 29 continue 30 elif loop_flag: 31 if token.startswith("_"): 32 if i > 0: 33 loop_flag = False 34 else: 35 self[token] = [] 36 keys.append(token) 37 n += 1 38 continue 39 else: 40 self[keys[i%n]].append(token) 41 i+=1 42 continue 43 if key is None: 44 key = token 45 else: 46 self[key] = token 47 key = None
48
49 - def _tokenize(self, handle):
50 for line in handle: 51 if line.startswith("#"): 52 continue 53 elif line.startswith(";"): 54 token = line[1:].strip() 55 for line in handle: 56 line = line.strip() 57 if line==';': 58 break 59 token += line 60 yield token 61 else: 62 tokens = shlex.split(line) 63 for token in tokens: 64 yield token
65 66 67 if __name__=="__main__": 68 69 import sys 70 71 if len(sys.argv)!=2: 72 print("Usage: python MMCIF2Dict filename.") 73 74 filename=sys.argv[1] 75 76 mmcif_dict = MMCIF2Dict(filename) 77 78 entry = "" 79 print("Now type a key ('q' to end, 'k' for a list of all keys):") 80 while(entry != "q"): 81 entry = _input("MMCIF dictionary key ==> ") 82 if entry == "q": 83 sys.exit() 84 if entry == "k": 85 for key in mmcif_dict: 86 print(key) 87 continue 88 try: 89 value=mmcif_dict[entry] 90 if isinstance(value, list): 91 for item in value: 92 print(item) 93 else: 94 print(value) 95 except KeyError: 96 print("No such key found.") 97