#!/usr/bin/env python# -*- coding: utf-8 -*-## This file is part of the `pypath` python module## Copyright 2014-2023# 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/#fromfuture.utilsimportiteritemsfrompast.builtinsimportxrange,rangeimportreimportcsvimportcollectionsimportitertoolsimportpypath.share.curlascurlimportpypath.resources.urlsasurlsimportpypath.utils.mappingasmappingimportpypath.internals.interaasinteraimportpypath.share.commonascommonimportpypath.share.sessionassessionimportpypath.inputs.uniprot_dbasuniprot_db_logger=session.Logger(name='cellphonedb_input')_log=_logger._logCellPhoneDBAnnotation=collections.namedtuple('CellPhoneDBAnnotation',('receptor','receptor_class','peripheral','secreted','secreted_class','transmembrane','integrin',))
[docs]defcellphonedb_ligands_receptors():""" Retrieves the set of ligands and receptors from CellPhoneDB. Returns tuple of sets. """receptors=set()ligands=set()proteins=cellphonedb_protein_annotations()complexes=cellphonedb_complex_annotations()for_id,annotinitertools.chain(iteritems(proteins),iteritems(complexes)):ifannot.receptor:receptors.add(_id)ifannot.secretedor(notannot.receptorand(annot.transmembraneorannot.peripheral)):ligands.add(_id)returnligands,receptors
[docs]defcellphonedb_protein_annotations(add_complex_annotations=False):""" :arg bool add_complex_annotations: Deprecated because results wrong annotations. Copy the annotations of complexes to each of their member proteins. """defname_method(rec):uniprot=rec['uniprot']uniprot=_cellphonedb_hla(uniprot)uniprot=mapping.map_names(uniprot,'uniprot','uniprot')returnuniprotprotein_annotations=_cellphonedb_annotations(url=urls.urls['cellphonedb_git']['proteins'],name_method=name_method,)returnprotein_annotations
def_cellphonedb_hla(uniprot):""" Returns *set*. """uniprots=None# for HLA genes in the uniprot column we have "HLA..." gene symbols# but not always in the standard format (with dash)ifuniprot.startswith('HLA')and'-'notinuniprot:genesymbol='HLA-%s'%uniprot[3:]uniprots=mapping.map_name(genesymbol,'genesymbol','uniprot')returnuniprotsor{uniprot}
[docs]defcellphonedb_interactions():""" Interactions between ligands and receptors from CellPhoneDB. Yields: Named tuples representing interactions. """defget_type(entity):return('ligand'ifentityinligandselse'receptor'ifentityinreceptorselse'unknown')defget_bool(rec,attr):returnattrinrecandrec[attr].upper()=='TRUE'CellphonedbInteraction=collections.namedtuple('CellphonedbInteraction',['id_a','id_b','sources','references','interaction_type','type_a','type_b','is_ppi',])repmid=re.compile(r'PMID: ([0-9]+)')recomma=re.compile(r'[,;]')ligands,receptors=cellphonedb_ligands_receptors()complexes=dict((_id,cplex)forcplexincellphonedb_complexes().values()for_idincplex.ids['CellPhoneDB'])url=urls.urls['cellphonedb_git']['interactions']c=curl.Curl(url,silent=False,large=True)reader=csv.DictReader(c.result)forrecinreader:_partner_a=_cellphonedb_get_entity(rec['partner_a'],complexes=complexes,)_partner_b=_cellphonedb_get_entity(rec['partner_b'],complexes=complexes,)_is_ppi=get_bool(rec,'is_ppi')forpartner_a,partner_binitertools.product(_partner_a,_partner_b):type_a=get_type(partner_a)type_b=get_type(partner_b)rev=type_b=='ligand'andtype_a=='receptor'_type_a=type_bifrevelsetype_a_type_b=type_aifrevelsetype_bsources=('CellPhoneDB'ifrec['annotation_strategy']=='curated'else'%s;CellPhoneDB'%(';'.join(recomma.split(rec['annotation_strategy'].replace('guidetopharmacology.org','Guide2Pharma')))))refs=';'.join(repmid.findall(rec['source']))yield(CellphonedbInteraction(id_a=partner_bifrevelsepartner_a,id_b=partner_aifrevelsepartner_b,sources=sources,references=refs,interaction_type='%s-%s'%(_type_a,_type_b),type_a=_type_a,type_b=_type_b,is_ppi=_is_ppi,))