{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-02T13:01:12.426712Z","iopub.execute_input":"2023-09-02T13:01:12.427121Z","iopub.status.idle":"2023-09-02T13:01:12.492364Z","shell.execute_reply.started":"2023-09-02T13:01:12.427095Z","shell.execute_reply":"2023-09-02T13:01:12.489071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the first few lines of the IA.txt file\nia_file_path = os.path.join('/kaggle/input/cafa-5-protein-function-prediction/IA.txt')\n\nwith open(ia_file_path, 'r') as file:\n    ia_contents = [next(file) for _ in range(5)]\n\nia_contents\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T13:01:12.495638Z","iopub.execute_input":"2023-09-02T13:01:12.497153Z","iopub.status.idle":"2023-09-02T13:01:12.514456Z","shell.execute_reply.started":"2023-09-02T13:01:12.497117Z","shell.execute_reply":"2023-09-02T13:01:12.513390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# List the contents of the Train folder\ntrain_dir = os.path.join('/kaggle/input/cafa-5-protein-function-prediction/Train')\ntrain_files = os.listdir(train_dir)\ntrain_files\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T13:01:12.519357Z","iopub.execute_input":"2023-09-02T13:01:12.521489Z","iopub.status.idle":"2023-09-02T13:01:12.530977Z","shell.execute_reply.started":"2023-09-02T13:01:12.521450Z","shell.execute_reply":"2023-09-02T13:01:12.530121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from Bio import SeqIO\n\n# Load the first few entries from the train_sequences.fasta file\ntrain_sequences_file_path = os.path.join(train_dir, 'train_sequences.fasta')\ntrain_sequences = list(SeqIO.parse(train_sequences_file_path, \"fasta\"))\n\n# Display the first 3 entries\ntrain_sequences[:3]\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T13:01:12.533660Z","iopub.execute_input":"2023-09-02T13:01:12.535274Z","iopub.status.idle":"2023-09-02T13:01:16.005711Z","shell.execute_reply.started":"2023-09-02T13:01:12.535236Z","shell.execute_reply":"2023-09-02T13:01:16.003909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Load the first few lines of the train_taxonomy.tsv file\ntrain_taxonomy_file_path = os.path.join(train_dir, 'train_taxonomy.tsv')\ntrain_taxonomy_sample = pd.read_csv(train_taxonomy_file_path, sep='\\t', nrows=5)\n\ntrain_taxonomy_sample\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T13:01:16.006853Z","iopub.execute_input":"2023-09-02T13:01:16.007204Z","iopub.status.idle":"2023-09-02T13:01:16.048455Z","shell.execute_reply.started":"2023-09-02T13:01:16.007172Z","shell.execute_reply":"2023-09-02T13:01:16.046594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the first few lines of the train_terms.tsv file\ntrain_terms_file_path = os.path.join(train_dir, 'train_terms.tsv')\ntrain_terms_sample = pd.read_csv(train_terms_file_path, sep='\\t', nrows=5)\n\ntrain_terms_sample\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T13:01:16.051069Z","iopub.execute_input":"2023-09-02T13:01:16.051518Z","iopub.status.idle":"2023-09-02T13:01:16.071112Z","shell.execute_reply.started":"2023-09-02T13:01:16.051494Z","shell.execute_reply":"2023-09-02T13:01:16.069953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# List the contents of the Test (Targets) folder\ntest_targets_dir = os.path.join('/kaggle/input/cafa-5-protein-function-prediction/Test (Targets)')\ntest_targets_files = os.listdir(test_targets_dir)\ntest_targets_files\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T13:01:16.072916Z","iopub.execute_input":"2023-09-02T13:01:16.073287Z","iopub.status.idle":"2023-09-02T13:01:16.081454Z","shell.execute_reply.started":"2023-09-02T13:01:16.073257Z","shell.execute_reply":"2023-09-02T13:01:16.079646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from Bio import SeqIO\n\n# Load the amino acid sequences from the train_sequences.fasta file\ntrain_sequences_file_path = '/kaggle/input/cafa-5-protein-function-prediction/Train/train_sequences.fasta'\ntrain_sequences = list(SeqIO.parse(train_sequences_file_path, \"fasta\"))\n\n# Display the first entry\nprint(train_sequences[0])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T13:01:16.083045Z","iopub.execute_input":"2023-09-02T13:01:16.083478Z","iopub.status.idle":"2023-09-02T13:01:18.647169Z","shell.execute_reply.started":"2023-09-02T13:01:16.083413Z","shell.execute_reply":"2023-09-02T13:01:18.645499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.preprocessing import MultiLabelBinarizer\n\n# Load the GO terms from the train_terms.tsv file\ntrain_terms_file_path = '/kaggle/input/cafa-5-protein-function-prediction/Train/train_terms.tsv'\ntrain_terms = pd.read_csv(train_terms_file_path, sep='\\t')\n\n# Encode the GO terms using multi-label binarization\nmlb = MultiLabelBinarizer()\ntrain_terms_encoded = mlb.fit_transform(train_terms['term'])\n\n# Display the first few rows of the encoded GO terms\nprint(train_terms_encoded[:5])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T13:01:18.648979Z","iopub.execute_input":"2023-09-02T13:01:18.649351Z","iopub.status.idle":"2023-09-02T13:01:34.019814Z","shell.execute_reply.started":"2023-09-02T13:01:18.649326Z","shell.execute_reply":"2023-09-02T13:01:34.018594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.preprocessing import LabelBinarizer\n\n# Load the GO terms from the train_terms.tsv file\ntrain_terms_file_path = '/kaggle/input/cafa-5-protein-function-prediction/Train/train_terms.tsv'\ntrain_terms = pd.read_csv(train_terms_file_path, sep='\\t')\n\n# Encode the GO terms using one-hot encoding\nlb = LabelBinarizer()\ntrain_terms_encoded = lb.fit_transform(train_terms['term'])\n\n# Display the first few rows of the encoded GO terms\nprint(train_terms_encoded[:5])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T13:01:34.023184Z","iopub.execute_input":"2023-09-02T13:01:34.023476Z","iopub.status.idle":"2023-09-02T14:01:28.011066Z","shell.execute_reply.started":"2023-09-02T13:01:34.023453Z","shell.execute_reply":"2023-09-02T14:01:28.008589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a list of all unique amino acids in the training data\nvocab = list(set(''.join(str(record.seq) for record in train_sequences)))\n\n# Get the maximum length of the amino acid sequences in the training data\nmax_length = max(len(record.seq) for record in train_sequences)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T14:01:28.014409Z","iopub.execute_input":"2023-09-02T14:01:28.014930Z","iopub.status.idle":"2023-09-02T14:01:29.860490Z","shell.execute_reply.started":"2023-09-02T14:01:28.014892Z","shell.execute_reply":"2023-09-02T14:01:29.856141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a list of all unique GO terms in the training data\ngo_terms = train_terms['term'].unique()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T14:01:29.864587Z","iopub.execute_input":"2023-09-02T14:01:29.864979Z","iopub.status.idle":"2023-09-02T14:01:30.457815Z","shell.execute_reply.started":"2023-09-02T14:01:29.864950Z","shell.execute_reply":"2023-09-02T14:01:30.454319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Embedding, Conv1D, GlobalMaxPooling1D, Dense\n\n# Define the model\nmodel = Sequential([\n    Embedding(input_dim=len(vocab), output_dim=128, input_length=max_length),\n    Conv1D(128, 3, activation='relu'),\n    GlobalMaxPooling1D(),\n    Dense(256, activation='relu'),\n    Dense(len(go_terms), activation='sigmoid')\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='binary_crossentropy')\n\n# Display the model's architecture\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T14:01:30.463829Z","iopub.execute_input":"2023-09-02T14:01:30.464416Z","iopub.status.idle":"2023-09-02T14:01:42.221692Z","shell.execute_reply.started":"2023-09-02T14:01:30.464386Z","shell.execute_reply":"2023-09-02T14:01:42.220340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.text import Tokenizer\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences\n\ndef preprocess_sequences(sequences):\n    # Tokenize the sequences\n    tokenizer = Tokenizer(char_level=True)\n    tokenizer.fit_on_texts(sequences)\n    sequences_tokenized = tokenizer.texts_to_sequences(sequences)\n    \n    # Pad the sequences\n    max_length = max(len(seq) for seq in sequences_tokenized)\n    sequences_padded = pad_sequences(sequences_tokenized, maxlen=max_length, padding='post')\n    \n    # One-hot encode the sequences\n    X = np.array([np.eye(len(tokenizer.word_index))[seq - 1] for seq in sequences_padded])\n    \n    return X\n\n# Example usage:\n# X_test = preprocess_sequences(test_sequences)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T14:01:42.224083Z","iopub.execute_input":"2023-09-02T14:01:42.224498Z","iopub.status.idle":"2023-09-02T14:01:42.234294Z","shell.execute_reply.started":"2023-09-02T14:01:42.224467Z","shell.execute_reply":"2023-09-02T14:01:42.232272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from Bio import SeqIO\n\ntest_sequences = list(SeqIO.parse(\"/kaggle/input/cafa-5-protein-function-prediction/Test (Targets)/testsuperset.fasta\", \"fasta\"))\n\n\ndef preprocess_sequences(sequences):\n    # Your code here to preprocess the sequences\n    pass\n\nX_test = preprocess_sequences(test_sequences)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T14:19:14.128078Z","iopub.execute_input":"2023-09-02T14:19:14.128496Z","iopub.status.idle":"2023-09-02T14:19:17.997201Z","shell.execute_reply.started":"2023-09-02T14:19:14.128468Z","shell.execute_reply":"2023-09-02T14:19:17.995406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"go_terms = []  # list of all GO terms\n# ... populate go_terms list ...\n\ngo_term_index = {go_term: i for i, go_term in enumerate(go_terms)}\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T14:19:56.401856Z","iopub.execute_input":"2023-09-02T14:19:56.402206Z","iopub.status.idle":"2023-09-02T14:19:56.408560Z","shell.execute_reply.started":"2023-09-02T14:19:56.402182Z","shell.execute_reply":"2023-09-02T14:19:56.407024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the submission file\nwith open('submission.tsv', 'w') as f:\n    for i, sequence in enumerate(test_sequences):\n        for go_term, index in go_term_index.items():\n            if y_test_pred_bin[i, index] == 1:\n                f.write(f'{sequence.id}\\t{go_term}\\t{y_test_pred[i, index]:.3f}\\n')","metadata":{"execution":{"iopub.status.busy":"2023-09-02T14:20:00.766683Z","iopub.execute_input":"2023-09-02T14:20:00.767029Z","iopub.status.idle":"2023-09-02T14:20:00.815778Z","shell.execute_reply.started":"2023-09-02T14:20:00.766979Z","shell.execute_reply":"2023-09-02T14:20:00.814537Z"},"trusted":true},"execution_count":null,"outputs":[]}],"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}}