{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":116062,"databundleVersionId":14875579,"sourceType":"competition"}],"dockerImageVersionId":31234,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","trusted":true,"execution":{"iopub.status.busy":"2026-02-02T21:16:06.479705Z","iopub.execute_input":"2026-02-02T21:16:06.479942Z","iopub.status.idle":"2026-02-02T21:16:07.595669Z","shell.execute_reply.started":"2026-02-02T21:16:06.479923Z","shell.execute_reply":"2026-02-02T21:16:07.594909Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install Bio","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-02T21:16:07.597323Z","iopub.execute_input":"2026-02-02T21:16:07.597695Z","iopub.status.idle":"2026-02-02T21:16:13.939959Z","shell.execute_reply.started":"2026-02-02T21:16:07.597671Z","shell.execute_reply":"2026-02-02T21:16:13.939264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(os.getcwd())\nfrom Bio import SeqIO\nos.chdir('/kaggle/input/cafa-6-protein-function-prediction/Train/')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-02T21:16:13.941006Z","iopub.execute_input":"2026-02-02T21:16:13.941252Z","iopub.status.idle":"2026-02-02T21:16:14.00787Z","shell.execute_reply.started":"2026-02-02T21:16:13.94123Z","shell.execute_reply":"2026-02-02T21:16:14.007156Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filename='train_sequences.fasta'\nrecord_dict = SeqIO.index(filename, \"fasta\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-02T21:16:14.009467Z","iopub.execute_input":"2026-02-02T21:16:14.00967Z","iopub.status.idle":"2026-02-02T21:16:14.832857Z","shell.execute_reply.started":"2026-02-02T21:16:14.009653Z","shell.execute_reply":"2026-02-02T21:16:14.832266Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=pd.DataFrame(columns=['ID','Sequence', 'Encoded Sequence', 'Length'])\nfor index,record in enumerate(SeqIO.parse(filename, \"fasta\")):\n    id=record.id\n    x.at[index,'ID']=id.split('|')[1]\n    x.at[index,'Sequence']=str(record.seq)\n    x.at[index,'Length']=len(x.at[index,'Sequence'])\n    #Early stop\n    #if index==199: break\nprint(x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-02T21:16:14.833559Z","iopub.execute_input":"2026-02-02T21:16:14.833723Z","iopub.status.idle":"2026-02-02T21:21:47.737577Z","shell.execute_reply.started":"2026-02-02T21:16:14.833706Z","shell.execute_reply":"2026-02-02T21:21:47.73668Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"aa_letter = 'XARNDCQEGHILKMFPSUTWYVBZ'\naa_to_int = dict((c, i) for i, c in enumerate(aa_letter))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-02T21:21:47.738478Z","iopub.execute_input":"2026-02-02T21:21:47.738711Z","iopub.status.idle":"2026-02-02T21:21:47.74238Z","shell.execute_reply.started":"2026-02-02T21:21:47.738686Z","shell.execute_reply":"2026-02-02T21:21:47.741557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(aa_letter)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-02T21:21:47.74342Z","iopub.execute_input":"2026-02-02T21:21:47.74369Z","iopub.status.idle":"2026-02-02T21:21:47.763009Z","shell.execute_reply.started":"2026-02-02T21:21:47.743673Z","shell.execute_reply":"2026-02-02T21:21:47.762185Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def encode_sequence(seq, max_length=0):\n    if max_length<len(seq):\n        max_length= len(seq)\n    arr= np.zeros((max_length,len(aa_letter)))\n    for i,aa in enumerate(seq):\n        arr[i,aa_to_int[aa]]=1\n    arr[0,i+1:]=1\n    return arr","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-02T21:21:47.764026Z","iopub.execute_input":"2026-02-02T21:21:47.764256Z","iopub.status.idle":"2026-02-02T21:21:47.778547Z","shell.execute_reply.started":"2026-02-02T21:21:47.764238Z","shell.execute_reply":"2026-02-02T21:21:47.777729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"max_length=x['Length'].max()\nx['Encoded Sequence']=x['Sequence'].apply(encode_sequence,args=(max_length,))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-02T21:21:47.779411Z","iopub.execute_input":"2026-02-02T21:21:47.779725Z","iopub.status.idle":"2026-02-02T21:21:58.457037Z","shell.execute_reply.started":"2026-02-02T21:21:47.779703Z","shell.execute_reply":"2026-02-02T21:21:58.456515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_encoded=x['Encoded Sequence'].to_numpy()\nx_encoded=np.stack(x_encoded)\nx_train=x_encoded\nprint(x_train.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-02T21:21:58.458988Z","iopub.execute_input":"2026-02-02T21:21:58.4592Z","execution_failed":"2026-02-02T21:22:10.689Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_terms=pd.read_csv('train_terms.tsv',sep='\\t')\ntrain_terms.head()","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.689Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"terms=train_terms['term'].unique()\nids=x['ID']\ny=pd.DataFrame(index=range(200),columns=np.insert(terms,0,'ID'))\nprint(y)","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.69Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for index,id in enumerate(ids):\n    subset=train_terms[train_terms['EntryID']==id]\n    #print(subset)\n    y.at[index,'ID']=id\n    for term in subset['term']:\n        y.at[index,term]=1\n    if index==199: break\n#y.fillna(0)\ny.head()\n","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.69Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y.fillna(0, inplace=True)\ny=y.drop('ID',axis=1)\ny_train=y[0:100]\ny_test=y[100:200]\ny_train.head()","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.69Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.chdir('/kaggle/input/cafa-6-protein-function-prediction/Test/')\nfilename='testsuperset.fasta'\nrecord_dict = SeqIO.index(filename, \"fasta\")\ntest_df=pd.DataFrame(columns=['ID','Sequence', 'Encoded Sequence', 'Length'])\nfor index,record in enumerate(SeqIO.parse(filename, \"fasta\")):\n    id=record.id\n    test_df.at[index,'ID']=id.split('|')[1]\n    test_df.at[index,'Sequence']=str(record.seq)\n    test_df.at[index,'Length']=len(x.at[index,'Sequence'])\n    #Early stop\n    #if index==199: break\nmax_length=test_df['Length'].max()\ntest_df['Encoded Sequence']=test_df['Sequence'].apply(encode_sequence,args=(max_length,))\nx_test=test_df['Encoded Sequence'].to_numpy()\nx_test=np.stack(x_test)","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.69Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\n\ndef build_model(num_features):\n    model = keras.Sequential([\n        #layers.Input(shape=(length,len(aa_letter))), #do we need this?\n        layers.Conv1D(filters=21,kernel_size=1,activation='relu', data_format='channels_last'),\n        layers.Conv1D(filters=32,kernel_size=3,activation='relu', data_format='channels_last'),\n        layers.MaxPooling1D(pool_size=2),\n        layers.Flatten(),\n        layers.Dense(64,activation='relu'),\n        layers.Dense(num_features,activation='sigmoid')\n    ])\n    return model","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.69Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_features=y_train.shape[1]\nmodel= build_model(num_features)","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\n\nmodel.compile(\n    optimizer=Adam(),\n    loss='binary_crossentropy',\n    metrics=['accuracy']\n)","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"keras.config.disable_traceback_filtering()","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history=model.fit(\n    x=x_train,\n    y=y_train,\n    batch_size=32,\n    epochs=100\n)","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#loss, accuracy = model.evaluate(x_test, y_test)\n#print(f\"Test Loss: {loss:.4f}\")\n#print(f\"Test Accuracy: {accuracy:.4f}\")","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_predict=model.predict(x_test)","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ids= ids[100:]\nnum_terms=len(terms)\nsubmission= pd.DataFrame(index=range(num_terms*len(ids)), columns=['ID','GO Term','Probability'])\nfor i,id in enumerate(test_ids):\n    submission.loc[i*num_terms:(i+1)*num_terms-1,'ID']=id\n    submission.loc[i*num_terms:(i+1)*num_terms-1,'GO Term']=terms\n    submission.loc[i*num_terms:(i+1)*num_terms-1,'Probability']=y_predict[i]\nsubmission=submission[submission['Probability']!=0]\n","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.chdir('/kaggle/working')\nsubmission.to_csv('submission.tsv',sep='\\t',header='False')","metadata":{"trusted":true,"execution":{"execution_failed":"2026-02-02T21:22:10.691Z"}},"outputs":[],"execution_count":null}]}