Exemple #1
0
 def setUp(self, get_raw_reads_function):
     #self.raw_reads = self.mock_get_raw_reads("", "")
     self.reads = short_mock_get_alignments('', '')
     self.raw_reads = mock_get_raw_reads('', '')
     self.matrix = DepthMatrix(3, allow_orphans=True)
     self.matrix.make_seq_matrix("", "")
 def setUp(self, get_raw_reads_function): 
     #self.raw_reads = self.mock_get_raw_reads("", "")
     self.reads = short_mock_get_alignments('', '')
     self.raw_reads = mock_get_raw_reads('', '')
     self.matrix = DepthMatrix(3, allow_orphans=True) 
     self.matrix.make_seq_matrix("", "")
Exemple #3
0
class SimpleTest(unittest.TestCase):

    #@mock.patch("subsample_mindepth.get_raw_reads", side_effect=short_mock_get_raw_reads)
    @mock.patch("subsample_mindepth.get_alignments",
                side_effect=short_mock_get_alignments)
    def setUp(self, get_raw_reads_function):
        #self.raw_reads = self.mock_get_raw_reads("", "")
        self.reads = short_mock_get_alignments('', '')
        self.raw_reads = mock_get_raw_reads('', '')
        self.matrix = DepthMatrix(3, allow_orphans=True)
        self.matrix.make_seq_matrix("", "")

    @mock.patch("subsample_mindepth.get_raw_reads",
                side_effect=mock_get_raw_reads)
    def complexSetUp(self, func):
        self.matrix.make_seq_matrix('', '')
        pass

    def test_simple_get_depths(self):
        expected_depths = np.array([3, 2, 1])
        result = self.matrix.get_depths(self.reads)
        #print(result)
        assert_equal(expected_depths, result)

    def test_get_candidate_sequences(self):
        # use the reads from setup and at position 0
        expected = self.reads
        result = self.matrix.get_candidate_sequences(0)
        self.assertEquals(expected, result)

    def test_get_candidate_sequences_no_candidates(self):
        result = self.matrix.get_candidate_sequences(5)
        self.assertEquals([], result)

    def test_yield_greatest_overlaps(self):
        expected = [Alignment('', '', 0, 'A' * 1, 0x2)]

    def test_parse_alignment(self):
        expected = Alignment(
            self.raw_reads[0],
            "gi|110640213|ref|NC_008253.1|_2_451_1:0:0_1:0:0_12b/1", 0, "GCT",
            0)
        result = sub.parse_alignment(self.raw_reads[0])
        self.assertEquals(expected, result)

    @mock.patch("subsample_mindepth.get_raw_reads",
                side_effect=mock_get_raw_reads)
    def test_make_seq_matrix(self, get_reads_function):
        expected_lengths = [4, 2, 2]
        self.matrix.make_seq_matrix("mockecoli.sam",
                                    "gi|110640213|ref|NC_008253.1|")
        actual_lengths = map(len, self.matrix.seq_matrix)
        self.assertEquals(expected_lengths, actual_lengths)

    def test_get_raw_reads(self):
        pass

    def test_yield_greatest_overlaps_ecoli(self):
        self.complexSetUp()
        expected = [
            sub.parse_alignment(self.raw_reads[-2]),
            sub.parse_alignment(self.raw_reads[4])
        ]
        for seq in expected:
            seq.pick()
        result = list(self.matrix.yield_greatest_overlaps(3, 2))
        self.assertEquals(expected, result)
        #expected = self.matrix.seq_matrix[2][0]
        pass

    def test_pickreads(self):
        pass

    def test_minimize_depths_complex(self):
        self.complexSetUp()
        seqs = map(sub.parse_alignment, self.raw_reads)
        expected_matrix = [[seqs[0], seqs[1], seqs[3]], [seqs[4]],
                           [seqs[6], seqs[7]]]
        expected_list = [seqs[0], seqs[1], seqs[3], seqs[4], seqs[6], seqs[7]]
        for seq in expected_list:
            seq.pick()
        expected_depths = np.array([3, 4, 6, 4, 3, 2, 2, 1])
        self.matrix.min_depth = 3
        self.matrix.minimize_depths()
        actual_matrix = self.matrix.seq_matrix
        for row in actual_matrix:
            for seq in row:
                if not seq.picked:
                    row.remove(seq)

        self.assertEquals(expected_matrix, actual_matrix)
        assert_equal(expected_depths, self.matrix.depth_array)
        self.assertEquals([seq.string for seq in expected_list],
                          sub.flatten_and_filter_matrix(
                              self.matrix.seq_matrix))

    def check_files(self, mock_stdout, expected_file):
        samfile = os.path.join(THISD, expected_file)
        expected = open(samfile, 'r').read().strip()
        result = str(mock_stdout.getvalue().strip().decode('UTF-8'))
        if expected_file == 'simplein.sam':
            with open('tests/result.sam', 'w') as out:
                out.write(result)
        return expected, result

    @mock.patch('subsample_mindepth.random.choice',
                side_effect=not_random_subsample)
    @mock.patch("subsamplebam.parse_args", side_effect=mock_args)
    @mock.patch('subsamplebam.sys.stdout', new_callable=BytesIO)
    def test_main(self, mock_stdout, func, func2):
        sub.main()
        self.assertEquals(*self.check_files(mock_stdout, 'test40.sam'))

    ''' The order of these mocks is the order they appear as arguments to the test method. '''
    #    @mock.patch('subsample_mindepth.random.sample', side_effect=not_random_subsample)
    #    @mock.patch("subsamplebam.parse_args", side_effect=more_random_mock_args)
    #    @mock.patch('subsamplebam.sys.stdout', new_callable=BytesIO)
    #    def test_main_more_random(self, mock_stdout, func, func2):
    #        #Expected will equal the input file becasue all reads are subsampled in this case
    #        sub.main()
    #        self.maxDiff = None
    #        expected, result = self.check_files(mock_stdout, 'simplein.sam')
    #        self.assertEquals(expected, result)

    @mock.patch('subsample_mindepth.random.choice',
                side_effect=not_random_subsample)
    def test_minimize_depths_complex_more_random(self, func):
        #TODO: Refactor this so as not to duplicate code with non-random ecoli test
        self.complexSetUp()
        seqs = map(sub.parse_alignment, self.raw_reads)
        expected_matrix = [[seqs[0], seqs[1], seqs[2], seqs[3]],
                           [seqs[4], seqs[5]], [seqs[6], seqs[7]]]
        expected_list = seqs
        for seq in expected_list:
            seq.pick()
        expected_depths = np.array([4, 6, 7, 4, 3, 2, 2, 1])
        self.matrix.min_depth = 3
        self.matrix.more_random = True
        self.matrix.minimize_depths()
        actual_matrix = self.matrix.seq_matrix
        for row in actual_matrix:
            for seq in row:
                if not seq.picked:
                    row.remove(seq)
        self.assertEquals(expected_matrix, actual_matrix)
        assert_equal(expected_depths, self.matrix.depth_array)
        self.assertEquals([seq.string for seq in expected_list],
                          sub.flatten_and_filter_matrix(
                              self.matrix.seq_matrix))
