{"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### Parsing UniProt response for protein localization and organism information (name, common name, taxon ID, lineage)\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\nimport time\nt0start = time.time() \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nfrom tqdm import tqdm\nimport requests, sys\nimport pandas as pd\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-06-14T11:46:05.020266Z","iopub.execute_input":"2023-06-14T11:46:05.020709Z","iopub.status.idle":"2023-06-14T11:46:05.033768Z","shell.execute_reply.started":"2023-06-14T11:46:05.020675Z","shell.execute_reply":"2023-06-14T11:46:05.03284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfn = '/kaggle/input/t5embeds/train_ids.npy'\nvec_train_protein_ids = np.load(fn)\nprint(vec_train_protein_ids.shape)\nvec_train_protein_ids\ndf = pd.DataFrame({'uniprot_id' : vec_train_protein_ids})\ndf.to_csv('train_protein_ids.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T11:46:05.0369Z","iopub.execute_input":"2023-06-14T11:46:05.037691Z","iopub.status.idle":"2023-06-14T11:46:05.392474Z","shell.execute_reply.started":"2023-06-14T11:46:05.037648Z","shell.execute_reply":"2023-06-14T11:46:05.391147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"WEBSITE_API = 'https://rest.uniprot.org'\n\nuniprot_proteins_data = {'UniProt_id': list(), 'Localization': list(), 'Organism': list(),\n                         'commonName': list(), 'taxonId': list(), 'lineage': list(), 'Sequence': list()}\n\ndef get_url(url, **kwargs):\n    response = requests.get(url, **kwargs)\n    if not response.ok:\n        response.raise_for_status()\n        sys.exit()\n    return response\n","metadata":{"execution":{"iopub.status.busy":"2023-06-14T11:46:05.394044Z","iopub.execute_input":"2023-06-14T11:46:05.394434Z","iopub.status.idle":"2023-06-14T11:46:05.402065Z","shell.execute_reply.started":"2023-06-14T11:46:05.394404Z","shell.execute_reply":"2023-06-14T11:46:05.400788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_uniprot_resp_by_protein_id():\n    for protein_id in tqdm(vec_train_protein_ids):\n        try:\n            r = get_url(f'{WEBSITE_API}/uniprotkb/{protein_id}')\n            yield r, protein_id\n        except:\n            print(f'Exception with id {id}')\n            continue\n\n\ndef get_organism_info(response):\n    if response.json().get('organism'):\n        organism = response.json().get('organism')\n        uniprot_proteins_data['Organism'].append(organism.get('scientificName'))\n        uniprot_proteins_data['commonName'].append(organism.get('commonName'))\n        uniprot_proteins_data['taxonId'].append(organism.get('taxonId'))\n        uniprot_proteins_data['lineage'].append(organism.get('lineage'))\n\n\ndef get_localization_info(response):\n    cellular_component_types = set()\n    if response.json().get('keywords'):\n        for item in response.json().get('keywords'):\n            if item.get('category') == 'Cellular component':\n                cellular_component_types.add(item.get('name'))\n        if not cellular_component_types:\n            if response.json().get('comments'):\n                for item in response.json().get('comments'):\n                    if item.get('subcellularLocations'):\n                        cellular_component_types.add(item.get('subcellularLocations')[0].get('location').get('value'))\n    id_components = ','.join([str(i) for i in cellular_component_types])\n    uniprot_proteins_data['Localization'].append(id_components)\n\n\ndef get_protein_sequence(response):\n    if response.json().get('sequence'):\n        sequence = response.json().get('sequence').get('value')\n        uniprot_proteins_data['Sequence'].append(sequence)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T11:46:05.403337Z","iopub.execute_input":"2023-06-14T11:46:05.403651Z","iopub.status.idle":"2023-06-14T11:46:05.421113Z","shell.execute_reply.started":"2023-06-14T11:46:05.403626Z","shell.execute_reply":"2023-06-14T11:46:05.419593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for resp, protein_id in get_uniprot_resp_by_protein_id():\n    uniprot_proteins_data['UniProt_id'].append(protein_id)\n    get_organism_info(resp)\n    get_localization_info(resp)\n    get_protein_sequence(resp)\n\ndf = pd.DataFrame(uniprot_proteins_data)\ndf.to_csv('protein_localization.csv', index=False, sep='\\t')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T11:46:05.42438Z","iopub.execute_input":"2023-06-14T11:46:05.425261Z","iopub.status.idle":"2023-06-14T11:46:13.347461Z","shell.execute_reply.started":"2023-06-14T11:46:05.425213Z","shell.execute_reply":"2023-06-14T11:46:13.346185Z"},"trusted":true},"execution_count":null,"outputs":[]}]}