def sample_template_overview_handler_get_request(study_id, user): # Check if the current user has access to the sample template sample_template_checks(study_id, user) # Check if the sample template exists exists = SampleTemplate.exists(study_id) # The following information should always be provided: # The files that have been uploaded to the system and can be a # sample template file files = [ f for _, f in get_files_from_uploads_folders(study_id) if f.endswith(('txt', 'tsv')) ] # If there is a job associated with the sample information, the job id job = None job_info = r_client.get(SAMPLE_TEMPLATE_KEY_FORMAT % study_id) if job_info: job = loads(job_info)['job_id'] # Specific information if it exists or not: data_types = [] st_fp_id = None old_files = [] num_samples = 0 num_cols = 0 if exists: # If it exists we need to provide: # The id of the sample template file so the user can download it and # the list of old filepaths st = SampleTemplate(study_id) all_st_files = st.get_filepaths() # The current sample template file is the first one in the list # (pop(0)) and we are interested only in the id ([0]) st_fp_id = all_st_files.pop(0)[0] # For the old filepaths we are only interested in their basename old_files = [basename(fp) for _, fp in all_st_files] # The number of samples - this is a space efficient way of counting # the number of samples. Doing len(list(st.keys())) creates a list # that we are not using num_samples = sum(1 for _ in st.keys()) # The number of columns num_cols = len(st.categories()) else: # It doesn't exist, we also need to provide the data_types in case # the user uploads a QIIME mapping file data_types = sorted(data_types_get_req()['data_types']) return { 'exists': exists, 'uploaded_files': files, 'data_types': data_types, 'user_can_edit': Study(study_id).can_edit(user), 'job': job, 'download_id': st_fp_id, 'old_files': old_files, 'num_samples': num_samples, 'num_columns': num_cols }
def sample_template_overview_handler_get_request(study_id, user): # Check if the current user has access to the sample template sample_template_checks(study_id, user) # Check if the sample template exists exists = SampleTemplate.exists(study_id) # The following information should always be provided: # The files that have been uploaded to the system and can be a # sample template file files = [f for _, f, _ in get_files_from_uploads_folders(study_id) if f.endswith(('txt', 'tsv', 'xlsx'))] # If there is a job associated with the sample information, the job id job = None job_info = r_client.get(SAMPLE_TEMPLATE_KEY_FORMAT % study_id) if job_info: job = loads(job_info)['job_id'] # Specific information if it exists or not: data_types = [] st_fp_id = None old_files = [] num_samples = 0 num_cols = 0 if exists: # If it exists we need to provide: # The id of the sample template file so the user can download it and # the list of old filepaths st = SampleTemplate(study_id) all_st_files = st.get_filepaths() # The current sample template file is the first one in the list # (pop(0)) and we are interested only in the id ([0]) st_fp_id = all_st_files.pop(0)[0] # For the old filepaths we are only interested in their basename old_files = [basename(fp) for _, fp in all_st_files] # The number of samples - this is a space efficient way of counting # the number of samples. Doing len(list(st.keys())) creates a list # that we are not using num_samples = sum(1 for _ in st.keys()) # The number of columns num_cols = len(st.categories()) else: # It doesn't exist, we also need to provide the data_types in case # the user uploads a QIIME mapping file data_types = sorted(data_types_get_req()['data_types']) return {'exists': exists, 'uploaded_files': files, 'data_types': data_types, 'user_can_edit': Study(study_id).can_edit(user), 'job': job, 'download_id': st_fp_id, 'old_files': old_files, 'num_samples': num_samples, 'num_columns': num_cols}
def display_template(self, preprocessed_data_id, msg, msg_level): """Simple function to avoid duplication of code""" preprocessed_data_id = int(preprocessed_data_id) try: preprocessed_data = Artifact(preprocessed_data_id) except QiitaDBUnknownIDError: raise HTTPError( 404, "Artifact %d does not exist!" % preprocessed_data_id) else: user = self.current_user if user.level != 'admin': raise HTTPError( 403, "No permissions of admin, " "get/VAMPSSubmitHandler: %s!" % user.id) prep_template = PrepTemplate(preprocessed_data.prep_template) sample_template = SampleTemplate(preprocessed_data.study) study = Study(preprocessed_data.study) stats = [('Number of samples', len(prep_template)), ('Number of metadata headers', len(sample_template.categories()))] demux = [ path for _, path, ftype in preprocessed_data.get_filepaths() if ftype == 'preprocessed_demux' ] demux_length = len(demux) if not demux_length: msg = ("Study does not appear to have demultiplexed " "sequences associated") msg_level = 'danger' elif demux_length > 1: msg = ("Study appears to have multiple demultiplexed files!") msg_level = 'danger' elif demux_length == 1: demux_file = demux[0] demux_file_stats = demux_stats(demux_file) stats.append(('Number of sequences', demux_file_stats.n)) msg_level = 'success' self.render('vamps_submission.html', study_title=study.title, stats=stats, message=msg, study_id=study.id, level=msg_level, preprocessed_data_id=preprocessed_data_id)
def display_template(self, preprocessed_data_id, msg, msg_level): """Simple function to avoid duplication of code""" preprocessed_data_id = int(preprocessed_data_id) try: preprocessed_data = Artifact(preprocessed_data_id) except QiitaDBUnknownIDError: raise HTTPError(404, "Artifact %d does not exist!" % preprocessed_data_id) else: user = self.current_user if user.level != "admin": raise HTTPError(403, "No permissions of admin, " "get/VAMPSSubmitHandler: %s!" % user.id) prep_template = PrepTemplate(preprocessed_data.prep_template) sample_template = SampleTemplate(preprocessed_data.study) study = Study(preprocessed_data.study) stats = [ ("Number of samples", len(prep_template)), ("Number of metadata headers", len(sample_template.categories())), ] demux = [path for _, path, ftype in preprocessed_data.get_filepaths() if ftype == "preprocessed_demux"] demux_length = len(demux) if not demux_length: msg = "Study does not appear to have demultiplexed " "sequences associated" msg_level = "danger" elif demux_length > 1: msg = "Study appears to have multiple demultiplexed files!" msg_level = "danger" elif demux_length == 1: demux_file = demux[0] demux_file_stats = demux_stats(demux_file) stats.append(("Number of sequences", demux_file_stats.n)) msg_level = "success" self.render( "vamps_submission.html", study_title=study.title, stats=stats, message=msg, study_id=study.id, level=msg_level, preprocessed_data_id=preprocessed_data_id, )
def test_delete_sample_or_column(self): st = SampleTemplate(1) # Delete a sample template column obs = delete_sample_or_column(SampleTemplate, 1, "columns", "season_environment") exp = {'status': "success", 'message': ""} self.assertEqual(obs, exp) self.assertNotIn('season_environment', st.categories()) # Delete a sample template sample - need to add one sample that we # will remove npt.assert_warns( QiitaDBWarning, st.extend, pd.DataFrame.from_dict({'Sample1': { 'taxon_id': '9606' }}, orient='index', dtype=str)) self.assertIn('1.Sample1', st.keys()) obs = delete_sample_or_column(SampleTemplate, 1, "samples", "1.Sample1") exp = {'status': "success", 'message': ""} self.assertEqual(obs, exp) self.assertNotIn('1.Sample1', st.keys()) # Delete a prep template column pt = PrepTemplate(2) obs = delete_sample_or_column(PrepTemplate, 2, "columns", "target_subfragment") exp = {'status': "success", 'message': ""} self.assertEqual(obs, exp) self.assertNotIn('target_subfragment', pt.categories()) # Delte a prep template sample metadata = pd.DataFrame.from_dict( { '1.SKB8.640193': { 'barcode': 'GTCCGCAAGTTA', 'primer': 'GTGCCAGCMGCCGCGGTAA' }, '1.SKD8.640184': { 'barcode': 'CGTAGAGCTCTC', 'primer': 'GTGCCAGCMGCCGCGGTAA' } }, orient='index', dtype=str) pt = npt.assert_warns(QiitaDBWarning, PrepTemplate.create, metadata, Study(1), "16S") obs = delete_sample_or_column(PrepTemplate, pt.id, "samples", '1.SKD8.640184') exp = {'status': "success", 'message': ""} self.assertEqual(obs, exp) self.assertNotIn('1.SKD8.640184', pt.categories()) # Exception obs = delete_sample_or_column(PrepTemplate, 2, "samples", "1.SKM9.640192") exp = { 'status': "danger", 'message': "Prep info file '2' has files attached, you cannot " "delete samples." } self.assertEqual(obs, exp) # No "samples" or "columns" obs = delete_sample_or_column(PrepTemplate, 2, "not_samples", "NOP") exp = { 'status': 'danger', 'message': 'Unknown value "not_samples". Choose between ' '"samples" and "columns"' } self.assertEqual(obs, exp)
def test_delete_sample_or_column(self): st = SampleTemplate(1) # Delete a sample template column job = self._create_job( 'delete_sample_or_column', { 'obj_class': 'SampleTemplate', 'obj_id': 1, 'sample_or_col': 'columns', 'name': 'season_environment' }) private_task(job.id) self.assertEqual(job.status, 'success') self.assertNotIn('season_environment', st.categories()) # Delete a sample template sample - need to add one # sample that we will remove npt.assert_warns( QiitaDBWarning, st.extend, pd.DataFrame.from_dict({'Sample1': { 'taxon_id': '9606' }}, orient='index', dtype=str)) self.assertIn('1.Sample1', st.keys()) job = self._create_job( 'delete_sample_or_column', { 'obj_class': 'SampleTemplate', 'obj_id': 1, 'sample_or_col': 'samples', 'name': '1.Sample1' }) private_task(job.id) self.assertEqual(job.status, 'success') self.assertNotIn('1.Sample1', st.keys()) # Delete a prep template column pt = PrepTemplate(1) job = self._create_job( 'delete_sample_or_column', { 'obj_class': 'PrepTemplate', 'obj_id': 1, 'sample_or_col': 'columns', 'name': 'target_subfragment' }) private_task(job.id) self.assertEqual(job.status, 'success') self.assertNotIn('target_subfragment', pt.categories()) # Delete a prep template sample metadata = pd.DataFrame.from_dict( { '1.SKB8.640193': { 'barcode': 'GTCCGCAAGTTA', 'primer': 'GTGCCAGCMGCCGCGGTAA' }, '1.SKD8.640184': { 'barcode': 'CGTAGAGCTCTC', 'primer': 'GTGCCAGCMGCCGCGGTAA' } }, orient='index', dtype=str) pt = npt.assert_warns(QiitaDBWarning, PrepTemplate.create, metadata, Study(1), "16S") job = self._create_job( 'delete_sample_or_column', { 'obj_class': 'PrepTemplate', 'obj_id': pt.id, 'sample_or_col': 'samples', 'name': '1.SKD8.640184' }) private_task(job.id) self.assertNotIn('1.SKD8.640184', pt.keys()) # Test exceptions job = self._create_job( 'delete_sample_or_column', { 'obj_class': 'UnknownClass', 'obj_id': 1, 'sample_or_col': 'columns', 'name': 'column' }) private_task(job.id) self.assertEqual(job.status, 'error') self.assertIn( 'Unknown value "UnknownClass". Choose between ' '"SampleTemplate" and "PrepTemplate"', job.log.msg) job = self._create_job( 'delete_sample_or_column', { 'obj_class': 'SampleTemplate', 'obj_id': 1, 'sample_or_col': 'unknown', 'name': 'column' }) private_task(job.id) self.assertEqual(job.status, 'error') self.assertIn( 'Unknown value "unknown". Choose between "samples" ' 'and "columns"', job.log.msg)
def test_delete_sample_or_column(self): st = SampleTemplate(1) # Delete a sample template column job = self._create_job('delete_sample_or_column', {'obj_class': 'SampleTemplate', 'obj_id': 1, 'sample_or_col': 'columns', 'name': 'season_environment'}) private_task(job.id) self.assertEqual(job.status, 'success') self.assertNotIn('season_environment', st.categories()) # Delete a sample template sample - need to add one # sample that we will remove npt.assert_warns( QiitaDBWarning, st.extend, pd.DataFrame.from_dict({'Sample1': {'taxon_id': '9606'}}, orient='index', dtype=str)) self.assertIn('1.Sample1', st.keys()) job = self._create_job('delete_sample_or_column', {'obj_class': 'SampleTemplate', 'obj_id': 1, 'sample_or_col': 'samples', 'name': '1.Sample1'}) private_task(job.id) self.assertEqual(job.status, 'success') self.assertNotIn('1.Sample1', st.keys()) # Delete a prep template column pt = PrepTemplate(1) job = self._create_job('delete_sample_or_column', {'obj_class': 'PrepTemplate', 'obj_id': 1, 'sample_or_col': 'columns', 'name': 'target_subfragment'}) private_task(job.id) self.assertEqual(job.status, 'success') self.assertNotIn('target_subfragment', pt.categories()) # Delete a prep template sample metadata = pd.DataFrame.from_dict( {'1.SKB8.640193': {'barcode': 'GTCCGCAAGTTA', 'primer': 'GTGCCAGCMGCCGCGGTAA'}, '1.SKD8.640184': {'barcode': 'CGTAGAGCTCTC', 'primer': 'GTGCCAGCMGCCGCGGTAA'}}, orient='index', dtype=str) pt = npt.assert_warns(QiitaDBWarning, PrepTemplate.create, metadata, Study(1), "16S") job = self._create_job('delete_sample_or_column', {'obj_class': 'PrepTemplate', 'obj_id': pt.id, 'sample_or_col': 'samples', 'name': '1.SKD8.640184'}) private_task(job.id) self.assertNotIn('1.SKD8.640184', pt.keys()) # Test exceptions job = self._create_job('delete_sample_or_column', {'obj_class': 'UnknownClass', 'obj_id': 1, 'sample_or_col': 'columns', 'name': 'column'}) private_task(job.id) self.assertEqual(job.status, 'error') self.assertIn('Unknown value "UnknownClass". Choose between ' '"SampleTemplate" and "PrepTemplate"', job.log.msg) job = self._create_job('delete_sample_or_column', {'obj_class': 'SampleTemplate', 'obj_id': 1, 'sample_or_col': 'unknown', 'name': 'column'}) private_task(job.id) self.assertEqual(job.status, 'error') self.assertIn('Unknown value "unknown". Choose between "samples" ' 'and "columns"', job.log.msg)
if not allow_submission: msg_list.append( "Only artifacts with a single prep template can be submitted") # If allow_submission is already false, we technically don't need to # do the following work. However, there is no clean way to fix this # using the current structure, so we perform the work as we # did so it doesn't fail. # We currently support only one prep template for submission, so # grabbing the first one prep_template = prep_templates[0] study = preprocessed_data.study sample_template = study.sample_template >>>>>>> 405cbef0c9f71c620da95a0c1ba6c7d3d588b3ed stats = [('Number of samples', len(prep_template)), ('Number of metadata headers', len(sample_template.categories()))] demux = [path for _, path, ftype in preprocessed_data.get_filepaths() if ftype == 'preprocessed_demux'] demux_length = len(demux) if not demux_length: msg = ("Study does not appear to have demultiplexed " "sequences associated") msg_level = 'danger' elif demux_length > 1: msg = ("Study appears to have multiple demultiplexed files!") msg_level = 'danger' elif demux_length == 1: demux_file = demux[0] demux_file_stats = demux_stats(demux_file)
def test_delete_sample_or_column(self): st = SampleTemplate(1) # Delete a sample template column obs = delete_sample_or_column(SampleTemplate, 1, "columns", "season_environment") exp = {'status': "success", 'message': ""} self.assertEqual(obs, exp) self.assertNotIn('season_environment', st.categories()) # Delete a sample template sample - need to add one sample that we # will remove npt.assert_warns( QiitaDBWarning, st.extend, pd.DataFrame.from_dict({'Sample1': {'taxon_id': '9606'}}, orient='index', dtype=str)) self.assertIn('1.Sample1', st.keys()) obs = delete_sample_or_column(SampleTemplate, 1, "samples", "1.Sample1") exp = {'status': "success", 'message': ""} self.assertEqual(obs, exp) self.assertNotIn('1.Sample1', st.keys()) # Delete a prep template column pt = PrepTemplate(2) obs = delete_sample_or_column(PrepTemplate, 2, "columns", "target_subfragment") exp = {'status': "success", 'message': ""} self.assertEqual(obs, exp) self.assertNotIn('target_subfragment', pt.categories()) # Delte a prep template sample metadata = pd.DataFrame.from_dict( {'1.SKB8.640193': {'barcode': 'GTCCGCAAGTTA', 'primer': 'GTGCCAGCMGCCGCGGTAA'}, '1.SKD8.640184': {'barcode': 'CGTAGAGCTCTC', 'primer': 'GTGCCAGCMGCCGCGGTAA'}}, orient='index', dtype=str) pt = npt.assert_warns(QiitaDBWarning, PrepTemplate.create, metadata, Study(1), "16S") obs = delete_sample_or_column(PrepTemplate, pt.id, "samples", '1.SKD8.640184') exp = {'status': "success", 'message': ""} self.assertEqual(obs, exp) self.assertNotIn('1.SKD8.640184', pt.categories()) # Exception obs = delete_sample_or_column(PrepTemplate, 2, "samples", "1.SKM9.640192") exp = {'status': "danger", 'message': "Prep info file '2' has files attached, you cannot " "delete samples."} self.assertEqual(obs, exp) # No "samples" or "columns" obs = delete_sample_or_column(PrepTemplate, 2, "not_samples", "NOP") exp = {'status': 'danger', 'message': 'Unknown value "not_samples". Choose between ' '"samples" and "columns"'} self.assertEqual(obs, exp)