#!/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/#from__future__importannotations"""Generic objects for representing resources."""fromfuture.utilsimportiteritemsfromtypingimportIterable,Mapping,TYPE_CHECKINGifTYPE_CHECKING:importpypath.internals.licenseasLicenseimportosimportcollectionsimportcopytry:importcPickleaspickleexcept:importpickleimportpypath.inputsasinputsimportpypath.share.commonascommonimportpypath.share.sessionassession_mod
[docs]classAbstractResource(session_mod.Logger):""" Generic class for downloading, processing and serving data from a resource. """
[docs]def__init__(self,name,ncbi_tax_id=9606,input_method=None,input_args=None,dump=None,data_attr_name=None,**kwargs):""" name : str Custom name for the resource. input_method : callable Method providing the input data. """ifnothasattr(self,'_log_name'):session_mod.Logger.__init__(self,name='resource')self.dump=dumpself.name=nameself._data_attr_name=data_attr_nameor'data'self._input_method=input_methodself.input_args=input_argsor{}self.ncbi_tax_id=ncbi_tax_id
[docs]defset_method(self):""" Sets the data input method by looking up in ``inputs`` module if necessary. """ifcallable(self._input_method):self.input_method=self._input_methodelifself._input_method:self.input_method=inputs.get_method(self._input_method)
[docs]defload_data(self):""" Loads the data by calling ``input_method``. """self._log('Loading data from `%s`.'%self.name)self.set_method()ifhasattr(self,'input_method'):self.data=self.input_method(**self.input_args)
[docs]defprocess(self):""" Calls the ``_process_method``. """self._log('Processing data from `%s`.'%self.name)self._process_method()
[docs]classNetworkResourceKey(collections.namedtuple('NetworkResourceKeyBase',['name','data_type','interaction_type','data_model','via',])):def__new__(cls,*args,**kwargs):returnsuper(NetworkResourceKey,cls).__new__(cls,*args,**kwargs)@propertydeflabel(self):""" Returns (str): A label containing the resource name, and if it's a secondary resource, the name of the primary resource separated by an underscore. """return'%s_%s'%(self.name,self.via)ifself.viaelseself.name@propertydeflast(self):""" Returns (str): The name of the resource where the data directly came from ignoring the primary resource. """returnself.viaorself.name
[docs]def__init__(self,name:str,resources:dict|list|None=None,):""" A set of network resources. Formerly the network datasets were represented by dicts. This is only a thin wrapper around that solution to better organise metadata of the datasets and resources within. """self._name=nameself._resources={}self.add(resources)
[docs]defget_via(self,name):""" Returns a copy of the same resource attributes but the ``name`` set to ``name`` and the ``via`` set to the original name. This means the data comes from the resource ``name`` via the resource ``via``. """args=dict((k,getattr(self,k))forkinself.__dir__()if(notk.startswith('__')andnotcallable(getattr(self,k))))args['via']=self.nameargs['name']=name_=args.pop('data_type',None)_=args.pop('key',None)returnEnzymeSubstrateResource(**args)