def do_mutation(self, solution: MSASolution) -> MSASolution: if random.random() <= self.probability: # Select one random sequence from all for seq in range(solution.number_of_variables): gaps_group = solution.gaps_groups[seq] if len(gaps_group) >= 4: random_gaps_group = random.randrange( 0, len(gaps_group) - 2, 2) shift_to = -1 if random.randint(0, 1) == 0 else 1 gaps_group[random_gaps_group] += shift_to gaps_group[random_gaps_group + 1] += shift_to solution.merge_gaps_groups() if self.remove_full_of_gap_columns: solution.remove_full_of_gaps_columns() # Sanity check: alignment is valid (same length for all sequences) if not solution.is_valid_msa(): raise Exception("Mutated solution is not valid! {0}".format( solution.decode_alignment_as_list_of_pairs())) return solution
def do_mutation(self, solution: MSASolution) -> MSASolution: if random.random() <= self.probability: if solution.number_of_variables >= 1: seq = random.randint(0, solution.number_of_variables - 1) else: seq = 0 gaps_group = solution.gaps_groups[seq] if len(gaps_group) >= 4: random_gaps_group = random.randrange(0, len(gaps_group) - 2, 2) right_is_closest = False if not right_is_closest: to_add = gaps_group[random_gaps_group + 3] - gaps_group[random_gaps_group + 2] + 1 gaps_group[random_gaps_group + 1] += to_add del gaps_group[random_gaps_group + 3] del gaps_group[random_gaps_group + 2] solution.merge_gaps_groups() if self.remove_full_of_gap_columns: solution.remove_full_of_gaps_columns() # Sanity check: alignment is valid (same length for all sequences) if not solution.is_valid_msa(): raise Exception("Mutated solution is not valid! {0}".format( solution.decode_alignment_as_list_of_pairs())) return solution
def evaluate(self, solution: MSASolution) -> MSASolution: solution.remove_full_of_gaps_columns() sequences = solution.decode_alignment_as_list_of_sequences() for i, score in enumerate(self.score_list): solution.objectives[i] = score.compute(sequences) if not score.is_minimization(): solution.objectives[i] = -solution.objectives[i] return solution
def test_should_remove_all_gap_columns_case_d(self): # setup problem = MSA(score_list=[]) problem.identifiers = ['seq1', 'seq2'] problem.number_of_variables = 2 msa = MSASolution(problem, msa=[('seq1', 'AB--CDE-'), ('seq2', 'AB--CD-E')]) msa.remove_full_of_gaps_columns() # check self.assertEqual(['ABCDE-', 'ABCD-E'], msa.decode_alignment_as_list_of_sequences())
def test_should_remove_all_gap_columns_case_b(self): # setup problem = MSA(score_list=[]) problem.identifiers = ['seq1', 'seq2', 'seq3'] problem.number_of_variables = 3 msa = MSASolution(problem, msa=[('seq1', 'AC--T--GC'), ('seq2', 'AC-----AC'), ('seq3', 'A---C--AC')]) msa.remove_full_of_gaps_columns() # check self.assertEqual(['ACTGC', 'AC-AC', 'A-CAC'], msa.decode_alignment_as_list_of_sequences())
def do_mutation(self, solution: MSASolution) -> MSASolution: if random.random() <= self.probability: for i in range(solution.number_of_variables): gaps_group = solution.gaps_groups[i] if len(gaps_group) >= 4: random_gaps_group = random.randrange( 0, len(gaps_group) - 2, 2) right_is_closest = False if not right_is_closest: diff = (gaps_group[random_gaps_group + 3] - gaps_group[random_gaps_group + 2]) - \ (gaps_group[random_gaps_group + 1] - gaps_group[random_gaps_group]) if diff < 0: # diff < 0 means that gaps group 2 is shorter than gaps group 1, thus we need to decrease # the length of the gaps group 1 diff = -1 * diff gaps_group[random_gaps_group + 1] -= diff gaps_group[random_gaps_group + 3] += diff # displace gaps group 2 one position to the left gaps_group[random_gaps_group + 2] -= diff gaps_group[random_gaps_group + 3] -= diff elif diff > 0: # diff > 0 means that gaps group 2 is larger than gaps group 1, thus we need to increase # the length of the gaps group 1 gaps_group[random_gaps_group + 1] += diff gaps_group[random_gaps_group + 3] -= diff # displace gaps group 2 one position to the right gaps_group[random_gaps_group + 2] += diff gaps_group[random_gaps_group + 3] += diff if self.remove_full_of_gap_columns: solution.remove_full_of_gaps_columns() # Sanity check: alignment is valid (same length for all sequences) if not solution.is_valid_msa(): raise Exception("Mutated solution is not valid! {0}".format( solution.decode_alignment_as_list_of_pairs())) return solution
def do_mutation(self, solution: MSASolution) -> MSASolution: if random.random() <= self.probability: length_of_alignment = solution.get_length_of_alignment() for seq_index in range(solution.number_of_variables): point = random.randint(0, length_of_alignment - 1) solution.add_gap_to_sequence_at_index(seq_index, point) if self.remove_full_of_gap_columns: solution.remove_full_of_gaps_columns() # Sanity check: alignment is valid (same length for all sequences) if not solution.is_valid_msa(): raise Exception("Mutated solution is not valid! {0}".format( solution.decode_alignment_as_list_of_pairs())) return solution