web-dev-qa-db-ja.com

awkは文字列をカットします

以下のコマンドを実行すると、文字列全体が出力されます。 Note="Peptidase S59%2C nucleoporin"

awk '$3=="mRNA"'  Nitab-v4.5_gene_models_Chr_Edwards2017.gff | head 
Nt01    maker   mRNA    143295  155540  .   +   .   ID=Nitab4.5_0006317g0010.1;Parent=Nitab4.5_0006317g0010;Name=Nitab4.5_0006317g0010.1;_AED=0.08;_eAED=0.08;_QI=0|0.45|0.25|1|0.90|0.75|12|0|1011;Note="Peptidase S59%2C nucleoporin"
Nt01    maker   mRNA    170633  173860  .   +   .   ID=Nitab4.5_0006317g0020.1;Parent=Nitab4.5_0006317g0020;Name=Nitab4.5_0006317g0020.1;_AED=0.26;_eAED=0.26;_QI=15|0|0|0.83|0.6|0.33|6|0|424;Note="Putative S-adenosyl-L-methionine-dependent methyltransferase"
Nt01    maker   mRNA    156516  160996  .   -   .   ID=Nitab4.5_0006317g0030.1;Parent=Nitab4.5_0006317g0030;Name=Nitab4.5_0006317g0030.1;_AED=0.01;_eAED=0.01;_QI=161|1|1|1|0|0.5|2|358|141;Note="Unknown"
Nt01    maker   mRNA    78554   80638   .   -   .   ID=Nitab4.5_0006317g0040.1;Parent=Nitab4.5_0006317g0040;Name=Nitab4.5_0006317g0040.1;_AED=0.02;_eAED=0.02;_QI=0|0|0|1|1|1|3|0|187;Note="Heavy metal-associated domain%2C HMA"
Nt01    maker   mRNA    111288  129916  .   -   .   ID=Nitab4.5_0006317g0050.1;Parent=Nitab4.5_0006317g0050;Name=Nitab4.5_0006317g0050.1;_AED=0.24;_eAED=0.24;_QI=0|0|0|0.5|1|1|2|0|72;Note="Unknown"
Nt01    maker   mRNA    470560  474346  .   +   .   ID=Nitab4.5_0002367g0010.1;Parent=Nitab4.5_0002367g0010;Name=Nitab4.5_0002367g0010.1;_AED=0.11;_eAED=0.11;_QI=0|0|0|1|1|1|14|0|668;Note="Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation"
Nt01    maker   mRNA    499946  502182  .   +   .   ID=Nitab4.5_0002367g0020.1;Parent=Nitab4.5_0002367g0020;Name=Nitab4.5_0002367g0020.1;_AED=0.26;_eAED=0.26;_QI=0|0.5|0|0.66|0|0|3|0|258;Note="Cellulose synthase"
Nt01    maker   mRNA    496891  497596  .   +   .   ID=Nitab4.5_0002367g0030.1;Parent=Nitab4.5_0002367g0030;Name=Nitab4.5_0002367g0030.1;_AED=0.33;_eAED=0.33;_QI=0|0|0|0.5|0|0.5|2|0|213;Note="Cellulose synthase"
Nt01    maker   mRNA    505125  506853  .   -   .   ID=Nitab4.5_0002367g0040.1;Parent=Nitab4.5_0002367g0040;Name=Nitab4.5_0002367g0040.1;_AED=0.09;_eAED=0.09;_QI=0|0|0|1|0.5|0.66|3|0|230;Note="Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type"
Nt01    maker   mRNA    564383  570328  .   +   .   ID=Nitab4.5_0002367g0050.1;Parent=Nitab4.5_0002367g0050;Name=Nitab4.5_0002367g0050.1;_AED=0.08;_eAED=0.08;_QI=75|1|1|1|1|1|6|146|267;Note="SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12"

ただし、次のコマンドを使用すると、文字列はたとえばに短縮されます。 Note = "ペプチダーゼ

