Skip to content

Commit 932edb4

Browse files
committed
fix: added exceptions, removed unnecessary function
1 parent 13d37be commit 932edb4

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

data_collection/tools.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,6 @@ class DownloadError(Exception):
146146
'Individual/Origin/Geographic': 'String'
147147
}
148148

149-
# CONFIGURATIONS
150-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
151-
152-
153149
def get_file_from_url(url, save_to, override=False):
154150
"""
155151
Gets file from url and saves it into provided path. Overrides, if override is True.
@@ -289,20 +285,6 @@ def from_clinvar_name_to_dna(name):
289285

290286
return name[start:end]
291287

292-
293-
def check_if_valid_response(text):
294-
"""
295-
Checks if gene symbol is valid
296-
297-
:param str text: response's text
298-
"""
299-
300-
if 'Error' in text:
301-
return False
302-
return True
303-
304-
305-
306288
def download_gene_lovd(gene_list:list,folder_path,raise_exception = False):
307289
"""
308290
Downloads data into txt files from gene_list.
@@ -315,11 +297,19 @@ def download_gene_lovd(gene_list:list,folder_path,raise_exception = False):
315297
for gene in gene_list:
316298
file_path = folder_path + '/'+gene + ".txt"
317299
url = f"https://databases.lovd.nl/shared/download/all/gene/{gene}"
318-
response = requests.get(url,timeout=10)
319-
valid = check_if_valid_response(response.text[:6])
300+
try:
301+
response = requests.get(url, timeout=10)
302+
except RequestException as e:
303+
raise DownloadError(f"Error while downloading file from {url}") from e
304+
305+
if response.status_code != 200:
306+
raise BadResponseException(f"Bad response from {url}."
307+
f" Status code: {response.status_code}")
308+
#If gene does not exist, the first word of the file will be Error
309+
valid = 'Error' not in response.text[:6]
320310
if valid:
321311
get_file_from_url(url,file_path)
322312
elif raise_exception:
323313
raise ValueError(f"Symbol: {gene} does not exist in the LOVD database")
324314
else:
325-
logging.info("Symbol: %s does not exist in the LOVD database",gene)
315+
logging.error("Symbol: %s does not exist in the LOVD database",gene)

0 commit comments

Comments
 (0)