{"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":"!git clone https://github.com/BioComputingUP/CAFA-evaluator.git","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:11:58.495898Z","iopub.execute_input":"2023-08-21T23:11:58.499345Z","iopub.status.idle":"2023-08-21T23:12:00.571235Z","shell.execute_reply.started":"2023-08-21T23:11:58.497368Z","shell.execute_reply":"2023-08-21T23:12:00.570005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\nimport sys\nsys.path.append('CAFA-evaluator/src/')\nfrom graph import Graph\nfrom parser import obo_parser\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\nimport cudf\nimport tqdm\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-08-21T23:12:00.575578Z","iopub.execute_input":"2023-08-21T23:12:00.575883Z","iopub.status.idle":"2023-08-21T23:12:04.948524Z","shell.execute_reply.started":"2023-08-21T23:12:00.575856Z","shell.execute_reply":"2023-08-21T23:12:04.947488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nmapper = []\n\nfor n, (ns, terms_dict) in enumerate(obo_parser(\n    '/kaggle/input/cafa-5-protein-function-prediction/Train/go-basic.obo'\n).items()):\n    G = Graph(ns, terms_dict, None, True)\n    terms = [x['id'] for x in G.terms_list]\n    mapper.append(pd.Series([n] * len(terms), index=terms))\n\nmapper = cudf.from_pandas(pd.concat(mapper))","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:12:04.949869Z","iopub.execute_input":"2023-08-21T23:12:04.950691Z","iopub.status.idle":"2023-08-21T23:12:10.84541Z","shell.execute_reply.started":"2023-08-21T23:12:04.950654Z","shell.execute_reply":"2023-08-21T23:12:10.844391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"goa = cudf.read_csv(\n    '/kaggle/input/cafa5-go-annotations/go_labels/labels/prop_test_leak_no_dup.tsv', \n    sep='\\t', usecols=['EntryID', 'term'])\ngoa['prob'] = 0.99","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:12:10.848333Z","iopub.execute_input":"2023-08-21T23:12:10.848955Z","iopub.status.idle":"2023-08-21T23:12:11.179915Z","shell.execute_reply.started":"2023-08-21T23:12:10.848918Z","shell.execute_reply":"2023-08-21T23:12:11.178863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"qg = cudf.read_csv(\n    '/kaggle/input/cafa5-go-annotations/prop_quickgo51.tsv', \n    sep='\\t', usecols=['EntryID', 'term'])\nqg['prob'] = 0.99","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:12:11.181438Z","iopub.execute_input":"2023-08-21T23:12:11.181785Z","iopub.status.idle":"2023-08-21T23:12:11.205647Z","shell.execute_reply.started":"2023-08-21T23:12:11.181752Z","shell.execute_reply":"2023-08-21T23:12:11.204515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diff = cudf.read_csv(\n    '/kaggle/input/cafa-terms-diff/pred.tsv', header=None, \n    sep='\\t', names=['EntryID', 'term', 'prob'])","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:14:06.387222Z","iopub.execute_input":"2023-08-21T23:14:06.387709Z","iopub.status.idle":"2023-08-21T23:14:06.40415Z","shell.execute_reply.started":"2023-08-21T23:14:06.387673Z","shell.execute_reply":"2023-08-21T23:14:06.40301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diff","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:14:06.64607Z","iopub.execute_input":"2023-08-21T23:14:06.648909Z","iopub.status.idle":"2023-08-21T23:14:06.696411Z","shell.execute_reply.started":"2023-08-21T23:14:06.648872Z","shell.execute_reply":"2023-08-21T23:14:06.695243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = cudf.read_csv(\n    '/kaggle/input/protein-subs/sub/submission.tsv',\n    header=None, names=['EntryID', 'term', 'prob'], sep='\\t'\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:14:06.935718Z","iopub.execute_input":"2023-08-21T23:14:06.93609Z","iopub.status.idle":"2023-08-21T23:14:07.862416Z","shell.execute_reply.started":"2023-08-21T23:14:06.936053Z","shell.execute_reply":"2023-08-21T23:14:07.861358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:14:07.864483Z","iopub.execute_input":"2023-08-21T23:14:07.864837Z","iopub.status.idle":"2023-08-21T23:14:07.924662Z","shell.execute_reply.started":"2023-08-21T23:14:07.864803Z","shell.execute_reply":"2023-08-21T23:14:07.92378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = cudf.concat([sub, qg, goa, diff], ignore_index=False)\nsub['ns'] = sub['term'].map(mapper).values\nsub = sub.groupby(['EntryID', 'term']).mean().reset_index()\nsub['rank'] = sub.groupby(['EntryID', 'ns'])['prob'].rank(method='dense', ascending=False) - 1\nsub = sub.query('rank < 500')","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:14:07.925912Z","iopub.execute_input":"2023-08-21T23:14:07.928427Z","iopub.status.idle":"2023-08-21T23:14:12.779525Z","shell.execute_reply.started":"2023-08-21T23:14:07.928383Z","shell.execute_reply":"2023-08-21T23:14:12.77844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sub[['EntryID', 'term', 'prob']].to_csv(index=False, header=False, sep='\\t')","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:14:12.781921Z","iopub.execute_input":"2023-08-21T23:14:12.782285Z","iopub.status.idle":"2023-08-21T23:14:12.78701Z","shell.execute_reply.started":"2023-08-21T23:14:12.782253Z","shell.execute_reply":"2023-08-21T23:14:12.786036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nBATCH_SIZE = 300000\nmode = 'w'\n\nfor i in tqdm.tqdm(range(0, sub.shape[0], BATCH_SIZE)):\n    with open('submission.tsv', mode) as f:\n        sub[['EntryID', 'term', 'prob']][i: i + BATCH_SIZE] \\\n            .to_csv(f, index=False, header=False, sep='\\t')\n        mode = 'a'","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:14:12.788768Z","iopub.execute_input":"2023-08-21T23:14:12.789496Z","iopub.status.idle":"2023-08-21T23:14:15.329906Z","shell.execute_reply.started":"2023-08-21T23:14:12.789458Z","shell.execute_reply":"2023-08-21T23:14:15.328824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head submission.tsv","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:14:15.331445Z","iopub.execute_input":"2023-08-21T23:14:15.332286Z","iopub.status.idle":"2023-08-21T23:14:16.306323Z","shell.execute_reply.started":"2023-08-21T23:14:15.332249Z","shell.execute_reply":"2023-08-21T23:14:16.305131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tail submission.tsv","metadata":{"execution":{"iopub.status.busy":"2023-08-21T23:14:16.308679Z","iopub.execute_input":"2023-08-21T23:14:16.309088Z","iopub.status.idle":"2023-08-21T23:14:17.386512Z","shell.execute_reply.started":"2023-08-21T23:14:16.309045Z","shell.execute_reply":"2023-08-21T23:14:17.385204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}