{"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":"markdown","source":"# What is about  ?\n\nExample to access Uniprot API \n\nThanks to Alex Trepetsky\n","metadata":{}},{"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-28T20:09:26.363457Z","iopub.execute_input":"2023-04-28T20:09:26.364003Z","iopub.status.idle":"2023-04-28T20:09:26.386142Z","shell.execute_reply.started":"2023-04-28T20:09:26.36395Z","shell.execute_reply":"2023-04-28T20:09:26.384877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nimport joblib\nimport requests, sys, json\n\nWEBSITE_API = 'https://rest.uniprot.org'\n\ndef get_url(url, **kwargs):\n    response = requests.get(url, **kwargs)\n\n    if not response.ok:\n        response.raise_for_status()\n        sys.exit()\n\n    return response\n\nuniprot_data_proteins = {}\n# proteins - is dataframe \n# for id in tqdm(proteins.UniProt.unique()):\nfor id in ['P62259']:\n    r = get_url(f'{WEBSITE_API}/uniprotkb/{id}')\n    uniprot_data_proteins[str(id)] = r.json()\n\njoblib.dump(uniprot_data_proteins, 'uniprot_data_proteins.pkl')","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:09:26.388719Z","iopub.execute_input":"2023-04-28T20:09:26.389948Z","iopub.status.idle":"2023-04-28T20:09:28.33182Z","shell.execute_reply.started":"2023-04-28T20:09:26.389894Z","shell.execute_reply":"2023-04-28T20:09:28.330591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(list(r.json().keys() ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:09:28.334682Z","iopub.execute_input":"2023-04-28T20:09:28.335818Z","iopub.status.idle":"2023-04-28T20:09:28.34219Z","shell.execute_reply.started":"2023-04-28T20:09:28.335775Z","shell.execute_reply":"2023-04-28T20:09:28.34102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(list(r.json().keys() ))\n\nprint(list(r.json()['comments'][0].keys() ))\nprint(list(r.json()['comments'][0]['texts'] ))\nprint(list(r.json()['comments'][0]['commentType'] ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:09:28.343776Z","iopub.execute_input":"2023-04-28T20:09:28.344742Z","iopub.status.idle":"2023-04-28T20:09:28.35639Z","shell.execute_reply.started":"2023-04-28T20:09:28.344702Z","shell.execute_reply":"2023-04-28T20:09:28.355215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r.json()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:09:28.359144Z","iopub.execute_input":"2023-04-28T20:09:28.359908Z","iopub.status.idle":"2023-04-28T20:09:28.433366Z","shell.execute_reply.started":"2023-04-28T20:09:28.359858Z","shell.execute_reply":"2023-04-28T20:09:28.432156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(list(r.json().keys() ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:10:08.224806Z","iopub.execute_input":"2023-04-28T20:10:08.225209Z","iopub.status.idle":"2023-04-28T20:10:08.231863Z","shell.execute_reply.started":"2023-04-28T20:10:08.225173Z","shell.execute_reply":"2023-04-28T20:10:08.230509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(list(r.json()['organism'].keys() ))\nprint(list(r.json()['organism']['lineage'] ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:11:16.593323Z","iopub.execute_input":"2023-04-28T20:11:16.593714Z","iopub.status.idle":"2023-04-28T20:11:16.601642Z","shell.execute_reply.started":"2023-04-28T20:11:16.593681Z","shell.execute_reply":"2023-04-28T20:11:16.600312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"j = r.json()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:14:53.51564Z","iopub.execute_input":"2023-04-28T20:14:53.516026Z","iopub.status.idle":"2023-04-28T20:14:53.522821Z","shell.execute_reply.started":"2023-04-28T20:14:53.515994Z","shell.execute_reply":"2023-04-28T20:14:53.521335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# uniProtKBCrossReferences","metadata":{}},{"cell_type":"code","source":"k = 'uniProtKBCrossReferences'\nt = j[k]\nif isinstance(t, list):\n    \n    print('list:',len(t), t[0]);print()\n    if len(t) > 1:\n        print(t[1]); print()\n    print(t)\n#     for i in range(len(t)):\n#         print(t[i].keys())\n    \nelif isinstance(t, dict):\n    print('dict', list(t.keys() )); print()\n    \n    for k in t.keys():\n        print(t, t[k]); print()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-28T21:03:13.228362Z","iopub.execute_input":"2023-04-28T21:03:13.22878Z","iopub.status.idle":"2023-04-28T21:03:13.237592Z","shell.execute_reply.started":"2023-04-28T21:03:13.228743Z","shell.execute_reply":"2023-04-28T21:03:13.236233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# keywords","metadata":{}},{"cell_type":"code","source":"k = 'keywords'\nt = j[k]\nif isinstance(t, list):\n    \n    print('list:',len(t), t[0]);print()\n    if len(t) > 1:\n        print(t[1]); print()\n    print(t)\n#     for i in range(len(t)):\n#         print(t[i].keys())\n    \nelif isinstance(t, dict):\n    print('dict', list(t.keys() )); print()\n    \n    for k in t.keys():\n        print(t, t[k]); print()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-28T21:02:04.867059Z","iopub.execute_input":"2023-04-28T21:02:04.868551Z","iopub.status.idle":"2023-04-28T21:02:04.87661Z","shell.execute_reply.started":"2023-04-28T21:02:04.8685Z","shell.execute_reply":"2023-04-28T21:02:04.875346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# extraAttributes","metadata":{}},{"cell_type":"code","source":"k = 'extraAttributes'\nt = j[k]\nif isinstance(t, list):\n    \n    print('list:',len(t), t[0])\n    if len(t) > 1:\n        print(t[1])\n    print(t)\n#     for i in range(len(t)):\n#         print(t[i].keys())\n    \nelif isinstance(t, dict):\n    print('dict', list(t.keys() )); print()\n    \n    for k in t.keys():\n        print(t, t[k]); print()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:59:40.667208Z","iopub.execute_input":"2023-04-28T20:59:40.668107Z","iopub.status.idle":"2023-04-28T20:59:40.682615Z","shell.execute_reply.started":"2023-04-28T20:59:40.668054Z","shell.execute_reply":"2023-04-28T20:59:40.680839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Features","metadata":{}},{"cell_type":"code","source":"k = 'features'\nt = j[k]\nif isinstance(t, list):\n    \n    print('list:',len(t), t[0])\n    if len(t) > 1:\n        print(t[1])\n    print(t)\n#     for i in range(len(t)):\n#         print(t[i].keys())\n    \nelif isinstance(t, dict):\n    print('dict', list(t.keys() )); print()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:59:15.589663Z","iopub.execute_input":"2023-04-28T20:59:15.590081Z","iopub.status.idle":"2023-04-28T20:59:15.597397Z","shell.execute_reply.started":"2023-04-28T20:59:15.590038Z","shell.execute_reply":"2023-04-28T20:59:15.596375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# References","metadata":{}},{"cell_type":"code","source":"k = 'references'\nt = j[k]\nif isinstance(t, list):\n    \n    print('list:',len(t), t[0])\n    if len(t) > 1:\n        print(t[1])\n    for i in range(len(t)):\n        print(t[i].keys())\n    \n#     print(t)\nelif isinstance(t, dict):\n    print('dict', list(t.keys() ))\n\n# print((r.json()['genes'][0]['geneName']['value'] ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:56:01.728019Z","iopub.execute_input":"2023-04-28T20:56:01.728491Z","iopub.status.idle":"2023-04-28T20:56:01.736802Z","shell.execute_reply.started":"2023-04-28T20:56:01.728452Z","shell.execute_reply":"2023-04-28T20:56:01.73533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Genes","metadata":{}},{"cell_type":"code","source":"k = 'genes'\nt = j[k]\nif isinstance(t, list):\n    print(t)\nelif isinstance(t, dict):\n    print(list(t.keys() ))\n\nprint((r.json()['genes'][0]['geneName']['value'] ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:26:48.447007Z","iopub.execute_input":"2023-04-28T20:26:48.447412Z","iopub.status.idle":"2023-04-28T20:26:48.456502Z","shell.execute_reply.started":"2023-04-28T20:26:48.447378Z","shell.execute_reply":"2023-04-28T20:26:48.45513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Comments ","metadata":{}},{"cell_type":"code","source":"print(list(r.json()['comments'][0].keys() ))\nprint(list(r.json()['comments'][0]['texts'] ))\nprint((r.json()['comments'][0]['commentType'] ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:42:13.277739Z","iopub.execute_input":"2023-04-28T20:42:13.278826Z","iopub.status.idle":"2023-04-28T20:42:13.287649Z","shell.execute_reply.started":"2023-04-28T20:42:13.278778Z","shell.execute_reply":"2023-04-28T20:42:13.286016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(list(r.json()['comments'][1].keys() ))\nprint(list(r.json()['comments'][1]['texts'] ))\nprint((r.json()['comments'][1]['commentType'] ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:44:15.372897Z","iopub.execute_input":"2023-04-28T20:44:15.373358Z","iopub.status.idle":"2023-04-28T20:44:15.382697Z","shell.execute_reply.started":"2023-04-28T20:44:15.373316Z","shell.execute_reply":"2023-04-28T20:44:15.381471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(list(r.json()['comments'][2].keys() ))\n# print(list(r.json()['comments'][2]['texts'] ))\nprint(list(r.json()['comments'][2]['interactions'] ))\nprint((r.json()['comments'][2]['commentType'] ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:45:39.377135Z","iopub.execute_input":"2023-04-28T20:45:39.37755Z","iopub.status.idle":"2023-04-28T20:45:39.386596Z","shell.execute_reply.started":"2023-04-28T20:45:39.377512Z","shell.execute_reply":"2023-04-28T20:45:39.385133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 3\nprint(list(r.json()['comments'][i].keys() ))\n# print(list(r.json()['comments'][i]['texts'] ))\nprint(list(r.json()['comments'][i]['subcellularLocations'] ))\n# print(list(r.json()['comments'][i]['interactions'] ))\nprint((r.json()['comments'][i]['commentType'] ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:53:17.297185Z","iopub.execute_input":"2023-04-28T20:53:17.297876Z","iopub.status.idle":"2023-04-28T20:53:17.307915Z","shell.execute_reply.started":"2023-04-28T20:53:17.297827Z","shell.execute_reply":"2023-04-28T20:53:17.306826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 4\nprint(list(r.json()['comments'][i].keys() ))\nprint(list(r.json()['comments'][i]['texts'] ))\n# print(list(r.json()['comments'][i]['subcellularLocations'] ))\n# print(list(r.json()['comments'][i]['interactions'] ))\nprint((r.json()['comments'][i]['commentType'] ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:54:37.030513Z","iopub.execute_input":"2023-04-28T20:54:37.030904Z","iopub.status.idle":"2023-04-28T20:54:37.039085Z","shell.execute_reply.started":"2023-04-28T20:54:37.03087Z","shell.execute_reply":"2023-04-28T20:54:37.038222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 5\nprint(list(r.json()['comments'][i].keys() ))\nprint(list(r.json()['comments'][i]['texts'] ))\n# print(list(r.json()['comments'][i]['subcellularLocations'] ))\n# print(list(r.json()['comments'][i]['interactions'] ))\nprint((r.json()['comments'][i]['commentType'] ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:55:08.135717Z","iopub.execute_input":"2023-04-28T20:55:08.136644Z","iopub.status.idle":"2023-04-28T20:55:08.144961Z","shell.execute_reply.started":"2023-04-28T20:55:08.136602Z","shell.execute_reply":"2023-04-28T20:55:08.143631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 6\nprint(list(r.json()['comments'][i].keys() ))\nprint(list(r.json()['comments'][i]['texts'] ))\n# print(list(r.json()['comments'][i]['subcellularLocations'] ))\n# print(list(r.json()['comments'][i]['interactions'] ))\nprint((r.json()['comments'][i]['commentType'] ))","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:55:27.650423Z","iopub.execute_input":"2023-04-28T20:55:27.651899Z","iopub.status.idle":"2023-04-28T20:55:27.660973Z","shell.execute_reply.started":"2023-04-28T20:55:27.65184Z","shell.execute_reply":"2023-04-28T20:55:27.659559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}