def vcf_line(input_data, reference):
    """
    Convert the var files information into VCF format.

    This is nontrivial because the var file can contain zero-length variants,
    which is not allowed by VCF. To handle these cases, we "move backwards"
    by one position, look up the reference sequence, and add that.

    The returned line is a very simple, VCF-valid row containing the
    genome's data for this position.
    """
    vcf_data = VCF_DATA_TEMPLATE.copy()
    start = int(input_data['start'])
    dbsnp_data = input_data['dbsnp_data']
    ref_allele = input_data['ref_seq']
    genome_alleles = input_data['alleles']

    # Get dbSNP IDs.
    dbsnp_cleaned = []
    for dbsnp in dbsnp_data:
        if dbsnp not in dbsnp_cleaned:
            dbsnp_cleaned.append(dbsnp)
    if dbsnp_cleaned:
        id_field = ';'.join(dbsnp_cleaned)
        if id_field == '':
            id_field = '.'
    else:
        id_field = '.'

    # Is this a matching reference line? Handle per gVCF spec.
    if input_data['ref_seq'] == '=':
        ref_allele = reference[input_data['chrom']][start].upper()
        vcf_data['CHROM'] = input_data['chrom']

        # Position notes: Complete Genomics uses 0-based start and 1-based end.
        # Reference seq retrieval is 0-based start, but VCF is 1-based start.
        vcf_data['POS'] = str(start + 1)
        vcf_data['ID'] = id_field
        vcf_data['REF'] = ref_allele
        vcf_data['ALT'] = '.'
        vcf_data['FORMAT'] = 'GT'
        assert input_data['allele_count'] in ['1', '2']
        if '?' in input_data['alleles']:
            if 'NOCALL' not in input_data['filters']:
                input_data['filters'].append('NOCALL')
            if input_data['allele_count'] == '2':
                vcf_data['SAMPLE'] = './.'
            else:
                vcf_data['SAMPLE'] = '.'
        elif input_data['allele_count'] == '2':
            vcf_data['SAMPLE'] = '0/0'
        else:
            vcf_data['SAMPLE'] = '0'

        if input_data['filters']:
            vcf_data['FILTER'] = ';'.join(input_data['filters'])
        else:
            vcf_data['FILTER'] = 'PASS'
        vcf_data['INFO'] = 'END={}'.format(input_data['end'])

        return formatted_vcf_line(vcf_data)

    # VCF doesn't allow zero-length sequences. If we have this situation,
    # move the start backwards by one position, get that reference base,
    # and prepend this base to all sequences.
    if len(ref_allele) == 0 or 0 in [len(v) for v in genome_alleles]:
        start = start - 1
        prepend = reference[input_data['chrom']][start].upper()
        ref_allele = prepend + ref_allele
        genome_alleles = [prepend + v if v != '?' else v for v in
                          genome_alleles]

    # Figure out what our alternate alleles are.
    alt_alleles = []
    for allele in genome_alleles:
        if allele not in [ref_allele] + alt_alleles and allele != '?':
            alt_alleles.append(allele)

    # Combine ref and alt for the full set of alleles, used for indexing.
    alleles = [ref_allele] + alt_alleles

    # Get the indexed genotype.
    allele_indexes = [str(alleles.index(x)) for x in genome_alleles if
                      x != '?']
    [allele_indexes.append('.') for x in genome_alleles if x == '?']
    genotype = '/'.join(allele_indexes)

    vcf_data['CHROM'] = input_data['chrom']
    vcf_data['POS'] = str(start + 1)
    vcf_data['ID'] = id_field
    vcf_data['REF'] = ref_allele
    vcf_data['ALT'] = ','.join(alt_alleles) if alt_alleles else '.'
    vcf_data['FORMAT'] = 'GT'
    vcf_data['SAMPLE'] = genotype

    if input_data['filters']:
        vcf_data['FILTER'] = ';'.join(sorted(input_data['filters']))
    else:
        vcf_data['FILTER'] = 'PASS'

    return formatted_vcf_line(vcf_data)
