Exemple #1
0
    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
Exemple #2
0
    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
Exemple #3
0
    def test_should_merge_gaps_groups_case_b(self):
        # setup
        problem = MSA(score_list=[])
        problem.identifiers = ['seq1']
        problem.number_of_variables = 1
        aln_seq = [('seq1', 'ACTGAC')]

        msa = MSASolution(problem, msa=aln_seq)
        msa.gaps_groups[0] = [2, 4, 4, 8, 8, 10]

        self.assertEqual(["AC-----------TGAC"],
                         msa.decode_alignment_as_list_of_sequences())

        # run
        msa.merge_gaps_groups()

        # check
        self.assertEqual([2, 10], msa.gaps_groups[0])
Exemple #4
0
    def test_should_merge_gaps_groups(self):
        # setup
        problem = MSA(score_list=[])
        problem.identifiers = ['seq1', 'seq2']
        problem.number_of_variables = 2
        aln_seq = [('seq1', 'ACTGAC'), ('seq2', 'ATCTC')]

        msa = MSASolution(problem, msa=aln_seq)
        msa.gaps_groups[0] = [2, 4, 4, 5]
        msa.gaps_groups[1] = [2, 4, 5, 8]

        self.assertEqual(["AC-----TGAC", "AT-------CTC"],
                         msa.decode_alignment_as_list_of_sequences())

        # run
        msa.merge_gaps_groups()

        # check
        self.assertEqual([2, 5], msa.gaps_groups[0])
        self.assertEqual([2, 8], msa.gaps_groups[1])