class SimpleTest(unittest.TestCase):

    #@mock.patch("subsample_mindepth.get_raw_reads", side_effect=short_mock_get_raw_reads)
    @mock.patch("subsample_mindepth.get_alignments", side_effect=short_mock_get_alignments)
    def setUp(self, get_raw_reads_function): 
        #self.raw_reads = self.mock_get_raw_reads("", "")
        self.reads = short_mock_get_alignments('', '')
        self.raw_reads = mock_get_raw_reads('', '')
        self.matrix = DepthMatrix(3, allow_orphans=True) 
        self.matrix.make_seq_matrix("", "")

    @mock.patch("subsample_mindepth.get_raw_reads", side_effect=mock_get_raw_reads)
    def complexSetUp(self, func):
        self.matrix.make_seq_matrix('', '')
        pass

    

    def test_simple_get_depths(self):
        expected_depths = np.array([3, 2, 1])
        result = self.matrix.get_depths(self.reads) 
        #print(result)
        assert_equal(expected_depths, result)

    def test_get_candidate_sequences(self):
        # use the reads from setup and at position 0
        expected = self.reads
        result = self.matrix.get_candidate_sequences(0)
        self.assertEquals(expected, result)

    def test_get_candidate_sequences_no_candidates(self):
        result = self.matrix.get_candidate_sequences(5)
        self.assertEquals([], result)


    def test_yield_greatest_overlaps(self):
        expected = [Alignment('', '', 0, 'A'*1, 0x2)]


    def test_parse_alignment(self):
        expected = Alignment(self.raw_reads[0], "gi|110640213|ref|NC_008253.1|_2_451_1:0:0_1:0:0_12b/1", 0, "GCT", 0)
        result = sub.parse_alignment(self.raw_reads[0]) 
        self.assertEquals(expected, result)

    @mock.patch("subsample_mindepth.get_raw_reads", side_effect=mock_get_raw_reads)
    def test_make_seq_matrix(self, get_reads_function):
        expected_lengths = [4, 2, 2]
        self.matrix.make_seq_matrix("mockecoli.sam", "gi|110640213|ref|NC_008253.1|")
        actual_lengths = map(len, self.matrix.seq_matrix)
        self.assertEquals(expected_lengths, actual_lengths)

    def test_get_raw_reads(self):
        pass

    def test_yield_greatest_overlaps_ecoli(self):
        self.complexSetUp()
        expected = [sub.parse_alignment(self.raw_reads[-2]),  sub.parse_alignment(self.raw_reads[4])]
        for seq in expected:
            seq.pick()
        result = list(self.matrix.yield_greatest_overlaps(3, 2))
        self.assertEquals(expected, result)
        #expected = self.matrix.seq_matrix[2][0] 
        pass

    def test_pickreads(self):
        pass

    def test_minimize_depths_complex(self):
        self.complexSetUp()
        seqs = map(sub.parse_alignment, self.raw_reads) 
        expected_matrix =  [
                [seqs[0],  seqs[1],  seqs[3]],
                [seqs[4]],
                [seqs[6], seqs[7]]
                ]
        expected_list = [  seqs[0], seqs[1], seqs[3], seqs[4], seqs[6], seqs[7] ]
        for seq in expected_list:
            seq.pick()
        expected_depths = np.array([3, 4, 6, 4, 3, 2, 2, 1])
        self.matrix.min_depth = 3
        self.matrix.minimize_depths()
        actual_matrix = self.matrix.seq_matrix
        for row in actual_matrix:
            for seq in row:
                if not seq.picked:
                    row.remove(seq)

        self.assertEquals(expected_matrix, actual_matrix)
        assert_equal(expected_depths, self.matrix.depth_array)
        self.assertEquals([seq.string for seq in expected_list], sub.flatten_and_filter_matrix(self.matrix.seq_matrix)) 




    def check_files(self, mock_stdout, expected_file):
        samfile = os.path.join(THISD, expected_file)
        expected = open(samfile, 'r').read().strip()
        result = str(mock_stdout.getvalue().strip().decode('UTF-8'));
        if expected_file == 'simplein.sam':
            with open('tests/result.sam', 'w') as out: out.write(result)
        return expected, result


    @mock.patch('subsample_mindepth.random.choice', side_effect=not_random_subsample)
    @mock.patch("subsamplebam.parse_args", side_effect=mock_args)
    @mock.patch('subsamplebam.sys.stdout', new_callable=BytesIO)
    def test_main(self, mock_stdout, func, func2):
        sub.main() 
        self.assertEquals(*self.check_files(mock_stdout, 'test40.sam'))



    ''' The order of these mocks is the order they appear as arguments to the test method. '''
