{"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":"# 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 \nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom Bio import SeqIO\nfrom tqdm import tqdm\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-04-30T08:30:33.19415Z","iopub.execute_input":"2023-04-30T08:30:33.194609Z","iopub.status.idle":"2023-04-30T08:30:33.350998Z","shell.execute_reply.started":"2023-04-30T08:30:33.194571Z","shell.execute_reply":"2023-04-30T08:30:33.349339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_term = pd.read_csv(\"/kaggle/input/cafa-5-protein-function-prediction/Train/train_terms.tsv\", sep = \"\\t\")","metadata":{"execution":{"iopub.status.busy":"2023-04-30T08:08:54.806152Z","iopub.execute_input":"2023-04-30T08:08:54.807198Z","iopub.status.idle":"2023-04-30T08:08:56.624175Z","shell.execute_reply.started":"2023-04-30T08:08:54.807153Z","shell.execute_reply":"2023-04-30T08:08:56.62292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_term.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-30T08:08:59.269167Z","iopub.execute_input":"2023-04-30T08:08:59.269588Z","iopub.status.idle":"2023-04-30T08:08:59.281857Z","shell.execute_reply.started":"2023-04-30T08:08:59.269551Z","shell.execute_reply":"2023-04-30T08:08:59.280599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vec_freqcount = (train_term['term'].value_counts())\nprint(vec_freqcount)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T08:18:11.947855Z","iopub.execute_input":"2023-04-30T08:18:11.948285Z","iopub.status.idle":"2023-04-30T08:18:12.393205Z","shell.execute_reply.started":"2023-04-30T08:18:11.948248Z","shell.execute_reply":"2023-04-30T08:18:12.392132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_readable_var = 1000\nltc = list(vec_freqcount.index[:n_readable_var] )\nprint(\"labels to consider:\", len(ltc), \"First20:\", ltc[:20])","metadata":{"execution":{"iopub.status.busy":"2023-04-30T08:18:16.068159Z","iopub.execute_input":"2023-04-30T08:18:16.06856Z","iopub.status.idle":"2023-04-30T08:18:16.074715Z","shell.execute_reply.started":"2023-04-30T08:18:16.068525Z","shell.execute_reply":"2023-04-30T08:18:16.073581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_fasta(fastaPath):    \n    fasta_sequences = SeqIO.parse(open('/kaggle/input/cafa-5-protein-function-prediction/Test (Targets)/testsuperset.fasta'), 'fasta')\n    ids = []\n    sequences = []\n    for fasta in fasta_sequences:\n        ids.append(fasta.id)\n        sequences.append(str(fasta.seq))\n    return pd.DataFrame({'Id': ids, 'Sequence': sequences})\n\ndef get_top_go_terms(data, num_terms):\n    term_counts = data['term'].value_counts()\n    freq_counts = term_counts / len(data)\n    freq_top = freq_counts.nlargest(num_terms)\n    return freq_top","metadata":{"execution":{"iopub.status.busy":"2023-04-30T08:41:11.031493Z","iopub.execute_input":"2023-04-30T08:41:11.031978Z","iopub.status.idle":"2023-04-30T08:41:11.04068Z","shell.execute_reply.started":"2023-04-30T08:41:11.031941Z","shell.execute_reply":"2023-04-30T08:41:11.039111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_terms = pd.read_csv('/kaggle/input/cafa-5-protein-function-prediction/Train/train_terms.tsv', sep='\\t')\ntop_terms = get_top_go_terms(train_terms, 10)\n\ntest_data = read_fasta('/kaggle/input/cafa-5-protein-function-prediction/Test (Targets)/testsuperset.fasta')\n\nresults = []\nfor index, row in tqdm(test_data.iterrows(), total=test_data.shape[0], position=0):\n    for term, freq in top_terms.items():\n        results.append((row['Id'], term, freq))\n\nfinal_results = pd.DataFrame(results, columns=['Id', 'GO term', 'Confidence'])\nfinal_results.to_csv('submission.tsv', sep='\\t', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T08:41:16.676975Z","iopub.execute_input":"2023-04-30T08:41:16.677412Z","iopub.status.idle":"2023-04-30T08:41:44.174044Z","shell.execute_reply.started":"2023-04-30T08:41:16.677376Z","shell.execute_reply":"2023-04-30T08:41:44.172748Z"},"trusted":true},"execution_count":null,"outputs":[]}]}