awk '$3=="mRNA"'  Nitab-v4.5_gene_models_Chr_Edwards2017.gff | awk '{print $9}' | head 
ID=Nitab4.5_0006317g0010.1;Parent=Nitab4.5_0006317g0010;Name=Nitab4.5_0006317g0010.1;_AED=0.08;_eAED=0.08;_QI=0|0.45|0.25|1|0.90|0.75|12|0|1011;Note="Peptidase
ID=Nitab4.5_0006317g0020.1;Parent=Nitab4.5_0006317g0020;Name=Nitab4.5_0006317g0020.1;_AED=0.26;_eAED=0.26;_QI=15|0|0|0.83|0.6|0.33|6|0|424;Note="Putative
ID=Nitab4.5_0006317g0030.1;Parent=Nitab4.5_0006317g0030;Name=Nitab4.5_0006317g0030.1;_AED=0.01;_eAED=0.01;_QI=161|1|1|1|0|0.5|2|358|141;Note="Unknown"
ID=Nitab4.5_0006317g0040.1;Parent=Nitab4.5_0006317g0040;Name=Nitab4.5_0006317g0040.1;_AED=0.02;_eAED=0.02;_QI=0|0|0|1|1|1|3|0|187;Note="Heavy
ID=Nitab4.5_0006317g0050.1;Parent=Nitab4.5_0006317g0050;Name=Nitab4.5_0006317g0050.1;_AED=0.24;_eAED=0.24;_QI=0|0|0|0.5|1|1|2|0|72;Note="Unknown"
ID=Nitab4.5_0002367g0010.1;Parent=Nitab4.5_0002367g0010;Name=Nitab4.5_0002367g0010.1;_AED=0.11;_eAED=0.11;_QI=0|0|0|1|1|1|14|0|668;Note="Auxin
ID=Nitab4.5_0002367g0020.1;Parent=Nitab4.5_0002367g0020;Name=Nitab4.5_0002367g0020.1;_AED=0.26;_eAED=0.26;_QI=0|0.5|0|0.66|0|0|3|0|258;Note="Cellulose
ID=Nitab4.5_0002367g0030.1;Parent=Nitab4.5_0002367g0030;Name=Nitab4.5_0002367g0030.1;_AED=0.33;_eAED=0.33;_QI=0|0|0|0.5|0|0.5|2|0|213;Note="Cellulose
ID=Nitab4.5_0002367g0040.1;Parent=Nitab4.5_0002367g0040;Name=Nitab4.5_0002367g0040.1;_AED=0.09;_eAED=0.09;_QI=0|0|0|1|0.5|0.66|3|0|230;Note="Zinc
ID=Nitab4.5_0002367g0050.1;Parent=Nitab4.5_0002367g0050;Name=Nitab4.5_0002367g0050.1;_AED=0.08;_eAED=0.08;_QI=75|1|1|1|1|1|6|146|267;Note="SAC3/GANP/Nin1/mts3/eIF-3

最終結果として、Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporinを取得したいと思います。

私は何を取りこぼしたか?

前もって感謝します

2
user977828

GFFはタブ区切り形式ですが、タブを使用していません。 -F'\t'またはBEGIN{FS="\t"}を使用しない限り、awkはフィールド区切り文字として空白を使用します。これにはスペースが含まれます。スペースを削減しているので、$9は最初のスペースで終わります。また、次の2つのコマンドは必要ありません。

