
def insert_disease_gds(conn, df):
    """
    Inserts data from a DataFrame into the 'expr_disease_gds' table in the MySQL database.

    This function takes a DataFrame with 'disease_id' and 'gds_id' columns and inserts the data into
    the 'expr_disease_gds' table within the MySQL database specified by the provided connection.

    Input Parameters:
        conn (mysql.connector.connection_cext.CMySQLConnection): The MySQL database connection object.
        df (pandas.DataFrame): The DataFrame containing the data to be inserted. It should have columns 'disease_id' and 'gds_id'.

    Returns:
        None

    Raises:
        mysql.connector.Error: If there is a MySQL-specific error during the insert operation.
        Exception: For any other exceptions that may occur.
    """
    try:
        # Create a cursor object to execute SQL commands
        with conn.cursor() as cursor:
            for index, row in df.iterrows():
                # Define the SQL INSERT query
                query = """
                INSERT INTO disnet_biolayer.expr_disease_gds (disease_id, gds_id)
                VALUES (%s, %s)
                """
                # Prepare the data to be inserted into the database
                data = (row['disease_id'], row['gds_id'])
                # Execute the SQL query with the data
                cursor.execute(query, data)
            # Commit the transaction to the database
            conn.commit()
    except mysql.connector.Error as err:
        # Handle MySQL-specific errors
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Something is wrong with your user or password")
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print("The database does not exist")
        else:
            print(f"Error from MySQL: {err}")
    except Exception as e:
        # Handle any other exceptions that might occur
        print(f"Error: {e}")
    finally:
        # Ensure the cursor is closed if it was opened
        cursor.close()



















def insert_gpl_main(conn, df):
    """
    Inserts data from a DataFrame into the 'expr_gpl_main' table in the MySQL database.

    This function takes a DataFrame with 'gpl_id' and 'gpl_title' columns and inserts the data into
    the 'expr_gpl_main' table within the MySQL database specified by the provided connection.

    Input Parameters:
        conn (mysql.connector.connection_cext.CMySQLConnection): The MySQL database connection object.
        df (pandas.DataFrame): The DataFrame containing the data to be inserted. It should have columns 'gpl_id' and 'gpl_title'.

    Returns:
        None

    Raises:
        mysql.connector.Error: If there is a MySQL-specific error during the insert operation.
        Exception: For any other exceptions that may occur.
    """
    try:
        # Create a cursor object to execute SQL commands
        with conn.cursor() as cursor:
            for index, row in df.iterrows():
                # Define the SQL INSERT query
                query = """
                INSERT INTO disnet_biolayer.expr_gpl_main (gpl_id, gpl_title)
                VALUES (%s, %s)
                """
                # Prepare the data to be inserted into the database
                data = (row['gpl_id'], row['gpl_title'])
                # Execute the SQL query with the data
                cursor.execute(query, data)
            # Commit the transaction to the database
            conn.commit()
    except mysql.connector.Error as err:
        # Handle MySQL-specific errors
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Something is wrong with your user or password")
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print("The database does not exist")
        else:
            print(f"Error from MySQL: {err}")
    except Exception as e:
        # Handle any other exceptions that might occur
        print(f"Error: {e}")
    finally:
        # Ensure the cursor is closed if it was opened
        cursor.close()







def insert_gds_metadata(conn, df):
    """
    Inserts data from a DataFrame into the 'expr_gds_metadata' table in the MySQL database.

    This function processes each row of the DataFrame and inserts the `gds_id`, `gds_title`, `gds_type`,
    `gpl_id`, `channel_count`, and `value_type` columns into the `expr_gds_metadata` table in the specified
    MySQL database.

    Input Parameters:
        conn (mysql.connector.connection_cext.CMySQLConnection): The MySQL database connection object.
        df (pandas.DataFrame): The DataFrame containing the data to be inserted. It should have the following columns: 'gds_id','gds_title','gds_type','gpl_id','channel_count' and 'value_type'

    Returns:
        None

    Raises:
        mysql.connector.Error: If there is a MySQL-specific error during the insert operation.
        Exception: For any other exceptions that may occur.
    """
    try:
        # Create a cursor object to execute SQL commands
        with conn.cursor() as cursor:
            for index, row in df.iterrows():
                # Define the SQL INSERT query for inserting data into the table
                query = """
                INSERT INTO disnet_biolayer.expr_gds_metadata (gds_id, gds_title, gds_type, gpl_id, channel_count, value_type)
                VALUES (%s, %s, %s, %s, %s, %s)
                """
                # Prepare the data to be inserted into the database
                data = (row['gds_id'], row['gds_title'], row['gds_type'], row['gpl_id'], row['channel_count'], row['value_type'])
                # Execute the SQL query with the data
                cursor.execute(query, data)
            # Commit the transaction to make the changes persistent
            conn.commit()
    except mysql.connector.Error as err:
        # Handle MySQL-specific errors
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Something is wrong with your user or password")
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print("The database does not exist")
        else:
            print(f"Error from MySQL: {err}")
    except Exception as e:
        # Handle any other exceptions that might occur
        print(f"Error: {e}")
    finally:
        # Ensure the cursor is closed if it was opened
        cursor.close()










