Find all permutations of a string that is variable only at specific positions in Python
Question:
I have a DNA sequence which is variable only at specific locations and need to find all possible scenarios:
DNA_seq='ANGK' #N can be T or C and K can be A or G
N=['T','C']
K=['A','G']
Results:
['ATGA','ATGG','ACGA','ACGG']
Searching online, It seems permutations
method from itertools
can be used for this purpose but not sure how it can be implemented. I would appreciate any help.
Answers:
Use itertools.product
:
from itertools import product
result = [f'A{n}G{k}' for n, k in product(N, K)]
Result:
['ATGA', 'ATGG', 'ACGA', 'ACGG']
I have a DNA sequence which is variable only at specific locations and need to find all possible scenarios:
DNA_seq='ANGK' #N can be T or C and K can be A or G
N=['T','C']
K=['A','G']
Results:
['ATGA','ATGG','ACGA','ACGG']
Searching online, It seems permutations
method from itertools
can be used for this purpose but not sure how it can be implemented. I would appreciate any help.
Use itertools.product
:
from itertools import product
result = [f'A{n}G{k}' for n, k in product(N, K)]
Result:
['ATGA', 'ATGG', 'ACGA', 'ACGG']