Package install :: Package MoSTBioDat :: Package DataBase :: Package ImportData :: Package User2DB :: Module SDF2DB
[hide private]
[frames] | no frames]

Source Code for Module install.MoSTBioDat.DataBase.ImportData.User2DB.SDF2DB

  1  #!/usr/bin/env python 
  2  ################################################### 
  3  # SDF2DB.py                                       # 
  4  #  Import data from SDF file to Ligand database   # 
  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      from MoSTBioDat.DataBase.ImportData.Data2DB.TaBuilder import TaBuilder 
 19      from MoSTBioDat.DataBase.ImportData.Data2DB.SDFile import SDFile 
 20      from MoSTBioDat.Log.MoSTBioDatLog import MoSTBioDatLog 
 21      from MoSTBioDat.DataBase.ImportData.Data2DB.InserTables import InserTables, all 
 22  except ImportError,e: 
 23      print 'Error: %s' %e 
 24      sys.exit(1) 
 25  ########## SDF to DataBase import class ############# 
26 -class SDF2DB(SDFile,TaBuilder,InserTables):
27 """ 28 INPUT: 29 Import data from SDF file to Ligand database 30 INPUT: 31 host - string, host to connect 32 user - string, user to connect as 33 passwd - string, password to use 34 db - string, database to use 35 port - integer, TCP/IP port to connect 36 log - boolean, logging flag 37 unix_socket - string, location of unix_socket to use 38 conv - conversion dictionary, see MySQLdb.converters 39 connect_timeout - number of seconds to wait before the connection attempt fails. 40 compress - if set, compression is enabled 41 named_pipe - if set, a named pipe is used to connect (Windows only) 42 init_command - command which is run once the connection is created 43 read_default_file - file from which default client values are read 44 read_default_group - configuration group to use from the default file 45 cursorclass - class object, used to create cursors (keyword only) 46 use_unicode - if True, text-like columns are returned as unicode objects 47 using the connection's character set. Otherwise, text-like 48 columns are returned as strings. columns are returned as 49 normal strings. Unicode objects will always be encoded to 50 the connection's character set regardless of this setting. 51 charset - if supplied, the connection character set will be changed 52 to this character set (MySQL-4.1 and newer). This implies 53 use_unicode=True. 54 sql_mode - if supplied, the session SQL mode will be changed to this 55 setting (MySQL-4.1 and newer). For more details and legal 56 values, see the MySQL documentation. 57 client_flag - integer, flags to use or 0 58 (see MySQL docs or constants/CLIENTS.py) 59 ssl - dictionary or mapping, contains SSL connection parameters; 60 see the MySQL documentation for more details 61 (mysql_ssl_set()). If this is set, and the client does not 62 support SSL, NotSupportedError will be raised. 63 local_infile - integer, non-zero enables LOAD LOCAL INFILE; zero disables 64 format - string format for log handler 65 filter - filter object from logger object 66 datefmt - data/time format 67 path - directory path to log file 68 filename - log filename, default log 69 filemode - mode to open log file, default='a' 70 level - set root logger level to specified level 71 logfilelevel- set level to log file 72 cache - create cache for query, default=True 73 scheme2file - boolean - save database scheme to shelve file 74 75 dictpath - str - path to dictionary 76 dictfilename - str - dictionary filename 77 dbfile - str - database file path 78 temporary - str - path to temporary dictionary file 79 log - log flag, default=False 80 81 OUTPUT: 82 class object 83 OUTPUT: 84 import to database 85 """
86 - def __init__(self,sdfile=None,host='localhost',db='Ligand',user=None,passwd=None,port=3306,log=False,**kwargs):
87 TaBuilder.__init__(self,host,db,user,passwd,port,log,**kwargs) 88 self.logobj.rmLogHandler()#remove logging handler 89 temp=self.kwargs#temporary for storing self.kwargs 90 SDFile.__init__(self,sdfile=sdfile,**kwargs) 91 self.kwargs.update(temp)#merge kwargs together 92 InserTables.__init__(self)
93
94 - def SDict2Tab(self,logdebug=False,baseImport=False,lowercasetablenames=True,**kwarg):
95 """ 96 import data from shelve dictionary to database tables 97 INPUT: 98 class object 99 baseImport - flag to avoid data importing to ChemComp table, default=False 100 lowercasetablenames - boolean, lower case table names MySQL engine settings, default True 101 OUTPUT: 102 importing data to database 103 """ 104 if self.status=='Disconnected': 105 print 'Error: Please reconnect to database! Connection has been closed by previous function.' 106 if logdebug: 107 self.log.error('Please reconnect to database. Connection has been closed by previous function.') 108 sys.exit(1) 109 print 'Importing %s table description.' %self.kwargs['db'] 110 self.tabdesc=self.genTables() 111 self.parseSDFOE(**kwarg) 112 try: 113 self.logobj=MoSTBioDatLog(**self.kwargs)#create logging object 114 self.log=self.logobj.getLogHandler()#create logging handler 115 except IOError,e: 116 print 'Error: %s, %s' %(e[0],e[1]) 117 sys.exit(1) 118 print 'Database Data Importing, please wait ....' 119 self.log.info('Database data importing') 120 self.openDict()#open dictionary 121 counter=0 122 123 ElemTabID=self.getElemTab() 124 if not ElemTabID: 125 print 'Error: Table of elements not available!' 126 self.log.error('Table of elements not available') 127 sys.exit(1) 128 129 PropDefID=self.getPropDef()#get property definitions 130 if not PropDefID: 131 print 'Error: Property definition not available!' 132 self.log.error('Incorrect import to PropDef table') 133 sys.exit(1) 134 135 for dictobj in self.retDict().itervalues():#iterate object in dictionary 136 counter+=1 137 print 'Importing compound number: %s'%counter 138 ##import to ChemComp table 139 ChemCompID=self.ChemComp(dictobj,baseImport=baseImport,logdebug=False,lowercasetablenames=lowercasetablenames) 140 if not ChemCompID: 141 continue 142 143 ##import to PropChemCompInt table 144 tablename='PropChemCompInt'#tablename 145 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ChemCompIdFk']#table columname list 146 #mapdict=['autoincrement', 'property{\'tPSA\',\'NRB\',\'HBD\',\'HBA\',\'Charge\'}','IDict', 'ID']# mapping dictionary object attributes 147 mapdict=['autoincrement', 'nbonds','IDict', 'ID']# mapping dictionary object attributes 148 PropChemCompIDInt=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ChemCompID) 149 150 ##import to PropChemCompFloat table 151 tablename='PropChemCompFloat'#table name 152 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ChemCompIdFk']#table columname list 153 #mapdict=['autoincrement', 'property{\'LogP\',\'Desolv_apolar\',\'MWT\',\'Desolv_polar\'}','IDict', 'ID']# mapping dictionary object attributes 154 mapdict=['autoincrement', 'natoms','IDict', 'ID']# mapping dictionary object attributes 155 PropChemCompIDFloat=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=True,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ChemCompID) 156 157 ##import to PropChemCompText table 158 tablename='PropChemCompText'#tablename 159 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ChemCompIdFk']#table columname list 160 #mapdict=['autoincrement', 'property{\'SMILES\'}','IDict', 'ID']# mapping dictionary object attributes 161 mapdict=['autoincrement', 'cansmi','IDict', 'ID']# mapping dictionary object attributes 162 PropChemCompIDText=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=True,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ChemCompID) 163 164 ##import to ProtStat table 165 ProtStatID=self.ProtStat(dictobj,logdebug=False,lowercasetablenames=lowercasetablenames,ID=ChemCompID) 166 if not ProtStatID: 167 continue 168 169 #import to PropProtStatInt table 170 tablename='PropProtStatInt'#tablename 171 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ProtStatIdFk']#table columname list 172 #mapdict=['autoincrement', 'property{\'tPSA\',\'NRB\',\'HBD\',\'HBA\',\'Charge\'}','IDict', 'ID']# mapping dictionary object attributes 173 mapdict=['autoincrement', 'natoms','IDict', 'ID']# mapping dictionary object attributes 174 PropProtStatIDInt=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ProtStatID) 175 176 ##import to PropProtStatFloat table 177 tablename='PropProtStatFloat'#tablename 178 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ProtStatIdFk']#table columname list 179 #mapdict=['autoincrement', 'property{\'LogP\',\'Desolv_apolar\',\'MWT\',\'Desolv_polar\'}','IDict', 'ID']# mapping dictionary object attributes 180 mapdict=['autoincrement', 'nbonds','IDict', 'ID']# mapping dictionary object attributes 181 PropProtStatIDFloat=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ProtStatID) 182 183 ##import to PropProtStatText table 184 tablename='PropProtStatText'#tablename 185 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ProtStatIdFk']#table columname list 186 #mapdict=['autoincrement', 'property{\'SMILES\'}','IDict', 'ID']# mapping dictionary object attributes 187 mapdict=['autoincrement', 'nbonds','IDict', 'ID']# mapping dictionary object attributes 188 PropProtStatIDText=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ProtStatID) 189 190 #import to ConnTab table 191 ConnTabID=self.ConnTab(dictobj,logdebug=False,lowercasetablenames=lowercasetablenames,ID=ProtStatID) 192 if not ConnTabID: 193 print 'Warning: ConnTab: Import rejected for: %s - %s'%(dictobj.kwargs['filename'],dictobj.kwargs['isosmi']) 194 self.log.warning('ConnTab: Import rejected for: %s - %s',dictobj.kwargs['filename'],dictobj.kwargs['isosmi']) 195 196 ##import to Atoms table 197 AtomsID=self.Atoms(dictobj,logdebug=False,lowercasetablenames=lowercasetablenames,ID=ProtStatID) 198 if not AtomsID: 199 continue 200 201 ##import to ConfStat table 202 ConfStatID=self.ConfStat(dictobj,logdebug=False,lowercasetablenames=lowercasetablenames,ID=ProtStatID) 203 if not ConfStatID: 204 continue 205 206 #import to PropConfStatInt table 207 tablename='PropConfStatInt'#tablename 208 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ConfStatIdFk']#table columname list 209 #mapdict=['autoincrement', 'property{\'Charge\'}','IDict', 'ID']# mapping dictionary object attributes 210 mapdict=['autoincrement', 'nbonds','IDict', 'ID']# mapping dictionary object attributes 211 PropConfStatIDInt=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ConfStatID) 212 213 ##import to PropConfStatInt table 214 tablename='PropConfStatFloat'#tablename 215 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ConfStatIdFk']#table columname list 216 #mapdict=['autoincrement', 'property{\'LogP\'}','IDict', 'ID']# mapping dictionary object attributes 217 mapdict=['autoincrement', 'natoms','IDict', 'ID']# mapping dictionary object attributes 218 PropConfStatIDFloat=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ConfStatID) 219 220 ##import to PropConfStatInt table 221 tablename='PropConfStatText'#tablename 222 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ConfStatIdFk']#table columname list 223 #mapdict=['autoincrement', 'property{\'SMILES\'}','IDict', 'ID']# mapping dictionary object attributes 224 mapdict=['autoincrement', 'natoms','IDict', 'ID']# mapping dictionary object attributes 225 PropConfStatIDText=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ConfStatID) 226 227 #import to AtomStat table 228 AtomStatID=self.AtomStat(dictobj,logdebug=False,lowercasetablenames=lowercasetablenames,ID=ConfStatID,IDlist=AtomsID) 229 if not AtomStatID: 230 continue 231 232 ##import to PropAtomStatInt table 233 tablename='PropAtomStatInt'#tablename 234 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'AtomStatIdFk']#table columname list 235 mapdict=['autoincrement', 'coords[5]:Charge','IDict', 'IDlist']# mapping dictionary object attributes 236 PropProtStatIDInt=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,IDlist=AtomStatID) 237 238 # ##import to PropAtomStatFloat table 239 # tablename='PropAtomStatFloat'#tablename 240 # tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'AtomStatIdFk']#table columname list 241 # mapdict=['autoincrement', 'coords[5]:Charge','IDict', 'IDlist']# mapping dictionary object attributes 242 # PropProtStatIDFloat=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,IDlist=AtomStatID) 243 # 244 # ##import to PropAtomStatText table 245 # tablename='PropAtomStatText'#tablename 246 # tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'AtomStatIdFk']#table columname list 247 # mapdict=['autoincrement', 'coords[5]:Charge','IDict', 'IDlist']# mapping dictionary object attributes 248 # PropProtStatIDText=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,IDlist=AtomStatID) 249 250 self.closeDict()#close shelve dictionary 251 self.closeDB()#close database connection
252
253 - def addConf(self,logdebug=False,baseImport=False,lowercasetablenames=True,**kwarg):
254 """ 255 add conformation from shelve dictionary to database tables 256 INPUT: 257 class object 258 baseImport - flag to avoid data importing to ChemComp table, default=False 259 lowercasetablenames - boolean, lower case table names MySQL engine settings, default True 260 OUTPUT: 261 addition conformer data to database 262 """ 263 if self.status=='Disconnected': 264 print 'Error: Please reconnect to database! Connection has been closed by previous function.' 265 if logdebug: 266 self.log.error('Please reconnect to database. Connection has been closed by previous function.') 267 sys.exit(1) 268 print 'Importing %s table description.' %self.kwargs['db'] 269 self.tabdesc=self.genTables() 270 self.parseSDFOE(**kwarg) 271 try: 272 self.logobj=MoSTBioDatLog(**self.kwargs)#create logging object 273 self.log=self.logobj.getLogHandler()#create logging handler 274 except IOError,e: 275 print 'Error: %s, %s' %(e[0],e[1]) 276 sys.exit(1) 277 print 'Inserting conformer data to database. Please wait ...' 278 self.log.info('Inserting conformer data to database. Please wait ...') 279 self.openDict()#open dictionary 280 counter=0 281 282 ElemTabID=self.getElemTab() 283 if not ElemTabID: 284 print 'Error: Table of elements not available!' 285 self.log.error('Table of elements not available') 286 sys.exit(1) 287 288 PropDefID=self.getPropDef()#get property definitions 289 if not PropDefID: 290 print 'Error: Property definition not available!' 291 self.log.error('Property definition not available') 292 sys.exit(1) 293 294 for dictobj in self.retDict().itervalues():#iterate object in dictionary 295 counter+=1 296 print 'Inserting conformer number: %s'%counter 297 ProtStatIDict={} 298 299 if dictobj.hasAttr('isosmi'):#check if shelve dictionary has isomeric SMILE 300 isosmi=dictobj.kwargs['isosmi']#get isomeric SMILE 301 if not (dictobj.kwargs['coords'] and dictobj.kwargs['bonds']):#check if sheleve dictionary has coords and bonds list 302 print 'Error %s: Coordinates or bonds not available!'%isosmi 303 self.log.error('%s: Coordinates or bonds not available!',isosmi) 304 sys.exit(1) 305 moltitle=dictobj.kwargs['filename']#get molecule title 306 ### check if isomeric smile exists in ProtStat table ### 307 chemcompidfk=self.getProtStat(logdebug=logdebug,tabcolvaldict={'IsoSmi':isosmi},lowercasetablenames=lowercasetablenames,iso=False) 308 if not chemcompidfk: 309 if moltitle: 310 print 'Error: Cannot add conformation for %s, molid: %s! Specified compound not available in database. Please, use SDFImport function.'%(isosmi,moltitle) 311 self.log.error('Cannot add conformation for %s, molid: %s! Specified compound not available in database. Please, use SDFImport function.',isosmi,moltitle) 312 else: 313 print 'Error: Cannot add conformation for %s! Specified compound not available in database. Please, use SDFImport function.'%isosmi 314 self.log.error('Cannot add conformation for %s! Specified compound not available in database. Please, use SDFImport function.',isosmi) 315 sys.exit(1) 316 ### get Id for specified foreign key from ProtStat table ### 317 protstatid=self.getProtStatonIdFk(logdebug=logdebug,tabcolvaldict={'ChemCompIdFk':chemcompidfk},lowercasetablenames=lowercasetablenames) 318 if protstatid:# if Id exist in ProtStat table 319 for protstatval in protstatid.values():#iterate dictionary values 320 ### get Id for specified foreign key from Atoms table ### 321 atomsid=self.getAtomsonIdFk(logdebug=logdebug,tabcolvaldict={'ProtStatIdFk':protstatval},lowercasetablenames=lowercasetablenames) 322 conntabidict=self.getConnTabonIdFk(logdebug=logdebug,tabcolvaldict={'ProtStatIdFk':protstatval},lowercasetablenames=lowercasetablenames) 323 if atomsid:#if Id exists in Atoms table 324 protstatval.append(atomsid)#add list 325 if conntabidict:#if Id exists in ConnTab table 326 conntabkey=sorted(conntabidict.keys())#sort Id from ConnTab table 327 conntablist=[conntabidict[item] for item in conntabkey]#sorted list 328 protstatval.append(conntablist) 329 ProtStatIDict.update(protstatid)#update dictionary 330 if not ProtStatIDict.has_key(isosmi): 331 if moltitle: 332 print 'Error: Unknown molecule code for %s, molid: %s!' %(isosmi,moltitle) 333 self.log.error('Unknown molecule code for %s, molid: %s',isosmi,moltitle) 334 else: 335 print 'Error: Unknown molecule code for %s!' %isosmi 336 self.log.error('Unknown molecule code for %s',isosmi) 337 sys.exit(1) 338 protstatid=[ProtStatIDict[isosmi][0]]#get Ligand.ProtStat foreign key 339 dbatlist=ProtStatIDict[isosmi][1]#get Ligand.Atoms list [[atomsidfk,elemidfk],...] 340 coords=dictobj.kwargs['coords']#get molecule coordinate list 341 molatlist=[[coorditem[0],coorditem[4],coorditem[5]]for coorditem in coords] 342 if len(molatlist)!=len(dbatlist):#compare length of atom list 343 if moltitle: 344 print 'Error: Different number of atoms in %s, molid: %s.' %(isosmi,moltitle) 345 self.log.error('Different number of atoms in %s, molid: %s.',isosmi,moltitle) 346 else: 347 print 'Error: Different number of atoms in %s.' %(isosmi) 348 self.log.error('Different number of atoms in %s',isosmi) 349 sys.exit(1) 350 conntablist=ProtStatIDict[isosmi][2]#get Ligand.ConnTab list [[begat,endat,bondtype],...] 351 bonds=dictobj.kwargs['bonds']#get molecule bond list 352 if len(conntablist)!=len(bonds):#compare length of bond list 353 if moltitle: 354 print 'Error: Different number of bonds in %s, molid: %s.' %(isosmi,moltitle) 355 self.log.error('Different number of bonds in %s, molid: %s.',isosmi,moltitle) 356 else: 357 print 'Error: Different number of bonds in %s.' %(isosmi) 358 self.log.error('Different number of bonds in %s',isosmi) 359 sys.exit(1) 360 ### compare molecule atom list with Ligand.Atoms list ### 361 if not all(map(lambda item: item[0][1]==item[1][1],zip(dbatlist,molatlist))): 362 if moltitle: 363 print 'Error: Different atoms list in %s, molid: %s.' %(isosmi,moltitle) 364 self.log.error('Different atoms list in %s, molid: %s',isosmi,moltitle) 365 else: 366 print 'Error: Different atoms list in %s.' %(isosmi) 367 self.log.error('Different atoms list in %s',isosmi) 368 sys.exit(1) 369 AtomsID=[atomitem[0] for atomitem in dbatlist] 370 ### compare molecule bond list wiht Ligand.ConnTab list ### 371 if not all(map(lambda item: (item[0][0]==item[1][0] and item[0][1]==item[1][1] and item[0][2]==item[1][2]),zip(conntablist,bonds))): 372 if moltitle: 373 print 'Error: Different bonds list in %s, molid: %s.' %(isosmi,moltitle) 374 self.log.error('Different bonds list in %s, molid: %s',isosmi,moltitle) 375 else: 376 print 'Error: Different bonds list in %s.' %(isosmi) 377 self.log.error('Different bonds list in %s',isosmi) 378 sys.exit(1) 379 ### import to ConfStat table ### 380 ProtStatID=[long(ID) for ID in protstatid] 381 ConfStatID=self.ConfStat(dictobj,logdebug=logdebug,lowercasetablenames=lowercasetablenames,ID=ProtStatID) 382 if not ConfStatID: 383 print 'Error: Ligand ConfStat foreign key for %s not available!'%(isosmi) 384 if logdebug: 385 self.log.debug('%s: Ligand ConfStat foreign key for %s not available',isosmi) 386 sys.exit(1) 387 ### import to AtomStat table ### 388 AtomsID=[long(ID) for ID in AtomsID] 389 AtomStatID=self.AtomStat(dictobj,lowercasetablenames=lowercasetablenames,ID=ConfStatID,IDlist=AtomsID) 390 if not AtomStatID: 391 print 'Error: Ligand AtomStat foreign key for %s not available!'%(isosmi) 392 if logdebug: 393 self.log.debug('%s: Ligand AtomStat foreign key for %s not available',isosmi) 394 sys.exit(1) 395 ##import to PropAtomStatFloat table 396 tablename='PropAtomStatFloat'#tablename 397 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'AtomStatIdFk']#table columname list 398 mapdict=['autoincrement', 'coords[5]:Charge','IDict', 'IDlist']# mapping dictionary object attributes 399 PropProtStatIDFloat=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,IDlist=AtomStatID) 400 print 'Conformer Id: %s' %ConfStatID 401 self.closeDict()#close shelve dictionary 402 self.closeDB()#close database connection
403
404 - def isTabDesCorr(self,tablename,tabcol):
405 """ 406 check table description with table name 407 INPUT: 408 tablename- name of table 409 tabcol - list of column names 410 OUTPUT: 411 boolean 412 """ 413 try: 414 colname=self.tabdesc[tablename].getColName()#get list of columns 415 except KeyError,error: 416 print 'Error: Missing description for %s!' %error 417 self.log.exception('%s: Missing description for %s',tablename,error) 418 return False 419 if len(colname)<len(tabcol):#check number of arguments 420 print 'Error: Too many arguments in table column list!!' 421 self.log.error('%s: Too many arguments in table column list',tablename) 422 return False 423 else: 424 for name in tabcol: 425 if name in colname:#check if key in column list 426 continue 427 else: 428 print 'Error: Incorrect column name for %s!'%name 429 self.log.error('%s: Incorrect column name for %s',tablename,name) 430 return False 431 return True
432
433 -class User2DB(TaBuilder,InserTables):
434 """ 435 INPUT: 436 User data import to Ligand database 437 INPUT: 438 host - string, host to connect 439 user - string, user to connect as 440 passwd - string, password to use 441 db - string, database to use 442 port - integer, TCP/IP port to connect 443 log - boolen, logging flag 444 unix_socket - string, location of unix_socket to use 445 conv - conversion dictionary, see MySQLdb.converters 446 connect_timeout - number of seconds to wait before the connection attempt fails. 447 compress - if set, compression is enabled 448 named_pipe - if set, a named pipe is used to connect (Windows only) 449 init_command - command which is run once the connection is created 450 read_default_file - file from which default client values are read 451 read_default_group - configuration group to use from the default file 452 cursorclass - class object, used to create cursors (keyword only) 453 use_unicode - if True, text-like columns are returned as unicode objects 454 using the connection's character set. Otherwise, text-like 455 columns are returned as strings. columns are returned as 456 normal strings. Unicode objects will always be encoded to 457 the connection's character set regardless of this setting. 458 charset - if supplied, the connection character set will be changed 459 to this character set (MySQL-4.1 and newer). This implies 460 use_unicode=True. 461 sql_mode - if supplied, the session SQL mode will be changed to this 462 setting (MySQL-4.1 and newer). For more details and legal 463 values, see the MySQL documentation. 464 client_flag - integer, flags to use or 0 465 (see MySQL docs or constants/CLIENTS.py) 466 ssl - dictionary or mapping, contains SSL connection parameters; 467 see the MySQL documentation for more details 468 (mysql_ssl_set()). If this is set, and the client does not 469 support SSL, NotSupportedError will be raised. 470 local_infile - integer, non-zero enables LOAD LOCAL INFILE; zero disables 471 format - string format for log handler 472 filter - filter object from logger object 473 datefmt - data/time format 474 path - directory path to log file 475 filename - log filename, default log 476 filemode - mode to open log file, default='a' 477 level - set root logger level to specified level 478 logfilelevel- set level to log file 479 cache - create cache for query, default=True 480 scheme2file - boolean - save database scheme to shelve file 481 """ 482
483 - def __init__(self,host='localhost',db='Ligand',user=None,passwd=None,port=3306,log=False,**kwargs):
484 TaBuilder.__init__(self,host,db,user,passwd,port,log,**kwargs) 485 self.tabdesc=self.genTables() 486 InserTables.__init__(self)
487
488 - def User2Tab(self):
489 """ 490 user table import 491 INPUT: 492 class object 493 OUTPUT: 494 string 495 """ 496 print 'Hash out suitable function!'
497 # ## Insert into ChemComp table 498 # ChemCompID=self.UserChemComp(logdebug=False,lowercasetablenames=True,tabcolvaldict={'Id':1,'IsoSmi':'C[C@H]1C(C(=NC(=C)C1C(=O)OC)C)C(=O)OCCSc2ccccc2','ZINCode':'1253'}) 499 # print ChemCompID 500 # ChemCompID=self.UserChemComp(logdebug=False,lowercasetablenames=True,tabcolvaldict={'IsoSmi':'CC(=O)[O-]'}) 501 # print ChemCompID 502 ### Insert into ProtStat table 503 ### User specify directly foreign key 504 # ProtStatId=self.UserProtStat(logdebug=False,lowercasetablenames=True,tabcolvaldict={'Id':2,'IsoSmi':'C[C@H]1C(C(=NC(=C)C1C(=O)OC)C)C(=O)OCCSc2ccccc2','ChemCompIdFk':'1'}) 505 # print ProtStatId 506 # ProtStatId=self.UserProtStat(logdebug=False,lowercasetablenames=True,tabcolvaldict={'IsoSmi':'CC(=O)[O-]'},addH=False) 507 # print ProtStatId 508 ### Get foreign key automatically 509 # ProtStatId=self.UserProtStat(logdebug=False,lowercasetablenames=True,tabcolvaldict={'Id':2,'IsoSmi':'C[C@H]1C(C(=NC(=C)C1C(=O)OC)C)C(=O)OCCSc2ccccc2'}) 510 # print ProtStatId 511 # ProtStatId=self.UserProtStat(logdebug=False,lowercasetablenames=True,tabcolvaldict={'Id':2,'IsoSmi':'CC'}) 512 # print ProtStatId 513 ### insert into PropDef table 514 # PropDefId=self.UserPropDef(logdebug=False,lowercasetablenames=True,tabcolvaldict={'PropName':'nbonds','PropDesc':'Number of bonds'}) 515 # print PropDefId 516 # PropDefId=self.UserPropDef(logdebug=False,lowercasetablenames=True,tabcolvaldict={'PropName':'natoms','PropDesc':'Number of atoms'}) 517 # print PropDefId 518 # tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ChemCompIdFk']#table columname list 519 # tablename='PropChemCompInt' 520 # propval={'nbonds':'3','natoms':'4'} 521 # smi='CC[C@@]12CCC[N@H+]3[C@@H]1c4c(c5ccccc5n4C(=C2)C(=O)OCC)CC3' 522 # PropChemCompIDInt=self.UserProp2Tab(tablename,tabcol,smi,propval,logdebug=False,lowercasetablenames=True) 523 # print PropChemCompIDInt 524 # self.closeDB()#close database connection 525
526 - def isTabDesCorr(self,tablename,tabcol):
527 """ 528 check table description with table name 529 INPUT: 530 tablename- name of table 531 tabcol - list of column names 532 OUTPUT: 533 boolean 534 """ 535 try: 536 colname=self.tabdesc[tablename].getColName()#get list of columns 537 except KeyError,error: 538 print 'Error: Missing description for %s!' %error 539 self.log.exception('%s: Missing description for %s',tablename,error) 540 return False 541 if len(colname)<len(tabcol):#check number of arguments 542 print 'Error: Too many arguments in table column list!!' 543 self.log.error('%s: Too many arguments in table column list',tablename) 544 return False 545 else: 546 for name in tabcol: 547 if name in colname:#check if key in column list 548 continue 549 else: 550 print 'Error: Incorrect column name for %s!'%name 551 self.log.error('%s: Incorrect column name for %s',tablename,name) 552 return False 553 return True
554 555 ################## End of class ######################################################## 556 ############## MAIN ################################################################ 557 ############ Example of usage ########################################################## 558 if __name__=='__main__': 559 pass 560 print '*** Import data from User dictionary *** ' 561 # ############ Import from SDF file ################################# 562 # sdfilename="/tmp/testFile/db.sdf" 563 # dictionarypath='/tmp/SDict' #path to shelve dictionary object 564 # indictfilename='SDict2DBStdInp' 565 # A=SDF2DB(host='',db='',user='',passwd='', 566 # sdfile=sdfilename,dictpath=dictionarypath,dictfilename=indictfilename,path='/tmp/Log',filename='sdf2db', 567 # natoms=True) 568 # A.SDict2Tab(baseImport=True,lowercasetablenames=True) 569 # 570 ## Add conformation from SDF file ### 571 # sdfilename="/tmp/testFile/addconfdb.sdf" 572 # dictionarypath='/tmp/SDict' #path to shelve dictionary object 573 # indictfilename='addconf' 574 # A=SDF2DB(host='',db='',user='',passwd='', 575 # sdfile=sdfilename,dictpath=dictionarypath,dictfilename=indictfilename,path='/tmp/Log',filename='sdf2db', 576 # natoms=True) 577 # A.addConf() 578 # ######################################################## 579 # A=User2DB(host='',db='',user='',passwd='',path='/tmp/Log',filename='user2db') 580 # A.User2Tab() 581