Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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
Regex perl打印两个文件中具有匹配字段的行,错误:在字符串中使用未初始化值_Regex_Perl_Matching - Fatal编程技术网

Regex perl打印两个文件中具有匹配字段的行,错误:在字符串中使用未初始化值

Regex perl打印两个文件中具有匹配字段的行,错误:在字符串中使用未初始化值,regex,perl,matching,Regex,Perl,Matching,我有两个文件,看起来像: 文件1: chr id position a0 a1 22 rs4820378:39869209:C:T 39869209 C T 22 22:16050075:A:G 16050075 A G 22 22:16050115:G:A 16050115 G A 22 rs199694733:39913976:C:CT 39913976 C CT 22 rs139408809:39937958:GC:G 39937958 GC G 文件2: SNP CHR BP A1 A

我有两个文件,看起来像:

文件1:

chr id position a0 a1
22 rs4820378:39869209:C:T 39869209 C T
22 22:16050075:A:G 16050075 A G
22 22:16050115:G:A 16050115 G A
22 rs199694733:39913976:C:CT 39913976 C CT
22 rs139408809:39937958:GC:G 39937958 GC G
文件2:

SNP CHR BP A1 A2
rs4820378 22 39869209 C T
rs4821900 22 39869719 G A
rs1984662 22 39869997 T G
rs35629588 22 39913976 I2 D
rs139408809 22 39937958 D I2
我想在哪里找到线路

  • 文件1中的字段1和3与文件2中的字段2和3匹配
而且

  • 文件1中的字段4和5与文件2中的字段4和5匹配

  • 两个文件中的字段4都超过1个字符

  • 两个文件中的字段5都超过1个字符

然后打印文件1中的字段2,以及文件2中的字段1和3

代码如下

#! perl -w

use strict;
use warnings;

my %kglocus;
open( my $loci_in, "<", "File1" ) or die $!;
while ( <$loci_in> ) {

    next if m/chr/;

    my ( $CHR, $id, $BP, $A1, $A2 ) = split;
    my $reg = "${CHR}_$BP";
    $kglocus{$reg} = [ $CHR, $id, $BP, $A1, $A2 ];
}
close $loci_in;

my $filename = shift @ARGV;
open( my $input, "<", $filename ) or die $!;
while ( <$input> ) {
    next if m/SNP/;

    my ( $SNP, $CHR, $BP, $A1, $A2 ) = split;
    my $reg = "${CHR}_$BP";

    if ( $A1 eq $kglocus{$reg}->[3] and $A2 eq $kglocus{$reg}->[4] ) {

        print "$kglocus{$reg}->[1] $SNP $BP\n";
    }
    elsif ( ( length( $A1 ) > 1 && length( $kglocus{$reg}->[3] ) > 1 ) ||
            ( length( $A2 ) > 1 && length( $kglocus{$reg}->[4] ) > 1 ) ) {

        print "$kglocus{$reg}->[1] $SNP $BP\n";
    }
}

close( $input );

有人能指出这个问题吗?

问题是哈希元素
$kglocus{$reg}
的存在形成了第一个测试,“File1中的字段1和3与File2中的字段2和3匹配”。但您将其视为测试总是通过,并简单地使用它访问
File1
记录的元素

除非$kglocus{$reg}在那里,否则您需要类似于
的next命令才能正常工作。我还希望将该值作为一个单独的变量取出,以避免一次又一次地索引散列

这里有一个对你有用的解决方案

use strict;
use warnings;
use v5.10.1;
use autodie;

my %kglocus;
{
    open my $in_fh, '<', 'File1';
    while ( <$in_fh> ) {
        next if /chr/;

        my ( $chr, $id, $bp, $a1, $a2 ) = split;
        my $key = "${chr}_$bp";
        $kglocus{$key} = [ $chr, $id, $bp, $a1, $a2 ];
    }
}

{
    my ( $filename ) = @ARGV;
    open my $in_fh, '<', $filename;
    while ( <$in_fh> ) {
        next if /SNP/;

        my ( $snp, $chr, $bp, $a1, $a2 ) = split;
        my $key = "${chr}_$bp";
        next unless my $item = $kglocus{$key};

        if ( $a1 eq $item->[3] and $a2 eq $item->[4]
                or length $a1 > 1 and length $item->[3] > 1
                or length $a2 > 1 and length $item->[4] > 1 ) {

            print "$item->[1] $snp $bp\n";
        }
    }
}
使用严格;
使用警告;
使用v5.10.1;
使用自动模具;
我的%kglocus;
{

打开我的$in_fh,“问题是哈希元素
$kglocus{$reg}
的存在形成了第一个测试,即“File1中的字段1和3与File2中的字段2和3匹配”。但您将其视为该测试始终通过,并简单地使用它访问
File1
记录的元素

除非$kglocus{$reg}
在那里,否则您需要类似于
的下一步,以使其正确工作。我还希望看到该值作为一个单独的变量拉出,以避免反复索引哈希

这里有一个对你有用的解决方案

use strict;
use warnings;
use v5.10.1;
use autodie;

my %kglocus;
{
    open my $in_fh, '<', 'File1';
    while ( <$in_fh> ) {
        next if /chr/;

        my ( $chr, $id, $bp, $a1, $a2 ) = split;
        my $key = "${chr}_$bp";
        $kglocus{$key} = [ $chr, $id, $bp, $a1, $a2 ];
    }
}

{
    my ( $filename ) = @ARGV;
    open my $in_fh, '<', $filename;
    while ( <$in_fh> ) {
        next if /SNP/;

        my ( $snp, $chr, $bp, $a1, $a2 ) = split;
        my $key = "${chr}_$bp";
        next unless my $item = $kglocus{$key};

        if ( $a1 eq $item->[3] and $a2 eq $item->[4]
                or length $a1 > 1 and length $item->[3] > 1
                or length $a2 > 1 and length $item->[4] > 1 ) {

            print "$item->[1] $snp $bp\n";
        }
    }
}
使用严格;
使用警告;
使用v5.10.1;
使用自动模具;
我的%kglocus;
{

打开我的$in_fh,'你提到的“错误”实际上是警告!尽管有这些警告,你仍然得到预期的输出吗?你也用
Data::Dumper
检查了
$kglocus{$reg}
?如果
$reg
不是
%kglocus
中的一个键怎么办?例如,你检查
如果($A1 eq$kglocus{$reg}->[3]
,但您没有检查
$reg
是否实际上是有效的键您提到的“错误”实际上是警告!尽管有这些警告,您仍然得到预期的输出吗?您也检查过
$kglocus{$reg}
使用
数据::转储程序
?如果
$reg
不是
%kglocate
中的键怎么办?例如,您检查
如果($A1 eq$kglocate{$reg}->[3]
,但不检查
$reg
是否实际上是有效键