Esempio n. 1
0
def run(proti):

    # create the initial straight string
    pos_x = [i for i in range(proti.length)]
    pos_y = [0] * proti.length
    pos_z = [0] * proti.length

    # number of iterations, higher N is better result
    N = 1000
    rotation_counter = 0

    # lists to keep track of the scores and best configuration
    lowest_score = 0
    best_x = []
    best_y = []
    best_z = []
    scores = []

    # probability functions depens on temperature and the boltzmann constant,
    # can be set to their actual values if you want to be physically responsible
    temperature = 1
    boltzmann = 1

    # loop that keeps folding the protein
    while rotation_counter < N:

        # a copy is made in case the fold is invalid or unfavourable
        log_pos_x = copy.deepcopy(pos_x)
        log_pos_y = copy.deepcopy(pos_y)
        log_pos_z = copy.deepcopy(pos_z)

        # protein is folded randomly
        rotating_amino = random.randint(0, proti.length - 1)
        random_rotation_xyz(list_x=pos_x,
                            list_y=pos_y,
                            list_z=pos_z,
                            n=rotating_amino,
                            proti=proti)

        # check whether the protein has not folded onto itself
        if double(pos_x, pos_y, pos_z):
            # if it is folded wrongly, restore to the previous configuration
            pos_x = log_pos_x
            pos_y = log_pos_y
            pos_z = log_pos_z
            continue

        # calculate the scores of the old and new structure
        old_score = score_it(proti, log_pos_x, log_pos_y, log_pos_z)
        new_score = score_it(proti, pos_x, pos_y, pos_z)

        # keep track of each score
        scores.append(old_score)

        # if a score beats the old one, remember that structure
        if new_score < lowest_score:
            best_x = copy.deepcopy(pos_x)
            best_y = copy.deepcopy(pos_y)
            best_z = copy.deepcopy(pos_z)
            lowest_score = copy.deepcopy(new_score)

        # probability function to determine whether a fold will be 'accepted'
        p = math.exp(-(new_score - old_score) / (temperature * boltzmann))

        # the treshhold for acceptance varies and is randomly determined
        treshhold = random.random()
        if p < treshhold:
            pos_x = log_pos_x
            pos_y = log_pos_y
            pos_z = log_pos_z

        rotation_counter += 1

        # print statement for time indication in long calculations
        if rotation_counter % 1000 == 0:
            print(f'{rotation_counter / N * 100}%')

    # the best structure is copied to a csv file and shown in a graph
    output(proti, lowest_score, best_x, best_y, best_z)
    plot(proti,
         lowest_score,
         best_x,
         best_y,
         'Score after rotation',
         'Rotation',
         'Score',
         best_z,
         scores=scores)
