ebi-eva-common-pyutils 0.6.7__py3-none-any.whl → 0.6.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,180 @@
1
+ #!/usr/bin/env python
2
+ # Copyright 2020 EMBL - European Bioinformatics Institute
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ import re
17
+
18
+ import requests
19
+ from functools import cached_property
20
+ from ebi_eva_common_pyutils.logger import AppLogger
21
+ from retry import retry
22
+
23
+
24
+ class HALNotReadyError(Exception):
25
+ pass
26
+
27
+
28
+ class HALCommunicator(AppLogger):
29
+ """
30
+ This class helps navigate through REST API that uses the HAL standard.
31
+ """
32
+ acceptable_code = [200, 201]
33
+
34
+ def __init__(self, auth_url, bsd_url, username, password):
35
+ self.auth_url = auth_url
36
+ self.bsd_url = bsd_url
37
+ self.username = username
38
+ self.password = password
39
+
40
+ def _validate_response(self, response):
41
+ """Check that the response has an acceptable code and raise if it does not"""
42
+ if response.status_code not in self.acceptable_code:
43
+ self.error(response.request.method + ': ' + response.request.url + " with " + str(response.request.body))
44
+ self.error("headers: {}".format(response.request.headers))
45
+ self.error("<{}>: {}".format(response.status_code, response.text))
46
+ raise ValueError('The HTTP status code ({}) is not one of the acceptable codes ({})'.format(
47
+ str(response.status_code), str(self.acceptable_code))
48
+ )
49
+ return response
50
+
51
+ @cached_property
52
+ def token(self):
53
+ """Retrieve the token from the AAP REST API then cache it for further quering"""
54
+ response = requests.get(self.auth_url, auth=(self.username, self.password))
55
+ self._validate_response(response)
56
+ return response.text
57
+
58
+ @retry(exceptions=(ValueError, requests.RequestException), tries=3, delay=2, backoff=1.2, jitter=(1, 3))
59
+ def _req(self, method, url, **kwargs):
60
+ """Private method that sends a request using the specified method. It adds the headers required by bsd"""
61
+ headers = kwargs.pop('headers', {})
62
+ headers.update({'Accept': 'application/hal+json'})
63
+ if self.token is not None:
64
+ headers.update({'Authorization': 'Bearer ' + self.token})
65
+ if 'json' in kwargs:
66
+ headers['Content-Type'] = 'application/json'
67
+ response = requests.request(
68
+ method=method,
69
+ url=url,
70
+ headers=headers,
71
+ **kwargs
72
+ )
73
+ self._validate_response(response)
74
+ return response
75
+
76
+ def follows(self, query, json_obj=None, method='GET', url_template_values=None, join_url=None, **kwargs):
77
+ """
78
+ Finds a link within the json_obj using a query string or list, modify the link using the
79
+ url_template_values dictionary then query the link using the method and any additional keyword argument.
80
+ If the json_obj is not specified then it will use the root query defined by the base url.
81
+ """
82
+ all_pages = kwargs.pop('all_pages', False)
83
+
84
+ if json_obj is None:
85
+ json_obj = self.root
86
+ # Drill down into a dict using dot notation
87
+ _json_obj = json_obj
88
+ if isinstance(query, str):
89
+ query_list = query.split('.')
90
+ else:
91
+ query_list = query
92
+ for query_element in query_list:
93
+ if query_element in _json_obj:
94
+ _json_obj = _json_obj[query_element]
95
+ else:
96
+ raise KeyError('{} does not exist in json object'.format(query_element, _json_obj))
97
+ if not isinstance(_json_obj, str):
98
+ raise ValueError('The result of the query_string must be a string to use as a url')
99
+ url = _json_obj
100
+ # replace the template in the url with the value provided
101
+ if url_template_values:
102
+ for k, v in url_template_values.items():
103
+ url = re.sub('{(' + k + ')(:.*)?}', v, url)
104
+ if join_url:
105
+ url += '/' + join_url
106
+ # Now query the url
107
+ json_response = self._req(method, url, **kwargs).json()
108
+
109
+ # Depaginate the call if requested
110
+ if all_pages is True:
111
+ # This depagination code will iterate over all the pages available until the pages comes back without a
112
+ # next page. It stores the embedded elements in the initial query's json response
113
+ content = json_response
114
+ while 'next' in content.get('_links'):
115
+ content = self._req(method, content.get('_links').get('next').get('href'), **kwargs).json()
116
+ for key in content.get('_embedded'):
117
+ json_response['_embedded'][key].extend(content.get('_embedded').get(key))
118
+ # Remove the pagination information as it is not relevant to the depaginated response
119
+ if 'page' in json_response: json_response.pop('page')
120
+ if 'first' in json_response['_links']: json_response['_links'].pop('first')
121
+ if 'last' in json_response['_links']: json_response['_links'].pop('last')
122
+ if 'next' in json_response['_links']: json_response['_links'].pop('next')
123
+ return json_response
124
+
125
+ def follows_link(self, key, json_obj=None, method='GET', url_template_values=None, join_url=None, **kwargs):
126
+ """
127
+ Same function as follows but construct the query_string from a single keyword surrounded by '_links' and 'href'.
128
+ """
129
+ return self.follows(('_links', key, 'href'),
130
+ json_obj=json_obj, method=method, url_template_values=url_template_values,
131
+ join_url=join_url, **kwargs)
132
+
133
+ @cached_property
134
+ def root(self):
135
+ return self._req('GET', self.bsd_url).json()
136
+
137
+ @property
138
+ def communicator_attributes(self):
139
+ raise NotImplementedError
140
+
141
+
142
+ class AAPHALCommunicator(HALCommunicator):
143
+ """Class to navigate BioSamples API using AAP authentication."""
144
+
145
+ def __init__(self, auth_url, bsd_url, username, password, domain=None):
146
+ super(AAPHALCommunicator, self).__init__(auth_url, bsd_url, username, password)
147
+ self.domain = domain
148
+
149
+ @property
150
+ def communicator_attributes(self):
151
+ return {'domain': self.domain}
152
+
153
+
154
+ class WebinHALCommunicator(HALCommunicator):
155
+ """Class to navigate BioSamples API using Webin authentication."""
156
+
157
+ @cached_property
158
+ def token(self):
159
+ """Retrieve the token from the ENA Webin REST API then cache it for further querying"""
160
+ response = requests.post(self.auth_url,
161
+ json={"authRealms": ["ENA"], "password": self.password,
162
+ "username": self.username})
163
+ self._validate_response(response)
164
+ return response.text
165
+
166
+ @property
167
+ def communicator_attributes(self):
168
+ return {'webinSubmissionAccountId': self.username}
169
+
170
+
171
+ class NoAuthHALCommunicator(HALCommunicator):
172
+ """Class to navigate BioSamples API without authentication."""
173
+
174
+ def __init__(self, bsd_url):
175
+ super(NoAuthHALCommunicator, self).__init__(None, bsd_url, None, None)
176
+
177
+ @cached_property
178
+ def token(self):
179
+ """No auth token, so errors will be raised if auth is required for requests"""
180
+ return None
@@ -22,6 +22,9 @@ class InternalServerError(Exception):
22
22
  pass
