{"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 # 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","execution":{"iopub.status.busy":"2023-04-28T11:47:47.520865Z","iopub.execute_input":"2023-04-28T11:47:47.521382Z","iopub.status.idle":"2023-04-28T11:47:47.547884Z","shell.execute_reply.started":"2023-04-28T11:47:47.521333Z","shell.execute_reply":"2023-04-28T11:47:47.546561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-04-28T11:47:44.733108Z","iopub.execute_input":"2023-04-28T11:47:44.733454Z","iopub.status.idle":"2023-04-28T11:47:47.518501Z","shell.execute_reply.started":"2023-04-28T11:47:44.733419Z","shell.execute_reply":"2023-04-28T11:47:47.51719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n\ndef extract_go_terms_and_branches(file_path):\n    with open(file_path, 'r') as file:\n        content = file.read()\n        # Match each stanza with [Term] in the OBO file\n        stanzas = re.findall(r'\\[Term\\][\\s\\S]*?(?=\\n\\[|$)', content)\n\n    go_terms_dict = {}\n    for stanza in stanzas:\n        # Extract the GO term ID\n        go_id = re.search(r'^id: (GO:\\d+)', stanza, re.MULTILINE)\n        if go_id:\n            go_id = go_id.group(1)\n\n        # Extract the namespace (branch)\n        namespace = re.search(r'^namespace: (\\w+)', stanza, re.MULTILINE)\n        if namespace:\n            namespace = namespace.group(1)\n\n        if go_id and namespace:\n            # Map the branch abbreviation to the corresponding BPO, CCO, or MFO\n            branch_abbr = {'biological_process': 'BPO', 'cellular_component': 'CCO', 'molecular_function': 'MFO'}\n            go_terms_dict[go_id] = branch_abbr[namespace]\n\n    return go_terms_dict\n\nfile_path = '/kaggle/input/cafa-5-protein-function-prediction/Train/go-basic.obo'\ngo_terms_dict = extract_go_terms_and_branches(file_path)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T11:49:24.396686Z","iopub.execute_input":"2023-04-28T11:49:24.397117Z","iopub.status.idle":"2023-04-28T11:49:26.543334Z","shell.execute_reply.started":"2023-04-28T11:49:24.397079Z","shell.execute_reply":"2023-04-28T11:49:26.542099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nfrom tqdm.auto import tqdm\nfile = open('/kaggle/input/quick-go-annotation/all_dict.pkl', 'rb')\nquickgo_ann = pickle.load(file)\nquickgo_tsv = open('qickgo_ann.tsv','w')\nfor prot in tqdm(quickgo_ann):\n    for go in quickgo_ann[prot]:\n        \n        line = '\\t'.join([prot, go, go_terms_dict.get(go,'not_in_comp')])+'\\n'\n        quickgo_tsv.write(line)\nquickgo_tsv.close()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T11:50:38.084616Z","iopub.execute_input":"2023-04-28T11:50:38.085049Z","iopub.status.idle":"2023-04-28T11:50:42.162655Z","shell.execute_reply.started":"2023-04-28T11:50:38.085009Z","shell.execute_reply":"2023-04-28T11:50:42.161284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head 'qickgo_ann.tsv'","metadata":{"execution":{"iopub.status.busy":"2023-04-28T11:50:52.787496Z","iopub.execute_input":"2023-04-28T11:50:52.787916Z","iopub.status.idle":"2023-04-28T11:50:53.832387Z","shell.execute_reply.started":"2023-04-28T11:50:52.787878Z","shell.execute_reply":"2023-04-28T11:50:53.830481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}