Beispiel #1
0
 def test_input_errors(self):
     with pytest.raises(ValueError):
         ibd.IbdFinder(self.ts, sample_pairs=[0])
     with pytest.raises(AssertionError):
         ibd.IbdFinder(self.ts, sample_pairs=[(0, 1, 2)])
     with pytest.raises(ValueError):
         ibd.IbdFinder(self.ts, sample_pairs=[(0, 5)])
     with pytest.raises(ValueError):
         ibd.IbdFinder(self.ts, sample_pairs=[(0, 1), (1, 0)])
Beispiel #2
0
 def test_length(self):
     ts = self.ts
     ibd_f = ibd.IbdFinder(ts, min_length=0.4)
     ibd_segs = ibd_f.find_ibd_segments()
     true_segs = {(0, 1): [ibd.Segment(0.2, 0.7, 4)]}
     ibd_segs = convert_dict_of_segmentlists(ibd_segs)
     assert ibd_is_equal(ibd_segs, true_segs)
Beispiel #3
0
def find_ibd(
    ts,
    sample_pairs,
    min_length=0,
    max_time=None,
    compare_lib=True,
    print_c=False,
    print_py=False,
):
    """
    Calculates IBD segments using Python and converts output to lists of segments.
    Also compares result with C library.
    """
    ibd_f = ibd.IbdFinder(ts,
                          sample_pairs=sample_pairs,
                          max_time=max_time,
                          min_length=min_length)
    ibd_segs = ibd_f.find_ibd_segments()
    ibd_segs = convert_ibd_output_to_seglists(ibd_segs)
    if compare_lib:
        c_out = ts.tables.find_ibd(sample_pairs,
                                   max_time=max_time,
                                   min_length=min_length)
        c_out = convert_ibd_output_to_seglists(c_out)
        if print_c:
            print("C output:\n")
            print(c_out)
        if print_py:
            print("Python output:\n")
            print(ibd_segs)
        assert ibd_is_equal(ibd_segs, c_out)
    return ibd_segs
Beispiel #4
0
def verify_equal_ibd(treeSequence):
    """
    Calculates IBD segments using both the 'naive' and sophisticated algorithms,
    verifies that the same output is produced.
    NB: May be good to expand this in the future so that many different combos
    of IBD options are tested simultaneously (all the MRCA and path-IBD combos),
    for example.
    """
    ts = treeSequence
    ibd0 = ibd.IbdFinder(ts, samples=ts.samples())
    ibd0 = ibd0.find_ibd_segments()
    ibd1 = get_ibd_all_pairs(ts, path_ibd=True, mrca_ibd=True)

    # Convert each SegmentList object into a list of Segment objects.
    ibd0_tolist = {}
    for key, val in ibd0.items():
        if val is not None:
            ibd0_tolist[key] = convert_segmentlist_to_list(val)

    # Check for equality.
    for key0, val0 in ibd0_tolist.items():

        assert key0 in ibd1.keys()
        val1 = ibd1[key0]
        val0.sort()
        val1.sort()
Beispiel #5
0
    def test_basic(self):
        ts = self.ts
        ibd_f = ibd.IbdFinder(ts)
        ibd_segs = ibd_f.find_ibd_segments()
        ibd_segs = convert_dict_of_segmentlists(ibd_segs)
        true_segs = {(0, 1): [ibd.Segment(0.0, 1.0, 1)]}

        assert ibd_is_equal(ibd_segs, true_segs)
Beispiel #6
0
 def test_defaults(self):
     ts = self.ts
     ibd_f = ibd.IbdFinder(ts)
     ibd_segs = ibd_f.find_ibd_segments()
     ibd_segs = convert_dict_of_segmentlists(ibd_segs)
     true_segs = {
         (0, 2): [ibd.Segment(0, 1, 3)],
     }
     assert ibd_is_equal(ibd_segs, true_segs)
Beispiel #7
0
 def test_time(self):
     ts = self.ts
     ibd_f = ibd.IbdFinder(ts, max_time=1.8)
     ibd_segs = ibd_f.find_ibd_segments()
     true_segs = {(0, 1): []}
     ibd_segs = convert_dict_of_segmentlists(ibd_segs)
     assert ibd_is_equal(ibd_segs, true_segs)
     ibd_f = ibd.IbdFinder(ts, max_time=2.8)
     ibd_segs = ibd_f.find_ibd_segments()
     true_segs = {
         (0, 1): [
             ibd.Segment(0.0, 0.2, 4),
             ibd.Segment(0.7, 1.0, 4),
             ibd.Segment(0.2, 0.7, 4),
         ]
     }
     ibd_segs = convert_dict_of_segmentlists(ibd_segs)
     assert ibd_is_equal(ibd_segs, true_segs)
Beispiel #8
0
 def test_time(self):
     ibd_f = ibd.IbdFinder(self.ts, max_time=1.5)
     ibd_segs = ibd_f.find_ibd_segments()
     ibd_segs = convert_dict_of_segmentlists(ibd_segs)
     true_segs = {
         (0, 1): [ibd.Segment(0.0, 1.0, 3)],
         (0, 2): [],
         (1, 2): []
     }
     assert ibd_is_equal(ibd_segs, true_segs)
