我从以下位置下载了一个制表符分隔的文件这里:
##gff-version 3
#!gff-spec-version 1.21
#!processor NCBI annotwriter
#!genome-build Nsyl
#!genome-build-accession NCBI_Assembly:GCF_000393655.1
#!annotation-source NCBI Nicotiana sylvestris Annotation Release 100
##sequence-region NW_009338801.1 1 504
##species https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=4096
NW_009338801.1 RefSeq region 1 504 . + . ID=NW_009338801.1:1..504;Dbxref=taxon:4096;Name=Unknown;bio-material=USDA:TW 136;chrom
osome=Unknown;gbkey=Src;genome=genomic;mol_type=genomic DNA;tissue-type=leaf
##sequence-region NW_009338802.1 1 9484
##species https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=4096
NW_009338802.1 RefSeq region 1 9484 . + . ID=NW_009338802.1:1..9484;Dbxref=taxon:4096;Name=Unknown;bio-material=USDA:TW 136;chro
mosome=Unknown;gbkey=Src;genome=genomic;mol_type=genomic DNA;tissue-type=leaf
##sequence-region NW_009338803.1 1 7523
##species https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=4096
NW_009338803.1 RefSeq region 1 7523 . + . ID=NW_009338803.1:1..7523;Dbxref=taxon:4096;Name=Unknown;bio-material=USDA:TW 136;chro
mosome=Unknown;gbkey=Src;genome=genomic;mol_type=genomic DNA;tissue-type=leaf
##sequence-region NW_009338804.1 1 46372
##species https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=4096
NW_009338804.1 RefSeq region 1 46372 . + . ID=NW_009338804.1:1..46372;Dbxref=taxon:4096;Name=Unknown;bio-material=USDA:TW 136;chr
omosome=Unknown;gbkey=Src;genome=genomic;mol_type=genomic DNA;tissue-type=leaf
NW_009338804.1 Gnomon pseudogene 32822 34172 . - . ID=gene-LOC104209938;Dbxref=GeneID:104209938;Name=LOC104209938;gbkey=Gene;gene
=LOC104209938;gene_biotype=pseudogene;pseudo=true
NW_009338804.1 Gnomon exon 32822 34172 . - . ID=id-LOC104209938-1;Parent=gene-LOC104209938;Dbxref=GeneID:104209938;exon_number=1;gb
key=exon;gene=LOC104209938;model_evidence=Supporting evidence includes similarity to: 2 Proteins;number=1
##sequence-region NW_009338805.1 1 53328
##species https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=4096
NW_009338805.1 RefSeq region 1 53328 . + . ID=NW_009338805.1:1..53328;Dbxref=taxon:4096;Name=Unknown;bio-material=USDA:TW 136;chr
omosome=Unknown;gbkey=Src;genome=genomic;mol_type=genomic DNA;tissue-type=leaf
NW_009338805.1 Gnomon gene 10570 12535 . - . ID=gene-LOC104217587;Dbxref=GeneID:104217587;Name=LOC104217587;gbkey=Gene;gene=LOC1042
17587;gene_biotype=protein_coding
NW_009338805.1 Gnomon mRNA 10570 12535 . - . ID=rna-XM_009770987.1;Parent=gene-LOC104217587;Dbxref=GeneID:104217587,Genbank:XM_0097
70987.1;Name=XM_009770987.1;gbkey=mRNA;gene=LOC104217587;model_evidence=Supporting evidence includes similarity to: 100%25 coverage of the annotated genomic f
eature by RNAseq alignments%2C including 2 samples with support for all annotated introns;product=ribosome-interacting GTPase 1-like;transcript_id=XM_00977098
7.1
NW_009338805.1 Gnomon exon 12140 12535 . - . ID=exon-XM_009770987.1-1;Parent=rna-XM_009770987.1;Dbxref=GeneID:104217587,Genbank:XM_
009770987.1;gbkey=mRNA;gene=LOC104217587;product=ribosome-interacting GTPase 1-like;transcript_id=XM_009770987.1
NW_009338805.1 Gnomon exon 11826 11939 . - . ID=exon-XM_009770987.1-2;Parent=rna-XM_009770987.1;Dbxref=GeneID:104217587,Genbank:XM_009770987.1;gbkey=mRNA;gene=LOC104217587;product=ribosome-interacting GTPase 1-like;transcript_id=XM_009770987.1
NW_009338805.1 Gnomon exon 11521 11695 . - . ID=exon-XM_009770987.1-3;Parent=rna-XM_009770987.1;Dbxref=GeneID:104217587,Genbank:XM_009770987.1;gbkey=mRNA;gene=LOC104217587;product=ribosome-interacting GTPase 1-like;transcript_id=XM_009770987.1
NW_009338805.1 Gnomon exon 10570 10889 . - . ID=exon-XM_009770987.1-4;Parent=rna-XM_009770987.1;Dbxref=GeneID:104217587,Genbank:XM_009770987.1;gbkey=mRNA;gene=LOC104217587;product=ribosome-interacting GTPase 1-like;transcript_id=XM_009770987.1
NW_009338805.1 Gnomon CDS 12140 12154 . - 0 ID=cds-XP_009769289.1;Parent=rna-XM_009770987.1;Dbxref=GeneID:104217587,Genbank:XP_009769289.1;Name=XP_009769289.1;gbkey=CDS;gene=LOC104217587;product=ribosome-interacting GTPase 1-like;protein_id=XP_009769289.1
NW_009338805.1 Gnomon CDS 11826 11939 . - 0 ID=cds-XP_009769289.1;Parent=rna-XM_009770987.1;Dbxref=GeneID:104217587,Genbank:XP_009769289.1;Name=XP_009769289.1;gbkey=CDS;gene=LOC104217587;product=ribosome-interacting GTPase 1-like;protein_id=XP_009769289.1
NW_009338805.1 Gnomon CDS 11521 11695 . - 0 ID=cds-XP_009769289.1;Parent=rna-XM_009770987.1;Dbxref=GeneID:104217587,Genbank:XP_009769289.1;Name=XP_009769289.1;gbkey=CDS;gene=LOC104217587;product=ribosome-interacting GTPase 1-like;protein_id=XP_009769289.1
NW_009338805.1 Gnomon CDS 10813 10889 . - 2 ID=cds-XP_009769289.1;Parent=rna-XM_009770987.1;Dbxref=GeneID:104217587,Genbank:XP_009769289.1;Name=XP_009769289.1;gbkey=CDS;gene=LOC104217587;product=ribosome-interacting GTPase 1-like;protein_id=XP_009769289.1
...
以下命令无法为不同的列名称创建多个文件,例如 NW_009592716.1.lst、NC_007500.1.lst、...
cat GCF_000393655.1_Nsyl_genomic.gff |awk '$3=="CDS"' |
sed 's/;/\t/g' |
awk '{print $1,$7,$12}' |
sed 's/Name=//g' |
awk 'substr($3,11,11)==1 {print $3$2,$1}' |
sort |
uniq |
awk '{print >> $2 ".lst"; close($2)}'
它只创建一个文件而不是多个文件:
$ head NC_007500.1.lst
YP_358649.1- NC_007500.1
YP_358650.1+ NC_007500.1
YP_358650.1- NC_007500.1
YP_358651.1- NC_007500.1
YP_358652.1- NC_007500.1
YP_358653.1- NC_007500.1
YP_358654.1+ NC_007500.1
YP_358655.1+ NC_007500.1
YP_358656.1+ NC_007500.1
YP_358657.1- NC_007500.1
...
如何使上述命令能够容忍不同的字符串长度?
先感谢您,
答案1
正如评论中提到的,应该可以将整个 AWK 和 sed 调用链实现为单个 AWK 程序。
要回答您提出的问题,要检查第三个字段的最后一个字符是否为“1”,您可以使用
$3 ~ /1$/
而不是substr
,所以在你的情况下
$3 ~ /1$/ {print $3$2,$1}
答案2
我想出
$3 == "CDS" && $1 ~ /1$/ {
split($9,A,";") ;
B=substr(A[4],6) ;
V[B $7] = $1 ;
}
END {
for (u in V) {
print u >> V[u] ;
close(V[u]) ;
}
}
结果是 17042 个文件。
$3 == "CDS" && $1 ~ /1$/
对于awk '$3=="CDS"'
和awk 'substr($3,11,11)==1
split($9,A,";") ;
对于sed 's/;/\t/g'
和awk '{print $1,$7,$12}'
B=substr(A[4],6) ;
为了sed 's/Name=//g'
V[B $7] = $1 ;
对于sort
和uniq
要执行脚本,请在 中插入代码filter.awk
,然后
awk -f filter.awk file_to_parse
答案3
使用gawk
和 变量tgt
来定义要选择的字段的名称......
awk -F"[\t;:,=]" -v tgt="Genbank" '$3=="CDS"{
for (f=4; f<=NF; f++) if ($f ~ tgt) {
if ( $(f+1) ~ /\.1$/ ) out[$(f+1)$7" "$1]=$1".lst"}}
END{PROCINFO["sorted_in"]="@ind_num_asc";
for (o in out) print o > out[o]}' GCF_000393655.1_Nsyl_genomic.gff
tail *.lst
==> NW_009592652.1.lst <==
XP_009779696.1- NW_009592652.1
==> NW_009592685.1.lst <==
XP_009779697.1+ NW_009592685.1
XP_009779699.1- NW_009592685.1
==> NW_009592688.1.lst <==
XP_009779700.1+ NW_009592688.1
XP_009779701.1+ NW_009592688.1
XP_009779702.1+ NW_009592688.1
==> NW_009592716.1.lst <==
XP_009779703.1+ NW_009592716.1
根据评论重复tgt="Parent"
并输入test.gff3
tail *.lst
==> NbV1Ch18.lst <==
NBlab18G26040.1+ NbV1Ch18
NBlab18G26050.1- NbV1Ch18
NBlab18G26060.1+ NbV1Ch18
NBlab18G26070.1+ NbV1Ch18
NBlab18G26080.1- NbV1Ch18
NBlab18G26090.1- NbV1Ch18
NBlab18G26100.1- NbV1Ch18
NBlab18G26110.1- NbV1Ch18
NBlab18G26120.1+ NbV1Ch18
NBlab18G26130.1+ NbV1Ch18
==> NbV1Ch19.lst <==
NBlab19G29030.1+ NbV1Ch19
NBlab19G29040.1- NbV1Ch19
NBlab19G29050.1- NbV1Ch19
NBlab19G29060.1- NbV1Ch19
NBlab19G29070.1+ NbV1Ch19
NBlab19G29080.1+ NbV1Ch19
NBlab19G29090.1- NbV1Ch19
NBlab19G29100.1- NbV1Ch19
NBlab19G29110.1- NbV1Ch19
NBlab19G29120.1- NbV1Ch19
演练
选择您所需的字段tgt
并首先选择记录CDS
awk -F"[\t;:,=]" -v tgt="Genbank" '$3=="CDS"{
迭代字段直到找到tgt
for (f=4; f<=NF; f++) if ($f ~ tgt) {
检查您的目标字段值是否$(f+1)
以结尾.1
,如果是,则将格式化输出存储在数组中,out
并使用您希望其转到的文件名值
if ( $(f+1) ~ /\.1$/ ) out[$(f+1)$7" "$1]=$1".lst"}}
完成后,然后设置awk
根据数组中索引的数字升序对输出数组进行迭代
END{PROCINFO["sorted_in"]="@ind_num_asc";
然后只需迭代数组,将所需的索引打印o
到相应的文件中out[o]
for (o in out) print o > out[o]}' GCF_000393655.1_Nsyl_genomic.gff