def test_merge_pair_without_shared_positions(merge): empty1 = Read("Name1") empty2 = Read("Name2") assert merge(empty1, empty2).name == "Name1" assert merge(empty2, empty1).name == "Name2" # add_variant parameters are: (position, allele, quality) left = Read("Name1") left.add_variant(100, 0, 31) left.add_variant(200, 0, 32) right = Read("Name2") right.add_variant(300, 1, 41) right.add_variant(400, 1, 42) expected = [ Variant(100, 0, 31), Variant(200, 0, 32), Variant(300, 1, 41), Variant(400, 1, 42), ] assert expected == list(merge(left, right)) assert expected == list(merge(right, left)) outer = Read("Name1") outer.add_variant(100, 0, 31) outer.add_variant(400, 1, 42) inner = Read("Name2") inner.add_variant(200, 0, 32) inner.add_variant(300, 1, 41) assert expected == list(merge(inner, outer)) assert expected == list(merge(outer, inner))
def test_read_iteration(): r = Read("name", 15) r.add_variant(100, 1, 37) r.add_variant(23, 0, 99) v1 = Variant(position=100, allele=1, quality=37) v2 = Variant(position=23, allele=0, quality=99) variants = list(r) assert variants == [v1, v2] # negative indices assert r[-1] == v2 assert r[-2] == v1
def test_readset(): rs = ReadSet() r = Read('Read A', 56) r.add_variant(100, 1, 37) r.add_variant(101, 0, 18) rs.add(r) r = Read('Read B', 0) r.add_variant(101, 0, 23) rs.add(r) r = Read('Read C', 17) r.add_variant(99, 1, 27) r.add_variant(80, 1, 17) r[1] = Variant(position=105, allele=0, quality=14) rs.add(r) assert rs[0].name == 'Read A' assert rs[1].name == 'Read B' assert rs[2].name == 'Read C' rs.sort() # should be sorted after finalization assert rs[0].name == 'Read C' assert rs[1].name == 'Read A' assert rs[2].name == 'Read B' assert len(rs) == 3 assert rs.get_positions() == [99, 100, 101, 105] r = rs[(0, 'Read A')] assert r.name == 'Read A' assert r.mapqs == (56, ), str(r.mapqs) r = rs[(0, 'Read B')] assert r.name == 'Read B' assert r.mapqs == (0, ) r = rs[(0, 'Read C')] assert r.name == 'Read C' assert r.mapqs == (17, ) assert len(r) == 2 assert r[0] == Variant(position=99, allele=1, quality=27) assert r[1] == Variant(position=105, allele=0, quality=14)
def test_merge_many_reads(): reads = [ Read("Name1"), Read("Name2"), Read("Name3"), ] reads[0].add_variant(100, 0, 31) reads[0].add_variant(200, 1, 32) reads[0].add_variant(300, 0, 33) reads[1].add_variant(200, 1, 41) reads[1].add_variant(400, 0, 42) reads[1].add_variant(500, 0, 43) reads[2].add_variant(200, 0, 51) reads[2].add_variant(500, 0, 52) reads[2].add_variant(600, 0, 53) expected = [ Variant(100, 0, 31), Variant(200, 1, 73), # see note: this depends on order of reads Variant(300, 0, 33), Variant(400, 0, 42), Variant(500, 0, 43 + 52), Variant(600, 0, 53), ] assert expected == list(merge_reads(*reads)) # TODO merging should not depend on the order of reads expected[1] = Variant(200, 0, 51) assert expected == list(merge_reads(*reads[::-1]))
def test_merge_pair_with_shared_positions(merge): left = Read("Name1") left.add_variant(100, 0, 31) left.add_variant(200, 0, 32) left.add_variant(300, 0, 33) right = Read("Name2") right.add_variant(200, 0, 41) # alleles disagree right.add_variant(300, 1, 42) # alleles agree right.add_variant(400, 1, 43) expected = [ Variant(100, 0, 31), Variant(200, 0, 32 + 41), Variant(300, 1, 42), Variant(400, 1, 43), ] assert expected == list(merge(left, right)) assert expected == list(merge(right, left))