comparison tools/stats/generate_matrix_for_pca_lda.pl @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9071e359b9a3
1 #!/usr/bin/perl -w
2
3 use strict;
4 use warnings;
5
6 my $Input_Matrix = $ARGV[0];
7 my $Input_Label = $ARGV[1];
8
9 my %Hash_X = ();
10 my %Hash_Y = ();
11 my $My_Num_X = 0;
12 my $My_Num_Y = 0;
13
14 open (OUT, "> $ARGV[2]");
15
16 open (LABEL, "< $Input_Label") ||
17 die "Sorry, I couldn't open the escape.txt for clone: $!\n";
18
19 my $Label_Index = 0;
20 my $X_Label;
21 my $input_Label;
22 while (defined($input_Label = <LABEL>)){
23 chomp($input_Label);
24 my @cArray_Label = $input_Label =~ /(\S+)\s*/g;
25 if ($input_Label =~ /\w/){
26 if ($Label_Index == 0){
27 $Hash_X{$cArray_Label[0]} = $cArray_Label[1];
28 $X_Label = $cArray_Label[1];
29 $Label_Index = 1;
30 }else{
31 if ($cArray_Label[1] eq $X_Label){
32 $Hash_X{$cArray_Label[0]} = $cArray_Label[1];
33 }else{
34 $Hash_Y{$cArray_Label[0]} = $cArray_Label[1];
35 }
36 }
37 }
38 }
39 close(LABEL);
40
41 open (MATRIX, "< $Input_Matrix") ||
42 die "Sorry, I couldn't open the escape.txt for clone: $!\n";
43
44 my %Hash_Matrix = ();
45 my %Hash_Features = ();
46 my @cArray_Features = ();
47
48 my %Hash_Sum = ();
49 my $Matrix_Index = 0;
50 my $input_Matrix;
51 while (defined($input_Matrix = <MATRIX>)){
52 chomp($input_Matrix);
53 my @cArray_Matrix = $input_Matrix =~ /(\S+)\s*/g;
54 if ($input_Matrix =~ /\w/){
55 if ($Matrix_Index == 0){
56 @cArray_Features = @cArray_Matrix;
57 my $Temp_Num_Array = scalar(@cArray_Matrix);
58 my $Temp_Index = 0;
59 for(;$Temp_Index < $Temp_Num_Array; $Temp_Index++){
60 $Hash_Features{$cArray_Matrix[$Temp_Index]} = "BOL";
61 $Hash_Sum{$cArray_Matrix[$Temp_Index]} = 0;
62 }
63 $Matrix_Index = 1;
64 }else{
65 $Hash_Matrix{$cArray_Matrix[0]} = $input_Matrix;
66 }
67 }
68 }
69 close(MATRIX);
70
71 my $Trace_Key;
72
73 foreach $Trace_Key (sort {$a cmp $b} keys %Hash_X){
74 my @cArray_Trace_X = $Hash_Matrix{$Trace_Key} =~ /(\S+)\s*/g;
75 my $Num_Array_Feature_X = scalar(@cArray_Features);
76 my $Index_Feature_X = 0;
77 for(;$Index_Feature_X < $Num_Array_Feature_X; $Index_Feature_X++){
78 if ($Hash_Features{$cArray_Features[$Index_Feature_X]} eq "BOL"){
79 $Hash_Features{$cArray_Features[$Index_Feature_X]} = $cArray_Trace_X[$Index_Feature_X + 1];
80 }else{
81 $Hash_Features{$cArray_Features[$Index_Feature_X]} = $Hash_Features{$cArray_Features[$Index_Feature_X]} . "\t" . $cArray_Trace_X[$Index_Feature_X + 1];
82 }
83
84 $Hash_Sum{$cArray_Features[$Index_Feature_X]} += $cArray_Trace_X[$Index_Feature_X + 1];
85 }
86 $My_Num_X ++;
87 }
88
89 my $Append_Key;
90 foreach $Append_Key (keys %Hash_Features){
91 $Hash_Features{$Append_Key} = $Hash_Features{$Append_Key} . "\t" . $Hash_Sum{$Append_Key};
92 $Hash_Sum{$Append_Key} = 0;
93 }
94
95 foreach $Trace_Key (sort {$a cmp $b} keys %Hash_Y){
96 my @cArray_Trace_Y = $Hash_Matrix{$Trace_Key} =~ /(\S+)\s*/g;
97 my $Num_Array_Feature_Y = scalar(@cArray_Features);
98 my $Index_Feature_Y = 0;
99 for(;$Index_Feature_Y < $Num_Array_Feature_Y; $Index_Feature_Y++){
100 if ($Hash_Features{$cArray_Features[$Index_Feature_Y]} eq "BOL"){
101 $Hash_Features{$cArray_Features[$Index_Feature_Y]} = $cArray_Trace_Y[$Index_Feature_Y + 1];
102 }else{
103 $Hash_Features{$cArray_Features[$Index_Feature_Y]} = $Hash_Features{$cArray_Features[$Index_Feature_Y]} . "\t" . $cArray_Trace_Y[$Index_Feature_Y + 1];
104 }
105
106 $Hash_Sum{$cArray_Features[$Index_Feature_Y]} += $cArray_Trace_Y[$Index_Feature_Y + 1];
107 }
108 $My_Num_Y ++;
109 }
110
111 foreach $Append_Key (keys %Hash_Features){
112 $Hash_Features{$Append_Key} = $Hash_Features{$Append_Key} . "\t" . $Hash_Sum{$Append_Key} . "\t" . "EOL";
113 }
114
115 my $Prt_Key;
116 print OUT " \t";
117 foreach $Prt_Key (sort {$a cmp $b} keys %Hash_X){
118 print OUT "$Prt_Key \t";
119 }
120 print OUT "X(SUM) \t";
121
122 foreach $Prt_Key (sort {$a cmp $b} keys %Hash_Y){
123 print OUT "$Prt_Key \t";
124 }
125 print OUT "Y(SUM) \t";
126 print OUT "\n";
127
128 my $Prt_Index = 0;
129 my $Prt_Array_Num = scalar (@cArray_Features);
130 for(;$Prt_Index < $Prt_Array_Num; $Prt_Index++){
131 print OUT "$cArray_Features[$Prt_Index] \t$Hash_Features{$cArray_Features[$Prt_Index]}\n";
132 }
133
134 print OUT " \t";
135 my $My_Label_Index = 0;
136 for(;$My_Label_Index < $My_Num_X; $My_Label_Index++){
137 print OUT "X \t";
138 }
139 print OUT " \t";
140
141 $My_Label_Index = 0;
142 for(;$My_Label_Index < $My_Num_Y; $My_Label_Index++){
143 print OUT "Y \t";
144 }
145 print OUT " \t\n";
146
147 close(OUT);