#!/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/#fromfuture.utilsimportiteritemsfrompast.builtinsimportxrange,rangeimportsysimportjsonimportwebbrowserimportpypath.resources.urlsasurlsimportpypath.share.curlascurlimportpypath_common._constantsas_constimportpypath.share.progressasprogressimportpypath.inputs.eutilsaseutils
[docs]defopen_pubmed(pmid):""" Opens PubMed record in web browser. @pmid : str or int PubMed ID """pmid=str(pmid)url=urls.urls['pubmed']['url']%pmidwebbrowser.open(url)
[docs]defonly_pmids(idList,strict=True):""" Return elements unchanged which comply with the PubMed ID format, and attempts to translate the DOIs and PMC IDs using NCBI E-utils. Returns list containing only PMIDs. @idList : list, str List of IDs or one single ID. @strict : bool Whether keep in the list those IDs which are not PMIDs, neither DOIs or PMC IDs or NIH manuscript IDs. """iftype(idList)in_const.SIMPLE_TYPES:idList=[idList]pmids={iforiinidListifisinstance(i,int)ori.isdigit()}pmcids=[iforiinidListifi.startswith('PMC')]dois=[iforiinidListif'/'ini]manuscids=[iforiinidListifi.startswith('NIHMS')]ifnotstrict:pmids=set(pmids)|set(dois)|set(pmcids)|set(manuscids)iflen(pmcids)>0:pmids=pmids|set(pmids_list(pmcids))iflen(dois)>0:pmids=pmids|set(pmids_list(dois))returnlist(pmids)
[docs]defget_pmid(idList):""" For a list of doi or PMC IDs fetches the corresponding PMIDs. """iftype(idList)in_const.SIMPLE_TYPES:idList=[idList]url=urls.urls['eutils']['pmc-idconv']%','.join(str(i)foriinidList)c=curl.Curl(url,silent=True)data=c.resulttry:js=json.loads(data)except:js={}returnjs
[docs]defget_pubmeds(pmids:list[str],cache_small:int=10)->dict:""" Metadata about PubMed records. Args: pmids: One or more PubMed IDs. cache_small: Small requests querying less than 10 IDs by default are not cached, except if this parameter is True or is set to a lower number. """returneutils.esummary(ids=pmids,db='pubmed',cache_small=cache_small,)