def test_finds_correct_max_path_among_many_paths(self):
        graph = GraphWithReversals(
            {
                1: Block(10),
                2: Block(10),
                3: Block(10),
                4: Block(10),
                5: Block(10)
            }, {
                1: [2, 3, 4],
                2: [5],
                4: [5],
                3: [5]
            })

        pileup = SparsePileup(graph)
        pileup.data = {
            1: ValuedIndexes([], [], 2, 10),
            # Higher qval, but two holes with low
            2: ValuedIndexes([1, 2, 7, 8], [0, 2.001, 0, 2.001], 2, 10),
            3: ValuedIndexes([], [], 1.5, 10),
            4: ValuedIndexes([], [], 2, 10),
            5: ValuedIndexes([], [], 2, 10)
        }
        self._assert_finds_max_paths([Interval(0, 10, [1, 4, 5])], graph,
                                     pileup)
 def __test_many_possible_holes(self):
     pileup = SparsePileup(self.multi_start_end_graph)
     pileup.data = {
         10: ValuedIndexes([], [], 2, 10),
         2: ValuedIndexes([], [], 2.2, 10),
         3: ValuedIndexes([1, 9], [2, 0], 0, 10),
         4: ValuedIndexes([], [], 2, 10),
         5: ValuedIndexes([1], [3], 0, 10),
     }
Esempio n. 3
0
 def setUp(self):
     self.graph = obg.GraphWithReversals(
         {i: obg.Block(10)
          for i in range(1, 5)}, {
              1: [2],
              2: [3],
              3: [4]
          })
     self.pileup = SparsePileup(self.graph)
 def _test_with_reversal_and_hole(self):
     pileup = SparsePileup(self.graph_with_reversal)
     pileup.data = {
         1: ValuedIndexes([], [], 2, 10),
         2: ValuedIndexes([9], [0], 2, 10),
         3: ValuedIndexes([1], [3], 0, 10),
     }
     self._assert_finds_max_paths([Interval(0, 10, [-3, -2])],
                                  self.graph_with_reversal, pileup)
    def test_find_max_path_on_split_graph_with_hole_fill(self):

        pileup = SparsePileup(self.split_graph)
        pileup.data = {
            1: ValuedIndexes([3], [2], 0, 10),
            2: ValuedIndexes([], [], 2.1, 10),
            3: ValuedIndexes([], [], 2, 10),
            4: ValuedIndexes([1, 3], [0, 3], 2, 10)
        }
        self._assert_finds_max_paths([Interval(3, 10, [1, 2, 4])],
                                     self.split_graph, pileup)
    def test_multiple_start_and_end_nodes2(self):
        pileup = SparsePileup(self.multi_start_end_graph)
        pileup.data = {
            1: ValuedIndexes([], [], 2, 10),
            2: ValuedIndexes([], [], 2.2, 10),
            3: ValuedIndexes([1, 9], [2, 0], 0, 10),
            4: ValuedIndexes([], [], 2, 10),
            5: ValuedIndexes([1], [3], 0, 10),
        }

        self._assert_finds_max_paths([Interval(0, 10, [2, 3, 5])],
                                     self.multi_start_end_graph, pileup)
    def test_find_max_path_on_split_graph(self):

        pileup = SparsePileup(self.split_graph)
        pileup.data = {
            1: ValuedIndexes([], [], 2, 10),
            2: ValuedIndexes([], [], 3, 10),
            3: ValuedIndexes([], [], 2, 10),
            4: ValuedIndexes([1, 4], [0, 3], 2, 10)
        }
        self._assert_finds_max_paths(
            [Interval(0, 1, [1, 2, 4]),
             Interval(4, 10, [4])], self.split_graph, pileup)
