def test_invert_cyclic(self): s = Span(5, 15, 20, True) i1 = s.invert()[0] assert i1.a == 15 assert i1.b == 5
def test_connectin_span_consecurive_is_empty(): s1 = Span(10, 30, 100, True) s2 = Span(30, 50, 100, True) assert not s1.overlaps_with(s2) s3 = s1.connecting_span(s2) assert len(s3) == 0
def test_invert_linear(self): s = Span(5, 15, 20, False) s1, s2 = s.invert() assert s1.a == 0 assert s1.b == 5 assert s2.a == 15 assert s2.b == 20
def test_connectin_span_over_origin(): s1 = Span(50, 60, 100, True) s2 = Span(5, 30, 100, True) s3 = s1.connecting_span(s2) assert s3.a == 60 assert s3.b == 5
def test_no_overlap_diff(self): s1 = Span(20, 80, 100, True) s2 = Span(90, 95, 100, True) diff = s1.differences(s2) assert len(diff) == 1 assert s1.a == 20 assert s1.b == 80
def test_get_slice_seqrecord(self): seq = SeqRecord(Seq("AGGGTGTGTGCGA")) span = Span(8, 22, len(seq), cyclic=True) seq2 = span.get_slice(seq) assert isinstance(seq2, SeqRecord) assert isinstance(seq2.seq, Seq) print(seq2)
def pcr_amplify( fwd: SeqRecord, rev: SeqRecord, template: SeqRecord, cyclic: bool, cyclic_buffer: int = 100, name: str = None, return_matches: bool = False, ): original_template = template if cyclic: template = template + template + template[:cyclic_buffer] fwd_matches, rev_matches = anneal(str(template.seq), [str(fwd.seq), str(rev.seq)]) products_by_sequence = {} for f, r in itertools.product(fwd_matches, rev_matches): i = f["top_strand_slice"][0] j = r["top_strand_slice"][1] try: span = Span( i, j, length=len(original_template.seq), cyclic=cyclic, ignore_wrap=True, ) except IndexError as e: if not cyclic: continue else: raise e f_rec = new_sequence( f["overhang"], name=fwd.name + "_overhang", auto_annotate=True ) r_rec = new_sequence( rc(r["overhang"]), name=rev.name + "_overhang", auto_annotate=True ) product = span.get_slice(original_template) # annotate(template_chunk, name="source: {}".format(template_name[:40])) if len(f_rec.seq): product = f_rec + product if len(r_rec.seq): product = product + r_rec if not name: name = original_template.name or original_template.id name += "[{}:{}]".format(span.a, span.b) # annotate(product, name=name) if len(product) <= len(original_template.seq): products_by_sequence[str(product.seq)] = (product, span.a, span.b) product_list = list(products_by_sequence.values()) product_list.sort(key=lambda x: (x[1], len(product))) if return_matches: return product_list, fwd_matches, rev_matches return product_list
def test_list(): s = Span(99, 10, 100, True, 0) assert list(s) == [99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] s = Span(99, 10, 100, True, 1) assert list(s) == [99, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9] s = Span(99, 10, 100, True, 2) assert list(s) == [99, 100, 101, 2, 3, 4, 5, 6, 7, 8, 9]
def test_invalid_slice(self): s = Span(90, 10, 100, True) sliced = s[:15] assert sliced.a == 90 assert sliced.b == 5 s = Span(90, 10, 100, True) sliced = s[-15:] assert sliced.a == 95 assert sliced.b == 10
def test_does_not_contain(self): s1 = Span(10, 50, 100, True) s2 = Span(11, 50, 100, True) assert s1 not in s2 assert s2 in s1 s1 = Span(10, 50, 100, True) s2 = Span(10, 49, 100, True) assert s1 not in s2 assert s2 in s1
def test_linear_diff(self): s1 = Span(20, 80, 100, True) s2 = Span(30, 50, 100, True) diff = s1.differences(s2) assert len(diff) == 2 assert diff[0].a == 20 assert diff[0].b == 30 assert diff[1].a == 50 assert diff[1].b == 80
def test_all_init(self, a, b, cyclic): length = 20 if a > b and not cyclic: with pytest.raises(IndexError): Span(a, b, length, cyclic) else: s = Span(a, b, length, cyclic) assert s.a == a assert s.b == b assert s.context_length == length assert s.context_length == length if cyclic and a > b: assert len(s) == length - a + b else: assert len(s) == b - a
def test_open_ended_slices(self): s = Span(0, 10, 20, cyclic=False) assert list(s[:10]) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] assert list(s[:-10]) == [] with pytest.raises(IndexError): assert list(s[10:]) == [] assert list(s[-10:]) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def test_eq(): s1 = Span(10, 90, 100, True) s2 = Span(10, 90, 100, True) assert s1 == s2 s1 = Span(10, 91, 100, True) s2 = Span(10, 90, 100, True) assert not s1 == s2 s1 = Span(11, 90, 100, True) s2 = Span(10, 90, 100, True) assert not s1 == s2 s1 = Span(10, 90, 100, False) s2 = Span(10, 90, 100, True) assert not s1 == s2 s1 = Span(10, 90, 100, True) s2 = Span(10, 90, 101, True) assert not s1 == s2
def test_open_ended_slice_right(self, i): s = Span(10, 20, 200, False) if i >= 10 or i < -10: with pytest.raises(IndexError): assert not s[i:] else: sliced = s[i:] assert sliced.b == 20 if i < 0: assert sliced.a == 20 + i else: assert sliced.a == 10 + i
def test_open_ended_slice_left(self, i): s = Span(10, 20, 200, False) if i > 10 or i < -10: with pytest.raises(IndexError): s[:i] else: sliced = s[:i] assert sliced.a == 10 if i < 0: assert sliced.b == 20 + i else: assert sliced.b == 10 + i
def test_indexing(self, i, cyclic): s = Span(5, 15, 20, cyclic) assert len(s) == 10 if (i < -len(s) or i >= len(s)) and not cyclic: with pytest.raises(IndexError): print(s[i]) else: if i < 0: assert s[i] == (i + 15) % 20 else: assert s[i] == (i + 5) % 20
def test_get_slice_list_of_ints(self): span = Span(8, 22, 10, cyclic=True) x = list(range(20, 140)) y = span.get_slice(x) print(list(span.get_slice_iter(x))) print(y) assert y == [x[_s] for _s in list(span)] span = Span(8, 22, 10, cyclic=True) x = tuple(range(20, 140)) y = span.get_slice(x) assert y == tuple([x[_s] for _s in list(span)])
def test_connecting_span_cyclic(): s1 = Span(10, 20, 100, True) s2 = Span(80, 90, 100, True) s3 = s1.connecting_span(s2) assert s3.a == 20 assert s3.b == 80 s4 = s2.connecting_span(s1) assert s4.a == 90 assert s4.b == 10
def test_intersection(self): s1 = Span(10, 50, 100, True) s2 = Span(40, 60, 100, True) sliced = s1.intersection(s2) assert sliced.a == 40 assert sliced.b == 50 sliced = s2.intersection(s1) assert sliced.a == 40 assert sliced.b == 50
def test_overhang_right_diff(self): s1 = Span(20, 80, 100, True) s2 = Span(70, 90, 100, True) diff = s1.differences(s2) assert len(diff) == 1 assert diff[0].a == 20 assert diff[0].b == 70 diff = s2.differences(s1) print(diff) assert len(diff) == 1 assert diff[0].a == 80 assert diff[0].b == 90
def test_overhang_left_diff(self): s1 = Span(20, 80, 100, True) s2 = Span(10, 30, 100, True) diff = s1.differences(s2) assert len(diff) == 1 assert diff[0].a == 30 assert diff[0].b == 80 diff = s2.differences(s1) print(diff) assert len(diff) == 1 assert diff[0].a == 10 assert diff[0].b == 20
def test_index_span(index): s = Span(90, 10, 100, True, index) assert len(s) == 20
def test_full(): s = Span(1, 9410, 9409, False, index=1) assert len(s) == 9409
def test_slicing_open_left2(length, index): s = Span(90, 10, 100, True, index) print(s[-length:]) assert len(s[-length:]) == length
def test_indexing(index): s = Span(99, 10, 100, True, index) assert s[0] == 99 assert s[-1] == 9 assert s[-2] == 8
def test_t(index): s = Span(99, 10, 100, True, index) assert s.t(-1) == 100 + index - 1 assert s.t(0) == index assert s.t(1) == index + 1
def test_str(): print(Span(90, 10, 100, True))
def test_iter_cyclic(): s = Span(90, 10, 100, True) assert list(s) == list(range(90, 100)) + list(range(10))
def test_iter_linear(): s = Span(10, 20, 100, False) assert list(s) == list(range(10, 20))