Skip to content

Commit 5795a01

Browse files
committed
download public prep data
1 parent ab4644c commit 5795a01

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

qiita_pet/handlers/download.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ def get(self):
571571
zip_fn = '%s_%s.zip' % (
572572
fname, datetime.now().strftime('%m%d%y-%H%M%S'))
573573
self._set_nginx_headers(zip_fn)
574-
else:
574+
elif study_id is not None:
575575
study_id = int(study_id)
576576
try:
577577
study = Study(study_id)
@@ -612,6 +612,53 @@ def get(self):
612612
'%m%d%y-%H%M%S'))
613613

614614
self._set_nginx_headers(zip_fn)
615+
else:
616+
prep_id = int(prep_id)
617+
try:
618+
prep = PrepTemplate(prep_id)
619+
except QiitaDBUnknownIDError:
620+
raise HTTPError(422, reason='Prep does not exist')
621+
else:
622+
public_raw_download = Study(prep.study_id).public_raw_download
623+
if prep.status != 'public':
624+
raise HTTPError(404, reason='Prep is not public. If this '
625+
'is a mistake contact: %s' %
626+
qiita_config.help_email)
627+
elif data == 'raw' and not public_raw_download:
628+
raise HTTPError(422, reason='No raw data access. If this '
629+
'is a mistake contact: %s'
630+
% qiita_config.help_email)
631+
else:
632+
# raw data
633+
if data == 'raw':
634+
artifacts = [prep.artifact]
635+
# bioms
636+
elif data == 'biom':
637+
artifacts = [a for a in
638+
prep.artifact.descendants.nodes()
639+
if a.artifact_type == 'BIOM' and
640+
a.visibility == 'public']
641+
else:
642+
raise HTTPError(
643+
422,
644+
reason='You can only download raw/biom from preps')
645+
for a in artifacts:
646+
if a.visibility != 'public' or a.has_human:
647+
continue
648+
to_download.extend(self._list_artifact_files_nginx(a))
649+
650+
if not to_download:
651+
raise HTTPError(422, reason='Nothing to download. If '
652+
'this is a mistake contact: %s'
653+
% qiita_config.help_email)
654+
else:
655+
self._write_nginx_file_list(to_download)
656+
657+
zip_fn = 'prep_%d_%s_%s.zip' % (
658+
prep_id, data, datetime.now().strftime(
659+
'%m%d%y-%H%M%S'))
660+
661+
self._set_nginx_headers(zip_fn)
615662

616663
self.finish()
617664

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)