搜尋模式並將行附加到另一個文件

搜尋模式並將行附加到另一個文件

我有一個這樣的檔案(五個製表符分隔的列)

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 …附加到第一行的文字是 的第八行KEGG.annotations,其中包含K07448(它是 的第一行的 ID 欄位(第五個欄位)allKO.txt)。

如何修改此程式碼才能使用我的模式檔案?這適用於只有一列包含要尋找的特定模式的模式檔案。

while read pat; do
    grep "$pat" --label="$pat" -H < KEGG.annotations;
done < allKO.txt > test1

答案1

您可以使用現有的程式碼。將該行儲存到數組中並匹配第五個元素:

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 欄位(“模式”)是第五文件中的字段allKO.txt,所以我們read w1 w2 w3 w4 ID.你說這是一個製表符分隔的文件;我假設所有字段都不包含空格。
  • 寫入 ( printf) 來自 的行(即字段)allKO.txt,末尾有一個空格,但沒有終止換行符。
  • 在( grep)KEGG.annotations文件中搜尋 ID(來自 的行中的第五個欄位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”.
    

相關內容