Given two words:
AT
TC
I want to generate all possible combinations (not sure if combination applies here) that can be produced by replacing one by one character of those two strings, as a result:
AT
TT
AC
TC
Edit:
I've tried:
from itertools import product
ref = "ACGT"
snp = "TGCA"
prod = product(ref,snp)
for p in prod:
print p
but the result is:
('A', 'T')
('A', 'G')
('A', 'C')
('A', 'A')
('C', 'T')
('C', 'G')
('C', 'C')
('C', 'A')
('G', 'T')
('G', 'G')
('G', 'C')
('G', 'A')
('T', 'T')
('T', 'G')
('T', 'C')
('T', 'A')
Not what I'm looking for. I'm expecting something like (each result should be same lenght as input):
ACGT
TCGT
AGGT
ACGA
....
You are looking for the itertools.product
which you may use like:
>>> from itertools import product
>>> my_list = [ 'AT' , 'TC']
>>> list(product(*my_list))
[('A', 'T'), ('A', 'C'), ('T', 'T'), ('T', 'C')]
To get these values as string, you may use a list comprehension as:
>>> [''.join(s) for s in product(*my_list)]
['AT', 'AC', 'TT', 'TC']
Edit (Based on the edit in the question)
For the new example you shared , you should be using zip
with the above list comprehension expression and itertools.product
as:
>>> ref = "ACGT"
>>> snp = "TGCA"
>>> [''.join(s) for s in product(*zip(ref,snp))]
['ACGT', 'ACGA', 'ACCT', 'ACCA', 'AGGT', 'AGGA', 'AGCT', 'AGCA', 'TCGT', 'TCGA', 'TCCT', 'TCCA', 'TGGT', 'TGGA', 'TGCT', 'TGCA']