def test_verbose_stream(self, mock_open): """ Test progress reporting fails gracefully on stream of unknown len. """ outfh = StringIO.StringIO() f_map = {"in.fa": self.file1} mock_open.side_effect = build_mock_open_side_effect(f_map) with mock.patch('sys.stderr', outfh): res = "\n".join([s.to_fasta_str(include_coords=False) for s in fastaIterator("in.fa", verbose=True)]) self.assertEqual("Warning: unable to show progress for stream. Reason:" + " 'Failed to get max size for stream'", outfh.getvalue()) self.assertEqual(res.strip(), self.file1.strip())
def testTwoFiles(self, mock_open): outfh = StringIO.StringIO() stream_d = {"out.bed": outfh} mock_open.side_effect = build_mock_open_side_effect( self.str_d, stream_d) main(["-o", "out.bed", "file1.bed", "file2.bed"]) expect = "\n".join([ "\t".join(["chr1", "10", "12", "X", "0", "+"]), "\t".join([ "chr1", "75", "80", "X", "0", "+" ]), "\t".join(["chr2", "73", "75", "X", "0", "+"]), "\t".join( ["chr2", "90", "92", "X", "0", "+"]) ]) + "\n" self.assertEqual(outfh.getvalue(), expect)
def setUp(self): f1_conts = "\n".join(["\t".join(["chr1", "10", "20", "F1_R1", "0", "+"]), "\t".join(["chr1", "70", "80", "F2_R2", "0", "+"]), "\t".join(["chr2", "05", "10", "F2_R2", "0", "+"]), "\t".join(["chr2", "70", "75", "F2_R2", "0", "+"]), "\t".join(["chr2", "90", "95", "F2_R2", "0", "+"])]) f2_conts = "\n".join(["\t".join(["chr1", "07", "12", "F1_R1", "0", "+"]), "\t".join(["chr1", "67", "70", "F2_R2", "0", "+"]), "\t".join(["chr1", "75", "85", "F2_R2", "0", "+"]), "\t".join(["chr2", "20", "30", "F2_R2", "0", "+"]), "\t".join(["chr2", "73", "92", "F2_R2", "0", "+"])]) string_d = {"file1.bed": f1_conts, "file2.bed": f2_conts} self.mock_open_side_effect = build_mock_open_side_effect(string_d, {})
def test_anchor_start_norm(self, mock_open): outfh = StringIO.StringIO() streams = {"out.dat": outfh} mock_open.side_effect = build_mock_open_side_effect(self.f_map, streams) _main(["-n", "-o", "out.dat", "regions.bed", "hits1.bed"], sys.argv[0]) expect = [[0, 0.0], [1, 1.0], [2, 0.0], [3, 0.0], [4, 0.0], [5, 0.6667], [6, 0.0], [7, 0.0], [8, 0.0], [9, 0.0], [10, 0.0], [11, 0.0], [12, 0.0], [13, 0.0], [14, 0.0], [15, 1.0]] got = outfh.getvalue() got = [[float(a) for a in x.split()] for x in got.split("\n") if x.strip() != ""] self.assertEqual(len(got), len(expect)) for i in range(0, len(got)): self.assertEqual(len(got[i]), 2) self.assertAlmostEqual(got[i][0], expect[i][0]) self.assertAlmostEqual(got[i][1], expect[i][1], places=4)
def testTwoFiles(self, mock_open): outfh = StringIO.StringIO() stream_d = {"out.bed": outfh} mock_open.side_effect = build_mock_open_side_effect(self.str_d, stream_d) main(["-o", "out.bed", "file1.bed", "file2.bed"]) expect = ( "\n".join( [ "\t".join(["chr1", "10", "12", "X", "0", "+"]), "\t".join(["chr1", "75", "80", "X", "0", "+"]), "\t".join(["chr2", "73", "75", "X", "0", "+"]), "\t".join(["chr2", "90", "92", "X", "0", "+"]), ] ) + "\n" ) self.assertEqual(outfh.getvalue(), expect)
def setUp(self): f1_conts = "\n".join([ "\t".join(["chr1", "10", "20", "F1_R1", "0", "+"]), "\t".join(["chr1", "70", "80", "F2_R2", "0", "+"]), "\t".join(["chr2", "05", "10", "F2_R2", "0", "+"]), "\t".join(["chr2", "70", "75", "F2_R2", "0", "+"]), "\t".join(["chr2", "90", "95", "F2_R2", "0", "+"]) ]) f2_conts = "\n".join([ "\t".join(["chr1", "07", "12", "F1_R1", "0", "+"]), "\t".join(["chr1", "67", "70", "F2_R2", "0", "+"]), "\t".join(["chr1", "75", "85", "F2_R2", "0", "+"]), "\t".join(["chr2", "20", "30", "F2_R2", "0", "+"]), "\t".join(["chr2", "73", "92", "F2_R2", "0", "+"]) ]) string_d = {"file1.bed": f1_conts, "file2.bed": f2_conts} self.mock_open_side_effect = build_mock_open_side_effect(string_d, {})
def test_anchor_start_norm(self, mock_open): outfh = StringIO.StringIO() streams = {"out.dat": outfh} mock_open.side_effect = build_mock_open_side_effect( self.f_map, streams) _main(["-n", "-o", "out.dat", "regions.bed", "hits1.bed"], sys.argv[0]) expect = [[0, 0.0], [1, 1.0], [2, 0.0], [3, 0.0], [4, 0.0], [5, 0.6667], [6, 0.0], [7, 0.0], [8, 0.0], [9, 0.0], [10, 0.0], [11, 0.0], [12, 0.0], [13, 0.0], [14, 0.0], [15, 1.0]] got = outfh.getvalue() got = [[float(a) for a in x.split()] for x in got.split("\n") if x.strip() != ""] self.assertEqual(len(got), len(expect)) for i in range(0, len(got)): self.assertEqual(len(got[i]), 2) self.assertAlmostEqual(got[i][0], expect[i][0]) self.assertAlmostEqual(got[i][1], expect[i][1], places=4)
def test_basic(self, mock_open): outfh1 = StringIO.StringIO() outfh2 = StringIO.StringIO() streams = {"out1.dat": outfh1, "out2.dat": outfh2} mock_open.side_effect = build_mock_open_side_effect(self.f_map, streams) _main(["-f", "sfile1", "-s", "sfile2", "-o", "out1.dat out2.dat"], sys.argv[0]) expectedOutput1 = "\n".join(["\t".join(["chr1", "1", "2", "read1"]), "\t".join(["chr2", "3", "4", "read4"]), "\t".join(["chr4", "3", "3", "read6"])]) expectedOutput2 = "\t".join(["chr1", "3", "4", "read3"]) gotOutput1 = outfh1.getvalue() gotOutput2 = outfh2.getvalue() self.assertTrue(gotOutput1 == (expectedOutput1 + "\n")) self.assertTrue(gotOutput2 == (expectedOutput2 + "\n"))
def test_simple(self, mock_open): out_strm = StringIO.StringIO() f = "@700156R:635:C7RW1ACXX:8:1101:1499:2065 2:N:0:41\n" +\ "ACGAC\n" +\ "+\n" +\ "B<<BB\n" +\ "@700156R:635:C7RW1ACXX:8:1101:1421:2159 2:N:0:41\n" +\ "TCGAC\n" +\ "+\n" +\ "<B0BF\n" f_map = {"in.fq": f} streams = {"out.dat": out_strm} mock_open.side_effect = build_mock_open_side_effect(f_map, streams) _main(["-o", "out.dat", "in.fq"], sys.argv[0]) expect = "\n".join([ "\t".join(["0", "A", "1"]), "\t".join(["0", "T", "1"]), "\t".join(["1", "C", "2"]), "\t".join(["2", "G", "2"]), "\t".join(["3", "A", "2"]), "\t".join(["4", "C", "2"]) ]) self.assertEqual(expect + "\n", out_strm.getvalue())
def test_basic(self, mock_open): outfh1 = StringIO.StringIO() outfh2 = StringIO.StringIO() streams = {"out1.dat": outfh1, "out2.dat": outfh2} mock_open.side_effect = build_mock_open_side_effect( self.f_map, streams) _main(["-f", "sfile1", "-s", "sfile2", "-o", "out1.dat out2.dat"], sys.argv[0]) expectedOutput1 = "\n".join([ "\t".join(["chr1", "1", "2", "read1"]), "\t".join(["chr2", "3", "4", "read4"]), "\t".join(["chr4", "3", "3", "read6"]) ]) expectedOutput2 = "\t".join(["chr1", "3", "4", "read3"]) gotOutput1 = outfh1.getvalue() gotOutput2 = outfh2.getvalue() self.assertTrue(gotOutput1 == (expectedOutput1 + "\n")) self.assertTrue(gotOutput2 == (expectedOutput2 + "\n"))
def test_verbose(self, mock_open, mock_getsize): """ Test progress reporting works properly """ def mock_get_size_se(*args, **kwargs): if (args[0] == "in.fa"): return self.file1_size else: raise ValueError("unknown file: " + args[0]) outfh = StringIO.StringIO() infh = StringIO.StringIO(self.file1) f_map = {"in.fa": infh} mock_open.side_effect = build_mock_open_side_effect({}, f_map) mock_getsize.side_effect = mock_get_size_se with mock.patch('sys.stderr', outfh): res = "" pctngs = [] for s in fastaIterator("in.fa", verbose=True): res += s.to_fasta_str(include_coords=False) res += "\n" pct = math.ceil(100 * infh.tell() / float(self.file1_size)) pctngs.append(" %d%% " % pct) expect = "\r" + "\r".join(["completed" + m + "of processing in.fa" for m in pctngs]) + "\n" self.assertEqual(outfh.getvalue(), expect)
def test_simple(self, mock_open): out_strm = StringIO.StringIO() f = "@700156R:635:C7RW1ACXX:8:1101:1499:2065 2:N:0:41\n" +\ "ACGAC\n" +\ "+\n" +\ "B<<BB\n" +\ "@700156R:635:C7RW1ACXX:8:1101:1421:2159 2:N:0:41\n" +\ "TCGAC\n" +\ "+\n" +\ "<B0BF\n" f_map = {"in.fq": f} streams = {"out.dat": out_strm} mock_open.side_effect = build_mock_open_side_effect(f_map, streams) _main(["-o", "out.dat", "in.fq"], sys.argv[0]) expect = "\n".join(["\t".join(["0", "A", "1"]), "\t".join(["0", "T", "1"]), "\t".join(["1", "C", "2"]), "\t".join(["2", "G", "2"]), "\t".join(["3", "A", "2"]), "\t".join(["4", "C", "2"])]) self.assertEqual(expect + "\n", out_strm.getvalue())