用python代码对文件夹下的所有fasta文件,提取序列的ID和部分范围内的序列,并以fasta文件形式输出
以下是Python代码,可以提取指定范围内的序列,并将其以fasta格式输出到新文件中。
import os
def extract_sequence(input_file, output_file, start, end):
with open(input_file, 'r') as f_in, open(output_file, 'w') as f_out:
for line in f_in:
if line.startswith('>'):
f_out.write(line)
else:
sequence = line.strip()[start-1:end]
f_out.write(sequence + '\n')
# 设置要处理的文件夹路径
folder_path = '/path/to/folder/'
# 设置要提取的区域范围
start_position = 10
end_position = 20
# 遍历文件夹下的所有fasta文件
for file_name in os.listdir(folder_path):
if file_name.endswith('.fasta'):
input_file = os.path.join(folder_path, file_name)
output_file = os.path.join(folder_path, file_name.replace('.fasta', '_extracted.fasta'))
extract_sequence(input_file, output_file, start_position, end_position)
代码解释:
- 首先定义了一个名为
extract_sequence()的函数,该函数接受4个参数:输入文件路径、输出文件路径、序列起始位置和序列终止位置。 - 函数使用
with open()语句打开输入和输出文件,然后遍历输入文件中的每一行。 - 如果行以
>开头,则表示当前行是序列ID,直接将其写入输出文件中。 - 否则,当前行是序列,将其从指定的起始位置到终止位置提取出来,并写入输出文件中。
- 在主程序中,首先设置要处理的文件夹路径和要提取的区域范围。
- 然后使用
os.listdir()遍历文件夹下的所有fasta文件。 - 对于每个fasta文件,构造输入和输出文件的路径,并调用
extract_sequence()函数提取序列。 - 提取后的序列将保存在新的fasta文件中,该文件名与原始文件名相同,但在文件名末尾添加了
_extracted后缀。
原文地址: https://www.cveoy.top/t/topic/yxM 著作权归作者所有。请勿转载和采集!