コード例 #1
0
 def __init__(self, user_input):
     self.current_x = 0
     self.current_y = 0
     self.side_lenght = 1
     self.best_protein = [[user_input[0], 2, 0, 0]]
     self.user_input = user_input
     self.stability = Stability()
     self.end = False
     self.current_fold = 0
コード例 #2
0
    def __init__(self, user_input):
        self.user_input = user_input

        # format: [amino, fold, x, y]
        first_amino = [[self.user_input[0], 2, 0, 0]]
        self.stack = []
        self.stack.append(first_amino)

        self.stability = Stability()
        self.possible = PossibleOptions(user_input)
        self.coordinate_update = CoordinateUpdate()

        self.best_score = 1
コード例 #3
0
def finish_protein(score, stability_connections):
    """
    Calculates final stability score for each protein, checks for new best
    scores and prepares data for the visualization of the final best protein
    folds.
    """

    stability = Stability()
    stability_coordinates = stability_connections

    # creates needed lists for the visualisation of the final protein
    stability.stability_score_coordinates(stability_coordinates)
    amino_stability_x = stability.amino_stability_x
    amino_stability_y = stability.amino_stability_y

    return amino_stability_x, amino_stability_y
コード例 #4
0
def set_stability(best_score, final_placement):
    """
    When each potential final protein is finished, stability scores are
    determined and the best score is saved.
    """

    stability = Stability()
    best_protein = []
    best_stability = 0

    done = True
    stability_score, stability_connections = stability.get_stability_score(final_placement)
    stability.stability_score_coordinates(stability_connections)
    amino_stability_x = stability.amino_stability_x
    amino_stability_y = stability.amino_stability_y

    # checks if current score is lower than the current lowest score
    if stability_score < best_score:
        best_score = stability_score
        best_protein = final_placement
        best_stability = stability_score

    return done, final_placement, best_score, best_protein, best_stability, amino_stability_x, amino_stability_y, stability_score
コード例 #5
0
    def run(self):
        """
        Runs depth-first algorithm until all possible protein folds have between
        evaluated; determines best fold.
        """

        # runs the program x times depending on the runamount
        for z in range(self.runamount):

            done = False
            self.best_score = 1

            # format: [amino acid, fold, x, y]
            first_amino = [[self.user_input[0], 2, 0, 0]]
            self.stack = []
            self.stack.append(first_amino)

            self.stability = Stability()
            self.average_score_per_depth = {}

            # average score for every depth starts at 0
            for i in range(1, (len(self.user_input))):
                self.average_score_per_depth[i] = list()

            # restarts the program if an error occurs
            while done == False:

                while self.stack:
                    current_path = self.get_next_path()
                    current_amino = current_path[-1][0]

                    # calculates stability score for each protein
                    if len(current_path) == len(self.user_input):
                        score, stability_connections = self.stability.get_stability_score(
                            current_path)

                    # if the protein is not yet finished
                    else:
                        score, stability_connections = self.stability.get_stability_score(
                            current_path)
                        average_score = self.average_score_thus_far(
                            score, current_path)

                        # keep all branches if score is same or better than best score
                        if score < self.best_score:
                            self.best_score = score
                            self.best_protein = current_path
                            self.add_new_options_to_stack(current_path)
                            self.amino_stability_x, self.amino_stability_y = finish_protein(
                                score, stability_connections)

                        # if current score is worse than average score, prune 70%
                        elif score > average_score:
                            if random.uniform(0, 1) > 0.9:
                                self.add_new_options_to_stack(current_path)

                        # if current score is between the best and average score, prune 30%
                        elif self.best_score < score < average_score:
                            if random.uniform(0, 1) > 0.6:
                                self.add_new_options_to_stack(current_path)

                        # always create new options if score hasn't changed or amino is a P amino
                        else:
                            self.add_new_options_to_stack(current_path)

                if self.best_score < self.very_best_score:
                    self.very_best_score = self.best_score
                    self.very_best_protein = self.best_protein

                if z == 99 or z == 199 or z == 299 or z == 399 or z == 499 or z == 599 or z == 699 or z == 799 or z == 899 or z == 999:
                    print("Score: ", self.very_best_score)

                done = True
