cosmictypist
cosmictypist

Reputation: 575

Loops for sequence output - python

I've been struggling to figure out a way to get my sequence printed out with a 6-mer in the sequence on separate lines. As so (note the spacing of each line):

atgctagtcatc
 tgctag
  gctagt
   ctagtc
    tagtca
     etc

So far, I've been able to get my sequence in string as shown:

from Bio import SeqIO
record = SeqIO.read(open("testSeq.fasta"), "fasta")
sequence = str(record.seq)

However, the only way I could seem to figure out to do the printing of the 6-mers is by:

print sequence 
print sequence[0:5]
print "", sequence[1:6]
print "", "", sequence[2:7]
print "", "", "", sequence [3:8]
etc

I feel like there should be an easier way to do this. I've tried this, but it doesn't seem to work:

x = 0
y = 6
for sequence in sequence[x:y]
    print sequence
    x = x + 1
    y = y + 1

Any opinions on how I should be attempting to accomplish this task would be greatly appreciated. I've only been using python for a couple days now and I'm sorry if my question seems simple.

Thank you!!

Upvotes: 3

Views: 120

Answers (2)

ljetibo
ljetibo

Reputation: 3094

You could try the following (as far as I see you're using python2)

seq = "atgctagtcatc"
spaces = " "
for i in range(0, len(seq)):
    print spaces*i+seq[i:i+6]

Output:

atgcta
 tgctag
  gctagt
   ctagtc
    tagtca
     agtcat
      gtcatc
       tcatc
        catc
         atc
          tc
           c

Upvotes: 0

tmr232
tmr232

Reputation: 1201

This should work:

width = 6
for i in range(len(sequence) - width):
    print " " * i + sequence[i:i+width]

Upvotes: 2

Related Questions