Skip to content Skip to sidebar Skip to footer

Python Switching Multiple Positions In String Each To Multiple Letters

I am trying to write a python code that finds restriction enzyme sites within a sequence of DNA. Restriction enzymes cut at specific DNA sequences, however some are not so strict,

Solution 1:

Maybe you should take a look into python's itertools library, which include product which creates an iterable with every combination of iterables, therefore:

from itertools import product

cutsequencequery = "GAANNNNTTC"
nseq = ["A", "C", "G", "T"]

size = cutsequencequery.count('N')

possibilities = product(*[nseq for i in range(size)]) 
# = ('A', 'A', 'A', 'A'), ... , ('T', 'T', 'T', 'T') 
# len(list(possibilities)) = 256 = 4^4, as expected

s = set()
for n in possibilities:
    print(''.join(n)) # = 'AAAA', ..., 'TTTT'
    new_sequence = cutsequencequery.replace('N' * size, ''.join(n))
    
    s.add(new_sequence)
    print(new_sequence) # = 'GAAAAAATTC', ..., 'GAATTTTTTC'
print(len(s)) # 256 unique sequences

Post a Comment for "Python Switching Multiple Positions In String Each To Multiple Letters"