Hi,

This appears to be a simple problem that I am unable to solve. I have some data that looks like this:

    CHROM    POS    REF    ALT    TYPE       AF
    chr1     1      A      T      MISSENSE   0.23
    chr2     1      A      T,G    MISSENSE   0.17, 0.09

The above is dummy meaningless data, but it is representative of the problem at hand.

I'd like to `separate_rows` such that the `ALT` and `AF` are separated in a couples manner. Running `separate_rows` on the 2 columns would give me 4 rows, not 2. I'd like my output to be:

    CHROM    POS    REF    ALT    TYPE       AF
    chr1     1      A      T      MISSENSE   0.23
    chr2     1      A      T      MISSENSE   0.17
    chr2     1      A      G      MISSENSE   0.09

Is there any way I can conserve this combination while separating the values out? I am really far out from the VCF to go back and split multi-allelics.


You could use a Python script to do this easily:

    #!/usr/bin/env python
    
    import sys
    
    headers = None
    idx = 0
    for line in sys.stdin:
        elems = line.rstrip().split('\t')
        if idx == 0:
            headers = elems
            sys.stdout.write(line)
        else:
            items = {x:y for x,y in zip(headers, elems)}
            alleles = items['ALT'].split(',')
            afs = items['AF'].split(',')
            for ai in range(len(alleles)):
                items['ALT'] = alleles[ai]
                items['AF'] = afs[ai]
                sys.stdout.write('{}\n'.format('\t'.join([items[x] for x in headers])))
        idx += 1

For example:

    $ ./split.py < variants.txt
    CHROM	POS	REF	ALT	TYPE	AF
    chr1	1	A	T	MISSENSE	0.23
    chr2	1	A	T	MISSENSE	0.17
    chr2	1	A	G	MISSENSE	 0.09

Write it out to a file and bring that back into R:

    $ ./split.py < variants.txt > variants.split.txt