{"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":"import pandas as pd#导入csv文件的库\nimport numpy as np#进行矩阵运算的库\nimport torch#一个深度学习的库Pytorch\nimport torch.nn as nn#neural network,神经网络\nfrom torch.autograd import Variable#从自动求导中引入变量\nimport torch.optim as optim#一个实现了各种优化算法的库\nimport torch.nn.functional as F#神经网络函数库\nfrom Bio import SeqIO#用bio来解析序列","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-26T11:16:22.485367Z","iopub.execute_input":"2023-04-26T11:16:22.486096Z","iopub.status.idle":"2023-04-26T11:16:22.491871Z","shell.execute_reply.started":"2023-04-26T11:16:22.486057Z","shell.execute_reply":"2023-04-26T11:16:22.490709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sequences = SeqIO.parse('/kaggle/input/cafa-5-protein-function-prediction/Train/train_sequences.fasta', \"fasta\")\n# get the name of each sequences\ntrainseqs = [str(seq).split('\\n')[-1][5:-2] for seq in sequences]","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:16:22.495693Z","iopub.execute_input":"2023-04-26T11:16:22.496827Z","iopub.status.idle":"2023-04-26T11:16:25.257287Z","shell.execute_reply.started":"2023-04-26T11:16:22.496788Z","shell.execute_reply":"2023-04-26T11:16:25.256225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"terms=pd.read_csv('/kaggle/input/cafa-5-protein-function-prediction/Train/train_terms.tsv',sep='\\t')\nterms.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:16:25.259399Z","iopub.execute_input":"2023-04-26T11:16:25.259838Z","iopub.status.idle":"2023-04-26T11:16:27.102534Z","shell.execute_reply.started":"2023-04-26T11:16:25.2598Z","shell.execute_reply":"2023-04-26T11:16:27.101441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EntryID=np.unique(terms['EntryID'].values)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:16:27.104722Z","iopub.execute_input":"2023-04-26T11:16:27.105653Z","iopub.status.idle":"2023-04-26T11:16:30.503315Z","shell.execute_reply.started":"2023-04-26T11:16:27.105515Z","shell.execute_reply":"2023-04-26T11:16:30.502301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainword=[[0 for i in range(26)]for j in range(len(trainseqs))]\nfor i in range(len(trainseqs)):\n    for j in range(len(trainseqs[i])):\n        trainword[i][ord(trainseqs[i][j])-ord('A')]+=1","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:16:30.512808Z","iopub.execute_input":"2023-04-26T11:16:30.513468Z","iopub.status.idle":"2023-04-26T11:16:34.201778Z","shell.execute_reply.started":"2023-04-26T11:16:30.513431Z","shell.execute_reply":"2023-04-26T11:16:34.200599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sequences = SeqIO.parse('/kaggle/input/cafa-5-protein-function-prediction/Test (Targets)/testsuperset.fasta', \"fasta\")\n# get the name of each sequences\ntestseqs = [str(seq).split('\\n')[-1][5:-2] for seq in sequences]","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:16:34.203371Z","iopub.execute_input":"2023-04-26T11:16:34.203814Z","iopub.status.idle":"2023-04-26T11:16:36.319129Z","shell.execute_reply.started":"2023-04-26T11:16:34.203773Z","shell.execute_reply":"2023-04-26T11:16:36.318105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testword=[[0 for i in range(26)]for j in range(len(testseqs))]\nfor i in range(len(testseqs)):\n    for j in range(len(testseqs[i])):\n        testword[i][ord(testseqs[i][j])-ord('A')]+=1","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:16:36.320794Z","iopub.execute_input":"2023-04-26T11:16:36.321192Z","iopub.status.idle":"2023-04-26T11:16:39.870508Z","shell.execute_reply.started":"2023-04-26T11:16:36.321153Z","shell.execute_reply":"2023-04-26T11:16:39.869177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainword=np.array(trainword)\ntestword=np.array(testword)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:16:39.872082Z","iopub.execute_input":"2023-04-26T11:16:39.872488Z","iopub.status.idle":"2023-04-26T11:16:40.603777Z","shell.execute_reply.started":"2023-04-26T11:16:39.872449Z","shell.execute_reply":"2023-04-26T11:16:40.602714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/cafa-5-protein-function-prediction/sample_submission.tsv',header=None ,sep='\\t')#index=False,导入submission","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:29:57.488987Z","iopub.execute_input":"2023-04-26T11:29:57.489716Z","iopub.status.idle":"2023-04-26T11:29:57.635602Z","shell.execute_reply.started":"2023-04-26T11:29:57.489666Z","shell.execute_reply":"2023-04-26T11:29:57.634578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:29:59.288146Z","iopub.execute_input":"2023-04-26T11:29:59.288514Z","iopub.status.idle":"2023-04-26T11:29:59.300975Z","shell.execute_reply.started":"2023-04-26T11:29:59.288481Z","shell.execute_reply":"2023-04-26T11:29:59.299562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=submission.values","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:30:06.552812Z","iopub.execute_input":"2023-04-26T11:30:06.553196Z","iopub.status.idle":"2023-04-26T11:30:06.579148Z","shell.execute_reply.started":"2023-04-26T11:30:06.553162Z","shell.execute_reply":"2023-04-26T11:30:06.578067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(testword)):\n    maxcos_sim=-1\n    maxj=0\n    for j in range(len(trainword)): \n        cos_sim = testword[i].dot(trainword[j]) / (np.linalg.norm(testword[i]) * np.linalg.norm(trainword[j]))\n        if cos_sim>maxcos_sim:\n            maxcos_sim=cos_sim\n            maxj=j\n            if maxcos_sim>0.9:\n                break\n    index=np.where(terms['EntryID'].values==EntryID[maxj])[0]\n    index1=index2=index[0]\n    if len(index)>=2:\n        index2=index[1]\n    #取index1和index2的GO\n    sub[2*i][1]=terms['term'].values[index1]   \n    sub[2*i+1][1]=terms['term'].values[index2]  \n    sub[2*i][2]=0.0625  \n    sub[2*i+1][2]=0.0625\n    if i%100==0:\n        print(i,len(testword))","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:30:13.531582Z","iopub.execute_input":"2023-04-26T11:30:13.532284Z","iopub.status.idle":"2023-04-26T11:31:20.615227Z","shell.execute_reply.started":"2023-04-26T11:30:13.532245Z","shell.execute_reply":"2023-04-26T11:31:20.61357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submiss=pd.DataFrame(sub)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:32:18.354607Z","iopub.execute_input":"2023-04-26T11:32:18.355017Z","iopub.status.idle":"2023-04-26T11:32:18.374724Z","shell.execute_reply.started":"2023-04-26T11:32:18.354981Z","shell.execute_reply":"2023-04-26T11:32:18.373716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submiss.to_csv(\"submission.tsv\",header=False, index=False, sep=\"\\t\")","metadata":{"execution":{"iopub.status.busy":"2023-04-26T11:32:24.524031Z","iopub.execute_input":"2023-04-26T11:32:24.524627Z","iopub.status.idle":"2023-04-26T11:32:24.915525Z","shell.execute_reply.started":"2023-04-26T11:32:24.524588Z","shell.execute_reply":"2023-04-26T11:32:24.914502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}