def post(self, analysis_id): analysis_id = int(analysis_id.split("/")[0]) analysis_id_sent = int(self.get_argument('analysis_id')) action = self.get_argument('action') if analysis_id != analysis_id_sent or action != 'delete_analysis': raise QiitaPetAuthorizationError( self.current_user.id, 'analysis/results/%d-delete' % analysis_id) analysis = Analysis(analysis_id) analysis_name = analysis.name check_analysis_access(self.current_user, analysis) try: Analysis.delete(analysis_id) msg = ("Analysis <b><i>%s</i></b> has been deleted." % (analysis_name)) level = "success" except Exception as e: e = str(e) msg = ("Couldn't remove <b><i>%s</i></b> analysis: %s" % (analysis_name, e)) level = "danger" LogEntry.create( 'Runtime', "Couldn't remove analysis ID %d: %s" % (analysis_id, e)) self.redirect(u"/analysis/show/?level=%s&message=%s" % (level, msg))
def test_create(self): """Makes sure creation works as expected""" # make first job new = Job.create("18S", "Alpha Rarefaction", {"opt1": 4}, Analysis(1)) self.assertEqual(new.id, 4) # make sure job inserted correctly obs = self.conn_handler.execute_fetchall("SELECT * FROM qiita.job " "WHERE job_id = 4") exp = [[4, 2, 1, 3, '{"opt1":4}', None]] self.assertEqual(obs, exp) # make sure job added to analysis correctly obs = self.conn_handler.execute_fetchall("SELECT * FROM " "qiita.analysis_job WHERE " "job_id = 4") exp = [[1, 4]] self.assertEqual(obs, exp) # make second job with diff datatype and command to test column insert new = Job.create("16S", "Beta Diversity", {"opt1": 4}, Analysis(1)) self.assertEqual(new.id, 5) # make sure job inserted correctly obs = self.conn_handler.execute_fetchall("SELECT * FROM qiita.job " "WHERE job_id = 5") exp = [[5, 1, 1, 2, '{"opt1":4}', None]] self.assertEqual(obs, exp) # make sure job added to analysis correctly obs = self.conn_handler.execute_fetchall("SELECT * FROM " "qiita.analysis_job WHERE " "job_id = 5") exp = [[1, 5]] self.assertEqual(obs, exp)
def test_set_step(self): new = Analysis.create(User("*****@*****.**"), "newAnalysis", "A New Analysis", Analysis(1)) new.step = 2 sql = "SELECT * FROM qiita.analysis_workflow WHERE analysis_id = 3" obs = self.conn_handler.execute_fetchall(sql) self.assertEqual(obs, [[3, 2]])
def post(self, analysis_id): analysis_id = int(analysis_id.split("/")[0]) analysis_id_sent = int(self.get_argument('analysis_id')) action = self.get_argument('action') if analysis_id != analysis_id_sent or action != 'delete_analysis': raise QiitaPetAuthorizationError( self.current_user.id, 'analysis/results/%d-delete' % analysis_id) analysis = Analysis(analysis_id) analysis_name = analysis.name check_analysis_access(self.current_user, analysis) try: Analysis.delete(analysis_id) msg = ("Analysis <b><i>%s</i></b> has been deleted." % ( analysis_name)) level = "success" except Exception as e: e = str(e) msg = ("Couldn't remove <b><i>%s</i></b> analysis: %s" % ( analysis_name, e)) level = "danger" LogEntry.create('Runtime', "Couldn't remove analysis ID %d: %s" % (analysis_id, e)) self.redirect(u"/analysis/show/?level=%s&message=%s" % (level, msg))
def post(self): analysis = Analysis(int(self.get_argument('analysis-id'))) # set to third step since this page is third step in workflow analysis.step = SELECT_COMMANDS data_types = analysis.data_types commands = Command.get_commands_by_datatype() self.render('select_commands.html', commands=commands, data_types=data_types, aid=analysis.id)
def test_check_portal(self): """Correctly checks if object is accessable in portal given""" qiita_config.portal = 'QIITA' tester = Analysis(1) self.assertTrue(tester._check_portal(1)) qiita_config.portal = 'EMP' self.assertFalse(tester._check_portal(1)) self.assertTrue(self.tester._check_portal(1))
def test_remove_portal(self): Portal.create("NEWPORTAL", "SOMEDESC") # Select some samples on a default analysis qiita_config.portal = "NEWPORTAL" a = Analysis(User("*****@*****.**").default_analysis) a.add_samples({1: ['1.SKB8.640193', '1.SKD5.640186']}) Portal.delete("NEWPORTAL") obs = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.portal_type") exp = [[1, 'QIITA', 'QIITA portal. Access to all data stored ' 'in database.'], [2, 'EMP', 'EMP portal']] self.assertItemsEqual(obs, exp) obs = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.analysis_portal") exp = [[1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [7, 2], [8, 2], [9, 2], [10, 2]] self.assertItemsEqual(obs, exp) with self.assertRaises(QiitaDBLookupError): Portal.delete("NOEXISTPORTAL") with self.assertRaises(QiitaDBError): Portal.delete("QIITA") Portal.create("NEWPORTAL2", "SOMEDESC") # Add analysis to this new portal and make sure error raised qiita_config.portal = "NEWPORTAL2" Analysis.create(User("*****@*****.**"), "newportal analysis", "desc") qiita_config.portal = "QIITA" with self.assertRaises(QiitaDBError): Portal.delete("NEWPORTAL2") # Add study to this new portal and make sure error raised info = { "timeseries_type_id": 1, "metadata_complete": True, "mixs_compliant": True, "number_samples_collected": 25, "number_samples_promised": 28, "study_alias": "FCM", "study_description": "Microbiome of people who eat nothing but " "fried chicken", "study_abstract": "Exploring how a high fat diet changes the " "gut microbiome", "emp_person_id": StudyPerson(2), "principal_investigator_id": StudyPerson(3), "lab_person_id": StudyPerson(1) } Portal.create("NEWPORTAL3", "SOMEDESC") qiita_config.portal = "NEWPORTAL3" Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], info) qiita_config.portal = "QIITA" with self.assertRaises(QiitaDBError): Portal.delete("NEWPORTAL3")
def test_exists(self): qiita_config.portal = 'QIITA' self.assertTrue(Analysis.exists(1)) new_id = get_count("qiita.analysis") + 1 self.assertFalse(Analysis.exists(new_id)) qiita_config.portal = 'EMP' self.assertFalse(Analysis.exists(1)) new_id = get_count("qiita.analysis") + 1 self.assertFalse(Analysis.exists(new_id))
def test_delete(self): # successful delete total_analyses = get_count("qiita.analysis") Analysis.delete(1) self.assertEqual(total_analyses - 1, get_count("qiita.analysis")) # no possible to delete with self.assertRaises(QiitaDBUnknownIDError): Analysis.delete(total_analyses + 1)
def test_create_nonqiita_portal(self): new_id = get_count("qiita.analysis") + 1 qiita_config.portal = "EMP" Analysis.create(User("*****@*****.**"), "newAnalysis", "A New Analysis") # make sure portal is associated obs = self.conn_handler.execute_fetchall( "SELECT * from qiita.analysis_portal WHERE analysis_id = %s", [new_id]) self.assertEqual(obs, [[new_id, 2], [new_id, 1]])
def test_get_by_status(self): qiita_config.portal = 'QIITA' self.assertEqual(Analysis.get_by_status('public'), set([])) qiita_config.portal = 'EMP' self.assertEqual(Analysis.get_by_status('public'), set([])) self.analysis.status = "public" qiita_config.portal = 'QIITA' self.assertEqual(Analysis.get_by_status('public'), {1}) qiita_config.portal = 'EMP' self.assertEqual(Analysis.get_by_status('public'), set([]))
def test_create_exists_return_existing(self): """Makes sure creation doesn't duplicate a job by returning existing""" Analysis.create(User("*****@*****.**"), "new", "desc") self.conn_handler.execute( "INSERT INTO qiita.analysis_sample (analysis_id, " "processed_data_id, sample_id) VALUES (3,1,'SKB8.640193'), " "(3,1,'SKD8.640184'), (3,1,'SKB7.640196'), (3,1,'SKM9.640192')," "(3,1,'SKM4.640180')") new = Job.create("18S", "Beta Diversity", {"--otu_table_fp": 1, "--mapping_fp": 1}, Analysis(3), return_existing=True) self.assertEqual(new.id, 2)
def test_create_exists_return_existing(self): """Makes sure creation doesn't duplicate a job by returning existing""" Analysis.create(User("*****@*****.**"), "new", "desc") self.conn_handler.execute( "INSERT INTO qiita.analysis_sample " "(analysis_id, processed_data_id, sample_id) VALUES " "(3, 1, '1.SKB8.640193'), (3, 1, '1.SKD8.640184'), " "(3, 1, '1.SKB7.640196'), (3, 1, '1.SKM9.640192'), " "(3, 1, '1.SKM4.640180')") new = Job.create("18S", "Beta Diversity", {"--otu_table_fp": 1, "--mapping_fp": 1}, Analysis(3), return_existing=True) self.assertEqual(new.id, 2)
def get(self, analysis_id): user = self.current_user analysis_id = int(analysis_id) check_analysis_access(User(user), analysis_id) analysis = Analysis(analysis_id) jobres = defaultdict(list) for job in analysis.jobs: jobject = Job(job) jobres[jobject.datatype].append( (jobject.command[0], jobject.results)) dropped = {} for proc_data_id, samples in viewitems(analysis.dropped_samples): proc_data = ProcessedData(proc_data_id) key = "Data type %s, Study: %s" % (proc_data.data_type(), proc_data.study) dropped[key] = samples self.render("analysis_results.html", user=self.current_user, jobres=jobres, aname=analysis.name, dropped=dropped, basefolder=get_db_files_base_dir()) # wipe out cached messages for this analysis r_server = Redis() key = '%s:messages' % self.current_user oldmessages = r_server.lrange(key, 0, -1) if oldmessages is not None: for message in oldmessages: if '"analysis": %d' % analysis_id in message: r_server.lrem(key, message, 1)
def post(self, analysis_id): user = self.current_user analysis_id = int(analysis_id) rarefaction_depth = self.get_argument('rarefaction-depth') # convert to integer if rarefaction level given if rarefaction_depth: rarefaction_depth = int(rarefaction_depth) else: rarefaction_depth = None check_analysis_access(User(user), analysis_id) command_args = self.get_arguments("commands") split = [x.split("#") for x in command_args] commands = ["%s: %s" % (s[0], s[1]) for s in split] analysis = Analysis(analysis_id) self.render("analysis_waiting.html", user=user, aid=analysis_id, aname=analysis.name, commands=commands) app = RunAnalysis() app(user, analysis, split, comm_opts={}, rarefaction_depth=rarefaction_depth)
def test_generate_analysis_tgz(self): obs_sout, obs_serr, obs_return = _generate_analysis_tgz(Analysis(1)) # not testing obs_serr as it will change depending on the system's tar # version self.assertEqual(obs_sout, "") self.assertEqual(obs_return, 0)
def test_create_exists(self): """Makes sure creation doesn't duplicate a job""" with self.assertRaises(QiitaDBDuplicateError): Job.create("18S", "Beta Diversity", { "--otu_table_fp": 1, "--mapping_fp": 1 }, Analysis(1))
def test_set_options(self): new = Job.create("18S", "Alpha Rarefaction", {"opt1": 4}, Analysis(1)) new.options = self.options self.options['--output_dir'] = join( get_db_files_base_dir(), 'job/4_alpha_rarefaction.' 'py_output_dir') self.assertEqual(new.options, self.options)
def test_add_samples(self): new = Analysis.create(User("*****@*****.**"), "newAnalysis", "A New Analysis") new.add_samples({1: ['1.SKB8.640193', '1.SKD5.640186']}) obs = new.samples self.assertEqual(obs.keys(), [1]) self.assertItemsEqual(obs[1], ['1.SKB8.640193', '1.SKD5.640186'])
def get(self, analysis_id): analysis_id = int(analysis_id.split("/")[0]) analysis = Analysis(analysis_id) check_analysis_access(self.current_user, analysis) jobres = defaultdict(list) for job in analysis.jobs: jobject = Job(job) jobres[jobject.datatype].append( (jobject.command[0], jobject.results)) dropped = {} dropped_samples = analysis.dropped_samples if dropped_samples: for proc_data_id, samples in viewitems(dropped_samples): proc_data = ProcessedData(proc_data_id) key = "Data type %s, Study: %s" % (proc_data.data_type(), proc_data.study) dropped[key] = samples self.render("analysis_results.html", jobres=jobres, aname=analysis.name, dropped=dropped, basefolder=get_db_files_base_dir())
def test_set_options(self): new = Job.create("18S", "Alpha Rarefaction", {"opt1": 4}, Analysis(1)) new.options = self.options self.options['--output_dir'] = join(self._job_folder, '4_alpha_rarefaction.' 'py_output_dir') self.assertEqual(new.options, self.options)
def test_redis_comms(self): """Make sure redis communication happens""" msgs = [] redis = Redis() pubsub = redis.pubsub() pubsub.subscribe("*****@*****.**") app = RunAnalysis() app("*****@*****.**", Analysis(2), [], rarefaction_depth=100) for msg in pubsub.listen(): if msg['type'] == 'message': msgs.append(msg['data']) if "allcomplete" in msg['data']: pubsub.unsubscribe("*****@*****.**") break self.assertEqual( msgs, ['{"msg": "Running", "command": "18S: Beta Diversity", ' '"analysis": 2}', '{"msg": "ERROR", "command": "18S: Beta Diversity", ' '"analysis": 2}', '{"msg": "allcomplete", "analysis": 2}']) log = self.conn_handler.execute_fetchall( "SELECT * from qiita.logging") self.assertEqual(1, len(log)) log = log[0] self.assertEqual(1, log[0]) self.assertEqual(2, log[2]) self.assertTrue(len(log[3]) > 0) self.assertTrue('[{"job": 3, "analysis": 2}]')
def analyisis_job_handler_get_request(analysis_id, user): """Returns the job information of the analysis Parameters ---------- analysis_id: int The analysis id user : qiita_db.user.User The user performing the request Returns ------- dict with the jobs information """ analysis = Analysis(analysis_id) # Check if the user actually has access to the analysis check_analysis_access(user, analysis) return { j.id: { 'status': j.status, 'step': j.step, 'error': j.log.msg if j.log else "" } for j in analysis.jobs }
def test_lock_check(self): for status in ["queued", "running", "public", "completed", "error"]: new = Analysis.create(User("*****@*****.**"), "newAnalysis", "A New Analysis") new.status = status with self.assertRaises(QiitaDBStatusError): new._lock_check(self.conn_handler)
def test_delete_analysis(self): # as samples have been submitted to EBI, this will fail job = self._create_job('delete_analysis', {'analysis_id': 1}) private_task(job.id) self.assertEqual(job.status, 'success') with self.assertRaises(QiitaDBUnknownIDError): Analysis(1)
def test_delete_study(self): # as samples have been submitted to EBI, this will fail job = self._create_job('delete_study', {'study': 1}) private_task(job.id) self.assertEqual(job.status, 'error') self.assertIn("Cannot delete artifact 2: Artifact 2 has been " "submitted to EBI", job.log.msg) # making sure the analysis, first thing to delete, still exists self.assertTrue(Analysis.exists(1)) # delete everything from the EBI submissions and the processing job so # we can try again: test success (with tags) with TRN: sql = """DELETE FROM qiita.ebi_run_accession""" TRN.add(sql) sql = """DELETE FROM qiita.artifact_processing_job""" TRN.add(sql) TRN.execute() # adding tags Study(1).update_tags(self.user, ['my new tag!']) job = self._create_job('delete_study', {'study': 1}) private_task(job.id) self.assertEqual(job.status, 'success') with self.assertRaises(QiitaDBUnknownIDError): Study(1)
def test_get(self): a = Analysis(1) u = User('*****@*****.**') self.assertEqual(a.shared_with, [u]) # deselecting args = {'deselected': u.id, 'id': a.id} response = self.get('/analysis/sharing/', args) self.assertEqual(response.code, 200) exp = {'users': [], 'links': ''} self.assertEqual(loads(response.body), exp) self.assertEqual(a.shared_with, []) # Make sure unshared message added to the system self.assertEqual("Analysis 'SomeAnalysis' has been unshared with you.", u.messages()[0][1]) # selecting args = {'selected': u.id, 'id': a.id} response = self.get('/analysis/sharing/', args) self.assertEqual(response.code, 200) exp = { 'users': ['*****@*****.**'], 'links': ('<a target="_blank" href="mailto:[email protected]">Shared</a>') } self.assertEqual(loads(response.body), exp) self.assertEqual(a.shared_with, [u]) # Make sure shared message added to the system self.assertEqual( 'Analysis <a href="/analysis/description/1">\'SomeAnalysis\'</a> ' 'has been shared with you.', u.messages()[0][1])
def test_delete_study(self): # as samples have been submitted to EBI, this will fail job = self._create_job('delete_study', {'study': 1}) private_task(job.id) self.assertEqual(job.status, 'error') self.assertIn( "Cannot delete artifact 2: Artifact 2 has been " "submitted to EBI", job.log.msg) # making sure the analysis, first thing to delete, still exists self.assertTrue(Analysis.exists(1)) # delete everything from the EBI submissions and the processing job so # we can try again: test success (with tags) with TRN: sql = """DELETE FROM qiita.ebi_run_accession""" TRN.add(sql) sql = """DELETE FROM qiita.artifact_processing_job""" TRN.add(sql) TRN.execute() # adding tags Study(1).update_tags(self.user, ['my new tag!']) job = self._create_job('delete_study', {'study': 1}) private_task(job.id) self.assertEqual(job.status, 'success') with self.assertRaises(QiitaDBUnknownIDError): Study(1)
def get(self, analysis_id): analysis_id = int(analysis_id.split("/")[0]) analysis = Analysis(analysis_id) check_analysis_access(self.current_user, analysis) jobres = defaultdict(list) for jobject in analysis.jobs: results = [] for res in jobject.results: name = basename(res) if name.startswith('index'): name = basename(dirname(res)).replace('_', ' ') results.append((res, name)) jobres[jobject.datatype].append((jobject.command[0], results)) dropped_samples = analysis.dropped_samples dropped = defaultdict(list) for proc_data_id, samples in viewitems(dropped_samples): if not samples: continue proc_data = Artifact(proc_data_id) data_type = proc_data.data_type dropped[data_type].append( (proc_data.study.title, len(samples), ', '.join(samples))) self.render("analysis_results.html", analysis_id=analysis_id, jobres=jobres, aname=analysis.name, dropped=dropped, basefolder=get_db_files_base_dir())
def test_post_create_analysis_handler(self): user = User('*****@*****.**') dflt_analysis = user.default_analysis dflt_analysis.add_samples({ 4: [ '1.SKB8.640193', '1.SKD8.640184', '1.SKB7.640196', '1.SKM9.640192', '1.SKM4.640180' ] }) args = { 'name': 'New Test Analysis', 'description': 'Test Analysis Description' } response = self.post('/analysis/create/', args) self.assertRegexpMatches( response.effective_url, r"http://localhost:\d+/analysis/description/\d+/") self.assertEqual(response.code, 200) # The new analysis id is located at the -2 position (see regex above) new_id = response.effective_url.split('/')[-2] a = Analysis(new_id) # Make sure that all jobs have completed before we exit this tests for j in a.jobs: wait_for_processing_job(j.id)
def test_get_deselected(self): a = Analysis(1) u = User('*****@*****.**') args = {'deselected': u.id, 'id': a.id} self.assertEqual(a.shared_with, [u]) response = self.get('/analysis/sharing/', args) self.assertEqual(response.code, 200) exp = {'users': [], 'links': ''} self.assertEqual(loads(response.body), exp) self.assertEqual(a.shared_with, []) # Make sure unshared message added to the system self.assertEqual('Analysis \'SomeAnalysis\' has been unshared from ' 'you.', u.messages()[0][1]) # Share the analysis back with the user a.share(u)
def test_get_no_access(self): s = Analysis(2) u = User('*****@*****.**') args = {'selected': u.id, 'id': 2} response = self.get('/analysis/sharing/', args) self.assertEqual(response.code, 403) self.assertEqual(s.shared_with, [])
def test_analyisis_graph_handler_get_request(self): obs = analyisis_graph_handler_get_request(1, User('*****@*****.**')) # The job id is randomly generated in the test environment. Gather # it here. There is only 1 job in the first artifact of the analysis job_id = Analysis(1).artifacts[0].jobs()[0].id exp = { 'edges': [(8, job_id), (job_id, 9)], 'nodes': [('job', 'job', job_id, 'Single Rarefaction', 'success'), ('artifact', 'BIOM', 9, 'noname\n(BIOM)', 'artifact'), ('artifact', 'BIOM', 8, 'noname\n(BIOM)', 'artifact')], 'workflow': None } self.assertItemsEqual(obs, exp) self.assertItemsEqual(obs['edges'], exp['edges']) self.assertItemsEqual(obs['nodes'], exp['nodes']) self.assertIsNone(obs['workflow']) # An admin has full access to the analysis obs = analyisis_graph_handler_get_request(1, User('*****@*****.**')) self.assertItemsEqual(obs, exp) self.assertItemsEqual(obs['edges'], exp['edges']) self.assertItemsEqual(obs['nodes'], exp['nodes']) # If the analysis is shared with the user he also has access obs = analyisis_graph_handler_get_request(1, User('*****@*****.**')) self.assertItemsEqual(obs, exp) self.assertItemsEqual(obs['edges'], exp['edges']) self.assertItemsEqual(obs['nodes'], exp['nodes']) # The user doesn't have access to the analysis with self.assertRaises(HTTPError): analyisis_graph_handler_get_request(1, User('*****@*****.**'))
def on_message(self, msg): """Selects samples on a message from the user Parameters ---------- msg : JSON str Message containing sample and prc_data information, in the form {proc_data_id': [s1, s2, ...], ...]} """ msginfo = loads(msg) default = Analysis(self.current_user.default_analysis) default.add_samples(msginfo['sel']) # Count total number of unique samples selected and return self.write_message(dumps({ 'sel': len(set( chain.from_iterable(s for s in viewvalues(msginfo['sel'])))) }))
def get(self): user = self.current_user analyses = [ Analysis(a) for a in user.shared_analyses + user.private_analyses ] self.render("show_analyses.html", analyses=analyses)
def post(self): name = self.get_argument('name') desc = self.get_argument('description') analysis = Analysis.create(self.current_user, name, desc, from_default=True) self.redirect(u"%s/analysis/description/%s/" % (qiita_config.portal_dir, analysis.id))
def test_create(self): new = Analysis.create(User("*****@*****.**"), "newAnalysis", "A New Analysis") self.assertEqual(new.id, 3) sql = "SELECT * FROM qiita.analysis WHERE analysis_id = 3" obs = self.conn_handler.execute_fetchall(sql) self.assertEqual(obs, [[3, '*****@*****.**', 'newAnalysis', 'A New Analysis', 1, None]])
def run_analysis(analysis_id, commands, comm_opts=None, rarefaction_depth=None, merge_duplicated_sample_ids=False, **kwargs): """Run an analysis""" analysis = Analysis(analysis_id) ar = RunAnalysis(**kwargs) return ar(analysis, commands, comm_opts, rarefaction_depth, merge_duplicated_sample_ids)
def test_exists_noexist_return_jobid(self): """tests that non-existant job with bad samples returns false""" exists, jid = Job.exists( "16S", "Beta Diversity", {"--otu_table_fp": 1, "--mapping_fp": 27}, Analysis(1), return_existing=True) self.assertFalse(exists) self.assertEqual(jid, None)
def test_get_deselected(self): a = Analysis(1) u = User('*****@*****.**') args = {'deselected': u.id, 'id': a.id} self.assertEqual(a.shared_with, [u]) response = self.get('/analysis/sharing/', args) self.assertEqual(response.code, 200) exp = {'users': [], 'links': ''} self.assertEqual(loads(response.body), exp) self.assertEqual(a.shared_with, []) # Make sure unshared message added to the system self.assertEqual( 'Analysis \'SomeAnalysis\' has been unshared from ' 'you.', u.messages()[0][1]) # Share the analysis back with the user a.share(u)
def test_set_step_twice(self): new_id = get_count("qiita.analysis") + 1 new = Analysis.create(User("*****@*****.**"), "newAnalysis", "A New Analysis", Analysis(1)) new.step = 2 new.step = 4 sql = "SELECT * FROM qiita.analysis_workflow WHERE analysis_id = %s" obs = self.conn_handler.execute_fetchall(sql, [new_id]) self.assertEqual(obs, [[new_id, 4]])
def test_post(self): new_aid = get_count('qiita.analysis') + 1 post_args = {'name': 'post-test', 'description': "test of posting"} response = self.post('/analysis/3', post_args) # Make sure page response loaded sucessfully self.assertEqual(response.code, 200) # make sure analysis created analysis = Analysis(new_aid) self.assertEqual(analysis.name, 'post-test')
def test_delete_analysis(self): # adding extra filepaths to make sure the delete works as expected, we # basically want 8 -> 9 -> 10 -> 12 -> 14 # -> 11 -> 13 fd, fp10 = mkstemp(suffix='_table.biom') close(fd) fd, fp11 = mkstemp(suffix='_table.biom') close(fd) fd, fp12 = mkstemp(suffix='_table.biom') close(fd) fd, fp13 = mkstemp(suffix='_table.biom') close(fd) fd, fp14 = mkstemp(suffix='_table.biom') close(fd) with biom_open(fp10, 'w') as f: et.to_hdf5(f, "test") with biom_open(fp11, 'w') as f: et.to_hdf5(f, "test") with biom_open(fp12, 'w') as f: et.to_hdf5(f, "test") with biom_open(fp13, 'w') as f: et.to_hdf5(f, "test") with biom_open(fp14, 'w') as f: et.to_hdf5(f, "test") self._clean_up_files.extend([fp10, fp11, fp12, fp13, fp14]) # copying some processing parameters a9 = Artifact(9) pp = a9.processing_parameters # 7: BIOM a10 = Artifact.create([(fp10, 7)], "BIOM", parents=[a9], processing_parameters=pp) a11 = Artifact.create([(fp11, 7)], "BIOM", parents=[a9], processing_parameters=pp) a12 = Artifact.create([(fp12, 7)], "BIOM", parents=[a10], processing_parameters=pp) Artifact.create([(fp13, 7)], "BIOM", parents=[a11], processing_parameters=pp) Artifact.create([(fp14, 7)], "BIOM", parents=[a12], processing_parameters=pp) job = self._create_job('delete_analysis', {'analysis_id': 1}) private_task(job.id) self.assertEqual(job.status, 'success') with self.assertRaises(QiitaDBUnknownIDError): Analysis(1)
def post(self, analysis_id): analysis = Analysis(analysis_id) check_analysis_access(self.current_user, analysis) message = '' try: Analysis(analysis_id).make_public() except Exception as e: message = str(e) res = analysis_description_handler_get_request( analysis_id, self.current_user) if message: # this will display the error message in the main banner res['level'] = 'danger' res['message'] = message self.render("analysis_description.html", **res)
def get(self): analysis_id = int(self.get_argument('id')) analysis = Analysis(analysis_id) if not analysis.has_access(self.current_user): raise HTTPError(403, 'User %s does not have permissions to share ' 'analysis %s' % ( self.current_user.id, analysis.id)) selected = self.get_argument('selected', None) deselected = self.get_argument('deselected', None) if selected is not None: yield Task(self._share, analysis, selected) if deselected is not None: yield Task(self._unshare, analysis, deselected) users, links = yield Task(self._get_shared_for_study, analysis) self.write(dumps({'users': users, 'links': links}))
def post(self): name = self.get_argument('name') desc = self.get_argument('description') analysis = Analysis.create(self.current_user, name, desc, from_default=True) # set to third step since this page is third step in workflow analysis.step = SELECT_COMMANDS data_types = analysis.data_types commands = Command.get_commands_by_datatype() self.render('select_commands.html', commands=commands, data_types=data_types, aid=analysis.id)
def post(self): name = self.get_argument('name') desc = self.get_argument('description') mdsi = self.get_argument('merge_duplicated_sample_ids', False) if mdsi in (b'on', 'on'): mdsi = True analysis = Analysis.create( self.current_user, name, desc, merge_duplicated_sample_ids=mdsi, from_default=True) self.redirect(u"%s/analysis/description/%s/" % (qiita_config.portal_dir, analysis.id))
def post(self): analysis_id = int(self.get_argument('analysis_id')) analysis = Analysis(analysis_id) analysis_name = analysis.name check_analysis_access(self.current_user, analysis) try: Analysis.delete(analysis_id) msg = ("Analysis <b><i>%s</i></b> has been deleted." % ( analysis_name)) level = "success" except Exception as e: e = str(e) msg = ("Couldn't remove <b><i>%s</i></b> analysis: %s" % ( analysis_name, e)) level = "danger" LogEntry.create('Runtime', "Couldn't remove analysis ID %d: %s" % (analysis_id, e)) self.redirect(u"/analysis/show/?level=%s&message=%s" % (level, msg))
def test_create(self): sql = "SELECT EXTRACT(EPOCH FROM NOW())" time1 = float(self.conn_handler.execute_fetchall(sql)[0][0]) new_id = get_count("qiita.analysis") + 1 new = Analysis.create(User("*****@*****.**"), "newAnalysis", "A New Analysis") self.assertEqual(new.id, new_id) sql = ("SELECT analysis_id, email, name, description, " "analysis_status_id, pmid, EXTRACT(EPOCH FROM timestamp) " "FROM qiita.analysis WHERE analysis_id = %s") obs = self.conn_handler.execute_fetchall(sql, [new_id]) self.assertEqual(obs[0][:-1], [new_id, '*****@*****.**', 'newAnalysis', 'A New Analysis', 1, None]) self.assertTrue(time1 < float(obs[0][-1]))
def post(self): analysis_id = self.get_argument('analysis-id') study_args = self.get_arguments('studies') split = [x.split("#") for x in study_args] # build dictionary of studies and datatypes selected # as well a set of unique datatypes selected study_dts = defaultdict(list) data_types = set() for study_id, data_type in split: study_dts[study_id].append(data_type) data_types.add(data_type) # sort the elements to have 16S be the first tho show on the tabs data_types = sorted(list(data_types)) # FIXME: Pull out from the database, see #111 commands = {'16S': ['Beta Diversity', 'Summarize Taxa'], '18S': ['Beta Diversity', 'Summarize Taxa'], 'Metabolomic': ['Beta Diversity'], 'Metagenomic': ['Beta Diversity']} self.render('select_commands.html', user=self.get_current_user(), commands=commands, data_types=data_types, aid=analysis_id) analysis = Analysis(analysis_id) for study_id in study_dts: study = Study(study_id) processed_data = {ProcessedData(pid).data_type: pid for pid in study.processed_data} sample_ids = SampleTemplate(study.id).keys() for data_type in study_dts[study.id]: samples = [(processed_data[data_type], sid) for sid in sample_ids] analysis.add_samples(samples)
def post(self): name = self.get_argument('name') description = self.get_argument('description') user = self.get_current_user() # create list of studies study_ids = {s.id for s in Study.get_public()} userobj = User(user) [study_ids.add(x) for x in userobj.private_studies] [study_ids.add(x) for x in userobj.shared_studies] studies = [Study(i) for i in study_ids] analysis = Analysis.create(User(user), name, description) self.render('select_studies.html', user=user, aid=analysis.id, studies=studies)
def test_select_samples(self): newaid = Analysis.create(User("*****@*****.**"), "test1", "testdesc").id post_args = { 'analysis-id': newaid, 'action': 'select', 'availstudies': "1#1", '1#1': 1, '1': 'SKD5.640186'} response = self.post('/analysis/2', post_args) # Make sure page response loaded sucessfully self.assertEqual(response.code, 200) # make sure sample added self.assertTrue("SKD5.640186" in str(response.body))
def test_get_analysis_jobs_handler(self): user = User('*****@*****.**') dflt_analysis = user.default_analysis dflt_analysis.add_samples( {4: ['1.SKB8.640193', '1.SKD8.640184', '1.SKB7.640196', '1.SKM9.640192', '1.SKM4.640180']}) new = Analysis.create(user, "newAnalysis", "A New Analysis", from_default=True) response = self.get('/analysis/description/%s/jobs/' % new.id) self.assertEqual(response.code, 200) # There is only one job job_id = new.jobs[0].id obs = loads(response.body) exp = {job_id: {'status': 'queued', 'step': None, 'error': ""}} self.assertEqual(obs, exp)
def on_message(self, msg): # When the websocket receives a message from the javascript client, # parse into JSON msginfo = loads(msg) default = Analysis(self.current_user.default_analysis) if 'remove_sample' in msginfo: data = msginfo['remove_sample'] default.remove_samples([data['proc_data']], data['samples']) elif 'remove_pd' in msginfo: data = msginfo['remove_pd'] default.remove_samples([data['proc_data']]) elif 'clear' in msginfo: data = msginfo['clear'] default.remove_samples(data['pids']) self.write_message(msg)
def check_analysis_access(user, analysis_id): """Checks whether user has access to an analysis Parameters ---------- user : User object User to check analysis_id : int Analysis to check access for Raises ------ RuntimeError Tried to access analysis that user does not have access to """ if analysis_id not in Analysis.get_public() + user.shared_analyses + \ user.private_analyses: raise HTTPError(403, "Analysis access denied to %s" % (analysis_id))
def test_create_parent(self): sql = "SELECT EXTRACT(EPOCH FROM NOW())" time1 = float(self.conn_handler.execute_fetchall(sql)[0][0]) new = Analysis.create(User("*****@*****.**"), "newAnalysis", "A New Analysis", Analysis(1)) self.assertEqual(new.id, 3) sql = ("SELECT analysis_id, email, name, description, " "analysis_status_id, pmid, EXTRACT(EPOCH FROM timestamp) " "FROM qiita.analysis WHERE analysis_id = 3") obs = self.conn_handler.execute_fetchall(sql) self.assertEqual(obs[0][:-1], [3, '*****@*****.**', 'newAnalysis', 'A New Analysis', 1, None]) self.assertTrue(time1 < float(obs[0][-1])) sql = "SELECT * FROM qiita.analysis_chain WHERE child_id = 3" obs = self.conn_handler.execute_fetchall(sql) self.assertEqual(obs, [[1, 3]])