Skip to content

Commit

Permalink
add checks for different accession types with browser API
Browse files Browse the repository at this point in the history
  • Loading branch information
josieburgin committed Jul 30, 2020
1 parent dbccf5d commit b490d5c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
5 changes: 3 additions & 2 deletions python3/assemblyGet.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def check_format(output_format):
)
sys.exit(1)

def get_default_format():
return utils.EMBL_FORMAT

def get_sequence_report_url(record):
for link in record.iter('URL_LINK'):
label = (link.find('LABEL').text)
Expand Down Expand Up @@ -137,8 +140,6 @@ def extract_wgs_scaffolds(assembly_dir, wgs_scaffolds, wgs_set, output_format, q
target_file.close()

def download_assembly(dest_dir, accession, output_format, fetch_wgs, extract_wgs, expanded, quiet=False):
if output_format is None:
output_format = utils.EMBL_FORMAT
assembly_dir = os.path.join(dest_dir, accession)
utils.create_dir(assembly_dir)
# download xml
Expand Down
27 changes: 20 additions & 7 deletions python3/enaDataGet.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def set_parser():
parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.5.3')
return parser

def check_availability(accession, output_format):
if not utils.is_available(accession, output_format):
sys.stderr.write(
'ERROR: Record does not exist or is not available for accession provided\n')
sys.exit(1)

if __name__ == '__main__':
parser = set_parser()
Expand All @@ -81,26 +86,34 @@ def set_parser():
if utils.is_wgs_set(accession):
if output_format is not None:
sequenceGet.check_format(output_format)
else:
output_format = sequenceGet.get_default_format()
check_availability(accession, output_format)
sequenceGet.download_wgs(dest_dir, accession, output_format)
elif not utils.is_available(accession, output_format):
sys.stderr.write('ERROR: Record does not exist or is not available for accession provided\n')
sys.exit(1)
elif utils.is_assembly(accession):
if output_format is not None:
assemblyGet.check_format(output_format)
else:
output_format = assemblyGet.get_default_format()
check_availability(accession, output_format)
assemblyGet.download_assembly(dest_dir, accession, output_format, fetch_wgs, extract_wgs, expanded)
elif utils.is_sequence(accession):
if output_format is not None:
sequenceGet.check_format(output_format)
else:
output_format = sequenceGet.get_default_format()
check_availability(accession, output_format)
sequenceGet.download_sequence(dest_dir, accession, output_format, expanded)
elif utils.is_analysis(accession):
if output_format is not None:
readGet.check_read_format(output_format)
check_availability(accession, output_format)
readGet.download_files(accession, output_format, dest_dir, fetch_index, fetch_meta, aspera)
elif utils.is_run(accession) or utils.is_experiment(accession):
if output_format is not None:
readGet.check_read_format(output_format)
check_availability(accession, output_format)
readGet.download_files(accession, output_format, dest_dir, fetch_index, fetch_meta, aspera)
elif utils.is_assembly(accession):
if output_format is not None:
assemblyGet.check_format(output_format)
assemblyGet.download_assembly(dest_dir, accession, output_format, fetch_wgs, extract_wgs, expanded)
else:
sys.stderr.write('ERROR: Invalid accession provided\n')
sys.exit(1)
Expand Down
9 changes: 3 additions & 6 deletions python3/sequenceGet.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ def write_record(dest_file, accession, output_format, expanded=False):
return utils.write_record(url, dest_file)

def download_sequence(dest_dir, accession, output_format, expanded):
if output_format is None:
output_format = utils.EMBL_FORMAT
success = utils.download_record(dest_dir, accession, output_format, expanded)
if not success:
print ('Unable to fetch file for {0}, format {1}'.format(accession, output_format))
Expand All @@ -45,8 +43,6 @@ def download_wgs(dest_dir, accession, output_format):

def download_versioned_wgs(dest_dir, accession, output_format):
prefix = accession[:6]
if output_format is None:
output_format = utils.EMBL_FORMAT
public_set_url = utils.get_wgs_ftp_url(prefix, utils.PUBLIC, output_format)
supp_set_url = utils.get_wgs_ftp_url(prefix, utils.SUPPRESSED, output_format)
success = utils.get_ftp_file(public_set_url, dest_dir)
Expand All @@ -58,8 +54,6 @@ def download_versioned_wgs(dest_dir, accession, output_format):

def download_unversioned_wgs(dest_dir, accession, output_format):
prefix = accession[:4]
if output_format is None:
output_format = utils.EMBL_FORMAT
public_set_url = utils.get_nonversioned_wgs_ftp_url(prefix, utils.PUBLIC, output_format)
if public_set_url is not None:
utils.get_ftp_file(public_set_url, dest_dir)
Expand All @@ -76,3 +70,6 @@ def check_format(output_format):
if output_format not in allowed_formats:
sys.stderr.write('Please select a valid format for this accession: {0}\n'.format(allowed_formats))
sys.exit(1)

def get_default_format():
return utils.EMBL_FORMAT
4 changes: 2 additions & 2 deletions python3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def get_record_url(accession, output_format):
def is_available(accession, output_format):
if is_taxid(accession):
url = get_record_url('Taxon:{0}'.format(accession), XML_FORMAT)
elif is_study(accession) or is_sample(accession):
elif is_study(accession) or is_sample(accession) or is_assembly(accession):
url = get_record_url(accession, XML_FORMAT)
else:
url = get_record_url(accession, output_format)
Expand All @@ -245,7 +245,7 @@ def is_available(accession, output_format):
try:
print('Checking availability of ' + url)
response = requests.get(url)
return response.status_code == 200
return response.status_code == 200 and len(response.content) != 0
except urlerror.URLError as e:
if 'CERTIFICATE_VERIFY_FAILED' in str(e):
print ('Error verifying SSL certificate. Have you run "Install Certificates" as part of your Python3 installation?')
Expand Down

0 comments on commit b490d5c

Please sign in to comment.