SilkyTuba
SilkyTuba

Reputation: 9

All possible combinations of options for a set of sequential decisions

I am trying to solve a problem in a programmatic fashion, having only been able to solve it by hard coding so far.

Say we have several sequential decisions to make, each with 2-3 possible options. How can I produce all possible option combinations for this sequential set of decisions?

Specifically the problem looks like this, we have several insertion sites along a length of DNA, where at these insertion sites there are 2-3 possible sequences that can be inserted. Using list comprehension (and specifying the number of place holder variables), I am able to produce all the possible combinations, code below. B1, B2, B3 are lists containing the possible sequences that can be inserted.

B3 = ['AAC','AGC']

B2 = ['AAA','AGA','GGA']

B1 = ['ACG','AGC']


LIST = [[a,b,c] for a in B3 for b in B2 for c in B1]
print(LIST)
print(len(LIST))

yields:

[['AAC', 'AAA', 'ACG'], ['AAC', 'AAA', 'AGC'], ['AAC', 'AGA', 'ACG'], ['AAC', 'AGA', 'AGC'], ['AAC', 'GGA', 'ACG'], ['AAC', 'GGA', 'AGC'], ['AGC', 'AAA', 'ACG'], ['AGC', 'AAA', 'AGC'], ['AGC', 'AGA', 'ACG'], ['AGC', 'AGA', 'AGC'], ['AGC', 'GGA', 'ACG'], ['AGC', 'GGA', 'AGC']]
12

However this relies on me hard coding how many insertion sites I will need. Which is unfeasible if we have say 15 insertion sites along one DNA segment. How can I do this in a more programmatic way?

Upvotes: 1

Views: 41

Answers (2)

Pixel_teK
Pixel_teK

Reputation: 803

If you want to code it yourself, you could use some dynamic programming concepts as in the example :

B1 = ['ACG','AGC']
B2 = ['AAA','AGA','GGA']
B3 = ['AAC','AGC']
B4 = ['XX', 'YYY']

def dynamic_program(*args) :
    all_combinations = []

    def generate_combinations(*args, combination=[]):
        if len(args) == 0 :
            all_combinations.append(combination)
            return

        B_i = args[0]
        for b_i in B_i:
            generate_combinations(*args[1:], combination=combination+[b_i])
        
    generate_combinations(*args)
    return all_combinations

print(*dynamic_program(B1, B2, B3, B4), sep='\n')

Outputs :

['ACG', 'AAA', 'AAC', 'XX']
['ACG', 'AAA', 'AAC', 'YYY']
['ACG', 'AAA', 'AGC', 'XX']
['ACG', 'AAA', 'AGC', 'YYY']
['ACG', 'AGA', 'AAC', 'XX']
['ACG', 'AGA', 'AAC', 'YYY']
['ACG', 'AGA', 'AGC', 'XX']
['ACG', 'AGA', 'AGC', 'YYY']
['ACG', 'GGA', 'AAC', 'XX']
['ACG', 'GGA', 'AAC', 'YYY']
['ACG', 'GGA', 'AGC', 'XX']
['ACG', 'GGA', 'AGC', 'YYY']
['AGC', 'AAA', 'AAC', 'XX']
['AGC', 'AAA', 'AAC', 'YYY']
['AGC', 'AAA', 'AGC', 'XX']
['AGC', 'AAA', 'AGC', 'YYY']
['AGC', 'AGA', 'AAC', 'XX']
['AGC', 'AGA', 'AAC', 'YYY']
['AGC', 'AGA', 'AGC', 'XX']
['AGC', 'AGA', 'AGC', 'YYY']
['AGC', 'GGA', 'AAC', 'XX']
['AGC', 'GGA', 'AAC', 'YYY']
['AGC', 'GGA', 'AGC', 'XX']
['AGC', 'GGA', 'AGC', 'YYY']

Upvotes: 0

Joran Beasley
Joran Beasley

Reputation: 114038

It seems like you are asking for the product

for c in itertools.product(B3,B2,B1):
    print(c)

Upvotes: 2

Related Questions