
このようなファイルがあります(タブで区切られた5つの列)
head allKO.txt
Metabolism Carbohydrate metabolism Glycolisis K07448
Metabolism Protein metabolism protesome K02217
ファイル の 5 列目でパターン (文字列) を検索しKEGG.annotations
、見つかった場合は、KEGG.annotations
パターンが見つかった行と のすべての列の両方を別のファイルに出力しますallKO.txt
。パターンを検索するファイルは次のとおりです。
head KEGG.annotations
>aai:AARI_24510 proP; proline/betaine transporter; K03762 MFS transporter, MHS family, proline/betaine transporter
>aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]
>aai:AARI_28260 hypothetical protein
>aai:AARI_29060 ABC drug resistance transporter, inner membrane subunit; K09686 antibiotic transport system permease protein
>aai:AARI_29070 ABC drug resistance transporter, ATP-binding subunit (EC:3.6.3.-); K09687 antibiotic transport system ATP-binding protein
>aai:AARI_29650 hypothetical protein
>aai:AARI_32480 iron-siderophore ABC transporter ATP-binding subunit (EC:3.6.3.-); K02013 iron complex transport system ATP-binding protein [EC:3.6.3.34]
>aai:AARI_33320 mrr; restriction system protein Mrr; K07448 restriction system protein
次のようなものがほしいです:
Metabolism Carbohydrate metabolism Glycolisis K07448 >aai:AARI_33320 mrr; restriction system protein Mrr; K07448 restriction system
Metabolism Protein metabolism proteasome K02217 >aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]
>aai:AARI_33320 mrr; restriction …
最初の行に追加されるテキストは の 8 行目であり、これはの 1 行目から ID フィールド (5 番目のフィールド) である をKEGG.annotations
含むものであることに注意してください。K07448
allKO.txt
パターン ファイルを使用するために、このコードをどのように変更すればよいでしょうか? これは、検索する特定のパターンを含む列が 1 つだけあるパターン ファイルで機能します。
while read pat; do
grep "$pat" --label="$pat" -H < KEGG.annotations;
done < allKO.txt > test1
答え1
すでに持っているコードを使って作業することもできます。行を配列に格納し、5 番目の要素に一致させます。
while read -r line; do
[ -z "$line" ] && continue
patlist=($line)
pat=${patlist[4]}
grep "$pat" --label="$line" -H < KEGG.annotations
done < allKO.txt
戻り値:
Metabolism Carbohydrate metabolism Glycolisis K07448:>aai:AARI_33320 mrr; restriction system protein Mrr; K07448 restriction system protein
Metabolism Protein metabolism protesome K02217:>aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]
答え2
これはあなたが求めているものを実現するようです:
while read w1 w2 w3 w4 ID
do
printf "%s " "$w1 $w2 $w3 $w4 $ID"
if ! grep "$ID" KEGG.annotations
then
echo
fi
done < allKO.txt
これにより、出力が画面に書き込まれます。出力をファイルにキャプチャするには、最後の行に出力 ( >
) リダイレクト (例) を追加します。> test1
- あなたの例に基づくと、キー/IDフィールド(「パターン」)は5番目の五ファイル内にフィールドがない
allKO.txt
ので、read w1 w2 w3 w4 ID
. これはタブ区切りのファイルだとおっしゃっていますが、どのフィールドにもスペースが含まれていないと想定しています。 printf
の行(つまり、フィールド)を( )で書き出しますallKO.txt
。末尾にスペースを入れますが、改行文字は入れません。- ファイル内で ID ( の行の 5 番目のフィールド)を検索します (
grep
) 。これらは完全な行 (改行を含む) になります。KEGG.annotations
allKO.txt
- が失敗した場合は
grep
、 が失敗したため、改行を書き込みますprintf
。 これにより、ID が存在しない行は
KEGG.annotations
そのまま出力に書き込まれることになります。Metabolism Protein metabolism proteasome K02217 >aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1] This ID doesn’t exist: K99999
複数存在する ID は追加行として書き込まれます ( のデータは繰り返されません
allKO.txt
)。Metabolism Protein metabolism proteasome K02217 >aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1] This is a hypothetical additional line from KEGG.annotations that mentions “K02217”.