如何用python代码把一个文件夹下的所有fasta文件中截取一部分子序列的ID和序列,并保存在一个文件里输出
你可以使用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}\n{seq}\n")
你需要将/path/to/folder替换为实际的文件夹路径,将start和end替换为你想要截取的子序列的起始和结束位置。输出文件将保存在当前工作目录中,命名为output.fasta。
原文地址: https://www.cveoy.top/t/topic/yHm 著作权归作者所有。请勿转载和采集!