我有代码可以帮助我从左向右移动大小为5的窗口。该文件是fasta格式,例如标题>染色体,后面是染色体的索引。我想根据确切的索引输出标题索引号。任何人都可以帮助我吗?
代码
from Bio import SeqIO
with open("test1_out.fasta","w") as f:
for seq_record in SeqIO.parse("test1.fasta", "fasta"):
for i in range(len(seq_record.seq) - 4) :
f.write(">" + str(seq_record.id) + "\n")
f.write(str(seq_record.seq[i:i+5]) + "\n")
test1.fasta
>chr1:1-8
ATCGCGTC
>chr2:1-10
ATTTTCGCGA
实际输出
>chr1:1-8
ATCGC
>chr1:1-8
TCGCG
>chr1:1-8
CGCGT
>chr1:1-8
GCGTC
>chr2:1-10
ATTTT
>chr2:1-10
TTTTC
>chr2:1-10
TTTCG
>chr2:1-10
TTCGC
>chr2:1-10
TCGCG
>chr2:1-10
CGCGA
所需的输出
>chr1:1-5
ATCGC
>chr1:2-6
TCGCG
>chr1:3-7
CGCGT
>chr1:4-8
GCGTC
>chr2:1-5
ATTTT
>chr2:2-6
TTTTC
>chr2:3-7
TTTCG
>chr2:4-8
TTCGC
>chr2:5-9
TCGCG
>chr2:6-10
CGCGA
答案 0 :(得分:2)
您只需修改标题编写:
seq_name = seq_record.id.split(":")[0] # Get the "chr1"
for i in range(len(seq_record.seq) - 4):
seq_coords = "{}-{}".format(i + 1, i + 5) # Make the coordinates
f.write(">" + seq_name + ":" + seq_coords + "\n") # Print them both