Skip to content

Commit

Permalink
Cigar tools: improve semantics of "closest"
Browse files Browse the repository at this point in the history
It is now based on op index.
  • Loading branch information
Donaim committed Nov 11, 2023
1 parent 017a93a commit 0158257
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
6 changes: 4 additions & 2 deletions micall/core/contig_stitcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,10 @@ def covered(contig, gap):
def gap_boundaries(gap):
midpoint = gap.r_st + (gap.r_ei - gap.r_st) / 2
left_slice, right_slice = contig.cut_reference(floor(midpoint) + 0.5)
left_closest_query = left_slice.alignment.coordinate_mapping.ref_to_closest_query(midpoint)
right_closest_query = right_slice.alignment.coordinate_mapping.ref_to_closest_query(midpoint)
left_midpoint_ref = left_slice.alignment.coordinate_mapping.find_closest_ref(midpoint)
left_closest_query = left_slice.alignment.coordinate_mapping.ref_to_closest_query(left_midpoint_ref)
right_midpoint_ref = right_slice.alignment.coordinate_mapping.find_closest_ref(midpoint)
right_closest_query = right_slice.alignment.coordinate_mapping.ref_to_closest_query(right_midpoint_ref)
left_closest_ref = left_slice.alignment.coordinate_mapping.query_to_ref(left_closest_query)
right_closest_ref = right_slice.alignment.coordinate_mapping.query_to_ref(right_closest_query)
return (left_closest_ref, right_closest_ref)
Expand Down
6 changes: 3 additions & 3 deletions micall/tests/test_cigar_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
{0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 3, 6: 4, 7: 4, 8: 5}),

# Edge cases
('', {}, ValueError()),
('12I', {}, ValueError()),
('12D', {}, ValueError()),
('', {}, KeyError()),
('12I', {}, KeyError()),
('12D', {}, KeyError()),
]


Expand Down
33 changes: 23 additions & 10 deletions micall/utils/cigar_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,40 @@ def query_to_ref(self, index) -> Optional[int]:
return self.query_to_ref_d.get(index, None)


def ref_to_leftsup_query(self, index) -> Optional[int]:
left_neihbourhood = (k for (k, v) in self.query_to_ref_d.items() if v <= index)
return max(left_neihbourhood, default=None)


def ref_to_rightinf_query(self, index) -> Optional[int]:
right_neihbourhood = (k for (k, v) in self.query_to_ref_d.items() if index <= v)
return min(right_neihbourhood, default=None)


@staticmethod
def _find_closest(collection, value) -> int:
return min(collection, key=lambda x: abs(x - value))


@staticmethod
def _find_closest_key(mapping: dict, index: int) -> int:
return min(mapping, key=lambda k: abs(mapping[k] - index))


def ref_to_closest_query(self, index) -> int:
return CoordinateMapping._find_closest_key(self.query_to_ref_d, index)
def find_closest_ref(self, index) -> int:
return CoordinateMapping._find_closest(self.all_reference_coordinates(), index)


def query_to_closest_ref(self, index) -> int:
return CoordinateMapping._find_closest_key(self.ref_to_query_d, index)
def find_closest_query(self, index) -> int:
return CoordinateMapping._find_closest(self.all_query_coordinates(), index)


def ref_to_leftsup_query(self, index) -> Optional[int]:
left_neihbourhood = (k for (k, v) in self.query_to_ref_d.items() if v <= index)
return max(left_neihbourhood, default=None)
def ref_to_closest_query(self, index) -> int:
return CoordinateMapping._find_closest_key(self.query_to_op_d, self.ref_to_op_d[index])


def ref_to_rightinf_query(self, index) -> Optional[int]:
right_neihbourhood = (k for (k, v) in self.query_to_ref_d.items() if index <= v)
return min(right_neihbourhood, default=None)
def query_to_closest_ref(self, index) -> int:
return CoordinateMapping._find_closest_key(self.ref_to_op_d, self.query_to_op_d[index])


def ref_or_query_to_op(self, ref_index: int, query_index: int, conflict):
Expand Down

0 comments on commit 0158257

Please sign in to comment.