2

list.txt:

58759__len__2903
58759__len__2903
673957__len__1655
673957__len__1655
3566454__len__1744

seq.fasta:

>58759__len__2903
TTTTCCGTAGAGGAGATCCCTATTTTTAGGTTTGTAAGAGATCATTTT
>67777__len__2978
TTTTTAGGTTTGTAAGACCGTAGAG
>673957__len__1655
CCCTATTTTTAGGTTTGTAAGGTTTGTAAGACCGTAGAG
>3566454__len__1744
GGTTTGTAAGACCGTAGAGGGTTTGTAAGACCGTAGAG

output.fasta:

>58759__len__2903
TTTTCCGTAGAGGAGATCCCTATTTTTAGGTTTGTAAGAGATCATTTT
>673957__len__1655
CCCTATTTTTAGGTTTGTAAGGTTTGTAAGACCGTAGAG
>3566454__len__1744
GGTTTGTAAGACCGTAGAGGGTTTGTAAGACCGTAGAG

Match line from list.txt (if duplicate line, use only unique line) to seq.fasta and extract the FASTA file as shown in output file.

  • Please include at least your current code. USE is not a programming service. – l0b0 Sep 22 '14 at 11:51
  • This is not duplicate question. My question is different in the aspect of sequence extraction. – jack Sep 24 '14 at 1:39
7

The simple case you show is trivial. Your sequences are never more than a single line so you can simply use grep to search for each of your IDs and the line after them:

grep -Fwf list.txt -A 1  seq.fasta | grep -v '^--$'  > out.fasta

The grep -v '^--$' simply filters out the lines with -- that grep adds between groups of output lines when using the -A option.

To also avoid the dupes, you can pass your list through (GNU) sort:

grep -Fwf <(sort -u list.txt) -A 1  seq.fasta | grep -v '^--$'  > out.fasta

The flags used are:

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)
   -w, --word-regexp
          Select  only  those  lines  containing  matches  that form whole
          words.  The test is that the matching substring must  either  be
          at  the  beginning  of  the  line,  or  preceded  by  a non-word
          constituent character.  Similarly, it must be either at the  end
          of  the  line  or  followed by a non-word constituent character.
          Word-constituent  characters  are  letters,  digits,   and   the
          underscore.
   -F, --fixed-strings
          Interpret PATTERN as a  list  of  fixed  strings,  separated  by
          newlines,  any  of  which is to be matched.  (-F is specified by
          POSIX.)
   -A NUM, --after-context=NUM
          Print NUM  lines  of  trailing  context  after  matching  lines.
          Places   a  line  containing  a  group  separator  (--)  between
          contiguous groups of matches.  With the  -o  or  --only-matching
          option, this has no effect and a warning is given.

However, in most cases, your sequences will be several lines and that will not be enough. If you will be doing this kind of thing often, I suggest you install the exonerate suite of tools. They are generally extremely useful for bioinformatics work and include a nice tool called fastafetch that is designed to do exactly what you want:

  1. Install the exonerate suite. This is in the repositories of Debian based systems, and is also available from here.

    sudo apt-get install exonerate
    
  2. Create an index for your fasta file. This is used for the rapid retrieval of sequences.

    fastaindex seq.fasta seq.idx 
    
  3. Extract your sequences:

    $ fastafetch -f seq.fasta -i seq.idx -Fq <(sort -u list.txt )
    >3566454__len__1744
    GGTTTGTAAGACCGTAGAGGGTTTGTAAGACCGTAGAG
    >58759__len__2903
    TTTTCCGTAGAGGAGATCCCTATTTTTAGGTTTGTAAGAGATCATTTT
    >673957__len__1655
    CCCTATTTTTAGGTTTGTAAGGTTTGTAAGACCGTAGAG
    

As a side note, I have written a script that can do this and a bit more. It can take one or more lists of input IDs and will extract the corresponding sequences from a multi fasta file. It can either print to STDOUT or create a file for each input list. You can find it here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.