Package bmrblib :: Package pystarlib :: Module FileTest
[hide private]
[frames] | no frames]

Source Code for Module bmrblib.pystarlib.FileTest

  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   
14 -class AllChecks(TestCase):
15 strf = File(verbosity=2) 16
17 - def testparse(self):
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 # print "unparsed text:[" +st+ "]" 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
62 - def testread2(self):
63 """STAR File read""" 64 testEntry('1edp')
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 # Freely available on the web so not included in package. 74 stage = "2-parsed" 75 # stage = "3-converted-DOCR" 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 # print "DEBUG: downloading url:", urlLocation 81 urllib.request.urlretrieve(urlLocation, fnamezip) 82 # print "DEBUG: opening local zip file:", fnamezip 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 # In order to make the tests below work you'll first need to install Wattos 107 # which is why this test is not standard. 108 try: 109 # print "Most likely the below diff will fail because it depends on diff being installed" 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 # print "Most likely the below check will fail because it depends on Wattos being installed" 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 # print "Most likely the below diff will fail because it depends on diff being installed" 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 # print "Most likely the below diff will fail because it depends on diff being installed" 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: 133 # print "DEBUG: failed the rewrite or diff but as mentioned that's totally understandable." 134 pass 135 136 try: 137 os.unlink(entry+".zip") 138 os.unlink(orgWattosWrittenFile) 139 os.unlink(pystarlibWrittenFile) 140 except: 141 pass
142 143
144 -def testAllEntries():
145 """ No need to test all entries for the unit testing frame work""" 146 # pdbList = ('1edp', '1q56', '1brv', '2hgh') 147 pdbList = ('1edp') 148 try: 149 from Wattos.Utils import PDBEntryLists #@UnresolvedImport 150 print("Imported Wattos.Utils; but it's not essential") 151 pdbList = PDBEntryLists.getBmrbNmrGridEntries()[0:1] # Decide on the range yourself. 152 except: 153 print("Skipping import of Wattos.Utils; it's not needed") 154 155 for entry in pdbList: 156 print(entry) 157 testEntry(entry)
158 # entry = '1edp' # 57 kb 159 # entry = '1q56' # 10 Mb takes 27 s to parse on 2GHz PIV CPU 160 # entry = '1brv' # 1 Mb 161 # entry = '1hue' # 6 Mb takes 26 s to parse on 2GHz PIV CPU 162 # entry = '2ihx' # ? Mb has weird quoted values 163 164 """ 165 Extra Test Routine going over some entries in the NMR Restraints Grid 166 """
167 -def testSingleFile( filename ):
168 strf = File() 169 strf.filename = filename 170 print("DEBUG: reading file ", strf.filename) 171 strf.read() 172 strf.filename = strf.filename + "_new.str" 173 print("DEBUG: writing file ", strf.filename) 174 strf.write()
175 176 177 if __name__ == "__main__": 178 testAllEntries() 179 # testSingleFile("S:\\jurgen\\2hgh_small_new_google.str") 180 # unittest.main() 181 print("Done with STAR.FileTest") 182