我想知道在bash中从fasta文件中删除某些行的最佳方法是什么。
在上面的例子中,假设我想删除它写成'GUITH'的行,你如何删除这一行和上面的行,直到找到其他'>'
字符?
fasta
档案:
>B4KSI7_DROMO
RGLKRKPMALIKKLRKAKKEAPPNEKPEIVKTHLRNMIIVPEMTGSIIGVYNGKDFGQVE
VKPEMIGHYLGEFALTYKPVKH
>O46898_GUITH
RSLSKGPYIAAHLLKKLNNVDIQKPDVVIKTWSRSSTILPNMVGATIAVYNGKQHVPVYI
SDQMVGHKLGEFSPTRTFRSH
>Q7RT13_PLAYO
RGIDKKAKSLLKKLRKAKKECEVGEKPKPIPTHLRNMTIIPEMVGSIVAVHNGKQYTNVE
IKPEMIGYYLGEFSITYKHTRH
使用bash过滤后 fasta
文件:
>B4KSI7_DROMO
RGLKRKPMALIKKLRKAKKEAPPNEKPEIVKTHLRNMIIVPEMTGSIIGVYNGKDFGQVE
VKPEMIGHYLGEFALTYKPVKH
>Q7RT13_PLAYO
RGIDKKAKSLLKKLRKAKKECEVGEKPKPIPTHLRNMTIIPEMVGSIVAVHNGKQYTNVE
IKPEMIGYYLGEFSITYKHTRH
问题的另一个版本,但操作更难。假设你有一个物种名称的文件:
species.txt
:
DROMO;
PLAYO;
并且您想删除fasta文件中除了species.txt文档中不存在物种的行。因此,您获得与上面相同的输出,但是由于其他文件(不直接输入'GUITH'
),您可以删除行。这样做的最佳方式是什么?
答案 0 :(得分:1)
删除它写成'GUITH'的行:
sed 's/>/\n&/' fasta.txt | sed '/_GUITH/,/^$/d' | sed '/^$/d'
删除fasta文件中除species.txt中不存在物种的行:
使用GNU sed和bash:
sed 's/>/\n&/' fasta.txt | sed -n -f <( sed 's/;$//;s|.*|/_&$/,/^$/p|' species.txt ) | sed '/^$/d'
输出:
>B4KSI7_DROMO RGLKRKPMALIKKLRKAKKEAPPNEKPEIVKTHLRNMIIVPEMTGSIIGVYNGKDFGQVE VKPEMIGHYLGEFALTYKPVKH >Q7RT13_PLAYO RGIDKKAKSLLKKLRKAKKECEVGEKPKPIPTHLRNMTIIPEMVGSIVAVHNGKQYTNVE IKPEMIGYYLGEFSITYKHTRH
答案 1 :(得分:1)
在awk中:
$ awk '/^>/{p=1} /GUITH/{p=0} p' file
>B4KSI7_DROMO
RGLKRKPMALIKKLRKAKKEAPPNEKPEIVKTHLRNMIIVPEMTGSIIGVYNGKDFGQVE
VKPEMIGHYLGEFALTYKPVKH
>Q7RT13_PLAYO
RGIDKKAKSLLKKLRKAKKECEVGEKPKPIPTHLRNMTIIPEMVGSIVAVHNGKQYTNVE
IKPEMIGYYLGEFSITYKHTRH
说明:
/^>/ { p=1 } # turn print flag up for each record starting with >
/GUITH/ { p=0 } # turn print flag down for GUITH
p # print if p
如果您想获得已批准名称的列表:
$ cat list
DROMO
PLAYO
$ awk 'NR==FNR{a[$1];next} /^>/{n=split($0,b,"_"); p=(b[n] in a)} p' list file
>B4KSI7_DROMO
RGLKRKPMALIKKLRKAKKEAPPNEKPEIVKTHLRNMIIVPEMTGSIIGVYNGKDFGQVE
VKPEMIGHYLGEFALTYKPVKH
>Q7RT13_PLAYO
RGIDKKAKSLLKKLRKAKKECEVGEKPKPIPTHLRNMTIIPEMVGSIVAVHNGKQYTNVE
IKPEMIGYYLGEFSITYKHTRH
说明:
NR==FNR { a[$1]; next } # read the list to array a
/^>/ { n=split($0,b,"_"); p=(b[n] in a) } # take the word after _ and if in a, enable print
p # if p, print