コード例 #6
0
class BranchBound:
    """
    Branch and Bound algorithm that folds a given protein
    3 ^ (length of user input) times to find the best possible stability score.
    """
    def __init__(self, user_input, runamount):
        self.user_input = user_input
        self.runamount = runamount
        self.very_best_score = 1

    def average_score_thus_far(self, score, current_path):
        """
        Returns the updated average score of given depth.
        """

        # calculates new average
        self.average_score_per_depth[len(current_path)].append(score)
        sum = 0
        for old_score in self.average_score_per_depth[len(current_path)]:
            sum += old_score

        average_score = sum / len(
            self.average_score_per_depth[len(current_path)])
        return average_score

    def get_next_path(self):
        """
        Gets the current path out of the stack of paths: the (partial) protein with
        corresponding information (fold and x,y coordinates).
        """

        current_path = self.stack.pop()
        return current_path

    def add_new_options_to_stack(self, current_path):
        """
        Determines possible options for every amino acid and appends them to stack.
        """

        possible = PossibleOptions(self.user_input)
        coordinate_update = CoordinateUpdate()

        # update coordinates for next amino based on current fold
        current_x, current_y = coordinate_update.update_coordinates(
            current_path)

        possible.define_folds(current_path)
        possible.define_coordinates(current_x, current_y)
        final_possible_options = possible.check_empty()

        for option in final_possible_options:
            new_path = copy.deepcopy(current_path)
            new_path.append(option)

            if new_path not in self.stack:
                self.stack.append(new_path)

    def run(self):
        """
        Runs depth-first algorithm until all possible protein folds have between
        evaluated; determines best fold.
        """

        # runs the program x times depending on the runamount
        for z in range(self.runamount):

            done = False
            self.best_score = 1

            # format: [amino acid, fold, x, y]
            first_amino = [[self.user_input[0], 2, 0, 0]]
            self.stack = []
            self.stack.append(first_amino)

            self.stability = Stability()
            self.average_score_per_depth = {}

            # average score for every depth starts at 0
            for i in range(1, (len(self.user_input))):
                self.average_score_per_depth[i] = list()

            # restarts the program if an error occurs
            while done == False:

                while self.stack:
                    current_path = self.get_next_path()
                    current_amino = current_path[-1][0]

                    # calculates stability score for each protein
                    if len(current_path) == len(self.user_input):
                        score, stability_connections = self.stability.get_stability_score(
                            current_path)

                    # if the protein is not yet finished
                    else:
                        score, stability_connections = self.stability.get_stability_score(
                            current_path)
                        average_score = self.average_score_thus_far(
                            score, current_path)

                        # keep all branches if score is same or better than best score
                        if score < self.best_score:
                            self.best_score = score
                            self.best_protein = current_path
                            self.add_new_options_to_stack(current_path)
                            self.amino_stability_x, self.amino_stability_y = finish_protein(
                                score, stability_connections)

                        # if current score is worse than average score, prune 70%
                        elif score > average_score:
                            if random.uniform(0, 1) > 0.9:
                                self.add_new_options_to_stack(current_path)

                        # if current score is between the best and average score, prune 30%
                        elif self.best_score < score < average_score:
                            if random.uniform(0, 1) > 0.6:
                                self.add_new_options_to_stack(current_path)

                        # always create new options if score hasn't changed or amino is a P amino
                        else:
                            self.add_new_options_to_stack(current_path)

                if self.best_score < self.very_best_score:
                    self.very_best_score = self.best_score
                    self.very_best_protein = self.best_protein

                if z == 99 or z == 199 or z == 299 or z == 399 or z == 499 or z == 599 or z == 699 or z == 799 or z == 899 or z == 999:
                    print("Score: ", self.very_best_score)

                done = True
