使用bash在符号之间提取文本

时间:2016-05-31 17:18:50

标签: awk sed

首先,我对此比较陌生,所以请耐心等待。

我有一个带注释的转录组.fasta文件,其中包含大约60,000个像这两个基因的记录:

>comp35897_c0_seq11 len=1039 path=[11:0-12;24:13-1038] Match_Acc=E5SX33 Gene=Putative_CAP-Gly_domain_protein 
TTTTAAATTGATTACTTTGCTATTTTTGGCAATGTTGGACTGAGTTGTCGTATTTTTTCG
>comp32620_c0_seq3 len=1874 path=[1:0-195;197:196-220;222:221-354;356:355-481;4197:482-487;489:488-579;581:580-1159;1161:1160-1712;1714:1713-1729;1731:1730-1794;5873:1795-1873] Match_Acc=K1PQJ1 Gene=HAUS_augmin-like_complex_subunit_3 GO=GO:0051225,GO:0070652 
CAGACTTTTGGATTTAGTACATGTATGTATGAATATGTGTTTCAATGTACAACTCAGGAT

我正在尝试创建一个两列,以空格分隔的.tab,第一列中包含组件编号,第二列中包含基因名称。我使用grepsedawk查看了许多类似的帖子,但没有一条建议的代码对我有效。

具体来说,我需要从.fasta中提取的是第一列的>和下一个space之间的编号,以及Gene=和下一个列之间的基因名称space。对于上面的两个基因,那应该给我:

comp35897_c0_seq11 Putative_CAP-Gly_domain_protein
comp32620_c0_seq3 HAUS_augmin-like_complex_subunit_3

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

你有没有尝试过任何东西?

你可以这样做:

sed 's/>\(comp[^ ]\+\) \+.*Gene=\([^ ]\+\) .*$/\1 \2/'

看起来很复杂,但如果你慢慢把它分解成它的组成部分,它就比较容易理解。

修改

好的,所以为了确保sed只输出你想要的东西,你需要在默认情况下打开“没有输出”'模式-n并明确打印您感兴趣的每一行p

我会尝试将其分解,以便可以理解。

comp[^ ]\+ #is a regex that says: 
           #text that starts with the string 'comp' 
           #and is followed by at least one character
           #that is anything that isn't a space (the [^ ]) 
\(comp[^ ]\+\) #is the sed construct that remembers what 
               #that regex matches.
.* #is the regex for zero or more of any chars.
'Gene=\([^ ]\+\) ' #look for the string 'Gene' followed by an
                   #equals sign, followed by at least one char
                   #that isn't a space, followed by a space
                   #oh, and remember the bit after = and before the space

所以,除了sed的-n和p开关,你可以使用:

sed -n 's/>\(comp[^ ]\+\) \+.*Gene=\([^ ]\+\) .*$/\1 \2/p' 

答案 1 :(得分:0)

用awk:

跳过基因名称,如果'基因'缺席

   awk 'BEGIN{RS=">"} NF>1{if($5 ~ /Gene=/){gsub("Gene=","",$5); print $1,$5} else {print $1}}' < transcriptome.fasta > space-delimited.tab

输出

comp35897_c0_seq11 Putative_CAP-Gly_domain_protein
comp32620_c0_seq3

跳过记录,如果&#39;基因&#39;缺席

awk 'BEGIN{RS=">"} NF>1{if($5 ~ /Gene=/){gsub("Gene=","",$5); print $1,$5}}' < transcriptome.fasta > space-delimited.tab

输出

comp35897_c0_seq11 Putative_CAP-Gly_domain_protein
相关问题