Ejemplo n.º 1
0
def invtobnd(var):

    strands = var.get_info('STRANDS')
    strand_dict = dict(x.split(':') for x in strands.split(','))

    for o in strand_dict.keys():
        if strand_dict[o] == '0':
            del (strand_dict[o])

    strands = ','.join(['%s:%s' % (o, strand_dict[o]) for o in strand_dict])
    var.set_info('STRANDS', strands)

    if strands[:2] == '++':
        ALT = 'N]' + var.chrom + ':' + str(var.get_info('END')) + ']'
    elif strands[:2] == '--':
        ALT = '[' + var.chrom + ':' + str(var.get_info('END')) + '[N'

    var.set_info('SVTYPE', 'BND')
    var.alt = ALT

    [tempci, temp95] = [var.get_info('CIPOS'), var.get_info('CIPOS95')]
    try:
        temppr = var.get_info('PRPOS')
    except KeyError:
        raise MissingProbabilitiesException('Required tag PRPOS not found.')

    var.set_info('CIPOS', var.get_info('CIEND'))
    var.set_info('CIEND', tempci)
    var.set_info('CIPOS95', var.get_info('CIEND95'))
    var.set_info('CIEND95', temp95)
    try:
        var.set_info('PRPOS', var.get_info('PREND'))
    except KeyError:
        raise MissingProbabilitiesException('Required tag PREND not found.')
    var.set_info('PREND', temppr)
Ejemplo n.º 2
0
def write_var(var, vcf_out, include_genotypes=False):

    v_id = var.var_id
    if var.get_info('CIPOS95') != '0,0' or var.get_info('CIEND95') != '0,0':
        var.set_info('IMPRECISE', True)
    else:
        var.set_info('IMPRECISE', False)

    if var.get_info('SVTYPE') == 'INV' and ('--:0' in var.get_info('STRANDS')
                                            or '++:0'
                                            in var.get_info('STRANDS')):

        invtobnd(var)

    if var.alt not in ['<DEL>', '<DUP>', '<INV>', '<INS>']:

        var.var_id = str(v_id) + '_1'
        var.set_info('EVENT', v_id)
        var.set_info('MATEID', str(v_id) + '_2')
        var.info.pop('END', None)
        var.info.pop('SVLEN', None)

        varstring = var.get_var_string(use_cached_gt_string=True)
        if not include_genotypes:
            varstring = '\t'.join(varstring.split('\t', 10)[:8])

        vcf_out.write(varstring + '\n')

        new_alt = ''

        if var.alt[0] == '[':
            new_alt = '[' + var.chrom + ':' + str(var.pos) + '[N'
        elif var.alt[0] == ']':
            new_alt = 'N[' + var.chrom + ':' + str(var.pos) + '['
        elif var.alt[-1] == '[':
            new_alt = ']' + var.chrom + ':' + str(var.pos) + ']N'
        elif var.alt[-1] == ']':
            new_alt = 'N]' + var.chrom + ':' + str(var.pos) + ']'

        sep, chrom, pos = parse_bnd_alt_string(var.alt)
        var.chrom = chrom
        var.pos = int(pos)
        var.var_id = str(v_id) + '_2'
        var.set_info('MATEID', str(v_id) + '_1')
        var.set_info('SECONDARY', True)
        var.alt = new_alt

        [tempci, temp95] = [var.get_info('CIPOS'), var.get_info('CIPOS95')]
        try:
            temppr = var.get_info('PRPOS')
        except KeyError:
            raise MissingProbabilitiesException(
                'Required tag PRPOS not found.')
        var.set_info('CIPOS', var.get_info('CIEND'))
        var.set_info('CIEND', tempci)
        var.set_info('CIPOS95', var.get_info('CIEND95'))
        var.set_info('CIEND95', temp95)
        try:
            var.set_info('PRPOS', var.get_info('PREND'))
        except KeyError:
            raise MissingProbabilitiesException(
                'Required tag PREND not found.')
        var.set_info('PREND', temppr)

        varstring = var.get_var_string(use_cached_gt_string=True)
        if not include_genotypes:
            varstring = '\t'.join(varstring.split('\t', 10)[:8])

        vcf_out.write(varstring + '\n')

    else:
        varstring = var.get_var_string(use_cached_gt_string=True)
        if not include_genotypes:
            varstring = '\t'.join(varstring.split('\t', 10)[:8])

        vcf_out.write(varstring + '\n')