gpt4 book ai didi

python - 如何根据步长提取短序列?

转载 作者:太空宇宙 更新时间:2023-11-03 17:39:19 24 4
gpt4 key购买 nike

下面的代码提取窗口大小为 100 的每个序列中的短序列。窗口将移动步长 1 并提取序列。我想提取每个步长为 50 的短序列。有人可以帮助我吗?

 from Bio import SeqIO

with open("B.fasta","w") as f:
for seq_record in SeqIO.parse("A.fasta", "fasta"):
for i in range(len(seq_record.seq) - 99) :
f.write(str(">"+seq_record.id) + "\n")
f.write(str(seq_record.seq[i:i+100]) + "\n")

fasta 文件示例:

>hg17_ct_ER_ER_142
CTAAAAAAGTAAAAAAGAAAAAAAGAGAAAGAAAGAATATAGAAGCAACAAGTGTAGATTTACATTCTATTAGACAGTGACCCATTAGACCCGGACAAGGGG

示例输出:

>hg17_ct_ER_ER_142
CTAAAAAAGTAAAAAAGAAAAAAAGAGAAAGAAAGAATATAGAAGCAACAAGTGTAGATTTACATTCTATTAGACAGTGACCCATTAGACCCGGACAAGG
>hg17_ct_ER_ER_142
TAAAAAAGTAAAAAAGAAAAAAAGAGAAAGAAAGAATATAGAAGCAACAAGTGTAGATTTACATTCTATTAGACAGTGACCCATTAGACCCGGACAAGGG
>hg17_ct_ER_ER_142
AAAAAAGTAAAAAAGAAAAAAAGAGAAAGAAAGAATATAGAAGCAACAAGTGTAGATTTACATTCTATTAGACAGTGACCCATTAGACCCGGACAAGGGG

预期输出:

>hg17_ct_ER_ER_142
CTAAAAAAGTAAAAAAGAAAAAAAGAGAAAGAAAGAATATAGAAGCAACA
>hg17_ct_ER_ER_142
AGTGTAGATTTACATTCTATTAGACAGTGACCCATTAGACCCGGACAAGG

最佳答案

只需使用范围函数的步长选项:

for i in range(0, len(seq_record.seq) - 99, 50) :

关于python - 如何根据步长提取短序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30796262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com