首页 文章

如果在某些条件下以2或22开头,则Perl哈希不会打印值

提问于
浏览
1

这真让我很沮丧 . 我正在编写的脚本是在哈希中索引坐标,然后使用这些索引号从数组中提取值 .

奇怪的是,如果值以2或22开头,则不会打印 . 任何其他号码都有效 . 我将向您展示脚本的两个变体和输出 .

第一个变化 . 这就是我希望脚本执行的操作 . 打印染色体,位置,值 .

#!/usr/bin/perl

use strict;
use warnings;
use File::Find;
use Scalar::Util qw(looks_like_number);

open IN, "/home/big/scratch/affy_map.txt" or die "Cannot open reference\n";
my %ref;
my $head = <IN>;
my $index = 0;
while(<IN>){
    chomp $_;
    my @row = split /\t/, $_;
    my $value = join "\t", $row[1],$row[2];

    if($row[1] == 2 && $row[2] <= 50000 && $row[2] <= 51113178) { $ref{$index}=$value; print $index."\t".$value."\n";}

    if($row[1] == 22 && $row[2] <= 16300001 && $row[2] <= 20500000) { $ref{$index}=$value; print $index."\t".$value."\n"; }

    $index++;
}
close(IN);

my @files;
my $masterDirect = "/nfs/archive02/big/Norm/norm_gcc/";

find(\&file_names, $masterDirect);

sub file_names {

  if( -f && $File::Find::name=~/\.nzd$/)
   {
       push @files, $File::Find::name;
   }
 }

my $count=0;
foreach(@files){
    $count++;
    if($count % 100 == 0 ){ print "\n","-" x 10, " $count ", "-" x 10,"\n";}

    undef my @probes;
    open IN, $_;

    #file name handling
    my @inDir = split "\/", $_;
    my $id = pop(@inDir);
    $id =~ s/\.gcc.nzd$//;

    #header test
    $head =<IN>;
    if(looks_like_number($head)) { push @probes, $head; }

    #open output
    open OUT, ">/home/big/scratch/phase1_affy/".$id."_select_probeset.txt";

    #load probe array
    @probes = <IN>;

    close(IN);

    foreach my $key (sort keys %ref){

        #intended function
        print OUT $ref{$key}."\t".$probes[$key];

        #testing   
        my @temp = split "\t", $ref{$key};
        foreach(@temp){if($temp[0] == 2){print $key."\t".$ref{$key}."\t".$probes[$key];}}
    }
    close(OUT);
}

这是测试的输出 . 参考文件的打印完美无瑕 . 第一个数字是$ key或索引号 . 第二个是$探针[$ key] why is the $ref{$key} missing?

146529  0.777314368326637
146529  0.777314368326637
146530  0.116241153901913
146530  0.116241153901913
146531  0.940593233609167
146531  0.940593233609167

变化2 .

...
foreach my $key (sort keys %ref){
    print OUT $ref{$key}."\t".$probes[$key];
    my @temp = split "\t", $ref{$key};
    foreach(@temp){if($temp[0] == 2){print $key."\t".$ref{$key}."\n";}}
}

而它的输出 . See now it's printing correctly. $key and $ref{$key}

146542  2       31852
146542  2       31852
146543  2       37693
146543  2       37693
146544  2       40415
146544  2       40415
146545  2       40814

我认为这可能是DOS-> UNIX文件问题,但我为脚本看到的所有输入执行了 perl -pi -e 's/\R/\n/g' input_files.txt . 它打印相同的值两次,因为@temp数组中有两个元素 . 我现在真的很茫然 .

1 回答

  • 0

    这是一个可能的问题提示 . 在开始部分,

    if($row[1] == 2 && $row[2] <= 50000 && $row[2] <= 51113178) { $ref{$index}=$value; print $index."\t".$value."\n";}
    

    请注意,您为$ row [2]使用了两个“<=”,这看起来很奇怪 . 下一行也有这样的“问题” . 请先仔细检查一下,否则您可能会先将它们过滤掉 .

相关问题