Package install :: Package MoSTBioDat :: Package DataBase :: Package ImportData :: Package Data2DB :: Module DBFile
[hide private]
[frames] | no frames]

Source Code for Module install.MoSTBioDat.DataBase.ImportData.Data2DB.DBFile

  1  #!/usr/bin/env python 
  2  ################################################## 
  3  # DBFile.py                                      # 
  4  # Base Database File Class for SDF and SMI files # 
  5  ################################################## 
  6   
  7  ###################################################### 
  8  # Copyright (c) 2007-2008 Andrzej Bak                # 
  9  # ARC Seibersdorf & University of Silesia            # 
 10  # Author: Andrzej Bak <Andrzej.Bak@us.edu.pl>        # 
 11  # License: GNU General Public License, version: 3    # 
 12  # URL: http://chemoinformatyka.us.edu.pl/mostbiodat/ # 
 13  # Version: 1, 06.01.2010                             # 
 14  ###################################################### 
 15   
 16  try: 
 17      import sys 
 18      import os 
 19      import shelve 
 20      import gzip 
 21      import anydbm 
 22  except ImportError,e: 
 23      print 'Error: %s' %e 
 24      sys.exit(1) 
 25  ########## DataBase Base file class ############### 
26 -class DBFile(object):
27 """ 28 Base class for SDF and SMI 29 INPUT: 30 dictpath - str - path to dictionary 31 dictfilename - str - dictionary filename 32 dbfile - str - database file path 33 temporary - str - path to temporary dictionary file 34 OUTPUT: 35 dictionary object 36 """
37 - def __init__(self,dbfile=None,**kwargs):
38 self.kwargs={} 39 self.kwargs=kwargs 40 self.kwargs['dbfile']=dbfile 41 keys=('dictpath','dictfilename','dbfile','temporary','format' 42 'filter','datefmt','path','filename','filemode','level','logfilelevel') 43 44 filter(lambda key: filterKeys(kwargs,keys,key),kwargs.keys())#filter keys 45 if not self.kwargs.has_key('dictpath'): 46 self.kwargs.setdefault('dictpath','/tmp') 47 if not self.kwargs.has_key('dictfilename'): 48 self.kwargs.setdefault('dictfilename','inputDB') 49 if not self.kwargs.has_key('temporary'): 50 self.kwargs.setdefault('temporary','/tmp/InputDB') 51 if self.kwargs['dbfile']: 52 self.findFile() 53 else: 54 sys.exit(1) 55 self.createdict()
56
57 - def findFile(self):
58 """ 59 find and open database file 60 INPUT: 61 class object 62 OUTPUT: 63 file object 64 """ 65 fullfilename=os.path.abspath(self.kwargs['dbfile']) 66 if (os.path.exists(fullfilename) and os.path.isfile(fullfilename)): 67 if os.access(fullfilename,os.R_OK): 68 try: 69 if fullfilename.endswith('.gz'): 70 file=gzip.open(fullfilename,'r') 71 else: 72 file=open(fullfilename,'r') 73 except IOError,error: 74 print 'Error: %s for %s' %(error,fullfilename) 75 else: 76 self.file=file 77 else: 78 print 'Permission denied: %s' %fullfilename 79 sys.exit(1) 80 else: 81 print 'Unable to find %s!' %fullfilename 82 sys.exit(1)
83
84 - def createdict(self):
85 """ 86 create dictionary 87 INPUT: 88 class object 89 OUTPUT: 90 dictionary filename absolute path 91 """ 92 if self.kwargs['dictpath']=='': 93 self.kwargs['dictpath']=os.path.abspath(os.curdir) 94 95 if not os.path.isdir(self.kwargs['dictpath']): 96 try: 97 os.makedirs(self.kwargs['dictpath']) 98 except Exception,error: 99 print 'Error: %s, %s for %s' %(error[0],error[1],self.kwargs['dictpath']) 100 print 'Shelve files in %s' %self.kwargs['temporary']#create directory in temporary path 101 self.kwargs['dictpath']=self.kwargs['temporary'] 102 if not os.path.exists(self.kwargs['dictpath']): 103 os.makedirs(self.kwargs['dictpath']) 104 else: 105 if not os.access(self.kwargs['dictpath'],os.W_OK): 106 print 'Error: Permission denied! Unable to write in %s' %self.kwargs['dictpath'] 107 self.kwargs['dictpath']=self.kwargs['temporary'] 108 if not os.path.exists(self.kwargs['dictpath']): 109 os.makedirs(self.kwargs['dictpath']) 110 elif not os.access(self.kwargs['dictpath'],os.W_OK): 111 print 'Error: Permission denied! Unable to write in %s' %self.kwargs['dictpath'] 112 sys.exit(1) 113 self.dictfullfilename=os.path.join(self.kwargs['dictpath'],self.kwargs['dictfilename']) 114 ###dictionary full filename path 115 print 'Database object dictionary: %s' %self.dictfullfilename
116
117 - def openDict(self):
118 """ 119 open shelve dictionary 120 INPUT: 121 class object 122 OUTPUT: 123 opened class object 124 """ 125 try: 126 self.dbinput=shelve.open(self.dictfullfilename,protocol=-1) 127 except anydbm.error,error: 128 print 'Error: %s, %s' %(error[0],error[1]) 129 sys.exit(1) 130 else: 131 return True
132
133 - def closeDict(self):
134 """ 135 close shelve dictionary 136 INPUT: 137 class object 138 OUTPUT: 139 closed class object 140 """ 141 if self.dbinput: 142 self.dbinput.close()
143
144 - def insertDict(self,key,value):
145 """ 146 insert to shelve dictionary 147 INPUT: 148 class object 149 key - shelve key dictionary 150 value - dictionary value 151 OUTPUT: 152 dictionary value 153 """ 154 self.dbinput[key]=value
155
156 - def getLength(self):
157 """ 158 get dictionary length 159 INPUT: 160 class object 161 OUTPUT: 162 dictionary length 163 """ 164 return len(self.dbinput)
165
166 - def getDict(self,key):
167 """ 168 retrieve value from shelve dictionary 169 INPUT: 170 class object 171 key - shelve key dictionary 172 OUTPUT: 173 dictionary value 174 """ 175 return self.dbinput.get(key,None)
176
177 - def delDictKey(self,key):
178 """ 179 delete dictionary key 180 INPUT: 181 class object 182 key - shelve key dictionary 183 OUTPUT: 184 deleted object 185 """ 186 if self.dbinput.get(key,None): 187 del(self.dbinput[key]) 188 else: 189 print 'No specified object in shelve dictionary!'
190
191 - def retDict(self):
192 """ 193 return dictionary 194 INPUT: 195 class object 196 OUTPUT: 197 shelve dictionary 198 """ 199 return self.dbinput
200
201 - def insAttrDict(self,constattr={}):
202 """ 203 insert attribute to dictionary object 204 INPUT: 205 class object 206 constattr - dictionary - add constant attribute to shelve dictionary object 207 OUTPUT: 208 """ 209 self.openDict() 210 if self.openDict(): 211 if constattr:#if dictionary not empty 212 if isinstance(constattr,dict): 213 for key,value in self.dbinput.iteritems():#iterate shelve dictionary 214 for constkey,constval in constattr.iteritems():#iterate const dictionary 215 if value.kwargs.has_key(constkey):#if key exists 216 continue 217 else:#key not exists 218 value.kwargs[constkey]=constval#add attribute to object 219 self.insertDict(key,value)#insert updated object into shelve dictionary 220 else: 221 print 'Error: Specify attribute dictionary!' 222 self.closeDict() 223 sys.exit(1) 224 else: 225 print 'Error: Define attribute!' 226 self.closeDict() 227 sys.exit(1) 228 else: 229 print 'Error: Can not open shelve dictionary!' 230 sys.exit(1)
231
232 - def delAttrDict(self,attrlist=[]):
233 """ 234 delete attribute from dictionary object 235 INPUT: 236 class object 237 OUTPUT: 238 attrlist - list - list of attribute to delete 239 """ 240 self.openDict() 241 if self.openDict(): 242 if attrlist:#if list not empty 243 if isinstance(attrlist,list): 244 for key,value in self.dbinput.iteritems():#iterate shelve dictionary 245 for attritem in attrlist:#iterate attribute list 246 if value.kwargs.has_key(attritem):#if attribute exists 247 del value.kwargs[attritem]# delete attribute 248 self.insertDict(key,value)#insert updated object into shelve dictionary 249 else:#if attribute not exists 250 continue 251 else: 252 print 'Error: Specify attribute list!' 253 self.closeDict() 254 sys.exit(1) 255 else: 256 print 'Error: Define attribute!' 257 self.closeDict() 258 sys.exit(1) 259 else: 260 print 'Error: Can not open shelve dictionary!' 261 sys.exit(1)
262
263 - def showDict(self):
264 """ 265 show shelve dictionary 266 INPUT: 267 class object 268 OUTPUT: 269 lists dictionary 270 """ 271 self.openDict() 272 if self.openDict(): 273 if len(self.dbinput): 274 for key,value in self.dbinput.iteritems(): 275 if isinstance(value,InputDB): 276 print 'key: %s' %(key) 277 for key1,value1 in value.kwargs.iteritems(): 278 print '%s: %s' %(key1,value1) 279 else: 280 print 'key: %s value: %s'%(key,value) 281 print '- - - - - - - - - - - - - - - - - - - - - - - - - - - ' 282 else: 283 print 'Empty dictionary!' 284 self.closeDict() 285 else: 286 print 'Error: Can not open shelve dictionary!'
287
288 - def showDictKey(self,key):
289 """ 290 show values for specified key in shelve dictionary 291 INUPUT: 292 class object 293 key - shelve key dictionary 294 OUTPUT: 295 class object 296 """ 297 self.openDict() 298 if self.openDict(): 299 if len(self.dbinput): 300 value=self.getDict(key) 301 if value: 302 if isinstance(value,InputDB): 303 print '- - - - - - - - - - - - - - - - - - - - - - - - - - - ' 304 print 'key: %s' %(key) 305 print '- - - - - - - - - - - - - - - - - - - - - - - - - - - ' 306 for key1,value1 in value.kwargs.iteritems(): 307 print '%s: %s' %(key1,value1) 308 else: 309 print 'key: %s value: %s'%(key,value) 310 print '- - - - - - - - - - - - - - - - - - - - - - - - - - - ' 311 else: 312 print 'None' 313 else: 314 print 'Empty dictionary!' 315 self.closeDict() 316 else: 317 print 'Error: Can not open shelve dictionary!'
318
319 - def shoValue(self,key,propname):
320 """ 321 display values for specified key 322 INPUT: 323 key - str, (isomeric SMILE dictionary key) 324 propname - str, property name 325 OUTPUT 326 value - dictionary object 327 """ 328 self.openDict() 329 dictobj=self.getDict(key) 330 self.closeDict() 331 if not dictobj: 332 return None 333 if dictobj.hasAttr(propname): 334 value=dictobj.kwargs[propname] 335 else: 336 print 'No specified property' 337 value=None 338 return value
339
340 - def showPropTag(self,propname,printflag=True,proptagname=None):
341 """ 342 display values for specific property name 343 INPUT: 344 propname- str, property name 345 printflag - boolean, print isomeric SMILES and property values, default True 346 proptagname - str, property tag name 347 OUTPUT: 348 print report 349 """ 350 self.openDict() 351 if self.openDict(): 352 nkeys=0 353 if len(self.dbinput): 354 for key,value in self.dbinput.iteritems(): 355 if isinstance(value,InputDB): 356 for key1,value1 in value.kwargs.iteritems(): 357 if key1==propname: 358 if isinstance(value1,(str,int,long,float,list)): 359 nkeys+=1 360 if printflag: 361 print 'key: %s' %(key) 362 print '%s: %s'%(key1,value1) 363 print '- - - - - - - - - - - - - - - - - - - - - - - - - - - ' 364 elif isinstance(value1,dict): 365 if len(value1): 366 if proptagname: 367 getagvalue=value1.get(proptagname,None) 368 if getagvalue: 369 nkeys+=1 370 if printflag: 371 print 'key: %s' %(key) 372 print '%s: %s: %s' %(key1,proptagname,getagvalue) 373 print '- - - - - - - - - - - - - - - - - - - - - - - - - - - ' 374 else: 375 nkeys+=1 376 if printflag: 377 print 'key: %s' %(key) 378 print '%s: %s'%(key1,value1) 379 print '- - - - - - - - - - - - - - - - - - - - - - - - - - - ' 380 381 else: 382 if printflag: 383 print 'key: %s value: %s'%(key,value) 384 print '- - - - - - - - - - - - - - - - - - - - - - - - - - - ' 385 else: 386 print 'Empty dictionary!' 387 self.closeDict() 388 print 'Number of compounds: %s '%nkeys 389 else: 390 print 'Error: Can not open shelve dictionary!'
391
392 - def showPropTagonZINCode(self,zincode,zinckey='filename',propname=None,proptagname=None):
393 """ 394 display values for specified ZINC code 395 INPUT: 396 zincode - str, database zinc code 397 zinckey - str, zink key name in shelve dictionary 398 propname - str, property name, default None 399 proptagname - str, property tag name, default None 400 OUTPUT: 401 string 402 """ 403 self.openDict() 404 if self.openDict(): 405 nkeys=0 406 if len(self.dbinput): 407 for key,value in self.dbinput.iteritems(): 408 if isinstance(value,InputDB): 409 if value.hasAttr(zinckey): 410 getzincvalue=value.kwargs.get(zinckey,None) 411 if not getzincvalue: 412 continue 413 if getzincvalue==zincode: 414 nkeys+=1 415 print '- - - - - - - - - - - - - - - - - - - - - - - - - - - ' 416 print 'key: %s' %(key) 417 print '- - - - - - - - - - - - - - - - - - - - - - - - - - - ' 418 if propname: 419 getpropvalue=value.kwargs.get(propname,None) 420 if not getpropvalue: 421 print '%s not available!' %propname 422 else: 423 if isinstance(getpropvalue,(str,int,long,float,list)): 424 print '%s: %s'%(propname,getpropvalue) 425 elif isinstance(getpropvalue,dict): 426 if proptagname: 427 proptagvalue=getpropvalue.get(proptagname,None) 428 if proptagvalue: 429 print '%s -> %s: %s' %(propname,proptagname,proptagvalue) 430 else: 431 print '%s -> %s: Not available!' %(propname,proptagname) 432 else: 433 for key1,value1 in getpropvalue.iteritems(): 434 print '%s -> %s: %s'%(propname,key1,value1) 435 else: 436 for key1,value1 in value.kwargs.iteritems(): 437 if isinstance(value1,(str,int,long,float,list)): 438 print '%s: %s'%(key1,value1) 439 elif isinstance(value1,dict): 440 if len(value1): 441 if proptagname: 442 getagvalue=value1.get(proptagname,None) 443 if getagvalue: 444 print '%s: %s: %s' %(key1,proptagname,getagvalue) 445 else: 446 print '%s: %s'%(key1,value1) 447 print '- - - - - - - - - - - - - - - - - - - - - - - - - - - ' 448 else: 449 print 'ZINCode not available' 450 continue 451 else: 452 print 'Empty dictionary!' 453 self.closeDict() 454 print 'Number of %s in shelve dictionary: %s '%(zincode,nkeys) 455 else: 456 print 'Error: Can not open shelve dictionary!'
457 458 ############# End of base class ############### 459 ############# Class InputDB ###################
460 -class InputDB(object):
461 """ 462 Attribute Input Database Object definition 463 INPUT: 464 dictionary of attributes 465 OUTPUT: 466 class object 467 """
468 - def __init__(self,**kwargs):
469 self.kwargs={} 470 self.kwargs=kwargs
471
472 - def hasAttr(self,key):
473 """ 474 Check if object has attribute 475 INPUT: 476 class object 477 key - dictionary key 478 OUTPUT: 479 boolean 480 """ 481 return self.kwargs.has_key(key)
482 483 484 ########### End of class ######################
485 -def filterKeys(dictionary,keys,key):
486 """ 487 check input settings 488 INPUT: 489 dictionary - dictionary object 490 keys - keys list 491 key - dictionary key 492 OUTPUT: 493 boolean 494 """ 495 if key not in keys: 496 del dictionary[key] 497 return False 498 else: 499 return True
500
501 -def rmfile(path):
502 """ 503 remove file 504 INPUT: 505 path - str- file path 506 OUTPUT: 507 removed file 508 """ 509 if os.path.exists(path): 510 try: 511 os.remove(path) 512 print 'Deleting: %s' %path 513 except OSError,error: 514 print 'Error: %s' %error
515 ############ MAIN ############################# 516 ########### example of usage ################## 517 if __name__=='__main__': 518 pass 519