File Coverage

blib/lib/List/Vectorize/lib/IO.pl
Criterion Covered Total %
statement 101 108 93.5
branch 30 36 83.3
condition 19 21 90.4
subroutine 8 8 100.0
pod 4 4 100.0
total 162 177 91.5


line stmt bran cond sub pod time code
1            
2             # ============================= IO subroutine ==============================================
3             # usage: print_ref( [TYPEGLOB], [SCALAR] )
4             # description: print the data structure of a reference
5             sub print_ref {
6            
7 11     11 1 58 check_prototype(@_, '*?($|\$|\@|\%|\&)+');
8            
9 11         43 local $handle = *STDOUT;
10 11 100       43 if(is_glob_ref(\$_[0])) {
11 4         12 $handle = shift(@_);
12             }
13 11         23 my $ref = shift;
14            
15 11 100       54 if(is_array_ref($ref)) {
    100          
    100          
    50          
    50          
16 3         470 print $handle "Reference of ARRAY.\n";
17 3         19 for (0..$#$ref) {
18 16         1096 print $handle "[$_] $ref->[$_]\n";
19             }
20 3         46 print $handle "\n";
21             } elsif(is_hash_ref($ref)) {
22 3         139 print $handle "Reference of HASH.\n";
23 3         16 foreach (keys %$ref) {
24 6         284 print $handle "$_\t$ref->{$_}\n";
25             }
26 3         51 print $handle "\n";
27             } elsif(is_scalar_ref($ref)) {
28 3         134 print $handle "Reference of SCALAR.\n";
29 3         68 print $handle $$ref;
30 3         117 print $handle "\n";
31             } elsif(is_ref_ref($ref)) {
32 0         0 print $handle "Reference of REF.\n";
33 0         0 print $handle $$ref;
34 0         0 print $handle "\n";
35             } elsif(is_code_ref($ref)) {
36 2         7 print $handle "Reference of CODE.\n";
37             } else {
38 0         0 print $handle "@_\n";
39             }
40 11         63 return $ref;
41             }
42            
43             # usage: print_matrix( [TYPEGLOB], [SCALAR] )
44             # description: print the matrix
45             sub print_matrix {
46            
47 3     3 1 27 check_prototype(@_, '*?\@');
48            
49 3         12 local $handle = *STDOUT;
50 3 100       17 if(is_glob_ref(\$_[0])) {
51 1         5 $handle = shift(@_);
52             }
53 3         9 my $mat = $_[0];
54 3         6 my $sep = "\t";
55            
56 3         19 my ($nrow, $ncol) = dim($mat);
57 3         383 print "$nrow x $ncol matrix:\n\n";
58            
59 3         17 for(my $i = 0; $i < len($mat); $i ++) {
60 6         17 print $handle join $sep, @{$mat->[$i]};
  6         102  
61 6         222 print $handle "\n";
62             }
63 3         63 print "\n";
64             }
65            
66             # usage: read_table( [SCALAR], %setup )
67             sub read_table {
68            
69 9     9 1 43 check_prototype(@_, '$($|\@){0,}');
70            
71 9         18 my $file = shift;
72            
73 9         32 my %setup = @_;
74 9   100     53 my $quote = $setup{"quote"} || "";
75 9   100     37 my $sep = $setup{"sep"} || "\t";
76 9   100     37 my $whether_rownames = $setup{"row.names"} || 0; # if set true, first item will be key
77 9   100     36 my $whether_colnames = $setup{"col.names"} || 0; # if set true, first item will be key
78            
79 9 50       543 open F, $file or croak "ERROR: cannot open $file.\n";
80 9         16 my $data;
81             my $rownames;
82 0         0 my $colnames;
83 9         16 my $i_line = 0;
84 9         13 my $i_array = 0;
85 9         11 my $flag = 0;
86 9         259 while( my $line = ) {
87 26         33 $i_line ++;
88            
89             # read the column names
90 26 100 100     104 if($flag == 0 and $whether_colnames) {
91 2         5 chomp $line;
92 2         34 $line =~s/^$quote|$quote$//g;
93 2         19 @$colnames = split "$quote$sep$quote", $line;
94 2 100       9 if($whether_rownames) {
95 1         2 shift(@$colnames);
96             }
97 2         4 $flag = 1;
98 2         3 $i_line --;
99 2         11 next;
100             }
101            
102 24         26 $i_array ++;
103            
104 24         31 chomp $line;
105 24         287 $line =~s/^$quote|$quote$//g;
106 24         152 my @tmp = split "$quote$sep$quote", $line;
107            
108             # read rownames
109 24 100       57 if($whether_rownames) {
110 8         16 push(@$rownames, shift(@tmp));
111             }
112            
113 24         23 push(@{$data->[$i_array - 1]}, @tmp);
  24         224  
114            
115             }
116 9         102 close F;
117            
118 9 100       86 wantarray ? ($data, $colnames, $rownames) : $data;
119             }
120            
121             # usage: write_table( [MATRIX], %setup )
122             sub write_table {
123            
124 7     7 1 651 check_prototype(@_, '\@($|\@){2,}');
125            
126 7         14 my $matrix = shift;
127            
128 7         30 my %setup = @_;
129 7   100     35 my $quote = $setup{"quote"} || "";
130 7   100     52 my $sep = $setup{"sep"} || "\t";
131 7         12 my $colnames = $setup{"col.names"}; # column names
132 7         10 my $rownames = $setup{"row.names"}; # row names
133 7         38 my $file = $setup{"file"};
134            
135 7         28 my ($nrow, $ncol) = dim($matrix);
136 7 50 66     117 if($rownames and $nrow != len($rownames)) {
137 0         0 croak "ERROR: Length of rownames should be equal to the length of rows in matrix\n";
138             }
139 7 50 66     25 if($colnames and $ncol != len($colnames)) {
140 0         0 croak "ERROR: Length of colnames should be equal to the length of columns in matrix\n";
141             }
142            
143 7 50       824 open OUT, ">$file" or croak "ERROR: Cannot create file:$file\n";
144 7 100       20 if($rownames) {
145 3 100       9 if($colnames) {
146             # print colnames
147 1         6 print OUT "$quote$quote$sep";
148 1     3   3 print OUT join $sep, @{sapply($colnames, sub{"$quote$_$quote"})};
  1         6  
  3         7  
149 1         4 print OUT "\n";
150             }
151 3         13 for(my $i = 0; $i < len($matrix); $i ++) {
152 8         56 print OUT "$quote$rownames->[$i]$quote$sep";
153 8     22   9 print OUT join $sep, @{sapply($matrix->[$i], sub{"$quote$_$quote"})};
  8         39  
  22         61  
154 8         42 print OUT "\n";
155             }
156             }
157             else {
158 4 100       9 if($colnames) {
159 1     3   3 print OUT join $sep, @{sapply($colnames, sub{"$quote$_$quote"})};
  1         8  
  3         8  
160 1         5 print OUT "\n";
161             }
162 4         14 for(my $i = 0; $i < len($matrix); $i ++) {
163 12     36   14 print OUT join $sep, @{sapply($matrix->[$i], sub{"$quote$_$quote"})};
  12         48  
  36         86  
164 12         58 print OUT "\n";
165             }
166             }
167 7         420 close OUT;
168             }
169            
170            
171            
172             1;