def _test_create_control(self): intervals = [obg.DirectedInterval(0, 20, [3]), obg.DirectedInterval(0, 10, [5]), obg.DirectedInterval(0, 21, [13])] mapped_intervals = self.snarl_map.map_interval_collection( intervals) linear_pileup = LinearPileup.create_from_starts_and_ends( mapped_intervals.starts, mapped_intervals.ends) graph_pileup = linear_pileup.to_sparse_pileup(self.snarl_map) true_sparse_pileup = OldSparsePileup(graph) true_data = {3: ValuedIndexes([], [], 2, 20), 12: ValuedIndexes([], [], 2, 20), 13: ValuedIndexes([], [], 2, 21), 5: ValuedIndexes([], [], 2, 10)} true_sparse_pileup.data = OldSparsePileupData([(key, val) for key, val in true_data.items()], graph=graph) #print(true_sparse_pileup.data) print("Graph pileup") print(graph_pileup) print("True") print(true_sparse_pileup) self.assertTrue(graph_pileup.equals_old_sparse_pileup(true_sparse_pileup))
def setUp(self): self._graph = sorted_wierd_graph(2, 100) self._pileup = DensePileup(self._graph) self._intervals = [ obg.DirectedInterval(0, 10, [1]), obg.DirectedInterval(90, 10, [1, 2]), obg.DirectedInterval(90, 10, [-3, -1]) ]
def get_differential(motif_location, sub_graphs, node_ids_list): peak_id = motif_location._id motif_rps = motif_location.location.region_paths graph = sub_graphs[peak_id][()] node_ids = list(node_ids_list[peak_id]) motif_idxs = [node_ids.index(rp) for rp in motif_rps] node_scores = np.asarray(np.min(graph, axis=1).todense()) node_scores = node_scores.flatten() valid_nodes = node_scores != 0 dists, predecessors = shortest_path(graph, return_predecessors=True, method="BF") alt, altD = find_alt(dists, motif_idxs, valid_nodes) if alt is None: return None mainD = dists[motif_idxs[0], motif_idxs[-1]] dist_diff = altD-mainD alt_node_score = abs(np.min(graph[alt])) start_path = backtrace(predecessors, motif_idxs[0], alt) end_path = backtrace(predecessors, alt, motif_idxs[-1]) path = start_path[:-1] + end_path node_idxs = node_ids_list[peak_id][path] alt_interval = obg.DirectedInterval( motif_location.location.start_position.offset, motif_location.location.end_position.offset, list(node_idxs)) obg_graph = motif_location.location.graph alt_interval.graph = obg_graph length_diff = motif_location.location.length()-alt_interval.length() alt_length = obg_graph.node_size(node_ids[alt]) base_score = alt_node_score/alt_length main_score = base_score + dist_diff return DiffExpression(peak_id, motif_location.location, alt_interval, main_score, base_score)
def test_internal_filled_interval(self): interval = obg.DirectedInterval(2, 8, [3]) areas = BinaryContinousAreas(graph) areas.filled_interval(interval) areas.sanitize() true_areas = BinaryContinousAreas(graph) true_areas.internal_intervals = {3: [2, 8]} self.assertEqual(areas, true_areas)
def test_filled_interval(self): interval = obg.DirectedInterval(4, 4, [2, 3, 4]) areas = BinaryContinousAreas(graph) areas.filled_interval(interval) areas.sanitize() true_areas = BinaryContinousAreas(graph) true_areas.full_areas = {3: 1} true_areas.starts = {-2: 6, 4: 4} self.assertEqual(areas, true_areas)
def setUp(self): self.snarl_map = LinearSnarlMap.from_snarl_graph(snarl_graph2, graph) self.graph_positions = [obg.Position(5, 4), obg.Position(3, 4), obg.Position(12, 4), obg.Position(13, 4)] self.linear_positions = [4, 31/20*4, 10+21/20*4, 14] self.linear_positions = [p for p in self.linear_positions] self.graph_interval = obg.DirectedInterval(self.graph_positions[0], self.graph_positions[2])
def _generate_interval_from_start_pos(self, start_pos): cur_node = start_pos.region_path_id start_offset = start_pos.offset l = self._interval_length - ( self._graph.node_size(cur_node)-start_offset) rps = [cur_node] while l > 0: cur_node = random.choice(self._graph.adj_list[cur_node]) rps.append(cur_node) l -= self._graph.node_size(cur_node) end_offset = self._graph.node_size(cur_node) + l return obg.DirectedInterval(start_pos.offset, end_offset, rps)
def test_long_fragments(self): intervals = [ obg.DirectedInterval(80, 90, [1]), obg.DirectedInterval(90, 10, [1, 2]), obg.DirectedInterval(90, 10, [-4, -2]), obg.DirectedInterval(90, 10, [-3, -1]) ] pileup = main(intervals, self._graph, 30) pileup = pileup.data._values true_array = np.zeros_like(pileup) true_array[60:80] = 1 true_array[80:90] = 2 true_array[90:100] = 3 true_array[100:120] = 2 true_array[120:140] = 1 true_array[160:200] = 1 true_array[200:210] = 2 true_array[210:220] = 1 true_array[300:310] = 1 self.assertTrue(np.all(pileup == true_array))
def get_interval(self, start, end): is_reversed = start > end if is_reversed: start, end = end, start assert end <= self.length first_idx = np.where(self._distances > start)[0][0]-1 last_idx = np.where(self._distances < end)[0][-1] rps = [self._nodes[i] for i in range(first_idx, last_idx+1)] if is_reversed: rps = [-rp for rp in rps[::-1]] start_offset = self._distances[last_idx+1]-end end_offset = self._distances[first_idx+1]-start else: start_offset = start-self._distances[first_idx] end_offset = end-self._distances[last_idx] return obg.DirectedInterval(start_offset, end_offset, rps)
def to_obg_with_reversals(self, ob_graph=False): if len(self.mappings) == 0: return False start_offset = self.mappings[0].get_start_offset() end_offset = self.mappings[-1].get_end_offset() obg_blocks = [m.node_id() for m in self.mappings] interval = offsetbasedgraph.DirectedInterval(start_offset, end_offset, obg_blocks, ob_graph or None) if ob_graph: if not interval.length() == self.length(): raise IntervalNotInGraphException( "Interval %s is not valid interval in graph" % interval) return interval
def expand_hole_sides_to_right(self, positions): logging.info("Number of holes to expand: %d" % len(positions)) extender = Extender(self._graph, self.hole_size) i = 0 for position in positions: if i % 500 == 0: logging.info("Expanding hole %d/%d" % (i, len(positions))) i += 1 node = position[0] offset = position[1] interval = obg.DirectedInterval(int(offset), int(offset) + 1, [node]) areas = extender.extend_interval(interval) self.pileup.set_area_to_value(areas, True) self.pileup.threshold(0.5)
self.assertTrue(np.all(pileup == true_array)) if False or __name__ == "__main__": node_size = 32 n_nodes = 100000 graph = swierd(n_nodes, node_size) intervals = [] for node in graph.get_sorted_node_ids(): if node % 20 == 0: next_nodes = graph.adj_list[node] if not next_nodes: continue next_node = next_nodes[0] for i in range(10): intervals.append( obg.DirectedInterval(i, i + 4, [node, next_node])) for node in graph.get_sorted_node_ids(reverse=True): node = -node if node % 20 == 0: next_nodes = graph.reverse_adj_list[node] if not next_nodes: continue next_node = next_nodes[0] for i in range(10): intervals.append( obg.DirectedInterval(i, i + 4, [node, next_node])) main(intervals, graph, 110)
paths = [Path("path1", mappings[:1])] intervals = [offsetbasedgraph.Interval(10, 15, [0])] nodes = [ Node("_", 0, 10), Node("_", 1, 20), Node("_", 2, 30), Node("_", 3, 40) ] edges = [Edge(0, 1), Edge(0, 2), Edge(1, 3), Edge(2, 3)] vg_graphs = [Graph(nodes, edges, [])] ob_graphs = [graph.get_offset_based_graph() for graph in vg_graphs] pileup_intervals = [ offsetbasedgraph.Interval(5, 5, [0, 1]), offsetbasedgraph.Interval(0, 10, [1]), offsetbasedgraph.Interval(10, 15, [1]) ] directed_pileup_intervals = [ offsetbasedgraph.DirectedInterval(5, 5, [0, 1]), offsetbasedgraph.DirectedInterval(10, 20, [-1], -1), offsetbasedgraph.DirectedInterval(10, 15, [1]) ] for interval in pileup_intervals + directed_pileup_intervals: interval.graph = ob_graphs[0]
def setUp(self): nodes = {i + 1: obg.Block(10) for i in range(5)} edges = {1: [2, 3], 2: [4], 3: [-4], 4: [5], -4: [5]} self.graph = obg.GraphWithReversals(nodes, edges) data = { node_id: ValuedIndexes( np.array([2 * i for i in range(1, 5)]), np.array([2 * i + 10 * node_id for i in range(1, 5)]), node_id * 10, 10) for node_id in nodes } self.pileup = SparsePileup(self.graph) self.pileup.data = data self.pileup = DensePileup.create_from_old_sparsepileup(self.pileup) flat_data = { node_id: ValuedIndexes(np.array([], dtype="int"), np.array([]), node_id * 10, 10) for node_id in nodes } self.flat_pileup = SparsePileup(self.graph) self.flat_pileup.data = flat_data self.flat_pileup = DensePileup.create_from_old_sparsepileup( self.flat_pileup) self.peak = BinaryContinousAreas(self.graph) self.peak.add_full(2) self.peak.add_full(4) self.peak.add_full(3) self.peak.add_start(5, 5) self.peak.add_start(-1, 5) indexes = np.array([2, 4, 6, 8]) values = np.array([2, 4, 6, 8]) self.scores = { 2: ValuedIndexes(indexes, values + 20, 20, 10), -2: ValuedIndexes(indexes, values + 20, 20, 10), 3: ValuedIndexes(indexes, values + 30, 30, 10), -3: ValuedIndexes(indexes, values + 30, 30, 10), 4: ValuedIndexes(indexes, values + 40, 40, 10), -4: ValuedIndexes(indexes, values + 40, 40, 10), 5: ValuedIndexes(np.array([2, 4]), np.array([52, 54]), 50, 5), -1: ValuedIndexes(np.array([1, 3]), np.array([16, 18]), 14, 5) } self.flat_scores = { 2: ValuedIndexes(np.array([], dtype="int"), np.array([]), 20, 10), 3: ValuedIndexes(np.array([], dtype="int"), np.array([]), 30, 10), 4: ValuedIndexes(np.array([], dtype="int"), np.array([]), 40, 10), -2: ValuedIndexes(np.array([], dtype="int"), np.array([]), 20, 10), -3: ValuedIndexes(np.array([], dtype="int"), np.array([]), 30, 10), -4: ValuedIndexes(np.array([], dtype="int"), np.array([]), 40, 10), 5: ValuedIndexes(np.array([], dtype="int"), np.array([]), 50, 5), -1: ValuedIndexes(np.array([], dtype="int"), np.array([]), 10, 5) } self.peak2 = BinaryContinousAreas(self.graph) self.peak2.add_full(2) self.peak2.add_start(-3, 7) self.peak2.add_full(4) self.peak2.add_start(5, 5) self.scores2 = { 2: ValuedIndexes(indexes, values + 20, 20, 10), -3: ValuedIndexes(np.array([1, 3, 5]), np.array([34, 36, 38]), 32, 7), 4: ValuedIndexes(indexes, values + 40, 40, 10), 5: ValuedIndexes(np.array([2, 4]), np.array([52, 54]), 50, 5), } self.scored_peak = ScoredPeak(self.peak, self.scores) self.flat_scored_peak = ScoredPeak(self.peak, self.flat_scores) self.scored_peak2 = ScoredPeak(self.peak2, self.scores2) self.max_path = obg.DirectedInterval(5, 5, [1, 3, -4, 5], graph=self.graph) self.max_path2 = obg.DirectedInterval(3, 5, [3, -4, 5], graph=self.graph)