The itertools module in Python has a lot of methods to deal with such combinatorial problems.
python3 - <<\eof
import itertools as it
dna = 'atcg'
step = 5
with open('yourfile') as f:
for nr,_ in enumerate(f):
l = _.rstrip('\n')
if not nr:
w = len(l)
I = [i for i in range(step-1,w,step)]
for t1 igenin = it.cycle(it.product(dna,repeat=int(w/step)))
:
t = list(next(igen)t1)[::-1]
print(*[
t.pop(0) if idx in I else e
for idx,e in enumerate(l)],sep="")
eof
- From the iterations module, the method product generates a Cartesian product of the iterations inputted, in our case tne dna sequence multiple times.
- We turn it into an infinite iterator that never ends and recycled from start once the number of Cartesian products is reached whilst the input file still has data in it.