3

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!!

2
  • 1
    "" does not occupy any space when printed Commented Feb 19, 2015 at 21:20
  • True, but any comma separation is replaced with a space when printed. Commented Feb 19, 2015 at 21:23

2 Answers 2

2

This should work:

width = 6
for i in range(len(sequence) - width):
    print " " * i + sequence[i:i+width]
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.