用dict函数编写python脚本,读取fasta格式的序列文件,并输出无回车和空行的fasta文件
时间: 2024-10-19 13:17:58 浏览: 40
在Python中,你可以使用内置的`open()`函数打开FASTA格式的文件,然后逐行解析内容,利用字典`dict`存储每条序列的相关信息。下面是一个简单的示例脚本,它会将序列名称和对应的序列存储到字典中,并去除回车和空行:
```python
def read_fasta(file_path):
sequence_dict = {}
with open(file_path, 'r') as f:
current_sequence = ""
is_header = True
for line in f:
# 忽略空行和注释行
if not line.strip() or line.startswith(">"):
continue
# 当遇到新的序列头开始记录新序列
if is_header:
name = line[1:].strip()
is_header = False
else:
current_sequence += line.strip() # 添加序列到当前序列
# 如果到达序列结束(又遇到新的序列头),添加旧序列到字典
if line.startswith(">"):
sequence_dict[name] = current_sequence
current_sequence = ""
# 清理最后一个未添加到字典的序列
if current_sequence:
sequence_dict[name] = current_sequence
return sequence_dict
# 使用函数并输出结果
output_file = "cleaned.fasta"
with open(output_file, 'w') as out:
for name, seq in read_fasta("input.fasta").items():
out.write(f">{name}\n{seq}\n")
阅读全文