Good evening! Given the dictionary with the probabilities of finding each letter in the first and second place:

profile = {'A': [0.5, 0.1], 'C': [0.3, 0.2], 'G': [0.2, 0.4], 'T': [0.0, 0.3]} 

I need, based on their probabilities, to randomly choose a dimer ('AA', 'AC', etc.)

I can get randomly selected non-integer number:

 random.uniform(0, 1) 

Then for the first letter of a dimer, I can write the following:

 0 to 0.5 —-> 'A', 0.5 to 0.8 —-> 'C', 0.8 to 1 —-> 'G', 

Considering that I cannot use the 'ACTG' keys directly, because the keys may have other names, I need to write a function that selects a random dimer based on the probabilities of each letter on each of the two places. Any ideas? I have been sitting on this task for quite a long time.

    2 answers 2

    The choice of the first and second letters is completely independent:

     letters = list(profile) dimer = ''.join([letters[weighted_choice(weights)] for weights in zip(*profile.values())]) 

    where zip(*matrix) transposes the matrix and weighed_choice(weights) selects a random index according to the given weights for the current position. See the weighted_choice () discussion in the related question .

       from random import random def dimer(profile): # Перевожу данные в структуру с фиксированным порядком элементов data = [(k, v[0], v[1]) for k, v in profile.items()] # Первый символ rnd1 = random() acc1 = 0 for d in data: acc1 += d[1] if acc1 > rnd1: first = d[0] break # Второй символ rnd2 = random() acc2 = 0 for d in data: acc2 += d[2] if acc2 > rnd2: second = d[0] break return first + second 

      The function works correctly if the sum of the probabilities in the source data is one.

      • This is not exactly what the vehicle needs. He takes into account the order of the elements of the dictionary. And the dictionary in Python is unordered. The order of the elements in the data will be different for different launches. - user194374
      • @kff, no, his order is preserved only at the stage where he builds the interval of the probability distribution. After the random choice has been made, the order has ceased to influence anything. - Xander
      • Yes, really ... Something I was stupid ... - user194374