diff --git a/src/sequence_analysis/pairwise_aligner.py b/src/sequence_analysis/pairwise_aligner.py index 6c7c22b..2d1475e 100644 --- a/src/sequence_analysis/pairwise_aligner.py +++ b/src/sequence_analysis/pairwise_aligner.py @@ -37,3 +37,25 @@ def print(self, char_per_line=80): print(query_aligned[i*char_per_line:(i+1)*char_per_line]) print(match[i*char_per_line:(i+1)*char_per_line]) print(target_aligned[i*char_per_line:(i+1)*char_per_line]) + + + # TODO: refactor below. + def get_query_range(self): + """ + Function to get the range of the query that aligns with the target. + """ + query_aligned = self.get_query_aligned().replace('-','') + start = self.query.find(query_aligned) + end = self.query[::-1].find(query_aligned[::-1]) + end = len(self.query) - end - 1 + return start, end + + def get_target_range(self): + """ + Function to get the range of the target that aligns with the query. + """ + target_aligned = self.get_target_aligned().replace('-','') + start= self.target.find(target_aligned) + end = self.target[::-1].find(target_aligned[::-1]) + end = len(self.target) - end - 1 + return start, end diff --git a/tests/test_pairwise_aligner.py b/tests/test_pairwise_aligner.py index 46172ad..cee2140 100644 --- a/tests/test_pairwise_aligner.py +++ b/tests/test_pairwise_aligner.py @@ -48,6 +48,21 @@ def test_align_3(self): pa.target = "MKA" pa.align() + def test_get_alignment_range(self): + pa = PairwiseAligner() + pa.algorithm = "local" + pa.query = "MCDDVAALVVD" + pa.target = "DVAALV" + pa.align() + + qs, qe = pa.get_query_range() + assert qs == 3 + assert qe == 8 + + ts, te = pa.get_target_range() + assert ts == 0 + assert te == len(pa.target)-1 + def test_align_blastn(self): pa = PairwiseAligner("blastn", -2) pa.algorithm = "local"