2018-05-08

#!/usr/bin/perl-w

use strict;

my %barcode_clinical;

open IN,"<$ARGV[0]" or die "can't open the file: $ARGV[0]\n";# Clinical File

while(){

     chomp; 

     my @array=split/\t/; 

     my $barcode=join "\-",$array[0],"01"; 

     next if $array[1]!~/^Stage/; 

     $array[1]=~s/A$//g; 

     $array[1]=~s/B$//g; 

     $array[1]=~s/C$//g; 

     my $clinical=$array[1];                      

$barcode_clinical{$barcode}=$clinical;

}

close IN;

##############################################################################################################

my %hash;

my @position;

my (@I_position,@II_position,@III_position,@IV_position);

my @barcode;

open IN,"<$ARGV[1]" or die "can't open this file:$ARGV[1]\n";#Merge Methylation File

while(){

        next if $_!~/^Key/;

        chomp;

        my @array=split/\t/,$_;

        shift @array;

        @barcode=@array;

        foreach my $i(@array){

                my @bar=split/\_/,$i;

#              print "$bar[0]\n";

                $barcode_clinical{$i} ="HC" if $bar[0]=~/\-11$/;

                if($barcode_clinical{$bar[0]}){

                        $hash{$i}=$barcode_clinical{$bar[0]};

                }else{

                        $hash{$i}="Tumor";

                }

        }

#      map{print  "$_\t$hash{$_}\n";}@array;

        my $hc=grep{$hash{$_} eq "HC";}@array;

        my $i=grep{$hash{$_} eq "Stage I";}@array;

        my $ii=grep{$hash{$_} eq "Stage II";}@array;

        my $iii=grep{$hash{$_} eq "Stage III";}@array;

        my $iv=grep{$hash{$_} eq "Stage IV";}@array;

        @position=sort{$hash{$array[$a]} cmp $hash{$array[$b]}} 0 .. $#array;

        @I_position=@position[0 .. ($hc+$i-1)];

@II_position=@position[0 .. ($hc-1),($hc+$i) .. ($hc+$i+$ii-1)];

        @III_position=@position[0 .. ($hc-1),($hc+$i+$ii) ..($hc+$i+$ii+$iii-1)];

        @IV_position=@position[0 .. ($hc-1),($hc+$i+$ii+$iii) .. ($i+$ii+$iii+$iv+$hc-1)];

}

print "@III_position\n";

open OUT1,">sample_file_1.txt";

open OUT2,">sample_file_2.txt";

open OUT3,">sample_file_3.txt";

open OUT4,">sample_file_4.txt";

for my$i (@I_position){

        print OUT1 "$barcode[$i]\t$hash{$barcode[$i]}\n";

}

for my $i(@II_position){

        print OUT2 "$barcode[$i]\t$hash{$barcode[$i]}\n";

}

for my $i(@III_position){

        print OUT3 "$barcode[$i]\t$hash{$barcode[$i]}\n";

}

for my $i(@IV_position){

        print OUT4 "$barcode[$i]\t$hash{$barcode[$i]}\n";

}

close OUT1;

close OUT2;

close OUT3;

close OUT4;

open OUT1,">methy_stage1.txt";

open OUT2,">methy_stage2.txt";

open OUT3,">methy_stage3.txt";

open OUT4,">methy_stage4.txt";close IN;open IN,"<$ARGV[1]";while(){

        chomp;

        my @array=split/\t/;

        my $SiteID=shift @array;

#      print "$SiteID\n";

        if($SiteID=~/^Key/){

                $SiteID = "SiteID";

        }

        print OUT1 "$SiteID";

        map{print OUT1 "\t$array[$_]";}@I_position;

        print OUT1 "\n";

        print OUT2 "$SiteID";

        map{print OUT2 "\t$array[$_]";}@II_position;

        print OUT2 "\n";

        print OUT3 "$SiteID";

        map{print OUT3 "\t$array[$_]";}@III_position;

        print  OUT3 "\n";

        print OUT4 "$SiteID";

        map{print OUT4 "\t$array[$_]";}@IV_position;

        print OUT4 "\n";

}

close IN;

close OUT1;

close OUT2;

close OUT3;

close OUT4;

你可能感兴趣的:(2018-05-08)