Skip to main content
1 of 2
guest_7
  • 5.8k
  • 1
  • 8
  • 13

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)]
      igen = it.cycle(it.product(dna,repeat=int(w/step)))

    t = list(next(igen))[::-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.
guest_7
  • 5.8k
  • 1
  • 8
  • 13