Read a two-character column as two separate columns

awktext formattingtext processing

I have a file with genotype data. The second column has both alleles for a particular genetic variant concatenated, as below.

rs969931    CA  1.000   2.000   2.000   2.000   2.000   2.000   1.000   1.000
rs2745406   CT  0.000   2.000   2.000   1.000   1.000   2.000   1.000   1.000
rs6939431   AG  0.000   0.000   0.000   0.000   0.000   0.000   1.000   0.000
rs1233427   AG  1.000   2.000   2.000   2.000   2.000   1.000   1.000   1.000
rs1233426   AG  1.000   2.000   2.000   2.000   2.000   1.000   1.000   1.000
rs1233425   GC  1.000   1.999   1.999   2.000   2.000   2.000   1.000   1.000
rs362546    GA  1.000   2.000   2.000   2.000   2.000   1.000   1.000   1.000
rs909968    AG  0.000   2.000   2.000   1.000   1.000   1.000   1.000   1.000
rs909967    GA  1.000   2.000   2.000   2.000   2.000   2.000   1.000   1.000
rs886381    AG  0.000   0.000   0.000   0.000   0.000   0.000   0.000   1.000

I need to create a new file with the alleles as two separate columns, i.e. splitting the second column into two columns. Desired output below. Is there a way to specify multiple field separators in awk to achieve this?

rs969931    C A  1.000  2.000   2.000   2.000   2.000   2.000   1.000   1.000
rs2745406   C T  0.000  2.000   2.000   1.000   1.000   2.000   1.000   1.000
rs6939431   A G  0.000  0.000   0.000   0.000   0.000   0.000   1.000   0.000
rs1233427   A G  1.000  2.000   2.000   2.000   2.000   1.000   1.000   1.000
rs1233426   A G  1.000  2.000   2.000   2.000   2.000   1.000   1.000   1.000
rs1233425   G C  1.000  1.999   1.999   2.000   2.000   2.000   1.000   1.000
rs362546    G A  1.000  2.000   2.000   2.000   2.000   1.000   1.000   1.000
rs909968    A G  0.000  2.000   2.000   1.000   1.000   1.000   1.000   1.000
rs909967    G A  1.000  2.000   2.000   2.000   2.000   2.000   1.000   1.000
rs886381    A G  0.000  0.000   0.000   0.000   0.000   0.000   0.000   1.000

Best Answer

You can do it using the sub function in awk:

awk 'sub(/./,"& ",$2)1;' file

If you want tab-separated output, you can use:

awk -v OFS="\t" 'sub(/./,"&\t",$2)1;' file

Or in a variety of other tools:

  • Perl

    perl -alne '$F[1]=~s/./$& /; print "@F"' file
    

    Or, for tab-separated output:

    perl -alne '$F[1]=~s/./$&\t/; print join "\t",@F' file
    
  • GNU sed

    sed -r 's/\S+\s+\S/& /' file
    
  • Other sed

    sed  's/^[[:alnum:]]*[[:blank:]]*./& /' file
    
  • Shell

    while read -r snp nt rest; do 
        printf "%s\t%s\t%s\t%s\n" "$snp" "${nt:0:1}" "${nt:0:1}" "$rest"
    done < file
    
Related Question