# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_download_data.ipynb.

# %% auto 0
__all__ = ['create_dataframe_from_edges']

# %% ../nbs/02_download_data.ipynb 4
import requests
import zipfile
from datetime import datetime
from tqdm import tqdm
import json
from pathlib import Path
import os
from itertools import combinations
import re
import pandas as pd
import ast
import rdflib
import multiprocessing
from joblib import Parallel, delayed
import gzip
import shutil
import cobra
import urllib.request
from collections import defaultdict

from bs4 import BeautifulSoup

# %% ../nbs/02_download_data.ipynb 17
def create_dataframe_from_edges(data):
    """
    Creates a DataFrame from a dictionary of edges, dynamically extracting attributes as columns,
    formatting column names to lowercase with underscores replacing spaces,
    and replacing spaces in all column values with underscores.

    Parameters:
        data (dict): A dictionary containing an 'edges' key with a list of edge dictionaries.

    Returns:
        pd.DataFrame: A DataFrame representing the edges with dynamically extracted attributes.
    """
    # Extract all unique attribute keys from 'attributes'
    attribute_keys = set()
    for item in data.get('edges', []):
        if 'attributes' in item and isinstance(item['attributes'], dict):
            attribute_keys.update(item['attributes'].keys())

    # Normalize attribute keys
    normalized_keys = {key: key.lower().replace(" ", "_") for key in attribute_keys}

    # Create the DataFrame
    df_edges = pd.DataFrame([
        {
            'source': item.get('source'),
            'target': item.get('target'),
            **{normalized_keys[key]: item.get('attributes', {}).get(key) for key in attribute_keys}
        }
        for item in data.get('edges', [])
    ])

    # Normalize column names for source and target
    df_edges.rename(columns=lambda col: col.lower().replace(" ", "_"), inplace=True)

    # Replace spaces with underscores in all column values
    df_edges = df_edges.map(lambda x: x.replace(" ", "_") if isinstance(x, str) else x)

    return df_edges

