Skip to content

Commit e886a8d

Browse files
authored
Merge 97e96cc into ab4644c
2 parents ab4644c + 97e96cc commit e886a8d

File tree

2 files changed

+87
-35
lines changed

2 files changed

+87
-35
lines changed

qiita_pet/handlers/download.py

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -523,19 +523,27 @@ def get(self):
523523
valid_data = ['raw', 'biom'] + templates
524524

525525
to_download = []
526+
# for this block to work we need 3 main inputs: prep_id/sample_id, data
527+
# and data_type - if one is missing raise an error, if both
528+
# prep_id/sample_id are defined or data_type doesn't exist in qiita
529+
# we should error
526530
if data is None or (study_id is None and prep_id is None) or \
527531
data not in valid_data:
528532
raise HTTPError(422, reason='You need to specify both data (the '
529533
'data type you want to download - %s) and '
530534
'study_id or prep_id' % '/'.join(valid_data))
531-
elif data_type is not None and data_type not in dtypes:
535+
if data_type is not None and data_type not in dtypes:
532536
raise HTTPError(422, reason='Not a valid data_type. Valid types '
533537
'are: %s' % ', '.join(dtypes))
534-
elif data in templates and prep_id is None and study_id is None:
538+
if data in templates and prep_id is None and study_id is None:
535539
raise HTTPError(422, reason='If downloading a sample or '
536540
'preparation file you need to define study_id or'
537541
' prep_id')
538-
elif data in templates:
542+
543+
# if we get here, then we have two main options: templates or raw/biom;
544+
# however, for raw/biom we need to retrieve the data via the study_id
545+
# or the prep_id so splitting the next block in study_id/pre_id
546+
if data in templates:
539547
if data_type is not None:
540548
raise HTTPError(422, reason='If requesting an information '
541549
'file you cannot specify the data_type')
@@ -572,47 +580,71 @@ def get(self):
572580
fname, datetime.now().strftime('%m%d%y-%H%M%S'))
573581
self._set_nginx_headers(zip_fn)
574582
else:
575-
study_id = int(study_id)
576-
try:
577-
study = Study(study_id)
578-
except QiitaDBUnknownIDError:
579-
raise HTTPError(422, reason='Study does not exist')
583+
# depending on if we have a study_id or a prep_id, instantiate
584+
# the study for basic permission validations
585+
if study_id is not None:
586+
study_id = int(study_id)
587+
try:
588+
study = Study(study_id)
589+
except QiitaDBUnknownIDError:
590+
raise HTTPError(422, reason='Study does not exist')
591+
zip_fn = 'study_%d_%s_%s.zip' % (
592+
study_id, data, datetime.now().strftime(
593+
'%m%d%y-%H%M%S'))
580594
else:
581-
public_raw_download = study.public_raw_download
582-
if study.status != 'public':
583-
raise HTTPError(404, reason='Study is not public. If this '
584-
'is a mistake contact: %s' %
585-
qiita_config.help_email)
586-
elif data == 'raw' and not public_raw_download:
595+
prep_id = int(prep_id)
596+
try:
597+
prep = PrepTemplate(prep_id)
598+
except QiitaDBUnknownIDError:
599+
raise HTTPError(422, reason='Prep does not exist')
600+
study = Study(prep.study_id)
601+
zip_fn = 'prep_%d_%s_%s.zip' % (
602+
prep_id, data, datetime.now().strftime(
603+
'%m%d%y-%H%M%S'))
604+
605+
public_raw_download = study.public_raw_download
606+
# just to be 100% that the data is public, let's start
607+
# with checking that the study is actually public
608+
if study.status != 'public':
609+
raise HTTPError(404, reason='Study is not public. If this '
610+
'is a mistake contact: %s' %
611+
qiita_config.help_email)
612+
# now let's check that if the data is raw, the study's
613+
# public_raw_download flag is on
614+
if data == 'raw':
615+
if not public_raw_download:
587616
raise HTTPError(422, reason='No raw data access. If this '
588617
'is a mistake contact: %s'
589618
% qiita_config.help_email)
590-
else:
591-
# raw data
619+
if study_id is not None:
592620
artifacts = [a for a in study.artifacts(dtype=data_type)
593621
if not a.parents]
594-
# bioms
595-
if data == 'biom':
596-
artifacts = study.artifacts(
597-
dtype=data_type, artifact_type='BIOM')
598-
for a in artifacts:
599-
if a.visibility != 'public' or a.has_human:
600-
continue
601-
to_download.extend(self._list_artifact_files_nginx(a))
602-
603-
if not to_download:
604-
raise HTTPError(422, reason='Nothing to download. If '
605-
'this is a mistake contact: %s'
606-
% qiita_config.help_email)
607622
else:
608-
self._write_nginx_file_list(to_download)
609-
610-
zip_fn = 'study_%d_%s_%s.zip' % (
611-
study_id, data, datetime.now().strftime(
612-
'%m%d%y-%H%M%S'))
623+
artifacts = [prep.artifact]
624+
else: # this is biom
625+
if study_id is not None:
626+
artifacts = study.artifacts(
627+
dtype=data_type, artifact_type='BIOM')
628+
else:
629+
artifacts = [a for a in
630+
prep.artifact.descendants.nodes()
631+
if a.artifact_type == 'BIOM']
632+
633+
# at this point artifacts has all the available artifact
634+
# so we need to make sure they are public and have no has_human
635+
# to be added to_download
636+
for a in artifacts:
637+
if a.visibility != 'public' or a.has_human:
638+
continue
639+
to_download.extend(self._list_artifact_files_nginx(a))
613640

614-
self._set_nginx_headers(zip_fn)
641+
if not to_download:
642+
raise HTTPError(422, reason='Nothing to download. If '
643+
'this is a mistake contact: %s'
644+
% qiita_config.help_email)
615645

646+
self._write_nginx_file_list(to_download)
647+
self._set_nginx_headers(zip_fn)
616648
self.finish()
617649

618650

qiita_pet/test/test_download.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,26 @@ def test_download(self):
535535
'mapping_files/5_mapping_file.txt')
536536
self.assertRegex(response.body.decode('ascii'), exp)
537537

538+
# Now let's check download prep with no raw data access
539+
response = self.get('/public_download/?data=raw&prep_id=1')
540+
self.assertTrue(response.reason.startswith('No raw data access.'))
541+
542+
# Now success
543+
Study(1).public_raw_download = True
544+
response = self.get('/public_download/?data=raw&prep_id=1')
545+
self.assertEqual(response.code, 200)
546+
exp = ('- [0-9]* /protected/raw_data/1_s_G1_L001_sequences.fastq.gz '
547+
'raw_data/1_s_G1_L001_sequences.fastq.gz\n- [0-9]* /protected'
548+
'/raw_data/1_s_G1_L001_sequences_barcodes.fastq.gz raw_data/'
549+
'1_s_G1_L001_sequences_barcodes.fastq.gz\n- [0-9]* /protected/'
550+
'templates/1_prep_1_qiime_19700101-000000.txt mapping_files/'
551+
'1_mapping_file.txt\n')
552+
self.assertRegex(response.body.decode('ascii'), exp)
553+
554+
# for simplicity, let's just check respose.code
555+
response = self.get('/public_download/?data=biom&prep_id=1')
556+
self.assertEqual(response.code, 200)
557+
538558
def test_download_sample_information(self):
539559
response = self.get('/public_artifact_download/')
540560
self.assertEqual(response.code, 422)

0 commit comments

Comments
 (0)