{"metadata":{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install obonet","metadata":{"execution":{"iopub.status.busy":"2023-04-26T23:27:48.222900Z","iopub.execute_input":"2023-04-26T23:27:48.223481Z","iopub.status.idle":"2023-04-26T23:28:01.170390Z","shell.execute_reply.started":"2023-04-26T23:27:48.223432Z","shell.execute_reply":"2023-04-26T23:28:01.169128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom Bio import SeqIO\nimport obonet\nimport numpy as np\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import MultiLabelBinarizer","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-26T23:28:01.172899Z","iopub.execute_input":"2023-04-26T23:28:01.173601Z","iopub.status.idle":"2023-04-26T23:28:09.478760Z","shell.execute_reply.started":"2023-04-26T23:28:01.173556Z","shell.execute_reply":"2023-04-26T23:28:09.477625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load ontology data\ngo_graph = obonet.read_obo('/kaggle/input/cafa-5-protein-function-prediction/Train/go-basic.obo')","metadata":{"execution":{"iopub.status.busy":"2023-04-26T23:28:11.331521Z","iopub.execute_input":"2023-04-26T23:28:11.333095Z","iopub.status.idle":"2023-04-26T23:28:18.810603Z","shell.execute_reply.started":"2023-04-26T23:28:11.333047Z","shell.execute_reply":"2023-04-26T23:28:18.809530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load train_sequences.fasta and parse it\ntrain_sequences = []\nfor record in SeqIO.parse('/kaggle/input/cafa-5-protein-function-prediction/Train/train_sequences.fasta', 'fasta'):\n    train_sequences.append((str(record.id), str(record.seq)))","metadata":{"execution":{"iopub.status.busy":"2023-04-26T23:28:18.812758Z","iopub.execute_input":"2023-04-26T23:28:18.813254Z","iopub.status.idle":"2023-04-26T23:28:21.009403Z","shell.execute_reply.started":"2023-04-26T23:28:18.813209Z","shell.execute_reply":"2023-04-26T23:28:21.008346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load train_terms.tsv\ntrain_terms = pd.read_csv('/kaggle/input/cafa-5-protein-function-prediction/Train/train_terms.tsv', sep='\\t', header=None, names=['id', 'go_term', 'ontology'])","metadata":{"execution":{"iopub.status.busy":"2023-04-26T23:28:21.013017Z","iopub.execute_input":"2023-04-26T23:28:21.013362Z","iopub.status.idle":"2023-04-26T23:28:24.247709Z","shell.execute_reply.started":"2023-04-26T23:28:21.013331Z","shell.execute_reply":"2023-04-26T23:28:24.246631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load train_taxonomy.tsv\ntrain_taxonomy = pd.read_csv('/kaggle/input/cafa-5-protein-function-prediction/Train/train_taxonomy.tsv', sep='\\t', header=None, names=['id', 'taxon_id'])","metadata":{"execution":{"iopub.status.busy":"2023-04-26T23:28:24.249873Z","iopub.execute_input":"2023-04-26T23:28:24.250169Z","iopub.status.idle":"2023-04-26T23:28:24.363642Z","shell.execute_reply.started":"2023-04-26T23:28:24.250142Z","shell.execute_reply":"2023-04-26T23:28:24.362512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tokenize and encode protein sequences (example: one-hot encoding)\ntokenizer = tf.keras.preprocessing.text.Tokenizer(char_level=True)\ntokenizer.fit_on_texts([seq for _, seq in train_sequences])\nencoded_sequences = tokenizer.texts_to_sequences([seq for _, seq in train_sequences])","metadata":{"execution":{"iopub.status.busy":"2023-04-26T23:28:26.508749Z","iopub.execute_input":"2023-04-26T23:28:26.509745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pad sequences to a fixed length (example: 1000 amino acids)\nmax_length = 1000\npadded_sequences = tf.keras.preprocessing.sequence.pad_sequences(encoded_sequences, maxlen=max_length)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Encode GO terms as target labels\nlabel_encoder = LabelEncoder()\nencoded_labels = label_encoder.fit_transform(train_terms['go_term'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a target matrix with binary values for GO terms\ngo_terms_by_protein = train_terms.groupby('id')['go_term'].apply(list)\ntarget_matrix = np.zeros((len(train_sequences), len(go_terms_by_protein)))\n\nmulti_label_binarizer = MultiLabelBinarizer()\nencoded_labels = multi_label_binarizer.fit_transform(go_terms_by_protein)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T21:18:36.406786Z","iopub.execute_input":"2023-04-26T21:18:36.407329Z","iopub.status.idle":"2023-04-26T21:18:50.516626Z","shell.execute_reply.started":"2023-04-26T21:18:36.407270Z","shell.execute_reply":"2023-04-26T21:18:50.515386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the first 5 elements in protein_id_to_index dictionary\nprint(list(protein_id_to_index.items())[:5])\n\n# Check the first 5 rows of train_terms DataFrame\nprint(train_terms.head())","metadata":{"execution":{"iopub.status.busy":"2023-04-26T21:23:27.162590Z","iopub.execute_input":"2023-04-26T21:23:27.163054Z","iopub.status.idle":"2023-04-26T21:23:27.333551Z","shell.execute_reply.started":"2023-04-26T21:23:27.163017Z","shell.execute_reply":"2023-04-26T21:23:27.332013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove the extra row from train_terms DataFrame\ntrain_terms = train_terms[train_terms['id'] != 'EntryID']","metadata":{"execution":{"iopub.status.busy":"2023-04-26T21:24:46.605324Z","iopub.execute_input":"2023-04-26T21:24:46.606395Z","iopub.status.idle":"2023-04-26T21:24:47.311667Z","shell.execute_reply.started":"2023-04-26T21:24:46.606335Z","shell.execute_reply":"2023-04-26T21:24:47.310221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now, let's try to fill the target_matrix again\nfor index, row in train_terms.iterrows():\n    protein_index = protein_id_to_index[row['id']]\n    target_index = label_encoder.transform([row['go_term']])[0]\n    target_matrix[protein_index, target_index] = 1","metadata":{"execution":{"iopub.status.busy":"2023-04-26T21:24:49.579974Z","iopub.execute_input":"2023-04-26T21:24:49.581407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Match the protein sequences to their corresponding labels\nprotein_id_to_index = {protein_id: index for index, (protein_id, _) in enumerate(train_sequences)}\nfor index, row in train_terms.iterrows():\n    protein_index = protein_id_to_index[row['id']]\n    target_matrix[protein_index, :] = encoded_labels[index]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the dataset into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(padded_sequences, target_matrix, test_size=0.2, random_state=42)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# simple CNN architecture\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Embedding(input_dim=len(tokenizer.word_index) + 1, output_dim=32, input_length=max_length),\n    tf.keras.layers.Conv1D(filters=64, kernel_size=5, activation='relu'),\n    tf.keras.layers.MaxPooling1D(pool_size=4),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dense(encoded_labels.shape[1], activation='sigmoid')\n])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  Compile the model\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train the model\nhistory = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=10, batch_size=32)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}