Esempio n. 8
0
    def test_single_read(self):
        fragment_length = 3
        reads = [Interval(0, 3, [2])]
        extension_sizes = [8]
        control = create_control("test_linear_map.tmp",
                                 reads,
                                 extension_sizes,
                                 fragment_length,
                                 ob_graph=self.graph)
        expected_bakground = len(reads) * fragment_length / self.linear_length
        value_in_extension = 1 * fragment_length / (extension_sizes[0])

        correct_pileup = OldSparsePileup.from_base_value(
            self.graph, expected_bakground)
        for rp in [2, 3, 1]:
            correct_pileup.data[rp] = ValuedIndexes([], [], value_in_extension,
                                                    3)

        for rp in [7, 8, 4, 5]:
            correct_pileup.data[rp] = ValuedIndexes([1], [expected_bakground],
                                                    value_in_extension, 3)

        for rp in [11]:
            correct_pileup.data[rp] = ValuedIndexes([2], [value_in_extension],
                                                    expected_bakground, 3)

        self.assertTrue(control.equals_old_sparse_pileup(correct_pileup))
Esempio n. 9
0
    def test_from_intervals2(self):
        graph = obg.GraphWithReversals(
            {
                1: obg.Block(10),
                2: obg.Block(10),
                3: obg.Block(10)
            }, {
                1: [2],
                2: [3]
            })
        intervals = [
            obg.Interval(1, 5, [1]),
            obg.Interval(3, 7, [1]),
            obg.Interval(5, 3, [1, 2]),
            obg.Interval(5, 6, [2]),
            obg.Interval(8, 8, [1, 2, 3])
        ]

        pileup = SparsePileup.from_intervals(graph, intervals)
        data = pileup.data
        self.assertEqual(data[1].start_value, False)
        self.assertEqual(data[2].start_value, 2)
        self.assertEqual(data[3].start_value, 1)

        self.assertTrue(np.all([1, 3, 5, 7, 8] == data[1].indexes))
        #print("data 1 values")
        #print(data[2].values)
        self.assertTrue(np.all([1, 2, 2, 1, 2] == data[1].values))

        self.assertTrue(np.all([3, 5, 6] == data[2].indexes))
        self.assertTrue(np.all([1, 2, 1] == data[2].values))

        self.assertTrue(np.all([8] == data[3].indexes))
        self.assertTrue(np.all([0] == data[3].values))
Esempio n. 10
0
 def test_call_peaks(self):
     print("###############################################")
     print(self.graph.adj_list[222])
     print(self.graph.reverse_adj_list[222])
     self.assertPileupFilesEqual("control_track.bdg",
                                 "macstest_control_lambda.bdg")
     self.assertPileupFilesEqual("sample_track.bdg",
                                 "macstest_treat_pileup.bdg")
     self.caller._control_pileup = SparsePileup.from_bed_graph(
         self.graph, "control_track.bdg")
     self.caller._sample_pileup = SparsePileup.from_bed_graph(
         self.graph, "sample_track.bdg")
     self.caller.get_score()
     self._get_scores("qpois")
     self.assertPileupFilesEqual("q_values.bdg", "lin_scores.bdg")
     self._call_peaks()
     self.caller.call_peaks()
     self.assertEqualBedFiles("final_peaks.bed", "lin_peaks.bed")
Esempio n. 11
0
def analyse_pileups_on_peaks(ob_graph, pileups_file_names,
                             peak_intervals_file_name):
    print("Analysing peaks")
    pileups = {
        name: SparsePileup.from_bed_graph(ob_graph, pileup)
        for name, pileup in pileups_file_names.items()
    }
    peaks = IntervalCollection.from_file(peak_intervals_file_name,
                                         text_file=True)

    for peak in peaks:
        print()
        print("Peak %s" % peak)
        rp = peak.region_paths[0]
        for name, pileup in pileups.items():
            pileup_sum = sum(pileup.data[rp].sum() for rp in peak.region_paths)
            print("Pileup %s: %d" % (name, pileup_sum))