Esempio n. 2
0
def run(proti):

    # start timing code
    start = timeit.default_timer()

    # create the initial straight string
    pos_x = [i for i in range(proti.length)]
    pos_y = [0] * proti.length

    rotation_counter = 0
    N = 5000

    # initialize progress bar
    bar = Bar('Progress', max=N/1000)

    # lists to keep track of the scores of each rotation and remember the one
    # with the best score
    lowest_score = 0
    best_x = []
    best_y = []
    scores = []

    # probability functions depens on temperature and the boltzmann constant, 
    # can be set to their actual values if you want to be physically responsible
    boltzmann = 1
    prob = []
    
    # loop that keeps folding the protein
    while rotation_counter < N:

        _max = 10
        scale = N/20
        center = 9 * N / 40
        temperature = _max / (1 + math.exp((rotation_counter - center) / scale)) + 0.5

        # a copy is made in case the fold is invalid or unfavourable
        log_pos_x = copy.deepcopy(pos_x)
        log_pos_y = copy.deepcopy(pos_y)

        # protein is folded randomly
        rotating_amino = random.randint(0, proti.length - 1)
        random_rotation_xy(list_x=pos_x, list_y=pos_y, n=rotating_amino, proti=proti)
        
        # check whether the protein has not folded onto itself
        if double(pos_x, pos_y):
            # if it is folded wrongly, restore to the previous configuration
            pos_x = log_pos_x
            pos_y = log_pos_y
            continue

        # calculate the scores of the old and new structure
        old_score = score_it(proti=proti, list_x=log_pos_x, list_y=log_pos_y)
        new_score = score_it(proti=proti, list_x=pos_x, list_y=pos_y)
        
        # keep track of each score
        scores.append(old_score)

        # if a score beats the old one, remember that structure 
        if new_score < lowest_score:
            best_x = copy.deepcopy(pos_x)
            best_y = copy.deepcopy(pos_y)
            lowest_score = copy.deepcopy(new_score)

        # probability function to determine whether a fold will be 'accepted'
        p = math.exp(-(new_score - old_score)/(temperature * boltzmann))

        # the treshhold for acceptance varies and is randomly determined
        treshhold = random.random()
        if p < treshhold:
            pos_x = log_pos_x
            pos_y = log_pos_y

        rotation_counter += 1
        
        # print statement for time indication in long calculations
        if rotation_counter % 1000 == 0:
            bar.next()
            prob.append(p)

    bar.finish()
    stop = timeit.default_timer()
    print('Runtime:', stop - start, 'seconds')

    # the best structure is copied to a csv file and shown in a graph
    output(proti=proti, score=lowest_score, list_x=best_x, list_y=best_y)
    plot(proti, lowest_score, best_x, best_y, 'Score after rotation', 'Rotation',
        'Score', scores=scores)
Esempio n. 3
0
def run(proti):

    # start timing the run of the code
    start = timeit.default_timer()

    # initialize the protein in a straight configuration
    x = [i for i in range(proti.length)]
    y = [0] * proti.length

    # high number of iterations for optimising result
    iterations = 1000
    rotations = 0

    # initialize progress bar
    bar = Bar('Progress', max=iterations / 1000)

    # list to keep track of best configuration and scores
    lowest_score = 0
    best_x = []
    best_y = []
    scores = []

    # set start temperature for annealing
    start_temp = 100

    # at the start no local optimum found
    local_optimum = False
    local_optimum_rotation = 0

    while rotations < iterations:

        # remember the previous configuration
        backup_x = copy.deepcopy(x)
        backup_y = copy.deepcopy(y)

        # remember the previous score
        old_score = score_it(list_x=backup_x, list_y=backup_y, proti=proti)
        scores.append(old_score)

        # fold protein at a random amino
        rotating_amino = random.randint(0, proti.length - 1)
        random_rotation_xy(list_x=x, list_y=y, n=rotating_amino, proti=proti)

        # if protein folded into itself restore and go back
        if double(list_x=x, list_y=y):
            x = backup_x
            y = backup_y
            continue

        # get the score of the current configuration
        new_score = score_it(list_x=x, list_y=y, proti=proti)

        if rotations % 1000 == 0 and new_score == old_score:
            local_optimum = True

        if local_optimum == True:
            local_optimum_rotation = rotations
            local_optimum = False

        # drop temperature gradually
        temperature = start_temp * (0.997
                                    **(rotations - local_optimum_rotation))
        acceptance_chance = 2**(-(new_score - old_score) / temperature)

        treshhold = random.random()

        # if new score is worse and it can't be accepted restore backup
        if new_score > old_score and acceptance_chance > treshhold:
            x = backup_x
            y = backup_y
            new_score = old_score

        # check if a lower score is found and remember configuration
        if new_score < lowest_score:
            best_x = copy.deepcopy(x)
            best_y = copy.deepcopy(y)
            lowest_score = copy.deepcopy(new_score)

        rotations += 1

        # continue the progress bar every thousand configurations
        if rotations % 1000 == 0:
            bar.next()

    # finish the progress bar and th timer and show information
    bar.finish()
    stop = timeit.default_timer()
    print('Runtime', stop - start, 'seconds')

    # render the output and plot the figure
    output(list_x=best_x, list_y=best_y, score=lowest_score, proti=proti)
    plot(proti,
         lowest_score,
         best_x,
         best_y,
         'Score after rotation',
         'Rotation',
         'Score',
         scores=scores)

    return stop - start, lowest_score, [best_x, best_y]