23
23
 
24
24
 
25
+ CONTING_ALIAS_URL = 'https://www.ebi.ac.uk/eva/webservices/contig-alias'
26
+
27
+
25
28
  # TODO add the get methods
26
29
  class ContigAliasClient(AppLogger):
27
30
  """
@@ -29,8 +32,13 @@ class ContigAliasClient(AppLogger):
29
32
  Authentication is required if using admin endpoints.
30
33
  """
31
34
 
32
- def __init__(self, base_url, username=None, password=None):
33
- self.base_url = base_url
35
+ def __init__(self, base_url=None, username=None, password=None, default_page_size=1000):
36
+ if base_url:
37
+ self.base_url = base_url
38
+ else:
39
+ self.base_url = os.environ.get('CONTING_ALIAS_URL') or CONTING_ALIAS_URL
40
+ # Used for get method
41
+ self.default_page_size=default_page_size
34
42
  # Only required for admin endpoints
35
43
  self.username = username
36
44
  self.password = password
@@ -69,3 +77,39 @@ class ContigAliasClient(AppLogger):
69
77
  raise InternalServerError
70
78
  else:
71
79
  self.error(f'Assembly accession {assembly} could not be deleted. Response: {response.text}')
80
+
81
+ @retry(tries=3, delay=2, backoff=1.2, jitter=(1, 3))
82
+ def _get_page_for_contig_alias_url(self, sub_url, page=0):
83
+ """queries the contig alias to retrieve the page of the provided url"""
84
+ url = f'{self.base_url}/{sub_url}?page={page}&size={self.default_page_size}'
85
+ response = requests.get(url, headers={'accept': 'application/json'})
86
+ response.raise_for_status()
87
+ response_json = response.json()
88
+ return response_json
89
+
90
+ def _depaginate_iter(self, sub_url, entity_to_retrieve):
91
+ """Generator that provides the contigs in the assembly requested."""
92
+ page = 0
93
+ response_json = self._get_page_for_contig_alias_url(sub_url, page=page)
94
+ for entity in response_json.get('_embedded', {}).get(entity_to_retrieve, []):
95
+ yield entity
96
+ while 'next' in response_json['_links']:
97
+ page += 1
98
+ response_json = self._get_page_for_contig_alias_url(sub_url, page=page)
99
+ for entity in response_json.get('_embedded', {}).get(entity_to_retrieve, []):
100
+ yield entity
101
+
102
+ def assembly_contig_iter(self, assembly_accession):
103
+ """Generator that provides the contigs in the assembly requested."""
104
+ sub_url = f'v1/assemblies/{assembly_accession}/chromosomes'
105
+ return self._depaginate_iter(sub_url, 'chromosomeEntities')
106
+
107
+ def assembly(self, assembly_accession):
108
+ """provides the description of the requested assembly."""
109
+ sub_url = f'v1/assemblies/{assembly_accession}'
110
+ response_json = self._get_page_for_contig_alias_url(sub_url)
111
+ return response_json.get('_embedded', {}).get('assemblyEntities', [])[0]
112
+
113
+ def contig_iter(self, insdc_accession):
114
+ sub_url = f'v1/chromosomes/genbank/{insdc_accession}'
115
+ return self._depaginate_iter(sub_url, 'chromosomeEntities')
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ebi-eva-common-pyutils
3
- Version: 0.6.7
3
+ Version: 0.6.9
4
4
  Summary: EBI EVA - Common Python Utilities