コード例 #7
0
    def run(self):
        """
        Runs three fold one step
        """

        self.best_score = 1

        # runs the program x times depending on the runamount
        for z in range(self.runamount):

            done = False

            # restarts the program if an error occurs
            while done == False:

                # sets first fold and adds to final
                protein = Protein(self.user_input)

                # splits protein sequence into chunks of three amino acids
                user_input_split = split_protein(self.user_input)

                # updates x,y coordinates based on most recently determined fold
                coordinate_update = CoordinateUpdate()
                coordinate_update.update_coordinates(protein.final_placement)

                for i in range(len(user_input_split)):

                    current_x, current_y = coordinate_update.update_coordinates(
                        protein.final_placement)
                    current_fold = protein.final_placement[-1][1]
                    current_amino = self.user_input[len(
                        protein.final_placement)]
                    best_option = three_fold(protein.final_placement,
                                             user_input_split, current_fold,
                                             current_x, current_y,
                                             current_amino, i)

                    # checks if an error has occured
                    if best_option == False:
                        break

                    placement = [
                        current_amino, best_option[0], current_x, current_y
                    ]
                    protein.add_amino_info(placement)

                    # checks if the end of protein hasn't been reached
                    if current_fold != 0:
                        current_x, current_y = coordinate_update.update_coordinates(
                            protein.final_placement)

                    # checks if last amino of sequence is reached
                    if len(protein.final_placement) == (len(self.user_input) -
                                                        1):
                        protein.add_last_amino_of_chunk_without_score(
                            current_x, current_y, self.user_input)

                    # end of protein has been reached
                    if protein.final_placement[-1][1] == 0:
                        done = True
                        self.stability = Stability()

                        stability_score, stability_connections = self.stability.get_stability_score(
                            protein.final_placement)
                        self.stability_coordinates = stability_connections
                        self.stability.stability_score_coordinates(
                            self.stability_coordinates)
                        amino_stability_x = self.stability.amino_stability_x
                        amino_stability_y = self.stability.amino_stability_y

                        # checks if current score is lower than the current lowest score
                        if stability_score < self.best_score:
                            self.best_score = stability_score
                            self.best_protein = protein.final_placement
                            self.best_stability = stability_score
                            self.amino_stability_x = amino_stability_x
                            self.amino_stability_y = amino_stability_y
