forked from biopython/biopython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMCIF2Dict.py
97 lines (82 loc) · 2.74 KB
/
MMCIF2Dict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Copyright (C) 2002, Thomas Hamelryck (thamelry@binf.ku.dk)
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
"""Turn an mmCIF file into a dictionary."""
from __future__ import print_function
from Bio._py3k import input as _input
import shlex
class MMCIF2Dict(dict):
def __init__(self, filename):
with open(filename) as handle:
loop_flag = False
key = None
tokens = self._tokenize(handle)
token = next(tokens)
self[token[0:5]]=token[5:]
for token in tokens:
if token=="loop_":
loop_flag = True
keys = []
i = 0
n = 0
continue
elif loop_flag:
if token.startswith("_"):
if i > 0:
loop_flag = False
else:
self[token] = []
keys.append(token)
n += 1
continue
else:
self[keys[i%n]].append(token)
i+=1
continue
if key is None:
key = token
else:
self[key] = token
key = None
def _tokenize(self, handle):
for line in handle:
if line.startswith("#"):
continue
elif line.startswith(";"):
token = line[1:].strip()
for line in handle:
line = line.strip()
if line==';':
break
token += line
yield token
else:
tokens = shlex.split(line)
for token in tokens:
yield token
if __name__=="__main__":
import sys
if len(sys.argv)!=2:
print("Usage: python MMCIF2Dict filename.")
filename=sys.argv[1]
mmcif_dict = MMCIF2Dict(filename)
entry = ""
print("Now type a key ('q' to end, 'k' for a list of all keys):")
while(entry != "q"):
entry = _input("MMCIF dictionary key ==> ")
if entry == "q":
sys.exit()
if entry == "k":
for key in mmcif_dict:
print(key)
continue
try:
value=mmcif_dict[entry]
if isinstance(value, list):
for item in value:
print(item)
else:
print(value)
except KeyError:
print("No such key found.")