def reset_biana_database(dbname, dbhost, dbuser, dbpassword, dbport=None): """ Deletes all the tables in the specified BIANA database without deleting the database itself. ALERT!!! This action is not reversible! All data in BIANA database will be deleted permanently! "dbname": Biana database name "dbhost" is the machine with the mysql server that holds the biana database (required) "dbuser" is the mysql user (not required in most systems) "dbpassword" is the mysql password (not required in most systems) "dbport" is the mysql port (not required in most systems) """ import BianaDB try: dbaccess = BianaDB.BianaDBaccess(dbname=dbname, dbhost=dbhost, dbuser=dbuser, dbpassword=dbpassword, dbport=dbport) tables = dbaccess.db._get_table_names() dbaccess.db.insert_db_content( sql_query=dbaccess.db._get_drop_sql_query(table_list=tables)) dbaccess.close() except: OutBianaInterface.send_error_notification( "Error while reseting database %s at %s" % (dbname, dbhost), "ERROR MESSAGE") # TO CHECK ERROR MESSAGE return
def delete_biana_database(dbname,dbhost,dbuser,dbpassword,dbport=None): """ Deletes the specified BIANA database. ALERT!!! This action is not reversible! All data in BIANA database will be deleted permanently! "dbname": BIANA database name "dbhost" is the machine with the mysql server that holds the biana database (required) "dbuser" is the mysql user (not required in most systems) "dbpassword" is the mysql password (not required in most systems) "dbport" is the mysql port (not required in most systems) """ import BianaDB try: dbaccess = BianaDB.BianaDBaccess( dbhost = dbhost,dbuser = dbuser, dbpassword = dbpassword, dbport = dbport ) dbaccess.db.insert_db_content( sql_query = "DROP DATABASE IF EXISTS %s" %dbname ) dbaccess.close() OutBianaInterface.send_data("<biana_database_deleted dbname=\"%s\" dbhost=\"%s\"/>" %(dbname,dbhost)) except: OutBianaInterface.send_error_notification("Error while deleting database %s at %s" %(dbname,dbhost),"ERROR MESSAGE") # TO CHECK ERROR MESSAGE return
def delete_biana_database(dbname, dbhost, dbuser, dbpassword, dbport=None): """ Deletes the specified BIANA database. ALERT!!! This action is not reversible! All data in BIANA database will be deleted permanently! "dbname": BIANA database name "dbhost" is the machine with the mysql server that holds the biana database (required) "dbuser" is the mysql user (not required in most systems) "dbpassword" is the mysql password (not required in most systems) "dbport" is the mysql port (not required in most systems) """ import BianaDB try: dbaccess = BianaDB.BianaDBaccess(dbhost=dbhost, dbuser=dbuser, dbpassword=dbpassword, dbport=dbport) dbaccess.db.insert_db_content( sql_query="DROP DATABASE IF EXISTS %s" % dbname) dbaccess.close() OutBianaInterface.send_data( "<biana_database_deleted dbname=\"%s\" dbhost=\"%s\"/>" % (dbname, dbhost)) except: OutBianaInterface.send_error_notification( "Error while deleting database %s at %s" % (dbname, dbhost), "ERROR MESSAGE") # TO CHECK ERROR MESSAGE return
def create_new_session(sessionID, dbname,dbhost,dbuser,dbpassword,unification_protocol,dbport=None): """ Creates a new biana session with the specified ID. A session must be started in a populated biana database using a specified unification protocol. "sessionID" is the identifier for the session. It must be unique! (required) "unification_protocol" """ import BianaObjects.BianaSessionManager as BianaSessionManager if sessionID in available_sessions: OutBianaInterface.send_error_notification("Trying to create two sessions with the same ID","Error in session creation") return try: available_sessions[sessionID] = BianaSessionManager.BianaSessionManager(pSessionID = sessionID, unification_protocol_name = unification_protocol, dbname = dbname, dbhost = dbhost, dbuser = dbuser, dbport = dbport, dbpassword = dbpassword, out_method = OutBianaInterface.send_data) return available_sessions[sessionID] except: OutBianaInterface.send_error_notification("Error in session creation.", traceback.format_exc())
def reset_biana_database(dbname,dbhost,dbuser,dbpassword,dbport=None): """ Deletes all the tables in the specified BIANA database without deleting the database itself. ALERT!!! This action is not reversible! All data in BIANA database will be deleted permanently! "dbname": Biana database name "dbhost" is the machine with the mysql server that holds the biana database (required) "dbuser" is the mysql user (not required in most systems) "dbpassword" is the mysql password (not required in most systems) "dbport" is the mysql port (not required in most systems) """ import BianaDB try: dbaccess = BianaDB.BianaDBaccess( dbname = dbname, dbhost = dbhost,dbuser = dbuser, dbpassword = dbpassword, dbport = dbport ) tables = dbaccess.db._get_table_names() dbaccess.db.insert_db_content( sql_query = dbaccess.db._get_drop_sql_query(table_list = tables) ) dbaccess.close() except: OutBianaInterface.send_error_notification("Error while reseting database %s at %s" %(dbname,dbhost),"ERROR MESSAGE") # TO CHECK ERROR MESSAGE return
def load_session(file_name): """ Loads a saved biana session """ import cPickle infile_fd = open(file_name) session = cPickle.load(infile_fd) if available_sessions.has_key(session.sessionID): OutBianaInterface.send_error_notification("Load session error","Trying to load an existing session (same ID)") else: available_sessions[session.sessionID] = session infile_fd.close() return
def delete_unification_protocol(unification_protocol_name, dbname, dbhost, dbuser, dbpassword, dbport=None): """ Creates a new unification protocol "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) "dbname": Biana database name "dbhost" is the machine with the mysql server that holds the biana database (required) "dbuser" is the mysql user (not required in most systems) "dbpassword" is the mysql password (not required in most systems) "dbport" is the mysql port (not required in most systems) """ import BianaDB OutBianaInterface.send_process_message("Deleting unification protocol") try: dbaccess = BianaDB.BianaDBaccess(dbname=dbname, dbhost=dbhost, dbuser=dbuser, dbpassword=dbpassword, dbport=dbport) dbaccess.drop_unification_protocol( unification_protocol_name=unification_protocol_name) dbaccess.close() except: OutBianaInterface.send_error_notification( "Error while deleting unification protocol", traceback.format_exc()) OutBianaInterface.send_end_process_message() return
def create_biana_database(dbname,dbhost,dbuser,dbpassword,description,dbport=None): """ Creates the BIANA database in the mysql server "dbname": Desired database name. It must not exist a database with the same name in the same database server (required) "dbhost" is the machine with the mysql server that holds the biana database (required) "dbuser" is the mysql user (not required in most systems) "dbpassword" is the mysql password (not required in most systems) "dbport" is the mysql port (not required in most systems) 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" """ OutBianaInterface.send_process_message("Creating new biana database...") import BianaDB try: dbaccess = BianaDB.BianaDBaccess( dbhost = dbhost, dbuser = dbuser, dbpassword = dbpassword, dbport = dbport ) databases_list = dbaccess.db.select_db_content( sql_query = "SHOW DATABASES", answer_mode = "list" ) if dbname in databases_list: 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) ) else: dbaccess.db.insert_db_content( sql_query = "CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci" %dbname ) dbaccess.db.insert_db_content( sql_query = "USE %s" %dbname ) dbaccess.create_database(description=description,ignore_primary_keys=True,dbname=dbname) dbaccess.close() administration.check_database(dbname,dbhost,dbuser,dbpassword,dbport) OutBianaInterface.send_info_message("BIANA Database correctly created. You can start now populating it using \"Parse external databases\" option") except: OutBianaInterface.send_error_notification("Error while creating database %s at %s" %(dbname, dbhost), traceback.format_exc()) OutBianaInterface.send_end_process_message()
def get_unification_protocol_atoms(unification_protocol_name, dbname,dbhost,dbuser,dbpassword,dbport=None): """ Returns unification protocol atom information of given unification protocol """ import BianaDB, BianaObjects.output_utilities try: dbaccess = BianaDB.BianaDBaccess( dbname = dbname, dbhost = dbhost, dbuser = dbuser, dbpassword = dbpassword, dbport = dbport ) unification_protocol_atoms = dbaccess.get_unification_protocol_atoms( unification_protocol_name = unification_protocol_name ) 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" ) ) dbaccess.close() except: OutBianaInterface.send_error_notification("Error while fetching unification protocol information",traceback.format_exc())
def delete_unification_protocol(unification_protocol_name, dbname,dbhost,dbuser,dbpassword,dbport=None): """ Creates a new unification protocol "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) "dbname": Biana database name "dbhost" is the machine with the mysql server that holds the biana database (required) "dbuser" is the mysql user (not required in most systems) "dbpassword" is the mysql password (not required in most systems) "dbport" is the mysql port (not required in most systems) """ import BianaDB OutBianaInterface.send_process_message("Deleting unification protocol") try: dbaccess = BianaDB.BianaDBaccess( dbname = dbname, dbhost = dbhost, dbuser = dbuser, dbpassword = dbpassword, dbport = dbport ) dbaccess.drop_unification_protocol( unification_protocol_name = unification_protocol_name ) dbaccess.close() except: OutBianaInterface.send_error_notification("Error while deleting unification protocol",traceback.format_exc()) OutBianaInterface.send_end_process_message() return
def create_unification_protocol(unification_protocol_name, list_unification_atom_elements, dbname, dbhost, dbuser, dbpassword, dbport=None, unify_self=True): """ Creates a new unification protocol "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) "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"])] "dbname": Biana database name "dbhost" is the machine with the mysql server that holds the biana database (required) "dbuser" is the mysql user (not required in most systems) "dbpassword" is the mysql password (not required in most systems) "dbport" is the mysql port (not required in most systems) "unify_self" Unify the external entities in one database with themselves (default = True) """ import BianaDB, BianaObjects OutBianaInterface.send_process_message( "Creating unification protocol. This process can take long time.") try: # create a temporal session to make all database checkings import BianaObjects.BianaSessionManager as BianaSessionManager import tempfile tfile = tempfile.TemporaryFile('w', bufsize=10000) temp_session = BianaSessionManager.BianaSessionManager( pSessionID="temp_session", unification_protocol_name="No unification", dbname=dbname, dbhost=dbhost, dbuser=dbuser, dbport=dbport, dbpassword=dbpassword, out_method=tfile.write) tfile.close() temp_session.close() del temp_session # 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 #create_new_session("temp_session", dbname,dbhost,dbuser,dbpassword,unification_protocol="No unification",dbport=None) dbaccess = BianaDB.BianaDBaccess(dbname=dbname, dbhost=dbhost, dbuser=dbuser, dbpassword=dbpassword, dbport=dbport) uProtocol = BianaObjects.UnificationProtocol( unification_protocol_name, "x") #in X should go the biana database version for db_ids, attr_list in list_unification_atom_elements: # Add all databases, for adding those databases that are not unified with anything else for current_db in db_ids: uProtocol.add_database(current_db) if len(attr_list) > 0: for current_db1_pos in xrange(len(db_ids)): k = current_db1_pos if unify_self: k += 1 for current_db2_pos in xrange(k): #for current_db2_pos in xrange(current_db1_pos): # before was not unifying the database with itself (emre) uProtocol.add_unification_atom_elements( BianaObjects.UnificationAtomElement( externalDatabaseID_A=db_ids[ current_db1_pos], externalDatabaseID_B=db_ids[ current_db2_pos], externalAttribute=attr_list)) dbaccess.create_new_user_entities(uProtocol) 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." ) #OutBianaInterface.send_data(uProtocol.get_xml()) # TO CHECK WHY IS IT COMMENTED dbaccess.close() except: OutBianaInterface.send_error_notification( "Error while creating unification protocol", traceback.format_exc()) OutBianaInterface.send_end_process_message() return
def create_unification_protocol(unification_protocol_name, list_unification_atom_elements, dbname,dbhost,dbuser,dbpassword,dbport=None): """ Creates a new unification protocol "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) "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"])] "dbname": Biana database name "dbhost" is the machine with the mysql server that holds the biana database (required) "dbuser" is the mysql user (not required in most systems) "dbpassword" is the mysql password (not required in most systems) "dbport" is the mysql port (not required in most systems) """ import BianaDB, BianaObjects OutBianaInterface.send_process_message("Creating unification protocol. This process can take long time.") try: # create a temporal session to make all database checkings import BianaObjects.BianaSessionManager as BianaSessionManager import tempfile tfile = tempfile.TemporaryFile('w', bufsize=10000) temp_session = BianaSessionManager.BianaSessionManager( pSessionID = "temp_session", unification_protocol_name = "No unification", dbname = dbname, dbhost = dbhost, dbuser = dbuser, dbport = dbport, dbpassword = dbpassword, out_method = tfile.write ) tfile.close() temp_session.close() del temp_session # 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 #create_new_session("temp_session", dbname,dbhost,dbuser,dbpassword,unification_protocol="No unification",dbport=None) dbaccess = BianaDB.BianaDBaccess( dbname = dbname, dbhost = dbhost, dbuser = dbuser, dbpassword = dbpassword, dbport = dbport ) uProtocol = BianaObjects.UnificationProtocol(unification_protocol_name, "x") #in X should go the biana database version for db_ids, attr_list in list_unification_atom_elements: # Add all databases, for adding those databases that are not unified with anything else for current_db in db_ids: uProtocol.add_database(current_db) if len(attr_list)>0: for current_db1_pos in xrange(len(db_ids)): for current_db2_pos in xrange(current_db1_pos+1): #for current_db2_pos in xrange(current_db1_pos): # before was not unifying the database with itself (emre) uProtocol.add_unification_atom_elements( BianaObjects.UnificationAtomElement(externalDatabaseID_A=db_ids[current_db1_pos], externalDatabaseID_B=db_ids[current_db2_pos], externalAttribute=attr_list) ) dbaccess.create_new_user_entities(uProtocol) 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.") #OutBianaInterface.send_data(uProtocol.get_xml()) # TO CHECK WHY IS IT COMMENTED dbaccess.close() except: OutBianaInterface.send_error_notification("Error while creating unification protocol",traceback.format_exc()) OutBianaInterface.send_end_process_message() return