特定の文字列を含む連続行を検索し、テーブルに基づいてファイルを変更します。

特定の文字列を含む連続行を検索し、テーブルに基づいてファイルを変更します。

テキスト操作に関する問題があり、解決できません。次のようなテキスト ファイル (text.txt) があるとします。 の行の後/locus_tagに の行が続く場合/geneと、そうでない場合があります。/locus_tagの後に が続かないすべての行を検索し/gene、次のような表 (table.txt) を使用して を に一致させて/locus_tag/geneそれを/geneテキスト ファイルの の後に追加します/locus_tag

これを実行する方法についてのアイデアがあれば、ぜひ教えてください。

/locus_tag="LOCUS_23770"
/note="ABC"
/locus_tag="LOCUS_23780"
/note="DEF"
/locus_tag="LOCUS_23980"
/note="GHI"
/locus_tag="LOCUS_24780"
/gene="BT_4758"
/note="ONP"
/locus_tag="LOCUS_25780"
/gene="BT_4768"
/note="WZX"

テーブル

/locus_tag       /gene
LOCUS_00010      BT_4578
LOCUS_00020      BT_4577
LOCUS_00030      BT_2429

答え1

リンクされたファイルを使用すると、これは機能します

awk 'BEGIN{FS="[ =]+"; OFS="="}
     BEGINFILE{fno++}
     fno==1{locus["\""$1"\""]="\""$2"\""; }
     fno>1{if (old ~ /LOCUS/ && $0 !~ /gene/) print "/gene", locus[old]; old=$3; print}
    ' table file1

前に

                     /locus_tag="LOCUS_00030"
                     /note="WP_011108293.1 hypothetical protein (Bacteroides

                     /locus_tag="LOCUS_00030"
/gene="BT_2429"
                     /note="WP_011108293.1 hypothetical protein (Bacteroides

awkウォークスルーに慣れていないので

awk 'BEGIN{FS="[ =]+"; OFS="="}
# set up the input field separator as any group of spaces and/or =
# and set the output field separator as =

     BEGINFILE{fno++}
     # Whenever you open a file, increment the file counter fno

     fno==1{locus["\""$1"\""]="\""$2"\""; }
     # if this is the first file (i.e. table) load the array `locus[]`
     # but wrap the fields in "..." so that they are exactly like the data file entries

     fno>1{if (old ~ /LOCUS/ && $0 !~ /gene/) print "/gene", locus[old]; old=$3; print}
     # if this is a data file
     # if the current value of old (i.e. the previous line) is a LOCUS
     # and && this line ($0) isn't a gene
     # add a gene by indexing into the locus array based upon the value of old
     # because old contains the last LOCUS we found
     # in all cases
     #    set old to the 3rd field on the current line,
     #       which on any LOCUS line is the string "LOCUS_?????" and
     #    print the current line
     # See note below re $2 vs $3 and FS

    ' table file1
    # your input files, table must be first, you can have more data files if you want

または、マルチ文字を使用しない場合は、データ ファイル内のテキストの前の空白で分割されないため、マルチ文字では分割されますが、マルチ文字では分割されないため、FSそのold=$2ままにします。

以下は、読み込んでいるファイルに応じてフィールドセパレーターを設定しますFS=(fno==1)?" ":"="。テーブルと=データ用のスペース

awk 'BEGIN{OFS="="}
     BEGINFILE{fno++;FS=(fno==1)?" ":"="}
     fno==1{locus["\""$1"\""]="\""$2"\""; }
     fno>1{if (old ~ /LOCUS/ && $0 !~ /gene/) print "/gene", locus[old]; old=$2; print}
    ' table file1

ただし、テーブル ファイルはメモリを消費するほど大きくありません。

そして、空っぽの遺伝子だけではなく、欠けている遺伝子にメッセージを挿入するテストを実施します。/gene=

fno>1{if (old ~ /LOCUS/ && $0 !~ /gene/) print "/gene", (old in locus)?locus[old]:"\"GENE_MISSING_AT_LOCUS\""; old=$3; print}

使用しているoldのバージョンに合わせてフィールド参照を変更します。FS

                     /locus_tag="LOCUS_00020"
/gene="GENE_MISSING_AT_LOCUS"
                     /note="WP_008765457.1 hypothetical protein (Bacteroides

編集

リンク先のサンプル ファイルを見ると、上記のサンプルと実際のデータとのフォーマットの違いがフィールド番号を混乱させているという問題があります。old=$2を に変更するだけで済みますold=$3。上記を修正しました。

関連情報