def setUp(self): self.linear_graph = Graph({i: Block(5) for i in range(1, 4)}, {i: [i + 1] for i in range(1, 3)}) self.scores = DensePileup.from_intervals( self.linear_graph, [Interval(0, 5, [i]) for i in range(1, 4)]) self.graph = Graph({i: Block(5) for i in range(1, 4)}, { 1: [3], 2: [3], 3: [4] })
def test_find_max_path_through_subgraph_multiple_paths(self): graph = Graph({ 1: Block(10), 2: Block(10), 3: Block(10), 4: Block(10) }, { 1: [2, 3], 2: [4], 3: [4] }) peak = ConnectedAreas(graph, { 2: [0, 10], 3: [0, 10], 1: [5, 10], 4: [0, 3] }) binary_peak = BinaryContinousAreas.from_old_areas(peak) qvalues = DensePileup.from_intervals( graph, [ Interval(7, 2, [1, 3, 4]) # Giving higher qvalue # through this path ]) print(qvalues) scored_peak = ScoredPeak.from_peak_and_pileup(binary_peak, qvalues) print(scored_peak) max_path = scored_peak.get_max_path() self.assertEqual(max_path, Interval(5, 3, [1, 3, 4]))
def simple_test(): graph = Graph({ 1: Block(10), 2: Block(1), 3: Block(1), 4: Block(10) }, { 1: [2, 3], 2: [4], 3: [4] }) graph.convert_to_numpy_backend() sequence_graph = SequenceGraph.create_empty_from_ob_graph(graph) sequence_graph.set_sequence(1, "GGGTTTATAC") sequence_graph.set_sequence(2, "A") sequence_graph.set_sequence(3, "C") sequence_graph.set_sequence(4, "GTACATTGTA") linear_ref = Interval(0, 10, [1, 2, 3], graph) linear_ref = linear_ref.to_numpy_indexed_interval() critical_nodes = set([4]) finder = MinimizerFinder(graph, sequence_graph, critical_nodes, linear_ref, k=3, w=3) minimizers = finder.find_minimizers() assert minimizers.has_minimizer(2, 0) assert minimizers.has_minimizer(3, 0) assert minimizers.has_minimizer(4, 4)
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_three_nodes_in(self): graph = Graph({i: Block(5) for i in range(1, 5)}, { 1: [4], 2: [4], 3: [4] }) intervals = [ Interval(2, 5, [1]), Interval(2, 5, [2]), Interval(2, 5, [3]), Interval(0, 3, [4]) ] pileup = DensePileup.from_intervals(graph, intervals) subgraphs = SubgraphCollectionPartiallyOrderedGraph.create_from_pileup( graph, pileup) print(subgraphs) correct1 = BinaryContinousAreas(graph) correct1.add_start(-1, 3) correct1.add_start(-2, 3) correct1.add_start(-3, 3) correct1.add_start(4, 3) self.assertTrue(correct1 in subgraphs)
def test_find_max_path_on_start_and_end_node(self): graph = Graph({ 1: Block(10), 2: Block(10), 3: Block(10), 4: Block(10) }, { 1: [2, 3], 2: [4], 3: [4] }) peak = ConnectedAreas(graph, { 2: [0, 10], 4: [0, 10], }) binary_peak = BinaryContinousAreas.from_old_areas(peak) qvalues = DensePileup.from_intervals(graph, [Interval(7, 2, [1, 2, 4])]) scored_peak = ScoredPeak.from_peak_and_pileup(binary_peak, qvalues) max_path = scored_peak.get_max_path() self.assertEqual(max_path, Interval(0, 10, [2, 4]))
def test_simple3(self): graph = Graph({i: Block(5) for i in range(1, 6)}, { 1: [3], 2: [3], 3: [4, 5] }) scores = DensePileup.from_intervals( graph, [Interval(0, 5, [i]) for i in range(1, 6)]) intervals = [ Interval(0, 5, [1]), Interval(0, 5, [3]), Interval(0, 5, [4]), Interval(0, 3, [5]) ] pileup = DensePileup.from_intervals(graph, intervals) subgraphs = SubgraphCollectionPartiallyOrderedGraph.create_from_pileup( graph, pileup) scored_peaks = (ScoredPeak.from_peak_and_pileup(peak, scores) for peak in subgraphs) max_paths = [peak.get_max_path() for peak in scored_peaks] self.assertTrue( Interval(0, 5, [1, 3, 4]) in max_paths or Interval(0, 3, [1, 3, 5]) in max_paths)
def set_graph(self): self.graph = Graph({ 1: Block(5), 2: Block(5), 3: Block(5) }, { 1: [2], 2: [3] })
def setUp(self): self.graph = Graph({i: Block(3) for i in range(1, 7)}, {i: [i + 1] for i in range(1, 6)}) self.peaks = PeakCollection([ Peak(3, 3, [1, 2, 3, 4], self.graph), Peak(3, 3, [5, 6], self.graph) ])
def set_graph(self): self.graph = Graph({ 1: Block(5), 2: Block(5), 3: Block(5), 4: Block(5) }, { 1: [2, 3], 2: [4], 3: [4] })
def setUp(self): self.graph = Graph({i: Block(10) for i in range(1, 4)}, {i: [i + 1] for i in range(1, 3)}) self.index = GraphIndex({ 1: [(2, 10), (3, 20)], 2: [(3, 10)], 3: [], -1: [], -2: [(-1, 10)], -3: [(-2, 10), (-1, 20)] }) self.extender = GraphExtender(self.index)
def setUp(self): self.complex_graph = Graph( {i: Block(3) for i in range(1, 13)}, { 1: [2, 3], 2: [7, 8], 3: [4, 5], 4: [6], 5: [6], 6: [10], 7: [9], 8: [9], 9: [10], 10: [12] }) self.complex_graph.convert_to_numpy_backend()
def test_find_max_path_through_subgraph_two_node_graph(self): graph = Graph({1: Block(10), 2: Block(10)}, {1: [2]}) peak = ConnectedAreas(graph, {2: [0, 4], 1: [5, 10]}) binary_peak = BinaryContinousAreas.from_old_areas(peak) qvalues = DensePileup.from_base_value(graph, 10) print("q values") print(qvalues) print(qvalues.data._values) scored_peak = ScoredPeak.from_peak_and_pileup(binary_peak, qvalues) print(scored_peak) max_path = scored_peak.get_max_path() self.assertEqual(max_path, Interval(5, 4, [1, 2]))
def test_reverse(): graph = Graph({ 1: Block(10), 2: Block(5), 3: Block(10), 4: Block(5) }, { 1: [2, 3], 2: [4], 3: [4] }) graph.convert_to_numpy_backend() linear_path = NumpyIndexedInterval.from_interval( Interval(0, 10, [1, 2, 4], graph)) alignments = [Interval(4, 5, [-3, -1], graph)] projected = project_alignments(alignments, linear_path) projected = list(projected) assert projected[0] == (5, 16, "-")
def test_many_nodes(): nodes = {i: Block(1) for i in range(2, 10)} nodes[1] = Block(10) nodes[10] = Block(10) graph = Graph( nodes, { 1: [2, 3], 2: [4], 3: [4], 4: [5, 6], 5: [7], 6: [7], 7: [8, 9], 8: [10], 9: [10] }) graph.convert_to_numpy_backend() sequence_graph = SequenceGraph.create_empty_from_ob_graph(graph) sequence_graph.set_sequence(1, "ACTGACTGAC") sequence_graph.set_sequence(10, "ACTGACTGAC") sequence_graph.set_sequence(2, "A") sequence_graph.set_sequence(3, "C") sequence_graph.set_sequence(4, "A") sequence_graph.set_sequence(5, "G") sequence_graph.set_sequence(6, "C") sequence_graph.set_sequence(7, "T") sequence_graph.set_sequence(8, "A") sequence_graph.set_sequence(9, "A") linear_ref = Interval(0, 10, [1, 2, 4, 6, 7, 8, 10], graph) linear_ref = linear_ref.to_numpy_indexed_interval() critical_nodes = {1, 4, 7, 10} finder = MinimizerFinder(graph, sequence_graph, critical_nodes, linear_ref, k=3, w=3) minimizers = finder.find_minimizers() print(len(minimizers.minimizers))
def test_simple(): graph = Graph({ 1: Block(10), 2: Block(5), 3: Block(10), 4: Block(5) }, { 1: [2, 3], 2: [4], 3: [4] }) graph.convert_to_numpy_backend() linear_path = NumpyIndexedInterval.from_interval( Interval(0, 10, [1, 2, 4], graph)) alignments = [Interval(5, 5, [1, 3], graph), Interval(5, 5, [3, 4], graph)] projected = project_alignments(alignments, linear_path) projected = list(projected) assert projected[0] == (5, 15, "+") assert projected[1] == (15, 25, "+")
def setUp(self): self.graph = Graph({i: Block(10) for i in range(1, 5)}, { 1: [2, 3], 2: [4], 3: [4] }) self.index = GraphIndex({ 1: [(2, 10), (3, 10), (4, 20)], 2: [(4, 10)], 3: [(4, 10)], 4: [], -1: [], -2: [(-1, 10)], -3: [(-1, 10)], -4: [(-2, 10), (-3, 10), (-1, 20)] }) self.extender = GraphExtender(self.index)
def setUp(self): self.simple_graph = Graph({i: Block(3) for i in range(1, 9)}, { 1: [2, 3], 2: [4], 3: [4], 4: [5], 5: [6, 7], 6: [8], 7: [8] }) print(self.simple_graph.get_first_blocks()) print(self.simple_graph.reverse_adj_list) self.simple_snarls = \ { 20: SimpleSnarl(1, 4, 20), 21: SimpleSnarl(5, 8, 21), 22: SimpleSnarl(4, 5, 22) }
def test_find_max_path_through_subgraph_with_illegal_paths(self): graph = Graph( { 1: Block(10), 2: Block(10), 3: Block(10), 4: Block(10) }, { 1: [2, 3], 2: [4], -4: [-3] # Making 3=>4 not allowed path }) peak = ConnectedAreas(graph, { 2: [0, 10], 3: [0, 10], 1: [5, 10], 4: [0, 8] }) binary_peak = BinaryContinousAreas.from_old_areas(peak) qvalues = DensePileup.from_intervals( graph, [ Interval(0, 10, [3]), # Higher value on 3 than 2 Interval(0, 10, [3]), Interval(0, 10, [4]), # Highest value if ending on 4 Interval(0, 10, [4]), Interval(0, 10, [1]), # Highest value if inncluding 1 Interval(0, 10, [1]), # Highest value if inncluding 1 Interval(0, 10, [1, 2, 4]) ]) scored_peak = ScoredPeak.from_peak_and_pileup(binary_peak, qvalues) max_path = scored_peak.get_max_path() print(max_path) self.assertEqual(max_path, Interval(5, 8, [1, 2, 4]))
def test_many_nodes(): nodes = {i: Block(1) for i in range(2, 10)} nodes[1] = Block(10) nodes[10] = Block(10) graph = Graph( nodes, { 1: [2, 3], 2: [4], 3: [4], 4: [5, 6], 5: [7], 6: [7], 7: [8, 9], 8: [10], 9: [10] }) graph.convert_to_numpy_backend() sequence_graph = SequenceGraph.create_empty_from_ob_graph(graph) sequence_graph.set_sequence(1, "ACTGACTGAC") sequence_graph.set_sequence(10, "ACTGACTGAC") sequence_graph.set_sequence(2, "A") sequence_graph.set_sequence(3, "C") sequence_graph.set_sequence(4, "A") sequence_graph.set_sequence(5, "G") sequence_graph.set_sequence(6, "C") sequence_graph.set_sequence(7, "T") sequence_graph.set_sequence(8, "T") sequence_graph.set_sequence(9, "A") linear_ref_nodes = {1, 2, 4, 6, 7, 8, 10} read_sequence = "ACTGACCAGTAACTGAC" start_node = 1 start_offset = 4 aligner = LocalGraphAligner(graph, sequence_graph, read_sequence, linear_ref_nodes, start_node, start_offset) alignment, score = aligner.align() assert alignment == [1, 3, 4, 5, 7, 9, 10]
def test_simple(self): graph = Graph( {i: Block(3) for i in range(1, 5)}, { 1: [2, 3], 2: [4], 3: [4] } ) graph.convert_to_numpy_backend() intervals = IntervalCollection([ Interval(0, 3, [1, 3]) ]) haplotyper = HaploTyper(graph, intervals) haplotyper.build() max_interval = haplotyper.get_maximum_interval_through_graph() self.assertEqual( max_interval, Interval(0, 3, [1, 3, 4]) )
def _create_data(self): node_offset = 1 for chrom_number, chromosome in enumerate(self.chromosomes): graph = Graph( {i + node_offset: Block(10) for i in range(0, 3)}, {i + node_offset: [i + 1 + node_offset] for i in range(0, 2)}) linear_map = LinearMap.from_graph(graph) linear_map_file_name = "linear_map_%s.npz" % chromosome linear_map.to_file(linear_map_file_name) self.linear_maps.append(linear_map_file_name) self.sequence_retrievers.append( SequenceRetriever( {i + node_offset: "A" * 10 for i in range(0, 3)})) self._create_reads(chrom_number, chromosome, graph) node_offset += 3 graph.convert_to_numpy_backend() SequenceGraph.create_empty_from_ob_graph(graph).to_file( chromosome + ".nobg.sequences") graph.to_file(chromosome + ".nobg")
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))
import pytest import unittest import numpy as np from offsetbasedgraph import GraphWithReversals as Graph,\ Block, DirectedInterval as Interval if pytest.__version__ < "3.0.0": pytest.skip() graph = Graph({i: Block(10) for i in range(1, 4)}, {1: [2], 2: [3]}) @pytest.mark.skip("Legacy") class TestDensePileup(unittest.TestCase): def test_init(self): DensePileup(graph) def test_from_starts_and_ends(self): starts = {1: [3, 5]} ends = {1: [7, 9]} pileup = DensePileup.from_starts_and_ends(graph, starts, ends) indexes, values = pileup.data.get_sparse_indexes_and_values(1) self.assertTrue(np.all(values == [0, 1, 2, 1, 0])) self.assertTrue(np.all(indexes == [0, 3, 5, 7, 9, 10])) starts = {1: [0, 3]} ends = {1: [5, 10]} pileup = DensePileup.from_starts_and_ends(graph, starts, ends) indexes, values = pileup.data.get_sparse_indexes_and_values(1)
def test_hierarchical(self): graph = Graph({i: Block(3) for i in range(1, 13)}, { 11: [1], 1: [2, 3], 2: [7, 8], 3: [4, 5], 4: [6], 5: [6], 6: [10], 7: [9], 8: [9], 9: [10], 10: [12] }) subsnarl1 = SimpleSnarl(3, 6, 21, parent=20) subsnarl2 = SimpleSnarl(2, 9, 22, parent=20) parent_snarl = SimpleSnarl(1, 10, 20, children=[subsnarl1, subsnarl2]) snarls = {20: parent_snarl, 21: subsnarl1, 22: subsnarl2} builder = SnarlGraphBuilder(graph, snarls, id_counter=13) snarlgraph = builder.build_snarl_graphs() print("Snarlgraph") print(snarlgraph) correct_snarl_graph = SnarlGraph( { 11: Block(3), 12: Block(3), 1: Block(3), 10: Block(3), 20: SnarlGraph( { 3: Block(3), 21: SnarlGraph({ 4: Block(3), 5: Block(3) }, { 3: [4, 5], 4: [6], 5: [6] }, start_node=3, end_node=6), 22: SnarlGraph({ 7: Block(3), 8: Block(3) }, { 2: [7, 8], 7: [9], 8: [9] }, start_node=2, end_node=9), 2: Block(3), 6: Block(3), 9: Block(3), }, { 3: [21], 2: [22], 21: [6], 22: [9], 1: [2, 3], 6: [10], 9: [10] }, start_node=1, end_node=10) }, { 11: [1], 1: [20], 20: [10], 10: [12], 13: [11], # Dummy 12: [14], # Dummy }, start_node=13, end_node=14) print("Snarlgraph") print(snarlgraph) self.assertEqual(correct_snarl_graph, snarlgraph)
import pytest # from graph_peak_caller.densepileup import DensePileupData, DensePileup from offsetbasedgraph import GraphWithReversals as Graph, Block,\ DirectedInterval as Interval import unittest import numpy as np # from graph_peak_caller.dagholecleaner import DagHoleCleaner if pytest.__version__ < "3.0.0": pytest.skip() graph = Graph({i: Block(10) for i in range(1, 4)}, {1: [2], 2: [3]}) split_graph = Graph({ 1: Block(10), 2: Block(10), 3: Block(10), 4: Block(10), }, { 1: [2, 3], 2: [4], 3: [4] }) @pytest.mark.skip("Legacy") class TestDagHoleCleanerGetLeftSideOfHoles(unittest.TestCase): def test_simple(self): pileup = DensePileup.from_intervals(graph, [Interval(0, 3, [1])]) cleaner = DagHoleCleaner(pileup, 3) left_holes = cleaner.get_left_side_of_holes()
def setUp(self): self.graph = Graph({i: Block(3) for i in range(1, 3)}, {1: [2]})