#!/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/#fromtypingimportUnionimportreimportcollectionsimportpypath.utils.mappingasmappingimportpypath.share.curlascurlimportpypath.resources.urlsasurlsimportpypath.formats.oboasobo
[docs]defhpo_annotations()->dict[str,set[tuple]]:""" Human Phenotype Ontology annotations. Returns Dict of proteins as keys and sets of HPO annotations as values. """url=urls.urls['hpo']['gene']c=curl.Curl(url,large=True,silent=False)_=next(c.result)fields=('entrez_gene_id','entrez_gene_symbol','hpo_id')HpoAnnotation=collections.namedtuple('HpoAnnotation',fields,defaults=("",)*len(fields))result=collections.defaultdict(set)forrinc.result:r=r.strip().split('\t')uniprots=mapping.map_name(r[0],'entrez','uniprot')foruniprotinuniprots:result[uniprot].add(HpoAnnotation(entrez_gene_id=r[0],entrez_gene_symbol=r[1],hpo_id=r[2],))returndict(result)
[docs]defhpo_terms()->dict[str,str]:""" Human Phenotype Ontology accession to term mapping. """returnhpo_ontology()['terms']
[docs]defhpo_diseases()->dict[str,set[tuple]]:""" HPO term-disease relationships from Human Phenotype Ontology. Returns A set of disease records for each HPO term. """url=urls.urls['hpo']['disease']c=curl.Curl(url,large=True,silent=False)HpoDisease=collections.namedtuple('HpoDisease',('omim','name','pmid','qualifier','evidence','onset','frequency','sex','modifier','aspect',),)result=collections.defaultdict(set)forrinc.result:ifr[0]=='#':continuer=r.split('\t')pmid=re.sub('^PMID:','',r[4])ifr[4][:4]=='PMID'elseNoneresult[r[3]].add(HpoDisease(omim=r[0],name=r[1],pmid=pmid,qualifier=r[2]orNone,evidence=r[5]orNone,onset=r[6]orNone,frequency=r[7]orNone,sex=r[8]orNone,modifier=r[9]orNone,aspect=r[10],))returndict(result)
[docs]defhpo_ontology()->dict[str,dict[str,Union[str,set[str]]]]:""" Ontology data from HPO. Returns Five dictionaries with term names, term definitions, parents in the ontology tree, term synonyms and cross references to other databases. The dicts "terms" and "defs" are one-to-one, while "parents", "synonyms" and "xrefs" are one-to-many mappings, the keys are always HPO terms. """url=urls.urls['hpo']['ontology']reader=obo.Obo(url)result={'terms':{},'defs':{},'parents':collections.defaultdict(set),'synonyms':collections.defaultdict(set),'xrefs':collections.defaultdict(set),}forrinreader:ifr.stanza!='Term':continueif(r.nameisNoneorr.name.value=='obsolete'orr.attrs.get('is_obsolete')):continueterm=r.id.valuename=((r.name.value,r.name.modifiers)ifr.name.modifierselser.name.value)ifisinstance(name,tuple):name=' '.join(nforninnameifn)result['terms'][term]=nameresult['defs'][term]=r.definition.valueifr.definitionelseNoneforkey,obokeyin(('parents','is_a'),('synonyms','synonym'),('xrefs','xref'),):proc=(lambdax:tuple(x.split(':'))ifkey=='xrefs'elselambdax:x)forxinr.attrs.get(obokey,()):y=proc(x.value)result[key][term].update({y(x.value)iftype(y)!=tupleelsey})return{k:dict(v)fork,vinresult.items()}