{ "cells": [ { "cell_type": "code", "execution_count": 47, "id": "945a82aa-1398-422a-98df-b3db93973271", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
EntryProtein familiesModified residueSequence
0A0A009GHC8Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...
1A0A009HTZ2Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...
2A0A009IVE2Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...
3A0A009MYL5Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...
4A0A009PHM9Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...
\n", "
" ], "text/plain": [ " Entry Protein families \\\n", "0 A0A009GHC8 Precorrin methyltransferase family; Precorrin ... \n", "1 A0A009HTZ2 Precorrin methyltransferase family; Precorrin ... \n", "2 A0A009IVE2 Precorrin methyltransferase family; Precorrin ... \n", "3 A0A009MYL5 Precorrin methyltransferase family; Precorrin ... \n", "4 A0A009PHM9 Precorrin methyltransferase family; Precorrin ... \n", "\n", " Modified residue \\\n", "0 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "1 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "2 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "3 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "4 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "\n", " Sequence \n", "0 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n", "1 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n", "2 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n", "3 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n", "4 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... " ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "# Load the TSV file\n", "file_path = 'PTM/uniprotkb_family_AND_ft_mod_res_AND_pro_2023_10_02.tsv'\n", "data = pd.read_csv(file_path, sep='\\t')\n", "\n", "# Display the first few rows of the data\n", "data.head()\n" ] }, { "cell_type": "code", "execution_count": 48, "id": "a03f8ff8-0612-4f8c-bccd-49fde3dce0f5", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
EntryProtein familiesModified residueSequencePTM sites
0A0A009GHC8Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
1A0A009HTZ2Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2A0A009IVE2Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
3A0A009MYL5Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
4A0A009PHM9Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
\n", "
" ], "text/plain": [ " Entry Protein families \\\n", "0 A0A009GHC8 Precorrin methyltransferase family; Precorrin ... \n", "1 A0A009HTZ2 Precorrin methyltransferase family; Precorrin ... \n", "2 A0A009IVE2 Precorrin methyltransferase family; Precorrin ... \n", "3 A0A009MYL5 Precorrin methyltransferase family; Precorrin ... \n", "4 A0A009PHM9 Precorrin methyltransferase family; Precorrin ... \n", "\n", " Modified residue \\\n", "0 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "1 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "2 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "3 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "4 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "\n", " Sequence \\\n", "0 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n", "1 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n", "2 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n", "3 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n", "4 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n", "\n", " PTM sites \n", "0 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n", "1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n", "2 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n", "3 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n", "4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... " ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import re\n", "\n", "def get_ptm_sites(row):\n", " # Extract the positions of modified residues from the 'Modified residue' column\n", " modified_positions = [int(i) for i in re.findall(r'MOD_RES (\\d+)', row['Modified residue'])]\n", " \n", " # Create a list of zeros of length equal to the protein sequence\n", " ptm_sites = [0] * len(row['Sequence'])\n", " \n", " # Replace the zeros with ones at the positions of modified residues\n", " for position in modified_positions:\n", " # Subtracting 1 because positions are 1-indexed, but lists are 0-indexed\n", " ptm_sites[position - 1] = 1\n", " \n", " return ptm_sites\n", "\n", "# Apply the function to each row in the DataFrame\n", "data['PTM sites'] = data.apply(get_ptm_sites, axis=1)\n", "\n", "# Display the first few rows of the updated DataFrame\n", "data.head()\n" ] }, { "cell_type": "code", "execution_count": 50, "id": "5d2e5043-e2f9-44ec-899b-7dad4f83f823", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
EntryProtein familiesModified residueSequencePTM sites
0A0A009GHC8Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
1A0A009HTZ2Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2A0A009IVE2Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
3A0A009MYL5Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE...[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
4A0A009PHM9Precorrin methyltransferase family; Precorrin ...MOD_RES 129; /note=\"Phosphoserine\"; /evidence=...MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE...[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
\n", "
" ], "text/plain": [ " Entry Protein families \\\n", "0 A0A009GHC8 Precorrin methyltransferase family; Precorrin ... \n", "1 A0A009HTZ2 Precorrin methyltransferase family; Precorrin ... \n", "2 A0A009IVE2 Precorrin methyltransferase family; Precorrin ... \n", "3 A0A009MYL5 Precorrin methyltransferase family; Precorrin ... \n", "4 A0A009PHM9 Precorrin methyltransferase family; Precorrin ... \n", "\n", " Modified residue \\\n", "0 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "1 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "2 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "3 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "4 MOD_RES 129; /note=\"Phosphoserine\"; /evidence=... \n", "\n", " Sequence \\\n", "0 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n", "1 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n", "2 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n", "3 MDIFPISLKLQQQHCLIVGGGHIALRKANLLAKAGAVIDIIAPAIE... \n", "4 MDIFPISLKLQQQRCLIVGGGHIALRKATLLAKAGAIIDVVAPAIE... \n", "\n", " PTM sites \n", "0 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n", "1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n", "2 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n", "3 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... \n", "4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... " ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Function to split sequences and PTM sites into chunks\n", "def split_into_chunks(row):\n", " sequence = row['Sequence']\n", " ptm_sites = row['PTM sites']\n", " chunk_size = 1000\n", " \n", " # Calculate the number of chunks\n", " num_chunks = (len(sequence) + chunk_size - 1) // chunk_size\n", " \n", " # Split sequences and PTM sites into chunks\n", " sequence_chunks = [sequence[i * chunk_size: (i + 1) * chunk_size] for i in range(num_chunks)]\n", " ptm_sites_chunks = [ptm_sites[i * chunk_size: (i + 1) * chunk_size] for i in range(num_chunks)]\n", " \n", " # Create new rows for each chunk\n", " rows = []\n", " for i in range(num_chunks):\n", " new_row = row.copy()\n", " new_row['Sequence'] = sequence_chunks[i]\n", " new_row['PTM sites'] = ptm_sites_chunks[i]\n", " rows.append(new_row)\n", " \n", " return rows\n", "\n", "# Create a new DataFrame to store the chunks\n", "chunks_data = []\n", "\n", "# Iterate through each row of the original DataFrame and split into chunks\n", "for _, row in data.iterrows():\n", " chunks_data.extend(split_into_chunks(row))\n", "\n", "# Convert the list of chunks into a DataFrame\n", "chunks_df = pd.DataFrame(chunks_data)\n", "\n", "# Reset the index of the DataFrame\n", "chunks_df.reset_index(drop=True, inplace=True)\n", "\n", "# Display the first few rows of the new DataFrame\n", "chunks_df.head()\n" ] }, { "cell_type": "code", "execution_count": 52, "id": "0e36e5bb-8e57-45af-a9da-9171875a0b88", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "% Test Data: 21.17% | % Test Families: 15.15%: 15%|█▌ | 661/4364 [00:05<00:30, 120.20it/s]\n" ] } ], "source": [ "from tqdm import tqdm\n", "import numpy as np\n", "\n", "# Function to split data into train and test based on families\n", "def split_data(df):\n", " # Get a unique list of protein families\n", " unique_families = df['Protein families'].unique().tolist()\n", " np.random.shuffle(unique_families) # Shuffle the list to randomize the order of families\n", " \n", " test_data = []\n", " test_families = []\n", " total_entries = len(df)\n", " total_families = len(unique_families)\n", " \n", " # Set up tqdm progress bar\n", " with tqdm(total=total_families) as pbar:\n", " for family in unique_families:\n", " # Separate out all proteins in the current family into the test data\n", " family_data = df[df['Protein families'] == family]\n", " test_data.append(family_data)\n", " \n", " # Update the list of test families\n", " test_families.append(family)\n", " \n", " # Remove the current family data from the original DataFrame\n", " df = df[df['Protein families'] != family]\n", " \n", " # Calculate the percentage of test data and the percentage of families in the test data\n", " percent_test_data = sum(len(data) for data in test_data) / total_entries * 100\n", " percent_test_families = len(test_families) / total_families * 100\n", " \n", " # Update tqdm progress bar with readout of percentages\n", " pbar.set_description(f'% Test Data: {percent_test_data:.2f}% | % Test Families: {percent_test_families:.2f}%')\n", " pbar.update(1)\n", " \n", " # Check if the 20% threshold for test data is crossed\n", " if percent_test_data >= 20:\n", " break\n", " \n", " # Concatenate the list of test data DataFrames into a single DataFrame\n", " test_df = pd.concat(test_data, ignore_index=True)\n", " \n", " return df, test_df # Return the remaining data and the test data\n", "\n", "# Split the data into train and test based on families\n", "train_df, test_df = split_data(chunks_df)\n" ] }, { "cell_type": "code", "execution_count": 53, "id": "0d5e7371-a6d0-4c5c-8587-dd0037f052f8", "metadata": { "tags": [] }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "# Assuming train_df and test_df are your dataframes\n", "fraction = 0.105 # 10.5%\n", "\n", "# Randomly select 10.5% of the data\n", "reduced_train_df = train_df.sample(frac=fraction, random_state=42)\n", "reduced_test_df = test_df.sample(frac=fraction, random_state=42)\n", "\n", "# Split the reduced dataframes into sequences and PTM sites\n", "#train_sequences = reduced_train_df['Sequence']\n", "#train_ptm_sites = reduced_train_df['PTM sites']\n", "#test_sequences = reduced_test_df['Sequence']\n", "#test_ptm_sites = reduced_test_df['PTM sites']\n", "\n", "# Save the reduced data as pickle files\n", "#train_sequences.to_pickle('train_sequences.pkl')\n", "#train_ptm_sites.to_pickle('train_ptm_sites.pkl')\n", "#test_sequences.to_pickle('test_sequences.pkl')\n", "#test_ptm_sites.to_pickle('test_ptm_sites.pkl')\n" ] }, { "cell_type": "code", "execution_count": 55, "id": "a5ac2515-2aaa-4417-b5bb-09b25ce31d44", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['50K_ptm_data/train_sequences_chunked_by_family.pkl',\n", " '50K_ptm_data/test_sequences_chunked_by_family.pkl',\n", " '50K_ptm_data/train_labels_chunked_by_family.pkl',\n", " '50K_ptm_data/test_labels_chunked_by_family.pkl']" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pickle \n", "\n", "# Extract sequences and PTM site labels from the reduced train and test DataFrames\n", "train_sequences_reduced = reduced_train_df['Sequence'].tolist()\n", "train_labels_reduced = reduced_train_df['PTM sites'].tolist()\n", "test_sequences_reduced = reduced_test_df['Sequence'].tolist()\n", "test_labels_reduced = reduced_test_df['PTM sites'].tolist()\n", "\n", "# Save the lists to the specified pickle files\n", "pickle_file_path = \"50K_ptm_data/\"\n", "\n", "with open(pickle_file_path + \"train_sequences_chunked_by_family.pkl\", \"wb\") as f:\n", " pickle.dump(train_sequences_reduced, f)\n", "\n", "with open(pickle_file_path + \"test_sequences_chunked_by_family.pkl\", \"wb\") as f:\n", " pickle.dump(test_sequences_reduced, f)\n", "\n", "with open(pickle_file_path + \"train_labels_chunked_by_family.pkl\", \"wb\") as f:\n", " pickle.dump(train_labels_reduced, f)\n", "\n", "with open(pickle_file_path + \"test_labels_chunked_by_family.pkl\", \"wb\") as f:\n", " pickle.dump(test_labels_reduced, f)\n", "\n", "# Return the paths to the saved pickle files\n", "saved_files = [\n", " pickle_file_path + \"train_sequences_chunked_by_family.pkl\",\n", " pickle_file_path + \"test_sequences_chunked_by_family.pkl\",\n", " pickle_file_path + \"train_labels_chunked_by_family.pkl\",\n", " pickle_file_path + \"test_labels_chunked_by_family.pkl\"\n", "]\n", "saved_files\n" ] }, { "cell_type": "code", "execution_count": 57, "id": "5ec5c5fc-7e9a-4c2c-a954-b2d2ad168b11", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'50K_ptm_data/train_sequences_chunked_by_family.pkl': 5132, '50K_ptm_data/test_sequences_chunked_by_family.pkl': 1378, '50K_ptm_data/train_labels_chunked_by_family.pkl': 5132, '50K_ptm_data/test_labels_chunked_by_family.pkl': 1378}\n" ] } ], "source": [ "import pickle\n", "\n", "def get_number_of_rows(pickle_file):\n", " with open(pickle_file, \"rb\") as f:\n", " data = pickle.load(f)\n", " return len(data)\n", "\n", "# Paths to the pickle files\n", "files = [\n", " \"50K_ptm_data/train_sequences_chunked_by_family.pkl\",\n", " \"50K_ptm_data/test_sequences_chunked_by_family.pkl\",\n", " \"50K_ptm_data/train_labels_chunked_by_family.pkl\",\n", " \"50K_ptm_data/test_labels_chunked_by_family.pkl\"\n", "]\n", "\n", "# Get the number of rows for each file\n", "number_of_rows = {file: get_number_of_rows(file) for file in files}\n", "print(number_of_rows)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "71cc9d3d-bb35-4e2a-a382-7218bff5cb53", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "esm2_binding_py38b", "language": "python", "name": "esm2_binding_py38b" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.17" } }, "nbformat": 4, "nbformat_minor": 5 }