Exemplo n.º 1
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)
Exemplo n.º 2
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]
Exemplo n.º 3
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)
Exemplo n.º 4
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)