0

I have a .bim file with variant identifier as chr:pos and a SNPs list.file matching chr:pos and rs IDs.

file1 (the .bim file):

1       1:10583         0       10583           G       A
1       1:10611         0       10611           G       C
... 
22      22:51242613     0       51242613        C       A
22      22:51243297     0       51243297        T       A

file2 (SNP list):

rs99999 4:163870478
rs99998 4:117161848
...

when column2 in file1 matches with column2 in file2 (both are chr:pos), I want to replace column2 in file1 with column1 in file2, otherwise, keep column2 in file1.

I tried the following script but didn't work, no replacement happened. Anyone can kindly help?

awk -F"\t" -v OFS="\t" '{
    if (FNR==NR) {
        a[FNR]=$2;b[FNR]=$1}
    else {
        if (a[FNR] == $2) {
            $2=b[FNR]} else {$2=$2};
            print $0
        }
    }' file2 file1 > file1_update
2
  • 1
    can you edit to show what you expect as final result from these two files? and which column(s) are the key between these two files? as I cannot get that; maybe that's second column and in your file1 at least you should show us a parable keys. Commented Dec 3, 2020 at 19:47
  • 1
    Also, are both files tab-separated? And please give us an example that we can use: none of the chr positions in your rsID list is actually present in your bim example.
    – terdon
    Commented Dec 3, 2020 at 19:50

1 Answer 1

1

I added an entry to the rsID list that matches your bim file:

$ cat file2.list 
rs99999 4:163870478
rs99998 4:117161848
rs123456 22:51242613

Then:

$ awk -F"\t" -v OFS="\t" '{ 
                            if(NR==FNR){
                                a[$2]=$1; next
                            }
                            if($2 in a){
                                $2=a[$2]
                            }
                         }1;' file2.list file1.bim 
1   1:10583 0   10583   G   A
1   1:10611 0   10611   G   C
... 
22  rs123456    0   51242613    C   A
22  22:51243297 0   51243297    T   A

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .