#!/usr/bin/env python# -*- coding: utf-8 -*-## This file is part of the `pypath` python module## Copyright 2014-2024# EMBL, EMBL-EBI, Uniklinik RWTH Aachen, Heidelberg University## Authors: see the file `README.rst`# Contact: Dénes Türei (turei.denes@gmail.com)## Distributed under the GPLv3 License.# See accompanying file LICENSE.txt or copy at# https://www.gnu.org/licenses/gpl-3.0.html## Website: https://pypath.omnipathdb.org/#from__future__importannotationsimportjsonimportpandasaspdimportpypath.resources.urlsasurlsimportpypath.share.curlascurl
[docs]defidtypes(pairs:bool=True,raw:bool=False,)->dict[str,pd.DataFrame]|set[tuple[str,str]]|dict:""" Identifier types in the UniProt ID mapping service. Args: pairs: Process the data into pairs of identifiers. raw: Return the raw data as extracted from JSON. Returns: The JSON contents as a dict if `raw` is `True`, a list of tuples if `pairs` is `True`, otherwise a set of tuples of ID types. """url=urls.urls['uniprot_idmapping']['fields']c=curl.Curl(url,large=False,silent=False)data=json.loads(c.result)ifraw:returndatagroups=(pd.DataFrame(data['groups']).explode('items').reset_index(drop=True))groups=(pd.concat([groups['groupName'],pd.DataFrame(groups['items'].tolist())],axis=1,).rename(columns={'from':'from_'}))rules=pd.DataFrame(data['rules'])ifnotpairs:return{'groups':groups,'rules':rules}rules={int(r.ruleId):r.tosforrinrules.itertuples()}groups.fillna(-1.,inplace=True)result=set()foridtypeingroups.itertuples():tos=rules.get(int(idtype.ruleId),[])from_to={(idtype.name,t)fortintos}ifidtype.from_:result.update(from_to)ifidtype.to:result.update({t[::-1]fortinfrom_to})returnresult