Esempio n. 4
0
def run(proti):

    # start timing script run time
    start = timeit.default_timer()

    # pruning chance, higher values mean a faster program but the results may not be as good
    p1 = 0.99
    p2 = 0.98

    # initialize progress bar
    bar = Bar('Progress', max=proti.length)
    bar.next()
    bar.next()
    k = 0
    amino_time = []

    # specifications for breadth first tree building
    depth = proti.length - 2
    q = queue.Queue()
    q.put(('', 0))
    final_configurations = []

    # keep track of scores per substring
    lowest_score_k = {}
    all_scores_k = {}
    lowest_score = 0

    # set inital values
    for i in range(proti.length + 1):
        lowest_score_k[i] = 0
        all_scores_k[i] = [0]

    amino_start = timeit.default_timer()
    # create a breadth first tree
    while not q.empty():

        state = q.get()

        # if all aminos are placed, put the string in a list
        state_x, state_y = direction_to_xy(state[0])
        if len(state[0]) == depth and not double(state_x, state_y):
            final_configurations.append(state)

        if len(state[0]) < depth:

            for i in ['L', 'R', 'S']:

                child = copy.deepcopy(state)

                temp_list = list(child)
                temp_list[0] += i
                child = tuple(temp_list)

                child_x, child_y = direction_to_xy(child[0])
                if double(child_x, child_y):
                    continue

                if len(child[0]) + 1 > k:
                    amino_stop = timeit.default_timer()
                    amino_time.append(amino_stop - amino_start)
                    bar.next()
                    amino_start = timeit.default_timer()

                k = len(child[0]) + 1

                # P's are always placed, rest have some conditions
                if not proti.listed[k] == 'P':
                    score = child[1] + score_con(child_x, child_y, proti)

                    possible_score = possible_score_func_dee(proti.listed[k+1:], \
                                                         score, proti.min_score, proti)

                    if score + possible_score > lowest_score:
                        continue

                    # random number between 0 and 1
                    r = random.random()

                    average_score_k = sum(all_scores_k[k]) / len(
                        all_scores_k[k])

                    # conditions for pruning
                    if score > average_score_k and r < p1:
                        continue
                    elif (average_score_k >= score and score > lowest_score_k[k])\
                         and r < p2:
                        continue

                    temp_list = list(child)
                    temp_list[1] = score
                    child = tuple(temp_list)

                    # add to tree
                    q.put(child)

                    all_scores_k[k].append(score)

                    if score < lowest_score_k[k]:
                        lowest_score_k[k] = copy.deepcopy(score)

                    if score < lowest_score:
                        lowest_score = copy.deepcopy(score)

                else:
                    q.put(child)

    if len(final_configurations) == 0:
        bar.finish()
        print('No conformations found.')
        return

    lowest_score = 0

    # weed out the best configuration from the remaining strings
    for c in final_configurations:
        if c[1] < lowest_score:
            best_config = copy.deepcopy(c[0])
            lowest_score = copy.deepcopy(c[1])

    best_x, best_y = direction_to_xy(best_config)
    # plot the result
    stop = timeit.default_timer()
    total_time = stop - start
    bar.finish()
    print(f'Length: {proti.length}')
    print(f'Score: {lowest_score}')
    print(f'Time: {total_time}')
    print(f'Conformation: {best_config}')
    plot(proti,
         lowest_score,
         best_x,
         best_y,
         'Time per amino placement',
         'Amino',
         'Time[s]',
         scores=amino_time)
    return total_time, lowest_score, best_config
