1
2
3
4
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
14
15 NAME=1
16 LOOP=2
17 DATA=3
18 SEMICOLONS=4
19 DOUBLEQUOTED=5
20 QUOTED=6
21 SIMPLE=7
22
33
35
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
45 loop_flag=0
46
47 temp_list=[]
48
49 current_name=None
50
51 token, value=get_token()
52
53 mmcif_dict=self.data
54
55 while token:
56 if token==NAME:
57 if loop_flag:
58
59 while token==NAME:
60
61 new_list=mmcif_dict[value]=[]
62 temp_list.append(new_list)
63 token, value=get_token()
64
65 loop_flag=0
66
67 data_counter=0
68
69 pos=0
70 nr_fields=len(temp_list)
71
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
78 if pos!=nr_fields-1:
79 print "ERROR: broken name-data pair (data missing)!"
80
81
82
83 else:
84
85
86 next_token, data=get_token()
87
88 mmcif_dict[value]=data
89 if next_token<4:
90 print "ERROR: broken name-data pair (name-non data pair)!"
91
92 else:
93
94 token=None
95 elif token==LOOP:
96 loop_flag=1
97 temp_list=[]
98
99 token=None
100 elif token==DATA:
101 mmcif_dict[value[0:5]]=value[5:]
102 token=None
103 else:
104
105 print "ERROR: broken name-data pair (missing name)!"
106 print token, value
107 mmcif_dict[None].append(value)
108
109 token=None
110 if token==None:
111 token, value=get_token()
112
113
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