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

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

  1  #!/usr/bin/env python 
  2  #################################################### 
  3  # SDict22DB.py                                     # 
  4  # Import shelve dictionary into DataBase or File   # 
  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 anydbm 
 21      import numpy 
 22      from MoSTBioDat.DataBase.ImportData.Data2DB.TaBuilder import TaBuilder 
 23      from MoSTBioDat.DataBase.ImportData.Data2DB.DBFile import DBFile 
 24      from MoSTBioDat.DataBase.ImportData.Data2DB.InserTables import InserTables 
 25  except ImportError,e: 
 26      print 'Error: %s' %e 
 27      sys.exit(1) 
 28  ####### Import Shelve Dictionary to DataBase class #### 
29 -class SDict2DB(DBFile,TaBuilder,InserTables):
30 """ 31 Import data from shelve dictionary to database 32 INPUT: 33 host - string, host to connect 34 user - string, user to connect as 35 passwd - string, password to use 36 db - string, database to use 37 port - integer, TCP/IP port to connect 38 log - boolean, logging flag 39 unix_socket - string, location of unix_socket to use 40 conv - conversion dictionary, see MySQLdb.converters 41 connect_timeout - number of seconds to wait before the connection attempt fails. 42 compress - if set, compression is enabled 43 named_pipe - if set, a named pipe is used to connect (Windows only) 44 init_command - command which is run once the connection is created 45 read_default_file - file from which default client values are read 46 read_default_group - configuration group to use from the default file 47 cursorclass - class object, used to create cursors (keyword only) 48 use_unicode - if True, text-like columns are returned as unicode objects 49 using the connection's character set. Otherwise, text-like 50 columns are returned as strings. columns are returned as 51 normal strings. Unicode objects will always be encoded to 52 the connection's character set regardless of this setting. 53 charset - if supplied, the connection character set will be changed 54 to this character set (MySQL-4.1 and newer). This implies 55 use_unicode=True. 56 sql_mode - if supplied, the session SQL mode will be changed to this 57 setting (MySQL-4.1 and newer). For more details and legal 58 values, see the MySQL documentation. 59 client_flag - integer, flags to use or 0 60 (see MySQL docs or constants/CLIENTS.py) 61 ssl - dictionary or mapping, contains SSL connection parameters; 62 see the MySQL documentation for more details 63 (mysql_ssl_set()). If this is set, and the client does not 64 support SSL, NotSupportedError will be raised. 65 local_infile - integer, non-zero enables LOAD LOCAL INFILE; zero disables 66 format - string format for log handler 67 filter - filter object from logger object 68 datefmt - data/time format 69 path - directory path to log file 70 filename - log filename, default log 71 filemode - mode to open log file, default='a' 72 level - set root logger level to specified level 73 logfilelevel- set level to log file 74 cache - create cache for query, default=True 75 scheme2file - boolean - save database scheme to shelve file 76 dictpath - str - path to dictionary 77 dictfilename - str - dictionary filename 78 dbfile - str - database file path 79 temporary - str - path to temporary dictionary file 80 log - log flag, default=False 81 82 OUTPUT: 83 class object 84 """ 85
86 - def __init__(self,dbfilepath=None,host='localhost',db='Ligand',user=None,passwd=None,port=3306,log=False,**kwargs):
87 DBFile.__init__(self,dbfile=dbfilepath,dictpath=os.path.dirname(dbfilepath),dictfilename=os.path.basename(dbfilepath),**kwargs) 88 TaBuilder.__init__(self,host,db,user,passwd,port,log,**kwargs) 89 InserTables.__init__(self)
90
91 - def SDict2Tab(self,baseImport=False,lowercasetablenames=True):
92 """ 93 import data from shelve dictionary to database tables 94 INPUT: 95 class object 96 baseImport - flag to avoid data importing to ChemComp table, default=False 97 lowercasetablenames - boolean, lower case table names MySQL engine settings, default True 98 OUTPUT: 99 importing data to database 100 """ 101 print 'Database Data Importing, please wait ....' 102 self.tabdesc=self.genTables() 103 self.log.info('Database data importing from %s',self.dictfullfilename) 104 105 ### import to ElemTab table ### 106 if lowercasetablenames: 107 name='propdef' 108 else: 109 name='PropDef' 110 propdefdict={name:[ 111 ['nbonds','number of bonds'], 112 ['natoms','number of atoms'], 113 ['Desolv_apolar',''], 114 ['Desolv_polar',''], 115 ['LogP',''], 116 ['MWT','molecular weight'], 117 ['tPSA', ''], 118 ['Charge','molecular charge'], 119 ['NRB', 'number of rotatory bonds'], 120 ['SMILES','smile code'], 121 ['ZINC_ID','ZINC database id number'], 122 ['HBA','hydrogen bond acceptor'], 123 ['HBD','hydrogen bond donor'], 124 ['absmi','absolute SMILE'], 125 ['cansmi','canonical SMILE'], 126 ['PUBCHEM_COMPOUND_CANONICALIZED',''], 127 ['PUBCHEM_ATOM_DEF_STEREO_COUNT',''], 128 ['PUBCHEM_MOLECULAR_FORMULA',''], 129 ['PUBCHEM_ISOTOPIC_ATOM_COUNT',''], 130 ['PUBCHEM_COMPOUND_CID',''], 131 ['PUBCHEM_OPENEYE_ISO_SMILES',''], 132 ['PUBCHEM_OPENEYE_CAN_SMILES',''], 133 ['PUBCHEM_CACTVS_COMPLEXITY',''], 134 ['PUBCHEM_BOND_DEF_STEREO_COUNT',''], 135 ['PUBCHEM_CACTVS_HBOND_DONOR',''], 136 ['PUBCHEM_IUPAC_OPENEYE_NAME',''], 137 ['PUBCHEM_ATOM_UDEF_STEREO_COUNT',''], 138 ['PUBCHEM_IUPAC_TRADITIONAL_NAME',''], 139 ['PUBCHEM_EXACT_MASS',''], 140 ['PUBCHEM_IUPAC_NAME',''], 141 ['PUBCHEM_MOLECULAR_WEIGHT',''], 142 ['PUBCHEM_CACTVS_TAUTO_COUNT',''], 143 ['PUBCHEM_CACTVS_HBOND_ACCEPTOR',''], 144 ['PUBCHEM_CACTVS_ROTATABLE_BOND',''], 145 ['PUBCHEM_TOTAL_CHARGE',''], 146 ['PUBCHEM_IUPAC_CAS_NAME',''], 147 ['PUBCHEM_MONOISOTOPIC_WEIGHT',''], 148 ['PUBCHEM_HEAVY_ATOM_COUNT',''], 149 ['PUBCHEM_BOND_UDEF_STEREO_COUNT',''], 150 ['PUBCHEM_CACTVS_SUBSKEYS',''], 151 ['PUBCHEM_IUPAC_SYSTEMATIC_NAME',''], 152 ['PUBCHEM_CACTVS_TPSA',''], 153 ['PUBCHEM_NIST_INCHI',''], 154 ['PUBCHEM_BONDANNOTATIONS',''], 155 ['PUBCHEM_CACTVS_XLOGP',''], 156 ['PUBCHEM_COMPONENT_COUNT',''], 157 ['DRUGBANK_ID',''], 158 ['DRUGBANK_GENERIC_NAME',''], 159 ['DRUGBANK_MOLECULAR_FORMULA',''], 160 ['DRUGBANK_MOLECULAR_WEIGHT',''], 161 ['DRUGBANK_EXACT_MASS',''], 162 ['DRUGBANK_IUPAC_NAME',''], 163 ['DRUGBANK_INCHI',''], 164 ['Generic_Name','DrugBank tag'], 165 ['Synonyms','DrugBank tag'], 166 ['Brand_Names','DrugBank tag'], 167 ['Description','DrugBank tag'], 168 ['Chemical_IUPAC_Name','DrugBank tag'], 169 ['Chemical_Formula','DrugBank tag'], 170 ['CAS_Registry_Number','DrugBank tag'], 171 ['InChI_Identifier','DrugBank tag'], 172 ['KEGG_Compound_ID','DrugBank tag'], 173 ['KEGG_Drug_ID','DrugBank tag'], 174 ['PubChem_Substance_ID','DrugBank tag'], 175 ['PubChem_Compound_ID','DrugBank tag'], 176 ['ChEBI_ID','DrugBank tag'], 177 ['PharmGKB_ID','DrugBank tag'], 178 ['SwissProt_ID','DrugBank tag'], 179 ['SwissProt_Name','DrugBank tag'], 180 ['HET_ID','DrugBank tag'], 181 ['GenBank_ID','DrugBank tag'], 182 ['DPD_Drug_ID_Number','DrugBank tag'], 183 ['RxList_Link','DrugBank tag'], 184 ['Wikipedia_Link','DrugBank tag'], 185 ['Synthesis_Reference','DrugBank tag'], 186 ['Molecular_Weight_Avg','DrugBank tag'], 187 ['Molecular_Weight_Mono','DrugBank tag'], 188 ['State','DrugBank tag'], 189 ['Melting_Point','DrugBank tag'], 190 ['Experimental_Water_Solubility','DrugBank tag'], 191 ['Predicted_Water_Solubility','DrugBank tag'], 192 ['Experimental_LogP_Hydrophobicity','DrugBank tag'], 193 ['Predicted_LogP_Hydrophobicity','DrugBank tag'], 194 ['Experimental_Logs','DrugBank tag'], 195 ['Experimental_Caco2_Permeability','DrugBank tag'], 196 ['pKa_Isoelectric_Point','DrugBank tag'], 197 ['PDB_Experimental_ID','DrugBank tag'], 198 ['PDB_Homology_ID','DrugBank tag'], 199 ['Smiles_String_isomeric','DrugBank tag'], 200 ['Smiles_String_canonical','DrugBank tag'], 201 ['Drug_Type','DrugBank tag'], 202 ['Drug_Category','DrugBank tag'], 203 ['ATC_Codes','DrugBank tag'], 204 ['AHFS_Codes','DrugBank tag'], 205 ['Indication','DrugBank tag'], 206 ['Pharmacology','DrugBank tag'], 207 ['Mechanism_Of_Action','DrugBank tag'], 208 ['Toxicity','DrugBank tag'], 209 ['Protein_Binding','DrugBank tag'], 210 ['Biotransformation','DrugBank tag'], 211 ['Half_Life','DrugBank tag'], 212 ['Dosage_Forms','DrugBank tag'], 213 ['Drug_Interactions','DrugBank tag'], 214 ['Food_Interactions','DrugBank tag'], 215 ['Organisms_Affected','DrugBank tag'], 216 ['Phase_1_Metabolizing_Enzyme','DrugBank tag'], 217 ['Phase_1_Metabolizing_Enzyme_Sequence','DrugBank tag'], 218 ['Phase_1_Enzyme_SwissProt_ID','DrugBank tag'], 219 ['FORMULA','Ligand_info tag'], 220 ['WEIGHT','Ligand_info tag'], 221 ['NAME','Ligand_info tag'], 222 ['Drug','Ligand_info tag'] 223 ]} 224 PropDefID=self.PropDef(lowercasetablenames=lowercasetablenames,**propdefdict) 225 if not PropDefID: 226 print 'Error: Incorrect import to PropDef table' 227 self.log.error('Incorrect import to PropDef table') 228 sys.exit(1) 229 230 ### import to ElemTab table ### 231 elemtabdict={'ElemTab':[ 232 ['1', 'Hydrogen', 'H', '1.0079'], 233 ['2', 'Helium', 'He', '4.0026'], 234 ['3', 'Lithium', 'Li', '6.941'], 235 ['4', 'Beryllium', 'Be', '9.0122'], 236 ['5', 'Boron', 'B', '10.811'], 237 ['6', 'Carbon', 'C', '12.0107'], 238 ['7', 'Nitrogen', 'N', '14.0067'], 239 ['8', 'Oxygen', 'O', '15.9994'], 240 ['9', 'Fluorine', 'F', '18.9984'], 241 ['10', 'Neon', 'Ne', '20.1797'], 242 ['11', 'Sodium', 'Na', '22.9897'], 243 ['12', 'Magnesium', 'Mg', '24.305'], 244 ['13', 'Aluminum', 'Al', '26.9815'], 245 ['14', 'Silicon', 'Si', '28.0855'], 246 ['15', 'Phosphorus', 'P', '30.9738'], 247 ['16', 'Sulfur', 'S', '32.065'], 248 ['17', 'Chlorine', 'Cl', '35.453'], 249 ['18', 'Argon', 'Ar', '39.948'], 250 ['19', 'Potassium', 'K', '39.0983'], 251 ['20', 'Calcium', 'Ca', '40.078'], 252 ['21', 'Scandium', 'Sc', '44.9559'], 253 ['22', 'Titanium', 'Ti', '47.867'], 254 ['23', 'Vanadium', 'V', '50.9415'], 255 ['24', 'Chromium', 'Cr', '51.9961'], 256 ['25', 'Manganese', 'Mn', '54.938'], 257 ['26', 'Iron', 'Fe', '55.845'], 258 ['27', 'Cobalt', 'Co', '58.9332'], 259 ['28', 'Nickel', 'Ni', '58.6934'], 260 ['29', 'Copper', 'Cu', '63.546'], 261 ['30', 'Zinc', 'Zn', '65.39'], 262 ['31', 'Gallium', 'Ga', '69.723'], 263 ['32', 'Germanium', 'Ge', '72.64'], 264 ['33', 'Arsenic', 'As', '74.9216'], 265 ['34', 'Selenium', 'Se', '78.96'], 266 ['35', 'Bromine', 'Br', '79.904'], 267 ['36', 'Krypton', 'Kr', '83.8'], 268 ['37', 'Rubidium', 'Rb', '85.4678'], 269 ['38', 'Strontium', 'Sr', '87.62'], 270 ['39', 'Yttrium', 'Y', '88.9059'], 271 ['40', 'Zirconium', 'Zr', '91.224'], 272 ['41', 'Niobium', 'Nb', '92.9064'], 273 ['42', 'Molybdenum', 'Mo', '95.94'], 274 ['43', 'Technetium', 'Tc', '98'], 275 ['44', 'Ruthenium', 'Ru', '101.07'], 276 ['45', 'Rhodium', 'Rh', '102.9055'], 277 ['46', 'Palladium', 'Pd', '106.42'], 278 ['47', 'Silver', 'Ag', '107.8682'], 279 ['48', 'Cadmium', 'Cd', '112.411'], 280 ['49', 'Indium', 'In', '114.818'], 281 ['50', 'Tin', 'Sn', '118.71'], 282 ['51', 'Antimony', 'Sb', '121.76'], 283 ['52', 'Tellurium', 'Te', '127.6'], 284 ['53', 'Iodine', 'I', '126.9045'], 285 ['54', 'Xenon', 'Xe', '131.293'], 286 ['55', 'Cesium', 'Cs', '132.9055'], 287 ['56', 'Barium', 'Ba', '137.327'], 288 ['57', 'Lanthanum', 'La', '138.9055'], 289 ['58', 'Cerium', 'Ce', '140.116'], 290 ['59', 'Praseodymium', 'Pr', '140.9077'], 291 ['60', 'Neodymium', 'Nd', '144.24'], 292 ['61', 'Promethium', 'Pm', '145'], 293 ['62', 'Samarium', 'Sm', '150.36'], 294 ['63', 'Europium', 'Eu', '151.964'], 295 ['64', 'Gadolinium', 'Gd', '157.25'], 296 ['65', 'Terbium', 'Tb', '158.9253'], 297 ['66', 'Dysprosium', 'Dy', '162.5'], 298 ['67', 'Holmium', 'Ho', '164.9303'], 299 ['68', 'Erbium', 'Er', '167.259'], 300 ['69', 'Thulium', 'Tm', '168.9342'], 301 ['70', 'Ytterbium', 'Yb', '173.04'], 302 ['71', 'Lutetium', 'Lu', '174.967'], 303 ['72', 'Hafnium', 'Hf', '178.49'], 304 ['73', 'Tantalum', 'Ta', '180.9479'], 305 ['74', 'Tungsten', 'W', '183.84'], 306 ['75', 'Rhenium', 'Re', '186.207'], 307 ['76', 'Osmium', 'Os', '190.23'], 308 ['77', 'Iridium', 'Ir', '192.217'], 309 ['78', 'Platinum', 'Pt', '195.078'], 310 ['79', 'Gold', 'Au', '196.9665'], 311 ['80', 'Mercury', 'Hg', '200.59'], 312 ['81', 'Thallium', 'Tl', '204.3833'], 313 ['82', 'Lead', 'Pb', '207.2'], 314 ['83', 'Bismuth', 'Bi', '208.9804'], 315 ['84', 'Polonium', 'Po', '209'], 316 ['85', 'Astatine', 'At', '210'], 317 ['86', 'Radon', 'Rn', '222'], 318 ['87', 'Francium', 'Fr', '223'], 319 ['88', 'Radium', 'Ra', '226'], 320 ['89', 'Actinium', 'Ac', '227'], 321 ['90', 'Thorium', 'Th', '232.0381'], 322 ['91', 'Protactinium', 'Pa', '231.0359'], 323 ['92', 'Uranium', 'U', '238.0289'], 324 ['93', 'Neptunium', 'Np', '237'], 325 ['94', 'Plutonium', 'Pu', '244'], 326 ['95', 'Americium', 'Am', '243'], 327 ['96', 'Curium', 'Cm', '247'], 328 ['97', 'Berkelium', 'Bk', '247'], 329 ['98', 'Californium', 'Cf',' 251'], 330 ['99', 'Einsteinium', 'Es', '252'], 331 ['100', 'Fermium', 'Fm', '257'], 332 ['101', 'Mendelevium', 'Md', '258'], 333 ['102', 'Nobelium', 'No', '259'], 334 ['103', 'Lawrencium', 'Lr', '262'], 335 ['104', 'Rutherfordium', 'Rf', '261'], 336 ['105', 'Dubnium', 'Db', '262'], 337 ['106', 'Seaborgium', 'Sg', '266'], 338 ['107', 'Bohrium', 'Bh', '264'], 339 ['108', 'Hassium', 'Hs', '277'], 340 ['109', 'Meitnerium', 'Mt', '268'], 341 ['110', 'Darmstadtium', 'Ds', '269'], 342 ['111', 'Roentgenium', 'Rg', '272'] 343 ]} 344 345 ElemTabID=self.ElemTab(lowercasetablenames=lowercasetablenames,**elemtabdict) 346 if not ElemTabID: 347 print 'Error: Incorrect import to ElemTab table' 348 self.log.error('Incorect import to ElemTab table') 349 sys.exit(1) 350 351 ## ITERATE SHELVE DICTIONARY 352 self.openDict()#open dictionary 353 counter=0 354 chemcompcounter=0 355 protstatcounter=0 356 conntabcounter=0 357 atomscounter=0 358 confstatcounter=0 359 atomstatcounter=0 360 for dictobj in self.retDict().itervalues():#iterate object in dictionary 361 counter+=1 362 print 'Importing compound number: %s'%counter 363 ## import to ChemComp table ### 364 ChemCompID=self.ChemComp(dictobj,baseImport=baseImport,logdebug=False,lowercasetablenames=lowercasetablenames) 365 if not ChemCompID: 366 #self.log.error('ChemComp: Import rejected for: %s - %s',dictobj.kwargs['filename'],dictobj.kwargs['isosmi']) 367 continue 368 if isinstance(ChemCompID[0],long): 369 chemcompcounter+=1 370 ### import to PropChemCompInt table ### 371 tablename='PropChemCompInt'#tablename 372 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ChemCompIdFk']#table columname list 373 mapdict=['autoincrement', 'property{\'tPSA\',\'NRB\',\'HBD\',\'HBA\',\'Charge\'}','IDict', 'ID']# mapping dictionary object attributes 374 PropChemCompIDInt=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ChemCompID) 375 376 ### import to PropChemCompFloat table ### 377 tablename='PropChemCompFloat'#table name 378 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ChemCompIdFk']#table columname list 379 mapdict=['autoincrement', 'property{\'LogP\',\'Desolv_apolar\',\'MWT\',\'Desolv_polar\'}','IDict', 'ID']# mapping dictionary object attributes 380 PropChemCompIDFloat=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ChemCompID) 381 382 ### import to PropChemCompText table ### 383 tablename='PropChemCompText'#tablename 384 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ChemCompIdFk']#table columname list 385 mapdict=['autoincrement', 'property{\'SMILES\'}','IDict', 'ID']# mapping dictionary object attributes 386 PropChemCompIDText=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ChemCompID) 387 388 ### import to ProtStat table ### 389 ProtStatID=self.ProtStat(dictobj=dictobj,logdebug=False,lowercasetablenames=lowercasetablenames,ID=ChemCompID) 390 if not ProtStatID: 391 #self.log.error('ProtStat: Import rejected for: %s - %s',dictobj.kwargs['filename'],dictobj.kwargs['isosmi']) 392 continue 393 if isinstance(ProtStatID[0],long): 394 protstatcounter+=1 395 396 ### import to PropProtStatInt table ### 397 tablename='PropProtStatInt'#tablename 398 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ProtStatIdFk']#table columname list 399 mapdict=['autoincrement', 'property{\'tPSA\',\'NRB\',\'HBD\',\'HBA\',\'Charge\'}','IDict', 'ID']# mapping dictionary object attributes 400 PropProtStatIDInt=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ProtStatID) 401 402 ### import to PropProtStatFloat table ### 403 tablename='PropProtStatFloat'#tablename 404 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ProtStatIdFk']#table columname list 405 mapdict=['autoincrement', 'property{\'LogP\',\'Desolv_apolar\',\'MWT\',\'Desolv_polar\'}','IDict', 'ID']# mapping dictionary object attributes 406 PropProtStatIDFloat=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ProtStatID) 407 408 ### import to PropProtStatText table ### 409 tablename='PropProtStatText'#tablename 410 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ProtStatIdFk']#table columname list 411 mapdict=['autoincrement', 'property{\'SMILES\',\'PUBCHEM_MOLECULAR_FORMULA\',\'DRUGBANK_ID\',\'FORMULA\',\'Synonyms\'}','IDict', 'ID']# mapping dictionary object attributes 412 PropProtStatIDText=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ProtStatID) 413 414 ### import to ConnTab table ### 415 ConnTabID=self.ConnTab(dictobj,logdebug=False,lowercasetablenames=lowercasetablenames,ID=ProtStatID) 416 if not ConnTabID: 417 self.log.warning('ConnTab: Import rejected for: %s - %s',dictobj.kwargs['filename'],dictobj.kwargs['isosmi']) 418 if ConnTabID: 419 if isinstance(ConnTabID[0],long): 420 conntabcounter+=1 421 422 ### import to Atoms table ### 423 AtomsID=self.Atoms(dictobj,logdebug=False,lowercasetablenames=lowercasetablenames,ID=ProtStatID) 424 if not AtomsID: 425 #self.log.error('Atoms: Import rejected for: %s - %s',dictobj.kwargs['filename'],dictobj.kwargs['isosmi']) 426 continue 427 if isinstance(AtomsID[0],long): 428 atomscounter+=1 429 430 ### import to ConfStat table ### 431 ConfStatID=self.ConfStat(dictobj,logdebug=False,lowercasetablenames=lowercasetablenames,ID=ProtStatID) 432 if not ConfStatID: 433 #self.log.error('ConfStat: Import rejected for: %s - %s',dictobj.kwargs['filename'],dictobj.kwargs['isosmi']) 434 continue 435 if isinstance(ConfStatID[0],long): 436 confstatcounter+=1 437 438 ### import to PropConfStatInt table ### 439 tablename='PropConfStatInt'#tablename 440 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ConfStatIdFk']#table columname list 441 mapdict=['autoincrement', 'property{\'Charge\'}','IDict', 'ID']# mapping dictionary object attributes 442 PropConfStatIDInt=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ConfStatID) 443 444 ### import to PropConfStatInt table ### 445 tablename='PropConfStatFloat'#tablename 446 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ConfStatIdFk']#table columname list 447 mapdict=['autoincrement', 'property{\'LogP\'}','IDict', 'ID']# mapping dictionary object attributes 448 PropConfStatIDFloat=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ConfStatID) 449 450 ### import to PropConfStatInt table ### 451 tablename='PropConfStatText'#tablename 452 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'ConfStatIdFk']#table columname list 453 mapdict=['autoincrement', 'property{\'SMILES\'}','IDict', 'ID']# mapping dictionary object attributes 454 PropConfStatIDText=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,ID=ConfStatID) 455 456 ### import to AtomStat table ### 457 AtomStatID=self.AtomStat(dictobj,lowercasetablenames=lowercasetablenames,ID=ConfStatID,IDlist=AtomsID) 458 if not AtomStatID: 459 #self.log.error('AtomStat: Import rejected for: %s - %s',dictobj.kwargs['filename'],dictobj.kwargs['isosmi']) 460 continue 461 if isinstance(AtomStatID[0],long): 462 atomstatcounter+=1 463 464 ##import to PropAtomStatInt table 465 tablename='PropAtomStatInt'#tablename 466 tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'AtomStatIdFk']#table columname list 467 mapdict=['autoincrement', 'coords[5]:Charge','IDict', 'IDlist']# mapping dictionary object attributes 468 PropProtStatIDInt=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,IDlist=AtomStatID) 469 470 # ### import to PropAtomStatFloat table ### 471 # tablename='PropAtomStatFloat'#tablename 472 # tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'AtomStatIdFk']#table columname list 473 # mapdict=['autoincrement', 'coords[5]:Charge','IDict', 'IDlist']# mapping dictionary object attributes 474 # PropProtStatIDFloat=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,IDlist=AtomStatID) 475 # 476 # ### import to PropAtomStatText table ### 477 # tablename='PropAtomStatText'#tablename 478 # tabcol= ['Id', 'PropVal', 'PropDefIdFk', 'AtomStatIdFk']#table columname list 479 # mapdict=['autoincrement', 'coords[5]:Charge','IDict', 'IDlist']# mapping dictionary object attributes 480 # PropProtStatIDText=self.Prop2Tab(tablename,tabcol,mapdict,dictobj,anyflag=True,logdebug=False,lowercasetablenames=lowercasetablenames,IDcheck=True,IDict=PropDefID,IDlist=AtomStatID) 481 482 self.log.info('Number of compounds in shelve dictionary: %s',counter) 483 self.log.info('Imported to database tables:') 484 self.log.info('ChemComp: %s', chemcompcounter) 485 self.log.info('ProtStat: %s', protstatcounter) 486 self.log.info('ConnTab: %s', conntabcounter) 487 self.log.info('Atoms: %s', atomscounter) 488 self.log.info('ConfStat: %s', confstatcounter) 489 self.log.info('AtomStat: %s', atomstatcounter) 490 self.closeDict()#close shelve dictionary 491 self.closeDB()#close database connection
492
493 - def isTabDesCorr(self,tablename,tabcol):
494 """ 495 check table description with table name 496 INPUT: 497 tablename- name of table 498 tabcol - list of column names 499 OUTPUT: 500 boolean 501 """ 502 try: 503 colname=self.tabdesc[tablename].getColName()#get list of columns 504 except KeyError,error: 505 print 'Error, %s: Missing description for %s!' %(tablename,error) 506 self.log.exception('%s: Missing description for %s',tablename,error) 507 return False 508 if len(colname)<len(tabcol):#check number of arguments 509 print 'Error, %s: Too many arguments in table column list!!'%tablename 510 self.log.error('%s: Too many arguments in table column list',tablename) 511 return False 512 else: 513 for name in tabcol: 514 if name in colname:#check if key in column list 515 continue 516 else: 517 print 'Error, %s: Incorrect column name for %s!'%(tablename,name) 518 self.log.error('%s: Incorrect column name for %s',tablename,name) 519 return False 520 return True
521 522 ################## End of class ######################################################### 523 ############## MAIN ################################################################ 524 ############ Example of usage ########################################################## 525 if __name__=='__main__': 526 pass 527 print '*** Import data from ZINC shelve dictionary *** ' 528 ############ BASE IMPORT ############################################################ 529 # A=SDict2DB(dbfilepath='/tmp/SDict/SDict2DBStdInp', 530 # path='/tmp/Log',filename='sdict2dbligand', 531 # host='localhost',db='',user='',passwd='',log=False) 532 # A.SDict2Tab(baseImport=True) 533 ############ EXTENDED IMPORT ######################################################## 534 # A=SDict2DB(dbfilepath='/tmp/SDict/SDict2DBExtInp0', 535 # path='/tmp/Log',filename='sdict2dbligand', 536 # host='localhost',db='',user='',passwd='',log=False) 537 # A.SDict2Tab(baseImport=False) 538 ######################################################## 539