Question: Sliding Window with specific window size and step size in BioPython
0
gravatar for adhirajnath14
14 months ago by
adhirajnath1430 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) :
           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.

biopython python • 1.5k views
ADD COMMENTlink written 14 months ago by adhirajnath1430

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 14 months ago by natasha.sernova3.7k
1
gravatar for Joe
14 months ago by
Joe18k
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).

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 COMMENTlink modified 14 months ago • written 14 months ago by Joe18k
Please log in to add an answer.

Help
Access

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