目標:將輸入檔案中的欄位列印到輸出檔案,但要特別注意用分號分隔的特定欄位(;請參閱下面示例輸入的欄位九)。
示例輸入(input.txt):
NC_051336.1 Gnomon gene 40042 56215 . - . gene_id "LOC102552844"; transcript_id ""; db_xref "GeneID:102552844"; db_xref "RGD:7551986"; gbkey "Gene"; gene "LOC102552844"; gene_biotype "pseudogene"; pseudo "true";
NC_051336.1 BestRefSeq,Gnomon gene 76909 85762 . . gene_id "Vom2r3"; transcript_id ""; db_xref "GeneID:502213"; db_xref "RGD:1565892"; description "vomeronasal 2 receptor, 3"; gbkey "Gene"; gene "Vom2r3"; gene_biotype "protein_coding"; gene_synonym "RGD1565892";
NC_051336.1 Gnomon gene 162525 192445 . - . gene_id "LOC103693496"; transcript_id ""; db_xref "GeneID:103693496"; db_xref "RGD:9090555"; gbkey "Gene"; gene "LOC103693496"; gene_biotype "protein_coding";
NC_051336.1 Gnomon transcript 162525 167758 . - . gene_id "LOC103693496"; transcript_id "XM_039098304.1"; db_xref "GeneID:103693496"; gbkey "mRNA"; gene "LOC103693496"; model_evidence "Supporting evidence includes similarity to: 4 Proteins, and 90% coverage of the annotated genomic feature by RNAseq alignments"; product "sperm motility kinase X-like, transcript variant X9"; transcript_biotype "mRNA";
NC_051336.1 BestRefSeq gene 226465 241532 . - . gene_id "Vom2r4"; transcript_id ""; db_xref "GeneID:308248"; db_xref "RGD:1564110"; description "vomeronasal 2 receptor, 4"; gbkey "Gene"; gene "Vom2r4"; gene_biotype "protein_coding"; gene_synonym "RGD1564110";
NC_051336.1 BestRefSeq transcript 226465 241532 . - . gene_id "Vom2r4"; transcript_id "NM_001099458.1"; db_xref "GeneID:308248"; gbkey "mRNA"; gene "Vom2r4"; product "vomeronasal 2 receptor, 4"; transcript_biotype "mRNA";
NC_051336.1 Curated Genomic gene 267100 275769 . - . gene_id "Vom2r-ps8"; transcript_id ""; db_xref "GeneID:502214"; db_xref "RGD:1563053"; description "vomeronasal 2 receptor, pseudogene 8"; gbkey "Gene"; gene "Vom2r-ps8"; gene_biotype "pseudogene"; gene_synonym "RGD1563053"; pseudo "true";
NC_051336.1 Gnomon gene 284195 301267 . - . gene_id "LOC102556157"; transcript_id ""; db_xref "GeneID:102556157"; db_xref "RGD:7626961"; gbkey "Gene"; gene "LOC102556157"; gene_biotype "protein_coding";
NC_051336.1 Gnomon gene 307758 313908 . - . gene_id "LOC108352169"; transcript_id ""; db_xref "GeneID:108352169"; db_xref "RGD:11507047"; gbkey "Gene"; gene "LOC108352169"; gene_biotype "lncRNA";
NC_051336.1 Gnomon transcript 307758 313908 . - . gene_id "LOC108352169"; transcript_id "XR_005497081.1"; db_xref "GeneID:108352169"; gbkey "ncRNA"; gene "LOC108352169"; model_evidence "Supporting evidence includes similarity to: 1 EST, and 98% coverage of the annotated genomic feature by RNAseq alignments, including 3 samples with support for all annotated introns"; product "uncharacterized LOC108352169, transcript variant X3"; transcript_biotype "lnc_RNA";
所需的輸出(output.txt):
LOC102552844 LOC102552844 NC_051336.1:40042-56215 NC_051336.1 40042 56215 - 16173 pseudogene
Vom2r3 Vom2r3 NC_051336.1:76909-85762 NC_051336.1 76909 85762 8853 protein_coding
LOC103693496 LOC103693496 NC_051336.1:162525-192445 NC_051336.1 162525 192445 - 29920 protein_coding
Vom2r4 Vom2r4 NC_051336.1:226465-241532 NC_051336.1 226465 241532 - 15067 protein_coding
Vom2r-ps8 Vom2r-ps8 NC_051336.1:267100-275769 NC_051336.1 267100 275769 - 8669 pseudogene
LOC102556157 LOC102556157 NC_051336.1:284195-301267 NC_051336.1 284195 301267 - 17072 protein_coding
LOC108352169 LOC108352169 NC_051336.1:307758-313908 NC_051336.1 307758 313908 - 6150 lncRNA
僅適用于 $3 為“gene”的輸入行...
第 1 列:$9 的“gene_id”子字串
第 2 列:$9 的“gene”子字串
第 3 列:$1 “:”$4 “-”$5
第 4 列:$1
第 5 列:$4
第 6 列:$5
第 7 列:$7
第 8 列:$5-$4
第 9 列:$9 的“gene_biotype”子串
嘗試:
1.
$ cat input.txt | awk ' # provide the input to awk
BEGIN{FS="\t"} # use tab as the field separator
{split($9,a,";"); # split field 9 into individual fields using semicolon as the field separator
if($3~"gene") # if the third field is "gene"
print a[1]"\t"a[6]"\t"$1":"$4"-"$5"\t"$1"\t"$4"\t"$5"\t"$7"\t"$5-$4"\t"a[7]}' | # print the indicated fields of each line in a tab-delimited fashion
sed 's/gene_id "//' | # remove any instance of the string gene_id "
sed 's/gene "//' | # remove any instance of the string gene "
sed 's/gene_biotype "//' | # remove any instance of the string gene_biotype "
sed 's/[" ]//g' > output.txt # remove any instance of the character " globally and send output to file
$ cat output.txt
LOC102552844 LOC102552844 NC_051336.1:40042-56215 NC_051336.1 40042 56215 - 16173 pseudogene
Vom2r3 gbkeyGene NC_051336.1:76909-85762 NC_051336.1 76909 85762 8853 Vom2r3
LOC103693496 LOC103693496 NC_051336.1:162525-192445 NC_051336.1 162525 192445 - 29920 protein_coding
Vom2r4 gbkeyGene NC_051336.1:226465-241532 NC_051336.1 226465 241532 - 15067 Vom2r4
Vom2r-ps8 gbkeyGene NC_051336.1:267100-275769 NC_051336.1 267100 275769 - 8669 Vom2r-ps8
LOC102556157 LOC102556157 NC_051336.1:284195-301267 NC_051336.1 284195 301267 - 17072 protein_coding
LOC108352169 LOC108352169 NC_051336.1:307758-313908 NC_051336.1 307758 313908 - 6150 lncRNA
這真的很接近我想要的......除了輸出線 2,4,5 不正確。我希望第二個輸出欄位是“ gene "”輸入欄位的子字串,第九個輸出欄位是“ gene_biotype "”輸入欄位的子字串(兩個所需的輸入欄位在第九個欄位的分號分隔系列中都有可變位置)。
雖然列印a[1]以提取“ gene_id "”欄位確實有效,因為它始終位于第 9 個以分號分隔的輸入欄位的第一個欄位中,但列印a[6]和a[7]提取上述其他欄位將不起作用,因為它們的索引位置會因某些行而改變(參見 2 ,4,5 的輸出)。顯而易見的解決方案是僅在包含感興趣的字串而不是特定索引位置時才列印欄位...
2.
$ cat $ANNOTATION | awk 'BEGIN{FS="\t"}{split($9,a,";"); if($3~"gene") print {for(i=1;i<=NF;i ){if($i~/gene_id "/){print a[$i]}}}"\t"{for(i=1;i<=NF;i ){if($i~/gene "/){print a[$i]}}}"\t"$1":"$4"-"$5"\t"$1"\t"$4"\t"$5"\t"$7"\t"$5-$4"\t"{for(i=1;i<=NF;i ){if($i~/gene_biotype "/){print a[$i]}}}}' | sed 's/gene_id "//' | sed 's/gene "//' | sed 's/gene_biotype "//' | sed 's/[" ]//g' > output.txt
$ cat output.txt
這個 awk 命令充滿了語法錯誤,因為使用{or}或;afterprint是不可接受的,因此該命令沒有執行并且沒有輸出。
總體思路是用 awk 友好的正則運算式代碼替換索引位置,以提取包含特定子字串的欄位:{for(i=1;i<=NF;i ){if($i~/gene_id "/){print a[$i]}}}. 但是,如果語法不正確,就像上面那樣,這將不起作用。
注意:我正在使用...
- 外殼版本:
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) - AWK 版本:
GNU Awk 4.0.2 - sed 版本:
sed (GNU sed) 4.2.2
uj5u.com熱心網友回復:
假設:
- 最后/第 9 個欄位始終包含
gene,gene_id和的條目gene_biotype(否則此答案將列印一個空字串,其中缺少的條目將顯示在輸出中)
一個awk想法:
awk '
BEGIN { FS=OFS="\t" }
$3 == "gene" { delete attr # reset attr[] array
split($NF,a,";") # split last field on ";"
for (i in a) { # loop through array
split(a[i],b,"\"") # split array entry on double quote; result: b[1]==attribute name, b[2]==attribute value; we do not care about b[3]
gsub(/ /,"",b[1]) # strip spaces from the attribute name
attr[b[1]]=b[2] # populate attr[] array
}
print attr["gene_id"],attr["gene"],$1":"$4"-"$5,$1,$4,$5,$7,($5-$4),attr["gene_biotype"]
}
' input.tsv
這會產生:
LOC102552844 LOC102552844 NC_051336.1:40042-56215 NC_051336.1 40042 56215 - 16173 pseudogene
Vom2r3 Vom2r3 NC_051336.1:76909-85762 NC_051336.1 76909 85762 8853 protein_coding
LOC103693496 LOC103693496 NC_051336.1:162525-192445 NC_051336.1 162525 192445 - 29920 protein_coding
Vom2r4 Vom2r4 NC_051336.1:226465-241532 NC_051336.1 226465 241532 - 15067 protein_coding
Vom2r-ps8 Vom2r-ps8 NC_051336.1:267100-275769 NC_051336.1 267100 275769 - 8669 pseudogene
LOC102556157 LOC102556157 NC_051336.1:284195-301267 NC_051336.1 284195 301267 - 17072 protein_coding
LOC108352169 LOC108352169 NC_051336.1:307758-313908 NC_051336.1 307758 313908 - 6150 lncRNA
uj5u.com熱心網友回復:
@Gawain:我沒有驗證所有邊緣情況,但是您可以使用它regex來快速將輸入拆分為分立組件。
從那里應該是微不足道的。
echo "${input}" |
{m,g}awk NF=NF OFS='\f\r\t' ORS='\n^^^\n' \
FS='[\t ]*[.][ \t]*|(\42[ \t]*[;][ \t\42]*|[,\42:] |[ \t][ \t] )*'
NC_051336
1
Gnomon
gene
40042
56215
-
gene_id
LOC102552844
transcript_id
db_xref
GeneID
102552844
db_xref
RGD
7551986
gbkey
Gene
gene
LOC102552844
gene_biotype
pseudogene
pseudo
true
^^^
NC_051336
1
BestRefSeq,Gnomon
gene
76909
85762
gene_id
Vom2r3
transcript_id
db_xref
GeneID
502213
db_xref
RGD
1565892
description
vomeronasal 2 receptor
3
gbkey
Gene
gene
Vom2r3
gene_biotype
protein_coding
gene_synonym
RGD1565892
^^^
NC_051336
1
Gnomon
gene
162525
192445
-
gene_id
LOC103693496
transcript_id
db_xref
GeneID
103693496
db_xref
RGD
9090555
gbkey
Gene
gene
LOC103693496
gene_biotype
protein_coding
^^^
NC_051336
1
Gnomon
transcript
162525
167758
-
gene_id
LOC103693496
transcript_id
XM_039098304
1
db_xref
GeneID
103693496
gbkey
mRNA
gene
LOC103693496
model_evidence
Supporting evidence includes similarity to
4 Proteins
and 90% coverage of the annotated genomic feature by RNAseq alignments
product
sperm motility kinase X-like
transcript variant X9
transcript_biotype
mRNA
^^^
NC_051336
1
BestRefSeq
gene
226465
241532
-
gene_id
Vom2r4
transcript_id
db_xref
GeneID
308248
db_xref
RGD
1564110
description
vomeronasal 2 receptor
4
gbkey
Gene
gene
Vom2r4
gene_biotype
protein_coding
gene_synonym
RGD1564110
^^^
NC_051336
1
BestRefSeq
transcript
226465
241532
-
gene_id
Vom2r4
transcript_id
NM_001099458
1
db_xref
GeneID
308248
gbkey
mRNA
gene
Vom2r4
product
vomeronasal 2 receptor
4
transcript_biotype
mRNA
^^^
NC_051336
1
Curated Genomic gene
267100
275769
-
gene_id
Vom2r-ps8
transcript_id
db_xref
GeneID
502214
db_xref
RGD
1563053
description
vomeronasal 2 receptor
pseudogene 8
gbkey
Gene
gene
Vom2r-ps8
gene_biotype
pseudogene
gene_synonym
RGD1563053
pseudo
true
^^^
NC_051336
1
Gnomon
gene
284195
301267
-
gene_id
LOC102556157
transcript_id
db_xref
GeneID
102556157
db_xref
RGD
7626961
gbkey
Gene
gene
LOC102556157
gene_biotype
protein_coding
^^^
NC_051336
1
Gnomon
gene
307758
313908
-
gene_id
LOC108352169
transcript_id
db_xref
GeneID
108352169
db_xref
RGD
11507047
gbkey
Gene
gene
LOC108352169
gene_biotype
lncRNA
^^^
NC_051336
1
Gnomon
transcript
307758
313908
-
gene_id
LOC108352169
transcript_id
XR_005497081
1
db_xref
GeneID
108352169
gbkey
ncRNA
gene
LOC108352169
model_evidence
Supporting evidence includes similarity to
1 EST
and 98% coverage of the annotated genomic feature by RNAseq alignments
including 3 samples with support for all annotated introns
product
uncharacterized LOC108352169
transcript variant X3
transcript_biotype
lnc_RNA
^^^
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/496584.html
上一篇:單擊外部和鏈接時如何關閉導航欄
