Exemplo n.º 1
0
def test_api_key():
    """tests that the API key is being used"""
    qs = QueryService()
    assert b"DbName" in qs.einfo()

    qs = QueryService(api_key="bogus")
    with pytest.raises(EutilsRequestError):
        qs.einfo()
Exemplo n.º 2
0
def test_api_key():
    """tests that the API key is being used"""
    qs = QueryService()
    assert b"DbName" in qs.einfo()

    qs = QueryService(api_key="bogus")
    with pytest.raises(EutilsRequestError):
        qs.einfo()
Exemplo n.º 3
0
class Client(object):

    def __init__(self,
                 cache_path=default_cache_path,
                 request_interval=0.4
                 ):
        self._qs = QueryService(cache_path=cache_path, request_interval=request_interval)
        #self.databases = self.einfo().databases


    def einfo(self,db=None):
        """query the einfo endpoint

        :param db: string (optional)
        :rtype: EInfo or EInfoDB object

        If db is None, the reply is a list of databases, which is returned
        in an EInfo object (which has a databases() method).

        If db is not None, the reply is information about the specified
        database, which is returned in an EInfoDB object.  (Version 2.0
        data is automatically requested.)
        """

        if db is None:
            return EInfo( self._qs.einfo() )
        return EInfoDB( self._qs.einfo({'db':db, 'version':'2.0'}) )
        

    def esearch(self, db, term, **kw):
        """query the esearch endpoint
        """
        kw.update({'db':db,'term':term})
        return ESearchResults( self._qs.esearch(kw) )


    def efetch(self, db, id=None, webenv=None, query_key=None, **kw):
        """query the efetch endpoint
        """
        db = db.lower()
        kw.update({'db':db})
        if id:
            kw.update({'id': id})
        else:
            kw.update({'webenv': webenv, 'query_key': query_key})
        xml = self._qs.efetch(kw)
        if db in ['gene']:
            return Gene(xml)
        if db in ['nuccore']:
            # TODO: GBSet is misnamed; it should be GBSeq and get the GBSeq XML node as root (see gbset.py)
            return GBSet(xml)
        if db in ['pubmed']:
            return PubMedArticleSet(xml)
        if db in ['snp']:
            return ExchangeSet(xml)
        raise EutilsError('database {db} is not currently supported by eutils'.format(db=db))
Exemplo n.º 4
0
class Client(object):
    def __init__(self, cache_path=default_cache_path, request_interval=0.4):
        self._qs = QueryService(cache_path=cache_path,
                                request_interval=request_interval)
        #self.databases = self.einfo().databases

    def einfo(self, db=None):
        """query the einfo endpoint

        :param db: string (optional)
        :rtype: EInfo or EInfoDB object

        If db is None, the reply is a list of databases, which is returned
        in an EInfo object (which has a databases() method).

        If db is not None, the reply is information about the specified
        database, which is returned in an EInfoDB object.  (Version 2.0
        data is automatically requested.)
        """

        if db is None:
            return EInfo(self._qs.einfo())
        return EInfoDB(self._qs.einfo({'db': db, 'version': '2.0'}))

    def esearch(self, db, term, **kw):
        """query the esearch endpoint
        """
        kw.update({'db': db, 'term': term})
        return ESearchResults(self._qs.esearch(kw))

    def efetch(self, db, id=None, webenv=None, query_key=None, **kw):
        """query the efetch endpoint
        """
        db = db.lower()
        kw.update({'db': db})
        if id:
            kw.update({'id': id})
        else:
            kw.update({'webenv': webenv, 'query_key': query_key})
        xml = self._qs.efetch(kw)
        if db in ['gene']:
            return Gene(xml)
        if db in ['nuccore']:
            # TODO: GBSet is misnamed; it should be GBSeq and get the GBSeq XML node as root (see gbset.py)
            return GBSet(xml)
        if db in ['pubmed']:
            return PubMedArticleSet(xml)
        if db in ['snp']:
            return ExchangeSet(xml)
        raise EutilsError(
            'database {db} is not currently supported by eutils'.format(db=db))
