def test_get_identical_intervals(self):
        identical = self.peaks.get_identical_intervals(
            PeakCollection([Peak(2, 3, [1, 2, 3, 4], self.graph)]))
        self.assertEqual(len(identical), 0)

        identical = self.peaks.get_identical_intervals(
            PeakCollection([Peak(3, 3, [1, 2, 3, 4], self.graph)]))
        self.assertEqual(len(identical), 1)
Example #2
0
    def from_graph_peaks_in_fasta(cls, graph, vg_graph_json_file_name,
                                  chromosome, fasta_file_name,
                                  regions_bed_file, true_peaks_file):
        reads = PeakCollection.from_fasta_file(fasta_file_name, graph=graph)
        vg_graph = pyvg.vg.Graph.create_from_file(
            vg_graph_json_file_name, limit_to_chromosomes=chromosome)
        logging.info("Finding linear path")

        linear_path_file = "linear_path_%s.intervalcollection" % chromosome
        try:
            linear_path = obg.IntervalCollection.from_file(
                linear_path_file, text_file=True).intervals[0]
            linear_path = linear_path.to_indexed_interval()
        except FileNotFoundError:
            linear_path = create_linear_path(graph,
                                             vg_graph,
                                             path_name=chromosome,
                                             write_to_file=linear_path_file)

        linear_path.graph = graph

        filtered_reads = []

        # Convert regions to intervals in graph
        logging.info("Converting regions to regions in graph")
        graph_regions = []
        bed_file = open(regions_bed_file)
        for line in bed_file:
            print(line)
            line = line.split()
            chr = line[0]
            start = int(line[1])
            end = int(line[2])

            if chr != "chr%s" % chromosome:
                logging.info("Skipping %s, %d, %d" % (chr, start, end))
                continue

            graph_interval = linear_path.get_subinterval(start, end)
            graph_regions.append(graph_interval)

        assert len(graph_regions
                   ) > 0, " Found not graph regions for chr %d" % chromosome
        graph_regions = PeakCollection(graph_regions)

        # Filter out reads not overlapping with linear regions
        for read in reads:
            n_overlapping = graph_regions.get_overlapping_intervals(
                read, minimum_overlap=1)

            if n_overlapping:
                filtered_reads.append(read)

        logging.info("Found %d reads in graph regions" % len(filtered_reads))

        return cls(chromosome, reads, true_peaks_file)
    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 test_approx_contains(self):

        peaks = PeakCollection(
            [Peak(3, 3, [1, 2, 3, 4]),
             Peak(3, 3, [-10, 11])])
        peaks.create_node_index()

        self.assertTrue(peaks.approx_contains_part_of_interval(Peak(1, 2,
                                                                    [1])))

        self.assertTrue(
            peaks.approx_contains_part_of_interval(Peak(1, 2, [10])))

        self.assertFalse(
            peaks.approx_contains_part_of_interval(Peak(1, 2, [100])))
Example #5
0
    def compare_with_correct_peaks(self):
        correct_peaks = PeakCollection(self.correct_peaks)
        #for peak in correct_peaks:
        #    print(peak)
        found_peaks = PeakCollection.create_list_from_file(
            "max_paths.intervalcollection", graph=self.graph)

        #for i in found_peaks:
        #    print(i)
        matched = correct_peaks.get_identical_intervals(found_peaks)
        subgraphs = self.caller.q_value_peak_caller.peaks_as_subgraphs
        print("%d subgraphs" % len(subgraphs.subgraphs))

        print(
            "%d correct peaks identically found, %3.f %% " %
            (len(matched), 100 * len(matched) / len(correct_peaks.intervals)))
    def test_intervals_to_fasta_from_fasta(self):
        run_argument_parser([
            "create_ob_graph", "-o", "tests/testgraph.obg",
            "tests/vg_test_graph.json"
        ])

        PeakCollection([Peak(0, 2, [1, 2], score=3)
                        ]).to_file("tests/testintervals.intervalcollection",
                                   text_file=True)
        run_argument_parser([
            "peaks_to_fasta", "tests/testgraph.obg.sequences",
            "tests/testintervals.intervalcollection",
            "tests/testsequences.fasta"
        ])

        collection = PeakCollection.from_fasta_file(
            "tests/testsequences.fasta")
        self.assertEqual(len(collection.intervals), 1)
        self.assertEqual(collection.intervals[0].sequence.lower(), "tttcccctt")
    def test_get_summits(self):

        qvalues = SparseValues(np.array([0]), np.array([3]))
        qvalues.track_size = 22
        qvalues.to_sparse_files("tests/test_qvalues")

        run_argument_parser([
            "create_ob_graph", "-o", "tests/testgraph.obg",
            "tests/vg_test_graph.json"
        ])
        max_paths = PeakCollection([Peak(0, 2, [1, 2], score=3)])
        PeakFasta(self.correct_sequence_graph).write_max_path_sequences(
            "tests/test_max_paths.fasta", max_paths)

        run_argument_parser([
            "get_summits", "-g", "tests/testgraph.obg",
            "tests/test_max_paths.fasta", "tests/test_qvalues", "2"
        ])

        result = PeakCollection.from_fasta_file(
            "tests/test_max_paths_summits.fasta")
        self.assertEqual(result.intervals[0], Peak(2, 6, [1]))
        self.assertEqual(result.intervals[0].sequence.lower(), "tccc")
    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))
from graph_peak_caller.peakcollection import PeakCollection

chrom = sys.argv[1]
fragment_length = int(sys.argv[2])

ref = NumpyIndexedInterval.from_file("/data/bioinf/tair2/" + chrom + "_linear_pathv2.interval")


graph = Graph.from_file("/data/bioinf/tair2/" + chrom + ".nobg")
direct = SparseValues.from_sparse_files(chrom + "_direct_pileup")
filtered_peaks = SparseValues.from_sparse_files(chrom + "_hole_cleaned")
variant_map = load_variant_maps(chrom, "/data/bioinf/tair2/")

max_paths, sub_graphs = SparseMaxPaths(filtered_peaks, graph, direct, ref, variant_map).run()
long_maxpaths = [path for path in max_paths if path.length() >= fragment_length]

for max_path in long_max_paths:
    assert max_path.length() > 0, "Max path %s has negative length" % max_path
    score = np.max(self.q_values.get_interval_values(max_path))
    max_path.set_score(score)
    assert not np.isnan(score), "Score %s is nan" % score


PeakCollection(long_maxpaths).to_file(chrom + "_max_paths.intervalcollection", text_file=True)

from graph_peak_caller.peakfasta import PeakFasta
from offsetbasedgraph import SequenceGraph
seqgraph = SequenceGraph.from_file("/data/bioinf/tair2/" + chrom + ".nobg.sequences")
PeakFasta(seqgraph).write_max_path_sequences(chrom + "_sequences.fasta", long_maxpaths)