コード例 #8
0
class Force:
    def __init__(self, user_input):
        self.current_x = 0
        self.current_y = 0
        self.side_lenght = 1
        self.best_protein = [[user_input[0], 2, 0, 0]]
        self.user_input = user_input
        self.stability = Stability()
        self.end = False
        self.current_fold = 0

    def placing(self):
        """
        Places de amino's on the right place by checking if the side length is
        odd or even.
        """

        while len(self.user_input) > len(self.best_protein):

            # checks step
            for i in range(2):
                for j in range(self.side_lenght):
                    if self.end == False:
                        # checks if is odd number
                        if (self.side_lenght % 2) != 0:
                            self.odd(i)

                        # checks if is even number
                        elif (self.side_lenght % 2) == 0:

                            # ensure the last amino isn't appended twice
                            try:
                                self.even(i)
                            except IndexError:
                                pass
            self.side_lenght += 1

    def odd(self, i):
        """
        Determines the coordinates and the fold of the amino.
        """

        # checks if is second step
        if i == 1:
            self.new_x = self.current_x + 1
            self.new_y = self.current_y

            # check if is last amino
            if len(self.user_input) - 1 == len(self.best_protein):
                self.current_fold = 0
                self.end = True

        # checks if is first step
        elif i == 0:
            self.new_y = self.current_y + 1
            self.new_x = self.current_x

            # check if is last amino
            if len(self.user_input) - 1 == len(self.best_protein):
                self.current_fold = 0
                self.end = True

        self.current_x = self.new_x
        self.current_y = self.new_y

        try:
            self.current_amino = self.user_input[len(self.best_protein)]
        except IndexError:
            pass

        self.best_protein.append(
            [self.current_amino, self.current_fold, self.new_x, self.new_y])

        return self.best_protein, self.current_x, self.current_y

    def even(self, i):
        """
        Determines the coordinates.
        """

        # checks if is first step
        if i == 0:
            self.new_x = self.current_x
            self.new_y = self.current_y - 1

            # check if is last amino
            if len(self.user_input) - 1 == len(self.best_protein):
                self.current_fold = 0
                self.end = True

        # checks if is second step
        if i == 1:
            self.new_y = self.current_y
            self.new_x = self.current_x - 1

            # check if is last amino
            if len(self.user_input) - 1 == len(self.best_protein):
                self.current_fold = 0
                self.end = True

        self.current_x = self.new_x
        self.current_y = self.new_y

        try:
            self.current_amino = self.user_input[len(self.best_protein)]
        except IndexError:
            pass

        self.best_protein.append(
            [self.current_amino, self.current_fold, self.new_x, self.new_y])

        return self.best_protein, self.current_x, self.current_y

    def fold(self):
        """
        Determines the fold.
        """

        for i in range(len(self.best_protein) - 1):
            j = i + 1

            # checks next amino and determines fold accordingly
            if self.best_protein[i][2] + 1 == self.best_protein[j][
                    2] and self.best_protein[i][3] == self.best_protein[j][3]:
                self.fold = 1
                self.best_protein[i][1] = self.fold

            elif self.best_protein[i][2] - 1 == self.best_protein[j][
                    2] and self.best_protein[i][3] == self.best_protein[j][3]:
                self.fold = -1
                self.best_protein[i][1] = self.fold

            elif self.best_protein[i][2] == self.best_protein[j][
                    2] and self.best_protein[i][3] + 1 == self.best_protein[j][
                        3]:
                self.fold = 2
                self.best_protein[i][1] = self.fold

            elif self.best_protein[i][2] == self.best_protein[j][
                    2] and self.best_protein[i][3] - 1 == self.best_protein[j][
                        3]:
                self.fold = -2
                self.best_protein[i][1] = self.fold

    def run(self):
        """
        Runs forcing algorithm until all possible protein folds have between
        evaluated; determines best fold.
        """

        self.placing()
        self.fold()

        self.best_score, stability_connections = self.stability.get_stability_score(
            self.best_protein)
        self.stability.stability_score_coordinates(stability_connections)
        self.amino_stability_x = self.stability.amino_stability_x
        self.amino_stability_y = self.stability.amino_stability_y
コード例 #9
0
class DepthFirst:
    """
    Depth first search algorithm that folds a given protein
    3 ^ (length of user input) times to find the best possible stability score.
    """
    def __init__(self, user_input):
        self.user_input = user_input

        # format: [amino, fold, x, y]
        first_amino = [[self.user_input[0], 2, 0, 0]]
        self.stack = []
        self.stack.append(first_amino)

        self.stability = Stability()
        self.possible = PossibleOptions(user_input)
        self.coordinate_update = CoordinateUpdate()

        self.best_score = 1

    def get_next_path(self):
        """
        Gets the current path out of the stack of paths: the (partial) protein with
        corresponding information (fold and x,y coordinates).
        """

        current_path = self.stack.pop()
        return current_path

    def add_new_options_to_stack(self, current_path):
        """
        Determines possible options for every amino acid and appends them to stack.
        """

        # updates coordinates for next amino based on current fold
        current_x, current_y = self.coordinate_update.update_coordinates(
            current_path)

        # finds possible options
        self.possible.define_folds(current_path)
        self.possible.define_coordinates(current_x, current_y)
        final_possible_options = self.possible.check_empty()

        # adds new option to stack if not appended already
        for option in final_possible_options:
            new_option = copy.deepcopy(current_path)
            new_option.append(option)

            if new_option not in self.stack:
                self.stack.append(new_option)

    def run(self):
        """
        Runs depth-first algorithm until all possible protein folds have between
        evaluated; determines best fold.
        """

        while self.stack:
            current_path = self.get_next_path()

            # calculates stability score for each protein
            if len(current_path) == len(self.user_input):
                score, stability_connections = self.stability.get_stability_score(
                    current_path)

                # updates current best protein option
                if score < self.best_score:
                    self.best_score = score
                    self.best_protein = current_path
                    self.amino_stability_x, self.amino_stability_y = finish_protein(
                        score, stability_connections)

            # creates new options if protein is not yet finished
            else:
                self.add_new_options_to_stack(current_path)