Exemplo n.º 5
0
class Client(object):
    """class-based access to NCBI E-Utilities, returning Python classes
    with rich data accessors

    """
    def __init__(self, cache_path=default_cache_path):
        """
        :param str cache_path: full path to sqlite database file (created if necessary)
        :raises EutilsError: if cache file couldn't be created
        """
        cache_dir = os.path.dirname(cache_path)
        if not os.path.exists(cache_dir):
            try:
                os.mkdir(cache_dir)
                logger.info("Made cache directory " + cache_dir)
            except OSError:
                raise EutilsError("Failed to make cache directory " +
                                  cache_dir)
        self._qs = QueryService(cache_path=cache_path)

    @property
    def databases(self):
        """
        list of databases available from eutils (per einfo query)
        """
        try:
            return self._databases
        except AttributeError:
            self._databases = self.einfo().databases
            return self._databases

    def einfo(self, db=None):
        """query the einfo endpoint

        :param db: string (optional)
        :rtype: EInfo or EInfoDB object

        If db is None, the reply is a list of databases, which is returned
        in an EInfo object (which has a databases() method).

        If db is not None, the reply is information about the specified
        database, which is returned in an EInfoDB object.  (Version 2.0
        data is automatically requested.)
        """

        if db is None:
            return EInfoResult(self._qs.einfo()).dblist
        return EInfoResult(self._qs.einfo({'db': db, 'version': '2.0'})).dbinfo

    def esearch(self, db, term):
        """query the esearch endpoint
        """
        esr = ESearchResult(self._qs.esearch({'db': db, 'term': term}))
        if esr.count > esr.retmax:
            logger.warn(
                "NCBI found {esr.count} results, but we truncated the reply at {esr.retmax}"
                " results; see https://github.com/biocommons/eutils/issues/124/"
                .format(esr=esr))
        return esr

    def efetch(self, db, id):
        """query the efetch endpoint
        """
        db = db.lower()
        xml = self._qs.efetch({'db': db, 'id': str(id)})
        doc = le.XML(xml)
        if db in ['gene']:
            return EntrezgeneSet(doc)
        if db in ['nuccore']:
            # TODO: GBSet is misnamed; it should be GBSeq and get the GBSeq XML node as root (see gbset.py)
            return GBSet(doc)
        if db in ['pubmed']:
            return PubmedArticleSet(doc)
        if db in ['snp']:
            return ExchangeSet(xml)
        if db in ['pmc']:
            return PubmedCentralArticleSet(doc)
        raise EutilsError(
            'database {db} is not currently supported by eutils'.format(db=db))
Exemplo n.º 6
0
class Client(object):
    """class-based access to NCBI E-Utilities, returning Python classes
    with rich data accessors

    """

    def __init__(self, cache_path=default_cache_path):
        """
        :param str cache_path: full path to sqlite database file (created if necessary)
        :raises EutilsError: if cache file couldn't be created
        """
        cache_dir = os.path.dirname(cache_path)
        if not os.path.exists(cache_dir):
            try:
                os.mkdir(cache_dir)
                logger.info("Made cache directory " + cache_dir)
            except OSError:
                raise EutilsError("Failed to make cache directory " + cache_dir)
        self._qs = QueryService(cache_path=cache_path)

    @property
    def databases(self):
        """
        list of databases available from eutils (per einfo query)
        """
        try:
            return self._databases
        except AttributeError:
            self._databases = self.einfo().databases
            return self._databases

    def einfo(self, db=None):
        """query the einfo endpoint

        :param db: string (optional)
        :rtype: EInfo or EInfoDB object

        If db is None, the reply is a list of databases, which is returned
        in an EInfo object (which has a databases() method).

        If db is not None, the reply is information about the specified
        database, which is returned in an EInfoDB object.  (Version 2.0
        data is automatically requested.)
        """

        if db is None:
            return EInfoResult(self._qs.einfo()).dblist
        return EInfoResult(self._qs.einfo({'db': db, 'version': '2.0'})).dbinfo

    def esearch(self, db, term):
        """query the esearch endpoint
        """
        esr = ESearchResult(self._qs.esearch({'db': db, 'term': term}))
        if esr.count > esr.retmax:
            logger.warn("NCBI found {esr.count} results, but we truncated the reply at {esr.retmax}"
                        " results; see https://github.com/biocommons/eutils/issues/124/".format(esr=esr))
        return esr

    def efetch(self, db, id):
        """query the efetch endpoint
        """
        db = db.lower()
        xml = self._qs.efetch({'db': db, 'id': str(id)})
        doc = le.XML(xml)
        if db in ['gene']:
            return EntrezgeneSet(doc)
        if db in ['nuccore', 'nucest']:
            # TODO: GBSet is misnamed; it should be GBSeq and get the GBSeq XML node as root (see gbset.py)
            return GBSet(doc)
        if db in ['pubmed']:
            return PubmedArticleSet(doc)
        if db in ['snp']:
            return ExchangeSet(xml)
        if db in ['pmc']:
            return PubmedCentralArticleSet(doc)
        raise EutilsError('database {db} is not currently supported by eutils'.format(db=db))