def test_should_create_new_gaps_group(self): # setup problem = MSA(score_list=[]) problem.identifiers = ['seq1'] problem.number_of_variables = 1 msa_1 = MSASolution(problem, msa=[('seq1', 'A-')]) msa_2 = MSASolution(problem, msa=[('seq1', '-A')]) msa_3 = MSASolution(problem, msa=[('seq1', 'A-C')]) msa_4 = MSASolution(problem, msa=[('seq1', 'AAA')]) # run msa_1.add_gap_to_sequence_at_index(seq_index=0, gap_position=0) msa_2.add_gap_to_sequence_at_index(seq_index=0, gap_position=2) msa_3.add_gap_to_sequence_at_index(seq_index=0, gap_position=3) msa_4.add_gap_to_sequence_at_index(seq_index=0, gap_position=1) # check self.assertEqual([('seq1', '-A-')], msa_1.decode_alignment_as_list_of_pairs()) self.assertEqual([('seq1', '-A-')], msa_2.decode_alignment_as_list_of_pairs()) self.assertEqual([('seq1', 'A-C-')], msa_3.decode_alignment_as_list_of_pairs()) self.assertEqual([('seq1', 'A-AA')], msa_4.decode_alignment_as_list_of_pairs())
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 test_should_return_alignment_as_list_of_pairs(self): # setup problem = MSA(score_list=[]) problem.identifiers = ['seq1', 'seq2', 'seq3'] problem.number_of_variables = 3 msa = MSASolution(problem, msa=[('seq1', 'AC---TGAC'), ('seq2', 'AT--CT--C'), ('seq3', 'AAC---TGC')]) # check self.assertEqual([('seq1', 'AC---TGAC'), ('seq2', 'AT--CT--C'), ('seq3', 'AAC---TGC')], msa.decode_alignment_as_list_of_pairs())
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