你可以使用 Biopython 库来处理 FASTA 文件。下面是一个示例代码,可以从一个文件夹中的所有 FASTA 文件中截取特定的子序列,并将其 ID 和序列保存到一个文件中。

from Bio import SeqIO
import os

input_folder = '/path/to/folder'
output_file = 'output.fasta'
start = 1  # 起始位置
end = 10  # 结束位置

with open(output_file, 'w') as outfile:
    for filename in os.listdir(input_folder):
        if filename.endswith('.fasta'):
            filepath = os.path.join(input_folder, filename)
            for record in SeqIO.parse(filepath, 'fasta'):
                seq_id = record.id + f'_{start}-{end}'  # ID后面加上起始和结束位置
                seq = record.seq[start-1:end]  # 截取子序列
                outfile.write(f'>{seq_id}
{seq}
')

你需要将 /path/to/folder 替换为实际的文件夹路径,将 startend 替换为你想要截取的子序列的起始和结束位置。输出文件将保存在当前工作目录中,命名为 output.fasta

Python 提取 FASTA 文件子序列并保存到新文件

原文地址: http://www.cveoy.top/t/topic/lMyf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录