构建完系统发育树后,根据需要有些情况下需要对物种名称重命名,这个python脚本就是可以将rename.list中的对应名称替换到tree中。
# 读取rename.list文件,将序列号和物种名称存储在一个字典中
rename_dict = {}
with open("rename.list", "r") as f:
for line in f:
seq_id, species_name = line.strip().split()
rename_dict[seq_id] = species_name
# 导入re模块,用于正则表达式的操作
import re
# 读取gene161.fa.contree文件,将每一行的序列号替换为序列号加物种名称
with open("tree.contree", "r") as f1, open("tree.renamed.contree", "w") as f2:
for line in f1:
for seq_id, species_name in rename_dict.items():
# 使用正则表达式的边界符\b,只替换完整的单词
line = re.sub(r"\b" + seq_id + r"\b", seq_id + "_" + species_name, line)
f2.write(line)
# 输出一个新的系统树文件tree.renamed.contree,其中包含了物种名称
print("完成!新的系统树文件是tree.renamed.contree")