#!/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";
}