-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #514 from padix-key/ncbi-key
Support NCBI API keys
- Loading branch information
Showing
11 changed files
with
127 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,10 @@ | |
"search", | ||
"fetch", | ||
"fetch_single_file" | ||
], | ||
"API keys" : [ | ||
"set_api_key", | ||
"get_api_key" | ||
] | ||
}, | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import os | ||
|
||
|
||
def set_ncbi_api_key_from_env(*args, **kwargs): | ||
# Import inside function as Biotite may not be known | ||
# at the time of function definition | ||
import biotite.database.entrez as entrez | ||
|
||
entrez.set_api_key(os.environ.get("NCBI_API_KEY")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ | |
|
||
from .dbnames import * | ||
from .download import * | ||
from .query import * | ||
from .query import * | ||
from .key import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
import requests | ||
from .check import check_for_errors | ||
from .dbnames import sanitize_database_name | ||
from .key import get_api_key | ||
from ..error import RequestError | ||
|
||
|
||
|
@@ -23,15 +24,15 @@ def fetch(uids, target_path, suffix, db_name, ret_type, | |
ret_mode="text", overwrite=False, verbose=False): | ||
""" | ||
Download files from the NCBI Entrez database in various formats. | ||
The data for each UID will be fetched into a separate file. | ||
A list of valid database, retrieval type and mode combinations can | ||
be found under | ||
`<https://www.ncbi.nlm.nih.gov/books/NBK25499/table/chapter4.T._valid_values_of__retmode_and/?report=objectonly>`_ | ||
This function requires an internet connection. | ||
Parameters | ||
---------- | ||
uids : str or iterable object of str | ||
|
@@ -58,7 +59,7 @@ def fetch(uids, target_path, suffix, db_name, ret_type, | |
verbose: bool, optional | ||
If true, the function will output the download progress. | ||
(Default: False) | ||
Returns | ||
------- | ||
files : str or StringIO or BytesIO or list of (str or StringIO or BytesIO) | ||
|
@@ -68,22 +69,22 @@ def fetch(uids, target_path, suffix, db_name, ret_type, | |
object) was given, a list of strings is returned. | ||
If `target_path` is ``None``, the file contents are stored in | ||
either `StringIO` or `BytesIO` objects. | ||
Warnings | ||
-------- | ||
Even if you give valid input to this function, in rare cases the | ||
database might return no or malformed data to you. | ||
In these cases the request should be retried. | ||
When the issue occurs repeatedly, the error is probably in your | ||
input. | ||
See also | ||
-------- | ||
fetch_single_file | ||
Examples | ||
-------- | ||
>>> import os.path | ||
>>> files = fetch(["1L2Y_A","3O5R_A"], path_to_directory, suffix="fa", | ||
... db_name="protein", ret_type="fasta") | ||
|
@@ -122,6 +123,9 @@ def fetch(uids, target_path, suffix, db_name, ret_type, | |
"tool" : "Biotite", | ||
"mail" : "[email protected]" | ||
} | ||
api_key = get_api_key() | ||
if api_key is not None: | ||
param_dict["api_key"] = api_key | ||
r = requests.get(_fetch_url, params=param_dict) | ||
content = r.text | ||
check_for_errors(content) | ||
|
@@ -147,7 +151,7 @@ def fetch_single_file(uids, file_name, db_name, ret_type, ret_mode="text", | |
""" | ||
Almost the same as :func:`fetch()`, but the data for the given UIDs | ||
will be stored in a single file. | ||
Parameters | ||
---------- | ||
uids : iterable object of str | ||
|
@@ -164,22 +168,22 @@ def fetch_single_file(uids, file_name, db_name, ret_type, ret_mode="text", | |
overwrite : bool, optional | ||
If false, the file is only downloaded, if no file with the same | ||
name already exists. | ||
Returns | ||
------- | ||
file : str or StringIO or BytesIO | ||
The file name of the downloaded file. | ||
If `file_name` is ``None``, the file content is stored in | ||
either a `StringIO` or a `BytesIO` object. | ||
Warnings | ||
-------- | ||
Even if you give valid input to this function, in rare cases the | ||
database might return no or malformed data to you. | ||
In these cases the request should be retried. | ||
When the issue occurs repeatedly, the error is probably in your | ||
input. | ||
See also | ||
-------- | ||
fetch | ||
|
@@ -203,6 +207,9 @@ def fetch_single_file(uids, file_name, db_name, ret_type, ret_mode="text", | |
"tool" : "Biotite", | ||
"mail" : "[email protected]" | ||
} | ||
api_key = get_api_key() | ||
if api_key is not None: | ||
param_dict["api_key"] = api_key | ||
r = requests.get(_fetch_url, params=param_dict) | ||
content = r.text | ||
check_for_errors(content) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# This source code is part of the Biotite package and is distributed | ||
# under the 3-Clause BSD License. Please see 'LICENSE.rst' for further | ||
# information. | ||
|
||
__name__ = "biotite.database.entrez" | ||
__author__ = "Patrick Kunzmann" | ||
__all__ = ["set_api_key", "get_api_key"] | ||
|
||
|
||
_API_KEY = None | ||
|
||
|
||
def get_api_key(): | ||
""" | ||
Get the | ||
`NCBI API key <https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/>`_. | ||
Returns | ||
------- | ||
api_key : str or None | ||
The API key, if it was already set before, ``None`` otherwise. | ||
""" | ||
global _API_KEY | ||
return _API_KEY | ||
|
||
|
||
def set_api_key(key): | ||
""" | ||
Set the | ||
`NCBI API key <https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/>`_. | ||
Using an API key increases the request limit on the NCBI servers | ||
and is automatically used by functions in | ||
:mod:`biotite.database.entrez`. | ||
This key is kept only in memory and hence removed in the end of the | ||
Python session. | ||
Parameters | ||
---------- | ||
api_key : str | ||
The API key. | ||
""" | ||
global _API_KEY | ||
_API_KEY = key |
Oops, something went wrong.