#    @mock.patch('subsample_mindepth.random.sample', side_effect=not_random_subsample)
#    @mock.patch("subsamplebam.parse_args", side_effect=more_random_mock_args)
#    @mock.patch('subsamplebam.sys.stdout', new_callable=BytesIO)
#    def test_main_more_random(self, mock_stdout, func, func2):
#        #Expected will equal the input file becasue all reads are subsampled in this case
#        sub.main()
#        self.maxDiff = None
#        expected, result = self.check_files(mock_stdout, 'simplein.sam')
#        self.assertEquals(expected, result)


    @mock.patch('subsample_mindepth.random.choice', side_effect=not_random_subsample)
    def test_minimize_depths_complex_more_random(self, func):
        #TODO: Refactor this so as not to duplicate code with non-random ecoli test
        self.complexSetUp()
        seqs = map(sub.parse_alignment, self.raw_reads) 
        expected_matrix =  [
                [seqs[0],  seqs[1],  seqs[2], seqs[3]],
                [seqs[4], seqs[5]],
                [seqs[6], seqs[7]]
                ]
        expected_list = seqs
        for seq in expected_list:
            seq.pick()
        expected_depths = np.array([4, 6, 7, 4, 3, 2, 2, 1])
        self.matrix.min_depth = 3
        self.matrix.more_random = True
        self.matrix.minimize_depths()
        actual_matrix = self.matrix.seq_matrix
        for row in actual_matrix:
            for seq in row:
                if not seq.picked:
                    row.remove(seq) 
        self.assertEquals(expected_matrix, actual_matrix)
        assert_equal(expected_depths, self.matrix.depth_array)
        self.assertEquals([seq.string for seq in expected_list], sub.flatten_and_filter_matrix(self.matrix.seq_matrix))