1 """Unit test
2 """
3 from bmrblib.pystarlib.File import File
4 from bmrblib.pystarlib import Utils
5
6 import __init__
7
8 import os
9 import zipfile
10 import urllib.request, urllib.parse, urllib.error
11 from unittest import TestCase
12
13
15 strf = File(verbosity=2)
16
18 """STAR parse"""
19 text = """data_no_comments_here
20
21 save_comment
22 _Saveframe_category comment
23 loop_
24 _comment
25 _every_flag
26 _category
27
28 '#It has very upfield-shifted H5', H5" @ 3.935,4.012 ppm'
29 ;
30 #######################
31 # BOGUS #
32 #######################
33
34 ;
35
36 BOGUS_CATEGORY
37
38 stop_
39 save_
40 """
41 self.assertFalse(self.strf.parse(text=text))
42 st = self.strf.star_text()
43
44
45 exp = """data_no_comments_here
46 save_comment _Saveframe_category comment loop_
47 _comment
48 _every_flag
49 _category
50 ;
51 #It has very upfield-shifted H5', H5" @ 3.935,4.012 ppm
52 ;
53 ;
54 #######################
55 # BOGUS #
56 #######################
57
58 ; BOGUS_CATEGORY stop_ save_
59 """
60 self.assertTrue(Utils.equalIgnoringWhiteSpace(exp, st))
61
65
66 """
67 Extra Test Routine going over some entries in the NMR Restraints Grid
68 """
69 -def testEntry(entry):
70 print("Testing Entry")
71 strf = File()
72 __init__.verbosity = 2
73
74 stage = "2-parsed"
75
76 urlLocation = ("http://www.bmrb.wisc.edu/WebModule/MRGridServlet?"+
77 "block_text_type=%s&file_detail=%s&pdb_id=%s"+
78 "&program=STAR&request_type=archive&subtype=full&type=entry") % (stage, stage, entry)
79 fnamezip = entry+".zip"
80
81 urllib.request.urlretrieve(urlLocation, fnamezip)
82
83 zfobj = zipfile.ZipFile(fnamezip)
84 fname = None
85 for name in zfobj.namelist():
86 if name.endswith('.str'):
87 fname = name
88 orgWattosWrittenFile = entry+"_org.str"
89 pystarlibWrittenFile = entry+"_pystar.str"
90 wattosWrittenFile = entry+"_wattos.str"
91 diffOrgPystarFile = entry+"_diff_org_pystar.str"
92 diffPystarWattosFile = entry+"_diff_pystar_wattos.str"
93 diffOrgWattosWattosFile = entry+"_diff_org_wattos_wattos.str"
94
95 outfile = open(orgWattosWrittenFile, 'w')
96 outfile.write(zfobj.read(fname))
97 outfile.close()
98 zfobj.close()
99 strf.filename = orgWattosWrittenFile
100
101 strf.read()
102 strf.filename = pystarlibWrittenFile
103 strf.write()
104
105 if False:
106
107
108 try:
109
110 cmd = "diff --ignore-all-space --ignore-blank-lines %s %s > %s" % ( orgWattosWrittenFile, pystarlibWrittenFile, diffOrgPystarFile)
111 os.system(cmd)
112 if not os.path.exists(diffOrgPystarFile):
113 print("WARNING: failed to diff files: ", orgWattosWrittenFile, pystarlibWrittenFile)
114
115
116 print("DEBUG: rewrite to Java formating for comparison")
117 cmd = "java -Xmx256m Wattos.Star.STARFilter %s %s ." % ( pystarlibWrittenFile, wattosWrittenFile)
118 os.system(cmd)
119 if not os.path.exists(wattosWrittenFile):
120 print("WARNING: failed to rewrite file: " + pystarlibWrittenFile)
121 else:
122
123 cmd = "diff --ignore-all-space --ignore-blank-lines %s %s > %s" % ( pystarlibWrittenFile, wattosWrittenFile, diffPystarWattosFile)
124 os.system(cmd)
125 if not os.path.exists(diffPystarWattosFile):
126 print("WARNING: failed to diff file: ", pystarlibWrittenFile, wattosWrittenFile)
127
128 cmd = "diff --ignore-all-space --ignore-blank-lines %s %s > %s" % ( orgWattosWrittenFile, wattosWrittenFile, diffOrgWattosWattosFile)
129 os.system(cmd)
130 if not os.path.exists(diffOrgWattosWattosFile):
131 print("WARNING: failed to diff file: ", orgWattosWrittenFile, wattosWrittenFile)
132 except KeyboardInterrupt:
133 raise
134 except:
135
136 pass
137
138 try:
139 os.unlink(entry+".zip")
140 os.unlink(orgWattosWrittenFile)
141 os.unlink(pystarlibWrittenFile)
142 except KeyboardInterrupt:
143 raise
144 except:
145 pass
146
147
149 """ No need to test all entries for the unit testing frame work"""
150
151 pdbList = ('1edp')
152 try:
153 from Wattos.Utils import PDBEntryLists
154 print("Imported Wattos.Utils; but it's not essential")
155 pdbList = PDBEntryLists.getBmrbNmrGridEntries()[0:1]
156 except KeyboardInterrupt:
157 raise
158 except:
159 print("Skipping import of Wattos.Utils; it's not needed")
160
161 for entry in pdbList:
162 print(entry)
163 testEntry(entry)
164
165
166
167
168
169
170 """
171 Extra Test Routine going over some entries in the NMR Restraints Grid
172 """
181
182
183 if __name__ == "__main__":
184 testAllEntries()
185
186
187 print("Done with STAR.FileTest")
188