#!/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/#importreimportitertoolsimportcollectionsimportpypath.share.curlascurlimportpypath.resources.urlsasurlsimportpypath.utils.taxonomyastaxonomyimportpypath.utils.mappingasmappingimportpypath.internals.interaasinteraimportpypath.inputs.pubmedaspubmedimportpypath.inputs.rdataasrdata
[docs]defcellchatdb_download(organism=9606,dataset='CellChatDB'):""" Retrieves data from CellChatDB, extracts fhe R objects from the RDA format and returns the raw data. :param int,str organism: Human and mouse are available, for incomprehensive values falls back to human. :param str dataset: Either *CellChatDB* or *PPI*. """organism=taxonomy.ensure_common_name(organism)organism=('mouse'iforganismandorganism.lower()=='mouse'else'human')dataset='PPI'ifdataset.lower()=='ppi'else'CellChatDB'key='%s.%s'%(dataset,organism)url=urls.urls['cellchatdb']['url']%keyc=curl.Curl(url,silent=False,large=True)rdata_path=c.fileobj.namec.fileobj.close()rdata_parsed=rdata.rdata.parser.parse_file(rdata_path)rdata_converted=rdata.rdata.conversion.convert(rdata_parsed)[key]ifdataset=='CellChatDB':df_names=rdata._rdata_list_get_names(rdata_parsed.object.value[0])rownames=[rdata._rdata_data_frame_get_rownames(df_obj)fordf_objinrdata_parsed.object.value[0].value]rownames=dict(zip(df_names,rownames))forname,dfinrdata_converted.items():df['rownames']=rownames[name]returnrdata_converted
[docs]defcellchatdb_complexes(organism=9606):""" Retrieves data from CellChatDB and processes protein complexes. :param int,str organism: Human and mouse are available, for incomprehensive values falls back to human. :return: Dictionary of complexes. """raw=cellchatdb_download(organism=organism)['complex']return_cellchatdb_process_complexes(raw,organism=organism)
[docs]defcellchatdb_interactions(organism=9606,ligand_receptor=True,cofactors=True,):""" Retrieves data from CellChatDB and processes interactions. :param int,str organism: Human and mouse are available, for incomprehensive values falls back to human. :param bool ligand_receptor: Include ligand-receptor interactions. :param bool cofactors: Include interactions between cofactors (agonists, antagonists), coreceptors and receptors. :return: Interactions as list of named tuples. """defprocess_name(name):return((complexes_by_name[name],)ifnameincomplexes_by_nameelsemapping.map_name(name,'genesymbol','uniprot',ncbi_tax_id=ncbi_tax_id,))CellChatDBInteraction=collections.namedtuple('CellChatDBInteraction',['id_a','id_b','role_a','role_b','effect','pathway','refs','category',])repmid=re.compile('PMID:\s*?(\d+)')repmcid=re.compile('PMC\d+')ncbi_tax_id=_cellchatdb_organism(organism)raw=cellchatdb_download(organism=organism)_complexes=_cellchatdb_process_complexes(raw,organism=organism)_cofactors=_cellchatdb_process_cofactors(raw,organism=organism)complexes_by_name=dict((cplex.name,cplex)forcplexin_complexes.values())raw_ia=raw['interaction']result=[]forrowinraw_ia.itertuples():refs=(set(repmid.findall(row.evidence))|set(pubmed.only_pmids(repmcid.findall(row.evidence))))# PMIDs starting with 23209 are a mistake in CellChatDBrefs=sorted(pmidforpmidinrefsifnotpmid.startswith('23209'))ligands=process_name(row.ligand)receptors=process_name(row.receptor)ifligand_receptor:i=0forligand,receptorinitertools.product(ligands,receptors):result.append(CellChatDBInteraction(id_a=ligand,id_b=receptor,role_a='ligand',role_b='receptor',effect='unknown',pathway=row.pathway_name,refs=refs,category=row.annotation,))ifcofactors:forcofactor_col,effect,targetsin(('agonist','stimulation',ligands),('antagonist','inhibition',ligands),('co_A_receptor','stimulation',receptors),('co_I_receptor','inhibition',receptors),):cofact_label=getattr(row,cofactor_col)ifcofact_labelnotin_cofactors:continueforcofactor,targetinitertools.product(_cofactors[cofact_label],targets):result.append(CellChatDBInteraction(id_a=cofactor,id_b=target,role_a=cofactor_col,role_b=('ligand'ifcofactor_col.endswith('onist')else'receptor'),effect=effect,pathway=row.pathway_name,refs=set(),category=row.annotation,))returnresult