import pysamdef add_ids_to_vcf(input_vcf, output_vcf):
vcf = pysam.VariantFile(input_vcf)
new_vcf = pysam.VariantFile(output_vcf, 'w', header=vcf.header)
for record in vcf:
# 如果ID字段为'.',则表示ID不存在 if record.id == '.':
# 检查是否为SNP if len(record.ref) == 1 and all(len(alt) == 1 for alt in record.alts):
record.id = f"SNP_{record.chrom}_{record.pos}" else:
record.id = f"SV_{record.chrom}_{record.pos}"
new_vcf.write(record)
vcf.close()
new_vcf.close()# 使用方法add_ids_to_vcf("your_input.vcf.gz", "output.vcf.gz")