Beispiel #9
0
 def test_length(self):
     ts = self.ts
     ibd_f = ibd.IbdFinder(ts, min_length=0.5)
     ibd_segs = ibd_f.find_ibd_segments()
     true_segs = {
         (0, 1): [ibd.Segment(0, 1, 4)],
         (0, 2): [ibd.Segment(0.3, 1, 5)],
         (0, 3): [ibd.Segment(0.3, 1, 4)],
         (1, 2): [ibd.Segment(0.3, 1, 5)],
         (1, 3): [ibd.Segment(0.3, 1, 4)],
         (2, 3): [ibd.Segment(0.3, 1, 5)],
     }
     ibd_segs = convert_dict_of_segmentlists(ibd_segs)
     assert ibd_is_equal(ibd_segs, true_segs)
Beispiel #10
0
 def test_time(self):
     ts = self.ts
     ibd_f = ibd.IbdFinder(ts, max_time=3)
     ibd_segs = ibd_f.find_ibd_segments()
     true_segs = {
         (0, 1): [ibd.Segment(0, 1, 4)],
         (0, 2): [ibd.Segment(0, 0.3, 4)],
         (0, 3): [ibd.Segment(0.3, 1, 4)],
         (1, 2): [ibd.Segment(0, 0.3, 4)],
         (1, 3): [ibd.Segment(0.3, 1, 4)],
         (2, 3): [],
     }
     ibd_segs = convert_dict_of_segmentlists(ibd_segs)
     assert ibd_is_equal(ibd_segs, true_segs)
Beispiel #11
0
 def test_input_sample_pairs(self):
     ibd_f = ibd.IbdFinder(self.ts, sample_pairs=[(0, 1), (2, 3), (1, 3)])
     ibd_segs = ibd_f.find_ibd_segments()
     ibd_segs = convert_ibd_output_to_seglists(ibd_segs)
     true_segs = {
         (0, 1): [
             ibd.Segment(0.0, 0.2, 4),
             ibd.Segment(0.7, 1.0, 4),
             ibd.Segment(0.2, 0.7, 4),
         ],
         (2, 3): [ibd.Segment(0.2, 0.7, 4)],
     }
     ibd_segs = find_ibd(
         self.ts,
         sample_pairs=[(0, 1), (2, 3)],
         compare_lib=True,
         print_c=False,
         print_py=False,
     )
     assert ibd_is_equal(ibd_segs, true_segs)
Beispiel #12
0
 def test_defaults(self):
     ts = self.ts
     ibd_f = ibd.IbdFinder(ts)
     ibd_segs = ibd_f.find_ibd_segments()
     # print(ibd_segs[(0,1)])
     true_segs = {
         (0, 1): [ibd.Segment(0, 1, 4)],
         (0, 2): [ibd.Segment(0, 0.3, 4),
                  ibd.Segment(0.3, 1, 5)],
         (0, 3): [ibd.Segment(0, 0.3, 5),
                  ibd.Segment(0.3, 1, 4)],
         (1, 2): [ibd.Segment(0, 0.3, 4),
                  ibd.Segment(0.3, 1, 5)],
         (1, 3): [ibd.Segment(0, 0.3, 5),
                  ibd.Segment(0.3, 1, 4)],
         (2, 3): [ibd.Segment(0.3, 1, 5),
                  ibd.Segment(0, 0.3, 5)],
     }
     ibd_segs = convert_dict_of_segmentlists(ibd_segs)
     # print(ibd_segs)
     assert ibd_is_equal(ibd_segs, true_segs)
Beispiel #13
0
 def test_no_samples(self):
     #
     #     2
     #    / \
     #   /   \
     #  /     \
     # (0)   (1)
     nodes = io.StringIO("""\
     id      is_sample   time
     0       0           0
     1       0           0
     2       0           1
     3       0           1
     """)
     edges = io.StringIO("""\
     left    right   parent  child
     0       1       2       0
     0       1       3       1
     """)
     ts = tskit.load_text(nodes=nodes, edges=edges, strict=False)
     with pytest.raises(ValueError):
         ibd.IbdFinder(ts, sample_pairs=[(0, 1)])
Beispiel #14
0
def ibd_segments(
    ts,
    *,
    within=None,
    between=None,
    min_span=0,
    max_time=None,
    compare_lib=True,
    print_c=False,
    print_py=False,
):
    """
    Calculates IBD segments using Python and converts output to lists of segments.
    Also compares result with C library.
    """
    ibd_f = ibd.IbdFinder(ts,
                          within=within,
                          between=between,
                          max_time=max_time,
                          min_span=min_span)
    ibd_segs = ibd_f.run()
    if print_py:
        print("Python output:\n")
        print(ibd_segs)
    # ibd_f.print_state()
    if compare_lib:
        c_out = ts.ibd_segments(
            within=within,
            between=between,
            max_time=max_time,
            min_span=min_span,
            store_segments=True,
        )
        if print_c:
            print("C output:\n")
            print(c_out)
        assert_ibd_equal(ibd_segs, c_out)
    return ibd_segs
Beispiel #15
0
 def test_length(self):
     ibd_f = ibd.IbdFinder(self.ts, min_length=0.2)
     ibd_segs = ibd_f.find_ibd_segments()
     ibd_segs = convert_dict_of_segmentlists(ibd_segs)
     true_segs = {(0, 1): []}
     assert ibd_is_equal(ibd_segs, true_segs)