Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 如何比较和合并多个文件?_Perl_Shell_Bioinformatics_Bioperl - Fatal编程技术网

Perl 如何比较和合并多个文件?

Perl 如何比较和合并多个文件?,perl,shell,bioinformatics,bioperl,Perl,Shell,Bioinformatics,Bioperl,参考文件 chr1 288598 288656 chr1 779518 779576 chr2 2569592 2569660 chr3 5018399 5018464 chr4 5182842 5182882 file1 chr1 288598 288656 12 chr1 779518 779576 14 chr2 2569592 2569660 26 chr3 5018399 5018464 27 chr4

参考文件

chr1    288598  288656

chr1    779518  779576

chr2    2569592 2569660

chr3    5018399 5018464

chr4    5182842 5182882
file1

chr1    288598  288656 12

chr1    779518  779576 14

chr2    2569592 2569660 26

chr3    5018399 5018464 27

chr4    5182842 5182882 37
文件2

chr1    288598  288656 35

chr2    2569592 2569660 348

chr3    5018399 5018464 4326

chr4    5182842 5182882 68
我有六个类似的文件,不包括参考文件

这里的前三个字段类似于参考文件。因此,我只想从所有6个文件中导出第4列,并将其放入参考文件中以生成新的输出。应与参考文件等效。如果它们不匹配,则将其归零

所需输出

chr1    288598  288656 23 35 57 68 769 68

chr1    779518  779576 23 0 57 68 768 0

chr2    2569592 2569660 23 35 0 68 79 0

chr3    5018399 5018464 0 36 0 68 769 0

chr4    5182842 5182882 23 0 0 0 0 0
注意:参考文件长度约为2000,而其他文件的长度并不总是相同(约为500、400、200、100等)。这就是为什么需要加零

我试着从他那里得到答案


但它似乎不起作用——有些价值观被忽略了。我不明白如何在没有匹配项的情况下加零。

我认为这样做应该可以满足您的需要。我们使用散列来收集“引用”文件,并将其转换为一组具有空数组的键

然后我们迭代其他文件,提取“3个值”作为键,最后一个值作为实际值

然后我们比较两者,用值或零更新“reference”散列。这里需要注意的是,任何不在引用文件中的行(或重复的行)都将消失

#!/usr/bin/perl

use strict;
use warnings;
use autodie;


#read 'reference file' into a hash:
my %ref;
open( my $ref_fh, "<", "reference_file" );
while (<$ref_fh>) {
    my ( $first, $second, $third ) = split;

    #turn the first three fields into space delimited key.
    $ref{"$first $second $third"} = ();
}

#open each of the files.
my @files = qw ( file1 file2 file3 file4 file5 file6 );
foreach my $input (@files) {
    open( my $input_fh, "<", $input );
    my %current;
    while (<$input_fh>) {

        #line by line, extract 'first 3 fields' to use as a key.
        #then 'value' which we store.
        my ( $first, $second, $third, $value ) = split;
        $current{"$first $second $third"} = $value;
    }

    #refer to 'reference file' and insert matching value or zero into
    #the array.
    foreach my $key ( keys %ref ) {
        push( @{ $ref{$key} }, $current{$key} ? $current{$key} : 0 );
    }
}

foreach my $key ( keys %ref ) {
    print join( " ", $key, @{ $ref{$key} } );
}
#/usr/bin/perl
严格使用;
使用警告;
使用自动模具;
#将“引用文件”读入哈希:
我的%ref;

打开(我的$ref\U fh,"提示:请看join命令。您的文件中真的有空行吗?我想没有,但为什么要这样显示它们?不,对不起……没有空行。对不起,不知道如何对多个文件使用join???同时考虑多个文件的列???出现错误。处理bed文件时,您只需要bed工具:非常感谢您的帮助我们很好的代码,它工作得很好,但是out-put需要为每一场比赛换一行。因为它都在一行中。非常感谢您的代码,它工作得很好,但是out-put需要为每一场比赛换一行和一个标签。然而,我已经完成了。再次感谢您宝贵的时间和帮助。
#!/usr/bin/perl

use strict;
use warnings;
use autodie;


#read 'reference file' into a hash:
my %ref;
open( my $ref_fh, "<", "reference_file" );
while (<$ref_fh>) {
    my ( $first, $second, $third ) = split;

    #turn the first three fields into space delimited key.
    $ref{"$first $second $third"} = ();
}

#open each of the files.
my @files = qw ( file1 file2 file3 file4 file5 file6 );
foreach my $input (@files) {
    open( my $input_fh, "<", $input );
    my %current;
    while (<$input_fh>) {

        #line by line, extract 'first 3 fields' to use as a key.
        #then 'value' which we store.
        my ( $first, $second, $third, $value ) = split;
        $current{"$first $second $third"} = $value;
    }

    #refer to 'reference file' and insert matching value or zero into
    #the array.
    foreach my $key ( keys %ref ) {
        push( @{ $ref{$key} }, $current{$key} ? $current{$key} : 0 );
    }
}

foreach my $key ( keys %ref ) {
    print join( " ", $key, @{ $ref{$key} } );
}