5
5
  Home-page: https://github.com/EBIVariation/eva-common-pyutils
6
6
  License: Apache
@@ -1,5 +1,6 @@
1
1
  ebi_eva_common_pyutils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  ebi_eva_common_pyutils/assembly_utils.py,sha256=CklyCGlCjlFp0e9pugg6kSsh5L0xfCe2qPvA2eLVtn0,4187
3
+ ebi_eva_common_pyutils/biosamples_communicators.py,sha256=ngWuSvEYkkttkMnJ9Gjxc5D7ZSi7D3_JX1c8BnEO3os,7596
3
4
  ebi_eva_common_pyutils/command_utils.py,sha256=PtelWWqcC0eOwIVesjwBw3F9KaXRzEE_uAUJhQFZ4l8,2340
4
5
  ebi_eva_common_pyutils/common_utils.py,sha256=ty_glvfRa3VGhnpAht4qtVkNNmv-IYfVtO958mY-BaA,1192
5
6
  ebi_eva_common_pyutils/config.py,sha256=PtD2SgHf96kk21OA9tVIjEgsDXEFuAU-INy_kfQdoPw,4828
@@ -11,7 +12,7 @@ ebi_eva_common_pyutils/network_utils.py,sha256=iJjs5PPzT1V4CceZnCHOTs711AmpwlDo5
11
12
  ebi_eva_common_pyutils/assembly/__init__.py,sha256=KSWPwBY5nZj00odxWFntk8Sqg_rw273xH8S5D6Jo-T4,67
12
13
  ebi_eva_common_pyutils/assembly/assembly.py,sha256=IEmleROX4ZchPyhINKCuMmET_Ih1Jg4ok-opAKY6Z9A,3142
13
14
  ebi_eva_common_pyutils/contig_alias/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
