This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Splicing window on fasta sequence

Hello,

I want to create a splicing window from the beginnin and from the end on a fasta sequence. I will save the results on two separate files.

I have :

from Bio import SeqIO

with open("splicing_window.beginning.fasta","w") as f1, open("splicing_window.end.fasta","w") as f2:
        for seq_record in SeqIO.parse("my_sequence.test.fasta", "fasta"):
            for i in range(len(seq_record.seq) - 9) :
               f1.write(">" + str(seq_record.id) + "\n")
               f1.write(str(seq_record.seq[i:i+10]) + "\n")
               f2.write(">" + str(seq_record.id) + "\n")
               f2.write(str(seq_record.seq[:-i+10]) + "\n")

The problem is for the splicing window from the end. For example, if I have that sequence : TCCGCCGGAAGG ; I'd like to get an output f2 fasta file (window of 10nts) like that :

>1
CGCCGGAAGG
>2
CCGCCGGAAG
>3
TCCGCCGGAA

Any help? Best

biopython python
from Bio import SeqIO

f= SeqIO.parse("test.fa" ,"fasta")
window_size = 10
step_size = 1

for i in f:
    for j in range(window_size,len(i.seq)+1):
        start=j-window_size
        end=j+1
        print(str(">")+i.description+"_"+str(start+1)+"_"+str(j)+"\n"+i.seq[start:end])


$ /bin/python3 sliding_window.py

>seq1_3_12
CGCCGGAAGG
>seq1_2_11
CCGCCGGAAGG
>seq1_1_10
TCCGCCGGAAG

Thanks a lot.

Updated the code again for better understanding.

0 answers

No answers yet.

Log in to answer this question.