#!/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/#"""This is a standalone module with the only purpose ofbuilding the tables for the webservice."""fromfuture.utilsimportiteritemsimportosimportimportlibasimpimportcopyimportpandasaspdimportpypath.core.enz_subasenz_subimportpypath.core.complexascompleximportpypath.core.annotasannotimportpypath.core.intercellasintercellimportpypath.omnipath.exportasexportimportpypath.resources.data_formatsasdata_formatsimportpypath.share.sessionassession_modimportpypath.omnipathasomnipathNO_HOMOLOGY_TRANSLATION={'mirna_mrna','lncrna_mrna','tf_mirna','small_molecule',}
[docs]classWebserviceTables(session_mod.Logger):""" Creates the data frames which the web service uses to serve the data from. """
defreload(self):modname=self.__class__.__module__mod=__import__(modname,fromlist=[modname.split('.')[0]])imp.reload(mod)new=getattr(mod,self.__class__.__name__)setattr(self,'__class__',new)defmain(self):self.interactions()self.enz_sub()self.complexes()self.annotations()self.intercell()definteractions(self):self._log('Building `interactions` data frame.')dataframes=[]fordatasetinself.network_datasets:self._log('Building `%s` interactions.'%dataset)netw=omnipath.db.get_db(dataset)exp=export.Export(netw)exp.webservice_interactions_df()dataframes.append(exp.df)ifdatasetnotinNO_HOMOLOGY_TRANSLATION:forrodentin(10090,10116):self._log('Translating `%s` interactions to organism `%u`'%(dataset,rodent,))rodent_netw=netw.homology_translate(rodent)exp=export.Export(rodent_netw)exp.webservice_interactions_df()dataframes.append(exp.df)delrodent_netwdelexpdelnetwomnipath.db.remove_db(dataset)self.df_interactions=pd.concat(dataframes)self.df_interactions.to_csv(self.outfile_interactions,sep='\t',index=False)self._log('Data frame `interactions` has been exported to `%s`.'%(self.outfile_interactions,))definteractions_legacy(self):self._log('Building `interactions` data frame from ''`legacy.main.PyPath` object.')importpypath.legacy.mainasmaindataframes=[]tf_target=copy.deepcopy(data_formats.transcription)tf_target['dorothea'].input_args['levels']={'A','B','C','D',}tf_target['dorothea'].must_have_references=Falseparam={'PPI':('load_omnipath',{'kinase_substrate_extra':True,'ligand_receptor_extra':True,'pathway_extra':True,},),'TF-target':('init_network',{'lst':tf_target},),'miRNA-target':('init_network',{'lst':data_formats.mirna_target},),'lncRNA-target':('init_network',{'lst':data_formats.lncrna_target},)}forname,(to_call,kwargs)initeritems(param):self._log('Building %s interactions.'%name)pa=main.PyPath()getattr(pa,to_call)(**kwargs)e=export.Export(pa)e.webservice_interactions_df()dataframes.append(e.df)ifnotself.only_humanandname!='lncRNA-target':graph_human=Noneforrodentin(10090,10116):self._log('Translating %s interactions to organism `%u`'%(name,rodent,))ifpa.ncbi_tax_id==9606:ifpa.graph.ecount()<100000:graph_human=copy.deepcopy(pa.graph)else:ifgraph_human:pa.graph=graph_humanpa.ncbi_tax_id=9606pa.genesymbol_labels(remap_all=True)pa.update_vname()else:deledelpapa=main.PyPath()getattr(pa,to_call)(**kwargs)pa.orthology_translation(rodent)e=export.Export(pa)e.webservice_interactions_df()dataframes.append(e.df)deledelpaself.df_interactions=pd.concat(dataframes)self.df_interactions.to_csv(self.outfile_interactions,sep='\t',index=False)self._log('Data frame `interactions` has been exported to `%s`.'%(self.outfile_interactions,))defenz_sub(self):self._log('Building `enz_sub` data frame.')dataframes=[]self._log('Building `enz_sub` data frame for organism `9606`.')enz_sub_a=omnipath.db.get_db('enz_sub')enz_sub_a.make_df(tax_id=True)dataframes.append(enz_sub_a.df)self._log('Finished building `enz_sub` data frame for organism `9606`.')omnipath.db.remove_db('enz_sub',ncbi_tax_id=9606)ifnotself.only_human:forrodentin(10090,10116):self._log('Building `enz_sub` data frame for ''organism `%s`.'%rodent)enz_sub_a=omnipath.db.get_db('enz_sub',ncbi_tax_id=rodent,)enz_sub_a.make_df(tax_id=True)dataframes.append(enz_sub_a.df)self._log('Finished building `enz_sub` data frame for ''organism `%s`.'%rodent)omnipath.db.remove_db('enz_sub',ncbi_tax_id=rodent)delenz_sub_aself.df_enz_sub=pd.concat(dataframes)self.df_enz_sub.to_csv(self.outfile_enz_sub,sep='\t',index=False)self._log('Data frame `enz_sub` has been exported to `%s`.'%(self.outfile_enz_sub,))defcomplexes(self):self._log('Building `complexes` data frame.')co=omnipath.db.get_db('complex')co.make_df()self.df_complexes=co.dfdelcoself.df_complexes.to_csv(self.outfile_complexes,sep='\t',index=False,)self._log('Data frame `complexes` has been exported to `%s`.'%(self.outfile_complexes,))defannotations(self):self._log('Building `annotations` data frame.')an=omnipath.db.get_db('annotations')an.make_narrow_df()self.df_annotations=an.narrow_dfself.df_annotations.to_csv(self.outfile_annotations,sep='\t',index=False,)self._log('Data frame `annotations` has been exported to `%s`.'%(self.outfile_annotations,))defintercell(self):self._log('Building `intercell` data frame.')i=omnipath.db.get_db('intercell')i.make_df()self.df_intercell=i.dfself.df_intercell.to_csv(self.outfile_intercell,sep='\t',index=False,)deliomnipath.db.remove_db('intercell')omnipath.db.remove_db('complex')omnipath.db.remove_db('annotations')self._log('Data frame `intercell` has been exported to `%s`.'%(self.outfile_intercell,))