获取匹配的 fasta 文件

获取匹配的 fasta 文件

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

匹配行list.txt(如果重复行,仅使用唯一行)seq.fasta并提取 FASTA 文件,如输出文件中所示。

答案1

你展示的简单案例是微不足道的。您的序列永远不会超过一行,因此您可以简单地用于grep搜索每个 ID 及其后面的行:

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

使用该选项时,只需过滤掉在输出行组之间添加的grep -v '^--$'行。--grep-A

为了避免欺骗,您可以通过 (GNU) 排序传递列表:

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

使用的标志是:

   -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.

然而,在大多数情况下,您的序列将是多行,而这还不够。如果您经常做这种事情,我建议您安装该exonerate工具套件。它们通常对于生物信息学工作非常有用,并且包含一个名为的很好的工具,fastafetch旨在完全满足您的需求:

  1. 安装无罪套件。它位于基于 Debian 的系统的存储库中,也可以从这里

     sudo apt-get install exonerate
    
  2. 为您的 fasta 文件创建索引。这用于快速检索序列。

     fastaindex seq.fasta seq.idx 
    
  3. 提取您的序列:

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

相关内容