Sliding Window with specific window size and step size in BioPython
1
0
Entering edit mode
4.7 years ago

I have multiple sequences in a fasta file and I want to divide it into sliding window with window size 90 and step size 10. I have a python script as follows:

from Bio import SeqIO
 with open("my90_out.txt","w") as f:
    for seq_record in SeqIO.parse("myseq.fasta", "fasta"):
        for i in range(len(seq_record.seq) - 9) :
           f.writestrseq_record.id) + "\n")
           f.write(str(seq_record.seq[i:i+90]) + "\n") 
i

The code produces sequences with window size 90 but step size 1. I need to change the step size to 10. I know I have made a blunder in the for loop. Thanks in advance for the help.

python biopython • 4.9k views
ADD COMMENT
0
Entering edit mode

See this script, try to change your step size accordingly:

How to extract short sequence from FASTA file with certain step size?

ADD REPLY
1
Entering edit mode
4.7 years ago
Joe 21k

Since your range() call, will give you i equal to all the integers in the range of the sequence length, you need to change the step size, you can do this with an optional argument within range() itself: range(start, stop, step).

https://www.pythoncentral.io/pythons-range-function-explained/

Try changing your loop to something like:

for seq in SeqIO.parse("Genes1.fasta", "fasta"):
     for i in range(0, len(seq.seq)-9, 10):
         printseq.id + "\n")
         print(str(seq.seq[i:i+90] + "\n"))

(I've done away with the file openings and closings for ease, but you can just sub those back in for the print statements.

ADD COMMENT

Login before adding your answer.

Traffic: 1528 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6