【问题标题】:How to merge two files based on data in multiple columns?如何根据多列中的数据合并两个文件?
【发布时间】:2020-04-10 15:58:01
【问题描述】:

我有两个单独的文件,每个文件都包含不同数量的列,我想根据多列中的数据合并它们。

文件1

VMNF01000015.1  1769465 1769675 .   .   -   Focub_II5_mimp_1
VMNF01000014.1  3225875 3226081 .   .   +   Focub_II5_mimp_1
VMNF01000014.1  3226046 3226081 .   .   -   Focub_II5_mimp_1
VMNF01000014.1  3585246 3585281 .   .   -   Focub_II5_mimp_1
VMNF01000014.1  3692468 3692503 .   .   -   Focub_II5_mimp_1
VMNF01000014.1  3715380 3715415 .   .   +   Focub_II5_mimp_1
VMNF01000014.1  2872478 2872511 .   .   -   Focub_II5_mimp_1

文件2

VMNF01000014.1  3225875-3226081(+)  gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3226046-3226081(-)  tacacacctgcgaatactttttgcatcccactgta
VMNF01000015.1  1769465-1769675(-)  gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3692468-3692503(-)  tacagtgggatgcaaaaagtattcgcaggtgt
VMNF01000014.1  3715380-3715415(+)  gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3585246-3585281(-)  tacagtgggatgcaaaaagtattcgcaggtgt
VMNF01000014.1  2872478-2872511(-)  gtacttcagcctggattcaaacttattgcatcccactgta

首先,我想我需要在 file2 中创建另外 2 列,用“-”分隔数字并为“(*)”创建一个新列,但我无法弄清楚如何在不替换“(- )“ 也。到目前为止,我一直在使用这个命令:

awk '{gsub("-","\t",$2);print;}'

完成此操作后,我想将 file2 中的最后一列添加到 file1。我已经能够使用以下命令执行此操作:

awk 'NR==FNR {a[$1]=$3; next} {print $1,$2,$3,$4,$5,$6,$7,a[$1];}' file2 file1 > file3. 

但是,数据不匹配。根据第 1 列中的条目进行匹配。第 1 列中的数据在很多情况下都是相同的,因此 file3 的第 8 列中的数据仅匹配其中一个条目,与第 2 列或第 3 列中的数据不匹配在 file1 例如

文件3:

VMNF01000015.1  1769465 1769675 .   .   -   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3225875 3226081 .   .   +   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3226046 3226081 .   .   -   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3585246 3585281 .   .   -   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3692468 3692503 .   .   -   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  3715380 3715415 .   .   +   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta
VMNF01000014.1  2872478 2872511 .   .   -   Focub_II5_mimp_1    gtacttcagcctggattcaaacttattgcatcccactgta

即使我能够分离 file2 的第 2 列中的数据,我仍然会遇到相同的问题,因为在某些情况下第 2 列中的数据是相同的。我需要的是这样的代码:将第 2 列中的数据分开(见下文);

VMNF01000014.1  3225875    3226081    (+)   gtacttcagcctggattcaaacttattgcatcccactgta

然后:

如果 file1 中的 $1,$2,$3 与 file2 中的 $1,$2,$3 匹配,则从 file1 打印 $1,$2,$3,$4,$5,$6,$7 并从 file2 添加 $5。

我该怎么做?我知道 awk 可以使用 if 语句,但我不知道如何在 awk 中使用它们。

有什么建议吗?

【问题讨论】:

    标签: if-statement awk multiple-columns


    【解决方案1】:

    请您尝试关注一下。

    awk '
    FNR==NR{
      split($2,array,"[-(]")
      mainarray[$1,array[1],array[2]]=$NF
      next
    }
    (($1,$2,$3) in mainarray){
      print $0,mainarray[$1,$2,$3]
    }
    '  Input_file2  Input_file1
    

    第二个解决方案:由于 OP 在上面的代码中出现错误,所以在上面做了一点改动。

    awk '
    FNR==NR{
      split($2,array,"[-(]")
      key=$1 OFS array[1] OFS array[2]
      mainarray[key]=$NF
      next
    }
    { key = $1 OFS $2 OFS $3 }
    (key in mainarray){
      print $0,mainarray[key]
    }
    '  Input_file2  Input_file1
    

    说明:为上述代码添加详细说明。

    awk '                                       ##Starting awk program from here.
    FNR==NR{                                    ##Checking condition FNR==NR when  Input_file2 is being read.
      split($2,array,"[-(]")                    ##Splitting 2nd field into an array named array where delimiter is - OR (
      mainarray[$1,array[1],array[2]]=$NF       ##Creating mainarray index of $1,array[1],array[2] and value is current line is last field.
      next                                      ##next will skip all further statements from here.
    }
    (($1,$2,$3) in mainarray){                  ##Checking condition if $1,$2,$3 of current line is present in mainaarray.
      print $0,mainarray[$1,$2,$3]              ##Printing current line with value of mainarray with index of $1,$2,$3
    }
    '  Input_file2  Input_file1                 ##Mentioning Input_file names here.
    

    【讨论】:

    • 我注意到在某些情况下,(-) 或 (+) 也是确保第 8 列中的正确顺序所必需的,有没有办法也包括在内?可以通过向key添加另一个数组来完成吗?
    【解决方案2】:
    $ awk '
        { key=$1 OFS $2 OFS $3 }
        NR==FNR { map[key]=$NF; next }
        { print $0, map[key] }
    ' FS='[[:space:](-]+' file2 FS=' ' file1
    VMNF01000015.1  1769465 1769675 .   .   -   Focub_II5_mimp_1 gtacttcagcctggattcaaacttattgcatcccactgta
    VMNF01000014.1  3225875 3226081 .   .   +   Focub_II5_mimp_1 gtacttcagcctggattcaaacttattgcatcccactgta
    VMNF01000014.1  3226046 3226081 .   .   -   Focub_II5_mimp_1 tacacacctgcgaatactttttgcatcccactgta
    VMNF01000014.1  3585246 3585281 .   .   -   Focub_II5_mimp_1 tacagtgggatgcaaaaagtattcgcaggtgt
    VMNF01000014.1  3692468 3692503 .   .   -   Focub_II5_mimp_1 tacagtgggatgcaaaaagtattcgcaggtgt
    VMNF01000014.1  3715380 3715415 .   .   +   Focub_II5_mimp_1 gtacttcagcctggattcaaacttattgcatcccactgta
    VMNF01000014.1  2872478 2872511 .   .   -   Focub_II5_mimp_1 gtacttcagcctggattcaaacttattgcatcccactgta
    

    【讨论】:

      猜你喜欢
      • 2015-01-18
      • 2021-07-06
      • 2019-06-28
      • 2019-06-20
      • 1970-01-01
      • 2020-10-13
      • 2020-12-21
      • 2021-10-05
      • 2021-09-18
      相关资源
      最近更新 更多