【问题标题】:Merge tab-delimited files based on first 8 columns根据前 8 列合并制表符分隔的文件
【发布时间】:2018-03-06 09:18:13
【问题描述】:

我有一个制表符分隔的文件(我们称之为 file1),如下所示:

NC_027300.1 Gnomon  exon    5501    5691    .   -   .   gene_id "1"; transcript_id "1.1";
NC_027300.1 Gnomon  exon    16966   17019   .   -   .   gene_id "1"; transcript_id "1.1";
NC_027300.1 Gnomon  exon    23978   24241   .   -   .   gene_id "1"; transcript_id "1.1";
NC_027300.1 Gnomon  exon    43486   43714   .   -   .   gene_id "1"; transcript_id "1.1";
NC_027300.1 Gnomon  exon    61647   62139   .   -   .   gene_id "1"; transcript_id "1.1";
NC_027300.1 Gnomon  CDS 5501    5691    .   -   2   gene_id "1"; transcript_id "1.1";
NC_027300.1 Gnomon  CDS 16966   17019   .   -   2   gene_id "1"; transcript_id "1.1";
NC_027300.1 Gnomon  CDS 23978   24241   .   -   2   gene_id "1"; transcript_id "1.1";
NC_027300.1 Gnomon  CDS 43486   43633   .   -   0   gene_id "1"; transcript_id "1.1";
NC_027300.1 Gnomon  exon    160437  160638  .   -   .   gene_id "2"; transcript_id "2.1";
NC_027300.1 Gnomon  exon    160913  161019  .   -   .   gene_id "2"; transcript_id "2.1";

还有一个更大的制表符分隔文件(file2),如下所示:

NC_027300.1 Gnomon  gene    5501    62139   .   -   .   ID=gene0;Dbxref=GeneID:106560212;Name=LOC106560212;gbkey=Gene;gene=LOC106560212;gene_biotype=protein_coding
NC_027300.1 Gnomon  mRNA    5501    62139   .   -   .   ID=rna0;Parent=gene0;Dbxref=GeneID:106560212,Genbank:XM_014160784.1;Name=XM_014160784.1;gbkey=mRNA;gene=LOC106560212;model_evidence=Supporting evidence includes similarity to: 99%25 coverage of the annotated genomic feature by RNAseq alignments%2C including 8 samples with support for all annotated introns;product=fibroblast growth factor receptor 3-like;transcript_id=XM_014160784.1
NC_027300.1 Gnomon  exon    61647   62139   .   -   .   ID=id1;Parent=rna0;Dbxref=GeneID:106560212,Genbank:XM_014160784.1;gbkey=mRNA;gene=LOC106560212;product=fibroblast growth factor receptor 3-like;transcript_id=XM_014160784.1
NC_027300.1 Gnomon  exon    43486   43714   .   -   .   ID=id2;Parent=rna0;Dbxref=GeneID:106560212,Genbank:XM_014160784.1;gbkey=mRNA;gene=LOC106560212;product=fibroblast growth factor receptor 3-like;transcript_id=XM_014160784.1
NC_027300.1 Gnomon  exon    23978   24241   .   -   .   ID=id3;Parent=rna0;Dbxref=GeneID:106560212,Genbank:XM_014160784.1;gbkey=mRNA;gene=LOC106560212;product=fibroblast growth factor receptor 3-like;transcript_id=XM_014160784.1
NC_027300.1 Gnomon  exon    16966   17019   .   -   .   ID=id4;Parent=rna0;Dbxref=GeneID:106560212,Genbank:XM_014160784.1;gbkey=mRNA;gene=LOC106560212;product=fibroblast growth factor receptor 3-like;transcript_id=XM_014160784.1
NC_027300.1 Gnomon  exon    5501    5691    .   -   .   ID=id5;Parent=rna0;Dbxref=GeneID:106560212,Genbank:XM_014160784.1;gbkey=mRNA;gene=LOC106560212;product=fibroblast growth factor receptor 3-like;transcript_id=XM_014160784.1
NC_027300.1 Gnomon  CDS 43486   43633   .   -   0   ID=cds0;Parent=rna0;Dbxref=GeneID:106560212,Genbank:XP_014016259.1;Name=XP_014016259.1;gbkey=CDS;gene=LOC106560212;product=fibroblast growth factor receptor 3-like;protein_id=XP_014016259.1
NC_027300.1 Gnomon  CDS 23978   24241   .   -   2   ID=cds0;Parent=rna0;Dbxref=GeneID:106560212,Genbank:XP_014016259.1;Name=XP_014016259.1;gbkey=CDS;gene=LOC106560212;product=fibroblast growth factor receptor 3-like;protein_id=XP_014016259.1
NC_027300.1 Gnomon  CDS 16966   17019   .   -   2   ID=cds0;Parent=rna0;Dbxref=GeneID:106560212,Genbank:XP_014016259.1;Name=XP_014016259.1;gbkey=CDS;gene=LOC106560212;product=fibroblast growth factor receptor 3-like;protein_id=XP_014016259.1

