Package biana :: Module biana_commands'
[hide private]
[frames] | no frames]

Source Code for Module biana.biana_commands'

  1  """ 
  2      BIANA: Biologic Interactions and Network Analysis 
  3      Copyright (C) 2009  Javier Garcia-Garcia, Emre Guney, Baldo Oliva 
  4   
  5      This program is free software: you can redistribute it and/or modify 
  6      it under the terms of the GNU General Public License as published by 
  7      the Free Software Foundation, either version 3 of the License, or 
  8      (at your option) any later version. 
  9   
 10      This program is distributed in the hope that it will be useful, 
 11      but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13      GNU General Public License for more details. 
 14   
 15      You should have received a copy of the GNU General Public License 
 16      along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 17   
 18  """ 
 19   
 20  import traceback 
 21  import sys 
 22  from OutBianaInterface import OutBianaInterface 
 23   
 24  # Change prompt 
 25  sys.ps1 = "BIANA> " 
 26   
 27  available_sessions = {}    # Dictionary to store all available sessions 
 28   
 29  ##################################### 
 30  ## DATABASE ADMINISTRATION METHODS ## 
 31  ##################################### 
 32   
33 -class administration:
34
35 - def create_biana_database(dbname,dbhost,dbuser,dbpassword,description,dbport=None):
36 """ 37 Creates the BIANA database in the mysql server 38 39 "dbname": Desired database name. It must not exist a database with the same name in the same database server (required) 40 41 "dbhost" is the machine with the mysql server that holds the biana database (required) 42 43 "dbuser" is the mysql user (not required in most systems) 44 45 "dbpassword" is the mysql password (not required in most systems) 46 47 "dbport" is the mysql port (not required in most systems) 48 49 In order to make parsers faster, it creates tables without indices and addicional checkings. Biana automatically enable indices when necessary. If user wants to enable indices manually, it is necessary to use the method "enable_indices" 50 """ 51 52 OutBianaInterface.send_process_message("Creating new biana database...") 53 54 import BianaDB 55 56 try: 57 dbaccess = BianaDB.BianaDBaccess( dbhost = dbhost, 58 dbuser = dbuser, 59 dbpassword = dbpassword, 60 dbport = dbport ) 61 databases_list = dbaccess.db.select_db_content( sql_query = "SHOW DATABASES", answer_mode = "list" ) 62 63 if dbname in databases_list: 64 OutBianaInterface.send_error_notification("ERROR: Duplicated database name %s at %s" %(dbname, dbhost), "Database %s exists at %s. If it is a biana database, import it by using the \"Add existing database\" option" %(dbname, dbhost) ) 65 66 else: 67 dbaccess.db.insert_db_content( sql_query = "CREATE DATABASE IF NOT EXISTS %s" %dbname ) 68 dbaccess.db.insert_db_content( sql_query = "USE %s" %dbname ) 69 dbaccess.create_database(description=description,ignore_primary_keys=True,dbname=dbname) 70 dbaccess.close() 71 administration.check_database(dbname,dbhost,dbuser,dbpassword,dbport) 72 OutBianaInterface.send_info_message("BIANA Database correctly created. You can start now populating it using \"Parse external databases\" option") 73 74 except: 75 OutBianaInterface.send_error_notification("Error while creating database %s at %s" %(dbname, dbhost), traceback.format_exc()) 76 77 OutBianaInterface.send_end_process_message()
78 79 create_biana_database = staticmethod(create_biana_database) 80 81
82 - def delete_biana_database(dbname,dbhost,dbuser,dbpassword,dbport=None):
83 """ 84 Deletes the specified BIANA database. 85 86 ALERT!!! This action is not reversible! All data in BIANA database will be deleted permanently! 87 88 "dbname": BIANA database name 89 90 "dbhost" is the machine with the mysql server that holds the biana database (required) 91 92 "dbuser" is the mysql user (not required in most systems) 93 94 "dbpassword" is the mysql password (not required in most systems) 95 96 "dbport" is the mysql port (not required in most systems) 97 """ 98 99 import BianaDB 100 try: 101 dbaccess = BianaDB.BianaDBaccess( dbhost = dbhost,dbuser = dbuser, dbpassword = dbpassword, dbport = dbport ) 102 dbaccess.db.insert_db_content( sql_query = "DROP DATABASE IF EXISTS %s" %dbname ) 103 dbaccess.close() 104 OutBianaInterface.send_data("<biana_database_deleted dbname=\"%s\" dbhost=\"%s\"/>" %(dbname,dbhost)) 105 except: 106 OutBianaInterface.send_error_notification("Error while deleting database %s at %s" %(dbname,dbhost),"ERROR MESSAGE") # TO CHECK ERROR MESSAGE 107 return
108 109 delete_biana_database = staticmethod(delete_biana_database) 110 111
112 - def reset_biana_database(dbname,dbhost,dbuser,dbpassword,dbport=None):
113 """ 114 Deletes all the tables in the specified BIANA database without deleting the database itself. 115 116 ALERT!!! This action is not reversible! All data in BIANA database will be deleted permanently! 117 118 "dbname": Biana database name 119 120 "dbhost" is the machine with the mysql server that holds the biana database (required) 121 122 "dbuser" is the mysql user (not required in most systems) 123 124 "dbpassword" is the mysql password (not required in most systems) 125 126 "dbport" is the mysql port (not required in most systems) 127 """ 128 129 import BianaDB 130 try: 131 dbaccess = BianaDB.BianaDBaccess( dbname = dbname, dbhost = dbhost,dbuser = dbuser, dbpassword = dbpassword, dbport = dbport ) 132 tables = dbaccess.db._get_table_names() 133 dbaccess.db.insert_db_content( sql_query = dbaccess.db._get_drop_sql_query(table_list = tables) ) 134 dbaccess.close() 135 except: 136 OutBianaInterface.send_error_notification("Error while reseting database %s at %s" %(dbname,dbhost),"ERROR MESSAGE") # TO CHECK ERROR MESSAGE 137 return
138 139 reset_biana_database = staticmethod(reset_biana_database) 140 141
142 - def check_database(dbname,dbhost,dbuser,dbpassword,dbport=None):
143 """ 144 Examines the current database and gets its basic information (external databases, unification protocols...) 145 """ 146 147 print "Checking BIANA database %s at %s..." %(dbname, dbhost) 148 149 OutBianaInterface.send_process_message("Checking BIANA database %s at %s..." %(dbname, dbhost) ) 150 151 import BianaDB 152 try: 153 dbaccess = BianaDB.BianaDBaccess( dbname = dbname, 154 dbhost = dbhost, 155 dbuser = dbuser, 156 dbpassword = dbpassword, 157 dbport = dbport ) 158 159 160 if( OutBianaInterface.outmethod is not None ): 161 tstr = [ "<unification_protocol description=\"%s\"/>" %x for x in dbaccess.get_available_unification_protocols_list() ] 162 163 source_databases = dbaccess.get_external_database_list() 164 165 eDstr = [] 166 for current_eD in source_databases: 167 attrStrList = [ "<eDB_external_entity_attribute name=\"%s\"/>" %x for x in current_eD.get_valid_external_entity_attribute_type() ] 168 attrStrList.extend( ["<eDB_external_entity_relation_attribute name=\"%s\"/>" %x for x in current_eD.get_valid_external_entity_relation_attribute_type() ] ) 169 attrStrList.extend( ["<eDB_external_entity_type name=\"%s\"/>" %x for x in current_eD.get_valid_external_entity_type() ] ) 170 attrStrList.extend( ["<eDB_external_entity_relation_type name=\"%s\"/>" %x for x in current_eD.get_valid_external_entity_relation_type() ] ) 171 eDstr.append("<external_database name=\"%s\" version=\"%s\" description=\"%s\" id=\"%s\">%s</external_database>" %(current_eD.get_name(), current_eD.get_version(), current_eD.get_description(), current_eD.get_id(), "".join(attrStrList))) 172 173 ontologies = dbaccess.get_available_ontology_names() 174 ontList = ["<ontology_attribute attribute=\"%s\" ontology_name=\"%s\"/>" %(ontologies[x],x) for x in ontologies ] 175 176 OutBianaInterface.send_data("<db_info dbname=\"%s\" dbhost=\"%s\" dbuser=\"%s\" dbpass=\"%s\">%s%s%s</db_info>" %(dbname,dbhost,dbuser,dbpassword,"\n".join(tstr),"\n".join(eDstr),"\n".join(ontList))) 177 178 dbaccess.close() 179 except: 180 traceback.print_exc() 181 OutBianaInterface.send_data("<not_available_database dbname=\"%s\" dbhost=\"%s\"/>" %(dbname,dbhost)) 182 183 OutBianaInterface.send_end_process_message()
184 185 186 187 check_database = staticmethod(check_database) 188 189
190 - def create_unification_protocol(unification_protocol_name, list_unification_atom_elements, dbname,dbhost,dbuser,dbpassword,dbport=None):
191 """ 192 Creates a new unification protocol 193 194 "unification_protocol_name" is the name of the unification protocol (it can be a string). It cannot contain blank spaces. It must be unique (required) 195 196 "listUnifiationAtomElements" is a list of tuples. Each tuple consists on two elements: the list of database to cross and the list of attributes as for example [([1,2],["uniprotAccession"]),([1],["geneSymbol"])] 197 198 "dbname": Biana database name 199 200 "dbhost" is the machine with the mysql server that holds the biana database (required) 201 202 "dbuser" is the mysql user (not required in most systems) 203 204 "dbpassword" is the mysql password (not required in most systems) 205 206 "dbport" is the mysql port (not required in most systems) 207 """ 208 209 import BianaDB, BianaObjects 210 211 OutBianaInterface.send_process_message("Creating unification protocol. This process can take long time.") 212 213 try: 214 215 # create a temporal session to make all database checkings 216 217 import BianaObjects.BianaSessionManager as BianaSessionManager 218 219 import tempfile 220 221 tfile = tempfile.TemporaryFile('w', bufsize=10000) 222 temp_session = BianaSessionManager.BianaSessionManager( pSessionID = "temp_session", 223 unification_protocol_name = "No unification", 224 dbname = dbname, 225 dbhost = dbhost, 226 dbuser = dbuser, 227 dbport = dbport, 228 dbpassword = dbpassword, 229 out_method = tfile.write ) 230 231 tfile.close() 232 233 temp_session.close() 234 del temp_session 235 236 # Commented because it generated an error in the GUI, as we only allow a session. If a session was started when creating a unification protocol, it generated some conflicts 237 #create_new_session("temp_session", dbname,dbhost,dbuser,dbpassword,unification_protocol="No unification",dbport=None) 238 239 dbaccess = BianaDB.BianaDBaccess( dbname = dbname, 240 dbhost = dbhost, 241 dbuser = dbuser, 242 dbpassword = dbpassword, 243 dbport = dbport ) 244 245 uProtocol = BianaObjects.UnificationProtocol(unification_protocol_name, "x") #in X should go the biana database version 246 247 for db_ids, attr_list in list_unification_atom_elements: 248 # Add all databases, for adding those databases that are not unified with anything else 249 for current_db in db_ids: 250 uProtocol.add_database(current_db) 251 252 if len(attr_list)>0: 253 for current_db1_pos in xrange(len(db_ids)): 254 for current_db2_pos in xrange(current_db1_pos+1): 255 uProtocol.add_unification_atom_elements( BianaObjects.UnificationAtomElement(externalDatabaseID_A=db_ids[current_db1_pos], 256 externalDatabaseID_B=db_ids[current_db2_pos], 257 externalAttribute=attr_list) ) 258 dbaccess.create_new_user_entities(uProtocol) 259 260 OutBianaInterface.send_info_message("New unification protocol successfully created. You can start a working session with it by using \"Create session\" option and selecting database and unification protocol.") 261 262 #OutBianaInterface.send_data(uProtocol.get_xml()) # TO CHECK WHY IS IT COMMENTED 263 264 dbaccess.close() 265 except: 266 OutBianaInterface.send_error_notification("Error while creating unification protocol",traceback.format_exc()) 267 268 OutBianaInterface.send_end_process_message() 269 270 return
271 272 create_unification_protocol = staticmethod(create_unification_protocol) 273 274 275 276 277
278 - def delete_unification_protocol(unification_protocol_name, dbname,dbhost,dbuser,dbpassword,dbport=None):
279 """ 280 Creates a new unification protocol 281 282 "unification_protocol_name" is the name of the unification protocol (it can be a string). It cannot contain blank spaces. It must be unique (required) 283 284 "dbname": Biana database name 285 286 "dbhost" is the machine with the mysql server that holds the biana database (required) 287 288 "dbuser" is the mysql user (not required in most systems) 289 290 "dbpassword" is the mysql password (not required in most systems) 291 292 "dbport" is the mysql port (not required in most systems) 293 """ 294 295 import BianaDB 296 297 OutBianaInterface.send_process_message("Deleting unification protocol") 298 299 try: 300 301 dbaccess = BianaDB.BianaDBaccess( dbname = dbname, 302 dbhost = dbhost, 303 dbuser = dbuser, 304 dbpassword = dbpassword, 305 dbport = dbport ) 306 307 dbaccess.drop_unification_protocol( unification_protocol_name = unification_protocol_name ) 308 309 dbaccess.close() 310 311 except: 312 OutBianaInterface.send_error_notification("Error while deleting unification protocol",traceback.format_exc()) 313 314 OutBianaInterface.send_end_process_message() 315 316 return
317 318 delete_unification_protocol = staticmethod(delete_unification_protocol) 319 320 321
322 - def get_unification_protocol_atoms(unification_protocol_name, dbname,dbhost,dbuser,dbpassword,dbport=None):
323 """ 324 Returns unification protocol atom information of given unification protocol 325 """ 326 import BianaDB, BianaObjects.output_utilities 327 328 try: 329 dbaccess = BianaDB.BianaDBaccess( dbname = dbname, 330 dbhost = dbhost, 331 dbuser = dbuser, 332 dbpassword = dbpassword, 333 dbport = dbport ) 334 335 unification_protocol_atoms = dbaccess.get_unification_protocol_atoms( unification_protocol_name = unification_protocol_name ) 336 OutBianaInterface.send_data( BianaObjects.output_utilities.get_html_table(columns=["External DB 1", "External DB 2", "Crossed Attributes"], values = [(dbaccess.get_external_database(atom.get_externalDatabaseID_A()), dbaccess.get_external_database(atom.get_externalDatabaseID_B()), atom.get_external_attribute_list()) for atom in unification_protocol_atoms ], title="Unification Protocol Details" ) ) 337 338 dbaccess.close() 339 340 except: 341 OutBianaInterface.send_error_notification("Error while fetching unification protocol information",traceback.format_exc())
342 343 get_unification_protocol_atoms = staticmethod(get_unification_protocol_atoms) 344 345 346 347
349 """ 350 Returns the description of available parsers 351 """ 352 353 import BianaParser 354 xml_list = ["<available_parsers>"] 355 xml_list.extend(["<parser name=\"%s\" description=\"%s\" external_entity_definition=\"%s\"/>" %(x[0],x[1],x[2]) for x in BianaParser.get_available_parsers_info()]) 356 xml_list.append("</available_parsers>") 357 OutBianaInterface.send_data("\n".join(xml_list))
358 359 get_available_parsers = staticmethod(get_available_parsers) 360 361 362 363
365 """ 366 Returns all the possible external entity types 367 """ 368 369 #from biana.BianaObjects.ExternalEntity import ExternalEntity 370 #valid_eE_types_list = ExternalEntity.get_valid_external_entity_types() 371 372 import biana.biana_globals as BIANA_GLOBALS 373 valid_eE_types_list = list(BIANA_GLOBALS.EXTERNAL_ENTITY_TYPES) 374 valid_eE_types_list.sort() 375 OutBianaInterface.send_data( "".join( ["<external_entity_type name=\"%s\" />" %x for x in valid_eE_types_list] ) )
376 377 get_available_external_entity_types = staticmethod(get_available_external_entity_types) 378 379 380 381
383 """ 384 Returns all the possible external entity relation types 385 """ 386 import biana.biana_globals as BIANA_GLOBALS 387 valid_eEr_types_list = list(BIANA_GLOBALS.EXTERNAL_ENTITY_RELATION_TYPES) 388 valid_eEr_types_list.sort() 389 OutBianaInterface.send_data( "".join( ["<external_entity_relation_type name=\"%s\" />" %x for x in valid_eEr_types_list] ) )
390 391 get_available_external_entity_relation_types = staticmethod(get_available_external_entity_relation_types) 392 393
395 """ 396 Returns all the possible external entity attributes 397 """ 398 399 import biana.biana_globals as BIANA_GLOBALS 400 OutBianaInterface.send_data( "".join( ["<external_entity_attribute name=\"%s\" />" % current_attribute[0] for current_attribute in BIANA_GLOBALS.EXTERNAL_ENTITY_IDENTIFIER_ATTRIBUTES ] ) ) 401 OutBianaInterface.send_data( "".join( ["<external_entity_attribute name=\"%s\" />" % current_attribute[0] for current_attribute in BIANA_GLOBALS.EXTERNAL_ENTITY_VERSIONABLE_IDENTIFIER_ATTRIBUTE_TYPES]))
402 403 404 get_available_external_entity_attributes = staticmethod(get_available_external_entity_attributes)
405 406 407 408 409 410 411 412 ############################ 413 ## BIANA SESSION METHODS ### 414 ############################ 415
416 -def create_new_session(sessionID, dbname,dbhost,dbuser,dbpassword,unification_protocol,dbport=None):
417 """ 418 Creates a new biana session with the specified ID. A session must be started in a populated biana database using a specified unification protocol. 419 420 "sessionID" is the identifier for the session. It must be unique! (required) 421 422 "unification_protocol" 423 """ 424 425 import BianaObjects.BianaSessionManager as BianaSessionManager 426 427 if sessionID in available_sessions: 428 OutBianaInterface.send_error_notification("Trying to create two sessions with the same ID","Error in session creation") 429 return 430 431 available_sessions[sessionID] = BianaSessionManager.BianaSessionManager(pSessionID = sessionID, 432 unification_protocol_name = unification_protocol, 433 dbname = dbname, 434 dbhost = dbhost, 435 dbuser = dbuser, 436 dbport = dbport, 437 dbpassword = dbpassword, 438 out_method = OutBianaInterface.send_data) 439 440 return available_sessions[sessionID]
441 442
443 -def save_session(sessionID, file_name):
444 """ 445 Saves the Session in the specified file 446 447 "sessionID" is the identifier of the session 448 449 "fileName" is the absolute or relative path where the session must be saved 450 """ 451 import cPickle 452 outfile_fd = open(file_name, 'w') 453 objBianaSessionManager = available_sessions[sessionID] 454 cPickle.dump(objBianaSessionManager, outfile_fd, cPickle.HIGHEST_PROTOCOL) 455 outfile_fd.close() 456 return
457 458
459 -def remove_session(sessionID):
460 """ 461 Removes a session from memory 462 463 "sessionID" is the identifier of the session 464 """ 465 466 available_sessions[sessionID].close() 467 del available_sessions[sessionID] 468 if( sessionID is not None ): 469 OutBianaInterface.send_data("<close_session sessionID=\"%s\"/>" %sessionID) 470 return
471 472 473
474 -def load_session(file_name):
475 """ 476 Loads a saved biana session 477 """ 478 import cPickle 479 infile_fd = open(file_name) 480 session = cPickle.load(infile_fd) 481 if available_sessions.has_key(session.sessionID): 482 OutBianaInterface.send_error_notification("Load session error","Trying to load an existing session (same ID)") 483 else: 484 available_sessions[session.sessionID] = session 485 486 infile_fd.close() 487 return
488
489 -def ping():
490 OutBianaInterface.send_data("<biana_ping_response />")
491
492 -def close():
493 OutBianaInterface.close() 494 sys.exit()
495