Esempio n. 5
0
def run(proti):
    # start timing the run of the code
    start = timeit.default_timer()

    # initialize the protein in a straight configuration
    x = [i for i in range(proti.length)]
    y = [0] * proti.length
    z = [0] * proti.length

    # high number of iterations for optimising result
    iterations = 50000
    rotations = 0

    # initialize progress bar
    bar = Bar('Progress', max=iterations / 1000)

    # list to keep track of best configuration and scores
    lowest_score = 0
    best_x = []
    best_y = []
    best_z = []
    scores = []

    # fold protein for number of iterations
    while rotations < iterations:

        # remember the previous configuration
        backup_x = copy.deepcopy(x)
        backup_y = copy.deepcopy(y)
        backup_z = copy.deepcopy(z)

        # remember the previous score
        old_score = score_it(list_x=backup_x,
                             list_y=backup_y,
                             list_z=backup_z,
                             proti=proti)
        scores.append(old_score)

        # fold protein at a random amino
        rotating_amino = random.randint(0, proti.length - 1)
        random_rotation_xyz(list_x=x,
                            list_y=y,
                            list_z=z,
                            n=rotating_amino,
                            proti=proti)

        # if protein folded into itself restore and go back
        if double(list_x=x, list_y=y):
            x = backup_x
            y = backup_y
            z = backup_z
            continue

        # get the score of the current configuration
        new_score = score_it(list_x=x, list_y=y, list_z=z, proti=proti)

        # if the new score is worse set configuration back
        if new_score > old_score:
            x = backup_x
            y = backup_y
            z = backup_z
            new_score = old_score

        # check if a lower score has been found and remember
        if new_score < lowest_score:
            best_x = copy.deepcopy(x)
            best_y = copy.deepcopy(y)
            best_z = copy.deepcopy(z)
            lowest_score = copy.deepcopy(new_score)

        rotations += 1

        # continue the progress bar every thousand configuration
        if rotations % 1000 == 0:
            bar.next()

    # finish the progress bar and the timer and show information
    bar.finish()
    stop = timeit.default_timer()
    print('Runtime:', stop - start, 'seconds')

    # render the output and plot the figure
    output(list_x=best_x,
           list_y=best_y,
           list_z=best_z,
           score=lowest_score,
           proti=proti)
    plot(proti,
         lowest_score,
         best_x,
         best_y,
         'Score after rotation',
         'Rotation',
         'Score',
         best_z,
         scores=scores)
Esempio n. 6
0
def run(proti):
    # start timer
    start = timeit.default_timer()

    bar  = Bar('Progress', max=proti.length)
    bar.next()
    bar.next()
    k = 0
    amino_time = []

    # specifications for depth first tree building
    depth = proti.length - 2
    q = queue.Queue()
    q.put('')
    final_configurations = []
    
    # keep track of scores per substring
    lowest_score_k = {}
    all_scores_k = {}
    lowest_score = 0
    
    # (0,1) probabilities of pruning a path, lower is more exact but less fast
    p1 = 1
    p2 = 1

    # set inital values
    for i in range(proti.length + 1):
        lowest_score_k[i] = 0
        all_scores_k[i] = [0]

    amino_start = timeit.default_timer()
    # create a breadth first tree
    while not q.empty():

        state = q.get()
        # if all aminos are placed, put the string in a list
        state_x, state_y, state_z = directions(state)
        if len(state) == depth and not double(state_x, state_y, state_z):
            final_configurations.append(state)
      
        if len(state) < depth:
            for i in ['L', 'R', 'S', 'U', 'D']:

                # substring
                child = copy.deepcopy(state) 

                # string after potentialy placing the next amino
                child += i 
                
                child_x, child_y, child_z = directions(child)
                # discard the string folding into themselves
                if double(child_x, child_y, child_z):
                    continue
                
                if len(child) + 1 > k:
                    amino_stop = timeit.default_timer()
                    amino_time.append(amino_stop-amino_start)
                    bar.next()
                    amino_start = timeit.default_timer()

                # identify how for into the string it is
                k = len(child) + 1
                
                # P's are always placed, rest have some conditions
                if not proti.listed[k] == 'P':

                    # score if placed 
                    score = score_it(proti, child_x, child_y, child_z)

                    # min score to get from remaining aminos
                    possible_score = possible_score_func_dee(proti.listed[k+1:],\
                                                         score, proti.min_score, proti)

                    if score + possible_score > lowest_score:
                        continue

                    # random number between 0 and 1
                    r = random.random()

                    # avergage of all strings of the same length
                    average_score_k = sum(all_scores_k[k]) / len(all_scores_k[k])

                    # conditions for pruning
                    if score > average_score_k and r < p1:
                        continue
                    elif (average_score_k >= score and score > lowest_score_k[k])\
                         and r < p2:
                        continue
                    
                    # add to tree
                    q.put(child) 
                    all_scores_k[k].append(score)

                    if score < lowest_score_k[k]:
                        lowest_score_k[k] = copy.deepcopy(score)
                    
                    if score < lowest_score:
                        lowest_score = copy.deepcopy(score)
                        
                else:
                    q.put(child) 

    lowest_score = 0

    # weed out the best configuration from the remaining strings
    for c in final_configurations:
        c_x, c_y, c_z = directions(c)
        if score_it(proti, c_x, c_y, c_z) < lowest_score:
            best_config = copy.deepcopy(c)
            lowest_score = copy.deepcopy(score_it(proti, c_x, c_y, c_z))

    best_x, best_y, best_z = directions(best_config)

    bar.finish()
    # plot the result
    stop = timeit.default_timer()
    print(f'Length: {proti.length}')
    print(f'Score: {lowest_score}')
    print(f'Time: {stop - start}')
    print(f'Conformation: {best_config}')
    plot(proti, lowest_score, best_x, best_y,'Time per amino placement', 'Amino', 'Time[s]', best_z, scores=amino_time)