Esempio n. 12
0
class TestSparsePileupSetIntervals(unittest.TestCase):
    def setUp(self):
        self.graph = obg.GraphWithReversals(
            {i: obg.Block(10)
             for i in range(1, 5)}, {
                 1: [2],
                 2: [3],
                 3: [4]
             })
        self.pileup = SparsePileup(self.graph)

    def test_simple(self):
        intervals = [obg.Interval(4, 6, [1]), obg.Interval(6, 8, [1])]
        values = [1, 2]

        self.pileup.set_sorted_interval_values(intervals, values)
        correct = ValuedIndexes([4, 6, 8], [1, 2, 0], 0, 10)
        print(correct)
        print(self.pileup.data[1])
        self.assertEqual(self.pileup.data[1], correct)

    def test_multiple_rps(self):
        intervals = [obg.Interval(4, 6, [1]), obg.Interval(6, 4, [1, 2])]
        values = [1, 2]
        self.pileup.set_sorted_interval_values(intervals, values)
        correct = {}
        correct[1] = ValuedIndexes([4, 6], [1, 2], 0, 10)
        correct[2] = ValuedIndexes([4], [0], 2, 10)
        self.assertEqual(self.pileup.data[1], correct[1])
        self.assertEqual(self.pileup.data[2], correct[2])

    def test_multiple_rps2(self):
        intervals = [obg.Interval(4, 6, [1]), obg.Interval(6, 10, [1, 2, 3])]
        values = [1, 2]
        self.pileup.set_sorted_interval_values(intervals, values)
        correct = {}
        correct[1] = ValuedIndexes([4, 6], [1, 2], 0, 10)
        correct[2] = ValuedIndexes([4], [0], 2, 10)
        correct[3] = ValuedIndexes([], [], 2, 10)
        self.assertEqual(self.pileup.data[1], correct[1])
        self.assertEqual(self.pileup.data[2], correct[2])
        self.assertEqual(self.pileup.data[3], correct[3])
 def test_finds_internal_peak_with_internal_hole(self):
     pileup = SparsePileup(self.single_block_graph)
     pileup.data = {1: ValuedIndexes([5, 8, 9, 15], [2, 0, 2, 0], 0, 20)}
     self._assert_finds_max_paths([Interval(5, 15, [1])],
                                  self.single_block_graph, pileup)
 def test_removes_too_small_peak(self):
     pileup = SparsePileup(self.single_block_graph)
     pileup.data = {1: ValuedIndexes([5, 9], [2, 0], 0, 20)}
     self._assert_finds_max_paths([], self.single_block_graph, pileup)
    def setUp(self):
        blocks = {i: Block(10) for i in range(1, 5)}
        edges = {i: [i + 1] for i in range(1, 4)}
        self.linear_graph = GraphWithReversals(blocks, edges)
        self.one_peak_q_values = SparsePileup(self.linear_graph)
        self.one_peak_q_values.data = \
            {
                1: ValuedIndexes([5], [2], 0, 10),
                2: ValuedIndexes([3], [0], 2, 10)
            }

        self.one_peak_with_hole = SparsePileup(self.linear_graph)
        self.one_peak_with_hole.data = \
            {
                1: ValuedIndexes([5, 8], [2, 0], 0, 10),
                2: ValuedIndexes([3], [0], 2, 10)
            }

        self.one_peak_with_big_hole = SparsePileup(self.linear_graph)
        self.one_peak_with_big_hole.data = \
            {
                1: ValuedIndexes([5, 7], [2, 0], 0, 10),
                2: ValuedIndexes([3], [0], 2, 10)
            }

        self.split_graph = GraphWithReversals(
            {i: Block(10)
             for i in range(1, 5)}, {
                 1: [2, 3],
                 2: [4],
                 3: [4]
             })

        self.split_graph_with_path_around = \
            GraphWithReversals(
                {i: Block(10) for i in range(1, 8)},
                {}
            )

        self.graph_with_reversal = \
            GraphWithReversals(
                {i: Block(10) for i in range(1, 4)},
                {
                    1: [2],
                    -3: [-2]
                }
            )

        self.single_block_graph = \
            GraphWithReversals({1: Block(20)}, {})

        self.multi_start_end_graph = \
                GraphWithReversals({i: Block(10) for i in range(1, 6)},
                           {
                               1: [3],
                               2: [3],
                               3: [4, 5]
                           })

        self.junction_graph = GraphWithReversals(
            {i: Block(5)
             for i in range(10, 20)}, {
                 10: [15],
                 11: [15],
                 12: [15],
                 13: [15],
                 14: [15],
                 15: [16, 17, 18, 19]
             })

        self.fragment_length = 6
        self.read_length = 2