我想创建一个新文件,该文件仅包含 file1 中也存在于 file2 中的行,基于前 8 列,其中包含 file1 的所有 9 列和 file2 的第 9 列作为第 10 列。像这样:

NC_027300.1 Gnomon  exon    5501    5691    .   -   .   gene_id "1"; transcript_id "1.1"; ID=id5;Parent=rna0;Dbxref=GeneID:106560212,Genbank:XM_014160784.1;gbkey=mRNA;gene=LOC106560212;product=fibroblast growth factor receptor 3-like;transcript_id=XM_014160784.1

我一直在尝试关注this example,这是(在我的知识非常有限的情况下)我想出的:

awk 'NR==FNR{a[$1,$2,$3,$4,$5,$6,$7,$8]=$10;next} ($1,$2,$3,$4,$5,$6,$7,$8) in a{print $0, a[$$1,$2,$3,$4,$5,$6,$7,$8]}' file1 file2 > newfile

如果我在附近,有人可以告诉我,如果有问题,可以帮忙吗?我的文件是 1M+ 行,目前正在运行,但我担心可能需要一段时间才能看到它是否正常工作!提前致谢

【问题讨论】:

    标签: unix text awk merge


    【解决方案1】:

    切换输入文件顺序并整理:

    awk '
    BEGIN { FS=OFS="\t" }
    { k = $1 FS $2 FS $3 FS $4 FS $5 FS $6 FS $7 FS $8 }
    NR==FNR { a[k]=$9; next }
    k in a { print $0, a[k] }
    ' file2 file1
    

    【讨论】:

    • 这对于较小的文件非常有效,但是当尝试在大文件(>700MB,>1M 行)上使用时,我会在一段时间后得到这个:分段错误(核心转储)。是否有替代 awk 可以更好地处理它的方法?也许是 Perl 脚本的替代品?
    • 很明显,这是因为您试图将大量文​​件读入内存。如果您在 perl 或 ruby​​ 或任何其他工具中实现此方法,您会遇到相同的问题,并且如果您采用不同的方法,它将在 awk 中工作,就像在 perl、ruby 等中一样。任何特定的处理工具都没有什么神奇之处文本。由于您发布此问题已有 4 个月了,因此现在没有人会重新访问它以尝试提出不同的方法,因此我建议您接受此答案,因为它修复了您的脚本,然后发布一个新问题与相关有关 segv 的信息。
    【解决方案2】:

    你走在正确的道路上,看起来你需要小修正

    改变

    a[$$1,$2,$3,$4,$5,$6,$7,$8]
      ^
     Here
    

    a[$1,$2,$3,$4,$5,$6,$7,$8]
    

    如果使用file1的第一个8个字段创建的数组a中存在由file2中的8个字段组成的索引键,它将打印数组a中的file1的第10个字段。

    【讨论】:

      猜你喜欢
      • 2014-09-22
      • 1970-01-01
      • 2016-06-10
      • 2013-02-27
      • 2018-08-05
      • 2022-01-09
      • 2018-07-10
      • 2018-08-20
      • 2011-05-30
      相关资源
      最近更新 更多