Esempio n. 7
0
def run(proti):
    length = proti.length
    strings_created = 0
    lowest_score = 0
    population = 100
    N = 300

    routes = []
    best_route = []
    best_yet = []

    start = timeit.default_timer()
    bar = Bar('Progress', max=N / 100)

    # create a swarm of protein strings that dont fold into themselves
    while strings_created < population:
        route = []

        for i in range(length - 2):
            route.append(['S', 'L', 'R'][random.randint(0, 2)])

        route_x, route_y = direction_to_xy(route)
        if not double(route_x, route_y):
            routes.append(route)
            strings_created += 1

    for k in range(N):
        # determine which route has the lowest score
        for r in routes:

            r_x, r_y = direction_to_xy(r)

            while double(r_x, r_y):
                route = []

                for i in range(length - 2):
                    route.append(['S', 'L', 'R'][random.randint(0, 2)])

                route_x, route_y = direction_to_xy(route)
                if not double(route_x, route_y):
                    r = route

            r_x, r_y = direction_to_xy(r)
            score = score_it(proti, r_x, r_y)

            if score <= lowest_score:
                lowest_score = copy.deepcopy(score)
                best_route = copy.deepcopy(r)
                best_yet.append(score)
        # bend the rest so as to be more like the best score
        for r in routes:

            log_r = copy.deepcopy(r)
            r_x, r_y = direction_to_xy(r)
            b_r_x, b_r_y = direction_to_xy(best_route)
            before = similarities(r_x, r_y, b_r_x, b_r_y)
            invalid = True
            for i in range(10):
                while invalid:
                    r[random.randint(0, len(r) - 1)] = \
                        ['S', 'L', 'R'][random.randint(0,2)]
                    r_x, r_y = direction_to_xy(r)
                    if not double(r_x, r_y):
                        invalid = False

            r_x, r_y = direction_to_xy(r)
            after = similarities(r_x, r_y, b_r_x, b_r_y)

            if after < before:
                r = log_r

        if k % 100 == 0:
            bar.next()

    pos_x, pos_y = direction_to_xy(best_route)
    score = score_it(proti, pos_x, pos_y)
    bar.finish()
    stop = timeit.default_timer()
    runtime = stop - start
    route_string = ''.join(best_route)
    print(f'Length: {proti.length}')
    print(f'Score: {score}')
    print(f'Time: {runtime}')
    print(f'Conformation: {route_string}')
    plot(proti,
         score,
         pos_x,
         pos_y,
         'Lowest score',
         'Iteration',
         'Score',
         scores=best_yet)

    return runtime, score, route_string