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.
1 answer
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.
Log in to answer this question.
See this script, try to change your step size accordingly:
How to extract short sequence from FASTA file with certain step size?