1

我有一个这样的文件:

fixedStep chrom=chr22 start=14430001 step=1
144300010.000
0.002
0.030
0.990
0.000
0.000
0.000
fixedStep chrom=chr22 start=14430001 step=1
0.000
0.000
0.000

我想从以 fixedStep 开头的每一行中获取开始的编号,并在其他每一行中放置一个计数器。例如。

fixedStep chrom=chr22 start=14430001 step=1
14430001   0.000
14430002   0.002
14430003   0.030
fixedStep chrom=chr22 start=16730005 step=1
16730005   0.990
16730006   0.000
.........

我写了这段代码,但不起作用。

open my $scores_info, $scores_file or die "Could not open $scores_file: $!";
while( my $sline = <$scores_info>)  {

    if ($sline=~ m/fixedStep/) {
        my @data = split(' ', $sline);
        my @begin = split('=', $data[2]);
        my $start = $begin[1];
        print "$start\n";
        my $nextline = <$scores_info>;
        $start++;
        print $start . "\t" .  $nextline;
    }

最后将其打印在新文件中。请问你能帮帮我吗??先感谢您

4

1 回答 1

3

如果遇到带有“fixedStep”的行,设置$count并打印该行。否则,打印并递增$count

my $count = 0;
while( my $sline = <$scores_info>)  {

    if ($sline=~ m/fixedStep/) {
        my @data = split(' ', $sline);
        my @begin = split('=', $data[2]);
        $count = $begin[1];
        print $sline;
    } else {
        print $count++ . "\t" . $sline;
    }
}

输出:

fixedStep chrom=chr22 start=14430001 step=1
14430001    144300010.000
14430002    0.002
14430003    0.030
14430004    0.990
14430005    0.000
14430006    0.000
14430007    0.000
fixedStep chrom=chr22 start=16730005 step=1
16730005    0.990
16730006    0.000
16730007    0.000
于 2013-08-23T15:23:43.097 回答