Grep word in one file, and use that word to match in FASTA file, adding the FASTA sequence to the first file

StackOverflow https://stackoverflow.com/questions/17091628

  •  31-05-2022
  •  | 
  •  

Question

I want to grep several words in file1, and use each word to grep what follows after its match in file2.fasta. And then I want to add the thing that followed the match to the word I used into file03, so that file03 contains information from both files. Part of files I have are:

file1:

Jan12345: ID1 ID2 ... IDN1
Jan67899: ID11 ID12 ... IDN2

And a Fasta file (file2) like this:

>ID1
ABCDEFG
>ID2
HIJKLMN
>IDN1
OPQRSTU
>ID11
WXYZABC
>ID12
DEFGHIJ
>IDN2
KLMNOPQ

The output I want is for this example:

Jan12345 ID1 ABCDEFG ID2 HIJKLMN ... IDN1 OPQRSTU
Jan67899: ID11 WXYZABC ID12 DEFGHIJ... IDN2 KLMNOPQ

As you can see, I simply want to add the FASTA sequence - which is contained in file2 – to file1. If anyone knows how to do this I would greatly appreciate it!

Was it helpful?

Solution

One way with awk

awk '
NR==FNR && /\>/ {
    x=$0
    getline b
    a[substr(x,2)]=b
    next
} 
{
    for (i=2;i<=NF;i++) {
        for (k in a) {
            if ($i==k) {
                $i=$i" "a[k]
            }
        }
    }
}1' file2 file1

One-liner:

awk 'NR==FNR{NF==2?k=$2:a[k]=$1;next}{for(i=2;i<=NF;i++){for(k in a){$i=$i==k?$i OFS a[k]:$i}}}1' FS="[> ]" file{2,1}

Output with your sample data:

$ awk 'NR==FNR {NF==2?k=$2:a[k]=$1;next}{for(i=2;i<=NF;i++){for(k in a){$i=$i==k?$i OFS a[k]:$i}}}1' FS="[> ]" file{2,1}
Jan12345: ID1 ABCDEFG ID2 HIJKLMN IDN1 OPQRSTU
Jan67899: ID11 WXYZABC ID12 DEFGHIJ IDN2 KLMNOPQ

OTHER TIPS

Reads fasta/file2 file into %h hash, and makes substitution for every line in file1,

perl -pe 'BEGIN{open F,pop;%h=map{y|\r\n>||d;$_}<F>} s|(ID\S+)|$1 $h{$1}|g' file1 file2

ugly way with GNU sed:

  • step I : make a command script

    sed -r 's#^(\S+)\s+#${x;s/^\\s\\\|>//g;p};1{s/.*/\1/;h};/\n#;h;s/\n.*//;x;s/.*\n//;:ka;s#(\S+)\s*#\\b\1\\b\\| #;H;g;s/\n(\S+).*/\1/;x;s/.*\n\S+\s*//;tka;s/\\\|\n/\/!d;$!N;H;x;s\/\\n\/ \/g;x/' file1 > file.sed
    
  • step II : make the outcome file with bash

    #!/bin/bash
    while read p; do 
    sed -n $p file2
    done < file.sed > file3
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top