def test_run_gprofiler_no_file(self): temp_dir = tempfile.mkdtemp() try: tfile = os.path.join(temp_dir, 'foo') myargs = [tfile] theargs = cdenrichrgenestoterm._parse_arguments('desc', myargs) try: cdenrichrgenestoterm.run_enrichr(tfile, theargs) self.fail('Expected FileNotFoundError') except FileNotFoundError: pass finally: shutil.rmtree(temp_dir)
def test_run_with_pvalue_exceeded(self): temp_dir = tempfile.mkdtemp() try: enrichr = MagicMock() df = pd.DataFrame(columns=[ 'Term', 'Gene_set', 'P-value', 'Adjusted P-value', 'Genes', 'Overlap' ], data=[['term1', 'set1', 1.5, 0.6, 'B;C', '2/25'], ['term2', 'set2', 1.6, 0.7, 'A;C', '7/9']]) txt_file = os.path.join(temp_dir, 'data.txt') df.to_csv(txt_file, index=False, sep='\t', encoding='utf-8') enrichr.enrichr = MagicMock(return_value=enrichr) tfile = os.path.join(temp_dir, 'foo') with open(tfile, 'w') as f: f.write('a,b,c') myargs = [tfile, '--tmpdir', temp_dir] theargs = cdenrichrgenestoterm._parse_arguments('desc', myargs) res = cdenrichrgenestoterm.run_enrichr(tfile, theargs, enrichr=enrichr) gs = self.get_default_genesets() self.assertEqual(None, res) enrichr.enrichr.assert_called_once_with(gene_list=['A', 'B', 'C'], cutoff=0.05, gene_sets=gs, no_plot=True, outdir=temp_dir) finally: shutil.rmtree(temp_dir)
def test_run_where_enrichr_raises_exception(self): temp_dir = tempfile.mkdtemp() try: enrichr = MagicMock() enrichr.enrichr = MagicMock() enrichr.enrichr.side_effect = Exception('Some exception') tfile = os.path.join(temp_dir, 'foo') with open(tfile, 'w') as f: f.write('a,b,c') myargs = [tfile, '--tmpdir', temp_dir] theargs = cdenrichrgenestoterm._parse_arguments('desc', myargs) res = cdenrichrgenestoterm.run_enrichr(tfile, theargs, enrichr=enrichr) gs = self.get_default_genesets() self.assertEqual(None, res) the_call = call(gene_list=['A', 'B', 'C'], cutoff=0.05, gene_sets=gs, no_plot=True, outdir=temp_dir) enrichr.enrichr.assert_has_calls([the_call, the_call]) finally: shutil.rmtree(temp_dir)
def test_run_gprofiler_empty_file(self): temp_dir = tempfile.mkdtemp() try: tfile = os.path.join(temp_dir, 'foo') open(tfile, 'a').close() myargs = [tfile] theargs = cdenrichrgenestoterm._parse_arguments('desc', myargs) res = cdenrichrgenestoterm.run_enrichr(tfile, theargs) self.assertEqual(None, res) finally: shutil.rmtree(temp_dir)
def test_run_with_valid_result_from_multiple_genesets(self): temp_dir = tempfile.mkdtemp() try: enrichr = MagicMock() df = pd.DataFrame( columns=[ 'Term', 'Gene_set', 'P-value', 'Adjusted P-value', 'Genes', 'Overlap' ], data=[['term1', 'set1', 0.6, 0.05, 'B;C', '2/25'], ['term2', 'set2', 0.5, 0.03, 'A;C', '7/9']]) txt_file = os.path.join(temp_dir, 'data.txt') df.to_csv(txt_file, index=False, sep='\t', encoding='utf-8') df = pd.DataFrame( columns=[ 'Term', 'Gene_set', 'P-value', 'Adjusted P-value', 'Genes', 'Overlap' ], data=[['term5', 'set1', 0.6, 0.01, 'B;C', '2/25'], ['term6', 'set2', 0.5, 0.03, 'X;Y', '7/8']]) txt_file = os.path.join(temp_dir, 'data2.txt') df.to_csv(txt_file, index=False, sep='\t', encoding='utf-8') # make a directory with .txt ending just for fun os.makedirs(os.path.join(temp_dir, 'haha.txt'), mode=0o755) enrichr.enrichr = MagicMock(return_value=enrichr) tfile = os.path.join(temp_dir, 'foo') with open(tfile, 'w') as f: f.write('a,b,c') myargs = [tfile, '--tmpdir', temp_dir] theargs = cdenrichrgenestoterm._parse_arguments('desc', myargs) res = cdenrichrgenestoterm.run_enrichr(tfile, theargs, enrichr=enrichr) gs = self.get_default_genesets() self.assertEqual('term5', res['name']) self.assertEqual('set1', res['source']) self.assertEqual(0.01, res['p_value']) self.assertEqual('', res['description']) self.assertEqual(['B', 'C'], res['intersections']) self.assertEqual(25, res['term_size']) enrichr.enrichr.assert_called_once_with(gene_list=['A', 'B', 'C'], cutoff=0.05, gene_sets=gs, no_plot=True, outdir=temp_dir) finally: shutil.rmtree(temp_dir)
def test_run_with_empty_result(self): temp_dir = tempfile.mkdtemp() try: enrichr = MagicMock() enrichr.enrichr = MagicMock(return_value=enrichr) tfile = os.path.join(temp_dir, 'foo') with open(tfile, 'w') as f: f.write('a,b,c') myargs = [tfile, '--tmpdir', temp_dir] theargs = cdenrichrgenestoterm._parse_arguments('desc', myargs) res = cdenrichrgenestoterm.run_enrichr(tfile, theargs, enrichr=enrichr) gs = self.get_default_genesets() self.assertEqual(None, res) enrichr.enrichr.assert_called_once_with(gene_list=['A', 'B', 'C'], cutoff=0.05, gene_sets=gs, no_plot=True, outdir=temp_dir) finally: shutil.rmtree(temp_dir)