def test_create_from_nongraphpeakcollection(self): graph = Graph({ 1: Block(10), 2: Block(10), 3: Block(10) }, { 1: [2], 2: [3] }) graph.convert_to_numpy_backend() linear_path = Interval(0, 10, [1, 2, 3], graph) linear_path = linear_path.to_numpy_indexed_interval() nongraph_peaks = NonGraphPeakCollection([ NonGraphPeak("chr1", 3, 10, 5), NonGraphPeak("chr1", 13, 15, 7), ]) peaks = PeakCollection.create_from_nongraph_peak_collection( graph, nongraph_peaks, linear_path, None) self.assertEqual(peaks.intervals[0], Interval(3, 10, [1])) self.assertEqual(peaks.intervals[1], Interval(3, 5, [2])) peaks = PeakCollection.create_from_nongraph_peak_collection( graph, nongraph_peaks, linear_path, LinearRegion("chr1", 3, 20)) self.assertEqual(peaks.intervals[0], Interval(0, 7, [1])) self.assertEqual(peaks.intervals[1], Interval(0, 2, [2]))
def test_multiple_peaks3(self): self.peaks = [ DirectedInterval(2, 2, [1, 3, 4], self.graph), DirectedInterval(0, 3, [7, 9], self.graph) ] self.do_asserts()
def test_multiple_peaks(self): self.peaks = [ DirectedInterval(3, 8, [1], self.graph), DirectedInterval(7, 12, [4], self.graph), DirectedInterval(14, 4, [3, 4], self.graph), ] self.do_asserts()
def test_multiple_peaks2(self): self.peaks = [ DirectedInterval(2, 2, [1, 3, 4], self.graph), DirectedInterval(2, 2, [6, 10, 11], self.graph), ] self.do_asserts()
def test_extend_cyclic(self): graph = get_small_cyclic_graph() interval = DirectedInterval(70, 90, [1], graph=graph) extender = Extender(graph, 40) areas = extender.extend_interval(interval) true_areas = BinaryContinousAreas(graph) true_areas.add_start(1, 10) true_areas.add_start(-1, 30) self.assertEqual(areas, true_areas)
def test_extend_large_cyclic(self): logging.debug("CYCLIC") graph = get_large_cyclic_graph() interval = DirectedInterval(90, 10, [1, 2], graph=graph) extender = Extender(graph, 40) areas = extender.extend_interval(interval) true_areas = BinaryContinousAreas(graph) true_areas.add_start(1, 10) true_areas.add_start(-1, 10) true_areas.add_start(2, 20) self.assertEqual(areas, true_areas)
def test_convert_to_approx_linear_peaks(self): graph = Graph({i: Block(3) for i in range(1, 10)}, { 1: [2], 2: [3], 3: [4], 4: [5], 5: [6], 6: [7, 8], 7: [9], 9: [9] }) graph.convert_to_numpy_backend() linear_interval = Interval(0, 3, [2, 4, 8, 9], graph) linear_interval = linear_interval.to_numpy_indexed_interval() peaks = PeakCollection([Peak(2, 2, [2, 3, 4]), Peak(1, 1, [3, 4, 5])]) linear_peaks = peaks.to_approx_linear_peaks(linear_interval, "chr4") linear_peaks = linear_peaks.peaks print(linear_peaks) self.assertEqual(linear_peaks[0], NonGraphPeak("chr4", 2, 5)) self.assertEqual(linear_peaks[1], NonGraphPeak("chr4", 3, 3))
def _get_graph_interval(self, tmp_start, tmp_end, direction): start = tmp_start end = tmp_end if direction == -1: start = -tmp_end end = -tmp_start start_rp = start // self.node_size end_rp = (end + 1) // self.node_size region_paths = list(range(start_rp, end_rp)) start_pos = Position(start_rp, start % self.node_size) end_pos = Position(end_rp, (end % self.node_size) + 1) return DirectedInterval(start_pos, end_pos, region_paths, direction=direction)
def neg_linear_to_graph_interval(self, lin_interval): start_rp = ((lin_interval.end - 1) // self.node_size + 1) end_rp = (lin_interval.start // self.node_size + 1) start_offset = start_rp * self.node_size - lin_interval.end end_offset = end_rp * self.node_size - lin_interval.start # start_offset = (-lin_interval.end) % self.node_size # end_offset = (-lin_interval.start) % self.node_size # start_rp = (lin_interval.end) // self.node_size + 1 # end_rp = (-lin_interval.start) // self.node_size + 1 rps = list(range(start_rp * -1, end_rp * -1 + 1)) interval = DirectedInterval(start_offset, end_offset, rps, graph=self.graph) return interval
def linear_to_graph_interval(self, lin_interval, is_control=None): if lin_interval.direction == -1: return self.neg_linear_to_graph_interval(lin_interval) start = lin_interval.start end = lin_interval.end start_rp = start // self.node_size + 1 end_rp = (end - 1) // self.node_size + 1 start_pos = Position(start_rp, start % self.node_size) end_pos = Position(end_rp, ((end - 1) % self.node_size) + 1) region_paths = list(range(start_rp, end_rp + 1)) interval = DirectedInterval(start_pos, end_pos, region_paths, direction=lin_interval.direction, graph=self.graph) return interval
def _create_reads(self, chrom_number, chrom, graph): i = chrom_number sample_reads = [] control_reads = [] peaks = [DirectedInterval(7, 2, [1 + 3 * i, 2 + 3 * i], graph)] self.peaks.append(peaks) for peak in peaks: for i in range(0, 10): left_sub = peak.get_subinterval(0, self.read_length) sample_reads.append(left_sub) control_reads.append(left_sub) right_sub = peak.get_subinterval( self.fragment_length - self.read_length, self.fragment_length) right_sub_reverse = right_sub.get_reverse() sample_reads.append(right_sub_reverse) control_reads.append(right_sub_reverse) self.sample_reads.append(Intervals(sample_reads)) self.control_reads.append(Intervals(control_reads))
def assertPeakSetsEqual(self, linear_peaks_file, graph_peaks_file): linear_path = DirectedInterval(0, self.node_size, list(range(1, self.n_nodes + 1)), graph=self.graph) comparer = PeaksComparer.create_from_graph_peaks_and_linear_peaks( linear_peaks_file, graph_peaks_file, self.graph, linear_path, graph_region=None) # for i, j in zip(sorted(comparer.peaks1.intervals, key=lambda x: x.region_paths[0]), # sorted(comparer.peaks2.intervals, key=lambda x: x.region_paths[0])): # print(i, j) assert len(comparer.peaks1.intervals) == len(comparer.peaks2.intervals) matches = comparer.get_peaks_at_same_position() # for m in matches: # print(m) assert len(matches) == len(comparer.peaks1.intervals)
def test_peak_crossing_blocks(self): self.peaks = [ DirectedInterval(13, 3, [1, 2], self.graph), DirectedInterval(13, 3, [2, 4], self.graph), ] self.do_asserts()
def test_single_peak(self): self.peaks = [ DirectedInterval(3, 8, [2], self.graph), ] self.do_asserts()
def test_single_peak(self): self.peaks = [ DirectedInterval(1, 1, [1, 2, 7], self.graph), ] self.do_asserts()
if pytest.__version__ < "3.0.0": pytest.skip() # from graph_peak_caller.extender import Extender, Areas # from graph_peak_caller.areas import BinaryContinousAreas from offsetbasedgraph import Block, GraphWithReversals,\ DirectedInterval, Position from cyclic_graph import get_small_cyclic_graph, get_large_cyclic_graph logging.getLogger("extender").setLevel("DEBUG") Graph = GraphWithReversals nodes = {i: Block(20) for i in range(1, 4)} tmp_edges = {1: [2, -2], 2: [3], -2: [3]} graph = Graph(nodes, tmp_edges) pos_interval = DirectedInterval(8, 18, [2]) neg_interval = DirectedInterval(2, 12, [-2]) neg_spanning_interval = DirectedInterval(12, 2, [-3, -2]) pos_spanning_interval = DirectedInterval(18, 8, [2, 3]) @pytest.mark.skip("Legacy") class TestExtender(unittest.TestCase): def setUp(self): self.extender = Extender(graph, 20) def test_extend(self): areas = self.extender.extend_interval(pos_interval) true_areas = BinaryContinousAreas(graph) true_areas.add_start(-2, 12) true_areas.add_start(3, 8)