mardi 18 février 2020

Making a random string based on random.random and a frequency table

I have this code:

base_distribution = {'A' : 0.345, 'C' : 0.158, 'G' : 0.059, 'T' : 0.437}

def get_random_uniform_sequence(alphabet, k):
    '''It return a random uniform distribuited sequence based in a alphabet
    with a k length (Bernoulli Distribuition)
    alphabet = list or array of strings representing a alphabet
    that the generated string is compsed.
    k = is a integer representing the length of the generated string.
    '''
    return ''.join(rd.choice(alphabet) for _ in range(k))

# choose a random symbol according to a given distribution 
def weighted_choice(distribuition_probs):
    r = random.random()
    # make a choise based on r
    for k, v in distribuition_probs.items():
        if v >= r:
            print(k, v)

# generate a random sequence
def bernoulli_sequence(symbol_distribution, length):
    return ''.join(weighted_choice(symbol_distribution) for i in range(length))

I need to make a random sequence based in a alphabet and k length based in certain probabilities(base_distributuion) using random.random. It is a task in a course I am doing, but I am not sure my weighted_choice functions is doing what is asked for.

I know that numpy random choices would be better, but the task not ask for that. What I am doing wrong?

I would appreciate any tip,

Thank you for your time and attention!

Paulo

PS- I hope I don't have offended anyone here because lately I have notice that I have being ignore around here! 8(




Aucun commentaire:

Enregistrer un commentaire