$ awk -F'\t' '$3=="mRNA"{print $9}' file.gff 
ID=Nitab4.5_0006317g0010.1;Parent=Nitab4.5_0006317g0010;Name=Nitab4.5_0006317g0010.1;_AED=0.08;_eAED=0.08;_QI=0|0.45|0.25|1|0.90|0.75|12|0|1011;Note="Peptidase S59%2C nucleoporin"
ID=Nitab4.5_0006317g0020.1;Parent=Nitab4.5_0006317g0020;Name=Nitab4.5_0006317g0020.1;_AED=0.26;_eAED=0.26;_QI=15|0|0|0.83|0.6|0.33|6|0|424;Note="Putative S-adenosyl-L-methionine-dependent methyltransferase"
ID=Nitab4.5_0006317g0030.1;Parent=Nitab4.5_0006317g0030;Name=Nitab4.5_0006317g0030.1;_AED=0.01;_eAED=0.01;_QI=161|1|1|1|0|0.5|2|358|141;Note="Unknown"
ID=Nitab4.5_0006317g0040.1;Parent=Nitab4.5_0006317g0040;Name=Nitab4.5_0006317g0040.1;_AED=0.02;_eAED=0.02;_QI=0|0|0|1|1|1|3|0|187;Note="Heavy metal-associated domain%2C HMA"
ID=Nitab4.5_0006317g0050.1;Parent=Nitab4.5_0006317g0050;Name=Nitab4.5_0006317g0050.1;_AED=0.24;_eAED=0.24;_QI=0|0|0|0.5|1|1|2|0|72;Note="Unknown"
ID=Nitab4.5_0002367g0010.1;Parent=Nitab4.5_0002367g0010;Name=Nitab4.5_0002367g0010.1;_AED=0.11;_eAED=0.11;_QI=0|0|0|1|1|1|14|0|668;Note="Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation"
ID=Nitab4.5_0002367g0020.1;Parent=Nitab4.5_0002367g0020;Name=Nitab4.5_0002367g0020.1;_AED=0.26;_eAED=0.26;_QI=0|0.5|0|0.66|0|0|3|0|258;Note="Cellulose synthase"
ID=Nitab4.5_0002367g0030.1;Parent=Nitab4.5_0002367g0030;Name=Nitab4.5_0002367g0030.1;_AED=0.33;_eAED=0.33;_QI=0|0|0|0.5|0|0.5|2|0|213;Note="Cellulose synthase"
ID=Nitab4.5_0002367g0040.1;Parent=Nitab4.5_0002367g0040;Name=Nitab4.5_0002367g0040.1;_AED=0.09;_eAED=0.09;_QI=0|0|0|1|0.5|0.66|3|0|230;Note="Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type"
ID=Nitab4.5_0002367g0050.1;Parent=Nitab4.5_0002367g0050;Name=Nitab4.5_0002367g0050.1;_AED=0.08;_eAED=0.08;_QI=75|1|1|1|1|1|6|146|267;Note="SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12"

Note=の値のみを取得したい場合は、次のようにします。

$ awk -F"\t" '$3=="mRNA"{sub(/.*Note=/,"",$9); print $9}' file.gff 
"Peptidase S59%2C nucleoporin"
"Putative S-adenosyl-L-methionine-dependent methyltransferase"
"Unknown"
"Heavy metal-associated domain%2C HMA"
"Unknown"
"Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation"
"Cellulose synthase"
"Cellulose synthase"
"Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type"
"SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12"

また、ノート自体の一部である可能性のあるものをすべて保持しながら、ノートの最初と最後の引用符を削除するには:

$ awk -F"\t" '$3=="mRNA"{sub(/.*Note=/,"",$9); print $9}' file.gff | sed 's/^"//; s/"$//'
Peptidase S59%2C nucleoporin
Putative S-adenosyl-L-methionine-dependent methyltransferase
Unknown
Heavy metal-associated domain%2C HMA
Unknown
Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation
Cellulose synthase
Cellulose synthase
Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type
SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12

最後に、NoteIDの両方の値を取得するには、次のようにします。

$ awk -F"\t" '$3=="mRNA"{n=$9; sub(/.*Note="/,"",n); sub(/"$/,"",n); sub(/.*ID=/,"",$9); sub(/;.*/,"",$9); print $9","n}' file.gff 
Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin
Nitab4.5_0006317g0020.1,Putative S-adenosyl-L-methionine-dependent methyltransferase
Nitab4.5_0006317g0030.1,Unknown
Nitab4.5_0006317g0040.1,Heavy metal-associated domain%2C HMA
Nitab4.5_0006317g0050.1,Unknown
Nitab4.5_0002367g0010.1,Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation
Nitab4.5_0002367g0020.1,Cellulose synthase
Nitab4.5_0002367g0030.1,Cellulose synthase
Nitab4.5_0002367g0040.1,Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type
Nitab4.5_0002367g0050.1,SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12

ただし、個人的には、代わりにPerlでこれを行います。

$ Perl -F'\t' -lane 'if($F[2] eq "mRNA"){/ID=([^\;]+).*Note="([^"]+)/; print "$1,$2"}' file.gff 
Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin
Nitab4.5_0006317g0020.1,Putative S-adenosyl-L-methionine-dependent methyltransferase
Nitab4.5_0006317g0030.1,Unknown
Nitab4.5_0006317g0040.1,Heavy metal-associated domain%2C HMA
Nitab4.5_0006317g0050.1,Unknown
Nitab4.5_0002367g0010.1,Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation
Nitab4.5_0002367g0020.1,Cellulose synthase
Nitab4.5_0002367g0030.1,Cellulose synthase
Nitab4.5_0002367g0040.1,Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type
Nitab4.5_0002367g0050.1,SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome 
7
terdon

