Question: Sliding Window with specific window size and step size in BioPython
gravatar for adhirajnath14
17 months ago by
adhirajnath1440 wrote:

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) :
  + "\n")
           f.write(str(seq_record.seq[i:i+90]) + "\n") 

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.

biopython python • 1.9k views
ADD COMMENTlink written 17 months ago by adhirajnath1440

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

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

ADD REPLYlink written 17 months ago by natasha.sernova3.8k
gravatar for Joe
17 months ago by
United Kingdom
Joe18k wrote:

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).

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): + "\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 COMMENTlink modified 17 months ago • written 17 months ago by Joe18k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2053 users visited in the last hour