import sys
def replacefastaids(fastafile, idfile): # 读取新id列表 with open(idfile, 'r') as f: newids = [line.strip() for line in f]
# 替换fasta文件中的id
new_fasta = ''
with open(fasta_file, 'r') as f:
    for line in f:
        if line.startswith('>'):
            # 获取旧id
            old_id = line.strip(None)
            if len(new_ids) == 0:  # 检查new_ids列表是否为空
                break
            # 获取新id
            new_id = new_ids.pop(0)
            # 替换id
            new_fasta += '>' + new_id + '\n'
        else:
            # 添加序列信息
            new_fasta += line
# 将替换后的fasta写入文件
with open('new.fasta', 'w') as f:
    f.write(new_fasta)
return new_fasta
if name == 'main': if len(sys.argv) != 3: print('Usage: python script.py fastafile idfile') sys.exit(1)
fasta_file = sys.argv[1]
id_file = sys.argv[2]
try:
    replace_fasta_ids(fasta_file, id_file)
except Exception as e:
    print('Error:', e)
    sys.exit(1)