def insert_raw_annot(conn, df):
    """
    Inserts data from a DataFrame into the 'expr_raw_annot' table in the MySQL database.

    This function takes a DataFrame with various annotation fields and inserts the data into the
    'expr_raw_annot' table within the MySQL database specified by the provided connection.

    Input Parameters:
        conn (mysql.connector.connection_cext.CMySQLConnection): The MySQL database connection object.
        df (pandas.DataFrame): The DataFrame containing the data to be inserted. It should have the following columns: 'gds_id','gsm_id','disease_state','age','cell_type','genotype','individual','gender','infection','other','specimen' and 'development_stage'.

    Returns:
        None

    Raises:
        mysql.connector.Error: If there is a MySQL-specific error during the insert operation.
        Exception: For any other exceptions that may occur.
    """
    try:
        # Create a cursor object to execute SQL commands
        with conn.cursor() as cursor:
            for index, row in df.iterrows():
                # Define the SQL INSERT query for inserting data into the table
                query = """
                INSERT INTO disnet_biolayer.expr_raw_annot (gds_id, gsm_id, disease_state, age, cell_type, genotype, individual, gender, infection, other, specimen, development_stage)
                VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
                """
                # Prepare the data to be inserted into the database
                data = (
                    row['gds_id'],
                    row['gsm_id'],
                    row['disease_state'],
                    row['age'],
                    row['cell_type'],
                    row['genotype'],
                    row['individual'],
                    row['gender'],
                    row['infection'],
                    row['other'],
                    row['specimen'],
                    row['development_stage']
                )
                # Execute the SQL query with the data
                cursor.execute(query, data)
            # Commit the transaction to make the changes persistent
            conn.commit()
    except mysql.connector.Error as err:
        # Handle MySQL-specific errors
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Something is wrong with your user or password")
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print("The database does not exist")
        else:
            print(f"Error from MySQL: {err}")
    except Exception as e:
        # Handle any other exceptions that might occur
        print(f"Error: {e}")
    finally:
        # Ensure the cursor is closed if it was opened
        cursor.close()





def insert_processed_annot(conn, df):
    """
    Inserts data from a DataFrame into the 'expr_processed_annot' table in the MySQL database.

    This function takes a DataFrame with various annotation fields and inserts the data into the
    'expr_processed_annot' table within the MySQL database specified by the provided connection.

    Input Parameters:
        conn (mysql.connector.connection_cext.CMySQLConnection): The MySQL database connection object.
        df (pandas.DataFrame): The DataFrame containing the data to be inserted. It should have the following columns: 'gds_id','gsm_id','disease_state','age','cell_type','genotype','individual','gender','infection','other','specimen' and 'development_stage'.

    Returns:
        None

    Raises:
        mysql.connector.Error: If there is a MySQL-specific error during the insert operation.
        Exception: For any other exceptions that may occur.
    """
    try:
        # Create a cursor object to execute SQL commands
        with conn.cursor() as cursor:
            for index, row in df.iterrows():
                # Define the SQL INSERT query for inserting data into the table
                query = """
                INSERT INTO disnet_biolayer.expr_processed_annot (
                    gds_id, gsm_id, disease_state, age, cell_type, genotype, individual, gender, infection, other, specimen, development_stage, flag
                )
                VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
                """
                # Prepare the data to be inserted into the database
                data = (
                    row['gds_id'],
                    row['gsm_id'],
                    row['disease_state'],
                    row['age'],
                    row['cell_type'],
                    row['genotype'],
                    row['individual'],
                    row['gender'],
                    row['infection'],
                    row['other'],
                    row['specimen'],
                    row['development_stage'],
                    row['flag']
                )
                # Execute the SQL query with the data
                cursor.execute(query, data)
            # Commit the transaction to make the changes persistent
            conn.commit()
    except mysql.connector.Error as err:
        # Handle MySQL-specific errors
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Something is wrong with your user or password")
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print("The database does not exist")
        else:
            print(f"Error from MySQL: {err}")
    except Exception as e:
        # Handle any other exceptions that might occur
        print(f"Error: {e}")
    finally:
        # Ensure the cursor is closed if it was opened
        cursor.close()







def insert_values(conn, df):
    """
    Inserts data from a DataFrame into the 'expr_values' table in the MySQL database.

    This function takes a DataFrame with gene expression data and inserts it into the
    'expr_values' table within the MySQL database specified by the provided connection.

    Input Parameters:
        conn (mysql.connector.connection_cext.CMySQLConnection): The MySQL database connection object.
            It should be established using the `mysql-connector-python` library, and it contains
            the necessary details for connecting to the MySQL server.

        df (pandas.DataFrame): The DataFrame containing the data to be inserted into the database.
            It must have the following columns: 'gds_id', 'gsm_id', 'id_ref', 'gene_symbol' and 'value'

    Returns:
        None
    """
    try:
        # Create a cursor object to execute SQL commands
        with conn.cursor() as cursor:
            # Define the SQL INSERT query for inserting data into the table
            for index, row in df.iterrows():
                query = """
                INSERT INTO disnet_biolayer.expr_values (gds_id, gsm_id, id_ref, gene_symbol, value)
                VALUES (%s, %s, %s, %s,%s)
                """
                # Prepare the data to be inserted into the database
                data = (
                    row['gds_id'],
                    row['gsm_id'],
                    row['id_ref'],
                    row['gene_symbol'],
                    row['value']
                )
                # Execute the SQL query with the data
                cursor.execute(query, data)
                
            # Commit the transaction to make the changes persistent
            conn.commit()

    except mysql.connector.Error as err:
        # Handle MySQL-specific errors
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Something is wrong with your user or password")
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print("The database does not exist")
        else:
            print(f"Error from MySQL: {err}")
    except Exception as e:
        # Handle any other exceptions that might occur
        print(f"Error: {e}")
    finally:
        # Ensure the cursor is closed if it was opened
        cursor.close()











