Perl 二维数组排序和过滤(a practise-sort and filter array according to clinical information)

#!/usr/bin/perl -w
use strict;
use warnings;

my $usage=<<USAGE;
    perl $0 cibersort_filter_file stage_file output_file
    cibersort_filter_file 行为样品名称,列为细胞类型
    stage_file 第一列为样品名称,第三列为分期类型
    output_file 是根据分期类型和样品是否存在临床信息,排序许的文件
    例如: sample_id   age stage   
USAGE
if(@ARGV==0){die $usage}

my $file1=$ARGV[0];
my $file2=$ARGV[1];
my $file3=$ARGV[2];
my %hash;

open(RF,$file2) || die $!;
while(my $line=<RF>){
    next if ($.==1);
    chomp $line;
    my @arr=split(/\t/,$line);
    $hash{$arr[0]}=$arr[2];
}
close(RF);

my @new_arr;
my $hang=0;
my $head;

open(RF,$file1) || die $!;
while(my $line=<RF>){
    if($.==1){
        $head=$line;
        next;
    }
    chomp $line;
    my @arr=split(/\t/,$line);
    my @temp=split(/-/,$arr[0]);
    if ((exists $hash{$arr[0]})&($temp[3]=~/^0/)){
        unshift (@arr,$hash{$arr[0]});
    }elsif((!(exists $hash{$arr[0]}))&($temp[3]=~/^1/)){
        unshift (@arr,"normal");
    }else{
        next;
    }
    for my $i (0..$#arr){
        $new_arr[$hang][$i]=$arr[$i];
    }
    $hang=$hang+1;
}
close(RF);

my @list=sort{$a -> [0] cmp $b -> [0]} @new_arr;

my %clinical;

open(WF,">".$file3.".txt") || die $!;
print WF $head;
for my $i (0..$#list){
    for my $j (0..$#{$list[$i]}){
        if ($j==0){
            $clinical{$list[$i][$j]}++;
        }else{
            print WF $list[$i][$j]."\t"
        }
    }
    print WF "\n";
}
close(WF);

for my $key (keys %clinical){
    print $key."\t".$clinical{$key}."\n";
}
此条目发表在Perl分类目录。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Protected with IP Blacklist CloudIP Blacklist Cloud