def test_map(self): """reversing a map with multiple spans should preserve span relative order""" forward = [Span(20, 30), Span(40, 50)] fmap = Map(spans=forward, parent_length=100) fmap_reversed = fmap.nucleic_reversed() reverse = [Span(70, 80, reverse=True), Span(50, 60, reverse=True)] rmap = Map(spans=reverse, parent_length=100) for i in range(2): self.assertEqual(fmap_reversed.spans[i], rmap.spans[i])
def _remap(map): start = map.start if start == 0: new_map = map new_map.parent_length = map.end else: spans = [] for span in map.spans: if span.lost: spans.append(span) else: span.start = span.start - start span.end = span.end - start length = span.end spans.append(span) new_map = Map(spans=spans, parent_length=length) return new_map
def cigar_to_map(cigar_text): """convert cigar string into Map""" assert "I" not in cigar_text spans, posn = [], 0 for n, c in pattern.findall(cigar_text): if n: n = int(n) else: n = 1 if c == "M": spans.append(Span(posn, posn + n)) posn += n else: spans.append(LostSpan(n)) map = Map(spans=spans, parent_length=posn) return map
def test_get_coords(self): """get_coordinates should return raw coordinates matching input""" spans = [(0, 9), (20, 32)] map = Map(spans, parent_length=100) coords = map.get_coordinates() self.assertEqual(coords, spans) # should work for reversed Maps too spans = [(32, 20), (9, 0)] map = Map(spans, parent_length=100) coords = map.get_coordinates() self.assertEqual(coords, spans)
def test_spans(self): # a simple two part map of length 10 map = Map([(0, 5), (5, 10)], parent_length=10) # try different spans on the above map for ((start, end), expected) in [ ((0, 4), "[0:4]"), ((0, 5), "[0:5]"), ((0, 6), "[0:5, 5:6]"), ((5, 10), "[5:10]"), ((-1, 10), "[-1-, 0:5, 5:10]"), ((5, 11), "[5:10, -1-]"), ((0, 10), "[0:5, 5:10]"), ((10, 0), "[10:5, 5:0]"), ]: r = repr(Span(start, end, reverse=start > end).remap_with(map)) # print (start, end), r, if r != expected: self.fail(repr((r, expected)))
self.assertEqual(str(answer[0]), "TCGAT") def test_get_by_seq_annotation(self): aln = make_aligned_seqs( data={"a": "ATCGAAATCGAT", "b": "ATCGA--TCGAT"}, array_align=False ) b = aln.get_seq("b") b.add_annotation(Feature, "test_type", "test_label", [(4, 6)]) answer = aln.get_by_seq_annotation("b", "test_type")[0].to_dict() self.assertEqual(answer, {"b": "A--T", "a": "AAAT"}) if 0: # old, needs fixes # Maps a = Map([(10, 20)], parent_length=100) for (desc, map, expected) in [ ("a ", a, "Map([10:20] on base)"), ("i ", a.inverse(), "Map([-10-, 0:10, -80-] on Map([10:20] on base))"), ("1 ", a[5:], "Map([5:10] on Map([10:20] on base))"), ("1r", a[5:].relative_to(b), "Map([15:20] on base)"), ("2 ", a[:5], "Map([0:5] on Map([10:20] on base))"), ("2r", a[:5].relative_to(b), "Map([10:15] on base)"), ( "r ", a.relative_to(a[5:]), "Map([-5-, 0:5] on Map([5:10] on Map([10:20] on base)))", ), ( "r ",