def vcf_line ( input_data , reference ) : vcf_data = VCF_DATA_TEMPLATE . copy ( ) start = int ( input_data [ 'start' ] ) dbsnp_data = input_data [ 'dbsnp_data' ] ref_allele = input_data [ 'ref_seq' ] genome_alleles = input_data [ 'alleles' ] dbsnp_cleaned = [ ] for dbsnp in dbsnp_data : if dbsnp not in dbsnp_cleaned : dbsnp_cleaned . append ( dbsnp ) if dbsnp_cleaned : id_field = ';' . join ( dbsnp_cleaned ) if id_field == '' : id_field = '.' else : id_field = '.' if input_data [ 'ref_seq' ] == '=' : ref_allele = reference [ input_data [ 'chrom' ] ] [ start ] . upper ( ) vcf_data [ 'CHROM' ] = input_data [ 'chrom' ] vcf_data [ 'POS' ] = str ( start + 1 ) vcf_data [ 'ID' ] = id_field vcf_data [ 'REF' ] = ref_allele vcf_data [ 'ALT' ] = '.' vcf_data [ 'FORMAT' ] = 'GT' assert input_data [ 'allele_count' ] in [ '1' , '2' ] if '?' in input_data [ 'alleles' ] : if 'NOCALL' not in input_data [ 'filters' ] : input_data [ 'filters' ] . append ( 'NOCALL' ) if input_data [ 'allele_count' ] == '2' : vcf_data [ 'SAMPLE' ] = './.' else : vcf_data [ 'SAMPLE' ] = '.' elif input_data [ 'allele_count' ] == '2' : vcf_data [ 'SAMPLE' ] = '0/0' else : vcf_data [ 'SAMPLE' ] = '0' if input_data [ 'filters' ] : vcf_data [ 'FILTER' ] = ';' . join ( input_data [ 'filters' ] ) else : vcf_data [ 'FILTER' ] = 'PASS' vcf_data [ 'INFO' ] = 'END={}' . format ( input_data [ 'end' ] ) return formatted_vcf_line ( vcf_data ) if len ( ref_allele ) == 0 or 0 in [ len ( v ) for v in genome_alleles ] : start = start - 1 prepend = reference [ input_data [ 'chrom' ] ] [ start ] . upper ( ) ref_allele = prepend + ref_allele genome_alleles = [ prepend + v if v != '?' else v for v in genome_alleles ] alt_alleles = [ ] for allele in genome_alleles : if allele not in [ ref_allele ] + alt_alleles and allele != '?' : alt_alleles . append ( allele ) alleles = [ ref_allele ] + alt_alleles allele_indexes = [ str ( alleles . index ( x ) ) for x in genome_alleles if x != '?' ] [ allele_indexes . append ( '.' ) for x in genome_alleles if x == '?' ] genotype = '/' . join ( allele_indexes ) vcf_data [ 'CHROM' ] = input_data [ 'chrom' ] vcf_data [ 'POS' ] = str ( start + 1 ) vcf_data [ 'ID' ] = id_field vcf_data [ 'REF' ] = ref_allele vcf_data [ 'ALT' ] = ',' . join ( alt_alleles ) if alt_alleles else '.' vcf_data [ 'FORMAT' ] = 'GT' vcf_data [ 'SAMPLE' ] = genotype if input_data [ 'filters' ] : vcf_data [ 'FILTER' ] = ';' . join ( sorted ( input_data [ 'filters' ] ) ) else : vcf_data [ 'FILTER' ] = 'PASS' return formatted_vcf_line ( vcf_data )
Convert the var files information into VCF format.

    This is nontrivial because the var file can contain zero-length variants,
    which is not allowed by VCF. To handle these cases, we "move backwards"
    by one position, look up the reference sequence, and add that.

    The returned line is a very simple, VCF-valid row containing the
    genome's data for this position.