在这种情况下,如何使用if和else进行多重确定?如果,否则不能很好地工作

时间:2015-11-18 21:22:35

标签: perl

我们需要制作一个用Perl分析DNA序列的程序。我的最后一项任务是:分析其他人的结果文件。

这是一列从最小到最大的数字。 The result file

我从STDIN得到三个参数。它们是$from$to$intervals。例如

1000 5000 200

在这种情况下,范围1000 .. 5000被分为20个区间。每个bin的大小为200.程序应扫描数据文件并查找该数字是否在当前bin中。如果它在这个箱子里,那么计算它并转到下一行,直到该数字超出这个箱子;然后去下一个垃圾箱。如果它不在当前的bin中,那么转到下一个bin并且不做任何事情。

最终结果应该看起来像this

这是关于这部分的我的脚本。我的问题在剧本中。

while ( defined( my $m_z_value = <$ff> ) ) {

    $m_z_value =~ s/^\s+//;           # /

  GOD:
    chomp $m_z_value;

    if ( $tmp_to <= $to and $m_z_value <= $to ) {    # limit the bin in the range

        if ( $m_z_value >> $tmp_to ) {               # Here if I don't use double ">", the program will just ignore it. Even I use double ">",sometime it still don't work, I don't know why?
            ++$bin_number;
            print "$bin_number\t\t $tmp_from\t $tmp_to\t 0\t\t -\n";
            $tmp_from = $tmp_from + $intervals;
            $tmp_to   = $tmp_to + $intervals;
            goto GOD;                                # if the value is in the main range but out of the bin, move to the next bin and test it again
        }
        else {
            if ( $m_z_value < $from ) {
                goto MIRACLE;                        # if the value is before the range, go to next line
            }
            else {
                if ( $m_z_value == $from ) {         # similar problem, if I don't use double "=", in this part, the program will define the value of $m_z_value the same as $from
                    $mass       = $mass + $m_z_value;
                    $whole_mass = $whole_mass + $m_z_value;
                    ++$bin_pepnumber;
                    ++$whole_pepnumber;

                    print "$bin_number\t\t $tmp_from\t $tmp_to\t $bin_pepnumber\t\t $mass/$bin_pepnumber\n";
                }
                else {
                    if ( $m_z_value >= $tmp_from and $m_z_value <= $tmp_to ) {
                        $mass       = $mass + $m_z_value;
                        $whole_mass = $whole_mass + $m_z_value;
                        ++$bin_pepnumber;
                        ++$whole_pepnumber;
                    }
                    else {
                        if ( $m_z_value > $tmp_to ) {
                            print "$bin_number\t\t $tmp_from\t $tmp_to\t $bin_pepnumber\t\t $mass/$bin_pepnumber\n";
                            ++$bin_number;
                            $mass          = $m_z_value;
                            $whole_mass    = $whole_mass + $m_z_value;
                            $bin_pepnumber = 0;
                            ++$whole_pepnumber;
                            $tmp_from = $tmp_from + $intervals;
                            $tmp_to   = $tmp_to + $intervals;
                            goto GOD;    #if the m/z value is bigger than the range, go to next bin and test it again
                        }
                    }
                }
            }
        }
      MIRACLE:
    }
}

1 个答案:

答案 0 :(得分:0)

首先,bin编号很容易直接计算:

$bin_number = int (($m_z_value - $from) / $intervals);

接下来,perl有用于表示像bin这样的东西的数组:

++$bin_pep_number[$bin_number];
$mass[$bin_number] += $m_z_value;

全部放在一起:

my @bin_pep_number;
my @mass;
my $bin_number;
while(defined(my $m_z_value = <$ff>)) {
    $m_z_value =~ s/^\s+//;
    chomp $m_z_value;

    $bin_number = int (($m_z_value - $from) / $intervals);
    ++$bin_pep_number[$bin_number];
    $mass[$bin_number] += $m_z_value;
    }

$bin_number = 0;
for (my $tmp_from=$from; $tmp_from<$to; $tmp_from+=$intervals) {
    my $tmp_to = $tmp_from + $intervals;
    print "$bin_number\t\t";
    print "$tmp_from\t";
    print "$tmp_to\t";
    print "$bin_pepnumber[$bin_number]\t\t";
    print "$mass[$bin_number]/$bin_pepnumber[$bin_number]" if $bin_pep_number[$bin_number];
    print "\n";
    ++$bin_number;
    }