各行の終わりの引用符で囲まれた文字列にタブまたは;を含めることができないとすると、おそらくこれだけで十分です。

$ awk -F'\t' -v OFS=',' '$3=="mRNA"{ gsub(/;/,FS); gsub(/[^\t=]+=|"/,""); print $9, $15 }' file
Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin
Nitab4.5_0006317g0020.1,Putative S-adenosyl-L-methionine-dependent methyltransferase
Nitab4.5_0006317g0030.1,Unknown
Nitab4.5_0006317g0040.1,Heavy metal-associated domain%2C HMA
Nitab4.5_0006317g0050.1,Unknown
Nitab4.5_0002367g0010.1,Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimeriion
Nitab4.5_0002367g0020.1,Cellulose synthase
Nitab4.5_0002367g0030.1,Cellulose synthase
Nitab4.5_0002367g0040.1,Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type
Nitab4.5_0002367g0050.1,SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12

または(元のアイデア):

$ cat tst.awk
BEGIN { FS="\t"; OFS="," }
$3 == "mRNA" {
    split($NF,f,/;/)
    for (i in f) {
        gsub(/^[^=]+="?|"$/,"",f[i])
    }
    print f[1], f[7]
}

$ awk -f tst.awk file
Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin
Nitab4.5_0006317g0020.1,Putative S-adenosyl-L-methionine-dependent methyltransferase
Nitab4.5_0006317g0030.1,Unknown
Nitab4.5_0006317g0040.1,Heavy metal-associated domain%2C HMA
Nitab4.5_0006317g0050.1,Unknown
Nitab4.5_0002367g0010.1,Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation
Nitab4.5_0002367g0020.1,Cellulose synthase
Nitab4.5_0002367g0030.1,Cellulose synthase
Nitab4.5_0002367g0040.1,Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type
Nitab4.5_0002367g0050.1,SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12

,を含めることができる場合は、OFSとして,を使用することを再検討し、代わりにタブまたは;を使用する必要があります。

5
Ed Morton

「最終結果」の出力形式が_ID=_および_Note=_フィールドから派生すると想定すると、次のように機能します。

_$ awk -F'\t' '$3=="mRNA"{split($9,f,";"); split(f[1],i,"="); split(f[7],n,"\""); printf("%s,%s\n",i[2],n[2])}' file.gff
Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin
Nitab4.5_0006317g0020.1,Putative S-adenosyl-L-methionine-dependent methyltransferase
Nitab4.5_0006317g0030.1,Unknown
Nitab4.5_0006317g0040.1,Heavy metal-associated domain%2C HMA
Nitab4.5_0006317g0050.1,Unknown
Nitab4.5_0002367g0010.1,Auxin response factor%2C B3 DNA binding domain%2C DNA-binding pseudobarrel domain%2C AUX/IAA protein%2C Aux/IAA-ARF-dimerisation
Nitab4.5_0002367g0020.1,Cellulose synthase
Nitab4.5_0002367g0030.1,Cellulose synthase
Nitab4.5_0002367g0040.1,Zinc finger%2C RING-type%2C Zinc finger%2C RING/FYVE/PHD-type
Nitab4.5_0002367g0050.1,SAC3/GANP/Nin1/mts3/eIF-3 p25%2C 26S proteasome non-ATPase regulatory subunit Rpn12

_

これは、9番目のタブ区切りフィールドを配列fに格納されている_;_区切りのサブフィールドに分割します。最初のサブフィールド(_ID=..._)は_=_および_Note=" ... "_の7番目のサブフィールド(_"_)は、興味深い部分をそれぞれ補助配列iおよびnに分離します。次に、これらの関連部分が印刷されます。

これにより、コードはかなりコンパクトになりますが、必ずしも最も効率的な方法ではありません。

3
AdminBee

GNU awkを使用:

awk -F'\t' -vOFS=, 'NF>=9&&$3=="mRNA"{
N = split($9, a, /[=;]/)
for ( i=1; i<=N; i+=2 )
    h[a[i]] = a[i+1]
print h["ID"], gensub(/"(.*)"/, "\\1", "g", h["Note"])
}' bioinformatic.file

Nitab4.5_0006317g0010.1,Peptidase S59%2C nucleoporin
1
Rakesh Sharma