misc/fixReferenceAllele.py
def fix_variant(variant, ref_allele):
    """Switch the incorrect reference allele with the true reference allele"""
    vcf_ref_allele = variant[3]
    alt_alleles = variant[4].split(',')
    try:
        true_ref_index = alt_alleles.index(ref_allele)
    except ValueError:
        sys.stderr.write("WARN: Reference allele not detected -- seen with biallelic filtered VCFs\n")
        sys.stderr.write(f"\tSNP:{variant[2]},REF:{ref_allele},VCF_REF:{vcf_ref_allele},ALTS:{alt_alleles}\n")
        true_ref_index = len(alt_alleles) 
    all_alleles = set([ref_allele, vcf_ref_allele]).union(set(alt_alleles))
    # change elements 9 and on -- zero-based index
    genotypes = "\t".join(variant[9:])
    genotypes = genotypes.replace("0","r") # old reference
    genotypes = genotypes.replace(str(true_ref_index+1), "0")
    genotypes = genotypes.replace("r",str(true_ref_index+1))
    if true_ref_index < len(alt_alleles):
        variant_fixed = variant[:3] + \
                [ref_allele] + \
                [variant[4].replace(ref_allele, vcf_ref_allele)] + \
                variant[5:9] + \
                genotypes.split()
    else:
        variant_fixed = variant[:3] + \
                [ref_allele] + \
                [variant[4] + f",{vcf_ref_allele}"] + \
                variant[5:9] + \
                genotypes.split()
    return "\t".join(variant_fixed) + "\n"
Switch the incorrect reference allele with the true reference allele
Switch the incorrect reference allele with the true reference allele
Switch the incorrect reference allele with the true reference allele
def fix_variant(variant, ref_allele):
    vcf_ref_allele = variant[3]
    alt_alleles = variant[4].split(',')
    try:
        true_ref_index = alt_alleles.index(ref_allele)
    except ValueError:
        sys.stderr.write("WARN: Reference allele not detected -- seen with biallelic filtered VCFs\n")
        sys.stderr.write(f"\tSNP:{variant[2]},REF:{ref_allele},VCF_REF:{vcf_ref_allele},ALTS:{alt_alleles}\n")
        true_ref_index = len(alt_alleles) 
    all_alleles = set([ref_allele, vcf_ref_allele]).union(set(alt_alleles))
    genotypes = "\t".join(variant[9:])
    genotypes = genotypes.replace("0","r") 
    genotypes = genotypes.replace(str(true_ref_index+1), "0")
    genotypes = genotypes.replace("r",str(true_ref_index+1))
    if true_ref_index < len(alt_alleles):
        variant_fixed = variant[:3] + \
                [ref_allele] + \
                [variant[4].replace(ref_allele, vcf_ref_allele)] + \
                variant[5:9] + \
                genotypes.split()
    else:
        variant_fixed = variant[:3] + \
                [ref_allele] + \
                [variant[4] + f",{vcf_ref_allele}"] + \
                variant[5:9] + \
                genotypes.split()
    return "\t".join(variant_fixed) + "\n"
def fix_variant ( variant , ref_allele ) : vcf_ref_allele = variant [ 3 ] alt_alleles = variant [ 4 ] . split ( ',' ) try : true_ref_index = alt_alleles . index ( ref_allele ) except ValueError : sys . stderr . write ( "WARN: Reference allele not detected -- seen with biallelic filtered VCFs\n" ) sys . stderr . write ( f"\tSNP:{variant[2]},REF:{ref_allele},VCF_REF:{vcf_ref_allele},ALTS:{alt_alleles}\n" ) true_ref_index = len ( alt_alleles ) all_alleles = set ( [ ref_allele , vcf_ref_allele ] ) . union ( set ( alt_alleles ) ) genotypes = "\t" . join ( variant [ 9 : ] ) genotypes = genotypes . replace ( "0" , "r" ) genotypes = genotypes . replace ( str ( true_ref_index + 1 ) , "0" ) genotypes = genotypes . replace ( "r" , str ( true_ref_index + 1 ) ) if true_ref_index < len ( alt_alleles ) : variant_fixed = variant [ : 3 ] + [ ref_allele ] + [ variant [ 4 ] . replace ( ref_allele , vcf_ref_allele ) ] + variant [ 5 : 9 ] + genotypes . split ( ) else : variant_fixed = variant [ : 3 ] + [ ref_allele ] + [ variant [ 4 ] + f",{vcf_ref_allele}" ] + variant [ 5 : 9 ] + genotypes . split ( ) return "\t" . join ( variant_fixed ) + "\n"
Switch the incorrect reference allele with the true reference allele
Switch the incorrect reference allele with the true reference allele
"""Switch the incorrect reference allele with the true reference allele""" # change elements 9 and on -- zero-based index # old reference
variant
ref_allele