#!/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/#"""Provides classes for representing molecular entities and their collections.A molecular entity is defined by its identifier, type and taxon."""fromfuture.utilsimportiteritemsimportitertoolsimportimportlibasimpimportcollectionsimportpypath.share.commonascommonimportpypath_common._constantsas_constimportpypath.share.sessionassession_modimportpypath.utils.mappingasmappingimportpypath.share.settingsassettingsimportpypath.core.attrsasattrs_modEntityKey=collections.namedtuple('EntityKey',['identifier','id_type','entity_type','taxon',])
[docs]classEntity(session_mod.Logger,attrs_mod.AttributeHandler):""" Represents a molecular entity such as protein, miRNA, lncRNA or small molecule. :arg str identifier: An identifier from the reference database e.g. UniProt ID for proteins. :arg str entity_type: The type of the molecular entity, defaults to ``'protein'``. :arg str id_type: The type of the identifier (the reference database), default is ``'uniprot'``. :arg int taxon: The NCBI Taxonomy Identifier of the molecular entity, e.g. ``9606`` for human. Use ``0`` for non taxon specific molecules e.g. metabolites or drug compounds. :arg NoneType,dict attrs: A dictionary of additional attributes. """__slots__=['identifier','entity_type','id_type','taxon','label','key',]_default_id_types=settings.get('default_name_types')_smol_types=settings.get('small_molecule_entity_types')_id_type_to_entity_type={'uniprot':'protein','genesymbol':'protein','mir-name':'mirna','mir-mat-name':'mirna','mir-pre':'mirna','mir-mat':'mirna','lncrna-genesymbol':'lncrna',}_label_types=set(mapping.Mapper.label_type_to_id_type.keys())
[docs]@classmethoddeffilter_entity_type(cls,entities,entity_type):""" Filters an iterable of entities or identifiers keeping only the ones of type(s) in ``entity_type``. :param iterable entities: A list, set, tuple or other iterable yielding entities or identifiers. :param str,set entity_type: One or more entity types e.g. ``{'protein', 'mirna'}``. :returns: Same type of object as ``entities`` if the type of the object is list, set or tuple, otherwise a generator. """ifnotentity_typeornotentities:returnentitiesentity_type=common.to_set(entity_type)obj_type=(type(entities)ifisinstance(entities,_const.LIST_LIKE)elselambdax:x)returnobj_type(eforeinentitiesifcls._get_entity_type(e)inentity_type)
[docs]@classmethoddefcount_entity_type(cls,entities,entity_type):""" Counts elements in an iterable of entities or identifiers of type(s) in ``entity_type``. :param iterable entities: A list, set, tuple or other iterable yielding entities or identifiers. :param str,set entity_type: One or more entity types e.g. ``{'protein', 'mirna'}``. :returns: int """entities=(entitiesifisinstance(entities,_const.LIST_LIKE)elselist(entities))returnlen(cls.filter_entity_type(entities,entity_type=entity_type,))