使用Perl在数组中循环以比较两个值

使用Perl在数组中循环以比较两个值,perl,if-statement,foreach,Perl,If Statement,Foreach,我有一个大数据集(29列乘以19000行),我希望能够比较每行的值并打印描述性输出 具体地说,我想查询列A(@WTcall)中的值,该列实际上是一个pass/fail语句。如果数据失败,我想打印“fail语句”并转到下一行,但如果数据通过,我想继续描述数据 下一个问题是确定X列(@正)和Y列(@负)中的数据属于哪种分类: (例如: 如果X列和Y列>=0.6,则打印“ABC” 如果X列和Y列=0.6,但列Y=0.6,Y>=0.6,然后是“ABC” X

我有一个大数据集(29列乘以19000行),我希望能够比较每行的值并打印描述性输出

具体地说,我想查询列A(@WTcall)中的值,该列实际上是一个pass/fail语句。如果数据失败,我想打印“fail语句”并转到下一行,但如果数据通过,我想继续描述数据

下一个问题是确定X列(@正)和Y列(@负)中的数据属于哪种分类:

(例如:

如果X列和Y列>=0.6,则打印“ABC”

如果X列和Y列<0.6,则打印“CBA”

如果列X>=0.6,但列Y<0.6,则打印“DEF”

如果列X<0.6但列Y>=0.6,则打印“FED”

否则打印“缺少数据”。)

我已经包括了我在下面编写的代码以及示例数据的子集

我在发布之前运行的测试在代码中被注释掉了。简而言之,如果我注释掉“if和elsif语句”列表,请打印“@WTcall”\t@positive\t@negative\n“并通过head命令进行传递-我的变量似乎正在提取正确的信息

问题出现在实际比较中,因为每一行都使用“甲基化\t甲基化\n”描述进行分类。 我不清楚为什么会这样。我知道我有@WTcall列应该匹配$BadPosition(通过/失败检查)的条目。此外,如果我再次注释掉“if语句”,请打印“@WTcall\n$BadPosition”并通过sort和uniq传递它-我只得到一个“No_WT_conconsensus”的值,因此应该不会出现打字错误或匹配这些值的问题

我确信这个问题是显而易见的,并且就在我面前,所以我真的非常感谢任何帮助

多谢各位

代码:


@
符号开头的变量是数组。在比较数组时,您将对其施加数值标量上下文,因此它返回其大小

单值不需要数组,只需使用标量即可

不要将特殊变量
$1
用作循环变量。它让人困惑,取消了它的特殊行为

下面是我如何重写你的程序。它仍然抱怨将“空白”与数字进行比较,但我不确定您想对这些值做什么

#!/usr/bin/perl
use strict;
use warnings;
die "Usage: $0 Filename\n" if @ARGV != 1;

my $file = shift;
my $BadPosition = 'No_WT_Concensus';

open my $infh, '<', $file or die "$file: $!";

while (<$infh>) {
    chomp;
    my @columns = split /\t/;
    my ($wt_call, $positive, $negative) = @columns[0, 14, 29];

    if ($wt_call eq $BadPosition) {
        print "No_WT_Concensus\tNo_WT_Concensus\n";
    } elsif ($positive >= 0.6 && $negative >= 0.6) {
        print "Methylated\tMethylated\n";
    } elsif ($positive <= 0.6 && $negative <= 0.6) {
        print "Under-methylated\tUnder-methylated\n";
    } elsif ($positive >= 0.6 && $negative <=0.6) {
        print "Hemimethylated (m6A)\tHemimethylated (A)\n";
    } elsif ($positive <= 0.6 && $negative >= 0.6) {
        print "Hemimethylated (A)\tHemimethylated (m6A)\n";
    } else {
        print "Missing_Site\tMissing_Site\n";
    }
}
#/usr/bin/perl
严格使用;
使用警告;
如果@ARGV!=1.
我的$file=shift;
我的$BadPosition='不同意';

打开我的$infh,“以下是您的需求,经过编辑以显示并行结构:

  • X>=0.6,Y>=0.6,然后是“ABC”

  • X<0.6,Y<0.6,然后是“CBA”

  • X>=0.6,但Y<0.6,则为“DEF”

  • X<0.6但Y>=0.6,然后“馈电”


一些要求是
<0.6
,但在代码中,您有
不要使用像
$1
这样的特殊变量作为循环变量。请注意,代码中存在一个小问题,
$positive
$negative
都是0.6,这两个变量匹配所有分支,所以排序很重要。@briandfoy我使用了与原始代码相同的顺序。此外,这也是为什么我没有将其转换为基于
$positive 0.6
$negative 0.6
的2D调度表的原因。感谢您帮助我尝试分析带有代码的问题。在示例数据上运行此脚本会引发很多警告&仍然无法正确解析数据。这是输出:
在CallMethylation.New.pl第18行第1行的数字ge(>=)中使用未初始化值$positive。在数值lt中使用未初始化值$positive(当我对示例数据运行脚本时,第一行返回
Methylated Methylated
。是否确实复制了“粘贴”我的代码而没有输入错误?此外,我收到以下警告:
参数“blank”在数值ge中不是数值(>=)在./1.pl第18行
…第20行
。如果您检查数据,列中确实有“空白”,但您没有说明如何处理此值。
Methylated  coding gene 619 thrA    NC_000913.3 pos 3   1   0.9535  1   NC_000913.3 619 +   18  0.8889  Methylated  coding gene 620 thrA    NC_000913.3 neg 3   0.9429  0.9756  0.9714  NC_000913.3 620 -   14  1
No_WT_Concensus coding gene 195410  ispU    NC_000913.3 pos 2   0.5789  0.766   0.6071  NC_000913.3 195410  +   39  0.5897  Methylated  coding gene 195411  ispU    NC_000913.3 neg 3   0.75    0.9074  0.9306  NC_000913.3 195411  -   21  0.8095
Under-methylated    pseudogene  3632965 yhiL    NC_000913.3 pos 0   0.0323  0.1429  0.0962  NC_000913.3 3632965 +   22  0.0909  Under-methylated    pseudogene  3632966 yhiL    NC_000913.3 neg 0   0.1463  0.175   0.1429  NC_000913.3 3632966 -   23  0
Methylated  intergenic  164636  hrpB-mrcB   NC_000913.3 pos 3   0.7381  0.7647  0.7273  NC_000913.3 164636  +   25  0.8 Methylated  intergenic  164637  hrpB-mrcB   NC_000913.3 neg 3   0.7 0.7931  0.7213  NC_000913.3 164637  -   25  0.4
Methylated  intergenic  269287  ykfA-perR   NC_000913.3 pos 3   0.875   0.8833  0.931   NC_000913.3 269287  +   22  0.8182  Methylated  intergenic  269288  ykfA-perR   NC_000913.3 neg 3   0.8077  0.6866  0.6491  NC_000913.3 269288  -   17  0.5294
Methylated  coding gene 4397856 mutL    NC_000913.3 pos 3   0.9245  0.9831  0.9661  blank   blank   blank   blank   blank   Methylated  coding gene 4397857 mutL    NC_000913.3 neg 3   0.9783  0.9808  0.9683  NC_000913.3 4397857 -   1   0
Methylated  coding gene 4397969 mutL    NC_000913.3 pos 3   0.9643  0.9524  1   blank   blank   blank   blank   blank   Methylated  coding gene 4397970 mutL    NC_000913.3 neg 3   1   1   1   blank   blank   blank   blank   blank
Methylated  coding gene 2761    thrA    NC_000913.3 pos 3   0.9259  0.8654  0.9242  NC_000913.3 2761    +   31  1   Methylated  coding gene 2762    thrA    NC_000913.3 neg 3   0.913   0.9636  0.9767  NC_000913.3 2762    -   29  0.9655
Methylated  coding gene 3073    thrB    NC_000913.3 pos 3   0.9677  0.8983  1   NC_000913.3 3073    +   29  1   Methylated  coding gene 3074    thrB    NC_000913.3 neg 3   1   0.9038  0.9778  NC_000913.3 3074    -   31  1
#!/usr/bin/perl
use strict;
use warnings;
die "Usage: $0 Filename\n" if @ARGV != 1;

my $file = shift;
my $BadPosition = 'No_WT_Concensus';

open my $infh, '<', $file or die "$file: $!";

while (<$infh>) {
    chomp;
    my @columns = split /\t/;
    my ($wt_call, $positive, $negative) = @columns[0, 14, 29];

    if ($wt_call eq $BadPosition) {
        print "No_WT_Concensus\tNo_WT_Concensus\n";
    } elsif ($positive >= 0.6 && $negative >= 0.6) {
        print "Methylated\tMethylated\n";
    } elsif ($positive <= 0.6 && $negative <= 0.6) {
        print "Under-methylated\tUnder-methylated\n";
    } elsif ($positive >= 0.6 && $negative <=0.6) {
        print "Hemimethylated (m6A)\tHemimethylated (A)\n";
    } elsif ($positive <= 0.6 && $negative >= 0.6) {
        print "Hemimethylated (A)\tHemimethylated (m6A)\n";
    } else {
        print "Missing_Site\tMissing_Site\n";
    }
}
if X >= 0.6
    if Y >= 0.6
        "ABC"
    else
        "DEF"
else
    if Y >= 0.6
        "FED"
    else
        "CBA"
my @x_y = (
    [ 0.1, 0.7 ],
    [ 0.1, 0.1 ],
    [ 0.7, 0.1 ],
    [ 0.7, 0.7 ]
    );

foreach my $x_y ( @x_y ) {
    printf "X: %.1f Y: %.1f --> %s\n", @$x_y, get_value( @$x_y );
    }

sub get_value {
    my( $x, $y ) = @_;

    if( $x >= 0.6 ) { $y >= 0.6 ? 'ABC' : 'DEF' }
    else            { $y >= 0.6 ? 'FED' : 'CBA' }
    }
sub get_value {
    my( $x, $y, $pivot ) = @_;
    $pivot //= 0.6;  # default value

    if( $x >= $pivot ) { $y >= $pivot ? 'ABC' : 'DEF' }
    else               { $y >= $pivot ? 'FED' : 'CBA' }
    }
use v5.22;
use feature qw(signatures);
no warnings qw(experimental::signatures);

sub get_value ( $x, $y, $pivot = 0.6 ){
    if( $x >= $pivot ) { $y >= $pivot ? 'ABC' : 'DEF' }
    else               { $y >= $pivot ? 'FED' : 'CBA' }
    }
use v5.22;
use feature qw(signatures);
no warnings qw(experimental::signatures);

sub get_value ( $x, $y, $pivot = 0.6 ){
    my $boolean = ($y >= $pivot);
    if( $x >= $pivot ) { $boolean ? 'ABC' : 'DEF' }
    else               { $boolean ? 'FED' : 'CBA' }
    }
use v5.22;
use feature qw(signatures);
no warnings qw(experimental::signatures);

sub get_value ( $x, $y, $pivot = 0.6 ){
    my $y_boolean = ($y >= $pivot);
    my $x_boolean = ($x >= $pivot);

    if( $x_boolean ) { $y_boolean ? 'ABC' : 'DEF' }
    else             { $y_boolean ? 'FED' : 'CBA' }
    }
use v5.28;
use feature qw(signatures);
no warnings qw(experimental::signatures);

sub get_value ( $x, $y, $pivot = 0.6 ) {
    state @table = (
        [ qw(CBA FED) ],
        [ qw(DEF ABC) ]
        );

    my $y_boolean = ($y >= $pivot);
    my $x_boolean = ($x >= $pivot);

    $table[$x_boolean][$y_boolean];
    }
use v5.28;
use feature qw(signatures);
no warnings qw(experimental::signatures);

sub get_value ( $x, $y, $pivot = 0.6 ){
    state @table = (
        [ qw(CBA FED) ],
        [ qw(DEF ABC) ]
        );
    $table[$x >= $pivot][$y >= $pivot];
    }
use v5.28;
use feature qw(signatures);
no warnings qw(experimental::signatures);

my @x_y = (
    [ 0.1, 0.7 ],
    [ 0.1, 0.1 ],
    [ 0.7, 0.1 ],
    [ 0.7, 0.7 ]
    );

my $pivot = 0.6;
my @table = ( [ qw(CBA FED) ], [ qw(DEF ABC) ] );

foreach my $x_y ( @x_y ) {
    printf "X: %.1f Y: %.1f --> %s\n", @$x_y,
        get_value( @$x_y, $pivot, \@table );
    }


sub get_value ( $x, $y, $pivot = 0.6,
    @table = ([ qw(DEF FED) ], [ qw(ABC CBA) ]) )
    {
    $table[$x >= $pivot][$y >= $pivot];
    }
while (<$infh>) {
    chomp;
    my @columns = split /\t/;
    my ($wt_call, $positive, $negative) = @columns[0, 14, 29];

    if ($wt_call eq $BadPosition) {
        print "No_WT_Concensus\tNo_WT_Concensus\n";
    } elsif ($positive >= 0.6 && $negative >= 0.6) {
        print "Methylated\tMethylated\n";
    } elsif ($positive <= 0.6 && $negative <= 0.6) {
        print "Under-methylated\tUnder-methylated\n";
    } elsif ($positive >= 0.6 && $negative <=0.6) {
        print "Hemimethylated (m6A)\tHemimethylated (A)\n";
    } elsif ($positive <= 0.6 && $negative >= 0.6) {
        print "Hemimethylated (A)\tHemimethylated (m6A)\n";
    } else {
        print "Missing_Site\tMissing_Site\n";
    }
}
use v5.28;
use feature qw(signatures);
no warnings qw(experimental::signatures);

while( <$infh> ) {
    chomp;
    my( $wt_call, $positive, $negative ) = (split /\t/)[0,14,29];
    if( $wt_call eq ... ) {
        print "No_WT_Concensus\tNo_WT_Concensus\n";
        next;
        }
    say get_value( $positive, $negative );
    }


sub get_value ( $x, $y, $pivot = 0.6 ){
    state @table = (
        [ qw(CBA FED) ],
        [ qw(DEF ABC) ]
        );
    $table[$x >= $pivot][$y >= $pivot];
    }
while( <$infh> ) {
    chomp;
    my( $wt_call, $positive, $negative ) = (split /\t/)[0,14,29];
    if( $wt_call eq ... ) {
        print "No_WT_Concensus\tNo_WT_Concensus\n";
        next;
        }
    # what if this is 1?
    unless( 2 == grep { length } ($positive, $negative) ) {
        print "No_WT_Concensus\tNo_WT_Concensus\n";
        next;
        }
    say get_value( $positive, $negative );
    }