14
- ebi_eva_common_pyutils/contig_alias/contig_alias.py,sha256=8mxcIbOhivqedgDjpzsHEY3UbYwHvk9DGoEiyHLLOBk,3056
15
+ ebi_eva_common_pyutils/contig_alias/contig_alias.py,sha256=7tQXljEJtRCMs_JPrglYvTEZ-fJvzl8txwnCvqRB3Ck,5214
15
16
  ebi_eva_common_pyutils/reference/__init__.py,sha256=NsDjGCu2H2CZLOPaEL6GCoZo5ND_EA9RFmbzGfpSXRQ,134
16
17
  ebi_eva_common_pyutils/reference/assembly.py,sha256=FK4zsvg4_GDeKd9z2z5ZeM9R84TsusgQYdMOGuggQU0,12162
17
18
  ebi_eva_common_pyutils/reference/sequence.py,sha256=bg96QcuB-oytQYpaUkV10OzM_RIFhxsy6t3PQOg2Gy0,3911
@@ -19,7 +20,7 @@ ebi_eva_common_pyutils/taxonomy/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm
19
20
  ebi_eva_common_pyutils/taxonomy/taxonomy.py,sha256=p3XV4g3y0hEjyeZ4PwgN7Q3Et9G515ctQkSIo1kdDbU,2259
20
21
  ebi_eva_common_pyutils/variation/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
22
  ebi_eva_common_pyutils/variation/contig_utils.py,sha256=kMNEW_P2yPnd8Xx1tep19hy5ee7ojxz6ZOO1grTQsRQ,5230
22
- ebi_eva_common_pyutils-0.6.7.data/scripts/archive_directory.py,sha256=0lWJ0ju_AB2ni7lMnJXPFx6U2OdTGbe-WoQs-4BfKOM,4976
23
+ ebi_eva_common_pyutils-0.6.9.data/scripts/archive_directory.py,sha256=0lWJ0ju_AB2ni7lMnJXPFx6U2OdTGbe-WoQs-4BfKOM,4976
23
24
  ebi_eva_internal_pyutils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
25
  ebi_eva_internal_pyutils/archive_directory.py,sha256=IxVEfh_gaCiT652k0Q_-58fonRusy1yzXu7BCO8yVLo,4989
25
26
  ebi_eva_internal_pyutils/config_utils.py,sha256=EGRC5rsmU_ug7OY9-t1UW1XZXRsauSyZB9xPcBux8ts,7909
@@ -31,8 +32,8 @@ ebi_eva_internal_pyutils/mongodb/__init__.py,sha256=0oyTlkYZCV7udlPl09Zl-sDyE3c9
31
32
  ebi_eva_internal_pyutils/mongodb/mongo_database.py,sha256=kesaJaaxYFeF_uYZBgL8tbufGKUXll7bXb4WlOS9vKM,9596
32
33
  ebi_eva_internal_pyutils/nextflow/__init__.py,sha256=OOiJS8jZOz98q0t77NNog7aI_fFrVxi4kGmiSskuAqM,122
33
34
  ebi_eva_internal_pyutils/nextflow/nextflow_pipeline.py,sha256=ew623hhK8jmFLQjJwLZbgBmW9RTiJBEULVqHfIUv_dc,10114
34
- ebi_eva_common_pyutils-0.6.7.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
35
- ebi_eva_common_pyutils-0.6.7.dist-info/METADATA,sha256=CkS0gM_NHH4fr1TBxoXn6FTnZ4vfT4fS41ReZCKAy0U,824
36
- ebi_eva_common_pyutils-0.6.7.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
37
- ebi_eva_common_pyutils-0.6.7.dist-info/top_level.txt,sha256=sXoiqiGU8vlMQpFWDlKrekxhlusk06AhkOH3kSvDT6c,48
38
- ebi_eva_common_pyutils-0.6.7.dist-info/RECORD,,
35
+ ebi_eva_common_pyutils-0.6.9.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
36
+ ebi_eva_common_pyutils-0.6.9.dist-info/METADATA,sha256=iEw5N92Ad8rSkru7KNyzfpSgUD0NCpuF4Eu1dPSVp1I,824
37
+ ebi_eva_common_pyutils-0.6.9.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
38
+ ebi_eva_common_pyutils-0.6.9.dist-info/top_level.txt,sha256=sXoiqiGU8vlMQpFWDlKrekxhlusk06AhkOH3kSvDT6c,48
39
+ ebi_eva_common_pyutils-0.6.9.dist-info/RECORD,,